195 lines
6.0 KiB
JavaScript
195 lines
6.0 KiB
JavaScript
import RetailDistributor from "../RetailDistributor/RetailDistributorModel.js";
|
|
import { RdOrder } from "./rdOrderModal.js";
|
|
|
|
// Controller to create a new order by RD
|
|
export const createOrderRD = async (req, res) => {
|
|
try {
|
|
const {
|
|
paymentMode,
|
|
shipTo,
|
|
billTo,
|
|
orderItems,
|
|
subtotal,
|
|
gstTotal,
|
|
grandTotal,
|
|
} = req.body;
|
|
|
|
const rdId = req.user._id;
|
|
// Fetch the Retail Distributor (RD) to find the associated Principal Distributor (PD)
|
|
const rd = await RetailDistributor.findById(rdId).populate(
|
|
"principal_distributer"
|
|
);
|
|
|
|
if (!rd) {
|
|
return res.status(404).json({ message: "Retail Distributor not found" });
|
|
}
|
|
|
|
const pdId = rd.principal_distributer._id; // Get the associated PD
|
|
|
|
// Create the order
|
|
const newOrder = new RdOrder({
|
|
paymentMode,
|
|
shipTo,
|
|
billTo,
|
|
orderItem: orderItems.map((item) => ({
|
|
productId: item._id,
|
|
SKU: item.SKU,
|
|
name: item.name,
|
|
categoryName: item.category.categoryName, // Store category name
|
|
|
|
brandName: item.brand.brandName, // Store brand name
|
|
|
|
price: item.price,
|
|
GST: item.GST,
|
|
HSN_Code: item.HSN_Code,
|
|
description: item.description,
|
|
image: item.image,
|
|
quantity: item.count,
|
|
})),
|
|
subtotal,
|
|
gstTotal,
|
|
grandTotal,
|
|
addedBy: rdId, // The RD who placed the order (Retail Distributor)
|
|
pd: pdId, // Reference to the PD associated with the RD
|
|
});
|
|
|
|
await newOrder.save();
|
|
|
|
res
|
|
.status(201)
|
|
.json({ message: "Order placed successfully", order: newOrder });
|
|
} catch (error) {
|
|
res.status(500).json({ message: "Server error", error });
|
|
}
|
|
};
|
|
export const getPlacedOrdersForRD = async (req, res) => {
|
|
try {
|
|
const rdId = req.user?._id; // Assuming the Retail Distributor's ID is obtained from the authenticated request
|
|
if (!rdId) {
|
|
return res.status(401).json({ message: "Unauthorized access" });
|
|
}
|
|
|
|
// Extract page and limit from query parameters, with default values
|
|
const page = parseInt(req.query.page, 10) || 1;
|
|
const limit = parseInt(req.query.limit, 10) || 5;
|
|
|
|
// Calculate how many documents to skip for pagination
|
|
const skip = (page - 1) * limit;
|
|
|
|
// Fetch total count of orders for this RD (for pagination purposes)
|
|
const totalOrders = await RdOrder.countDocuments({ addedBy: rdId });
|
|
|
|
// Fetch orders for the logged-in RD
|
|
const placedOrders = await RdOrder.find({ addedBy: rdId })
|
|
.sort({ createdAt: -1 }) // Sort by creation date, newest first
|
|
.skip(skip) // Skip documents for pagination
|
|
.limit(limit); // Limit number of documents returned
|
|
|
|
if (!placedOrders || placedOrders.length === 0) {
|
|
return res
|
|
.status(404)
|
|
.json({ message: "No orders found for this Retail Distributor" });
|
|
}
|
|
|
|
// Send the paginated order list and total count of orders
|
|
res.status(200).json({ placedOrders, totalOrders });
|
|
} catch (error) {
|
|
console.error(error);
|
|
res.status(500).json({ message: "Server error", error });
|
|
}
|
|
};
|
|
export const getSinglePlacedOrderForRD = async (req, res) => {
|
|
try {
|
|
const rdId = req.user?._id;
|
|
if (!rdId) {
|
|
return res.status(401).json({ message: "Unauthorized access" });
|
|
} // Assuming the Retail Distributor's ID is obtained from the authenticated request
|
|
const orderId = req.params.id; // Assuming the order ID is passed in the URL as a parameter
|
|
|
|
if (!rdId) {
|
|
return res.status(401).json({ message: "Unauthorized access" });
|
|
}
|
|
|
|
if (!orderId) {
|
|
return res.status(400).json({ message: "Order ID is required" });
|
|
}
|
|
|
|
// Fetch the specific order for the logged-in RD
|
|
const order = await RdOrder.findOne({ _id: orderId, addedBy: rdId });
|
|
|
|
if (!order) {
|
|
return res
|
|
.status(404)
|
|
.json({ message: "Order not found for this Retail Distributor" });
|
|
}
|
|
|
|
// Send the single order document
|
|
res.status(200).json({ singleOrder: order });
|
|
} catch (error) {
|
|
console.error(error);
|
|
res.status(500).json({ message: "Server error", error });
|
|
}
|
|
};
|
|
|
|
export const getPlacedOrdersForPD = async (req, res) => {
|
|
try {
|
|
const pdId = req.user?._id;
|
|
if (!pdId) {
|
|
return res.status(401).json({ return_message: "Unauthorized access " });
|
|
}
|
|
// Extract page and limit from query parameters
|
|
const page = parseInt(req.query.page, 10) || 1;
|
|
const limit = parseInt(req.query.limit, 10) || 5;
|
|
|
|
// Calculate the number of documents to skip
|
|
const skip = (page - 1) * limit;
|
|
const totalOrders = await RdOrder.countDocuments({ pd: pdId });
|
|
|
|
// Fetch all orders where the PD is associated with the order
|
|
const plcaedOrders = await RdOrder.find({ pd: pdId })
|
|
.sort({ createdAt: -1 })
|
|
.skip(skip)
|
|
.limit(limit);
|
|
|
|
if (!plcaedOrders || plcaedOrders.length === 0) {
|
|
return res
|
|
.status(404)
|
|
.json({ message: "No orders found for this Principal Distributor" });
|
|
}
|
|
|
|
res.status(200).json({ plcaedOrders, totalOrders });
|
|
} catch (error) {
|
|
console.error(error);
|
|
res.status(500).json({ message: "Server error", error });
|
|
}
|
|
};
|
|
export const getSinglePlacedOrderForPD = async (req, res) => {
|
|
try {
|
|
const pdId = req.user?._id; // Assuming the Principal Distributor's ID is obtained from the authenticated request
|
|
const orderId = req.params.id; // Assuming the order ID is passed in the URL as a parameter
|
|
|
|
if (!pdId) {
|
|
return res.status(401).json({ message: "Unauthorized access" });
|
|
}
|
|
|
|
if (!orderId) {
|
|
return res.status(400).json({ message: "Order ID is required" });
|
|
}
|
|
|
|
// Fetch the specific order for the logged-in PD
|
|
const order = await RdOrder.findOne({ _id: orderId, pd: pdId });
|
|
|
|
if (!order) {
|
|
return res
|
|
.status(404)
|
|
.json({ message: "Order not found for this Principal Distributor" });
|
|
}
|
|
|
|
// Send the single order document
|
|
res.status(200).json({ singleOrder: order });
|
|
} catch (error) {
|
|
console.error(error);
|
|
res.status(500).json({ message: "Server error", error });
|
|
}
|
|
};
|