diff --git a/resources/PD_Orders/pdOrderController.js b/resources/PD_Orders/pdOrderController.js index 7a8300b..10446ea 100644 --- a/resources/PD_Orders/pdOrderController.js +++ b/resources/PD_Orders/pdOrderController.js @@ -31,6 +31,10 @@ export const createOrder = async (req, res) => { } const addedBy = req.user._id; // Create a new PdOrder instance + // Ensure subtotal, gstTotal, and grandTotal are formatted to two decimal places + const formattedSubtotal = parseFloat(subtotal).toFixed(2); + const formattedGstTotal = parseFloat(gstTotal).toFixed(2); + const formattedGrandTotal = parseFloat(grandTotal).toFixed(2); const newOrder = new PdOrder({ paymentMode, shipTo, @@ -51,9 +55,9 @@ export const createOrder = async (req, res) => { quantity: item.count, remainingQuantity: item.count, })), - subtotal, - gstTotal, - grandTotal, + subtotal: formattedSubtotal, + gstTotal: formattedGstTotal, + grandTotal: formattedGrandTotal, addedBy, }); @@ -131,7 +135,10 @@ export const processOrder = async (req, res) => { gstTotal += itemGST; invoiceAmount += itemSubtotal + itemGST; }); - + // Round the final totals to two decimal places + subtotal = parseFloat(subtotal.toFixed(2)); + gstTotal = parseFloat(gstTotal.toFixed(2)); + invoiceAmount = parseFloat(invoiceAmount.toFixed(2)); // Create a new invoice const newInvoice = new Invoice({ invoiceId, @@ -180,7 +187,9 @@ export const processOrder = async (req, res) => { pendingTotalAmount += itemPendingSubtotal + itemPendingGST; } }); - + itemPendingSubtotal = parseFloat(itemPendingSubtotal.toFixed(2)); + itemPendingGST = parseFloat(itemPendingGST.toFixed(2)); + pendingTotalAmount = parseFloat(pendingTotalAmount.toFixed(2)); // Only update order status if all items have been fully processed if (allItemsProcessed) { order.status = "processing"; // All items are fully processed @@ -251,13 +260,13 @@ export const processOrder = async (req, res) => { item.remainingQuantity } ₹${ - item.price + (item.price).toFixed(2) } ₹${ - (item.GST * item.price) / 100 + ((item.GST * item.price) / 100).toFixed(2) } ₹${ - (item.price + (item.GST * item.price) / 100) * item.remainingQuantity + ((item.price + (item.GST * item.price) / 100) * item.remainingQuantity).toFixed(2) } ` @@ -415,9 +424,9 @@ export const cancelOrderController = async (req, res) => { const processedItems = invoice.items .map((item) => { - const itemSubtotal = item.price * item.processquantity; + const itemSubtotal = (item.price * item.processquantity).toFixed(2); const itemGST = - ((item.price * item.GST) / 100) * item.processquantity; + (((item.price * item.GST) / 100) * item.processquantity).toFixed(2); subtotal += itemSubtotal; gstTotal += itemGST; @@ -441,13 +450,13 @@ export const cancelOrderController = async (req, res) => { item.processquantity } ₹${ - item.price + (item.price).toFixed(2) } ₹${ - (item.price * item.GST) / 100 + ((item.price * item.GST) / 100).toFixed(2) } ₹${ - itemSubtotal + itemGST + (itemSubtotal + itemGST).toFixed(2) } `; @@ -468,9 +477,9 @@ export const cancelOrderController = async (req, res) => { if (Array.isArray(order.orderItem)) { order.orderItem.forEach((item) => { if (item.remainingQuantity > 0) { - const itemSubtotal = item.price * item.remainingQuantity; + const itemSubtotal = i(tem.price * item.remainingQuantity).toFixed(2); const itemGST = - ((item.price * item.GST) / 100) * item.remainingQuantity; + (((item.price * item.GST) / 100) * item.remainingQuantity).toFixed(2); totalCancelAmount += itemSubtotal + itemGST; @@ -492,13 +501,13 @@ export const cancelOrderController = async (req, res) => { item.remainingQuantity } ₹${ - item.price + (item.price).toFixed(2) } ₹${ - (item.price * item.GST) / 100 + ((item.price * item.GST) / 100).toFixed(2) } ₹${ - itemSubtotal + itemGST + (itemSubtotal + itemGST).toFixed(2) } `; @@ -968,7 +977,7 @@ export const updateCourierStatusToDispatched = async (req, res) => { product.processquantity } ₹${ - product.price + (product.price).toFixed(2) } ₹${( (product.GST * product.price) / @@ -985,7 +994,7 @@ export const updateCourierStatusToDispatched = async (req, res) => { Total Amount: ₹${ - invoice.invoiceAmount + (invoice.invoiceAmount).toFixed(2) } @@ -1181,7 +1190,7 @@ export const updateCourierStatusToDelivered = async (req, res) => { product.processquantity } ₹${ - product.price + (product.price).toFixed(2) } ₹${( (product.GST * product.price) / @@ -1490,13 +1499,11 @@ export const gettotalorderandvalueofpd = async (req, res) => { // Get the date of the last order const lastPurchaseOrderDate = totalOrders > 0 ? orders[0].createdAt : null; - res - .status(200) - .json({ - totalOrders, - totalValue: parseFloat(totalValue), - lastPurchaseOrderDate, - }); + res.status(200).json({ + totalOrders, + totalValue: parseFloat(totalValue), + lastPurchaseOrderDate, + }); } catch (error) { console.error("Error fetching orders:", error); res.status(500).json({ message: "Server error", error }); diff --git a/resources/Reports/OpeningInventoryReports.js b/resources/Reports/OpeningInventoryReports.js index 6317a17..05e521d 100644 --- a/resources/Reports/OpeningInventoryReports.js +++ b/resources/Reports/OpeningInventoryReports.js @@ -80,9 +80,11 @@ export const getProductsWithOpenInventoryInfo = async (req, res) => { name: product.name, brand: product.brand?.brandName || "N/A", // Access brandName here category: product.category?.categoryName || "N/A", // Access categoryName here - allPDs: pdMap[product.SKU] || 0, - allRDs: rdMap[product.SKU] || 0, - allPdAndRd: (pdMap[product.SKU] || 0) + (rdMap[product.SKU] || 0), + allPDs: (pdMap[product.SKU] || 0).toFixed(2), + allRDs: (rdMap[product.SKU] || 0).toFixed(2), + allPdAndRd: ( + (pdMap[product.SKU] || 0) + (rdMap[product.SKU] || 0) + ).toFixed(2), })); // Step 7: Get total count for pagination @@ -179,9 +181,11 @@ export const getProductsWithStockInfo = async (req, res) => { name: product.name, brand: product.brand?.brandName || "N/A", // Access brandName here category: product.category?.categoryName || "N/A", // Access categoryName here - allPDs: pdMap[product.SKU] || 0, - allRDs: rdMap[product.SKU] || 0, - allPdAndRd: (pdMap[product.SKU] || 0) + (rdMap[product.SKU] || 0), + allPDs: (pdMap[product.SKU] || 0).toFixed(2), + allRDs: (rdMap[product.SKU] || 0).toFixed(2), + allPdAndRd: ( + (pdMap[product.SKU] || 0) + (rdMap[product.SKU] || 0) + ).toFixed(2), })); // Step 7: Get total count for pagination @@ -261,24 +265,37 @@ export const DownloadProductsWithOpenInventoryInfo = async (req, res) => { // Combine product info with PD/RD counts using SKU const productData = products.map((product) => ({ "SKU Code": product.SKU, - "SKU Description": product.name, // SKU Description - "Category Name": product.category?.categoryName || "N/A", // Category Name - "Brand Name": product.brand?.brandName || "N/A", // Brand Name - "Total At PDs And Retailers": (pdMap[product.SKU] || 0) + (rdMap[product.SKU] || 0), // Total At PDs & Retailers - "All PDs": pdMap[product.SKU] || 0, // All PDs - "All Retailers": rdMap[product.SKU] || 0, // All Retailers + "SKU Description": product.name, // SKU Description + "Category Name": product.category?.categoryName || "N/A", // Category Name + "Brand Name": product.brand?.brandName || "N/A", // Brand Name + "Total At PDs And Retailers": ( + (pdMap[product.SKU] || 0) + (rdMap[product.SKU] || 0) + ).toFixed(2), // Total At PDs & Retailers + "All PDs": (pdMap[product.SKU] || 0).toFixed(2), // All PDs + "All Retailers": (rdMap[product.SKU] || 0).toFixed(2), // All Retailers })); // Define headers (first row in the sheet) const headers = [ - ["SKU Code", "SKU Description", "Category Name", "Brand Name", "Total At PDs And Retailers", "All PDs", "All Retailers"] + [ + "SKU Code", + "SKU Description", + "Category Name", + "Brand Name", + "Total At PDs And Retailers", + "All PDs", + "All Retailers", + ], ]; // Create worksheet with headers const worksheet = XLSX.utils.aoa_to_sheet(headers); // Append product data below headers - XLSX.utils.sheet_add_json(worksheet, productData, { skipHeader: true, origin: "A2" }); + XLSX.utils.sheet_add_json(worksheet, productData, { + skipHeader: true, + origin: "A2", + }); // Calculate and set column widths based on content const columnWidths = headers[0].map((header, index) => { @@ -289,20 +306,28 @@ export const DownloadProductsWithOpenInventoryInfo = async (req, res) => { } return { wch: maxLength + 2 }; // Add some padding }); - worksheet['!cols'] = columnWidths; + worksheet["!cols"] = columnWidths; // Create workbook and append worksheet const workbook = XLSX.utils.book_new(); XLSX.utils.book_append_sheet(workbook, worksheet, "OpeningInventoryReport"); // Write workbook to buffer - const excelBuffer = XLSX.write(workbook, { bookType: "xlsx", type: "buffer" }); + const excelBuffer = XLSX.write(workbook, { + bookType: "xlsx", + type: "buffer", + }); // Send Excel file as response - res.setHeader("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); - res.setHeader("Content-Disposition", "attachment; filename=OpeningInventoryReport.xlsx"); + res.setHeader( + "Content-Type", + "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" + ); + res.setHeader( + "Content-Disposition", + "attachment; filename=OpeningInventoryReport.xlsx" + ); res.send(excelBuffer); - } catch (error) { res.status(500).json({ success: false, message: error.message }); } @@ -367,24 +392,37 @@ export const DownloadProductsWithStockInfo = async (req, res) => { // Combine product info with PD/RD counts using SKU const productData = products.map((product) => ({ "SKU Code": product.SKU, - "SKU Description": product.name, // SKU Description - "Category Name": product.category?.categoryName || "N/A", // Category Name - "Brand Name": product.brand?.brandName || "N/A", // Brand Name - "Total At PDs And Retailers": (pdMap[product.SKU] || 0) + (rdMap[product.SKU] || 0), // Total At PDs & Retailers - "All PDs": pdMap[product.SKU] || 0, // All PDs - "All Retailers": rdMap[product.SKU] || 0, // All Retailers + "SKU Description": product.name, // SKU Description + "Category Name": product.category?.categoryName || "N/A", // Category Name + "Brand Name": product.brand?.brandName || "N/A", // Brand Name + "Total At PDs And Retailers": ( + (pdMap[product.SKU] || 0) + (rdMap[product.SKU] || 0) + ).toFixed(2), // Total At PDs & Retailers + "All PDs": (pdMap[product.SKU] || 0).toFixed(2), // All PDs + "All Retailers": (rdMap[product.SKU] || 0).toFixed(2), // All Retailers })); // Define headers (first row in the sheet) const headers = [ - ["SKU Code", "SKU Description", "Category Name", "Brand Name", "Total At PDs And Retailers", "All PDs", "All Retailers"] + [ + "SKU Code", + "SKU Description", + "Category Name", + "Brand Name", + "Total At PDs And Retailers", + "All PDs", + "All Retailers", + ], ]; // Create worksheet with headers const worksheet = XLSX.utils.aoa_to_sheet(headers); // Append product data below headers - XLSX.utils.sheet_add_json(worksheet, productData, { skipHeader: true, origin: "A2" }); + XLSX.utils.sheet_add_json(worksheet, productData, { + skipHeader: true, + origin: "A2", + }); // Calculate and set column widths based on content const columnWidths = headers[0].map((header, index) => { @@ -395,20 +433,28 @@ export const DownloadProductsWithStockInfo = async (req, res) => { } return { wch: maxLength + 2 }; // Add some padding }); - worksheet['!cols'] = columnWidths; + worksheet["!cols"] = columnWidths; // Create workbook and append worksheet const workbook = XLSX.utils.book_new(); XLSX.utils.book_append_sheet(workbook, worksheet, "StockReport"); // Write workbook to buffer - const excelBuffer = XLSX.write(workbook, { bookType: "xlsx", type: "buffer" }); + const excelBuffer = XLSX.write(workbook, { + bookType: "xlsx", + type: "buffer", + }); // Send Excel file as response - res.setHeader("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); - res.setHeader("Content-Disposition", "attachment; filename=StockReport.xlsx"); + res.setHeader( + "Content-Type", + "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" + ); + res.setHeader( + "Content-Disposition", + "attachment; filename=StockReport.xlsx" + ); res.send(excelBuffer); - } catch (error) { res.status(500).json({ success: false, message: error.message }); }