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

23 lines
1.9 KiB
Transact-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 ALTER VIEW [dbo].[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,
CASE WHEN i.DueDate < CAST(GETDATE() AS date) THEN 'Late'
WHEN i.DueDate <= DATEADD(day, 7, CAST(GETDATE() AS date)) THEN 'Week' ELSE 'Later' END AS DueState,
i.RequestReason,
pl.PurchaseQuantity, pl.TransferQuantity, pl.RemainingQuantity, ISNULL(st.Quantity, 0) AS StockQuantity,
(SELECT SUM(x.Quantity) FROM Prc_T_MaterialRequestPlanLine x WHERE x.MaterialRequestItemId = i.Id AND x.IsDeleted = 0) AS DraftQuantity,
i.IsDeleted
FROM Prc_T_MaterialRequestItem i
INNER JOIN Prc_T_MaterialRequest r ON r.Id = i.MaterialRequestId
INNER JOIN Prc_V_MaterialRequestItemPlan pl ON pl.Id = i.Id
LEFT JOIN Stk_T_Product p ON p.Id = i.ProductId
OUTER APPLY (SELECT SUM(c.Quantity) AS Quantity
FROM (SELECT dbo.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 = 0) c) st
WHERE i.IsDeleted = 0 AND r.IsDeleted = 0 AND i.LineStatus = 'Waiting' AND pl.RemainingQuantity > 0
AND r.ApprovalStatus IN (N'Onaylandı', N'Teklif Talebi', N'Teklif Talebi Gönderildi', N'Satınalma Siparişi')