mindee 1.0.4 → 1.0.9

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.
Files changed (48) hide show
  1. package/.nyc_output/f57ba3bc-1777-4ecc-81e1-230327c2a401.json +1 -0
  2. package/.nyc_output/processinfo/f57ba3bc-1777-4ecc-81e1-230327c2a401.json +1 -0
  3. package/.nyc_output/processinfo/index.json +1 -0
  4. package/CHANGELOG.md +42 -8
  5. package/README.md +1 -1
  6. package/coverage/coverage.json +1 -0
  7. package/coverage/lcov-report/base.css +224 -0
  8. package/coverage/lcov-report/block-navigation.js +79 -0
  9. package/coverage/lcov-report/favicon.png +0 -0
  10. package/coverage/lcov-report/index.html +171 -0
  11. package/coverage/lcov-report/prettify.css +1 -0
  12. package/coverage/lcov-report/prettify.js +2 -0
  13. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  14. package/coverage/lcov-report/sorter.js +170 -0
  15. package/coverage/lcov.info +1595 -0
  16. package/coverage.lcov +1595 -0
  17. package/examples/documents/invoices/credit_note.pdf +899 -1
  18. package/examples/invoice.js +17 -0
  19. package/lib/api/financialDocument.js +3 -3
  20. package/lib/api/invoice.js +18 -3
  21. package/lib/api/receipt.js +3 -3
  22. package/lib/api/request.js +7 -3
  23. package/lib/inputs.js +78 -24
  24. package/mindee/api/financialDocument.js +45 -0
  25. package/mindee/api/index.js +5 -0
  26. package/mindee/api/invoice.js +53 -0
  27. package/mindee/api/object.js +82 -0
  28. package/mindee/api/receipt.js +39 -0
  29. package/mindee/api/request.js +67 -0
  30. package/mindee/api/response.js +62 -0
  31. package/mindee/documents/document.js +69 -0
  32. package/mindee/documents/fields/amount.js +25 -0
  33. package/mindee/documents/fields/date.js +29 -0
  34. package/mindee/documents/fields/field.js +89 -0
  35. package/mindee/documents/fields/index.js +7 -0
  36. package/mindee/documents/fields/locale.js +30 -0
  37. package/mindee/documents/fields/orientation.js +24 -0
  38. package/mindee/documents/fields/paymentDetails.js +52 -0
  39. package/mindee/documents/fields/tax.js +47 -0
  40. package/mindee/documents/financialDocument.js +204 -0
  41. package/mindee/documents/index.js +5 -0
  42. package/mindee/documents/invoice.js +395 -0
  43. package/mindee/documents/receipt.js +272 -0
  44. package/mindee/errors/handler.js +16 -0
  45. package/mindee/index.js +36 -0
  46. package/mindee/inputs.js +155 -0
  47. package/mindee/logger.js +33 -0
  48. package/package.json +6 -2
@@ -0,0 +1,395 @@
1
+ const Document = require("./document");
2
+ const Field = require("./fields").field;
3
+ const Date = require("./fields").date;
4
+ const Amount = require("./fields").amount;
5
+ const Locale = require("./fields").locale;
6
+ const Orientation = require("./fields").orientation;
7
+ const PaymentDetails = require("./fields").paymentDetails;
8
+ const Tax = require("./fields").tax;
9
+
10
+ class Invoice extends Document {
11
+ /**
12
+ * @param {Object} apiPrediction - Json parsed prediction from HTTP response
13
+ * @param {Input} input - Input object
14
+ * @param {Integer} pageNumber - Page number for multi pages pdf input
15
+ * @param {Object} locale - locale value for creating Invoice object from scratch
16
+ * @param {Object} totalIncl - total tax included value for creating Invoice object from scratch
17
+ * @param {Object} totalExcl - total tax excluded value for creating Invoice object from scratch
18
+ * @param {Object} invoiceDate - invoice date value for creating Invoice object from scratch
19
+ * @param {Object} invoiceNumber - invoice number value for creating Invoice object from scratch
20
+ * @param {Object} taxes - taxes value for creating Invoice object from scratch
21
+ * @param {Object} supplier - supplier value for creating Invoice object from scratch
22
+ * @param {Object} paymentDetails - payment details value for creating Invoice object from scratch
23
+ * @param {Object} companyNumber - company number value for creating Invoice object from scratch
24
+ * @param {Object} vatNumber - vat number value for creating Invoice object from scratch
25
+ * @param {Object} orientation - orientation value for creating Invoice object from scratch
26
+ * @param {Object} totalTax - total tax value for creating Invoice object from scratch
27
+ * @param {Object} pageNumber - pageNumber for multi pages pdf input
28
+ */
29
+ constructor({
30
+ apiPrediction = undefined,
31
+ inputFile = undefined,
32
+ locale = undefined,
33
+ totalIncl = undefined,
34
+ totalExcl = undefined,
35
+ invoiceDate = undefined,
36
+ invoiceNumber = undefined,
37
+ dueDate = undefined,
38
+ taxes = undefined,
39
+ supplier = undefined,
40
+ paymentDetails = undefined,
41
+ companyNumber = undefined,
42
+ vatNumber = undefined,
43
+ orientation = undefined,
44
+ totalTax = undefined,
45
+ pageNumber = 0,
46
+ }) {
47
+ super(inputFile);
48
+ if (apiPrediction === undefined) {
49
+ this.#initFromScratch({
50
+ locale,
51
+ totalIncl,
52
+ totalExcl,
53
+ invoiceDate,
54
+ invoiceNumber,
55
+ dueDate,
56
+ taxes,
57
+ supplier,
58
+ paymentDetails,
59
+ companyNumber,
60
+ vatNumber,
61
+ orientation,
62
+ pageNumber,
63
+ totalTax,
64
+ });
65
+ } else {
66
+ this.#initFromApiPrediction(apiPrediction, pageNumber);
67
+ }
68
+ this.#checklist();
69
+ this.#reconstruct();
70
+ }
71
+
72
+ #initFromScratch({
73
+ locale,
74
+ totalIncl,
75
+ totalExcl,
76
+ totalTax,
77
+ invoiceDate,
78
+ invoiceNumber,
79
+ dueDate,
80
+ taxes,
81
+ supplier,
82
+ paymentDetails,
83
+ companyNumber,
84
+ vatNumber,
85
+ orientation,
86
+ pageNumber,
87
+ }) {
88
+ const constructPrediction = function (item) {
89
+ return { prediction: { value: item }, valueKey: "value", pageNumber };
90
+ };
91
+ this.locale = new Locale(constructPrediction(locale));
92
+ this.totalIncl = new Amount(constructPrediction(totalIncl));
93
+ this.totalExcl = new Amount(constructPrediction(totalExcl));
94
+ this.totalTax = new Amount(constructPrediction(totalTax));
95
+ this.date = new Date(constructPrediction(invoiceDate));
96
+ this.invoiceDate = new Date(constructPrediction(invoiceDate));
97
+ this.dueDate = new Date(constructPrediction(dueDate));
98
+ this.supplier = new Field(constructPrediction(supplier));
99
+ this.orientation = new Orientation(constructPrediction(orientation));
100
+ this.invoiceNumber = new Field(constructPrediction(invoiceNumber));
101
+ this.paymentDetails = new Field(constructPrediction(paymentDetails));
102
+ this.companyNumber = new Field(constructPrediction(companyNumber));
103
+ this.vatNumber = new Field(constructPrediction(vatNumber));
104
+ if (taxes !== undefined) {
105
+ this.taxes = [];
106
+ for (const t of taxes) {
107
+ this.taxes.push(
108
+ new Tax({
109
+ prediction: { value: t[0], rate: t[1] },
110
+ pageNumber,
111
+ valueKey: "value",
112
+ rateKey: "rate",
113
+ })
114
+ );
115
+ }
116
+ }
117
+ }
118
+
119
+ #initFromApiPrediction(apiPrediction, pageNumber) {
120
+ this.words = [];
121
+ this.locale = new Locale({ prediction: apiPrediction.locale, pageNumber });
122
+ this.totalIncl = new Amount({
123
+ prediction: apiPrediction.total_incl,
124
+ valueKey: "value",
125
+ pageNumber,
126
+ });
127
+ this.totalTax = new Amount({
128
+ prediction: { value: undefined, probability: 0.0 },
129
+ valueKey: "value",
130
+ pageNumber,
131
+ });
132
+ this.totalExcl = new Amount({
133
+ prediction: apiPrediction.total_excl,
134
+ valueKey: "value",
135
+ pageNumber,
136
+ });
137
+ this.date = new Date({
138
+ prediction: apiPrediction.date,
139
+ valueKey: "value",
140
+ pageNumber,
141
+ });
142
+ this.invoiceDate = new Date({
143
+ prediction: apiPrediction.date,
144
+ valueKey: "value",
145
+ pageNumber,
146
+ });
147
+ this.taxes = apiPrediction.taxes.map(function (taxPrediction) {
148
+ return new Tax({
149
+ prediction: taxPrediction,
150
+ pageNumber,
151
+ valueKey: "value",
152
+ rateKey: "rate",
153
+ codeKey: "code",
154
+ });
155
+ });
156
+ this.orientation = new Orientation({
157
+ prediction: apiPrediction.orientation,
158
+ pageNumber,
159
+ });
160
+ this.companyNumber = apiPrediction.company_registration.map(function (
161
+ companyNumber
162
+ ) {
163
+ return new Field({
164
+ prediction: companyNumber,
165
+ pageNumber,
166
+ extraFields: ["type"],
167
+ });
168
+ });
169
+ this.dueDate = new Date({
170
+ prediction: apiPrediction.due_date,
171
+ valueKey: "value",
172
+ pageNumber,
173
+ });
174
+ this.invoiceNumber = new Field({
175
+ prediction: apiPrediction.invoice_number,
176
+ pageNumber,
177
+ });
178
+ this.supplier = new Field({
179
+ prediction: apiPrediction.supplier,
180
+ pageNumber,
181
+ });
182
+ this.paymentDetails = apiPrediction.payment_details.map(function (
183
+ paymentDetail
184
+ ) {
185
+ return new PaymentDetails({ prediction: paymentDetail, pageNumber });
186
+ });
187
+
188
+ if ("mvision" in apiPrediction) this.words = apiPrediction.mvision;
189
+ }
190
+
191
+ toString() {
192
+ return `
193
+ -----Invoice data-----
194
+ Filename: ${this.filename}
195
+ Invoice number: ${this.invoiceNumber.value}
196
+ Total amount including taxes: ${this.totalIncl.value}
197
+ Total amount excluding taxes: ${this.totalExcl.value}
198
+ Invoice Date: ${this.invoiceDate.value}
199
+ Supplier name: ${this.supplier.value}
200
+ Taxes: ${this.taxes.map((tax) => tax.toString()).join(" - ")}
201
+ Total taxes: ${this.totalTax.value}
202
+ `;
203
+ }
204
+
205
+ #checklist() {
206
+ this.checklist = {
207
+ taxesMatchTotalIncl: this.#taxesMatchTotalIncl(),
208
+ taxesMatchTotalExcl: this.#taxesMatchTotalExcl(),
209
+ taxesPlusTotalExclMatchTotalIncl: this.#taxesPlusTotalExclMatchTotalIncl(),
210
+ };
211
+ }
212
+
213
+ #reconstruct() {
214
+ this.#reconstructTotalTax();
215
+ this.#reconstructTotalExcl();
216
+ this.#reconstructTotalIncl();
217
+ this.#reconstructTotalTaxFromTotals();
218
+ }
219
+
220
+ #taxesMatchTotalIncl() {
221
+ // Check taxes and total include exist
222
+ if (this.taxes.length === 0 || this.totalIncl.value === undefined)
223
+ return false;
224
+
225
+ // Reconstruct totalIncl from taxes
226
+ let totalVat = 0;
227
+ let reconstructedTotal = 0;
228
+ this.taxes.forEach((tax) => {
229
+ if (tax.value === undefined || !tax.rate) return false;
230
+ totalVat += tax.value;
231
+ reconstructedTotal += tax.value + (100 * tax.value) / tax.rate;
232
+ });
233
+
234
+ // Sanity check
235
+ if (totalVat <= 0) return false;
236
+
237
+ // Crate epsilon
238
+ const eps = 1 / (100 * totalVat);
239
+
240
+ if (
241
+ this.totalIncl.value * (1 - eps) - 0.02 <= reconstructedTotal &&
242
+ reconstructedTotal <= this.totalIncl.value * (1 + eps) + 0.02
243
+ ) {
244
+ this.taxes = this.taxes.map((tax) => ({ ...tax, probability: 1.0 }));
245
+ this.totalTax.probability = 1.0;
246
+ this.totalIncl.probability = 1.0;
247
+ return true;
248
+ }
249
+ return false;
250
+ }
251
+
252
+ /**
253
+ *
254
+ */
255
+ #taxesMatchTotalExcl() {
256
+ // Check taxes and total amount exist
257
+ if (this.taxes.length === 0 || this.totalExcl.value == null) return false;
258
+
259
+ // Reconstruct total_incl from taxes
260
+ let totalVat = 0;
261
+ let reconstructedTotal = 0;
262
+ this.taxes.forEach((tax) => {
263
+ if (tax.value == null || !tax.rate) return false;
264
+ totalVat += tax.value;
265
+ reconstructedTotal += (100 * tax.value) / tax.rate;
266
+ });
267
+
268
+ // Sanity check
269
+ if (totalVat <= 0) return false;
270
+
271
+ // Crate epsilon
272
+ const eps = 1 / (100 * totalVat);
273
+
274
+ if (
275
+ this.totalExcl.value * (1 - eps) - 0.02 <= reconstructedTotal &&
276
+ reconstructedTotal <= this.totalExcl.value * (1 + eps) + 0.02
277
+ ) {
278
+ this.taxes = this.taxes.map((tax) => ({ ...tax, probability: 1.0 }));
279
+ this.totalTax.probability = 1.0;
280
+ this.totalExcl.probability = 1.0;
281
+ return true;
282
+ }
283
+ return false;
284
+ }
285
+
286
+ #taxesPlusTotalExclMatchTotalIncl() {
287
+ if (
288
+ this.totalExcl.value === undefined ||
289
+ this.taxes.length == 0 ||
290
+ this.totalIncl === undefined
291
+ )
292
+ return false;
293
+ let totalVat = 0;
294
+ this.taxes.forEach((tax) => (totalVat += tax.value));
295
+ const reconstructedTotal = totalVat + this.totalExcl.value;
296
+
297
+ if (totalVat <= 0) return false;
298
+
299
+ if (
300
+ this.totalIncl.value - 0.01 <= reconstructedTotal &&
301
+ reconstructedTotal <= this.totalIncl.value + 0.01
302
+ ) {
303
+ this.taxes = this.taxes.map((tax) => ({ ...tax, probability: 1.0 }));
304
+ this.totalTax.probability = 1.0;
305
+ this.totalIncl.probability = 1.0;
306
+ return true;
307
+ }
308
+ return false;
309
+ }
310
+
311
+ #reconstructTotalTax() {
312
+ if (this.taxes.length > 0) {
313
+ const totalTax = {
314
+ value: this.taxes.reduce((acc, tax) => {
315
+ return tax.value !== undefined ? acc + tax.value : acc;
316
+ }, 0),
317
+ probability: Field.arrayProbability(this.taxes),
318
+ };
319
+ if (totalTax.value > 0)
320
+ this.totalTax = new Amount({
321
+ prediction: totalTax,
322
+ valueKey: "value",
323
+ reconstructed: true,
324
+ });
325
+ }
326
+ }
327
+
328
+ #reconstructTotalTaxFromTotals() {
329
+ if (
330
+ this.totalTax.value === undefined &&
331
+ this.totalIncl.value > 0 &&
332
+ this.totalExcl.value > 0 &&
333
+ this.totalExcl.value <= this.totalIncl.value
334
+ ) {
335
+ const totalTax = {
336
+ value: this.totalIncl.value - this.totalExcl.value,
337
+ probability: this.totalIncl.probability * this.totalExcl.probability,
338
+ };
339
+ if (totalTax.value > 0)
340
+ this.totalTax = new Amount({
341
+ prediction: totalTax,
342
+ valueKey: "value",
343
+ reconstructed: true,
344
+ });
345
+ }
346
+ }
347
+
348
+ #reconstructTotalExcl() {
349
+ if (
350
+ this.taxes.length &&
351
+ this.totalIncl.value != null &&
352
+ this.totalExcl.value === undefined
353
+ ) {
354
+ const totalExcl = {
355
+ value:
356
+ this.totalIncl.value -
357
+ this.taxes.reduce((acc, tax) => {
358
+ return tax.value !== undefined ? acc + tax.value : acc;
359
+ }, 0),
360
+ probability:
361
+ Field.arrayProbability(this.taxes) * this.totalIncl.probability,
362
+ };
363
+ this.totalExcl = new Amount({
364
+ prediction: totalExcl,
365
+ valueKey: "value",
366
+ reconstructed: true,
367
+ });
368
+ }
369
+ }
370
+
371
+ #reconstructTotalIncl() {
372
+ if (
373
+ this.taxes.length &&
374
+ this.totalExcl.value != null &&
375
+ this.totalIncl.value === undefined
376
+ ) {
377
+ const totalIncl = {
378
+ value:
379
+ this.totalExcl.value +
380
+ this.taxes.reduce((acc, tax) => {
381
+ return tax.value ? acc + tax.value : acc;
382
+ }, 0.0),
383
+ probability:
384
+ Field.arrayProbability(this.taxes) * this.totalExcl.probability,
385
+ };
386
+ this.totalIncl = new Amount({
387
+ prediction: totalIncl,
388
+ valueKey: "value",
389
+ reconstructed: true,
390
+ });
391
+ }
392
+ }
393
+ }
394
+
395
+ module.exports = Invoice;
@@ -0,0 +1,272 @@
1
+ const Document = require("./document");
2
+ const Field = require("./fields").field;
3
+ const Date = require("./fields").date;
4
+ const Amount = require("./fields").amount;
5
+ const Locale = require("./fields").locale;
6
+ const Orientation = require("./fields").orientation;
7
+ const Tax = require("./fields").tax;
8
+ const fs = require("fs").promises;
9
+
10
+ class Receipt extends Document {
11
+ /**
12
+ * @param {Object} apiPrediction - Json parsed prediction from HTTP response
13
+ * @param {Input} input - Input object
14
+ * @param {Integer} pageNumber - Page number for multi pages pdf input
15
+ * @param {Object} locale - locale value for creating Receipt object from scratch
16
+ * @param {Object} totalIncl - total tax Included value for creating Receipt object from scratch
17
+ * @param {Object} date - date value for creating Receipt object from scratch
18
+ * @param {Object} category - category value for creating Receipt object from scratch
19
+ * @param {Object} merchantName - merchant name value for creating Receipt object from scratch
20
+ * @param {Object} time - time value for creating Receipt object from scratch
21
+ * @param {Object} taxes - taxes value for creating Receipt object from scratch
22
+ * @param {Object} orientation - orientation value for creating Receipt object from scratch
23
+ * @param {Object} totalTax - total taxes value for creating Receipt object from scratch
24
+ * @param {Object} totalExcl - total taxes excluded value for creating Receipt object from scratch
25
+ */
26
+ constructor({
27
+ apiPrediction = undefined,
28
+ inputFile = undefined,
29
+ locale = undefined,
30
+ totalIncl = undefined,
31
+ date = undefined,
32
+ category = undefined,
33
+ merchantName = undefined,
34
+ time = undefined,
35
+ taxes = undefined,
36
+ orientation = undefined,
37
+ totalTax = undefined,
38
+ totalExcl = undefined,
39
+ pageNumber = 0,
40
+ }) {
41
+ super(inputFile);
42
+ if (apiPrediction === undefined) {
43
+ this.#initFromScratch({
44
+ locale,
45
+ totalExcl,
46
+ totalIncl,
47
+ date,
48
+ category,
49
+ merchantName,
50
+ time,
51
+ taxes,
52
+ orientation,
53
+ totalTax,
54
+ pageNumber,
55
+ });
56
+ } else {
57
+ this.#initFromApiPrediction(apiPrediction, pageNumber);
58
+ }
59
+ this.#checklist();
60
+ this.#reconstruct();
61
+ }
62
+
63
+ #initFromScratch({
64
+ locale,
65
+ totalExcl,
66
+ totalIncl,
67
+ date,
68
+ category,
69
+ merchantName,
70
+ time,
71
+ taxes,
72
+ orientation,
73
+ totalTax,
74
+ pageNumber,
75
+ }) {
76
+ const constructPrediction = function (item) {
77
+ return { prediction: { value: item }, valueKey: "value", pageNumber };
78
+ };
79
+ this.locale = new Locale(constructPrediction(locale));
80
+ this.totalIncl = new Amount(constructPrediction(totalIncl));
81
+ this.date = new Date(constructPrediction(date));
82
+ this.category = new Field(constructPrediction(category));
83
+ this.merchantName = new Field(constructPrediction(merchantName));
84
+ this.time = new Field(constructPrediction(time));
85
+ if (taxes !== undefined) {
86
+ this.taxes = [];
87
+ for (const t of taxes) {
88
+ this.taxes.push(
89
+ new Tax({
90
+ prediction: { value: t[0], rate: t[1] },
91
+ pageNumber,
92
+ valueKey: "value",
93
+ rateKey: "rate",
94
+ })
95
+ );
96
+ }
97
+ }
98
+ this.orientation = new Orientation(constructPrediction(orientation));
99
+ this.totalTax = new Amount(constructPrediction(totalTax));
100
+ this.totalExcl = new Amount(constructPrediction(totalExcl));
101
+ }
102
+
103
+ /**
104
+ Set the object attributes with api prediction values
105
+ @param apiPrediction: Raw prediction from HTTP response
106
+ @param pageNumber: Page number for multi pages pdf input
107
+ */
108
+ #initFromApiPrediction(apiPrediction, pageNumber) {
109
+ this.words = [];
110
+ this.locale = new Locale({ prediction: apiPrediction.locale, pageNumber });
111
+ this.totalIncl = new Amount({
112
+ prediction: apiPrediction.total_incl,
113
+ valueKey: "value",
114
+ pageNumber,
115
+ });
116
+ this.date = new Date({
117
+ prediction: apiPrediction.date,
118
+ valueKey: "value",
119
+ pageNumber,
120
+ });
121
+ this.category = new Field({
122
+ prediction: apiPrediction.category,
123
+ pageNumber,
124
+ });
125
+ this.merchantName = new Field({
126
+ prediction: apiPrediction.supplier,
127
+ valueKey: "value",
128
+ pageNumber,
129
+ });
130
+ this.time = new Field({
131
+ prediction: apiPrediction.time,
132
+ valueKey: "value",
133
+ pageNumber,
134
+ });
135
+ this.taxes = apiPrediction.taxes.map(
136
+ (taxPrediction) =>
137
+ new Tax({
138
+ prediction: taxPrediction,
139
+ pageNumber,
140
+ valueKey: "value",
141
+ rateKey: "rate",
142
+ codeKey: "code",
143
+ })
144
+ );
145
+ this.orientation = new Orientation({
146
+ prediction: apiPrediction.orientation,
147
+ pageNumber,
148
+ });
149
+ this.totalTax = new Amount({
150
+ prediction: { value: undefined, probability: 0 },
151
+ valueKey: "value",
152
+ pageNumber,
153
+ });
154
+ this.totalExcl = new Amount({
155
+ prediction: { value: undefined, probability: 0 },
156
+ valueKey: "value",
157
+ pageNumber,
158
+ });
159
+ if ("mvision" in apiPrediction) this.words = apiPrediction.mvision;
160
+ }
161
+
162
+ toString() {
163
+ return `
164
+ -----Receipt data-----
165
+ Filename: ${this.filename}
166
+ Total amount: ${this.totalIncl.value}
167
+ Date: ${this.date.value}
168
+ Category: ${this.category.value}
169
+ Time: ${this.time.value}
170
+ Merchant name: ${this.merchantName.value}
171
+ Taxes: ${this.taxes.map((tax) => tax.toString()).join(" - ")}
172
+ Total taxes: ${this.totalTax.value}
173
+ `;
174
+ }
175
+
176
+ static async load(path) {
177
+ const file = fs.readFile(path);
178
+ const args = JSON.parse(file);
179
+ return new Receipt({ reconsctruted: true, ...args });
180
+ }
181
+
182
+ /**
183
+ * Call all check methods
184
+ */
185
+ #checklist() {
186
+ this.checklist = { taxesMatchTotalIncl: this.#taxesMatchTotal() };
187
+ }
188
+
189
+ #taxesMatchTotal() {
190
+ // Check taxes and total amount exist
191
+
192
+ if (this.taxes.length === 0 || this.totalIncl.value == null) return false;
193
+
194
+ // Reconstruct total_incl from taxes
195
+ let totalVat = 0;
196
+ let reconstructedTotal = 0;
197
+ this.taxes.forEach((tax) => {
198
+ if (tax.value == null || !tax.rate) return false;
199
+ totalVat += tax.value;
200
+ reconstructedTotal += tax.value + (100 * tax.value) / tax.rate;
201
+ });
202
+
203
+ // Sanity check
204
+ if (totalVat <= 0) return false;
205
+
206
+ // Crate epsilon
207
+ const eps = 1 / (100 * totalVat);
208
+
209
+ if (
210
+ this.totalIncl.value * (1 - eps) - 0.02 <= reconstructedTotal &&
211
+ reconstructedTotal <= this.totalIncl.value * (1 + eps) + 0.02
212
+ ) {
213
+ this.taxes = this.taxes.map((tax) => ({ ...tax, probability: 1.0 }));
214
+ this.totalTax.probability = 1.0;
215
+ this.totalIncl.probability = 1.0;
216
+ return true;
217
+ }
218
+ return false;
219
+ }
220
+
221
+ /**
222
+ * Call all fields that need to be reconstructed
223
+ */
224
+ #reconstruct() {
225
+ this.#reconstructTotalExclFromTCCAndTaxes();
226
+ this.#reconstructTotalTax();
227
+ }
228
+
229
+ /**
230
+ * Set this.totalExcl with Amount object
231
+ * The totalExcl Amount value is the difference between totalIncl and sum of taxes
232
+ * The totalExcl Amount probability is the product of this.taxes probabilities multiplied by totalIncl probability
233
+ */
234
+ #reconstructTotalExclFromTCCAndTaxes() {
235
+ if (this.taxes.length && this.totalIncl.value != null) {
236
+ const totalExcl = {
237
+ value: this.totalIncl.value - Field.arraySum(this.taxes),
238
+ probability:
239
+ Field.arrayProbability(this.taxes) * this.totalIncl.probability,
240
+ };
241
+ this.totalExcl = new Amount({
242
+ prediction: totalExcl,
243
+ valueKey: "value",
244
+ reconstructed: true,
245
+ });
246
+ }
247
+ }
248
+
249
+ /**
250
+ * Set this.totalTax with Amount object
251
+ * The totalTax Amount value is the sum of all this.taxes value
252
+ * The totalTax Amount probability is the product of this.taxes probabilities
253
+ */
254
+ #reconstructTotalTax() {
255
+ if (this.taxes.length && this.totalTax.value == null) {
256
+ const totalTax = {
257
+ value: this.taxes
258
+ .map((tax) => tax.value || 0)
259
+ .reduce((a, b) => a + b, 0),
260
+ probability: Field.arrayProbability(this.taxes),
261
+ };
262
+ if (totalTax.value > 0)
263
+ this.totalTax = new Amount({
264
+ prediction: totalTax,
265
+ valueKey: "value",
266
+ reconstructed: true,
267
+ });
268
+ }
269
+ }
270
+ }
271
+
272
+ module.exports = Receipt;
@@ -0,0 +1,16 @@
1
+ const logger = require("../logger");
2
+
3
+ class ErrorHandler {
4
+ constructor(throwOnError = true) {
5
+ this.throwOnError = throwOnError;
6
+ }
7
+
8
+ throw(error, force = true) {
9
+ if (this.throwOnError || force) throw error;
10
+ else logger.error(error.message);
11
+ }
12
+ }
13
+
14
+ const errorHandler = new ErrorHandler();
15
+
16
+ module.exports = errorHandler;