From 839f942f085c72a4b63f2430716d7dbe5b4ebc01 Mon Sep 17 00:00:00 2001 From: Sibunnayak Date: Thu, 19 Sep 2024 15:11:44 +0530 Subject: [PATCH] order partial and fully processing with sending mail --- resources/PD_Orders/invoiceModel.js | 2 +- resources/PD_Orders/pdOrderController.js | 138 ++++++++++++++++++++--- 2 files changed, 125 insertions(+), 15 deletions(-) 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) => ` + + ${index + 1} + ${item.name} + + ${item.image && item.image.length > 0 ? `${item.name}` : 'No Image'} + + ${item.processquantity} + ₹${item.price} + ₹${(item.GST * item.price) / 100} + ₹${(item.price + (item.GST * item.price) / 100) * item.processquantity} + + `).join(""); + + const pendingItems = order.orderItem.filter(item => item.remainingQuantity > 0).map((item, index) => ` + + ${index + 1} + ${item.name} + + ${item.image && item.image.length > 0 ? `${item.name}` : 'No Image'} + + ${item.remainingQuantity} + ₹${item.price} + ₹${(item.GST * item.price) / 100} + ₹${(item.price + (item.GST * item.price) / 100) * item.remainingQuantity} + + `).join(""); + + // Dynamic email subject and message based on the order status + const emailSubject = allItemsProcessed + ? `Your Order #${order.uniqueId} is in Processing!` + : `Your Order #${order.uniqueId} is Partially Processed!`; + + const emailMessage = allItemsProcessed + ? ` +

Exciting news! Your order #${order.uniqueId} has entered the processing phase. We're working hard to ensure everything is perfect for you.

+

Your invoice ID is: ${savedInvoice.invoiceId}

+ ` + : ` +

Good news! Some items of your order #${order.uniqueId} have been processed. The remaining items will be processed soon.

+

Your invoice ID is: ${savedInvoice.invoiceId}

+ `; + + await sendEmail({ + to: order.addedBy.email, + from: process.env.SEND_EMAIL_FROM, + subject: emailSubject, + html: ` +
+ ${emailMessage} + Hi ${order.addedBy.name}, +

Order Status: ${order.status.charAt(0).toUpperCase() + order.status.slice(1)}

+ +

Processed Items:

+ + + + + + + + + + + + + + ${processedItems} + + + + + +
S No.Product NameImageQuantityPriceGST AmountSubTotal
Total Amount:₹${savedInvoice.invoiceAmount.toFixed(2)}
+ + ${pendingItems.length > 0 ? ` +

Pending Items:

+ + + + + + + + + + + + + + ${pendingItems} + + + + + +
S No.Product NameImageQuantityPriceGST AmountSubTotal
Pending Amount:₹${pendingTotalAmount.toFixed(2)}
+ ` : ''} +
+ `, }); + + res.status(200).json({ message: 'Order processed and invoice created successfully', invoiceId: savedInvoice.invoiceId }); + } catch (error) { - console.error(error); - return res.status(500).json({ error: 'Internal server error' }); + console.error('Error processing order:', error); + res.status(500).json({ error: 'An error occurred while processing the order' }); } };