未清銷售訂單及預測的全部工序物料需求(按BOM展開不考慮已發料)

--按截止日考慮庫存分配(全部物料及工序)ide

DECLARE @dueDate DATETIME --截止交期spa


SELECT @dueDate = T0.T_DueDate FROM OFPR T0 WHERE T0.T_DueDate = [%0]ip


SELECT @dueDate = '[%0]'產品

---------------------------------------------------------------------------------------------it



-- ======================================io

-- 程序:庫存分配表-全部物料及工序class

-- wangtp擴展

-- 2017.5.10循環

-- 預測未完工部分爲未清需求 20170628程序

--按截止日考慮庫存分配(全部物料及工序)

--DECLARE @dueDate DATETIME --截止交期

--SELECT @dueDate = T0.T_DueDate FROM OFPR T0 WHERE T0.T_DueDate = [%0]

--SELECT @dueDate = '[%0]'

--EXEC [U_P_STOCK2NEED_WITHOUT_ISSUED] @dueDate

-- ======================================

ALTER PROCEDURE [dbo].[U_P_STOCK2NEED_WITHOUT_ISSUED]

(

@dueDate DATETIME

)

AS

BEGIN

DECLARE @owe_line_count INT

DECLARE @level INT

DECLARE @maxLevel INT --需求計算的最大層級


--截止日默認爲下個月最後一天

IF @dueDate IS NULL OR @dueDate = '19000101' OR @dueDate < CONVERT(VARCHAR(8),GETDATE(),112)

BEGIN

 SET @dueDate = DATEADD(dd,-1, DATEADD(MM,2,GETDATE()))

 SET @dueDate = DATEADD(dd,-DATEPART(dd,@dueDate),@dueDate)

 SET @dueDate = CONVERT(VARCHAR(8),@dueDate,112)

END

--SELECT @end


----------------------------------------------------------------------------------------------------------------------------------------

--T1:僅涉及未清銷售訂單的產品取得BOM清單,並計算各組件在其中的需求優先級關係 BOM裏面子件不能再擴展的,優先級最大,其父件優先級-1

--T1:父件、子件、基數、BOM展開層級、需求計算優先級

CREATE TABLE #BOM(Father NVARCHAR(20),ItemCode NVARCHAR(20),BaseQty DECIMAL(19,6),[Level] INT)


SET @level = 1

--2.1 訂單涉及BOM分解首層

INSERT #BOM

SELECT T0.Code Father,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'

--WHERE T0.Code IN (SELECT DISTINCT T1.ITEMCODE FROM ORDR T0 INNER JOIN RDR1 T1 ON T0.DocStatus= 'O' AND T1.LINESTATUS = 'O')

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 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 

(

[LEVEL] INT, --需求等級

Father NVARCHAR(20), --分解從

ItemCode NVARCHAR(20), --需求物料

QtyNeedF DECIMAL(19,6), --父件需求數量

BaseQty DECIMAL(19,6), --基數

QtyNeed DECIMAL(19,6), --按訂單數展開毛需求量

QtyIssued DECIMAL(19,6) --已發料(父件子件匹配 行發料數量 - 完工數*基數 舍負值爲0)

)


--訂單需求寫入需求表

INSERT INTO #TEMP_NEED_ALL

SELECT DISTINCT CASE WHEN T21.Father IS NULL THEN 0 WHEN T21.ItemCode IS NOT NULL THEN T21.Level ELSE @maxLevel END [LEVEL]

,'' Father,T20.ItemCode, 

T20.OpenQty [QtyFather],1.00 [BaseQty],T20.OpenQty [QtyNeed],0.00 [QtyIssued]

FROM 

