mindee 1.4.0 → 2.0.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.
- package/CHANGELOG.md +37 -19
- package/README.md +46 -126
- package/bin/mindee.d.ts +2 -0
- package/bin/mindee.js +5 -0
- package/package.json +68 -58
- package/src/api/endpoint.d.ts +29 -0
- package/src/api/endpoint.js +151 -0
- package/src/api/index.d.ts +2 -0
- package/src/api/index.js +18 -0
- package/src/api/response.d.ts +52 -0
- package/src/api/response.js +107 -0
- package/src/cli.d.ts +1 -0
- package/src/cli.js +114 -0
- package/src/client.d.ts +48 -0
- package/src/client.js +123 -0
- package/src/constants.d.ts +13 -0
- package/src/constants.js +54 -0
- package/src/documents/custom.d.ts +14 -0
- package/src/documents/custom.js +48 -0
- package/src/documents/document.d.ts +38 -0
- package/src/documents/document.js +59 -0
- package/src/documents/documentConfig.d.ts +44 -0
- package/src/documents/documentConfig.js +104 -0
- package/src/documents/financialDocument.d.ts +32 -0
- package/src/documents/financialDocument.js +133 -0
- package/src/documents/index.d.ts +12 -0
- package/src/documents/index.js +20 -0
- package/src/documents/invoice.d.ts +37 -0
- package/src/documents/invoice.js +303 -0
- package/src/documents/passport.d.ts +36 -0
- package/src/documents/passport.js +218 -0
- package/src/documents/receipt.d.ts +24 -0
- package/src/documents/receipt.js +157 -0
- package/src/errors/handler.d.ts +7 -0
- package/src/errors/handler.js +18 -0
- package/src/fields/amount.d.ts +13 -0
- package/src/fields/amount.js +34 -0
- package/src/fields/companyRegistration.d.ts +6 -0
- package/src/fields/companyRegistration.js +11 -0
- package/src/fields/customDocs.d.ts +42 -0
- package/src/fields/customDocs.js +61 -0
- package/src/fields/date.d.ts +12 -0
- package/src/fields/date.js +25 -0
- package/src/fields/field.d.ts +74 -0
- package/src/fields/field.js +106 -0
- package/src/fields/fullText.d.ts +24 -0
- package/src/fields/fullText.js +89 -0
- package/src/fields/index.d.ts +11 -0
- package/src/fields/index.js +27 -0
- package/src/fields/locale.d.ts +13 -0
- package/src/fields/locale.js +40 -0
- package/src/fields/orientation.d.ts +10 -0
- package/src/fields/orientation.js +22 -0
- package/src/fields/paymentDetails.d.ts +31 -0
- package/src/fields/paymentDetails.js +58 -0
- package/src/fields/tax.d.ts +30 -0
- package/src/fields/tax.js +50 -0
- package/src/geometry.d.ts +59 -0
- package/src/geometry.js +113 -0
- package/src/index.d.ts +2 -0
- package/src/index.js +12 -0
- package/src/inputs.d.ts +69 -0
- package/src/inputs.js +196 -0
- package/src/logger.d.ts +20 -0
- package/src/logger.js +35 -0
- package/mindee/api/financialDocument.js +0 -47
- package/mindee/api/index.js +0 -5
- package/mindee/api/invoice.js +0 -53
- package/mindee/api/object.js +0 -82
- package/mindee/api/receipt.js +0 -39
- package/mindee/api/request.js +0 -72
- package/mindee/api/response.js +0 -80
- package/mindee/documents/document.js +0 -69
- package/mindee/documents/fields/amount.js +0 -25
- package/mindee/documents/fields/date.js +0 -29
- package/mindee/documents/fields/field.js +0 -86
- package/mindee/documents/fields/index.js +0 -7
- package/mindee/documents/fields/locale.js +0 -30
- package/mindee/documents/fields/orientation.js +0 -24
- package/mindee/documents/fields/paymentDetails.js +0 -52
- package/mindee/documents/fields/tax.js +0 -47
- package/mindee/documents/financialDocument.js +0 -269
- package/mindee/documents/index.js +0 -5
- package/mindee/documents/invoice.js +0 -456
- package/mindee/documents/receipt.js +0 -287
- package/mindee/errors/handler.js +0 -16
- package/mindee/index.js +0 -39
- package/mindee/inputs.js +0 -181
- package/mindee/logger.js +0 -34
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
const fs = require("fs").promises;
|
|
2
|
-
|
|
3
|
-
class Document {
|
|
4
|
-
/**
|
|
5
|
-
* Takes a list of Documents and return one Document where
|
|
6
|
-
* each field is set with the maximum probability field
|
|
7
|
-
* @param {Input} inputFile - input file given to parse the document
|
|
8
|
-
*/
|
|
9
|
-
constructor(inputFile = undefined) {
|
|
10
|
-
this.filepath = undefined;
|
|
11
|
-
this.filename = undefined;
|
|
12
|
-
this.fileExtension = undefined;
|
|
13
|
-
|
|
14
|
-
if (inputFile != undefined) {
|
|
15
|
-
this.filepath = inputFile.filepath;
|
|
16
|
-
this.filename = inputFile.filename;
|
|
17
|
-
this.fileExtension = inputFile.fileExtension;
|
|
18
|
-
}
|
|
19
|
-
this.checklist = {};
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
clone() {
|
|
23
|
-
return JSON.parse(JSON.stringify(this));
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/** return true if all checklist of the document if true */
|
|
27
|
-
checkAll() {
|
|
28
|
-
return this.checklist.every((item) => item == true);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/** Export document into a JSON file */
|
|
32
|
-
async dump(path) {
|
|
33
|
-
return await fs.writeFile(path, JSON.stringify(Object.entries(this)));
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/** Create a Document from a JSON file */
|
|
37
|
-
static async load(path) {
|
|
38
|
-
const file = fs.readFile(path);
|
|
39
|
-
const args = JSON.parse(file);
|
|
40
|
-
return new Document({ reconsctruted: true, ...args });
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Takes a list of Documents and return one Document where
|
|
45
|
-
* each field is set with the maximum probability field
|
|
46
|
-
* @param {Array<Document>} documents - A list of Documents
|
|
47
|
-
*/
|
|
48
|
-
static mergePages(documents) {
|
|
49
|
-
const finalDocument = documents[0].clone();
|
|
50
|
-
const attributes = Object.getOwnPropertyNames(finalDocument);
|
|
51
|
-
for (const document of documents) {
|
|
52
|
-
for (const attribute of attributes) {
|
|
53
|
-
if (Array.isArray(document?.[attribute])) {
|
|
54
|
-
finalDocument[attribute] = finalDocument[attribute]?.length
|
|
55
|
-
? finalDocument[attribute]
|
|
56
|
-
: document?.[attribute];
|
|
57
|
-
} else if (
|
|
58
|
-
document?.[attribute]?.probability >
|
|
59
|
-
finalDocument[attribute].probability
|
|
60
|
-
) {
|
|
61
|
-
finalDocument[attribute] = document?.[attribute];
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
return finalDocument;
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
module.exports = Document;
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
const Field = require("./field");
|
|
2
|
-
|
|
3
|
-
class Amount extends Field {
|
|
4
|
-
/**
|
|
5
|
-
* @param {Object} prediction - Prediction object from HTTP response
|
|
6
|
-
* @param {String} valueKey - Key to use in the prediction dict
|
|
7
|
-
* @param {Boolean} reconstructed - Does the object is reconstructed (not extracted by the API)
|
|
8
|
-
* @param {Integer} pageNumber - Page number for multi pages pdf
|
|
9
|
-
*/
|
|
10
|
-
constructor({
|
|
11
|
-
prediction,
|
|
12
|
-
valueKey = "amount",
|
|
13
|
-
reconstructed = false,
|
|
14
|
-
pageNumber = 0,
|
|
15
|
-
}) {
|
|
16
|
-
super({ prediction, valueKey, reconstructed, pageNumber });
|
|
17
|
-
this.value = +parseFloat(prediction[valueKey]).toFixed(3);
|
|
18
|
-
if (isNaN(this.value)) {
|
|
19
|
-
this.value = undefined;
|
|
20
|
-
this.probability = 0;
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
module.exports = Amount;
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
const Field = require("./field");
|
|
2
|
-
|
|
3
|
-
class DateField extends Field {
|
|
4
|
-
/**
|
|
5
|
-
* @param {Object} prediction - Prediction object from HTTP response
|
|
6
|
-
* @param {String} valueKey - Key to use in the prediction dict
|
|
7
|
-
* @param {Boolean} reconstructed - Does the object is reconstructed (not extracted by the API)
|
|
8
|
-
* @param {Integer} pageNumber - Page number for multi pages pdf
|
|
9
|
-
*/
|
|
10
|
-
constructor({
|
|
11
|
-
prediction,
|
|
12
|
-
valueKey = "iso",
|
|
13
|
-
reconstructed = false,
|
|
14
|
-
pageNumber = 0,
|
|
15
|
-
}) {
|
|
16
|
-
super({ prediction, valueKey, reconstructed, pageNumber });
|
|
17
|
-
this.dateObject = new Date(this.value);
|
|
18
|
-
if (
|
|
19
|
-
!(this.dateObject instanceof Date) ||
|
|
20
|
-
isNaN(this.dateObject.valueOf())
|
|
21
|
-
) {
|
|
22
|
-
this.dateObject = undefined;
|
|
23
|
-
this.probability = 0.0;
|
|
24
|
-
this.value = undefined;
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
module.exports = DateField;
|
|
@@ -1,86 +0,0 @@
|
|
|
1
|
-
class Field {
|
|
2
|
-
/**
|
|
3
|
-
* @param {Object} prediction - Prediction object from HTTP response
|
|
4
|
-
* @param {String} valueKey - Key to use in the prediction dict
|
|
5
|
-
* @param {Boolean} reconstructed - Does the object is reconstructed (not extracted by the API)
|
|
6
|
-
* @param {Integer} pageNumber - Page number for multi pages pdf
|
|
7
|
-
* @param {Array<String>} extraFields - Extra fields to get from the prediction and to set as attribute of the Field
|
|
8
|
-
*/
|
|
9
|
-
constructor({
|
|
10
|
-
prediction,
|
|
11
|
-
valueKey = "value",
|
|
12
|
-
reconstructed = false,
|
|
13
|
-
extraFields,
|
|
14
|
-
pageNumber,
|
|
15
|
-
}) {
|
|
16
|
-
this.pageNumber = pageNumber;
|
|
17
|
-
this.reconstructed = reconstructed;
|
|
18
|
-
this.value = undefined;
|
|
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
|
-
this.value = prediction[valueKey];
|
|
23
|
-
if (extraFields) {
|
|
24
|
-
for (const fieldName of extraFields) {
|
|
25
|
-
this[fieldName] = prediction[fieldName];
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
compare(other) {
|
|
32
|
-
if (this.value == null && other.value == null) return true;
|
|
33
|
-
else if (this.value == null || other.value == null) return false;
|
|
34
|
-
else {
|
|
35
|
-
if (typeof this.value == "string") {
|
|
36
|
-
return this.value.toLowerCase() === other.value.toLowerCase();
|
|
37
|
-
} else {
|
|
38
|
-
return this.value === other.value;
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
@param {Array<Field>} array1 - first Array of Fields
|
|
45
|
-
@param {Array<Field>} array2 - second Array of Fields
|
|
46
|
-
@param {String} attr - Attribute to compare
|
|
47
|
-
@returns {Boolean} - true if all elements in array1 exist in array2 and vice-versa, false otherwise
|
|
48
|
-
*/
|
|
49
|
-
static compareArrays(array1, array2, attr = "value") {
|
|
50
|
-
const list1 = array1.map((item) => item[attr]);
|
|
51
|
-
const list2 = array2.map((item) => item[attr]);
|
|
52
|
-
if (list1.length !== list2.length) return false;
|
|
53
|
-
for (const item1 of list1) {
|
|
54
|
-
if (!list2.includes(item1)) return false;
|
|
55
|
-
}
|
|
56
|
-
return true;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* @param {Array<Field>} array - Array of Fields
|
|
61
|
-
* @returns {Number} product of all the fields probaility
|
|
62
|
-
*/
|
|
63
|
-
static arrayProbability(array) {
|
|
64
|
-
let total = 1.0;
|
|
65
|
-
for (const field of array) {
|
|
66
|
-
total *= field.probability;
|
|
67
|
-
if (isNaN(total)) return 0.0;
|
|
68
|
-
}
|
|
69
|
-
return total;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* @param {Array<Field>} array - Array of Fields
|
|
74
|
-
* @returns {Number} Sum of all the Fields values in the array
|
|
75
|
-
*/
|
|
76
|
-
static arraySum(array) {
|
|
77
|
-
let total = 0;
|
|
78
|
-
for (const field of array) {
|
|
79
|
-
total += field.value;
|
|
80
|
-
if (isNaN(total)) return 0.0;
|
|
81
|
-
}
|
|
82
|
-
return total;
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
module.exports = Field;
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
exports.field = require("./field");
|
|
2
|
-
exports.date = require("./date");
|
|
3
|
-
exports.amount = require("./amount");
|
|
4
|
-
exports.locale = require("./locale");
|
|
5
|
-
exports.orientation = require("./orientation");
|
|
6
|
-
exports.paymentDetails = require("./paymentDetails");
|
|
7
|
-
exports.tax = require("./tax.js");
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
const Field = require("./field");
|
|
2
|
-
|
|
3
|
-
class Locale extends Field {
|
|
4
|
-
/**
|
|
5
|
-
* @param {Object} prediction - Prediction object from HTTP response
|
|
6
|
-
* @param {String} valueKey - Key to use in the prediction dict
|
|
7
|
-
* @param {Boolean} reconstructed - Does the object is reconstructed (not extracted by the API)
|
|
8
|
-
* @param {Integer} pageNumber - Page number for multi pages pdf
|
|
9
|
-
*/
|
|
10
|
-
constructor({
|
|
11
|
-
prediction,
|
|
12
|
-
valueKey = "value",
|
|
13
|
-
reconstructed = false,
|
|
14
|
-
pageNumber = 0,
|
|
15
|
-
}) {
|
|
16
|
-
super({ prediction, valueKey, reconstructed, pageNumber });
|
|
17
|
-
|
|
18
|
-
this.language = undefined;
|
|
19
|
-
this.country = undefined;
|
|
20
|
-
this.currency = undefined;
|
|
21
|
-
if ("language" in prediction && prediction.language !== "N/A")
|
|
22
|
-
this.language = prediction.language;
|
|
23
|
-
if ("country" in prediction && prediction.country !== "N/A")
|
|
24
|
-
this.country = prediction.country;
|
|
25
|
-
if ("currency" in prediction && prediction.currency !== "N/A")
|
|
26
|
-
this.currency = prediction.currency;
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
module.exports = Locale;
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
const Field = require("./field");
|
|
2
|
-
|
|
3
|
-
class Orientation extends Field {
|
|
4
|
-
/**
|
|
5
|
-
* @param {Object} prediction - Prediction object from HTTP response
|
|
6
|
-
* @param {String} valueKey - Key to use in the prediction dict
|
|
7
|
-
* @param {Boolean} reconstructed - Does the object is reconstructed (not extracted by the API)
|
|
8
|
-
* @param {Integer} pageNumber - Page number for multi pages pdf
|
|
9
|
-
*/
|
|
10
|
-
constructor({
|
|
11
|
-
prediction,
|
|
12
|
-
valueKey = "degrees",
|
|
13
|
-
reconstructed = false,
|
|
14
|
-
pageNumber = 0,
|
|
15
|
-
}) {
|
|
16
|
-
const orientations = [0, 90, 180, 270];
|
|
17
|
-
super({ prediction, valueKey, reconstructed, pageNumber });
|
|
18
|
-
this.value = parseInt(prediction[valueKey]);
|
|
19
|
-
if (isNaN(this.value)) this.probability = 0.0;
|
|
20
|
-
if (!orientations.includes(this.value)) this.value = 0;
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
module.exports = Orientation;
|
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
const Field = require("./field");
|
|
2
|
-
|
|
3
|
-
class PaymentDetails extends Field {
|
|
4
|
-
/**
|
|
5
|
-
* @param {Object} prediction - Prediction object from HTTP response
|
|
6
|
-
* @param {String} valueKey - Key to use in the prediction dict to get the iban
|
|
7
|
-
* @param {String} accountNumberKey - Key to use to get the account number in the prediction dict
|
|
8
|
-
* @param {String} ibanKey - Key to use to get the IBAN in the prediction dict
|
|
9
|
-
* @param {String} routingNumberKey - Key to use to get the routing number in the prediction dict
|
|
10
|
-
* @param {String} swiftKey - Key to use to get the SWIFT in the prediction dict
|
|
11
|
-
* @param {Boolean} reconstructed - Does the object is reconstructed (not extracted by the API)
|
|
12
|
-
* @param {Integer} pageNumber - Page number for multi pages pdf
|
|
13
|
-
*/
|
|
14
|
-
constructor({
|
|
15
|
-
prediction,
|
|
16
|
-
valueKey = "iban",
|
|
17
|
-
accountNumberKey = "account_number",
|
|
18
|
-
ibanKey = "iban",
|
|
19
|
-
routingNumberKey = "routing_number",
|
|
20
|
-
swiftKey = "swift",
|
|
21
|
-
reconstructed = false,
|
|
22
|
-
pageNumber = 0,
|
|
23
|
-
}) {
|
|
24
|
-
super({ prediction, valueKey, reconstructed, pageNumber });
|
|
25
|
-
|
|
26
|
-
this.accountNumber = undefined;
|
|
27
|
-
this.iban = undefined;
|
|
28
|
-
this.routingNumber = undefined;
|
|
29
|
-
this.swift = undefined;
|
|
30
|
-
|
|
31
|
-
this.#setKey(prediction[accountNumberKey], "accountNumber");
|
|
32
|
-
this.#setKey(prediction[ibanKey], "iban");
|
|
33
|
-
this.#setKey(prediction[routingNumberKey], "routingNumber");
|
|
34
|
-
this.#setKey(prediction[swiftKey], "swift");
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
#setKey(value, key) {
|
|
38
|
-
if (typeof value === "string" && value !== "N/A") this[key] = value;
|
|
39
|
-
else this[key] = undefined;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
toString() {
|
|
43
|
-
let str = "";
|
|
44
|
-
const keys = ["accountNumber", "iban", "routingNumber", "swift"];
|
|
45
|
-
for (const key of keys) {
|
|
46
|
-
if (this[key]) str += `${this[key]}; `;
|
|
47
|
-
}
|
|
48
|
-
return str;
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
module.exports = PaymentDetails;
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
const Field = require("./field");
|
|
2
|
-
|
|
3
|
-
class Tax extends Field {
|
|
4
|
-
/**
|
|
5
|
-
* @param {Object} prediction - Prediction object from HTTP response
|
|
6
|
-
* @param {String} valueKey - Key to use in the prediction dict to get the tax value
|
|
7
|
-
* @param {String} rateKey - Key to use to get the tax rate in the prediction dict
|
|
8
|
-
* @param {String} codeKey - Key to use to get the tax code in the prediction dict
|
|
9
|
-
* @param {Boolean} reconstructed - Does the object is reconstructed (not extracted by the API)
|
|
10
|
-
* @param {Integer} pageNumber - Page number for multi pages pdf
|
|
11
|
-
*/
|
|
12
|
-
constructor({
|
|
13
|
-
prediction,
|
|
14
|
-
valueKey = "value",
|
|
15
|
-
rateKey = "rate",
|
|
16
|
-
codeKey = "code",
|
|
17
|
-
reconstructed = false,
|
|
18
|
-
pageNumber = 0,
|
|
19
|
-
}) {
|
|
20
|
-
super({ prediction, valueKey, reconstructed, pageNumber });
|
|
21
|
-
|
|
22
|
-
this.rate = parseFloat(prediction[rateKey]);
|
|
23
|
-
if (isNaN(this.rate)) this.rate = undefined;
|
|
24
|
-
|
|
25
|
-
this.code = prediction[codeKey]?.toString();
|
|
26
|
-
if (this.code === "N/A") this.code = undefined;
|
|
27
|
-
|
|
28
|
-
this.value = parseFloat(prediction[valueKey]);
|
|
29
|
-
if (isNaN(this.value)) {
|
|
30
|
-
this.value = undefined;
|
|
31
|
-
this.probability = 0.0;
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
toString() {
|
|
36
|
-
let str = "";
|
|
37
|
-
const keys = ["value", "rate", "code"];
|
|
38
|
-
for (const [i, key] of keys.entries()) {
|
|
39
|
-
const value = this[key] === undefined ? "_" : this[key].toString();
|
|
40
|
-
if (i < keys.length - 1) str += `${value}${key === "rate" ? "%" : ""}; `;
|
|
41
|
-
else str += value;
|
|
42
|
-
}
|
|
43
|
-
return str;
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
module.exports = Tax;
|
|
@@ -1,269 +0,0 @@
|
|
|
1
|
-
const Document = require("./document");
|
|
2
|
-
const Invoice = require("./invoice");
|
|
3
|
-
const Receipt = require("./receipt");
|
|
4
|
-
const Field = require("./fields").field;
|
|
5
|
-
const Date = require("./fields").date;
|
|
6
|
-
const Amount = require("./fields").amount;
|
|
7
|
-
const Locale = require("./fields").locale;
|
|
8
|
-
const Orientation = require("./fields").orientation;
|
|
9
|
-
const Tax = require("./fields").tax;
|
|
10
|
-
|
|
11
|
-
class FinancialDocument extends Document {
|
|
12
|
-
/**
|
|
13
|
-
* @param {Object} apiPrediction - Json parsed prediction from HTTP response
|
|
14
|
-
* @param {Input} input - Input object
|
|
15
|
-
* @param {Integer} pageNumber - Page number for multi pages pdf input
|
|
16
|
-
* @param {Object} locale - locale value for creating FinancialDocument object from scratch
|
|
17
|
-
* @param {Object} totalIncl - total tax included value for creating FinancialDocument object from scratch
|
|
18
|
-
* @param {Object} totalExcl - total tax excluded value for creating FinancialDocument object from scratch
|
|
19
|
-
* @param {Object} Date - date value for creating FinancialDocument object from scratch
|
|
20
|
-
* @param {Object} InvoiceNumber - Invoice number value for creating FinancialDocument object from scratch
|
|
21
|
-
* @param {Object} taxes - taxes value for creating FinancialDocument object from scratch
|
|
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
|
|
24
|
-
* @param {Object} paymentDetails - payment details value for creating FinancialDocument object from scratch
|
|
25
|
-
* @param {Object} companyNumber - company number value for creating FinancialDocument object from scratch
|
|
26
|
-
* @param {Object} vatNumber - vat number value for creating FinancialDocument object from scratch
|
|
27
|
-
* @param {Object} orientation - orientation value for creating FinancialDocument object from scratch
|
|
28
|
-
* @param {Object} totalTax - total tax value for creating FinancialDocument object from scratch
|
|
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
|
|
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
|
|
35
|
-
*/
|
|
36
|
-
constructor({
|
|
37
|
-
apiPrediction = undefined,
|
|
38
|
-
inputFile = undefined,
|
|
39
|
-
locale = undefined,
|
|
40
|
-
totalIncl = undefined,
|
|
41
|
-
totalExcl = undefined,
|
|
42
|
-
date = undefined,
|
|
43
|
-
invoiceNumber = undefined,
|
|
44
|
-
dueDate = undefined,
|
|
45
|
-
taxes = undefined,
|
|
46
|
-
supplier = undefined,
|
|
47
|
-
supplierAddress = undefined,
|
|
48
|
-
paymentDetails = undefined,
|
|
49
|
-
companyNumber = undefined,
|
|
50
|
-
vatNumber = undefined,
|
|
51
|
-
orientation = undefined,
|
|
52
|
-
totalTax = undefined,
|
|
53
|
-
time = undefined,
|
|
54
|
-
customerName = undefined,
|
|
55
|
-
customerAddress = undefined,
|
|
56
|
-
customerCompanyRegistration = undefined,
|
|
57
|
-
words = undefined,
|
|
58
|
-
pageNumber = 0,
|
|
59
|
-
level = "page",
|
|
60
|
-
}) {
|
|
61
|
-
super(inputFile);
|
|
62
|
-
this.level = level;
|
|
63
|
-
if (apiPrediction === undefined) {
|
|
64
|
-
this.#initFromScratch({
|
|
65
|
-
locale,
|
|
66
|
-
totalIncl,
|
|
67
|
-
totalExcl,
|
|
68
|
-
date,
|
|
69
|
-
invoiceNumber,
|
|
70
|
-
dueDate,
|
|
71
|
-
taxes,
|
|
72
|
-
supplier,
|
|
73
|
-
supplierAddress,
|
|
74
|
-
paymentDetails,
|
|
75
|
-
companyNumber,
|
|
76
|
-
vatNumber,
|
|
77
|
-
orientation,
|
|
78
|
-
pageNumber,
|
|
79
|
-
totalTax,
|
|
80
|
-
time,
|
|
81
|
-
customerName,
|
|
82
|
-
customerAddress,
|
|
83
|
-
customerCompanyRegistration,
|
|
84
|
-
});
|
|
85
|
-
} else {
|
|
86
|
-
this.#initFromApiPrediction(apiPrediction, inputFile, pageNumber, words);
|
|
87
|
-
}
|
|
88
|
-
this.#checklist();
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
#initFromScratch({
|
|
92
|
-
locale,
|
|
93
|
-
totalIncl,
|
|
94
|
-
totalExcl,
|
|
95
|
-
totalTax,
|
|
96
|
-
date,
|
|
97
|
-
invoiceNumber,
|
|
98
|
-
dueDate,
|
|
99
|
-
taxes,
|
|
100
|
-
paymentDetails,
|
|
101
|
-
companyNumber,
|
|
102
|
-
vatNumber,
|
|
103
|
-
orientation,
|
|
104
|
-
pageNumber,
|
|
105
|
-
supplier,
|
|
106
|
-
supplierAddress,
|
|
107
|
-
time,
|
|
108
|
-
customerName,
|
|
109
|
-
customerAddress,
|
|
110
|
-
customerCompanyRegistration,
|
|
111
|
-
}) {
|
|
112
|
-
const constructPrediction = function (item) {
|
|
113
|
-
return { prediction: { value: item }, valueKey: "value", pageNumber };
|
|
114
|
-
};
|
|
115
|
-
this.locale = new Locale(constructPrediction(locale));
|
|
116
|
-
this.totalIncl = new Amount(constructPrediction(totalIncl));
|
|
117
|
-
this.totalExcl = new Amount(constructPrediction(totalExcl));
|
|
118
|
-
this.totalTax = new Amount(constructPrediction(totalTax));
|
|
119
|
-
this.date = new Date(constructPrediction(date));
|
|
120
|
-
this.dueDate = new Date(constructPrediction(dueDate));
|
|
121
|
-
this.supplier = new Field(constructPrediction(supplier));
|
|
122
|
-
this.supplierAddress = new Field(this.constructPrediction(supplierAddress));
|
|
123
|
-
this.time = new Field(constructPrediction(time));
|
|
124
|
-
this.orientation = new Orientation(constructPrediction(orientation));
|
|
125
|
-
this.invoiceNumber = new Field(constructPrediction(invoiceNumber));
|
|
126
|
-
this.paymentDetails = new Field(constructPrediction(paymentDetails));
|
|
127
|
-
this.companyNumber = new Field(constructPrediction(companyNumber));
|
|
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
|
-
);
|
|
134
|
-
if (taxes !== undefined) {
|
|
135
|
-
this.taxes = [];
|
|
136
|
-
for (const t of taxes) {
|
|
137
|
-
this.taxes.push(
|
|
138
|
-
new Tax({
|
|
139
|
-
prediction: { value: t[0], rate: t[1] },
|
|
140
|
-
pageNumber,
|
|
141
|
-
valueKey: "value",
|
|
142
|
-
rateKey: "rate",
|
|
143
|
-
})
|
|
144
|
-
);
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
#initFromApiPrediction(apiPrediction, inputFile, pageNumber, words) {
|
|
150
|
-
if (Object.keys(apiPrediction).includes("invoice_number")) {
|
|
151
|
-
const invoice = new Invoice({
|
|
152
|
-
apiPrediction,
|
|
153
|
-
inputFile,
|
|
154
|
-
pageNumber,
|
|
155
|
-
level: this.level,
|
|
156
|
-
words: words,
|
|
157
|
-
});
|
|
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 },
|
|
174
|
-
});
|
|
175
|
-
this.customerName = invoice.customerName;
|
|
176
|
-
this.customerAddress = invoice.customerAddress;
|
|
177
|
-
this.customerCompanyRegistration = invoice.customerCompanyRegistration;
|
|
178
|
-
} else {
|
|
179
|
-
const receipt = new Receipt({
|
|
180
|
-
apiPrediction,
|
|
181
|
-
inputFile,
|
|
182
|
-
pageNumber,
|
|
183
|
-
level: this.level,
|
|
184
|
-
words: words,
|
|
185
|
-
});
|
|
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 },
|
|
196
|
-
});
|
|
197
|
-
this.time = receipt.time;
|
|
198
|
-
this.totalTax = receipt.totalTax;
|
|
199
|
-
this.invoiceNumber = new Field({
|
|
200
|
-
prediction: { value: undefined, confidence: 0.0 },
|
|
201
|
-
});
|
|
202
|
-
this.paymentDetails = new Field({
|
|
203
|
-
prediction: { value: undefined, confidence: 0.0 },
|
|
204
|
-
});
|
|
205
|
-
this.companyNumber = new Field({
|
|
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 },
|
|
213
|
-
});
|
|
214
|
-
this.customerCompanyRegistration = [];
|
|
215
|
-
if (receipt.words) this.words = receipt.words;
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
toString() {
|
|
220
|
-
return `
|
|
221
|
-
-----Financial document-----
|
|
222
|
-
Filename: ${this.filename}
|
|
223
|
-
Total amount: ${this.totalIncl.value}
|
|
224
|
-
Date: ${this.date.value}
|
|
225
|
-
Supplier: ${this.supplier.value}
|
|
226
|
-
Total taxes: ${this.totalTax.value}
|
|
227
|
-
`;
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
#checklist() {
|
|
231
|
-
this.checklist = {
|
|
232
|
-
taxesMatchTotalIncl: this.#taxesMatchTotalIncl(),
|
|
233
|
-
};
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
#taxesMatchTotalIncl() {
|
|
237
|
-
// Check taxes and total include exist
|
|
238
|
-
if (this.taxes.length === 0 || this.totalIncl.value === undefined)
|
|
239
|
-
return false;
|
|
240
|
-
|
|
241
|
-
// Reconstruct totalIncl from taxes
|
|
242
|
-
let totalVat = 0;
|
|
243
|
-
let reconstructedTotal = 0;
|
|
244
|
-
this.taxes.forEach((tax) => {
|
|
245
|
-
if (tax.value === undefined || !tax.rate) return false;
|
|
246
|
-
totalVat += tax.value;
|
|
247
|
-
reconstructedTotal += tax.value + (100 * tax.value) / tax.rate;
|
|
248
|
-
});
|
|
249
|
-
|
|
250
|
-
// Sanity check
|
|
251
|
-
if (totalVat <= 0) return false;
|
|
252
|
-
|
|
253
|
-
// Crate epsilon
|
|
254
|
-
const eps = 1 / (100 * totalVat);
|
|
255
|
-
|
|
256
|
-
if (
|
|
257
|
-
this.totalIncl.value * (1 - eps) - 0.02 <= reconstructedTotal &&
|
|
258
|
-
reconstructedTotal <= this.totalIncl.value * (1 + eps) + 0.02
|
|
259
|
-
) {
|
|
260
|
-
this.taxes = this.taxes.map((tax) => ({ ...tax, confidence: 1.0 }));
|
|
261
|
-
this.totalTax.probability = 1.0;
|
|
262
|
-
this.totalIncl.probability = 1.0;
|
|
263
|
-
return true;
|
|
264
|
-
}
|
|
265
|
-
return false;
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
module.exports = FinancialDocument;
|