-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinvoice-pdf-service.ts
131 lines (119 loc) · 4.31 KB
/
invoice-pdf-service.ts
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/**
* SudoSOS back-end API service.
* Copyright (C) 2024 Study association GEWIS
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* @license
*/
/**
* This is the page of invoice-pdf-service.
*
* @module internal/pdf/invoice-pdf-service
*/
import Invoice from '../../entity/invoices/invoice';
import {
Address,
Company,
Dates, FileResponse,
Identity,
InvoiceParameters,
InvoiceReferences,
InvoiceRouteParams,
InvoiceType,
TotalPricing,
} from 'pdf-generator-client';
import InvoicePdf from '../../entity/file/invoice-pdf';
import {
emptyIdentity,
PDF_VAT_HIGH,
PDF_VAT_LOW,
PDF_VAT_ZERO,
subTransactionRowToProduct,
UNUSED_PARAM,
} from '../../helpers/pdf';
import { PdfService } from './pdf-service';
import SubTransactionRow from '../../entity/transactions/sub-transaction-row';
import InvoiceService from '../invoice-service';
import { InvoiceState } from '../../entity/invoices/invoice-status';
export default class InvoicePdfService extends PdfService<InvoicePdf, Invoice, InvoiceRouteParams> {
routeConstructor = InvoiceRouteParams;
pdfConstructor = InvoicePdf;
generator(routeParams: InvoiceRouteParams): Promise<FileResponse> {
return this.client.generateInvoice(InvoiceType.Invoice, routeParams);
}
invoiceToPricing(invoice: Invoice): TotalPricing {
let exclVat: number = 0, lowVat: number = 0, highVat: number = 0, inclVat = 0;
const rows = InvoiceService.isState(invoice, InvoiceState.DELETED)
? invoice.subTransactionRowsDeletedInvoice
: invoice.subTransactionRows;
rows.map((str: SubTransactionRow) => {
const exclVatPerUnit = Math.round(str.product.priceInclVat.getAmount() / (1 + (str.product.vat.percentage / 100)));
exclVat += exclVatPerUnit * str.amount;
switch (str.product.vat.percentage) {
case PDF_VAT_LOW:
lowVat += (str.product.priceInclVat.getAmount() - exclVatPerUnit) * str.amount;
break;
case PDF_VAT_HIGH:
highVat += (str.product.priceInclVat.getAmount() - exclVatPerUnit) * str.amount;
break;
case PDF_VAT_ZERO:
break;
default:
throw new Error(`Unsupported vat percentage ${str.product.vat.percentage} during pdf generation.`);
}
inclVat += str.product.priceInclVat.getAmount() * str.amount;
});
return new TotalPricing({
exclVat,
lowVat,
highVat,
inclVat,
});
}
async getParameters(entity: Invoice): Promise<InvoiceParameters> {
const products = InvoiceService.isState(entity, InvoiceState.DELETED)
? entity.subTransactionRowsDeletedInvoice.map((str: SubTransactionRow) => subTransactionRowToProduct(str))
: entity.subTransactionRows.map((str: SubTransactionRow) => subTransactionRowToProduct(str));
products.sort((a, b) => a.name.localeCompare(b.name));
const pricing = this.invoiceToPricing(entity);
return new InvoiceParameters({
reference: new InvoiceReferences({
ourReference: entity.reference,
yourReference: String(entity.id),
costCenter: true,
}),
products,
pricing,
subject: entity.description,
sender: emptyIdentity(),
// Partly unused, but still required.
recipient: new Identity({ firstName: UNUSED_PARAM, lastName: UNUSED_PARAM, lastNamePreposition: UNUSED_PARAM, fullName: entity.attention }),
description: entity.description,
dates: new Dates({
date: entity.date,
}),
company: new Company({
name: entity.addressee,
id: String(entity.toId),
}),
address: new Address({
street: entity.street,
postalCode: entity.postalCode,
city: entity.city,
country: entity.country,
}),
});
}
}