|
|
|
@ -2136,43 +2136,49 @@ exports.updateadvanceForSupplier = async (req, reply) => {
|
|
|
|
|
exports.splitBookingForSupplier = async (req, reply) => {
|
|
|
|
|
try {
|
|
|
|
|
const { id } = req.params;
|
|
|
|
|
const { capacity, quantity, date, time, quoted_amount } = req.body;
|
|
|
|
|
const { splits } = req.body;
|
|
|
|
|
|
|
|
|
|
// 1) Find existing booking
|
|
|
|
|
if (!Array.isArray(splits) || splits.length === 0) {
|
|
|
|
|
return reply.code(400).send({ status_code: 400, message: "splits array is required" });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 1) Find the existing booking
|
|
|
|
|
const existing = await RequestedBooking.findById(id);
|
|
|
|
|
if (!existing) {
|
|
|
|
|
return reply.code(404).send({ status_code: 404, message: "Booking not found" });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 2) Update the original booking
|
|
|
|
|
existing.capacity = `${capacity} L`;
|
|
|
|
|
// 2) Update the original booking with the first split
|
|
|
|
|
const first = splits[0];
|
|
|
|
|
existing.capacity = `${first.capacity} L`;
|
|
|
|
|
existing.quantity = "1";
|
|
|
|
|
existing.total_required_capacity = capacity;
|
|
|
|
|
if (date) existing.date = date;
|
|
|
|
|
if (time) existing.time = time;
|
|
|
|
|
if (quoted_amount) {
|
|
|
|
|
existing.requested_suppliers[0].quoted_amount = quoted_amount;
|
|
|
|
|
existing.total_required_capacity = first.capacity;
|
|
|
|
|
if (first.date) existing.date = first.date;
|
|
|
|
|
if (first.time) existing.time = first.time;
|
|
|
|
|
if (first.quoted_amount) {
|
|
|
|
|
existing.requested_suppliers[0].quoted_amount = first.quoted_amount;
|
|
|
|
|
}
|
|
|
|
|
await existing.save();
|
|
|
|
|
|
|
|
|
|
// 3) Create new bookings for the rest
|
|
|
|
|
// 3) Create new bookings for remaining splits
|
|
|
|
|
const newBookings = [];
|
|
|
|
|
for (let i = 1; i < quantity; i++) {
|
|
|
|
|
for (let i = 1; i < splits.length; i++) {
|
|
|
|
|
const s = splits[i];
|
|
|
|
|
const newBooking = new RequestedBooking({
|
|
|
|
|
status: "pending",
|
|
|
|
|
customerId: existing.customerId,
|
|
|
|
|
type_of_water: existing.type_of_water,
|
|
|
|
|
capacity: `${capacity} L`,
|
|
|
|
|
capacity: `${s.capacity} L`,
|
|
|
|
|
quantity: "1",
|
|
|
|
|
total_required_capacity: capacity,
|
|
|
|
|
date: date || existing.date,
|
|
|
|
|
time: time || existing.time,
|
|
|
|
|
total_required_capacity: s.capacity,
|
|
|
|
|
date: s.date || existing.date,
|
|
|
|
|
time: s.time || existing.time,
|
|
|
|
|
requested_suppliers: [
|
|
|
|
|
{
|
|
|
|
|
time: new Date().toISOString().slice(0, 16).replace("T", " "),
|
|
|
|
|
status: "pending",
|
|
|
|
|
supplierId: existing.requested_suppliers[0].supplierId,
|
|
|
|
|
quoted_amount: quoted_amount || existing.requested_suppliers[0].quoted_amount,
|
|
|
|
|
quoted_amount: s.quoted_amount || existing.requested_suppliers[0].quoted_amount,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
});
|
|
|
|
@ -2185,7 +2191,7 @@ exports.splitBookingForSupplier = async (req, reply) => {
|
|
|
|
|
|
|
|
|
|
return reply.code(200).send({
|
|
|
|
|
status_code: 200,
|
|
|
|
|
message: `${quantity} booking(s) created/updated successfully`,
|
|
|
|
|
message: `${splits.length} booking(s) created/updated successfully`,
|
|
|
|
|
updated: existing,
|
|
|
|
|
newEntries: newBookings,
|
|
|
|
|
});
|
|
|
|
@ -2195,3 +2201,4 @@ exports.splitBookingForSupplier = async (req, reply) => {
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|