Gitlab@Informatics

Skip to content
Snippets Groups Projects
Commit b291ec67 authored by 65160383's avatar 65160383
Browse files

werehouse_db

parent e06804cc
No related branches found
No related tags found
No related merge requests found
......@@ -86,23 +86,14 @@ exports.updateProduct = (req, res) => {
exports.deleteProduct = (req, res) => {
const { id } = req.params;
// ลบข้อมูลในตาราง orders ก่อน
const deleteOrdersQuery = 'DELETE FROM orders WHERE product_id = ?';
db.query(deleteOrdersQuery, [id], (err, orderResult) => {
if (err) {
console.error('Error deleting orders:', err);
return res.redirect('/products?error=เกิดข้อผิดพลาดในการลบประวัติการเบิก');
}
// หลังจากลบข้อมูลใน orders แล้ว จึงลบสินค้า
const deleteProductQuery = 'DELETE FROM products WHERE id = ?';
db.query(deleteProductQuery, [id], (err, productResult) => {
// ลบสินค้าโดยตรง ไม่ต้องตรวจสอบ orders
const deleteQuery = 'DELETE FROM products WHERE id = ?';
db.query(deleteQuery, [id], (err, result) => {
if (err) {
console.error('Error deleting product:', err);
return res.redirect('/products?error=เกิดข้อผิดพลาดในการลบสินค้า');
}
res.redirect('/products?success=ลบสินค้าและประวัติการเบิกสำเร็จ');
});
res.redirect('/products?success=ลบสินค้าสำเร็จ');
});
};
......@@ -176,34 +167,61 @@ exports.showWithdrawConfirmation = (req, res) => {
};
// ยืนยันการเบิกสินค้า
exports.confirmWithdraw = (req, res) => {
exports.withdrawProduct = (req, res) => {
const { product_id, quantity } = req.body;
// ตรวจสอบสินค้าในคลัง
const query = 'SELECT * FROM products WHERE id = ?';
db.query(query, [product_id], (err, result) => {
if (err) {
console.error(err);
return res.redirect('/products?error=เกิดข้อผิดพลาดในการเบิกสินค้า');
return res.send(`
<script>
alert('เกิดข้อผิดพลาดในการเบิกสินค้า');
window.location.href = '/products';
</script>
`);
}
if (result.length > 0) {
const product = result[0];
if (product.quantity < quantity) {
return res.redirect('/products?error=สินค้าในคลังไม่เพียงพอ');
return res.send(`
<script>
alert('สินค้าในคลังไม่เพียงพอ');
window.location.href = '/products';
</script>
`);
}
// อัพเดทจำนวนสินค้า
const updateQuery = 'UPDATE products SET quantity = quantity - ? WHERE id = ?';
db.query(updateQuery, [quantity, product_id], (err, updateResult) => {
if (err) {
console.error(err);
return res.redirect('/products?error=เกิดข้อผิดพลาดในการอัพเดทจำนวนสินค้า');
}
res.redirect('/products?success=เบิกสินค้าสำเร็จ');
return res.send(`
<script>
alert('เกิดข้อผิดพลาดในการอัพเดทจำนวนสินค้า');
window.location.href = '/products';
</script>
`);
}
res.send(`
<script>
alert('เบิกสินค้าสำเร็จ');
window.location.href = '/products';
</script>
`);
});
} else {
res.redirect('/products?error=ไม่พบสินค้า');
return res.send(`
<script>
alert('ไม่พบสินค้า');
window.location.href = '/products';
</script>
`);
}
});
};
......@@ -15,7 +15,7 @@ router.post('/create', productController.createProduct);
router.get('/edit/:id', productController.editProductPage);
// อัพเดทสินค้า
router.put('/update/:id', productController.updateProduct);
router.post('/update/:id', productController.updateProduct);
// ลบสินค้า
router.post('/delete/:id', productController.deleteProduct);
......@@ -24,7 +24,6 @@ router.post('/delete/:id', productController.deleteProduct);
router.get('/search', productController.searchProducts);
// เบิกสินค้า
router.post('/withdraw', productController.showWithdrawConfirmation);
router.post('/confirm-withdraw', productController.confirmWithdraw);
router.post('/withdraw', productController.withdrawProduct);
module.exports = router;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment