feat(ZUGFERD): Add dedicated ZUGFERD v1/v2 support and refine invoice format detection logic
This commit is contained in:
@ -1,5 +1,7 @@
|
||||
import { InvoiceFormat } from '../../interfaces/common.js';
|
||||
import { DOMParser } from 'xmldom';
|
||||
import * as xpath from 'xpath';
|
||||
import { CII_PROFILE_IDS, ZUGFERD_V1_NAMESPACES } from '../cii/cii.types.js';
|
||||
|
||||
/**
|
||||
* Utility class for detecting invoice formats
|
||||
@ -26,11 +28,91 @@ export class FormatDetector {
|
||||
return InvoiceFormat.XRECHNUNG;
|
||||
}
|
||||
|
||||
// Factur-X/ZUGFeRD detection (CrossIndustryInvoice root element)
|
||||
// Factur-X/ZUGFeRD detection (CrossIndustryInvoice or CrossIndustryDocument root element)
|
||||
if (root.nodeName === 'rsm:CrossIndustryInvoice' || root.nodeName === 'CrossIndustryInvoice') {
|
||||
// For simplicity, we'll treat all CII documents as Factur-X for now
|
||||
// In a real implementation, we would check for specific profiles
|
||||
return InvoiceFormat.FACTURX;
|
||||
// Set up namespaces for XPath queries (ZUGFeRD v2/Factur-X)
|
||||
const namespaces = {
|
||||
rsm: 'urn:un:unece:uncefact:data:standard:CrossIndustryInvoice:100',
|
||||
ram: 'urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100'
|
||||
};
|
||||
|
||||
// Create XPath selector with namespaces
|
||||
const select = xpath.useNamespaces(namespaces);
|
||||
|
||||
// Look for profile identifier
|
||||
const profileNode = select(
|
||||
'string(//rsm:ExchangedDocumentContext/ram:GuidelineSpecifiedDocumentContextParameter/ram:ID)',
|
||||
doc
|
||||
);
|
||||
|
||||
if (profileNode) {
|
||||
const profileText = profileNode.toString();
|
||||
|
||||
// Check for ZUGFeRD profiles
|
||||
if (profileText.includes('zugferd') ||
|
||||
profileText === CII_PROFILE_IDS.ZUGFERD_BASIC ||
|
||||
profileText === CII_PROFILE_IDS.ZUGFERD_COMFORT ||
|
||||
profileText === CII_PROFILE_IDS.ZUGFERD_EXTENDED) {
|
||||
return InvoiceFormat.ZUGFERD;
|
||||
}
|
||||
|
||||
// Check for Factur-X profiles
|
||||
if (profileText.includes('factur-x') ||
|
||||
profileText === CII_PROFILE_IDS.FACTURX_MINIMUM ||
|
||||
profileText === CII_PROFILE_IDS.FACTURX_BASIC ||
|
||||
profileText === CII_PROFILE_IDS.FACTURX_EN16931) {
|
||||
return InvoiceFormat.FACTURX;
|
||||
}
|
||||
}
|
||||
|
||||
// If we can't determine the specific CII format, default to generic CII
|
||||
return InvoiceFormat.CII;
|
||||
}
|
||||
|
||||
// ZUGFeRD v1 detection (CrossIndustryDocument root element)
|
||||
if (root.nodeName === 'rsm:CrossIndustryDocument' || root.nodeName === 'CrossIndustryDocument' ||
|
||||
root.nodeName === 'ram:CrossIndustryDocument') {
|
||||
|
||||
// Check for ZUGFeRD v1 namespace in the document
|
||||
const xmlString = xml.toString();
|
||||
if (xmlString.includes('urn:ferd:CrossIndustryDocument:invoice:1p0') ||
|
||||
xmlString.includes('urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:12')) {
|
||||
return InvoiceFormat.ZUGFERD;
|
||||
}
|
||||
|
||||
// Set up namespaces for XPath queries (ZUGFeRD v1)
|
||||
try {
|
||||
const namespaces = {
|
||||
rsm: ZUGFERD_V1_NAMESPACES.RSM,
|
||||
ram: ZUGFERD_V1_NAMESPACES.RAM
|
||||
};
|
||||
|
||||
// Create XPath selector with namespaces
|
||||
const select = xpath.useNamespaces(namespaces);
|
||||
|
||||
// Look for profile identifier
|
||||
const profileNode = select(
|
||||
'string(//rsm:SpecifiedExchangedDocumentContext/ram:GuidelineSpecifiedDocumentContextParameter/ram:ID)',
|
||||
doc
|
||||
);
|
||||
|
||||
if (profileNode) {
|
||||
const profileText = profileNode.toString();
|
||||
|
||||
// Check for ZUGFeRD v1 profiles
|
||||
if (profileText.includes('ferd:CrossIndustryDocument:invoice:1p0') ||
|
||||
profileText === CII_PROFILE_IDS.ZUGFERD_V1_BASIC ||
|
||||
profileText === CII_PROFILE_IDS.ZUGFERD_V1_COMFORT ||
|
||||
profileText === CII_PROFILE_IDS.ZUGFERD_V1_EXTENDED) {
|
||||
return InvoiceFormat.ZUGFERD;
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.log('Error in ZUGFeRD v1 XPath detection:', error);
|
||||
}
|
||||
|
||||
// If we can't determine the specific profile but it's a CrossIndustryDocument, it's likely ZUGFeRD v1
|
||||
return InvoiceFormat.ZUGFERD;
|
||||
}
|
||||
|
||||
// FatturaPA detection would be implemented here
|
||||
|
Reference in New Issue
Block a user