@financica/ubl 0.15.0 → 0.16.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/CHANGELOG.md +161 -0
- package/dist/build/index.d.ts +76 -40
- package/dist/build/index.d.ts.map +1 -1
- package/dist/build/index.js +55 -109
- package/dist/build/index.js.map +1 -1
- package/dist/index.d.ts +149 -121
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +277 -233
- package/dist/index.js.map +1 -1
- package/package.json +16 -7
- package/src/attachments.ts +67 -0
- package/src/build/address.ts +64 -0
- package/src/build/attachment.ts +33 -0
- package/src/build/identifiers.ts +253 -0
- package/src/build/index.ts +69 -0
- package/src/build/numeric.ts +41 -0
- package/src/build/party.ts +127 -0
- package/src/build/tax-category.ts +152 -0
- package/src/build/tax-totals.ts +143 -0
- package/src/build/ubl/constants.ts +30 -0
- package/src/build/ubl/serialize.ts +258 -0
- package/src/build/ubl/types.ts +179 -0
- package/src/build/utils.ts +6 -0
- package/src/build/xml.ts +84 -0
- package/src/errors.ts +15 -0
- package/src/index.ts +40 -0
- package/src/mlr.ts +156 -0
- package/src/normalize.ts +407 -0
- package/src/parser.ts +596 -0
- package/src/types.ts +234 -0
- package/src/xml-dom.ts +37 -0
package/src/types.ts
ADDED
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
// --- UBL parsed types ---
|
|
2
|
+
//
|
|
3
|
+
// Parse-side model. A field the document may omit is an optional key: the
|
|
4
|
+
// parser never writes `""`/`0` to fill a gap. Fields EN 16931 makes mandatory
|
|
5
|
+
// (document id, issue date, currency, line id) stay required; a document that
|
|
6
|
+
// lacks one is rejected with `UblParseError`. Containers the consumer iterates
|
|
7
|
+
// (`lines`, `taxSubtotals`) and the `seller`/`buyer`/`monetaryTotal` objects
|
|
8
|
+
// are always present.
|
|
9
|
+
|
|
10
|
+
export interface UblAddress {
|
|
11
|
+
street?: string | undefined;
|
|
12
|
+
additionalStreet?: string | undefined;
|
|
13
|
+
city?: string | undefined;
|
|
14
|
+
postalZone?: string | undefined;
|
|
15
|
+
countrySubentity?: string | undefined;
|
|
16
|
+
countryCode?: string | undefined;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface UblContact {
|
|
20
|
+
name?: string | undefined;
|
|
21
|
+
phone?: string | undefined;
|
|
22
|
+
email?: string | undefined;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface UblPartyIdentification {
|
|
26
|
+
id: string;
|
|
27
|
+
schemeId?: string | undefined;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface UblParty {
|
|
31
|
+
name?: string | undefined;
|
|
32
|
+
registrationName?: string | undefined;
|
|
33
|
+
companyLegalForm?: string | undefined;
|
|
34
|
+
vatId?: string | undefined;
|
|
35
|
+
taxSchemeId?: string | undefined;
|
|
36
|
+
companyId?: string | undefined;
|
|
37
|
+
companyIdSchemeId?: string | undefined;
|
|
38
|
+
endpointId?: string | undefined;
|
|
39
|
+
endpointSchemeId?: string | undefined;
|
|
40
|
+
partyIdentifications?: UblPartyIdentification[] | undefined;
|
|
41
|
+
address?: UblAddress | undefined;
|
|
42
|
+
contact?: UblContact | undefined;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface UblItemProperty {
|
|
46
|
+
name: string;
|
|
47
|
+
value?: string | undefined;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface UblLine {
|
|
51
|
+
id: string;
|
|
52
|
+
description?: string | undefined;
|
|
53
|
+
quantity?: number | undefined;
|
|
54
|
+
unitCode?: string | undefined;
|
|
55
|
+
unitPrice?: number | undefined;
|
|
56
|
+
lineExtensionAmount?: number | undefined;
|
|
57
|
+
taxPercent?: number | undefined;
|
|
58
|
+
taxAmount?: number | undefined;
|
|
59
|
+
taxCategoryId?: string | undefined;
|
|
60
|
+
taxSchemeId?: string | undefined;
|
|
61
|
+
taxSubtotals?: UblTaxSubtotal[] | undefined;
|
|
62
|
+
allowanceCharges?: UblAllowanceCharge[] | undefined;
|
|
63
|
+
discountAmount?: number | undefined;
|
|
64
|
+
chargeAmount?: number | undefined;
|
|
65
|
+
itemName?: string | undefined;
|
|
66
|
+
sellersItemId?: string | undefined;
|
|
67
|
+
buyersItemId?: string | undefined;
|
|
68
|
+
additionalItemProperties?: UblItemProperty[] | undefined;
|
|
69
|
+
/** EN 16931 BT-134/BT-135: the line's own service period. */
|
|
70
|
+
invoicePeriod?: UblInvoicePeriod | undefined;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export interface UblAllowanceCharge {
|
|
74
|
+
chargeIndicator: boolean;
|
|
75
|
+
amount?: number | undefined;
|
|
76
|
+
baseAmount?: number | undefined;
|
|
77
|
+
multiplierFactorNumeric?: number | undefined;
|
|
78
|
+
reason?: string | undefined;
|
|
79
|
+
reasonCode?: string | undefined;
|
|
80
|
+
taxPercent?: number | undefined;
|
|
81
|
+
taxCategoryId?: string | undefined;
|
|
82
|
+
taxSchemeId?: string | undefined;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export interface UblTaxSubtotal {
|
|
86
|
+
taxableAmount?: number | undefined;
|
|
87
|
+
taxAmount?: number | undefined;
|
|
88
|
+
taxPercent?: number | undefined;
|
|
89
|
+
taxCategoryId?: string | undefined;
|
|
90
|
+
taxSchemeId?: string | undefined;
|
|
91
|
+
taxExemptionReason?: string | undefined;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export interface UblMonetaryTotal {
|
|
95
|
+
lineExtensionAmount?: number | undefined;
|
|
96
|
+
taxExclusiveAmount?: number | undefined;
|
|
97
|
+
taxInclusiveAmount?: number | undefined;
|
|
98
|
+
allowanceTotalAmount?: number | undefined;
|
|
99
|
+
chargeTotalAmount?: number | undefined;
|
|
100
|
+
prepaidAmount?: number | undefined;
|
|
101
|
+
payableRoundingAmount?: number | undefined;
|
|
102
|
+
payableAmount?: number | undefined;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export interface UblPaymentMeans {
|
|
106
|
+
code?: string | undefined;
|
|
107
|
+
codeName?: string | undefined;
|
|
108
|
+
paymentId?: string | undefined;
|
|
109
|
+
iban?: string | undefined;
|
|
110
|
+
bic?: string | undefined;
|
|
111
|
+
accountName?: string | undefined;
|
|
112
|
+
mandateId?: string | undefined;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export interface UblInvoicePeriod {
|
|
116
|
+
startDate?: string | undefined;
|
|
117
|
+
endDate?: string | undefined;
|
|
118
|
+
descriptionCode?: string | undefined;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export interface UblAttachment {
|
|
122
|
+
id?: string | undefined;
|
|
123
|
+
filename?: string | undefined;
|
|
124
|
+
mimeCode?: string | undefined;
|
|
125
|
+
description?: string | undefined;
|
|
126
|
+
base64Content?: string | undefined;
|
|
127
|
+
externalUri?: string | undefined;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export interface UblDocumentReference {
|
|
131
|
+
id?: string | undefined;
|
|
132
|
+
description?: string | undefined;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export interface UblDelivery {
|
|
136
|
+
actualDeliveryDate?: string | undefined;
|
|
137
|
+
address?: UblAddress | undefined;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export interface UblBillingReference {
|
|
141
|
+
invoiceId?: string | undefined;
|
|
142
|
+
invoiceIssueDate?: string | undefined;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export interface UblInvoice {
|
|
146
|
+
documentType: "Invoice" | "CreditNote";
|
|
147
|
+
customizationId?: string | undefined;
|
|
148
|
+
profileId?: string | undefined;
|
|
149
|
+
id: string;
|
|
150
|
+
invoiceTypeCode?: string | undefined;
|
|
151
|
+
issueDate: string;
|
|
152
|
+
dueDate?: string | undefined;
|
|
153
|
+
taxPointDate?: string | undefined;
|
|
154
|
+
currency: string;
|
|
155
|
+
buyerReference?: string | undefined;
|
|
156
|
+
orderReference?: string | undefined;
|
|
157
|
+
salesOrderId?: string | undefined;
|
|
158
|
+
contractReference?: string | undefined;
|
|
159
|
+
projectReference?: string | undefined;
|
|
160
|
+
billingReference?: UblBillingReference | undefined;
|
|
161
|
+
seller: UblParty;
|
|
162
|
+
buyer: UblParty;
|
|
163
|
+
delivery?: UblDelivery | undefined;
|
|
164
|
+
lines: UblLine[];
|
|
165
|
+
taxSubtotals: UblTaxSubtotal[];
|
|
166
|
+
monetaryTotal: UblMonetaryTotal;
|
|
167
|
+
paymentMeans?: UblPaymentMeans | undefined;
|
|
168
|
+
paymentMeansList?: UblPaymentMeans[] | undefined;
|
|
169
|
+
invoicePeriod?: UblInvoicePeriod | undefined;
|
|
170
|
+
note?: string | undefined;
|
|
171
|
+
paymentTermsNote?: string | undefined;
|
|
172
|
+
attachments?: UblAttachment[] | undefined;
|
|
173
|
+
documentReferences?: UblDocumentReference[] | undefined;
|
|
174
|
+
allowanceCharges?: UblAllowanceCharge[] | undefined;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// --- Normalized DTO types ---
|
|
178
|
+
|
|
179
|
+
export interface InvoiceExtractionDTO {
|
|
180
|
+
provider: string;
|
|
181
|
+
document_id: string;
|
|
182
|
+
invoice: {
|
|
183
|
+
invoice_number: string | null;
|
|
184
|
+
invoice_date: string | null;
|
|
185
|
+
due_date: string | null;
|
|
186
|
+
/** EN 16931 BT-73/BT-74: the period the document bills for. */
|
|
187
|
+
period_start: string | null;
|
|
188
|
+
period_end: string | null;
|
|
189
|
+
currency: string | null;
|
|
190
|
+
subtotal: number | null;
|
|
191
|
+
tax_total: number | null;
|
|
192
|
+
total: number | null;
|
|
193
|
+
amount_due: number | null;
|
|
194
|
+
amount_paid: number | null;
|
|
195
|
+
discount_total: number | null;
|
|
196
|
+
shipping_total: number | null;
|
|
197
|
+
/** EN 16931 BT-114, signed. Outside the VAT breakdown (BR-CO-16). */
|
|
198
|
+
rounding_total: number | null;
|
|
199
|
+
payment_terms: string | null;
|
|
200
|
+
po_number: string | null;
|
|
201
|
+
supplier: {
|
|
202
|
+
name: string | null;
|
|
203
|
+
address: string | null;
|
|
204
|
+
tax_id: string | null;
|
|
205
|
+
iban: string | null;
|
|
206
|
+
bic: string | null;
|
|
207
|
+
};
|
|
208
|
+
receiver: {
|
|
209
|
+
name: string | null;
|
|
210
|
+
address: string | null;
|
|
211
|
+
tax_id: string | null;
|
|
212
|
+
};
|
|
213
|
+
extra: Record<string, unknown>;
|
|
214
|
+
};
|
|
215
|
+
line_items: Array<{
|
|
216
|
+
description: string;
|
|
217
|
+
quantity: number | null;
|
|
218
|
+
unit: string | null;
|
|
219
|
+
unit_price: number | null;
|
|
220
|
+
amount: number | null;
|
|
221
|
+
tax_amount: number | null;
|
|
222
|
+
tax_rate: number | null;
|
|
223
|
+
product_code: string | null;
|
|
224
|
+
discount_amount: number | null;
|
|
225
|
+
/** EN 16931 BT-134/BT-135: the line's own service period. */
|
|
226
|
+
period_start: string | null;
|
|
227
|
+
period_end: string | null;
|
|
228
|
+
extra: Record<string, unknown>;
|
|
229
|
+
}>;
|
|
230
|
+
confidence: {
|
|
231
|
+
overall: number | null;
|
|
232
|
+
fields: Record<string, number>;
|
|
233
|
+
};
|
|
234
|
+
}
|
package/src/xml-dom.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { type Document, DOMParser as XmlDomParser } from "@xmldom/xmldom";
|
|
2
|
+
import { UblParseError } from "./errors.js";
|
|
3
|
+
|
|
4
|
+
export const CBC_NS =
|
|
5
|
+
"urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2";
|
|
6
|
+
export const CAC_NS =
|
|
7
|
+
"urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Strip DOCTYPE declarations to prevent XXE (XML External Entity) attacks.
|
|
11
|
+
* @xmldom/xmldom resolves external entities by default, so we remove DOCTYPE
|
|
12
|
+
* blocks (including inline DTD subsets) before parsing.
|
|
13
|
+
*/
|
|
14
|
+
export const stripDoctype = (xml: string): string =>
|
|
15
|
+
xml.replace(/<!DOCTYPE\s[^>[]*(?:\[[^\]]*\])?>/gi, "");
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Parse XML into a DOM, preferring the host's `DOMParser` (browsers) and
|
|
19
|
+
* falling back to @xmldom/xmldom. Throws {@link UblParseError} when the XML
|
|
20
|
+
* is malformed.
|
|
21
|
+
*/
|
|
22
|
+
export const parseXmlDocument = (xml: string): Document => {
|
|
23
|
+
const safeXml = stripDoctype(xml);
|
|
24
|
+
const BrowserDomParser = (globalThis as { DOMParser?: typeof XmlDomParser })
|
|
25
|
+
.DOMParser;
|
|
26
|
+
const parser = BrowserDomParser ? new BrowserDomParser() : new XmlDomParser();
|
|
27
|
+
let doc: Document;
|
|
28
|
+
try {
|
|
29
|
+
doc = parser.parseFromString(safeXml, "text/xml");
|
|
30
|
+
} catch (cause) {
|
|
31
|
+
throw new UblParseError("Malformed XML", { cause });
|
|
32
|
+
}
|
|
33
|
+
if (doc.getElementsByTagName("parsererror").length > 0 || !doc.documentElement) {
|
|
34
|
+
throw new UblParseError("Malformed XML");
|
|
35
|
+
}
|
|
36
|
+
return doc;
|
|
37
|
+
};
|