diff --git a/resources/PD_Orders/invoiceModel.js b/resources/PD_Orders/invoiceModel.js index 441d4ef..77abefb 100644 --- a/resources/PD_Orders/invoiceModel.js +++ b/resources/PD_Orders/invoiceModel.js @@ -34,7 +34,7 @@ const orderItemSchema = new Schema({ type: Number, required: true, }, - quantity: { + processquantity: { //updated quantity type: Number, required: true, default: 1, diff --git a/resources/PD_Orders/pdOrderController.js b/resources/PD_Orders/pdOrderController.js index 89699fa..272fa21 100644 --- a/resources/PD_Orders/pdOrderController.js +++ b/resources/PD_Orders/pdOrderController.js @@ -79,7 +79,7 @@ export const processOrder = async (req, res) => { } // Find the order by ID - const order = await PdOrder.findById(orderId); + const order = await PdOrder.findById(orderId).populate('addedBy'); if (!order) { return res.status(404).json({ error: 'Order not found' }); @@ -88,25 +88,25 @@ export const processOrder = async (req, res) => { // Validate quantities for (const item of invoiceItems) { const orderItem = order.orderItem.find(i => i.productId.toString() === item.productId.toString()); - if (orderItem && item.quantity > orderItem.remainingQuantity) { + if (orderItem && item.processquantity > orderItem.remainingQuantity) { return res.status(400).json({ error: `Product '${item.name}' has more quantity than remaining in the order.` }); } } // Generate unique invoice number const existingInvoices = await Invoice.find({ orderId }); - const invoiceNumber = existingInvoices.length + 1; + const invoiceNumber = existingInvoices.length + 1; const invoiceId = `ch/${order.uniqueId}/${invoiceItems.length}/${invoiceNumber}`; - // Calculate subtotal, gstTotal, and invoiceAmount + // Calculate subtotal, gstTotal, and invoiceAmount for processed items let subtotal = 0; let gstTotal = 0; let invoiceAmount = 0; invoiceItems.forEach(item => { - const itemSubtotal = item.price * item.quantity; - const itemGST = (item.price * item.GST) / 100 * item.quantity; - + const itemSubtotal = item.price * item.processquantity; + const itemGST = (item.price * item.GST / 100) * item.processquantity; + subtotal += itemSubtotal; gstTotal += itemGST; invoiceAmount += itemSubtotal + itemGST; @@ -130,7 +130,7 @@ export const processOrder = async (req, res) => { order.orderItem.forEach(item => { const invoicedItem = invoiceItems.find(i => i.productId.toString() === item.productId.toString()); if (invoicedItem) { - item.remainingQuantity -= invoicedItem.quantity; + item.remainingQuantity -= invoicedItem.processquantity; if (item.remainingQuantity < 0) { item.remainingQuantity = 0; // Ensure it does not go negative } @@ -140,20 +140,130 @@ export const processOrder = async (req, res) => { } }); + // 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'; // Save the updated order await order.save(); - return res.status(201).json({ - message: 'Invoice created and order processed successfully', - invoice: savedInvoice, - order: order, + // Prepare the email content + const processedItems = invoiceItems.map((item, index) => ` +
Your invoice ID is: ${savedInvoice.invoiceId}
+ ` + : ` +Your invoice ID is: ${savedInvoice.invoiceId}
+ `; + + await sendEmail({ + to: order.addedBy.email, + from: process.env.SEND_EMAIL_FROM, + subject: emailSubject, + html: ` +S No. | +Product Name | +Image | +Quantity | +Price | +GST Amount | +SubTotal | +
---|---|---|---|---|---|---|
Total Amount: | +₹${savedInvoice.invoiceAmount.toFixed(2)} | +
S No. | +Product Name | +Image | +Quantity | +Price | +GST Amount | +SubTotal | +
---|---|---|---|---|---|---|
Pending Amount: | +₹${pendingTotalAmount.toFixed(2)} | +