-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInvoiceGenerator.js
56 lines (50 loc) · 1.96 KB
/
InvoiceGenerator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { Helper } from './helper.js';
import { taxRate, InvoiceStatus } from './constants.js';
import { World } from './World.js';
export class InvoiceGenerator {
static generateInvoice(items, supplier, customer, category, date = null, isExternal = false) {
const itemsInInvoice = new Map(); // Map, um gleiche Artikel zusammenzufassen
let totalAmount = 0;
items.forEach(({ item, quantity }) => {
const itemTotal = parseFloat((item.price * quantity).toFixed(2));
totalAmount += itemTotal;
if (itemsInInvoice.has(item.name)) {
const existing = itemsInInvoice.get(item.name);
itemsInInvoice.set(item.name, {
...existing,
quantity: existing.quantity + quantity,
totalPrice: existing.totalPrice + itemTotal,
});
} else {
itemsInInvoice.set(item.name, {
name: item.name,
quantity,
unit: item.unit,
unitPrice: item.price,
totalPrice: itemTotal,
});
}
});
if (itemsInInvoice.size === 0) {
console.warn('Keine Artikel wurden geliefert. Rechnung kann nicht erstellt werden.');
return null;
}
const netAmount = parseFloat((totalAmount / (1 + taxRate)).toFixed(2));
const taxAmount = parseFloat((totalAmount - netAmount).toFixed(2));
const invoice = {
invoiceId: Helper.generateUUID(),
date: date ? date : World.getInstance().getDate(),
isExternal,
category,
supplier,
customer,
items: Array.from(itemsInInvoice.values()),
totalAmount: parseFloat(totalAmount.toFixed(2)),
taxRate,
taxAmount,
netAmount,
status: InvoiceStatus.OPEN
};
return invoice;
}
}