@financica/ubl 0.16.0 → 0.18.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/index.js CHANGED
@@ -1,20 +1,5 @@
1
+ import { n as UblParseError, t as UblBuildError } from "./errors-C9YYJ1xR.js";
1
2
  import { DOMParser } from "@xmldom/xmldom";
2
- //#region src/errors.ts
3
- /**
4
- * Thrown by the parse side of this package when input is broken: malformed
5
- * XML, or a UBL document missing an element EN 16931 makes mandatory. A parser
6
- * returns `null` instead when the input is well-formed but not the document
7
- * type it parses.
8
- */
9
- var UblParseError = class extends Error {
10
- cause;
11
- constructor(message, options) {
12
- super(message);
13
- this.name = "UblParseError";
14
- if (options && "cause" in options) this.cause = options.cause;
15
- }
16
- };
17
- //#endregion
18
3
  //#region src/xml-dom.ts
19
4
  const CBC_NS = "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2";
20
5
  const CAC_NS = "urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2";
@@ -90,6 +75,15 @@ function cbcDirectNumber(parent, tag) {
90
75
  return toNumber(cbcDirectText$1(parent, tag));
91
76
  }
92
77
  const attribute = (el, name) => trimmed$1(el?.getAttribute(name));
78
+ /** A direct `cbc:<tag>` child with its `@schemeID`. */
79
+ function cbcDirectSchemedId(parent, tag) {
80
+ const element = childElementsByTagNs(parent, CBC_NS, tag)[0];
81
+ const value = trimmed$1(element?.textContent);
82
+ return value !== void 0 ? {
83
+ value,
84
+ scheme: attribute(element, "schemeID")
85
+ } : void 0;
86
+ }
93
87
  function cacDirectElement(parent, tag) {
94
88
  return childElementsByTagNs(parent, "urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2", tag)[0] ?? null;
95
89
  }
@@ -144,9 +138,21 @@ function parsePartyIdentifications(party) {
144
138
  return result;
145
139
  }
