From 13349cc82e2b7a4a4de3b508b06783876bdc9f1b Mon Sep 17 00:00:00 2001 From: Sibunnayak Date: Mon, 23 Sep 2024 12:04:51 +0530 Subject: [PATCH] fixing bug in order section --- resources/PD_Orders/pdOrderController.js | 101 ++++++++++++++--------- 1 file changed, 64 insertions(+), 37 deletions(-) diff --git a/resources/PD_Orders/pdOrderController.js b/resources/PD_Orders/pdOrderController.js index 0129fa9..d19215a 100644 --- a/resources/PD_Orders/pdOrderController.js +++ b/resources/PD_Orders/pdOrderController.js @@ -88,16 +88,29 @@ export const processOrder = async (req, res) => { } // Validate quantities + const exceededItems = []; + + // Check each item in invoiceItems for quantity limits for (const item of invoiceItems) { const orderItem = order.orderItem.find( (i) => i.productId.toString() === item.productId.toString() ); + + // If processquantity exceeds remainingQuantity, add the item name to exceededItems if (orderItem && item.processquantity > orderItem.remainingQuantity) { - return res.status(400).json({ - error: `Product '${item.name}' has more quantity than remaining in the order.`, - }); + exceededItems.push(item.name); } } + + // If there are any exceeded items, return an error with their names + if (exceededItems.length > 0) { + return res.status(400).json({ + error: `The following items have more quantity than remaining in the order: ${exceededItems.join(", ")}`, + }); + } + + // Continue with the rest of the logic if no quantity limits are exceeded + // Generate unique invoice number const existingInvoices = await Invoice.find({ orderId }); @@ -131,40 +144,55 @@ export const processOrder = async (req, res) => { // Save the invoice const savedInvoice = await newInvoice.save(); - // Update the order's order items with the remaining quantity - let allItemsProcessed = true; - order.orderItem.forEach((item) => { - const invoicedItem = invoiceItems.find( - (i) => i.productId.toString() === item.productId.toString() - ); - if (invoicedItem) { - item.remainingQuantity -= invoicedItem.processquantity; - if (item.remainingQuantity < 0) { - item.remainingQuantity = 0; // Ensure it does not go negative - } - if (item.remainingQuantity > 0) { - allItemsProcessed = false; - } - } - }); +// Update the order's order items with the remaining quantity +let allItemsProcessed = true; // Flag to check if all items are processed - // Calculate total amount for pending items - let pendingTotalAmount = 0; - order.orderItem.forEach((item) => { - if (item.remainingQuantity > 0) { - const itemPendingSubtotal = item.price * item.remainingQuantity; - const itemPendingGST = - ((item.price * item.GST) / 100) * item.remainingQuantity; - pendingTotalAmount += itemPendingSubtotal + itemPendingGST; - } - }); +order.orderItem.forEach((item) => { + const invoicedItem = invoiceItems.find( + (i) => i.productId.toString() === item.productId.toString() + ); - // Update the order status - order.status = allItemsProcessed ? "processing" : "pending"; - order.invoices.push(savedInvoice._id); + // If the item was invoiced, update the remaining quantity + if (invoicedItem) { + // Deduct the processed quantity from remaining quantity + item.remainingQuantity -= invoicedItem.processquantity; + + // Ensure remaining quantity does not go negative + if (item.remainingQuantity < 0) { + item.remainingQuantity = 0; + } + } + + // Check if the remaining quantity is greater than 0, even for items not invoiced + if (item.remainingQuantity > 0) { + allItemsProcessed = false; + } +}); + +// Calculate total amount for pending items +let pendingTotalAmount = 0; +order.orderItem.forEach((item) => { + if (item.remainingQuantity > 0) { + const itemPendingSubtotal = item.price * item.remainingQuantity; + const itemPendingGST = + ((item.price * item.GST) / 100) * item.remainingQuantity; + pendingTotalAmount += itemPendingSubtotal + itemPendingGST; + } +}); + +// Only update order status if all items have been fully processed +if (allItemsProcessed) { + order.status = "processing"; // All items are fully processed +} else { + order.status = "pending"; // There are still remaining quantities +} + +// Add the invoice to the order +order.invoices.push(savedInvoice._id); + +// Save the updated order +await order.save(); - // Save the updated order - await order.save(); // Prepare the email content const processedItems = invoiceItems @@ -668,12 +696,12 @@ export const getPlacedPendingOrderAdmin = async (req, res) => { // Fetch paginated new orders const placedOrders = await PdOrder.find({ status: "pending" }) - .sort({ createdAt: -1 }) .skip(skip) .limit(limit) .populate({ path: "orderItem.productId" }) .populate({ path: "invoices" }) - .populate({ path: "addedBy" }); + .populate({ path: "addedBy" }) + .sort({ createdAt: -1 }); if (placedOrders.length > 0) { return res.status(200).json({ placedOrders, totalOrders }); @@ -699,7 +727,6 @@ export const getCancelledOrdersAdmin = async (req, res) => { // Fetch paginated cancelled orders const cancelledOrders = await PdOrder.find({ status: "cancelled" }) - .sort({ createdAt: -1 }) .skip(skip) .limit(limit) .populate({ path: "orderItem.productId" })