|
|
@ -2131,3 +2131,67 @@ exports.updateadvanceForSupplier = async (req, reply) => {
|
|
|
|
throw boom.boomify(err);
|
|
|
|
throw boom.boomify(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.splitBookingForSupplier = async (req, reply) => {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
const { id } = req.params;
|
|
|
|
|
|
|
|
const { capacity, quantity, date, time, quoted_amount } = req.body;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 1) Find 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`;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
await existing.save();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 3) Create new bookings for the rest
|
|
|
|
|
|
|
|
const newBookings = [];
|
|
|
|
|
|
|
|
for (let i = 1; i < quantity; i++) {
|
|
|
|
|
|
|
|
const newBooking = new RequestedBooking({
|
|
|
|
|
|
|
|
status: "pending",
|
|
|
|
|
|
|
|
customerId: existing.customerId,
|
|
|
|
|
|
|
|
type_of_water: existing.type_of_water,
|
|
|
|
|
|
|
|
capacity: `${capacity} L`,
|
|
|
|
|
|
|
|
quantity: "1",
|
|
|
|
|
|
|
|
total_required_capacity: capacity,
|
|
|
|
|
|
|
|
date: date || existing.date,
|
|
|
|
|
|
|
|
time: 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,
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
],
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
newBookings.push(newBooking);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (newBookings.length > 0) {
|
|
|
|
|
|
|
|
await RequestedBooking.insertMany(newBookings);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return reply.code(200).send({
|
|
|
|
|
|
|
|
status_code: 200,
|
|
|
|
|
|
|
|
message: `${quantity} booking(s) created/updated successfully`,
|
|
|
|
|
|
|
|
updated: existing,
|
|
|
|
|
|
|
|
newEntries: newBookings,
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
|
|
console.error("splitBookingForSupplier error:", err);
|
|
|
|
|
|
|
|
return reply.code(500).send({ status_code: 500, message: "Internal Server Error" });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|