-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
288 lines (243 loc) · 11.1 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
const express = require('express');
const path = require('path'); // Import path module to handle file paths
const db = require('./config/db'); // Import the database connection
const bodyParser = require('body-parser'); // Import body-parser
const QRCode = require('qrcode'); // Import the QRCode library
const fs = require('fs'); // Import fs to handle file system operations
// Initialize the Express app
const app = express();
// Set EJS as the templating engine
app.set('view engine', 'ejs');
// Set the directory for EJS views
app.set('views', path.join(__dirname, 'views'));
// Use body-parser to parse form data
app.use(bodyParser.urlencoded({ extended: false }));
// Define the root route to render the EJS template
app.get('/', (req, res) => {
res.render('index', { title: 'Welcome to LionelV2' }); // Render 'index.ejs' with a title variable
});
// Add this route to handle the '/settings' page
app.get('/settings', (req, res) => {
const { productId, productName, entryQR, exitQR } = req.query;
res.render('settings', {
title: 'Settings Page',
productId,
productName,
entryQR,
exitQR
});
});
// Utility function to handle QR code downloads
function downloadQRCode(res, productId, type) {
const qrCodePath = path.join(__dirname, 'qrcodes', `${type}_${productId}.png`);
res.download(qrCodePath, `${type}_${productId}.png`, (err) => {
if (err) {
console.error(`Error downloading ${type} QR code:`, err.message, err.stack);
return res.status(500).send(`Failed to download ${type} QR code.`);
}
});
}
// Utility function to handle stock transactions (entry/exit)
function handleStockTransaction(res, productId, transactionType) {
const productQuery = `SELECT Products.id, Products.name, Products.packaging_type, Stock.stock_level
FROM Products
JOIN Stock ON Products.id = Stock.product_id
WHERE Products.id = ?`;
db.query(productQuery, [productId], (err, results) => {
if (err) {
console.error(`Error fetching product details for ${transactionType}:`, err.message, err.stack);
return res.status(500).send('Failed to fetch product details.');
}
if (results.length === 0) {
return res.status(404).send('Product not found.');
}
const product = results[0];
const stockLevel = product.stock_level;
if (transactionType === 'exit') {
// Fetch chantier names for stock exit
const chantierQuery = 'SELECT id, chantier_nom FROM chantier';
db.query(chantierQuery, (err, chantierResults) => {
if (err) {
console.error('Error fetching chantier data:', err.message, err.stack);
return res.status(500).send('Failed to fetch chantier data.');
}
const chantierList = chantierResults;
// Render the product page for stock exit with chantier list
res.render('product', {
product,
stockLevel,
transactionType,
chantierList
});
});
} else {
// Render the product page for stock entry (no chantier list needed)
res.render('product', {
product,
stockLevel,
transactionType,
chantierList: [] // Empty list for stock entry
});
}
});
}
// POST route to handle form submission for adding a product
app.post('/add-product', (req, res) => {
console.log('Form submission received:', req.body);
const { productName, initialStock } = req.body;
const packagingType = req.body['packagingType[]'];
if (!productName || !packagingType || initialStock === undefined) {
console.error('Validation Error: Missing required fields.');
return res.status(400).send('All fields are required.');
}
const packagingTypeString = Array.isArray(packagingType) ? packagingType.join(', ') : packagingType;
const insertProductQuery = `INSERT INTO Products (name, packaging_type) VALUES (?, ?)`;
db.query(insertProductQuery, [productName, packagingTypeString], (err, productResult) => {
if (err) {
console.error('Error inserting product:', err.message, err.stack);
return res.status(500).send('Failed to add product.');
}
const productId = productResult.insertId;
const insertStockQuery = `INSERT INTO Stock (product_id, stock_level) VALUES (?, ?)`;
db.query(insertStockQuery, [productId, initialStock], (err, stockResult) => {
if (err) {
console.error('Error inserting stock:', err.message, err.stack);
return res.status(500).send('Failed to add stock.');
}
console.log(`Product added successfully with ID: ${productId}`);
console.log(`Stock added successfully for product ID: ${productId}`);
// Generate QR codes for stock entry and stock exit
const entryUrl = `http://localhost:3000/stock-entry/${productId}`;
const exitUrl = `http://localhost:3000/stock-exit/${productId}`;
const qrCodeDir = path.join(__dirname, 'qrcodes');
// Generate QR code for entry (PNG file and Data URL)
const entryQRPath = path.join(qrCodeDir, `entry_${productId}.png`);
QRCode.toFile(entryQRPath, entryUrl, {
color: {
dark: '#00FF00', // Green QR code shape (foreground)
light: '#FFFFFF' // White background
},
width: 512 // Set the size to 512x512px
}, (err) => {
if (err) {
console.error('Error generating entry QR code:', err.message, err.stack);
return res.status(500).send('Failed to generate entry QR code.');
}
QRCode.toDataURL(entryUrl, { width: 512 }, (err, entryQR) => {
if (err) {
console.error('Error generating entry QR code (Data URL):', err.message, err.stack);
return res.status(500).send('Failed to generate entry QR code (Data URL).');
}
// Generate QR code for exit (PNG file and Data URL)
const exitQRPath = path.join(qrCodeDir, `exit_${productId}.png`);
QRCode.toFile(exitQRPath, exitUrl, {
color: {
dark: '#FF0000', // Red QR code shape (foreground)
light: '#FFFFFF' // White background
},
width: 512 // Set the size to 512x512px
}, (err) => {
if (err) {
console.error('Error generating exit QR code:', err.message, err.stack);
return res.status(500).send('Failed to generate exit QR code.');
}
QRCode.toDataURL(exitUrl, { width: 512 }, (err, exitQR) => {
if (err) {
console.error('Error generating exit QR code (Data URL):', err.message, err.stack);
return res.status(500).send('Failed to generate exit QR code (Data URL).');
}
// Log QR code generation success
console.log('QR codes generated successfully for product ID:', productId);
// Redirect to settings page with QR codes as query parameters
res.redirect(`/settings?productId=${productId}&productName=${encodeURIComponent(productName)}&entryQR=${encodeURIComponent(entryQR)}&exitQR=${encodeURIComponent(exitQR)}`);
});
});
});
});
});
});
});
// Route to download entry QR code
app.get('/download-entry-qr/:productId', (req, res) => {
const productId = req.params.productId;
downloadQRCode(res, productId, 'entry');
});
// Route to download exit QR code
app.get('/download-exit-qr/:productId', (req, res) => {
const productId = req.params.productId;
downloadQRCode(res, productId, 'exit');
});
// Route to handle stock entry page
app.get('/stock-entry/:productId', (req, res) => {
const productId = req.params.productId;
handleStockTransaction(res, productId, 'entry');
});
// Route to handle stock exit page
app.get('/stock-exit/:productId', (req, res) => {
const productId = req.params.productId;
handleStockTransaction(res, productId, 'exit');
});
// POST route to handle stock update (entry/exit)
app.post('/update-stock', (req, res) => {
const { productId, transactionType, quantity, site } = req.body;
// Ensure that the quantity is a positive integer
if (!productId || !quantity || quantity <= 0 || (transactionType === 'exit' && !site)) {
console.error('Validation Error: Missing required fields or invalid quantity.');
return res.status(400).send('All fields are required and quantity must be a positive number.');
}
// Convert quantity to an integer
const quantityInt = parseInt(quantity, 10);
// Determine the SQL query for stock update based on transaction type
const stockUpdateQuery = transactionType === 'entry'
? `UPDATE Stock SET stock_level = stock_level + ? WHERE product_id = ?`
: `UPDATE Stock SET stock_level = stock_level - ? WHERE product_id = ?`;
// Update the stock level
db.query(stockUpdateQuery, [quantityInt, productId], (err, result) => {
if (err) {
console.error(`Error updating stock for ${transactionType}:`, err.message, err.stack);
return res.status(500).send('Failed to update stock.');
}
// Log the transaction in the Transactions table
const transactionQuery = transactionType === 'exit'
? `INSERT INTO Transactions (product_id, transaction_type, quantity, chantier_nom) VALUES (?, 'exit', ?, ?)`
: `INSERT INTO Transactions (product_id, transaction_type, quantity) VALUES (?, 'entry', ?)`;
const transactionParams = transactionType === 'exit'
? [productId, quantityInt, site]
: [productId, quantityInt];
db.query(transactionQuery, transactionParams, (err) => {
if (err) {
console.error('Error inserting transaction data:', err.message, err.stack);
return res.status(500).send('Failed to insert transaction data.');
}
console.log(`Transaction data inserted successfully for product ID: ${productId}, type: ${transactionType}, quantity: ${quantityInt}`);
res.send(`
<html>
<head>
<title>Stock ${transactionType === 'entry' ? 'Entry' : 'Exit'} Success</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body>
<div class="container mt-5">
<div class="alert alert-success text-center">
Stock ${transactionType === 'entry' ? 'entry' : 'exit'} successful! Quantity: ${quantityInt}.
</div>
<div class="text-center">
<a href="/" class="btn btn-primary">Return to Home</a>
</div>
</div>
</body>
</html>
`);
});
});
});
// Start the server on port 3000
const PORT = 3000;
const qrCodeDir = path.join(__dirname, 'qrcodes');
// Ensure the directory for storing QR codes exists when the server starts
if (!fs.existsSync(qrCodeDir)) {
fs.mkdirSync(qrCodeDir);
}
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});