(

--合計需求

SELECT T10.[ItemCode],SUM(T10.OpenQty) OpenQty,MIN(T10.ShipDate) ShipDate

--INTO #TEMP_OPEN_NEED

FROM 

(

SELECT T1.[ItemCode],T1.[OpenCreQty] OpenQty,T1.ShipDate

FROM [dbo].ORDR T0

INNER JOIN RDR1 T1 ON T1.DocEntry=T0.DocEntry 

--LEFT JOIN OSLP T2 ON T2.SlpCode=T0.SlpCode

WHERE T0.DocStatus = 'O' AND T0.DocType ='I' AND T1.LineStatus='O' 

AND T1.ShipDate <= @dueDate

UNION ALL

SELECT T10.ItemCode,T10.Quantity - ISNULL(SUM(T11.CmpltQty),0) Quantity,T10.[ShipDate]

FROM

(

SELECT T0.Code, 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.[Date] BETWEEN '20170401' AND @dueDate AND T1.Quantity > 0 

GROUP BY T0.Code,T1.ItemCode

)T10 

LEFT JOIN OWOR T11 ON T10.Code = T11.PickRmrk AND T10.ItemCode = T11.ItemCode

GROUP BY T10.ItemCode,T10.Quantity,T10.ShipDate

HAVING T10.Quantity > ISNULL(SUM(T11.CmpltQty),0)

)T10

GROUP BY T10.[ItemCode]

) T20

