mindee 1.3.2 → 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.
- package/CHANGELOG.md +47 -18
- package/README.md +43 -127
- package/bin/mindee +3 -0
- package/dist/package.json +70 -0
- package/dist/src/api/endpoint.d.ts +29 -0
- package/dist/src/api/endpoint.js +148 -0
- package/dist/src/api/index.d.ts +2 -0
- package/dist/src/api/index.js +18 -0
- package/dist/src/api/response.d.ts +52 -0
- package/dist/src/api/response.js +107 -0
- package/dist/src/cli.d.ts +1 -0
- package/dist/src/cli.js +117 -0
- package/dist/src/client.d.ts +48 -0
- package/dist/src/client.js +134 -0
- package/dist/src/constants.d.ts +13 -0
- package/dist/src/constants.js +54 -0
- package/dist/src/documents/custom.d.ts +14 -0
- package/dist/src/documents/custom.js +48 -0
- package/dist/src/documents/document.d.ts +38 -0
- package/dist/{documents → src/documents}/document.js +15 -35
- package/dist/src/documents/documentConfig.d.ts +44 -0
- package/dist/src/documents/documentConfig.js +121 -0
- package/dist/src/documents/financialDocument.d.ts +32 -0
- package/dist/src/documents/financialDocument.js +130 -0
- package/dist/src/documents/index.d.ts +12 -0
- package/dist/src/documents/index.js +20 -0
- package/dist/src/documents/invoice.d.ts +37 -0
- package/dist/src/documents/invoice.js +303 -0
- package/dist/src/documents/passport.d.ts +36 -0
- package/dist/src/documents/passport.js +218 -0
- package/dist/src/documents/receipt.d.ts +24 -0
- package/dist/src/documents/receipt.js +157 -0
- package/dist/src/errors/handler.d.ts +7 -0
- package/dist/{errors → src/errors}/handler.js +5 -4
- package/dist/src/fields/amount.d.ts +13 -0
- package/dist/{documents → src}/fields/amount.js +19 -6
- package/dist/src/fields/companyRegistration.d.ts +6 -0
- package/dist/src/fields/companyRegistration.js +11 -0
- package/dist/src/fields/customDocs.d.ts +42 -0
- package/dist/src/fields/customDocs.js +61 -0
- package/dist/src/fields/date.d.ts +12 -0
- package/dist/src/fields/date.js +25 -0
- package/dist/src/fields/field.d.ts +74 -0
- package/dist/{documents → src}/fields/field.js +48 -27
- package/dist/src/fields/fullText.d.ts +24 -0
- package/dist/src/fields/fullText.js +89 -0
- package/dist/src/fields/index.d.ts +11 -0
- package/dist/src/fields/index.js +27 -0
- package/dist/src/fields/locale.d.ts +13 -0
- package/dist/{documents → src}/fields/locale.js +20 -5
- package/dist/src/fields/orientation.d.ts +10 -0
- package/dist/{documents → src}/fields/orientation.js +5 -5
- package/dist/src/fields/paymentDetails.d.ts +31 -0
- package/dist/{documents → src}/fields/paymentDetails.js +14 -6
- package/dist/src/fields/tax.d.ts +30 -0
- package/dist/src/fields/tax.js +51 -0
- package/dist/src/geometry.d.ts +59 -0
- package/dist/src/geometry.js +113 -0
- package/dist/src/index.d.ts +2 -0
- package/dist/src/index.js +12 -0
- package/dist/src/inputs.d.ts +69 -0
- package/dist/src/inputs.js +223 -0
- package/dist/src/logger.d.ts +20 -0
- package/dist/src/logger.js +35 -0
- package/package.json +45 -26
- package/.eslintrc +0 -24
- package/.mocharc.yaml +0 -2
- package/.nyc_output/f57ba3bc-1777-4ecc-81e1-230327c2a401.json +0 -1
- package/.nyc_output/processinfo/f57ba3bc-1777-4ecc-81e1-230327c2a401.json +0 -1
- package/.nyc_output/processinfo/index.json +0 -1
- package/.prettierrc +0 -4
- package/.tool-versions +0 -1
- package/CODE_OF_CONDUCT.md +0 -63
- package/CONTRIBUTING.md +0 -84
- package/ISSUE_TEMPLATE.md +0 -45
- package/PULL_REQUEST_TEMPLATE.md +0 -45
- package/babel.config.json +0 -10
- package/coverage/coverage.json +0 -1
- package/coverage/lcov-report/base.css +0 -224
- package/coverage/lcov-report/block-navigation.js +0 -79
- package/coverage/lcov-report/favicon.png +0 -0
- package/coverage/lcov-report/index.html +0 -171
- package/coverage/lcov-report/prettify.css +0 -1
- package/coverage/lcov-report/prettify.js +0 -2
- package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/coverage/lcov-report/sorter.js +0 -170
- package/coverage/lcov.info +0 -1595
- package/coverage.lcov +0 -1595
- package/dist/api/financialDocument.js +0 -51
- package/dist/api/index.js +0 -13
- package/dist/api/invoice.js +0 -59
- package/dist/api/object.js +0 -80
- package/dist/api/receipt.js +0 -45
- package/dist/api/request.js +0 -90
- package/dist/api/response.js +0 -91
- package/dist/documents/fields/date.js +0 -17
- package/dist/documents/fields/index.js +0 -17
- package/dist/documents/fields/tax.js +0 -42
- package/dist/documents/financialDocument.js +0 -216
- package/dist/documents/index.js +0 -13
- package/dist/documents/invoice.js +0 -361
- package/dist/documents/receipt.js +0 -231
- package/dist/index.js +0 -23
- package/dist/inputs.js +0 -208
- package/dist/logger.js +0 -34
- package/examples/.env.example +0 -2
- package/examples/documents/invoices/credit_note.pdf +0 -900
- package/examples/documents/invoices/invoice.pdf +0 -0
- package/examples/documents/invoices/invoice_6p.pdf +0 -0
- package/examples/documents/receipts/receipt.jpg +0 -0
- package/examples/financialDocument.js +0 -32
- package/examples/invoice.js +0 -74
- package/examples/receipt.js +0 -45
- package/lib/api/financialDocument.js +0 -59
- package/lib/api/index.js +0 -7
- package/lib/api/invoice.js +0 -73
- package/lib/api/object.js +0 -90
- package/lib/api/receipt.js +0 -58
- package/lib/api/request.js +0 -84
- package/lib/api/response.js +0 -92
- package/lib/documents/document.js +0 -97
- package/lib/documents/fields/amount.js +0 -35
- package/lib/documents/fields/date.js +0 -36
- package/lib/documents/fields/field.js +0 -102
- package/lib/documents/fields/index.js +0 -9
- package/lib/documents/fields/locale.js +0 -35
- package/lib/documents/fields/orientation.js +0 -33
- package/lib/documents/fields/paymentDetails.js +0 -71
- package/lib/documents/fields/tax.js +0 -57
- package/lib/documents/financialDocument.js +0 -280
- package/lib/documents/index.js +0 -7
- package/lib/documents/invoice.js +0 -451
- package/lib/documents/receipt.js +0 -332
- package/lib/errors/handler.js +0 -19
- package/lib/index.js +0 -40
- package/lib/inputs.js +0 -190
- package/lib/logger.js +0 -52
- 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 -36
- package/mindee/inputs.js +0 -158
- package/mindee/logger.js +0 -33
package/lib/documents/receipt.js
DELETED
|
@@ -1,332 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
|
|
4
|
-
|
|
5
|
-
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
|
|
6
|
-
|
|
7
|
-
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
8
|
-
|
|
9
|
-
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
|
10
|
-
|
|
11
|
-
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
|
12
|
-
|
|
13
|
-
function _classPrivateMethodGet(receiver, privateSet, fn) { if (!privateSet.has(receiver)) { throw new TypeError("attempted to get private field on non-instance"); } return fn; }
|
|
14
|
-
|
|
15
|
-
var Document = require("./document");
|
|
16
|
-
|
|
17
|
-
var Field = require("./fields").field;
|
|
18
|
-
|
|
19
|
-
var Date = require("./fields").date;
|
|
20
|
-
|
|
21
|
-
var Amount = require("./fields").amount;
|
|
22
|
-
|
|
23
|
-
var Locale = require("./fields").locale;
|
|
24
|
-
|
|
25
|
-
var Orientation = require("./fields").orientation;
|
|
26
|
-
|
|
27
|
-
var Tax = require("./fields").tax;
|
|
28
|
-
|
|
29
|
-
var fs = require("fs").promises;
|
|
30
|
-
|
|
31
|
-
var _initFromScratch = new WeakSet();
|
|
32
|
-
|
|
33
|
-
var _initFromApiPrediction = new WeakSet();
|
|
34
|
-
|
|
35
|
-
var _checklist = new WeakSet();
|
|
36
|
-
|
|
37
|
-
var _taxesMatchTotal = new WeakSet();
|
|
38
|
-
|
|
39
|
-
var _reconstruct = new WeakSet();
|
|
40
|
-
|
|
41
|
-
var _reconstructTotalExclFromTCCAndTaxes = new WeakSet();
|
|
42
|
-
|
|
43
|
-
var _reconstructTotalTax = new WeakSet();
|
|
44
|
-
|
|
45
|
-
class Receipt extends Document {
|
|
46
|
-
/**
|
|
47
|
-
* @param {Object} apiPrediction - Json parsed prediction from HTTP response
|
|
48
|
-
* @param {Input} input - Input object
|
|
49
|
-
* @param {Integer} pageNumber - Page number for multi pages pdf input
|
|
50
|
-
* @param {Object} locale - locale value for creating Receipt object from scratch
|
|
51
|
-
* @param {Object} totalIncl - total tax Included value for creating Receipt object from scratch
|
|
52
|
-
* @param {Object} date - date value for creating Receipt object from scratch
|
|
53
|
-
* @param {Object} category - category value for creating Receipt object from scratch
|
|
54
|
-
* @param {Object} merchantName - merchant name value for creating Receipt object from scratch
|
|
55
|
-
* @param {Object} time - time value for creating Receipt object from scratch
|
|
56
|
-
* @param {Object} taxes - taxes value for creating Receipt object from scratch
|
|
57
|
-
* @param {Object} orientation - orientation value for creating Receipt object from scratch
|
|
58
|
-
* @param {Object} totalTax - total taxes value for creating Receipt object from scratch
|
|
59
|
-
* @param {Object} totalExcl - total taxes excluded value for creating Receipt object from scratch
|
|
60
|
-
* @param {String} level - specify whether object is built from "page" level or "document" level prediction
|
|
61
|
-
*/
|
|
62
|
-
constructor(_ref) {
|
|
63
|
-
var {
|
|
64
|
-
apiPrediction: _apiPrediction = undefined,
|
|
65
|
-
inputFile = undefined,
|
|
66
|
-
locale: _locale = undefined,
|
|
67
|
-
totalIncl: _totalIncl = undefined,
|
|
68
|
-
date: _date = undefined,
|
|
69
|
-
category: _category = undefined,
|
|
70
|
-
merchantName: _merchantName = undefined,
|
|
71
|
-
time: _time = undefined,
|
|
72
|
-
taxes: _taxes = undefined,
|
|
73
|
-
orientation: _orientation = undefined,
|
|
74
|
-
totalTax: _totalTax = undefined,
|
|
75
|
-
totalExcl: _totalExcl = undefined,
|
|
76
|
-
pageNumber: _pageNumber = 0,
|
|
77
|
-
level = "page"
|
|
78
|
-
} = _ref;
|
|
79
|
-
super(inputFile);
|
|
80
|
-
|
|
81
|
-
_reconstructTotalTax.add(this);
|
|
82
|
-
|
|
83
|
-
_reconstructTotalExclFromTCCAndTaxes.add(this);
|
|
84
|
-
|
|
85
|
-
_reconstruct.add(this);
|
|
86
|
-
|
|
87
|
-
_taxesMatchTotal.add(this);
|
|
88
|
-
|
|
89
|
-
_checklist.add(this);
|
|
90
|
-
|
|
91
|
-
_initFromApiPrediction.add(this);
|
|
92
|
-
|
|
93
|
-
_initFromScratch.add(this);
|
|
94
|
-
|
|
95
|
-
this.level = level;
|
|
96
|
-
|
|
97
|
-
this.constructPrediction = function (item) {
|
|
98
|
-
return {
|
|
99
|
-
prediction: {
|
|
100
|
-
value: item
|
|
101
|
-
},
|
|
102
|
-
valueKey: "value",
|
|
103
|
-
pageNumber: _pageNumber
|
|
104
|
-
};
|
|
105
|
-
};
|
|
106
|
-
|
|
107
|
-
if (_apiPrediction === undefined) {
|
|
108
|
-
_classPrivateMethodGet(this, _initFromScratch, _initFromScratch2).call(this, {
|
|
109
|
-
locale: _locale,
|
|
110
|
-
totalExcl: _totalExcl,
|
|
111
|
-
totalIncl: _totalIncl,
|
|
112
|
-
date: _date,
|
|
113
|
-
category: _category,
|
|
114
|
-
merchantName: _merchantName,
|
|
115
|
-
time: _time,
|
|
116
|
-
taxes: _taxes,
|
|
117
|
-
orientation: _orientation,
|
|
118
|
-
totalTax: _totalTax,
|
|
119
|
-
pageNumber: _pageNumber
|
|
120
|
-
});
|
|
121
|
-
} else {
|
|
122
|
-
_classPrivateMethodGet(this, _initFromApiPrediction, _initFromApiPrediction2).call(this, _apiPrediction, _pageNumber);
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
_classPrivateMethodGet(this, _checklist, _checklist2).call(this);
|
|
126
|
-
|
|
127
|
-
_classPrivateMethodGet(this, _reconstruct, _reconstruct2).call(this);
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
toString() {
|
|
131
|
-
return "\n -----Receipt data-----\n Filename: ".concat(this.filename, "\n Total amount: ").concat(this.totalIncl.value, "\n Date: ").concat(this.date.value, "\n Category: ").concat(this.category.value, "\n Time: ").concat(this.time.value, "\n Merchant name: ").concat(this.merchantName.value, "\n Taxes: ").concat(this.taxes.map(tax => tax.toString()).join(" - "), "\n Total taxes: ").concat(this.totalTax.value, "\n ");
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
static load(path) {
|
|
135
|
-
return _asyncToGenerator(function* () {
|
|
136
|
-
var file = fs.readFile(path);
|
|
137
|
-
var args = JSON.parse(file);
|
|
138
|
-
return new Receipt(_objectSpread({
|
|
139
|
-
reconsctruted: true
|
|
140
|
-
}, args));
|
|
141
|
-
})();
|
|
142
|
-
}
|
|
143
|
-
/**
|
|
144
|
-
* Call all check methods
|
|
145
|
-
*/
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
var _initFromScratch2 = function _initFromScratch2(_ref2) {
|
|
151
|
-
var {
|
|
152
|
-
locale,
|
|
153
|
-
totalExcl,
|
|
154
|
-
totalIncl,
|
|
155
|
-
date,
|
|
156
|
-
category,
|
|
157
|
-
merchantName,
|
|
158
|
-
time,
|
|
159
|
-
taxes,
|
|
160
|
-
orientation,
|
|
161
|
-
totalTax,
|
|
162
|
-
pageNumber
|
|
163
|
-
} = _ref2;
|
|
164
|
-
this.locale = new Locale(this.constructPrediction(locale));
|
|
165
|
-
this.totalIncl = new Amount(this.constructPrediction(totalIncl));
|
|
166
|
-
this.date = new Date(this.constructPrediction(date));
|
|
167
|
-
this.category = new Field(this.constructPrediction(category));
|
|
168
|
-
this.merchantName = new Field(this.constructPrediction(merchantName));
|
|
169
|
-
this.time = new Field(this.constructPrediction(time));
|
|
170
|
-
|
|
171
|
-
if (taxes !== undefined) {
|
|
172
|
-
this.taxes = [];
|
|
173
|
-
|
|
174
|
-
for (var t of taxes) {
|
|
175
|
-
this.taxes.push(new Tax({
|
|
176
|
-
prediction: {
|
|
177
|
-
value: t[0],
|
|
178
|
-
rate: t[1]
|
|
179
|
-
},
|
|
180
|
-
pageNumber,
|
|
181
|
-
valueKey: "value",
|
|
182
|
-
rateKey: "rate"
|
|
183
|
-
}));
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
this.orientation = new Orientation(this.constructPrediction(orientation));
|
|
188
|
-
this.totalTax = new Amount(this.constructPrediction(totalTax));
|
|
189
|
-
this.totalExcl = new Amount(this.constructPrediction(totalExcl));
|
|
190
|
-
};
|
|
191
|
-
|
|
192
|
-
var _initFromApiPrediction2 = function _initFromApiPrediction2(apiPrediction, pageNumber) {
|
|
193
|
-
this.words = [];
|
|
194
|
-
this.locale = new Locale({
|
|
195
|
-
prediction: apiPrediction.locale,
|
|
196
|
-
pageNumber
|
|
197
|
-
});
|
|
198
|
-
this.totalIncl = new Amount({
|
|
199
|
-
prediction: apiPrediction.total_incl,
|
|
200
|
-
valueKey: "value",
|
|
201
|
-
pageNumber
|
|
202
|
-
});
|
|
203
|
-
this.date = new Date({
|
|
204
|
-
prediction: apiPrediction.date,
|
|
205
|
-
valueKey: "value",
|
|
206
|
-
pageNumber
|
|
207
|
-
});
|
|
208
|
-
this.category = new Field({
|
|
209
|
-
prediction: apiPrediction.category,
|
|
210
|
-
pageNumber
|
|
211
|
-
});
|
|
212
|
-
this.merchantName = new Field({
|
|
213
|
-
prediction: apiPrediction.supplier,
|
|
214
|
-
valueKey: "value",
|
|
215
|
-
pageNumber
|
|
216
|
-
});
|
|
217
|
-
this.time = new Field({
|
|
218
|
-
prediction: apiPrediction.time,
|
|
219
|
-
valueKey: "value",
|
|
220
|
-
pageNumber
|
|
221
|
-
});
|
|
222
|
-
this.taxes = apiPrediction.taxes.map(taxPrediction => new Tax({
|
|
223
|
-
prediction: taxPrediction,
|
|
224
|
-
pageNumber,
|
|
225
|
-
valueKey: "value",
|
|
226
|
-
rateKey: "rate",
|
|
227
|
-
codeKey: "code"
|
|
228
|
-
}));
|
|
229
|
-
this.totalTax = new Amount({
|
|
230
|
-
prediction: {
|
|
231
|
-
value: undefined,
|
|
232
|
-
probability: 0
|
|
233
|
-
},
|
|
234
|
-
valueKey: "value",
|
|
235
|
-
pageNumber
|
|
236
|
-
});
|
|
237
|
-
this.totalExcl = new Amount({
|
|
238
|
-
prediction: {
|
|
239
|
-
value: undefined,
|
|
240
|
-
probability: 0
|
|
241
|
-
},
|
|
242
|
-
valueKey: "value",
|
|
243
|
-
pageNumber
|
|
244
|
-
});
|
|
245
|
-
|
|
246
|
-
if (this.level === "page") {
|
|
247
|
-
this.orientation = new Orientation({
|
|
248
|
-
prediction: apiPrediction.orientation,
|
|
249
|
-
pageNumber
|
|
250
|
-
});
|
|
251
|
-
} else {
|
|
252
|
-
this.orientation = new Orientation(this.constructPrediction({
|
|
253
|
-
prediction: {
|
|
254
|
-
value: undefined,
|
|
255
|
-
probability: 0.0,
|
|
256
|
-
degrees: undefined
|
|
257
|
-
}
|
|
258
|
-
}));
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
if ("mvision" in apiPrediction) this.words = apiPrediction.mvision;
|
|
262
|
-
};
|
|
263
|
-
|
|
264
|
-
var _checklist2 = function _checklist2() {
|
|
265
|
-
this.checklist = {
|
|
266
|
-
taxesMatchTotalIncl: _classPrivateMethodGet(this, _taxesMatchTotal, _taxesMatchTotal2).call(this)
|
|
267
|
-
};
|
|
268
|
-
};
|
|
269
|
-
|
|
270
|
-
var _taxesMatchTotal2 = function _taxesMatchTotal2() {
|
|
271
|
-
// Check taxes and total amount exist
|
|
272
|
-
if (this.taxes.length === 0 || this.totalIncl.value == null) return false; // Reconstruct total_incl from taxes
|
|
273
|
-
|
|
274
|
-
var totalVat = 0;
|
|
275
|
-
var reconstructedTotal = 0;
|
|
276
|
-
this.taxes.forEach(tax => {
|
|
277
|
-
if (tax.value == null || !tax.rate) return false;
|
|
278
|
-
totalVat += tax.value;
|
|
279
|
-
reconstructedTotal += tax.value + 100 * tax.value / tax.rate;
|
|
280
|
-
}); // Sanity check
|
|
281
|
-
|
|
282
|
-
if (totalVat <= 0) return false; // Crate epsilon
|
|
283
|
-
|
|
284
|
-
var eps = 1 / (100 * totalVat);
|
|
285
|
-
|
|
286
|
-
if (this.totalIncl.value * (1 - eps) - 0.02 <= reconstructedTotal && reconstructedTotal <= this.totalIncl.value * (1 + eps) + 0.02) {
|
|
287
|
-
this.taxes = this.taxes.map(tax => _objectSpread(_objectSpread({}, tax), {}, {
|
|
288
|
-
probability: 1.0
|
|
289
|
-
}));
|
|
290
|
-
this.totalTax.probability = 1.0;
|
|
291
|
-
this.totalIncl.probability = 1.0;
|
|
292
|
-
return true;
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
return false;
|
|
296
|
-
};
|
|
297
|
-
|
|
298
|
-
var _reconstruct2 = function _reconstruct2() {
|
|
299
|
-
_classPrivateMethodGet(this, _reconstructTotalExclFromTCCAndTaxes, _reconstructTotalExclFromTCCAndTaxes2).call(this);
|
|
300
|
-
|
|
301
|
-
_classPrivateMethodGet(this, _reconstructTotalTax, _reconstructTotalTax2).call(this);
|
|
302
|
-
};
|
|
303
|
-
|
|
304
|
-
var _reconstructTotalExclFromTCCAndTaxes2 = function _reconstructTotalExclFromTCCAndTaxes2() {
|
|
305
|
-
if (this.taxes.length && this.totalIncl.value != null) {
|
|
306
|
-
var totalExcl = {
|
|
307
|
-
value: this.totalIncl.value - Field.arraySum(this.taxes),
|
|
308
|
-
probability: Field.arrayProbability(this.taxes) * this.totalIncl.probability
|
|
309
|
-
};
|
|
310
|
-
this.totalExcl = new Amount({
|
|
311
|
-
prediction: totalExcl,
|
|
312
|
-
valueKey: "value",
|
|
313
|
-
reconstructed: true
|
|
314
|
-
});
|
|
315
|
-
}
|
|
316
|
-
};
|
|
317
|
-
|
|
318
|
-
var _reconstructTotalTax2 = function _reconstructTotalTax2() {
|
|
319
|
-
if (this.taxes.length && this.totalTax.value == null) {
|
|
320
|
-
var totalTax = {
|
|
321
|
-
value: this.taxes.map(tax => tax.value || 0).reduce((a, b) => a + b, 0),
|
|
322
|
-
probability: Field.arrayProbability(this.taxes)
|
|
323
|
-
};
|
|
324
|
-
if (totalTax.value > 0) this.totalTax = new Amount({
|
|
325
|
-
prediction: totalTax,
|
|
326
|
-
valueKey: "value",
|
|
327
|
-
reconstructed: true
|
|
328
|
-
});
|
|
329
|
-
}
|
|
330
|
-
};
|
|
331
|
-
|
|
332
|
-
module.exports = Receipt;
|
package/lib/errors/handler.js
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
var logger = require("../logger");
|
|
4
|
-
|
|
5
|
-
class ErrorHandler {
|
|
6
|
-
constructor() {
|
|
7
|
-
var throwOnError = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
|
|
8
|
-
this.throwOnError = throwOnError;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
throw(error) {
|
|
12
|
-
var force = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
|
|
13
|
-
if (this.throwOnError || force) throw error;else logger.error(error.message);
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
var errorHandler = new ErrorHandler();
|
|
19
|
-
module.exports = errorHandler;
|
package/lib/index.js
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
exports.__esModule = true;
|
|
4
|
-
exports.Client = void 0;
|
|
5
|
-
|
|
6
|
-
var handler_1 = require("@errors/handler");
|
|
7
|
-
|
|
8
|
-
var logger_1 = require("@mindee/logger");
|
|
9
|
-
|
|
10
|
-
var receipt_1 = require("@mindee/api/receipt");
|
|
11
|
-
|
|
12
|
-
var invoice_1 = require("@mindee/api/invoice");
|
|
13
|
-
|
|
14
|
-
var financialDocument_1 = require("@mindee/api/financialDocument");
|
|
15
|
-
|
|
16
|
-
var Client =
|
|
17
|
-
/** @class */
|
|
18
|
-
function () {
|
|
19
|
-
function Client(_a) {
|
|
20
|
-
var receiptToken = _a.receiptToken,
|
|
21
|
-
invoiceToken = _a.invoiceToken,
|
|
22
|
-
_b = _a.throwOnError,
|
|
23
|
-
throwOnError = _b === void 0 ? true : _b,
|
|
24
|
-
debug = _a.debug;
|
|
25
|
-
this.receiptToken = receiptToken || process.env.MINDEE_RECEIPT_TOKEN;
|
|
26
|
-
this.invoiceToken = invoiceToken || process.env.MINDEE_INVOICE_TOKEN;
|
|
27
|
-
handler_1.errorHandler.throwOnError = throwOnError;
|
|
28
|
-
logger_1.logger.level = (debug !== null && debug !== void 0 ? debug : process.env.MINDEE_DEBUG) ? "debug" : "warn";
|
|
29
|
-
this.receipt = new receipt_1.APIReceipt(this.receiptToken);
|
|
30
|
-
this.invoice = new invoice_1.APIInvoice(this.invoiceToken);
|
|
31
|
-
this.financialDocument = new financialDocument_1.APIFinancialDocument(this.invoiceToken, this.receiptToken);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
return Client;
|
|
35
|
-
}();
|
|
36
|
-
|
|
37
|
-
exports.Client = Client;
|
|
38
|
-
exports.documents = require("./documents");
|
|
39
|
-
exports.api = require("./api");
|
|
40
|
-
exports.inputs = require("./inputs");
|
package/lib/inputs.js
DELETED
|
@@ -1,190 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
|
4
|
-
|
|
5
|
-
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
|
6
|
-
|
|
7
|
-
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
8
|
-
|
|
9
|
-
var fs = require("fs").promises;
|
|
10
|
-
|
|
11
|
-
var errorHandler = require("./errors/handler");
|
|
12
|
-
|
|
13
|
-
var path = require("path");
|
|
14
|
-
|
|
15
|
-
var {
|
|
16
|
-
PDFDocument
|
|
17
|
-
} = require("pdf-lib");
|
|
18
|
-
|
|
19
|
-
var concat = require("concat-stream");
|
|
20
|
-
|
|
21
|
-
var {
|
|
22
|
-
Base64Encode
|
|
23
|
-
} = require("base64-stream");
|
|
24
|
-
|
|
25
|
-
var fileType = require("file-type");
|
|
26
|
-
|
|
27
|
-
var ArrayBufferEncode = require("base64-arraybuffer");
|
|
28
|
-
|
|
29
|
-
class Input {
|
|
30
|
-
/**
|
|
31
|
-
* @param {(String | Buffer)} file - the file that will be read. Either path or base64 string, or a steam
|
|
32
|
-
* @param {String} inputType - the type of input used in file ("base64", "path", "dummy").
|
|
33
|
-
* NB: dummy is only used for tests purposes
|
|
34
|
-
* @param {String} filename - File name of the input
|
|
35
|
-
* @param {Boolean} cut_pdf: Automatically reconstruct pdf with more than 4 pages
|
|
36
|
-
* NB: Because of async calls, init() should be called after creating the object
|
|
37
|
-
*/
|
|
38
|
-
constructor(_ref) {
|
|
39
|
-
var {
|
|
40
|
-
file,
|
|
41
|
-
filename = undefined,
|
|
42
|
-
inputType,
|
|
43
|
-
allowCutPdf = true
|
|
44
|
-
} = _ref;
|
|
45
|
-
|
|
46
|
-
_defineProperty(this, "MIMETYPES", {
|
|
47
|
-
png: "image/png",
|
|
48
|
-
jpg: "image/jpg",
|
|
49
|
-
jpeg: "image/jpeg",
|
|
50
|
-
webp: "image/webp",
|
|
51
|
-
pdf: "application/pdf"
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
_defineProperty(this, "ALLOWED_INPUT_TYPE", ["base64", "path", "stream", "dummy"]);
|
|
55
|
-
|
|
56
|
-
_defineProperty(this, "CUT_PDF_SIZE", 5);
|
|
57
|
-
|
|
58
|
-
// Check if inputType is valid
|
|
59
|
-
if (!this.ALLOWED_INPUT_TYPE.includes(inputType)) {
|
|
60
|
-
errorHandler.throw(new Error("The input type is invalid. It should be ".concat(this.ALLOWED_INPUT_TYPE.toString())));
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
this.file = file;
|
|
64
|
-
this.filename = filename;
|
|
65
|
-
this.inputType = inputType;
|
|
66
|
-
this.allowCutPdf = allowCutPdf;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
init() {
|
|
70
|
-
var _this = this;
|
|
71
|
-
|
|
72
|
-
return _asyncToGenerator(function* () {
|
|
73
|
-
if (_this.inputType === "base64") yield _this.initBase64();else if (_this.inputType === "path") yield _this.initFile();else if (_this.inputType === "stream") yield _this.initStream();else _this.initDummy();
|
|
74
|
-
})();
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
initBase64() {
|
|
78
|
-
var _this2 = this;
|
|
79
|
-
|
|
80
|
-
return _asyncToGenerator(function* () {
|
|
81
|
-
_this2.fileObject = _this2.file;
|
|
82
|
-
_this2.filepath = undefined;
|
|
83
|
-
_this2.fileExtension = undefined;
|
|
84
|
-
|
|
85
|
-
if (_this2.allowCutPdf == true) {
|
|
86
|
-
var typeOfFile = yield fileType.fromBuffer(Buffer.from(_this2.fileObject, "base64"));
|
|
87
|
-
|
|
88
|
-
if (typeOfFile === undefined) {
|
|
89
|
-
console.error("Cannot detect mime type of: ".concat(_this2.fileObject));
|
|
90
|
-
} else if (typeOfFile.mime === "application/pdf") {
|
|
91
|
-
yield _this2.cutPdf();
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
})();
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
initFile() {
|
|
98
|
-
var _this3 = this;
|
|
99
|
-
|
|
100
|
-
return _asyncToGenerator(function* () {
|
|
101
|
-
_this3.fileObject = yield fs.readFile(_this3.file);
|
|
102
|
-
_this3.filepath = _this3.file;
|
|
103
|
-
_this3.filename = _this3.filename || path.basename(_this3.file); // Check if file type is valid
|
|
104
|
-
|
|
105
|
-
var filetype = _this3.filename.split(".").pop();
|
|
106
|
-
|
|
107
|
-
console.log(_this3.filename.split("."));
|
|
108
|
-
|
|
109
|
-
if (!(filetype in _this3.MIMETYPES)) {
|
|
110
|
-
errorHandler.throw(new Error("File type is not allowed. It must be ".concat(Object.keys(_this3.MIMETYPES).toString())));
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
_this3.fileExtension = _this3.MIMETYPES[filetype];
|
|
114
|
-
|
|
115
|
-
if (_this3.fileExtension === "application/pdf" && _this3.allowCutPdf == true) {
|
|
116
|
-
yield _this3.cutPdf();
|
|
117
|
-
}
|
|
118
|
-
})();
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
initStream() {
|
|
122
|
-
var _this4 = this;
|
|
123
|
-
|
|
124
|
-
return _asyncToGenerator(function* () {
|
|
125
|
-
var file = yield _this4.streamToBase64(_this4.file);
|
|
126
|
-
_this4.file = file;
|
|
127
|
-
_this4.inputType = "base64";
|
|
128
|
-
yield _this4.initBase64();
|
|
129
|
-
})();
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
initDummy() {
|
|
133
|
-
this.fileObject = "";
|
|
134
|
-
this.filename = "";
|
|
135
|
-
this.filepath = "";
|
|
136
|
-
this.fileExtension = "";
|
|
137
|
-
}
|
|
138
|
-
/**
|
|
139
|
-
* Convert ReadableStream to Base64 encoded String
|
|
140
|
-
*
|
|
141
|
-
* @param {*} stream ReadableStream to encode
|
|
142
|
-
* @returns Base64 encoded String
|
|
143
|
-
*/
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
streamToBase64(stream) {
|
|
147
|
-
return _asyncToGenerator(function* () {
|
|
148
|
-
return new Promise((resolve, reject) => {
|
|
149
|
-
var base64 = new Base64Encode();
|
|
150
|
-
|
|
151
|
-
var cbConcat = base64 => {
|
|
152
|
-
resolve(base64);
|
|
153
|
-
};
|
|
154
|
-
|
|
155
|
-
stream.pipe(base64).pipe(concat(cbConcat)).on("error", error => {
|
|
156
|
-
reject(error);
|
|
157
|
-
});
|
|
158
|
-
});
|
|
159
|
-
})();
|
|
160
|
-
}
|
|
161
|
-
/** Cut PDF if pages > 5 */
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
cutPdf() {
|
|
165
|
-
var _this5 = this;
|
|
166
|
-
|
|
167
|
-
return _asyncToGenerator(function* () {
|
|
168
|
-
// convert document to PDFDocument & cut CUT_PDF_SIZE - 1 first pages and last page
|
|
169
|
-
var pdfDocument = yield PDFDocument.load(_this5.fileObject, {
|
|
170
|
-
ignoreEncryption: true
|
|
171
|
-
});
|
|
172
|
-
var splitedPdfDocument = yield PDFDocument.create();
|
|
173
|
-
var pdfLength = pdfDocument.getPageCount();
|
|
174
|
-
if (pdfLength <= _this5.CUT_PDF_SIZE) return;
|
|
175
|
-
var pagesNumbers = [...Array(_this5.CUT_PDF_SIZE - 1).keys(), pdfLength - 1];
|
|
176
|
-
var pages = yield splitedPdfDocument.copyPages(pdfDocument, pagesNumbers);
|
|
177
|
-
pages.forEach(page => splitedPdfDocument.addPage(page));
|
|
178
|
-
var data = yield splitedPdfDocument.save();
|
|
179
|
-
|
|
180
|
-
if (_this5.inputType === "path") {
|
|
181
|
-
_this5.fileObject = Buffer.from(data);
|
|
182
|
-
} else if (_this5.inputType === "base64") {
|
|
183
|
-
_this5.fileObject = ArrayBufferEncode.encode(data);
|
|
184
|
-
}
|
|
185
|
-
})();
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
module.exports = Input;
|
package/lib/logger.js
DELETED
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
var LOGGER_LEVELS = {
|
|
4
|
-
debug: 0,
|
|
5
|
-
info: 1,
|
|
6
|
-
warn: 2,
|
|
7
|
-
error: 3
|
|
8
|
-
};
|
|
9
|
-
|
|
10
|
-
class Logger {
|
|
11
|
-
constructor() {
|
|
12
|
-
var level = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "debug";
|
|
13
|
-
if (!(level in LOGGER_LEVELS)) level = "debug";
|
|
14
|
-
this.level = LOGGER_LEVELS[level];
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
debug() {
|
|
18
|
-
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
19
|
-
args[_key] = arguments[_key];
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
if (this.level <= LOGGER_LEVELS["debug"]) console.debug(args);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
info() {
|
|
26
|
-
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
|
|
27
|
-
args[_key2] = arguments[_key2];
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
if (this.level <= LOGGER_LEVELS["info"]) console.info(args);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
warn() {
|
|
34
|
-
for (var _len3 = arguments.length, args = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
|
|
35
|
-
args[_key3] = arguments[_key3];
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
if (this.level <= LOGGER_LEVELS["warn"]) console.warn(args);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
error() {
|
|
42
|
-
for (var _len4 = arguments.length, args = new Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
|
|
43
|
-
args[_key4] = arguments[_key4];
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
if (this.level <= LOGGER_LEVELS["error"]) console.error(args);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
var logger = new Logger();
|
|
52
|
-
module.exports = logger;
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
const APIObject = require("./object");
|
|
2
|
-
const Input = require("../inputs");
|
|
3
|
-
|
|
4
|
-
class APIFinancialDocument extends APIObject {
|
|
5
|
-
constructor(invoiceToken, receiptToken) {
|
|
6
|
-
super(undefined, "financialDocument");
|
|
7
|
-
this.invoiceToken = invoiceToken;
|
|
8
|
-
this.receiptToken = receiptToken;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* @param {String} file: Receipt filepath (allowed jpg, png, tiff, pdf)
|
|
13
|
-
* @param {String} inputType: String in {'path', 'stream', 'base64'}
|
|
14
|
-
* @param {Boolean} includeWords: extract all words into http_response
|
|
15
|
-
* @param {String} version: expense_receipt api version
|
|
16
|
-
* @param {Boolean} cutPdf: Automatically reconstruct pdf with more than 4 pages
|
|
17
|
-
* @returns {Response} Wrapped response with Receipts objects parsed
|
|
18
|
-
*/
|
|
19
|
-
async parse({
|
|
20
|
-
input,
|
|
21
|
-
inputType = "path",
|
|
22
|
-
filename = undefined,
|
|
23
|
-
version = "3",
|
|
24
|
-
cutPdf = true,
|
|
25
|
-
includeWords = false,
|
|
26
|
-
}) {
|
|
27
|
-
const inputFile = new Input({
|
|
28
|
-
file: input,
|
|
29
|
-
inputType: inputType,
|
|
30
|
-
filename: filename,
|
|
31
|
-
allowCutPdf: cutPdf,
|
|
32
|
-
});
|
|
33
|
-
await inputFile.init();
|
|
34
|
-
this.apiToken =
|
|
35
|
-
inputFile.fileExtension === "application/pdf"
|
|
36
|
-
? this.invoiceToken
|
|
37
|
-
: this.receiptToken;
|
|
38
|
-
const url =
|
|
39
|
-
inputFile.fileExtension === "application/pdf"
|
|
40
|
-
? `/invoices/v${version}/predict`
|
|
41
|
-
: `/expense_receipts/v${version}/predict`;
|
|
42
|
-
super.parse();
|
|
43
|
-
return super._request(url, inputFile, includeWords);
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
module.exports = APIFinancialDocument;
|