import { VerbrauchsausweisWohnenClient } from "#components/Ausweis/types.js"; import { LEX_OFFICE_API_KEY } from "#lib/constants.js"; import { Aufnahme, BedarfsausweisWohnen, Benutzer, Rechnung, VerbrauchsausweisGewerbe, VerbrauchsausweisWohnen } from "@ibcornelsen/database/client"; import { prisma } from "@ibcornelsen/database/server"; import moment from "moment"; export function lexOfficeAnfrageSenden(ausweis: VerbrauchsausweisWohnen | BedarfsausweisWohnen | VerbrauchsausweisGewerbe, options: { bezeichnung: string, positionstext: string, preis: number, anschreiben: string }, aufnahme: Aufnahme, rechnung: Rechnung, user: Benutzer) {; if (!user.lex_office_id) { const lex_office_id = await createLexOfficeCustomer(user); if (!lex_office_id) { throw new Error("Beim erstellen der Lex Office ID ist etwas schiefgelaufen.") } user.lex_office_id = lex_office_id await prisma.benutzer.update({ where: { id: user.id }, data: { lex_office_id } }) } let response: Response; response = await fetch("https://api.lexoffice.io/v1/quotations?finalize=true", { method: "POST", headers: { Accept: "application/json", Authorization: `Bearer ${LEX_OFFICE_API_KEY}`, "Content-Type": "application/json" }, body: JSON.stringify({ "voucherDate": moment(aufnahme.erstellungsdatum).format("YYYY-MM-DDTHH:mm:ss.SSSZ"), "expirationDate": moment(aufnahme.erstellungsdatum).add(1, "month").format("YYYY-MM-DDTHH:mm:ss.SSSZ"), "taxConditions": { "taxType": "gross" }, "shippingConditions": { "shippingType": "none" }, "address": { "contactId": user.lex_office_id, "name": `${user.vorname} ${user.name}`, "street": rechnung.strasse, "city": rechnung.ort, "zip": rechnung.plz, "countryCode": "DE", "supplement": rechnung.zusatzzeile }, "lineItems": [ { "type": "custom", "name": options.bezeichnung, "description": options.positionstext, "quantity": 1, "lineItemAmount": options.preis, "unitName": options.bezeichnung, "unitPrice": { "currency": "EUR", "grossAmount": options.preis, "taxRatePercentage": 19 } } ], "totalPrice": { "currency": "EUR", "totalGrossAmount": options.preis, }, "introduction": options.anschreiben, "remark": "Wir freuen uns auf Ihre Auftragserteilung und sichern eine einwandfreie Ausführung zu." }) }) const result = await response.json(); if (!result.id) { throw new Error(`Ein Fehler ist aufgetreten: ${result.message}`); } // Rechnung PDF Holen const lex_office_id = result.id; response = await fetch(`https://api.lexoffice.io/v1/quotations/${lex_office_id}/document`, { headers: { Accept: "application/json", Authorization: `Bearer ${LEX_OFFICE_API_KEY}`, "Content-Type": "application/json" }, }) const pdfResult = await response.json() if (!pdfResult.documentFileId) { throw new Error(`Ein Fehler ist aufgetreten: ${pdfResult.message}`); } $pdf_id = $render_response["documentFileId"]; db()->execute("UPDATE Angebotsanfragen SET Adresse_Objekt = :Adresse_Objekt, PLZ_Objekt = :PLZ_Objekt, Ort_Objekt = :Ort_Objekt, Dienstleistung = :Dienstleistung, Empfaengerrechnung = :Empfaengerrechnung, Zusatzzeilerechnung = :Zusatzzeilerechnung, Adresse = :Adresse, PLZ = :PLZ, Ort = :Ort, Anlass = :Anlass, Baujahrheizung = :Baujahrheizung, Baujahrgebaeude = :Baujahrgebaeude, Baujahrklima = :Baujahrklima, Gebaeudetyp = :Gebaeudetyp, Nutzflaeche = :Nutzflaeche, Sanierungsstatus = :Sanierungsstatus, Nachricht = :Nachricht, Datei = :Datei, Rolle = :Rolle, Email = :Email, Angebotspreis = :Angebotspreis, Rabatt = :Rabatt, `Status` = 'Verschickt', lex_office_id = :lex_office_id, Textanschreiben = :Textanschreiben, Positionstext = :Positionstext WHERE Auftragsnummer = :Auftragsnummer", [ "Adresse_Objekt": $_POST["gebaeude_strasse"], "PLZ_Objekt": $_POST["gebaeude_plz"], "Ort_Objekt": $_POST["gebaeude_ort"], "Dienstleistung": $_POST["dienstleistung"], "Empfaengerrechnung": $_POST["rechnung_empfaenger"], "Zusatzzeilerechnung": $_POST["rechnung_zusatzzeile"], "Adresse": $_POST["rechnung_strasse"], "PLZ": $_POST["rechnung_plz"], "Ort": $_POST["rechnung_ort"], "Anlass": $_POST["anlass"], "Baujahrheizung": $_POST["baujahr_heizung"], "Baujahrgebaeude": $_POST["baujahr_gebaeude"], "Baujahrklima": $_POST["baujahr_klimaanlage"], "Gebaeudetyp": $_POST["gebaeudetyp"], "Nutzflaeche": $_POST["nutzflaeche"], "Sanierungsstatus": $_POST["sanierungsstatus"], "Nachricht": $_POST["nachricht"], "Datei": $dateien, "Rolle": $_POST["rolle"], "Email": $_POST["email"], "Angebotspreis": $_POST["Angebotspreis"], "Rabatt": $_POST["Rabatt"], "lex_office_id": $lex_office_id, "Textanschreiben": $_POST["Textanschreiben"], "Positionstext": $_POST["Positionstext"], "Auftragsnummer": $_POST["Auftragsnummer"] ]); // Die Auftragsnummer abfragen $curl_nummer = curl_init(); curl_setopt_array($curl_nummer, [ CURLOPT_URL: "https://api.lexoffice.io/v1/quotations/$lex_office_id", CURLOPT_RETURNTRANSFER: true, CURLOPT_ENCODING: "", CURLOPT_MAXREDIRS: 10, CURLOPT_TIMEOUT: 30, CURLOPT_HTTP_VERSION: CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST: "GET", CURLOPT_HTTPHEADER: [ "Accept: application/json", "Authorization: Bearer " . LEX_OFFICE_API_KEY ] ]); $nummer_response = curl_exec($curl_nummer); $nummer_response = json_decode($nummer_response, true); $err = curl_error($curl_nummer); curl_close($curl_nummer); // Und die Mail verschicken $mail = new PHPMailer\PHPMailer\PHPMailer(true); $mail->IsSMTP(); $mail->Host = "smtp.ionos.de"; $mail->SMTPAuth = true; $mail->Username = 'info@online-energieausweis.org'; $mail->Password = 'Katendeich5a2024!'; $mail->CharSet = 'UTF-8'; $mail->Encoding = 'base64'; $mail->setFrom("info@online-energieausweis.org", "IB Cornelsen",0); $mail->addAddress($_POST["email"], $_POST["vorname"] . " " . $_POST["name"]); $mail->addCC("jens.cornelsen@ib-cornelsen.de", "Jens Cornelsen"); $mail->isHTML(true); $mail->Subject = "{$nummer_response["voucherNumber"]} - {$_POST["Angebotsbezeichnung"]}"; // carriage return type (RFC) $eol = "
"; $anrede = "Sehr geehrte" . ($_POST["anrede"] == "Herr" ? "r " : " ") . $_POST["anrede"]; $message = "$anrede {$_POST["name"]},{$eol}{$eol}"; $message .= "vielen Dank für Ihre Anfrage. Im Anhang finden Sie das ausgearbeitete Angebot als PDF-Dokument.{$eol}{$eol}"; $message .= "Mit freundlichen Grüßen,$eol"; $message .= "Jens Cornelsen$eol"; $message .= "--{$eol}IB Cornelsen{$eol}Katendeich 5A{$eol}21035 Hamburg{$eol}{$eol}www.ib-cornelsen.de{$eol}{$eol}Fon 040 209339858{$eol}Fax 040 209339859"; $mail->Body = $message; $curl_pdf = curl_init(); curl_setopt_array($curl_pdf, [ CURLOPT_URL: "https://api.lexoffice.io/v1/files/$pdf_id", CURLOPT_RETURNTRANSFER: true, CURLOPT_ENCODING: "", CURLOPT_MAXREDIRS: 10, CURLOPT_TIMEOUT: 30, CURLOPT_HTTP_VERSION: CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST: "GET", CURLOPT_HTTPHEADER: [ "Accept: application/pdf", "Authorization: Bearer " . LEX_OFFICE_API_KEY ] ]); $response = curl_exec($curl_pdf); $err = curl_error($curl_pdf); $HOME = dirname($ROOT); curl_close($curl_pdf); $fp = fopen("$HOME/{$PERSISTENT_FOLDER}/angebote/$pdf_id.pdf", "w+"); fwrite($fp, $response); fclose($fp); $mail->addAttachment("$HOME/{$PERSISTENT_FOLDER}/angebote/$pdf_id.pdf", "Angebot.pdf"); $mail->send(); echo "Alles wurde abgesendet! Du kannst die Webseite nun verlassen."; } export async function createLexOfficeCustomer(user: Benutzer) { if (!user.adresse) { return null; } const response = await fetch("https://api.lexoffice.io/v1/contacts", { body: JSON.stringify({ "version": "0", "company": { "name": `${user.vorname} ${user.name}` }, "note": "", "roles": { "customer": {} }, "addresses": { "billing": [{ "street": user.adresse, "zip": user.plz, "city": user.ort, "countryCode": "DE" }] }, "emailAddresses": { "business": user.email } }), method: "POST", headers: { Accept: "application/json", Authorization: `Bearer ${LEX_OFFICE_API_KEY}`, "Content-Type": "application/json" } }) const result = await response.json(); if (!result.id) { return null; } return result.id; }