Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*model*/ {
- function Item (json) {
- this.name = json.name;
- }
- function Product (json) {
- this.sku = json.sku;
- this.price = json.price;
- //if (!(json.sku && json.price)) throw JSON.stringify(this, null, 2);
- }
- function Order (json) {
- this.name = json.name;
- this.tax = parseFloat(json.tax);
- this.country = json.country;
- this.city = json.city;
- this.address = json.address;
- this.exchangeRate = parseFloat(json.exchangeRate);
- this.invoiceId = parseInt(json.invoiceId);
- this.invoiceYear = parseInt(json.invoiceYear);
- this.shippingDate = json.shippingDate;
- this.currency = json.currency;
- this.date = json.date
- this.id = parseInt(json.id);
- this.sku = json.sku ? json.sku.toString() : json.sku;
- this.quantity = parseInt(json.quantity);
- this.status = json.status;
- this.total = parseFloat(json.total);
- this.paymentMethod = json.paymentMethod;
- this.time = (new Date(json.date)).getTime();
- this.storno = json.storno;
- }
- function Stock (json) {
- this.date = json.date;
- this.sku = json.sku ? json.sku.toString() : json.sku;
- this.quantity = parseInt(json.quantity);
- this.price = parseFloat(json.price);
- this.time = (new Date(json.date)).getTime();
- if (!(this.date && this.sku && this.quantity && this.price && this.time)) process.exit(Dump(this));
- }
- }
- /*validate*/ {
- function validateOrder (Order) {
- var e = [];
- if (!Order.paymentMethod) e.push('paymentMethod');
- if (!Order.shippingDate && !config.refundedStatus.includes(Order.status)) e.push('shippingDate');
- if (!Order.total) e.push('total');
- if (!Order.name) e.push('name');
- if (!Order.tax && Order.tax !== 0) e.push('tax');
- if (!Order.country) e.push('country');
- if (!Order.city) e.push('city');
- if (!Order.address) e.push('address');
- if (!Order.exchangeRate && (Order.time > config.exchangeRateStartTime || config.baseCurrency != Order.currency)) {
- if (config.refundedStatus.includes(Order.status)) {
- var stornoDigits = parseInt(Order.storno.replace('-', ''));
- var startStronoDigits = parseInt(config.exchangeRateStartStorno.replace('-', ''));
- if (stornoDigits > startStronoDigits) e.push('exchangeRate');
- }
- }
- if (!Order.invoiceId) e.push('invoiceId');
- if (!Order.invoiceYear) e.push('invoiceYear');
- if (!Order.currency) e.push('currency');
- if (!Order.date) e.push('date');
- if (!Order.id) e.push('id');
- if (!Order.sku) e.push('sku');
- if (!Order.quantity) e.push('quantity');
- if (!Order.time) e.push('time');
- if (!Order.status) e.push('status');
- if (!Order.storno && config.refundedStatus.includes(Order.status)) e.push('storno');
- if (e[0]) process.exit(Dump([Order, e]));
- }
- }
- /*functions*/ {
- function readXlsx(dirname, onFileContent) {
- var filenames = fs.readdirSync(dirname);
- var jsons = [];
- for (i in filenames) {
- var filename = filenames[i];
- if (!filename.match(/\.xlsx$/)) return;
- var book = XLSX.readFile(dirname + filename);
- var sheets = book.SheetNames;
- var json = XLSX.utils.sheet_to_json(book.Sheets[sheets[0]]);
- jsons.push(json);
- }
- return jsons;
- }
- function InventoryByTime(time) {
- var Inventory = {};
- Inventory["#date_time#"] = (new Date(time)).toLocaleString();
- Inventory["#missing#"] = [];
- for (var i = 0; i < Stocks.length; i++) {
- var Stock = Stocks[i];
- if (Stock.time > time) break;
- if (!Inventory[Stock.sku]) Inventory[Stock.sku] = [];
- Inventory[Stock.sku].push(iterationCopy(Stock));
- }
- //console.log(Inventory);
- orders: for (i = 0; i < Orders.length; i++) {
- var Order = iterationCopy(Orders[i]);
- if (!config.completedStatus.includes(Order.status)) continue;
- if (Order.time > time) break;
- if (!Inventory[Order.sku] || Inventory[Order.sku][0].time > Order.time) {
- Inventory['#missing#'].push(iterationCopy(Order));
- continue;
- }
- var j;
- for (j = 0; j < Inventory[Order.sku].length; j++) {
- var Stock = Inventory[Order.sku][j];
- if (Stock.quantity > 0 && Stock.time < Order.time) break;
- var nextStock = Inventory[Order.sku][j+1];
- if (!nextStock) break;
- if (Stock.time < Order.time && (!nextStock || nextStock.time > Order.time)) break;
- }
- for (var k = 0; k < Order.quantity; k++) {
- var Stock = Inventory[Order.sku][j];
- var nextStock = Inventory[Order.sku][j+1];
- if (Stock.quantity == 0 && nextStock && nextStock.time < Order.time) {
- j++;
- k--;
- continue;
- }
- Stock.quantity--;
- }
- }
- return Inventory;
- }
- function areInvoicesContinous () {
- var _Orders = [];
- for (i in Orders) {
- var Order = iterationCopy(Orders[i]);
- _Orders.push(Order);
- }
- _Orders = _Orders.sort(function (a, b) {
- return a.invoiceId - b.invoiceId;
- });
- var years = {};
- for (var i = 0; i < _Orders.length -1; i++) {
- var Order = _Orders[i];
- if (years[Order.invoiceYear]) years[Order.invoiceYear] = [];
- years[Order.invoiceYear].push(Order.invoiceId);
- }
- var res = [];
- for (i in years) {
- var NextOrder = _Orders[i+1];
- if (NextOrder.invoiceId == Order.invoiceId || NextOrder.invoiceId == Order.invoiceId + 1) continue;
- if (NextOrder.invoiceYear != Order.invoiceYear) continue;
- res.push('missing invoiceId ' + (Order.invoiceId + 1) + ' invoiceYear ' + Order.invoiceYear);
- }
- return res;
- }
- function allCancelledHaveStorno() {
- var cancelled = [];
- var storno = [];
- for (i in Orders) {
- var Order = Orders[i];
- if (Order.storno && config.refundedStatus.includes(Order.status)) storno.push(Order.storno);
- if (Order.status == config.cancelledStatus) cancelled.push([Order.invoiceYear, Order.invoiceId].join('-'));
- }
- var res = [];
- for (i in cancelled) {
- if (!storno.includes(cancelled[i])) res.push('missing cancelled ' + cancelled[i]);
- }
- for (i in storno) {
- if (!cancelled.includes(storno[i])) res.push('missing storno ' + storno[i]);
- }
- return res;
- }
- function getDateTime() {
- return new Date().toLocaleString();
- }
- function Dump (dump, format='json') {
- if (format == 'json') {
- dump = dump || {Stocks: Stocks, Orders: Orders, Products: Products};
- var folder = dir + '/dump/';
- var data = JSON.stringify(dump, null, 4);
- var time = new Date().getTime();
- var file = folder + time + '.json';
- fs.writeFileSync(file, data);
- console.log(time);
- return time;
- } else if (format == 'xlsx') {
- }
- /*var wb = XLSX.utils.book_new();
- var ws = XLSX.utils.json_to_sheet(dump);
- XLSX.utils.book_append_sheet(wb, ws);
- XLSX.writeFile(wb, folder + new Date().getTime() + '.xlsx');*/
- }
- function findNegativeStocks () {
- var negative = {};
- var times = [];
- for (i in Orders) {
- var Order = iterationCopy(Orders[i]);
- var time = [new Date(Order.time).toLocaleString(), Order.sku].join(' ');
- if (times.includes(time)) continue;
- var I = InventoryByTime(Order.time);
- I: for (j in I) {
- for (k in I[j]) {
- if (I[j][k].quantity < 0 && I[j][k].sku == Order.sku) {
- times.push(time);
- negative[time] = {Stock: I[j][k], Order: Order};
- break I;
- }
- }
- }
- }
- return negative;
- }
- function invoiceReportByTimeInterval (time1=0, time2=Infinity) {
- var ids = [];
- var line = {
- name: null,
- country: null,
- city: null,
- address: null,
- invoiceYear: null,
- invoiceId: null,
- date: null,
- shippingDate: null,
- exchangeRate: null,
- tax: null,
- total: null,
- calcTaxEur: null,
- calcTotalEur: null,
- storno: null,
- paymentMethod: null,
- currency: null
- };
- var report = [];
- for (i in Orders) {
- var Order = iterationCopy(Orders[i]);
- if (ids.includes(Order.id)) continue;
- ids.push(Order.id);
- if (Order.time < time1 || Order.time > time2) continue;
- var r = {};
- r.id = Order.id;
- r.name = Order.name;
- r.country = Order.country;
- r.city = Order.city;
- r.address = Order.address;
- r.invoiceYear = Order.invoiceYear;
- r.invoiceId = Order.invoiceId;
- r.date = Order.date;
- r.shippingDate = Order.shippingDate;
- r.exchangeRate = Order.exchangeRate;
- r.tax = Order.tax;
- r.total = Order.total;
- r.calcTaxEur = r.tax / r.exchangeRate;
- r.calcTotalEur = r.total / r.exchangeRate;
- r.storno = Order.storno;
- r.paymentMethod = Order.paymentMethod;
- r.currency = Order.currency;
- report.push(r);
- }
- return report;
- }
- }
- /*utils*/ {
- function isObject(obj) {
- var type = typeof obj;
- return type === 'function' || type === 'object' && !!obj;
- };
- function iterationCopy(src) {
- let target = {};
- for (let prop in src) {
- if (src.hasOwnProperty(prop)) {
- // if the value is a nested object, recursively copy all it's properties
- if (isObject(src[prop])) {
- target[prop] = iterationCopy(src[prop]);
- } else {
- target[prop] = src[prop];
- }
- }
- }
- return target;
- }
- }
- /*view*/ {
- fs = require('fs');
- //path = require('path');
- XLSX = require('xlsx');
- dir = __dirname;
- args = process.argv.slice(2);
- config = require(dir + '/config.json');
- mode = args[0];
- }
- /*controller*/ {
- /*init*/ {
- Products = [];
- var jsons = readXlsx(dir + "/products/");
- for (i in jsons) {
- for (j in jsons[i]) {
- var json = jsons[i][j];
- var P = new Product(json);
- Products.push(P);
- }
- }
- Orders = [];
- var jsons = readXlsx(dir + "/orders/");
- var filter = config.orderStatusFilter[0] ? config.orderStatusFilter : false;
- for (i in jsons) {
- for (j in jsons[i]) {
- var json = jsons[i][j];
- if (filter && !filter.includes(json.status)) continue;
- var O = new Order(json);
- validateOrder(O);
- //group by sku
- for (j in config.groupSkuByRegexp) {
- var search = config.groupSkuByRegexp[j];
- var rex = new RegExp(search);
- if (O.sku.toString().match(rex)) O.sku = search;
- }
- Orders.push(O);
- }
- }
- Orders = Orders.sort(function (a, b) {
- return a.time - b.time;
- });
- Stocks = [];
- var jsons = readXlsx(dir + "/stocks/");
- for (i in jsons) {
- for (j in jsons[i]) {
- var json = jsons[i][j];
- var S = new Stock(json);
- for (j in config.groupSkuByRegexp) {
- var search = config.groupSkuByRegexp[j];
- var rex = new RegExp(search);
- if (S.sku.toString().match(rex)) S.sku = search;
- }
- Stocks.push(S);
- }
- }
- Stocks = Stocks.sort(function (a, b) {
- return a.time - b.time;
- });
- }
- if (mode == 6) {
- time = new Date(args[1]).getTime();
- var I = InventoryByTime(time);
- process.exit(Dump(I));
- } else if (mode == 1) {
- var res = areInvoicesContinous();
- process.exit(Dump(res));
- } else if (mode == 2) {
- process.exit(Dump());
- } else if (mode == 3) {
- var ne = findNegativeStocks();
- process.exit(Dump(ne));
- } else if (mode == 4) {
- var res = allCancelledHaveStorno();
- process.exit(Dump(res));
- } else if (mode == 5) {
- var time1 = new Date(args[1]).getTime() || 0;
- var time2 = new Date(args[2]).getTime() || Infinity;
- var r = invoiceReportByTimeInterval(time1, time2);
- process.exit(Dump(r));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement