-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
291 lines (248 loc) · 7.29 KB
/
index.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
const express = require("express");
const path = require("path");
const ejsMate = require("ejs-mate");
const session = require("express-session");
const mongoose = require("mongoose");
const passport = require("passport");
const LocalStrategy = require("passport-local");
const Window = require("window");
const flash = require("connect-flash");
const window = new Window();
const Bill = require("./models/bill");
const User = require("./models/user");
const Item = require("./models/item");
const Sales = require("./models/sales");
const { isLoggedIn } = require("./middleware");
mongoose.connect("mongodb://127.0.0.1:27017/supermarket");
const db = mongoose.connection;
db.on("error", console.error.bind(console, "connection error:"));
db.once("open", () => {
console.log("Database connected");
});
const app = express();
app.engine("ejs", ejsMate);
app.set("view engine", "ejs");
app.set("views", path.join(__dirname, "views"));
app.use(express.urlencoded({ extended: true }));
const sessionConfig = {
secret: "supermarketapp",
resave: false,
saveUninitialized: true,
cookie: {
httpOnly: true,
expires: Date.now() + 1000 * 60 * 60 * 24 * 2, //expires after two days
maxAge: 1000 * 60 * 60 * 24 * 2,
},
};
app.use(session(sessionConfig));
app.use(flash());
app.use(passport.initialize());
app.use(passport.session());
passport.use(new LocalStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
app.use((req, res, next) => {
res.locals.currentUser = req.user;
res.locals.success = req.flash("success");
res.locals.error = req.flash("error");
next();
});
app.get("/", (req, res) => {
res.render("login");
});
// authentications
app.get("/login", (req, res) => {
res.render("login");
});
app.post(
"/login",
passport.authenticate("local", {
failureFlash: true,
failureRedirect: "/login",
}),
async (req, res) => {
req.flash("success", "Welcome back!");
res.redirect("/welcome");
}
);
app.get("/logout",isLoggedIn, async (req, res) => {
req.logout(req.user, (err) => {
if (err) return next(err);
req.flash("success", "Goodbye!");
res.redirect("/login");
});
});
app.get("/register", (req, res) => {
res.render("register");
});
app.post("/register", async (req, res) => {
try {
const { name, username, user_type, password } = req.body;
joining_date = Date.now();
const user = new User({ name, user_type, joining_date, username });
const newuser = await User.register(user, password);
req.login(newuser, (err) => {
if (err) return next(err);
req.flash("success", "Welcome new user!");
res.redirect("/welcome");
});
} catch (e) {
req.flash("error", e.message);
res.redirect("register");
}
});
app.post("/stat_ind",async(req,res)=>{
var x= req.body;
})
//common
app.get('/profile',isLoggedIn,async(req,res)=>{
res.render('profile')
})
app.get('/about',isLoggedIn,async(req,res)=>{
res.render('about')
})
app.get('/welcome',isLoggedIn,async(req,res)=>{
res.render('welcome')
})
//manager
app.get('/stat',isLoggedIn,async (req,res)=>{
if(res.locals.currentUser.user_type!='Manager'){
req.flash('error', 'Only Manager is authorized for this action');
res.redirect('/welcome');
}
var filter=0;
try{
const allsalesforpie = await Sales.aggregate([
{
$match:{}
},
{
$group: {
_id: "$item_name",
total: {
$sum: {$multiply:["$quantity","$unit_price"]}
},
}
},
{
$addFields: {
"item_name":"$_id"
}
}
]);
const allsales=await Sales.find({})
const allDetails = await Item.find({});
res.render('sales_stat',{ allsales, allDetails,allsalesforpie, filter });
} catch (err) {
console.error(err);
res.status(500).send("Internal Server Error");
}
});
app.post('/stat',isLoggedIn,async (req,res)=>{
var filter=req.body.filter
try{
const allsalesforpie = await Sales.aggregate([
{
$match:{}
},
{
$group: {
_id: "$item_name",
total: {
$sum: {$multiply:["$quantity","$unit_price"]}
},
}
},
{
$addFields: {
"item_name":"$_id"
}
}
]);
const allsales=await Sales.find({})
const allDetails = await Item.find({});
res.render('sales_stat',{ allsales, allDetails,allsalesforpie, filter });
} catch (err) {
console.error(err);
res.status(500).send("Internal Server Error");
}
});
app.get('/bill',isLoggedIn,async(req,res)=>{
if(res.locals.currentUser.user_type!='Clerk'){
req.flash('error', 'Only Sales Clerk is authorized for this action');
res.redirect('/welcome');
}
const items = await Item.find({})
res.render('bill',{items})
})
app.post('/bill',isLoggedIn,async(req,res)=>{
if(res.locals.currentUser.user_type!='Clerk'){
req.flash('error', 'Only Sales Clerk is authorized for this action');
res.redirect('/welcome');
}
var bill= req.body
console.log(bill)
const date=new Date()
bill.date=date
var bill_items=[]
for (let i = 0; i < bill.code.length; i++) {
var q = await Item.find({item_code:bill.code[i]})
const x = await Item.findOneAndUpdate({item_code:bill.code[i]},{quantity:q[0].quantity-bill.qty[i]})
bill_items.push({
item_code:bill.code[i],
name: q[0].item_name,
quantity:bill.qty[i],
unit_price:bill.price[i],
})
const sell = new Sales({
item_code:bill.code[i],
item_name: q[0].item_name,
unit_price:bill.price[i],
quantity:bill.qty[i],
date:date,
})
await sell.save()
console.log(q)
}
const new_bill = new Bill({
items:bill_items,
total_cost:bill.sub_total,
date:bill.date
})
await new_bill.save();
bill.id = await Bill.countDocuments();
bill.bill_items = bill_items;
res.render("print_bill", { bill });
});
app.get('/print',isLoggedIn,(req,res)=>{
if(res.locals.currentUser.user_type!='Clerk'){
req.flash('error', 'Only Sales ClerTOOLSk is authorized for this action');
res.redirect('/welcome');
}
res.render('print_bill')
})
//inventory portion
app.get('/inventory',isLoggedIn,async(req,res)=>{
if(res.locals.currentUser.user_type=='Clerk'){
req.flash('error', 'You are not authorized for this action');
res.redirect('/welcome');
}
const allDetails = await Item.find({});
res.render('inventory', { details: allDetails })
})
app.post('/add',isLoggedIn,async(req,res)=>{
newitem=req.body;
const item = new Item({item_name:newitem.i1,item_code:await Item.countDocuments()+1,quantity:newitem.i4,unit_price:newitem.i3,description:newitem.i2})
await item.save();
const allDetails = await Item.find({});
res.render('inventory', { details: allDetails})
})
app.post('/updateItems',isLoggedIn,async(req,res)=>{
newitem=req.body;
const x = await Item.findOneAndUpdate({item_code: parseInt(newitem.i1)},{unit_price:newitem.i3 , quantity:newitem.i4})
const allDetails = await Item.find({});
res.render('inventory', { details: allDetails})
})
app.listen(3000,()=>{
console.log("Listening on port 3000!!..")
})