-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
250 lines (195 loc) · 6.28 KB
/
app.js
File metadata and controls
250 lines (195 loc) · 6.28 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
const express = require("express");
const bodyParser = require("body-parser");
const redis = require("redis");
const ejs = require("ejs");
const { keys } = require("lodash");
const app = express();
var SalesArray = [];
var PurchasesArray = [];
var StocksArray = [];
app.set('view engine', 'ejs');
app.use(express.static("public"));
app.use(bodyParser.urlencoded({ extended: true }));
async function addKeyValue(key, obj) {
try {
const added = await client.HSET(key, obj)
// console.log(added)
const char1 = key.substr(0, 1);
if (char1 === 'P') {
PurchasesArray.push(await getKeyValue(key))
} else if (char1 === 'S') {
SalesArray.push(await getKeyValue(key))
} else if (char1 === 'I') {
StocksArray.push(await getKeyValue(key))
}
} catch (err) {
console.log(err)
}
}
async function getKeyValue(key) {
try {
const getValue = await client.HGETALL(key);
const char = key.substr(0, 1);
if (char === 'P') {
getValue.id = key.substr(9);
} else if (char === 'S') {
getValue.id = key.substr(5);
} else if (char === 'I') {
getValue.id = key.substr(10);
} else {
getValue.id = key;
}
return getValue;
// console.log(getValue)
} catch (err) {
console.log(err)
}
}
async function existsOrNot(key) {
try {
const exists = await client.EXISTS(key);
return exists;
} catch (err) {
console.log(err)
}
}
async function updateValue(key, field, value) {
try {
const newValue = await client.HSET(key, field, value);
console.log(newValue);
} catch (err) {
console.log(err)
}
}
async function getKeysOfSameType(pattern) {
try {
const Keys = await client.KEYS(`${pattern}*`);
return Keys;
} catch (err) {
console.log(err)
}
}
const client = redis.createClient();
client.on('error', (err) => console.log('Redis Client Error', err));
client.on('connect', function () {
console.log("Connected to redis server");
});
(async () => {
await client.connect();
let temp1 = await getKeysOfSameType('Inventory:');
console.log(temp1);
for (let index = 0; index < temp1.length; index++) {
StocksArray.push(await getKeyValue(temp1[index]));
}
console.log(StocksArray);
let temp2 = await getKeysOfSameType('Sale:');
console.log(temp2);
for (let index = 0; index < temp2.length; index++) {
StocksArray.push(await getKeyValue(temp2[index]));
}
let temp3 = await getKeysOfSameType('Purchase:');
console.log(temp3);
for (let index = 0; index < temp3.length; index++) {
StocksArray.push(await getKeyValue(temp3[index]));
}
})();
async function addLeftStockEntry(itemname, type, qty, rate) {
if (type === "Purchase") {
let temp = await existsOrNot("Inventory:" + itemname);
console.log(temp);
if (temp == 1) {
console.log("Updating Data to Inventory due to purchase");
updateValue("Inventory:" + itemname, "STOCK", parseInt(qty));
}
else {
let tempObj = {
STOCK: parseInt(qty),
SOLD_QUANTITY: parseInt('0'),
RATE: parseInt(rate)
};
console.log("Adding Data to Inventory");
addKeyValue("Inventory:" + itemname, tempObj);
}
}
else {
console.log("Updating Data to Inventory due to sales");
let temp = await getKeyValue("Inventory:" + itemname);
console.log(temp);
await updateValue("Inventory:" + itemname, "STOCK", parseInt(temp.STOCK) - parseInt(qty));
await updateValue("Inventory:" + itemname, "SOLD_QUANTITY", parseInt(temp.SOLD_QUANTITY) + parseInt(qty));
let temp2 = await getKeysOfSameType('Inventory:');
console.log(temp2);
StocksArray = [];
for (let index = 0; index < temp2.length; index++) {
StocksArray.push(await getKeyValue(temp2[index]));
}
}
}
function addSalesEntry(billNo, date, itemname, qty, rate) {
let tempObj = {
DATE: date,
ITEM_NAME: itemname,
QUANTITY: qty,
RATE: rate
};
console.log("Adding Data to Sales");
addKeyValue("Sale:" + billNo, tempObj);
}
function addPurchaseEntry(billNo, date, itemname, qty, rate) {
let tempObj = {
DATE: date,
ITEM_NAME: itemname,
QUANTITY: qty,
RATE: rate
};
console.log("Adding Data to Purchase");
addKeyValue("Purchase:" + billNo, tempObj);
}
function addBillEntry(billNo, date, type, name, itemname, qty, rate) {
let tempObj = {
DATE: date,
TYPE: type,
NAME: name,
ITEM_NAME: itemname,
QUANTITY: qty,
RATE: rate
};
console.log("Adding Data to Bill");
addKeyValue(billNo, tempObj);
if (type === "Purchase") {
console.log("xx xx xx qty ", qty)
addPurchaseEntry(billNo, date, itemname, qty, rate);
} else {
addSalesEntry(billNo, date, itemname, qty, rate);
}
addLeftStockEntry(itemname, type, qty, rate);
}
app.get("/", function (req, res) {
// let temp3=await getKeysOfSameType('Inventory:');
res.render("index");
})
app.get("/bill", function (req,res) {
res.render("home", { Inventory: StocksArray });
})
app.post("/bill", function (req, res) {
// console.log(req.body.Qty);
console.log(req.body.ItemName);
// console.log("New item Name ",req.body.NewItemName);
if (req.body.NewItemName === "")
addBillEntry(req.body.billNo, req.body.date, req.body.type, req.body.Name, req.body.ItemName, parseInt(req.body.Qty), parseInt(req.body.Rate));
else
addBillEntry(req.body.billNo, req.body.date, req.body.type, req.body.Name, req.body.NewItemName, parseInt(req.body.Qty), parseInt(req.body.Rate));
res.redirect("/");
})
app.get("/purchase", async (req, res) => {
res.render("page2", { Purchases: PurchasesArray });
})
app.get("/sales", async (req, res) => {
res.render("page1", { Sales: SalesArray });
})
app.get("/stock", async (req, res) => {
res.render("page3", { Stocks: StocksArray });
})
app.listen(3000, function () {
console.log("Server started running at port 3000.");
})