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
|
// Validate quantities
|
||||||
|
const exceededItems = [];
|
||||||
|
|
||||||
|
// Check each item in invoiceItems for quantity limits
|
||||||
for (const item of invoiceItems) {
|
for (const item of invoiceItems) {
|
||||||
const orderItem = order.orderItem.find(
|
const orderItem = order.orderItem.find(
|
||||||
(i) => i.productId.toString() === item.productId.toString()
|
(i) => i.productId.toString() === item.productId.toString()
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// If processquantity exceeds remainingQuantity, add the item name to exceededItems
|
||||||
if (orderItem && item.processquantity > orderItem.remainingQuantity) {
|
if (orderItem && item.processquantity > orderItem.remainingQuantity) {
|
||||||
return res.status(400).json({
|
exceededItems.push(item.name);
|
||||||
error: `Product '${item.name}' has more quantity than remaining in the order.`,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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
|
// Generate unique invoice number
|
||||||
const existingInvoices = await Invoice.find({ orderId });
|
const existingInvoices = await Invoice.find({ orderId });
|
||||||
@ -131,40 +144,55 @@ export const processOrder = async (req, res) => {
|
|||||||
// Save the invoice
|
// Save the invoice
|
||||||
const savedInvoice = await newInvoice.save();
|
const savedInvoice = await newInvoice.save();
|
||||||
|
|
||||||
// Update the order's order items with the remaining quantity
|
// 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 (invoicedItem) {
|
|
||||||
item.remainingQuantity -= invoicedItem.processquantity;
|
|
||||||
if (item.remainingQuantity < 0) {
|
|
||||||
item.remainingQuantity = 0; // Ensure it does not go negative
|
|
||||||
}
|
|
||||||
if (item.remainingQuantity > 0) {
|
|
||||||
allItemsProcessed = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Calculate total amount for pending items
|
order.orderItem.forEach((item) => {
|
||||||
let pendingTotalAmount = 0;
|
const invoicedItem = invoiceItems.find(
|
||||||
order.orderItem.forEach((item) => {
|
(i) => i.productId.toString() === item.productId.toString()
|
||||||
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
|
// If the item was invoiced, update the remaining quantity
|
||||||
order.status = allItemsProcessed ? "processing" : "pending";
|
if (invoicedItem) {
|
||||||
order.invoices.push(savedInvoice._id);
|
// 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
|
// Prepare the email content
|
||||||
const processedItems = invoiceItems
|
const processedItems = invoiceItems
|
||||||
@ -668,12 +696,12 @@ export const getPlacedPendingOrderAdmin = async (req, res) => {
|
|||||||
|
|
||||||
// Fetch paginated new orders
|
// Fetch paginated new orders
|
||||||
const placedOrders = await PdOrder.find({ status: "pending" })
|
const placedOrders = await PdOrder.find({ status: "pending" })
|
||||||
.sort({ createdAt: -1 })
|
|
||||||
.skip(skip)
|
.skip(skip)
|
||||||
.limit(limit)
|
.limit(limit)
|
||||||
.populate({ path: "orderItem.productId" })
|
.populate({ path: "orderItem.productId" })
|
||||||
.populate({ path: "invoices" })
|
.populate({ path: "invoices" })
|
||||||
.populate({ path: "addedBy" });
|
.populate({ path: "addedBy" })
|
||||||
|
.sort({ createdAt: -1 });
|
||||||
|
|
||||||
if (placedOrders.length > 0) {
|
if (placedOrders.length > 0) {
|
||||||
return res.status(200).json({ placedOrders, totalOrders });
|
return res.status(200).json({ placedOrders, totalOrders });
|
||||||
@ -699,7 +727,6 @@ export const getCancelledOrdersAdmin = async (req, res) => {
|
|||||||
|
|
||||||
// Fetch paginated cancelled orders
|
// Fetch paginated cancelled orders
|
||||||
const cancelledOrders = await PdOrder.find({ status: "cancelled" })
|
const cancelledOrders = await PdOrder.find({ status: "cancelled" })
|
||||||
.sort({ createdAt: -1 })
|
|
||||||
.skip(skip)
|
.skip(skip)
|
||||||
.limit(limit)
|
.limit(limit)
|
||||||
.populate({ path: "orderItem.productId" })
|
.populate({ path: "orderItem.productId" })
|
||||||
|
Loading…
Reference in New Issue
Block a user