sozsoft-platform/configs/seeds/postgres/object/Prc_V_MaterialRequestPlanSource.sql
2026-10-14 12:07:11 +03:00

34 lines
3.2 KiB
SQL
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.

-- Planla diyaloğunun İç Transfer ızgarası (App.Prc.MaterialRequestPlanSource) ve iç transfer / stoktan karşılama kararlarının stok kuralı:
-- talep kaleminin ürününün stoğu olan iç konumlar. Talebin teslim konumu da listelenir (IsDestination): oraya yazılan plan transfer açmaz,
-- stoktan karşılanır (PlanMethod = Stock). Miktarlar kalemin biriminde; ayrılan = açık (Taslak/Bekleyen/Hazır) transferlerde o
-- konumdan istenen, serbest = eldeki − ayrılan. Plan Miktarı kalemin bu konum için taslak planıdır. BaseId diyaloğun süzdüğü kalem, Id
-- kalem + ':' + konumdur (Prc_P_MaterialRequestPlanLineSave ayırır); taslak kaydı ve onay serbest stoğu buradan okur.
-- MaxQuantity satıra yazılabilecek en fazla plan miktarıdır: serbest stok ile planlanacak kalandan kalemin diğer taslak satırları
-- düşüldükten sonra kalanın küçüğü (ızgaranın doğrulaması; kayıt yordamı aynı kuralı yeniden denetler).
CREATE OR REPLACE VIEW public."Prc_V_MaterialRequestPlanSource" AS
SELECT CAST(i."Id" AS varchar(36)) || ':' || CAST(st."LocationId" AS varchar(36)) AS "Id", i."TenantId", i."Id" AS "BaseId",
l."BranchId", l."WarehouseId", st."LocationId", st."OnHand", COALESCE(rs."Quantity", 0) AS "ReservedQuantity",
st."OnHand" - COALESCE(rs."Quantity", 0) AS "FreeQuantity", i."UomId", pl."Quantity" AS "PlanQuantity", i."IsDeleted",
st."LocationId" = r."LocationId" AS "IsDestination",
GREATEST(0, LEAST(st."OnHand" - COALESCE(rs."Quantity", 0), ip."RemainingQuantity" - COALESCE(od."Quantity", 0))) AS "MaxQuantity"
FROM "Prc_T_MaterialRequestItem" i
INNER JOIN "Prc_T_MaterialRequest" r ON r."Id" = i."MaterialRequestId"
CROSS JOIN LATERAL (SELECT s."LocationId", SUM(public."Inv_F_UomConvert"(s."Quantity", s."UomId", i."UomId")) AS "OnHand"
FROM "Inv_T_Stock" s
WHERE s."ProductId" = i."ProductId" AND s."IsDeleted" = false
GROUP BY s."LocationId"
HAVING SUM(s."Quantity") > 0) st
INNER JOIN "Inv_T_Location" l ON l."Id" = st."LocationId" AND l."LocationType" = 'Internal' AND l."IsDeleted" = false
LEFT JOIN LATERAL (SELECT SUM(public."Inv_F_UomConvert"(tl."DemandQuantity", tl."UomId", i."UomId")) AS "Quantity"
FROM "Inv_T_TransferLine" tl
INNER JOIN "Inv_T_Transfer" t ON t."Id" = tl."TransferId"
WHERE t."SourceLocationId" = st."LocationId" AND tl."ProductId" = i."ProductId" AND tl."IsDeleted" = false
AND t."IsDeleted" = false AND t."Status" IN ('Draft', 'Waiting', 'Ready')) rs ON TRUE
LEFT JOIN "Prc_T_MaterialRequestPlanLine" pl
ON pl."MaterialRequestItemId" = i."Id" AND pl."PlanMethod" IN ('Transfer', 'Stock') AND pl."SourceLocationId" = st."LocationId" AND pl."IsDeleted" = false
INNER JOIN public."Prc_V_MaterialRequestItemPlan" ip ON ip."Id" = i."Id"
LEFT JOIN LATERAL (SELECT SUM(x."Quantity") AS "Quantity"
FROM "Prc_T_MaterialRequestPlanLine" x
WHERE x."MaterialRequestItemId" = i."Id" AND x."IsDeleted" = false
AND (x."SourceLocationId" IS NULL OR x."SourceLocationId" <> st."LocationId")) od ON TRUE
WHERE i."IsDeleted" = false;