-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.js
More file actions
303 lines (287 loc) · 10.7 KB
/
Copy pathcontroller.js
File metadata and controls
303 lines (287 loc) · 10.7 KB
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
const path = require("path");
// const fs = require("fs");
const xlsx = require("xlsx");
// const _ = require("lodash");
class FileProcessor {
constructor() {
this.globalErpFormat = path.join(__dirname, 'globalErpFormat.xlsx');
}
static getWs(fileName) {
let workBook = xlsx.readFile(fileName, { cellDates: true });
// console.log("workBook", workBook);
let firstSheetName = workBook.SheetNames[0];
let workSheet = workBook.Sheets[firstSheetName];
return workSheet;
}
static getAllWs(fileName) {
let workBook = xlsx.readFile(fileName, { cellDates: true });
let workSheets = [];
workBook.SheetNames.map((wsName) => {
workSheets.push(workBook.Sheets[wsName]);
});
return workSheets;
}
static joinWsJson(wsArray) {
let result = [];
wsArray.map((ws) => {
// result = result.concat(this.getJson(ws));
//Checking is the item is in the result already, if so, update it's price wit the highest between the 2
//otherwise just push the object to the results array
this.getJson(ws).forEach((rec) => {
const exists = result.find((x) => rec.__EMPTY_2 === x.__EMPTY_2);
rec;
if (exists) {
const index = result.findIndex(
(obj) => obj.__EMPTY_2 === exists.__EMPTY_2
);
result[index].price =
exists.price > result[index].price
? exists.price
: result[index].price;
} else {
result.push(rec);
}
});
});
return result;
}
static getJson(ws) {
// console.log("Reaching..");
let wsJson = xlsx.utils.sheet_to_json(ws);
return wsJson;
}
static bosch(supplierFilePath, erpFilePath) {
const supplierWs = FileProcessor.getAllWs(supplierFilePath);
let supplier = this.joinWsJson(supplierWs);
let erp = this.getJson(this.getWs(erpFilePath));
console.log("Reaching");
erp = erp.map((rec, index) => {
if (index == 0) return {};
let descriptionArr = rec.__EMPTY.split(" ") || [];
// // console.log("Supplier",supplier )
// console.log("descriptionArr ",descriptionArr )
let matchedSupplier = supplier.find((sup) => {
let supl = descriptionArr.find((word) => {
// console.log("SUp", sup);
// console.log("Code", sup["supplier codeς"]);
// console.log("Word",word);
return sup["supplier code"] === word;
});
// console.log("SUPL",supl)
return supl ? true : false;
});
// console.log("matchedSupplier",matchedSupplier);
// console.log("Rec",rec)
if (matchedSupplier) {
// console.log("matched", matchedSupplier);
let price = matchedSupplier.price;
let supplierCode = matchedSupplier["supplier code"];
return {
barcode: rec["Υπόλοιπα ανά ΑΧ"],
description: rec.__EMPTY,
supplier: "BOSCH",
price: price,
"supplier code": supplierCode,
"ean code":
matchedSupplier["EAN Code"] ||
matchedSupplier["EAN Code 3165140…"] ||
matchedSupplier["ean code"],
};
} else {
let code = /[\w|\d]{10}|(\w\d{7})\w+|\d{3,}\W\d+/.exec(rec.__EMPTY);
// console.log("rec", rec);
return {
barcode: rec["Υπόλοιπα ανά ΑΧ"],
description: rec.__EMPTY,
supplier: "BOSCH",
price: rec.__EMPTY_5,
"supplier code": code ? code[0] : "",
"ean code": rec["ean code"] || rec["EAN-14"],
};
// console.log(rec)
}
});
console.log("Updating complete");
//creating a new ws from the result json
const resultsWs = xlsx.utils.json_to_sheet(erp);
// resultsWs["!margins"] = this.getWs("erp.xlsx")["!margins"];
// resultsWs["!formatRows"] = false;
//creating a new wb
const resultsWb = xlsx.utils.book_new();
// adding ws to wb
xlsx.utils.book_append_sheet(resultsWb, resultsWs, "result");
//new filenme
const priceFileName =
"priceLists/bosch/" +
`${new Date().toLocaleDateString().split("/").join("-")}-${Date.now()}` +
".xlsx";
//writing the wb to file
xlsx.writeFile(resultsWb, priceFileName);
console.log("writing file complete");
return priceFileName;
}
static generateFile(supplierFilePath, erpFilePath, supplierName) {
const supplier = this.getJson(this.getWs(supplierFilePath));
let erp = this.getJson(this.getWs(erpFilePath));
// let supplierCodeRegex = /[\w|\d]{10}|(\w\d{7})\w+|\d{3,}\W\d+|[\w+|\d+|\\,|\\.|\w+|\d+]{6,}/
erp = erp.map((rec, index) => {
if (index == 0) return {};
// let supplierCodeFromDescription = supplierCodeRegex.exec(rec.__EMPTY_1)
let matchedSupplier = supplier.find((sup) => {
const matched = rec.__EMPTY_3 == sup['supplier code'] || rec['supplier code'] == sup['supplier code']
return matched
});
// let defaultSupplierCode = /[\w|\d]{10}|(\w\d{7})\w+|\d{3,}\W\d+/.exec(rec.__EMPTY_1);
const defaultRecord = {
'erp code': rec['erp code'] || rec.__EMPTY,
description: rec.description || rec.__EMPTY_1,
supplier: rec.supplier || supplierName,
"supplier code": rec['supplier code'] || rec.__EMPTY_3,
"ean code": rec['ean code'] || rec.__EMPTY_4,
price: rec.price || rec.__EMPTY_5,
};
// console.log("Rec",rec)
if (matchedSupplier) {
// console.log("matched", matchedSupplier);
return {
...defaultRecord,
price: matchedSupplier.price,
"supplier code": matchedSupplier["supplier code"],
"ean code":
matchedSupplier["ean code"],
};
} else {
return defaultRecord
}
});
console.log("Updating complete");
//creating a new ws from the result json
const resultsWs = xlsx.utils.json_to_sheet(erp);
const resultsWb = xlsx.utils.book_new();
// adding ws to wb
xlsx.utils.book_append_sheet(resultsWb, resultsWs, "result");
//new filenme
const priceFileName =
`priceLists/${supplierName}/` +
`${new Date().toLocaleDateString().split("/").join("-")}-${Date.now()}` +
".xlsx";
//writing the wb to file
xlsx.writeFile(resultsWb, priceFileName);
console.log("writing file complete");
return priceFileName;
}
static dremel(supplierFilePath, erpFilePath) {
// console.log(supplierFilePath, erpFilePath);
let erp = this.getJson(this.getWs(erpFilePath));
console.log("Reaching...");
const supplier = this.getJson(this.getWs(supplierFilePath));
console.log("Reading json complete...");
erp = erp.map((rec, index) => {
if (index == 0) return {};
let descriptionArr = rec.__EMPTY.split(" ") || [];
// // console.log("Supplier",supplier )
// console.log("descriptionArr ",descriptionArr )
let matchedSupplier = supplier.find((sup) => {
let supl = descriptionArr.find((word) => {
// console.log("SUp", sup);
// console.log("Code", sup["supplier codeς"]);
// console.log("Word",word);
return sup["supplier codeς"] === word;
});
// console.log("SUPL",supl)
return supl ? true : false;
});
// console.log("matchedSupplier",matchedSupplier);
// console.log("Rec",rec)
if (matchedSupplier) {
// console.log("matched",matchedSupplier)
let price =
matchedSupplier["retail price"] || matchedSupplier["__EMPTY_3"];
let supplierCode =
matchedSupplier["supplier codeς"] || matchedSupplier["__EMPTY"];
return {
barcode: rec["Υπόλοιπα ανά ΑΧ"],
description: rec.__EMPTY,
price: price,
"supplier code": supplierCode,
"ean code": matchedSupplier["EAN Code"],
};
} else {
let supplierCodeRegex = /[\w|\d]{10}|(\w\d{7})\w+|\d{3,}\W\d+|[\w+|\d+|\\,|\\.|\w+|\d+]{6,}/
return {
barcode: rec["Υπόλοιπα ανά ΑΧ"],
description: rec.__EMPTY,
price: rec.__EMPTY_1,
"supplier code": rec.__EMPTY ? rec.__EMPTY.split(" ").find(w => supplierCodeRegex.test(w)) : "",
"ean code": rec["ean code"],
};
// console.log(rec)
}
});
console.log("Updating complete");
//creating a new ws from the result json
const resultsWs = xlsx.utils.json_to_sheet(erp);
// resultsWs["!margins"] = this.getWs("erp.xlsx")["!margins"];
// resultsWs["!formatRows"] = false;
//creating a new wb
const resultsWb = xlsx.utils.book_new();
// adding ws to wb
xlsx.utils.book_append_sheet(resultsWb, resultsWs, "result");
//new filenme
const priceFileName =
"priceLists/dremel/" +
`${new Date().toLocaleDateString().split("/").join("-")}-${Date.now()}` +
".xlsx";
//writing the wb to file
xlsx.writeFile(resultsWb, priceFileName);
console.log("writing file complete");
return priceFileName;
}
static facom(supplierFilePath, erpFilePath) {
let erp = this.getJson(this.getWs(erpFilePath));
const supplier = this.getJson(this.getWs(supplierFilePath));
erp = erp.map((rec, index) => {
if (index == 0) return {};
let matchedSupplier = supplier.find((sup) => {
let erpSupCode = rec["supplier code"];
return sup.__EMPTY_2 === erpSupCode;
// console.log('sup["ean code and barcode"]', sup["ean code and barcode"]);
// console.log("rec.__EMPTY_4", rec.__EMPTY_4);
// console.log(
// "rec.__EMPTY_4",
// rec.__EMPTY_4 === sup["ean code and barcode"]
// );
// return rec.__EMPTY_4 === sup["ean code and barcode"];
});
if (matchedSupplier) {
return {
"erp code": rec["erp code"],
description: rec["erp description"],
price: matchedSupplier.__EMPTY_3,
"supplier code": matchedSupplier.__EMPTY_2,
barcode: matchedSupplier.__EMPTY_1,
intrastat: matchedSupplier.__EMPTY_5,
};
}
return {};
});
erp = erp.filter((obj) => Object.keys(obj).length > 0);
// console.log("Updating complete", erp);
//creating a new ws from the result json
const resultsWs = xlsx.utils.json_to_sheet(erp);
console.log("reaching ");
const resultsWb = xlsx.utils.book_new();
// adding ws to wb
xlsx.utils.book_append_sheet(resultsWb, resultsWs, "result");
//new filenme
const priceFileName =
"priceLists/facom/" +
`${new Date().toLocaleDateString().split("/").join("-")}-${Date.now()}` +
".xlsx";
//writing the wb to file
xlsx.writeFile(resultsWb, priceFileName);
console.log("writing file complete");
return priceFileName;
}
}
module.exports = FileProcessor;