fixing bug in order section

This commit is contained in:
Sibunnayak 2024-09-23 12:04:51 +05:30
parent 5e534f5bc0
commit 13349cc82e

View File

@ -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) {
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: `Product '${item.name}' has more quantity than remaining in the order.`,
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) => {
// 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()
);
// 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 it does not go negative
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) => {
// 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;
}
});
});
// Update the order status
order.status = allItemsProcessed ? "processing" : "pending";
order.invoices.push(savedInvoice._id);
// 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" })