mindee 1.3.0 → 1.3.3
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 +47 -32
- package/dist/api/financialDocument.js +51 -0
- package/dist/api/index.js +13 -0
- package/dist/api/invoice.js +59 -0
- package/dist/api/object.js +80 -0
- package/dist/api/receipt.js +45 -0
- package/dist/api/request.js +90 -0
- package/dist/api/response.js +91 -0
- package/dist/documents/document.js +80 -0
- package/dist/documents/fields/amount.js +21 -0
- package/dist/documents/fields/date.js +17 -0
- package/dist/documents/fields/field.js +85 -0
- package/dist/documents/fields/index.js +17 -0
- package/dist/documents/fields/locale.js +25 -0
- package/dist/documents/fields/orientation.js +22 -0
- package/dist/documents/fields/paymentDetails.js +50 -0
- package/dist/documents/fields/tax.js +42 -0
- package/dist/documents/financialDocument.js +216 -0
- package/dist/documents/index.js +13 -0
- package/dist/documents/invoice.js +361 -0
- package/dist/documents/receipt.js +231 -0
- package/dist/errors/handler.js +17 -0
- package/dist/index.js +23 -0
- package/dist/inputs.js +208 -0
- package/dist/logger.js +34 -0
- package/lib/index.js +29 -30
- package/mindee/api/request.js +1 -1
- package/mindee/inputs.js +1 -4
- package/package.json +1 -1
|
@@ -0,0 +1,361 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
4
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
5
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
6
|
+
};
|
|
7
|
+
var _Invoice_instances, _Invoice_initFromScratch, _Invoice_initFromApiPrediction, _Invoice_checklist, _Invoice_reconstruct, _Invoice_taxesMatchTotalIncl, _Invoice_taxesMatchTotalExcl, _Invoice_taxesPlusTotalExclMatchTotalIncl, _Invoice_reconstructTotalTax, _Invoice_reconstructTotalTaxFromTotals, _Invoice_reconstructTotalExcl, _Invoice_reconstructTotalIncl;
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.Invoice = void 0;
|
|
10
|
+
const document_1 = require("@documents/document");
|
|
11
|
+
const fields_1 = require("@documents/fields");
|
|
12
|
+
class Invoice extends document_1.Document {
|
|
13
|
+
/**
|
|
14
|
+
* @param {Object} apiPrediction - Json parsed prediction from HTTP response
|
|
15
|
+
* @param {Input} input - Input object
|
|
16
|
+
* @param {Object} locale - locale value for creating Invoice object from scratch
|
|
17
|
+
* @param {Object} totalIncl - total tax included value for creating Invoice object from scratch
|
|
18
|
+
* @param {Object} totalExcl - total tax excluded value for creating Invoice object from scratch
|
|
19
|
+
* @param {Object} invoiceDateField - invoice DateField value for creating Invoice object from scratch
|
|
20
|
+
* @param {Object} invoiceNumber - invoice number value for creating Invoice object from scratch
|
|
21
|
+
* @param {Object} taxes - taxes value for creating Invoice object from scratch
|
|
22
|
+
* @param {Object} supplier - supplier value for creating Invoice object from scratch
|
|
23
|
+
* @param {Object} supplierAddress - supplier address value for creating Invoice object from scratch
|
|
24
|
+
* @param {Object} paymentDetails - payment details value for creating Invoice object from scratch
|
|
25
|
+
* @param {Object} companyNumber - company number value for creating Invoice object from scratch
|
|
26
|
+
* @param {Object} vatNumber - vat number value for creating Invoice object from scratch
|
|
27
|
+
* @param {Object} orientation - orientation value for creating Invoice object from scratch
|
|
28
|
+
* @param {Object} totalTax - total tax value for creating Invoice object from scratch
|
|
29
|
+
* @param {Object} customerName - customer name value for creating Invoice object from scratch
|
|
30
|
+
* @param {Object} customerAddress - customer address value for creating Invoice object from scratch
|
|
31
|
+
* @param {Object} customerCompanyRegistration - customer company registration value for creating Invoice object from scratch
|
|
32
|
+
* @param {Number} pageNumber - pageNumber for multi pages pdf input
|
|
33
|
+
* @param {String} level - specify whether object is built from "page" level or "document" level prediction
|
|
34
|
+
*/
|
|
35
|
+
constructor({ apiPrediction = undefined, inputFile = undefined, locale = undefined, totalIncl = undefined, totalExcl = undefined, invoiceDateField = undefined, invoiceNumber = undefined, dueDateField = undefined, taxes = undefined, supplier = undefined, supplierAddress = undefined, paymentDetails = undefined, companyNumber = undefined, vatNumber = undefined, orientation = undefined, totalTax = undefined, customerName = undefined, customerAddress = undefined, customerCompanyRegistration = undefined, words = undefined, pageNumber = 0, level = "page", }) {
|
|
36
|
+
super(inputFile);
|
|
37
|
+
_Invoice_instances.add(this);
|
|
38
|
+
this.level = level;
|
|
39
|
+
this.constructPrediction = function (item) {
|
|
40
|
+
return { prediction: { value: item }, valueKey: "value", pageNumber };
|
|
41
|
+
};
|
|
42
|
+
if (apiPrediction === undefined) {
|
|
43
|
+
__classPrivateFieldGet(this, _Invoice_instances, "m", _Invoice_initFromScratch).call(this, {
|
|
44
|
+
locale,
|
|
45
|
+
totalIncl,
|
|
46
|
+
totalExcl,
|
|
47
|
+
invoiceDateField,
|
|
48
|
+
invoiceNumber,
|
|
49
|
+
dueDateField,
|
|
50
|
+
taxes,
|
|
51
|
+
supplier,
|
|
52
|
+
supplierAddress,
|
|
53
|
+
paymentDetails,
|
|
54
|
+
companyNumber,
|
|
55
|
+
vatNumber,
|
|
56
|
+
orientation,
|
|
57
|
+
pageNumber,
|
|
58
|
+
totalTax,
|
|
59
|
+
customerName,
|
|
60
|
+
customerAddress,
|
|
61
|
+
customerCompanyRegistration,
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
__classPrivateFieldGet(this, _Invoice_instances, "m", _Invoice_initFromApiPrediction).call(this, apiPrediction, pageNumber, words);
|
|
66
|
+
}
|
|
67
|
+
__classPrivateFieldGet(this, _Invoice_instances, "m", _Invoice_checklist).call(this);
|
|
68
|
+
__classPrivateFieldGet(this, _Invoice_instances, "m", _Invoice_reconstruct).call(this);
|
|
69
|
+
}
|
|
70
|
+
toString() {
|
|
71
|
+
return `
|
|
72
|
+
-----Invoice data-----
|
|
73
|
+
Filename: ${this.filename}
|
|
74
|
+
Invoice number: ${this.invoiceNumber.value}
|
|
75
|
+
Total amount including taxes: ${this.totalIncl.value}
|
|
76
|
+
Total amount excluding taxes: ${this.totalExcl.value}
|
|
77
|
+
Invoice DateField: ${this.invoiceDateField.value}
|
|
78
|
+
Supplier name: ${this.supplier.value}
|
|
79
|
+
Supplier address: ${this.supplierAddress.value}
|
|
80
|
+
Customer name: ${this.customerName.value}
|
|
81
|
+
Customer address: ${this.customerAddress.value}
|
|
82
|
+
Taxes: ${this.taxes.map((tax) => tax.toString()).join(" - ")}
|
|
83
|
+
Total taxes: ${this.totalTax.value}
|
|
84
|
+
`;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
exports.Invoice = Invoice;
|
|
88
|
+
_Invoice_instances = new WeakSet(), _Invoice_initFromScratch = function _Invoice_initFromScratch({ locale, totalIncl, totalExcl, totalTax, invoiceDateField, invoiceNumber, dueDateField, taxes, supplier, supplierAddress, paymentDetails, companyNumber, vatNumber, orientation, pageNumber, customerName, customerAddress, customerCompanyRegistration, }) {
|
|
89
|
+
this.locale = new fields_1.Locale(this.constructPrediction(locale));
|
|
90
|
+
this.totalIncl = new fields_1.Amount(this.constructPrediction(totalIncl));
|
|
91
|
+
this.totalExcl = new fields_1.Amount(this.constructPrediction(totalExcl));
|
|
92
|
+
this.totalTax = new fields_1.Amount(this.constructPrediction(totalTax));
|
|
93
|
+
this.DateField = new fields_1.DateField(this.constructPrediction(invoiceDateField));
|
|
94
|
+
this.invoiceDateField = new fields_1.DateField(this.constructPrediction(invoiceDateField));
|
|
95
|
+
this.dueDateField = new fields_1.DateField(this.constructPrediction(dueDateField));
|
|
96
|
+
this.supplier = new fields_1.Field(this.constructPrediction(supplier));
|
|
97
|
+
this.supplierAddress = new fields_1.Field(this.constructPrediction(supplierAddress));
|
|
98
|
+
this.invoiceNumber = new fields_1.Field(this.constructPrediction(invoiceNumber));
|
|
99
|
+
this.paymentDetails = new fields_1.Field(this.constructPrediction(paymentDetails));
|
|
100
|
+
this.companyNumber = new fields_1.Field(this.constructPrediction(companyNumber));
|
|
101
|
+
this.vatNumber = new fields_1.Field(this.constructPrediction(vatNumber));
|
|
102
|
+
this.customerName = new fields_1.Field(this.constructPrediction(customerName));
|
|
103
|
+
this.customerAddress = new fields_1.Field(this.constructPrediction(customerAddress));
|
|
104
|
+
this.customerCompanyRegistration = new fields_1.Field(this.constructPrediction(customerCompanyRegistration));
|
|
105
|
+
if (taxes !== undefined) {
|
|
106
|
+
this.taxes = [];
|
|
107
|
+
for (const t of taxes) {
|
|
108
|
+
this.taxes.push(new fields_1.Tax({
|
|
109
|
+
prediction: { value: t[0], rate: t[1] },
|
|
110
|
+
pageNumber,
|
|
111
|
+
valueKey: "value",
|
|
112
|
+
rateKey: "rate",
|
|
113
|
+
}));
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
if (this.level === "page") {
|
|
117
|
+
this.orientation = new fields_1.Orientation(this.constructPrediction(orientation));
|
|
118
|
+
}
|
|
119
|
+
}, _Invoice_initFromApiPrediction = function _Invoice_initFromApiPrediction(apiPrediction, pageNumber, words) {
|
|
120
|
+
this.locale = new fields_1.Locale({ prediction: apiPrediction.locale, pageNumber });
|
|
121
|
+
this.totalIncl = new fields_1.Amount({
|
|
122
|
+
prediction: apiPrediction.total_incl,
|
|
123
|
+
valueKey: "value",
|
|
124
|
+
pageNumber,
|
|
125
|
+
});
|
|
126
|
+
this.totalTax = new fields_1.Amount({
|
|
127
|
+
prediction: { value: undefined, confidence: 0.0 },
|
|
128
|
+
valueKey: "value",
|
|
129
|
+
pageNumber,
|
|
130
|
+
});
|
|
131
|
+
this.totalExcl = new fields_1.Amount({
|
|
132
|
+
prediction: apiPrediction.total_excl,
|
|
133
|
+
valueKey: "value",
|
|
134
|
+
pageNumber,
|
|
135
|
+
});
|
|
136
|
+
this.DateField = new fields_1.DateField({
|
|
137
|
+
prediction: apiPrediction.DateField,
|
|
138
|
+
valueKey: "value",
|
|
139
|
+
pageNumber,
|
|
140
|
+
});
|
|
141
|
+
this.invoiceDateField = new fields_1.DateField({
|
|
142
|
+
prediction: apiPrediction.DateField,
|
|
143
|
+
valueKey: "value",
|
|
144
|
+
pageNumber,
|
|
145
|
+
});
|
|
146
|
+
this.taxes = apiPrediction.taxes.map(function (taxPrediction) {
|
|
147
|
+
return new fields_1.Tax({
|
|
148
|
+
prediction: taxPrediction,
|
|
149
|
+
pageNumber,
|
|
150
|
+
valueKey: "value",
|
|
151
|
+
rateKey: "rate",
|
|
152
|
+
codeKey: "code",
|
|
153
|
+
});
|
|
154
|
+
});
|
|
155
|
+
this.companyNumber = apiPrediction.company_registration.map(function (companyNumber) {
|
|
156
|
+
return new fields_1.Field({
|
|
157
|
+
prediction: companyNumber,
|
|
158
|
+
pageNumber,
|
|
159
|
+
extraFields: ["type"],
|
|
160
|
+
});
|
|
161
|
+
});
|
|
162
|
+
this.dueDateField = new fields_1.DateField({
|
|
163
|
+
prediction: apiPrediction.due_DateField,
|
|
164
|
+
valueKey: "value",
|
|
165
|
+
pageNumber,
|
|
166
|
+
});
|
|
167
|
+
this.invoiceNumber = new fields_1.Field({
|
|
168
|
+
prediction: apiPrediction.invoice_number,
|
|
169
|
+
pageNumber,
|
|
170
|
+
});
|
|
171
|
+
this.supplier = new fields_1.Field({
|
|
172
|
+
prediction: apiPrediction.supplier,
|
|
173
|
+
pageNumber,
|
|
174
|
+
});
|
|
175
|
+
this.supplierAddress = new fields_1.Field({
|
|
176
|
+
prediction: apiPrediction.supplier_address,
|
|
177
|
+
pageNumber,
|
|
178
|
+
});
|
|
179
|
+
this.customerName = new fields_1.Field({
|
|
180
|
+
prediction: apiPrediction.customer,
|
|
181
|
+
pageNumber,
|
|
182
|
+
});
|
|
183
|
+
this.customerAddress = new fields_1.Field({
|
|
184
|
+
prediction: apiPrediction.customer_address,
|
|
185
|
+
pageNumber,
|
|
186
|
+
});
|
|
187
|
+
this.customerCompanyRegistration =
|
|
188
|
+
apiPrediction.customer_company_registration.map(function (customerCompanyRegistration) {
|
|
189
|
+
return new fields_1.Field({
|
|
190
|
+
prediction: customerCompanyRegistration,
|
|
191
|
+
pageNumber,
|
|
192
|
+
extraFields: ["type"],
|
|
193
|
+
});
|
|
194
|
+
});
|
|
195
|
+
this.paymentDetails = apiPrediction.payment_details.map(function (paymentDetail) {
|
|
196
|
+
return new fields_1.PaymentDetails({ prediction: paymentDetail, pageNumber });
|
|
197
|
+
});
|
|
198
|
+
if (this.level === "page") {
|
|
199
|
+
this.orientation = new fields_1.Orientation({
|
|
200
|
+
prediction: apiPrediction.orientation,
|
|
201
|
+
pageNumber,
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
else {
|
|
205
|
+
this.orientation = new fields_1.Orientation(this.constructPrediction({
|
|
206
|
+
prediction: {
|
|
207
|
+
value: undefined,
|
|
208
|
+
confidence: 0.0,
|
|
209
|
+
degrees: undefined,
|
|
210
|
+
},
|
|
211
|
+
}));
|
|
212
|
+
}
|
|
213
|
+
if (words && words.length > 0)
|
|
214
|
+
this.words = words;
|
|
215
|
+
}, _Invoice_checklist = function _Invoice_checklist() {
|
|
216
|
+
this.checklist = {
|
|
217
|
+
taxesMatchTotalIncl: __classPrivateFieldGet(this, _Invoice_instances, "m", _Invoice_taxesMatchTotalIncl).call(this),
|
|
218
|
+
taxesMatchTotalExcl: __classPrivateFieldGet(this, _Invoice_instances, "m", _Invoice_taxesMatchTotalExcl).call(this),
|
|
219
|
+
taxesPlusTotalExclMatchTotalIncl: __classPrivateFieldGet(this, _Invoice_instances, "m", _Invoice_taxesPlusTotalExclMatchTotalIncl).call(this),
|
|
220
|
+
};
|
|
221
|
+
}, _Invoice_reconstruct = function _Invoice_reconstruct() {
|
|
222
|
+
__classPrivateFieldGet(this, _Invoice_instances, "m", _Invoice_reconstructTotalTax).call(this);
|
|
223
|
+
__classPrivateFieldGet(this, _Invoice_instances, "m", _Invoice_reconstructTotalExcl).call(this);
|
|
224
|
+
__classPrivateFieldGet(this, _Invoice_instances, "m", _Invoice_reconstructTotalIncl).call(this);
|
|
225
|
+
__classPrivateFieldGet(this, _Invoice_instances, "m", _Invoice_reconstructTotalTaxFromTotals).call(this);
|
|
226
|
+
}, _Invoice_taxesMatchTotalIncl = function _Invoice_taxesMatchTotalIncl() {
|
|
227
|
+
// Check taxes and total include exist
|
|
228
|
+
if (this.taxes.length === 0 || this.totalIncl.value === undefined)
|
|
229
|
+
return false;
|
|
230
|
+
// Reconstruct totalIncl from taxes
|
|
231
|
+
let totalVat = 0;
|
|
232
|
+
let reconstructedTotal = 0;
|
|
233
|
+
this.taxes.forEach((tax) => {
|
|
234
|
+
if (tax.value === undefined || !tax.rate)
|
|
235
|
+
return false;
|
|
236
|
+
totalVat += tax.value;
|
|
237
|
+
reconstructedTotal += tax.value + (100 * tax.value) / tax.rate;
|
|
238
|
+
});
|
|
239
|
+
// Sanity check
|
|
240
|
+
if (totalVat <= 0)
|
|
241
|
+
return false;
|
|
242
|
+
// Crate epsilon
|
|
243
|
+
const eps = 1 / (100 * totalVat);
|
|
244
|
+
if (this.totalIncl.value * (1 - eps) - 0.02 <= reconstructedTotal &&
|
|
245
|
+
reconstructedTotal <= this.totalIncl.value * (1 + eps) + 0.02) {
|
|
246
|
+
this.taxes = this.taxes.map((tax) => (Object.assign(Object.assign({}, tax), { probability: 1.0 })));
|
|
247
|
+
this.totalTax.probability = 1.0;
|
|
248
|
+
this.totalIncl.probability = 1.0;
|
|
249
|
+
return true;
|
|
250
|
+
}
|
|
251
|
+
return false;
|
|
252
|
+
}, _Invoice_taxesMatchTotalExcl = function _Invoice_taxesMatchTotalExcl() {
|
|
253
|
+
// Check taxes and total amount exist
|
|
254
|
+
if (this.taxes.length === 0 || this.totalExcl.value == null)
|
|
255
|
+
return false;
|
|
256
|
+
// Reconstruct total_incl from taxes
|
|
257
|
+
let totalVat = 0;
|
|
258
|
+
let reconstructedTotal = 0;
|
|
259
|
+
this.taxes.forEach((tax) => {
|
|
260
|
+
if (tax.value == null || !tax.rate)
|
|
261
|
+
return false;
|
|
262
|
+
totalVat += tax.value;
|
|
263
|
+
reconstructedTotal += (100 * tax.value) / tax.rate;
|
|
264
|
+
});
|
|
265
|
+
// Sanity check
|
|
266
|
+
if (totalVat <= 0)
|
|
267
|
+
return false;
|
|
268
|
+
// Crate epsilon
|
|
269
|
+
const eps = 1 / (100 * totalVat);
|
|
270
|
+
if (this.totalExcl.value * (1 - eps) - 0.02 <= reconstructedTotal &&
|
|
271
|
+
reconstructedTotal <= this.totalExcl.value * (1 + eps) + 0.02) {
|
|
272
|
+
this.taxes = this.taxes.map((tax) => (Object.assign(Object.assign({}, tax), { probability: 1.0 })));
|
|
273
|
+
this.totalTax.probability = 1.0;
|
|
274
|
+
this.totalExcl.probability = 1.0;
|
|
275
|
+
return true;
|
|
276
|
+
}
|
|
277
|
+
return false;
|
|
278
|
+
}, _Invoice_taxesPlusTotalExclMatchTotalIncl = function _Invoice_taxesPlusTotalExclMatchTotalIncl() {
|
|
279
|
+
if (this.totalExcl.value === undefined ||
|
|
280
|
+
this.taxes.length == 0 ||
|
|
281
|
+
this.totalIncl === undefined)
|
|
282
|
+
return false;
|
|
283
|
+
let totalVat = 0;
|
|
284
|
+
this.taxes.forEach((tax) => (totalVat += tax.value));
|
|
285
|
+
const reconstructedTotal = totalVat + this.totalExcl.value;
|
|
286
|
+
if (totalVat <= 0)
|
|
287
|
+
return false;
|
|
288
|
+
if (this.totalIncl.value - 0.01 <= reconstructedTotal &&
|
|
289
|
+
reconstructedTotal <= this.totalIncl.value + 0.01) {
|
|
290
|
+
this.taxes = this.taxes.map((tax) => (Object.assign(Object.assign({}, tax), { probability: 1.0 })));
|
|
291
|
+
this.totalTax.probability = 1.0;
|
|
292
|
+
this.totalIncl.probability = 1.0;
|
|
293
|
+
return true;
|
|
294
|
+
}
|
|
295
|
+
return false;
|
|
296
|
+
}, _Invoice_reconstructTotalTax = function _Invoice_reconstructTotalTax() {
|
|
297
|
+
if (this.taxes.length > 0) {
|
|
298
|
+
const totalTax = {
|
|
299
|
+
value: this.taxes.reduce((acc, tax) => {
|
|
300
|
+
return tax.value !== undefined ? acc + tax.value : acc;
|
|
301
|
+
}, 0),
|
|
302
|
+
confidence: fields_1.Field.arrayProbability(this.taxes),
|
|
303
|
+
};
|
|
304
|
+
if (totalTax.value > 0)
|
|
305
|
+
this.totalTax = new fields_1.Amount({
|
|
306
|
+
prediction: totalTax,
|
|
307
|
+
valueKey: "value",
|
|
308
|
+
reconstructed: true,
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
}, _Invoice_reconstructTotalTaxFromTotals = function _Invoice_reconstructTotalTaxFromTotals() {
|
|
312
|
+
if (this.totalTax.value === undefined &&
|
|
313
|
+
this.totalIncl.value > 0 &&
|
|
314
|
+
this.totalExcl.value > 0 &&
|
|
315
|
+
this.totalExcl.value <= this.totalIncl.value) {
|
|
316
|
+
const totalTax = {
|
|
317
|
+
value: this.totalIncl.value - this.totalExcl.value,
|
|
318
|
+
confidence: this.totalIncl.probability * this.totalExcl.probability,
|
|
319
|
+
};
|
|
320
|
+
if (totalTax.value > 0)
|
|
321
|
+
this.totalTax = new fields_1.Amount({
|
|
322
|
+
prediction: totalTax,
|
|
323
|
+
valueKey: "value",
|
|
324
|
+
reconstructed: true,
|
|
325
|
+
});
|
|
326
|
+
}
|
|
327
|
+
}, _Invoice_reconstructTotalExcl = function _Invoice_reconstructTotalExcl() {
|
|
328
|
+
if (this.taxes.length &&
|
|
329
|
+
this.totalIncl.value != null &&
|
|
330
|
+
this.totalExcl.value === undefined) {
|
|
331
|
+
const totalExcl = {
|
|
332
|
+
value: this.totalIncl.value -
|
|
333
|
+
this.taxes.reduce((acc, tax) => {
|
|
334
|
+
return tax.value !== undefined ? acc + tax.value : acc;
|
|
335
|
+
}, 0),
|
|
336
|
+
confidence: fields_1.Field.arrayProbability(this.taxes) * this.totalIncl.probability,
|
|
337
|
+
};
|
|
338
|
+
this.totalExcl = new fields_1.Amount({
|
|
339
|
+
prediction: totalExcl,
|
|
340
|
+
valueKey: "value",
|
|
341
|
+
reconstructed: true,
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
}, _Invoice_reconstructTotalIncl = function _Invoice_reconstructTotalIncl() {
|
|
345
|
+
if (this.taxes.length &&
|
|
346
|
+
this.totalExcl.value != null &&
|
|
347
|
+
this.totalIncl.value === undefined) {
|
|
348
|
+
const totalIncl = {
|
|
349
|
+
value: this.totalExcl.value +
|
|
350
|
+
this.taxes.reduce((acc, tax) => {
|
|
351
|
+
return tax.value ? acc + tax.value : acc;
|
|
352
|
+
}, 0.0),
|
|
353
|
+
confidence: fields_1.Field.arrayProbability(this.taxes) * this.totalExcl.probability,
|
|
354
|
+
};
|
|
355
|
+
this.totalIncl = new fields_1.Amount({
|
|
356
|
+
prediction: totalIncl,
|
|
357
|
+
valueKey: "value",
|
|
358
|
+
reconstructed: true,
|
|
359
|
+
});
|
|
360
|
+
}
|
|
361
|
+
};
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
12
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
13
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
14
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
15
|
+
};
|
|
16
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
17
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
18
|
+
};
|
|
19
|
+
var _Receipt_instances, _Receipt_initFromScratch, _Receipt_initFromApiPrediction, _Receipt_checklist, _Receipt_taxesMatchTotal, _Receipt_reconstruct, _Receipt_reconstructTotalExclFromTCCAndTaxes, _Receipt_reconstructTotalTax;
|
|
20
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
21
|
+
exports.Receipt = void 0;
|
|
22
|
+
const document_1 = require("@documents/document");
|
|
23
|
+
const fields_1 = require("@documents/fields");
|
|
24
|
+
const promises_1 = __importDefault(require("fs/promises"));
|
|
25
|
+
class Receipt extends document_1.Document {
|
|
26
|
+
/**
|
|
27
|
+
* @param {Object} apiPrediction - Json parsed prediction from HTTP response
|
|
28
|
+
* @param {Input} input - Input object
|
|
29
|
+
* @param {Integer} pageNumber - Page number for multi pages pdf input
|
|
30
|
+
* @param {Object} locale - locale value for creating Receipt object from scratch
|
|
31
|
+
* @param {Object} totalIncl - total tax Included value for creating Receipt object from scratch
|
|
32
|
+
* @param {Object} date - date value for creating Receipt object from scratch
|
|
33
|
+
* @param {Object} category - category value for creating Receipt object from scratch
|
|
34
|
+
* @param {Object} merchantName - merchant name value for creating Receipt object from scratch
|
|
35
|
+
* @param {Object} time - time value for creating Receipt object from scratch
|
|
36
|
+
* @param {Object} taxes - taxes value for creating Receipt object from scratch
|
|
37
|
+
* @param {Object} orientation - orientation value for creating Receipt object from scratch
|
|
38
|
+
* @param {Object} totalTax - total taxes value for creating Receipt object from scratch
|
|
39
|
+
* @param {Object} totalExcl - total taxes excluded value for creating Receipt object from scratch
|
|
40
|
+
* @param {String} level - specify whether object is built from "page" level or "document" level prediction
|
|
41
|
+
*/
|
|
42
|
+
constructor({ apiPrediction = undefined, inputFile = undefined, locale = undefined, totalIncl = undefined, date = undefined, category = undefined, merchantName = undefined, time = undefined, taxes = undefined, orientation = undefined, totalTax = undefined, totalExcl = undefined, words = undefined, pageNumber = 0, level = "page", }) {
|
|
43
|
+
super(inputFile);
|
|
44
|
+
_Receipt_instances.add(this);
|
|
45
|
+
this.level = level;
|
|
46
|
+
this.constructPrediction = function (item) {
|
|
47
|
+
return { prediction: { value: item }, valueKey: "value", pageNumber };
|
|
48
|
+
};
|
|
49
|
+
if (apiPrediction === undefined) {
|
|
50
|
+
__classPrivateFieldGet(this, _Receipt_instances, "m", _Receipt_initFromScratch).call(this, {
|
|
51
|
+
locale,
|
|
52
|
+
totalExcl,
|
|
53
|
+
totalIncl,
|
|
54
|
+
date,
|
|
55
|
+
category,
|
|
56
|
+
merchantName,
|
|
57
|
+
time,
|
|
58
|
+
taxes,
|
|
59
|
+
orientation,
|
|
60
|
+
totalTax,
|
|
61
|
+
pageNumber,
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
__classPrivateFieldGet(this, _Receipt_instances, "m", _Receipt_initFromApiPrediction).call(this, apiPrediction, pageNumber, words);
|
|
66
|
+
}
|
|
67
|
+
__classPrivateFieldGet(this, _Receipt_instances, "m", _Receipt_checklist).call(this);
|
|
68
|
+
__classPrivateFieldGet(this, _Receipt_instances, "m", _Receipt_reconstruct).call(this);
|
|
69
|
+
}
|
|
70
|
+
toString() {
|
|
71
|
+
return `
|
|
72
|
+
-----Receipt data-----
|
|
73
|
+
Filename: ${this.filename}
|
|
74
|
+
Total amount: ${this.totalIncl.value}
|
|
75
|
+
Date: ${this.date.value}
|
|
76
|
+
Category: ${this.category.value}
|
|
77
|
+
Time: ${this.time.value}
|
|
78
|
+
Merchant name: ${this.merchantName.value}
|
|
79
|
+
Taxes: ${this.taxes.map((tax) => tax.toString()).join(" - ")}
|
|
80
|
+
Total taxes: ${this.totalTax.value}
|
|
81
|
+
`;
|
|
82
|
+
}
|
|
83
|
+
static load(path) {
|
|
84
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
85
|
+
const file = promises_1.default.readFile(path);
|
|
86
|
+
const args = JSON.parse(file);
|
|
87
|
+
return new Receipt(Object.assign({ reconsctruted: true }, args));
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
exports.Receipt = Receipt;
|
|
92
|
+
_Receipt_instances = new WeakSet(), _Receipt_initFromScratch = function _Receipt_initFromScratch({ locale, totalExcl, totalIncl, date, category, merchantName, time, taxes, orientation, totalTax, pageNumber, }) {
|
|
93
|
+
this.locale = new fields_1.Locale(this.constructPrediction(locale));
|
|
94
|
+
this.totalIncl = new fields_1.Amount(this.constructPrediction(totalIncl));
|
|
95
|
+
this.date = new fields_1.DateField(this.constructPrediction(date));
|
|
96
|
+
this.category = new fields_1.Field(this.constructPrediction(category));
|
|
97
|
+
this.merchantName = new fields_1.Field(this.constructPrediction(merchantName));
|
|
98
|
+
this.time = new fields_1.Field(this.constructPrediction(time));
|
|
99
|
+
if (taxes !== undefined) {
|
|
100
|
+
this.taxes = [];
|
|
101
|
+
for (const t of taxes) {
|
|
102
|
+
this.taxes.push(new fields_1.Tax({
|
|
103
|
+
prediction: { value: t[0], rate: t[1] },
|
|
104
|
+
pageNumber,
|
|
105
|
+
valueKey: "value",
|
|
106
|
+
rateKey: "rate",
|
|
107
|
+
}));
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
this.orientation = new fields_1.Orientation(this.constructPrediction(orientation));
|
|
111
|
+
this.totalTax = new fields_1.Amount(this.constructPrediction(totalTax));
|
|
112
|
+
this.totalExcl = new fields_1.Amount(this.constructPrediction(totalExcl));
|
|
113
|
+
}, _Receipt_initFromApiPrediction = function _Receipt_initFromApiPrediction(apiPrediction, pageNumber, words) {
|
|
114
|
+
this.locale = new fields_1.Locale({ prediction: apiPrediction.locale, pageNumber });
|
|
115
|
+
this.totalIncl = new fields_1.Amount({
|
|
116
|
+
prediction: apiPrediction.total_incl,
|
|
117
|
+
valueKey: "value",
|
|
118
|
+
pageNumber,
|
|
119
|
+
});
|
|
120
|
+
this.date = new fields_1.DateField({
|
|
121
|
+
prediction: apiPrediction.date,
|
|
122
|
+
valueKey: "value",
|
|
123
|
+
pageNumber,
|
|
124
|
+
});
|
|
125
|
+
this.category = new fields_1.Field({
|
|
126
|
+
prediction: apiPrediction.category,
|
|
127
|
+
pageNumber,
|
|
128
|
+
});
|
|
129
|
+
this.merchantName = new fields_1.Field({
|
|
130
|
+
prediction: apiPrediction.supplier,
|
|
131
|
+
valueKey: "value",
|
|
132
|
+
pageNumber,
|
|
133
|
+
});
|
|
134
|
+
this.time = new fields_1.Field({
|
|
135
|
+
prediction: apiPrediction.time,
|
|
136
|
+
valueKey: "value",
|
|
137
|
+
pageNumber,
|
|
138
|
+
});
|
|
139
|
+
this.taxes = apiPrediction.taxes.map((taxPrediction) => new fields_1.Tax({
|
|
140
|
+
prediction: taxPrediction,
|
|
141
|
+
pageNumber,
|
|
142
|
+
valueKey: "value",
|
|
143
|
+
rateKey: "rate",
|
|
144
|
+
codeKey: "code",
|
|
145
|
+
}));
|
|
146
|
+
this.totalTax = new fields_1.Amount({
|
|
147
|
+
prediction: { value: undefined, confidence: 0 },
|
|
148
|
+
valueKey: "value",
|
|
149
|
+
pageNumber,
|
|
150
|
+
});
|
|
151
|
+
this.totalExcl = new fields_1.Amount({
|
|
152
|
+
prediction: { value: undefined, confidence: 0 },
|
|
153
|
+
valueKey: "value",
|
|
154
|
+
pageNumber,
|
|
155
|
+
});
|
|
156
|
+
if (this.level === "page") {
|
|
157
|
+
this.orientation = new fields_1.Orientation({
|
|
158
|
+
prediction: apiPrediction.orientation,
|
|
159
|
+
pageNumber,
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
else {
|
|
163
|
+
this.orientation = new fields_1.Orientation(this.constructPrediction({
|
|
164
|
+
prediction: {
|
|
165
|
+
value: undefined,
|
|
166
|
+
confidence: 0.0,
|
|
167
|
+
degrees: undefined,
|
|
168
|
+
},
|
|
169
|
+
}));
|
|
170
|
+
}
|
|
171
|
+
if (words && words.length > 0)
|
|
172
|
+
this.words = words;
|
|
173
|
+
}, _Receipt_checklist = function _Receipt_checklist() {
|
|
174
|
+
this.checklist = { taxesMatchTotalIncl: __classPrivateFieldGet(this, _Receipt_instances, "m", _Receipt_taxesMatchTotal).call(this) };
|
|
175
|
+
}, _Receipt_taxesMatchTotal = function _Receipt_taxesMatchTotal() {
|
|
176
|
+
// Check taxes and total amount exist
|
|
177
|
+
if (this.taxes.length === 0 || this.totalIncl.value == null)
|
|
178
|
+
return false;
|
|
179
|
+
// Reconstruct total_incl from taxes
|
|
180
|
+
let totalVat = 0;
|
|
181
|
+
let reconstructedTotal = 0;
|
|
182
|
+
this.taxes.forEach((tax) => {
|
|
183
|
+
if (tax.value == null || !tax.rate)
|
|
184
|
+
return false;
|
|
185
|
+
totalVat += tax.value;
|
|
186
|
+
reconstructedTotal += tax.value + (100 * tax.value) / tax.rate;
|
|
187
|
+
});
|
|
188
|
+
// Sanity check
|
|
189
|
+
if (totalVat <= 0)
|
|
190
|
+
return false;
|
|
191
|
+
// Crate epsilon
|
|
192
|
+
const eps = 1 / (100 * totalVat);
|
|
193
|
+
if (this.totalIncl.value * (1 - eps) - 0.02 <= reconstructedTotal &&
|
|
194
|
+
reconstructedTotal <= this.totalIncl.value * (1 + eps) + 0.02) {
|
|
195
|
+
this.taxes = this.taxes.map((tax) => (Object.assign(Object.assign({}, tax), { probability: 1.0 })));
|
|
196
|
+
this.totalTax.probability = 1.0;
|
|
197
|
+
this.totalIncl.probability = 1.0;
|
|
198
|
+
return true;
|
|
199
|
+
}
|
|
200
|
+
return false;
|
|
201
|
+
}, _Receipt_reconstruct = function _Receipt_reconstruct() {
|
|
202
|
+
__classPrivateFieldGet(this, _Receipt_instances, "m", _Receipt_reconstructTotalExclFromTCCAndTaxes).call(this);
|
|
203
|
+
__classPrivateFieldGet(this, _Receipt_instances, "m", _Receipt_reconstructTotalTax).call(this);
|
|
204
|
+
}, _Receipt_reconstructTotalExclFromTCCAndTaxes = function _Receipt_reconstructTotalExclFromTCCAndTaxes() {
|
|
205
|
+
if (this.taxes.length && this.totalIncl.value != null) {
|
|
206
|
+
const totalExcl = {
|
|
207
|
+
value: this.totalIncl.value - fields_1.Field.arraySum(this.taxes),
|
|
208
|
+
confidence: fields_1.Field.arrayProbability(this.taxes) * this.totalIncl.probability,
|
|
209
|
+
};
|
|
210
|
+
this.totalExcl = new fields_1.Amount({
|
|
211
|
+
prediction: totalExcl,
|
|
212
|
+
valueKey: "value",
|
|
213
|
+
reconstructed: true,
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
}, _Receipt_reconstructTotalTax = function _Receipt_reconstructTotalTax() {
|
|
217
|
+
if (this.taxes.length && this.totalTax.value == null) {
|
|
218
|
+
const totalTax = {
|
|
219
|
+
value: this.taxes
|
|
220
|
+
.map((tax) => tax.value || 0)
|
|
221
|
+
.reduce((a, b) => a + b, 0),
|
|
222
|
+
confidence: fields_1.Field.arrayProbability(this.taxes),
|
|
223
|
+
};
|
|
224
|
+
if (totalTax.value > 0)
|
|
225
|
+
this.totalTax = new fields_1.Amount({
|
|
226
|
+
prediction: totalTax,
|
|
227
|
+
valueKey: "value",
|
|
228
|
+
reconstructed: true,
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.errorHandler = void 0;
|
|
4
|
+
const logger_1 = require("@mindee/logger");
|
|
5
|
+
class ErrorHandler {
|
|
6
|
+
constructor(throwOnError = true) {
|
|
7
|
+
this.throwOnError = throwOnError;
|
|
8
|
+
this.throwOnError = throwOnError;
|
|
9
|
+
}
|
|
10
|
+
throw(error) {
|
|
11
|
+
if (this.throwOnError || force)
|
|
12
|
+
throw error;
|
|
13
|
+
else
|
|
14
|
+
logger_1.logger.error(error.message);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
exports.errorHandler = new ErrorHandler();
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Client = void 0;
|
|
4
|
+
const handler_1 = require("@errors/handler");
|
|
5
|
+
const logger_1 = require("@mindee/logger");
|
|
6
|
+
const receipt_1 = require("@mindee/api/receipt");
|
|
7
|
+
const invoice_1 = require("@mindee/api/invoice");
|
|
8
|
+
const financialDocument_1 = require("@mindee/api/financialDocument");
|
|
9
|
+
class Client {
|
|
10
|
+
constructor({ receiptToken, invoiceToken, throwOnError = true, debug, }) {
|
|
11
|
+
this.receiptToken = receiptToken || process.env.MINDEE_RECEIPT_TOKEN;
|
|
12
|
+
this.invoiceToken = invoiceToken || process.env.MINDEE_INVOICE_TOKEN;
|
|
13
|
+
handler_1.errorHandler.throwOnError = throwOnError;
|
|
14
|
+
logger_1.logger.level = (debug !== null && debug !== void 0 ? debug : process.env.MINDEE_DEBUG) ? "debug" : "warn";
|
|
15
|
+
this.receipt = new receipt_1.APIReceipt(this.receiptToken);
|
|
16
|
+
this.invoice = new invoice_1.APIInvoice(this.invoiceToken);
|
|
17
|
+
this.financialDocument = new financialDocument_1.APIFinancialDocument(this.invoiceToken, this.receiptToken);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
exports.Client = Client;
|
|
21
|
+
exports.documents = require("./documents");
|
|
22
|
+
exports.api = require("./api");
|
|
23
|
+
exports.inputs = require("./inputs");
|