sozsoft-platform/configs/seeds/postgres/object/Prc_V_MaterialRequestPlan.sql
2026-10-19 17:35:08 +03:00

23 lines
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.

-- Planlama (Prc.MaterialRequestPlan): onaylanmış (satınalmada ilerleyen dahil) talebin bekleyen ve planlanacak miktarı kalan kalemleri.
-- Stok bütün iç konumların toplamıdır (kalemin biriminde); DueState termin süzgecidir (Late / Week = 7 gün içinde / Later). Taslak Plan,
-- Planla diyaloğunda girilip henüz onaylanmamış dağıtımın toplamıdır (Prc_T_MaterialRequestPlanLine).
CREATE OR REPLACE VIEW public."Prc_V_MaterialRequestPlan" AS
SELECT i."Id", i."TenantId", i."MaterialRequestId", r."RequestNo", r."Priority", r."RequesterUserId", r."BranchId", r."DepartmentId", r."WarehouseId", r."LocationId",
r."RequestDate", i."ProductId", p."CategoryId", i."Quantity", i."UomId", i."DueDate",
CAST(CASE WHEN i."DueDate" < CURRENT_DATE THEN 'Late'
WHEN i."DueDate" <= CURRENT_DATE + 7 THEN 'Week' ELSE 'Later' END AS varchar(8)) AS "DueState",
i."RequestReason",
pl."PurchaseQuantity", pl."TransferQuantity", pl."RemainingQuantity", COALESCE(st."Quantity", 0) AS "StockQuantity",
(SELECT SUM(x."Quantity") FROM "Prc_T_MaterialRequestPlanLine" x WHERE x."MaterialRequestItemId" = i."Id" AND x."IsDeleted" = false) AS "DraftQuantity",
i."IsDeleted"
FROM "Prc_T_MaterialRequestItem" i
INNER JOIN "Prc_T_MaterialRequest" r ON r."Id" = i."MaterialRequestId"
INNER JOIN public."Prc_V_MaterialRequestItemPlan" pl ON pl."Id" = i."Id"
LEFT JOIN "Stk_T_Product" p ON p."Id" = i."ProductId"
LEFT JOIN LATERAL (
SELECT SUM(public."Stk_F_UomConvert"(s."Quantity", s."UomId", i."UomId")) AS "Quantity"
FROM "Stk_T_Stock" s
INNER JOIN "Stk_T_Location" l ON l."Id" = s."LocationId" AND l."LocationType" = 'Internal'
WHERE s."ProductId" = i."ProductId" AND s."IsDeleted" = false) st ON true
WHERE i."IsDeleted" = false AND r."IsDeleted" = false AND i."LineStatus" = 'Waiting' AND pl."RemainingQuantity" > 0
AND r."ApprovalStatus" IN ('Onaylandı', 'Teklif Talebi', 'Teklif Talebi Gönderildi', 'Satınalma Siparişi');