LEFT JOIN (SELECT DISTINCT Father,ItemCode,Level FROM #BOM) T21 ON T20.[ItemCode] = T21.ItemCode



--最終逐級彙總需求

CREATE TABLE #TEMP_NEED_SUM

(

[LEVEL] INT, --需求等級

ItemCode NVARCHAR(20), --需求物料

QtyNeed DECIMAL(19,6), --按訂單數展開需求量

OnHand DECIMAL(19,6), --在庫存數量

OnHand_GK DECIMAL(19,6), --灌口庫存

OnHand_TA DECIMAL(19,6), --同安庫存

QtyEnd DECIMAL(19,6), --溢缺

OnQC_GK DECIMAL(19,6), --灌口待檢數量

OnQC_TA DECIMAL(19,6) --同安行檢數量

)


--將0級彙總寫入最終表

SET @level = 0;

INSERT INTO #TEMP_NEED_SUM

SELECT T10.[LEVEL],T10.ItemCode,T10.QtyNeed,T11.OnHand,T11.OnHand_GK

,T11.OnHand_TA,ISNULL(T11.OnHand,0) - T10.QtyNeed [QtyEnd],T11.OnQC_GK,T11.OnQC_TA

FROM #TEMP_NEED_ALL T10

LEFT JOIN #TMP_ONHAND T11 ON T10.ItemCode = T11.ItemCode

WHERE T10.[LEVEL] = @level


--循環計算下級需求

WHILE(1=1)

BEGIN

--合計需求求下階需求

INSERT INTO #TEMP_NEED_ALL 

SELECT T1.[LEVEL], T0.ItemCode [Father],T1.ItemCode 

,ABS(T0.QtyEnd) QtyNeedF,T1.[BaseQty],SUM(CEILING(ABS(T0.QtyEnd)*T1.[BaseQty])) [QtyNeed],0 [QtyIssued]

FROM #TEMP_NEED_SUM T0

INNER JOIN #BOM T1 ON T0.ItemCode = T1.Father 

WHERE T0.[Level] = @level AND T0.QtyEnd < 0 --上級庫存及發料不足時時才計算下階物料

GROUP BY  T1.[LEVEL],T0.ItemCode,T1.ItemCode,T0.QtyEnd,T1.[BaseQty]

--彙總需求下階需求

SELECT @level = @level + 1

INSERT INTO #TEMP_NEED_SUM

SELECT T10.[LEVEL],T10.ItemCode,SUM(T10.QtyNeed) QtyNeed,T11.OnHand,T11.OnHand_GK

,T11.OnHand_TA,ISNULL(T11.OnHand,0) - SUM(T10.QtyNeed ) [QtyEnd],T11.OnQC_GK,T11.OnQC_TA

FROM #TEMP_NEED_ALL T10

LEFT JOIN #TMP_ONHAND T11 ON T10.ItemCode = T11.ItemCode

WHERE T10.[LEVEL] = @level 

AND T10.QtyNeed >= T10.QtyIssued --針對該父件-子件對 發料已超過需求數量 則不進入彙總了(不須要分攤庫存量)

GROUP BY T10.[LEVEL],T10.ItemCode,T11.OnHand,T11.OnHand_GK,T11.OnHand_TA,T11.OnQC_GK,T11.OnQC_TA

IF @level > @maxLevel+1 BREAK

END


SELECT DISTINCT


@dueDate [截止日期]

,T10.ItemCode [物料編號]

,T10.InvntryUom [單位]

,T11.QtyNeed [毛需求]

,T12.OnHand [可用庫存]

,T12.OnHand_GK [灌口庫存]

,T12.OnHand_TA [同安庫存]

,ISNULL(T11.[QtyEnd],T12.OnHand) [溢缺]

,ISNULL(T13.OnOrder,0) [採購/生產在途]

,ISNULL(T12.OnQC_GK,0) [灌口待檢]

,ISNULL(T12.OnQC_TA,0) [同安待檢]

,T10.ItemName [物料描述]

,T10.LeadTime [提早期-天(未考慮)]

--,T10.ShipDate [交期]

,T10.Spec [工序]

,T10.U_Location [儲位]

,CASE T10.U_Factory WHEN 'GK' THEN '灌口' WHEN 'TN' THEN '同安' ELSE '-' END [工廠]

,T10.TaxCtg [工做中心]

,T10.U_Buyer [物料採購員]

,T15.[Level] [需求等級]

,'C02-原料倉庫/C03-成品倉庫/C05-委外倉庫/C06-配件倉/C11-包材倉庫/C17-高儀倉庫/C20-非生產類倉庫/C31-同安三樓龍頭倉庫/C32-同安四樓配件倉/C33-同安成品倉/C39-同安包材倉' [考慮庫存的倉庫]

FROM OITM T10

LEFT JOIN #TEMP_NEED_SUM T11 ON T10.ItemCode = T11.ItemCode

LEFT JOIN #TMP_ONHAND T12 ON T10.ItemCode = T12.ItemCode

LEFT JOIN

(

SELECT U0.ItemCode,SUM(U0.OnOrder) OnOrder

FROM(

SELECT ItemCode,PlannedQty - CmpltQty OnOrder

FROM OWOR 

WHERE Status IN ('P','R') AND DueDate <= @dueDate AND PlannedQty > CmpltQty 

UNION ALL

SELECT T1.ItemCode ,OpenCreQty

FROM OPOR T0

INNER JOIN POR1 T1 On T0.DocEntry = T1.DocEntry 

WHERE T0.DocStatus = 'O' AND T0.DocType = 'I' AND T1.LineStatus = 'O' AND T1.ShipDate <= @dueDate

)U0

GROUP BY ItemCode 

)T13 ON T10.ItemCode = T13.ItemCode

LEFT JOIN 

(

SELECT ItemCode,SUM(QtyNeed) QtyNeed0,SUM(QtyIssued) QtyIssued0

FROM #TEMP_NEED_ALL 

GROUP BY ItemCode

)T14 ON T10.ItemCode = T14.ItemCode 

LEFT JOIN 

(

SELECT DISTINCT ItemCode ,[LEVEL] FROM #BOM

)T15 ON T10.ItemCode = T15.ItemCode

ORDER BY T10.ItemCode

FOR BROWSE


/* 數據追溯

SELECT * FROM #TEMP_NEED_SUM WHERE ItemCode IN ('TDB2112-00-41C-5030','2DB2112-00-41A-000', '6G03-DB2112-01-41A0')

SELECT * FROM #TEMP_NEED_ALL WHERE ItemCode IN ('TDB2112-00-41C-5030','2DB2112-00-41A-000', '6G03-DB2112-01-41A0')

*/


DROP TABLE #TEMP_NEED_SUM

DROP TABLE #TEMP_NEED_ALL

DROP TABLE #TMP_ONHAND

DROP TABLE #BOM

END

相關文章
相關標籤/搜索