fixing bug in order section
This commit is contained in:
parent
5e534f5bc0
commit
13349cc82e
@ -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 });
|
||||
@ -132,20 +145,28 @@ export const processOrder = async (req, res) => {
|
||||
const savedInvoice = await newInvoice.save();
|
||||
|
||||
// Update the order's order items with the remaining quantity
|
||||
let allItemsProcessed = true;
|
||||
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
|
||||
@ -159,13 +180,20 @@ export const processOrder = async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// Update the order status
|
||||
order.status = allItemsProcessed ? "processing" : "pending";
|
||||
// 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();
|
||||
|
||||
|
||||
// Prepare the email content
|
||||
const processedItems = invoiceItems
|
||||
.map(
|
||||
@ -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" })
|
||||
|
Loading…
Reference in New Issue
Block a user