訂單需求
USE [DEL_DATA]
GO
/****** Object: StoredProcedure [dbo].[U_P_STOCK2NEED_EXPEND2] Script Date: 08/14/2017 10:40:30 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- ======================================
-- Fuction:按業務訂單號查訂單執行狀態
-- wangtp
-- 2017.8.11
-- 預測未完工部分為未清需求 20170628
-- 采購生產在途 包含灌口C08待檢數量 20170727
--按截止日考慮庫存分配(所有物料及工序)
--EXEC [U_P_STOCK2NEED_EXPEND2]
-- ======================================
ALTER PROCEDURE [dbo].[U_P_STOCK2NEED_EXPEND2]
AS
BEGIN
DECLARE @owe_line_count INT
DECLARE @level INT
DECLARE @maxLevel INT --需求計算的最大層級
DECLARE @id INT
----------------------------------------------------------------------------------------------------------------------------------------
--T1:僅涉及未清銷售訂單的產品取得BOM清單,並計算各組件在其中的需求優先級關系 BOM裏面子件不能再擴展的,優先級最大,其父件優先級-1
--T1:父件、子件、基數、BOM展開層級、需求計算優先級
CREATE TABLE #BOM(Father NVARCHAR(20),ChildNum VARCHAR(3),ItemCode NVARCHAR(20),BaseQty DECIMAL(19,6),[Level] INT)
SET @level = 1
--2.1 訂單涉及BOM分解首層
INSERT #BOM
SELECT T0.Code Father,CAST(10+MIN(T1.ChildNum) AS VARCHAR(3)),T1.Code ItemCode,CAST(SUM(T1.Quantity)/T0.Qauntity AS DECIMAL(19,6)) Quantity ,0 [Level]
--INTO #BOM
FROM OITT T0
INNER JOIN ITT1 T1 ON T0.Code = T1.Father AND T1.[Type]= ‘4‘
GROUP BY T0.Code,T1.Code,T0.Qauntity
-- 第三步 排需求優先級 Level----------------------------------------
--3.1 末級葉子節點處理
SET @level = 99
UPDATE T0 SET T0.[Level] = @level
FROM #BOM T0
LEFT JOIN #BOM T1 ON T0.ItemCode = T1.Father
WHERE T1.Father IS NULL
--3.2 非末級逐級處理
WHILE(@level > 0)
BEGIN
UPDATE #BOM SET [Level] = @level - 1 WHERE ItemCode IN (SELECT DISTINCT FATHER FROM #BOM WHERE [Level] = @level)
IF @@ROWCOUNT = 0 BREAK
SET @level = @level - 1
END
--減去落差
UPDATE #BOM SET [Level] = [Level] - (@level - 1)
--求最大層級
SELECT @maxLevel = MAX([Level]) FROM #BOM
SELECT DISTINCT ItemCode,Level INTO #TMP_LEVEL FROM #BOM
--可用數量
SELECT T0.ItemCode,ISNULL(SUM(CASE WHEN T0.WHSCODE NOT IN(‘C08‘,‘C38‘) THEN T0.OnHand ELSE 0 END),0) OnHand
,ISNULL(SUM(CASE WHEN T0.WhsCode NOT LIKE ‘C3%‘ AND T0.WHSCODE <> ‘C08‘ THEN T0.OnHand ELSE 0 END ),0) AS OnHand_GK
,ISNULL(SUM(CASE WHEN T0.WhsCode LIKE ‘C3%‘ AND T0.WhsCode <> ‘C38‘ THEN T0.OnHand ELSE 0 END ),0) AS OnHand_TA
,ISNULL(SUM(CASE WHEN T0.WhsCode = ‘C08‘ THEN T0.OnHand ELSE 0 END ),0) AS OnQC_GK
,ISNULL(SUM(CASE WHEN T0.WhsCode = ‘C38‘ THEN T0.OnHand ELSE 0 END ),0) AS OnQC_TA
INTO #TMP_ONHAND
FROM OITW T0
WHERE T0.OnHand >0
AND T0.WhsCode NOT IN (‘C01‘,‘C04‘,‘C10‘,‘C13‘,‘C14‘,‘C15‘,‘C16‘,‘C18‘,‘C19‘,‘C34‘,‘C99‘)
GROUP BY T0.ItemCode
----按BOM擴展--------------------------------------------
CREATE TABLE #TEMP_NEED_ALL
(
NumAtCard NVARCHAR(100), --業務訂單號
DocEntry INT , --ORDR.DocEntry
AbsId INT, --OFCT.AbsId
Position VARCHAR(MAX), --需求位置
[LEVEL] INT, --需求等級
Father NVARCHAR(20), --分解從
ItemCode NVARCHAR(20), --需求物料
QtyNeedF DECIMAL(19,6), --父件需求數量
BaseQty DECIMAL(19,6), --基數
QtyNeed DECIMAL(19,6) --按訂單數展開毛需求量
,DocDueDate DATETIME --訂單交期
,DueDate DATETIME --組件交期
)
--訂單需求寫入需求表
INSERT INTO #TEMP_NEED_ALL
SELECT T20.NumAtCard,T20.DocEntry,T20.AbsId,CAST(1000+ROW_NUMBER() OVER(order by T20.[ItemCode],T20.ShipDate ASC,T20.DocEntry,T20.AbsId) AS VARCHAR(MAX)) [Position],T21.[Level],
‘‘ Father,T20.ItemCode,T20.Quantity [QtyFather],1.00 [BaseQty],T20.Quantity [QtyNeed]
,T20.ShipDate,T20.ShipDate
FROM
(
--合計需求
SELECT T10.NumAtCard,T10.DocEntry,-1 AbsId,T10.ItemCode,SUM(T10.OpenQty) Quantity,MIN(T10.ShipDate) ShipDate
FROM(
SELECT T0.NumAtCard,T0.DocEntry,-1 [AbsId], T1.[ItemCode],T1.[OpenCreQty]*T1.NumPerMsr OpenQty,T1.ShipDate
FROM [dbo].ORDR T0
INNER JOIN RDR1 T1 ON T1.DocEntry=T0.DocEntry
WHERE T0.DocStatus = ‘O‘ AND T0.DocType =‘I‘ AND T1.LineStatus=‘O‘
AND T0.DocEntry <= (SELECT MAX(T1.DocEntry) FROM OWOR T0 INNER JOIN ORDR T1 ON T0.PickRmrk = T1.NumAtCard WHERE T0.Status <> ‘C‘)
AND T0.DocEntry NOT IN (SELECT T0.DocEntry FROM ORDR T0 LEFT JOIN MSN5 T1 ON T1.DocType = 17 AND T0.DocEntry = T1.DocEntry
WHERE T0.DocEntry > 1520 AND T1.DocEntry IS NULL) --20170807 未跑MRP的銷售訂單
)T10
GROUP BY T10.NumAtCard,T10.DocEntry,T10.ItemCode
UNION ALL
SELECT T10.Code,-1 [DocEntry],T10.AbsID, T10.ItemCode,T10.Quantity - ISNULL(SUM(T11.CmpltQty),0) Quantity,T10.[ShipDate]
FROM
(
SELECT T0.Code,T0.AbsID, T1.ItemCode,SUM(T1.Quantity) Quantity ,MIN(T1.[Date]) [ShipDate]
--SELECT T1.ItemCode,T1.Quantity ,T1.[Date] [ShipDate]
FROM OFCT T0 INNER JOIN FCT1 T1 ON T0.AbsID = T1.AbsID
WHERE T1.U_Status = ‘O‘ AND T1.Quantity > 0
AND T0.AbsID <= (SELECT MAX(T1.AbsId) FROM OWOR T0 INNER JOIN OFCT T1 ON T0.PickRmrk = T1.Code WHERE T0.Status <> ‘C‘)
GROUP BY T0.Code,T0.AbsID,T1.ItemCode
)T10
LEFT JOIN OWOR T11 ON T10.Code = T11.PickRmrk AND T10.ItemCode = T11.ItemCode
GROUP BY T10.Code,T10.AbsID,T10.ItemCode,T10.Quantity,T10.ShipDate
HAVING T10.Quantity > ISNULL(SUM(T11.CmpltQty),0)
) T20
LEFT JOIN #TMP_LEVEL T21 ON T20.[ItemCode] = T21.ItemCode
--頂層默認0級
UPDATE #TEMP_NEED_ALL SET LEVEL = 0 WHERE LEVEL IS NULL
--逐級計算已發料 按業務訂單號、上級料號、子件料號 匹配計算 未清工單考慮庫存收發
CREATE TABLE #TEMP_NEED_ISSUED
(
NumAtCard NVARCHAR(100), --業務訂單號
DocEntry INT , --ORDR.DocEntry
AbsId INT, --OFCT.AbsId
Position VARCHAR(MAX), --需求位置
[LEVEL] INT, --需求等級
Father NVARCHAR(20), --父件
BaseQty DECIMAL(19,6), --基本數量
ItemCode NVARCHAR(20), --需求物料
QtyNeedOpen DECIMAL(19,6), --按訂單數展開需求量
QtyIssued DECIMAL(19,6), --根據父件-子件對 計算已發料數量
DocDueDate DATETIME, --訂單交期
DueDate DATETIME --組件交期
)
--最終逐級匯總需求
CREATE TABLE #TEMP_NEED_SUM
(
ID INT, --行編號
NumAtCard NVARCHAR(100), --業務訂單號
DocEntry INT , --ORDR.DocEntry
AbsId INT, --OFCT.AbsId
Position VARCHAR(MAX), --需求位置
[LEVEL] INT, --需求等級
Father NVARCHAR(20), --父件
BaseQty DECIMAL(19,6), --基本數量
ItemCode NVARCHAR(20), --需求物料
QtyNeedOpen DECIMAL(19,6), --按訂單數展開需求量
QtyIssued DECIMAL(19,6), --根據父件-子件對 計算已發料數量
QtyOrder DECIMAL(19,6), --在途數量
QtyOPRQ DECIMAL(19,6),--申請數量
QtyNeed DECIMAL(19,6), --欠料 = 總需求 - 已發料 - 在途 - 采購申請
QtyYQ DECIMAL(19,6), --溢缺 = 已發料 + 在途 + 采購申請 - 總需求
OnHandAll DECIMAL(19,6), --總可用庫存
OnHand DECIMAL(19,6), --在庫存數量
QtyEnd DECIMAL(19,6), --溢缺2 要傳遞到下層分解的數量=總需求 - 庫存分配 - 已發料
DocDueDate DATETIME, --訂單交期
DueDate DATETIME --組件交期
);
/*----0級-------------------------------------------------------------------------------*/
--將0級匯總寫入最終表 已發料當成0
SET @level = 0;
INSERT INTO #TEMP_NEED_SUM(ID,NumAtCard,DocEntry,AbsId,Position,[LEVEL], Father,BaseQty,ItemCode,QtyNeedOpen,QtyNeed,QtyIssued,OnHandAll, DocDueDate,DueDate )
SELECT ROW_NUMBER() OVER(order by T10.[ItemCode],T10.DocDueDate ASC,T10.DocEntry,T10.AbsId) [ID]
,T10.NumAtCard ,T10.DocEntry,T10.AbsId,MIN(T10.Position) [Position],MIN(T10.LEVEL) [LEVEL]
,T10.Father,1,T10.ItemCode,SUM(T10.QtyNeed),SUM(T10.QtyNeed),0 ,T11.OnHand ,T10.DocDueDate ,DATEADD(DD,-ISNULL(T12.LeadTime,0),T10.DocDueDate) [DueDate]
FROM #TEMP_NEED_ALL T10
LEFT JOIN #TMP_ONHAND T11 ON T10.ItemCode = T11.ItemCode
LEFT JOIN OITM T12 ON T10.ItemCode = T12.ItemCode
WHERE T10.[LEVEL] = @level
GROUP BY T10.ItemCode,T10.NumAtCard ,T10.DocEntry,T10.AbsId,T10.Father,T11.OnHand ,T10.DocDueDate ,T12.LeadTime
;
--0級在途 --欠料 = 總需求 - 已發料 - 在途
UPDATE T10 SET T10.QtyOrder = ISNULL(T11.QtyOrder,0)
,T10.QtyOPRQ = ISNULL(T12.QtyOPRQ,0) --采購申請
,T10.QtyYQ = ISNULL(T11.QtyOrder,0)+ISNULL(T12.QtyOPRQ,0) - T10.QtyNeedOpen --溢缺 = 采購生產在途 + 已發料 + 采購申請 - 未清需求
,T10.QtyNeed = CASE WHEN T10.QtyNeedOpen > ISNULL(T11.QtyOrder,0)+ISNULL(T12.QtyOPRQ,0) --欠料 = 未清需求 - 采購生產在途 - 已發料 - 采購申請
THEN T10.QtyNeedOpen-(ISNULL(T11.QtyOrder,0)+ISNULL(T12.QtyOPRQ,0)) ELSE 0 END
FROM #TEMP_NEED_SUM T10
LEFT JOIN(
SELECT U0.NumAtCard,U0.ItemCode,SUM(ISNULL(PlannedQty,0)) PlannedQty,CEILING(SUM(ISNULL(OpenQty,0))) QtyOrder
FROM
(
--生產訂單
SELECT T0.NumAtCard,T0.ItemCode,T1.PlannedQty PlannedQty ,CASE WHEN T1.Status IN (‘P‘,‘R‘) AND T1.PlannedQty > T1.CmpltQty THEN T1.PlannedQty - T1.CmpltQty ELSE 0 END OpenQty
FROM #TEMP_NEED_SUM T0
INNER JOIN OWOR T1 ON T0.NumAtCard = T1.PickRmrk AND T0.ItemCode = T1.ItemCode
WHERE T0.LEVEL = @level AND T1.Status IN (‘P‘,‘R‘)
UNION ALL
--采購訂單
SELECT T0.NumAtCard,T0.ItemCode,T1.Quantity*T1.NumPerMsr PlannedQty ,T1.OpenCreQty*T1.NumPerMsr
FROM (SELECT DISTINCT NumAtCard,ItemCode FROM #TEMP_NEED_SUM WHERE LEVEL = @level)T0
INNER JOIN POR1 T1 ON T0.NumAtCard = T1.U_OrderNo AND T0.ItemCode = T1.ItemCode
INNER JOIN OPOR T2 ON T1.DocEntry = T2.DocEntry
WHERE T2.DocType = ‘I‘ AND T2.DocStatus = ‘O‘ AND T1.LineStatus = ‘O‘ AND T1.OpenCreQty > 0
--UNION ALL --采購退貨單不會產生在途
)U0
GROUP BY U0.NumAtCard,U0.ItemCode
)T11 ON T10.NumAtCard = T11.NumAtCard AND T10.ItemCode = T11.ItemCode
LEFT JOIN
(
--采購申請數量
SELECT T0.NumAtCard,T0.ItemCode,SUM(T1.OpenCreQty) QtyOPRQ
FROM #TEMP_NEED_SUM T0
INNER JOIN PRQ1 T1 ON T0.NumAtCard = T1.U_OrderNo AND T0.ItemCode = T1.ItemCode
INNER JOIN OPRQ T2 ON T1.DocEntry = T2.DocEntry
WHERE T0.LEVEL = @level AND T2.DocStatus = ‘O‘ AND T1.LineStatus = ‘O‘ AND T1.OpenCreQty>0
GROUP BY T0.NumAtCard,T0.ItemCode
)T12 ON T10.NumAtCard = T12.NumAtCard AND T10.ItemCode = T12.ItemCode
--0級分配庫存
UPDATE T11 SET T11.OnHand = CASE WHEN T11.OnHandAll >= T10.QtyNeedSumUp1 THEN T11.QtyNeed
WHEN T11.OnHandAll BETWEEN T10.QtyNeedSumUp1-T11.QtyNeed AND T10.QtyNeedSumUp1 THEN T11.QtyNeed - (T10.QtyNeedSumUp1 - T11.OnHandAll)
ELSE 0 END
--SELECT T11.*,T10.QtyNeedSumUp1,CASE WHEN T11.OnHandAll >= T10.QtyNeedSumUp1 THEN T11.QtyNeed
-- WHEN T11.OnHandAll BETWEEN T10.QtyNeedSumUp1-T11.QtyNeed AND T10.QtyNeedSumUp1 THEN T11.QtyNeed - (T10.QtyNeedSumUp1 - T11.OnHandAll)
-- ELSE 0 END [FP]
FROM (
SELECT T1.ID,isnull(SUM(T0.QtyNeed),0) QtyNeedSumUp1
FROM #TEMP_NEED_SUM T0
INNER JOIN #TEMP_NEED_SUM T1 ON T0.ID <= T1.ID AND T0.ItemCode = T1.ItemCode --AND T0.FATHER = T1.FATHER AND T0.ITEMCODE = T1.ITEMCODE
WHERE T0.LEVEL = @level AND T1.LEVEL = @level
GROUP BY T1.ID--,T1.ItemCode,T1.FATHER,T1.ITEMCODE
)T10
INNER JOIN #TEMP_NEED_SUM T11 ON T10.ID = T11.ID
--計算最終下級分解需求量 = 總需求 - 庫存分配 - 已發料
UPDATE #TEMP_NEED_SUM SET QtyEnd = CASE WHEN QtyNeedOpen > ISNULL(OnHand,0) + ISNULL(QtyIssued,0) THEN QtyNeedOpen - (ISNULL(OnHand,0) + ISNULL(QtyIssued,0)) ELSE 0 END WHERE LEVEL = @level
/*----0級結束-------------------------------------------------------------------------------*/
--循環計算下級需求
WHILE(1=1)
BEGIN
--展開下階需求
INSERT INTO #TEMP_NEED_ALL
SELECT T0.NumAtCard ,T0.DocEntry,T0.AbsId ,T0.Position+‘-‘+T1.ChildNum [Position],T1.[LEVEL]
, T0.ItemCode [Father],T1.ItemCode,ABS(T0.QtyEnd) QtyNeedF,T1.[BaseQty],SUM(CEILING(ABS(T0.QtyEnd)*T1.[BaseQty])) [QtyNeed]
,T0.DocDueDate ,MIN(DATEADD(DD,-ISNULL(T2.LeadTime,0),T0.DueDate)) [DueDate]
FROM #TEMP_NEED_SUM T0
INNER JOIN #BOM T1 ON T0.ItemCode = T1.Father
INNER JOIN OITM T2 ON T1.ItemCode = T2.ItemCode
LEFT JOIN #TMP_LEVEL T3 ON T1.ItemCode = T3.ItemCode
WHERE T0.[Level] = @level
GROUP BY T1.[LEVEL],T0.NumAtCard ,T0.DocEntry,T0.AbsId,T0.Position,T1.ChildNum,T0.ItemCode,T1.ItemCode,T0.QtyEnd,T1.[BaseQty],T0.DocDueDate,T2.LeadTime
--匯總需求及已發料
SET @level = @level + 1;
--小匯總
INSERT INTO #TEMP_NEED_ISSUED(NumAtCard,DocEntry,AbsId,Position,[LEVEL] ,Father,BaseQty,ItemCode,QtyNeedOpen,QtyIssued ,DocDueDate,DueDate )
SELECT T10.NumAtCard ,T10.DocEntry,T10.AbsId,MIN(T10.Position) [Position],MIN(T10.LEVEL) [LEVEL]
,T10.Father,T10.BaseQty,T10.ItemCode,SUM(T10.QtyNeed),0
,T10.DocDueDate ,DATEADD(DD,-ISNULL(T12.LeadTime,0),T10.DocDueDate) [DueDate]
FROM #TEMP_NEED_ALL T10
LEFT JOIN #TMP_ONHAND T11 ON T10.ItemCode = T11.ItemCode
LEFT JOIN OITM T12 ON T10.ItemCode = T12.ItemCode
WHERE T10.[LEVEL] = @level
GROUP BY T10.ItemCode,T10.NumAtCard ,T10.DocEntry,T10.AbsId,T10.Father,T10.BaseQty,T11.OnHand ,T10.DocDueDate ,T12.LeadTime
;
--統計已發料
UPDATE T10 SET T10.QtyIssued = ISNULL(T13.QtyIssued,0) --已發料
FROM #TEMP_NEED_ISSUED T10
LEFT JOIN
(
SELECT T0.NumAtCard,T0.Father,T0.ItemCode
,ISNULL(SUM(
CASE WHEN T2.IssuedQty > CEILING(T1.CmpltQty*T0.BaseQty) THEN
CASE WHEN T2.IssuedQty < CEILING(T1.PlannedQty*T0.BaseQty) THEN T2.IssuedQty - CEILING(T1.CmpltQty*T0.BaseQty)
ELSE CEILING(T1.PlannedQty*T0.BaseQty) - CEILING(T1.CmpltQty*T0.BaseQty)
END
ELSE 0 END
)
,0) AS [QtyIssued]
FROM #TEMP_NEED_SUM T0
INNER JOIN OWOR T1 ON T0.NumAtCard = T1.PickRmrk AND T1.Status IN (‘P‘,‘R‘) AND T0.Father = T1.ItemCode
INNER JOIN WOR1 T2 ON T1.DocEntry = T2.DocEntry AND T0.ItemCode = T2.ItemCode
WHERE T0.LEVEL = @level
GROUP BY T0.NumAtCard,T0.Father,T0.ItemCode
)T13 ON T10.NumAtCard = T13.NumAtCard AND T10.Father = T13.Father AND T10.ItemCode = T13.ItemCode
INSERT INTO #TEMP_NEED_SUM(ID,NumAtCard,DocEntry,AbsId,Position,[LEVEL],ItemCode,QtyNeedOpen,QtyNeed,QtyIssued,OnHandAll, DocDueDate,DueDate )
SELECT ROW_NUMBER() OVER(order by T10.ItemCode,T10.NumAtCard ,T10.DocEntry,T10.AbsId,T11.OnHand ,T10.DocDueDate ASC) [ID]
,T10.NumAtCard ,T10.DocEntry,T10.AbsId,MIN(T10.Position) [Position],MIN(T10.LEVEL) [LEVEL]
,T10.ItemCode,SUM(T10.QtyNeedOpen),SUM(T10.QtyNeedOpen),SUM(T10.QtyIssued) ,T11.OnHand ,T10.DocDueDate ,MIN(T10.[DueDate])
FROM #TEMP_NEED_ISSUED T10
LEFT JOIN #TMP_ONHAND T11 ON T10.ItemCode = T11.ItemCode
WHERE T10.[LEVEL] = @level
GROUP BY T10.ItemCode,T10.NumAtCard ,T10.DocEntry,T10.AbsId,T11.OnHand ,T10.DocDueDate
;
--更新已發料數量
UPDATE T10 SET T10.QtyOrder = ISNULL(T11.QtyOrder,0) --采購生產在途
,T10.QtyOPRQ = ISNULL(T12.QtyOPRQ,0) --采購申請
,T10.QtyYQ =ISNULL(T11.QtyOrder,0)+ISNULL(T10.QtyIssued,0)+ISNULL(T12.QtyOPRQ,0) - T10.QtyNeedOpen --溢缺 = 采購生產在途 + 已發料 + 采購申請 - 未清需求
,T10.QtyNeed = CASE WHEN T10.QtyNeedOpen > ISNULL(T11.QtyOrder,0)+ISNULL(T10.QtyIssued,0)+ISNULL(T12.QtyOPRQ,0) --欠料 = 未清需求 - 采購生產在途 - 已發料 - 采購申請
THEN T10.QtyNeedOpen-(ISNULL(T11.QtyOrder,0)+ISNULL(T10.QtyIssued,0)+ISNULL(T12.QtyOPRQ,0)) ELSE 0 END
FROM #TEMP_NEED_SUM T10
LEFT JOIN(
SELECT U0.NumAtCard,U0.ItemCode,SUM(ISNULL(PlannedQty,0)) PlannedQty,CEILING(SUM(ISNULL(OpenQty,0))) QtyOrder
FROM
(
--生產訂單
SELECT T0.NumAtCard,T0.ItemCode,T1.PlannedQty PlannedQty ,CASE WHEN T1.Status IN (‘P‘,‘R‘) AND T1.PlannedQty > T1.CmpltQty THEN T1.PlannedQty - T1.CmpltQty ELSE 0 END OpenQty
FROM #TEMP_NEED_SUM T0
INNER JOIN OWOR T1 ON T0.NumAtCard = T1.PickRmrk AND T0.ItemCode = T1.ItemCode
WHERE T0.LEVEL = @level AND T1.Status IN (‘P‘,‘R‘)
UNION ALL
--采購訂單
SELECT T0.NumAtCard,T0.ItemCode,T1.Quantity*T1.NumPerMsr PlannedQty ,T1.OpenCreQty*T1.NumPerMsr
FROM (SELECT DISTINCT NumAtCard,ItemCode FROM #TEMP_NEED_SUM WHERE LEVEL = @level)T0
INNER JOIN POR1 T1 ON T0.NumAtCard = T1.U_OrderNo AND T0.ItemCode = T1.ItemCode
INNER JOIN OPOR T2 ON T1.DocEntry = T2.DocEntry
WHERE T2.DocType = ‘I‘ AND T2.DocStatus = ‘O‘ AND T1.LineStatus = ‘O‘ AND T1.OpenCreQty > 0
--UNION ALL --采購退貨單不會產生在途
)U0
GROUP BY U0.NumAtCard,U0.ItemCode
)T11 ON T10.NumAtCard = T11.NumAtCard AND T10.ItemCode = T11.ItemCode
LEFT JOIN
(
--采購申請數量
SELECT T0.NumAtCard,T0.ItemCode,SUM(T1.OpenCreQty) QtyOPRQ
FROM #TEMP_NEED_SUM T0
INNER JOIN PRQ1 T1 ON T0.NumAtCard = T1.U_OrderNo AND T0.ItemCode = T1.ItemCode
INNER JOIN OPRQ T2 ON T1.DocEntry = T2.DocEntry
WHERE T0.LEVEL = @level AND T2.DocStatus = ‘O‘ AND T1.LineStatus = ‘O‘ AND T1.OpenCreQty>0
GROUP BY T0.NumAtCard,T0.ItemCode
)T12 ON T10.NumAtCard = T12.NumAtCard AND T10.ItemCode = T12.ItemCode
WHERE T10.LEVEL = @level
;
--庫存分配
UPDATE T11 SET T11.OnHand = CASE WHEN T11.OnHandAll >= T10.QtyNeedSumUp1 THEN T11.QtyNeed
WHEN T11.OnHandAll BETWEEN T10.QtyNeedSumUp1-T11.QtyNeed AND T10.QtyNeedSumUp1 THEN T11.QtyNeed - (T10.QtyNeedSumUp1 - T11.OnHandAll)
ELSE 0 END
FROM (
SELECT T1.ID,isnull(SUM(T0.QtyNeed),0) QtyNeedSumUp1
FROM #TEMP_NEED_SUM T0
INNER JOIN #TEMP_NEED_SUM T1 ON T0.ID <= T1.ID AND T0.ItemCode = T1.ItemCode --AND T0.FATHER = T1.FATHER AND T0.ITEMCODE = T1.ITEMCODE
WHERE T0.LEVEL = @level AND T1.LEVEL = @level
GROUP BY T1.ID--,T1.ItemCode,T1.FATHER,T1.ITEMCODE
)T10
INNER JOIN #TEMP_NEED_SUM T11 ON T10.ID = T11.ID
WHERE T11.LEVEL = @level
;
--計算最終下級分解需求量 = 總需求 - 庫存分配 - 已發料
UPDATE #TEMP_NEED_SUM SET QtyEnd = CASE WHEN QtyNeedOpen > ISNULL(OnHand,0) + ISNULL(QtyIssued,0) THEN QtyNeedOpen - (ISNULL(OnHand,0) + ISNULL(QtyIssued,0)) ELSE 0 END WHERE LEVEL = @level
IF @level > @maxLevel+1 BREAK
END
--數據查詢
SELECT DISTINCT
T10.Position [層次]
,‘.‘+replicate(‘ ‘,2*(LEN(T10.Position)-4)/3)+ T10.ItemCode [物料(左縮進)]
,T10.NumAtCard [業務訂單號]
,T19.DocEntry [銷售訂單]
,T18.AbsId [銷售預測]
,T17.ItemCode [物料編號]
,T17.Spec [工序]
,T17.InvntryUom [單位]
,T16.BZ [已下達]
,T10.QtyNeedOpen [未清需求]
,T10.QtyIssued [已發料]
,T10.QtyOrder [采購/生產在途]
,T10.QtyOPRQ [采購申請]
,T10.QtyNeed [欠料(未清需求-已發料-采購/生產在途-采購申請)]
,T10.OnHandAll [總庫存]
,T10.OnHand [分配庫存]
,T10.QtyYQ [溢缺(已發料+采購/生產在途+采購申請-未清需求)]
,T10.OnHand + T10.QtyYQ [溢缺2(分配庫存+溢缺)]
,T10.QtyEnd [下層展開數量(未清需求-已發料-分配庫存)]
,T14.DocNum [生產訂單]
--,CASE WHEN T17.Spec = ‘外購‘ THEN T10.OnHand - T10.QtyNeed + ISNULL(T11.OpenQty,0) + ISNULL(T12.Quantity,0) ELSE NULL END [溢缺3(外購件 溢缺2+采購申請)]
--,T31.DocNum [生產訂單]
--,T32.DocEntry [采購訂單]
--,T32.LineNum + 1 [采購行]
--,T33.DocEntry [采購申請]
--,T33.LineNum + 1 [申請行]
,T13.OnHand_GK [灌口庫存]
,T13.OnHand_TA [同安庫存]
,ISNULL(T13.OnQC_GK,0) [灌口待檢]
,ISNULL(T13.OnQC_TA,0) [同安待檢]
,T10.DocDueDate [訂單交期]
,T10.DueDate [組件交期]
,T17.LeadTime [提前期-天]
--,T10.ShipDate [交期]
,T17.U_Location [儲位]
,CASE T17.U_Factory WHEN ‘GK‘ THEN ‘灌口‘ WHEN ‘TN‘ THEN ‘同安‘ ELSE ‘-‘ END [工廠]
,T17.TaxCtg [工作中心]
,T17.U_Buyer [物料采購員]
,T17.ItemName [物料描述]
,‘C02-原料倉庫/C03-成品倉庫/C05-委外倉庫/C06-配件倉/C11-包材倉庫/C17-高儀倉庫/C20-非生產類倉庫/C31-同安三樓龍頭倉庫/C32-同安四樓配件倉/C33-同安成品倉/C39-同安包材倉‘ [考慮庫存的倉庫]
,T10.ID
FROM #TEMP_NEED_SUM T10
LEFT JOIN OITM T15 ON T10.Father = T15.ItemCode --父件
INNER JOIN OITM T17 ON T10.ItemCode = T17.ItemCode --需求物料
LEFT JOIN ORDR T19 ON T10.DocEntry = T19.DocEntry
LEFT JOIN OFCT T18 ON T10.AbsId = T18.AbsId
LEFT JOIN
(
SELECT T0.NumAtCard,T0.ItemCode,‘Y‘ BZ
FROM #TEMP_NEED_SUM T0
INNER JOIN OWOR T1 ON T0.NumAtCard = T1.PickRmrk AND T0.ItemCode = T1.ItemCode
UNION ALL
SELECT T0.NumAtCard,T0.ItemCode,‘Y‘ BZ
FROM #TEMP_NEED_SUM T0
INNER JOIN POR1 T1 ON T0.NumAtCard = T1.U_OrderNo AND T0.ItemCode = T1.ItemCode
)T16 ON T10.NumAtCard = T16.NumAtCard AND T10.ItemCode = T16.ItemCode
LEFT JOIN #TMP_ONHAND T13 ON T10.ItemCode = T13.ItemCode
LEFT JOIN OWOR T14 ON T10.ItemCode = T14.ItemCode AND T10.NumAtCard = T14.PickRmrk AND T14.Status IN (‘P‘,‘R‘)
ORDER BY T10.Position ASC -- T17.ItemCode ,T10.DocDueDate ASC,T19.DocEntry ASC,T18.AbsId ASC
FOR BROWSE
END
訂單需求