api/resources/RD_Ordes/rdOrderController.js
2024-09-16 12:54:57 +05:30

51 lines
1.2 KiB
JavaScript

import { RetailDistributor } from "../models/RetailDistributor";
import { RdOrder } from "../models/RdOrder";
// Controller to create a new order by RD
export const createOrderRD = async (req, res) => {
try {
const {
rdId,
paymentMode,
shipTo,
billTo,
orderItem,
subtotal,
gstTotal,
grandTotal,
} = req.body;
// 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,
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 });
}
};