rd order andpd order fixed

This commit is contained in:
Sibunnayak 2024-10-03 12:02:58 +05:30
parent 68f120e854
commit 3e7452a484
3 changed files with 29 additions and 4 deletions

View File

@ -1477,11 +1477,15 @@ export const getAllOrdersByDistributor = async (req, res) => {
export const gettotalorderandvalueofpd = async (req, res) => { export const gettotalorderandvalueofpd = async (req, res) => {
const { distributorId } = req.params; const { distributorId } = req.params;
try { try {
const orders = await PdOrder.find({ addedBy: distributorId }); const orders = await PdOrder.find({ addedBy: distributorId }).sort({ createdAt: -1 });
const totalOrders = orders.length; const totalOrders = orders.length;
const totalValue = orders.reduce((acc, order) => acc + order.grandTotal, 0); const totalValue = orders.reduce((acc, order) => acc + order.grandTotal, 0).toFixed(2);
// Get the date of the last order
const lastPurchaseOrderDate = totalOrders > 0 ? orders[0].createdAt : null;
res.status(200).json({ totalOrders, totalValue }); res.status(200).json({ totalOrders, totalValue: parseFloat(totalValue), lastPurchaseOrderDate });
} catch (error) { } catch (error) {
console.error("Error fetching orders:", error); console.error("Error fetching orders:", error);
res.status(500).json({ message: "Server error", error }); res.status(500).json({ message: "Server error", error });

View File

@ -1535,3 +1535,21 @@ export const getAllOrdersByDistributor = async (req, res) => {
res.status(500).json({ message: "Server error", error }); res.status(500).json({ message: "Server error", error });
} }
}; };
export const gettotalorderandvalueofrd = async (req, res) => {
const { distributorId } = req.params;
try {
const orders = await RdOrder.find({ addedBy: distributorId }).sort({ createdAt: -1 });
const totalOrders = orders.length;
const totalValue = orders.reduce((acc, order) => acc + order.grandTotal, 0).toFixed(2);
// Get the date of the last order
const lastPurchaseOrderDate = totalOrders > 0 ? orders[0].createdAt : null;
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 });
}
};

View File

@ -15,6 +15,7 @@ import {
getProcessingInvoicesForPd, getProcessingInvoicesForPd,
getSinglePlacedOrderForPD, getSinglePlacedOrderForPD,
getSinglePlacedOrderForRD, getSinglePlacedOrderForRD,
gettotalorderandvalueofrd,
processOrder, processOrder,
updateCourierStatusToDeliveredForPD, updateCourierStatusToDeliveredForPD,
updateCourierStatusToDispatchedForPD, updateCourierStatusToDispatchedForPD,
@ -80,5 +81,7 @@ router.route("/invoice/delivered/:invoiceId").put(
updateCourierStatusToDeliveredForPD updateCourierStatusToDeliveredForPD
); );
router
.route("/single-rd-ordercount/:distributorId")
.get(isAuthenticatedUser, authorizeRoles("admin"), gettotalorderandvalueofrd);
export default router; export default router;