image for orders and rd order show in admin
This commit is contained in:
parent
8c23775362
commit
2d4550f566
@ -34,6 +34,12 @@ const orderItemSchema = new Schema({
|
||||
type: Number,
|
||||
required: true,
|
||||
},
|
||||
image: [
|
||||
{
|
||||
public_id: String,
|
||||
url: String,
|
||||
},
|
||||
],
|
||||
processquantity: {
|
||||
//updated quantity
|
||||
type: Number,
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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")
|
||||
|
Loading…
Reference in New Issue
Block a user