Skip to content

Commit

Permalink
added functions for cleaning VAT and creating companynumber
Browse files Browse the repository at this point in the history
  • Loading branch information
JochemVH1 committed Jul 5, 2024
1 parent aeb4685 commit fe7be38
Showing 1 changed file with 31 additions and 18 deletions.
49 changes: 31 additions & 18 deletions backend/src/controllers/utils/peppol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,13 @@ export const createAccountingSupplierParty = (invoice: IInvoice, taxScheme: TaxS
postalZone: invoice.your.postalCode.trim(),
});

const companyNumber = invoice.your.btw.replace('BE', '')
.split('.').join('')
.split(' ').join('')
.padStart(10, '0');
const companyNumber = createCompanyNumber(invoice.your.btw, 'BE');

const supplierEndpointID = new UdtIdentifier(companyNumber, {
schemeID: companyNumberScheme
});

const cleanedVat = invoice.your.btw
.split('.').join('')
.split(' ').join('');
const cleanedVat = cleanVat(invoice.your.btw);

const supplierLegalEntity = new PartyLegalEntity({
registrationName: invoice.your.name,
Expand Down Expand Up @@ -81,19 +76,14 @@ export const createAccountingCustomerParty = (invoice: IInvoice, taxScheme: TaxS
postalZone: invoice.client.postalCode?.trim() ?? '',
});

const companyNumber = invoice.your.btw
.replace('BE', '')
.split('.').join('')
.split(' ').join('');
const companyNumber = createCompanyNumber(invoice.client.btw, customerCountryAndCode?.code ?? DEFAULT_COUNTRY_CODE);

const customerLegalEntity = new PartyLegalEntity({
registrationName: invoice.client.name,
companyID: companyNumber
});

const cleanedVat = invoice.your.btw
.split('.').join('')
.split(' ').join('');
const cleanedVat = cleanVat(invoice.your.btw);

const customerEndpointScheme = ENDPOINT_SCHEMES.find(scheme => scheme.country === customerCountryAndCode?.country ? customerCountryAndCode.code : DEFAULT_COUNTRY_CODE);

Expand Down Expand Up @@ -276,10 +266,8 @@ export const postProccess = (invoice: Invoice, pdf: Buffer | undefined, savedInv
let jObj = parser.parse(xml);

//somehow ublbuilder removes leading 0 so we readd it here
const companyNumber = savedInvoice.your.btw.replace('BE', '')
.split('.').join('')
.split(' ').join('')
.padStart(10, '0');
const companyNumber = createCompanyNumber(savedInvoice.your.btw, 'BE');

if(jObj.Invoice['cac:AccountingSupplierParty']['cac:Party']['cbc:EndpointID']){
jObj.Invoice['cac:AccountingSupplierParty']['cac:Party']['cbc:EndpointID'] = {
'#text': companyNumber,
Expand All @@ -306,3 +294,28 @@ export const postProccess = (invoice: Invoice, pdf: Buffer | undefined, savedInv

return xmlContent;
}

const cleanVat = (vat: string): string => {
const cleanedVat = vat
.split('.').join('')
.split(' ').join('');

return cleanedVat;
}

const createCompanyNumber = (vat: string, countryCode: string): string => {
const companyNumber = cleanVat(vat)
.replace(countryCode, '');

switch(countryCode){
case 'NL':
return companyNumber.padStart(12, '0')
case 'UK':
case 'DE':
case 'FR':
return companyNumber.padStart(9, '0')
case 'BE':
default:
return companyNumber.padStart(10, '0')
}
}

0 comments on commit fe7be38

Please sign in to comment.