-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
412 lines (351 loc) · 12.8 KB
/
app.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
const express = require('express');
const app = express();
let userInfo;
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
let dolar = 0;
const consultaDolar = require('consulta-dolar-venezuela');
consultaDolar.$monitor().then($ => { dolar = parseFloat($['$bcv'].slice(4, 9)); })
const dotenv = require('dotenv');
dotenv.config({ path: './env/.env' });
app.use('/resources', express.static('public'));
app.use('/resources', express.static(__dirname + '/public'));
app.set('view engine', 'ejs');
const session = require('express-session');
app.use(session({
secret: 'secret',
resave: true,
saveUninitialized: true
}))
const connection = require('./database/dbConnection');
app.get('/', (req, res) => {
res.render('login');
})
app.get('/registrar', (req, res) => {
res.render('registrar');
})
app.post('/register', (req, res) => {
const name = req.body.name
const lastname = req.body.lastname
const identify = req.body.identify
const phonenumber = req.body.phonenumber
const email = req.body.email
const pass = req.body.pass
connection.query(`INSERT INTO login (nombre, apellido, cedula, telefono, correo, contrasena) VALUES ('${name}','${lastname}','${identify}','${phonenumber}','${email}','${pass}')`,
(err,results) => {
res.render('login');
if(err){ console.log(err);}
});
})
app.get('/inicio', (req, res) => {
res.render('inicio');
})
app.get('/recuperar', (req, res) => {
res.render('recuperar');
})
app.get('/mercado', (req, res) => {
connection.query(`SELECT * FROM cuentas`, (err, results) => {
res.render('mercado', {
metodosPago: results,
});
})
})
app.get('/perfil', (req, res) => {
res.render('perfil', {
perfil: userInfo,
})
})
app.post('/updateUser', (req, res) => {
const id = req.body.id;
const name = req.body.name;
const lastname = req.body.lastname;
const identity = req.body.identity;
const phone = req.body.phone;
const email = req.body.email;
const password = req.body.password;
connection.query(`UPDATE login SET nombre='${name}',apellido='${lastname}',cedula='${identity}',telefono='${phone}',correo='${email}',contrasena='${password}' WHERE ID='${id}'`,
(err, resuls) => {
if (err) { console.log(err); }
else {
res.render('login');
}
})
})
app.post('/editCount', (req, res) => {
const idCount = req.body.idCount;
const banco = req.body.banco;
const cedula = req.body.cedula;
const numeroCuenta = req.body.numeroCuenta;
const telefono = req.body.telefono;
const correo = req.body.correo;
connection.query(`UPDATE cuentas SET cedula='${cedula}',telefono='${telefono}',correo='${correo}',numeroCuenta='${numeroCuenta}',banco='${banco}' WHERE ID = ${idCount}`,
(err, resuls) => {
res.redirect('/mercado');
if(err){console.log(err);};
});
})
app.post('/payClient', (req, res) => {
const cliente = req.body.cliente;
const cashBs = req.body.cashBs == '' ? 0 : (parseFloat(req.body.cashBs, 10)).toFixed(2);
const cashUSD = req.body.cashUSD == '' ? 0 : (parseFloat(req.body.cashUSD, 10)).toFixed(2);
const transBs = req.body.transBs == '' ? 0 : (parseFloat(req.body.transBs, 10)).toFixed(2);
const phonePay = req.body.phonePay == '' ? 0 : (parseFloat(req.body.phonePay, 10)).toFixed(2);
const transUSD = req.body.transUSD == '' ? 0 : (parseFloat(req.body.transUSD, 10)).toFixed(2);
const debitCard = req.body.debitCard == '' ? 0 : (parseFloat(req.body.debitCard, 10)).toFixed(2);
const creditCard = req.body.creditCard == '' ? 0 : (parseFloat(req.body.creditCard, 10)).toFixed(2);
const pay = (
parseFloat(cashUSD) +
parseFloat(transUSD) +
((
parseFloat(cashBs) +
parseFloat(transBs) +
parseFloat(phonePay) +
parseFloat(debitCard) +
parseFloat(creditCard)
) / dolar)).toFixed(2);
connection.query(`SELECT * FROM clientes WHERE ID=${cliente}`, (res, results2) => {
const count = (parseFloat(results2[0].cuenta)).toFixed(2);
const pay2 = (parseFloat(results2[0].abono) + parseFloat(pay)).toFixed(2);;
const restante = count - pay;
if (restante >= 0) {
connection.query(`UPDATE clientes SET abono='${pay2}', cuenta='${restante}' WHERE clientes.ID='${cliente}'`, (err, resuls) => {
if (err) { console.log(err); }
})
} else {
console.log('La cantidad a cancelar es Invalida');
}
})
res.redirect('/cuentas');
})
app.get('/cuentas', (req, res) => {
const consultaDolar = require('consulta-dolar-venezuela');
consultaDolar.$monitor().then($ => { dolar = parseFloat($['$bcv'].slice(4, 9)); });
connection.query(`SELECT * FROM clientes`, (req, results) => {
res.render('cuentas', {
cuentas: results,
dolar: dolar
});
})
})
app.get('/locales', (req, res) => {
// for(let i = 1; i <= 400; i++){
// connection.query(`INSERT INTO locales(local, ocupado) VALUES ('${i}','NO')`);
// }
connection.query(`SELECT * FROM locales`, (req, results) => {
res.render('locales', {
locales: results,
});
})
})
app.get('/limpieza', (req, res) => {
connection.query(`SELECT * FROM limpieza`, (err, results) => {
res.render('limpieza', {
limpieza: results,
})
})
})
app.get('/seguridad', (req, res) => {
connection.query(`SELECT * FROM seguridad`, (err, results) => {
res.render('seguridad', {
seguridad: results,
})
})
})
app.get('/clientes', (req, res) => {
Promise.all([
new Promise((resolve, reject) => {
connection.query(`SELECT * FROM clientes`, (error, results) => {
if (error) {
reject(error);
} else {
resolve(results);
}
});
}),
new Promise((resolve, reject) => {
connection.query(`SELECT * FROM locales WHERE ocupado = 'NO'`, (error, results) => {
if (error) {
reject(error);
} else {
resolve(results);
}
});
})
]).then(([results1, results2]) => {
res.render('clientes', { clientes: results1, locales: results2 });
}).catch((error) => {
console.error(error);
});
});
app.get('/editClient/:id', (req, res) => {
const ID = req.params.id;
connection.query(`SELECT * FROM clientes WHERE ID = '${ID}'`, (err, results) => {
res.render('edit', { clientUpdate: results[0] });
})
})
app.get('/editClear/:id', (req, res) => {
const ID = req.params.id;
connection.query(`SELECT * FROM limpieza WHERE ID = '${ID}'`, (err, results) => {
res.render('editClear', { jobUpdate: results[0] });
})
})
app.get('/editSecurity/:id', (req, res) => {
const ID = req.params.id;
connection.query(`SELECT * FROM seguridad WHERE ID = '${ID}'`, (err, results) => {
res.render('editSecurity', { securityUpdate: results[0] });
})
})
app.post('/updateClear', (req, res) => {
const id = req.body.id
const name = req.body.name
const lastname = req.body.lastname
const identify = req.body.identify
const job = req.body.job
connection.query(`UPDATE limpieza SET nombre='${name}',apellido='${lastname}',cedula='${identify}',cargo='${job}' WHERE ID='${id}'`, (err, resuls) => {
if (err) { console.log(err); }
else {
res.redirect('/limpieza');
}
})
})
app.post('/updateSecurity', (req, res) => {
const id = req.body.id;
const name = req.body.name;
const lastname = req.body.lastname;
const identify = req.body.identify;
const job = req.body.job;
connection.query(`UPDATE seguridad SET nombre='${name}',apellido='${lastname}',cedula='${identify}',cargo='${job}' WHERE ID='${id}'`, (err, resuls) => {
if (err) { console.log(err); }
else {
res.redirect('/seguridad');
}
})
})
app.post('/updateClient', (req, res) => {
const id = req.body.id;
const local = req.body.local;
const name = req.body.name;
const lastname = req.body.lastname;
const identify = req.body.identify;
console.log(req.body);
connection.query(`UPDATE clientes SET local='${local}',nombre='${name}',apellido='${lastname}',cedula='${identify}' WHERE ID='${id}'`, (err, resuls) => {
if (err) { console.log(err); }
else {res.redirect('/clientes');}
})
})
app.get('/eraserSecurity/:id', (req, res) => {
const ID = req.params.id;
connection.query(`DELETE FROM seguridad WHERE ID = '${ID}'`, (err, results) => {
res.redirect('/seguridad');
})
})
app.get('/eraserClear/:id', (req, res) => {
const ID = req.params.id;
connection.query(`DELETE FROM limpieza WHERE ID = '${ID}'`, (err, results) => {
res.redirect('/limpieza');
})
})
app.get('/eraserClient/:id', (req, res) => {
const ID = req.params.id;
let idLocal;
connection.query(`SELECT * FROM clientes WHERE ID = ${ID}`, (err, res2) => {
idLocal = res2[0].local;
connection.query(`UPDATE locales SET ocupado='NO' WHERE ID = ${idLocal}`, (err, result) => { if (err) { console.log(err); } });
})
connection.query(`DELETE FROM clientes WHERE ID = '${ID}'`, (err, results) => {
res.redirect('/clientes');
})
})
app.post('/createClient', (req, res) => {
const local = req.body.local
const name = req.body.name
const lastname = req.body.lastname
const identify = req.body.identify
const date = new Date();
const dia = date.getDay();
const mes = date.getMonth();
const ano = date.getFullYear();
const fecha = `${dia}/${mes}/${ano}`;
connection.query(`INSERT INTO clientes (local, nombre, apellido, cedula,cuenta,abono,fecha) VALUES ('${local}','${name}','${lastname}','${identify}','50','0','${fecha}')`,
(err, result) => {
connection.query(`UPDATE locales SET ocupado='SI' WHERE ID = ${local}`);
res.redirect('/clientes');
if(err){console.log(err);};
});
})
app.post('/updateCount', (req, res) => {
const cliente = req.body.cliente;
const count = req.body.count;
let oldCount = 0;
connection.query(`SELECT cuenta FROM clientes WHERE ID = '${cliente}';`, (err, result) => {
oldCount = result[0].cuenta;
oldCount += parseFloat(count);
connection.query(`UPDATE clientes SET cuenta='${oldCount}', abono='0' WHERE ID = '${cliente}'`, (err, result) => {
res.redirect('/cuentas')
})
});
})
app.post('/createJob', (req, res) => {
const name = req.body.name
const lastname = req.body.lastname
const identify = req.body.identify
const job = req.body.job
connection.query(`INSERT INTO limpieza (nombre, apellido, cedula, cargo) VALUES ('${name}','${lastname}','${identify}','${job}')`,
(err, result) => {
res.redirect('/limpieza');
if(err){console.log(err);};
});
})
app.post('/createSec', (req, res) => {
const name = req.body.name
const lastname = req.body.lastname
const identify = req.body.identify
const job = req.body.job
connection.query(`INSERT INTO seguridad (nombre, apellido, cedula, cargo) VALUES ('${name}','${lastname}','${identify}','${job}')`,
(err, result) => {
res.redirect('/seguridad');
if(err){console.log(err);};
});
})
app.post('/recover', (req, res) => {
const telefono = req.body.telefono;
const pass = req.body.pass;
const repass = req.body.repass;
let idUser;
connection.query(`SELECT * FROM login WHERE telefono = '${telefono}'`,
(err, results) => {
if (results) {
idUser = results[0].ID;
connection.query(`UPDATE login SET contrasena='${pass}' WHERE ID ='${idUser}'`, (err, result) => {
if (err) throw err;
});
res.render('login');
}
else {
console.log(err);
}
})
})
app.post('/auth', (req, res) => {
const user = req.body.user;
const pass = req.body.pass;
if (user && pass) {
connection.query(`SELECT * FROM login WHERE nombre ='${user}' AND contrasena = '${pass}'`,
(err, results) => {
if (results.length > 0) {
res.render('inicio');
userInfo = results;
}
else {
res.render('login', {
seguridad: results,
})
}
if(err){console.log(err);}
})
}
})
const port = process.env.PORT || 3000;
app.listen(port, (res, req) => {
console.log('Servido Conectado');
})