146
140
  function parseParty(root, role) {
147
- const wrapper = cacElement$1(root, role);
141
+ const wrapper = cacDirectElement(root, role);
148
142
  const party = wrapper ? cacElement$1(wrapper, "Party") : null;
149
- if (!party) return {};
143
+ return party ? parsePartyElement(party) : {};
144
+ }
145
+ /**
146
+ * `cac:PayeeParty` (BG-10) and `cac:TaxRepresentativeParty` (BG-11) carry the
147
+ * party children directly, without a `cac:Party` wrapper.
148
+ */
149
+ function parseUnwrappedParty(root, tag) {
150
+ const party = cacDirectElement(root, tag);
151
+ if (!party) return void 0;
152
+ const parsed = parsePartyElement(party);
153
+ return Object.values(parsed).some((value) => value !== void 0) ? parsed : void 0;
154
+ }
155
+ function parsePartyElement(party) {
150
156
  const partyName = cacElement$1(party, "PartyName");
151
157
  const legalEntity = cacElement$1(party, "PartyLegalEntity");
152
158
  const taxScheme = cacElement$1(party, "PartyTaxScheme");
@@ -154,39 +160,68 @@ function parseParty(root, role) {
154
160
  const endpointEl = party.getElementsByTagNameNS(CBC_NS, "EndpointID")[0];
155
161
  const partyIdentifications = parsePartyIdentifications(party);
156
162
  const companyIdEl = legalEntity ? legalEntity.getElementsByTagNameNS(CBC_NS, "CompanyID")[0] : null;
163
+ const endpointValue = trimmed$1(endpointEl?.textContent);
164
+ const companyIdValue = companyIdEl ? trimmed$1(companyIdEl.textContent) : partyId ? cbcText$1(partyId, "ID") : void 0;
157
165
  return {
158
166
  name: partyName ? cbcText$1(partyName, "Name") : void 0,
159
167
  registrationName: legalEntity ? cbcText$1(legalEntity, "RegistrationName") : void 0,
160
168
  companyLegalForm: legalEntity ? cbcText$1(legalEntity, "CompanyLegalForm") : void 0,
161
169
  vatId: taxScheme ? cbcText$1(taxScheme, "CompanyID") : void 0,
162
170
  taxSchemeId: parseTaxSchemeId(taxScheme),
163
- companyId: companyIdEl ? trimmed$1(companyIdEl.textContent) : partyId ? cbcText$1(partyId, "ID") : void 0,
164
- companyIdSchemeId: attribute(companyIdEl, "schemeID"),
165
- endpointId: trimmed$1(endpointEl?.textContent),
166
- endpointSchemeId: attribute(endpointEl, "schemeID"),
171
+ companyId: companyIdValue !== void 0 ? {
172
+ value: companyIdValue,
173
+ scheme: attribute(companyIdEl, "schemeID")
174
+ } : void 0,
175
+ endpoint: endpointValue !== void 0 ? {
176
+ value: endpointValue,
177
+ scheme: attribute(endpointEl, "schemeID")
178
+ } : void 0,
167
179
  partyIdentifications: partyIdentifications.length > 0 ? partyIdentifications : void 0,
168
180
  address: parseAddress(party),
169
181
  contact: parseContact(party)
170
182
  };
171
183
  }
184
+ /**
185
+ * `cac:TaxCategory` / `cac:ClassifiedTaxCategory`. Emitted whenever the
186
+ * element states anything; `percentFallback` covers the E-FFF habit of putting
187
+ * `cbc:Percent` on the subtotal instead of inside the category.
188
+ */
189
+ function parseTaxCategory(cat, percentFallback) {
190
+ const percentFromParent = percentFallback ? cbcDirectNumber(percentFallback, "Percent") : void 0;
191
+ if (!cat) return percentFromParent !== void 0 ? { percent: percentFromParent } : void 0;
192
+ const category = {
193
+ id: cbcText$1(cat, "ID"),
194
+ percent: cbcNumber(cat, "Percent") ?? percentFromParent,
195
+ schemeId: parseTaxSchemeId(cat),
196
+ exemptionReason: cbcText$1(cat, "TaxExemptionReason"),
197
+ exemptionReasonCode: cbcText$1(cat, "TaxExemptionReasonCode")
198
+ };
199
+ return Object.values(category).some((value) => value !== void 0) ? category : void 0;
200
+ }
172
201
  function parseTaxSubtotal(sub) {
173
- const cat = cacElement$1(sub, "TaxCategory");
174
202
  return {
175
203
  taxableAmount: cbcNumber(sub, "TaxableAmount"),
176
204
  taxAmount: cbcNumber(sub, "TaxAmount"),
177
- taxPercent: (cat ? cbcNumber(cat, "Percent") : void 0) ?? cbcNumber(sub, "Percent"),
178
- taxCategoryId: cat ? cbcText$1(cat, "ID") : void 0,
179
- taxSchemeId: parseTaxSchemeId(cat),
180
- taxExemptionReason: cat ? cbcText$1(cat, "TaxExemptionReason") : void 0
205
+ category: parseTaxCategory(cacElement$1(sub, "TaxCategory"), sub)
181
206
  };
182
207
  }
183
208
  function parseTaxSubtotalsFromTaxTotal(taxTotal) {
184
209
  return cacDirectElements(taxTotal, "TaxSubtotal").map(parseTaxSubtotal);
185
210
  }
186
- function parseTaxSubtotals(root) {
211
+ /**
212
+ * `cac:TaxTotal` (BG-22). BT-110 is read from the first `cac:TaxTotal` as
213
+ * stated, never summed from the subtotals; the subtotals of every `TaxTotal`
214
+ * (a second one carries the accounting-currency figure) are concatenated.
215
+ */
216
+ function parseTaxTotal(root) {
187
217
  const taxTotals = cacDirectElements(root, "TaxTotal");
188
- if (taxTotals.length === 0) return [];
189
- return taxTotals.flatMap(parseTaxSubtotalsFromTaxTotal);
218
+ const first = taxTotals[0];
219
+ const second = taxTotals[1];
220
+ return {
221
+ taxAmount: first ? cbcDirectNumber(first, "TaxAmount") : void 0,
222
+ subtotals: taxTotals.flatMap(parseTaxSubtotalsFromTaxTotal),
223
+ taxAmountInTaxCurrency: second ? cbcDirectNumber(second, "TaxAmount") : void 0
224
+ };
190
225
  }
191
226
  function parseAllowanceCharge(charge) {
192
227
  const taxCategory = cacElement$1(charge, "TaxCategory");
@@ -198,21 +233,30 @@ function parseAllowanceCharge(charge) {
198
233
  multiplierFactorNumeric: cbcDirectNumber(charge, "MultiplierFactorNumeric"),
199
234
  reason: cbcText$1(charge, "AllowanceChargeReason"),
200
235
  reasonCode: cbcText$1(charge, "AllowanceChargeReasonCode"),
201
- taxPercent: taxCategory ? cbcNumber(taxCategory, "Percent") : void 0,
202
- taxCategoryId: taxCategory ? cbcText$1(taxCategory, "ID") : void 0,
203
- taxSchemeId: parseTaxSchemeId(taxCategory)
236
+ taxCategory: parseTaxCategory(taxCategory)
204
237
  };
205
238
  }
206
239
  function parseAllowanceCharges(parent) {
207
240
  return cacDirectElements(parent, "AllowanceCharge").map(parseAllowanceCharge);
208
241
  }
242
+ function mergeTaxCategories(preferred, fallback) {
243
+ if (!preferred) return fallback;
244
+ if (!fallback) return preferred;
245
+ return {
246
+ id: preferred.id ?? fallback.id,
247
+ percent: preferred.percent ?? fallback.percent,
248
+ schemeId: preferred.schemeId ?? fallback.schemeId,
249
+ exemptionReason: preferred.exemptionReason ?? fallback.exemptionReason,
250
+ exemptionReasonCode: preferred.exemptionReasonCode ?? fallback.exemptionReasonCode
251
+ };
252
+ }
209
253
  function parseLines(root, isCreditNote) {
210
254
  const qtyTag = isCreditNote ? "CreditedQuantity" : "InvoicedQuantity";
211
255
  const lineTag = isCreditNote ? "CreditNoteLine" : "InvoiceLine";
212
256
  return cacElements(root, lineTag).map((line) => {
213
257
  const item = cacElement$1(line, "Item");
214
258
  const price = cacElement$1(line, "Price");
215
- const taxCategory = item ? cacElement$1(item, "ClassifiedTaxCategory") : null;
259
+ const classifiedCategory = parseTaxCategory(item ? cacElement$1(item, "ClassifiedTaxCategory") : null);
216
260
  const sellersId = item ? cacElement$1(item, "SellersItemIdentification") : null;
217
261
  const buyersId = item ? cacElement$1(item, "BuyersItemIdentification") : null;
218
262
  const qtyEl = line.getElementsByTagNameNS(CBC_NS, qtyTag)[0];
@@ -221,12 +265,18 @@ function parseLines(root, isCreditNote) {
221
265
  const lineTaxSubtotals = lineTaxTotal ? parseTaxSubtotalsFromTaxTotal(lineTaxTotal) : [];
222
266
  const taxAmountFromSubtotals = lineTaxSubtotals.reduce((sum, subtotal) => sum + (subtotal.taxAmount ?? 0), 0);
223
267
  const taxAmountFromLineTotal = lineTaxTotal ? cbcDirectNumber(lineTaxTotal, "TaxAmount") : void 0;
224
- const taxPercent = lineTaxSubtotals[0]?.taxPercent ?? (taxCategory ? cbcNumber(taxCategory, "Percent") : void 0);
268
+ const subtotalCategory = lineTaxSubtotals[0]?.category;
269
+ const taxCategory = mergeTaxCategories(subtotalCategory, classifiedCategory);
270
+ const taxPercent = taxCategory?.percent;
225
271
  const computedTaxAmount = taxPercent !== void 0 && lineExtensionAmount !== void 0 ? Number((lineExtensionAmount * taxPercent / 100).toFixed(2)) : void 0;
226
- const lineAllowanceCharges = parseAllowanceCharges(line);
227
- const priceAllowanceCharges = price ? parseAllowanceCharges(price) : [];
228
- const allowanceCharges = [...lineAllowanceCharges, ...priceAllowanceCharges];
229
- const discountAmount = allowanceCharges.reduce((sum, charge) => charge.chargeIndicator ? sum : sum + Math.abs(charge.amount ?? 0), 0);
272
+ const allowanceCharges = parseAllowanceCharges(line);
273
+ const priceAllowanceElement = price ? parseAllowanceCharges(price).find((charge) => !charge.chargeIndicator) : void 0;
274
+ const priceAllowance = priceAllowanceElement ? {
275
+ amount: priceAllowanceElement.amount,
276
+ baseAmount: priceAllowanceElement.baseAmount,
277
+ reason: priceAllowanceElement.reason
278
+ } : void 0;
279
+ const discountAmount = allowanceCharges.reduce((sum, charge) => charge.chargeIndicator ? sum : sum + Math.abs(charge.amount ?? 0), 0) + Math.abs(priceAllowance?.amount ?? 0);
230
280
  const chargeAmount = allowanceCharges.reduce((sum, charge) => charge.chargeIndicator ? sum + Math.abs(charge.amount ?? 0) : sum, 0);
231
281
  const additionalItemProperties = [];
232
282
  if (item) for (const prop of cacElements(item, "AdditionalItemProperty")) {
@@ -240,24 +290,46 @@ function parseLines(root, isCreditNote) {
240
290
  const priceAmount = price ? cbcNumber(price, "PriceAmount") : void 0;
241
291
  const priceBaseQuantity = price ? cbcDirectNumber(price, "BaseQuantity") : void 0;
242
292
  const unitPrice = priceAmount !== void 0 && priceBaseQuantity && priceBaseQuantity > 0 ? priceAmount / priceBaseQuantity : priceAmount;
293
+ const orderLineReference = cacDirectElement(line, "OrderLineReference");
294
+ const lineDocumentReference = cacDirectElements(line, "DocumentReference").find((reference) => cbcDirectText$1(reference, "DocumentTypeCode") === "130");
295
+ const standardId = item ? cacDirectElement(item, "StandardItemIdentification") : null;
296
+ const originCountry = item ? cacDirectElement(item, "OriginCountry") : null;
297
+ const commodityClassifications = [];
298
+ for (const classification of item ? cacDirectElements(item, "CommodityClassification") : []) {
299
+ const codeElement = childElementsByTagNs(classification, CBC_NS, "ItemClassificationCode")[0];
300
+ const value = trimmed$1(codeElement?.textContent);
301
+ if (value === void 0) continue;
302
+ commodityClassifications.push({
303
+ value,
304
+ listId: attribute(codeElement, "listID"),
305
+ listVersionId: attribute(codeElement, "listVersionID")
306
+ });
307
+ }
243
308
  return {
244
309
  id: requiredCbcText(line, "ID", `${lineTag} identifier`),
310
+ note: cbcDirectText$1(line, "Note"),
311
+ objectIdentifier: lineDocumentReference ? cbcDirectSchemedId(lineDocumentReference, "ID") : void 0,
312
+ orderLineReference: orderLineReference ? cbcDirectText$1(orderLineReference, "LineID") : void 0,
313
+ accountingCost: cbcDirectText$1(line, "AccountingCost"),
245
314
  description: item ? cbcText$1(item, "Description") : void 0,
246
315
  quantity: cbcNumber(line, qtyTag),
247
316
  unitCode: attribute(qtyEl, "unitCode"),
248
317
  unitPrice,
249
318
  lineExtensionAmount,
250
- taxPercent,
319
+ baseQuantity: priceBaseQuantity,
320
+ taxCategory,
251
321
  taxAmount: taxAmountFromLineTotal ?? (lineTaxSubtotals.length > 0 ? taxAmountFromSubtotals : computedTaxAmount),
252
- taxCategoryId: lineTaxSubtotals[0]?.taxCategoryId ?? (taxCategory ? cbcText$1(taxCategory, "ID") : void 0),
253
- taxSchemeId: lineTaxSubtotals[0]?.taxSchemeId ?? parseTaxSchemeId(taxCategory),
254
322
  taxSubtotals: lineTaxSubtotals.length > 0 ? lineTaxSubtotals : void 0,
255
323
  allowanceCharges: allowanceCharges.length > 0 ? allowanceCharges : void 0,
324
+ priceAllowance,
256
325
  discountAmount: discountAmount > 0 ? discountAmount : void 0,
257
326
  chargeAmount: chargeAmount > 0 ? chargeAmount : void 0,
258
327
  itemName: item ? cbcText$1(item, "Name") : void 0,
259
328
  sellersItemId: sellersId ? cbcText$1(sellersId, "ID") : void 0,
260
329
  buyersItemId: buyersId ? cbcText$1(buyersId, "ID") : void 0,
330
+ standardItemId: standardId ? cbcDirectSchemedId(standardId, "ID") : void 0,
331
+ commodityClassifications: commodityClassifications.length > 0 ? commodityClassifications : void 0,
332
+ originCountryCode: originCountry ? cbcDirectText$1(originCountry, "IdentificationCode") : void 0,
261
333
  additionalItemProperties: additionalItemProperties.length > 0 ? additionalItemProperties : void 0,
262
334
  invoicePeriod: parseInvoicePeriod(line)
263
335
  };
@@ -282,6 +354,8 @@ function parsePaymentMeansElement(pm) {
282
354
  const branch = account ? cacElement$1(account, "FinancialInstitutionBranch") : null;
283
355
  const paymentMeansCodeEl = pm.getElementsByTagNameNS(CBC_NS, "PaymentMeansCode")[0];
284
356
  const mandate = cacElement$1(pm, "PaymentMandate");
357
+ const debitedAccount = mandate ? cacElement$1(mandate, "PayerFinancialAccount") : null;
358
+ const card = cacElement$1(pm, "CardAccount");
285
359
  return {
286
360
  code: cbcText$1(pm, "PaymentMeansCode"),
287
361
  codeName: attribute(paymentMeansCodeEl, "name"),
@@ -289,7 +363,10 @@ function parsePaymentMeansElement(pm) {
289
363
  iban: account ? cbcText$1(account, "ID") : void 0,
290
364
  bic: branch ? cbcText$1(branch, "ID") : void 0,
291
365
  accountName: account ? cbcText$1(account, "Name") : void 0,
292
- mandateId: mandate ? cbcText$1(mandate, "ID") : void 0
366
+ mandateId: mandate ? cbcDirectText$1(mandate, "ID") : void 0,
367
+ debitedAccount: debitedAccount ? cbcText$1(debitedAccount, "ID") : void 0,
368
+ cardNumber: card ? cbcText$1(card, "PrimaryAccountNumberID") : void 0,
369
+ cardHolder: card ? cbcText$1(card, "HolderName") : void 0
293
370
  };
294
371
  }
295
372
  function parsePaymentMeansList(root) {
@@ -318,11 +395,16 @@ function parseDelivery(root) {
318
395
  if (!delivery) return void 0;
319
396
  const actualDeliveryDate = cbcText$1(delivery, "ActualDeliveryDate");
320
397
  const deliveryLocation = cacElement$1(delivery, "DeliveryLocation");
398
+ const locationId = deliveryLocation ? cbcDirectSchemedId(deliveryLocation, "ID") : void 0;
321
399
  const address = parseAddressFromElement(deliveryLocation ? cacElement$1(deliveryLocation, "Address") : null);
322
- if (!actualDeliveryDate && !address) return void 0;
400
+ const deliveryParty = cacElement$1(delivery, "DeliveryParty");
401
+ const partyName = deliveryParty ? cbcText$1(deliveryParty, "Name") : void 0;
402
+ if (!actualDeliveryDate && !locationId && !address && !partyName) return void 0;
323
403
  return {
324
404
  actualDeliveryDate,
325
- address
405
+ locationId,
406
+ address,
407
+ partyName
326
408
  };
327
409
  }
328
410
  function parsePaymentTermsNote(root) {
@@ -343,14 +425,31 @@ function parseNotes(root) {
343
425
  }
344
426
  return notes.length > 0 ? notes.join("\n") : void 0;
345
427
  }
428
+ /**
429
+ * `cac:AdditionalDocumentReference` (BG-24), plus the two references EN 16931
430
+ * types by `cbc:DocumentTypeCode`: 130 is the invoiced object (BT-18) and 50
431
+ * is the tender or lot (BT-17), which on a credit note stands in for the
432
+ * project reference (BT-11) UBL's CreditNote has no element for.
433
+ */
346
434
  function parseAttachments(root) {
347
- const refs = cacElements(root, "AdditionalDocumentReference");
435
+ const refs = cacDirectElements(root, "AdditionalDocumentReference");
348
436
  const attachments = [];
349
437
  const documentReferences = [];
438
+ let invoicedObjectId;
439
+ let typeCode50Reference;
350
440
  for (const ref of refs) {
351
441
  const attachment = cacElement$1(ref, "Attachment");
352
442
  const refId = cbcText$1(ref, "ID");
353
443
  const description = cbcText$1(ref, "DocumentDescription");
444
+ const typeCode = cbcDirectText$1(ref, "DocumentTypeCode");
445
+ if (typeCode === "130" && !attachment) {
446
+ invoicedObjectId ??= cbcDirectSchemedId(ref, "ID");
447
+ continue;
448
+ }
449
+ if (typeCode === "50" && !attachment) {
450
+ typeCode50Reference ??= refId;
451
+ continue;
452
+ }
354
453
  if (!attachment) {
355
454
  if (refId || description) documentReferences.push({
356
455
  id: refId,
@@ -380,7 +479,9 @@ function parseAttachments(root) {
380
479
  }
381
480
  return {
382
481
  attachments: attachments.length > 0 ? attachments : void 0,
383
- documentReferences: documentReferences.length > 0 ? documentReferences : void 0
482
+ documentReferences: documentReferences.length > 0 ? documentReferences : void 0,
483
+ invoicedObjectId,
484
+ typeCode50Reference
384
485
  };
385
486
  }
386
487
  function parseBillingReference(root) {
@@ -432,9 +533,15 @@ function parseUblInvoice(xml) {
432
533
  const currency = requiredCbcText(root, "DocumentCurrencyCode", "currency code");
433
534
  const paymentMeansList = parsePaymentMeansList(root);
434
535
  const orderReference = cacElement$1(root, "OrderReference");
435
- const contractReference = cacElement$1(root, "ContractDocumentReference");
436
- const projectReference = cacElement$1(root, "ProjectReference");
437
- const { attachments, documentReferences } = parseAttachments(root);
536
+ const contractReference = cacDirectElement(root, "ContractDocumentReference");
537
+ const projectReference = cacDirectElement(root, "ProjectReference");
538
+ const despatchReference = cacDirectElement(root, "DespatchDocumentReference");
539
+ const receiptReference = cacDirectElement(root, "ReceiptDocumentReference");
540
+ const { attachments, documentReferences, invoicedObjectId, typeCode50Reference } = parseAttachments(root);
541
+ const paymentDueDate = (() => {
542
+ const first = cacDirectElements(root, "PaymentMeans")[0];
543
+ return first ? cbcDirectText$1(first, "PaymentDueDate") : void 0;
544
+ })();
438
545
  return omitUndefined({
439
546
  documentType,
440
547
  customizationId: cbcText$1(root, "CustomizationID"),
@@ -442,20 +549,28 @@ function parseUblInvoice(xml) {
442
549
  id,
443
550
  invoiceTypeCode: cbcText$1(root, "InvoiceTypeCode") ?? cbcText$1(root, "CreditNoteTypeCode"),
444
551
  issueDate,
445
- dueDate: cbcText$1(root, "DueDate"),
446
- taxPointDate: cbcText$1(root, "TaxPointDate"),
552
+ dueDate: cbcDirectText$1(root, "DueDate") ?? paymentDueDate,
553
+ taxPointDate: cbcDirectText$1(root, "TaxPointDate"),
447
554
  currency,
555
+ taxCurrency: cbcDirectText$1(root, "TaxCurrencyCode"),
556
+ accountingCost: cbcDirectText$1(root, "AccountingCost"),
448
557
  buyerReference: cbcText$1(root, "BuyerReference"),
449
558
  orderReference: orderReference ? cbcText$1(orderReference, "ID") : void 0,
450
559
  salesOrderId: orderReference ? cbcText$1(orderReference, "SalesOrderID") : void 0,
451
560
  contractReference: contractReference ? cbcText$1(contractReference, "ID") : void 0,
452
- projectReference: projectReference ? cbcText$1(projectReference, "ID") : void 0,
561
+ projectReference: projectReference ? cbcText$1(projectReference, "ID") : isCreditNote ? typeCode50Reference : void 0,
562
+ receivingAdviceReference: receiptReference ? cbcDirectText$1(receiptReference, "ID") : void 0,
563
+ despatchReference: despatchReference ? cbcDirectText$1(despatchReference, "ID") : void 0,
564
+ tenderReference: isCreditNote ? void 0 : typeCode50Reference,
565
+ invoicedObjectId,
453
566
  billingReference: parseBillingReference(root),
454
567
  seller: parseParty(root, "AccountingSupplierParty"),
455
568
  buyer: parseParty(root, "AccountingCustomerParty"),
569
+ payee: parseUnwrappedParty(root, "PayeeParty"),
570
+ taxRepresentative: parseUnwrappedParty(root, "TaxRepresentativeParty"),
456
571
  delivery: parseDelivery(root),
457
572
  lines: parseLines(root, isCreditNote),
458
- taxSubtotals: parseTaxSubtotals(root),
573
+ taxTotal: parseTaxTotal(root),
459
574
  monetaryTotal: parseMonetaryTotal(root),
460
575
  paymentMeansList,
461
576
  paymentMeans: paymentMeansList?.[0],
@@ -685,9 +800,9 @@ const sanitizeAllowanceCharges = (charges) => (charges ?? []).map((charge) => ({
685
800
  multiplier_factor_numeric: toNumberOrNull(charge.multiplierFactorNumeric),
686
801
  reason: normalizeText(charge.reason),
687
802
  reason_code: normalizeText(charge.reasonCode),
688
- tax_percent: toNumberOrNull(charge.taxPercent),
689
- tax_category_id: normalizeText(charge.taxCategoryId),
690
- tax_scheme_id: normalizeText(charge.taxSchemeId)
803
+ tax_percent: toNumberOrNull(charge.taxCategory?.percent),
804
+ tax_category_id: normalizeText(charge.taxCategory?.id),
805
+ tax_scheme_id: normalizeText(charge.taxCategory?.schemeId)
691
806
  }));
692
807
  const sanitizeRawUbl = (ubl) => ({
693
808
  ...ubl,
@@ -695,8 +810,9 @@ const sanitizeRawUbl = (ubl) => ({
695
810
  documentReferences: ubl.documentReferences ?? []
696
811
  });
697
812
  const sumTaxTotal = (ubl) => {
698
- const subtotalSum = ubl.taxSubtotals.reduce((sum, subtotal) => sum + (subtotal.taxAmount ?? 0), 0);
699
- if (ubl.taxSubtotals.length > 0 && Number.isFinite(subtotalSum)) return subtotalSum;
813
+ const { subtotals } = ubl.taxTotal;
814
+ const subtotalSum = subtotals.reduce((sum, subtotal) => sum + (subtotal.taxAmount ?? 0), 0);
815
+ if (subtotals.length > 0 && Number.isFinite(subtotalSum)) return subtotalSum;
700
816
  const { taxInclusiveAmount, taxExclusiveAmount } = ubl.monetaryTotal;
701
817
  if (taxInclusiveAmount === void 0 || taxExclusiveAmount === void 0) return null;
702
818
  return taxInclusiveAmount - taxExclusiveAmount;
@@ -743,7 +859,7 @@ const normalizeUblResponse = (xml, documentId) => {
743
859
  unit_price: toNumberOrNull(line.unitPrice),
744
860
  amount: toNumberOrNull(line.lineExtensionAmount),
745
861
  tax_amount: toNumberOrNull(line.taxAmount),
746
- tax_rate: toNumberOrNull(line.taxPercent),
862
+ tax_rate: toNumberOrNull(line.taxCategory?.percent),
747
863
  product_code: normalizeText(line.sellersItemId) ?? normalizeText(line.buyersItemId),
748
864
  discount_amount: toNumberOrNull(line.discountAmount),
749
865
  period_start: parseDate(line.invoicePeriod?.startDate),
@@ -753,10 +869,13 @@ const normalizeUblResponse = (xml, documentId) => {
753
869
  item_name: normalizeText(line.itemName),
754
870
  sellers_item_id: normalizeText(line.sellersItemId),
755
871
  buyers_item_id: normalizeText(line.buyersItemId),
756
- tax_category_id: normalizeText(line.taxCategoryId),
757
- tax_scheme_id: normalizeText(line.taxSchemeId),
872
+ tax_category_id: normalizeText(line.taxCategory?.id),
873
+ tax_scheme_id: normalizeText(line.taxCategory?.schemeId),
758
874
  tax_subtotals: line.taxSubtotals ?? [],
759
- allowance_charges: sanitizeAllowanceCharges(line.allowanceCharges),
875
+ allowance_charges: sanitizeAllowanceCharges([...line.allowanceCharges ?? [], ...line.priceAllowance ? [{
876
+ chargeIndicator: false,
877
+ ...line.priceAllowance
878
+ }] : []]),
760
879
  charge_amount: toNumberOrNull(line.chargeAmount),
761
880
  metadata: line.additionalItemProperties ? Object.fromEntries(line.additionalItemProperties.map((p) => [p.name, p.value])) : null
762
881
  }
@@ -791,14 +910,14 @@ const normalizeUblResponse = (xml, documentId) => {
791
910
  supplier: {
792
911
  name: normalizeText(ubl.seller.name) ?? normalizeText(ubl.seller.registrationName),
793
912
  address: supplierAddress,
794
- tax_id: normalizeText(ubl.seller.vatId) ?? normalizeText(ubl.seller.companyId),
913
+ tax_id: normalizeText(ubl.seller.vatId) ?? normalizeText(ubl.seller.companyId?.value),
795
914
  iban: normalizeText(ubl.paymentMeans?.iban),
796
915
  bic: normalizeText(ubl.paymentMeans?.bic)
797
916
  },
798
917
  receiver: {
799
918
  name: normalizeText(ubl.buyer.name) ?? normalizeText(ubl.buyer.registrationName),
800
919
  address: receiverAddress,
801
- tax_id: normalizeText(ubl.buyer.vatId) ?? normalizeText(ubl.buyer.companyId)
920
+ tax_id: normalizeText(ubl.buyer.vatId) ?? normalizeText(ubl.buyer.companyId?.value)
802
921
  },
803
922
  extra: {
804
923
  document_type: ubl.documentType,
@@ -824,11 +943,11 @@ const normalizeUblResponse = (xml, documentId) => {
824
943
  address: addressToStructured(ubl.delivery.address)
825
944
  } : null,
826
945
  supplier: {
827
- endpoint_id: normalizeText(ubl.seller.endpointId),
828
- endpoint_scheme_id: normalizeText(ubl.seller.endpointSchemeId),
946
+ endpoint_id: normalizeText(ubl.seller.endpoint?.value),
947
+ endpoint_scheme_id: normalizeText(ubl.seller.endpoint?.scheme),
829
948
  registration_name: normalizeText(ubl.seller.registrationName),
830
949
  company_legal_form: normalizeText(ubl.seller.companyLegalForm),
831
- company_id_scheme_id: normalizeText(ubl.seller.companyIdSchemeId),
950
+ company_id_scheme_id: normalizeText(ubl.seller.companyId?.scheme),
832
951
  tax_scheme_id: normalizeText(ubl.seller.taxSchemeId),
833
952
  party_identifications: ubl.seller.partyIdentifications ?? [],
834
953
  contact_name: normalizeText(ubl.seller.contact?.name),
@@ -836,11 +955,11 @@ const normalizeUblResponse = (xml, documentId) => {
836
955
  contact_email: normalizeText(ubl.seller.contact?.email)
837
956
  },
838
957
  receiver: {
839
- endpoint_id: normalizeText(ubl.buyer.endpointId),
840
- endpoint_scheme_id: normalizeText(ubl.buyer.endpointSchemeId),
958
+ endpoint_id: normalizeText(ubl.buyer.endpoint?.value),
959
+ endpoint_scheme_id: normalizeText(ubl.buyer.endpoint?.scheme),
841
960
  registration_name: normalizeText(ubl.buyer.registrationName),
842
961
  company_legal_form: normalizeText(ubl.buyer.companyLegalForm),
843
- company_id_scheme_id: normalizeText(ubl.buyer.companyIdSchemeId),
962
+ company_id_scheme_id: normalizeText(ubl.buyer.companyId?.scheme),
844
963
  tax_scheme_id: normalizeText(ubl.buyer.taxSchemeId),
845
964
  party_identifications: ubl.buyer.partyIdentifications ?? [],
846
965
  contact_name: normalizeText(ubl.buyer.contact?.name),
@@ -849,7 +968,7 @@ const normalizeUblResponse = (xml, documentId) => {
849
968
  },
850
969
  supplier_address_structured: supplierAddressStructured,
851
970
  receiver_address_structured: receiverAddressStructured,
852
- tax_subtotals: ubl.taxSubtotals,
971
+ tax_subtotals: ubl.taxTotal.subtotals,
853
972
  allowance_charges: sanitizeAllowanceCharges(ubl.allowanceCharges),
854
973
  attachments: attachmentMetadata,
855
974
  document_references: ubl.documentReferences ?? []
@@ -889,6 +1008,6 @@ const parseUblInvoiceDocument = (params) => {
889
1008
  };
890
1009
  };
891
1010
  //#endregion
892
- export { PEPPOL_MLR_DOCUMENT_TYPE_VALUE, PEPPOL_MLR_PROCESS_VALUE, UblParseError, decodeXmlBytes, extractUblEmbeddedAttachments, isPdfLikeAttachment, isPeppolMessageLevelResponse, normalizeCurrency, normalizeUblResponse, parseDate, parsePeppolMessageLevelResponse, parseUblInvoice, parseUblInvoiceDocument };
1011
+ export { PEPPOL_MLR_DOCUMENT_TYPE_VALUE, PEPPOL_MLR_PROCESS_VALUE, UblBuildError, UblParseError, decodeXmlBytes, extractUblEmbeddedAttachments, isPdfLikeAttachment, isPeppolMessageLevelResponse, normalizeCurrency, normalizeUblResponse, parseDate, parsePeppolMessageLevelResponse, parseUblInvoice, parseUblInvoiceDocument };
893
1012
 
894
1013
  //# sourceMappingURL=index.js.map