feat: автосменя статуса при сохранении отгрузки
- Все позиции отгружены → delivery_status = 'delivered' - Частичная отгрузка (есть неотгруженные) → delivery_status = 'problem' - Без сохранения отгрузки — статус не меняется
This commit is contained in:
parent
b888eba09b
commit
c288251cec
|
|
@ -394,19 +394,33 @@ export const saveShipmentData = async ({ orderGroupId, shipmentData }) => {
|
||||||
return safeSupabaseCall(async () => {
|
return safeSupabaseCall(async () => {
|
||||||
const client = requireSupabase();
|
const client = requireSupabase();
|
||||||
const unshipped = shipmentData.filter((i) => !i.shipped);
|
const unshipped = shipmentData.filter((i) => !i.shipped);
|
||||||
|
const allShipped = unshipped.length === 0 && shipmentData.length > 0;
|
||||||
const hasProblem = unshipped.length > 0;
|
const hasProblem = unshipped.length > 0;
|
||||||
const problemNote = hasProblem
|
const problemNote = hasProblem
|
||||||
? unshipped.map((i) => `${i.name}${i.quantity ? ` (${i.quantity}${i.unit ? ` ${i.unit}` : ""})` : ""}${i.comment ? ` — ${i.comment}` : ""}`).join("; ")
|
? unshipped.map((i) => `${i.name}${i.quantity ? ` (${i.quantity}${i.unit ? ` ${i.unit}` : ""})` : ""}${i.comment ? ` — ${i.comment}` : ""}`).join("; ")
|
||||||
: null;
|
: null;
|
||||||
|
|
||||||
|
// Auto-update delivery status based on shipment
|
||||||
|
let newDeliveryStatus = undefined;
|
||||||
|
if (allShipped) {
|
||||||
|
newDeliveryStatus = "delivered";
|
||||||
|
} else if (hasProblem) {
|
||||||
|
newDeliveryStatus = "problem";
|
||||||
|
}
|
||||||
|
|
||||||
|
const updatePayload = {
|
||||||
|
driver_shipment_data: shipmentData,
|
||||||
|
has_delivery_problem: hasProblem,
|
||||||
|
delivery_problem_note: problemNote,
|
||||||
|
updated_at: new Date().toISOString(),
|
||||||
|
};
|
||||||
|
if (newDeliveryStatus) {
|
||||||
|
updatePayload.delivery_status = newDeliveryStatus;
|
||||||
|
}
|
||||||
|
|
||||||
const { error: updateError } = await client
|
const { error: updateError } = await client
|
||||||
.from("order_groups")
|
.from("order_groups")
|
||||||
.update({
|
.update(updatePayload)
|
||||||
driver_shipment_data: shipmentData,
|
|
||||||
has_delivery_problem: hasProblem,
|
|
||||||
delivery_problem_note: problemNote,
|
|
||||||
updated_at: new Date().toISOString(),
|
|
||||||
})
|
|
||||||
.eq("id", orderGroupId);
|
.eq("id", orderGroupId);
|
||||||
|
|
||||||
if (updateError) throw updateError;
|
if (updateError) throw updateError;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue