53 lines
1.7 KiB
TypeScript
53 lines
1.7 KiB
TypeScript
import { jsPDF } from 'jspdf'
|
|
import {applyPlugin} from 'jspdf-autotable'
|
|
import { ContainerWithDrinkAndHistory, DrinkComposite, containerTypeToString } from '~/models/types';
|
|
import { volume } from '../conversions';
|
|
|
|
export async function createPdf(filename: string, reportId: string, dateStart: string, dateEnd: string, containers: ContainerWithDrinkAndHistory[]){
|
|
applyPlugin(jsPDF);
|
|
|
|
let body : string[][] = [];
|
|
let total : number = 0;
|
|
|
|
for(let container of containers){
|
|
let price = Math.round((container.price * container.checkouts.length) * 100) / 100;
|
|
total += price;
|
|
|
|
let bodyRow = [
|
|
container.drink.name + " - " + containerTypeToString(container.type) + " " + volume(container.volume),
|
|
"€" + container.price,
|
|
container.checkouts.length.toString(),
|
|
"€" + price];
|
|
|
|
body.push(bodyRow);
|
|
}
|
|
|
|
const doc = new jsPDF();
|
|
doc.setCreationDate(new Date(Date.now()));
|
|
|
|
doc.setFontSize(20);
|
|
doc.text("K-FRIDGE", 15, 25);
|
|
|
|
doc.setFontSize(12);
|
|
doc.text("Report #" + reportId, 160, 25);
|
|
|
|
doc.setFontSize(10);
|
|
doc.text("Selected date range: " + dateStart.slice(0, -8).replace("T", "") + " - " + dateEnd.slice(0,-8).replace("T", ""), 15, 35);
|
|
|
|
const table = doc.autoTable({
|
|
startY: 40,
|
|
head: [['Drink', 'Price', 'Amount', 'Total']],
|
|
body: body,
|
|
foot: [["", "", "Total", "€" + (Math.round(total*100)/100)]]
|
|
});
|
|
|
|
const finalY = table?.lastAutoTable?.finalY ?? 40;
|
|
|
|
doc.text("Generated on " + new Date(Date.now()).toString(), 15, finalY + 10);
|
|
|
|
const filepath = "/reports/" + filename + ".pdf";
|
|
|
|
doc.save("public" + filepath);
|
|
|
|
return filepath;
|
|
} |