diff --git a/resources/PD_Orders/pdOrderController.js b/resources/PD_Orders/pdOrderController.js index d19215a..d0d5a61 100644 --- a/resources/PD_Orders/pdOrderController.js +++ b/resources/PD_Orders/pdOrderController.js @@ -95,22 +95,23 @@ export const processOrder = async (req, res) => { 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) { 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(", ")}`, + 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 }); @@ -144,55 +145,54 @@ 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; // Flag to check if all items are processed + // Update the order's order items with the remaining quantity + let allItemsProcessed = true; // Flag to check if all items are processed -order.orderItem.forEach((item) => { - const invoicedItem = invoiceItems.find( - (i) => i.productId.toString() === item.productId.toString() - ); + order.orderItem.forEach((item) => { + const invoicedItem = invoiceItems.find( + (i) => i.productId.toString() === item.productId.toString() + ); - // If the item was invoiced, update the remaining quantity - if (invoicedItem) { - // Deduct the processed quantity from remaining quantity - item.remainingQuantity -= invoicedItem.processquantity; + // 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; + // 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 } - } - // 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(); + // Add the invoice to the order + order.invoices.push(savedInvoice._id); + // Save the updated order + await order.save(); // Prepare the email content const processedItems = invoiceItems @@ -806,31 +806,47 @@ export const getProcessingInvoices = async (req, res) => { try { const page = parseInt(req.query.page, 10) || 1; const limit = parseInt(req.query.limit, 10) || 5; - - // Calculate the number of documents to skip + const { invoiceId, orderId } = req.query; const skip = (page - 1) * limit; - // Get the total count of 'processing' invoices - const totalInvoices = await Invoice.countDocuments({ - courierStatus: "processing", - }); + let query = { courierStatus: "processing" }; - // Fetch the invoices with pagination - const invoices = await Invoice.find({ courierStatus: "processing" }) + if (invoiceId) { + query.invoiceId = { $regex: invoiceId, $options: "i" }; + } + const invoices = await Invoice.find(query) .sort({ createdAt: -1 }) .skip(skip) - .limit(limit); - // Respond with the invoices and the total count - res.status(200).json({ - totalCount: totalInvoices, - currentPage: page, - totalPages: Math.ceil(totalInvoices / limit), - invoices, - }); + .limit(limit) + .populate({ + path: "orderId", + select: "uniqueId", + }); + + if (orderId) { + const filteredInvoices = invoices.filter(invoice => + invoice.orderId && invoice.orderId.uniqueId && + invoice.orderId.uniqueId.toLowerCase().includes(orderId.toLowerCase()) + ); + res.status(200).json({ + totalCount: filteredInvoices.length, + currentPage: page, + totalPages: Math.ceil(filteredInvoices.length / limit), + invoices: filteredInvoices, + }); + } else { + res.status(200).json({ + totalCount: invoices.length, + currentPage: page, + totalPages: Math.ceil(invoices.length / limit), + invoices, + }); + } } catch (error) { res.status(500).json({ error: error.message }); } }; + export const getInvoiceDetailsById = async (req, res) => { const { invoiceId } = req.params; @@ -950,13 +966,14 @@ export const updateCourierStatusToDispatched = async (req, res) => {