sozsoft-platform/configs/seeds/postgres/object/Inv_P_TransferAfterSave.sql
2026-10-13 00:43:05 +03:00

52 lines
3.6 KiB
PL/PgSQL
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

-- Transfer belgesi kaydı (ve iş akışı adımları) sonrası: tür, şube, depo ve boş konumlar operasyon türünden gelir (formda olmayan alımın
-- kaynağı ve teslimatın hedefi her zaman, iade belgesi hariç); teslimat politikası
-- yalnız teslimatta tutulur; iç transferin onaycısı kaynak konumun depo sorumlusudur (kullanıcı adı; iş akışı onaycıyı adla eşler), sorumlusu olmayan konumdan transfer onay beklemez.
-- Numara, özet ve durum butonunun işlemi (PendingAction) buradan çalışır.
CREATE OR REPLACE PROCEDURE public."Inv_P_TransferAfterSave"(p_id uuid, p_user_id uuid DEFAULT NULL)
LANGUAGE plpgsql AS $$
DECLARE v_act varchar(16);
BEGIN
SELECT "PendingAction" INTO v_act FROM "Inv_T_Transfer" WHERE "Id" = p_id;
UPDATE "Inv_T_Transfer" t SET
"OperationKind" = ot."OperationKind",
"BranchId" = ot."BranchId",
"WarehouseId" = ot."WarehouseId",
"SourceLocationId" = CASE WHEN t."ReturnOfId" IS NULL AND (ot."OperationKind" = 'Receipt' OR t."SourceLocationId" IS NULL)
THEN COALESCE(ot."SourceLocationId", t."SourceLocationId") ELSE t."SourceLocationId" END,
"DestinationLocationId" = CASE WHEN t."ReturnOfId" IS NULL AND (ot."OperationKind" = 'Delivery' OR t."DestinationLocationId" IS NULL)
THEN COALESCE(ot."DestinationLocationId", t."DestinationLocationId") ELSE t."DestinationLocationId" END,
"DeliveryPolicy" = CASE WHEN ot."OperationKind" = 'Delivery' THEN COALESCE(t."DeliveryPolicy", 'Direct') END
FROM "Inv_T_OperationType" ot
WHERE t."Id" = p_id AND ot."Id" = t."OperationTypeId";
UPDATE "Inv_T_Transfer" t SET
"SourceApproverUserNames" = a."UserName",
"ApprovalStatus" = CASE WHEN a."UserName" IS NULL AND t."ApprovalStatus" IS NULL THEN 'Onay Gerekmez'
WHEN a."UserName" IS NOT NULL AND t."ApprovalStatus" = 'Onay Gerekmez' THEN NULL
ELSE t."ApprovalStatus" END
FROM (SELECT x."Id", NULLIF(l."ResponsibleUserName", '') AS "UserName"
FROM "Inv_T_Transfer" x
LEFT JOIN "Inv_T_Location" l ON l."Id" = x."SourceLocationId"
WHERE x."Id" = p_id) a
WHERE t."Id" = a."Id" AND t."OperationKind" = 'InternalTransfer' AND t."Status" = 'Draft';
CALL public."Inv_P_TransferNumber"(p_id);
CALL public."Inv_P_TransferRecalc"(p_id);
-- Durum butonları işlemi PendingAction alanına yazıp ekranın güncelleme ucuyla kaydeder.
IF v_act IS NOT NULL THEN
CALL public."Inv_P_TransferSetStatus"(p_id, v_act, p_user_id);
-- Kaynak depo onayı verilen taslak iç transfer kendiliğinden hazırlanır (kullanıcı kararı: onay → Hazırla tek adım).
-- Hazırla'nın şartları (satır, farklı iki iç konum) sağlanmıyorsa denenmez: onay kaydı bozulmaz, belge taslak kalır ve
-- Hazırla elle basılınca hatayı gösterir.
ELSIF EXISTS (SELECT 1
FROM "Inv_T_Transfer" t
INNER JOIN "Inv_T_Location" s ON s."Id" = t."SourceLocationId" AND s."LocationType" = 'Internal'
INNER JOIN "Inv_T_Location" d ON d."Id" = t."DestinationLocationId" AND d."LocationType" = 'Internal'
WHERE t."Id" = p_id AND t."OperationKind" = 'InternalTransfer' AND t."Status" = 'Draft' AND t."ApprovalStatus" = 'Onaylandı'
AND s."Id" <> d."Id"
AND EXISTS (SELECT 1 FROM "Inv_T_TransferLine" l WHERE l."TransferId" = t."Id" AND l."IsDeleted" = false)) THEN
CALL public."Inv_P_TransferSetStatus"(p_id, 'Confirm', p_user_id);
END IF;
END;
$$;