From 2d4550f5661d0cae817d5e7f82c36343115e2b13 Mon Sep 17 00:00:00 2001 From: Sibunnayak Date: Fri, 7 Feb 2025 18:03:54 +0530 Subject: [PATCH] image for orders and rd order show in admin --- resources/PD_Orders/invoiceModel.js | 6 ++ resources/PD_Orders/pdOrderController.js | 8 ++- resources/RD_Ordes/rdOrderController.js | 82 ++++++++++++++++++++++-- resources/RD_Ordes/rdOrderRoutes.js | 4 ++ 4 files changed, 90 insertions(+), 10 deletions(-) diff --git a/resources/PD_Orders/invoiceModel.js b/resources/PD_Orders/invoiceModel.js index 3e54afc..6f3af7a 100644 --- a/resources/PD_Orders/invoiceModel.js +++ b/resources/PD_Orders/invoiceModel.js @@ -34,6 +34,12 @@ const orderItemSchema = new Schema({ type: Number, required: true, }, + image: [ + { + public_id: String, + url: String, + }, + ], processquantity: { //updated quantity type: Number, diff --git a/resources/PD_Orders/pdOrderController.js b/resources/PD_Orders/pdOrderController.js index 40ce974..e08b585 100644 --- a/resources/PD_Orders/pdOrderController.js +++ b/resources/PD_Orders/pdOrderController.js @@ -51,7 +51,7 @@ export const createOrder = async (req, res) => { GST: item.GST, HSN_Code: item.HSN_Code, description: item.description, - image: item.image, + image: item.brand?.image?.length > 0 ? item.brand.image[0] : item.image, quantity: item.count, remainingQuantity: item.count, })), @@ -179,10 +179,12 @@ export const processOrder = async (req, res) => { // Calculate total amount for pending items let pendingTotalAmount = 0; + let itemPendingSubtotal = 0; + let itemPendingGST = 0; order.orderItem.forEach((item) => { if (item.remainingQuantity > 0) { - const itemPendingSubtotal = item.price * item.remainingQuantity; - const itemPendingGST = + itemPendingSubtotal = item.price * item.remainingQuantity; + itemPendingGST = ((item.price * item.GST) / 100) * item.remainingQuantity; pendingTotalAmount += itemPendingSubtotal + itemPendingGST; } diff --git a/resources/RD_Ordes/rdOrderController.js b/resources/RD_Ordes/rdOrderController.js index de447da..71a221a 100644 --- a/resources/RD_Ordes/rdOrderController.js +++ b/resources/RD_Ordes/rdOrderController.js @@ -169,13 +169,15 @@ export const getPlacedOrderById = async (req, res) => { .json({ return_msg: "Not Valid id to search the doc " }); } const doc = await RdOrder.findById(id) - .populate({ - path: "orderItem.productId", - populate: { - path: "brand", - }, - }) - .populate({ path: "invoices" }); + // .populate({ + // path: "orderItem.productId", + // populate: { + // path: "brand", + // }, + // }) + .populate({ path: "invoices" }) + .populate({ path: "addedBy" }) + .populate({ path: "pd" }); if (doc) { return res .status(200) @@ -1621,6 +1623,72 @@ export const getAllOrdersByDistributor = async (req, res) => { res.status(500).json({ message: "Server error", error }); } }; +export const getAllOrdersforAdmin = async (req, res) => { + const { searchText, searchField } = req.query; + // console.log(searchText, searchField); + // Handle pagination parameters + const page = parseInt(req.query.page, 10) || 1; + const limit = parseInt(req.query.limit, 10) || 5; + const skip = (page - 1) * limit; + + try { + let filter = {}; // Initialize query filter + + // Search by orderId (case-insensitive) + if (searchField == "Order ID") { + filter.uniqueId = { $regex: new RegExp(searchText, "i") }; + } + + // Search by status (case-insensitive) + if (searchField == "Status") { + filter.status = { $regex: new RegExp(searchText, "i") }; + } + + // Search by retailer (case-insensitive, populating addedBy) + if (searchField == "retailer") { + const retailerRegex = new RegExp(searchText, "i"); + const retailerUsers = await RetailDistributor.find( + { name: { $regex: retailerRegex } }, + "_id" + ); // Find matching users + const retailerIds = retailerUsers.map((user) => user._id); + + if (retailerIds.length > 0) { + filter.addedBy = { $in: retailerIds }; // Filter orders by found user IDs + } else { + return res + .status(200) + .json({ + totalOrders: 0, + totalPages: 0, + currentPage: page, + orders: [], + }); + } + } + + // Fetch orders with population + const orders = await RdOrder.find(filter) + .populate({ path: "addedBy", select: "name email" }) // Populate retailer details + .sort({ createdAt: -1 }) + .skip(skip) + .limit(limit); + + // Get total count for pagination + const totalOrders = await RdOrder.countDocuments(filter); + + // Send response with pagination info + return res.status(200).json({ + totalOrders, + totalPages: Math.ceil(totalOrders / limit), + currentPage: page, + orders, + }); + } catch (error) { + console.error("Error fetching orders:", error); + res.status(500).json({ message: "Server error", error }); + } +}; export const gettotalorderandvalueofrd = async (req, res) => { const { distributorId } = req.params; diff --git a/resources/RD_Ordes/rdOrderRoutes.js b/resources/RD_Ordes/rdOrderRoutes.js index 7ac11d2..0cf243c 100644 --- a/resources/RD_Ordes/rdOrderRoutes.js +++ b/resources/RD_Ordes/rdOrderRoutes.js @@ -3,6 +3,7 @@ import { cancelOrderController, createOrderRD, getAllOrdersByDistributor, + getAllOrdersforAdmin, getCancelledOrders, getDeliveredInvoicesForPd, getDispatchedInvoicesForPd, @@ -38,6 +39,9 @@ router router .route("/single-rd-order/:distributorId") .get(isAuthenticatedUser, authorizeRoles("admin"), getAllOrdersByDistributor); + router + .route("/rd-order") + .get(isAuthenticatedUser, authorizeRoles("admin"), getAllOrdersforAdmin); // routes for the PD router .route("/pd-get-all-place-order")