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

38 lines
3.4 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.

-- 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 ALTER VIEW [dbo].[Prc_V_MaterialRequestPlanSource] AS
SELECT CONCAT(CAST(i.Id AS nvarchar(36)), ':', CAST(st.LocationId AS nvarchar(36))) AS Id, i.TenantId, i.Id AS BaseId,
l.BranchId, l.WarehouseId, st.LocationId, st.OnHand, ISNULL(rs.Quantity, 0) AS ReservedQuantity,
st.OnHand - ISNULL(rs.Quantity, 0) AS FreeQuantity, i.UomId, pl.Quantity AS PlanQuantity, i.IsDeleted,
CAST(CASE WHEN st.LocationId = r.LocationId THEN 1 ELSE 0 END AS bit) AS IsDestination, mx.Quantity AS MaxQuantity
FROM Prc_T_MaterialRequestItem i
INNER JOIN Prc_T_MaterialRequest r ON r.Id = i.MaterialRequestId
CROSS APPLY (SELECT c.LocationId, SUM(c.Quantity) AS OnHand
FROM (SELECT s.LocationId, s.Quantity AS Raw, dbo.Inv_F_UomConvert(s.Quantity, s.UomId, i.UomId) AS Quantity
FROM Inv_T_Stock s
WHERE s.ProductId = i.ProductId AND s.IsDeleted = 0) c
GROUP BY c.LocationId
HAVING SUM(c.Raw) > 0) st
INNER JOIN Inv_T_Location l ON l.Id = st.LocationId AND l.LocationType = 'Internal' AND l.IsDeleted = 0
OUTER APPLY (SELECT SUM(c.Quantity) AS Quantity
FROM (SELECT dbo.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 = 0 AND t.IsDeleted = 0
AND t.Status IN ('Draft', 'Waiting', 'Ready')) c) rs
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 = 0
INNER JOIN Prc_V_MaterialRequestItemPlan ip ON ip.Id = i.Id
OUTER APPLY (SELECT SUM(x.Quantity) AS Quantity
FROM Prc_T_MaterialRequestPlanLine x
WHERE x.MaterialRequestItemId = i.Id AND x.IsDeleted = 0 AND (x.SourceLocationId IS NULL OR x.SourceLocationId <> st.LocationId)) od
CROSS APPLY (SELECT CASE WHEN v.Quantity < 0 THEN 0 ELSE v.Quantity END AS Quantity
FROM (SELECT CASE WHEN st.OnHand - ISNULL(rs.Quantity, 0) < ip.RemainingQuantity - ISNULL(od.Quantity, 0)
THEN st.OnHand - ISNULL(rs.Quantity, 0) ELSE ip.RemainingQuantity - ISNULL(od.Quantity, 0) END AS Quantity) v) mx
WHERE i.IsDeleted = 0;
GO