image for orders and rd order show in admin

This commit is contained in:
Sibunnayak 2025-02-07 18:03:54 +05:30
parent 8c23775362
commit 2d4550f566
4 changed files with 90 additions and 10 deletions

View File

@ -34,6 +34,12 @@ const orderItemSchema = new Schema({
type: Number, type: Number,
required: true, required: true,
}, },
image: [
{
public_id: String,
url: String,
},
],
processquantity: { processquantity: {
//updated quantity //updated quantity
type: Number, type: Number,

View File

@ -51,7 +51,7 @@ export const createOrder = async (req, res) => {
GST: item.GST, GST: item.GST,
HSN_Code: item.HSN_Code, HSN_Code: item.HSN_Code,
description: item.description, description: item.description,
image: item.image, image: item.brand?.image?.length > 0 ? item.brand.image[0] : item.image,
quantity: item.count, quantity: item.count,
remainingQuantity: item.count, remainingQuantity: item.count,
})), })),
@ -179,10 +179,12 @@ export const processOrder = async (req, res) => {
// Calculate total amount for pending items // Calculate total amount for pending items
let pendingTotalAmount = 0; let pendingTotalAmount = 0;
let itemPendingSubtotal = 0;
let itemPendingGST = 0;
order.orderItem.forEach((item) => { order.orderItem.forEach((item) => {
if (item.remainingQuantity > 0) { if (item.remainingQuantity > 0) {
const itemPendingSubtotal = item.price * item.remainingQuantity; itemPendingSubtotal = item.price * item.remainingQuantity;
const itemPendingGST = itemPendingGST =
((item.price * item.GST) / 100) * item.remainingQuantity; ((item.price * item.GST) / 100) * item.remainingQuantity;
pendingTotalAmount += itemPendingSubtotal + itemPendingGST; pendingTotalAmount += itemPendingSubtotal + itemPendingGST;
} }

View File

@ -169,13 +169,15 @@ export const getPlacedOrderById = async (req, res) => {
.json({ return_msg: "Not Valid id to search the doc " }); .json({ return_msg: "Not Valid id to search the doc " });
} }
const doc = await RdOrder.findById(id) const doc = await RdOrder.findById(id)
.populate({ // .populate({
path: "orderItem.productId", // path: "orderItem.productId",
populate: { // populate: {
path: "brand", // path: "brand",
}, // },
}) // })
.populate({ path: "invoices" }); .populate({ path: "invoices" })
.populate({ path: "addedBy" })
.populate({ path: "pd" });
if (doc) { if (doc) {
return res return res
.status(200) .status(200)
@ -1621,6 +1623,72 @@ export const getAllOrdersByDistributor = async (req, res) => {
res.status(500).json({ message: "Server error", error }); 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) => { export const gettotalorderandvalueofrd = async (req, res) => {
const { distributorId } = req.params; const { distributorId } = req.params;

View File

@ -3,6 +3,7 @@ import {
cancelOrderController, cancelOrderController,
createOrderRD, createOrderRD,
getAllOrdersByDistributor, getAllOrdersByDistributor,
getAllOrdersforAdmin,
getCancelledOrders, getCancelledOrders,
getDeliveredInvoicesForPd, getDeliveredInvoicesForPd,
getDispatchedInvoicesForPd, getDispatchedInvoicesForPd,
@ -38,6 +39,9 @@ router
router router
.route("/single-rd-order/:distributorId") .route("/single-rd-order/:distributorId")
.get(isAuthenticatedUser, authorizeRoles("admin"), getAllOrdersByDistributor); .get(isAuthenticatedUser, authorizeRoles("admin"), getAllOrdersByDistributor);
router
.route("/rd-order")
.get(isAuthenticatedUser, authorizeRoles("admin"), getAllOrdersforAdmin);
// routes for the PD // routes for the PD
router router
.route("/pd-get-all-place-order") .route("/pd-get-all-place-order")