Abrechnung Generierung
This commit is contained in:
136
src/pages/dashboard/abrechnung/monatlich.pdf.astro
Normal file
136
src/pages/dashboard/abrechnung/monatlich.pdf.astro
Normal file
@@ -0,0 +1,136 @@
|
||||
---
|
||||
import abrechnungTemplateHTML from "../../../templates/pdf/abrechnung.handlebars?raw";
|
||||
import puppeteer from "puppeteer";
|
||||
import Handlebars from "handlebars";
|
||||
import moment from "moment";
|
||||
import { getCurrentUser } from "#lib/server/user";
|
||||
import { prisma } from "#lib/server/prisma";
|
||||
import { extrahiereAusweisAusFeldMitMehrerenAusweisen } from "#lib/server/ausweis";
|
||||
|
||||
const datum = moment(Astro.url.searchParams.get("d"));
|
||||
const benutzer = await getCurrentUser(Astro);
|
||||
|
||||
if (!benutzer) {
|
||||
return Astro.redirect("/404");
|
||||
}
|
||||
|
||||
let bestellungen = await prisma.rechnung.findMany({
|
||||
where: {
|
||||
partner_code: "immowelt",
|
||||
OR: [
|
||||
{
|
||||
verbrauchsausweis_gewerbe: {
|
||||
ausgestellt: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
bedarfsausweis_wohnen: {
|
||||
ausgestellt: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
verbrauchsausweis_wohnen: {
|
||||
ausgestellt: true,
|
||||
},
|
||||
},
|
||||
],
|
||||
AND: [
|
||||
{
|
||||
created_at: {
|
||||
gte: datum.startOf("month").toDate(),
|
||||
},
|
||||
},
|
||||
{
|
||||
created_at: {
|
||||
lte: datum.endOf("month").toDate(),
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
orderBy: {
|
||||
created_at: "desc",
|
||||
},
|
||||
include: {
|
||||
bedarfsausweis_wohnen: {
|
||||
include: {
|
||||
aufnahme: {
|
||||
include: {
|
||||
objekt: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
verbrauchsausweis_gewerbe: {
|
||||
include: {
|
||||
aufnahme: {
|
||||
include: {
|
||||
objekt: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
verbrauchsausweis_wohnen: {
|
||||
include: {
|
||||
aufnahme: {
|
||||
include: {
|
||||
objekt: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const provisionen = await prisma.provisionen.findMany({
|
||||
where: {
|
||||
benutzer_id: benutzer.id
|
||||
}
|
||||
})
|
||||
|
||||
const ausweisBestellungen = bestellungen.map(bestellung => extrahiereAusweisAusFeldMitMehrerenAusweisen(bestellung));
|
||||
|
||||
console.log(ausweisBestellungen);
|
||||
|
||||
|
||||
const browser = await puppeteer.launch({
|
||||
headless: true,
|
||||
args: ["--no-sandbox", "--disable-setuid-sandbox"],
|
||||
});
|
||||
const page = await browser.newPage();
|
||||
|
||||
// Wir splitten die Daten in Blöcke auf, der erste Block hat 11 Einträge, die folgenden Blöcke haben 15 Einträge.
|
||||
|
||||
const blocks = [];
|
||||
const firstBlock = ausweisBestellungen.slice(0, 16);
|
||||
const remainingBlocks = ausweisBestellungen.slice(16);
|
||||
|
||||
blocks.push(firstBlock);
|
||||
for (let i = 0; i < remainingBlocks.length; i += 20) {
|
||||
blocks.push(remainingBlocks.slice(i, i + 20));
|
||||
}
|
||||
|
||||
Handlebars.registerHelper("get-provision-prozent", function (ausweisart) {
|
||||
const provisionEintrag = provisionen.find((p) => p.ausweisart === ausweisart);
|
||||
return provisionEintrag ? provisionEintrag.provision_prozent : 0;
|
||||
});
|
||||
|
||||
Handlebars.registerHelper("get-provision-betrag", function (ausweisart) {
|
||||
const provisionEintrag = provisionen.find((p) => p.ausweisart === ausweisart);
|
||||
return provisionEintrag ? provisionEintrag.provision_betrag.toFixed(2) : "0.00";
|
||||
});
|
||||
|
||||
const template = Handlebars.compile(abrechnungTemplateHTML);
|
||||
const html = template({ monat: datum.format("MMMM YYYY"), bestellungen: blocks });
|
||||
await page.goto(`data:text/html;charset=UTF-8,${encodeURIComponent(html)}`, {
|
||||
waitUntil: "networkidle0",
|
||||
});
|
||||
const pdf = await page.pdf({ path: "abrechnung.pdf", format: "A4" });
|
||||
await browser.close();
|
||||
|
||||
return new Response(pdf, {
|
||||
headers: {
|
||||
"Content-Type": "application/pdf",
|
||||
"Content-Disposition": "attachment; filename=abrechnung.pdf",
|
||||
},
|
||||
});
|
||||
---
|
||||
Reference in New Issue
Block a user