订单需求

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

时间: 2024-10-20 05:40:54

订单需求的相关文章

odoo开发历史订单需求整体思路

第一步:找到客户对应页面,并找到他所下过的销售订单,用数据库语句查出所有数据,并去除重复数据,显示在前端, sql="select DISTINCT t2.product_id as product_id, t6.material as material,t6.cust_spec as cust_spec,t6.list_price as price,t2.product_uom as uom, t3.packing_type1 as pick_type from res_partner t0

SAP销售订单需求类型的确定优秀级

需求类型的确定优秀级:1.策略组里的需求类型:2.MRP组里的:3.SO行项目类别+MRP类型4.SO行项目类别 部分截图: 原文地址:https://www.cnblogs.com/mingdashu/p/12055236.html

未清销售订单及预测的所有工序物料需求(按BOM展开不考虑已发料)

--按截止日考虑库存分配(所有物料及工序) DECLARE @dueDate DATETIME --截止交期 SELECT @dueDate = T0.T_DueDate FROM OFPR T0 WHERE T0.T_DueDate = [%0] SELECT @dueDate = '[%0]' --------------------------------------------------------------------------------------------- -- ===

4.4 多组织物料[供应/需求]查询

4.4.1   业务方案描述 1.  系统标准功能的物料供应需求查询是供同组织内部查询使用. 2.  由于一般的企业集团存在多组织物料共用的现象,为此,需要将该功能进行扩展,以实现跨组织使用,方便计划等业务部门查看多组织的计划平衡. 4.4.2   业务方案逻辑控制 建立供应需求明细中包含: 1)库存(排除供应和需求组织的结算仓): 2)离散任务(装配件)供应和(组件)需求:离散任务.离散任务需求: 3)未发货的销售订单:销售订单: 4)接收未入库的采购订单:正在接收的PO: 5)未接收的采购订

Oracle EBS-SQL (MRP-3):检查例外信息查询_建议取消_采购订单.sql

select msi.segment1                                    编码 ,msi.description                                   描述 ,mr.old_order_quantity                        数量 ,mr.old_schedule_date             订单需求日期 ,mipo.po_number                             订单号

[易飞]批次需求计划-致命缺陷

标题比较虎人,我们先来了解一个案例:(以下是我模拟的测试的案例) 模拟条件:主件品号:10101008010001          品名  : ACM0801B-FL-GBH     固定前置天数8天 变动前置天数1天 元件品号:30100008010001          品名  : LCD                               固定前置天数10天,MOQ:3000 主件.元件 现有库存为0. BOM结构:比例1:1 2014年10月1号 接到客户:A 订单C: 需求

供应链需求调研CheckList

总体(General) 基本情况 1.    企业地址.邮编.电话.传真,项目联系人等基本资料. 2.    企业经营范围,产品线和主导产品. 3.    企业近几年的产值及销售额. 4.    企业高层领导机构.员工总人数. 5.    组织结构图,列出各主要部门的人员,职责及部门经理姓名. 6.    概要描述企业的运营模式. 7.    画出一张位置示意图,标明市场.销售.生产计划.采购.库房.制造.质量.财务等关键部门的位置. 企业发展 1.    企业目前主营业务的现状及成长性预测.

防止提交重复订单的方法

背景交代: 使用ajax来提交创建订单,需求是不能创建重复的订单 使用js限制提交的频率 // 设置内容为可用,但是不能继续操作 $.fn.setBtnTxtEnabled = function (func) { if (!this) { throw new Error("element 不能为空"); } this.text(this.data('text')); } // 设置按钮不可用 $.fn.setBtnEnabled = function (func) { if (!thi

QQ_990814268 摘抄Django项目购物车、订单(三)

购物车 提示 使用redis数据库存储购物车数据 购物车需要完成增.删.改.查的逻辑 查询的结果,需要由服务器响应界面给客户端展示出来 增删改的操作,是客户端发数据给服务器,两者之间的交互是局部刷新的效果,需要用ajax交互 添加购物车的请求方法:post 服务器和客户端传输数据格式:json 服务器接收的数据 用户id:user_id 商品id:sku_id 商品数量:count 定义添加购物车视图 # 项目的urls url(r'^cart/', include('cart.urls', n