einvoice/ts/formats/validation/en16931.validator.ts

135 lines
5.3 KiB
TypeScript
Raw Permalink Normal View History

2025-05-27 21:03:10 +00:00
/**
* EN16931 mandatory field validator
* Validates that invoices contain the minimum required fields per the EN16931 standard
*/
export class EN16931Validator {
/**
* Valid ISO 4217 currency codes (common subset)
*/
private static readonly VALID_CURRENCIES = [
'AED', 'AFN', 'ALL', 'AMD', 'ANG', 'AOA', 'ARS', 'AUD', 'AWG', 'AZN',
'BAM', 'BBD', 'BDT', 'BGN', 'BHD', 'BIF', 'BMD', 'BND', 'BOB', 'BRL',
'BSD', 'BTN', 'BWP', 'BYN', 'BZD', 'CAD', 'CDF', 'CHF', 'CLP', 'CNY',
'COP', 'CRC', 'CUC', 'CUP', 'CVE', 'CZK', 'DJF', 'DKK', 'DOP', 'DZD',
'EGP', 'ERN', 'ETB', 'EUR', 'FJD', 'FKP', 'GBP', 'GEL', 'GGP', 'GHS',
'GIP', 'GMD', 'GNF', 'GTQ', 'GYD', 'HKD', 'HNL', 'HRK', 'HTG', 'HUF',
'IDR', 'ILS', 'IMP', 'INR', 'IQD', 'IRR', 'ISK', 'JEP', 'JMD', 'JOD',
'JPY', 'KES', 'KGS', 'KHR', 'KMF', 'KPW', 'KRW', 'KWD', 'KYD', 'KZT',
'LAK', 'LBP', 'LKR', 'LRD', 'LSL', 'LYD', 'MAD', 'MDL', 'MGA', 'MKD',
'MMK', 'MNT', 'MOP', 'MRU', 'MUR', 'MVR', 'MWK', 'MXN', 'MYR', 'MZN',
'NAD', 'NGN', 'NIO', 'NOK', 'NPR', 'NZD', 'OMR', 'PAB', 'PEN', 'PGK',
'PHP', 'PKR', 'PLN', 'PYG', 'QAR', 'RON', 'RSD', 'RUB', 'RWF', 'SAR',
'SBD', 'SCR', 'SDG', 'SEK', 'SGD', 'SHP', 'SLL', 'SOS', 'SPL', 'SRD',
'STN', 'SYP', 'SZL', 'THB', 'TJS', 'TMT', 'TND', 'TOP', 'TRY', 'TTD',
'TVD', 'TWD', 'TZS', 'UAH', 'UGX', 'USD', 'UYU', 'UZS', 'VEF', 'VND',
'VUV', 'WST', 'XAF', 'XCD', 'XDR', 'XOF', 'XPF', 'YER', 'ZAR', 'ZMW',
'ZWD'
];
/**
* Validates that an invoice object contains all mandatory EN16931 fields
* @param invoice The invoice object to validate
* @throws Error if mandatory fields are missing
*/
public static validateMandatoryFields(invoice: any): void {
const errors: string[] = [];
// BR-01: Invoice number is mandatory
if (!invoice.id && !invoice.accountingDocId && !invoice.invoiceId) {
errors.push('BR-01: Invoice number is mandatory');
}
// BR-02: Invoice issue date is mandatory
if (!invoice.date && !invoice.issueDate) {
errors.push('BR-02: Invoice issue date is mandatory');
}
// BR-06: Seller name is mandatory
if (!invoice.from?.name) {
errors.push('BR-06: Seller name is mandatory');
}
// BR-07: Buyer name is mandatory
if (!invoice.to?.name) {
errors.push('BR-07: Buyer name is mandatory');
}
// BR-16: Invoice line is mandatory (at least one)
if (!invoice.items || invoice.items.length === 0) {
errors.push('BR-16: At least one invoice line is mandatory');
}
// BR-03: Invoice type code is mandatory
if (!invoice.accountingDocType) {
errors.push('BR-03: Invoice type code is mandatory');
}
// BR-05: Invoice currency code is mandatory
if (!invoice.currency) {
errors.push('BR-05: Invoice currency code is mandatory');
} else {
// Validate currency format
if (!this.VALID_CURRENCIES.includes(invoice.currency.toUpperCase())) {
errors.push(`Invalid currency code: ${invoice.currency}. Must be a valid ISO 4217 currency code`);
}
}
// BR-08: Seller postal address is mandatory
2025-05-28 11:31:31 +00:00
if (!invoice.from?.address?.city || !invoice.from?.address?.postalCode || (!invoice.from?.address?.countryCode && !invoice.from?.address?.country)) {
2025-05-27 21:03:10 +00:00
errors.push('BR-08: Seller postal address (city, postal code, country) is mandatory');
}
// BR-10: Buyer postal address is mandatory
2025-05-28 11:31:31 +00:00
if (!invoice.to?.address?.city || !invoice.to?.address?.postalCode || (!invoice.to?.address?.countryCode && !invoice.to?.address?.country)) {
2025-05-27 21:03:10 +00:00
errors.push('BR-10: Buyer postal address (city, postal code, country) is mandatory');
}
if (errors.length > 0) {
throw new Error(`EN16931 validation failed:\n${errors.join('\n')}`);
}
}
/**
* Validates mandatory fields for a parsed XML structure
* @param doc The parsed XML document
* @param format The invoice format
* @throws Error if mandatory fields are missing in XML
*/
public static validateXmlMandatoryFields(doc: Document, format: string): void {
const errors: string[] = [];
if (format === 'ubl' || format === 'xrechnung') {
// Check for UBL mandatory fields
const id = doc.getElementsByTagName('cbc:ID')[0]?.textContent;
if (!id) {
errors.push('BR-01: Invoice ID is missing in XML');
}
const issueDate = doc.getElementsByTagName('cbc:IssueDate')[0]?.textContent;
if (!issueDate) {
errors.push('BR-02: Invoice issue date is missing in XML');
}
const sellerName = doc.querySelector('AccountingSupplierParty PartyName Name')?.textContent ||
doc.querySelector('AccountingSupplierParty PartyLegalEntity RegistrationName')?.textContent;
if (!sellerName) {
errors.push('BR-06: Seller name is missing in XML');
}
const buyerName = doc.querySelector('AccountingCustomerParty PartyName Name')?.textContent ||
doc.querySelector('AccountingCustomerParty PartyLegalEntity RegistrationName')?.textContent;
if (!buyerName) {
errors.push('BR-07: Buyer name is missing in XML');
}
const invoiceLines = doc.getElementsByTagName('InvoiceLine');
if (invoiceLines.length === 0) {
errors.push('BR-16: No invoice lines found in XML');
}
}
if (errors.length > 0) {
throw new Error(`EN16931 XML validation failed:\n${errors.join('\n')}`);
}
}
}