@financica/ubl 0.8.0 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/build/index.d.ts +183 -169
- package/dist/build/index.d.ts.map +1 -0
- package/dist/build/index.js +660 -595
- package/dist/build/index.js.map +1 -1
- package/dist/index.d.ts +204 -198
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +752 -756
- package/dist/index.js.map +1 -1
- package/package.json +26 -40
package/dist/index.js
CHANGED
|
@@ -1,839 +1,835 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
function cbcText(parent, tag) {
|
|
6
|
-
|
|
7
|
-
return el?.textContent?.trim() ?? "";
|
|
1
|
+
import { DOMParser } from "@xmldom/xmldom";
|
|
2
|
+
//#region src/parser.ts
|
|
3
|
+
const CBC_NS$1 = "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2";
|
|
4
|
+
const CAC_NS$1 = "urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2";
|
|
5
|
+
function cbcText$1(parent, tag) {
|
|
6
|
+
return parent.getElementsByTagNameNS(CBC_NS$1, tag)[0]?.textContent?.trim() ?? "";
|
|
8
7
|
}
|
|
9
|
-
function cacElement(parent, tag) {
|
|
10
|
-
|
|
8
|
+
function cacElement$1(parent, tag) {
|
|
9
|
+
return parent.getElementsByTagNameNS(CAC_NS$1, tag)[0] ?? null;
|
|
11
10
|
}
|
|
12
11
|
function cacElements(parent, tag) {
|
|
13
|
-
|
|
12
|
+
return Array.from(parent.getElementsByTagNameNS(CAC_NS$1, tag));
|
|
14
13
|
}
|
|
15
14
|
function cbcNumber(parent, tag) {
|
|
16
|
-
|
|
17
|
-
|
|
15
|
+
const n = Number.parseFloat(cbcText$1(parent, tag));
|
|
16
|
+
return Number.isNaN(n) ? 0 : n;
|
|
18
17
|
}
|
|
19
18
|
function childElementsByTagNs(parent, namespace, tag) {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
}
|
|
29
|
-
return elements;
|
|
19
|
+
const elements = [];
|
|
20
|
+
for (let i = 0; i < parent.childNodes.length; i++) {
|
|
21
|
+
const node = parent.childNodes[i];
|
|
22
|
+
if (!node || node.nodeType !== 1) continue;
|
|
23
|
+
const element = node;
|
|
24
|
+
if (element.namespaceURI === namespace && element.localName === tag) elements.push(element);
|
|
25
|
+
}
|
|
26
|
+
return elements;
|
|
30
27
|
}
|
|
31
|
-
function cbcDirectText(parent, tag) {
|
|
32
|
-
|
|
33
|
-
return el?.textContent?.trim() ?? "";
|
|
28
|
+
function cbcDirectText$1(parent, tag) {
|
|
29
|
+
return childElementsByTagNs(parent, CBC_NS$1, tag)[0]?.textContent?.trim() ?? "";
|
|
34
30
|
}
|
|
35
31
|
function cbcDirectNumber(parent, tag) {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
32
|
+
const value = cbcDirectText$1(parent, tag);
|
|
33
|
+
if (!value) return null;
|
|
34
|
+
const parsed = Number.parseFloat(value);
|
|
35
|
+
return Number.isNaN(parsed) ? null : parsed;
|
|
40
36
|
}
|
|
41
37
|
function cacDirectElement(parent, tag) {
|
|
42
|
-
|
|
38
|
+
return childElementsByTagNs(parent, CAC_NS$1, tag)[0] ?? null;
|
|
43
39
|
}
|
|
44
40
|
function cacDirectElements(parent, tag) {
|
|
45
|
-
|
|
41
|
+
return childElementsByTagNs(parent, CAC_NS$1, tag);
|
|
46
42
|
}
|
|
47
43
|
function parseAddressFromElement(address) {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
44
|
+
if (!address) return void 0;
|
|
45
|
+
const country = cacElement$1(address, "Country");
|
|
46
|
+
return {
|
|
47
|
+
street: cbcText$1(address, "StreetName"),
|
|
48
|
+
additionalStreet: cbcText$1(address, "AdditionalStreetName") || void 0,
|
|
49
|
+
city: cbcText$1(address, "CityName"),
|
|
50
|
+
postalZone: cbcText$1(address, "PostalZone"),
|
|
51
|
+
countrySubentity: cbcText$1(address, "CountrySubentity") || void 0,
|
|
52
|
+
countryCode: country ? cbcText$1(country, "IdentificationCode") : ""
|
|
53
|
+
};
|
|
58
54
|
}
|
|
59
55
|
function parseAddress(party) {
|
|
60
|
-
|
|
61
|
-
return parseAddressFromElement(postal);
|
|
56
|
+
return parseAddressFromElement(cacElement$1(party, "PostalAddress"));
|
|
62
57
|
}
|
|
63
58
|
function parseTaxSchemeId(parent) {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
59
|
+
if (!parent) return void 0;
|
|
60
|
+
const taxScheme = cacElement$1(parent, "TaxScheme");
|
|
61
|
+
if (!taxScheme) return void 0;
|
|
62
|
+
return cbcText$1(taxScheme, "ID") || void 0;
|
|
68
63
|
}
|
|
69
64
|
function parseContact(party) {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
65
|
+
const contact = cacElement$1(party, "Contact");
|
|
66
|
+
if (!contact) return void 0;
|
|
67
|
+
const name = cbcText$1(contact, "Name");
|
|
68
|
+
const phone = cbcText$1(contact, "Telephone");
|
|
69
|
+
const email = cbcText$1(contact, "ElectronicMail");
|
|
70
|
+
if (!name && !phone && !email) return void 0;
|
|
71
|
+
return {
|
|
72
|
+
name: name || void 0,
|
|
73
|
+
phone: phone || void 0,
|
|
74
|
+
email: email || void 0
|
|
75
|
+
};
|
|
81
76
|
}
|
|
82
77
|
function parsePartyIdentifications(party) {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
78
|
+
const ids = cacElements(party, "PartyIdentification");
|
|
79
|
+
const result = [];
|
|
80
|
+
for (const idEl of ids) {
|
|
81
|
+
const idValue = cbcText$1(idEl, "ID");
|
|
82
|
+
if (!idValue) continue;
|
|
83
|
+
const schemeIdAttr = idEl.getElementsByTagNameNS(CBC_NS$1, "ID")[0]?.getAttribute("schemeID");
|
|
84
|
+
result.push({
|
|
85
|
+
id: idValue,
|
|
86
|
+
schemeId: schemeIdAttr || void 0
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
return result;
|
|
95
90
|
}
|
|
96
91
|
function parseParty(root, role) {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
92
|
+
const wrapper = cacElement$1(root, role);
|
|
93
|
+
const party = wrapper ? cacElement$1(wrapper, "Party") : null;
|
|
94
|
+
if (!party) return { name: "Unknown" };
|
|
95
|
+
const partyName = cacElement$1(party, "PartyName");
|
|
96
|
+
const legalEntity = cacElement$1(party, "PartyLegalEntity");
|
|
97
|
+
const taxScheme = cacElement$1(party, "PartyTaxScheme");
|
|
98
|
+
const partyId = cacElement$1(party, "PartyIdentification");
|
|
99
|
+
const endpointEl = party.getElementsByTagNameNS(CBC_NS$1, "EndpointID")[0];
|
|
100
|
+
const partyIdentifications = parsePartyIdentifications(party);
|
|
101
|
+
const companyIdEl = legalEntity ? legalEntity.getElementsByTagNameNS(CBC_NS$1, "CompanyID")[0] : null;
|
|
102
|
+
return {
|
|
103
|
+
name: partyName ? cbcText$1(partyName, "Name") : "",
|
|
104
|
+
registrationName: legalEntity ? cbcText$1(legalEntity, "RegistrationName") || void 0 : void 0,
|
|
105
|
+
companyLegalForm: legalEntity ? cbcText$1(legalEntity, "CompanyLegalForm") || void 0 : void 0,
|
|
106
|
+
vatId: taxScheme ? cbcText$1(taxScheme, "CompanyID") || void 0 : void 0,
|
|
107
|
+
taxSchemeId: parseTaxSchemeId(taxScheme),
|
|
108
|
+
companyId: companyIdEl ? companyIdEl.textContent?.trim() || void 0 : partyId ? cbcText$1(partyId, "ID") || void 0 : void 0,
|
|
109
|
+
companyIdSchemeId: companyIdEl?.getAttribute("schemeID") || void 0,
|
|
110
|
+
endpointId: endpointEl?.textContent?.trim() || void 0,
|
|
111
|
+
endpointSchemeId: endpointEl?.getAttribute("schemeID") || void 0,
|
|
112
|
+
partyIdentifications: partyIdentifications.length > 0 ? partyIdentifications : void 0,
|
|
113
|
+
address: parseAddress(party),
|
|
114
|
+
contact: parseContact(party)
|
|
115
|
+
};
|
|
121
116
|
}
|
|
122
117
|
function parseTaxSubtotal(sub) {
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
118
|
+
const cat = cacElement$1(sub, "TaxCategory");
|
|
119
|
+
return {
|
|
120
|
+
taxableAmount: cbcNumber(sub, "TaxableAmount"),
|
|
121
|
+
taxAmount: cbcNumber(sub, "TaxAmount"),
|
|
122
|
+
taxPercent: (cat ? cbcNumber(cat, "Percent") : 0) || cbcNumber(sub, "Percent"),
|
|
123
|
+
taxCategoryId: cat ? cbcText$1(cat, "ID") || void 0 : void 0,
|
|
124
|
+
taxSchemeId: parseTaxSchemeId(cat),
|
|
125
|
+
taxExemptionReason: cat ? cbcText$1(cat, "TaxExemptionReason") || void 0 : void 0
|
|
126
|
+
};
|
|
132
127
|
}
|
|
133
128
|
function parseTaxSubtotalsFromTaxTotal(taxTotal) {
|
|
134
|
-
|
|
129
|
+
return cacDirectElements(taxTotal, "TaxSubtotal").map(parseTaxSubtotal);
|
|
135
130
|
}
|
|
136
131
|
function parseTaxSubtotals(root) {
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
132
|
+
const taxTotals = cacDirectElements(root, "TaxTotal");
|
|
133
|
+
if (taxTotals.length === 0) return [];
|
|
134
|
+
return taxTotals.flatMap(parseTaxSubtotalsFromTaxTotal);
|
|
140
135
|
}
|
|
141
136
|
function parseAllowanceCharge(charge) {
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
};
|
|
137
|
+
const taxCategory = cacElement$1(charge, "TaxCategory");
|
|
138
|
+
const indicatorRaw = cbcDirectText$1(charge, "ChargeIndicator").toLowerCase();
|
|
139
|
+
return {
|
|
140
|
+
chargeIndicator: indicatorRaw === "true" || indicatorRaw === "1",
|
|
141
|
+
amount: cbcNumber(charge, "Amount"),
|
|
142
|
+
baseAmount: cbcDirectNumber(charge, "BaseAmount") ?? void 0,
|
|
143
|
+
multiplierFactorNumeric: cbcDirectNumber(charge, "MultiplierFactorNumeric") ?? void 0,
|
|
144
|
+
reason: cbcText$1(charge, "AllowanceChargeReason") || void 0,
|
|
145
|
+
reasonCode: cbcText$1(charge, "AllowanceChargeReasonCode") || void 0,
|
|
146
|
+
taxPercent: taxCategory ? cbcNumber(taxCategory, "Percent") : void 0,
|
|
147
|
+
taxCategoryId: taxCategory ? cbcText$1(taxCategory, "ID") || void 0 : void 0,
|
|
148
|
+
taxSchemeId: parseTaxSchemeId(taxCategory)
|
|
149
|
+
};
|
|
156
150
|
}
|
|
157
151
|
function parseAllowanceCharges(parent) {
|
|
158
|
-
|
|
152
|
+
return cacDirectElements(parent, "AllowanceCharge").map(parseAllowanceCharge);
|
|
159
153
|
}
|
|
160
154
|
function parseLines(root, isCreditNote) {
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
taxPercent,
|
|
215
|
-
taxAmount: taxAmountFromLineTotal ?? (lineTaxSubtotals.length > 0 ? taxAmountFromSubtotals : computedTaxAmount),
|
|
216
|
-
taxCategoryId: lineTaxSubtotals[0]?.taxCategoryId ?? (taxCategory ? cbcText(taxCategory, "ID") || void 0 : void 0),
|
|
217
|
-
taxSchemeId: lineTaxSubtotals[0]?.taxSchemeId ?? parseTaxSchemeId(taxCategory),
|
|
218
|
-
taxSubtotals: lineTaxSubtotals.length > 0 ? lineTaxSubtotals : void 0,
|
|
219
|
-
allowanceCharges: allowanceCharges.length > 0 ? allowanceCharges : void 0,
|
|
220
|
-
discountAmount: discountAmount > 0 ? discountAmount : void 0,
|
|
221
|
-
chargeAmount: chargeAmount > 0 ? chargeAmount : void 0,
|
|
222
|
-
itemName: item ? cbcText(item, "Name") || void 0 : void 0,
|
|
223
|
-
sellersItemId: sellersId ? cbcText(sellersId, "ID") || void 0 : void 0,
|
|
224
|
-
buyersItemId: buyersId ? cbcText(buyersId, "ID") || void 0 : void 0,
|
|
225
|
-
additionalItemProperties: additionalItemProperties.length > 0 ? additionalItemProperties : void 0
|
|
226
|
-
};
|
|
227
|
-
});
|
|
155
|
+
const qtyTag = isCreditNote ? "CreditedQuantity" : "InvoicedQuantity";
|
|
156
|
+
return cacElements(root, isCreditNote ? "CreditNoteLine" : "InvoiceLine").map((line) => {
|
|
157
|
+
const item = cacElement$1(line, "Item");
|
|
158
|
+
const price = cacElement$1(line, "Price");
|
|
159
|
+
const taxCategory = item ? cacElement$1(item, "ClassifiedTaxCategory") : null;
|
|
160
|
+
const sellersId = item ? cacElement$1(item, "SellersItemIdentification") : null;
|
|
161
|
+
const buyersId = item ? cacElement$1(item, "BuyersItemIdentification") : null;
|
|
162
|
+
const qtyEl = line.getElementsByTagNameNS(CBC_NS$1, qtyTag)[0];
|
|
163
|
+
const lineExtensionAmount = cbcNumber(line, "LineExtensionAmount");
|
|
164
|
+
const lineTaxTotal = cacDirectElement(line, "TaxTotal");
|
|
165
|
+
const lineTaxSubtotals = lineTaxTotal ? parseTaxSubtotalsFromTaxTotal(lineTaxTotal) : [];
|
|
166
|
+
const taxAmountFromSubtotals = lineTaxSubtotals.reduce((sum, subtotal) => sum + subtotal.taxAmount, 0);
|
|
167
|
+
const taxAmountFromLineTotal = lineTaxTotal ? cbcDirectNumber(lineTaxTotal, "TaxAmount") : null;
|
|
168
|
+
const taxPercent = lineTaxSubtotals[0]?.taxPercent ?? (taxCategory ? cbcNumber(taxCategory, "Percent") : void 0);
|
|
169
|
+
const computedTaxAmount = taxPercent !== void 0 ? Number((lineExtensionAmount * taxPercent / 100).toFixed(2)) : void 0;
|
|
170
|
+
const lineAllowanceCharges = parseAllowanceCharges(line);
|
|
171
|
+
const priceAllowanceCharges = price ? parseAllowanceCharges(price) : [];
|
|
172
|
+
const allowanceCharges = [...lineAllowanceCharges, ...priceAllowanceCharges];
|
|
173
|
+
const discountAmount = allowanceCharges.reduce((sum, charge) => charge.chargeIndicator ? sum : sum + Math.abs(charge.amount), 0);
|
|
174
|
+
const chargeAmount = allowanceCharges.reduce((sum, charge) => charge.chargeIndicator ? sum + Math.abs(charge.amount) : sum, 0);
|
|
175
|
+
const additionalItemProperties = [];
|
|
176
|
+
if (item) for (const prop of cacElements(item, "AdditionalItemProperty")) {
|
|
177
|
+
const propName = cbcText$1(prop, "Name");
|
|
178
|
+
const propValue = cbcText$1(prop, "Value");
|
|
179
|
+
if (propName) additionalItemProperties.push({
|
|
180
|
+
name: propName,
|
|
181
|
+
value: propValue
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
const priceAmount = price ? cbcNumber(price, "PriceAmount") : 0;
|
|
185
|
+
const priceBaseQuantity = price ? cbcDirectNumber(price, "BaseQuantity") : null;
|
|
186
|
+
const unitPrice = priceBaseQuantity && priceBaseQuantity > 0 ? priceAmount / priceBaseQuantity : priceAmount;
|
|
187
|
+
return {
|
|
188
|
+
id: cbcText$1(line, "ID"),
|
|
189
|
+
description: item ? cbcText$1(item, "Description") : "",
|
|
190
|
+
quantity: cbcNumber(line, qtyTag),
|
|
191
|
+
unitCode: qtyEl?.getAttribute("unitCode") ?? "",
|
|
192
|
+
unitPrice,
|
|
193
|
+
lineExtensionAmount,
|
|
194
|
+
taxPercent,
|
|
195
|
+
taxAmount: taxAmountFromLineTotal ?? (lineTaxSubtotals.length > 0 ? taxAmountFromSubtotals : computedTaxAmount),
|
|
196
|
+
taxCategoryId: lineTaxSubtotals[0]?.taxCategoryId ?? (taxCategory ? cbcText$1(taxCategory, "ID") || void 0 : void 0),
|
|
197
|
+
taxSchemeId: lineTaxSubtotals[0]?.taxSchemeId ?? parseTaxSchemeId(taxCategory),
|
|
198
|
+
taxSubtotals: lineTaxSubtotals.length > 0 ? lineTaxSubtotals : void 0,
|
|
199
|
+
allowanceCharges: allowanceCharges.length > 0 ? allowanceCharges : void 0,
|
|
200
|
+
discountAmount: discountAmount > 0 ? discountAmount : void 0,
|
|
201
|
+
chargeAmount: chargeAmount > 0 ? chargeAmount : void 0,
|
|
202
|
+
itemName: item ? cbcText$1(item, "Name") || void 0 : void 0,
|
|
203
|
+
sellersItemId: sellersId ? cbcText$1(sellersId, "ID") || void 0 : void 0,
|
|
204
|
+
buyersItemId: buyersId ? cbcText$1(buyersId, "ID") || void 0 : void 0,
|
|
205
|
+
additionalItemProperties: additionalItemProperties.length > 0 ? additionalItemProperties : void 0
|
|
206
|
+
};
|
|
207
|
+
});
|
|
228
208
|
}
|
|
229
209
|
function parseMonetaryTotal(root) {
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
payableAmount: cbcNumber(total, "PayableAmount")
|
|
248
|
-
};
|
|
210
|
+
const total = cacDirectElement(root, "LegalMonetaryTotal");
|
|
211
|
+
if (!total) return {
|
|
212
|
+
lineExtensionAmount: 0,
|
|
213
|
+
taxExclusiveAmount: 0,
|
|
214
|
+
taxInclusiveAmount: 0,
|
|
215
|
+
payableAmount: 0
|
|
216
|
+
};
|
|
217
|
+
return {
|
|
218
|
+
lineExtensionAmount: cbcNumber(total, "LineExtensionAmount"),
|
|
219
|
+
taxExclusiveAmount: cbcNumber(total, "TaxExclusiveAmount"),
|
|
220
|
+
taxInclusiveAmount: cbcNumber(total, "TaxInclusiveAmount"),
|
|
221
|
+
allowanceTotalAmount: cbcNumber(total, "AllowanceTotalAmount") || void 0,
|
|
222
|
+
chargeTotalAmount: cbcNumber(total, "ChargeTotalAmount") || void 0,
|
|
223
|
+
prepaidAmount: cbcNumber(total, "PrepaidAmount") || void 0,
|
|
224
|
+
payableRoundingAmount: cbcNumber(total, "PayableRoundingAmount") || void 0,
|
|
225
|
+
payableAmount: cbcNumber(total, "PayableAmount")
|
|
226
|
+
};
|
|
249
227
|
}
|
|
250
228
|
function parsePaymentMeansElement(pm) {
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
229
|
+
const account = cacElement$1(pm, "PayeeFinancialAccount");
|
|
230
|
+
const branch = account ? cacElement$1(account, "FinancialInstitutionBranch") : null;
|
|
231
|
+
const paymentMeansCodeEl = pm.getElementsByTagNameNS(CBC_NS$1, "PaymentMeansCode")[0];
|
|
232
|
+
const mandate = cacElement$1(pm, "PaymentMandate");
|
|
233
|
+
return {
|
|
234
|
+
code: cbcText$1(pm, "PaymentMeansCode"),
|
|
235
|
+
codeName: paymentMeansCodeEl?.getAttribute("name") || void 0,
|
|
236
|
+
paymentId: cbcText$1(pm, "PaymentID") || void 0,
|
|
237
|
+
iban: account ? cbcText$1(account, "ID") || void 0 : void 0,
|
|
238
|
+
bic: branch ? cbcText$1(branch, "ID") || void 0 : void 0,
|
|
239
|
+
accountName: account && cbcText$1(account, "Name") || void 0,
|
|
240
|
+
mandateId: mandate ? cbcText$1(mandate, "ID") || void 0 : void 0
|
|
241
|
+
};
|
|
264
242
|
}
|
|
265
243
|
function parsePaymentMeansList(root) {
|
|
266
|
-
|
|
267
|
-
|
|
244
|
+
const list = cacElements(root, "PaymentMeans").map(parsePaymentMeansElement);
|
|
245
|
+
return list.length > 0 ? list : void 0;
|
|
268
246
|
}
|
|
269
247
|
function parseInvoicePeriod(root) {
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
248
|
+
const period = cacElement$1(root, "InvoicePeriod");
|
|
249
|
+
if (!period) return void 0;
|
|
250
|
+
const start = cbcText$1(period, "StartDate");
|
|
251
|
+
const end = cbcText$1(period, "EndDate");
|
|
252
|
+
if (!start && !end) return void 0;
|
|
253
|
+
return {
|
|
254
|
+
startDate: start || void 0,
|
|
255
|
+
endDate: end || void 0,
|
|
256
|
+
descriptionCode: cbcText$1(period, "DescriptionCode") || void 0
|
|
257
|
+
};
|
|
280
258
|
}
|
|
281
259
|
function parseDelivery(root) {
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
address
|
|
293
|
-
};
|
|
260
|
+
const delivery = cacElement$1(root, "Delivery");
|
|
261
|
+
if (!delivery) return void 0;
|
|
262
|
+
const actualDeliveryDate = cbcText$1(delivery, "ActualDeliveryDate") || void 0;
|
|
263
|
+
const deliveryLocation = cacElement$1(delivery, "DeliveryLocation");
|
|
264
|
+
const address = parseAddressFromElement(deliveryLocation ? cacElement$1(deliveryLocation, "Address") : null);
|
|
265
|
+
if (!actualDeliveryDate && !address) return void 0;
|
|
266
|
+
return {
|
|
267
|
+
actualDeliveryDate,
|
|
268
|
+
address
|
|
269
|
+
};
|
|
294
270
|
}
|
|
295
271
|
function parsePaymentTermsNote(root) {
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
return note || void 0;
|
|
272
|
+
const paymentTerms = cacElement$1(root, "PaymentTerms");
|
|
273
|
+
if (!paymentTerms) return void 0;
|
|
274
|
+
return cbcText$1(paymentTerms, "Note") || void 0;
|
|
300
275
|
}
|
|
301
276
|
function parseNotes(root) {
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
277
|
+
const noteEls = root.getElementsByTagNameNS(CBC_NS$1, "Note");
|
|
278
|
+
const notes = [];
|
|
279
|
+
for (let i = 0; i < noteEls.length; i++) {
|
|
280
|
+
const el = noteEls[i];
|
|
281
|
+
if (!el) continue;
|
|
282
|
+
if (el.parentElement === root || el.parentNode === root) {
|
|
283
|
+
const text = el.textContent?.trim();
|
|
284
|
+
if (text) notes.push(text);
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
return notes.length > 0 ? notes.join("\n") : void 0;
|
|
313
288
|
}
|
|
314
289
|
function parseAttachments(root) {
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
}
|
|
354
|
-
}
|
|
355
|
-
return {
|
|
356
|
-
attachments: attachments.length > 0 ? attachments : void 0,
|
|
357
|
-
documentReferences: documentReferences.length > 0 ? documentReferences : void 0
|
|
358
|
-
};
|
|
290
|
+
const refs = cacElements(root, "AdditionalDocumentReference");
|
|
291
|
+
const attachments = [];
|
|
292
|
+
const documentReferences = [];
|
|
293
|
+
for (const ref of refs) {
|
|
294
|
+
const attachment = cacElement$1(ref, "Attachment");
|
|
295
|
+
const refId = cbcText$1(ref, "ID");
|
|
296
|
+
const description = cbcText$1(ref, "DocumentDescription") || void 0;
|
|
297
|
+
if (!attachment) {
|
|
298
|
+
if (refId || description) documentReferences.push({
|
|
299
|
+
id: refId,
|
|
300
|
+
description
|
|
301
|
+
});
|
|
302
|
+
continue;
|
|
303
|
+
}
|
|
304
|
+
const binaryEl = attachment.getElementsByTagNameNS(CBC_NS$1, "EmbeddedDocumentBinaryObject")[0];
|
|
305
|
+
const externalRef = cacElement$1(attachment, "ExternalReference");
|
|
306
|
+
if (binaryEl) {
|
|
307
|
+
const content = binaryEl.textContent?.trim();
|
|
308
|
+
if (content) attachments.push({
|
|
309
|
+
id: refId,
|
|
310
|
+
filename: binaryEl.getAttribute("filename") || void 0,
|
|
311
|
+
mimeCode: binaryEl.getAttribute("mimeCode") || void 0,
|
|
312
|
+
description,
|
|
313
|
+
base64Content: content
|
|
314
|
+
});
|
|
315
|
+
} else if (externalRef) {
|
|
316
|
+
const uri = cbcText$1(externalRef, "URI");
|
|
317
|
+
if (uri) attachments.push({
|
|
318
|
+
id: refId,
|
|
319
|
+
description,
|
|
320
|
+
externalUri: uri
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
return {
|
|
325
|
+
attachments: attachments.length > 0 ? attachments : void 0,
|
|
326
|
+
documentReferences: documentReferences.length > 0 ? documentReferences : void 0
|
|
327
|
+
};
|
|
359
328
|
}
|
|
360
329
|
function parseBillingReference(root) {
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
330
|
+
const billingRef = cacElement$1(root, "BillingReference");
|
|
331
|
+
if (!billingRef) return void 0;
|
|
332
|
+
const invoiceRef = cacElement$1(billingRef, "InvoiceDocumentReference");
|
|
333
|
+
if (!invoiceRef) return void 0;
|
|
334
|
+
const invoiceId = cbcText$1(invoiceRef, "ID") || void 0;
|
|
335
|
+
const invoiceIssueDate = cbcText$1(invoiceRef, "IssueDate") || void 0;
|
|
336
|
+
if (!invoiceId && !invoiceIssueDate) return void 0;
|
|
337
|
+
return {
|
|
338
|
+
invoiceId,
|
|
339
|
+
invoiceIssueDate
|
|
340
|
+
};
|
|
369
341
|
}
|
|
370
|
-
|
|
342
|
+
/**
|
|
343
|
+
* Strip DOCTYPE declarations to prevent XXE (XML External Entity) attacks.
|
|
344
|
+
* @xmldom/xmldom resolves external entities by default, so we remove
|
|
345
|
+
* DOCTYPE blocks (including inline DTD subsets) before parsing.
|
|
346
|
+
*/
|
|
347
|
+
const stripDoctype$1 = (xml) => xml.replace(/<!DOCTYPE\s[^>[]*(?:\[[^\]]*\])?>/gi, "");
|
|
371
348
|
function parseUblInvoice(xml) {
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
};
|
|
427
|
-
} catch {
|
|
428
|
-
return null;
|
|
429
|
-
}
|
|
349
|
+
try {
|
|
350
|
+
const safeXml = stripDoctype$1(xml);
|
|
351
|
+
const BrowserDomParser = globalThis.DOMParser;
|
|
352
|
+
const doc = (BrowserDomParser ? new BrowserDomParser() : new DOMParser()).parseFromString(safeXml, "text/xml");
|
|
353
|
+
if (doc.getElementsByTagName("parsererror").length > 0) return null;
|
|
354
|
+
const root = doc.documentElement;
|
|
355
|
+
if (!root) return null;
|
|
356
|
+
const localName = root.localName;
|
|
357
|
+
let documentType;
|
|
358
|
+
if (localName === "Invoice") documentType = "Invoice";
|
|
359
|
+
else if (localName === "CreditNote") documentType = "CreditNote";
|
|
360
|
+
else return null;
|
|
361
|
+
const isCreditNote = documentType === "CreditNote";
|
|
362
|
+
const id = cbcText$1(root, "ID");
|
|
363
|
+
if (!id) return null;
|
|
364
|
+
const paymentMeansList = parsePaymentMeansList(root);
|
|
365
|
+
const orderReference = cacElement$1(root, "OrderReference");
|
|
366
|
+
const contractReference = cacElement$1(root, "ContractDocumentReference");
|
|
367
|
+
const projectReference = cacElement$1(root, "ProjectReference");
|
|
368
|
+
const { attachments, documentReferences } = parseAttachments(root);
|
|
369
|
+
return {
|
|
370
|
+
documentType,
|
|
371
|
+
customizationId: cbcText$1(root, "CustomizationID") || void 0,
|
|
372
|
+
profileId: cbcText$1(root, "ProfileID") || void 0,
|
|
373
|
+
id,
|
|
374
|
+
invoiceTypeCode: cbcText$1(root, "InvoiceTypeCode") || cbcText$1(root, "CreditNoteTypeCode") || void 0,
|
|
375
|
+
issueDate: cbcText$1(root, "IssueDate"),
|
|
376
|
+
dueDate: cbcText$1(root, "DueDate") || void 0,
|
|
377
|
+
taxPointDate: cbcText$1(root, "TaxPointDate") || void 0,
|
|
378
|
+
currency: cbcText$1(root, "DocumentCurrencyCode"),
|
|
379
|
+
buyerReference: cbcText$1(root, "BuyerReference") || void 0,
|
|
380
|
+
orderReference: orderReference ? cbcText$1(orderReference, "ID") : void 0,
|
|
381
|
+
salesOrderId: orderReference ? cbcText$1(orderReference, "SalesOrderID") || void 0 : void 0,
|
|
382
|
+
contractReference: contractReference ? cbcText$1(contractReference, "ID") : void 0,
|
|
383
|
+
projectReference: projectReference ? cbcText$1(projectReference, "ID") : void 0,
|
|
384
|
+
billingReference: parseBillingReference(root),
|
|
385
|
+
seller: parseParty(root, "AccountingSupplierParty"),
|
|
386
|
+
buyer: parseParty(root, "AccountingCustomerParty"),
|
|
387
|
+
delivery: parseDelivery(root),
|
|
388
|
+
lines: parseLines(root, isCreditNote),
|
|
389
|
+
taxSubtotals: parseTaxSubtotals(root),
|
|
390
|
+
monetaryTotal: parseMonetaryTotal(root),
|
|
391
|
+
paymentMeansList,
|
|
392
|
+
paymentMeans: paymentMeansList?.[0],
|
|
393
|
+
invoicePeriod: parseInvoicePeriod(root),
|
|
394
|
+
note: parseNotes(root),
|
|
395
|
+
paymentTermsNote: parsePaymentTermsNote(root),
|
|
396
|
+
attachments,
|
|
397
|
+
documentReferences,
|
|
398
|
+
allowanceCharges: parseAllowanceCharges(root)
|
|
399
|
+
};
|
|
400
|
+
} catch {
|
|
401
|
+
return null;
|
|
402
|
+
}
|
|
430
403
|
}
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
404
|
+
//#endregion
|
|
405
|
+
//#region src/attachments.ts
|
|
406
|
+
/** Approximate decoded byte length of a base64 string. */
|
|
407
|
+
const base64DecodedSize = (base64) => Math.round(base64.length * 3 / 4);
|
|
408
|
+
/**
|
|
409
|
+
* Extract the embedded attachments (typically the human-readable PDF) from a
|
|
410
|
+
* UBL invoice or credit note XML. Attachments referenced only by external URI
|
|
411
|
+
* (no `EmbeddedDocumentBinaryObject`) are skipped; use {@link parseUblInvoice}
|
|
412
|
+
* directly when those matter.
|
|
413
|
+
*/
|
|
414
|
+
const extractUblEmbeddedAttachments = (xml) => {
|
|
415
|
+
const parsed = parseUblInvoice(xml);
|
|
416
|
+
if (!parsed?.attachments || parsed.attachments.length === 0) return [];
|
|
417
|
+
return parsed.attachments.map((attachment, index) => {
|
|
418
|
+
const base64Content = attachment.base64Content?.trim();
|
|
419
|
+
if (!base64Content) return null;
|
|
420
|
+
return {
|
|
421
|
+
id: attachment.id || null,
|
|
422
|
+
filename: attachment.filename || `attachment-${index + 1}`,
|
|
423
|
+
mimeCode: attachment.mimeCode || "application/octet-stream",
|
|
424
|
+
sizeBytes: base64DecodedSize(base64Content),
|
|
425
|
+
lineOrder: index,
|
|
426
|
+
base64Content
|
|
427
|
+
};
|
|
428
|
+
}).filter((entry) => entry !== null);
|
|
449
429
|
};
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
430
|
+
/**
|
|
431
|
+
* Whether an attachment looks like a PDF, by MIME type first and filename
|
|
432
|
+
* extension as a fallback.
|
|
433
|
+
*/
|
|
434
|
+
const isPdfLikeAttachment = (attachment) => {
|
|
435
|
+
const mimeCode = attachment.mimeCode?.toLowerCase() ?? "";
|
|
436
|
+
if (mimeCode === "application/pdf" || mimeCode.endsWith("/pdf")) return true;
|
|
437
|
+
return attachment.filename?.toLowerCase().endsWith(".pdf") ?? false;
|
|
454
438
|
};
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
439
|
+
//#endregion
|
|
440
|
+
//#region src/mlr.ts
|
|
441
|
+
const CBC_NS = "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2";
|
|
442
|
+
const CAC_NS = "urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2";
|
|
443
|
+
/**
|
|
444
|
+
* Peppol BIS MLR (Message Level Response) document-type identifier value
|
|
445
|
+
* (Peppol `busdox-docid-qns` scheme).
|
|
446
|
+
*/
|
|
447
|
+
const PEPPOL_MLR_DOCUMENT_TYPE_VALUE = "urn:oasis:names:specification:ubl:schema:xsd:ApplicationResponse-2::ApplicationResponse##urn:fdc:peppol.eu:poacc:trns:mlr:3::2.1";
|
|
448
|
+
/** Peppol BIS MLR process identifier value. */
|
|
449
|
+
const PEPPOL_MLR_PROCESS_VALUE = "urn:fdc:peppol.eu:poacc:bis:mlr:3";
|
|
450
|
+
/**
|
|
451
|
+
* Strip DOCTYPE declarations to prevent XXE (XML External Entity) attacks.
|
|
452
|
+
* @xmldom/xmldom resolves external entities by default, so we remove DOCTYPE
|
|
453
|
+
* blocks (including inline DTD subsets) before parsing.
|
|
454
|
+
*/
|
|
455
|
+
const stripDoctype = (xml) => xml.replace(/<!DOCTYPE\s[^>[]*(?:\[[^\]]*\])?>/gi, "");
|
|
456
|
+
const parseXmlDocument = (xml) => {
|
|
457
|
+
const safeXml = stripDoctype(xml);
|
|
458
|
+
const BrowserDomParser = globalThis.DOMParser;
|
|
459
|
+
const doc = (BrowserDomParser ? new BrowserDomParser() : new DOMParser()).parseFromString(safeXml, "text/xml");
|
|
460
|
+
if (doc.getElementsByTagName("parsererror").length > 0) return null;
|
|
461
|
+
return doc;
|
|
470
462
|
};
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
463
|
+
const trimmed = (value) => {
|
|
464
|
+
const text = value?.trim();
|
|
465
|
+
return text ? text : null;
|
|
474
466
|
};
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
467
|
+
/** First descendant CBC element's trimmed text, or null. */
|
|
468
|
+
const cbcText = (parent, tag) => trimmed(parent.getElementsByTagNameNS(CBC_NS, tag)[0]?.textContent);
|
|
469
|
+
/**
|
|
470
|
+
* First direct-child CBC element's trimmed text, or null. Used for the
|
|
471
|
+
* top-level scalars so a nested `cbc:ID` (e.g. inside DocumentReference) cannot
|
|
472
|
+
* shadow the ApplicationResponse's own id.
|
|
473
|
+
*/
|
|
474
|
+
const cbcDirectText = (parent, tag) => {
|
|
475
|
+
for (let i = 0; i < parent.childNodes.length; i++) {
|
|
476
|
+
const node = parent.childNodes[i];
|
|
477
|
+
if (!node || node.nodeType !== 1) continue;
|
|
478
|
+
const el = node;
|
|
479
|
+
if (el.namespaceURI === CBC_NS && el.localName === tag) return trimmed(el.textContent);
|
|
480
|
+
}
|
|
481
|
+
return null;
|
|
486
482
|
};
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
483
|
+
/** First descendant CAC element, or null. */
|
|
484
|
+
const cacElement = (parent, tag) => parent.getElementsByTagNameNS(CAC_NS, tag)[0] ?? null;
|
|
485
|
+
/**
|
|
486
|
+
* The Peppol participant identifier of a SenderParty / ReceiverParty, rendered
|
|
487
|
+
* as `schemeID:value` when the EndpointID carries a scheme and the value does
|
|
488
|
+
* not already include one, otherwise the raw value.
|
|
489
|
+
*/
|
|
490
|
+
const endpointIdentifier = (party) => {
|
|
491
|
+
if (!party) return null;
|
|
492
|
+
const endpoint = party.getElementsByTagNameNS(CBC_NS, "EndpointID")[0];
|
|
493
|
+
if (!endpoint) return null;
|
|
494
|
+
const id = trimmed(endpoint.textContent);
|
|
495
|
+
const scheme = endpoint.getAttribute("schemeID");
|
|
496
|
+
if (scheme && id && !id.includes(":")) return `${scheme}:${id}`;
|
|
497
|
+
return id;
|
|
496
498
|
};
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
499
|
+
/**
|
|
500
|
+
* Locate the `ApplicationResponse` element, unwrapping a Standard Business
|
|
501
|
+
* Document (SBDH) envelope when present. Returns null when the XML is not
|
|
502
|
+
* parseable or contains no ApplicationResponse.
|
|
503
|
+
*/
|
|
504
|
+
const findApplicationResponse = (xml) => {
|
|
505
|
+
let doc;
|
|
506
|
+
try {
|
|
507
|
+
doc = parseXmlDocument(xml);
|
|
508
|
+
} catch {
|
|
509
|
+
return null;
|
|
510
|
+
}
|
|
511
|
+
const root = doc?.documentElement;
|
|
512
|
+
if (!root) return null;
|
|
513
|
+
if (root.localName === "ApplicationResponse") return root;
|
|
514
|
+
const candidates = root.getElementsByTagName("*");
|
|
515
|
+
for (let i = 0; i < candidates.length; i++) {
|
|
516
|
+
const el = candidates[i];
|
|
517
|
+
if (el?.localName === "ApplicationResponse") return el;
|
|
518
|
+
}
|
|
519
|
+
return null;
|
|
513
520
|
};
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
521
|
+
/**
|
|
522
|
+
* Whether an inbound document is a Peppol Message Level Response, identified by
|
|
523
|
+
* its document-type value, its process value, or by parsing the XML and finding
|
|
524
|
+
* an `ApplicationResponse` root (SBDH-wrapped or not).
|
|
525
|
+
*/
|
|
526
|
+
const isPeppolMessageLevelResponse = (params) => {
|
|
527
|
+
if (trimmed(params.documentTypeValue) === "urn:oasis:names:specification:ubl:schema:xsd:ApplicationResponse-2::ApplicationResponse##urn:fdc:peppol.eu:poacc:trns:mlr:3::2.1") return true;
|
|
528
|
+
if (trimmed(params.processValue) === "urn:fdc:peppol.eu:poacc:bis:mlr:3") return true;
|
|
529
|
+
const xml = trimmed(params.xml);
|
|
530
|
+
if (!xml) return false;
|
|
531
|
+
return findApplicationResponse(xml) !== null;
|
|
522
532
|
};
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
533
|
+
/**
|
|
534
|
+
* Parse a Peppol Message Level Response (UBL `ApplicationResponse`), unwrapping
|
|
535
|
+
* an SBDH envelope when present. Throws when the XML is not an
|
|
536
|
+
* ApplicationResponse.
|
|
537
|
+
*/
|
|
538
|
+
const parsePeppolMessageLevelResponse = (xml) => {
|
|
539
|
+
const root = findApplicationResponse(xml);
|
|
540
|
+
if (!root) throw new Error("Document is not a Peppol Message Level Response");
|
|
541
|
+
const documentResponse = cacElement(root, "DocumentResponse");
|
|
542
|
+
const response = documentResponse ? cacElement(documentResponse, "Response") : null;
|
|
543
|
+
const documentReference = documentResponse ? cacElement(documentResponse, "DocumentReference") : null;
|
|
544
|
+
return {
|
|
545
|
+
customizationId: cbcDirectText(root, "CustomizationID"),
|
|
546
|
+
profileId: cbcDirectText(root, "ProfileID"),
|
|
547
|
+
responseId: cbcDirectText(root, "ID"),
|
|
548
|
+
issueDate: cbcDirectText(root, "IssueDate"),
|
|
549
|
+
issueTime: cbcDirectText(root, "IssueTime"),
|
|
550
|
+
referencedDocumentId: documentReference ? cbcText(documentReference, "ID") : null,
|
|
551
|
+
responseCode: response ? cbcText(response, "ResponseCode") : null,
|
|
552
|
+
description: response ? cbcText(response, "Description") : null,
|
|
553
|
+
senderIdentifier: endpointIdentifier(cacElement(root, "SenderParty")),
|
|
554
|
+
receiverIdentifier: endpointIdentifier(cacElement(root, "ReceiverParty"))
|
|
555
|
+
};
|
|
543
556
|
};
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
557
|
+
//#endregion
|
|
558
|
+
//#region src/normalize.ts
|
|
559
|
+
const UTF8_BOM = 65279;
|
|
560
|
+
const normalizeText = (value) => {
|
|
561
|
+
if (!value) return null;
|
|
562
|
+
const trimmed = value.trim();
|
|
563
|
+
if (!trimmed) return null;
|
|
564
|
+
const upper = trimmed.toUpperCase();
|
|
565
|
+
if (upper === "NA" || upper === "N/A") return null;
|
|
566
|
+
return trimmed;
|
|
554
567
|
};
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
568
|
+
const toNumberOrNull = (value) => Number.isFinite(value ?? NaN) ? value : null;
|
|
569
|
+
const normalizeCurrency = (value) => {
|
|
570
|
+
if (!value) return null;
|
|
571
|
+
const trimmed = value.trim();
|
|
572
|
+
if (!trimmed) return null;
|
|
573
|
+
return trimmed.toUpperCase();
|
|
561
574
|
};
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
return iso.slice(0, 10);
|
|
575
|
+
const parseDate = (value) => {
|
|
576
|
+
if (!value) return null;
|
|
577
|
+
const trimmed = value.trim();
|
|
578
|
+
if (!trimmed) return null;
|
|
579
|
+
const timestamp = Date.parse(trimmed);
|
|
580
|
+
if (Number.isNaN(timestamp)) return null;
|
|
581
|
+
return new Date(timestamp).toISOString().slice(0, 10);
|
|
570
582
|
};
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
583
|
+
const addressToString = (address) => {
|
|
584
|
+
if (!address) return null;
|
|
585
|
+
const lines = [
|
|
586
|
+
[address.street, address.additionalStreet].filter((part) => part && part.trim().length > 0).join(" "),
|
|
587
|
+
[address.postalZone, address.city].filter((part) => part && part.trim().length > 0).join(" "),
|
|
588
|
+
[address.countrySubentity, address.countryCode].filter((part) => part && part.trim().length > 0).join(" ")
|
|
589
|
+
].filter((line) => line.length > 0);
|
|
590
|
+
return lines.length > 0 ? lines.join("\n") : null;
|
|
578
591
|
};
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
return hasValue ? result : null;
|
|
592
|
+
const addressToStructured = (address) => {
|
|
593
|
+
if (!address) return null;
|
|
594
|
+
const line1 = [address.street, address.additionalStreet].filter((part) => part && part.trim().length > 0).join(" ");
|
|
595
|
+
const result = {
|
|
596
|
+
line1: normalizeText(line1),
|
|
597
|
+
line2: null,
|
|
598
|
+
city: normalizeText(address.city),
|
|
599
|
+
state: normalizeText(address.countrySubentity),
|
|
600
|
+
postal_code: normalizeText(address.postalZone),
|
|
601
|
+
country: normalizeText(address.countryCode)
|
|
602
|
+
};
|
|
603
|
+
return Object.values(result).some((value) => value) ? result : null;
|
|
592
604
|
};
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
605
|
+
const base64ByteLength = (content) => {
|
|
606
|
+
const sanitized = content.replace(/\s+/g, "");
|
|
607
|
+
if (!sanitized) return 0;
|
|
608
|
+
const paddingLength = sanitized.endsWith("==") ? 2 : sanitized.endsWith("=") ? 1 : 0;
|
|
609
|
+
return Math.floor(sanitized.length * 3 / 4) - paddingLength;
|
|
598
610
|
};
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
611
|
+
const sanitizeAttachments = (attachments) => (attachments ?? []).map((attachment) => ({
|
|
612
|
+
id: normalizeText(attachment.id),
|
|
613
|
+
filename: normalizeText(attachment.filename),
|
|
614
|
+
mime_code: normalizeText(attachment.mimeCode),
|
|
615
|
+
description: normalizeText(attachment.description),
|
|
616
|
+
size_bytes: attachment.base64Content ? base64ByteLength(attachment.base64Content) : null,
|
|
617
|
+
external_uri: normalizeText(attachment.externalUri)
|
|
606
618
|
}));
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
619
|
+
const sanitizeAllowanceCharges = (charges) => (charges ?? []).map((charge) => ({
|
|
620
|
+
charge_indicator: charge.chargeIndicator,
|
|
621
|
+
amount: toNumberOrNull(charge.amount),
|
|
622
|
+
base_amount: toNumberOrNull(charge.baseAmount),
|
|
623
|
+
multiplier_factor_numeric: toNumberOrNull(charge.multiplierFactorNumeric),
|
|
624
|
+
reason: normalizeText(charge.reason),
|
|
625
|
+
reason_code: normalizeText(charge.reasonCode),
|
|
626
|
+
tax_percent: toNumberOrNull(charge.taxPercent),
|
|
627
|
+
tax_category_id: normalizeText(charge.taxCategoryId),
|
|
628
|
+
tax_scheme_id: normalizeText(charge.taxSchemeId)
|
|
617
629
|
}));
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
630
|
+
const sanitizeRawUbl = (ubl) => ({
|
|
631
|
+
...ubl,
|
|
632
|
+
attachments: sanitizeAttachments(ubl.attachments),
|
|
633
|
+
documentReferences: ubl.documentReferences ?? []
|
|
622
634
|
});
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
);
|
|
628
|
-
if (ubl.taxSubtotals.length > 0 && Number.isFinite(subtotalSum)) {
|
|
629
|
-
return subtotalSum;
|
|
630
|
-
}
|
|
631
|
-
return ubl.monetaryTotal.taxInclusiveAmount - ubl.monetaryTotal.taxExclusiveAmount;
|
|
635
|
+
const sumTaxTotal = (ubl) => {
|
|
636
|
+
const subtotalSum = ubl.taxSubtotals.reduce((sum, subtotal) => sum + subtotal.taxAmount, 0);
|
|
637
|
+
if (ubl.taxSubtotals.length > 0 && Number.isFinite(subtotalSum)) return subtotalSum;
|
|
638
|
+
return ubl.monetaryTotal.taxInclusiveAmount - ubl.monetaryTotal.taxExclusiveAmount;
|
|
632
639
|
};
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
640
|
+
const sumAllowanceChargesByType = (charges, chargeIndicator) => (charges ?? []).reduce((sum, charge) => {
|
|
641
|
+
if (charge.chargeIndicator !== chargeIndicator) return sum;
|
|
642
|
+
return sum + Math.abs(charge.amount);
|
|
636
643
|
}, 0);
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
644
|
+
/**
|
|
645
|
+
* Decode raw XML bytes, stripping an optional UTF-8 BOM.
|
|
646
|
+
*/
|
|
647
|
+
const decodeXmlBytes = (bytes) => {
|
|
648
|
+
const text = new TextDecoder("utf-8").decode(bytes);
|
|
649
|
+
return text.charCodeAt(0) === UTF8_BOM ? text.slice(1) : text;
|
|
640
650
|
};
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
extracted,
|
|
807
|
-
rawPayload: {
|
|
808
|
-
format: "ubl_xml",
|
|
809
|
-
invoice: sanitizeRawUbl(ubl)
|
|
810
|
-
}
|
|
811
|
-
};
|
|
651
|
+
/**
|
|
652
|
+
* Parse a UBL XML string and normalize it into a flat DTO suitable for
|
|
653
|
+
* database storage or API responses.
|
|
654
|
+
*/
|
|
655
|
+
const normalizeUblResponse = (xml, documentId) => {
|
|
656
|
+
const ubl = parseUblInvoice(xml);
|
|
657
|
+
if (!ubl) throw new Error("Failed to parse UBL invoice XML");
|
|
658
|
+
const subtotal = toNumberOrNull(ubl.monetaryTotal.taxExclusiveAmount) ?? toNumberOrNull(ubl.monetaryTotal.lineExtensionAmount);
|
|
659
|
+
const total = toNumberOrNull(ubl.monetaryTotal.taxInclusiveAmount) ?? toNumberOrNull(ubl.monetaryTotal.payableAmount);
|
|
660
|
+
const amountDue = toNumberOrNull(ubl.monetaryTotal.payableAmount);
|
|
661
|
+
const amountPaid = toNumberOrNull(ubl.monetaryTotal.prepaidAmount) ?? (total !== null && amountDue !== null && total > amountDue ? toNumberOrNull(total - amountDue) : null);
|
|
662
|
+
const taxTotal = toNumberOrNull(sumTaxTotal(ubl));
|
|
663
|
+
const hasHeaderAllowanceCharges = (ubl.allowanceCharges?.length ?? 0) > 0;
|
|
664
|
+
const headerDiscountTotal = sumAllowanceChargesByType(ubl.allowanceCharges, false);
|
|
665
|
+
const headerChargeTotal = sumAllowanceChargesByType(ubl.allowanceCharges, true);
|
|
666
|
+
const currency = normalizeCurrency(ubl.currency);
|
|
667
|
+
const paymentTerms = normalizeText(ubl.paymentTermsNote);
|
|
668
|
+
const poNumber = normalizeText(ubl.orderReference) ?? normalizeText(ubl.salesOrderId);
|
|
669
|
+
const lineItems = ubl.lines.map((line) => ({
|
|
670
|
+
description: normalizeText(line.description) ?? normalizeText(line.itemName) ?? "Line item",
|
|
671
|
+
quantity: toNumberOrNull(line.quantity),
|
|
672
|
+
unit: normalizeText(line.unitCode),
|
|
673
|
+
unit_price: toNumberOrNull(line.unitPrice),
|
|
674
|
+
amount: toNumberOrNull(line.lineExtensionAmount),
|
|
675
|
+
tax_amount: toNumberOrNull(line.taxAmount),
|
|
676
|
+
tax_rate: toNumberOrNull(line.taxPercent),
|
|
677
|
+
product_code: normalizeText(line.sellersItemId) ?? normalizeText(line.buyersItemId),
|
|
678
|
+
discount_amount: toNumberOrNull(line.discountAmount),
|
|
679
|
+
extra: {
|
|
680
|
+
ubl_line_id: normalizeText(line.id),
|
|
681
|
+
item_name: normalizeText(line.itemName),
|
|
682
|
+
sellers_item_id: normalizeText(line.sellersItemId),
|
|
683
|
+
buyers_item_id: normalizeText(line.buyersItemId),
|
|
684
|
+
tax_category_id: normalizeText(line.taxCategoryId),
|
|
685
|
+
tax_scheme_id: normalizeText(line.taxSchemeId),
|
|
686
|
+
tax_subtotals: line.taxSubtotals ?? [],
|
|
687
|
+
allowance_charges: sanitizeAllowanceCharges(line.allowanceCharges),
|
|
688
|
+
charge_amount: toNumberOrNull(line.chargeAmount),
|
|
689
|
+
metadata: line.additionalItemProperties ? Object.fromEntries(line.additionalItemProperties.map((p) => [p.name, p.value])) : null
|
|
690
|
+
}
|
|
691
|
+
}));
|
|
692
|
+
const supplierAddress = addressToString(ubl.seller.address);
|
|
693
|
+
const receiverAddress = addressToString(ubl.buyer.address);
|
|
694
|
+
const supplierAddressStructured = addressToStructured(ubl.seller.address);
|
|
695
|
+
const receiverAddressStructured = addressToStructured(ubl.buyer.address);
|
|
696
|
+
const attachmentMetadata = sanitizeAttachments(ubl.attachments);
|
|
697
|
+
const paymentMeansList = ubl.paymentMeansList && ubl.paymentMeansList.length > 0 ? ubl.paymentMeansList : ubl.paymentMeans ? [ubl.paymentMeans] : [];
|
|
698
|
+
return {
|
|
699
|
+
extracted: {
|
|
700
|
+
provider: "ubl_xml",
|
|
701
|
+
document_id: documentId,
|
|
702
|
+
invoice: {
|
|
703
|
+
invoice_number: normalizeText(ubl.id),
|
|
704
|
+
invoice_date: parseDate(ubl.issueDate),
|
|
705
|
+
due_date: parseDate(ubl.dueDate),
|
|
706
|
+
currency,
|
|
707
|
+
subtotal,
|
|
708
|
+
tax_total: taxTotal,
|
|
709
|
+
total,
|
|
710
|
+
amount_due: amountDue,
|
|
711
|
+
amount_paid: amountPaid,
|
|
712
|
+
discount_total: toNumberOrNull(ubl.monetaryTotal.allowanceTotalAmount) ?? (hasHeaderAllowanceCharges ? toNumberOrNull(headerDiscountTotal) : null),
|
|
713
|
+
shipping_total: toNumberOrNull(ubl.monetaryTotal.chargeTotalAmount) ?? (hasHeaderAllowanceCharges ? toNumberOrNull(headerChargeTotal) : null),
|
|
714
|
+
payment_terms: paymentTerms,
|
|
715
|
+
po_number: poNumber,
|
|
716
|
+
supplier: {
|
|
717
|
+
name: normalizeText(ubl.seller.name) ?? normalizeText(ubl.seller.registrationName),
|
|
718
|
+
address: supplierAddress,
|
|
719
|
+
tax_id: normalizeText(ubl.seller.vatId) ?? normalizeText(ubl.seller.companyId),
|
|
720
|
+
iban: normalizeText(ubl.paymentMeans?.iban),
|
|
721
|
+
bic: normalizeText(ubl.paymentMeans?.bic)
|
|
722
|
+
},
|
|
723
|
+
receiver: {
|
|
724
|
+
name: normalizeText(ubl.buyer.name) ?? normalizeText(ubl.buyer.registrationName),
|
|
725
|
+
address: receiverAddress,
|
|
726
|
+
tax_id: normalizeText(ubl.buyer.vatId) ?? normalizeText(ubl.buyer.companyId)
|
|
727
|
+
},
|
|
728
|
+
extra: {
|
|
729
|
+
document_type: ubl.documentType,
|
|
730
|
+
customization_id: normalizeText(ubl.customizationId),
|
|
731
|
+
profile_id: normalizeText(ubl.profileId),
|
|
732
|
+
invoice_type_code: normalizeText(ubl.invoiceTypeCode),
|
|
733
|
+
buyer_reference: normalizeText(ubl.buyerReference),
|
|
734
|
+
order_reference_id: normalizeText(ubl.orderReference),
|
|
735
|
+
sales_order_id: normalizeText(ubl.salesOrderId),
|
|
736
|
+
contract_reference: normalizeText(ubl.contractReference),
|
|
737
|
+
project_reference: normalizeText(ubl.projectReference),
|
|
738
|
+
billing_reference: ubl.billingReference ? {
|
|
739
|
+
invoice_id: normalizeText(ubl.billingReference.invoiceId),
|
|
740
|
+
invoice_issue_date: parseDate(ubl.billingReference.invoiceIssueDate)
|
|
741
|
+
} : null,
|
|
742
|
+
tax_point_date: parseDate(ubl.taxPointDate),
|
|
743
|
+
invoice_period: ubl.invoicePeriod ?? null,
|
|
744
|
+
note: normalizeText(ubl.note),
|
|
745
|
+
payment_means: ubl.paymentMeans ? {
|
|
746
|
+
code: normalizeText(ubl.paymentMeans.code),
|
|
747
|
+
code_name: normalizeText(ubl.paymentMeans.codeName),
|
|
748
|
+
payment_id: normalizeText(ubl.paymentMeans.paymentId),
|
|
749
|
+
iban: normalizeText(ubl.paymentMeans.iban),
|
|
750
|
+
bic: normalizeText(ubl.paymentMeans.bic),
|
|
751
|
+
account_name: normalizeText(ubl.paymentMeans.accountName),
|
|
752
|
+
mandate_id: normalizeText(ubl.paymentMeans.mandateId)
|
|
753
|
+
} : null,
|
|
754
|
+
payment_means_list: paymentMeansList.map((item) => ({
|
|
755
|
+
code: normalizeText(item.code),
|
|
756
|
+
code_name: normalizeText(item.codeName),
|
|
757
|
+
payment_id: normalizeText(item.paymentId),
|
|
758
|
+
iban: normalizeText(item.iban),
|
|
759
|
+
bic: normalizeText(item.bic),
|
|
760
|
+
account_name: normalizeText(item.accountName),
|
|
761
|
+
mandate_id: normalizeText(item.mandateId)
|
|
762
|
+
})),
|
|
763
|
+
delivery: ubl.delivery ? {
|
|
764
|
+
actual_delivery_date: parseDate(ubl.delivery.actualDeliveryDate),
|
|
765
|
+
address: addressToStructured(ubl.delivery.address)
|
|
766
|
+
} : null,
|
|
767
|
+
supplier: {
|
|
768
|
+
endpoint_id: normalizeText(ubl.seller.endpointId),
|
|
769
|
+
endpoint_scheme_id: normalizeText(ubl.seller.endpointSchemeId),
|
|
770
|
+
registration_name: normalizeText(ubl.seller.registrationName),
|
|
771
|
+
company_legal_form: normalizeText(ubl.seller.companyLegalForm),
|
|
772
|
+
company_id_scheme_id: normalizeText(ubl.seller.companyIdSchemeId),
|
|
773
|
+
tax_scheme_id: normalizeText(ubl.seller.taxSchemeId),
|
|
774
|
+
party_identifications: ubl.seller.partyIdentifications ?? [],
|
|
775
|
+
contact_name: normalizeText(ubl.seller.contact?.name),
|
|
776
|
+
contact_phone: normalizeText(ubl.seller.contact?.phone),
|
|
777
|
+
contact_email: normalizeText(ubl.seller.contact?.email)
|
|
778
|
+
},
|
|
779
|
+
receiver: {
|
|
780
|
+
endpoint_id: normalizeText(ubl.buyer.endpointId),
|
|
781
|
+
endpoint_scheme_id: normalizeText(ubl.buyer.endpointSchemeId),
|
|
782
|
+
registration_name: normalizeText(ubl.buyer.registrationName),
|
|
783
|
+
company_legal_form: normalizeText(ubl.buyer.companyLegalForm),
|
|
784
|
+
company_id_scheme_id: normalizeText(ubl.buyer.companyIdSchemeId),
|
|
785
|
+
tax_scheme_id: normalizeText(ubl.buyer.taxSchemeId),
|
|
786
|
+
party_identifications: ubl.buyer.partyIdentifications ?? [],
|
|
787
|
+
contact_name: normalizeText(ubl.buyer.contact?.name),
|
|
788
|
+
contact_phone: normalizeText(ubl.buyer.contact?.phone),
|
|
789
|
+
contact_email: normalizeText(ubl.buyer.contact?.email)
|
|
790
|
+
},
|
|
791
|
+
supplier_address_structured: supplierAddressStructured,
|
|
792
|
+
receiver_address_structured: receiverAddressStructured,
|
|
793
|
+
tax_subtotals: ubl.taxSubtotals,
|
|
794
|
+
allowance_charges: sanitizeAllowanceCharges(ubl.allowanceCharges),
|
|
795
|
+
attachments: attachmentMetadata,
|
|
796
|
+
document_references: ubl.documentReferences ?? []
|
|
797
|
+
}
|
|
798
|
+
},
|
|
799
|
+
line_items: lineItems,
|
|
800
|
+
confidence: {
|
|
801
|
+
overall: 1,
|
|
802
|
+
fields: {
|
|
803
|
+
invoice_id: 1,
|
|
804
|
+
invoice_date: 1,
|
|
805
|
+
due_date: 1,
|
|
806
|
+
total_amount: 1,
|
|
807
|
+
supplier_name: 1
|
|
808
|
+
}
|
|
809
|
+
}
|
|
810
|
+
},
|
|
811
|
+
rawPayload: {
|
|
812
|
+
format: "ubl_xml",
|
|
813
|
+
invoice: sanitizeRawUbl(ubl)
|
|
814
|
+
}
|
|
815
|
+
};
|
|
812
816
|
};
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
PEPPOL_MLR_PROCESS_VALUE,
|
|
828
|
-
decodeXmlBytes,
|
|
829
|
-
extractUblEmbeddedAttachments,
|
|
830
|
-
isPdfLikeAttachment,
|
|
831
|
-
isPeppolMessageLevelResponse,
|
|
832
|
-
normalizeCurrency,
|
|
833
|
-
normalizeUblResponse,
|
|
834
|
-
parseDate,
|
|
835
|
-
parsePeppolMessageLevelResponse,
|
|
836
|
-
parseUblInvoice,
|
|
837
|
-
parseUblInvoiceDocument
|
|
817
|
+
/**
|
|
818
|
+
* Parse a UBL invoice from raw bytes and normalize it into a DTO.
|
|
819
|
+
*/
|
|
820
|
+
const parseUblInvoiceDocument = (params) => {
|
|
821
|
+
const xml = decodeXmlBytes(params.bytes);
|
|
822
|
+
const { extracted, rawPayload } = normalizeUblResponse(xml, params.documentId);
|
|
823
|
+
return {
|
|
824
|
+
rawPayload: {
|
|
825
|
+
...rawPayload,
|
|
826
|
+
mime_type: params.mimeType
|
|
827
|
+
},
|
|
828
|
+
extracted,
|
|
829
|
+
providerJobId: null
|
|
830
|
+
};
|
|
838
831
|
};
|
|
832
|
+
//#endregion
|
|
833
|
+
export { PEPPOL_MLR_DOCUMENT_TYPE_VALUE, PEPPOL_MLR_PROCESS_VALUE, decodeXmlBytes, extractUblEmbeddedAttachments, isPdfLikeAttachment, isPeppolMessageLevelResponse, normalizeCurrency, normalizeUblResponse, parseDate, parsePeppolMessageLevelResponse, parseUblInvoice, parseUblInvoiceDocument };
|
|
834
|
+
|
|
839
835
|
//# sourceMappingURL=index.js.map
|