Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pixeldrive/peppol-toolkit",
"version": "0.8.1",
"version": "0.8.2",
"description": "A TypeScript toolkit for building and reading peppol UBL documents",
"keywords": [
"peppol",
Expand Down
10 changes: 7 additions & 3 deletions src/builder/DocumentBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import XMLAttributes from '../helpers/XMLAttributes';
import getDateString from '../helpers/getDateString';
import { z } from 'zod';
import { isNullish } from '../helpers/isNullish';

const DEFAULT_CUSTOMIZATION_ID =
'urn:cen.eu:en16931:2017#compliant#urn:fdc:peppol.eu:2017:poacc:billing:3.0';
Expand Down Expand Up @@ -43,7 +44,7 @@
* @param invoice The invoice data
* @returns The the invoice in Peppol format as an object
*/
private __buildInvoice(invoice: Invoice) {

Check warning on line 47 in src/builder/DocumentBuilder.ts

View workflow job for this annotation

GitHub Actions / lint

Refactor this function to reduce its Cognitive Complexity from 25 to the 15 allowed
return {
...this.__xmlHeader,
Invoice: {
Expand Down Expand Up @@ -236,7 +237,7 @@
* @param creditNote The credit-note data
* @returns The creditNote in Peppol format as an object
*/
private __buildCreditNote(creditNote: CreditNote) {

Check warning on line 240 in src/builder/DocumentBuilder.ts

View workflow job for this annotation

GitHub Actions / lint

Refactor this function to reduce its Cognitive Complexity from 23 to the 15 allowed
return {
...this.__xmlHeader,
CreditNote: {
Expand Down Expand Up @@ -766,17 +767,20 @@
...this.__buildAmount(
'PayableAmount',
total.payableAmount,
total.payableAmountCurrency ?? currency
total.payableAmountCurrency ?? currency,
true
),
};
}

private __buildAmount(
name: string,
amount: number | undefined,
currency: CurrencyCode
currency: CurrencyCode,
generateOnZero = false
) {
if (!amount) return {};
if (isNullish(amount)) return {};
if (!amount && !generateOnZero) return {};
return {
[`cbc:${name}`]: {
'#text': amount.toFixed(2),
Expand Down Expand Up @@ -990,7 +994,7 @@
);
}

private __buildLineItem(

Check warning on line 997 in src/builder/DocumentBuilder.ts

View workflow job for this annotation

GitHub Actions / lint

Refactor this function to reduce its Cognitive Complexity from 25 to the 15 allowed
line: Invoice['invoiceLines'][number],
quantityTag: string
) {
Expand Down
2 changes: 2 additions & 0 deletions src/helpers/isNullish.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const isNullish = (value: any): value is null | undefined =>

Check warning on line 1 in src/helpers/isNullish.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type

Check warning on line 1 in src/helpers/isNullish.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/helpers/isNullish.ts#L1

Unexpected any. Specify a different type.
value === null || value === undefined;
Comment thread
SimonLoir marked this conversation as resolved.
36 changes: 36 additions & 0 deletions tests/builder-creditnote.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,42 @@ describe('Creditnote Builder', () => {
);
});

it('should always include payable amount even when it is zero', () => {
const creditNoteXML = toolkit.creditNoteToPeppolUBL({
...basicCreditNote,
legalMonetaryTotal: {
...basicCreditNote.legalMonetaryTotal,
payableAmount: 0,
},
});

expect(creditNoteXML).toContain(
'<cbc:PayableAmount currencyID="EUR">0.00</cbc:PayableAmount>'
);
});

it('should omit other zero monetary amounts when generateOnZero is false', () => {
const creditNoteXML = toolkit.creditNoteToPeppolUBL({
...basicCreditNote,
legalMonetaryTotal: {
...basicCreditNote.legalMonetaryTotal,
payableAmount: 0,
prepaidAmount: 0,
allowanceTotalAmount: 0,
chargeTotalAmount: 0,
payableRoundingAmount: 0,
},
});

expect(creditNoteXML).toContain(
'<cbc:PayableAmount currencyID="EUR">0.00</cbc:PayableAmount>'
);
expect(creditNoteXML).not.toContain('<cbc:PrepaidAmount');
expect(creditNoteXML).not.toContain('<cbc:AllowanceTotalAmount');
expect(creditNoteXML).not.toContain('<cbc:ChargeTotalAmount');
expect(creditNoteXML).not.toContain('<cbc:PayableRoundingAmount');
});

it('should not nest cac:Party inside specialized party nodes', () => {
const creditNoteXML = toolkit.creditNoteToPeppolUBL({
...basicCreditNote,
Expand Down
36 changes: 36 additions & 0 deletions tests/builder-invoice.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,42 @@ describe('Invoices Builder', () => {
);
});

it('should always include payable amount even when it is zero', () => {
const invoiceXML = toolkit.invoiceToPeppolUBL({
...basicInvoice,
legalMonetaryTotal: {
...basicInvoice.legalMonetaryTotal,
payableAmount: 0,
},
});

expect(invoiceXML).toContain(
'<cbc:PayableAmount currencyID="EUR">0.00</cbc:PayableAmount>'
);
});

it('should omit other zero monetary amounts when generateOnZero is false', () => {
const invoiceXML = toolkit.invoiceToPeppolUBL({
...basicInvoice,
legalMonetaryTotal: {
...basicInvoice.legalMonetaryTotal,
payableAmount: 0,
prepaidAmount: 0,
allowanceTotalAmount: 0,
chargeTotalAmount: 0,
payableRoundingAmount: 0,
},
});

expect(invoiceXML).toContain(
'<cbc:PayableAmount currencyID="EUR">0.00</cbc:PayableAmount>'
);
expect(invoiceXML).not.toContain('<cbc:PrepaidAmount');
expect(invoiceXML).not.toContain('<cbc:AllowanceTotalAmount');
expect(invoiceXML).not.toContain('<cbc:ChargeTotalAmount');
expect(invoiceXML).not.toContain('<cbc:PayableRoundingAmount');
});

it('should not nest cac:Party inside specialized party nodes', () => {
const invoiceXML = toolkit.invoiceToPeppolUBL({
...basicInvoice,
Expand Down
Loading