﻿USE [AIS202401040030]
GO
/****** Object:  StoredProcedure [dbo].[P_UpdatePlan2]    Script Date: 2024-01-17 17:38:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================                                        
-- Author:  阿斌<Author,,Name>                                        
-- Create date: 2023-05-23<Create Date,,>                                        
-- Description: 更新计划订单信息<Description,,>                                        
-- =============================================                                        
ALTER PROCEDURE [dbo].[P_UpdatePlan2]
(                  
 @FBILLNO NVARCHAR(1000)                  
)                  
                                         
AS
BEGIN                                        
    DECLARE @RC int
    SET @RC = 1;

    WHILE(@RC >0)
    BEGIN
        EXEC @RC = PROC_UPDATE_PLAN_BY_PAGE
    END
--SET NOCOUNT ON;
              
--SELECT 
--    A.FBILLNO AS '计划订单单号'
--    ,A.FSALENOROW,A.F_SOURCE_ORDER_LINE_NUMBER as '计划订单销售订单行号'
--    ,A.F_Source_order_number as '计划订单销售订单单号'                              
--    ,A.F_FORECASTNUMBER as '计划订单预测单单号'
--    ,A.F_Linenumber as '计划订单预测单行号'
--    ,E.FBILLNO AS '预测单单号'
--    ,D.FSEQ,D.F_Source_order_number as '预测单销售订单单号'
--    ,D.F_SOURCE_ORDER_LINE_NUMBER as '预测单销售订单行号'                             
--INTO #TEMP1      
--FROM T_PLN_PLANORDER A                              
--INNER JOIN T_PLN_RESERVELINKENTRY B ON B.FSUPPLYBILLNO = A.FBILLNO                              
--INNER JOIN T_PLN_RESERVELINK C ON C.FID=B.FID                              
--INNER JOIN T_PLN_FORECASTENTRY D ON D.FENTRYID=C.FSRCENTRYID                              
--INNER JOIN T_PLN_FORECAST e on e .fid=d.FID                  
----inner join #temp2 f on f.fname = a.FBILLNO                
--WHERE  A.F_FORECASTNUMBER=''                     
----AND A.FBILLNO = @FBILLNO                  
----where A.FBILLNO = 'MRP00260726'                              
----WHERE A.F_SOURCE_ORDER_LINE_NUMBER = ''         
    


-- SELECT                                  
--计划订单单号, 预测单单号,                              
--'预测单行号'=ISNULL( stuff((select ','+CONVERT(nvarchar(100), FSEQ) from #TEMP1 where 计划订单单号=t.计划订单单号 and 预测单单号 =t.预测单单号 for xml path('')),1,1,''),''),                              
--'销售订单单号'=ISNULL( stuff((select distinct ','+CONVERT(nvarchar(100), 预测单销售订单单号) from #TEMP1 where  预测单销售订单单号<>'' and 计划订单单号=t.计划订单单号 and 预测单单号 =t.预测单单号 for xml path('')),1,1,''),''),                              
--'销售订单行号'=ISNULL( stuff((select distinct ','+CONVERT(nvarchar(100), 预测单销售订单行号) from #TEMP1 where  预测单销售订单行号<>'' and 计划订单单号=t.计划订单单号 and 预测单单号 =t.预测单单号 for xml path('')),1,1,''),'')   
--into #temp3
-- FROM #TEMP1 t                                
-- GROUP BY 计划订单单号,预测单单号  


-- UPDATE T_PLN_PLANORDER SET  F_FORECASTNUMBER=B.预测单单号, F_Linenumber=B.预测单行号, F_SOURCE_ORDER_LINE_NUMBER=b.销售订单行号, F_SOURCE_ORDER_NUMBER=b.销售订单单号 
--FROM T_PLN_PLANORDER A                                
--inner join  #temp3 B                         
--ON  A.FBILLNO=B.计划订单单号                              
----inner join #temp2 f on f.fname = a.FBILLNO        
  
  
                                     
--DROP TABLE #TEMP1    
--DROP TABLE #temp3
                          
                          
--UPDATE A SET A.F_CUSTOMERNAME=D.FCUSTID,A.F_MATERIALCODE=D.FMATERIALID                          
--from T_PLN_PLANORDER A                              
--INNER JOIN T_PLN_RESERVELINKENTRY B ON B.FSUPPLYBILLNO = A.FBILLNO                              
--INNER JOIN T_PLN_RESERVELINK C ON C.FID=B.FID                              
--INNER JOIN T_PLN_FORECASTENTRY D ON D.FENTRYID=C.FSRCENTRYID                              
--INNER JOIN T_PLN_FORECAST e on e .fid=d.FID                    
----inner join #temp2 f on f.fname = a.FBILLNO                
----where (a.F_CUSTOMERNAME=0 or a.F_MATERIALCODE=0 )                     
----AND A.FBILLNO = @FBILLNO                  
                         
--update a set F_CUSTOMERNAME=d.FCUSTID,F_MATERIALCODE=d.FMATERIALID,F_FORECASTNUMBER=c.FBILLNO,F_Linenumber=d.FSEQ,F_SOURCE_ORDER_LINE_NUMBER=d.F_SOURCE_ORDER_LINE_NUMBER,F_Source_order_number=d.F_Source_order_number  
--FROM T_PLN_PLANORDER A   
--INNER JOIN T_PLN_PLANORDER_B B ON A.FID=B.FID  
--INNER JOIN T_PLN_FORECAST C ON C .FBILLNO = B.FSALEORDERNO  
--INNER JOIN T_PLN_FORECASTENTRY D ON D .FID= C.FID AND D.FSEQ=B.FSALEORDERENTRYSEQ  
----INNER JOIN #temp2 e ON A.FBILLNO = e.Fname   
----WHERE A.FBILLNO = 'MRP00640407'  
                                      
  
             
----UPDATE A SET A.F_Remark=ISNULL( dbo.GetForecast(A.F_FORECASTNUMBER  ,A.F_LINENUMBER),'')                      
----from T_PLN_PLANORDER A                             
----INNER JOIN T_PLN_FORECAST B ON B.FBILLNO=A.F_FORECASTNUMBER                        
----INNER JOIN T_PLN_FORECAST_L C ON C.FID=B.FID                        
----INNER JOIN T_PLN_FORECASTENTRY D on D.FID=B.FID AND D.FSEQ=A.F_Linenumber                        
----where ( a.F_Remark='' )                        
                        
                        
--UPDATE E SET F_DESCRIPTION=ISNULL(C.FDESCRIPTION ,'' )                      
--from T_PLN_PLANORDER A                             
--INNER JOIN T_PLN_PLANORDER_L E ON E.FID=A.FID                        
--INNER JOIN T_PLN_FORECAST B ON B.FBILLNO=A.F_FORECASTNUMBER                        
--INNER JOIN T_PLN_FORECAST_L C ON C.FID=B.FID                      
----inner join #temp2 f on f.fname = a.FBILLNO                
----where ( e.F_DESCRIPTION='' )                        
----AND A.FBILLNO = @FBILLNO                  
                  
                  
                  
                   
                  
--UPDATE T_PLN_PLANORDER  SET F_Remark=                  
-- ISNULL( dbo.GetForecast(A.F_FORECASTNUMBER  ,A.F_LINENUMBER),'')                     
--from T_PLN_PLANORDER A                     
----inner join #temp2 f on f.fname = a.FBILLNO                
--where                   
--1=1                   
----and ( a.F_Remark='' )                     
--AND   CONVERT(nvarchar(100),  a.FCREATEDATE,23)  =   CONVERT(nvarchar(100),  GETDATE(),23)                   
----AND A.FBILLNO = @FBILLNO           
    
        
----UPDATE T_PLN_PLANORDER SET F_WAUG_LAYER =                
----CASE WHEN B.层数 IS NULL THEN 订单号 ELSE CONCAT(订单号,'-',层数) END                
---- from  T_PLN_PLANORDER A                 
---- inner join                 
----(                
----select  A.FID, dbo.GetBomSeq( B .FBOMID,a.FMATERIALID) as '层数', A.FMATERIALID,B.FBOMID,A.f_WAUG_LAYER,A.F_FORECASTNUMBER,A.F_LINENUMBER,        
----A.F_MATERIALCODE,a.FBILLNO as '1',b.FBILLNO as '订单号' from T_PLN_PLANORDER A                 
----INNER JOIN T_PLN_PLANORDER B ON A.F_FORECASTNUMBER=B.F_FORECASTNUMBER AND A.F_LINENUMBER=B.F_LINENUMBER AND A.F_MATERIALCODE =B.FMATERIALID                
----INNER JOIN #temp2 C ON C.Fname=A.FBILLNO        
------WHERE a.F_WAUG_LAYER = ''                 
        
----)  B  ON A.FID = B.FID        
    
----DROP TABLE #temp2              
                                  
                                       
                                       
                                     
------select a.FBILLNO AS '计划订单单号',A.FSALENOROW,A.F_SOURCE_ORDER_LINE_NUMBER as '计划订单销售订单行号',A.F_Source_order_number as '计划订单销售订单单号'                              
------, A.F_FORECASTNUMBER as '计划订单预测单单号', A.F_Linenumber as '计划订单预测单行号',                              
------E.FBILLNO AS '预测单单号',D.FSEQ,D.F_Source_order_number as '预测单销售订单单号',D.F_SOURCE_ORDER_LINE_NUMBER as '预测单销售订单行号'                             
------INTO #TEMP1                  
------from T_PLN_PLANORDER A                              
------INNER JOIN T_PLN_RESERVELINKENTRY B ON B.FSUPPLYBILLNO = A.FBILLNO                              
------INNER JOIN T_PLN_RESERVELINK C ON C.FID=B.FID                              
------INNER JOIN T_PLN_FORECASTENTRY D ON D.FENTRYID=C.FSRCENTRYID                              
------INNER JOIN T_PLN_FORECAST e on e .fid=d.FID                              
------WHERE  A.F_FORECASTNUMBER=''                     
------AND A.FBILLNO = @FBILLNO                  
--------where A.FBILLNO = 'MRP00260726'                              
--------WHERE A.F_SOURCE_ORDER_LINE_NUMBER = ''                              
                            
------ --select * from #TEMP1                              
--------             
------UPDATE A SET A.F_FORECASTNUMBER=B.预测单单号,A.F_Linenumber=B.预测单行号,a.F_SOURCE_ORDER_LINE_NUMBER=b.销售订单行号,a.F_SOURCE_ORDER_NUMBER=b.销售订单单号                              
------FROM T_PLN_PLANORDER A                                
------inner join                               
------(                              
------SELECT                                  
------计划订单单号, 预测单单号,                              
------'预测单行号'=ISNULL( stuff((select ','+CONVERT(nvarchar(100), FSEQ) from #TEMP1 where 计划订单单号=t.计划订单单号 and 预测单单号 =t.预测单单号 for xml path('')),1,1,''),''),                              
------'销售订单单号'=ISNULL( stuff((select distinct ','+CONVERT(nvarchar(100), 预测单销售订单单号) from #TEMP1 where  预测单销售订单单号<>'' and 计划订单单号=t.计划订单单号 and 预测单单号 =t.预测单单号 for xml path('')),1,1,''),''),                              
------'销售订单行号'=ISNULL( stuff((select distinct ','+CONVERT(nvarchar(100), 预测单销售订单行号) from #TEMP1 where  预测单销售订单行号<>'' and 计划订单单号=t.计划订单单号 and 预测单单号 =t.预测单单号 for xml path('')),1,1,''),'')                              
------ FROM #TEMP1 t                                
------ GROUP BY 计划订单单号,预测单单号                              
------) B                         
------ON  A.FBILLNO=B.计划订单单号                              
------WHERE  A.F_FORECASTNUMBER=''                           
------AND A.FBILLNO = @FBILLNO                  
                              
                                      
                                     
------DROP TABLE #TEMP1                            
                          
                          
----UPDATE A SET A.F_CUSTOMERNAME=D.FCUSTID,A.F_MATERIALCODE=D.FMATERIALID                  
----from T_PLN_PLANORDER A                              
----INNER JOIN T_PLN_RESERVELINKENTRY B ON B.FSUPPLYBILLNO = A.FBILLNO                              
----INNER JOIN T_PLN_RESERVELINK C ON C.FID=B.FID                              
----INNER JOIN T_PLN_FORECASTENTRY D ON D.FENTRYID=C.FSRCENTRYID                              
----INNER JOIN T_PLN_FORECAST e on e .fid=d.FID                             
----where (a.F_CUSTOMERNAME=0 or a.F_MATERIALCODE=0 )                     
----AND A.FBILLNO = @FBILLNO                  
                        
                        
------UPDATE A SET A.F_Remark=ISNULL( dbo.GetForecast(A.F_FORECASTNUMBER  ,A.F_LINENUMBER),'')                      
------from T_PLN_PLANORDER A                             
------INNER JOIN T_PLN_FORECAST B ON B.FBILLNO=A.F_FORECASTNUMBER                        
------INNER JOIN T_PLN_FORECAST_L C ON C.FID=B.FID                        
------INNER JOIN T_PLN_FORECASTENTRY D on D.FID=B.FID AND D.FSEQ=A.F_Linenumber                        
------where ( a.F_Remark='' )                        
                        
                        
----UPDATE E SET F_DESCRIPTION=ISNULL(C.FDESCRIPTION ,'' )                      
----from T_PLN_PLANORDER A                  
----INNER JOIN T_PLN_PLANORDER_L E ON E.FID=A.FID                        
----INNER JOIN T_PLN_FORECAST B ON B.FBILLNO=A.F_FORECASTNUMBER                        
----INNER JOIN T_PLN_FORECAST_L C ON C.FID=B.FID                        
----where ( e.F_DESCRIPTION='' )                        
----AND A.FBILLNO = @FBILLNO                  
                  
                  
                  
                   
                  
----UPDATE T_PLN_PLANORDER  SET F_Remark=                  
---- ISNULL( dbo.GetForecast(A.F_FORECASTNUMBER  ,A.F_LINENUMBER),'')                     
----from T_PLN_PLANORDER A                     
----where                   
----1=1                   
----and ( a.F_Remark='' )                     
----AND   CONVERT(nvarchar(100),  a.FCREATEDATE,23)  =   CONVERT(nvarchar(100),  GETDATE(),23)                   
----AND A.FBILLNO = @FBILLNO                  
                                  
                  
                  
END 