mindee 1.1.2 → 1.3.1

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 (46) hide show
  1. package/CHANGELOG.md +47 -26
  2. package/LICENSE +21 -202
  3. package/README.md +15 -2
  4. package/coverage.lcov +0 -0
  5. package/dist/api/financialDocument.js +51 -0
  6. package/dist/api/index.js +13 -0
  7. package/dist/api/invoice.js +59 -0
  8. package/dist/api/object.js +80 -0
  9. package/dist/api/receipt.js +45 -0
  10. package/dist/api/request.js +90 -0
  11. package/dist/api/response.js +91 -0
  12. package/dist/documents/document.js +80 -0
  13. package/dist/documents/fields/amount.js +21 -0
  14. package/dist/documents/fields/date.js +17 -0
  15. package/dist/documents/fields/field.js +85 -0
  16. package/dist/documents/fields/index.js +17 -0
  17. package/dist/documents/fields/locale.js +25 -0
  18. package/dist/documents/fields/orientation.js +22 -0
  19. package/dist/documents/fields/paymentDetails.js +50 -0
  20. package/dist/documents/fields/tax.js +42 -0
  21. package/dist/documents/financialDocument.js +216 -0
  22. package/dist/documents/index.js +13 -0
  23. package/dist/documents/invoice.js +361 -0
  24. package/dist/documents/receipt.js +231 -0
  25. package/dist/errors/handler.js +17 -0
  26. package/dist/index.js +23 -0
  27. package/dist/inputs.js +208 -0
  28. package/dist/logger.js +34 -0
  29. package/lib/api/financialDocument.js +1 -1
  30. package/lib/api/invoice.js +1 -1
  31. package/lib/api/response.js +1 -1
  32. package/lib/documents/fields/field.js +4 -2
  33. package/lib/documents/financialDocument.js +8 -51
  34. package/lib/documents/invoice.js +9 -48
  35. package/lib/documents/receipt.js +5 -5
  36. package/lib/index.js +29 -30
  37. package/lib/inputs.js +2 -0
  38. package/mindee/api/financialDocument.js +3 -3
  39. package/mindee/api/object.js +1 -1
  40. package/mindee/api/request.js +17 -15
  41. package/mindee/api/response.js +19 -3
  42. package/mindee/documents/financialDocument.js +7 -2
  43. package/mindee/documents/invoice.js +4 -5
  44. package/mindee/documents/receipt.js +4 -4
  45. package/mindee/inputs.js +14 -8
  46. package/package.json +5 -5
