mindee 1.0.9 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +27 -0
- package/LICENSE +202 -0
- package/README.md +13 -13
- package/coverage.lcov +0 -0
- package/lib/api/financialDocument.js +2 -2
- package/lib/api/invoice.js +1 -1
- package/lib/api/object.js +2 -2
- package/lib/api/request.js +6 -4
- package/lib/api/response.js +8 -5
- package/lib/documents/document.js +5 -1
- package/lib/documents/fields/field.js +3 -5
- package/lib/documents/financialDocument.js +81 -30
- package/lib/documents/invoice.js +96 -39
- package/lib/documents/receipt.js +44 -29
- package/mindee/api/financialDocument.js +8 -6
- package/mindee/api/invoice.js +1 -1
- package/mindee/api/object.js +2 -2
- package/mindee/api/request.js +8 -5
- package/mindee/api/response.js +24 -6
- package/mindee/documents/fields/field.js +3 -6
- package/mindee/documents/financialDocument.js +90 -25
- package/mindee/documents/invoice.js +93 -32
- package/mindee/documents/receipt.js +39 -24
- package/package.json +3 -3
- package/LICENSE.md +0 -674
package/mindee/api/object.js
CHANGED
|
@@ -10,7 +10,7 @@ const request = require("./request");
|
|
|
10
10
|
class APIObject {
|
|
11
11
|
constructor(apiToken = undefined, apiName = "") {
|
|
12
12
|
this.apiToken = apiToken;
|
|
13
|
-
this.baseUrl = "https://api.mindee.net/products";
|
|
13
|
+
this.baseUrl = "https://api.mindee.net/v1/products/mindee";
|
|
14
14
|
this.apiName = apiName;
|
|
15
15
|
}
|
|
16
16
|
|
|
@@ -54,7 +54,7 @@ class APIObject {
|
|
|
54
54
|
@returns {Response}
|
|
55
55
|
*/
|
|
56
56
|
wrapResponse(inputFile, response, documentType) {
|
|
57
|
-
if (response.statusCode
|
|
57
|
+
if (response.statusCode > 201) {
|
|
58
58
|
const errorMessage = JSON.stringify(response.data, null, 4);
|
|
59
59
|
errorHandler.throw(
|
|
60
60
|
new Error(
|
package/mindee/api/request.js
CHANGED
|
@@ -2,20 +2,23 @@ const https = require("https");
|
|
|
2
2
|
const { version: sdkVersion } = require("../../package.json");
|
|
3
3
|
const { URL } = require("url");
|
|
4
4
|
const FormData = require("form-data");
|
|
5
|
+
const os = require("os");
|
|
5
6
|
|
|
6
7
|
const request = (url, method, headers, input, includeWords = false) => {
|
|
7
8
|
return new Promise(function (resolve, reject) {
|
|
8
|
-
|
|
9
|
+
let form = new FormData();
|
|
9
10
|
let body;
|
|
10
|
-
headers["User-Agent"] = `mindee-node/${sdkVersion} node/${process.version}`;
|
|
11
11
|
|
|
12
|
+
headers["User-Agent"] = `mindee-api-nodejs@v${sdkVersion} nodejs-${
|
|
13
|
+
process.version
|
|
14
|
+
} ${os.type().toLowerCase()}`;
|
|
12
15
|
if (["path", "stream"].includes(input.inputType)) {
|
|
13
16
|
const fileParams = { filename: input.filename };
|
|
14
|
-
form.append("
|
|
17
|
+
form.append("document", input.fileObject, fileParams);
|
|
15
18
|
if (includeWords) form.append("include_mvision", "true");
|
|
16
19
|
headers = { ...headers, ...form.getHeaders() };
|
|
17
20
|
} else if (input.inputType === "base64") {
|
|
18
|
-
let body_obj = {
|
|
21
|
+
let body_obj = { document: input.fileObject };
|
|
19
22
|
if (includeWords) body_obj["include_mvision"] = "true";
|
|
20
23
|
body = JSON.stringify(body_obj);
|
|
21
24
|
headers["Content-Type"] = "application/json";
|
|
@@ -44,7 +47,7 @@ const request = (url, method, headers, input, includeWords = false) => {
|
|
|
44
47
|
data: JSON.parse(responseBody),
|
|
45
48
|
});
|
|
46
49
|
} catch (error) {
|
|
47
|
-
console.log(responseBody);
|
|
50
|
+
console.log(responseBody, error);
|
|
48
51
|
}
|
|
49
52
|
});
|
|
50
53
|
});
|
package/mindee/api/response.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
const Document = require("../documents").document;
|
|
2
1
|
const Receipt = require("../documents").receipt;
|
|
3
2
|
const Invoice = require("../documents").invoice;
|
|
4
3
|
const FinancialDocument = require("../documents").financialDocument;
|
|
@@ -33,29 +32,48 @@ class Response {
|
|
|
33
32
|
}
|
|
34
33
|
|
|
35
34
|
formatResponse() {
|
|
35
|
+
let http_data_document = this.httpResponse.data.document;
|
|
36
36
|
const constructors = {
|
|
37
37
|
receipt: (params) => new Receipt(params),
|
|
38
38
|
invoice: (params) => new Invoice(params),
|
|
39
39
|
financialDocument: (params) => new FinancialDocument(params),
|
|
40
40
|
};
|
|
41
|
-
|
|
41
|
+
|
|
42
|
+
const predictions = http_data_document.inference.pages.entries();
|
|
42
43
|
this[`${this.documentType}s`] = [];
|
|
44
|
+
let document_words_content = [];
|
|
43
45
|
|
|
44
46
|
// Create a list of Document (Receipt, Invoice...) for each page of the input document
|
|
45
47
|
for (const [pageNumber, prediction] of predictions) {
|
|
48
|
+
let page_words_content = [];
|
|
49
|
+
if (
|
|
50
|
+
"ocr" in http_data_document &&
|
|
51
|
+
Object.keys(http_data_document.ocr).length > 0
|
|
52
|
+
) {
|
|
53
|
+
page_words_content =
|
|
54
|
+
http_data_document.ocr["mvision-v1"].pages[pageNumber].all_words;
|
|
55
|
+
document_words_content.push(
|
|
56
|
+
...http_data_document.ocr["mvision-v1"].pages[pageNumber].all_words
|
|
57
|
+
);
|
|
58
|
+
}
|
|
46
59
|
this[`${this.documentType}s`].push(
|
|
47
60
|
constructors[this.documentType]({
|
|
48
|
-
apiPrediction: prediction,
|
|
61
|
+
apiPrediction: prediction.prediction,
|
|
49
62
|
inputFile: this.input,
|
|
50
63
|
pageNumber: pageNumber,
|
|
64
|
+
words: page_words_content,
|
|
51
65
|
})
|
|
52
66
|
);
|
|
53
67
|
}
|
|
54
68
|
|
|
55
69
|
// Merge the list of Document into a unique Document
|
|
56
|
-
this[this.documentType] =
|
|
57
|
-
|
|
58
|
-
|
|
70
|
+
this[this.documentType] = constructors[this.documentType]({
|
|
71
|
+
apiPrediction: http_data_document.inference.prediction,
|
|
72
|
+
inputFile: this.input,
|
|
73
|
+
pageNumber: http_data_document.n_pages,
|
|
74
|
+
level: "document",
|
|
75
|
+
words: document_words_content,
|
|
76
|
+
});
|
|
59
77
|
}
|
|
60
78
|
}
|
|
61
79
|
|
|
@@ -16,13 +16,10 @@ class Field {
|
|
|
16
16
|
this.pageNumber = pageNumber;
|
|
17
17
|
this.reconstructed = reconstructed;
|
|
18
18
|
this.value = undefined;
|
|
19
|
-
this.probability = 0.0;
|
|
20
|
-
this.bbox = [];
|
|
21
|
-
if (valueKey in prediction && prediction[valueKey] !==
|
|
19
|
+
this.probability = prediction.confidence ? prediction.confidence : 0.0;
|
|
20
|
+
this.bbox = prediction.polygon ? prediction.polygon : [];
|
|
21
|
+
if (valueKey in prediction && prediction[valueKey] !== null) {
|
|
22
22
|
this.value = prediction[valueKey];
|
|
23
|
-
if (prediction.probability) this.probability = prediction.probability;
|
|
24
|
-
if (prediction.segmentation)
|
|
25
|
-
this.bbox = prediction.segmentation.bounding_box || [];
|
|
26
23
|
if (extraFields) {
|
|
27
24
|
for (const fieldName of extraFields) {
|
|
28
25
|
this[fieldName] = prediction[fieldName];
|
|
@@ -19,14 +19,19 @@ class FinancialDocument extends Document {
|
|
|
19
19
|
* @param {Object} Date - date value for creating FinancialDocument object from scratch
|
|
20
20
|
* @param {Object} InvoiceNumber - Invoice number value for creating FinancialDocument object from scratch
|
|
21
21
|
* @param {Object} taxes - taxes value for creating FinancialDocument object from scratch
|
|
22
|
-
* @param {Object}
|
|
22
|
+
* @param {Object} supplier - supplier value for creating FinancialDocument object from scratch
|
|
23
|
+
* @param {Object} supplierAddress - supplier address value for creating FinancialDocument object from scratch
|
|
23
24
|
* @param {Object} paymentDetails - payment details value for creating FinancialDocument object from scratch
|
|
24
25
|
* @param {Object} companyNumber - company number value for creating FinancialDocument object from scratch
|
|
25
26
|
* @param {Object} vatNumber - vat number value for creating FinancialDocument object from scratch
|
|
26
27
|
* @param {Object} orientation - orientation value for creating FinancialDocument object from scratch
|
|
27
28
|
* @param {Object} totalTax - total tax value for creating FinancialDocument object from scratch
|
|
28
29
|
* @param {Object} time - time value for creating FinancialDocument object from scratch
|
|
30
|
+
* @param {Object} customerName - customer name value for creating FinancialDocument object from scratch
|
|
31
|
+
* @param {Object} customerAddress - customer address value for creating FinancialDocument object from scratch
|
|
32
|
+
* @param {Object} customerCompanyRegistration - customer company registration value for creating FinancialDocument object from scratch
|
|
29
33
|
* @param {Object} pageNumber - pageNumber for multi pages pdf input
|
|
34
|
+
* @param {String} level - specify whether object is built from "page" level or "document" level prediction
|
|
30
35
|
*/
|
|
31
36
|
constructor({
|
|
32
37
|
apiPrediction = undefined,
|
|
@@ -38,16 +43,23 @@ class FinancialDocument extends Document {
|
|
|
38
43
|
invoiceNumber = undefined,
|
|
39
44
|
dueDate = undefined,
|
|
40
45
|
taxes = undefined,
|
|
41
|
-
|
|
46
|
+
supplier = undefined,
|
|
47
|
+
supplierAddress = undefined,
|
|
42
48
|
paymentDetails = undefined,
|
|
43
49
|
companyNumber = undefined,
|
|
44
50
|
vatNumber = undefined,
|
|
45
51
|
orientation = undefined,
|
|
46
52
|
totalTax = undefined,
|
|
47
53
|
time = undefined,
|
|
54
|
+
customerName = undefined,
|
|
55
|
+
customerAddress = undefined,
|
|
56
|
+
customerCompanyRegistration = undefined,
|
|
57
|
+
words = undefined,
|
|
48
58
|
pageNumber = 0,
|
|
59
|
+
level = "page",
|
|
49
60
|
}) {
|
|
50
61
|
super(inputFile);
|
|
62
|
+
this.level = level;
|
|
51
63
|
if (apiPrediction === undefined) {
|
|
52
64
|
this.#initFromScratch({
|
|
53
65
|
locale,
|
|
@@ -57,7 +69,8 @@ class FinancialDocument extends Document {
|
|
|
57
69
|
invoiceNumber,
|
|
58
70
|
dueDate,
|
|
59
71
|
taxes,
|
|
60
|
-
|
|
72
|
+
supplier,
|
|
73
|
+
supplierAddress,
|
|
61
74
|
paymentDetails,
|
|
62
75
|
companyNumber,
|
|
63
76
|
vatNumber,
|
|
@@ -65,9 +78,12 @@ class FinancialDocument extends Document {
|
|
|
65
78
|
pageNumber,
|
|
66
79
|
totalTax,
|
|
67
80
|
time,
|
|
81
|
+
customerName,
|
|
82
|
+
customerAddress,
|
|
83
|
+
customerCompanyRegistration,
|
|
68
84
|
});
|
|
69
85
|
} else {
|
|
70
|
-
this.#initFromApiPrediction(apiPrediction, inputFile, pageNumber);
|
|
86
|
+
this.#initFromApiPrediction(apiPrediction, inputFile, pageNumber, words);
|
|
71
87
|
}
|
|
72
88
|
this.#checklist();
|
|
73
89
|
}
|
|
@@ -86,8 +102,12 @@ class FinancialDocument extends Document {
|
|
|
86
102
|
vatNumber,
|
|
87
103
|
orientation,
|
|
88
104
|
pageNumber,
|
|
89
|
-
|
|
105
|
+
supplier,
|
|
106
|
+
supplierAddress,
|
|
90
107
|
time,
|
|
108
|
+
customerName,
|
|
109
|
+
customerAddress,
|
|
110
|
+
customerCompanyRegistration,
|
|
91
111
|
}) {
|
|
92
112
|
const constructPrediction = function (item) {
|
|
93
113
|
return { prediction: { value: item }, valueKey: "value", pageNumber };
|
|
@@ -98,13 +118,19 @@ class FinancialDocument extends Document {
|
|
|
98
118
|
this.totalTax = new Amount(constructPrediction(totalTax));
|
|
99
119
|
this.date = new Date(constructPrediction(date));
|
|
100
120
|
this.dueDate = new Date(constructPrediction(dueDate));
|
|
101
|
-
this.
|
|
121
|
+
this.supplier = new Field(constructPrediction(supplier));
|
|
122
|
+
this.supplierAddress = new Field(this.constructPrediction(supplierAddress));
|
|
102
123
|
this.time = new Field(constructPrediction(time));
|
|
103
124
|
this.orientation = new Orientation(constructPrediction(orientation));
|
|
104
125
|
this.invoiceNumber = new Field(constructPrediction(invoiceNumber));
|
|
105
126
|
this.paymentDetails = new Field(constructPrediction(paymentDetails));
|
|
106
127
|
this.companyNumber = new Field(constructPrediction(companyNumber));
|
|
107
128
|
this.vatNumber = new Field(constructPrediction(vatNumber));
|
|
129
|
+
this.customerName = new Field(this.constructPrediction(customerName));
|
|
130
|
+
this.customerAddress = new Field(this.constructPrediction(customerAddress));
|
|
131
|
+
this.customerCompanyRegistration = new Field(
|
|
132
|
+
this.constructPrediction(customerCompanyRegistration)
|
|
133
|
+
);
|
|
108
134
|
if (taxes !== undefined) {
|
|
109
135
|
this.taxes = [];
|
|
110
136
|
for (const t of taxes) {
|
|
@@ -120,34 +146,73 @@ class FinancialDocument extends Document {
|
|
|
120
146
|
}
|
|
121
147
|
}
|
|
122
148
|
|
|
123
|
-
#initFromApiPrediction(apiPrediction, inputFile, pageNumber) {
|
|
149
|
+
#initFromApiPrediction(apiPrediction, inputFile, pageNumber, words) {
|
|
124
150
|
if (Object.keys(apiPrediction).includes("invoice_number")) {
|
|
125
|
-
const invoice = new Invoice({
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
151
|
+
const invoice = new Invoice({
|
|
152
|
+
apiPrediction,
|
|
153
|
+
inputFile,
|
|
154
|
+
pageNumber,
|
|
155
|
+
level: this.level,
|
|
156
|
+
words: words,
|
|
129
157
|
});
|
|
130
|
-
this.
|
|
131
|
-
|
|
158
|
+
this.locale = invoice.locale;
|
|
159
|
+
this.totalIncl = invoice.totalIncl;
|
|
160
|
+
this.totalExcl = invoice.totalExcl;
|
|
161
|
+
this.date = invoice.invoiceDate;
|
|
162
|
+
this.invoiceNumber = invoice.invoiceNumber;
|
|
163
|
+
this.dueDate = invoice.dueDate;
|
|
164
|
+
this.taxes = invoice.taxes;
|
|
165
|
+
this.supplier = invoice.supplier;
|
|
166
|
+
this.supplierAddress = invoice.supplierAddress;
|
|
167
|
+
this.paymentDetails = invoice.paymentDetails;
|
|
168
|
+
this.companyNumber = invoice.companyNumber;
|
|
169
|
+
this.orientation = invoice.orientation;
|
|
170
|
+
this.totalTax = invoice.totalTax;
|
|
171
|
+
if (invoice.words) this.words = invoice.words;
|
|
172
|
+
this.time = new Field({
|
|
173
|
+
prediction: { value: undefined, confidence: 0.0 },
|
|
132
174
|
});
|
|
175
|
+
this.customerName = invoice.customerName;
|
|
176
|
+
this.customerAddress = invoice.customerAddress;
|
|
177
|
+
this.customerCompanyRegistration = invoice.customerCompanyRegistration;
|
|
133
178
|
} else {
|
|
134
|
-
const receipt = new Receipt({
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
179
|
+
const receipt = new Receipt({
|
|
180
|
+
apiPrediction,
|
|
181
|
+
inputFile,
|
|
182
|
+
pageNumber,
|
|
183
|
+
level: this.level,
|
|
184
|
+
words: words,
|
|
138
185
|
});
|
|
139
|
-
this.
|
|
140
|
-
|
|
186
|
+
this.orientation = receipt.orientation;
|
|
187
|
+
this.date = receipt.date;
|
|
188
|
+
this.dueDate = receipt.date;
|
|
189
|
+
this.taxes = receipt.taxes;
|
|
190
|
+
this.locale = receipt.locale;
|
|
191
|
+
this.totalIncl = receipt.totalIncl;
|
|
192
|
+
this.totalExcl = receipt.totalExcl;
|
|
193
|
+
this.supplier = receipt.merchantName;
|
|
194
|
+
this.supplierAddress = new Field({
|
|
195
|
+
prediction: { value: undefined, confidence: 0.0 },
|
|
141
196
|
});
|
|
142
|
-
this.
|
|
143
|
-
|
|
197
|
+
this.time = receipt.time;
|
|
198
|
+
this.totalTax = receipt.totalTax;
|
|
199
|
+
this.invoiceNumber = new Field({
|
|
200
|
+
prediction: { value: undefined, confidence: 0.0 },
|
|
144
201
|
});
|
|
145
202
|
this.paymentDetails = new Field({
|
|
146
|
-
prediction: { value: undefined,
|
|
203
|
+
prediction: { value: undefined, confidence: 0.0 },
|
|
147
204
|
});
|
|
148
205
|
this.companyNumber = new Field({
|
|
149
|
-
prediction: { value: undefined,
|
|
206
|
+
prediction: { value: undefined, confidence: 0.0 },
|
|
207
|
+
});
|
|
208
|
+
this.customerName = new Field({
|
|
209
|
+
prediction: { value: undefined, confidence: 0.0 },
|
|
210
|
+
});
|
|
211
|
+
this.customerAddress = new Field({
|
|
212
|
+
prediction: { value: undefined, confidence: 0.0 },
|
|
150
213
|
});
|
|
214
|
+
this.customerCompanyRegistration = [];
|
|
215
|
+
if (receipt.words) this.words = receipt.words;
|
|
151
216
|
}
|
|
152
217
|
}
|
|
153
218
|
|
|
@@ -157,7 +222,7 @@ class FinancialDocument extends Document {
|
|
|
157
222
|
Filename: ${this.filename}
|
|
158
223
|
Total amount: ${this.totalIncl.value}
|
|
159
224
|
Date: ${this.date.value}
|
|
160
|
-
|
|
225
|
+
Supplier: ${this.supplier.value}
|
|
161
226
|
Total taxes: ${this.totalTax.value}
|
|
162
227
|
`;
|
|
163
228
|
}
|
|
@@ -192,7 +257,7 @@ class FinancialDocument extends Document {
|
|
|
192
257
|
this.totalIncl.value * (1 - eps) - 0.02 <= reconstructedTotal &&
|
|
193
258
|
reconstructedTotal <= this.totalIncl.value * (1 + eps) + 0.02
|
|
194
259
|
) {
|
|
195
|
-
this.taxes = this.taxes.map((tax) => ({ ...tax,
|
|
260
|
+
this.taxes = this.taxes.map((tax) => ({ ...tax, confidence: 1.0 }));
|
|
196
261
|
this.totalTax.probability = 1.0;
|
|
197
262
|
this.totalIncl.probability = 1.0;
|
|
198
263
|
return true;
|
|
@@ -11,7 +11,6 @@ class Invoice extends Document {
|
|
|
11
11
|
/**
|
|
12
12
|
* @param {Object} apiPrediction - Json parsed prediction from HTTP response
|
|
13
13
|
* @param {Input} input - Input object
|
|
14
|
-
* @param {Integer} pageNumber - Page number for multi pages pdf input
|
|
15
14
|
* @param {Object} locale - locale value for creating Invoice object from scratch
|
|
16
15
|
* @param {Object} totalIncl - total tax included value for creating Invoice object from scratch
|
|
17
16
|
* @param {Object} totalExcl - total tax excluded value for creating Invoice object from scratch
|
|
@@ -19,12 +18,17 @@ class Invoice extends Document {
|
|
|
19
18
|
* @param {Object} invoiceNumber - invoice number value for creating Invoice object from scratch
|
|
20
19
|
* @param {Object} taxes - taxes value for creating Invoice object from scratch
|
|
21
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
22
|
* @param {Object} paymentDetails - payment details value for creating Invoice object from scratch
|
|
23
23
|
* @param {Object} companyNumber - company number value for creating Invoice object from scratch
|
|
24
24
|
* @param {Object} vatNumber - vat number value for creating Invoice object from scratch
|
|
25
25
|
* @param {Object} orientation - orientation value for creating Invoice object from scratch
|
|
26
26
|
* @param {Object} totalTax - total tax value for creating Invoice object from scratch
|
|
27
|
-
* @param {Object}
|
|
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
|
|
28
32
|
*/
|
|
29
33
|
constructor({
|
|
30
34
|
apiPrediction = undefined,
|
|
@@ -37,14 +41,24 @@ class Invoice extends Document {
|
|
|
37
41
|
dueDate = undefined,
|
|
38
42
|
taxes = undefined,
|
|
39
43
|
supplier = undefined,
|
|
44
|
+
supplierAddress = undefined,
|
|
40
45
|
paymentDetails = undefined,
|
|
41
46
|
companyNumber = undefined,
|
|
42
47
|
vatNumber = undefined,
|
|
43
48
|
orientation = undefined,
|
|
44
49
|
totalTax = undefined,
|
|
50
|
+
customerName = undefined,
|
|
51
|
+
customerAddress = undefined,
|
|
52
|
+
customerCompanyRegistration = undefined,
|
|
53
|
+
words = undefined,
|
|
45
54
|
pageNumber = 0,
|
|
55
|
+
level = "page",
|
|
46
56
|
}) {
|
|
47
57
|
super(inputFile);
|
|
58
|
+
this.level = level;
|
|
59
|
+
this.constructPrediction = function (item) {
|
|
60
|
+
return { prediction: { value: item }, valueKey: "value", pageNumber };
|
|
61
|
+
};
|
|
48
62
|
if (apiPrediction === undefined) {
|
|
49
63
|
this.#initFromScratch({
|
|
50
64
|
locale,
|
|
@@ -55,15 +69,19 @@ class Invoice extends Document {
|
|
|
55
69
|
dueDate,
|
|
56
70
|
taxes,
|
|
57
71
|
supplier,
|
|
72
|
+
supplierAddress,
|
|
58
73
|
paymentDetails,
|
|
59
74
|
companyNumber,
|
|
60
75
|
vatNumber,
|
|
61
76
|
orientation,
|
|
62
77
|
pageNumber,
|
|
63
78
|
totalTax,
|
|
79
|
+
customerName,
|
|
80
|
+
customerAddress,
|
|
81
|
+
customerCompanyRegistration,
|
|
64
82
|
});
|
|
65
83
|
} else {
|
|
66
|
-
this.#initFromApiPrediction(apiPrediction, pageNumber);
|
|
84
|
+
this.#initFromApiPrediction(apiPrediction, pageNumber, words);
|
|
67
85
|
}
|
|
68
86
|
this.#checklist();
|
|
69
87
|
this.#reconstruct();
|
|
@@ -79,28 +97,34 @@ class Invoice extends Document {
|
|
|
79
97
|
dueDate,
|
|
80
98
|
taxes,
|
|
81
99
|
supplier,
|
|
100
|
+
supplierAddress,
|
|
82
101
|
paymentDetails,
|
|
83
102
|
companyNumber,
|
|
84
103
|
vatNumber,
|
|
85
104
|
orientation,
|
|
86
105
|
pageNumber,
|
|
106
|
+
customerName,
|
|
107
|
+
customerAddress,
|
|
108
|
+
customerCompanyRegistration,
|
|
87
109
|
}) {
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
this.
|
|
92
|
-
this.
|
|
93
|
-
this.
|
|
94
|
-
this.
|
|
95
|
-
this.
|
|
96
|
-
this.
|
|
97
|
-
this.
|
|
98
|
-
this.
|
|
99
|
-
this.
|
|
100
|
-
this.
|
|
101
|
-
this.
|
|
102
|
-
this.
|
|
103
|
-
this.
|
|
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
|
+
);
|
|
104
128
|
if (taxes !== undefined) {
|
|
105
129
|
this.taxes = [];
|
|
106
130
|
for (const t of taxes) {
|
|
@@ -114,10 +138,12 @@ class Invoice extends Document {
|
|
|
114
138
|
);
|
|
115
139
|
}
|
|
116
140
|
}
|
|
141
|
+
if (this.level === "page") {
|
|
142
|
+
this.orientation = new Orientation(this.constructPrediction(orientation));
|
|
143
|
+
}
|
|
117
144
|
}
|
|
118
145
|
|
|
119
|
-
#initFromApiPrediction(apiPrediction, pageNumber) {
|
|
120
|
-
this.words = [];
|
|
146
|
+
#initFromApiPrediction(apiPrediction, pageNumber, words) {
|
|
121
147
|
this.locale = new Locale({ prediction: apiPrediction.locale, pageNumber });
|
|
122
148
|
this.totalIncl = new Amount({
|
|
123
149
|
prediction: apiPrediction.total_incl,
|
|
@@ -125,7 +151,7 @@ class Invoice extends Document {
|
|
|
125
151
|
pageNumber,
|
|
126
152
|
});
|
|
127
153
|
this.totalTax = new Amount({
|
|
128
|
-
prediction: { value: undefined,
|
|
154
|
+
prediction: { value: undefined, confidence: 0.0 },
|
|
129
155
|
valueKey: "value",
|
|
130
156
|
pageNumber,
|
|
131
157
|
});
|
|
@@ -153,10 +179,6 @@ class Invoice extends Document {
|
|
|
153
179
|
codeKey: "code",
|
|
154
180
|
});
|
|
155
181
|
});
|
|
156
|
-
this.orientation = new Orientation({
|
|
157
|
-
prediction: apiPrediction.orientation,
|
|
158
|
-
pageNumber,
|
|
159
|
-
});
|
|
160
182
|
this.companyNumber = apiPrediction.company_registration.map(function (
|
|
161
183
|
companyNumber
|
|
162
184
|
) {
|
|
@@ -179,13 +201,49 @@ class Invoice extends Document {
|
|
|
179
201
|
prediction: apiPrediction.supplier,
|
|
180
202
|
pageNumber,
|
|
181
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
|
+
);
|
|
182
225
|
this.paymentDetails = apiPrediction.payment_details.map(function (
|
|
183
226
|
paymentDetail
|
|
184
227
|
) {
|
|
185
228
|
return new PaymentDetails({ prediction: paymentDetail, pageNumber });
|
|
186
229
|
});
|
|
187
|
-
|
|
188
|
-
|
|
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;
|
|
189
247
|
}
|
|
190
248
|
|
|
191
249
|
toString() {
|
|
@@ -197,6 +255,9 @@ class Invoice extends Document {
|
|
|
197
255
|
Total amount excluding taxes: ${this.totalExcl.value}
|
|
198
256
|
Invoice Date: ${this.invoiceDate.value}
|
|
199
257
|
Supplier name: ${this.supplier.value}
|
|
258
|
+
Supplier address: ${this.supplierAddress.value}
|
|
259
|
+
Customer name: ${this.customerName.value}
|
|
260
|
+
Customer address: ${this.customerAddress.value}
|
|
200
261
|
Taxes: ${this.taxes.map((tax) => tax.toString()).join(" - ")}
|
|
201
262
|
Total taxes: ${this.totalTax.value}
|
|
202
263
|
`;
|
|
@@ -314,7 +375,7 @@ class Invoice extends Document {
|
|
|
314
375
|
value: this.taxes.reduce((acc, tax) => {
|
|
315
376
|
return tax.value !== undefined ? acc + tax.value : acc;
|
|
316
377
|
}, 0),
|
|
317
|
-
|
|
378
|
+
confidence: Field.arrayProbability(this.taxes),
|
|
318
379
|
};
|
|
319
380
|
if (totalTax.value > 0)
|
|
320
381
|
this.totalTax = new Amount({
|
|
@@ -334,7 +395,7 @@ class Invoice extends Document {
|
|
|
334
395
|
) {
|
|
335
396
|
const totalTax = {
|
|
336
397
|
value: this.totalIncl.value - this.totalExcl.value,
|
|
337
|
-
|
|
398
|
+
confidence: this.totalIncl.probability * this.totalExcl.probability,
|
|
338
399
|
};
|
|
339
400
|
if (totalTax.value > 0)
|
|
340
401
|
this.totalTax = new Amount({
|
|
@@ -357,7 +418,7 @@ class Invoice extends Document {
|
|
|
357
418
|
this.taxes.reduce((acc, tax) => {
|
|
358
419
|
return tax.value !== undefined ? acc + tax.value : acc;
|
|
359
420
|
}, 0),
|
|
360
|
-
|
|
421
|
+
confidence:
|
|
361
422
|
Field.arrayProbability(this.taxes) * this.totalIncl.probability,
|
|
362
423
|
};
|
|
363
424
|
this.totalExcl = new Amount({
|
|
@@ -380,7 +441,7 @@ class Invoice extends Document {
|
|
|
380
441
|
this.taxes.reduce((acc, tax) => {
|
|
381
442
|
return tax.value ? acc + tax.value : acc;
|
|
382
443
|
}, 0.0),
|
|
383
|
-
|
|
444
|
+
confidence:
|
|
384
445
|
Field.arrayProbability(this.taxes) * this.totalExcl.probability,
|
|
385
446
|
};
|
|
386
447
|
this.totalIncl = new Amount({
|