mindee 1.4.0 → 2.0.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.
Files changed (89) hide show
  1. package/CHANGELOG.md +33 -19
  2. package/README.md +43 -127
  3. package/bin/mindee +3 -0
  4. package/dist/package.json +70 -0
  5. package/dist/src/api/endpoint.d.ts +29 -0
  6. package/dist/src/api/endpoint.js +148 -0
  7. package/dist/src/api/index.d.ts +2 -0
  8. package/dist/src/api/index.js +18 -0
  9. package/dist/src/api/response.d.ts +52 -0
  10. package/dist/src/api/response.js +107 -0
  11. package/dist/src/cli.d.ts +1 -0
  12. package/dist/src/cli.js +117 -0
  13. package/dist/src/client.d.ts +48 -0
  14. package/dist/src/client.js +134 -0
  15. package/dist/src/constants.d.ts +13 -0
  16. package/dist/src/constants.js +54 -0
  17. package/dist/src/documents/custom.d.ts +14 -0
  18. package/dist/src/documents/custom.js +48 -0
  19. package/dist/src/documents/document.d.ts +38 -0
  20. package/dist/src/documents/document.js +60 -0
  21. package/dist/src/documents/documentConfig.d.ts +44 -0
  22. package/dist/src/documents/documentConfig.js +121 -0
  23. package/dist/src/documents/financialDocument.d.ts +32 -0
  24. package/dist/src/documents/financialDocument.js +130 -0
  25. package/dist/src/documents/index.d.ts +12 -0
  26. package/dist/src/documents/index.js +20 -0
  27. package/dist/src/documents/invoice.d.ts +37 -0
  28. package/dist/src/documents/invoice.js +303 -0
  29. package/dist/src/documents/passport.d.ts +36 -0
  30. package/dist/src/documents/passport.js +218 -0
  31. package/dist/src/documents/receipt.d.ts +24 -0
  32. package/dist/src/documents/receipt.js +157 -0
  33. package/dist/src/errors/handler.d.ts +7 -0
  34. package/dist/src/errors/handler.js +18 -0
  35. package/dist/src/fields/amount.d.ts +13 -0
  36. package/dist/src/fields/amount.js +34 -0
  37. package/dist/src/fields/companyRegistration.d.ts +6 -0
  38. package/dist/src/fields/companyRegistration.js +11 -0
  39. package/dist/src/fields/customDocs.d.ts +42 -0
  40. package/dist/src/fields/customDocs.js +61 -0
  41. package/dist/src/fields/date.d.ts +12 -0
  42. package/dist/src/fields/date.js +25 -0
  43. package/dist/src/fields/field.d.ts +74 -0
  44. package/dist/src/fields/field.js +106 -0
  45. package/dist/src/fields/fullText.d.ts +24 -0
  46. package/dist/src/fields/fullText.js +89 -0
  47. package/dist/src/fields/index.d.ts +11 -0
  48. package/dist/src/fields/index.js +27 -0
  49. package/dist/src/fields/locale.d.ts +13 -0
  50. package/dist/src/fields/locale.js +40 -0
  51. package/dist/src/fields/orientation.d.ts +10 -0
  52. package/dist/src/fields/orientation.js +22 -0
  53. package/dist/src/fields/paymentDetails.d.ts +31 -0
  54. package/dist/src/fields/paymentDetails.js +58 -0
  55. package/dist/src/fields/tax.d.ts +30 -0
  56. package/dist/src/fields/tax.js +51 -0
  57. package/dist/src/geometry.d.ts +59 -0
  58. package/dist/src/geometry.js +113 -0
  59. package/dist/src/index.d.ts +2 -0
  60. package/dist/src/index.js +12 -0
  61. package/dist/src/inputs.d.ts +69 -0
  62. package/dist/src/inputs.js +223 -0
  63. package/dist/src/logger.d.ts +20 -0
  64. package/dist/src/logger.js +35 -0
  65. package/package.json +38 -28
  66. package/mindee/api/financialDocument.js +0 -47
  67. package/mindee/api/index.js +0 -5
  68. package/mindee/api/invoice.js +0 -53
  69. package/mindee/api/object.js +0 -82
  70. package/mindee/api/receipt.js +0 -39
  71. package/mindee/api/request.js +0 -72
  72. package/mindee/api/response.js +0 -80
  73. package/mindee/documents/document.js +0 -69
  74. package/mindee/documents/fields/amount.js +0 -25
  75. package/mindee/documents/fields/date.js +0 -29
  76. package/mindee/documents/fields/field.js +0 -86
  77. package/mindee/documents/fields/index.js +0 -7
  78. package/mindee/documents/fields/locale.js +0 -30
  79. package/mindee/documents/fields/orientation.js +0 -24
  80. package/mindee/documents/fields/paymentDetails.js +0 -52
  81. package/mindee/documents/fields/tax.js +0 -47
  82. package/mindee/documents/financialDocument.js +0 -269
  83. package/mindee/documents/index.js +0 -5
  84. package/mindee/documents/invoice.js +0 -456
  85. package/mindee/documents/receipt.js +0 -287
  86. package/mindee/errors/handler.js +0 -16
  87. package/mindee/index.js +0 -39
  88. package/mindee/inputs.js +0 -181
  89. package/mindee/logger.js +0 -34