@@ -0,0 +1,216 @@
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 _FinancialDocument_instances, _FinancialDocument_initFromScratch, _FinancialDocument_initFromApiPrediction, _FinancialDocument_checklist, _FinancialDocument_taxesMatchTotalIncl;
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.FinancialDocument = void 0;
10
+ const document_1 = require("@documents/document");
11
+ const invoice_1 = require("@documents/invoice");
12
+ const receipt_1 = require("@documents/receipt");
13
+ const fields_1 = require("@documents/fields");
14
+ class FinancialDocument extends document_1.Document {
15
+ /**
16
+ * @param {Object} apiPrediction - Json parsed prediction from HTTP response
17
+ * @param {Input} input - Input object
18
+ * @param {Integer} pageNumber - Page number for multi pages pdf input
19
+ * @param {Object} locale - locale value for creating FinancialDocument object from scratch
20
+ * @param {Object} totalIncl - total tax included value for creating FinancialDocument object from scratch
21
+ * @param {Object} totalExcl - total tax excluded value for creating FinancialDocument object from scratch
22
+ * @param {Object} Date - date value for creating FinancialDocument object from scratch
23
+ * @param {Object} InvoiceNumber - Invoice number value for creating FinancialDocument object from scratch
24
+ * @param {Object} taxes - taxes value for creating FinancialDocument object from scratch
25
+ * @param {Object} supplier - supplier value for creating FinancialDocument object from scratch
26
+ * @param {Object} supplierAddress - supplier address value for creating FinancialDocument object from scratch
27
+ * @param {Object} paymentDetails - payment details value for creating FinancialDocument object from scratch
28
+ * @param {Object} companyNumber - company number value for creating FinancialDocument object from scratch
29
+ * @param {Object} vatNumber - vat number value for creating FinancialDocument object from scratch
30
+ * @param {Object} orientation - orientation value for creating FinancialDocument object from scratch
31
+ * @param {Object} totalTax - total tax value for creating FinancialDocument object from scratch
32
+ * @param {Object} time - time value for creating FinancialDocument object from scratch
33
+ * @param {Object} customerName - customer name value for creating FinancialDocument object from scratch
34
+ * @param {Object} customerAddress - customer address value for creating FinancialDocument object from scratch
35
+ * @param {Object} customerCompanyRegistration - customer company registration value for creating FinancialDocument object from scratch
36
+ * @param {Object} pageNumber - pageNumber for multi pages pdf input
37
+ * @param {String} level - specify whether object is built from "page" level or "document" level prediction
38
+ */
39
+ constructor({ apiPrediction = undefined, inputFile = undefined, locale = undefined, totalIncl = undefined, totalExcl = undefined, date = undefined, invoiceNumber = undefined, dueDate = undefined, taxes = undefined, supplier = undefined, supplierAddress = undefined, paymentDetails = undefined, companyNumber = undefined, vatNumber = undefined, orientation = undefined, totalTax = undefined, time = undefined, customerName = undefined, customerAddress = undefined, customerCompanyRegistration = undefined, words = undefined, pageNumber = 0, level = "page", }) {
40
+ super(inputFile);
41
+ _FinancialDocument_instances.add(this);
42
+ this.level = level;
43
+ if (apiPrediction === undefined) {
44
+ __classPrivateFieldGet(this, _FinancialDocument_instances, "m", _FinancialDocument_initFromScratch).call(this, {
45
+ locale,
46
+ totalIncl,
47
+ totalExcl,
48
+ date,
49
+ invoiceNumber,
50
+ dueDate,
51
+ taxes,
52
+ supplier,
53
+ supplierAddress,
54
+ paymentDetails,
55
+ companyNumber,
56
+ vatNumber,
57
+ orientation,
58
+ pageNumber,
59
+ totalTax,
60
+ time,
61
+ customerName,
62
+ customerAddress,
63
+ customerCompanyRegistration,
64
+ });
65
+ }
66
+ else {
67
+ __classPrivateFieldGet(this, _FinancialDocument_instances, "m", _FinancialDocument_initFromApiPrediction).call(this, apiPrediction, inputFile, pageNumber, words);
68
+ }
69
+ __classPrivateFieldGet(this, _FinancialDocument_instances, "m", _FinancialDocument_checklist).call(this);
70
+ }
71
+ toString() {
72
+ return `
73
+ -----Financial document-----
74
+ Filename: ${this.filename}
75
+ Total amount: ${this.totalIncl.value}
76
+ Date: ${this.date.value}
77
+ Supplier: ${this.supplier.value}
78
+ Total taxes: ${this.totalTax.value}
79
+ `;
80
+ }
81
+ }
82
+ exports.FinancialDocument = FinancialDocument;
83
+ _FinancialDocument_instances = new WeakSet(), _FinancialDocument_initFromScratch = function _FinancialDocument_initFromScratch({ locale, totalIncl, totalExcl, totalTax, date, invoiceNumber, dueDate, taxes, paymentDetails, companyNumber, vatNumber, orientation, pageNumber, supplier, supplierAddress, time, customerName, customerAddress, customerCompanyRegistration, }) {
84
+ const constructPrediction = function (item) {
85
+ return { prediction: { value: item }, valueKey: "value", pageNumber };
86
+ };
87
+ this.locale = new fields_1.Locale(constructPrediction(locale));
88
+ this.totalIncl = new fields_1.Amount(constructPrediction(totalIncl));
89
+ this.totalExcl = new fields_1.Amount(constructPrediction(totalExcl));
90
+ this.totalTax = new fields_1.Amount(constructPrediction(totalTax));
91
+ this.date = new fields_1.DateField(constructPrediction(date));
92
+ this.dueDate = new fields_1.DateField(constructPrediction(dueDate));
93
+ this.supplier = new fields_1.Field(constructPrediction(supplier));
94
+ this.supplierAddress = new fields_1.Field(this.constructPrediction(supplierAddress));
95
+ this.time = new fields_1.Field(constructPrediction(time));
96
+ this.orientation = new fields_1.Orientation(constructPrediction(orientation));
97
+ this.invoiceNumber = new fields_1.Field(constructPrediction(invoiceNumber));
98
+ this.paymentDetails = new fields_1.Field(constructPrediction(paymentDetails));
99
+ this.companyNumber = new fields_1.Field(constructPrediction(companyNumber));
100
+ this.vatNumber = new fields_1.Field(constructPrediction(vatNumber));
101
+ this.customerName = new fields_1.Field(this.constructPrediction(customerName));
102
+ this.customerAddress = new fields_1.Field(this.constructPrediction(customerAddress));
103
+ this.customerCompanyRegistration = new fields_1.Field(this.constructPrediction(customerCompanyRegistration));
104
+ if (taxes !== undefined) {
105
+ this.taxes = [];
106
+ for (const t of taxes) {
107
+ this.taxes.push(new fields_1.Tax({
108
+ prediction: { value: t[0], rate: t[1] },
109
+ pageNumber,
110
+ valueKey: "value",
111
+ rateKey: "rate",
112
+ }));
113
+ }
114
+ }
115
+ }, _FinancialDocument_initFromApiPrediction = function _FinancialDocument_initFromApiPrediction(apiPrediction, inputFile, pageNumber, words) {
116
+ if (Object.keys(apiPrediction).includes("invoice_number")) {
117
+ const invoice = new invoice_1.Invoice({
118
+ apiPrediction,
119
+ inputFile,
120
+ pageNumber,
121
+ level: this.level,
122
+ words: words,
123
+ });
124
+ this.locale = invoice.locale;
125
+ this.totalIncl = invoice.totalIncl;
126
+ this.totalExcl = invoice.totalExcl;
127
+ this.date = invoice.invoiceDate;
128
+ this.invoiceNumber = invoice.invoiceNumber;
129
+ this.dueDate = invoice.dueDate;
130
+ this.taxes = invoice.taxes;
131
+ this.supplier = invoice.supplier;
132
+ this.supplierAddress = invoice.supplierAddress;
133
+ this.paymentDetails = invoice.paymentDetails;
134
+ this.companyNumber = invoice.companyNumber;
135
+ this.orientation = invoice.orientation;
136
+ this.totalTax = invoice.totalTax;
137
+ if (invoice.words)
138
+ this.words = invoice.words;
139
+ this.time = new fields_1.Field({
140
+ prediction: { value: undefined, confidence: 0.0 },
141
+ });
142
+ this.customerName = invoice.customerName;
143
+ this.customerAddress = invoice.customerAddress;
144
+ this.customerCompanyRegistration = invoice.customerCompanyRegistration;
145
+ }
146
+ else {
147
+ const receipt = new receipt_1.Receipt({
148
+ apiPrediction,
149
+ inputFile,
150
+ pageNumber,
151
+ level: this.level,
152
+ words: words,
153
+ });
154
+ this.orientation = receipt.orientation;
155
+ this.date = receipt.date;
156
+ this.dueDate = receipt.date;
157
+ this.taxes = receipt.taxes;
158
+ this.locale = receipt.locale;
159
+ this.totalIncl = receipt.totalIncl;
160
+ this.totalExcl = receipt.totalExcl;
161
+ this.supplier = receipt.merchantName;
162
+ this.supplierAddress = new fields_1.Field({
163
+ prediction: { value: undefined, confidence: 0.0 },
164
+ });
165
+ this.time = receipt.time;
166
+ this.totalTax = receipt.totalTax;
167
+ this.invoiceNumber = new fields_1.Field({
168
+ prediction: { value: undefined, confidence: 0.0 },
169
+ });
170
+ this.paymentDetails = new fields_1.Field({
171
+ prediction: { value: undefined, confidence: 0.0 },
172
+ });
173
+ this.companyNumber = new fields_1.Field({
174
+ prediction: { value: undefined, confidence: 0.0 },
175
+ });
176
+ this.customerName = new fields_1.Field({
177
+ prediction: { value: undefined, confidence: 0.0 },
178
+ });
179
+ this.customerAddress = new fields_1.Field({
180
+ prediction: { value: undefined, confidence: 0.0 },
181
+ });
182
+ this.customerCompanyRegistration = [];
183
+ if (receipt.words)
184
+ this.words = receipt.words;
185
+ }
186
+ }, _FinancialDocument_checklist = function _FinancialDocument_checklist() {
187
+ this.checklist = {
188
+ taxesMatchTotalIncl: __classPrivateFieldGet(this, _FinancialDocument_instances, "m", _FinancialDocument_taxesMatchTotalIncl).call(this),
189
+ };
190
+ }, _FinancialDocument_taxesMatchTotalIncl = function _FinancialDocument_taxesMatchTotalIncl() {
191
+ // Check taxes and total include exist
192
+ if (this.taxes.length === 0 || this.totalIncl.value === undefined)
193
+ return false;
194
+ // Reconstruct totalIncl from taxes
195
+ let totalVat = 0;
196
+ let reconstructedTotal = 0;
197
+ this.taxes.forEach((tax) => {
198
+ if (tax.value === undefined || !tax.rate)
199
+ return false;
200
+ totalVat += tax.value;
201
+ reconstructedTotal += tax.value + (100 * tax.value) / tax.rate;
202
+ });
203
+ // Sanity check
204
+ if (totalVat <= 0)
205
+ return false;
206
+ // Crate epsilon
207
+ const eps = 1 / (100 * totalVat);
208
+ if (this.totalIncl.value * (1 - eps) - 0.02 <= reconstructedTotal &&
209
+ reconstructedTotal <= this.totalIncl.value * (1 + eps) + 0.02) {
210
+ this.taxes = this.taxes.map((tax) => (Object.assign(Object.assign({}, tax), { confidence: 1.0 })));
211
+ this.totalTax.probability = 1.0;
212
+ this.totalIncl.probability = 1.0;
213
+ return true;
214
+ }
215
+ return false;
216
+ };
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FinancialDocument = exports.Invoice = exports.Receipt = exports.Document = exports.Field = void 0;
4
+ var fields_1 = require("@documents/fields");
5
+ Object.defineProperty(exports, "Field", { enumerable: true, get: function () { return fields_1.Field; } });
6
+ var document_1 = require("@documents/document");
7
+ Object.defineProperty(exports, "Document", { enumerable: true, get: function () { return document_1.Document; } });
8
+ var receipt_1 = require("@documents/receipt");
9
+ Object.defineProperty(exports, "Receipt", { enumerable: true, get: function () { return receipt_1.Receipt; } });
10
+ var invoice_1 = require("@documents/invoice");
11
+ Object.defineProperty(exports, "Invoice", { enumerable: true, get: function () { return invoice_1.Invoice; } });
12
+ var financialDocument_1 = require("@documents/financialDocument");
13
+ Object.defineProperty(exports, "FinancialDocument", { enumerable: true, get: function () { return financialDocument_1.FinancialDocument; } });
@@ -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
+ };