-
Notifications
You must be signed in to change notification settings - Fork 0
/
script-solo-fatture.js
413 lines (397 loc) · 17.7 KB
/
script-solo-fatture.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
const convert = require('xml-js');
const { parse } = require('json2csv');
const fs = require('fs');
let atob = require('atob')
const re = /FatturaElettronica/;
const comoRegEx = /ntgen/i;
const PDFJS = require('pdfjs-dist')
//*************************************************** */
//DECLARING ALL OUR VARIABLES
//*************************************************** */
const PDV = ['SAVONA', 'EUSTACHI', 'MARGHERA', 'CARMAGNOLA', 'TICINESE', 'GIACOMO MORA', 'COMO', 'BLIGNY'];
const SAVONA = ['AGRICOLA VARESINA S.R.L.', ]
const TICINESE = ['AARON Service Srl', 'Metro Italia Cash and Carry S.p.A']
const UFFICIO = ['NESPRESSO ITALIANA SPA', 'Notarbartolo & Gervasi S.p.A.', 'CARPOFORO SRL', 'EDOARDO SCINETTI', 'DriveNow Italy S.r.l. c/o BMW Group',];
const Delivero = [[17183, 'MARGHERA'], [82848, 'EUSTACHI'], [76908, 'CARMAGNOLA'], [77408, 'SAVONA'], [112001, 'TICINESE'], [127360, 'COMO']]
const Glovo = [['P44026', 'CARMAGNOLA'], ['P2292', 'SAVONA'], ['P8413', 'MARGHERA'], ['P8280', 'EUSTACHI'], ['P94710', 'TICINESE'], ['P114791', 'COMO']]
const individualFattura = {
xml: '',
numeroFattura: '',
fornitore: '',
dataFattura: '',
dataScadenza: '',
importo: '',
puntoVendita: ''
}
//**************************************************************************************************************************************************************************** */
//*************************************************** */
//INPUT FOLDER
//*************************************************** */
const folder = '../Fatture 01-11 - 14-11/xml/'
let fileArray = fs.readdirSync(folder);
//*************************************************** */
//MOVE INVOICES TO RELATIVE FOLDERS?
//*************************************************** */
const moveByPDV = false
const destinationFolder = './Locali/'
//*************************************************** */
//WRITE CSV FILE?
//*************************************************** */
const writeToCSV = true
const writeFileName = 'Fatture_14-11-19'
//FUNCTION USED BY getText() TO PROCESS PDF TEXT
const getPageText = async (pdf, pageNo) => {
try {
const page = await pdf.getPage(pageNo);
const tokenText = await page.getTextContent();
const pageText = tokenText.items.map(token => token.str).join('')
return pageText;
} catch (error) {
console.log('failed at getPageText')
}
}
// GET TEXT (FROM PDF OR OTHER MEANS) TO USE FOR PDV DETECTION
const getText = async (data) => {
let pdfArray = []
for (let unit of data) {
let buf = unit[1];
let file = unit[0].xml
try {
if (typeof buf === "string") {
pdfArray.push([buf, file])
} else if (typeof buf === "number") {
buf = "No pdv in allegato"
pdfArray.push([buf, file])
} else {
const pdf = await PDFJS.getDocument(buf);
const maxPages = pdf['_pdfInfo'].numPages;
const pageTextPromises = [];
for (let pageNo = 1; pageNo <= maxPages; pageNo += 1) {
pageTextPromises.push(getPageText(pdf, pageNo))
}
const pageTexts = await Promise.all(pageTextPromises)
pdfArray.push([pageTexts.join(''), file])
}
} catch (error) {
console.log(error)
console.log('failed at getText', file)
}
}
return pdfArray;
}
//CONTROLS AND SETS THE PDV FOR EVERY INVOICE
const pdv = async (pdf) => {
let output = [];
for (let individualPdf of pdf) {
let check = false;
let file = individualPdf[1];
let text = individualPdf[0].toUpperCase();
for (let pdv of PDV) {
if (text.includes(pdv)) {
if (pdv === "GIACOMO MORA") {
check = true;
output.push([PDV[4], file]);
} else if (comoRegEx.test(text)) {
check = true;
output.push(['COMO', file])
} else {
check = true;
output.push([pdv, file]);
}
}
}
if (!check) {
let result = 'No pdv in allegato';
output.push([result, file]);
}
}
return output;
}
//INITIAL READ OF ALL FILES
const readingFile = async (fileArray) => {
let entriesArray = []
for (let file of fileArray) {
const fatturaXML = fs.readFileSync(folder + file);
const json = convert.xml2json(fatturaXML, { compact: true, spaces: 4 });
let fattura = JSON.parse(json);
const entries = Object.entries(fattura);
error = file;
entriesArray.push([entries, file])
}
return entriesArray;
}
const stringXML = async (fileArray) => {
let stringArray = []
for (let file of fileArray) {
let check = false;
error = file;
const fatturaXML = fs.readFileSync(folder + file);
for (let _pdv of PDV) {
if (fatturaXML.toString().toUpperCase().includes(_pdv)) {
if (_pdv === "GIACOMO MORA") {
check = true;
stringArray.push([PDV[4], file]);
} else {
check = true;
stringArray.push([_pdv, file]);
}
} else if (comoRegEx.test(fatturaXML.toString())) {
check = true;
stringArray.push(['BLIGNY', file]);
}
}
if (!check) {
let result = 'No pdv in allegato';
stringArray.push([result, file]);
}
}
return stringArray;
}
//FETCHES THE DATA FROM THE NODES
const fetchData = async (array, file) => {
let temp = {}
try {
array.FatturaElettronicaBody.DatiPagamento === undefined || array.FatturaElettronicaBody.DatiPagamento.DettaglioPagamento.DataScadenzaPagamento === undefined
? dataScadenza = ''
: dataScadenza = array.FatturaElettronicaBody.DatiPagamento.DettaglioPagamento.DataScadenzaPagamento['_text']
numeroFattura = array.FatturaElettronicaBody.DatiGenerali.DatiGeneraliDocumento.Numero['_text']
dataFattura = array.FatturaElettronicaBody.DatiGenerali.DatiGeneraliDocumento.Data['_text']
array.FatturaElettronicaHeader.CedentePrestatore.DatiAnagrafici.Anagrafica.Denominazione !== undefined
? fornitore = array.FatturaElettronicaHeader.CedentePrestatore.DatiAnagrafici.Anagrafica.Denominazione['_text']
: fornitore = array.FatturaElettronicaHeader.CedentePrestatore.DatiAnagrafici.Anagrafica.Nome['_text'] + " " + array.FatturaElettronicaHeader.CedentePrestatore.DatiAnagrafici.Anagrafica.Cognome['_text']
array.FatturaElettronicaBody.DatiPagamento !== undefined
? array.FatturaElettronicaBody.DatiPagamento.DettaglioPagamento.ImportoPagamento === undefined
? importo = array.FatturaElettronicaBody.DatiGenerali.DatiGeneraliDocumento.ImportoTotaleDocumento['_text']
: importo = array.FatturaElettronicaBody.DatiPagamento.DettaglioPagamento.ImportoPagamento['_text']
: importo = array.FatturaElettronicaBody.DatiGenerali.DatiGeneraliDocumento.ImportoTotaleDocumento['_text']
// Checks if document is Credit Note
if (array.FatturaElettronicaBody.DatiGenerali.DatiGeneraliDocumento.TipoDocumento['_text'] === 'TD04') {
importo = importo * -1
importo = importo.toString();
}
individualFattura.xml = file
individualFattura.numeroFattura = numeroFattura
individualFattura.dataScadenza = dataScadenza
individualFattura.dataFattura = dataFattura
individualFattura.fornitore = fornitore
individualFattura.importo = importo
Object.assign(temp, individualFattura)
return temp;
} catch (error) {
console.log(error)
console.log('failed at fetchData', file)
}
}
//COMBINES THE INVOICE AND PDV DATA AND PUSHES IT TO INVOICE OBJECT
const pushData = async (invData, pdvData, xmlData) => {
let tempData = []
let newPdvData = []
let check;
let prev = []
pdvData.map((data) => {
check = false
for (let xml of xmlData) {
if (data[1] === xml[1]) {
if (data[0] === xml[0]) {
check = true
if (data[1] !== prev) {
newPdvData.push(data)
}
prev = data[1]
} else if (data[0] === "No pdv in allegato" && xml[0] !== "No pdv in allegato") {
// console.log('not equal' ,data[0],xml[0])
if (data[1] !== prev) {
newPdvData.push([xml[0], data[1]])
}
prev = data[1]
} else {
if (data[1] !== prev) {
newPdvData.push(data)
}
prev = data[1]
}
}
}
})
for (let invoices of invData) {
let invoice = invoices[0];
for (let file of newPdvData) {
if (file[1] === invoice.xml) {
invoice.puntoVendita = file[0]
try {
//*************************************************** */
//USEFUL ONLY FOR INSERTING ACQUISTI
//*************************************************** */
if (moveByPDV) {
switch(file[0]) {
case 'SAVONA':
fs.renameSync(folder + file[1], `${destinationFolder}Savona/${invoice.fornitore}_${file[1]}`)
break;
case 'EUSTACHI':
fs.renameSync(folder + file[1], `${destinationFolder}Eustachi/${invoice.fornitore}_${file[1]}`)
break;
case 'MARGHERA':
fs.renameSync(folder + file[1], `${destinationFolder}Marghera/${invoice.fornitore}_${file[1]}`)
break;
case 'CARMAGNOLA':
fs.renameSync(folder + file[1], `${destinationFolder}Carmagnola/${invoice.fornitore}_${file[1]}`)
break;
case 'TICINESE':
fs.renameSync(folder + file[1], `${destinationFolder}Ticinese/${invoice.fornitore}_${file[1]}`)
break;
case 'COMO':
fs.renameSync(folder + file[1], `${destinationFolder}Como/${invoice.fornitore}_${file[1]}`)
break;
default:
switch(invoice.fornitore) {
case 'Foodinho, SRL':
fs.renameSync(folder + file[1], `${destinationFolder}Delivery/${invoice.fornitore}_${file[1]}`)
break;
case 'DELIVEROO ITALY S.r.l.':
fs.renameSync(folder + file[1], `${destinationFolder}Delivery/${invoice.fornitore}_${file[1]}`)
break;
case 'Just-Eat Italy S.r.l':
fs.renameSync(folder + file[1], `${destinationFolder}Delivery/${invoice.fornitore}_${file[1]}`)
break;
default:
fs.renameSync(folder + file[1], `${destinationFolder}${invoice.fornitore}_${file[1]}`)
break;
}
break;
}
}
} catch (err) {
console.log(err)
}
}
//*************************************************** */
//TESTS FOR STABLE SUPPLIERS
//*************************************************** */
for (let fornitore of UFFICIO) {
if (invoice.fornitore === fornitore) {
invoice.puntoVendita = 'UFFICIO'
}
}
for (let fornitore of SAVONA) {
if (invoice.fornitore === fornitore) {
invoice.puntoVendita = 'SAVONA'
}
}
for (let fornitore of TICINESE) {
if (invoice.fornitore === fornitore) {
invoice.puntoVendita = 'TICINESE'
}
}
}
tempData.push(invoice)
}
return myData = tempData
}
//MAIN LOOP WHERE SOME CHECKS TAKE PLACE AND WHERE PDV PATH DECISION TAKES PLACE (number, string and pdf)
const loop = async (entries) => {
let buf
let file
let invData
let invDataArray = []
for (let invoices of entries) {
let rawInv = invoices[0]
file = invoices[1]
for (let array of rawInv) {
if (re.test(array[0])) {
error = file;
invData = await fetchData(array[1], file)
try {
if(invData.fornitore === "DELIVEROO ITALY S.r.l.") {
for (let codice of Delivero) {
if (array[1].FatturaElettronicaBody.DatiGenerali.DatiGeneraliDocumento.Causale["_text"].includes(codice[0])) {
buf = codice[1]
}
}
} else if(invData.fornitore === "Foodinho, SRL") {
for (let codice of Glovo) {
if (array[1].FatturaElettronicaBody.DatiBeniServizi.DettaglioLinee.Descrizione["_text"].includes(codice[0])) {
buf = codice[1]
}
}
} else if (array[1].FatturaElettronicaBody.Allegati === undefined) {
let test = JSON.stringify(array[1].FatturaElettronicaBody)
buf = 0;
} else if (invData.fornitore === "MARR Spa") { //For suppliers like MARR which have an array of attachments
if (array[1].FatturaElettronicaBody.DatiGenerali.DatiGeneraliDocumento.Causale === undefined) {
buf = "no pdv"
} else if (!Array.isArray(array[1].FatturaElettronicaBody.DatiGenerali.DatiGeneraliDocumento.Causale)) {
console.log('im here')
let causale = array[1].FatturaElettronicaBody.DatiGenerali.DatiGeneraliDocumento.Causale["_text"]
for (_pdv of PDV) {
if (causale.includes(_pdv)) {
buf = _pdv;
}
}
} else {
for (let innerArray of array[1].FatturaElettronicaBody.DatiGenerali.DatiGeneraliDocumento.Causale) {
for (_pdv of PDV) {
if (innerArray["_text"].includes(_pdv)) {
buf = _pdv;
}
}
}
}
} else if (array[1].FatturaElettronicaBody.Allegati.FormatoAttachment === undefined) { // usually means there are multiple attachments
attachments = array[1].FatturaElettronicaBody.Allegati
for (let attachment in attachments) {
if (attachment.FormatoAttachment === undefined) {
buf = 0;
} else {
if (attachment.FormatoAttachment['_text'] === "PDF") {
const raw = attachment.Attachment["_text"]
let bin = atob(raw)
buf = Buffer.from(bin, 'binary')
} else {
buf = 0;
}
}
}
buf = 0;
} else if (array[1].FatturaElettronicaBody.Allegati.FormatoAttachment['_text'] === "TXT") {
buf = 0;
} else {
const raw = array[1].FatturaElettronicaBody.Allegati.Attachment["_text"]
let bin = atob(raw)
buf = Buffer.from(bin, 'binary')
}
invDataArray.push([invData, buf])
} catch (e) {
console.log(e, file);
}
} else { }
}
}
return invDataArray
}
//MAIN FUNCTION
const mainTest = async () => {
let entries = await readingFile(fileArray);
let xmlPDV = await stringXML(fileArray)
let data = await loop(entries)
const pdf = await getText(data)
let PDV = await pdv(pdf);
let csvData = await pushData(data, PDV, xmlPDV);
if(writeToCSV) {
writeCSV(csvData)
}
}
//WRITE TO CSV FUNCTION
const writeCSV = async (data) => {
const fields = ['xml', 'dataScadenza', '', 'fornitore', 'numeroFattura', 'dataFattura', 'puntoVendita', 'importo'];
const opts = { fields };
let csv;
try {
csv = await parse(data, opts);
fs.writeFileSync(`./${writeFileName}.csv`, csv, err => { if (err) console.log(err) })
console.log(csv);
return
} catch (err) {
console.error(err.name);
}
}
mainTest();