-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustomers.js
144 lines (131 loc) · 4.11 KB
/
customers.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
const util = require('util');
const default_hourly_wage = 16.00;
const default_minute_wage = default_hourly_wage / 60;
const default_cost_coeff = .5;
const default_boh_time = 1;
const default_foh_time = 1;
const fs = require('fs');
//const lo = require('lodash');
const catFilePath=process.argv[2];
const transFilePath=process.argv[3];
const csv=require('csvtojson');
const costs = {};
const prices = {};
// sortable list of percent and real dollar profits from the catalog
const margins = [];
//tally of the earned profits from transactions on the margins
const profit = {};
const counts = {};
const visits = {};
csv()
.fromFile(catFilePath)
.then((catalog)=>{
for(const item of catalog){
if(!item.Cost){
item.Cost = parseFloat(item.Price) * default_cost_coeff;
}
if(!item.BohPrepTime){
item.BohPrepTime = default_boh_time;
}
if(!item.FohPrepTime){
item.FohPrepTime = default_boh_time;
}
item.totalCost = parseFloat(item.Cost)
item.totalCost += (parseFloat(item.BohPrepTime) * default_minute_wage);
item.totalCost += (parseFloat(item.FohPrepTime) * default_minute_wage);
item.name = util.format('%s (%s)', item['Item Name'], item['Variation Name'])
console.log('%s - Price: %d Cost: %d', item.name, item.Price, item.totalCost);
costs[item.name] = item.totalCost;
const price = parseFloat(item.Price);
prices[item.name] = price;
let name = item.name;
let profit = price - item.totalCost;
if (profit < 0){
continue;
}
let percent = profit / price;
margins.push({name, profit, percent});
profit[name] = 0;
}
console.log('Catalog Loaded...');
return csv().fromFile(transFilePath);
})
.then(async listing =>{
for(const trans of listing){
let customer = trans['Customer Name'];
if(!customer){
continue;
}
if(!visits[customer]){
visits[customer] = 1;
}
else{
visits[customer] += 1;
}
let items = await csv({noheader:true, output: "csv"}).fromString(trans['Description']);
let partial = undefined;
let multiple = 1;
if(!items[0]){
continue;
}
//console.log(trans['Description']);
for(const item of items[0]){
if(partial){
// Unfortunately the square CSV format includes parens with commas if the user enters values like this
partial = partial + ", " + item;
}
else{
// Sometimes the sq system pads out some spaces???
let itemName = item.replace(/ /g, ' ');
if(itemName.indexOf(' x ') > 0){
[multiple, itemName] = itemName.split(' x ');
}
partial = itemName;
multiple = parseInt(multiple);
}
if(costs[partial] || partial === "Custom Amount"){
if(partial === "Custom Amount"){
//console.log("Custom amount");
partial = undefined;
continue;
}
// console.log("found %s: %d (%d), ... %i, %d", partial, costs[partial], prices[partial], multiple, gross );
let itemCost = costs[partial];
let itemPrice = prices[partial];
let itemProfit = (itemPrice - itemCost) * multiple;
if(!profit[customer]){
profit[customer] = itemProfit;
counts[customer] = multiple;
}
else{
profit[customer] += itemProfit;
counts[customer] += multiple;
}
// reset
partial = undefined;
multiple = 1;
}
else{
// console.log("NF " + partial)
}
}
}
//console.log("Total gross sales: " + total);
//console.log("Total Costs: " + totalCost);
}).then(() => {
const customers = [];
for(const name in profit){
let net = profit[name];
let count = counts[name];
let visitCount = visits[name];
let perVisit = net / visitCount;
customers.push({name, net, count, visitCount, perVisit});
}
return customers;
})
.then((customers) => {
customers.sort((a,b) => (a.net < b.net) ? 1 : ((b.net < a.net) ? -1 : 0));
for(const customer of customers){
console.log('%s, %d, %i, %i, %d', customer.name, customer.net.toFixed(2), customer.count, customer.visitCount, customer.perVisit.toFixed(2));
}
});