@@ -1,456 +0,0 @@
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 {Object} locale - locale value for creating Invoice object from scratch
15
- * @param {Object} totalIncl - total tax included value for creating Invoice object from scratch
16
- * @param {Object} totalExcl - total tax excluded value for creating Invoice object from scratch
17
- * @param {Object} invoiceDate - invoice date value for creating Invoice object from scratch
18
- * @param {Object} invoiceNumber - invoice number value for creating Invoice object from scratch
19
- * @param {Object} taxes - taxes value for creating Invoice object from scratch
20
- * @param {Object} supplier - supplier value for creating Invoice object from scratch
21
- * @param {Object} supplierAddress - supplier address 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} customerName - customer name value for creating Invoice object from scratch
28
- * @param {Object} customerAddress - customer address value for creating Invoice object from scratch
29
- * @param {Object} customerCompanyRegistration - customer company registration value for creating Invoice object from scratch
30
- * @param {Number} pageNumber - pageNumber for multi pages pdf input
31
- * @param {String} level - specify whether object is built from "page" level or "document" level prediction
32
- */
33
- constructor({
34
- apiPrediction = undefined,
35
- inputFile = undefined,
36
- locale = undefined,
37
- totalIncl = undefined,
38
- totalExcl = undefined,
39
- invoiceDate = undefined,
40
- invoiceNumber = undefined,
41
- dueDate = undefined,
42
- taxes = undefined,
43
- supplier = undefined,
44
- supplierAddress = undefined,
45
- paymentDetails = undefined,
46
- companyNumber = undefined,
47
- vatNumber = undefined,
48
- orientation = undefined,
49
- totalTax = undefined,
50
- customerName = undefined,
51
- customerAddress = undefined,
52
- customerCompanyRegistration = undefined,
53
- words = undefined,
54
- pageNumber = 0,
55
- level = "page",
56
- }) {
57
- super(inputFile);
58
- this.level = level;
59
- this.constructPrediction = function (item) {
60
- return { prediction: { value: item }, valueKey: "value", pageNumber };
61
- };
62
- if (apiPrediction === undefined) {
63
- this.#initFromScratch({
64
- locale,
65
- totalIncl,
66
- totalExcl,
67
- invoiceDate,
68
- invoiceNumber,
69
- dueDate,
70
- taxes,
71
- supplier,
72
- supplierAddress,
73
- paymentDetails,
74
- companyNumber,
75
- vatNumber,
76
- orientation,
77
- pageNumber,
78
- totalTax,
79
- customerName,
80
- customerAddress,
81
- customerCompanyRegistration,
82
- });
83
- } else {
84
- this.#initFromApiPrediction(apiPrediction, pageNumber, words);
85
- }
86
- this.#checklist();
87
- this.#reconstruct();
88
- }
89
-
90
- #initFromScratch({
91
- locale,
92
- totalIncl,
93
- totalExcl,
94
- totalTax,
95
- invoiceDate,
96
- invoiceNumber,
97
- dueDate,
98
- taxes,
99
- supplier,
100
- supplierAddress,
101
- paymentDetails,
102
- companyNumber,
103
- vatNumber,
104
- orientation,
105
- pageNumber,
106
- customerName,
107
- customerAddress,
108
- customerCompanyRegistration,
109
- }) {
110
- this.locale = new Locale(this.constructPrediction(locale));
111
- this.totalIncl = new Amount(this.constructPrediction(totalIncl));
112
- this.totalExcl = new Amount(this.constructPrediction(totalExcl));
113
- this.totalTax = new Amount(this.constructPrediction(totalTax));
114
- this.date = new Date(this.constructPrediction(invoiceDate));
115
- this.invoiceDate = new Date(this.constructPrediction(invoiceDate));
116
- this.dueDate = new Date(this.constructPrediction(dueDate));
117
- this.supplier = new Field(this.constructPrediction(supplier));
118
- this.supplierAddress = new Field(this.constructPrediction(supplierAddress));
119
- this.invoiceNumber = new Field(this.constructPrediction(invoiceNumber));
120
- this.paymentDetails = new Field(this.constructPrediction(paymentDetails));
121
- this.companyNumber = new Field(this.constructPrediction(companyNumber));
122
- this.vatNumber = new Field(this.constructPrediction(vatNumber));
123
- this.customerName = new Field(this.constructPrediction(customerName));
124
- this.customerAddress = new Field(this.constructPrediction(customerAddress));
125
- this.customerCompanyRegistration = new Field(
126
- this.constructPrediction(customerCompanyRegistration)
127
- );
128
- if (taxes !== undefined) {
129
- this.taxes = [];
130
- for (const t of taxes) {
131
- this.taxes.push(
132
- new Tax({
133
- prediction: { value: t[0], rate: t[1] },
134
- pageNumber,
135
- valueKey: "value",
136
- rateKey: "rate",
137
- })
138
- );
139
- }
140
- }
141
- if (this.level === "page") {
142
- this.orientation = new Orientation(this.constructPrediction(orientation));
143
- }
144
- }
145
-
146
- #initFromApiPrediction(apiPrediction, pageNumber, words) {
147
- this.locale = new Locale({ prediction: apiPrediction.locale, pageNumber });
148
- this.totalIncl = new Amount({
149
- prediction: apiPrediction.total_incl,
150
- valueKey: "value",
151
- pageNumber,
152
- });
153
- this.totalTax = new Amount({
154
- prediction: { value: undefined, confidence: 0.0 },
155
- valueKey: "value",
156
- pageNumber,
157
- });
158
- this.totalExcl = new Amount({
159
- prediction: apiPrediction.total_excl,
160
- valueKey: "value",
161
- pageNumber,
162
- });
163
- this.date = new Date({
164
- prediction: apiPrediction.date,
165
- valueKey: "value",
166
- pageNumber,
167
- });
168
- this.invoiceDate = new Date({
169
- prediction: apiPrediction.date,
170
- valueKey: "value",
171
- pageNumber,
172
- });
173
- this.taxes = apiPrediction.taxes.map(function (taxPrediction) {
174
- return new Tax({
175
- prediction: taxPrediction,
176
- pageNumber,
177
- valueKey: "value",
178
- rateKey: "rate",
179
- codeKey: "code",
180
- });
181
- });
182
- this.companyNumber = apiPrediction.company_registration.map(function (
183
- companyNumber
184
- ) {
185
- return new Field({
186
- prediction: companyNumber,
187
- pageNumber,
188
- extraFields: ["type"],
189
- });
190
- });
191
- this.dueDate = new Date({
192
- prediction: apiPrediction.due_date,
193
- valueKey: "value",
194
- pageNumber,
195
- });
196
- this.invoiceNumber = new Field({
197
- prediction: apiPrediction.invoice_number,
198
- pageNumber,
199
- });
200
- this.supplier = new Field({
201
- prediction: apiPrediction.supplier,
202
- pageNumber,
203
- });
204
- this.supplierAddress = new Field({
205
- prediction: apiPrediction.supplier_address,
206
- pageNumber,
207
- });
208
- this.customerName = new Field({
209
- prediction: apiPrediction.customer,
210
- pageNumber,
211
- });
212
- this.customerAddress = new Field({
213
- prediction: apiPrediction.customer_address,
214
- pageNumber,
215
- });
216
- this.customerCompanyRegistration = apiPrediction.customer_company_registration.map(
217
- function (customerCompanyRegistration) {
218
- return new Field({
219
- prediction: customerCompanyRegistration,
220
- pageNumber,
221
- extraFields: ["type"],
222
- });
223
- }
224
- );
225
- this.paymentDetails = apiPrediction.payment_details.map(function (
226
- paymentDetail
227
- ) {
228
- return new PaymentDetails({ prediction: paymentDetail, pageNumber });
229
- });
230
- if (this.level === "page") {
231
- this.orientation = new Orientation({
232
- prediction: apiPrediction.orientation,
233
- pageNumber,
234
- });
235
- } else {
236
- this.orientation = new Orientation(
237
- this.constructPrediction({
238
- prediction: {
239
- value: undefined,
240
- confidence: 0.0,
241
- degrees: undefined,
242
- },
243
- })
244
- );
245
- }
246
- if (words && words.length > 0) this.words = words;
247
- }
248
-
249
- toString() {
250
- return `
251
- -----Invoice data-----
252
- Filename: ${this.filename}
253
- Invoice number: ${this.invoiceNumber.value}
254
- Total amount including taxes: ${this.totalIncl.value}
255
- Total amount excluding taxes: ${this.totalExcl.value}
256
- Invoice Date: ${this.invoiceDate.value}
257
- Supplier name: ${this.supplier.value}
258
- Supplier address: ${this.supplierAddress.value}
259
- Customer name: ${this.customerName.value}
260
- Customer address: ${this.customerAddress.value}
261
- Taxes: ${this.taxes.map((tax) => tax.toString()).join(" - ")}
262
- Total taxes: ${this.totalTax.value}
263
- `;
264
- }
265
-
266
- #checklist() {
267
- this.checklist = {
268
- taxesMatchTotalIncl: this.#taxesMatchTotalIncl(),
269
- taxesMatchTotalExcl: this.#taxesMatchTotalExcl(),
270
- taxesPlusTotalExclMatchTotalIncl: this.#taxesPlusTotalExclMatchTotalIncl(),
271
- };
272
- }
273
-
274
- #reconstruct() {
275
- this.#reconstructTotalTax();
276
- this.#reconstructTotalExcl();
277
- this.#reconstructTotalIncl();
278
- this.#reconstructTotalTaxFromTotals();
279
- }
280
-
281
- #taxesMatchTotalIncl() {
282
- // Check taxes and total include exist
283
- if (this.taxes.length === 0 || this.totalIncl.value === undefined)
284
- return false;
285
-
286
- // Reconstruct totalIncl from taxes
287
- let totalVat = 0;
288
- let reconstructedTotal = 0;
289
- this.taxes.forEach((tax) => {
290
- if (tax.value === undefined || !tax.rate) return false;
291
- totalVat += tax.value;
292
- reconstructedTotal += tax.value + (100 * tax.value) / tax.rate;
293
- });
294
-
295
- // Sanity check
296
- if (totalVat <= 0) return false;
297
-
298
- // Crate epsilon
299
- const eps = 1 / (100 * totalVat);
300
-
301
- if (
302
- this.totalIncl.value * (1 - eps) - 0.02 <= reconstructedTotal &&
303
- reconstructedTotal <= this.totalIncl.value * (1 + eps) + 0.02
304
- ) {
305
- this.taxes = this.taxes.map((tax) => ({ ...tax, probability: 1.0 }));
306
- this.totalTax.probability = 1.0;
307
- this.totalIncl.probability = 1.0;
308
- return true;
309
- }
310
- return false;
311
- }
312
-
313
- /**
314
- *
315
- */
316
- #taxesMatchTotalExcl() {
317
- // Check taxes and total amount exist
318
- if (this.taxes.length === 0 || this.totalExcl.value == null) return false;
319
-
320
- // Reconstruct total_incl from taxes
321
- let totalVat = 0;
322
- let reconstructedTotal = 0;
323
- this.taxes.forEach((tax) => {
324
- if (tax.value == null || !tax.rate) return false;
325
- totalVat += tax.value;
326
- reconstructedTotal += (100 * tax.value) / tax.rate;
327
- });
328
-
329
- // Sanity check
330
- if (totalVat <= 0) return false;
331
-
332
- // Crate epsilon
333
- const eps = 1 / (100 * totalVat);
334
-
335
- if (
336
- this.totalExcl.value * (1 - eps) - 0.02 <= reconstructedTotal &&
337
- reconstructedTotal <= this.totalExcl.value * (1 + eps) + 0.02
338
- ) {
339
- this.taxes = this.taxes.map((tax) => ({ ...tax, probability: 1.0 }));
340
- this.totalTax.probability = 1.0;
341
- this.totalExcl.probability = 1.0;
342
- return true;
343
- }
344
- return false;
345
- }
346
-
347
- #taxesPlusTotalExclMatchTotalIncl() {
348
- if (
349
- this.totalExcl.value === undefined ||
350
- this.taxes.length == 0 ||
351
- this.totalIncl === undefined
352
- )
353
- return false;
354
- let totalVat = 0;
355
- this.taxes.forEach((tax) => (totalVat += tax.value));
356
- const reconstructedTotal = totalVat + this.totalExcl.value;
357
-
358
- if (totalVat <= 0) return false;
359
-
360
- if (
361
- this.totalIncl.value - 0.01 <= reconstructedTotal &&
362
- reconstructedTotal <= this.totalIncl.value + 0.01
363
- ) {
364
- this.taxes = this.taxes.map((tax) => ({ ...tax, probability: 1.0 }));
365
- this.totalTax.probability = 1.0;
366
- this.totalIncl.probability = 1.0;
367
- return true;
368
- }
369
- return false;
370
- }
371
-
372
- #reconstructTotalTax() {
373
- if (this.taxes.length > 0) {
374
- const totalTax = {
375
- value: this.taxes.reduce((acc, tax) => {
376
- return tax.value !== undefined ? acc + tax.value : acc;
377
- }, 0),
378
- confidence: Field.arrayProbability(this.taxes),
379
- };
380
- if (totalTax.value > 0)
381
- this.totalTax = new Amount({
382
- prediction: totalTax,
383
- valueKey: "value",
384
- reconstructed: true,
385
- });
386
- }
387
- }
388
-
389
- #reconstructTotalTaxFromTotals() {
390
- if (
391
- this.totalTax.value === undefined &&
392
- this.totalIncl.value > 0 &&
393
- this.totalExcl.value > 0 &&
394
- this.totalExcl.value <= this.totalIncl.value
395
- ) {
396
- const totalTax = {
397
- value: this.totalIncl.value - this.totalExcl.value,
398
- confidence: this.totalIncl.probability * this.totalExcl.probability,
399
- };
400
- if (totalTax.value > 0)
401
- this.totalTax = new Amount({
402
- prediction: totalTax,
403
- valueKey: "value",
404
- reconstructed: true,
405
- });
406
- }
407
- }
408
-
409
- #reconstructTotalExcl() {
410
- if (
411
- this.taxes.length &&
412
- this.totalIncl.value != null &&
413
- this.totalExcl.value === undefined
414
- ) {
415
- const totalExcl = {
416
- value:
417
- this.totalIncl.value -
418
- this.taxes.reduce((acc, tax) => {
419
- return tax.value !== undefined ? acc + tax.value : acc;
420
- }, 0),
421
- confidence:
422
- Field.arrayProbability(this.taxes) * this.totalIncl.probability,
423
- };
424
- this.totalExcl = new Amount({
425
- prediction: totalExcl,
426
- valueKey: "value",
427
- reconstructed: true,
428
- });
429
- }
430
- }
431
-
432
- #reconstructTotalIncl() {
433
- if (
434
- this.taxes.length &&
435
- this.totalExcl.value != null &&
436
- this.totalIncl.value === undefined
437
- ) {
438
- const totalIncl = {
439
- value:
440
- this.totalExcl.value +
441
- this.taxes.reduce((acc, tax) => {
442
- return tax.value ? acc + tax.value : acc;
443
- }, 0.0),
444
- confidence:
445
- Field.arrayProbability(this.taxes) * this.totalExcl.probability,
446
- };
447
- this.totalIncl = new Amount({
448
- prediction: totalIncl,
449
- valueKey: "value",
450
- reconstructed: true,
451
- });
452
- }
453
- }
454
- }
455
-
456
- module.exports = Invoice;