Snowfun

导航

 

一、新增4张表OK

IN_BOM_CHANGE、IN_STOCK_CUSTOMER、IN_ORDER_PLANNING、IN_STOCK_LOCATION

CREATE TABLE STG.IN_BOM_CHANGE
(
  SO_ID           VARCHAR2(20 BYTE),
  SO_LINE_ID      VARCHAR2(10 BYTE),
  LOC_ID          VARCHAR2(5 BYTE),
  PARENT_ITEM_ID  VARCHAR2(30 BYTE),
  BOM_ID          VARCHAR2(50 BYTE),
  OLD_ITEM_ID     VARCHAR2(30 BYTE),
  OLD_QUANTITY    NUMBER,
  NEW_ITEM_ID     VARCHAR2(30 BYTE),
  NEW_QUANTITY    NUMBER,
  ACTION          VARCHAR2(10 BYTE),
  PERIOD_DATE     DATE
)
TABLESPACE STGDATA;


CREATE BITMAP INDEX STG.IDX_IN_BOM_CHANGE_DBA01 ON STG.IN_BOM_CHANGE
(ACTION)
NOLOGGING
TABLESPACE STGDATA
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          64K
            NEXT             1M
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
PARALLEL ( DEGREE 4 INSTANCES 1 );


CREATE TABLE STG.IN_STOCK_CUSTOMER
(
  CUSTOMER_ID  VARCHAR2(90 BYTE)                NOT NULL,
  NAME_ID      VARCHAR2(90 BYTE)                NOT NULL,
  ID           NUMBER(10)                       NOT NULL,
  LOCATION_ID  VARCHAR2(10 BYTE)
)
TABLESPACE STGDATA;



CREATE TABLE STG.IN_ORDER_PLANNING
(
  SO_ID           VARCHAR2(40 CHAR),
  SO_LINE_ID      VARCHAR2(40 CHAR),
  FG_ITEM_ID      VARCHAR2(40 CHAR),
  SEMI_ITEM_ID    VARCHAR2(40 CHAR),
  ORDER_PLANNING  INTEGER,
  CUSTOMER_ID     VARCHAR2(40 CHAR),
  VIP             INTEGER,
  WERKS           VARCHAR2(10 CHAR),
  ZREMARK1        VARCHAR2(50 CHAR),
  ZREMARK2        VARCHAR2(50 CHAR)
)
TABLESPACE STGDATA;

CREATE TABLE STG.IN_STOCK_LOCATION
(
  LOCATION_NAME  VARCHAR2(20 BYTE),
  LOCATION_ID    VARCHAR2(10 BYTE),
  WERKS          VARCHAR2(5 BYTE)
)
TABLESPACE STGDATA;
View Code

二、新建索引OK

ALTER TABLE ABPPMGR.MST_ITEMBOMROUTING  MODIFY   BOMID  VARCHAR2(100 BYTE);
ALTER TABLE ABPPMGR.MST_ITEMBOMROUTING_P  MODIFY   BOMID  VARCHAR2(100 BYTE);
ALTER TABLE ABPPMGR.SALESORDERLINE_P  MODIFY   ATTRIBUTECLASS  VARCHAR2(80 BYTE);
ALTER TABLE ABPPMGR.MST_BOMCOMPONENTS  MODIFY   BOMID  VARCHAR2(100 BYTE);
create index ABPPMGR.idx_MST_BOMCOMPONENTS_DBA01 on  ABPPMGR.MST_BOMCOMPONENTS(BOMID);
ALTER TABLE ABPPMGR.MST_BOMCOMPONENTS_P  MODIFY   BOMID  VARCHAR2(100 BYTE);
ALTER TABLE ABPPMGR.MST_BOMCOMPONENTSALT  MODIFY   BOMID  VARCHAR2(100 BYTE);
ALTER TABLE ABPPMGR.MST_BOMCOMPONENTSALT_P  MODIFY   BOMID  VARCHAR2(100 BYTE);
ALTER TABLE ABPPMGR.MST_BOMHEADER  MODIFY   BOMID  VARCHAR2(100 BYTE);
create bitmap index   idx_MST_BOMCOMPONENTS_dba02 on ABPPMGR.MST_BOMCOMPONENTS(SYS_CREATED_BY);
ALTER TABLE ABPPMGR.PRODUCTIONORDERS_P  MODIFY   ATTRIBUTECLASS  VARCHAR2(80 BYTE);


--ADD ON 20180607
ALTER TABLE ABPPMGR.PRODUCTIONORDERS  MODIFY   BOMID  VARCHAR2(100 BYTE);
ALTER TABLE ABPPMGR.SFCHEADER  MODIFY   BOMID  VARCHAR2(100 BYTE);
ALTER TABLE ABPPMGR.INVENTORYPROPERTY  MODIFY   ATTRIBUTECLASS  VARCHAR2(80 BYTE);


ALTER TABLE ABPPMGR.INVENTORY  MODIFY   LOTLEVEL1  VARCHAR2(80 BYTE);  --6.15号还原6.10的DB后,此加长字段取消
ALTER TABLE ABPPMGR.INVENTORYPROPERTY  MODIFY   LOCATIONID  VARCHAR2(80 BYTE);--6.15号还原6.10的DB后,此加长字段取消
ALTER TABLE ABPPMGR.MST_ITEMBOMROUTING  MODIFY   ROUTINGID  VARCHAR2(80 BYTE);--add on 20180711
ALTER TABLE ABPPMGR.MST_ITEMBOMROUTING_P  MODIFY   ROUTINGID  VARCHAR2(80 BYTE);--add on 20180711

三、创建存储过程SAP_VIP_YANHUO OK

CREATE OR REPLACE PROCEDURE STG.SAP_VIP_YANHUO(EXITCODE OUT NUMBER)
IS
   /***********************************************************************
    **  存储过程详细说明
    **  存储过程名称:  SAP_VIP_YANHUO
    **  存储过程创建日期: 2017/12/08
    **  存储过程创建人: LVXUEFEN
    **  目的:
    **   1.将ZFP037\ZFP026整合成到一张表IN_ORDER_PLANNING(数据来源SAP_037A\SAP_QM_CUSTOMER\SAP_QM_CUSTOMER_FAKE)
    **   2.将ZFP027A和ZFP025a整合进一个表IN_BOM_CHANGE
           SAP_BOM_CHANGE_ALT-->IN_BOM_CHANGE
           SAP_SO_BOM-->IN_BOM_CHANGE
    **  输入参数:
    **  输出参数: EXITCODE: 0=Success/1=Failure
    **  返回值: 
    **  用法: 
    ** IN_ORDER_PLANNING表中的ORDER_PLANNING代表是否是真驗貨 1:表示為真驗貨 0:為假驗貨;VIP字段:1代表VIP,0非VIP
    ** IN_BOM_CHANGE 變更方式:VIP:VIP換標簽 NEW:新增 DEL:刪除 MODIFY:修改
    *-\*+"P;9U7YTR+ 修订版本:
    **  版本号     修订时间    修订人        描述
    **  ---------  ----------  ------------  ------------------------
    **    1.0     2017/12/08  lvxuefen    1. 新建存储过程.
    
   **********************************************************************/
   
      ----------------------- 以下定义存储过程使用的变量---------------------------------
   E_ErrMessage   VARCHAR2 (3000);
   
BEGIN
   EXITCODE := 0;
   
   EXECUTE IMMEDIATE 'TRUNCATE TABLE IN_ORDER_PLANNING';
   
    ----一、1.查找VIP客户成品编码的下层半成品编码(有BOM)
    INSERT INTO IN_ORDER_PLANNING(SO_ID,  SO_LINE_ID ,  FG_ITEM_ID ,  SEMI_ITEM_ID,  ORDER_PLANNING ,  CUSTOMER_ID ,VIP,WERKS )
    SELECT distinct ISO.SO_ID,ISO.SO_LINE_ID, VIP.CMATNR,DT.ITEM_ID,'',VIP.KUNNR,'1',ISO.SITEID
    from SAP_037A VIP, IN_BOM_DETAILS DT, IN_BOM_HEADER HD,IN_SALES_ORDER ISO
     WHERE    VIP.CMATNR= HD.PRODUCT_ID  AND  DT.BOM_ID = HD.BOM_ID AND ISO.ITEM_ID=VIP.CMATNR AND ISO.CUSTOMER_ID=VIP.KUNNR
            AND HD.IS_VALID = '1'and VIP.WERKS= SUBSTR(HD.BOM_ID,instr(HD.BOM_ID,'_',1)+1 ,'4') --SUBSTR(HD.BOM_ID,10,4)
            AND  DT.ITEM_ID LIKE '000000102%' ;
    COMMIT;
    
    --既是VIP又是真验货
    MERGE INTO IN_ORDER_PLANNING IOP
        USING SAP_QM_CUSTOMER  A
        ON (IOP.SEMI_ITEM_ID=A.MATNR AND IOP.CUSTOMER_ID=A.KUNNR)
    WHEN MATCHED THEN 
    UPDATE SET IOP.ORDER_PLANNING='1' WHERE VIP=1;
    COMMIT;
    
    --既是VIP又是假验货
    MERGE INTO IN_ORDER_PLANNING IOP
        USING SAP_QM_CUSTOMER_fake  A
        ON (IOP.SEMI_ITEM_ID=A.MATNR AND IOP.CUSTOMER_ID=A.KUNNR)
    WHEN MATCHED THEN 
    UPDATE SET IOP.ORDER_PLANNING='0' WHERE VIP=1;
    COMMIT;
    
    --真假验假UI表中没有维护,则VIP客户一定是假验货
    UPDATE IN_ORDER_PLANNING SET ORDER_PLANNING='0' WHERE  ORDER_PLANNING IS  NULL;
    COMMIT;
    
    ---真验货
    INSERT INTO IN_ORDER_PLANNING(SO_ID,  SO_LINE_ID ,  FG_ITEM_ID ,  SEMI_ITEM_ID,  ORDER_PLANNING ,  CUSTOMER_ID ,VIP,WERKS  )
    SELECT distinct ISO.SO_ID,ISO.SO_LINE_ID, HD.PRODUCT_ID,ZYH.MATNR,'1',ZYH.KUNNR,'',ISO.SITEID
        from SAP_QM_CUSTOMER ZYH, IN_BOM_DETAILS DT, IN_BOM_HEADER HD,IN_SALES_ORDER ISO
         WHERE    ZYH.MATNR= DT.ITEM_ID  AND  DT.BOM_ID = HD.BOM_ID AND ISO.ITEM_ID=HD.PRODUCT_ID AND ISO.CUSTOMER_ID=ZYH.KUNNR
                AND HD.IS_VALID = '1'  
                AND ISO.SO_ID || SO_LINE_ID  NOT IN (SELECT SO_ID || SO_LINE_ID FROM IN_ORDER_PLANNING);
    COMMIT;                
                
    ---假验货
    INSERT INTO IN_ORDER_PLANNING(SO_ID,  SO_LINE_ID ,  FG_ITEM_ID ,  SEMI_ITEM_ID,  ORDER_PLANNING ,  CUSTOMER_ID ,VIP,WERKS  )
    SELECT distinct ISO.SO_ID,ISO.SO_LINE_ID, HD.PRODUCT_ID,ZYH.MATNR,'0',ZYH.KUNNR,'',ISO.SITEID
        from SAP_QM_CUSTOMER_fake ZYH, IN_BOM_DETAILS DT, IN_BOM_HEADER HD,IN_SALES_ORDER ISO
         WHERE    ZYH.MATNR= DT.ITEM_ID  AND  DT.BOM_ID = HD.BOM_ID AND ISO.ITEM_ID=HD.PRODUCT_ID AND ISO.CUSTOMER_ID=ZYH.KUNNR
                AND HD.IS_VALID = '1'  
                AND ISO.SO_ID || SO_LINE_ID  NOT IN (SELECT SO_ID || SO_LINE_ID FROM IN_ORDER_PLANNING);                
     COMMIT;
     
     --真假验假UI表中没有维护,则VIP客户一定是假验货
    UPDATE IN_ORDER_PLANNING SET VIP='0' WHERE  VIP IS  NULL;
    COMMIT;
    
    --针对外购半成品验货报short的问题(外购半成品不带SO号,无法按单生产)add by landor on 20180806
    DELETE FROM IN_ORDER_PLANNING WHERE SEMI_ITEM_ID LIKE '00000010304%';
    COMMIT;
                 
    /*--1.无BOM
    INSERT INTO IN_ORDER_PLANNING(MANDT,  KUNNRV ,  CMATNR ,  BMATNR,  MENGE ,  SIGEN ,  WERKS ,HASBOM)
    SELECT VIP.* ,'N' HASBOM FROM SAP_037A VIP WHERE NOT EXISTS(SELECT * FROM IN_ORDER_PLANNING A WHERE A.CMATNR=VIP.CMATNR AND VIP.WERKS=A.WERKS );
    --and VIP.CMATNR='000000101002001847' ;

    COMMIT;
     
    --2.将真验货表数据与IN_ORDER_PLANNING关联,匹配成功的数据标识为1,用于后面将IN_ORDER_PLANNING表的MATNR更新为空
     MERGE INTO IN_ORDER_PLANNING VIP
    USING SAP_QM_CUSTOMER  A
        ON (VIP.MATNR=A.MATNR AND VIP.KUNNRV=A.KUNNR)
    WHEN MATCHED THEN 
    UPDATE SET VIP.ZFLAG2 = '1',VIP.KUNNR=A.KUNNR,VIP.ROW_ID=A.ROW_ID,VIP.ZFLAG=A.ZFLAG;
    COMMIT;
    
    
    update SAP_QM_CUSTOMER_FAKE set matnr='000000102001001122' where matnr='000000102002000563' and kunnr='R3540';--用于测试
    
    
    ---2.将假验货表数据与IN_ORDER_PLANNING关联,匹配成功的数据标识为2,用于后面将IN_ORDER_PLANNING表的MATNR更新为空
    MERGE INTO IN_ORDER_PLANNING VIP
    USING SAP_QM_CUSTOMER_FAKE  A
        ON (VIP.MATNR=A.MATNR  AND VIP.KUNNRV=A.KUNNR)
    WHEN MATCHED THEN 
    UPDATE SET VIP.ZFLAG2 = '2' ,VIP.KUNNR=A.KUNNR,VIP.ROW_ID=A.ROW_ID;
    COMMIT;
  
    --3.将不符合条件 的半成品编码删除掉(ZFLAG2状态不为1的数据或者为空的数据)
    UPDATE IN_ORDER_PLANNING SET MATNR=''  WHERE ZFLAG2 IS NULL;
    COMMIT;
    
    --4.将余下的真验货数据写入IN_ORDER_PLANNING表
    INSERT INTO IN_ORDER_PLANNING(KUNNR,MATNR,ROW_ID,ZFLAG)
    SELECT A.* FROM SAP_QM_CUSTOMER  A WHERE NOT EXISTS(SELECT * FROM IN_ORDER_PLANNING  VIP  WHERE VIP.MATNR=A.MATNR AND VIP.KUNNRV=A.KUNNR );
    COMMIT;
    
     --4.将余下的假验货数据写入IN_ORDER_PLANNING表
    INSERT INTO IN_ORDER_PLANNING(KUNNR,MATNR,ROW_ID)
    SELECT KUNNR,MATNR,ROW_ID FROM SAP_QM_CUSTOMER_FAKE  A WHERE NOT EXISTS(SELECT * FROM IN_ORDER_PLANNING  VIP  WHERE VIP.MATNR=A.MATNR AND VIP.KUNNRV=A.KUNNR );
    COMMIT;
*/

   -------二、将ZFP027A和ZFP025a整合进一个表IN_BOM_CHANGE      
  --TRUNCATE IN_BOM_CHANGE
  EXECUTE IMMEDIATE 'TRUNCATE TABLE IN_BOM_CHANGE'; 
  
  
  
  /* hide by landor on 20180727  
     --INSERT THE DATA
     INSERT INTO IN_BOM_CHANGE(
       SO_ID 
      ,SO_LINE_ID
      ,LOC_ID
      ,PARENT_ITEM_ID
      ,BOM_ID
      ,OLD_ITEM_ID
      --,OLD_QUANTITY
      ,NEW_ITEM_ID
      --,NEW_QUANTITY
      ,PERIOD_DATE
      ,ACTION   
    )
SELECT DISTINCT      ISO.SO_ID
                    ,ISO.SO_LINE_ID
                    ,ST.WERKS
                    ,ST.PMATNR
                    ,ISO.BOMID
                    ,ST.IDNRK
                    ,ST.CMATNR
                    ,ST.DATUB
                    ,'MODIFY'
     FROM SAP_BOM_CHANGE_ALT ST 
      JOIN IN_SALES_ORDER ISO
       ON ST.PMATNR=ISO.ITEM_ID
    UNION
    --半成品编码,先用半成品编码在BOM中找出对应所有成品编码,再用成品编码去找所有对应未清SO明细
   SELECT DISTINCT ISO.SO_ID
                   ,ISO.SO_LINE_ID
                   ,ST.WERKS
                   ,ST.PMATNR
                   ,IH.BOM_ID
                   ,ST.IDNRK
                   ,ST.CMATNR
                   ,ST.DATUB
                   ,'MODIFY'
     FROM SAP_BOM_CHANGE_ALT ST
      JOIN IN_BOM_HEADER IH
       ON ST.PMATNR=IH.PRODUCT_ID
      JOIN IN_BOM_DETAILS IB
       ON IB.ITEM_ID=ST.PMATNR
      JOIN IN_BOM_HEADER IR
       ON IB.BOM_ID=IR.BOM_ID
      JOIN IN_SALES_ORDER ISO
       ON  IR.PRODUCT_ID=ISO.ITEM_ID   
    WHERE SUBSTR(LTRIM(ST.PMATNR,0),1,3)
    NOT IN('101','401')
    UNION
  --直接取SAP_SO_BOM表数据,有效期按照最早的soid交期
    SELECT ISO.SO_ID
          ,ISO.SO_LINE_ID
          ,SB.SITE_ID
          ,SB.PITEM
          ,ISO.BOMID
          ,SB.CITEM
          ,SB.N_CITEM
          ,MIN(NVL(ISO.UDF_PROMD,ISO.UDF_CRSD))PERIODDATE
          ,'MODIFY'
           FROM SAP_SO_BOM SB
    JOIN IN_SALES_ORDER ISO
      ON SB.SO_ID=ISO.SO_ID
     AND LTRIM(SB.SO_LINE_ID,0)=SUBSTR(ISO.SO_LINE_ID,1,INSTR(ISO.SO_LINE_ID,'_')-1)
    GROUP BY ISO.SO_ID
            ,ISO.SO_LINE_ID
            ,SB.SITE_ID
            ,SB.PITEM
            ,SB.CITEM
            ,SB.N_CITEM
            ,ISO.BOMID
            ,'MODIFY';

COMMIT;
--VIP标签的加减  成品对应的标识减 DEL 半成品对应的表示新增NEW
 INSERT INTO IN_BOM_CHANGE(
       SO_ID 
      ,SO_LINE_ID
      ,LOC_ID
      ,PARENT_ITEM_ID
      ,BOM_ID
      ,OLD_ITEM_ID
      ,OLD_QUANTITY
      ,NEW_ITEM_ID
      ,NEW_QUANTITY
      ,PERIOD_DATE
      ,ACTION   
    )
    --成品减标签标识 DEL
SELECT DISTINCT IP.SO_ID
               ,IP.SO_LINE_ID
               ,IP.WERKS
               ,IP.FG_ITEM_ID
               ,IH.BOM_ID
               ,SA.BMATNR OLD_ITEM_ID
               ,NULL NEW_QUANTITY
               ,NULL NEW_ITEM_ID
               ,NULL OLD_QUANTITY  
               ,MIN(NVL(ISO.UDF_PROMD,ISO.UDF_CRSD))PERIODDATE            
               ,'DEL' ACTION
               
FROM 
IN_ORDER_PLANNING IP
JOIN SAP_037A SA
ON IP.FG_ITEM_ID=SA.CMATNR
AND IP.WERKS=SA.WERKS
JOIN IN_BOM_HEADER IH
ON IP.FG_ITEM_ID=IH.PRODUCT_ID
AND IP.WERKS=SUBSTR(IH.BOM_ID,INSTR(IH.BOM_ID,'_')+1,INSTR(IH.BOM_ID,'_',2,1)-5)
JOIN IN_SALES_ORDER ISO
ON IP.SO_ID=ISO.SO_ID
AND IP.SO_LINE_ID=ISO.SO_LINE_ID
 WHERE IP.VIP=1 AND LENGTH(TRIM(BOM_ID))<=16
 GROUP BY       IP.SO_ID
               ,IP.SO_LINE_ID
               ,IP.WERKS
               ,IP.FG_ITEM_ID
               ,IH.BOM_ID
               ,SA.BMATNR
               ,NULL
               ,NULL
               ,NULL            
               ,'DEL'
UNION
--半成品新增标签标识 NEW
SELECT DISTINCT IP.SO_ID
               ,IP.SO_LINE_ID
               ,IP.WERKS
               ,IP.SEMI_ITEM_ID
               ,IH.BOM_ID
               ,NULL OLD_ITEM_ID
               ,NULL NEW_QUANTITY
               ,SA.BMATNR NEW_ITEM_ID
               ,NULL OLD_QUANTITY     
               ,MIN(NVL(ISO.UDF_PROMD,ISO.UDF_CRSD))PERIODDATE         
               ,'NEW' ACTION
               
FROM 
IN_ORDER_PLANNING IP
JOIN SAP_037A SA
ON IP.FG_ITEM_ID=SA.CMATNR
AND IP.WERKS=SA.WERKS
JOIN IN_BOM_HEADER IH
ON IP.SEMI_ITEM_ID=IH.PRODUCT_ID
AND IP.WERKS=SUBSTR(IH.BOM_ID,INSTR(IH.BOM_ID,'_')+1,INSTR(IH.BOM_ID,'_',2,1)-5)
JOIN IN_SALES_ORDER ISO
ON IP.SO_ID=ISO.SO_ID
AND IP.SO_LINE_ID=ISO.SO_LINE_ID
 WHERE IP.VIP=1 AND LENGTH(TRIM(BOM_ID))<=16
 GROUP BY       IP.SO_ID
               ,IP.SO_LINE_ID
               ,IP.WERKS
               ,IP.SEMI_ITEM_ID
               ,IH.BOM_ID
               ,SA.BMATNR
               ,NULL
               ,NULL
               ,NULL            
               ,'NEW'
;
COMMIT;
*/

--EXCEPTIONS
EXCEPTION
   WHEN OTHERS
   THEN
      ROLLBACK;

      e_ErrMessage :=
            SUBSTR (SQLERRM, 1, 1000)
         || SUBSTR (DBMS_UTILITY.FORMAT_ERROR_BACKTRACE, 1, 2000);
      PKG_UTIL.CREATELOG ('IN_ORDER_PLANNING',
                          'IN_ORDER_PLANNING',
                          '1',
                          'INSERT',
                          PKG_CONSTANT.CONS_MSG_EXCEPTION,
                          e_ErrMessage,
                          'IN_ORDER_PLANNING');

      EXITCODE := 1;
END;
/
View Code

 

四、SSIS导数

INSERT INTO IN_STOCK_LOCATION(
WERKS,LOCATION_ID,LOCATION_NAME)
SELECT WERKS,LGORT,LGOBE FROM ZTPP_FP_046@SAP_SEP;


INSERT INTO IN_STOCK_CUSTOMER(CUSTOMER_ID,
                              NAME_ID,
                              ID,
                           LOCATION_ID)
                   SELECT KUNNR 
                         ,ZQUNZ
                         ,ZINX
,LGORT
FROM  SAPSR3.ZTPP_FP_042@SAP_SEP

SAP_SEMI_ONHAND_LOCK

按单生产,不考虑尾续、过滤没有需求的半成品库存数据

CREATE OR REPLACE PROCEDURE STG.SAP_SEMI_ONHAND_LOCK (EXITCODE OUT NUMBER)
IS
   /***********************************************************************
         **  存储过程名称:  SAP_SEMI_ONHAND_LOCK
        **  存储过程创建日期: 2014/6/30
        **  存储过程创建人: huangyangxiong
        **  目的:  半成品库存绑定订单
        **  输入参数: 无
        **  输出参数:EXITCODE 预留接口给外部调用者,让其知道存储过程的执行结果,0.预定逻辑执行完成; 1.遇到异常,预定逻辑未执行完成
        **  返回值:
        **  用法:
        **
        ** 修订版本:
        **  版本号     修订时间    修订人        描述
        **  ---------  ----------  ------------  ------------------------
        **    1.0      2014/6/30   HuangYangXiong 1. 新建
        **    1.1      2016/06/21  Landor         汇总数量 
        **   1.2      2018/07/24   Landor       按单优化项目需要
   **********************************************************************/

   E_ErrMessage   VARCHAR2 (3000);
BEGIN
   EXITCODE := 0;

   --清空IN表数据
   EXECUTE IMMEDIATE 'truncate table IN_SEMI_ONHAND_LOCKED';

   --写入数据
   INSERT INTO IN_SEMI_ONHAND_LOCKED (PEGGED_SO_ID,
                                      PEGGED_SO_LINE_ID,
                                      ITEM_ID,
                                      QUANTITY,
                                      LOCATION,
                                      SITEID,
                                      WZNUM
                                      )
      SELECT SAP_MSKA.VBELN,
             SAP_MSKA.POSNR,
             SAP_MSKA.MATNR,
              sum(SAP_MSKA.KALAB+SAP_MSKA.KAINS+SAP_MSKA.KASPE) ,--SAP_MSKA.KALAB+SAP_MSKA.KAINS+SAP_MSKA.KASPE,
             SAP_MSKA.WERKS||'_'||SAP_MSKA.LGORT,
             SAP_MSKA.WERKS
             ,SUBSTR(SAP_MSKA.MATNR,19,2) 
        FROM SAP_MSKA
       WHERE EXISTS
                (SELECT NULL
                   FROM SAP_MARA
                  WHERE     SUBSTR(TRIM(SAP_MSKA.MATNR),1,18) = SAP_MARA.MATNR AND  
                         (SUBSTR(TRIM(SAP_MSKA.MATNR),1,18) -- (TRIM(SAP_MSKA.MATNR)    --alter by landor on 20180723按单生产,不考虑尾续 
                            IN (SELECT IIS.ITEM_ID FROM STG.IN_ITEM_SITE IIS WHERE SUBSTR(IIS.ITEM_ID,7,3) IN('102') AND TRIM(IIS.PROC_TYPE)='E') 
                            OR(TO_NUMBER(SUBSTR(TRIM(SAP_MSKA.MATNR),7,6))>402500 AND TO_NUMBER(SUBSTR(TRIM(SAP_MSKA.MATNR),7,6))<=402999)))
         --ADD BY HUANGYANGXIONG ON 20160823  销售订单下半成品与对应的半成品订单库存绑定不需要进入FP计算
         AND EXISTS(SELECT NULL FROM SAP_VBAP VBAP WHERE SAP_MSKA.VBELN = VBAP.VBELN AND SAP_MSKA.POSNR = VBAP.POSNR /*AND SAP_MSKA.MATNR<>VBAP.MATNR*/)            
         AND SAP_MSKA.KALAB+SAP_MSKA.KAINS+SAP_MSKA.KASPE>0
         group by SAP_MSKA.VBELN,SAP_MSKA.POSNR,SAP_MSKA.MATNR,SAP_MSKA.WERKS,SAP_MSKA.WERKS,SAP_MSKA.LGORT;-- add by Landor on 2016/06/21


   COMMIT;
   
   
   --add不要zhangguipeng on 20170411 监控当天原表数据
   EXECUTE IMMEDIATE 'TRUNCATE TABLE IN_SEMI_ONHAND_LOCKED_JIANKONG';

   INSERT INTO IN_SEMI_ONHAND_LOCKED_JIANKONG (PEGGED_SO_ID,
                                      PEGGED_SO_LINE_ID,
                                      ITEM_ID,
                                      QUANTITY,
                                      LOCATION,
                                      SITEID, WZNUM)
      SELECT SAP_MSKA.VBELN,
             SAP_MSKA.POSNR,
             SAP_MSKA.MATNR,
              sum(SAP_MSKA.KALAB+SAP_MSKA.KAINS+SAP_MSKA.KASPE) ,--SAP_MSKA.KALAB+SAP_MSKA.KAINS+SAP_MSKA.KASPE,
             SAP_MSKA.WERKS||'_'||SAP_MSKA.LGORT,
             SAP_MSKA.WERKS
             ,SUBSTR(SAP_MSKA.MATNR,19,2)
        FROM SAP_MSKA
      WHERE EXISTS
                (SELECT NULL
                   FROM SAP_MARA
                  WHERE     SUBSTR(TRIM(SAP_MSKA.MATNR),1,18) = SAP_MARA.MATNR AND  
                         (SUBSTR(TRIM(SAP_MSKA.MATNR),1,18) -- (TRIM(SAP_MSKA.MATNR)    --alter by landor on 20180723按单生产,不考虑尾续 
                            IN (SELECT IIS.ITEM_ID FROM STG.IN_ITEM_SITE IIS WHERE SUBSTR(IIS.ITEM_ID,7,3) IN('102') AND TRIM(IIS.PROC_TYPE)='E') 
                            OR(TO_NUMBER(SUBSTR(TRIM(SAP_MSKA.MATNR),7,6))>402500 AND TO_NUMBER(SUBSTR(TRIM(SAP_MSKA.MATNR),7,6))<=402999)))
         --ADD BY HUANGYANGXIONG ON 20160823  销售订单下半成品与对应的半成品订单库存绑定不需要进入FP计算
         AND EXISTS(SELECT NULL FROM SAP_VBAP VBAP WHERE SAP_MSKA.VBELN = VBAP.VBELN AND SAP_MSKA.POSNR = VBAP.POSNR /*AND SAP_MSKA.MATNR<>VBAP.MATNR*/)            
         AND SAP_MSKA.KALAB+SAP_MSKA.KAINS+SAP_MSKA.KASPE>0
         group by SAP_MSKA.VBELN,SAP_MSKA.POSNR,SAP_MSKA.MATNR,SAP_MSKA.WERKS,SAP_MSKA.WERKS,SAP_MSKA.LGORT;-- add by Landor on 2016/06/21

   COMMIT;
   
   
   --过滤没有需求的半成品库存数据add by landor on 20180724
   DELETE FROM IN_SEMI_ONHAND_LOCKED A 
    WHERE NOT EXISTS (SELECT NULL FROM IN_SALES_ORDER S WHERE A.PEGGED_SO_ID=S.SO_ID AND SUBSTR(S.SO_LINE_ID,1,INSTR(S.SO_LINE_ID,'_')-1)=A.PEGGED_SO_LINE_ID )
    AND A.WZNUM IS NOT NULL; --只针对于真验货客户
    COMMIT;
    
   
   
EXCEPTION
   --EXCEPTIONS
   WHEN OTHERS
   THEN
      BEGIN
         ROLLBACK;
         EXITCODE := 1;

         e_ErrMessage :=
               SUBSTR (SQLERRM, 1, 1000)
            || SUBSTR (DBMS_UTILITY.FORMAT_ERROR_BACKTRACE, 1, 2000);
         PKG_UTIL.CREATELOG ('SAP_SEMI_ONHAND_LOCK',
                             'SAP_SEMI_ONHAND_LOCK',
                             '1',
                             'INSERT',
                             PKG_CONSTANT.CONS_MSG_EXCEPTION,
                             e_ErrMessage,
                             'IN_SEMI_ONHAND_LOCKED');
      END;
END;
/
View Code

SAP_SO_BOM_OUTSOURCING_SEMI

去掉尾续、增加SAP_VIP_YANHUO、将IN_INVENTORY和IN_SEMI_ONHAND_LOCKED去掉尾续

CREATE OR REPLACE PROCEDURE STG.SAP_SO_BOM_OUTSOURCING_SEMI(EXITCODE OUT NUMBER)
IS
   /***********************************************************************
         **  存储过程详细说明
    **  存储过程名称:  SAP_SO_BOM_OUTSOURCING_SEMI
    **  存储过程创建日期: 2014/12/09
    **  存储过程创建人: HuangYangXiong
    **  目的:  针对委外半成品的处理方案,新建一个虚拟的编码,指定下层使用的BOM物料,调用SAP_MANUAL_ITEM_BOM_ROUTING存储过程
    **  输入参数:
    **  输出参数: EXITCODE: 0=Success/1=Failure
    **  返回值:
    **  用法:
    **
    ** 修订版本:
    **  版本号     修订时间    修订人        描述
        **  ---------  ----------  ------------  ------------------------
        **    1.0      2014/12/09    Huangyangxiong 1.新建存储过程
        **   2.0      2018/3/15    Landor 重新优化验货客户的偌,将所有循环体拆分成多个存储过程
        **   3.0      2018/7/24     取消真验货数据带尾续功能,改为按单功能
   **********************************************************************/


   V_STEP            NUMBER := 0;
   V_PKGNAME         VARCHAR2 (80) := 'SAP_SO_BOM_OUTSOURCING_SEMI';
   V_PRONAME         VARCHAR2 (80) := 'SAP_SO_BOM_OUTSOURCING_SEMI';
   V_ACTION          VARCHAR2 (1);
   V_MAINTABLE       VARCHAR2 (80);
   EXITCODE1         NUMBER:=0;
   e_ErrMessage      VARCHAR2(3000);
   CTYPE             VARCHAR2(20);
   EXITCODE2         NUMBER:=0;
BEGIN
    EXITCODE:=0;
   -- CTYPE:='OS';
    
    --ADD BY HUANGYANGXIONG ON 20150310 新增处理同一个BOM下同一替代组不同编码分配给不同订单使用的情况,需要新增一套BOM数据
    MERGE INTO SAP_SO_BOM_MOD M1
USING(SELECT SUBSTR (S.ITEM_ID, 1, 18) ITEM_ID,
       SUBSTR (S.BOMID, INSTR (S.BOMID, '_') + 1, 4) SITE_ID,
       B.MATNR,
       B.ALPGR,
       S.SO_ID,
       S.SO_LINE_ID,
       ROW_NUMBER() OVER(PARTITION BY SUBSTR (S.ITEM_ID, 1, 18) ORDER BY S.SO_ID,S.SO_LINE_ID,SUBSTR (S.ITEM_ID, 1, 18),B.ALPGR,B.MATNR) RN
  FROM SAP_SO_BOM_MOD B, IN_SALES_ORDER S
 WHERE     B.VBELN = S.SO_ID
       AND B.POSNR = SUBSTR (S.SO_LINE_ID, 1, INSTR (S.SO_LINE_ID, '_') - 1)
       AND NOT EXISTS
              (SELECT NULL
                 FROM IN_MO MO
                WHERE S.SO_ID = MO.SO_ID AND S.SO_LINE_ID = MO.SO_LINE_ID)
       AND NOT EXISTS
                  (SELECT NULL
                     FROM IN_SFCHEADER SFC
                    WHERE     S.SO_ID = SFC.SO_ID
                          AND SUBSTR (S.SO_LINE_ID,
                                      1,
                                      INSTR (S.SO_LINE_ID, '_') - 1) =
                                 SFC.SO_LINE_ID)
       AND EXISTS
              (SELECT NULL
                 FROM IN_BOM_HEADER H
                WHERE     H.PRODUCT_ID = SUBSTR (S.ITEM_ID, 1, 18)
                      AND H.BOM_ID = S.BOMID)
       AND EXISTS
              (SELECT NULL
                 FROM IN_BOM_DETAILS D
                WHERE     S.BOMID = D.BOM_ID
                      AND D.COMPONENTGROUP = B.ALPGR
                      AND D.ITEM_ID = B.MATNR)) M2
ON (M1.VBELN = M2.SO_ID AND
    M1.POSNR = SUBSTR(M2.SO_LINE_ID,1,INSTR(M2.SO_LINE_ID,'_')-1) AND
    M1.MATNR = M2.MATNR AND
    M1.ALPGR = M2.ALPGR)
WHEN MATCHED THEN
UPDATE SET M1.ROWNO = M2.RN;
    
    --第一步,查出维护了外挂表,需要处理的订单
    V_STEP:=V_STEP+1;
    V_ACTION:='I';
    V_MAINTABLE:='C_SO';

    FOR C_SO IN (SELECT  SUBSTR(S.ITEM_ID,1,18) ITEM_ID,SUBSTR(S.BOMID,INSTR(S.BOMID,'_')+1,4) SITE_ID,B.MATNR,B.ALPGR,S.SO_ID,S.SO_LINE_ID,B.ROWNO
    FROM SAP_SO_BOM_MOD B,IN_SALES_ORDER S
    WHERE B.VBELN = S.SO_ID
      AND B.POSNR = SUBSTR(S.SO_LINE_ID,1,INSTR(S.SO_LINE_ID,'_')-1)
      AND NOT EXISTS (SELECT NULL FROM IN_MO MO WHERE S.SO_ID = MO.SO_ID AND S.SO_LINE_ID = MO.SO_LINE_ID)
      AND NOT EXISTS (SELECT NULL FROM IN_SFCHEADER SFC WHERE S.SO_ID = SFC.SO_ID AND SUBSTR(S.SO_LINE_ID,1,INSTR(S.SO_LINE_ID,'_')-1) = SFC.SO_LINE_ID)
      AND EXISTS (SELECT NULL FROM IN_BOM_HEADER H WHERE H.PRODUCT_ID = SUBSTR(S.ITEM_ID,1,18) AND H.BOM_ID = S.BOMID )
      AND EXISTS (SELECT NULL FROM IN_BOM_DETAILS D WHERE S.BOMID = D.BOM_ID AND D.COMPONENTGROUP = B.ALPGR AND D.ITEM_ID = B.MATNR))
    LOOP
    
    --第二步,如果存在要处理的SO和SO行号,开始循环。考虑到维护外挂表时,有可能会出现一张SO行号对应多个物料的情况,所以用循环来处理
    V_STEP:=V_STEP+1;
    V_ACTION:='I';
    V_MAINTABLE:='SAP_MANUAL_ITEM_BOM_ROUTING';
    
    IF(TRIM(C_SO.SO_ID) IS NOT NULL AND TRIM(C_SO.SO_LINE_ID) IS NOT NULL)
    THEN
    CTYPE:='OS'||C_SO.ROWNO;
    
    --新增所需要的物料数据,BOM数据以及工艺路,物料BOM工艺路线数据对应
    SAP_MANUAL_ITEM_BOM_ROUTING(C_SO.ITEM_ID,C_SO.SITE_ID,C_SO.MATNR,C_SO.ALPGR,CTYPE,EXITCODE1);
    
    --更新销售订单的物料编码和BOM编码
    UPDATE IN_SALES_ORDER S SET S.ITEM_ID = ITEM_ID || CTYPE,S.BOMID = TRIM(BOMID) || CTYPE 
    WHERE EXISTS (SELECT NULL FROM SAP_SO_BOM_MOD B WHERE B.VBELN = S.SO_ID AND B.POSNR = SUBSTR(S.SO_LINE_ID,1,INSTR(S.SO_LINE_ID,'_')-1))
      AND EXISTS (SELECT NULL FROM IN_ITEM I WHERE S.ITEM_ID||CTYPE = I.ITEM_ID)
      AND EXISTS (SELECT NULL FROM IN_BOM_HEADER H,IN_BOM_DETAILS D WHERE H.BOM_ID = D.BOM_ID AND TRIM(S.BOMID)||CTYPE = D.BOM_ID)
      AND EXISTS (SELECT NULL FROM IN_ITEMBOMROUTING R WHERE TRIM(S.BOMID)||CTYPE = R.BOM_ID AND S.ITEM_ID||CTYPE = R.PRODUCED_ITEM_ID )
      AND NOT EXISTS (SELECT NULL FROM IN_MO MO WHERE S.SO_ID = MO.SO_ID AND SUBSTR(S.SO_LINE_ID,1,INSTR(S.SO_LINE_ID,'_')-1) = MO.SO_LINE_ID)
      AND NOT EXISTS (SELECT NULL FROM IN_SFCHEADER SFC WHERE S.SO_ID = SFC.SO_ID AND SUBSTR(S.SO_LINE_ID,1,INSTR(S.SO_LINE_ID,'_')-1) = SFC.SO_LINE_ID)
      AND S.SO_ID = C_SO.SO_ID AND S.SO_LINE_ID = C_SO.SO_LINE_ID
      ;
      
    COMMIT;
    
    END IF;
    
    END LOOP;
    
    --第三步,更新SO的编码和BOM_ID
    V_STEP:=V_STEP+1;
    V_ACTION:='U';
    V_MAINTABLE:='IN_SALES_ORDER';
   
   
    -- 处理验货客户的逻辑
    --SAP_SO_QM_CUSTOMER(EXITCODE2);此存储过程可删除
    --处理验货客户的逻辑
    --ADD BY Landor on 20180317 解决SAP_SO_QM_CUSTOMER存储过程无法加尾续问题,单独存储过程进行修改
    --SAP_SO_QM_CUSTOMER_ADDTYPE(EXITCODE);此存储过程可删除
  

  /* --add by landor on 2018035将每个循环体环拆分开为多个存储过程
    --hide by landor on 20180724 按单功能需要,将带尾续的代码取消
    
    SAP_SO_QM_CUSTOMER_ADDITEM(EXITCODE);
    SAP_SO_QM_CUSTOMER_ADDBOM(EXITCODE);
    --SAP_SO_QM_CUSTOMER_ADDBOMWG(EXITCODE);--外购半成品BOM处理  hide by landor on 20180328
    SAP_SO_QM_CUSTOMER_ADDROUTING(EXITCODE);
    SAP_SO_QM_CUSTOMER_ADDSO(EXITCODE); 
    --SAP_SO_QM_CUSTOMER_MO(EXITCODE);此存储过程可删除
    SAP_SO_QM_CUSTOMER_ADDMO(EXITCODE);
    SAP_SO_QM_CUSTOMER_ADDSFC(EXITCODE);
    SAP_SO_QM_CUSTOMER_UPOTHER(EXITCODE);
    */
    
    
    
    --增加CA数据 半成品CA 带SO未取到库存 add by landor on 20170505 
    SAP_SEMI_ONHAND_LOCK(EXITCODE);
    
    --按单功能需要,增加UI区分按单、VIP换标签的数据add by landor on 20180724
    SAP_VIP_YANHUO(EXITCODE);
    
    
    --ADD BY HUANGYANGXIONG ON 20150315   如果库存数据里面不存在维护的半成品库存绑定SO的数据时,把这部分数据当作库存插入到库存表中
     MERGE INTO IN_INVENTORY V1
       USING(
            SELECT OL.ITEM_ID,OL.LOCATION,SUM(OL.QUANTITY) AS QUANTITY,II.UDF_MEINS
            FROM IN_SEMI_ONHAND_LOCKED OL,IN_ITEM II
            WHERE SUBSTR(OL.ITEM_ID,1,18 ) = II.ITEM_ID  --alter by landor on 20180724按单需求修改
            GROUP BY OL.ITEM_ID,OL.LOCATION,II.UDF_MEINS
       ) V2
       ON(V1.ITEM_ID = V2.ITEM_ID AND
          V1.LOC_ID = V2.LOCATION )
       WHEN NOT MATCHED THEN
       INSERT (V1.ITEM_ID,
               V1.LOC_ID,
               V1.ONHAND_QTY,
               V1.QTYUOM)
       VALUES (V2.ITEM_ID,
               V2.LOCATION,
               V2.QUANTITY,
               V2.UDF_MEINS);
       COMMIT;

 
    --将物料编码去掉尾续 add by landor on 20180724
    UPDATE IN_INVENTORY SET  EXT_FORTEXT1=SUBSTR(ITEM_ID,19,2),ITEM_ID=SUBSTR(ITEM_ID,1,18) WHERE ITEM_ID LIKE '%C%';
    COMMIT;
    UPDATE IN_SEMI_ONHAND_LOCKED SET ITEM_ID =SUBSTR(ITEM_ID,1,18) WHERE ITEM_ID LIKE '%C%';
    COMMIT;

   
    EXCEPTION
   --总程序异常处理部分
   WHEN OTHERS
   THEN
      BEGIN
         ROLLBACK;
         EXITCODE := 1;

         e_ErrMessage :=
               e_ErrMessage
            || SUBSTR (SQLERRM, 1, 1000)
            || SUBSTR (DBMS_UTILITY.FORMAT_ERROR_BACKTRACE, 1, 2000);
         PKG_UTIL.CREATELOG (V_PKGNAME,
                             V_PRONAME,
                             V_STEP,
                             V_ACTION,
                             PKG_CONSTANT.CONS_MSG_EXCEPTION,
                             e_ErrMessage,
                             V_MAINTABLE);
      END;

END;
/
View Code

 

更新IN2ODS的代码

FP_BOM

CREATE OR REPLACE PROCEDURE STG.FP_BOM (EXITCODE OUT NUMBER)
IS
   /***********************************************************************
         **  存储过程名称:  FP_BOM
        **  存储过程创建日期: 2014/6/19
        **  存储过程创建人: Tanxiang
        **  目的:  从STG的IN表导数据到ODS对应表,对应关系如下
        IN_BOM_HEADER  =>  MST_BOMHEADER
        IN_BOM_DETAILS  =>  MST_BOMCOMPONENTS
        IN_HIS_CONSUME  =>  MST_BOMCOMPONENTS

        **  输入参数:
        **  输出参数:EXITCODE 预留接口给外部调用者,让其知道存储过程的执行结果,0.预定逻辑执行完成; 1.遇到异常,预定逻辑未执行完成
        **  返回值:
        **  用法:
        **
        ** 修订版本:
        **  版本号     修订时间    修订人        描述
        **  ---------  ----------  ------------  ------------------------
        **    1.0      2014/6/19        Tanxiang 1. 新建存储过程.
        **   1.1      2018/4/16         Landor  修改主替料损耗异常问题
        **  1.2      2018/5/10          Landor  修复主替料CTRD绑定失败问题
        **  2.0     20180522            Landor  按单生产业务开发
   **********************************************************************/


   ----------------------- 以下定义存储过程使用的变量---------------------------------
   V_PKGName                VARCHAR2 (40) := 'PKG_FP';
   V_ProName                VARCHAR2 (40) := 'FP_BOM';
   V_Action                 VARCHAR2 (1);
   V_Step                   VARCHAR2 (40);
   V_MainTable              VARCHAR2 (40);
   E_ErrMessage             VARCHAR2 (3000);
   V_Engine                 NUMBER;
   V_Enterprise             VARCHAR2 (40);
   V_SiteID                 VARCHAR2 (40);
   V_ScenarioID             NUMBER;
   V_OrganizationID         VARCHAR2 (40);
   V_SupplierOrganization   VARCHAR2 (40);
   V_PurOrganizationID      VARCHAR2 (40);
   V_SupplierEnterprise     VARCHAR2 (40);
   V_CalendarName           VARCHAR2 (40);
   V_ProcureBucketName      VARCHAR2 (40);
   V_ItemGroup              VARCHAR2 (40);
   V_SQL                    VARCHAR2 (3000);
   V_RowCount               NUMBER := 0;
   V_COUNT                  NUMBER;
   V_MaxSeq                 NUMBER;
   V_MinSeq                 NUMBER;
   V_CurrentTime            DATE ;
--ADD BY HUANGYANGXIONG ON 20150918  新增工厂以参数形式获取   
   V_FACTORY1     VARCHAR2 (4);  --原2011
   V_FACTORY2     VARCHAR2 (4);  --原2012
   V_FACTORY3     VARCHAR2 (4);  --原2061
   V_FACTORY4     VARCHAR2 (4);  --原2062
   V_FACTORY5     VARCHAR2 (4);  --原2081
   V_FACTORY6     VARCHAR2 (4);  --原2501
----------------------- 以下是存储过程的主体------------------------------------------

BEGIN
   EXITCODE := 0;
   V_ScenarioID := 0;
   V_Step := 0;
   V_Engine := PKG_CONSTANT.CONS_FP_ENGINE;
   V_Enterprise := PKG_CONSTANT.CONS_ENTERPRISE;
   V_SiteID := PKG_CONSTANT.CONS_SITE_ZHUHAI;
   V_CalendarName := 'RESOURCE_CALENDAR'; --TODO: No value set to V_CalendarName
      SELECT TRUNC (M.CURRENTDATE)
     INTO V_CurrentTime
     FROM ABPPMGR.MST_PLANMASTER M
    WHERE ROWNUM = 1;
   --IN_ROUTING_HEADER  =>  MST_ROUTINGHEADER
   V_Step := V_Step + 1;                                                 -- 0;
   V_Action := PKG_CONSTANT.CONS_ACTION_MERGE;
   V_MainTable := 'MST_ROUTINGHEADER';
--ADD BY HUANGYANGXIONG ON 20150918 新增工厂以参数形式获取
   SELECT SUBSTR (CONFIG_VALUE, 1, INSTR (CONFIG_VALUE, ',') - 1) AS WERKS1,
          SUBSTR (CONFIG_VALUE, INSTR (CONFIG_VALUE, ',') + 1, 4) AS WERKS2,
          SUBSTR (CONFIG_VALUE, 11,4) AS WERKS3,
          SUBSTR (CONFIG_VALUE, 16,4) AS WERKS4,
          SUBSTR (CONFIG_VALUE, 21,4) AS WERKS5,
          SUBSTR (CONFIG_VALUE, 26,4) AS WERKS6
     INTO V_FACTORY1, V_FACTORY2, V_FACTORY3, V_FACTORY4, V_FACTORY5, V_FACTORY6
     FROM STG.IN_FP_CONFIG
    WHERE CONFIG_ID = 'FP_FACTORY'; 
   
   
   --记录MST_ROUTINGHEADER导入日志
   PKG_UTIL.CREATELOG (V_PKGName,                      --Put package name here
                       V_ProName,                    --Put procedure name here
                       V_Step,                      --Step seqence No. if have
                       V_Action,                      --Action 'I','D','U','T'
                       PKG_CONSTANT.CONS_MSG_INFO, --Message tppe, 'SUCCESS' or 'EXCEPTION'
                       'START',                 --Any generic information here
                       V_MainTable);              --Major table name processed

   --先把MST_ROUTINGHEADER中所有SYS_ENT_STATE置为INACTIVE 
   UPDATE ABPPMGR.MST_ROUTINGHEADER m
      SET M.SYS_ENT_STATE = 'INACTIVE';  --add by landor on 20161206 执行时间1分49秒

   --再从IN_ROUTING_HEADER导数据到MST_ROUTINGHEADER中,如果已经存在就更新,不存在就新加 
   MERGE INTO ABPPMGR.MST_ROUTINGHEADER M   ---add by landor on 20161206 执行时间1分钟
        USING  (select distinct routing_id from IN_ROUTING_HEADER) IM
           ON (    M.ROUTINGID = IM.ROUTING_ID
               AND M.SITEID = V_SiteID
               AND M.ENTERPRISE = V_Enterprise
               AND M.SCENARIO_ID = V_ScenarioID)
   WHEN MATCHED
   THEN
      UPDATE SET
         M.SYS_ENT_STATE = 'ACTIVE',
         M.SYS_LAST_MODIFIED_BY = V_ProName,
         M.SYS_LAST_MODIFIED_DATE = V_CurrentTime
   WHEN NOT MATCHED
   THEN
      INSERT     (SCENARIO_ID,
                  ENTERPRISE,
                  SITEID,
                  ENGINE_ID,
                  ROUTINGID,
                  SYS_CREATED_BY)
          VALUES (V_ScenarioID,
                  V_Enterprise,
                  V_SiteID,
                  V_Engine,
                  IM.ROUTING_ID,
                  V_ProName);

   COMMIT;
   V_Step := V_Step + 1;                                                 -- 1;
   V_Action := PKG_CONSTANT.CONS_ACTION_MERGE;
   V_MainTable := 'MST_ROUTINGHEADER';
   --记录MST_ROUTINGHEADER导入日志
   PKG_UTIL.CREATELOG (V_PKGName,                      --Put package name here
                       V_ProName,                    --Put procedure name here
                       V_Step,                      --Step seqence No. if have
                       V_Action,                      --Action 'I','D','U','T'
                       PKG_CONSTANT.CONS_MSG_INFO, --Message tppe, 'SUCCESS' or 'EXCEPTION'
                       'Success',               --Any generic information here
                       V_MainTable);              --Major table name processed

   --IN_ROUTINGOPERATION  =>  MST_ROUTINGOPERATION
   V_Step := V_Step + 1;                                                 -- 2;
   V_Action := PKG_CONSTANT.CONS_ACTION_MERGE;
   V_MainTable := 'MST_ROUTINGOPERATION';
   --记录MST_ROUTINGOPERATION导入日志
   PKG_UTIL.CREATELOG (V_PKGName,                      --Put package name here
                       V_ProName,                    --Put procedure name here
                       V_Step,                      --Step seqence No. if have
                       V_Action,                      --Action 'I','D','U','T'
                       PKG_CONSTANT.CONS_MSG_INFO, --Message tppe, 'SUCCESS' or 'EXCEPTION'
                       'START',                 --Any generic information here
                       V_MainTable);              --Major table name processed

--以下程序执行4分钟 154 -266 add by landor on 20161206
   --先把MST_ROUTINGOPERATION中所有SYS_ENT_STATE置为INACTIVE
   UPDATE ABPPMGR.MST_ROUTINGOPERATION m
      SET M.SYS_ENT_STATE = 'INACTIVE';

   --再从IN_ROUTINGOPERATION导数据到MST_ROUTINGOPERATION中,如果已经存在就更新,不存在就新加
   MERGE INTO ABPPMGR.MST_ROUTINGOPERATION M
        USING (SELECT ROUTINGID,
                      SITEID,
                      OPERATION,
                      OPERATIONSEQ,
                      DECODE(RN,1,NVL(MINQUEUETIME,1),0)*1440 MINQUEUETIME,
                      NULL TEARDOWNTIME,
                      VARQTYREJECTED,
                      DECODE(RN,1,NVL(RUNTIMEPER,0),0) RUNTIMEPER
                 FROM (SELECT RO.ROUTING_ID ROUTINGID,
                              RO.SITEID,
                              RO.OPERATION,
                              RO.OPERATIONSEQ,
                              nvl(RO.MINQUEUETIME,RT.OTHERMINQUEUETIME) MINQUEUETIME,
                              RO.VARQTYREJECTED,
                              RT.RUNTIMEPER,
                              ROW_NUMBER ()
                              OVER (PARTITION BY RO.ROUTING_ID
                                    ORDER BY RO.OPERATIONSEQ)
                                 RN,
                              COUNT (RO.OPERATIONSEQ)
                                 OVER (PARTITION BY RO.ROUTING_ID)
                                 OPERATIONSEQCOUNT
                         FROM IN_ROUTINGOPERATION RO ,(SELECT BR.ROUTING_ID, NULL RUNTIMEPER, 
                         MAX(CASE WHEN IIS.SITE_ID=V_FACTORY3 AND LTRIM(SUBSTR(IIS.ITEM_ID,1,9),'0') ='401' THEN 2
                                       --
                                       --WHEN IIS.SITE_ID=V_FACTORY3 AND LTRIM(SUBSTR(IIS.ITEM_ID,1,9),'0') IN('402','407') THEN 3
                                       WHEN IIS.SITE_ID=V_FACTORY3 AND LTRIM(SUBSTR(IIS.ITEM_ID,1,9),'0') ='402' THEN 3
                                       --MODIFIED BY SHENG 2015/12/30,将102修改成102E
                                       --WHEN IIS.SITE_ID IN(V_FACTORY1,V_FACTORY2) AND LTRIM(SUBSTR(IIS.ITEM_ID,1,9),'0') IN('102','201','202','203') THEN 3
                                       WHEN IIS.SITE_ID IN(V_FACTORY1,V_FACTORY2) AND (LTRIM(SUBSTR(IIS.ITEM_ID,1,9),'0') IN('201','202','203') OR (LTRIM

(IIS.PROC_TYPE)='E' AND LTRIM(SUBSTR(IIS.ITEM_ID,1,9),'0') ='102')) THEN 3
                                       WHEN IIS.SITE_ID IN(V_FACTORY1,V_FACTORY2) AND LTRIM(SUBSTR(IIS.ITEM_ID,1,9),'0') ='101' THEN 1
                                       ELSE 1 END) OTHERMINQUEUETIME
                                    FROM IN_RES_CAPABILITY RC,IN_ITEM_SITE IIS, IN_ITEM TM, IN_ITEMBOMROUTING BR
                                        WHERE IIS.ITEM_ID = BR.PRODUCED_ITEM_ID
                                                   AND RC.PRODUCT_FAMILY(+) = TM.FAMILY_ID
                                                   AND BR.PRODUCED_ITEM_ID = TM.ITEM_ID
                                                   AND BR.SITEID = IIS.SITE_ID
                                                   AND IIS.UDF_ITEM_CATEGORY IN ('ASS', 'SEMI') GROUP BY BR.ROUTING_ID) RT
                         WHERE EXISTS(SELECT NULL FROM IN_ROUTING_HEADER RH WHERE RH.ROUTING_ID=RO.ROUTING_ID AND RH.SITEID=RO.SITEID)
                                   AND RO.ROUTING_ID= RT.ROUTING_ID(+))
                WHERE SITEID NOT IN (V_FACTORY4, V_FACTORY3)
                      OR (SITEID IN (V_FACTORY4, V_FACTORY3) AND RN = 1
                          OR RN = OPERATIONSEQCOUNT)
      UNION ALL
      SELECT BR.ROUTING_ID ROUTINGID,
                      BR.SITEID,
                      'ASSBUFOP' OPERATION,
                      (SELECT MAX(RO.OPERATIONSEQ)+10 FROM IN_ROUTINGOPERATION RO WHERE RO.ROUTING_ID=BR.ROUTING_ID)
                       OPERATIONSEQ,
                      NULL MINQUEUETIME,
                      4*1440 TEARDOWNTIME,
                      null VARQTYREJECTED,
                      null RUNTIMEPER  
                      FROM IN_ITEMBOMROUTING BR,IN_ITEM_SITE IIS 
                                 WHERE BR.PRODUCED_ITEM_ID=IIS.ITEM_ID
                                  AND BR.SITEID=IIS.SITE_ID AND IIS.UDF_ITEM_CATEGORY='ASS' 
                                  GROUP BY BR.ROUTING_ID,BR.SITEID) IM
           ON (    M.ROUTINGID = IM.ROUTINGID
               AND M.OPERATIONSEQ = IM.OPERATIONSEQ
               AND M.SITEID = V_SiteID
               AND M.ENTERPRISE = V_Enterprise
               AND M.SCENARIO_ID = V_ScenarioID)
   WHEN MATCHED
   THEN
      UPDATE SET M.SYS_ENT_STATE = 'ACTIVE',
                 --M.STYLENAME = V_StyleName,
                 M.OPERATION = IM.OPERATION,
                 --M.MINQUEUETIME = IM.MINQUEUETIME,
                 M.MINQUEUETIME =NULL,
                 M.AVGQUEUETIME = NULL,
                 M.MINFIXEDRUNTIME=IM.MINQUEUETIME,
                 M.VARQTYREJECTED = IM.VARQTYREJECTED,
                 M.RUNTIMEPER = IM.RUNTIMEPER,
                 M.TEARDOWNTIME=IM.TEARDOWNTIME,
                 M.TIMEUOM='MINUTES',
                 M.SYS_LAST_MODIFIED_BY = V_ProName,
                 M.SYS_LAST_MODIFIED_DATE = V_CurrentTime
   WHEN NOT MATCHED
   THEN
      INSERT     (SCENARIO_ID,
                  ENTERPRISE,
                  SITEID,
                  ENGINE_ID,
                  ROUTINGID,
                  OPERATIONSEQ,
                  OPERATION,
                  TIMEUOM,--'MINUTES'
                  TEARDOWNTIME,
                  --MINQUEUETIME,
                  --AVGQUEUETIME,
                  MINFIXEDRUNTIME,
                  VARQTYREJECTED,
                  RUNTIMEPER,
                  SYS_CREATED_BY)
          VALUES (V_ScenarioID,
                  V_Enterprise,
                  V_SiteID,
                  V_Engine,
                  IM.ROUTINGID,
                  IM.OPERATIONSEQ,
                  IM.OPERATION,
                  'MINUTES',
                  IM.TEARDOWNTIME,
                  IM.MINQUEUETIME,
                  IM.VARQTYREJECTED,
                  IM.RUNTIMEPER,
                  V_ProName);

   COMMIT;
   V_Step := V_Step + 1;                                                 -- 3;
   V_Action := PKG_CONSTANT.CONS_ACTION_MERGE;
   V_MainTable := 'MST_ROUTINGOPERATION';
   --记录MST_ROUTINGOPERATION导入日志
   PKG_UTIL.CREATELOG (V_PKGName,                      --Put package name here
                       V_ProName,                    --Put procedure name here
                       V_Step,                      --Step seqence No. if have
                       V_Action,                      --Action 'I','D','U','T'
                       PKG_CONSTANT.CONS_MSG_INFO, --Message tppe, 'SUCCESS' or 'EXCEPTION'
                       'Success',               --Any generic information here
                       V_MainTable);              --Major table name processed

   V_Step := V_Step + 1;                                                 -- 0;
   V_Action := PKG_CONSTANT.CONS_ACTION_TRUNCATE;
   V_MainTable := 'TEMP_BOM_CHANGE';
   --清理临时表TEMP_BOM_CHANGE
   PKG_UTIL.CREATELOG (V_PKGName,                      --Put package name here
                       V_ProName,                    --Put procedure name here
                       V_Step,                      --Step seqence No. if have
                       V_Action,                      --Action 'I','D','U','T'
                       PKG_CONSTANT.CONS_MSG_INFO, --Message tppe, 'SUCCESS' or 'EXCEPTION'
                       'START',                 --Any generic information here
                       V_MainTable);              --Major table name processed
   --开始导入IN_HIS_CONSUME数据
   -- 清理临时表TEMP_BOM_CHANGE,准备放入数据
   V_SQL := 'TRUNCATE TABLE TEMP_BOM_CHANGE ';

   EXECUTE IMMEDIATE V_SQL;

  DELETE FROM ABPPMGR.MST_BOMCOMPONENTS_P;
   COMMIT;
   DELETE FROM ABPPMGR.MST_BOMCOMPONENTSALT_P;
    COMMIT;
   /*
      --群组替代,需要把虚拟聊的采购比例放到真实料上
      MERGE INTO IN_BOM_DETAILS M
           USING (SELECT HD.BOM_ID, DT.RATIO
                    FROM IN_BOM_DETAILS DT, IN_BOM_HEADER HD
                   WHERE     DT.COMPONENTGROUP IS NOT NULL
                         AND HD.PRODUCT_ID = DT.ITEM_ID
                         AND DT.IS_PHANTOM = '1'
                         AND HD.IS_VALID = '1') IM
              ON (M.BOM_ID = IM.BOM_ID)
      WHEN MATCHED
      THEN
         UPDATE SET M.RATIO = IM.RATIO;

      COMMIT;
   */
   V_Step := V_Step + 1;                                                 -- 0;
   V_Action := PKG_CONSTANT.CONS_ACTION_TRUNCATE;
   V_MainTable := 'TEMP_BOM_CHANGE';
   --清理临时表TEMP_BOM_CHANGE
   PKG_UTIL.CREATELOG (V_PKGName,                      --Put package name here
                       V_ProName,                    --Put procedure name here
                       V_Step,                      --Step seqence No. if have
                       V_Action,                      --Action 'I','D','U','T'
                       PKG_CONSTANT.CONS_MSG_INFO, --Message tppe, 'SUCCESS' or 'EXCEPTION'
                       'Success',               --Any generic information here
                       V_MainTable);              --Major table name processed


   V_Step := V_Step + 1;                                                 -- 0;
   V_Action := PKG_CONSTANT.CONS_ACTION_INSERT;
   V_MainTable := 'TEMP_BOM_CHANGE';
   --准备临时表TEMP_BOM_CHANGE数据
   PKG_UTIL.CREATELOG (V_PKGName,                      --Put package name here
                       V_ProName,                    --Put procedure name here
                       V_Step,                      --Step seqence No. if have
                       V_Action,                      --Action 'I','D','U','T'
                       PKG_CONSTANT.CONS_MSG_INFO, --Message tppe, 'SUCCESS' or 'EXCEPTION'
                       'START',                 --Any generic information here
                       V_MainTable);              --Major table name processed

   --将不完全替代料放进临时表TEMP_BOM_CHANGE,但不是替代料整租都会进来,这里只找出有单独使用的料
   INSERT INTO TEMP_BOM_CHANGE (BOM_ID,
                                PRODUCED_ITEM_ID,
                                COMPONENTGROUP,
                                ITEM_ID,
                                PRIORITY,
                                TYPE,
                                PURRATIO,
                                effstartdate,
                                effenddate)
      SELECT DT.BOM_ID,
             HD.PRODUCT_ID,
             DT.COMPONENTGROUP,
             DT.ITEM_ID,
             DT.PRIORITY,
             'Untotal Alt' TYPE,
             DT.RATIO / 100 RATIO,
                                DT.EFFECTIVE_START_DATE,
                                DT.EFFECTIVE_END_DATE
        FROM IN_BOM_DETAILS DT, IN_BOM_HEADER HD
       WHERE     DT.BOM_ID = HD.BOM_ID
             AND HD.IS_VALID = '1'
             AND DT.COMPONENTGROUP IS NOT NULL                          --有替代料
             AND EXISTS                                               --找不完全替代
                    (SELECT NULL
                       FROM IN_BOM_DETAILS X1
                      WHERE X1.ITEM_ID = DT.ITEM_ID
                            AND X1.COMPONENTGROUP IS NULL);

   V_RowCount := V_RowCount + SQL%ROWCOUNT;

   COMMIT;

   --再利用已经找到的不完全替代料所在组COMPONENTGROUP,找出所有不完全替代料和他们的替代关系,全部认为是不完全替代料
   INSERT INTO TEMP_BOM_CHANGE (BOM_ID,
                                PRODUCED_ITEM_ID,
                                COMPONENTGROUP,
                                ITEM_ID,
                                PRIORITY,
                                TYPE,
                                PURRATIO,
                                effstartdate,
                                effenddate)
      SELECT DT.BOM_ID,
             HD.PRODUCT_ID,
             DT.COMPONENTGROUP,
             DT.ITEM_ID,
             DT.PRIORITY,
             'Untotal Alt' TYPE,
             DT.RATIO / 100 RATIO,
            DT.EFFECTIVE_START_DATE,
            DT.EFFECTIVE_END_DATE
        FROM IN_BOM_DETAILS DT, IN_BOM_HEADER HD
       WHERE     DT.BOM_ID = HD.BOM_ID
             AND HD.IS_VALID = '1'
             AND DT.COMPONENTGROUP IS NOT NULL
             AND EXISTS                                              --找不完全替代组
                    (SELECT NULL
                       FROM TEMP_BOM_CHANGE BC
                      WHERE DT.BOM_ID = BC.BOM_ID -- AND BC.TYPE ='Untotal Alt'  --之前找出的都是不完全替代,不用加此条件
                            AND DT.COMPONENTGROUP = BC.COMPONENTGROUP)
             AND NOT EXISTS                    --已经进入临时表TEMP_BOM_CHANGE的则不用找出来
                        (SELECT NULL
                           FROM TEMP_BOM_CHANGE BC
                          WHERE     DT.BOM_ID = BC.BOM_ID
                                AND DT.COMPONENTGROUP = BC.COMPONENTGROUP
                                -- AND BC.TYPE ='Untotal Alt'  --之前找出的都是不完全替代,不用加此条件
                                AND DT.ITEM_ID = BC.ITEM_ID);

   V_RowCount := V_RowCount + SQL%ROWCOUNT;

   --将完全替代料放进临时表TEMP_BOM_CHANGE,其他有替代关系的全部是完全替代料
   INSERT INTO TEMP_BOM_CHANGE (BOM_ID,
                                PRODUCED_ITEM_ID,
                                COMPONENTGROUP,
                                ITEM_ID,
                                PRIORITY,
                                TYPE,
                                PURRATIO,
                                effstartdate,
                                effenddate)
      SELECT DT.BOM_ID,
             HD.PRODUCT_ID,
             DT.COMPONENTGROUP,
             DT.ITEM_ID,
             DT.PRIORITY,
             'Total Alt' TYPE,
             DT.RATIO / 100 RATIO,
            DT.EFFECTIVE_START_DATE,
            DT.EFFECTIVE_END_DATE
        FROM IN_BOM_DETAILS DT, IN_BOM_HEADER HD
       WHERE     DT.BOM_ID = HD.BOM_ID
             AND HD.IS_VALID = '1'
             AND DT.COMPONENTGROUP IS NOT NULL
             AND NOT EXISTS
                        (SELECT NULL
                           FROM TEMP_BOM_CHANGE BC
                          WHERE DT.BOM_ID = BC.BOM_ID -- AND BC.TYPE ='Untotal Alt'  --之前找出的都是不完全替代,不用加此条件
                                AND DT.COMPONENTGROUP = BC.COMPONENTGROUP);

   V_RowCount := V_RowCount + SQL%ROWCOUNT;

   COMMIT;
   V_Step := V_Step + 1;                                                 -- 0;
   V_Action := PKG_CONSTANT.CONS_ACTION_INSERT;
   V_MainTable := 'TEMP_BOM_CHANGE';
   PKG_UTIL.CREATELOG (V_PKGName,                      --Put package name here
                       V_ProName,                    --Put procedure name here
                       V_Step,                      --Step seqence No. if have
                       V_Action,                      --Action 'I','D','U','T'
                       PKG_CONSTANT.CONS_MSG_INFO, --Message tppe, 'SUCCESS' or 'EXCEPTION'
                       'Success: ' || V_RowCount || ' rows imported.', --Any generic information here
                       V_MainTable);              --Major table name processed

   V_Step := V_Step + 1;                                                 -- 0;
   V_Action := PKG_CONSTANT.CONS_ACTION_UPDATE;
   V_MainTable := 'TEMP_BOM_CHANGE';
   --计算采购比率减使用比率,根据结果决定主替代料,改变BOM中主替代料优先级
   PKG_UTIL.CREATELOG (
      V_PKGName,                                       --Put package name here
      V_ProName,                                     --Put procedure name here
      V_Step,                                       --Step seqence No. if have
      V_Action,                                       --Action 'I','D','U','T'
      PKG_CONSTANT.CONS_MSG_INFO,     --Message tppe, 'SUCCESS' or 'EXCEPTION'
      'Start: update priority according to the BOM ratio-consume ratio.', --Any generic information here
      V_MainTable);                               --Major table name processed

    DELETE FROM TEMP_BOM_CHANGE
          WHERE ROWID NOT IN (  SELECT MAX (ROWID)
                                  FROM TEMP_BOM_CHANGE
                              GROUP BY BOM_ID, COMPONENTGROUP, ITEM_ID);

    COMMIT;
   --计算采购比率减使用比率,根据结果决定主替代料,改变BOM中主替代料优先级
   MERGE INTO TEMP_BOM_CHANGE CH
        USING (SELECT BOM_ID,
                      PRODUCED_ITEM_ID,
                      COMPONENTGROUP,
                      ITEM_ID,
                      TYPE,
                      PURRATIO,
                      PRIORITY,
                      UDF_CONSUME_QTY,
                      SUMQTY,
                      DECODE (SUMQTY, 0, 0, UDF_CONSUME_QTY / SUMQTY)
                         USERATIO,
                      ROW_NUMBER ()
                      OVER (
                         PARTITION BY BOM_ID, COMPONENTGROUP
                         ORDER BY
                            CASE
                               WHEN TYPE = 'Untotal Alt'
                               THEN
                                  CASE
                                     WHEN PRIORITY = 1
                                     THEN
                                        -999999999
                                     ELSE
                                        DECODE (SUMQTY,
                                                0, 0,
                                                UDF_CONSUME_QTY / SUMQTY)
                                        - NVL (PURRATIO, 1)
                                  END
                               ELSE
                                  DECODE (SUMQTY,
                                          0, 0,
                                          UDF_CONSUME_QTY / SUMQTY)
                                  - NVL (PURRATIO, 1)
                            END,
                            PRIORITY)
                         CHANGED_PRIORITY
                 FROM (SELECT CH.BOM_ID,
                              CH.PRODUCED_ITEM_ID,
                              CH.COMPONENTGROUP,
                              CH.ITEM_ID,
                              CH.TYPE,
                              CH.PURRATIO,
                              ROW_NUMBER ()
                              OVER (
                                 PARTITION BY CH.BOM_ID,
                                              CH.PRODUCED_ITEM_ID,
                                              CH.COMPONENTGROUP
                                 ORDER BY CH.PRIORITY)
                                 PRIORITY,
                              NVL (HC.UDF_CONSUME_QTY, 0) UDF_CONSUME_QTY,
                              NVL (
                                 SUM (
                                    HC.UDF_CONSUME_QTY)
                                 OVER (
                                    PARTITION BY CH.BOM_ID,
                                                 CH.PRODUCED_ITEM_ID,
                                                 CH.COMPONENTGROUP),
                                 0)
                                 SUMQTY
                         FROM TEMP_BOM_CHANGE CH, IN_HIS_CONSUME HC
                        WHERE CH.PRODUCED_ITEM_ID = HC.PARENT_ID(+)
                              AND CH.ITEM_ID = HC.ITEM_ID(+)
                              AND SUBSTR(CH.BOM_ID,INSTR(CH.BOM_ID,'_')+1,4)=HC.SITE_ID(+))) IM
           ON (    IM.BOM_ID = CH.BOM_ID
               AND CH.ITEM_ID = IM.ITEM_ID
               AND CH.PRODUCED_ITEM_ID = IM.PRODUCED_ITEM_ID
               AND CH.COMPONENTGROUP = IM.COMPONENTGROUP)
   WHEN MATCHED
   THEN
      UPDATE SET
         CH.CHANGED_PRIORITY =case when CH.PURRATIO IN (0,1) then IM.PRIORITY else IM.CHANGED_PRIORITY end
         , CH.USERATIO = IM.USERATIO ;

   COMMIT;

insert into TEMP_BOM_CHANGE(BOM_ID,
                                PRODUCED_ITEM_ID,
                                COMPONENTGROUP,
                                ITEM_ID,
                                PRIORITY,
                                CHANGED_PRIORITY,
                                TYPE,
                                PURRATIO,
                                effstartdate,
                                effenddate)
      SELECT DT.BOM_ID,
             HD.PRODUCT_ID,
             NVL(DT.COMPONENTGROUP,DT.FOLLOW_UP_MATERIAL) COMPONENTGROUP,
             DT.FOLLOW_UP_MATERIAL,
             DT.PRIORITY,
             2 CHANGED_PRIORITY,
             'ECO' TYPE,
             1 RATIO,
            DT.FOLLOW_UP_ACTIVE_DATE,
            NULL EFFECTIVE_END_DATE
        FROM IN_BOM_DETAILS DT, IN_BOM_HEADER HD
       WHERE     DT.BOM_ID = HD.BOM_ID
             AND HD.IS_VALID = '1'
             AND EXISTS (SELECT NULL FROM IN_ITEM TM WHERE TM.ITEM_ID=DT.FOLLOW_UP_MATERIAL) 
             AND NOT EXISTS(SELECT NULL FROM TEMP_BOM_CHANGE BC 
             WHERE BC.BOM_ID=DT.BOM_ID AND BC.ITEM_ID=DT.ITEM_ID AND DT.COMPONENTGROUP=BC.COMPONENTGROUP);
commit;
   V_Step := V_Step + 1;                                                 -- 0;
   V_Action := PKG_CONSTANT.CONS_ACTION_UPDATE;
   V_MainTable := 'IN_BOM_DETAILS';
   --计算采购比率减使用比率,根据结果决定主替代料,改变BOM中主替代料优先级
   PKG_UTIL.CREATELOG (V_PKGName,                      --Put package name here
                       V_ProName,                    --Put procedure name here
                       V_Step,                      --Step seqence No. if have
                       V_Action,                      --Action 'I','D','U','T'
                       PKG_CONSTANT.CONS_MSG_INFO, --Message tppe, 'SUCCESS' or 'EXCEPTION'
                       'Success: ' || SQL%ROWCOUNT || ' rows updated', --Any generic information here
                       V_MainTable);              --Major table name processed

   --IN_BOM_HEADER  =>  MST_BOMHEADER
   V_Step := V_Step + 1;                                                 -- 0;
   V_Action := PKG_CONSTANT.CONS_ACTION_MERGE;
   V_MainTable := 'MST_BOMHEADER';
   --记录MST_BOMHEADER导入日志
   PKG_UTIL.CREATELOG (V_PKGName,                      --Put package name here
                       V_ProName,                    --Put procedure name here
                       V_Step,                      --Step seqence No. if have
                       V_Action,                      --Action 'I','D','U','T'
                       PKG_CONSTANT.CONS_MSG_INFO, --Message tppe, 'SUCCESS' or 'EXCEPTION'
                       'START',                 --Any generic information here
                       V_MainTable);              --Major table name processed
---以下程序执行3分钟 add by landor on 20161206
   UPDATE ABPPMGR.MST_BOMHEADER m
      SET M.SYS_ENT_STATE = 'INACTIVE';

   MERGE INTO ABPPMGR.MST_BOMHEADER m
        USING (
        --ALTER BY HUANGYANGXIONG ON 20150708   因为寻源过程中出现了BOMID号超过40位长度的,因此对BOMID做了处理,
        --MODIFY BY HUANGYANGXIONG ON 20180608 BOMID字段已经扩充到100,不存在40的位数限制。
        SELECT 
        --CASE WHEN LENGTH('' || LTRIM (PRODUCT_ID, '0') || '_' || TRIM(BOM_ID))>40 THEN '' || SUBSTR(LTRIM (PRODUCT_ID, '0'),1,13) || '_' || TRIM(BOM_ID)
        --ELSE 
        '' || LTRIM (PRODUCT_ID, '0') || '_' || TRIM(BOM_ID) AS BOM_ID,
        LENGTH('' || LTRIM (PRODUCT_ID, '0') || '_' || TRIM(BOM_ID)) as len,
                      BOM_TYPE,
                      EFFECTIVE_START_DATE,
                      EFFECTIVE_END_DATE
                 FROM IN_BOM_HEADER
                WHERE IS_VALID = '1'
        /*
        SELECT '' || LTRIM (PRODUCT_ID, '0') || '_' || TRIM(BOM_ID) BOM_ID,
                      BOM_TYPE,
                      EFFECTIVE_START_DATE,
                      EFFECTIVE_END_DATE
                 FROM IN_BOM_HEADER
                WHERE IS_VALID = '1'
         */       
                ) IM
           ON (M.BOMID = IM.BOM_ID)
   WHEN MATCHED
   THEN
      UPDATE SET M.SCENARIO_ID = V_ScenarioID,
                 M.ENTERPRISE = V_Enterprise,
                 M.SITEID = V_SiteID,
                 M.BOMTYPE = IM.BOM_TYPE,
                 M.EFFSTARTDATE = IM.EFFECTIVE_START_DATE,
                 M.EFFENDDATE = IM.EFFECTIVE_END_DATE,
                 M.ENGINE_ID = V_Engine,
                 M.SYS_ENT_STATE = 'ACTIVE',
                 M.SYS_LAST_MODIFIED_BY = V_ProName,
                 M.SYS_LAST_MODIFIED_DATE = V_CurrentTime
   WHEN NOT MATCHED
   THEN
      INSERT     (SCENARIO_ID,
                  ENTERPRISE,
                  SITEID,
                  ENGINE_ID,
                  BOMID,
                  BOMTYPE,
                  EFFSTARTDATE,
                  EFFENDDATE,
                  SYS_CREATED_BY)
          VALUES (V_ScenarioID,
                  V_Enterprise,
                  V_SiteID,
                  V_Engine,
                  IM.BOM_ID,
                  IM.BOM_TYPE,
                  IM.EFFECTIVE_START_DATE,
                  IM.EFFECTIVE_END_DATE,
                  V_ProName);

   COMMIT;
   V_Step := V_Step + 1;                                                 -- 1;
   V_Action := PKG_CONSTANT.CONS_ACTION_MERGE;
   V_MainTable := 'MST_BOMHEADER';
   --记录MST_BOMHEADER导入日志
   PKG_UTIL.CREATELOG (V_PKGName,                      --Put package name here
                       V_ProName,                    --Put procedure name here
                       V_Step,                      --Step seqence No. if have
                       V_Action,                      --Action 'I','D','U','T'
                       PKG_CONSTANT.CONS_MSG_INFO, --Message tppe, 'SUCCESS' or 'EXCEPTION'
                       'Success',               --Any generic information here
                       V_MainTable);              --Major table name processed
   --IN_BOM_DETAILS  =>  MST_BOMCOMPONENTS & MST_BOMCOMPONENTSALT
   V_Step := V_Step + 1;                                                 -- 2;
   V_Action := PKG_CONSTANT.CONS_ACTION_MERGE;
   V_MainTable := 'MST_BOMCOMPONENTS';
   --记录MST_BOMCOMPONENTS导入日志
   PKG_UTIL.CREATELOG (V_PKGName,                      --Put package name here
                       V_ProName,                    --Put procedure name here
                       V_Step,                      --Step seqence No. if have
                       V_Action,                      --Action 'I','D','U','T'
                       PKG_CONSTANT.CONS_MSG_INFO, --Message tppe, 'SUCCESS' or 'EXCEPTION'
                       'START',                 --Any generic information here
                       V_MainTable);              --Major table name processed
--   execute immediate 'TRUNCATE TABLE ABPPMGR.MST_BOMCOMPONENTSALT';
   DELETE FROM ABPPMGR.MST_BOMCOMPONENTSALT;
   COMMIT;
---    execute immediate 'TRUNCATE TABLE ABPPMGR.MST_BOMCOMPONENTS';
   DELETE FROM ABPPMGR.MST_BOMCOMPONENTS;

   COMMIT;
   execute immediate 'alter session enable parallel dml';
   INSERT /*+ append parallel(6) */ ALL
   WHEN PRIORITY=1 and RN=1
   THEN
        INTO ABPPMGR.MST_BOMCOMPONENTS (SCENARIO_ID,
                                        ENGINE_ID,
                                        ENTERPRISE,
                                        SITEID,
                                        PROPERTY,
                                        --COMPONENTSITEID,
                                        --YIELDCALENDAR,
                                        ITEM,
                                        BOMID,
                                        ROUTINGID,
                                        OPERATIONSEQ,
                                        PHANTOM,
                                        QTYPER,
                                        QTYUOM,
                                        SATISFYDMDCOMPLETE,
                                        COMPONENTGROUP,
                                        EFFSTARTDATE,
                                        EFFENDDATE,
                                        YIELD,
                                        SPLITPERCENT,
                                        SYS_CREATED_BY)
      VALUES (V_ScenarioID,
              V_Engine,
              V_Enterprise,
              V_SiteID,
              ITEM || '_' ||SITE_ID,
              --V_SiteID,
              --V_CalendarName,
              ITEM,
              BOMID,
              SUBSTR(ROUTING,instr(ROUTING,'^')+1),
              SUBSTR(ROUTING,1,instr(ROUTING,'^')-1),
              NULL,-- PHANTOM,
              QTYPER*(1+YIELD),
              QTYUOM,
              SATISFYDMDCOMPLETE,
              FOLLOWGRP,
              to_date(EFFSTARTDATE,'yyyy/mm/dd')+RC/(24*60*60),
              EFFECTIVE_END_DATE,
              NULL,
              decode(COMPONENTGROUP,null,null,RATIO),
              V_ProName)
      SELECT MT.*,SUM(USAGE_QTY) OVER (PARTITION BY BOMID, ITEM, EFFECTIVE_START_DATE,COMPONENTGROUP,ROUTING) QTYPER,
      EFFECTIVE_START_DATE EFFSTARTDATE,
      ROW_NUMBER() OVER (PARTITION BY BOMID, ITEM, EFFECTIVE_START_DATE,ROUTING ORDER BY PRIORITY) RC,
      ROW_NUMBER() OVER (PARTITION BY BOMID, ITEM, EFFECTIVE_START_DATE,ROUTING,COMPONENTGROUP ORDER BY PRIORITY) RN
                 FROM (
      SELECT CASE WHEN LENGTH('' || LTRIM (HD.PRODUCT_ID, '0') || '_' || TRIM(DT.BOM_ID))>40 THEN '' || SUBSTR(LTRIM (HD.PRODUCT_ID, '0'),1,13) || '_' || 

TRIM(DT.BOM_ID)
        ELSE '' || LTRIM (HD.PRODUCT_ID, '0') || '_' || TRIM(DT.BOM_ID) END AS
         BOMID,
             DT.BOM_ID,
             SUBSTR (DT.BOM_ID, INSTR (DT.BOM_ID, '_') + 1, 4) SITE,
             DT.ITEM_ID ITEM,
             NVL(DT.COMPONENTGROUP,trim(DT.FOLLOW_UP_MATERIAL))  FOLLOWGRP,
             DT.COMPONENTGROUP COMPONENTGROUP,
             --ALTER BY HUANGYANGXIONG ON 20160303  FOR VC BOM
             CASE WHEN SUBSTR(DT.BOM_ID,1,2)='SO' AND
             TRIM(NVL (CH.CHANGED_PRIORITY, DT.PRIORITY)) = 0
             THEN 1
             ELSE
                NVL (CH.CHANGED_PRIORITY, DT.PRIORITY)
             END AS PRIORITY,
             --ROW_NUMBER() OVER (PARTITION BY DT.BOM_ID, DT.ITEM_ID, DT.EFFECTIVE_START_DATE ORDER BY NVL (CH.CHANGED_PRIORITY, DT.PRIORITY)) PRIORITY,
             TM.UDF_MEINS QTYUOM,
             NULL SATISFYDMDCOMPLETE,
             DT.IS_PHANTOM PHANTOM,
             --DT.SETUP_NUMBER OPERATIONSEQ,
             TO_NUMBER(NVL(DT.COMPONENT_YIELD_UOM,'0')) YIELD,
             'PERCENT' COMPONENT_YIELD_UOM,
             DT.RATIO/100 RATIO,
             CASE WHEN DT.EFFECTIVE_START_DATE<SYSDATE THEN
                CASE WHEN DT.COMPONENTGROUP IS NOT NULL THEN
                        TO_CHAR(V_CURRENTTIME,'yyyy/mm/dd') 
                    ELSE
                        NULL 
                    END
              ELSE TO_CHAR(DT.EFFECTIVE_START_DATE,'yyyy/mm/dd') END
              EFFECTIVE_START_DATE,
             case when HD.mo_bom_mark ='1' then null else DT.EFFECTIVE_END_DATE end EFFECTIVE_END_DATE ,
             DT.USAGE_QTY*FP_GET_CONVRATE(DT.ITEM_ID,DT.USAGE_QTY_UOM,TM.UDF_MEINS) USAGE_QTY,
             CASE
                WHEN DT.COMPONENTGROUP IS NULL
                THEN
                   '0'                                           --no alt item
                ELSE
                   MIN (
                         NVL (CH.CHANGED_PRIORITY, DT.PRIORITY)
                      || '-'
                      || DT.ITEM_ID)
                   OVER (PARTITION BY DT.BOM_ID, DT.COMPONENTGROUP)
             END
                MAIN_ITEM,
       CASE
          WHEN SUBSTR (DT.BOM_ID, INSTR (DT.BOM_ID, '_') + 1, 4) IN
                  (V_FACTORY3, V_FACTORY4)
          THEN
          --2061和2062厂只需要第一个工序和最后一个工序
          --如果物料不在最后一个工序则将他们放到第一个工序
             (SELECT CASE
                        WHEN TO_NUMBER (DT.SETUP_NUMBER) =
                                MAX (X.OPERATIONSEQ)
                        THEN
                           MAX (LPAD(X.OPERATIONSEQ,5,'0') || '^' || X.routing_id)
                        ELSE
                           MIN (LPAD(X.OPERATIONSEQ,5,'0') || '^' || X.routing_id)
                     END
                FROM IN_ROUTINGOPERATION X
               WHERE     X.ROUTING_ID = BR.ROUTING_ID)
          ELSE
          --为防止数据导入的OPERATIONSEQ错误,
          --将SETUP_NUMBER和OPERATIONSEQ比对,如果能找到该SETUP_NUMBER则用该数据
          --否则将使用工艺路线中的第一个工序
             (SELECT CASE
                WHEN MIN (ABS (X.OPERATIONSEQ - TO_NUMBER (DT.SETUP_NUMBER) )) = 0
                THEN
                    REPLACE(MIN (LPAD (ABS (X.OPERATIONSEQ - TO_NUMBER (DT.SETUP_NUMBER)), 5, '0') || '^' || X.routing_id),'00000^',LPAD (TO_NUMBER 

(DT.SETUP_NUMBER), 5, '0')||'^')
                ELSE
                    MIN (LPAD (X.OPERATIONSEQ, 5, '0') || '^' || X.routing_id)
                END
                FROM IN_ROUTINGOPERATION X
               WHERE     X.ROUTING_ID = BR.ROUTING_ID
                     )
       END
          ROUTING,
          BR.SITEID SITE_ID
        FROM IN_BOM_DETAILS DT, IN_BOM_HEADER HD, TEMP_BOM_CHANGE CH,IN_ITEMBOMROUTING BR,IN_ITEM TM
       WHERE     DT.BOM_ID = HD.BOM_ID --and HD.BOM_ID='000018005653_2011'
             AND BR.BOM_ID=HD.BOM_ID
             and TM.item_id=DT.item_id
             AND DT.PRODUCT_TYPE IS NULL --联产品、副产品不进FP BOM
             AND DT.usage_qty>0
             AND DT.BOM_ID = CH.BOM_ID(+)
             AND DT.COMPONENTGROUP = CH.COMPONENTGROUP(+)
             AND DT.ITEM_ID = CH.ITEM_ID(+)) MT;

   COMMIT;
   /*  hide by  landor on 20180705
   --add start
   --按单h.再新增BOM数据在MST_BOMCOMPONENTS,条件是IN_BOM_CHANGE. OLD_ITEM_ID=MST_BOMCOMPONENTS.ITEM
    --且IN_BOM_CHANGE. BOM_ID= MST_BOMCOMPONENTS.BOMID:add by Landor on 20180522
    Insert INTO ABPPMGR.MST_BOMCOMPONENTS (SCENARIO_ID,
                                     ENGINE_ID,
                                     ENTERPRISE,
                                     SITEID,
                                     PROPERTY,
                                     ITEM,
                                     BOMID,
                                     ROUTINGID,
                                     OPERATIONSEQ,
                                     PHANTOM,
                                     QTYPER,
                                     QTYUOM,
                                     SATISFYDMDCOMPLETE,
                                     COMPONENTGROUP,
                                     EFFSTARTDATE,
                                     EFFENDDATE,
                                     YIELD,
                                     SPLITPERCENT,
                                     SYS_CREATED_BY)
    SELECT DISTINCT AM.SCENARIO_ID,
                    AM.ENGINE_ID,
                    AM.ENTERPRISE,
                    AM.SITEID,
                    AM.PROPERTY,
                    CASE WHEN IC.OLD_ITEM_ID=AM.ITEM THEN IC.NEW_ITEM_ID
                    ELSE AM.ITEM
                    END AS ITEM,
                    AG.BOMID,
                    AG.ROUTINGID,
                    AM.OPERATIONSEQ,
                    AM.PHANTOM,
                    1 NEW_QUANTITY,
                    AM.QTYUOM,
                    AM.SATISFYDMDCOMPLETE,
                    AM.COMPONENTGROUP,
                    AM.EFFSTARTDATE,
                    AM.EFFENDDATE,
                    AM.YIELD,
                    AM.SPLITPERCENT,
                    'AnDan'
     FROM IN_BOM_CHANGE IC 
     JOIN ABPPMGR.MST_ITEMBOMROUTING AG
      ON IC.PARENT_ITEM_ID=AG.ITEM
    JOIN  ABPPMGR.MST_BOMCOMPONENTS AM
    ON  TRIM(AM.BOMID)=SUBSTR(AG.BOMID,1,INSTR(AG.BOMID,'_',1,4)-1)
    WHERE IC.ACTION='MODIFY'
    AND AG.SYS_CREATED_BY='AnDan';
    COMMIT;
     --VIP标签的新增,当in_bom_change.action=new表示要新增到in_bom_change.parent_item_id(半成品)的下层
    Insert INTO ABPPMGR.MST_BOMCOMPONENTS (SCENARIO_ID,
                                     ENGINE_ID,
                                     ENTERPRISE,
                                     SITEID,
                                     PROPERTY,
                                     ITEM,
                                     BOMID,
                                     ROUTINGID,
                                     OPERATIONSEQ,
                                     PHANTOM,
                                     QTYPER,
                                     QTYUOM,
                                     SATISFYDMDCOMPLETE,
                                     COMPONENTGROUP,
                                     EFFSTARTDATE,
                                     EFFENDDATE,
                                     YIELD,
                                     SPLITPERCENT,
                                     SYS_CREATED_BY)
    --复制原数据改BOM成品层
    SELECT DISTINCT AM.SCENARIO_ID,
                    AM.ENGINE_ID,
                    AM.ENTERPRISE,
                    AM.SITEID,
                    AM.PROPERTY,
                    AM.ITEM,
                    AG.BOMID,
                    AG.ROUTINGID,
                    AM.OPERATIONSEQ,
                    AM.PHANTOM,
                    1 NEW_QUANTITY,
                    AM.QTYUOM,
                    AM.SATISFYDMDCOMPLETE,
                    AM.COMPONENTGROUP,
                    AM.EFFSTARTDATE,
                    AM.EFFENDDATE,
                    AM.YIELD,
                    AM.SPLITPERCENT,
                    'AnDan'
     FROM IN_BOM_CHANGE IC 
     JOIN ABPPMGR.MST_ITEMBOMROUTING AG
      ON IC.PARENT_ITEM_ID=AG.ITEM
    JOIN  ABPPMGR.MST_BOMCOMPONENTS AM
    ON  TRIM(AM.BOMID)=SUBSTR(AG.BOMID,1,INSTR(AG.BOMID,'_',1,4)-1)
    WHERE IC.ACTION='DEL'
    AND AG.SYS_CREATED_BY='AnDan'
    union all
    --复制原数据改BOM半成品层
    SELECT DISTINCT AM.SCENARIO_ID,
                    AM.ENGINE_ID,
                    AM.ENTERPRISE,
                    AM.SITEID,
                    AM.PROPERTY,
                    AM.ITEM,
                    AG.BOMID,
                    AG.ROUTINGID,
                    AM.OPERATIONSEQ,
                    AM.PHANTOM,
                    1 NEW_QUANTITY,
                    AM.QTYUOM,
                    AM.SATISFYDMDCOMPLETE,
                    AM.COMPONENTGROUP,
                    AM.EFFSTARTDATE,
                    AM.EFFENDDATE,
                    AM.YIELD,
                    AM.SPLITPERCENT,
                    'AnDan'
     FROM IN_BOM_CHANGE IC 
     JOIN ABPPMGR.MST_ITEMBOMROUTING AG
      ON IC.PARENT_ITEM_ID=AG.ITEM
    JOIN  ABPPMGR.MST_BOMCOMPONENTS AM
    ON  TRIM(AM.BOMID)=SUBSTR(AG.BOMID,1,INSTR(AG.BOMID,'_',1,4)-1)
    WHERE IC.ACTION='NEW'
    AND AG.SYS_CREATED_BY='AnDan'
    UNION ALL
    --半成品层加标签
    SELECT          SCENARIO_ID,
                    ENGINE_ID,
                    ENTERPRISE,
                    SITEID,
                    PROPERTY,
                    NEW_ITEM_ID,
                    BOMID,
                    ROUTINGID,
                    OPERATIONSEQ,
                    PHANTOM,
                    NEW_QUANTITY,
                    UDF_MEINS,
                    SATISFYDMDCOMPLETE,
                    COMPONENTGROUP,
                    EFFSTARTDATE,
                    EFFENDDATE,
                    YIELD,
                    SPLITPERCENT,
                    'AnDan'
     FROM
    (
    SELECT DISTINCT AM.SCENARIO_ID,
                    AM.ENGINE_ID,
                    AM.ENTERPRISE,
                    AM.SITEID,
                    AM.PROPERTY,
                    IC.NEW_ITEM_ID,
                    AG.BOMID,
                    AG.ROUTINGID,
                    AM.OPERATIONSEQ,
                    AM.PHANTOM,
                    1 NEW_QUANTITY,
                    IM.UDF_MEINS,
                    AM.SATISFYDMDCOMPLETE,
                    AM.COMPONENTGROUP,
                    AM.EFFSTARTDATE,
                    AM.EFFENDDATE,
                    AM.YIELD,
                    AM.SPLITPERCENT,
                    'AnDan',
                    ROW_NUMBER()
                     OVER(PARTITION BY  IC.NEW_ITEM_ID,AG.BOMID ORDER BY NULL)RN
     FROM IN_BOM_CHANGE IC 
     JOIN ABPPMGR.MST_ITEMBOMROUTING AG
      ON IC.PARENT_ITEM_ID=AG.ITEM
    JOIN  ABPPMGR.MST_BOMCOMPONENTS AM
    ON  TRIM(AM.BOMID)=SUBSTR(AG.BOMID,1,INSTR(AG.BOMID,'_',1,4)-1)
    LEFT JOIN IN_ITEM IM
    ON IC.NEW_ITEM_ID=IM.ITEM_ID
    WHERE IC.ACTION='NEW'
    AND AG.SYS_CREATED_BY='AnDan')
    WHERE RN=1;
    COMMIT;
     --    若是ACTION为替换,且原来的BOMCOMPONENTS. ITEM= IN_BOM_CHANGE. OLD_ITEM_ID

     ---    若是ACTION为删除,且原来的BOMCOMPONENTS. ITEM= IN_BOM_CHANGE. OLD_ITEM_ID则删除此列数据(目前无此厂景,保留功能)
         ---VIP标签的删除,从成品层删减标签
       DELETE FROM  ABPPMGR.MST_BOMCOMPONENTS AM WHERE 
         EXISTS(SELECT NULL FROM IN_BOM_CHANGE IC JOIN ABPPMGR.MST_ITEMBOMROUTING AA
                  ON IC.PARENT_ITEM_ID=AA.ITEM         
                 WHERE AM.ITEM=IC.OLD_ITEM_ID
                  AND  AA.BOMID=AM.BOMID
                  AND  IC.ACTION='DEL'
                  AND LENGTH(AA.BOMID)>29)
               AND AM.SYS_CREATED_BY='AnDan';
      COMMIT;   
 
   --end add  
   
   */
   
INSERT INTO ABPPMGR.MST_BOMCOMPONENTSALT (SCENARIO_ID,
                                           SITEID,
                                           ENGINE_ID,
                                           ENTERPRISE,
                                           property,
                                           --ALTCOMPONENTSITEID,
                                           --YIELDCALENDAR,
                                           ALTEFFSTARTDATE,
                                           ALTERNATEITEM,
                                           BOMID,
                                           COMPONENTGROUP,
                                           EFFSTARTDATE,
                                           ITEM,
                                           OPERATIONSEQ,
                                           ROUTINGID,
                                           PHANTOM,
                                           PRIORITY,
                                           QTYPER,
                                           QTYUOM,
                                           SATISFYDMDCOMPLETE,
                                           YIELD,
                                           SPLITPERCENT,
                                           SYS_CREATED_BY)
     SELECT B.SCENARIO_ID,
              B.SITEID,
              B.ENGINE_ID,
              B.ENTERPRISE,
              b.PROPERTY,  
              --B.SITEID,
              --YIELDCALENDAR,
              case when C.EFFSTARTDATE<sysdate then null else C.EFFSTARTDATE end ALTEFFSTARTDATE,
              C.ITEM_ID ALTERNATEITEM,
              B.BOMID,
              B.COMPONENTGROUP,
              B.EFFSTARTDATE,
              B.ITEM,
              B.OPERATIONSEQ,
              B.ROUTINGID,
              B.PHANTOM,
              C.CHANGED_PRIORITY PRIORITY,
              B.QTYPER,
              B.QTYUOM,
              B.SATISFYDMDCOMPLETE,
              B.YIELD,
              C.PURRATIO RATIO,
              B.SYS_CREATED_BY
             FROM  ABPPMGR.MST_BOMCOMPONENTS B , TEMP_BOM_CHANGE C 
             WHERE B.BOMID = 
             CASE WHEN LENGTH('' || LTRIM (C.PRODUCED_ITEM_ID, '0') || '_' || TRIM(C.BOM_ID))>40 THEN '' || SUBSTR(LTRIM (C.PRODUCED_ITEM_ID, '0'),1,13) || 

'_' || TRIM(C.BOM_ID)
        ELSE '' || LTRIM (C.PRODUCED_ITEM_ID, '0') || '_' || TRIM(C.BOM_ID) END 
             AND C.CHANGED_PRIORITY>1 AND C.COMPONENTGROUP=B.COMPONENTGROUP 
             AND B.SYS_CREATED_BY<>'AnDan' ;--add by Landor 20180522 原始程序不动新增
    COMMIT;
    
   --ADD BY HUANGYANGXIONG ON 20150423  更新MST_BOMCOMPONENTSALT替代料的用量.
   --原逻辑是直接取上层BOM的用量,更新后的逻辑是通过单位转换,来获取替代料的用量
   --第1步,先处理替代料的BOM用量
   STG.SAP_BOMALT_QTY_CONVERT;
   
   --第2步,更新MST_BOMCOMPONENTSALT的用量和单位
   UPDATE  ABPPMGR.MST_BOMCOMPONENTSALT AM SET (AM.QTYPER,AM.QTYUOM) = (SELECT DISTINCT
   UC.CONVERT_USAGE_QTY,CONVERT_USAGE_QTY_UOM
   FROM TEMP_BOMALT_UOM_CONVERT UC
   WHERE AM.ALTERNATEITEM = UC.ITEM_ID
     AND TRIM(SUBSTR(AM.BOMID,INSTR(AM.BOMID,'_')+1)) = TRIM(UC.BOM_ID)
     AND AM.COMPONENTGROUP = UC.COMPONENTGROUP
   )
   WHERE EXISTS (SELECT NULL FROM TEMP_BOMALT_UOM_CONVERT BU WHERE AM.ALTERNATEITEM = BU.ITEM_ID
     AND TRIM(SUBSTR(AM.BOMID,INSTR(AM.BOMID,'_')+1)) = TRIM(BU.BOM_ID)
     AND AM.COMPONENTGROUP = BU.COMPONENTGROUP )
   ; 
   
   COMMIT;
    
    
   V_Step := V_Step + 1;                                                 -- 3;
   V_Action := PKG_CONSTANT.CONS_ACTION_MERGE;
   V_MainTable := 'MST_BOMCOMPONENTS';
   --记录MST_BOMCOMPONENTS导入日志
   PKG_UTIL.CREATELOG (V_PKGName,                      --Put package name here
                       V_ProName,                    --Put procedure name here
                       V_Step,                      --Step seqence No. if have
                       V_Action,                      --Action 'I','D','U','T'
                       PKG_CONSTANT.CONS_MSG_INFO, --Message tppe, 'SUCCESS' or 'EXCEPTION'
                       'Success',               --Any generic information here
                       V_MainTable);              --Major table name processed

   --3.    保税非保税处理/验货客户处理: 原材設定屬性讓IN_ITEM_SITE.PROC_TYPE是E70或F71可以使用非保稅庫存

   MERGE INTO ABPPMGR.MST_BOMCOMPONENTS M
        USING (SELECT ITEM_ID, SITE_ID, TRIM(PROC_TYPE)  ||  TRIM(SP_PROC_TYPE)  PROC_TYPE
                 FROM IN_ITEM_SITE
                WHERE TRIM(PROC_TYPE) || TRIM(SP_PROC_TYPE) IN ('E70', 'F71')) IM
           ON (M.ITEM = IM.ITEM_ID
               AND SUBSTR (M.BOMID,
                           INSTR (M.BOMID,
                                  '_',
                                  1,
                                  2)
                           + 1,
                           4) = IM.SITE_ID)
   WHEN MATCHED
   THEN
      UPDATE SET M.PROPERTY = IM.ITEM_ID ||'_'|| IM.SITE_ID,
         M.SYS_LAST_MODIFIED_BY = V_ProName,
         M.SYS_LAST_MODIFIED_DATE = V_CurrentTime;

   MERGE INTO ABPPMGR.MST_BOMCOMPONENTSALT M
        USING (SELECT ITEM_ID, SITE_ID, TRIM(PROC_TYPE) || TRIM(SP_PROC_TYPE) PROC_TYPE
                 FROM IN_ITEM_SITE
                WHERE TRIM(PROC_TYPE) || TRIM(SP_PROC_TYPE) IN ('E70', 'F71')) IM
           ON (M.ITEM = IM.ITEM_ID
               AND SUBSTR (M.BOMID,
                           INSTR (M.BOMID,
                                  '_',
                                  1,
                                  2)
                           + 1,
                           4) = IM.SITE_ID)
   WHEN MATCHED
   THEN
      UPDATE SET M.PROPERTY =IM.ITEM_ID ||'_'|| IM.SITE_ID,
         M.SYS_LAST_MODIFIED_BY = V_ProName,
         M.SYS_LAST_MODIFIED_DATE = V_CurrentTime;

   V_Step := V_Step + 1;
   V_Action := PKG_CONSTANT.CONS_ACTION_UPDATE;
   V_MainTable := 'MST_BOMCOMPONENTS';
   --3.    动态安全库存: Calendar based Safety Lead Times for Procured Parts
   PKG_UTIL.CREATELOG (V_PKGName,                      --Put package name here
                       V_ProName,                    --Put procedure name here
                       V_Step,                      --Step seqence No. if have
                       V_Action,                      --Action 'I','D','U','T'
                       PKG_CONSTANT.CONS_MSG_INFO, --Message tppe, 'SUCCESS' or 'EXCEPTION'
                       'Success: ' || SQL%ROWCOUNT || ' rows updated.', --Any generic information here
                       V_MainTable);
   COMMIT;

   INSERT INTO ABPPMGR.MST_BOMCOMPONENTS_P (BOMID,
                                            ITEM,
                                            SITEID,
                                            ENGINE_ID,
                                            ENTERPRISE,
                                            EFFSTARTDATE,
                                            ROUTINGID,
                                            OPERATIONSEQ,
                                            SCENARIO_ID,
                                            ATTRIBUTECLASS,
                                            ATTRIBUTE,
                                            PASSTHRU,
                                            RELATIONSHIP,
                                            VALUE,
                                            SYS_CREATED_BY)
      SELECT BOMID,
             ITEM,
             SITEID,
             ENGINE_ID,
             ENTERPRISE,
             EFFSTARTDATE,
             ROUTINGID,
             OPERATIONSEQ,
             SCENARIO_ID,
             'PASS_ALL' ATTRIBUTECLASS,
             'FACTORY' ATTRIBUTE,
             NULL PASSTHRU,
             'PASS' RELATIONSHIP,
             NULL     VALUE,
                V_ProName SYS_CREATED_BY
        FROM ABPPMGR.MST_BOMCOMPONENTS M
       WHERE M.PROPERTY is not null
       AND NOT EXISTS(SELECT NULL FROM IN_ITEM_SITE IM
          WHERE IM.ITEM_ID=M.ITEM  AND SUBSTR (M.BOMID, INSTR (M.BOMID, '_', 1, 2)+ 1, 4) = IM.SITE_ID
          AND  TRIM(IM.PROC_TYPE) || TRIM(IM.SP_PROC_TYPE)='E70')
          AND M.SYS_CREATED_BY<>'AnDan'--add by Landor 20180522 原始程序不动新增
          ;
    COMMIT;
    
    --start add 1212
    --ADD BY Landor ON 20180522 复制ABPPMGR.MST_BOMCOMPONENTS_P数据再修改BOMID
    INSERT INTO ABPPMGR.MST_BOMCOMPONENTS_P (BOMID,
                                            ITEM,
                                            SITEID,
                                            ENGINE_ID,
                                            ENTERPRISE,
                                            EFFSTARTDATE,
                                            ROUTINGID,
                                            OPERATIONSEQ,
                                            SCENARIO_ID,
                                            ATTRIBUTECLASS,
                                            ATTRIBUTE,
                                            PASSTHRU,
                                            RELATIONSHIP,
                                            VALUE,
                                            SYS_CREATED_BY)
                    SELECT DISTINCT         P.BOMID||'_'||IC.SO_ID||SUBSTR(IC.SO_LINE_ID,1,INSTR(IC.SO_LINE_ID,'_')-1)BOMID,
                                            P.ITEM,
                                            P.SITEID,
                                            P.ENGINE_ID,
                                            P.ENTERPRISE,
                                            P.EFFSTARTDATE,
                                            P.ROUTINGID,
                                            P.OPERATIONSEQ,
                                            P.SCENARIO_ID,
                                            P.ATTRIBUTECLASS,
                                            P.ATTRIBUTE,
                                            P.PASSTHRU,
                                            P.RELATIONSHIP,
                                            P.VALUE,
                                            G.SYS_CREATED_BY
                        FROM ABPPMGR.MST_BOMCOMPONENTS_P P 
                             JOIN ABPPMGR.MST_ITEMBOMROUTING G
                              ON P.BOMID=SUBSTR(G.BOMID,1,INSTR(G.BOMID,'_',1,4)-1)
                             JOIN IN_BOM_CHANGE IC
                              ON  SUBSTR(P.BOMID,INSTR(P.BOMID,'_')+1,LENGTH(P.BOMID))=TRIM(IC.BOM_ID)
                            WHERE G.SYS_CREATED_BY='AnDan'
              union
              SELECT                        BOMID,
                                            ITEM,
                                            SITEID,
                                            ENGINE_ID,
                                            ENTERPRISE,
                                            EFFSTARTDATE,
                                            ROUTINGID,
                                            OPERATIONSEQ,
                                            SCENARIO_ID,
                                            'PASS_ALL'ATTRIBUTECLASS,
                                            'FACTORY'ATTRIBUTE,
                                            null PASSTHRU,
                                            'PASS'RELATIONSHIP,
                                            ''VALUE,
                                            SYS_CREATED_BY
                        FROM ABPPMGR.MST_BOMCOMPONENTS WHERE SYS_CREATED_BY='AnDan';
     COMMIT; 
    --end add1212
    
    
   INSERT INTO ABPPMGR.MST_BOMCOMPONENTSALT_P (BOMID,
                                               ITEM,
                                               ALTERNATEITEM,
                                               SITEID,
                                               ENGINE_ID,
                                               ENTERPRISE,
                                               EFFSTARTDATE,
                                               ROUTINGID,
                                               OPERATIONSEQ,
                                               COMPONENTGROUP,
                                               ALTEFFSTARTDATE,
                                               ALTCOMPONENTSITEID,
                                               SCENARIO_ID,
                                               ATTRIBUTECLASS,
                                               ATTRIBUTE,
                                               PASSTHRU,
                                               RELATIONSHIP,
                                               VALUE,SYS_CREATED_BY)
      SELECT BOMID,
             ITEM,
             ALTERNATEITEM,
             SITEID,
             ENGINE_ID,
             ENTERPRISE,
             EFFSTARTDATE,
             ROUTINGID,
             OPERATIONSEQ,
             COMPONENTGROUP,
             ALTEFFSTARTDATE,
             ALTCOMPONENTSITEID,
             SCENARIO_ID,
             'PASS_ALL',
             'FACTORY' ATTRIBUTE,
             NULL PASSTHRU,
             'PASS' RELATIONSHIP,
             NULL  VALUE,
                V_ProName SYS_CREATED_BY
        FROM ABPPMGR.MST_BOMCOMPONENTSALT M
       WHERE M.PROPERTY IS NOT NULL
       AND NOT EXISTS(SELECT NULL FROM IN_ITEM_SITE IM
          WHERE IM.ITEM_ID=M.ALTERNATEITEM  AND SUBSTR (M.BOMID, INSTR (M.BOMID, '_', 1, 2)+ 1, 4) = IM.SITE_ID
          AND  TRIM(IM.PROC_TYPE) || TRIM(IM.SP_PROC_TYPE) ='E70')
          AND M.SYS_CREATED_BY<>'AnDan'--add by Landor 20180522 原始程序不动新增
          ;
       COMMIT;                       
       
       /*  hide by landor on 20180705
       --start add 1111  add by Landor on 20180522 
          ---按单h若是IN_BOM_CHANGE. PARENT_ITEM_ID不在MST_BOMCOMPONENTS.ITEM中,表示在替料表中,所以新增BOM数据在MST_BOMCOMPONENTSALT
        INSERT INTO ABPPMGR.MST_BOMCOMPONENTSALT (SCENARIO_ID,
                                           SITEID,
                                           ENGINE_ID,
                                           ENTERPRISE,
                                           property,
                                           ALTEFFSTARTDATE,
                                           ALTERNATEITEM,
                                           BOMID,
                                           COMPONENTGROUP,
                                           EFFSTARTDATE,
                                           ITEM,
                                           OPERATIONSEQ,
                                           ROUTINGID,
                                           PHANTOM,
                                           PRIORITY,
                                           QTYPER,
                                           QTYUOM,
                                           SATISFYDMDCOMPLETE,
                                           YIELD,
                                           SPLITPERCENT,
                                           SYS_CREATED_BY)
              SELECT DISTINCT   MB.SCENARIO_ID
                        ,MB.SITEID
                        ,MB.ENGINE_ID
                        ,MB.ENTERPRISE
                        ,MB.property
                        ,MB.ALTEFFSTARTDATE
                        ,CASE WHEN MB.ITEM=IC.OLD_ITEM_ID THEN IC.NEW_ITEM_ID
                         ELSE MB.ALTERNATEITEM--MB.ITEM
                         END AS ITEM
                        ,AG.BOMID
                        ,MB.COMPONENTGROUP
                        ,MB.EFFSTARTDATE                             
                        ,MB.ITEM
                        ,MB.OPERATIONSEQ
                        ,AG.ROUTINGID
                        ,MB.PHANTOM
                        ,MB.PRIORITY
                        ,MB.QTYPER--IC.NEW_QUANTITY
                        ,MB.QTYUOM
                        ,MB.SATISFYDMDCOMPLETE
                        ,MB.YIELD
                        ,MB.SPLITPERCENT
                        ,'AnDan'
                        
            FROM IN_BOM_CHANGE IC 
            JOIN ABPPMGR.MST_ITEMBOMROUTING AG
              ON IC.PARENT_ITEM_ID=AG.ITEM
            JOIN  ABPPMGR.MST_BOMCOMPONENTSALT MB
            ON  TRIM(MB.BOMID)=SUBSTR(AG.BOMID,1,INSTR(AG.BOMID,'_',1,4)-1)
            --AND IC.ACTION='MODIFY'
            AND AG.SYS_CREATED_BY='AnDan';
            COMMIT;
            --    若是ACTION为替换,且原来的MST_BOMCOMPONENTSALT. ITEM= IN_BOM_CHANGE. OLD_ITEM_ID

             ---    若是ACTION为删除,且原来的MST_BOMCOMPONENTSALT. ITEM= IN_BOM_CHANGE. OLD_ITEM_ID则删除此列数据(目前无此厂景,保留功能)  
               DELETE ABPPMGR.MST_BOMCOMPONENTSALT AM WHERE 
                 EXISTS(SELECT NULL FROM IN_BOM_CHANGE IC
                    WHERE AM.ALTERNATEITEM=IC.OLD_ITEM_ID
                      AND  TRIM(IC.BOM_ID)=SUBSTR(AM.BOMID,INSTR(AM.BOMID,'_')+1,LENGTH(AM.BOMID))
                      AND  IC.ACTION='DEL')
                      AND AM.SYS_CREATED_BY='AnDan';
              COMMIT; 
       --end add 1111
       */
       
       --ADD BY HUANGYANGXIONG ON 20160615 更新替代料单位不为PCS的用量,旧的逻辑在处理多个替代料且用量单位不同时替代料的用量有问题,同一个BOM里面,对于同一个编码只允许一个单位的存在。碳粉存在KG和G两种。(举例:102011000619,2022工厂,工单号:1200226572)
       MERGE INTO ABPPMGR.MST_BOMCOMPONENTSALT AM
        USING (SELECT DISTINCT LTRIM(PRODUCT_ID,'0')||'_'||TRIM (BOM_ID) BOM_ID,
                  ITEM_ID,
                  COMPONENTGROUP,
                  UOM,
                  SUM (USAGE_QTY) OVER (PARTITION BY BOM_ID, ITEM_ID) SUM_QTY
             FROM (SELECT DT.BOM_ID,
                          BH.PRODUCT_ID,
                          DT.ITEM_ID,
                          DT.COMPONENTGROUP,
                            DT.USAGE_QTY
                          * FP_GET_CONVRATE (DT.ITEM_ID,
                                             DT.USAGE_QTY_UOM,
                                             TM.UDF_MEINS)
                             USAGE_QTY,
                          DT.USAGE_QTY_UOM,
                          DT.USAGE_QTY USAGE_QTY_BOM,
                          TM.UDF_MEINS UOM,
                          DT.PRODUCT_TYPE
                     FROM IN_BOM_DETAILS DT, IN_ITEM TM, IN_BOM_HEADER BH
                    WHERE  BH.BOM_ID = DT.BOM_ID
                      AND   EXISTS
                                 (SELECT NULL
                                    FROM OUT_MO_item_BACK MO
                                   WHERE TRIM (DT.BOM_ID) = MO.BOM_ID)
                          AND TM.ITEM_ID = DT.ITEM_ID
                          AND TRIM(DT.COMPONENTGROUP) IS NOT NULL
                          AND TRIM(DT.PRIORITY)<>'1' )
               WHERE UOM <>'PCS') ALT
        ON (AM.BOMID = ALT.BOM_ID AND AM.ALTERNATEITEM = ALT.ITEM_ID AND TRIM(AM.COMPONENTGROUP) = TRIM(ALT.COMPONENTGROUP) AND AM.QTYUOM = ALT.UOM)
        WHEN MATCHED THEN
        UPDATE SET AM.QTYPER = ALT.SUM_QTY;

        COMMIT;

/*  hide by landor on 20180705
        --add by Landor on 20180522 复制ABPPMGR.MST_BOMCOMPONENTSALT_P数据再修改BOMID
        INSERT INTO ABPPMGR.MST_BOMCOMPONENTSALT_P    (BOMID,
                                               ITEM,
                                               ALTERNATEITEM,
                                               SITEID,
                                               ENGINE_ID,
                                               ENTERPRISE,
                                               EFFSTARTDATE,
                                               ROUTINGID,
                                               OPERATIONSEQ,
                                               COMPONENTGROUP,
                                               ALTEFFSTARTDATE,
                                               ALTCOMPONENTSITEID,
                                               SCENARIO_ID,
                                               ATTRIBUTECLASS,
                                               ATTRIBUTE,
                                               PASSTHRU,
                                               RELATIONSHIP,
                                               VALUE,
                                               SYS_CREATED_BY)
                    SELECT       DISTINCT      P.BOMID||'_'||IC.SO_ID||SUBSTR(IC.SO_LINE_ID,1,INSTR(IC.SO_LINE_ID,'_')-1)BOMID,
                                               P.ITEM,
                                               P.ALTERNATEITEM,
                                               P.SITEID,
                                               P.ENGINE_ID,
                                               P.ENTERPRISE,
                                               P.EFFSTARTDATE,
                                               P.ROUTINGID,
                                               P.OPERATIONSEQ,
                                               P.COMPONENTGROUP,
                                               P.ALTEFFSTARTDATE,
                                               P.ALTCOMPONENTSITEID,
                                               P.SCENARIO_ID,
                                               P.ATTRIBUTECLASS,
                                               P.ATTRIBUTE,
                                               to_char(P.PASSTHRU)PASSTHRU,
                                               P.RELATIONSHIP,
                                               P.VALUE,
                                               G.SYS_CREATED_BY
                      FROM ABPPMGR.MST_BOMCOMPONENTSALT_P P 
                      JOIN ABPPMGR.MST_ITEMBOMROUTING G
                              ON P.BOMID=SUBSTR(G.BOMID,1,INSTR(G.BOMID,'_',1,4)-1)
                             JOIN IN_BOM_CHANGE IC
                              ON  SUBSTR(P.BOMID,INSTR(P.BOMID,'_')+1,LENGTH(P.BOMID))=TRIM(IC.BOM_ID)
                            WHERE G.SYS_CREATED_BY='AnDan'
                            union
                                        SELECT BOMID,
                                               ITEM,
                                               ALTERNATEITEM,
                                               SITEID,
                                               ENGINE_ID,
                                               ENTERPRISE,
                                               EFFSTARTDATE,
                                               ROUTINGID,
                                               OPERATIONSEQ,
                                               COMPONENTGROUP,
                                               ALTEFFSTARTDATE,
                                               ALTCOMPONENTSITEID,
                                               SCENARIO_ID,
                                               'PASS_ALL'ATTRIBUTECLASS,
                                               'FACTORY'ATTRIBUTE,
                                               ''PASSTHRU,
                                               'PASS'RELATIONSHIP,
                                               ''VALUE,
                                               SYS_CREATED_BY
                     FROM ABPPMGR.MST_BOMCOMPONENTSALT WHERE SYS_CREATED_BY='AnDan';

             COMMIT;
*/

        --ADD BY ZHANGGUIPENG ON 20170729 针对部分组件维护了损耗比例没将其取进FP计算的组件加上损耗比例
        MERGE INTO ABPPMGR.MST_BOMCOMPONENTSALT MB
              USING(SELECT DISTINCT IB.ITEM_ID,
                            IB.USAGE_QTY,
                            IB.COMPONENT_YIELD_UOM,
                            IB.USAGE_QTY_UOM,
                            IB.BOM_ID,
                            IB.USAGE_QTY+IB.COMPONENT_YIELD_UOM FUSE_QTY,
                            MST.QTYPER,
                            ROW_NUMBER()
                              OVER(PARTITION BY IB.ITEM_ID,IB.BOM_ID
                                        ORDER BY 1,2 DESC)RN                    
             FROM IN_BOM_DETAILS IB 
             JOIN
             ABPPMGR.MST_BOMCOMPONENTSALT MST
                   ON  IB.ITEM_ID=MST.ALTERNATEITEM
                   AND SUBSTR(MST.BOMID,INSTR(MST.BOMID,'_',1)+1)=TRIM(IB.BOM_ID)
             WHERE IB.COMPONENT_YIELD_UOM<>0
                   AND IB.USAGE_QTY+IB.COMPONENT_YIELD_UOM<>MST.QTYPER
                   ORDER BY 1 DESC)BI
         ON(MB.ALTERNATEITEM=BI.ITEM_ID
           AND SUBSTR(MB.BOMID,INSTR(MB.BOMID,'_',1)+1)=TRIM(BI.BOM_ID)
           AND BI.RN=1)
         WHEN MATCHED THEN 
             UPDATE SET MB.QTYPER=BI.FUSE_QTY;
         
         COMMIT;

        --start add 2222   add by Landor on 20180522
         --按单h在之前新增的ROUTINGID都需要新增到MST_ROUTINGHEADER
        INSERT --ALL 
            INTO ABPPMGR.MST_ROUTINGHEADER AM( SCENARIO_ID,
                                       ENTERPRISE,
                                       SITEID,
                                       ENGINE_ID,
                                       ROUTINGID,
                                       SYS_CREATED_BY)
                      SELECT * FROM      ( SELECT DISTINCT MB.SCENARIO_ID
                                     ,MB.ENTERPRISE
                                     ,MB.SITEID
                                     ,MB.ENGINE_ID
                                     ,MB.ROUTINGID
                                     ,SYS_CREATED_BY
                      FROM ABPPMGR.MST_BOMCOMPONENTS MB 
                       WHERE SYS_CREATED_BY='AnDan'
                      UNION 
                      SELECT DISTINCT MB.SCENARIO_ID
                                     ,MB.ENTERPRISE
                                     ,MB.SITEID
                                     ,MB.ENGINE_ID
                                     ,MB.ROUTINGID
                                     ,SYS_CREATED_BY
                      FROM ABPPMGR.MST_BOMCOMPONENTSALT MB 
                       WHERE SYS_CREATED_BY='AnDan'
                     UNION
                   SELECT    DISTINCT MB.SCENARIO_ID
                                     ,MB.ENTERPRISE
                                     ,MB.SITEID
                                     ,MB.ENGINE_ID
                                     ,SUBSTR(MB.ROUTINGID,1,INSTR(MB.ROUTINGID,'_',1,2)-1)||'_ORDER_BOM' ROUTINGID
                                     ,SYS_CREATED_BY
                      FROM ABPPMGR.MST_BOMCOMPONENTS MB 
                       WHERE SYS_CREATED_BY='AnDan'
                      UNION 
                      SELECT DISTINCT MB.SCENARIO_ID
                                     ,MB.ENTERPRISE
                                     ,MB.SITEID
                                     ,MB.ENGINE_ID
                                     ,SUBSTR(MB.ROUTINGID,1,INSTR(MB.ROUTINGID,'_',1,2)-1)||'_ORDER_BOM' ROUTINGID
                                     ,SYS_CREATED_BY
                      FROM ABPPMGR.MST_BOMCOMPONENTSALT MB 
                       WHERE SYS_CREATED_BY='AnDan') A WHERE NOT EXISTS (SELECT NULL  FROM  ABPPMGR.MST_ROUTINGHEADER B WHERE A.ROUTINGID=B.ROUTINGID);  --去掉重复add by landor on 20180605  
                             
            COMMIT;
    
         --按单h在之前新增的BOMID都需要新增到MST_BOMHEADER  add by Landor on 20180522
         --新增之前删除已有数据,避免重复
        INSERT INTO ABPPMGR.MST_BOMHEADER AM( SCENARIO_ID
                                      ,ENTERPRISE
                                      ,SITEID
                                      ,ENGINE_ID
                                      ,BOMID
                                      ,BOMTYPE
                                      ,EFFSTARTDATE
                                      ,EFFENDDATE
                                      ,SYS_CREATED_BY)
                      SELECT DISTINCT MB.SCENARIO_ID
                                     ,MB.ENTERPRISE
                                     ,MB.SITEID
                                     ,MB.ENGINE_ID
                                     ,MB.BOMID
                                     ,IH.BOM_TYPE
                                     ,IH.EFFECTIVE_START_DATE
                                     ,IH.EFFECTIVE_END_DATE
                                     ,SYS_CREATED_BY
                      FROM ABPPMGR.MST_BOMCOMPONENTS MB 
                          LEFT  JOIN  IN_BOM_HEADER IH 
                          ON SUBSTR(MB.BOMID,1,12)=LTRIM(IH.PRODUCT_ID,0)
                         AND SUBSTR(MB.BOMID,INSTR(MB.BOMID,'_')+1,INSTR(MB.BOMID,'_',1,2)-6)=TRIM(BOM_ID)
                       WHERE SYS_CREATED_BY='AnDan' AND NOT EXISTS(SELECT NULL FROM ABPPMGR.MST_BOMHEADER AM WHERE MB.BOMID=AM.BOMID)
                      UNION 
                      SELECT DISTINCT MB.SCENARIO_ID
                                     ,MB.ENTERPRISE
                                     ,MB.SITEID
                                     ,MB.ENGINE_ID
                                     ,MB.BOMID
                                     ,IH.BOM_TYPE
                                     ,IH.EFFECTIVE_START_DATE
                                     ,IH.EFFECTIVE_END_DATE
                                     ,SYS_CREATED_BY
                      FROM ABPPMGR.MST_BOMCOMPONENTSALT MB 
                          LEFT  JOIN  IN_BOM_HEADER IH 
                          ON SUBSTR(MB.BOMID,1,12)=LTRIM(IH.PRODUCT_ID,0)
                         AND SUBSTR(MB.BOMID,INSTR(MB.BOMID,'_')+1,INSTR(MB.BOMID,'_',1,2)-6)=TRIM(BOM_ID)
                       WHERE SYS_CREATED_BY='AnDan' AND NOT EXISTS(SELECT NULL FROM ABPPMGR.MST_BOMHEADER AM WHERE MB.BOMID=AM.BOMID); 
            COMMIT;     
                 
        --按单h在之前新增的ROUTINGID都需要新增MST_ROUTINGOPERATION  add by Landor on 20180522
         INSERT INTO ABPPMGR.MST_ROUTINGOPERATION(SCENARIO_ID,
                                          ENTERPRISE,
                                          SITEID,
                                          ENGINE_ID,
                                          ROUTINGID,
                                          OPERATIONSEQ,
                                          OPERATION,
                                          TIMEUOM,
                                          TEARDOWNTIME,
                                          MINFIXEDRUNTIME,
                                          VARQTYREJECTED,
                                          RUNTIMEPER,
                                          SYS_CREATED_BY)                                                                    
                         SELECT * FROM  (SELECT  DISTINCT MB.SCENARIO_ID,
                                           MB.ENTERPRISE,
                                           MB.SITEID,
                                           MB.ENGINE_ID,
                                           SUBSTR(MB.ROUTINGID,1,13)||'_ORDER_BOM'ROUTINGID,
                                          '10'  OPERATIONSEQ,
                                          '0010'OPERATION,
                                          'MINUTES'TIMEUOM,
                                          ''TEARDOWNTIME,
                                          '2880'MINFIXEDRUNTIME,
                                          '0'VARQTYREJECTED,
                                          '0'RUNTIMEPER,                                                                       
                                           MB.SYS_CREATED_BY
                      FROM ABPPMGR.MST_BOMCOMPONENTS MB      
                       WHERE MB.SYS_CREATED_BY='AnDan' 
                     UNION
                    SELECT  DISTINCT MB.SCENARIO_ID,
                                           MB.ENTERPRISE,
                                           MB.SITEID,
                                           MB.ENGINE_ID,
                                           MB.ROUTINGID,
                                          '10'  OPERATIONSEQ,
                                          '0010'OPERATION,
                                          'MINUTES'TIMEUOM,
                                          ''TEARDOWNTIME,
                                          '2880'MINFIXEDRUNTIME,
                                          '0'VARQTYREJECTED,
                                          '0'RUNTIMEPER,                                                                       
                                           MB.SYS_CREATED_BY
                      FROM ABPPMGR.MST_BOMCOMPONENTS MB      
                       WHERE MB.SYS_CREATED_BY='AnDan' 
                  UNION
                    SELECT  DISTINCT MB.SCENARIO_ID,
                                           MB.ENTERPRISE,
                                           MB.SITEID,
                                           MB.ENGINE_ID,
                                           SUBSTR(MB.ROUTINGID,1,13)||'_ORDER_BOM'ROUTINGID,
                                          '10'  OPERATIONSEQ,
                                          '0010'OPERATION,
                                          'MINUTES'TIMEUOM,
                                          ''TEARDOWNTIME,
                                          '2880'MINFIXEDRUNTIME,
                                          '0'VARQTYREJECTED,
                                          '0'RUNTIMEPER,                                                                       
                                           MB.SYS_CREATED_BY
                      FROM ABPPMGR.MST_BOMCOMPONENTSALT MB      
                       WHERE MB.SYS_CREATED_BY='AnDan' 
                       UNION
                     SELECT  DISTINCT MB.SCENARIO_ID,
                                           MB.ENTERPRISE,
                                           MB.SITEID,
                                           MB.ENGINE_ID,
                                           MB.ROUTINGID,
                                          '10'  OPERATIONSEQ,
                                          '0010'OPERATION,
                                          'MINUTES'TIMEUOM,
                                          ''TEARDOWNTIME,
                                          '2880'MINFIXEDRUNTIME,
                                          '0'VARQTYREJECTED,
                                          '0'RUNTIMEPER,                                                                       
                                           MB.SYS_CREATED_BY
                      FROM ABPPMGR.MST_BOMCOMPONENTSALT MB      
                       WHERE MB.SYS_CREATED_BY='AnDan') A 
                       WHERE NOT EXISTS (SELECT NULL FROM ABPPMGR.MST_ROUTINGOPERATION B WHERE A.ROUTINGID=B.ROUTINGID AND  A.OPERATIONSEQ=B.OPERATIONSEQ) 
                       ----add by landor on 20180609 ORA-02291: 违反完整约束条件 (ABPPMGR.C0248833319_8303) - 未找到父项关键字ORA-06512
                       AND EXISTS (SELECT NULL FROM ABPPMGR.MST_ROUTINGHEADER C  WHERE C.ROUTINGID=A.ROUTINGID AND  C.SCENARIO_ID=A.SCENARIO_ID ) 
                       ; 


        COMMIT;
       

        --end add 2222





    --如果替料无损耗的话,替料的=主料用量/(1+0.02) -add by landor on 2018.4.16
    MERGE INTO ABPPMGR.MST_BOMCOMPONENTSALT M 
    USING(SELECT A.BOM_ID ,A.COMPONENTGROUP,A.COMPONENT_YIELD_UOM ,A.USAGE_QTY,C.ITEM_ID
                 FROM IN_BOM_DETAILS A, IN_BOM_DETAILS C  
                WHERE  A.BOM_ID=C.BOM_ID AND A.COMPONENTGROUP=C.COMPONENTGROUP  AND A.USAGE_QTY_UOM=C.USAGE_QTY_UOM
                and  A.PRIORITY=1 --1代表主料,大于1代表替料
                AND A.COMPONENT_YIELD_UOM>0  --大于0代表主料有损耗
                and C.PRIORITY>1  --大于1为替料
                AND  C.COMPONENT_YIELD_UOM=0  --替料损耗为0
                 AND C.USAGE_QTY_UOM!='G'AND C.USAGE_QTY_UOM!='KG'  ----除碳粉外
                 )K
    ON (TRIM(K.BOM_ID)=Substr(M.BOMID,length(M.BOMID)-15,16) 
        AND K.COMPONENTGROUP=M.COMPONENTGROUP 
        AND K.ITEM_ID=M.ALTERNATEITEM)
    WHEN MATCHED THEN
    UPDATE SET  M.QTYPER= M.QTYPER/(1+K.COMPONENT_YIELD_UOM) ;
    COMMIT;            
               

   /* --如果替料有损耗的话,替料的=主料用量/(1+0.02)*(1+替料的损耗比例) 不含真验货客户数据 add by landor on 2018.4.16  hide on landor on 20180705
    MERGE INTO ABPPMGR.MST_BOMCOMPONENTSALT M 
    USING(SELECT A.BOM_ID ,A.COMPONENTGROUP,A.COMPONENT_YIELD_UOM ZLUOM ,C.COMPONENT_YIELD_UOM TLUOM ,A.USAGE_QTY,C.ITEM_ID
               FROM IN_BOM_DETAILS A, IN_BOM_DETAILS C  
                WHERE  A.BOM_ID=C.BOM_ID AND A.COMPONENTGROUP=C.COMPONENTGROUP  AND A.USAGE_QTY_UOM=C.USAGE_QTY_UOM
                and  A.PRIORITY=1 --1代表主料,大于1代表替料
                and C.PRIORITY>1  --大于1为替料
                AND  C.COMPONENT_YIELD_UOM>0  ----替料有损耗;
                 AND C.USAGE_QTY_UOM!='G'AND C.USAGE_QTY_UOM!='KG'  ----除碳粉外
                -- AND  NOT EXISTS
                -- (SELECT NULL
                 --   FROM TB_BOM_COMPONENTGROUP_HASYIELD X
                 --  WHERE X.BOM_ID = C.BOM_ID AND X.COMPONENTGROUP = C.COMPONENTGROUP) ---BOM 主替料中优化级序号维护异常,需过滤掉
                 )K
    ON (TRIM(K.BOM_ID)=Substr(M.BOMID,length(M.BOMID)-15,16) 
        AND K.COMPONENTGROUP=M.COMPONENTGROUP 
        AND K.ITEM_ID=M.ALTERNATEITEM
        AND M.BOMID NOT LIKE '%C%')
    WHEN MATCHED THEN
    UPDATE SET  M.QTYPER=M.QTYPER/(1+ZLUOM)*(1+TLUOM)      ;   
    COMMIT;  


     上面异常问题的跟踪查询
     SELECT K.BOM_ID ,K.ITEM_ID,K.COMPONENTGROUP,K.ZLUOM ,K.TLUOM ,K.USAGE_QTY
     FROM ABPPMGR.MST_BOMCOMPONENTSALT M
    JOIN  (
            SELECT A.BOM_ID ,A.COMPONENTGROUP,A.COMPONENT_YIELD_UOM ZLUOM ,C.COMPONENT_YIELD_UOM TLUOM ,A.USAGE_QTY,C.ITEM_ID
                   FROM IN_BOM_DETAILS A, IN_BOM_DETAILS C  
                    WHERE  A.BOM_ID=C.BOM_ID AND A.COMPONENTGROUP=C.COMPONENTGROUP  AND A.USAGE_QTY_UOM=C.USAGE_QTY_UOM
                    and  A.PRIORITY=1 --1代表主料,大于1代表替料
                    and C.PRIORITY>1  --大于1为替料
                    AND  C.COMPONENT_YIELD_UOM>0  ----替料有损耗;
                     AND C.USAGE_QTY_UOM!='G'AND C.USAGE_QTY_UOM!='KG'  )K
        ON (TRIM(K.BOM_ID)=Substr(M.BOMID,length(M.BOMID)-15,16) 
            AND K.COMPONENTGROUP=M.COMPONENTGROUP 
            AND K.ITEM_ID=M.ALTERNATEITEM
            AND M.BOMID NOT LIKE '%C%')
        ORDER BY K.BOM_ID ,K.ITEM_ID,K.COMPONENTGROUP*/
    

    --如果替料有损耗的话,替料的=主料用量/(1+0.02)*(1+替料的损耗比例) 
    --对真验货客户数据进行单独处理,方便按单生产切换上线后可取消 add by landor on 2018.4.17
    MERGE INTO ABPPMGR.MST_BOMCOMPONENTSALT M 
    USING(SELECT A.BOM_ID ,A.COMPONENTGROUP,A.COMPONENT_YIELD_UOM ZLUOM ,C.COMPONENT_YIELD_UOM TLUOM ,A.USAGE_QTY,C.ITEM_ID
               FROM IN_BOM_DETAILS A, IN_BOM_DETAILS C  
                WHERE  A.BOM_ID=C.BOM_ID AND A.COMPONENTGROUP=C.COMPONENTGROUP  AND A.USAGE_QTY_UOM=C.USAGE_QTY_UOM
                and  A.PRIORITY=1 --1代表主料,大于1代表替料
                and C.PRIORITY>1  --大于1为替料
                AND  C.COMPONENT_YIELD_UOM>0  ----替料有损耗;
                 AND C.USAGE_QTY_UOM!='G'AND C.USAGE_QTY_UOM!='KG'  ----除碳粉外
                 )K
    ON (TRIM(K.BOM_ID)=Substr(M.BOMID,length(M.BOMID)-17,18)
        AND K.COMPONENTGROUP=M.COMPONENTGROUP 
        AND K.ITEM_ID=M.ALTERNATEITEM
        AND M.BOMID  LIKE '%C%')
    WHEN MATCHED THEN
    UPDATE SET  M.QTYPER=M.QTYPER/(1+ZLUOM)*(1+TLUOM)      ;   
    COMMIT;  

    --如果替料无损耗的话,替料的=主料用量/(1+0.02) -add by landor on 2018.4.17
    MERGE INTO ABPPMGR.MST_BOMCOMPONENTSALT M 
    USING(SELECT A.BOM_ID ,A.COMPONENTGROUP,A.COMPONENT_YIELD_UOM ,A.USAGE_QTY,C.ITEM_ID
                 FROM IN_BOM_DETAILS A, IN_BOM_DETAILS C  
                WHERE  A.BOM_ID=C.BOM_ID AND A.COMPONENTGROUP=C.COMPONENTGROUP  AND A.USAGE_QTY_UOM=C.USAGE_QTY_UOM
                and  A.PRIORITY=1 --1代表主料,大于1代表替料
                AND A.COMPONENT_YIELD_UOM>0  --大于0代表主料有损耗
                and C.PRIORITY>1  --大于1为替料
                AND  C.COMPONENT_YIELD_UOM=0  --替料损耗为0
                 AND C.USAGE_QTY_UOM!='G'AND C.USAGE_QTY_UOM!='KG'  ----除碳粉外
                 )K
    ON (TRIM(K.BOM_ID)=Substr(M.BOMID,length(M.BOMID)-17,18) 
        AND K.COMPONENTGROUP=M.COMPONENTGROUP 
        AND K.ITEM_ID=M.ALTERNATEITEM
        AND M.BOMID  LIKE '%C%')
    WHEN MATCHED THEN
    UPDATE SET  M.QTYPER= M.QTYPER/(1+K.COMPONENT_YIELD_UOM) ;
    COMMIT;   


    --修复主替料场景中CTRD绑定失败问题  add by landor on 20180510
    --(paul回复说需要将工艺路线放空,那么所有相同編碼的不同routingid都可以使用)
    UPDATE ABPPMGR.MST_BOMCOMPONENTSALT SET ROUTINGID='' WHERE ALTERNATEITEM LIKE '%C%' AND  ITEM  LIKE '%C%' ;
    COMMIT;

       
EXCEPTION
   --总程序异常处理部分
   WHEN OTHERS
   THEN
      BEGIN
         ROLLBACK;
         EXITCODE := 1;

         e_ErrMessage :=
               e_ErrMessage
            || SUBSTR (SQLERRM, 1, 1000)
            || SUBSTR (DBMS_UTILITY.FORMAT_ERROR_BACKTRACE, 1, 2000);
         PKG_UTIL.CREATELOG (V_PKGName,
                             V_ProName,
                             V_Step,
                             V_Action,
                             PKG_CONSTANT.CONS_MSG_EXCEPTION,
                             e_ErrMessage,
                             V_MainTable);
      END;
END;
/
View Code

 

FP_MFG

CREATE OR REPLACE PROCEDURE STG.FP_MFG (EXITCODE OUT NUMBER)
IS
   /***********************************************************************
         **  存储过程名称:  FP_MFG
        **  存储过程创建日期: 2014/6/20
        **  存储过程创建人: Tanxiang
        **  目的:  从IN表导入数据到ODS schema
        --IN_ROUTING_HEADER  =>  MST_ROUTINGHEADER
        --IN_ROUTINGOPERATION  =>  MST_ROUTINGOPERATION
        --IN_RESOURCE  =>  MST_RESOURCEMASTER
        --IN_OPRESOURCE  => MST_OPRESOURCE
        --IN_ITEMBOMROUTING  =>  MST_ITEMBOMROUTING
        --IN_RES_CAPABILITY  =>MST_WORKCENTERMASTER,MST_WORKCENTERDETAIL
        **  输入参数:
        **  输出参数:EXITCODE 预留接口给外部调用者,让其知道存储过程的执行结果,0.预定逻辑执行完成; 1.遇到异常,预定逻辑未执行完成
        **  返回值:
        **  用法:
        **
        ** 修订版本:
        **  版本号     修订时间    修订人        描述
        **  ---------  ----------  ------------  ------------------------
        **    1.0      2014/6/20       Tanxiang 1. 新建存储过程.
        **   2.0      2015/01/15      huangyangxiong 1.增加硬绑逻辑
        **   3.0      20180522      Landor         新增按单生产业务
   **********************************************************************/

   ----------------------- 以下定义存储过程使用的变量---------------------------------
   V_PKGName          VARCHAR2 (40) := 'PKG_FP';
   V_ProName          VARCHAR2 (40) := 'FP_MFG';
   V_Action           VARCHAR2 (1);
   V_Step             NUMBER := 0;
   V_MainTable        VARCHAR2 (40);
   E_ErrMessage       VARCHAR2 (3000);
   V_Engine           NUMBER;
   V_Enterprise       VARCHAR2 (40);
   V_ExitCode         NUMBER := 0;
   V_SiteID           VARCHAR2 (40);
   V_Scenario_ID      NUMBER;
   V_StyleName        VARCHAR (40);
   V_WorkCenterName   VARCHAR (40);
   V_YieldCalendar    VARCHAR2 (40);
   V_ResourceGroup    VARCHAR2 (40) := 'RES_GRP';
   V_AuthID           VARCHAR2 (40) := 'System';
   V_LocationID       VARCHAR2 (40) := 'DUMMY_LOC';
   V_PatternName      VARCHAR2 (40) := '';
   V_CalendarName     VARCHAR2 (40) := 'RESOURCE_CALENDAR';
   V_CalendarType     VARCHAR2 (40) := 'CAPACITY';
   V_CurrentTime      DATE := SYSDATE;
--ADD BY HUANGYANGXIONG ON 20150918  新增工厂以参数形式获取   
   V_FACTORY1     VARCHAR2 (4);  --原2011
   V_FACTORY2     VARCHAR2 (4);  --原2012
   V_FACTORY3     VARCHAR2 (4);  --原2061
   V_FACTORY4     VARCHAR2 (4);  --原2062
   V_FACTORY5     VARCHAR2 (4);  --原2081
   V_FACTORY6     VARCHAR2 (4);  --原2501   
   
----------------------- 以下是存储过程的主体------------------------------------------
PROCEDURE GET_PARENT_BOM(P_PARENT_ID VARCHAR2,P_ITEM_ID VARCHAR2,P_USAGE_QTY NUMBER,P_PRODUCT_TIME NUMBER) IS
BEGIN
    FOR C_BOM IN (SELECT BD.ITEM_ID,substr(BH.BOM_ID,instr( BH.BOM_ID,'_')+1,4) SITE_ID, BD.USAGE_QTY*P_USAGE_QTY  USAGE_QTY,
       (select nvl(Max(ro.MINFIXEDRUNTIME),0) from abppmgr.mst_routingoperation ro,IN_ITEMBOMROUTING BR
         where ro.routingid=BR.routing_id and BR.produced_item_id=BD.ITEM_ID and BR.siteid=substr(BH.BOM_ID,instr( BH.BOM_ID,'_')+1,4)) 
     + P_PRODUCT_TIME PRODUCT_TIME
  FROM IN_BOM_DETAILS BD, IN_BOM_HEADER BH
 WHERE     TRIM(BD.BOM_ID) = TRIM(BH.BOM_ID)
       AND BH.PRODUCT_ID = P_ITEM_ID
       AND BH.MO_BOM_MARK = '0'
       AND exists( select null from in_item_site iis where IIS.ITEM_ID = BD.ITEM_ID
       AND IIS.SITE_ID= substr(BH.BOM_ID,instr( BH.BOM_ID,'_')+1,4)
       AND IIS.UDF_ITEM_CATEGORY = 'SEMI')) LOOP
    INSERT INTO TEMP_BOM_ITEM(PARENT_ID,SITE_ID,ITEM_ID,USAGE_QTY,PRODUCT_TIME)
    VALUES(P_PARENT_ID,C_BOM.SITE_ID,C_BOM.ITEM_ID,C_BOM.USAGE_QTY,C_BOM.PRODUCT_TIME/(24*60));
    COMMIT;
        GET_PARENT_BOM(P_PARENT_ID,C_BOM.ITEM_ID,C_BOM.USAGE_QTY,C_BOM.PRODUCT_TIME);
    END LOOP;
END;
BEGIN
   EXITCODE := 0;
   V_Scenario_ID := 0;
   V_Engine := PKG_CONSTANT.CONS_FP_ENGINE;
   V_Enterprise := PKG_CONSTANT.CONS_ENTERPRISE;
   V_SiteID := PKG_CONSTANT.CONS_SITE_ZHUHAI;
   
--ADD BY HUANGYANGXIONG ON 20150918 新增工厂以参数形式获取
   SELECT SUBSTR (CONFIG_VALUE, 1, INSTR (CONFIG_VALUE, ',') - 1) AS WERKS1,
          SUBSTR (CONFIG_VALUE, INSTR (CONFIG_VALUE, ',') + 1, 4) AS WERKS2,
          SUBSTR (CONFIG_VALUE, 11,4) AS WERKS3,
          SUBSTR (CONFIG_VALUE, 16,4) AS WERKS4,
          SUBSTR (CONFIG_VALUE, 21,4) AS WERKS5,
          SUBSTR (CONFIG_VALUE, 26,4) AS WERKS6
     INTO V_FACTORY1, V_FACTORY2, V_FACTORY3, V_FACTORY4, V_FACTORY5, V_FACTORY6
     FROM STG.IN_FP_CONFIG
    WHERE CONFIG_ID = 'FP_FACTORY';   

   /******************7/9 remarked by tanxiang start
      --IN_RESOURCE  =>  MST_RESOURCEMASTER
      V_Step := V_Step + 1;                                                 -- 4;
      V_Action := PKG_CONSTANT.CONS_ACTION_MERGE;
      V_MainTable := 'MST_RESOURCEMASTER';
      --记录MST_RESOURCEMASTER导入日志
      PKG_UTIL.CREATELOG (V_PKGName,                      --Put package name here
                          V_ProName,                    --Put procedure name here
                          V_Step,                      --Step seqence No. if have
                          V_Action,                      --Action 'I','D','U','T'
                          PKG_CONSTANT.CONS_MSG_INFO, --Message tppe, 'SUCCESS' or 'EXCEPTION'
                          'START',                 --Any generic information here
                          V_MainTable);              --Major table name processed

      --先把MST_RESOURCEMASTER中所有SYS_ENT_STATE置为INACTIVE
      UPDATE ABPPMGR.MST_RESOURCEMASTER m
         SET M.SYS_ENT_STATE = 'INACTIVE';

      --再从IN_RESOURCE导数据到MST_RESOURCEMASTER中,如果已经存在就更新,不存在就新加
      MERGE INTO ABPPMGR.MST_RESOURCEMASTER M
           USING IN_RESOURCE IM
              ON (    M.RESOURCENAME = IM.RESOURCENAME
                  AND M.SITEID = V_SiteID
                  AND M.ENTERPRISE = V_Enterprise
                  AND M.SCENARIO_ID = V_Scenario_ID)
      WHEN MATCHED
      THEN
         UPDATE SET M.SYS_ENT_STATE = 'ACTIVE'
      WHEN NOT MATCHED
      THEN
         INSERT     (SCENARIO_ID,
                     ENTERPRISE,
                     SITEID,
                     ENGINE_ID,
                     RESOURCENAME)
             VALUES (V_Scenario_ID,
                     V_Enterprise,
                     V_SiteID,
                     V_Engine,
                     IM.RESOURCENAME);

      COMMIT;
      V_Step := V_Step + 1;                                                 -- 5;
      V_Action := PKG_CONSTANT.CONS_ACTION_MERGE;
      V_MainTable := 'MST_RESOURCEMASTER';
      --记录MST_RESOURCEMASTER导入日志
      PKG_UTIL.CREATELOG (V_PKGName,                      --Put package name here
                          V_ProName,                    --Put procedure name here
                          V_Step,                      --Step seqence No. if have
                          V_Action,                      --Action 'I','D','U','T'
                          PKG_CONSTANT.CONS_MSG_INFO, --Message tppe, 'SUCCESS' or 'EXCEPTION'
                          'Success',               --Any generic information here
                          V_MainTable);              --Major table name processed

      --IN_OPRESOURCE  => MST_OPRESOURCE
      V_Step := V_Step + 1;                                                 -- 6;
      V_Action := PKG_CONSTANT.CONS_ACTION_MERGE;
      V_MainTable := 'MST_OPRESOURCE';
      --记录MST_OPRESOURCE导入日志
      PKG_UTIL.CREATELOG (V_PKGName,                      --Put package name here
                          V_ProName,                    --Put procedure name here
                          V_Step,                      --Step seqence No. if have
                          V_Action,                      --Action 'I','D','U','T'
                          PKG_CONSTANT.CONS_MSG_INFO, --Message tppe, 'SUCCESS' or 'EXCEPTION'
                          'START',                 --Any generic information here
                          V_MainTable);              --Major table name processed

      --先把MST_ROUTINGOPERATION中所有数据删除
      DELETE FROM ABPPMGR.MST_OPRESOURCE M;

      --再从IN_ROUTINGOPERATION导数据到MST_OPRESOURCE中,如果已经存在就更新,不存在就新加
      INSERT INTO ABPPMGR.MST_OPRESOURCE (SCENARIO_ID,
                                          ENTERPRISE,
                                          SITEID,
                                          ENGINE_ID,
                                          WORKCENTERNAME,
                                          ROUTINGID,
                                          OPERATIONSEQ,
                                          OPERATION,
                                          RESOURCENAME,
                                          RESOURCESITEID)
         SELECT V_Scenario_ID,
                V_Enterprise,
                V_SiteID,
                V_Engine,
                V_WorkCenterName,
                IM.ROUTING_ID,
                IM.OPERATIONSEQ,
                IM.OPERATION,
                IM.RESOURCE_NAME,
                IM.RESOURCESITEID
           FROM IN_OPRESOURCE IM;

      COMMIT;
      V_Step := V_Step + 1;                                                 -- 7;
      V_Action := PKG_CONSTANT.CONS_ACTION_MERGE;
      V_MainTable := 'MST_OPRESOURCE';
      --记录MST_OPRESOURCE导入日志
      PKG_UTIL.CREATELOG (V_PKGName,                      --Put package name here
                          V_ProName,                    --Put procedure name here
                          V_Step,                      --Step seqence No. if have
                          V_Action,                      --Action 'I','D','U','T'
                          PKG_CONSTANT.CONS_MSG_INFO, --Message tppe, 'SUCCESS' or 'EXCEPTION'
                          'Success',               --Any generic information here
                          V_MainTable);              --Major table name processed
   ******************7/9 remarked by tanxiang end*/
/* remarked  by tanxiang 20140903

   DELETE FROM TEMP_ITEM_MAX_PACKAGE;

   COMMIT;

   INSERT INTO TEMP_ITEM_MAX_PACKAGE (PRODUCT_ID, MATNR, MAX_PACKAGE)
      SELECT PRODUCT_ID, MATNR, ROUND ( (1 / USAGE_QTY)) MAX_PACKAGE
        FROM (SELECT BH.PRODUCT_ID PRODUCT_ID, BD.ITEM_ID MATNR, BD.USAGE_QTY,row_number() over(partition by BH.PRODUCT_ID order by BD.USAGE_QTY) rn
                FROM IN_BOM_HEADER BH, IN_BOM_DETAILS BD
               WHERE BH.BOM_ID = BD.BOM_ID and BH.MO_BOM_MARK='0'
                     AND LTRIM (BD.ITEM_ID, '0') LIKE '4067%') where rn=1;
   COMMIT;
   */
   -- WHERE MINUSAGE_QTY <> MAXUSAGE_QTY;

   --IN_ITEMBOMROUTING  =>  MST_ITEMBOMROUTING
   V_Step := V_Step + 1;                                                 -- 8;
   V_Action := PKG_CONSTANT.CONS_ACTION_MERGE;
   V_MainTable := 'MST_ITEMBOMROUTING';
   --记录MST_ITEMBOMROUTING导入日志
   PKG_UTIL.CREATELOG (V_PKGName,                      --Put package name here
                       V_ProName,                    --Put procedure name here
                       V_Step,                      --Step seqence No. if have
                       V_Action,                      --Action 'I','D','U','T'
                       PKG_CONSTANT.CONS_MSG_INFO, --Message tppe, 'SUCCESS' or 'EXCEPTION'
                       'START',                 --Any generic information here
                       V_MainTable);              --Major table name processed
    FOR C_ISSUE IN (SELECT PARENT_ID FROM IN_BOM_QUALITY_ISSUES WHERE TRIM(PARENT_ID)  IS NOT NULL GROUP BY PARENT_ID ) LOOP
            GET_PARENT_BOM(C_ISSUE.PARENT_ID,C_ISSUE.PARENT_ID,1,0);
    END LOOP;
   --先把MST_ITEMBOMROUTING中所有数据删除
   DELETE FROM ABPPMGR.MST_ITEMBOMROUTING M;
   COMMIT;
   DELETE FROM ABPPMGR.MST_ITEMBOMROUTING_P;
   COMMIT;
   --再从IN_ITEMBOMROUTING导数据到MST_ITEMBOMROUTING中
   INSERT INTO ABPPMGR.MST_ITEMBOMROUTING (SCENARIO_ID,
                                           ENTERPRISE,
                                           SITEID,
                                           ENGINE_ID,
                                           BOMID,
                                           ROUTINGID,
                                           ITEM,
                                           PRIORITY,
                                           MAXLOTSIZE,
                                           MINLOTSIZE,
                                           USABLEBYNEWMFGORD,
                                           EFFSTARTDATE,
                                           --YIELDCALENDAR,
                                           --PROPERTY,
                                           SYS_CREATED_BY)
      SELECT DISTINCT V_Scenario_ID,
             V_Enterprise,
             V_SiteID,
             V_Engine,
             CASE WHEN LENGTH('' || LTRIM (IM.PRODUCED_ITEM_ID, '0') || '_' || TRIM(IM.BOM_ID))>40 THEN '' || SUBSTR(LTRIM (IM.PRODUCED_ITEM_ID, '0'),1,13) || '_' || TRIM(IM.BOM_ID)
        ELSE '' || LTRIM (IM.PRODUCED_ITEM_ID, '0') || '_' || TRIM(IM.BOM_ID) END AS BOM_ID,
             IM.ROUTING_ID,
             IM.PRODUCED_ITEM_ID,
             DECODE(BH.MO_BOM_MARK,'1',0,1) PRIORITY,
             case when BH.MO_BOM_MARK='1' then
                NULL
             else
                -- case when  IM.SITEID='2061' and substr( IM.PRODUCED_ITEM_ID,7,'3')='401'  then
                 case when  IM.SITEID=V_FACTORY3 and substr( IM.PRODUCED_ITEM_ID,7,'3')='401'  then
                    MP.MAX_PACKAGE
                 --when  IM.SITEID='2061' and substr( IM.PRODUCED_ITEM_ID,7,'3')='407' then
                 --MODIFIED BY SHENG,将原来的407变成402501-402999 
                 --when  IM.SITEID=V_FACTORY3 and substr( IM.PRODUCED_ITEM_ID,7,'3')='407' then
                 when  IM.SITEID=V_FACTORY3 and TO_NUMBER(substr( IM.PRODUCED_ITEM_ID,7,'6'))>=402501 AND TO_NUMBER(substr( IM.PRODUCED_ITEM_ID,7,'6'))<=402999 then
                    null
                    --MODIFIED BY SHENG,将原来的402变成402000-402500
                 --when  IM.SITEID='2061' and substr( IM.PRODUCED_ITEM_ID,7,'3')='402' then 
                 when  IM.SITEID=V_FACTORY3 and TO_NUMBER(substr( IM.PRODUCED_ITEM_ID,7,'6'))>=402000 AND TO_NUMBER(substr( IM.PRODUCED_ITEM_ID,7,'6'))<=402500 then
                    null
                 --when  IM.SITEID in ('2011','2012') and substr( IM.PRODUCED_ITEM_ID,7,'3')='101' then 
                 when  IM.SITEID in (V_FACTORY1,V_FACTORY2) and substr( IM.PRODUCED_ITEM_ID,7,'3')='101' then
                    null
                 --add by huangyangxiong on 20160614  增加成品VC物料不按最大生产批量拆分
                 when  IM.SITEID in (V_FACTORY1,V_FACTORY2) and substr( LTRIM(IM.PRODUCED_ITEM_ID,'0'),1,4)='1111' then
                    null
                 --when  IM.SITEID in ('2011','2012') and substr( IM.PRODUCED_ITEM_ID,7,'3')='102' then 
                 --MODIFIED BY SHENG,将102变为102E
                 --when  IM.SITEID in (V_FACTORY1,V_FACTORY2) and substr( IM.PRODUCED_ITEM_ID,7,'3')='102' then                 
                 when  IM.SITEID in (V_FACTORY1,V_FACTORY2) and IM.PRODUCED_ITEM_ID IN (SELECT IIS.ITEM_ID FROM STG.IN_ITEM_SITE IIS WHERE TRIM(IIS.PROC_TYPE)='E' AND LTRIM(SUBSTR(IIS.ITEM_ID,1,9),'0') IN ('102') ) then
                    null
                 else
                    IM.MAXLOTSIZE
                 end
             end  MAXLOTSIZE,
             case when BH.MO_BOM_MARK='1' then
                NULL
             else
                 --case when  IM.SITEID='2061' and substr( IM.PRODUCED_ITEM_ID,7,'3')='401'  then
                 case when  IM.SITEID=V_FACTORY3 and substr( IM.PRODUCED_ITEM_ID,7,'3')='401'  then
                    NULL
                 --when  IM.SITEID='2061' and substr( IM.PRODUCED_ITEM_ID,7,'3')='407' then
                 --MODIFIED BY SHENG,将407变成402501-402999 
--                 when  IM.SITEID=V_FACTORY3 and substr( IM.PRODUCED_ITEM_ID,7,'3')='407' then
                 when  IM.SITEID=V_FACTORY3 and TO_NUMBER(substr( IM.PRODUCED_ITEM_ID,7,'6'))>=402501 AND TO_NUMBER(substr( IM.PRODUCED_ITEM_ID,7,'6'))<=402999 then
                    null
                 --when  IM.SITEID='2061' and substr( IM.PRODUCED_ITEM_ID,7,'3')='402' then
                 --MODIFIED BY SHENG,将402变成402000-402500 
--                 when  IM.SITEID=V_FACTORY3 and substr( IM.PRODUCED_ITEM_ID,7,'3')='402' then
                 when  IM.SITEID=V_FACTORY3 and TO_NUMBER(substr( IM.PRODUCED_ITEM_ID,7,'6'))>=402000 AND TO_NUMBER(substr( IM.PRODUCED_ITEM_ID,7,'6'))<=402500 then
                    decode(IM.MINLOTSIZE,0,null,IM.MINLOTSIZE)
                 --when  IM.SITEID in ('2011','2012') and substr( IM.PRODUCED_ITEM_ID,7,'3')='101' then 
                 when  IM.SITEID in (V_FACTORY1,V_FACTORY2) and substr( IM.PRODUCED_ITEM_ID,7,'3')='101' then
                    null
                 --when  IM.SITEID in ('2011','2012') and substr( IM.PRODUCED_ITEM_ID,7,'3')='102' then 
                 --MODIFIED BY SHENG,将102变成102E
--                 when  IM.SITEID in (V_FACTORY1,V_FACTORY2) and substr( IM.PRODUCED_ITEM_ID,7,'3')='102' then
                 when  IM.SITEID in (V_FACTORY1,V_FACTORY2) and IM.PRODUCED_ITEM_ID IN (SELECT IIS.ITEM_ID FROM STG.IN_ITEM_SITE IIS WHERE TRIM(IIS.PROC_TYPE)='E' AND LTRIM(SUBSTR(IIS.ITEM_ID,1,9),'0') IN ('102') ) then
                    decode(IM.MINLOTSIZE,0,null,IM.MINLOTSIZE)
                 else
                    decode(IM.MINLOTSIZE,0,null,IM.MINLOTSIZE)
                 end
             end MINLOTSIZE,
             DECODE(BH.MO_BOM_MARK,'1',0,1) USABLEBYNEWMFGORD,
             --V_YieldCalendar,
             --IM.SITEID PROPERTY,
             (select BQ.EFFECTIVE_START_DATE+nvl(BI.PRODUCT_TIME,0) from IN_BOM_QUALITY_ISSUES BQ,TEMP_BOM_ITEM BI
              where BQ.SITE_ID=BI.SITE_ID(+) and BQ.PARENT_ID=BI.PARENT_ID(+) and BQ.ITEM_ID=BI.ITEM_ID(+)
              and NVL(BQ.PARENT_ID,BQ.ITEM_ID)=IM.PRODUCED_ITEM_ID and IM.SITEID=BQ.SITE_ID and rownum=1) EFFECTIVESTARTDATE,
             V_ProName
        FROM IN_ITEMBOMROUTING IM, (SELECT MAX(X.MAX_PACKAGE) MAX_PACKAGE,X.PRODUCT_ID  FROM TEMP_ITEM_MAX_PACKAGE X
         WHERE X.PRODUCT_ID LIKE '000000401%'  GROUP BY X.PRODUCT_ID ) MP,IN_BOM_HEADER BH
       WHERE IM.PRODUCED_ITEM_ID = MP.PRODUCT_ID(+) and IM.BOM_ID=BH.BOM_ID;
   COMMIT;
   
   --f. ADD BY Landor ON 20180522 start    处理MST_ITEMBOMROUTING 及MST_ITEMBOMROUTING _P:针对所有按单生产的成品及半成品BOM都要新增ORDER_BOM(没带批量数据)
    --及NORMAL_BOM(--照原来数据该带批量数据就带批量数据)。
    INSERT INTO ABPPMGR.MST_ITEMBOMROUTING (SCENARIO_ID,
                                               ENTERPRISE,
                                               SITEID,
                                               ENGINE_ID,
                                               BOMID,
                                               ROUTINGID,
                                               ITEM,
                                               PRIORITY,
                                               MAXLOTSIZE,
                                               MINLOTSIZE,
                                               USABLEBYNEWMFGORD,
                                               EFFSTARTDATE,
                                               SYS_CREATED_BY)
                                               
     SELECT DISTINCT  AM.SCENARIO_ID
                    ,AM.ENTERPRISE
                    ,AM.SITEID
                    ,AM.ENGINE_ID
                    ,Am.Bomid
                    ,Am.RoutingId||'_'||'ORDER_BOM' RoutingId
                    ,Am.Item
                    ,Am.PRIORITY
                    ,null
                    ,null
                    ,USABLEBYNEWMFGORD
                    ,EFFSTARTDATE
                    ,'AnDan'
      From In_Order_Planning Ip
         Join Abppmgr.Mst_Itembomrouting Am
           On Am.Item=Ip.Fg_Item_Id--成品层
      Where Am.Minlotsize Is Not Null
        And Am.Maxlotsize Is Not Null --两个值为空代表 没带批量数据
    union
    SELECT DISTINCT  AM.SCENARIO_ID
                    ,AM.ENTERPRISE
                    ,AM.SITEID
                    ,AM.ENGINE_ID
                    ,Am.Bomid
                    ,Am.RoutingId||'_'||'ORDER_BOM' RoutingId
                    ,Am.Item
                    ,Am.PRIORITY
                    ,null
                    ,null
                    ,USABLEBYNEWMFGORD
                    ,EFFSTARTDATE
                    ,'AnDan'
      From In_Order_Planning Ip
         Join Abppmgr.Mst_Itembomrouting Am
           On Am.Item=Ip.Semi_Item_Id--半成品层
      Where Am.Minlotsize Is Not Null
        And Am.Maxlotsize Is Not Null;   
   --end add f
   
   
     --g.ADD BY Landor ON 20180522 add g    把IN_BOM_CHANGE中换BOM的数据进MST_ITEMBOMROUTING及MST_ITEMBOMROUTING_P:
     --先在MST_ITEMBOMROUTING拷贝原来BOM数据然后修改BOM及ROUTING等数据 
    INSERT INTO ABPPMGR.MST_ITEMBOMROUTING (SCENARIO_ID,
                                           ENTERPRISE,
                                           SITEID,
                                           ENGINE_ID,
                                           BOMID,
                                           ROUTINGID,
                                           ITEM,
                                           PRIORITY,
                                           MAXLOTSIZE,
                                           MINLOTSIZE,
                                           USABLEBYNEWMFGORD,
                                           EFFSTARTDATE,
                                           SYS_CREATED_BY)
    SELECT DISTINCT V_Scenario_ID,
         V_Enterprise,
         V_SiteID,
         V_Engine,
         A.BOMID || '_' || IBC.SO_ID ||SUBSTR(IBC.SO_LINE_ID,1,INSTR(IBC.SO_LINE_ID,'_')-1) BOMID,
         A.ROUTINGID || '_'||  IBC.SO_ID ||SUBSTR(IBC.SO_LINE_ID,1,INSTR(IBC.SO_LINE_ID,'_')-1) ROUTINGID,
         A.ITEM,
         A.PRIORITY,
         null  MAXLOTSIZE,--为空(是按单生产订单则为空)
         null MINLOTSIZE, --为空(是按单生产订单则为空)
         A.USABLEBYNEWMFGORD,
         A.EFFSTARTDATE,
         'AnDan'
     FROM IN_BOM_CHANGE IBC
     JOIN ABPPMGR.MST_ITEMBOMROUTING A  ON A.ITEM=IBC.PARENT_ITEM_ID
     AND A.SYS_CREATED_BY<>'AnDan';
    COMMIT;  
    --end add g
     

   DELETE FROM ABPPMGR.MST_ITEMBOMROUTING M 
   WHERE EXISTS (SELECT NULL FROM IN_ITEM_SITE IM
   WHERE  M.ITEM=IM.ITEM_ID AND SUBSTR(M.ROUTINGID,-4)=IM.SITE_ID AND TRIM(IM.PROC_TYPE)||TRIM(IM.SP_PROC_TYPE)='E70');
   COMMIT;

   INSERT INTO ABPPMGR.MST_ITEMBOMROUTING_P (SCENARIO_ID,
                                             ENTERPRISE,
                                             SITEID,
                                             ENGINE_ID,
                                             BOMID,
                                             ROUTINGID,
                                             ITEM,
                                             ATTRIBUTE,
                                             ATTRIBUTECLASS,
                                             RELATIONSHIP,
                                             VALUE,
                                             SYS_CREATED_BY)
      SELECT IM.SCENARIO_ID,
             IM.ENTERPRISE,
             IM.SITEID,
             IM.ENGINE_ID,
             IM.BOMID,
             IM.ROUTINGID,
             IM.ITEM,
             'FACTORY' ATTRIBUTE,
             --modify by zhangguipeng on 20171021 工艺路线取工厂值没有考虑尾缀为CA、CZ等部分,不能直接取倒数4位
              IM.ITEM||'_'||substr(im.ROUTINGID,instr(im.routingid,'_')+1,4) ATTRIBUTECLASS,
              --IM.ITEM||'_'|| substr(IM.ROUTINGID,-4) ATTRIBUTECLASS,
             'EQ' RELATIONSHIP,
             substr(im.ROUTINGID,instr(im.routingid,'_')+1,4) VALUE
             --substr(IM.ROUTINGID,-4) VALUE
             , V_ProName
        FROM ABPPMGR.MST_ITEMBOMROUTING  IM
     --ALTER BY HUANGYANGXIONG ON 20150115   增加硬绑逻辑
     WHERE NOT EXISTS (SELECT NULL FROM IN_SEMI_MO_LOCKED ML
     WHERE ML.MO_ID =substr( IM.ROUTINGID,1,12))
     /*OR NOT EXISTS (SELECT NULL FROM IN_MO MO,in_sales_order so
     WHERE IM.ROUTINGID=MO.ROUTING_ID
     and MO.SO_ID=so.so_id)*/
     ;--硬棒工单的属性不在这边处理,需要单独处理

   COMMIT;
   
   --处理工单硬棒的ITEMBOMROUTING属性
   INSERT INTO ABPPMGR.MST_ITEMBOMROUTING_P (SCENARIO_ID,
                                             ENTERPRISE,
                                             SITEID,
                                             ENGINE_ID,
                                             BOMID,
                                             ROUTINGID,
                                             ITEM,
                                             ATTRIBUTE,
                                             ATTRIBUTECLASS,
                                             RELATIONSHIP,
                                             VALUE,
                                             SYS_CREATED_BY)
      SELECT IM.SCENARIO_ID,
             IM.ENTERPRISE,
             IM.SITEID,
             IM.ENGINE_ID,
             IM.BOMID,
             IM.ROUTINGID,
             IM.ITEM,
             'FACTORY' ATTRIBUTE,
             SEMIMO.MO_ID||'_'||SEMIMO.PEGGED_SO_ID||SEMIMO.PEGGED_SO_LINE_ID ATTRIBUTECLASS,
             'EQ' RELATIONSHIP,
             --SEMIMO.PEGGED_SO_ID||SEMIMO.PEGGED_SO_LINE_ID VALUE
             SUBSTR(ROUTINGID,LENGTH(ROUTINGID)-3,4) VALUE   --ALTER BY Landor on 20180428解决CRTD工单重复规划问题中,PAUL要求当ATTRIBUTE=FACTORY时,VALUE需修改为厂别
             , V_ProName
        FROM ABPPMGR.MST_ITEMBOMROUTING  IM 
        ,(SELECT PEGGED_SO_ID, PEGGED_SO_LINE_ID,MO_ID,ROW_NUMBER()OVER(PARTITION BY MO_ID ORDER BY QUANTITY DESC) RN
         FROM IN_SEMI_MO_LOCKED ) SEMIMO
        WHERE SEMIMO.RN=1 AND SEMIMO.MO_ID=SUBSTR(IM.ROUTINGID,1,LENGTH(SEMIMO.MO_ID))
          AND EXISTS (SELECT NULL FROM IN_SEMI_MO_LOCKED ML
         WHERE ML.MO_ID =substr( IM.ROUTINGID,1,12))--硬棒工单的属性单独处理
         AND IM.SYS_CREATED_BY<>'AnDan'; --add by Landor on 20180522       
    COMMIT;
    
      --add by Landor ON 20180522 按单方案更新补充处理MST_ITEMBOMROUTING _P.ATTRIBUTECLASS
   UPDATE ABPPMGR.MST_ITEMBOMROUTING_P AP SET AP.ATTRIBUTECLASS=AP.ATTRIBUTECLASS||':'||AP.VALUE;
   COMMIT;  
   
    --start f ADD BY Landor ON 20180522按单新增数据
   --f同时新增到ABPPMGR.MST_ITEMBOMROUTING_P表
   --f.    处理MST_ITEMBOMROUTING _P:针对所有按单生产的成品及半成品BOM都要新增ORDER_BOM(没带批量数据)及NORMAL_BOM(照原来数据该带批量数据就带批量数据)
    INSERT INTO ABPPMGR.MST_ITEMBOMROUTING_P    (SCENARIO_ID,
                                                 ENTERPRISE,
                                                 SITEID,
                                                 ENGINE_ID,
                                                 BOMID,
                                                 ROUTINGID,
                                                 ITEM,
                                                 ATTRIBUTE,
                                                 ATTRIBUTECLASS,
                                                 RELATIONSHIP,
                                                 VALUE,
                                                 SYS_CREATED_BY)
    SELECT DISTINCT   AM.SCENARIO_ID
                     ,AM.ENTERPRISE
                     ,AM.SITEID
                     ,AM.ENGINE_ID
                     ,Am.Bomid
                     ,CASE WHEN AM.ROUTINGID LIKE '%ORDER_BOM%' THEN AP.ROUTINGID||'_ORDER_BOM'
                      ELSE AP.ROUTINGID
                      END AS ROUTINGID
                     ,Am.Item
                     ,'ORDER_PLANNING' ATTRIBUTE
                     ,CASE WHEN AM.ROUTINGID LIKE '%ORDER_BOM%' THEN SUBSTR(AP.ATTRIBUTECLASS,1,INSTR(AP.ATTRIBUTECLASS,':')-1)||'_ORDER_BOM:200'
                      ELSE SUBSTR(AP.ATTRIBUTECLASS,1,INSTR(AP.ATTRIBUTECLASS,':')-1)||':100'
                      END AS ATTRIBUTECLASS
                     ,'EQ'
                     ,CASE WHEN AM.ROUTINGID LIKE '%ORDER_BOM%' THEN '200'
                      ELSE '100'
                      END AS VALUE
                     ,'AnDan'
      From  Abppmgr.Mst_Itembomrouting Am
         join Abppmgr.Mst_Itembomrouting_P Ap
          on Am.item=Ap.item
         and Am.bomid=ap.bomid;
     COMMIT;
     
     --方案外:沒帶批量就只要新增MST_ITEMBOMROUTING_P 多數據等於200 add by landor on 20180522 20180601
     INSERT INTO ABPPMGR.MST_ITEMBOMROUTING_P    (SCENARIO_ID,
                                                 ENTERPRISE,
                                                 SITEID,
                                                 ENGINE_ID,
                                                 BOMID,
                                                 ROUTINGID,
                                                 ITEM,
                                                 ATTRIBUTE,
                                                 ATTRIBUTECLASS,
                                                 RELATIONSHIP,
                                                 VALUE,
                                                 SYS_CREATED_BY)
    SELECT DISTINCT   AM.SCENARIO_ID
                     ,AM.ENTERPRISE
                     ,AM.SITEID
                     ,AM.ENGINE_ID
                     ,Am.Bomid
                     ,CASE WHEN AM.ROUTINGID LIKE '%ORDER_BOM%' THEN AP.ROUTINGID||'_ORDER_BOM'
                      ELSE AP.ROUTINGID
                      END AS ROUTINGID
                     ,Am.Item
                     ,'ORDER_PLANNING' ATTRIBUTE
                     , SUBSTR(AP.ATTRIBUTECLASS,1,INSTR(AP.ATTRIBUTECLASS,':')-1)||':200' AS ATTRIBUTECLASS
                     ,'EQ'
                     , '200' AS VALUE
                     ,'AnDan'
      From  Abppmgr.Mst_Itembomrouting Am
         join Abppmgr.Mst_Itembomrouting_P Ap on Am.item=Ap.item and Am.bomid=ap.bomid 
         WHERE  MAXLOTSIZE IS NULL AND MINLOTSIZE IS NULL  ;--代表无批量数据,只需增加200
      COMMIT;
      --end add f
         
  
   ----adby Landor on 20180522
   --g再针对MST_ITEMBOMROUTING_P拷贝原来BOM数据然后修改BOM及ROUTING等数据
    INSERT INTO ABPPMGR.MST_ITEMBOMROUTING_P    (SCENARIO_ID,
                                                 ENTERPRISE,
                                                 SITEID,
                                                 ENGINE_ID,
                                                 BOMID,
                                                 ROUTINGID,
                                                 ITEM,
                                                 ATTRIBUTE,
                                                 ATTRIBUTECLASS,
                                                 RELATIONSHIP,
                                                 VALUE,
                                                 SYS_CREATED_BY)
        SELECT DISTINCT A.SCENARIO_ID
                     ,A.ENTERPRISE
                     ,A.SITEID
                     ,A.ENGINE_ID,
         A.BOMID  BOMID,
         A.ROUTINGID  ROUTINGID,
         A.ITEM,
         'ORDER_PLANNING'ATTRIBUTE, 
         IBC.SO_ID||'_'||IBC.SO_LINE_ID||':'||IBC.SO_ID||SUBSTR(SO_LINE_ID,1,INSTR(SO_LINE_ID,'_')-1) ATTRIBUTECLASS,
         'EQ'RELATIONSHIP,
         IBC.SO_ID||SUBSTR(IBC.SO_LINE_ID,1,INSTR(IBC.SO_LINE_ID,'_')-1) VALUE,
         'AnDan'
     FROM IN_BOM_CHANGE IBC 
     JOIN ABPPMGR.MST_ITEMBOMROUTING A  ON A.ITEM=IBC.PARENT_ITEM_ID
      AND SUBSTR(A.ROUTINGID,INSTR(A.ROUTINGID,'_',1,2)+1,LENGTH(A.ROUTINGID))=IBC.SO_ID||SUBSTR(IBC.SO_LINE_ID,1,INSTR(IBC.SO_LINE_ID,'_')-1)
     AND A.SYS_CREATED_BY='AnDan';   
     COMMIT; 
       
     
     
    /*
--处理工单硬棒的ITEMBOMROUTING属性
   INSERT INTO ABPPMGR.MST_ITEMBOMROUTING_P (SCENARIO_ID,
                                             ENTERPRISE,
                                             SITEID,
                                             ENGINE_ID,
                                             BOMID,
                                             ROUTINGID,
                                             ITEM,
                                             ATTRIBUTE,
                                             ATTRIBUTECLASS,
                                             RELATIONSHIP,
                                             VALUE,
                                             SYS_CREATED_BY)
      SELECT IM.SCENARIO_ID,
             IM.ENTERPRISE,
             IM.SITEID,
             IM.ENGINE_ID,
             IM.BOMID,
             IM.ROUTINGID,
             IM.ITEM,
             MO.PEGGED_SO_ID||MO.PEGGED_SO_LINE_ID||'_PEG' ATTRIBUTE,
             MO.PEGGED_SO_ID||MO.PEGGED_SO_LINE_ID ATTRIBUTECLASS,
             'EQ' RELATIONSHIP,
             MO.PEGGED_SO_ID||MO.PEGGED_SO_LINE_ID VALUE
             , V_ProName
        FROM ABPPMGR.MST_ITEMBOMROUTING  IM 
        ,(SELECT SO_ID PEGGED_SO_ID, SO_LINE_ID PEGGED_SO_LINE_ID,MO_ID
         FROM IN_MO WHERE SO_ID IS NOT NULL ) MO
        WHERE MO.MO_ID=SUBSTR(IM.ROUTINGID,1,LENGTH(MO.MO_ID))
          AND EXISTS (SELECT NULL FROM IN_MO MO,IN_SALES_ORDER SO
         WHERE IM.ROUTINGID=MO.ROUTING_ID AND MO.SO_ID=SO.SO_ID);--硬棒工单的属性单独处理
*/   

   V_Step := V_Step + 1;                                                 -- 9;
   V_Action := PKG_CONSTANT.CONS_ACTION_MERGE;
   V_MainTable := 'IN_ITEMBOMROUTING';
   --记录IN_ITEMBOMROUTING导入日志
   PKG_UTIL.CREATELOG (V_PKGName,                      --Put package name here
                       V_ProName,                    --Put procedure name here
                       V_Step,                      --Step seqence No. if have
                       V_Action,                      --Action 'I','D','U','T'
                       PKG_CONSTANT.CONS_MSG_INFO, --Message tppe, 'SUCCESS' or 'EXCEPTION'
                       'Success',               --Any generic information here
                       V_MainTable);              --Major table name processed

   /*
      MERGE INTO ABPPMGR.MST_RESOURCEGRPMASTER M
           USING DUAL IM
              ON (M.RESOURCEGROUPNAME = V_ResourceGroup)
      WHEN NOT MATCHED
      THEN
         INSERT     (SCENARIO_ID,
                     ENTERPRISE,
                     ENGINE_ID,
                     RESOURCEGROUPNAME,
                     SYS_AUTH_ID)
             VALUES (V_Scenario_ID,
                     V_Enterprise,
                     V_Engine,
                     V_ResourceGroup,
                     V_AuthID);
   */
   UPDATE ABPPMGR.MST_WORKCENTERDETAIL M
      SET M.SYS_ENT_STATE = 'INACTIVE';

   UPDATE ABPPMGR.MST_RESOURCEMASTER M
      SET M.SYS_ENT_STATE = 'INACTIVE';

   UPDATE ABPPMGR.MST_WORKCENTERMASTER M
      SET M.SYS_ENT_STATE = 'INACTIVE';

   COMMIT;

   V_Step := V_Step + 1;                                                -- 10;
   V_Action := PKG_CONSTANT.CONS_ACTION_MERGE;
   V_MainTable := 'MST_WORKCENTERMASTER,MST_RESOURCEMASTER';
   --记录IN_RES_CAPABILITY导入日志
   PKG_UTIL.CREATELOG (V_PKGName,                      --Put package name here
                       V_ProName,                    --Put procedure name here
                       V_Step,                      --Step seqence No. if have
                       V_Action,                      --Action 'I','D','U','T'
                       PKG_CONSTANT.CONS_MSG_INFO, --Message tppe, 'SUCCESS' or 'EXCEPTION'
                       'Start',                 --Any generic information here
                       V_MainTable);              --Major table name processed

   FOR C_Workcenter
      IN (SELECT DISTINCT
                 'RES_'
                 || DECODE (RC.PRODUCT_FAMILY,
                            NULL, 'DUMMY',
                            RC.PRODUCT_FAMILY || '_' || ST.UDF_ITEM_CATEGORY)
                    WORKCENTER,
                 NVL (RC.CAPA_QTY, 0) QTY,
                 ST.UDF_ITEM_CATEGORY CATEGORY
            FROM IN_ITEM IT, (select ITEM_ID,MAX(UDF_ITEM_CATEGORY) UDF_ITEM_CATEGORY from IN_ITEM_SITE GROUP BY ITEM_ID) ST,
             IN_RES_CAPABILITY RC
           WHERE     IT.ITEM_ID = ST.ITEM_ID
                 AND RC.PRODUCT_FAMILY(+) = IT.FAMILY_ID)
   LOOP
      MERGE INTO ABPPMGR.MST_WORKCENTERMASTER M
           USING DUAL IM
              ON (M.WORKCENTERNAME = C_Workcenter.WORKCENTER)
      WHEN MATCHED
      THEN
         UPDATE SET
            M.SYS_ENT_STATE = 'ACTIVE',
            M.SYS_LAST_MODIFIED_BY = V_ProName,
            M.SYS_LAST_MODIFIED_DATE = V_CurrentTime
      WHEN NOT MATCHED
      THEN
         INSERT     (SCENARIO_ID,
                     ENTERPRISE,
                     SITEID,
                     ENGINE_ID,
                     LOCATIONID,
                     WORKCENTERNAME,
                     SYS_CREATED_BY)
             VALUES (V_Scenario_ID,
                     V_Enterprise,
                     V_SiteID,
                     V_Engine,
                     V_LocationID,
                     C_Workcenter.WORKCENTER,
                     V_ProName);

      MERGE INTO ABPPMGR.MST_RESOURCEMASTER M
           USING DUAL IM
              ON (M.RESOURCENAME = C_Workcenter.WORKCENTER)
      WHEN MATCHED
      THEN
         UPDATE SET
            M.SYS_ENT_STATE = 'ACTIVE',
            M.SYS_LAST_MODIFIED_BY = V_ProName,
            M.SYS_LAST_MODIFIED_DATE = V_CurrentTime
      WHEN NOT MATCHED
      THEN
         INSERT     (SCENARIO_ID,
                     ENTERPRISE,
                     SITEID,
                     ENGINE_ID,
                     RESOURCENAME,
                     SYS_CREATED_BY)
             VALUES (V_Scenario_ID,
                     V_Enterprise,
                     V_SiteID,
                     V_Engine,
                     C_Workcenter.WORKCENTER,
                     V_ProName);

      MERGE INTO ABPPMGR.MST_WORKCENTERDETAIL M
           USING DUAL IM
              ON (    M.SCENARIO_ID = V_Scenario_ID
                  AND M.ENTERPRISE = V_Enterprise
                  AND M.SITEID = V_SiteID
                  AND M.RESOURCENAME = C_Workcenter.WORKCENTER
                  AND M.WORKCENTERNAME = C_Workcenter.WORKCENTER)
      WHEN MATCHED
      THEN
         UPDATE SET
            M.SYS_ENT_STATE = 'ACTIVE',
            M.MAXPRODRATE =
               DECODE (C_Workcenter.WORKCENTER,
                       'RES_DUMMY', NULL,
                       C_Workcenter.QTY),
            M.AGGWORKCENTERNAME=null,
            M.AGGRESOURCENAME=null,
            M.SYS_LAST_MODIFIED_BY = V_ProName,
            M.SYS_LAST_MODIFIED_DATE = V_CurrentTime
      WHEN NOT MATCHED
      THEN
         INSERT     (SCENARIO_ID,
                     ENTERPRISE,
                     SITEID,
                     ENGINE_ID,
                     RESOURCENAME,
                     WORKCENTERNAME,
                     --AGGWORKCENTERNAME,
                     --AGGRESOURCENAME,
                     AGGSITEID,
                     LOCATIONID,
                     MAXPRODRATE,
                     PRODRATEUOM,
                     PRODRATEPER,
                     SYS_CREATED_BY)
             VALUES (
                       V_Scenario_ID,
                       V_Enterprise,
                       V_SiteID,
                       V_Engine,
                       C_Workcenter.WORKCENTER,
                       C_Workcenter.WORKCENTER,
                       --C_Workcenter.WORKCENTER,
                       --C_Workcenter.WORKCENTER,
                       V_SiteID,
                       V_LocationID,
                       DECODE (C_Workcenter.WORKCENTER,
                               'RES_DUMMY', NULL,
                               C_Workcenter.QTY) ,
                       DECODE (C_Workcenter.WORKCENTER,
                               'RES_DUMMY', NULL,
                               --'Material_Quantity'),
                               'PCS'),
                       DECODE (C_Workcenter.WORKCENTER,
                               'RES_DUMMY', NULL,
                               'DAYS'),
                       V_ProName);
   END LOOP;

   DELETE FROM ABPPMGR.MST_OPRESOURCE;

   COMMIT;

   V_Step := V_Step + 1;                                                -- 10;
   V_Action := PKG_CONSTANT.CONS_ACTION_INSERT;
   V_MainTable := 'MST_OPRESOURCE';
   PKG_UTIL.CREATELOG (V_PKGName,                      --Put package name here
                       V_ProName,                    --Put procedure name here
                       V_Step,                      --Step seqence No. if have
                       V_Action,                      --Action 'I','D','U','T'
                       PKG_CONSTANT.CONS_MSG_INFO, --Message tppe, 'SUCCESS' or 'EXCEPTION'
                       'Start importing to table MST_OPRESOURCE.', --Any generic information here
                       V_MainTable);              --Major table name processed

   INSERT INTO ABPPMGR.MST_OPRESOURCE (SCENARIO_ID,
                                       ENTERPRISE,
                                       SITEID,
                                       ENGINE_ID,
                                       ROUTINGID,
                                       OPERATIONSEQ,
                                       --RESOURCEGROUPNAME,
                                       --RESOURCECONFIGURATIONID,
                                       RESOURCENAME,
                                       WORKCENTERNAME,
                                       SYS_CREATED_BY)
      --alter by huangyangxiong on 20161024   alter for the alternate group                                 
      SELECT V_Scenario_ID,
             V_Enterprise,
             V_SiteID,
             V_Engine,
             ROUTING_ID,
             OPERATIONSEQ,
             --V_ResourceGroup,
             --ITEM_ID,
             WORKCENTER,
             WORKCENTER1,
             V_ProName
 FROM 
 (SELECT V_Scenario_ID,
             V_Enterprise,
             V_SiteID,
             V_Engine,
             ROUTING_ID,
             OPERATIONSEQ,
             --V_ResourceGroup,
             --ITEM_ID,
             WORKCENTER,
             WORKCENTER AS WORKCENTER1,
             V_ProName,
             ROW_NUMBER ()
               OVER (PARTITION BY ROUTING_ID
                     ORDER BY WORKCENTER DESC)
             RN
        FROM (SELECT ROUTING_ID,
                     OPERATIONSEQ,
                     CASE
                        WHEN UDF_ITEM_CATEGORY = 'ASS'
                             AND PRODUCT_FAMILY IS NOT NULL
                        THEN
                           CASE
                              WHEN RC = 1 OR RNASC < RC
                              THEN
                                    'RES_'
                                 || PRODUCT_FAMILY
                                 || '_'
                                 || UDF_ITEM_CATEGORY
                              ELSE
                                 'RES_DUMMY'
                           END
                        WHEN UDF_ITEM_CATEGORY = 'SEMI'
                             AND PRODUCT_FAMILY IS NOT NULL
                        THEN
                           CASE
                              WHEN RC = 1 OR RNDESC < RC
                              THEN
                                    'RES_'
                                 || PRODUCT_FAMILY
                                 || '_'
                                 || UDF_ITEM_CATEGORY
                              ELSE
                                 'RES_DUMMY'
                           END
                        ELSE
                           'RES_DUMMY'
                     END
                        WORKCENTER
                FROM (SELECT RO.ROUTING_ID ROUTING_ID,
               RO.OPERATIONSEQ OPERATIONSEQ,
               'ASS' UDF_ITEM_CATEGORY,
               RC.PRODUCT_FAMILY,
               RO.SITEID,
               ROW_NUMBER ()
               OVER (PARTITION BY RO.ROUTING_ID
                     ORDER BY RO.OPERATIONSEQ DESC)
                  RNDESC,
               ROW_NUMBER ()
               OVER (PARTITION BY RO.ROUTING_ID ORDER BY RO.OPERATIONSEQ ASC)
                  RNASC,
               COUNT (RO.OPERATIONSEQ) OVER (PARTITION BY RO.ROUTING_ID) RC
          FROM IN_ROUTINGOPERATION RO, IN_RES_CAPABILITY RC
         WHERE  RO.ROUTING_ID NOT LIKE 'DUMMYROUTING%' AND EXISTS
                  (SELECT NULL
                     FROM IN_ITEM_SITE ST, IN_ITEMBOMROUTING BR, IN_ITEM IT
                    WHERE     ST.UDF_ITEM_CATEGORY = 'ASS'
                          AND BR.PRODUCED_ITEM_ID = ST.ITEM_ID
                          AND BR.SITEID = ST.SITE_ID
                          AND BR.ROUTING_ID = RO.ROUTING_ID
                          AND RO.SITEID = ST.SITE_ID
                          AND ST.ITEM_ID = IT.ITEM_ID
                          AND RC.PRODUCT_FAMILY = IT.FAMILY_ID)
        UNION ALL
        SELECT RO.ROUTING_ID ROUTING_ID,
               RO.OPERATIONSEQ OPERATIONSEQ,
               'SEMI' UDF_ITEM_CATEGORY,
               RC.PRODUCT_FAMILY,
               RO.SITEID,
               ROW_NUMBER ()
               OVER (PARTITION BY RO.ROUTING_ID
                     ORDER BY RO.OPERATIONSEQ DESC)
                  RNDESC,
               ROW_NUMBER ()
               OVER (PARTITION BY RO.ROUTING_ID ORDER BY RO.OPERATIONSEQ ASC)
                  RNASC,
               COUNT (RO.OPERATIONSEQ) OVER (PARTITION BY RO.ROUTING_ID) RC
          FROM IN_ROUTINGOPERATION RO, IN_RES_CAPABILITY RC
         WHERE RO.ROUTING_ID not like 'DUMMYROUTING%' and  EXISTS
                  (SELECT NULL
                     FROM IN_ITEM_SITE ST, IN_ITEMBOMROUTING BR, IN_ITEM IT
                    WHERE     ST.UDF_ITEM_CATEGORY = 'SEMI'
                          AND BR.PRODUCED_ITEM_ID = ST.ITEM_ID
                          AND BR.SITEID = ST.SITE_ID
                          AND BR.ROUTING_ID = RO.ROUTING_ID
                          AND RO.SITEID = ST.SITE_ID
                          AND ST.ITEM_ID = IT.ITEM_ID
                          AND RC.PRODUCT_FAMILY = IT.FAMILY_ID))
                          -- WHERE SITEID NOT IN ('2062', '2061')
                           WHERE SITEID NOT IN (V_FACTORY4, V_FACTORY3)
                           --WHERE SITEID NOT IN ('2021')
                      --OR (SITEID IN ('2062', '2061') AND RNASC = 1
                      OR (SITEID IN (V_FACTORY4, V_FACTORY3) AND RNASC = 1
                      --OR (SITEID IN ('2021') AND RNASC = 1
                          OR RNASC = RC)))
               WHERE RN = 1;

   V_Step := V_Step + 1;                                                -- 10;
   V_Action := PKG_CONSTANT.CONS_ACTION_INSERT;
   V_MainTable := 'MST_OPRESOURCE';
   PKG_UTIL.CREATELOG (V_PKGName,                      --Put package name here
                       V_ProName,                    --Put procedure name here
                       V_Step,                      --Step seqence No. if have
                       V_Action,                      --Action 'I','D','U','T'
                       PKG_CONSTANT.CONS_MSG_INFO, --Message tppe, 'SUCCESS' or 'EXCEPTION'
                       'Success: ' || SQL%ROWCOUNT || ' rows imported.', --Any generic information here
                       V_MainTable);              --Major table name processed
   COMMIT;
   V_Step := V_Step + 1;                                                -- 10;
   V_Action := PKG_CONSTANT.CONS_ACTION_INSERT;
   V_MainTable := 'MST_RESOURCECALENDAR';
   PKG_UTIL.CREATELOG (V_PKGName,                      --Put package name here
                       V_ProName,                    --Put procedure name here
                       V_Step,                      --Step seqence No. if have
                       V_Action,                      --Action 'I','D','U','T'
                       PKG_CONSTANT.CONS_MSG_INFO, --Message tppe, 'SUCCESS' or 'EXCEPTION'
                       'Start',                 --Any generic information here
                       V_MainTable);              --Major table name processed

   --DELETE FROM ABPPMGR.MST_RESOURCECALENDAR ;
   MERGE INTO ABPPMGR.MST_RESOURCECALENDAR M
        USING ABPPMGR.MST_WORKCENTERDETAIL IM
           ON (    M.SCENARIO_ID = IM.SCENARIO_ID
               AND M.ENTERPRISE = IM.ENTERPRISE
               AND M.RESOURCENAME = IM.RESOURCENAME
               AND M.SITEID = IM.SITEID
               AND M.WORKCENTERNAME = IM.WORKCENTERNAME)
   WHEN MATCHED
   THEN
      UPDATE SET M.CALENDARNAME = V_CALENDARNAME,
                 M.ENGINE_ID = V_Engine,
                 M.SYS_ENT_STATE = IM.SYS_ENT_STATE,
                 M.SYS_LAST_MODIFIED_BY = V_ProName,
                 M.SYS_LAST_MODIFIED_DATE = V_CurrentTime
   WHEN NOT MATCHED
   THEN
      INSERT     (SCENARIO_ID,
                  ENTERPRISE,
                  SITEID,
                  ENGINE_ID,
                  RESOURCENAME,
                  WORKCENTERNAME,
                  CALENDARNAME,
                  CALENDARTYPE,
                  SYS_CREATED_BY)
          VALUES (V_Scenario_ID,
                  V_Enterprise,
                  V_SiteID,
                  V_Engine,
                  IM.RESOURCENAME,
                  IM.WORKCENTERNAME,
                  V_CalendarName,
                  V_CalendarType,
                  V_ProName);

   V_Step := V_Step + 1;                                                -- 10;
   V_Action := PKG_CONSTANT.CONS_ACTION_INSERT;
   V_MainTable := 'MST_RESOURCECALENDAR';
   PKG_UTIL.CREATELOG (V_PKGName,                      --Put package name here
                       V_ProName,                    --Put procedure name here
                       V_Step,                      --Step seqence No. if have
                       V_Action,                      --Action 'I','D','U','T'
                       PKG_CONSTANT.CONS_MSG_INFO, --Message tppe, 'SUCCESS' or 'EXCEPTION'
                       'Success: ' || SQL%ROWCOUNT || ' rows imported.', --Any generic information here
                       V_MainTable);              --Major table name processed
   V_Step := V_Step + 1;                                                -- 10;
   V_Action := PKG_CONSTANT.CONS_ACTION_INSERT;
   V_MainTable := 'MST_CALBASEDATTR,MST_CALENDARDETAIL';
   PKG_UTIL.CREATELOG (V_PKGName,                      --Put package name here
                       V_ProName,                    --Put procedure name here
                       V_Step,                      --Step seqence No. if have
                       V_Action,                      --Action 'I','D','U','T'
                       PKG_CONSTANT.CONS_MSG_INFO, --Message tppe, 'SUCCESS' or 'EXCEPTION'
                       'Sart: insert from IN_RES_CALENDAR.', --Any generic information here
                       V_MainTable);              --Major table name processed

   --2014-7-7在FP_INIT中加删除动作,故此注释
   DELETE FROM ABPPMGR.MST_CALBASEDATTR M where M.SYS_CREATED_BY= V_ProName;
   DELETE FROM ABPPMGR.MST_CALENDARDETAIL M where M.SYS_CREATED_BY= V_ProName; 

   INSERT INTO ABPPMGR.MST_CALENDARDETAIL (SCENARIO_ID,
                                           ENTERPRISE,
                                           ENGINE_ID,
                                           CALENDARNAME,
                                           CALENDARTYPE,
                                           EFFSTARTDATE,
                                           EFFENDDATE,
                                           PATTERNNAME,
                                           PATTERNSEQ,
                                           SHIFTNUMBER,
                                           SYS_CREATED_BY)
      SELECT V_Scenario_ID,
             V_Enterprise,
             V_Engine,
             V_CalendarName,
             V_CalendarType,
             X.EFFSTARTDATE,
             X.EFFENDDATE,
             V_PatternName,
             X.PATTERNSEQ,
             1 SHIFTNUMBER,
             V_ProName
        FROM (  SELECT ROWNUM PATTERNSEQ,
                       RC.END_DATETIME + 1 EFFSTARTDATE,
                       LEAD (RC.START_DATETIME, 1)
                          OVER (ORDER BY RC.START_DATETIME)
                       - 1 / (24 * 60 * 60)
                          EFFENDDATE
                  FROM IN_RES_CALENDAR RC
                 --WHERE RC.SITE_ID = '2012'
                 WHERE RC.SITE_ID = V_FACTORY2
              ORDER BY START_DATETIME) X
       WHERE X.EFFENDDATE IS NOT NULL;

   INSERT INTO ABPPMGR.MST_CALBASEDATTR (SCENARIO_ID,
                                         ENTERPRISE,
                                         ENGINE_ID,
                                         ATTRIBUTE,
                                         CALENDARNAME,
                                         PATTERNSEQ,
                                         SHIFTNUMBER,
                                         VALUE,
                                         SYS_CREATED_BY)
      SELECT V_Scenario_ID,
             V_Enterprise,
             V_Engine,
             'UNAVAILABLE_RESOURCES',
             V_CalendarName,
             X.PATTERNSEQ,
             1 SHIFTNUMBER,
             '1' VALUE,
             V_ProName SYS_CREATED_BY
        FROM (  SELECT ROWNUM PATTERNSEQ,
                       RC.END_DATETIME + 1 EFFSTARTDATE,
                       LEAD (RC.START_DATETIME, 1)
                          OVER (ORDER BY RC.START_DATETIME)
                       - 1 / (24 * 60 * 60)
                          EFFENDDATE
                  FROM IN_RES_CALENDAR RC
                 --WHERE RC.SITE_ID = '2012'
                 WHERE RC.SITE_ID = V_FACTORY2
              ORDER BY START_DATETIME) X
       WHERE X.EFFENDDATE IS NOT NULL;

   V_Step := V_Step + 1;                                                -- 10;
   V_Action := PKG_CONSTANT.CONS_ACTION_INSERT;
   V_MainTable := 'MST_CALBASEDATTR,MST_CALENDARDETAIL';
   PKG_UTIL.CREATELOG (V_PKGName,                      --Put package name here
                       V_ProName,                    --Put procedure name here
                       V_Step,                      --Step seqence No. if have
                       V_Action,                      --Action 'I','D','U','T'
                       PKG_CONSTANT.CONS_MSG_INFO, --Message tppe, 'SUCCESS' or 'EXCEPTION'
                       'Success: ' || SQL%ROWCOUNT || ' rows imported.', --Any generic information here
                       V_MainTable);              --Major table name processed
   COMMIT;
   
   
    --add by Landor on 20180522 mo拆分处理厂别 测试是否还需要
    UPDATE ABPPMGR.MST_ITEMBOMROUTING_P AA SET AA.VALUE=SUBSTR(AA.ROUTINGID,INSTR(AA.ROUTINGID,'_')+1,4) WHERE VALUE IS NULL;
    
    --ADD BY Landor ON 20180522 VIP订单处理MST_ITEMBOMROUTING_P表新增的ATTRIBUTECLASS为半成品的前缀不带六个0
    UPDATE ABPPMGR.MST_ITEMBOMROUTING_P P SET P.ATTRIBUTECLASS=LTRIM(P.ATTRIBUTECLASS,0) 
             WHERE P.SYS_CREATED_BY='AnDan'AND SUBSTR(LTRIM(ITEM,0),1,3) IN('102','402');
                     
    COMMIT;  
    
    --按单h在之前新增的ROUTINGID都需要新增到MST_OPRESOURCE,拷贝原来ROUTINGID数据然后修改
    --add by Landor on 20180522
    INSERT INTO ABPPMGR.MST_OPRESOURCE (SCENARIO_ID,
                                       ENTERPRISE,
                                       SITEID,
                                       ENGINE_ID,
                                       ROUTINGID,
                                       OPERATIONSEQ,
                                       RESOURCENAME,
                                       WORKCENTERNAME,
                                       SYS_CREATED_BY)
               SELECT        DISTINCT MB.SCENARIO_ID
                                     ,MB.ENTERPRISE
                                     ,MB.SITEID
                                     ,MB.ENGINE_ID
                                     ,MB.ROUTINGID
                                     ,'10'OPERATIONSEQ
                                     ,'DUMMY_RESOURCE'RESOURCENAME
                                     ,'DUMMY_RESOURCE'WORKCENTERNAME
                                     ,MB.SYS_CREATED_BY
                      FROM ABPPMGR.MST_BOMCOMPONENTS MB 
                       WHERE MB.SYS_CREATED_BY='AnDan'
                 UNION 
                SELECT        DISTINCT MB.SCENARIO_ID
                                     ,MB.ENTERPRISE
                                     ,MB.SITEID
                                     ,MB.ENGINE_ID
                                     ,SUBSTR(MB.ROUTINGID,1,13)||'_ORDER_BOM'ROUTINGID
                                     ,'10'OPERATIONSEQ
                                     ,'DUMMY_RESOURCE'RESOURCENAME
                                     ,'DUMMY_RESOURCE'WORKCENTERNAME
                                     ,MB.SYS_CREATED_BY
                      FROM ABPPMGR.MST_BOMCOMPONENTS MB 
                       WHERE MB.SYS_CREATED_BY='AnDan'
            UNION
            SELECT        DISTINCT MB.SCENARIO_ID
                                     ,MB.ENTERPRISE
                                     ,MB.SITEID
                                     ,MB.ENGINE_ID
                                     ,MB.ROUTINGID
                                     ,'10'OPERATIONSEQ
                                     ,'DUMMY_RESOURCE'RESOURCENAME
                                     ,'DUMMY_RESOURCE'WORKCENTERNAME
                                     ,MB.SYS_CREATED_BY
                      FROM ABPPMGR.MST_BOMCOMPONENTSALT MB 
                       WHERE MB.SYS_CREATED_BY='AnDan'
                 UNION 
                SELECT        DISTINCT MB.SCENARIO_ID
                                     ,MB.ENTERPRISE
                                     ,MB.SITEID
                                     ,MB.ENGINE_ID
                                     ,SUBSTR(MB.ROUTINGID,1,13)||'_ORDER_BOM'ROUTINGID
                                     ,'10'OPERATIONSEQ
                                     ,'DUMMY_RESOURCE'RESOURCENAME
                                     ,'DUMMY_RESOURCE'WORKCENTERNAME
                                     ,MB.SYS_CREATED_BY
                      FROM ABPPMGR.MST_BOMCOMPONENTSALT MB 
                       WHERE MB.SYS_CREATED_BY='AnDan';

    COMMIT;


    --add by landor on 20180522 20180614针对非限制库存MO 将VALUE改为0代表只能给非按单生产订单使用用
    MERGE INTO ABPPMGR.MST_ITEMBOMROUTING_P MST
        USING(SELECT MO_ID,ROUTING_ID FROM IN_MO WHERE SO_ID IS NULL) MO
        ON (MST.ROUTINGID=MO.ROUTING_ID)
    WHEN MATCHED
    THEN
        UPDATE SET MST.VALUE=0, ATTRIBUTECLASS=(SUBSTR(ATTRIBUTECLASS,1,INSTR(ATTRIBUTECLASS,':',1,1)-1)||':0') 
            WHERE  MST.ATTRIBUTE='ORDER_PLANNING' AND MST.VALUE='200';
    COMMIT;
    
    --解决AUTO的工单报short问题  add by landor on 20180801 
    UPDATE ABPPMGR.MST_ITEMBOMROUTING_P A SET ATTRIBUTECLASS='000000'||ATTRIBUTECLASS 
    WHERE ATTRIBUTECLASS NOT LIKE '000000%' AND ATTRIBUTE='ORDER_PLANNING' ;
    
    
    /*测试阶段,先注释 
     --修复非限制MO无法正常分配(库存绑定给MTL,无法分配给其它非限制SO)add by landor on 20180809
     --start
    UPDATE ABPPMGR.MST_ITEMBOMROUTING_P A  SET ATTRIBUTECLASS=SUBSTR(ATTRIBUTECLASS,1,24)||'0'  ,VALUE='0'
    WHERE SUBSTR(A.ITEM,1,7)!='0000004' AND SUBSTR(A.ITEM,1,7)!='0000005' 
    --AND LENGTH(A.ROUTINGID)=13 --13位代表标准工艺路线
    AND NOT EXISTS (SELECT NULL FROM IN_ORDER_PLANNING B WHERE A.ITEM=FG_ITEM_ID OR A.ITEM=SEMI_ITEM_ID)
    AND ATTRIBUTE='ORDER_PLANNING' 
    AND VALUE='200';
    COMMIT;

    --工单BOM 且不是验货客户数据需要将属性更新为0
    UPDATE ABPPMGR.MST_ITEMBOMROUTING_P A  
    SET ATTRIBUTECLASS=SUBSTR(ATTRIBUTECLASS,1,24)||'0'  ,VALUE='0'
    WHERE SUBSTR(A.ITEM,1,7)!='0000004' AND SUBSTR(A.ITEM,1,7)!='0000005' AND SUBSTR(A.ROUTINGID,1,4) IN('0011','0012') --工单工艺路线
    AND  EXISTS (SELECT NULL FROM IN_ORDER_PLANNING B WHERE A.ITEM=FG_ITEM_ID OR A.ITEM=SEMI_ITEM_ID)
    AND ATTRIBUTE='ORDER_PLANNING' 
    AND VALUE='200'
    AND NOT EXISTS(---通过MST_ITEMBOMROUTING_P表的ROUTINGID找到MO对应的SO和SO行号,再判断是否真验货客户数据
                    SELECT DISTINCT W.SO_ID,W.SO_LINE_ID,W.ROUTING_ID FROM(
                    SELECT  SO_ID,SO_LINE_ID,ROUTING_ID FROM IN_MO M WHERE  M.SO_ID IS NOT NULL 
                    UNION
                    SELECT  SO_ID,SO_LINE_ID,ROUTING_ID FROM IN_SFCHEADER O WHERE  O.SO_ID IS NOT NULL) W
                    WHERE EXISTS (SELECT NULL FROM IN_ORDER_PLANNING X WHERE W.SO_ID=X.SO_ID   AND W.SO_LINE_ID||'_1'=X.SO_LINE_ID) 
                          AND W.ROUTING_ID=A.ROUTINGID
                );
    COMMIT;   
    --end
    */


    
 /*   --解决按单吃了非限制半成品MO问题,Paul要求需要将此非限制MO由200改为0 add by landor on 20180724
    UPDATE ABPPMGR.MST_ITEMBOMROUTING_P B  
    SET ATTRIBUTECLASS=SUBSTR(ATTRIBUTECLASS,1,INSTR(ATTRIBUTECLASS,':'))||'0'
        ,VALUE='0'
    WHERE  ATTRIBUTE='ORDER_PLANNING' 
            --AND LENGTH(ROUTINGID)=13 --标准工艺路线
            AND VALUE='200'
            AND (EXISTS (SELECT NULL FROM IN_MO M WHERE M.SO_ID IS  NULL AND M.ROUTING_ID =M.MO_ID||B.ROUTINGID) --针对标准工艺路线场景的 非限制mo
                  OR EXISTS (SELECT NULL FROM IN_SFCHEADER M WHERE M.SO_ID IS  NULL AND M.ROUTING_ID =M.MO_ID||B.ROUTINGID)--针对标准工艺路线场景的 非限制mo
                  OR EXISTS (SELECT NULL FROM IN_MO M WHERE M.SO_ID IS  NULL AND M.ROUTING_ID =B.ROUTINGID) --针对工单工艺路线场景的 非限制mo
                  OR EXISTS (SELECT NULL FROM IN_SFCHEADER M WHERE M.SO_ID IS  NULL AND M.ROUTING_ID =B.ROUTINGID)--针对工单工艺路线场景的 非限制mo
                  );
    COMMIT;     */             

--SELECT ATTRIBUTE,ATTRIBUTECLASS,VALUE ,SUBSTR(ATTRIBUTECLASS,1,INSTR(ATTRIBUTECLASS,':'))||'0'
--FROM ABPPMGR.MST_ITEMBOMROUTING_p B 
--WHERE  ATTRIBUTE='ORDER_PLANNING' 
--AND LENGTH(ROUTINGID)=13 --标准工艺路线
--AND VALUE='200'
--AND (EXISTS (SELECT NULL FROM IN_MO M WHERE M.SO_ID IS  NULL AND M.ROUTING_ID =M.MO_ID||B.ROUTINGID) --非限制mo
--      OR EXISTS (SELECT NULL FROM IN_MO M WHERE M.SO_ID IS  NULL AND M.ROUTING_ID =M.MO_ID||B.ROUTINGID))
--      AND ROWNUM<10

   
EXCEPTION
   --总程序异常处理部分
   WHEN OTHERS
   THEN
      BEGIN
         ROLLBACK;
         EXITCODE := 1;

         e_ErrMessage :=
            SUBSTR (SQLERRM, 1, 1000)
            || SUBSTR (DBMS_UTILITY.FORMAT_ERROR_BACKTRACE, 1, 2000);
         PKG_UTIL.CREATELOG (V_PKGName,
                             V_ProName,
                             V_Step,
                             V_Action,
                             PKG_CONSTANT.CONS_MSG_EXCEPTION,
                             e_ErrMessage,
                             V_MainTable);
      END;
END;
/
View Code

 

 FP_INVENTORY

CREATE OR REPLACE PROCEDURE STG.FP_INVENTORY (EXITCODE OUT NUMBER)
IS
   /***********************************************************************
    **  存储过程详细说明
    **  存储过程名称:  FP_INVENTORY
    **  存储过程创建日期: 2014/06/19
    **  存储过程创建人: Kyoko Kato
    **  目的:  Populate the following ODS tables from IN tables
    **    IN_INVENTORY (- IN_MO_RESERVATION) => INVENTORY
    **    IN_PO       => PURCHORDMASTER
    **    IN_PO       => PURCHORDLINE
    **    IN_SOURCING => SALESORDERLINE_P  (+update SALESORDERLINE)
    **    IN_SOURCING => INVENTORYPROPERTY (+update INVENTORY)
    **  输入参数:
    **  输出参数: EXITCODE: 0=Success/1=Failure
    **  返回值: 
    **  用法: 
    **
    ** 修订版本:
    **  版本号     修订时间    修订人        描述
    **  ---------  ----------  ------------  ------------------------
    **    1.0      2014/06/19  Kyoko Kato    1. 新建存储过程.(Except INVENTORY)
    **             2014/07/02  Kyoko Kato    2. Modified V_EngineId, V_Enterprise to refer to PKG_CONSTANT
    **    2.0      2014/07/04  Kyoko Kato    1. Added steps for INVENTORY
    **    3.0      2014/07/10  Kyoko Kato    1. Added steps for IN_SOURCING
    **             2014/07/11  Kyoko Kato    2. Merged step to populate INVENTORY.PROPERTY to Step2
    **   4.0      2015/01/13  huangyangxiong 1.更新当前日期也为过期的判断
    **   5.0     2015/01/14  huangyangxiong 1.为了便于判断,维护了物料到料计划(IN_SUPPLY_ITEM表)的数据,在采购订单行号加个下划线做区分
    **  6.0     2018.05.22  Landor          新增按单生产业务
    **********************************************************************/


   ----------------------- 以下定义存储过程使用的变量---------------------------------
   V_PKGName     VARCHAR2 (40) := 'PKG_FP';
   V_ProName     VARCHAR2 (40) := 'FP_INVENTORY';
   V_Action      VARCHAR2 (1);
   V_Step        VARCHAR2 (40);
   V_MainTable   VARCHAR2 (40);
   V_StartDate   DATE;
   V_EndDate     DATE;
   V_Line        VARCHAR2 (40) := 'TEMP';
   V_NUM         NUMBER := 0;
   V_Count       NUMBER := 0;
   V_Time        DATE;
   E_ErrMessage   varchar2(2000); 
   v_debug number; -- for debug
   --



   V_EngineId    NUMBER        := PKG_CONSTANT.CONS_FP_ENGINE ;
   V_Enterprise  VARCHAR2 (40) := PKG_CONSTANT.CONS_ENTERPRISE ;
   V_SiteId      VARCHAR2 (40) := PKG_CONSTANT.CONS_SITE_ZHUHAI ;
----------------------- 以下是存储过程的主体------------------------------------------

BEGIN
   EXITCODE := 0;

  /*
   第一段逻辑描述,处理规则等
   Delete existing records from ODS
  */
  V_Step := '1';
  V_Action := PKG_CONSTANT.CONS_ACTION_DELETE ;
  V_MainTable := 'MULTI-TABLES' ;

    PKG_UTIL.CREATELOG (V_PKGName,
                      V_ProName,
                      V_Step,
                      V_Action,
                       PKG_CONSTANT.CONS_MSG_INFO,
                      'START',
                      V_MainTable);

  V_Step := '1.1';
  V_MainTable := 'PURCHORDLINE' ;
  DELETE FROM ABPPMGR.PURCHORDLINE_P;
  DELETE FROM abppmgr.PURCHORDLINE ;

  /*
   第一段逻辑处理结束,记录日志
  */
   PKG_UTIL.CREATELOG (V_PKGName,
                       V_ProName,
                       V_Step,
                       V_Action,
                       PKG_CONSTANT.CONS_MSG_INFO,
                       'Record Count: '|| SQL%ROWCOUNT,
                       V_MainTable);
    COMMIT;
  V_Step := '1.2';
  V_MainTable := 'PURCHORDMASTER' ;

  DELETE FROM abppmgr.PURCHORDMASTER ;

  /*
   第一段逻辑处理结束,记录日志
  */
   PKG_UTIL.CREATELOG (V_PKGName,
                       V_ProName,
                       V_Step,
                       V_Action,
                       PKG_CONSTANT.CONS_MSG_INFO,
                       'Record Count: '|| SQL%ROWCOUNT,
                       V_MainTable);

/*
 V3.0 [IN_SOURCING] Added to initialize INVENTORYPROPERTY
*/

  V_Step := '1.3';
  V_MainTable := 'INVENTORYPROPERTY' ;

    --hide by landor on 20180718
  --DELETE FROM abppmgr.INVENTORYPROPERTY
 --  WHERE nvl(SYS_CREATED_BY, 'X') LIKE V_ProName || '%' 
-- ;
--COMMIT;

 --ADD BY Landor ON 20180522 删除按单新增库存数据
 DELETE FROM abppmgr.INVENTORYPROPERTY ;--WHERE SYS_CREATED_BY='AnDan';
 COMMIT;


  /*
   第一段逻辑处理结束,记录日志
  */
   PKG_UTIL.CREATELOG (V_PKGName,
                       V_ProName,
                       V_Step,
                       V_Action,
                       PKG_CONSTANT.CONS_MSG_INFO,
                       'Record Count: '|| SQL%ROWCOUNT,
                       V_MainTable);

  V_Step := '1.4';
  V_MainTable := 'INVENTORY' ;

  DELETE FROM abppmgr.INVENTORY ;
  COMMIT;

  /*
   第一段逻辑处理结束,记录日志
  */
   PKG_UTIL.CREATELOG (V_PKGName,
                       V_ProName,
                       V_Step,
                       V_Action,
                       PKG_CONSTANT.CONS_MSG_INFO,
                       'Record Count: '|| SQL%ROWCOUNT,
                       V_MainTable);

/*
 V3.0 [IN_SOURCING] Added to initialize SALESORDERLINE_P
      [IN_SOURCING] Added to initialize SALESORDERLINE.PROPERTY
*/

  V_Step := '1.5';
  V_MainTable := 'SALESORDERLINE_P' ;

  DELETE FROM abppmgr.SALESORDERLINE_P
   WHERE nvl(SYS_CREATED_BY, 'X') LIKE V_ProName || '%' 
 ;

  /*
   第一段逻辑处理结束,记录日志
  */
   PKG_UTIL.CREATELOG (V_PKGName,
                       V_ProName,
                       V_Step,
                       V_Action,
                       PKG_CONSTANT.CONS_MSG_INFO,
                       'Record Count: '|| SQL%ROWCOUNT,
                       V_MainTable);

  V_Step := '1.6';
  V_Action := PKG_CONSTANT.CONS_ACTION_UPDATE ;
  V_MainTable := 'SALESORDERLINE' ;

  UPDATE abppmgr.SALESORDERLINE
     SET PROPERTY = NULL
       , SYS_LAST_MODIFIED_BY   = V_ProName||'_'||V_Step
       , SYS_LAST_MODIFIED_DATE = SYSDATE
   WHERE PROPERTY IS NOT NULL
  ;

  /*
   第一段逻辑处理结束,记录日志
  */
   PKG_UTIL.CREATELOG (V_PKGName,
                       V_ProName,
                       V_Step,
                       V_Action,
                       PKG_CONSTANT.CONS_MSG_INFO,
                       'Record Count: '|| SQL%ROWCOUNT,
                       V_MainTable);

-- ==================================================
--  IN_INVENTORY related
-- ==================================================

  /*
   第二段逻辑描述,处理规则等
    - Populate abppmgr.INVENTORY -Step1
    - [IN_SOURCING] Set INVENTORY.PROPERTY to LOCATIONID
  */                       
  V_Step := '2';
  V_Action := PKG_CONSTANT.CONS_ACTION_INSERT ;
  V_MainTable := 'INVENTORY' ;

    PKG_UTIL.CREATELOG (V_PKGName,
                      V_ProName,
                      V_Step,
                      V_Action,
                       PKG_CONSTANT.CONS_MSG_INFO,
                      'START',
                      V_MainTable);

    MERGE INTO abppmgr.mst_locationmaster M
         USING (SELECT DISTINCT LOC_ID LOCATIONID FROM IN_INVENTORY) IM
            ON (    M.LOCATIONID = IM.LOCATIONID
                AND M.ENTERPRISE = V_Enterprise
                AND M.SITEID = V_SiteId
                AND M.SCENARIO_ID = 0)
    WHEN MATCHED
    THEN
       UPDATE SET
          M.sys_ent_state = 'ACTIVE',
          M.SYS_LAST_MODIFIED_BY = V_ProName || '_' || V_Step,
          M.SYS_LAST_MODIFIED_DATE = SYSDATE
    WHEN NOT MATCHED
    THEN
       INSERT     (ENTERPRISE,
                   SITEID,
                   ENGINE_ID,
                   SCENARIO_ID,
                   LOCATIONID,
                   SYS_CREATED_BY)
           VALUES (V_Enterprise,
                   V_SiteId,
                   V_EngineId,
                   0,
                   IM.LOCATIONID,
                   V_ProName);

/*
 V3.0 [IN_SOURCING] Modified to populate PROPERTY
*/
 --按单生产需要先考虑半成品销售订单库存  add by landor on 20180522 20180610
 INSERT INTO abppmgr.INVENTORY
        ( ENTERPRISE
        , ENGINE_ID
        , SITEID
        , ITEM
        , LOCATIONID
        , LOCID  --ADD BY Landor 20180522
        , QTYAVAILABLE
        , QTYUOM
        , EFFSTARTDATE
        , LOTLEVEL1
        , LOTLEVEL2
        , SERIALNUM  
        , PROPERTY
        , SYS_CREATED_BY
        )
   SELECT V_Enterprise
        , V_EngineId
        , V_SiteId
        , iinv.ITEM_ID
        , iinv.LOCATION
        , iinv.LOCATION --ADD BY Landor 20180522
        , iinv.QUANTITY
        , TM.udf_meins QTYUOM
         , NULL  EFFSTARTDATE
        ,  substr(iinv.LOCATION,1,4) LOTLEVEL1
         ,  substr(iinv.LOCATION,6) LOTLEVEL2--,  substr(iinv.LOCATION,6)|| '_PEG' LOTLEVEL2
        , iinv.PEGGED_SO_ID||iinv.PEGGED_SO_LINE_ID SERIALNUM  
        , iinv.PEGGED_SO_ID||'_'||iinv.PEGGED_SO_LINE_ID PROPERTY  --, 'OH_' || iinv.PEGGED_SO_ID||'_'||iinv.PEGGED_SO_LINE_ID PROPERTY  paul要求将OH去掉,跟INVENTORYPROPERTY的ATTRIBUCLASS保持一致 20180611 
        , V_ProName||'_'||V_Step
     FROM IN_SEMI_ONHAND_LOCKED iinv , in_item TM  --此处包含有C和无C的SO与半成品绑定
     where iinv.item_id=tm.item_id 
        and exists(select null from IN_INVENTORY iiv   where iiv.item_id=iinv.item_id and iiv.LOC_ID =iinv.LOCATION)
        and iinv.WZNUM LIKE 'C%' --add by lando on 20180719 取消下面语句,改为增加此语句
    -- AND EXISTS (select null from in_order_planning B WHERE B.SO_ID=iinv.PEGGED_SO_ID AND substr(B.SO_LINE_ID,1,instr(B.SO_LINE_ID,'_',-1)-1) =to_char(iinv.PEGGED_SO_LINE_ID)) --add by landor on 20180610 hide by landor on 20180719
     -- AND NOT EXISTS(SELECT NULL FROM ABPPMGR.INVENTORY AI WHERE IINV.ITEM_ID = AI.ITEM AND IINV.LOCATION = AI.LOCATIONID) --hide by landor on 20180610
     ;
     COMMIT;

     --只计算非限制库存  add by landor on 20180522 20180610
    /* INSERT INTO abppmgr.INVENTORY
        ( ENTERPRISE
        , ENGINE_ID
        , SITEID
        , ITEM
        , LOCATIONID
        , LOCID --ADD BY Landor 20180522
        , QTYAVAILABLE
        , QTYUOM
        , EFFSTARTDATE
        , LOTLEVEL1
        , LOTLEVEL2
        , PROPERTY  -- Added at V3.0
        , SYS_CREATED_BY
        )
   SELECT
         V_Enterprise , V_EngineId , V_SiteId , 
         iinv.ITEM_ID
        , iinv.LOC_ID
        , iinv.LOC_ID--ADD BY Landor 20180522
        , (iinv.ONHAND_QTY-A.QTY)*FP_GET_CONVRATE(tm.item_id,iinv.QTYUOM,TM.udf_meins)
        , TM.udf_meins QTYUOM
         , case when substr(iinv.LOC_ID,6,1)='1' then sysdate  else null end  EFFSTARTDATE
        ,  substr(iinv.LOC_ID,1,4) LOTLEVEL1
        ,  substr(iinv.LOC_ID,6) LOTLEVEL2
        , iinv.ITEM_ID|| '_' ||substr( iinv.LOC_ID,1,4) 
        , V_ProName||'_'||V_Step
     FROM IN_INVENTORY iinv , in_item TM
     ,(SELECT ITEM_ID,LOCATION,SUM(QUANTITY) QTY  FROM IN_SEMI_ONHAND_LOCKED WHERE WZNUM IS NULL   GROUP BY ITEM_ID,LOCATION) A
     where iinv.item_id=tm.item_id and  A.ITEM_ID=iinv.ITEM_ID  AND A.LOCATION=iinv.LOC_ID 
     and iinv.ONHAND_QTY>A.QTY  AND iinv.EXT_FORTEXT1 IS  NULL;*/

    --不存在销售订单库存中的数据(非限制库存) add by landor on 20180522 20180610
    INSERT INTO abppmgr.INVENTORY
        ( ENTERPRISE
        , ENGINE_ID
        , SITEID
        , ITEM
        , LOCATIONID
        , LOCID --ADD BY Landor 20180522
        , QTYAVAILABLE
        , QTYUOM
        , EFFSTARTDATE
        , LOTLEVEL1
        , LOTLEVEL2
        , PROPERTY  
        , SYS_CREATED_BY
        )
   SELECT V_Enterprise
        , V_EngineId
        , V_SiteId
        , iinv.ITEM_ID
        , iinv.LOC_ID
        , iinv.LOC_ID--ADD BY Landor 20180522
        , iinv.ONHAND_QTY*FP_GET_CONVRATE(tm.item_id,iinv.QTYUOM,TM.udf_meins)
        , TM.udf_meins QTYUOM
         , case when substr(iinv.LOC_ID,6,1)='1' then sysdate  else null end  EFFSTARTDATE
        ,  substr(iinv.LOC_ID,1,4) LOTLEVEL1
        ,  substr(iinv.LOC_ID,6) LOTLEVEL2
        , iinv.ITEM_ID|| '_' ||substr( iinv.LOC_ID,1,4)  
        , V_ProName||'_'||V_Step
     FROM IN_INVENTORY iinv , in_item TM
     where iinv.item_id=tm.item_id 
     AND NOT EXISTS (SELECT NULL FROM IN_SEMI_ONHAND_LOCKED A where  A.ITEM_ID=iinv.ITEM_ID  AND A.LOCATION=iinv.LOC_ID AND WZNUM IS NULL ) ---有C尾续的全部忽略(此表中C尾续的数据全部存在IN_INVENTORY中,可忽略)
     AND iinv.EXT_FORTEXT1 IS  NULL; --有C尾续的全部忽略

 COMMIT;
   /*  hide by landor on 20180621
  INSERT INTO abppmgr.INVENTORY
        ( ENTERPRISE
        , ENGINE_ID
        , SITEID
        , ITEM
        , LOCATIONID
        , LOCID --ADD BY Landor 20180522
        , QTYAVAILABLE
        , QTYUOM
        , EFFSTARTDATE
        , LOTLEVEL1
        , LOTLEVEL2
        , PROPERTY  -- Added at V3.0
        , SYS_CREATED_BY
        )
   SELECT V_Enterprise
        , V_EngineId
        , V_SiteId
        , iinv.ITEM_ID
        , iinv.LOC_ID
        , iinv.LOC_ID--ADD BY Landor 20180522
        , iinv.ONHAND_QTY*FP_GET_CONVRATE(tm.item_id,iinv.QTYUOM,TM.udf_meins)
        , TM.udf_meins QTYUOM
         , case when substr(iinv.LOC_ID,6,1)='1' then sysdate  else null end  EFFSTARTDATE
        ,  substr(iinv.LOC_ID,1,4) LOTLEVEL1
        ,  substr(iinv.LOC_ID,6) LOTLEVEL2
        , iinv.ITEM_ID|| '_' ||substr( iinv.LOC_ID,1,4)  -- Added at V3.0
        , V_ProName||'_'||V_Step
     FROM IN_INVENTORY iinv , in_item TM
     where iinv.item_id=tm.item_id
  ; 


   INSERT INTO abppmgr.INVENTORY
        ( ENTERPRISE
        , ENGINE_ID
        , SITEID
        , ITEM
        , LOCATIONID
        , LOCID  --ADD BY Landor 20180522
        , QTYAVAILABLE
        , QTYUOM
        , EFFSTARTDATE
        , LOTLEVEL1
        , LOTLEVEL2
        , SERIALNUM  
        , PROPERTY
        , SYS_CREATED_BY
        )
   SELECT V_Enterprise
        , V_EngineId
        , V_SiteId
        , iinv.ITEM_ID
        , iinv.LOCATION
        , iinv.LOCATION --ADD BY Landor 20180522
        , iinv.QUANTITY
        , TM.udf_meins QTYUOM
         , NULL  EFFSTARTDATE
        ,  substr(iinv.LOCATION,1,4) LOTLEVEL1
        ,  substr(iinv.LOCATION,6)|| '_PEG' LOTLEVEL2
        , iinv.PEGGED_SO_ID||iinv.PEGGED_SO_LINE_ID SERIALNUM  
        , 'OH_' || iinv.PEGGED_SO_ID||'_'||iinv.PEGGED_SO_LINE_ID PROPERTY 
        , V_ProName||'_'||V_Step
     FROM IN_SEMI_ONHAND_LOCKED iinv , in_item TM
     where iinv.item_id=tm.item_id and exists(select null from IN_INVENTORY iiv 
        where iiv.item_id=iinv.item_id and iiv.LOC_ID =iinv.LOCATION)

     AND NOT EXISTS(SELECT NULL FROM ABPPMGR.INVENTORY AI WHERE IINV.ITEM_ID = AI.ITEM AND IINV.LOCATION = AI.LOCATIONID);
*/

  /*
   第二段逻辑处理结束,记录日志
  */
   PKG_UTIL.CREATELOG (V_PKGName,
                       V_ProName,
                       V_Step,
                       V_Action,
                       PKG_CONSTANT.CONS_MSG_INFO,
                       'Record Count: '|| SQL%ROWCOUNT,
                       V_MainTable);

  /*
   第三段逻辑描述,处理规则等
   Populate abppmgr.INVENTORY - Step2
   If associated ItemxLoc exists in IN_MO_RESERVATION, reduce that Qty 
  */                       
  V_Step := '3';
  V_Action := PKG_CONSTANT.CONS_ACTION_UPDATE ;
  V_MainTable := 'INVENTORY' ;

    PKG_UTIL.CREATELOG (V_PKGName,
                      V_ProName,
                      V_Step,
                      V_Action,
                       PKG_CONSTANT.CONS_MSG_INFO,
                      'START',
                      V_MainTable);

  MERGE INTO abppmgr.INVENTORY tgt
  USING ( 
          SELECT ITEM_ID
                , LOC_ID
                , sum(UDF_RESERVATION_QTY) UDF_RESERVATION_QTY
             FROM IN_MO_RESERVATION
            GROUP BY ITEM_ID, LOC_ID
        ) src
     ON (    tgt.ITEM = src.ITEM_ID
         AND tgt.LOCATIONID = src.LOC_ID
        )
   WHEN MATCHED THEN
        UPDATE SET tgt.QTYAVAILABLE = tgt.QTYAVAILABLE - src.UDF_RESERVATION_QTY
                 , SYS_LAST_MODIFIED_BY   = V_ProName||'_'||V_Step
                 , SYS_LAST_MODIFIED_DATE = SYSDATE ; 


    --ADD BY Landor 20180522 MO拆分对应库存处理   针对只更新半成品与销售订单库存数据绑定的数据20180610
     UPDATE ABPPMGR.INVENTORY SET  --alter by landor on 20180610
                LOTLEVEL1  = replace(property,'_','') ||'_'||LOTLEVEL1,--SUBSTR(REPLACE(property,'_',''),3,LENGTH(REPLACE(property,'_',''))-2 ) ||'_'||LOTLEVEL1,
               LOCATIONID =  replace(property,'_','')  ||'_'||LOCATIONID--SUBSTR(REPLACE(property,'_',''),3,LENGTH(REPLACE(property,'_',''))-2 ) ||'_'||LOCATIONID
     WHERE property not like '000000%'--property like 'OH%' --AND ITEM IN('000000101011003137','000000102011000038')   alter by landor on 20180611
     ;     
       /*   hide by landor on 20180610取消以下语句,改为上面语句进行更新
         MERGE INTO  ABPPMGR.INVENTORY AI
         USING(SELECT * FROM (SELECT DISTINCT  PEGGED_SO_ID,PEGGED_SO_LINE_ID  ,ITEM_ID  ,QUANTITY ,LOCATION ,SITEID
                     ,ROW_NUMBER()
                     OVER(PARTITION BY ITEM_ID,LOCATION ORDER BY NULL)RN
                FROM IN_SEMI_ONHAND_LOCKED)where rn=1)AC
         ON(       AI.ITEM         = AC.ITEM_ID 
            AND AI.LOCID   = AC.LOCATION
            AND SUBSTR(AI.LOCID,1,4)=AC.SITEID)
        WHEN MATCHED THEN
              UPDATE SET AI.LOTLEVEL1  = AC.PEGGED_SO_ID||AC.PEGGED_SO_LINE_ID||'_'||AI.LOTLEVEL1,
                         AI.LOCATIONID = AC.PEGGED_SO_ID||AC.PEGGED_SO_LINE_ID||'_'||AI.LOCATIONID;*/




  /*
   第三段逻辑处理结束,记录日志
  */
   PKG_UTIL.CREATELOG (V_PKGName,
                       V_ProName,
                       V_Step,
                       V_Action,
                       PKG_CONSTANT.CONS_MSG_INFO,
                       'Record Count: '|| SQL%ROWCOUNT,
                       V_MainTable);

  /*
   第四段逻辑描述,处理规则等
   Populate abppmgr.INVENTORY - Step3
   In case of QTYAVAILABLE <= 0, Delete record from table.
  */                       
  V_Step := '4';
  V_Action := PKG_CONSTANT.CONS_ACTION_DELETE ;
  V_MainTable := 'INVENTORY' ;

    PKG_UTIL.CREATELOG (V_PKGName,
                      V_ProName,
                      V_Step,
                      V_Action,
                       PKG_CONSTANT.CONS_MSG_INFO,
                      'START',
                      V_MainTable);

  DELETE FROM abppmgr.INVENTORY
   WHERE QTYAVAILABLE <= 0
  ; 

  /*
   第四段逻辑处理结束,记录日志
  */
   PKG_UTIL.CREATELOG (V_PKGName,
                       V_ProName,
                       V_Step,
                       V_Action,
                       PKG_CONSTANT.CONS_MSG_INFO,
                       'Record Count: '|| SQL%ROWCOUNT,
                       V_MainTable);

-- ==================================================
--  IN_PO related
-- ==================================================

  /*
   第五段逻辑描述,处理规则等
   Populate abppmgr.PURCHORDMASTER
  */                       
  V_Step := '5';
  V_Action := PKG_CONSTANT.CONS_ACTION_INSERT ;
  V_MainTable := 'PURCHORDMASTER' ;

    PKG_UTIL.CREATELOG (V_PKGName,
                      V_ProName,
                      V_Step,
                      V_Action,
                       PKG_CONSTANT.CONS_MSG_INFO,
                      'START',
                      V_MainTable);

  INSERT INTO abppmgr.PURCHORDMASTER
         ( ENGINE_ID
         , ENTERPRISE
         , PURCHASEORDERID
         , SUPPLIERID
         , SYS_CREATED_BY
         )
   SELECT DISTINCT V_EngineId
        , V_Enterprise
        ,  trim(M.PO_ID) PO_ID
        , TRIM(M.SUPPLIER_ID) SUPPLIER_ID
        , V_ProName||'_'||V_Step
     FROM IN_PO M 
     where not exists(select null from abppmgr.purchordmaster ap 
     where AP.PURCHASEORDERID = TRIM(m.po_id) 
     and AP.SUPPLIERID = TRIM(M.SUPPLIER_ID)
     and AP.ENGINE_ID = v_engineid
     and AP.ENTERPRISE = v_enterprise
     and AP.SYS_CREATED_BY = V_ProName||'_'||V_Step
     )  
     GROUP BY M.PO_ID,M.SUPPLIER_ID
/*     UNION 
     SELECT  DISTINCT V_EngineId
        , V_Enterprise,
        M.PO_ID PO_ID,
        --'' SUPPLIER_ID,
        V_ProName||'_'||V_Step
        FROM IN_SUPPLY_ARRIVALS M WHERE TRIM(M.PO_ID) IS NOT NULL
*/
  ; 

  /*
   第五段逻辑处理结束,记录日志
  */
   PKG_UTIL.CREATELOG (V_PKGName,
                       V_ProName,
                       V_Step,
                       V_Action,
                       PKG_CONSTANT.CONS_MSG_INFO,
                       'Record Count: '|| SQL%ROWCOUNT,
                       V_MainTable);

commit;

  INSERT INTO abppmgr.PURCHORDMASTER
         ( ENGINE_ID
         , ENTERPRISE
         , PURCHASEORDERID
         , SUPPLIERID
         , SOURCEDATE
         , SYS_CREATED_BY
         )
     SELECT DISTINCT V_EngineId
        , V_Enterprise,
        trim(M.BOM_ID) PO_ID,
        '' SUPPLIER_ID,
        SYSDATE,
        V_ProName||'_'||V_Step
        FROM IN_BOM_HEADER M WHERE M.MO_BOM_MARK='1' 
        AND EXISTS(SELECT NULL FROM IN_BOM_DETAILS D WHERE M.BOM_ID=D.BOM_ID AND D.PRODUCT_TYPE IN('CO','BY'))
        and not exists(select null from abppmgr.purchordmaster ap 
     where AP.PURCHASEORDERID = m.BOM_ID 
     --and AP.SUPPLIERID = M.SUPPLIER_ID
     and AP.ENGINE_ID = v_engineid
     and AP.SOURCEDATE = sysdate
     and AP.ENTERPRISE = v_enterprise
     and AP.SYS_CREATED_BY = V_ProName||'_'||V_Step)

        ;
   commit;
  /*
   第六段逻辑描述,处理规则等
   Populate abppmgr.PURCHORDLINE
    retrieved from Operation table
  */                       
  V_Step := '6';
  V_Action := PKG_CONSTANT.CONS_ACTION_INSERT ;
  V_MainTable := 'PURCHORDLINE' ;

    PKG_UTIL.CREATELOG (V_PKGName,
                      V_ProName,
                      V_Step,
                      V_Action,
                       PKG_CONSTANT.CONS_MSG_INFO,
                      'START',
                      V_MainTable);

  INSERT INTO abppmgr.PURCHORDLINE
         ( ENGINE_ID
         , ENTERPRISE
         , PURCHASEORDERID
         , POLINENUM
         , ITEM
         , QTYOPEN
         , QTYUOM
         , SCHEDULEDDELRYDATE
         , UDF_PO_CREATION_DATETIME
         , PROPERTY
         , SYS_CREATED_BY
         , SYS_CREATION_DATE
         )
   SELECT V_EngineId
        , V_Enterprise
        , M.PO_ID
        , M.PO_LINE_NO
        , M.ITEM_ID
        , M.PO_QTY*FP_GET_CONVRATE(tm.item_id,M.UOM,TM.udf_meins) PO_QTY
        , TM.udf_meins UOM
        --, case when M.DUE_DATE<trunc(sysdate) then trunc(sysdate)+7 else M.DUE_DATE end--过期PO加7天
        --ALTER BY HUANGYANGXIONG ON 20150121 ZPAK PO +1  ELSE  +7
        --ALTER BY HUANGYANGXIONG ON 20150122 不管物料类型,过期PO都加1天
        /*
        , case when TM.EXT_FORTEXT1 = 'ZPAK' AND M.DUE_DATE<trunc(sysdate) then trunc(sysdate)+1 
        WHEN TM.EXT_FORTEXT1 <> 'ZPAK' AND M.DUE_DATE<trunc(sysdate) then trunc(sysdate)+7
        else M.DUE_DATE end--过期PO加7天
        */
        , CASE WHEN  M.DUE_DATE<=trunc(sysdate) then trunc(sysdate)+1 ELSE M.DUE_DATE end
        , M.CREATION_DATETIME
        , M.ITEM_ID ||'_'|| M.SITE_ID
        , V_ProName||'_'||V_Step
        , sysdate
     FROM IN_PO  M, in_item tm WHERE tm.item_id=M.item_id
     and  NOT EXISTS(SELECT NULL FROM IN_SUPPLY_ARRIVALS SA WHERE SA.PO_ID=M.PO_ID  AND to_char(SA.PO_LINE_ID)=M.PO_LINE_NO)
 UNION ALL
  SELECT V_EngineId
        , V_Enterprise
        , M. PO_ID
        --ALTER BY HUANGYANGXIONG ON 20150114  为了便于判断和匹配,加多了个下划线做区分
        , to_char(M.PO_LINE_ID) ||'_'|| ROWNUM PO_LINE_ID
        , M.ITEM_ID
        , M.ARRIVAL_QTY*FP_GET_CONVRATE(tm.item_id,PO.UOM,TM.udf_meins) ARRIVAL_QTY
        , TM.udf_meins UOM
        --, case when M.ARRIVAL_DATE<trunc(sysdate) then trunc(sysdate)+7 else M.ARRIVAL_DATE end--过期PO加7天
        --ALTER BY HUANGYANGXIONG ON 20150121 ZPAK PO +1  ELSE  +7
        --ALTER BY HUANGYANGXIONG ON 20150122 不管物料类型,过期PO都加1天
        /*
        , case when TM.EXT_FORTEXT1 = 'ZPAK' AND M.ARRIVAL_DATE<trunc(sysdate) then trunc(sysdate)+1 
        WHEN TM.EXT_FORTEXT1 <> 'ZPAK' AND M.ARRIVAL_DATE<trunc(sysdate) then trunc(sysdate)+7
        else M.ARRIVAL_DATE end--过期PO加7天
        */
        ,CASE WHEN  M.ARRIVAL_DATE<=trunc(sysdate) then trunc(sysdate)+1 ELSE M.ARRIVAL_DATE end
        , NULL CREATION_DATETIME
        , M.ITEM_ID ||'_'|| M.SITE_ID
        , V_ProName||'_'||V_Step
        , sysdate
        FROM IN_SUPPLY_ARRIVALS M,IN_PO PO, in_item tm WHERE tm.item_id=M.item_id
        and M.PO_ID=PO.PO_ID  AND to_char(M.PO_LINE_ID)=PO.PO_LINE_NO
 ; 

  /*
   第六段逻辑处理结束,记录日志
  */
   PKG_UTIL.CREATELOG (V_PKGName,
                       V_ProName,
                       V_Step,
                       V_Action,
                       PKG_CONSTANT.CONS_MSG_INFO,
                       'Record Count: '|| SQL%ROWCOUNT,
                       V_MainTable);

   INSERT INTO abppmgr.PURCHORDLINE
         ( ENGINE_ID
         , ENTERPRISE
         , PURCHASEORDERID
         , POLINENUM
         , ITEM
         , QTYOPEN
         , QTYUOM
         , SCHEDULEDDELRYDATE
         , PROPERTY 
         , SYS_CREATION_DATE
         , SYS_CREATED_BY
         )
   SELECT V_EngineId
        , V_Enterprise
        , M.BOM_ID PO_ID
        , M.ITEM_ID PO_LINE_NO
        , M.ITEM_ID
        , sum(abs(M.USAGE_QTY*FP_GET_CONVRATE(tm.item_id,M.USAGE_QTY_UOM,TM.udf_meins) ))*MO.MO_LOT  QTYOPEN
        , M.USAGE_QTY_UOM QTYUOM
        , case when MO.PLANNED_END_DATE<sysdate then sysdate else MO.PLANNED_END_DATE end +2 SCHEDULEDDELRYDATE
        , M.ITEM_ID || '_' || MO.SITEID
        , SYSDATE SYS_CREATION_DATE
        , V_ProName||'_'||V_Step  SYS_CREATED_BY
   FROM IN_BOM_DETAILS M,IN_item TM,
    (SELECT MO_ID,PRODUCT_ID,SITEID,PLANNED_START_DATE,PLANNED_END_DATE,BOM_ID,ON_LINE_QTY MO_LOT FROM  IN_SFCHEADER
    UNION ALL
    SELECT MO_ID,ITEM_ID PRODUCT_ID,SITEID, PLANNED_START_DATE,PLANNED_END_DATE,BOM_ID,PLANNED_QTY MO_LOT FROM IN_MO
    )  MO WHERE M.BOM_ID=MO.BOM_ID  AND M.PRODUCT_TYPE IN('CO','BY')  and M.item_id=tm.item_id
    group by M.BOM_ID 
        , M.ITEM_ID 
        , MO.MO_LOT
        , M.USAGE_QTY_UOM
        , M.ITEM_ID || '_' || MO.SITEID
        , case when MO.PLANNED_END_DATE<sysdate then sysdate else MO.PLANNED_END_DATE end +2;
    COMMIT;

 insert into ABPPMGR.PURCHORDLINE_P(
           ENGINE_ID
         , ENTERPRISE
         , PURCHASEORDERID
         , POLINENUM
         , ATTRIBUTE
         , ATTRIBUTECLASS
         , RELATIONSHIP
         ,VALUE
         )
select ENGINE_ID
         , ENTERPRISE
         , PURCHASEORDERID
         , POLINENUM
         , 'FACTORY' ATTRIBUTE
         , PROPERTY ATTRIBUTECLASS
         , 'EQ' RELATIONSHIP
         ,substr(PROPERTY,-4) VALUE
          from abppmgr.PURCHORDLINE;
commit;
-- ==================================================
--  IN_SOURCING related
-- ==================================================


  /*
   第八段逻辑描述,处理规则等
    (- Set INVENTORY.PROPERTY is merged in Step2)
    - Populate INVENTORYPROPERTY
       if associated ItemxLoc exists in IN_SOURCING 
  */                       
  V_Step := '8';
  V_Action := PKG_CONSTANT.CONS_ACTION_INSERT ;
  V_MainTable := 'INVENTORYPROPERTY' ;

    PKG_UTIL.CREATELOG (V_PKGName,
                      V_ProName,
                      V_Step,
                      V_Action,
                       PKG_CONSTANT.CONS_MSG_INFO,
                      'START',
                      V_MainTable);

  INSERT INTO abppmgr.INVENTORYPROPERTY
       ( ENTERPRISE
       , ENGINE_ID
       , SITEID
       , LOCATIONID
       , LOCID --add by landor on 20180522
       , ITEM
       , ATTRIBUTECLASS
       , ATTRIBUTE
       , RELATIONSHIP
       , LOTLEVEL1
       , LOTLEVEL2
       , VALUE
       , SERIALNUM
       , SYS_CREATED_BY
       )
  SELECT inv.ENTERPRISE
       , inv.ENGINE_ID
       , inv.SITEID
       , inv.LOCATIONID
       , inv.LOCATIONID  --add by landor on 20180522
       , inv.ITEM
       , inv.PROPERTY  ATTRIBUTECLASS
       --, isrc.CUSTOMER_ID ||'_'|| isrc.FINISH_ITEM_ID -- ATTRIBUTE
       ,'FACTORY' ATTRIBUTE
       , 'EQ' --RELATIONSHIP
       , inv.LOTLEVEL1
       , inv.LOTLEVEL2
       , SUBSTR( inv.LOCATIONID,1,4) VALUE
       , inv.SERIALNUM
       , V_ProName||'_'||V_Step
    FROM abppmgr.INVENTORY inv
   WHERE EXISTS(SELECT NULL FROM IN_ITEM_SITE ITS 
            WHERE INV.ITEM=ITS.ITEM_ID 
                AND substr(INV.LOCATIONID,1,4)=ITS.SITE_ID ) and inv.serialnum is null
   UNION ALL
   SELECT inv.ENTERPRISE
       , inv.ENGINE_ID
       , inv.SITEID
       , inv.LOCATIONID
       , inv.LOCATIONID  --add by landor on 20180522
       , inv.ITEM
       , inv.PROPERTY  ATTRIBUTECLASS
       --, isrc.CUSTOMER_ID ||'_'|| isrc.FINISH_ITEM_ID -- ATTRIBUTE
       ,'FACTORY' ATTRIBUTE
       , 'EQ' --RELATIONSHIP
       , inv.LOTLEVEL1
       , inv.LOTLEVEL2
       , SUBSTR(inv.LOTLEVEL1,LENGTH(inv.LOTLEVEL1)-3,4) VALUE--  alter by landor on 20180612 paul回复attribute=FACTORY的VALUE只能带厂别(此VALUE值取ATTRIBUTECLASS后面:的值),否则会出错
       , inv.SERIALNUM SERIALNUM
       , V_ProName||'_'||V_Step
    FROM abppmgr.INVENTORY inv
   WHERE --EXISTS(SELECT NULL FROM IN_ITEM_SITE ITS  WHERE INV.ITEM=ITS.ITEM_ID   AND SUBSTR(INV.LOCATIONID,1,4)=ITS.SITE_ID ) 
        EXISTS(SELECT NULL FROM IN_ITEM_SITE ITS  WHERE INV.ITEM=ITS.ITEM_ID  AND SUBSTR(INV.LOTLEVEL1,LENGTH(INV.LOTLEVEL1)-3,4)=ITS.SITE_ID )  --alter by landor on 20180610
                AND INV.SERIALNUM IS NOT NULL;     

    COMMIT;                        

  /*
   =OH+_ +IN_SEMI_ONHAND_LOCKED. PEGGED_SO_ID +_+ IN_SEMI_ONHAND_LOCKED.. PEGGED_SO_ID+ IN_SEMI_MO_LOCKED. PEGGED_SO_LINE_ID(不包含计划号)
= IN_SEMI_ ONHAND _LOCKED. PEGGED_SO_ID+ IN_SEMI_ ONHAND _LOCKED. PEGGED_SO_LINE_ID(不包含计划号)+_+”PEG”

  */


  --add start 1
      /* 已在上面进行处理
     --ADD BY Landor 20180522 MO拆分对应库存处理 hide on 20180610
     UPDATE ABPPMGR.INVENTORY SET  --alter by landor on 20180610
                LOTLEVEL1  = SUBSTR(REPLACE(property,'_',''),3,LENGTH(REPLACE(property,'_',''))-2 ) ||'_'||LOTLEVEL1,
               LOCATIONID = SUBSTR(REPLACE(property,'_',''),3,LENGTH(REPLACE(property,'_',''))-2 ) ||'_'||LOCATIONID
     WHERE property like 'OH%' --AND ITEM IN('000000101011003137','000000102011000038')
     ;


        hide by landor on 20180610取消以下语句,改为上面语句进行更新
         MERGE INTO  ABPPMGR.INVENTORY AI
         USING(SELECT * FROM (SELECT DISTINCT  PEGGED_SO_ID,PEGGED_SO_LINE_ID  ,ITEM_ID  ,QUANTITY ,LOCATION ,SITEID
                     ,ROW_NUMBER()
                     OVER(PARTITION BY ITEM_ID,LOCATION ORDER BY NULL)RN
                FROM IN_SEMI_ONHAND_LOCKED)where rn=1)AC
         ON(       AI.ITEM         = AC.ITEM_ID 
            AND AI.LOCID   = AC.LOCATION
            AND SUBSTR(AI.LOCID,1,4)=AC.SITEID)
        WHEN MATCHED THEN
              UPDATE SET AI.LOTLEVEL1  = AC.PEGGED_SO_ID||AC.PEGGED_SO_LINE_ID||'_'||AI.LOTLEVEL1,
                         AI.LOCATIONID = AC.PEGGED_SO_ID||AC.PEGGED_SO_LINE_ID||'_'||AI.LOCATIONID;*/

    /* 因为是Copy INVNTORY的数据所以以下语句不用执行了

        MERGE INTO  ABPPMGR.INVENTORYPROPERTY AI
         USING(SELECT * FROM (SELECT DISTINCT PEGGED_SO_ID,PEGGED_SO_LINE_ID  ,ITEM_ID  ,QUANTITY ,LOCATION ,SITEID
                     ,ROW_NUMBER()
                     OVER(PARTITION BY ITEM_ID,LOCATION ORDER BY NULL)RN
                FROM IN_SEMI_ONHAND_LOCKED)
                WHERE RN=1)AC
         ON(  AI.ITEM         = AC.ITEM_ID 
            AND AI.LOCID        = AC.LOCATION
            AND SUBSTR(AI.LOCID,1,4)=AC.SITEID
            AND AI.SYS_CREATED_BY<>'AnDan')
        WHEN MATCHED THEN
              UPDATE SET AI.LOTLEVEL1       = AC.PEGGED_SO_ID||AC.PEGGED_SO_LINE_ID||'_'||AI.LOTLEVEL1,
                         AI.LOCATIONID      = AC.PEGGED_SO_ID||AC.PEGGED_SO_LINE_ID||'_'||AI.LOCATIONID,
                         AI.ATTRIBUTECLASS  = AC.PEGGED_SO_ID||AC.PEGGED_SO_LINE_ID||'_'||AI.VALUE;
                         COMMIT;   */



         /*按单e.    处理INVENTORYPROPERT:针对非限制半成品库存(非高质量)做处理:从IN_ORDER_PLANNING找出半成品编号需要处理,
        然后再去IN_SEMI_ONHAND_LOCKED,IN_STOCK_LOCATION(不在此仓位的为非高质量)及INVENTORY找出非限制半成品库存(非高质量)。
        然后拷贝原来INVENTORYPROPERT的数据新增到INVENTORYPROPERT中
        ,只有ATTRIBUTE、ATTRIBUTECLASS、VALUE不同,其他相同。*/
        --非高质量非限制库存
          --Question:真验货不能使用非限制半成品库存,只能使用带SO的半成品库存;成品只能使用带SO的MO和库存
        INSERT INTO abppmgr.INVENTORYPROPERTY
               ( ENTERPRISE
               , ENGINE_ID
               , SITEID
               , LOCATIONID
               , LOCID
               , ITEM
               , ATTRIBUTECLASS
               , ATTRIBUTE
               , RELATIONSHIP
               , LOTLEVEL1
               , LOTLEVEL2
               , VALUE
               , SERIALNUM
               , SYS_CREATED_BY
               )

        SELECT DISTINCT ENTERPRISE
               , ENGINE_ID
               , SITEID
               , LOCATIONID
               , LOCID
               , ITEM
               , CASE WHEN ATTRIBUTECLASS LIKE '%:%' THEN SUBSTR(ATTRIBUTECLASS,1,INSTR(ATTRIBUTECLASS,':')-1)||':0'
                 ELSE ATTRIBUTECLASS||':0'
                 END AS ATTRIBUTECLASS --无优先级 =0
               , 'ORDER_PLANNING'ATTRIBUTE
               , RELATIONSHIP--, RELATIONSHIP||'XXX'  alter by landor on 20180605
               , LOTLEVEL1
               , LOTLEVEL2
               , '0'VALUE  
               , SERIALNUM
               , 'AnDan' 
        FROM abppmgr.INVENTORYPROPERTY  AI  
        JOIN IN_ORDER_PLANNING IP 
        ON AI.ITEM=IP.SEMI_ITEM_ID        
        WHERE LENGTH(LOCATIONID)<=9
        AND AI.LOTLEVEL2 NOT IN(
        SELECT LOCATION_ID FROM IN_STOCK_LOCATION)
        UNION ALL
        --增加库存指定订单
        SELECT DISTINCT ENTERPRISE
               , ENGINE_ID
               , SITEID
               , LOCATIONID
               , LOCID
               , ITEM
               , CASE WHEN ATTRIBUTECLASS LIKE '%:%' THEN SUBSTR(ATTRIBUTECLASS,1,INSTR(ATTRIBUTECLASS,':')-1)||':'||SUBSTR(ATTRIBUTECLASS,1,INSTR(ATTRIBUTECLASS,'_')-1) 
                 ELSE ATTRIBUTECLASS   ||':'|| REPLACE(ATTRIBUTECLASS,'_','')--  hide by landor on 20180611
                 END AS ATTRIBUTECLASS 
               , 'ORDER_PLANNING'ATTRIBUTE
               , RELATIONSHIP
               , LOTLEVEL1
               , LOTLEVEL2
               ,REPLACE(ATTRIBUTECLASS,'_','') --, SUBSTR(ATTRIBUTECLASS,1,INSTR(ATTRIBUTECLASS,'_')-1) VALUE  alter by landor on 20180611  
               , SERIALNUM
               , 'AnDan' 
        FROM abppmgr.INVENTORYPROPERTY  AI  
        JOIN IN_ORDER_PLANNING IP 
        ON AI.ITEM=IP.SEMI_ITEM_ID        
        WHERE LENGTH(LOCATIONID)>9
        UNION ALL
        --高质量非限制库存
        SELECT DISTINCT ENTERPRISE
               , ENGINE_ID
               , SITEID
               , LOCATIONID
               , LOCID
               , ITEM
               ,CASE WHEN ATTRIBUTECLASS LIKE '%:%' THEN SUBSTR(ATTRIBUTECLASS,1,INSTR(ATTRIBUTECLASS,':')-1)||':'||TO_CHAR(10000-IC.ID)
                 ELSE ATTRIBUTECLASS||':'||TO_CHAR(10000-IC.ID)
                 END AS ATTRIBUTECLASS 
               , 'ORDER_PLANNING'ATTRIBUTE
               , RELATIONSHIP
               , LOTLEVEL1
               , LOTLEVEL2
               , TO_CHAR(10000-IC.ID) VALUE
               , SERIALNUM
               , 'AnDan' 
        FROM abppmgr.INVENTORYPROPERTY  AI
        JOIN IN_ORDER_PLANNING IP 
        ON AI.ITEM=IP.SEMI_ITEM_ID  
        JOIN  IN_STOCK_LOCATION IL
        ON  AI.LOTLEVEL2= IL.LOCATION_ID
        JOIN   IN_STOCK_CUSTOMER IC
        ON IL.LOCATION_ID=IC.LOCATION_ID
        AND IP.CUSTOMER_ID=IC.CUSTOMER_ID
        WHERE LENGTH(LOTLEVEL1)<=4;

        --ADD BY Landor 20180522 IN_ORDER_PLANING V4 新增一笔处理过order_Planning的数据
        INSERT INTO abppmgr.INVENTORYPROPERTY
               ( ENTERPRISE
               , ENGINE_ID
               , SITEID
               , LOCATIONID
               , LOCID
               , ITEM
               , ATTRIBUTECLASS
               , ATTRIBUTE
               , RELATIONSHIP
               , LOTLEVEL1
               , LOTLEVEL2
               , VALUE
               , SERIALNUM
               , SYS_CREATED_BY
               )
        SELECT DISTINCT ENTERPRISE
               , ENGINE_ID
               , SITEID
               , NULL
               , NULL
               , ITEM
               , 'PASS_ALL:-1' ATTRIBUTECLASS 
               , 'ORDER_PLANNING'ATTRIBUTE
               , 'PASS'RELATIONSHIP
               , NULL
               , NULL
               , NULL VALUE  
               , SERIALNUM
               , 'AnDan' 
        FROM abppmgr.INVENTORYPROPERTY WHERE SYS_CREATED_BY='AnDan' AND ROWNUM=1;
  --end add 1

    PKG_UTIL.CREATELOG (V_PKGName,
                       V_ProName,
                       V_Step,
                       V_Action,
                       PKG_CONSTANT.CONS_MSG_INFO,
                       'Record Count: '|| SQL%ROWCOUNT,
                       V_MainTable);

    COMMIT;
    DELETE FROM ABPPMGR.MST_ITEMSITEFPPARAMS;
    COMMIT;
  /*
    insert into ABPPMGR.MST_ITEMSITEFPPARAMS(ENTERPRISE
    , ENGINE_ID
    , SITEID
    ,ITEM
    ,SORTATTRIBUTE
    , EFFSTARTDATE )
    select 
    V_Enterprise
    ,V_EngineId
    , V_SiteId
    , X.ITEM_ID
    , X.SORTATTRIBUTE || ',FACTORY' SORTATTRIBUTE
    , NULL EFFSTARTDATE
   FROM (SELECT OH.ITEM_ID,
   TO_CHAR(WM_CONCAT(OH.PEGGED_SO_ID ||  OH.PEGGED_SO_LINE_ID||'_PEG')) SORTATTRIBUTE
   FROM (SELECT DISTINCT ITEM_ID,PEGGED_SO_ID,PEGGED_SO_LINE_ID FROM  IN_SEMI_ONHAND_LOCKED) OH GROUP BY OH.ITEM_ID
   UNION
   SELECT MO.ITEM_ID,
   TO_CHAR(WM_CONCAT(MO.PEGGED_SO_ID ||  MO.PEGGED_SO_LINE_ID||'_PEG')) SORTATTRIBUTE
   FROM (SELECT DISTINCT ITEM_ID,PEGGED_SO_ID,PEGGED_SO_LINE_ID FROM IN_SEMI_MO_LOCKED) MO GROUP BY MO.ITEM_ID
   ) X;
   COMMIT;
   */


   PKG_UTIL.CREATELOG (V_PKGName,
                       V_ProName,
                       V_Step,
                       V_Action,
                       PKG_CONSTANT.CONS_MSG_INFO,
                       'COMMIT Executed',
                       V_MainTable);


    --ADD BY Landor ON 20180522 按单方案处理库存INVENTORY.LOTLEVEL1字段值
    MERGE INTO ABPPMGR.INVENTORY AI
    USING(
    SELECT DISTINCT LISTAGG(A.NAME_ID,'_')WITHIN GROUP(ORDER BY NULL)AS NAME_ID
                   ,B.WERKS||'_'||B.LOCATION_ID LOC_ID
    FROM IN_STOCK_CUSTOMER A,IN_STOCK_LOCATION B WHERE 
    CASE WHEN A.LOCATION_ID='*' THEN B.LOCATION_ID--*表示所有高品质仓
    ELSE A.LOCATION_ID
    END 
    =B.LOCATION_ID
    GROUP BY B.WERKS||'_'||B.LOCATION_ID)BI
    ON(AI.LOCATIONID=BI.LOC_ID)
    WHEN MATCHED THEN 
     UPDATE SET AI.LOTLEVEL1=BI.NAME_ID||'_'||AI.LOTLEVEL1;

     COMMIT; 

    --add by Landor on 20180522 按单把原来的ATTRIBUTECLASS+:+ INVENTORYPROPERTY.VALUE  
    UPDATE  ABPPMGR.INVENTORYPROPERTY SET ATTRIBUTECLASS=ATTRIBUTECLASS || ':' || SUBSTR(LOTLEVEL1,LENGTH(LOTLEVEL1)-3,4) --VALUE   --alter by landor on20180611
        WHERE SYS_CREATED_BY<>'AnDan' AND ATTRIBUTECLASS NOT LIKE '%:%';

   COMMIT;

   --add by landor on 20180522 20180627针对有按单生产的半成品库存。解决 库存无法分配
   INSERT INTO ABPPMGR.INVENTORYPROPERTY(SCENARIO_ID,  ATTRIBUTE,ATTRIBUTECLASS,  ENGINE_ID,  ENTERPRISE ,  ITEM ,  LOCATIONID,  LOTLEVEL1,  LOTLEVEL2 ,    
                                         RELATIONSHIP,  SERIALNUM ,  SITEID ,  SOURCEDATE,  SYNCIND , VALUE , SYS_SOURCE ,  SYS_CREATED_BY ,  SYS_CREATION_DATE,  SYS_ENT_STATE,  LOCID)
     SELECT SCENARIO_ID,  ATTRIBUTE,SUBSTR(ATTRIBUTECLASS,1,INSTR(ATTRIBUTECLASS,':'))||'200' , ENGINE_ID,  ENTERPRISE ,  ITEM ,  LOCATIONID,  LOTLEVEL1,  LOTLEVEL2 ,    
            RELATIONSHIP,  SERIALNUM ,  SITEID ,  SOURCEDATE,  SYNCIND ,  '200',  SYS_SOURCE ,  SYS_CREATED_BY ,  SYS_CREATION_DATE,  SYS_ENT_STATE,  LOCID
     FROM  ABPPMGR.INVENTORYPROPERTY  WHERE ATTRIBUTE='ORDER_PLANNING';
   COMMIT;    
   
   --半成品库存与SO绑定 且满足以下条件的,不给任何订单使用start add by  landor on 20180718
   --1.没有未清SO但存在半成品SO库存的,直接将库存的工厂FACTORY属性更新为0,目的是不给任何订单使用
   UPDATE    ABPPMGR.INVENTORYPROPERTY V SET  ATTRIBUTECLASS=SUBSTR(ATTRIBUTECLASS,1,LENGTH(ATTRIBUTECLASS)-4)||'0'
    WHERE ATTRIBUTE='FACTORY' AND ATTRIBUTECLASS NOT LIKE '000000%'
     AND  NOT EXISTS (SELECT NULL FROM IN_SALES_ORDER S WHERE  V.SERIALNUM = S.SO_ID|| SUBSTR(S.SO_LINE_ID,1,INSTR(S.SO_LINE_ID,'_')-1));
    COMMIT;     
   
   --2. 有半成品SO库存、有未清SO、不欠料  、 有REL成品工单且工单数量大于未清SO数量,确保已开工单库存满足未清SO、              
    MERGE INTO ABPPMGR.INVENTORYPROPERTY V 
    USING (    
            SELECT distinct PEGGED_SO_ID,PEGGED_SO_LINE_ID FROM IN_SEMI_ONHAND_LOCKED SL    --此处将多计划行去掉了,日后需要启用
            JOIN IN_SALES_ORDER S ON S.SO_ID=SL.PEGGED_SO_ID AND SUBSTR(S.SO_LINE_ID,1,INSTR(S.SO_LINE_ID,'_')-1)=SL.PEGGED_SO_LINE_ID  ----有半成品SO库存、有未清SO
            JOIN IN_SFCHEADER  SF ON SF.SO_ID =S.SO_ID AND SF.SO_LINE_ID=SL.PEGGED_SO_LINE_ID  AND SF.MO_ID LIKE '0011%'  --有REL成品工单
                                       AND SF.ON_LINE_QTY>= S.ORDERED_QTY --工单数量大于未清SO数量,确保已开工单库存满足未清SO
            WHERE NOT EXISTS (SELECT NULL FROM  IN_MO_SHORT MS  WHERE  MS.MO_ID=SF.MO_ID AND MS.ITEM_ID=SL.ITEM_ID)) B  --成品MO对应的半成品在IN_MO_SHORT没数据,代表不欠料  
       ON(V.SERIALNUM=B.PEGGED_SO_ID||B.PEGGED_SO_LINE_ID)
    WHEN MATCHED THEN 
     UPDATE SET ATTRIBUTECLASS=SUBSTR(ATTRIBUTECLASS,1,LENGTH(ATTRIBUTECLASS)-4)||'0' WHERE ATTRIBUTE='FACTORY'AND ATTRIBUTECLASS NOT LIKE '000000%' ;

     COMMIT;                        
    --END ADD  by  landor on 20180718
    
    --解决按单吃非限制库存问题add by landor on 20180723
    --PAUL回复:把INVENTORYPROPERTY中如果是非限制庫存不能在ORDER_PLANNING中有等於200的值那會可以給按單生產的訂單使用
    DELETE FROM ABPPMGR.INVENTORYPROPERTY WHERE ATTRIBUTE='ORDER_PLANNING' AND ATTRIBUTECLASS LIKE '000000%' AND ATTRIBUTECLASS LIKE '%:200';
    COMMIT;

    

EXCEPTION
   --总程序异常处理部分
   WHEN OTHERS
   THEN
      BEGIN
         ROLLBACK;
         EXITCODE := 1;

         e_ErrMessage := SUBSTR (SQLERRM, 1, 1000)
                      || SUBSTR (DBMS_UTILITY.FORMAT_ERROR_BACKTRACE, 1, 2000);
         PKG_UTIL.CREATELOG (V_PKGName,
                             V_ProName,
                             V_Step,
                             V_Action,
                             PKG_CONSTANT.CONS_MSG_EXCEPTION,
                             e_ErrMessage,
                             V_MainTable);
      END;

END FP_INVENTORY;
/
View Code

 

FP_MO

CREATE OR REPLACE PROCEDURE STG.FP_MO (EXITCODE OUT NUMBER)
IS
   /***********************************************************************
    **  存储过程详细说明
    **  存储过程名称:  FP_MO
    **  存储过程创建日期: 2014/06/18
    **  存储过程创建人: Kyoko Kato
    **  目的:  Populate the following ODS tables from IN tables
    **    IN_MO        => PRODUCTIONORDERS
    **    IN_MO        => SUPPLYDMDPEGGING
    **    IN_SFCHEADER => SFCHEADER
    **    IN_SFCLINE   => SFCLINE
    **  输入参数:
    **  输出参数: EXITCODE: 0=Success/1=Failure
    **  返回值: 
    **  用法: 
    **
    *-\*+"P;9U7YTR+ 修订版本:
    **  版本号     修订时间    修订人        描述
    **  ---------  ----------  ------------  ------------------------
    **    1.0      2014/06/18  Kyoko Kato    1. 新建存储过程.
    **             2014/07/02  Kyoko Kato    2. Modified V_EngineId, V_Enterprise to refer to PKG_CONSTANT
    **                                       3. Modified BOMID from "BOM_ID" to "'FP_'|| ITEM_ID ||'_'|| BOM_ID"
    **    2.0      2014/07/03  Kyoko Kato    1. Added steps to delete/populate SUPPLYDMDPEGGING
    **   3.0      2018/04/27    Landor         解决CRTD重复规划问题
    **   4.0      2018/05/22    Landor      增加按单生产业务
   **********************************************************************/

   ----------------------- 以下定义存储过程使用的变量---------------------------------
   V_PKGName     VARCHAR2 (40) := 'PKG_FP';
   V_ProName     VARCHAR2 (40) := 'FP_MO';
   V_Action      VARCHAR2 (1);
   V_Step        VARCHAR2 (40);
   V_MainTable   VARCHAR2 (40);
   V_StartDate   DATE;
   V_EndDate     DATE;
   V_Line        VARCHAR2 (40) := 'TEMP';
   V_NUM         NUMBER := 0;
   V_Count       NUMBER := 0;
   V_Time        DATE;
   E_ErrMessage   varchar2(2000); 
   v_debug number; -- for debug
   --
   V_EngineId    NUMBER        := PKG_CONSTANT.CONS_FP_ENGINE ;
   V_Enterprise  VARCHAR2 (40) := PKG_CONSTANT.CONS_ENTERPRISE ;
   V_DummyCustomer varchar2(40):='Dummy Customer';
   V_SiteID varchar2(40):=PKG_CONSTANT.CONS_SITE_ZHUHAI;
   V_SalesLevel VARCHAR2 (40) :='1';
   V_SalesName VARCHAR2 (40) :='1';
   V_OrganizationID VARCHAR2(40):='ORG';
   V_ScenarioID number:=0;
   V_Qty number;
   v_loc varchar2(40);
   v_locs varchar2(500);
   v_OhQty number;
----------------------- 以下是存储过程的主体------------------------------------------

BEGIN
   EXITCODE := 0;

  /*
   第一段逻辑描述,处理规则等
   Delete existing records from ODS
  */
  V_Step := 1;
  V_Action := PKG_CONSTANT.CONS_ACTION_DELETE ;

  V_MainTable := 'SFCLINE' ;
  PKG_UTIL.CREATELOG (V_PKGName,
                      V_ProName,
                      V_Step,
                      V_Action,
                      PKG_CONSTANT.CONS_MSG_INFO,
                      'START',
                      V_MainTable);
  DELETE FROM  ABPPMGR.SFCLINEPROPERTY;
  DELETE FROM abppmgr.SFCLINE ;
  /*
   第一段逻辑处理结束,记录日志
  */
   PKG_UTIL.CREATELOG (V_PKGName,
                       V_ProName,
                       V_Step,
                       V_Action,
                       PKG_CONSTANT.CONS_MSG_INFO,
                       'Record Count: '|| SQL%ROWCOUNT,
                       V_MainTable);
                       commit;
  execute immediate 'truncate table TEMP_MO_PEG_SO';
  V_MainTable := 'SFCHEADER' ;
  PKG_UTIL.CREATELOG (V_PKGName,
                      V_ProName,
                      V_Step,
                      V_Action,
                      PKG_CONSTANT.CONS_MSG_INFO,
                      'START',
                      V_MainTable);
  DELETE FROM  ABPPMGR.SFCHEADERPROPERTY;
  DELETE FROM abppmgr.SFCHEADER ;

  /*
   第一段逻辑处理结束,记录日志
  */
   PKG_UTIL.CREATELOG (V_PKGName,
                       V_ProName,
                       V_Step,
                       V_Action,
                       PKG_CONSTANT.CONS_MSG_INFO,
                       'Record Count: '|| SQL%ROWCOUNT,
                       V_MainTable);
    COMMIT;
  V_MainTable := 'PRODUCTIONORDERS' ;
  PKG_UTIL.CREATELOG (V_PKGName,
                      V_ProName,
                      V_Step,
                      V_Action,
                      PKG_CONSTANT.CONS_MSG_INFO,
                      'START',
                      V_MainTable);
  DELETE FROM abppmgr.PRODUCTIONORDERS_P ;
  DELETE FROM abppmgr.PRODUCTIONORDERS ;

  /*
   第一段逻辑处理结束,记录日志
  */
   PKG_UTIL.CREATELOG (V_PKGName,
                       V_ProName,
                       V_Step,
                       V_Action,
                       PKG_CONSTANT.CONS_MSG_INFO,
                       'Record Count: '|| SQL%ROWCOUNT,
                       V_MainTable);

  V_MainTable := 'SUPPLYDMDPEGGING' ;
  PKG_UTIL.CREATELOG (V_PKGName,
                      V_ProName,
                      V_Step,
                      V_Action,
                      PKG_CONSTANT.CONS_MSG_INFO,
                      'START',
                      V_MainTable);

  DELETE FROM abppmgr.SUPPLYDMDPEGGING ;

/*
--半成品工单硬绑成品工单
update in_mo mo set MO.PEGGED_MO_ID=(select ml.PEGGED_MO_ID from  in_semi_mo_locked ml where ml.mo_id=mo.mo_id and rownum=1)
where exists(select null from in_semi_mo_locked ml where ml.mo_id=mo.mo_id);

update in_sfcheader mo set MO.PEGGED_MO_ID=(select ml.PEGGED_MO_ID from  in_semi_mo_locked ml where ml.mo_id=mo.mo_id  and rownum=1)
where exists(select null from in_semi_mo_locked ml where ml.mo_id=mo.mo_id);

commit;
*/
--20141021
--工单绑定销售订单功能不完善
--故先关闭该功能
/*
delete from in_semi_mo_locked;
delete from in_semi_onhand_locked;
*/ 
 /*
   第一段逻辑处理结束,记录日志
  */
   PKG_UTIL.CREATELOG (V_PKGName,
                       V_ProName,
                       V_Step,
                       V_Action,
                       PKG_CONSTANT.CONS_MSG_INFO,
                       'Record Count: '|| SQL%ROWCOUNT,
                       V_MainTable);
  commit;
  /*
   第二段逻辑描述,处理规则等
   Populate abppmgr.PRODUCTIONORDERS
  */                       
  V_Step := '2';
  V_Action := PKG_CONSTANT.CONS_ACTION_INSERT ;
  V_MainTable := 'PRODUCTIONORDERS' ;

    PKG_UTIL.CREATELOG (V_PKGName,
                      V_ProName,
                      V_Step,
                      V_Action,
                      PKG_CONSTANT.CONS_MSG_INFO,
                      'START',
                      V_MainTable);

  INSERT INTO abppmgr.PRODUCTIONORDERS
         ( ENTERPRISE
         , ENGINE_ID
         , BOMID
         , ITEM
         , PLANNEDSTARTDATE
         , PRODUCTIONORDID
         , QTYORDERED
         , REQUIREDDATE
         , ROUTINGID
         , SITEID
         , SOURCEDATE
         , SYS_CREATED_BY
         , ISFEEDSLOCKED
         , ISQTYLOCKED
         , ISROUTINGLOCKED
         , PROPERTY
         )
   SELECT V_Enterprise
        , V_EngineId
        , ''|| LTRIM(ITEM_ID,'0') ||'_'|| TRIM(BOM_ID) BOM_ID
        , ITEM_ID
        , case when PLANNED_START_DATE<sysdate 
        then PLANNED_START_DATE+3 else
        PLANNED_START_DATE end PLANNED_START_DATE
        , MO_ID
        , PLANNED_QTY
        , PLANNED_END_DATE
        , ROUTING_ID
        , V_SITEID
        , CREATION_DATETIME
        , V_ProName||'_'||V_Step
        ,'1' ISFEEDSLOCKED
        ,'1' ISQTYLOCKED
        ,'1' ISROUTINGLOCKED
        , ITEM_ID || '_' || SITEID
     FROM IN_MO
     /*
--add by huangyangxiong on 20150315    add WIP MO into productionorderid table which used in the in_semi_mo_locked
     union all 
     select V_Enterprise
        , V_EngineId
        , ''|| LTRIM(SH.PRODUCT_ID,'0') ||'_'|| SH.BOM_ID BOM_ID
        , SH.PRODUCT_ID
        , NULL PLANNED_START_DATE
        , SH.MO_ID
        , SH.ON_LINE_QTY PLANNED_QTY
        , SL.PLANNED_END_DATE
        , SH.ROUTING_ID
        , V_SITEID
        , null CREATION_DATETIME
        , V_ProName||'_'||V_Step
        ,'1' ISFEEDSLOCKED
        ,'1' ISQTYLOCKED
        ,'1' ISROUTINGLOCKED 
        ,SH.PRODUCT_ID||'_'||SH.SITEID
        from IN_SFCLINE SL,IN_SFCHEADER SH 
        where sl.mo_id=SH.mo_id
          and (exists (select null from in_semi_mo_locked ml where SH.MO_ID = ML.PEGGED_MO_ID )
           or exists (select null from in_semi_mo_locked ml where SH.MO_ID = ML.MO_ID ))
          and not exists (select null from abppmgr.PRODUCTIONORDERS ap where ap.PRODUCTIONORDID = SH.MO_ID); 
  --试产工单不能成为供给,工单类型为‘ZP13', 'ZP23' , 'ZP56', 'ZP66’的工单增加虚拟的SO,使其不能被其他需求占用
  */
commit;
INSERT INTO abppmgr.PRODUCTIONORDERS
         ( ENTERPRISE
         , ENGINE_ID
         , BOMID
         , ITEM
         , PLANNEDSTARTDATE
         , PRODUCTIONORDID
         , QTYORDERED
         , REQUIREDDATE
         , ROUTINGID
         , SITEID
         , SOURCEDATE
         , SYS_CREATED_BY
         ,ISFEEDSLOCKED
         ,ISQTYLOCKED
         ,ISROUTINGLOCKED
         , PROPERTY
         )
   SELECT V_Enterprise
        , V_EngineId
        ,  LTRIM(BR.PRODUCED_ITEM_ID,'0') ||'_'|| TRIM(BR.BOM_ID) BOM_ID
        , IM.SEMI_ID ITEM
        , SYSDATE PLANNED_START_DATE
        , 'GB2828_' || IM.SEMI_ID || '_' || IM.SITE_ID||'/10-MFG000'
        , IM.DEMAND_QTY
        , NULL PLANNED_END_DATE
        , BR.ROUTING_ID
        , V_SITEID
        , sysdate
        , V_ProName||'_'||V_Step
        ,'1' ISFEEDSLOCKED
        ,'1' ISQTYLOCKED
        ,'1' ISROUTINGLOCKED
        , IM.SEMI_ID || '_' || IM.SITE_ID
     FROM IN_GB2828 IM , IN_ITEMBOMROUTING BR,IN_BOM_HEADER BH 
     WHERE IM.SEMI_ID=BR.PRODUCED_ITEM_ID AND IM.SITE_ID=BR.SITEID 
     AND BH.BOM_ID=BR.BOM_ID AND BH.MO_BOM_MARK='0' AND IM.SEMI_ID=BH.PRODUCT_ID;
    COMMIT;

    delete from ABPPMGR.SALESORDERLINE M where M.sys_created_BY=v_proname;
    commit;
    delete from ABPPMGR.SALESORDERMASTER M where M.sys_created_BY=v_proname;
    commit;
     INSERT INTO ABPPMGR.SALESORDERMASTER (
                                         ENTERPRISE,
                                         ENGINE_ID,
                                         SITEID,
                                         SALESNAME,
                                         SALESLEVEL,
                                         SALESORDERID,
                                         CUSTOMERID,
                                         SYS_CREATED_BY)
      SELECT      V_Enterprise,
                      V_EngineID,
                      V_SiteID,
                      V_SalesLevel,
                      V_SalesName,
                      M.MO_TYPE|| '_' ||M.MO_ID,
                      V_DummyCustomer CUSTOMER_ID,
                      V_ProName
--        FROM IN_MO M where M.MO_TYPE in('ZP13', 'ZP23' , 'ZP56', 'ZP66') and trim(M.so_id) is null
        FROM IN_MO M where M.MO_TYPE in('ZP12') and trim(M.so_id) is null
        UNION ALL
        SELECT      V_Enterprise,
                      V_EngineID,
                      V_SiteID,
                      V_SalesLevel,
                      V_SalesName,
                      M.MO_TYPE|| '_' ||M.MO_ID,
                      V_DummyCustomer CUSTOMER_ID,
                      V_ProName
--          FROM IN_MO M where M.MO_TYPE in('ZP13', 'ZP23' , 'ZP56', 'ZP66') and trim(M.so_id) is null;
        FROM IN_SFCHEADER M where M.MO_TYPE in('ZP12') and trim(M.so_id) is null;

    MERGE INTO ABPPMGR.MST_LOCATIONMASTER M
         USING (SELECT DISTINCT SUBSTR(BOM_ID,INSTR(BOM_ID,'_')+1,4) SITEID FROM IN_MO) IM
            ON (    M.ENTERPRISE = V_Enterprise
                AND M.SITEID = V_SiteID
                AND M.LOCATIONID = IM.siteid
                AND M.SCENARIO_ID = 0)
    WHEN MATCHED
    THEN
       UPDATE SET M.sys_ent_state = 'ACTIVE'
    WHEN NOT MATCHED
    THEN
       INSERT     (ENTERPRISE,
                   SITEID,
                   LOCATIONID,
                   SCENARIO_ID,
                   ENGINE_ID,
                   SYS_CREATED_BY)
           VALUES (V_Enterprise,
                   V_SiteID,
                   IM.siteid,
                   0,
                   V_EngineID,
                   V_ProName);

    COMMIT;
     INSERT INTO ABPPMGR.SALESORDERLINE (
                                       ENTERPRISE,
                                       ENGINE_ID,
                                       SITEID,
                                       SALESNAME,
                                       SALESLEVEL,
                                       ORGANIZATIONID,
                                       ITEM,
                                       --REQUESTEDITEM,
                                       LOCATIONID,
                                       PROMISEDAVAILDATE,
                                       QTYOPEN,
                                       SALESORDERID,
                                       SOLINENUM,
                                       CUSTOMERID,
                                       DELIVERYGROUP,
                                       UDF_CREATION_DATETIME,
                                       PRIORITY,
                                       SYS_CREATED_BY)
      SELECT V_Enterprise,
             V_EngineID,
             V_SiteID,
             V_SalesLevel,
             V_SalesName,
             V_OrganizationID,
             ITEM_ID ITEM,
             --ITEM_ID REQUESTEDITEM,
             SUBSTR(BOM_ID,INSTR(BOM_ID,'_')+1,4) LOCATIONID,
             PLANNED_END_DATE,
             PLANNED_QTY,
             M.MO_TYPE|| '_' ||MO_ID SO_ID,
             ITEM_ID SO_LINE_ID,
             V_DummyCustomer CUSTOMER_ID,
             '' DEMAND_SHIPPING_CODE,
             CREATION_DATETIME,
             '100000' PRIORITY,
             V_ProName
--         FROM IN_MO M WHERE M.MO_TYPE IN ('ZP13', 'ZP23' , 'ZP56', 'ZP66')  and trim(M.so_id) is null
         FROM IN_MO M WHERE M.MO_TYPE IN ('ZP12')  and trim(M.so_id) is null
   UNION ALL
   SELECT V_Enterprise,
             V_EngineID,
             V_SiteID,
             V_SalesLevel,
             V_SalesName,
             V_OrganizationID,
             PRODUCT_ID ITEM,
             --PRODUCT_ID REQUESTEDITEM,
             SITEID LOCATIONID,
             (SELECT case when SL.PLANNED_END_DATE<=SYSDATE
             then SL.PLANNED_END_DATE
             else
             SYSDATE+3
             end
              from IN_SFCLINE SL where SL.MO_ID=M.MO_ID and rownum=1) 
             PLANNED_END_DATE,
             ON_LINE_QTY PLANNED_QTY,
             M.MO_TYPE|| '_' ||MO_ID SO_ID,
             PRODUCT_ID SO_LINE_ID,
             V_DummyCustomer CUSTOMER_ID,
             '' DEMAND_SHIPPING_CODE,
             null CREATION_DATETIME,
             '100000' PRIORITY,
             V_ProName
--         FROM IN_SFCHEADER M WHERE M.MO_TYPE IN ('ZP13', 'ZP23' , 'ZP56', 'ZP66')  and trim(M.so_id) is null;
         FROM IN_SFCHEADER M WHERE M.MO_TYPE IN ('ZP12')  and trim(M.so_id) is null;
   COMMIT;
--  UPDATE IN_MO M SET M.SO_ID=M.MO_TYPE|| '_' || M.MO_ID ,M.SO_LINE_ID=ITEM_ID,M.PEGGED_MO_ID=NULL WHERE M.MO_TYPE IN ('ZP13', 'ZP23' , 'ZP56', 'ZP66')  AND TRIM(M.SO_ID) IS NULL;
  UPDATE IN_MO M SET M.SO_ID=M.MO_TYPE|| '_' || M.MO_ID ,M.SO_LINE_ID=ITEM_ID,M.PEGGED_MO_ID=NULL WHERE M.MO_TYPE IN ('ZP12')  AND TRIM(M.SO_ID) IS NULL;
--  UPDATE IN_SFCHEADER M SET M.SO_ID=M.MO_TYPE|| '_' || M.MO_ID ,M.SO_LINE_ID=PRODUCT_ID,M.PEGGED_MO_ID=NULL WHERE M.MO_TYPE IN ('ZP13', 'ZP23' , 'ZP56', 'ZP66')  AND TRIM(M.SO_ID) IS NULL;
  UPDATE IN_SFCHEADER M SET M.SO_ID=M.MO_TYPE|| '_' || M.MO_ID ,M.SO_LINE_ID=PRODUCT_ID,M.PEGGED_MO_ID=NULL WHERE M.MO_TYPE IN ('ZP12')  AND TRIM(M.SO_ID) IS NULL;
  COMMIT;

     /*****************************2014/7/17 增加GB2828逻辑
   在赛纳因为存在破坏式检验半成品,需要依照GB2828标准多准备一些半成品供检验之用
   直接在SO中添加GB2828检验所需要的数量
   *************************************************/
   V_Step := V_Step + 1;
   V_Action := PKG_CONSTANT.CONS_ACTION_INSERT;
   V_MainTable := 'IN_GB2828';
   PKG_UTIL.CREATELOG (V_PKGName,                      --Put package name here
                       V_ProName,                    --Put procedure name here
                       V_Step,                      --Step seqence No. if have
                       V_Action,                      --Action 'I','D','U','T'
                       PKG_CONSTANT.CONS_MSG_INFO, --Message tppe, 'SUCCESS' or 'EXCEPTION'
                       'Start: load data into SALESORDER from IN_GB2828.', --Any generic information here
                       V_MainTable);              --Major table name processed

   INSERT INTO ABPPMGR.SALESORDERMASTER (SCENARIO_ID,
                                         ENTERPRISE,
                                         ENGINE_ID,
                                         SITEID,
                                         SALESNAME,
                                         SALESLEVEL,
                                         SALESORDERID,
                                         SYS_CREATED_BY,
                                         SYS_CREATION_DATE)
      SELECT DISTINCT V_ScenarioID,
                      V_Enterprise,
                      V_EngineID,
                      V_SiteID,
                      V_SalesLevel,
                      V_SalesName,
                      'GB2828_' || IM.SEMI_ID || '_' || IM.SITE_ID SO_ID,
                      V_ProName,
                      SYSDATE
        FROM IN_GB2828 IM;

   INSERT INTO ABPPMGR.SALESORDERLINE (SCENARIO_ID,
                                       ENTERPRISE,
                                       ENGINE_ID,
                                       SITEID,
                                       SALESNAME,
                                       SALESLEVEL,
                                       ORGANIZATIONID,
                                       ITEM,
                                       --REQUESTEDITEM,
                                       LOCATIONID,
                                       PROMISEDAVAILDATE,
                                       QTYOPEN,
                                       QTYUOM,
                                       SALESORDERID,
                                       SOLINENUM,
                                       UDF_CREATION_DATETIME,
                                       PRIORITY,
                                       SYS_CREATED_BY,
                                       SYS_CREATION_DATE)
      SELECT V_ScenarioID,
             V_Enterprise,
             V_EngineID,
             V_SiteID,
             V_SalesLevel,
             V_SalesName,
             V_OrganizationID,
             SEMI_ID ITEM,
             --SEMI_ID REQUESTEDITEM,
             SITE_ID LOCATIONID,
             SYSDATE,
             DEMAND_QTY,
             NULL QTYUOM,
             'GB2828_' || IM.SEMI_ID || '_' || IM.SITE_ID SO_ID,
             '10' SOLINEID,
             SYSDATE CREATION_DATETIME,
             25000,
             V_ProName,
             SYSDATE
        FROM IN_GB2828 IM;

   V_Step := V_Step + 1;
   V_Action := PKG_CONSTANT.CONS_ACTION_INSERT;
   V_MainTable := 'IN_GB2828';
   PKG_UTIL.CREATELOG (V_PKGName,                      --Put package name here
                       V_ProName,                    --Put procedure name here
                       V_Step,                      --Step seqence No. if have
                       V_Action,                      --Action 'I','D','U','T'
                       PKG_CONSTANT.CONS_MSG_INFO, --Message tppe, 'SUCCESS' or 'EXCEPTION'
                       'Success: ' || SQL%ROWCOUNT || ' rows inserted.', --Any generic information here
                       V_MainTable);              --Major table name processed
   COMMIT;

  /*
   第三段逻辑描述,处理规则等
   Populate abppmgr.SFCHEADER
  */                       
  V_Step := '3';
  V_Action := PKG_CONSTANT.CONS_ACTION_INSERT ;
  V_MainTable := 'SFCHEADER' ;

    PKG_UTIL.CREATELOG (V_PKGName,
                      V_ProName,
                      V_Step,
                      V_Action,
                      PKG_CONSTANT.CONS_MSG_INFO,
                      'START',
                      V_MainTable);

  INSERT INTO abppmgr.SFCHEADER
         ( ENTERPRISE
         , ENGINE_ID
         , BOMID
         , ITEM
         , PRODUCTIONORDERID
         , QTYORDERED
         , ROUTINGID
         , SITEID
         , PROPERTY
         , ISFEEDSLOCKED
         , ISQTYLOCKED
         , SYS_CREATED_BY
         )
   SELECT V_Enterprise
        , V_EngineId
        , ''|| LTRIM(PRODUCT_ID,'0') ||'_'|| TRIM(BOM_ID) BOM_ID
        , PRODUCT_ID
        , MO_ID
        , ON_LINE_QTY
        , ROUTING_ID
        , V_SITEID
        , PRODUCT_ID||'_'|| SITEID
        , 1 ISFEEDSLOCKED
        , 1 ISQTYLOCKED
        , V_ProName||'_'||V_Step
     FROM IN_SFCHEADER
  ; 

    UPDATE ABPPMGR.MST_ROUTINGOPERATION  RO SET RO.MINFIXEDRUNTIME=0,
    RO.SYS_LAST_MODIFIED_DATE=sysdate,RO.SYS_LAST_MODIFIED_BY=V_ProName||'_'||V_Step
  WHERE  EXISTS(SELECT NULL FROM IN_SFCHEADER MO ,IN_ITEM_SITE IIS 
    WHERE RO.ROUTINGID=MO.ROUTING_ID 
    AND MO.PLANNED_END_DATE<=SYSDATE AND IIS.UDF_ITEM_CATEGORY IN('ASS','SEMI')
    AND  MO.PRODUCT_ID=IIS.ITEM_ID AND MO.SITEID=IIS.SITE_ID);
    COMMIT;

  UPDATE ABPPMGR.MST_ROUTINGOPERATION  RO SET RO.MINFIXEDRUNTIME=1 *1440,
    RO.SYS_LAST_MODIFIED_DATE=sysdate,RO.SYS_LAST_MODIFIED_BY=V_ProName||'_'||V_Step
  WHERE  EXISTS(SELECT NULL FROM IN_SFCHEADER MO ,IN_ITEM_SITE IIS 
    WHERE RO.ROUTINGID=MO.ROUTING_ID 
    AND MO.PLANNED_END_DATE<=SYSDATE AND IIS.UDF_ITEM_CATEGORY IN('ASS','SEMI')
    AND  MO.PRODUCT_ID=IIS.ITEM_ID AND MO.SITEID=IIS.SITE_ID)
          AND NOT EXISTS(SELECT NULL FROM ABPPMGR.MST_ROUTINGOPERATION RR 
          WHERE RR.ROUTINGID=RO.ROUTINGID AND RR.OPERATIONSEQ< RO.OPERATIONSEQ);
  COMMIT;

  /*
  --ADD BY HUANGYANGXIONG ON 20151012
  UPDATE ABPPMGR.MST_ROUTINGOPERATION  RO SET RO.MINFIXEDRUNTIME=(SELECT DECODE(RM.ZDAYS,0,1,RM.ZDAYS)*1440 FROM IN_SFCHEADER MO ,IN_ITEM_SITE IIS ,STG.SAP_ROUTING_MANUAL RM
    WHERE RO.ROUTINGID=MO.ROUTING_ID 
    AND MO.PLANNED_END_DATE<=SYSDATE AND IIS.UDF_ITEM_CATEGORY IN('ASS','SEMI')
    AND  MO.PRODUCT_ID=IIS.ITEM_ID AND MO.SITEID=IIS.SITE_ID
    AND IIS.ITEM_ID = RM.MATNR AND IIS.SITE_ID = RM.WERKS
    )
  WHERE  NOT EXISTS(SELECT NULL FROM ABPPMGR.MST_ROUTINGOPERATION RR 
          WHERE RR.ROUTINGID=RO.ROUTINGID AND RR.OPERATIONSEQ< RO.OPERATIONSEQ);
  COMMIT;
  */

  INSERT INTO ABPPMGR.SFCHEADERPROPERTY(
        ENTERPRISE
         , ENGINE_ID
         , SITEID
         , ATTRIBUTECLASS
         , ATTRIBUTE
         , QTYORDERED
         , PRODUCTIONORDERID
         , RELATIONSHIP
         , VALUE
         , SYS_CREATED_BY)
  select 
  ENTERPRISE
         , ENGINE_ID
         , SITEID
         , PROPERTY ATTRIBUTECLASS
         , 'FACTORY' ATTRIBUTE
         , QTYORDERED
         , PRODUCTIONORDERID
         , 'EQ' RELATIONSHIP
         , substr(PROPERTY,-4) VALUE
         , SYS_CREATED_BY from abppmgr.SFCHEADER;
  /*
   第三段逻辑处理结束,记录日志
  */
   PKG_UTIL.CREATELOG (V_PKGName,
                       V_ProName,
                       V_Step,
                       V_Action,
                       PKG_CONSTANT.CONS_MSG_INFO,
                       'Record Count: '|| SQL%ROWCOUNT,
                       V_MainTable);

commit;


    ---ADD BY Landor ON 20180522 按单方案更新补充处理SFCHEADERPROPERTY.ATTRIBUTECLASS字段值
   UPDATE ABPPMGR.SFCHEADERPROPERTY AY SET AY.ATTRIBUTECLASS=AY.PRODUCTIONORDERID||':'||AY.VALUE;
   COMMIT;

  /*
   第四段逻辑描述,处理规则等
   Populate abppmgr.SFCLINE
   OPERATIONSEQ: minimum OperationSeq for the Routing should be 
    retrieved from Operation table
  */                       
  V_Step := '4';
  V_Action := PKG_CONSTANT.CONS_ACTION_INSERT ;
  V_MainTable := 'SFCLINE' ;

    PKG_UTIL.CREATELOG (V_PKGName,
                      V_ProName,
                      V_Step,
                      V_Action,
                      PKG_CONSTANT.CONS_MSG_INFO,
                      'START',
                      V_MainTable);

  INSERT INTO abppmgr.SFCLINE
         ( ENTERPRISE
         , ENGINE_ID
         , OPERATIONSEQ
         --, ACTUALENDDATE
         , PRODUCTIONORDERID
         , SITEID
         ,QTYINRUN
         , PROPERTY
         , SYS_CREATED_BY
         )
   SELECT V_Enterprise
        , V_EngineId
        , NVL(op.OPERATIONSEQ,10) OPERATIONSEQ
        --, CASE WHEN SL.PLANNED_END_DATE<=SYSDATE THEN SYSDATE +3 ELSE  SL.PLANNED_END_DATE END  PLANNED_END_DATE
        , sl.MO_ID
        ,V_SITEID
        , sl.UDF_REQ_QTY
        , sl.ITEM_ID||'_'||sl.SITEID
        , V_ProName||'_'||V_Step
     FROM (select MAX(PLANNED_END_DATE) PLANNED_END_DATE,MO_ID,ROUTING_ID, SITEID,ITEM_ID ,sum(UDF_REQ_QTY) UDF_REQ_QTY
     from IN_SFCLINE group by MO_ID,ROUTING_ID, SITEID,ITEM_ID ) sl, 
          (SELECT ROUTING_ID,
               SITEID,
               min(OPERATIONSEQ) OPERATIONSEQ
          FROM IN_ROUTINGOPERATION group by ROUTING_ID,
               SITEID) op
    WHERE sl.ROUTING_ID = op.ROUTING_ID(+)
      AND sl.SITEID = op.SITEID(+)
  ; 

  V_Step := '4';
  V_Action := PKG_CONSTANT.CONS_ACTION_INSERT ;
  V_MainTable := 'SFCLINEPROPERTY' ;

    PKG_UTIL.CREATELOG (V_PKGName,
                      V_ProName,
                      V_Step,
                      V_Action,
                      PKG_CONSTANT.CONS_MSG_INFO,
                      'START',
                      V_MainTable);
insert into abppmgr.SFCLINEPROPERTY( ENTERPRISE
         , ENGINE_ID
         , SITEID
         , ATTRIBUTECLASS
         , ATTRIBUTE
         , QTYORDERED
         , PRODUCTIONORDERID
         , OPERATIONSEQ
         , RELATIONSHIP
         , VALUE
         , SYS_CREATED_BY)
select   ENTERPRISE
         , ENGINE_ID
         , SITEID
         , PROPERTY ATTRIBUTECLASS
         , 'FACTORY' ATTRIBUTE
         , QTYINRUN
         , PRODUCTIONORDERID
         , OPERATIONSEQ
         ,'EQ' RELATIONSHIP
         , substr(PROPERTY,-4) VALUE
         , SYS_CREATED_BY from abppmgr.SFCLINE sl where not exists (select null from IN_SEMI_MO_LOCKED ML 
         where SL.PRODUCTIONORDERID=ML.MO_ID)
 UNION
    SELECT   SL.ENTERPRISE
         , SL.ENGINE_ID
         , SL.SITEID
         , ML.MO_ID || '_' ||ML.PEGGED_SO_ID||ML.PEGGED_SO_LINE_ID ATTRIBUTECLASS
         , 'FACTORY' ATTRIBUTE
         , SL.QTYINRUN
         , SL.PRODUCTIONORDERID
         , SL.OPERATIONSEQ
         ,'EQ' RELATIONSHIP
         --, ML.PEGGED_SO_ID||ML.PEGGED_SO_LINE_ID VALUE --hide by lvxuefen on 2017.07.28
         ,ML.SITEID  VALUE --alter by lvxuefen on 2017.07.28 验货及VIP带SO的半成品MO无法分配给对应的SO问题
         , SL.SYS_CREATED_BY FROM ABPPMGR.SFCLINE SL, IN_SEMI_MO_LOCKED ML
         WHERE SL.PRODUCTIONORDERID=ML.MO_ID;         

  /*
   第四段逻辑处理结束,记录日志
  */
   PKG_UTIL.CREATELOG (V_PKGName,
                       V_ProName,
                       V_Step,
                       V_Action,
                       PKG_CONSTANT.CONS_MSG_INFO,
                       'Record Count: '|| SQL%ROWCOUNT,
                       V_MainTable);
    COMMIT;

  /*
   第五段逻辑描述,处理规则等
   Populate abppmgr.SUPPLYDMDPEGGING for PEGGED_MO_ID is Null
  */       

  V_Step := '5';
  V_Action := PKG_CONSTANT.CONS_ACTION_INSERT ;
  V_MainTable := 'SUPPLYDMDPEGGING' ;

    PKG_UTIL.CREATELOG (V_PKGName,
                      V_ProName,
                      V_Step,
                      V_Action,
                      PKG_CONSTANT.CONS_MSG_INFO,
                      'START',
                      V_MainTable);

        INSERT INTO TEMP_MO_PEG_SO (SALESORDERID,
                                    SOLINENUM,
                                    QTYOPEN,
                                    SO_LINE_ID,
                                    SUMQTY,
                                    RN,
                                    RC)
           SELECT SO.SALESORDERID,
                  SO.SOLINENUM,
                  SO.QTYOPEN,
                  DECODE (INSTR (SO.SOLINENUM, '_'),
                          0, SO.SOLINENUM,
                          SUBSTR (SO.SOLINENUM, 1, INSTR (SO.SOLINENUM, '_') - 1))
                     SO_LINE_ID,
                  SUM (
                     SO.QTYOPEN)
                  OVER (
                     PARTITION BY SO.SALESORDERID,
                                  DECODE (
                                     INSTR (SO.SOLINENUM, '_'),
                                     0, SO.SOLINENUM,
                                     SUBSTR (SO.SOLINENUM,
                                             1,
                                             INSTR (SO.SOLINENUM, '_') - 1))
                     ORDER BY SO.SOLINENUM
                     ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
                     SUMQTY,
                  ROW_NUMBER ()
                  OVER (
                     PARTITION BY SO.SALESORDERID,
                                  DECODE (
                                     INSTR (SO.SOLINENUM, '_'),
                                     0, SO.SOLINENUM,
                                     SUBSTR (SO.SOLINENUM,
                                             1,
                                             INSTR (SO.SOLINENUM, '_') - 1))
                     ORDER BY SO.SOLINENUM)
                     RN,
                  COUNT (
                     SO.SOLINENUM)
                  OVER (
                     PARTITION BY SO.SALESORDERID,
                                  DECODE (
                                     INSTR (SO.SOLINENUM, '_'),
                                     0, SO.SOLINENUM,
                                     SUBSTR (SO.SOLINENUM,
                                             1,
                                             INSTR (SO.SOLINENUM, '_') - 1))
                     ORDER BY SO.SOLINENUM)
                     RC
             FROM ABPPMGR.SALESORDERLINE SO
            WHERE EXISTS
                     (SELECT NULL FROM
                        (SELECT S.SO_LINE_ID, S.SO_ID
                     FROM IN_SFCHEADER S
                    WHERE TRIM (S.PEGGED_MO_ID) IS NULL
                          AND TRIM (S.SO_ID) IS NOT NULL
                   UNION ALL
                   SELECT O.SO_LINE_ID, O.SO_ID
                     FROM IN_MO O
                    WHERE TRIM (O.PEGGED_MO_ID) IS NULL
                          AND TRIM (O.SO_ID) IS NOT NULL) M 
                          WHERE SO.ENTERPRISE = V_Enterprise AND SO.SALESORDERID = M.SO_ID
                  AND DECODE (
                         INSTR (SO.SOLINENUM, '_'),
                         0, SO.SOLINENUM,
                         SUBSTR (SO.SOLINENUM,
                                 1,
                                 INSTR (SO.SOLINENUM, '_') - 1)) =
                         M.SO_LINE_ID);

        COMMIT;

      ---半成品没有绑定成品工单PEGGED_MO_ID IS NULL 
      FOR C_MO IN (SELECT MO_ID,ITEM_ID,SO_ID,SO_LINE_ID,SITEID,PLANNED_QTY FROM (SELECT M.PLANNED_QTY,M.MO_ID,M.ITEM_ID,TRIM (M.SO_ID) SO_ID, M.SO_LINE_ID,M.SITEID,2 PRIORITY
                            FROM IN_MO M WHERE  TRIM (M.PEGGED_MO_ID) IS NULL  ---半成品无绑定成品工单
                            AND TRIM (M.SO_ID) IS NOT NULL
                            UNION ALL
                           SELECT M.ON_LINE_QTY PLANNED_QTY,M.MO_ID,M.PRODUCT_ID ITEM_ID,TRIM (M.SO_ID) SO_ID, M.SO_LINE_ID ,M.SITEID,1 PRIORITY
                            FROM IN_SFCHEADER M WHERE  TRIM (M.PEGGED_MO_ID) IS NULL
                            AND TRIM (M.SO_ID) IS NOT NULL
                            AND EXISTS (SELECT NULL FROM IN_SALES_ORDER SO WHERE M.SO_ID = SO.SO_ID AND M.SO_LINE_ID = SUBSTR(SO.SO_LINE_ID,1,INSTR(SO.SO_LINE_ID,'_')-1) AND M.PRODUCT_ID = SO.ITEM_ID)
                            ) WHERE MO_ID LIKE '0011%' --解决CRTD重复规划问题(必须是已开左成品工单,只开半成品工单但未开成品工单的不参与此方法)  add by landor on 20180427
                            ORDER BY PRIORITY)LOOP
                            V_QTY:=C_MO.PLANNED_QTY;
        FOR C_SO IN (SELECT SALESORDERID,
                                        SOLINENUM,
                                        QTYOPEN,
                                        SO_LINE_ID,
                                        SUMQTY,
                                        RN,
                                        RC FROM TEMP_MO_PEG_SO SO
     WHERE SO.SALESORDERID = C_MO.SO_ID
        AND SO.SO_LINE_ID =C_MO.SO_LINE_ID AND SO.QTYOPEN>0 ORDER BY RN
        ) LOOP 
             V_QTY:=V_QTY-C_SO.QTYOPEN;

             UPDATE TEMP_MO_PEG_SO SO SET SO.QTYOPEN =CASE WHEN V_QTY>0 THEN 0 ELSE ABS(V_QTY) END WHERE  SO.SALESORDERID=C_SO.SALESORDERID AND SO.SOLINENUM=C_SO.SOLINENUM;
             IF(C_SO.QTYOPEN+V_QTY)>0 THEN
                 INSERT INTO ABPPMGR.SUPPLYDMDPEGGING
                 ( ENTERPRISE
                 , ENGINE_ID
                 , DEMANDLINEID
                 , DEMANDORDERID
                 , DEMANDTYPE
                 , ITEM
                 , QTYALLOCATED
                 , SITEID
                 , SUPPLYLINEID
                 , SUPPLYORDERID
                 , SUPPLYTYPE
                 , ISDEMANDLOCKED
                 , SYS_CREATED_BY
                 )
                  VALUES( V_ENTERPRISE
                , V_ENGINEID
                , C_SO.SOLINENUM
                , C_SO.SALESORDERID
                , 'SO'
                , C_MO.ITEM_ID
                , CASE WHEN V_QTY>0 THEN C_SO.QTYOPEN ELSE C_SO.QTYOPEN+V_QTY END
                , V_SITEID
                , NULL
                , C_MO.MO_ID
                , 'MFG'
                , '1' 
                , V_PRONAME||'_'||V_STEP);
            END IF;
            COMMIT;
            IF V_QTY<=0 THEN
                EXIT;
            END IF;
         END LOOP;
      END LOOP;
      COMMIT;
  /*
   第五段逻辑处理结束,记录日志
  */
   PKG_UTIL.CREATELOG (V_PKGName,
                       V_ProName,
                       V_Step,
                       V_Action,
                       PKG_CONSTANT.CONS_MSG_INFO,
                       'Record Count: '|| SQL%ROWCOUNT,
                       V_MainTable);

INSERT INTO ABPPMGR.SUPPLYDMDPEGGING
                 ( ENTERPRISE
                 , ENGINE_ID
                 , DEMANDLINEID
                 , DEMANDORDERID
                 , DEMANDTYPE
                 , ITEM
                 , QTYALLOCATED
                 , SITEID
                 , SUPPLYLINEID
                 , SUPPLYORDERID
                 , SUPPLYTYPE
                 , ISDEMANDLOCKED
                 , SYS_CREATED_BY
                 )
           select V_ENTERPRISE
                , V_ENGINEID
                , '10' SOLINEID
                ,  'GB2828_' || IM.SEMI_ID || '_' || IM.SITE_ID SO_ID
                , 'SO'
                ,  IM.SEMI_ID ITEM
                , IM.DEMAND_QTY
                , V_SITEID
                , NULL
                , 'GB2828_' || IM.SEMI_ID || '_' || IM.SITE_ID||'/10-MFG000'
                , 'MFG'
                , '1' 
                , V_PRONAME||'_'||V_STEP     
        FROM IN_GB2828 IM;
       COMMIT;

  /*
   第六段逻辑描述,处理规则等   半成品有绑定成品工单 PEGGED_MO_ID is NOT Null
   Populate abppmgr.SUPPLYDMDPEGGING for PEGGED_MO_ID is NOT Null
  */                       
  V_Step := '6';
  V_Action := PKG_CONSTANT.CONS_ACTION_INSERT ;
  V_MainTable := 'SUPPLYDMDPEGGING' ;

    PKG_UTIL.CREATELOG (V_PKGName,
                      V_ProName,
                      V_Step,
                      V_Action,
                      PKG_CONSTANT.CONS_MSG_INFO,
                      'START',
                      V_MainTable);

  INSERT INTO abppmgr.SUPPLYDMDPEGGING
         ( ENTERPRISE
         , ENGINE_ID
         , DEMANDLINEID
         , DEMANDORDERID
         , DEMANDTYPE
         , ITEM
         , QTYALLOCATED
         , SITEID
         , SUPPLYLINEID
         , SUPPLYORDERID
         , SUPPLYTYPE
         , ISDEMANDLOCKED
         , SYS_CREATED_BY
         )
   SELECT V_Enterprise
        , V_EngineId
        , NULL
        , PEGGED_MO_ID
        , 'MFG'
        , ITEM_ID
        , PLANNED_QTY
        , V_SITEID
        , NULL
        , MO_ID
        , 'MFG'
        , '1' ISDEMANDLOCKED
        , V_ProName||'_'||V_Step
     FROM IN_MO
    WHERE trim(PEGGED_MO_ID) IS NOT NULL and PLANNED_QTY>0
    UNION ALL
     SELECT V_Enterprise
        , V_EngineId
        , NULL
        , PEGGED_MO_ID
        , 'MFG'
        , PRODUCT_ID
        , ON_LINE_QTY
        , V_SITEID
        , NULL
        , MO_ID
        , 'MFG'
        , '1' ISDEMANDLOCKED
        , V_ProName||'_'||V_Step
     FROM IN_SFCHEADER
    WHERE trim(PEGGED_MO_ID) IS NOT NULL and ON_LINE_QTY>0; 

--将数据进行软绑
MERGE INTO ABPPMGR.SUPPLYDMDPEGGING M
     USING (SELECT MO.MO_ID, SI.UDF_ITEM_SOFTLOCK
              FROM IN_MO MO, IN_ITEM_SITE SI
             WHERE MO.ITEM_ID = SI.ITEM_ID
                   AND SUBSTR (MO.BOM_ID,
                               INSTR (MO.BOM_ID,
                                      '_')
                               + 1,
                               4) = SI.SITE_ID) IM
        ON (M.SUPPLYORDERID = IM.MO_ID)
WHEN MATCHED
THEN
   UPDATE SET M.ISDEMANDLOCKED = DECODE (IM.UDF_ITEM_SOFTLOCK, '0', '1');
  /*
   第六段逻辑处理结束,记录日志
  */
   PKG_UTIL.CREATELOG (V_PKGName,
                       V_ProName,
                       V_Step,
                       V_Action,
                       PKG_CONSTANT.CONS_MSG_INFO,
                       'Record Count: '|| SQL%ROWCOUNT,
                       V_MainTable);


  /*
   Execute Final Commit.
  */

COMMIT ;
   /*
开始执行寻源逻辑 IN_SOURCING
*/
    FOR C_SOURCING IN(SELECT SO.SO_ID, SO.SO_LINE_ID,SO.SITEID,SO.CUSTOMER_ID,SO.ITEM_ID,SO.ORDERED_QTY
    --,sc.FINISH_ITEM_ID,sc.START_LOCATION_ID||',' locations
    ,sc.FINISH_ITEM_ID,trim(sc.SOURCING_SEQUENCE_ID)||',' locations
    ,row_number() over(partition by SO.SO_ID, SO.SO_LINE_ID order by TRIM(SC.FINISH_ITEM_ID) NULLS LAST) RN
     FROM IN_SALES_ORDER SO , IN_SOURCING SC, ABPPMGR.SALESORDERLINE SOL
    WHERE SOL.SALESORDERID=SO.so_id and SOL.solinenum=so.so_line_id and
    SC.CUSTOMER_ID=SO.CUSTOMER_ID AND SO.ITEM_id LIKE TRIM(SC.FINISH_ITEM_ID)||'%'
    order by SOL.PRIORITY desc)LOOP
    IF C_SOURCING.RN=1 THEN
     FOR iCount in 1.. length(C_SOURCING.locations)-length(replace(C_SOURCING.locations,',',null)) loop
     --按照“,”出现的次数进行循环
     if iCount=1 then 
     --第一次循环取第一个location
     v_locs:=C_SOURCING.locations;
     else
     --第二次起开始截取location
      v_locs:=substr(v_locs,instr(v_locs,',')+1);
     end if;
     v_loc:=substr(v_locs,1,instr(v_locs,',')-1);
     SELECT nvl(max(inv.QTYAVAILABLE-nvl(inv.QTYALLOCATED,0)),0) into v_OhQty FROM ABPPMGR.INVENTORY INV 
      where INV.item=C_SOURCING.item_id and INV.locationid=v_loc;
      if v_OhQty>0 then
            insert into abppmgr.SUPPLYDMDPEGGING
             ( ENTERPRISE
             , ENGINE_ID
             , DEMANDLINEID
             , DEMANDORDERID
             , DEMANDTYPE
             , ITEM
             , QTYALLOCATED
             , SITEID
             , LOCATIONID
             , LOTLEVEL1
             , LOTLEVEL2
             , SERIALNUM
              ,operationseq
             , SUPPLYORDERID
             , SUPPLYTYPE
             , ISDEMANDLOCKED
             , SYS_CREATED_BY
             ) 
           values( V_Enterprise
            , V_EngineId
             , C_SOURCING.SO_LINE_ID
             , C_SOURCING.SO_ID
             , 'SO'
             , C_SOURCING.ITEM_ID
             , case when C_SOURCING.ORDERED_QTY - v_OhQty >= 0 then v_OhQty else C_SOURCING.ORDERED_QTY end
             , V_SITEID
             , v_loc
            ,  substr(v_loc,1,4) 
            ,  substr(v_loc,6)
             , null
             , null
             , NULL
             , 'OH'
             , '1'
             ,  V_ProName||'_'||V_Step
             );
          update ABPPMGR.INVENTORY INV set   inv.QTYALLOCATED =nvl(inv.QTYALLOCATED,0)+case when C_SOURCING.ORDERED_QTY - v_OhQty >= 0 then v_OhQty else C_SOURCING.ORDERED_QTY end
          --case when v_OhQty- C_SOURCING.ORDERED_QTY <=0 then v_OhQty else v_OhQty- C_SOURCING.ORDERED_QTY end
            where INV.item=C_SOURCING.item_id and INV.locationid=v_loc;
       commit;
      C_SOURCING.ORDERED_QTY:=  C_SOURCING.ORDERED_QTY - v_OhQty;
       if   C_SOURCING.ORDERED_QTY<=0 then
        exit; 
       end if;
      end if;
     end loop;
    END IF;
     NULL;
    END LOOP;

--删除寻源的库存属性
DELETE FROM ABPPMGR.INVENTORYPROPERTY VP 
WHERE EXISTS (SELECT NULL FROM IN_LOCATION LC WHERE LC.LOCATION_ID = VP.LOCATIONID);
COMMIT;

--将SO要使用的量带入FP
UPDATE ABPPMGR.INVENTORY INV SET  INV.QTYAVAILABLE= INV.QTYALLOCATED WHERE  INV.QTYALLOCATED IS NOT NULL
AND EXISTS (SELECT NULL FROM IN_LOCATION LC WHERE LC.LOCATION_ID = INV.LOCATIONID);
COMMIT;
--将没有被使用到的库存删除,不使其进FP
delete from ABPPMGR.INVENTORY INV  WHERE  INV.QTYALLOCATED IS NULL
AND EXISTS (SELECT NULL FROM IN_LOCATION LC WHERE LC.LOCATION_ID = INV.LOCATIONID);
COMMIT;
--将临时数据字段进行清理
UPDATE ABPPMGR.INVENTORY INV SET  INV.QTYALLOCATED=NULL WHERE  
 EXISTS (SELECT NULL FROM IN_LOCATION LC WHERE LC.LOCATION_ID = INV.LOCATIONID);
COMMIT;

if 1=1 then
  --add for SEMI item in stock peg to SO
  --already has MO
 INSERT INTO abppmgr.SUPPLYDMDPEGGING
         ( ENTERPRISE
         , ENGINE_ID
         , DEMANDLINEID
         , DEMANDORDERID
         , DEMANDTYPE
         , ITEM
         , QTYALLOCATED
         , SITEID
         , LOCATIONID
         , LOTLEVEL1
         , LOTLEVEL2
         , SERIALNUM
          ,operationseq
         , SUPPLYORDERID
         , SUPPLYTYPE
         , ISDEMANDLOCKED
         , SYS_CREATED_BY
         )
   SELECT distinct V_Enterprise
        , V_EngineId
        , NULL
        , MO.MO_ID
        , 'MFG'
        , OH.ITEM_ID
        , OH.QUANTITY
        , V_SITEID
        ,OH.LOCATION
        ,  substr(OH.LOCATION,1,4) LOTLEVEL1
        ,  substr(OH.LOCATION,6) LOTLEVEL2
        , OH.PEGGED_SO_ID||'/'||OH.PEGGED_SO_LINE_ID SERIALNUM
       , (SELECT B.OPERATIONSEQ FROM ABPPMGR.MST_BOMCOMPONENTS B WHERE B.BOMID like '%' || MO.bom_id
       AND B.ITEM=OH.ITEM_ID
       AND MO.ROUTING_ID=B.ROUTINGID AND ROWNUM=1) OPERATIONSEQ
        , NULL
        , 'OH'
        , '1' ISDEMANDLOCKED
        , V_ProName||'_'||V_Step
     FROM (SELECT IN_MO.MO_ID,IN_MO.BOM_ID,IN_MO.ROUTING_ID,IN_MO.SO_ID,IN_MO.SO_LINE_ID FROM IN_MO
          UNION ALL
          SELECT IN_SFCHEADER.MO_ID,IN_SFCHEADER.BOM_ID,IN_SFCHEADER.ROUTING_ID,IN_SFCHEADER.SO_ID,IN_SFCHEADER.SO_LINE_ID FROM IN_SFCHEADER ) MO ,IN_SEMI_ONHAND_LOCKED OH
    WHERE MO.SO_ID=OH.PEGGED_SO_ID AND MO.SO_LINE_ID=OH.PEGGED_SO_LINE_ID
            AND MO.MO_ID LIKE '0011%' --add by landor on 20180704 半成品工单0012不需要作为需求
            ;

  COMMIT;

  --no MO yet
   INSERT ALL
   when SOCOUNT =1 then INTO abppmgr.SUPPLYDMDPEGGING
         ( ENTERPRISE
         , ENGINE_ID
         , DEMANDLINEID
         , DEMANDORDERID
         , DEMANDTYPE
         , ITEM
         , QTYALLOCATED
         , SITEID
         , LOCATIONID
         , SUPPLYORDERID
         , SUPPLYTYPE
         , ISDEMANDLOCKED
         , SYS_CREATED_BY
         ) 
         values( ENTERPRISE
         , ENGINE_ID
         , SO_LINE_ID
         , SO_ID
         , 'SO'
         , PRODUCT_ITEM
         , QTYORDERED
         , SITEID
         , NULL 
         , PRODUCTIONORDID
         , 'MFG'
         , '1'
         , SYS_CREATED_BY
         )
   when SOCOUNT =1 then INTO abppmgr.PRODUCTIONORDERS
         ( ENTERPRISE
         , ENGINE_ID
         , BOMID
         , ITEM
         , PLANNEDSTARTDATE
         , PRODUCTIONORDID
         , QTYORDERED
         , REQUIREDDATE
         , ROUTINGID
         , SITEID
         , SYS_CREATED_BY
         ,ISFEEDSLOCKED
         ,ISQTYLOCKED
         ,ISROUTINGLOCKED
         )
         values(ENTERPRISE
         , ENGINE_ID
         , BOMID
         , PRODUCT_ITEM
         , PLANNEDSTARTDATE
         , PRODUCTIONORDID
         , QTYORDERED
         , REQUIREDDATE
         , ROUTINGID
         , SITEID
         , SYS_CREATED_BY
         ,ISFEEDSLOCKED
         ,ISQTYLOCKED
         ,ISROUTINGLOCKED)
     when SOCOUNT is not null then  INTO abppmgr.SUPPLYDMDPEGGING
         ( ENTERPRISE
         , ENGINE_ID
         , DEMANDLINEID
         , DEMANDORDERID
         , DEMANDTYPE
         , ITEM
         , QTYALLOCATED
         , SITEID
         , LOCATIONID
         , LOTLEVEL1
         , LOTLEVEL2
          , SERIALNUM
          ,operationseq
         , SUPPLYORDERID
         , SUPPLYTYPE
         , ISDEMANDLOCKED
         , SYS_CREATED_BY
         ) 
         values( ENTERPRISE
         , ENGINE_ID
         , NULL
         , PRODUCTIONORDID
         , 'MFG'
         , SEMI_ITEM
         , ONHAND_QTY
         , SITEID
         , LOCATIONID
         ,LOTLEVEL1
         ,LOTLEVEL2
         , SERIALNUM
         , operationseq
         , NULL
         , 'OH'
         , '1'
         , SYS_CREATED_BY
         )
         SELECT ENTERPRISE
         , ENGINE_ID
         , BOMID
         , SO_ID
         , SO_LINE_ID
         , SEMI_ITEM
         , PRODUCT_ITEM
         , PLANNEDSTARTDATE
         , PRODUCTIONORDID
         , QTYORDERED
         , ONHAND_QTY
         , REQUIREDDATE
         , ROUTINGID
         , SITEID
         , SYS_CREATED_BY
         ,ISFEEDSLOCKED
         ,ISQTYLOCKED
         ,LOCATIONID
         ,LOTLEVEL1
         ,LOTLEVEL2
          , SERIALNUM
          , operationseq
         ,ISROUTINGLOCKED,
         SOCOUNT from (
   SELECT V_ENTERPRISE ENTERPRISE
        , V_ENGINEID ENGINE_ID
        , SO.SO_ID
        , SO.SO_LINE_ID
        , DECODE(TRIM(BR.BOM_ID),NULL,NULL,''|| LTRIM(SO.ITEM_ID,'0') ||'_'|| TRIM(BR.BOM_ID)) BOMID
        , SO.ITEM_ID PRODUCT_ITEM
        ,OH.ITEM_ID SEMI_ITEM
        , SYSDATE PLANNEDSTARTDATE
        , OH.PEGGED_SO_ID||'/'||SO.SO_LINE_ID||'-'||'MFG000' PRODUCTIONORDID
        , SO.ORDERED_QTY QTYORDERED
        , OH.QUANTITY ONHAND_QTY
        , NVL(SO.UDF_PROMD,SO.UDF_CRSD) REQUIREDDATE
        , BR.ROUTING_ID ROUTINGID
        , V_SITEID SITEID
        , OH.LOCATION LOCATIONID
        ,  substr(OH.LOCATION,1,4) LOTLEVEL1
        ,  substr(OH.LOCATION,6) LOTLEVEL2
       , OH.PEGGED_SO_ID||'/'||SO.SO_LINE_ID SERIALNUM
       , (select b.operationseq from abppmgr.mst_bomcomponents b where b.bomid=
       DECODE(TRIM(BR.BOM_ID),NULL,NULL,''|| LTRIM(SO.ITEM_ID,'0') ||'_'|| TRIM(BR.BOM_ID)) 
       and b.item=OH.ITEM_ID
       and BR.ROUTING_ID=b.routingid and rownum=1) operationseq
        , OH.ITEM_ID
        , V_PRONAME||'_'||V_STEP SYS_CREATED_BY
        ,'1' ISFEEDSLOCKED
        ,'1' ISQTYLOCKED
        ,'1' ISROUTINGLOCKED,
        ROW_NUMBER() OVER(PARTITION BY OH.PEGGED_SO_ID,OH.PEGGED_SO_LINE_ID ORDER BY SO.SO_LINE_ID desc) SOCOUNT,
        ROW_NUMBER() OVER(PARTITION BY OH.PEGGED_SO_ID,OH.PEGGED_SO_LINE_ID,OH.ITEM_ID,OH.LOCATION ORDER BY SO.SO_LINE_ID) RN
     FROM IN_SEMI_ONHAND_LOCKED OH,IN_SALES_ORDER SO,IN_ITEMBOMROUTING BR WHERE
     OH.PEGGED_SO_ID=SO.SO_ID AND OH.PEGGED_SO_LINE_ID=SUBSTR(SO.SO_LINE_ID,1,INSTR(SO.SO_LINE_ID,'_')-1)
     AND BR.PRODUCED_ITEM_ID=SO.ITEM_ID
     AND BR.siteid=SO.siteid
     AND EXISTS(SELECT NULL FROM IN_BOM_HEADER BH WHERE BH.BOM_ID=BR.BOM_ID AND BH.MO_BOM_MARK='0')
     AND  NOT EXISTS (SELECT NULL FROM IN_MO MO
      WHERE MO.SO_ID=OH.PEGGED_SO_ID AND MO.SO_LINE_ID=OH.PEGGED_SO_LINE_ID)
     AND  NOT EXISTS (SELECT NULL FROM IN_SFCHEADER MO
      WHERE MO.SO_ID=OH.PEGGED_SO_ID AND MO.SO_LINE_ID=OH.PEGGED_SO_LINE_ID) ) WHERE RN=1;
    COMMIT;

  --add by huangyangxiong on 20150314
  --has ASS MO  assembly mo 装配MO    即有半成品工单
  /*
   INSERT INTO abppmgr.SUPPLYDMDPEGGING
         ( ENTERPRISE
         , ENGINE_ID
         , DEMANDLINEID
         , DEMANDORDERID
         , DEMANDTYPE
         , ITEM
         , QTYALLOCATED
         , SITEID
         , SUPPLYORDERID
         , SUPPLYTYPE
         , ISDEMANDLOCKED
         , SYS_CREATED_BY
         ) 
         SELECT ENTERPRISE
         , ENGINE_ID
         , SO_LINE_ID
         , SO_ID
         ,'SO' AS DEMANDTYPE
         --, SEMI_ITEM 
         , PRODUCT_ITEM
         , QTYORDERED
         , SITEID
        -- , PLANNEDSTARTDATE
         , PRODUCTIONORDID
         ,'MFG' AS SUPPLYTYPE
         --, SEMI_QTY
        -- , SEMI_MO
        -- , REQUIREDDATE
       --  , ROUTINGID
        ,ISFEEDSLOCKED  
        , SYS_CREATED_BY

        -- ,ISQTYLOCKED
        -- ,ISROUTINGLOCKED
         --, SOCOUNT 
         from (
   SELECT DISTINCT V_ENTERPRISE ENTERPRISE
        , V_ENGINEID ENGINE_ID
        , SO.SO_ID
        , SO.SO_LINE_ID
        , DECODE(TRIM(BR.BOM_ID),NULL,NULL,''|| LTRIM(SO.ITEM_ID,'0') ||'_'|| TRIM(BR.BOM_ID)) BOMID
        , SO.ITEM_ID PRODUCT_ITEM
        , SYSDATE PLANNEDSTARTDATE
        , MO.PEGGED_MO_ID PRODUCTIONORDID
        , SO.ORDERED_QTY QTYORDERED
        , MO.QUANTITY SEMI_QTY
        , MO.MO_ID SEMI_MO
        , NVL(SO.UDF_PROMD,SO.UDF_CRSD) REQUIREDDATE
        , BR.ROUTING_ID ROUTINGID
        , V_SITEID SITEID
        , MO.ITEM_ID SEMI_ITEM
        , V_PRONAME||'_'||V_STEP SYS_CREATED_BY
        ,'1' ISFEEDSLOCKED
        ,'1' ISQTYLOCKED
        ,'1' ISROUTINGLOCKED,
        ROW_NUMBER() OVER(PARTITION BY MO.PEGGED_SO_ID,MO.PEGGED_SO_LINE_ID ORDER BY SO.SO_LINE_ID desc) SOCOUNT,
        ROW_NUMBER() OVER(PARTITION BY MO.PEGGED_SO_ID,MO.PEGGED_SO_LINE_ID,MO.MO_ID ORDER BY SO.SO_LINE_ID desc) RN
     FROM IN_SEMI_MO_LOCKED MO,IN_SALES_ORDER SO,IN_ITEMBOMROUTING BR WHERE
     MO.PEGGED_SO_ID=SO.SO_ID AND MO.PEGGED_SO_LINE_ID=SUBSTR(SO.SO_LINE_ID,1,INSTR(SO.SO_LINE_ID,'_')-1)
     AND BR.PRODUCED_ITEM_ID=SO.ITEM_ID and MO.PEGGED_MO_ID is not null
     AND BR.siteid=SO.siteid
     AND EXISTS(SELECT NULL FROM IN_BOM_HEADER BH WHERE BH.BOM_ID=BR.BOM_ID AND BH.MO_BOM_MARK='0')
     AND NOT EXISTS (SELECT NULL FROM ABPPMGR.SUPPLYDMDPEGGING SU WHERE SU.DEMANDORDERID = SO.SO_ID AND SU.DEMANDLINEID = SO.SO_LINE_ID AND SU.SUPPLYORDERID = MO.PEGGED_MO_ID )
     AND SO.SO_ID||'_'||SO.SO_LINE_ID NOT IN ('5000013367_410_2','5000005789_20_1')
      ) WHERE RN=1;
   COMMIT;
*/
  --no ASS MO yet
   INSERT ALL
   WHEN SOCOUNT =1 THEN
   INTO abppmgr.SUPPLYDMDPEGGING
         ( ENTERPRISE
         , ENGINE_ID
         , DEMANDLINEID
         , DEMANDORDERID
         , DEMANDTYPE
         , ITEM
         , QTYALLOCATED
         , SITEID
         , LOCATIONID
         , SUPPLYORDERID
         , SUPPLYTYPE
         , ISDEMANDLOCKED
         , SYS_CREATED_BY
         ) 
         values( ENTERPRISE
         , ENGINE_ID
         , SO_LINE_ID
         , SO_ID
         , 'SO'
         , PRODUCT_ITEM
         , QTYORDERED
         , SITEID
         , NULL 
         , PRODUCTIONORDID
         , 'MFG'
         , '1'
         , SYS_CREATED_BY
         )
  when SOCOUNT=1 then INTO abppmgr.PRODUCTIONORDERS
         ( ENTERPRISE
         , ENGINE_ID
         , BOMID
         , ITEM
         , PLANNEDSTARTDATE
         , PRODUCTIONORDID
         , QTYORDERED
         , REQUIREDDATE
         , ROUTINGID
         , SITEID
         , SYS_CREATED_BY
         ,ISFEEDSLOCKED
         ,ISQTYLOCKED
         ,ISROUTINGLOCKED
         )
         values(ENTERPRISE
         , ENGINE_ID
         , BOMID
         , PRODUCT_ITEM
         , PLANNEDSTARTDATE
         , PRODUCTIONORDID
         , QTYORDERED
         , REQUIREDDATE
         , ROUTINGID
         , SITEID
         , SYS_CREATED_BY
         ,ISFEEDSLOCKED
         ,ISQTYLOCKED
         ,ISROUTINGLOCKED)
    when SOCOUNT is not null then
     INTO abppmgr.SUPPLYDMDPEGGING
         ( ENTERPRISE
         , ENGINE_ID
         , DEMANDLINEID
         , DEMANDORDERID
         , DEMANDTYPE
         , ITEM
         , QTYALLOCATED
         , SITEID
         , SUPPLYORDERID
         , SUPPLYTYPE
         , ISDEMANDLOCKED
         , SYS_CREATED_BY
         ) 
         values( ENTERPRISE
         , ENGINE_ID
         , NULL
         , PRODUCTIONORDID
         , 'MFG'
         , SEMI_ITEM
         , SEMI_QTY
         , SITEID
         , SEMI_MO
         , 'MFG'
         , '1'
         , SYS_CREATED_BY
         )
         SELECT ENTERPRISE
         , ENGINE_ID
         , BOMID
         , SO_ID
         , SO_LINE_ID
         , SEMI_ITEM 
         , PRODUCT_ITEM
         , PLANNEDSTARTDATE
         , PRODUCTIONORDID
         , QTYORDERED
         , SEMI_QTY
         , SEMI_MO
         , REQUIREDDATE
         , ROUTINGID
         , SITEID
         , SYS_CREATED_BY
         ,ISFEEDSLOCKED
         ,ISQTYLOCKED
         ,ISROUTINGLOCKED
         , SOCOUNT from (
   SELECT V_ENTERPRISE ENTERPRISE
        , V_ENGINEID ENGINE_ID
        , SO.SO_ID
        , SO.SO_LINE_ID
        , DECODE(TRIM(BR.BOM_ID),NULL,NULL,''|| LTRIM(SO.ITEM_ID,'0') ||'_'|| TRIM(BR.BOM_ID)) BOMID
        , SO.ITEM_ID PRODUCT_ITEM
        , SYSDATE PLANNEDSTARTDATE
        , SO.SO_ID||'/'||SO. SO_LINE_ID||'-'||'MFG001' PRODUCTIONORDID
        , SO.ORDERED_QTY QTYORDERED
        , MO.QUANTITY SEMI_QTY
        , MO.MO_ID SEMI_MO
        , NVL(SO.UDF_PROMD,SO.UDF_CRSD) REQUIREDDATE
        , BR.ROUTING_ID ROUTINGID
        , V_SITEID SITEID
        , MO.ITEM_ID SEMI_ITEM
        , V_PRONAME||'_'||V_STEP SYS_CREATED_BY
        ,'1' ISFEEDSLOCKED
        ,'1' ISQTYLOCKED
        ,'1' ISROUTINGLOCKED,
        ROW_NUMBER() OVER(PARTITION BY MO.PEGGED_SO_ID,MO.PEGGED_SO_LINE_ID ORDER BY SO.SO_LINE_ID desc) SOCOUNT,
        ROW_NUMBER() OVER(PARTITION BY MO.PEGGED_SO_ID,MO.PEGGED_SO_LINE_ID,MO.MO_ID ORDER BY SO.SO_LINE_ID desc) RN
     FROM IN_SEMI_MO_LOCKED MO,IN_SALES_ORDER SO,IN_ITEMBOMROUTING BR WHERE
     MO.PEGGED_SO_ID=SO.SO_ID AND MO.PEGGED_SO_LINE_ID=SUBSTR(SO.SO_LINE_ID,1,INSTR(SO.SO_LINE_ID,'_')-1)
     AND BR.PRODUCED_ITEM_ID=SO.ITEM_ID and MO.PEGGED_MO_ID is null
     AND BR.siteid=SO.siteid
     AND EXISTS(SELECT NULL FROM IN_BOM_HEADER BH WHERE BH.BOM_ID=BR.BOM_ID AND BH.MO_BOM_MARK='0')
     --ADD BY HUANGYANGXIONG ON 20150314   WHEN SUPPLYORDER IS NOT MFG
     --AND NOT EXISTS (SELECT NULL FROM ABPPMGR.SUPPLYDMDPEGGING SU WHERE SU.DEMANDORDERID = SO.SO_ID AND SU.DEMANDLINEID = SO.SO_LINE_ID AND SU.SUPPLYORDERID NOT LIKE '%MFG%' )
      ) WHERE RN=1 AND EXISTS (SELECT NULL FROM ABPPMGR.MST_ITEMMASTER AM WHERE PRODUCT_ITEM = AM.ITEM) AND EXISTS (SELECT NULL FROM ABPPMGR.MST_ITEMMASTER AM WHERE SEMI_ITEM = AM.ITEM) 
      --ADD BY HUANGYANGXIONG ON 20170607  因为成品工单重复产生,查明原因是SUPPLYDMDPEGGING里的数据重复插入,加入下面过滤语句做测试
      AND NOT EXISTS (SELECT NULL FROM abppmgr.SUPPLYDMDPEGGING SUP WHERE SO_ID = SUP.DEMANDORDERID AND SO_LINE_ID = SUP.DEMANDLINEID)
      ;
    COMMIT;
end if;


---处理成品MFG001绑定半成品工单加半成品库存的问题add by landor on 20180727(处理MFG001场景的库存数据没有进入此表)
INSERT INTO ABPPMGR.SUPPLYDMDPEGGING(ENTERPRISE
         , ENGINE_ID
         ,LOCATIONID
         ,LOTLEVEL1
         ,LOTLEVEL2
         ,ITEM
         , SERIALNUM
         , DEMANDLINEID
         , DEMANDORDERID
         , DEMANDTYPE
         , OPERATIONSEQ
         , QTYALLOCATED
         , SITEID
         , SUPPLYORDERID
         , SUPPLYTYPE
         , ISDEMANDLOCKED
         , SYS_CREATED_BY,SOURCEDATE)         
SELECT    V_Enterprise ENTERPRISE, --V_Enterprise
          V_EngineId ENGINE_ID,   --V_EngineId
         A.PEGGED_SO_ID || A.PEGGED_SO_LINE_ID||'_'||LOCATION LOCATIONID,
         A.PEGGED_SO_ID || A.PEGGED_SO_LINE_ID||'_'||A.SITEID LOTLEVEL1,
          SUBSTR(LOCATION,6,4) LOTLEVEL2,
         A.ITEM_ID ITEM,
         A.PEGGED_SO_ID || A.PEGGED_SO_LINE_ID SERIALNUM,
         '' DEMANDLINEID,
         A.PEGGED_SO_ID || '/'||A.PEGGED_SO_LINE_ID||'_1-MFG001' DEMANDORDERID,
         'MFG'DEMANDTYPE, '10' OPERATIONSEQ,A.QUANTITY QTYALLOCATED,
          V_SITEID,
         ''  SUPPLYORDERID,
         'OH' SUPPLYTYPE,
         '0' ISDEMANDLOCKED,
         'AndanMFG' SYS_CREATED_BY,
         SYSDATE SORUCEDATE
         FROM IN_SEMI_ONHAND_LOCKED A WHERE --ITEM_ID='000000102001002018' AND
 EXISTS (SELECT NULL FROM IN_SEMI_MO_LOCKED  B WHERE A.PEGGED_SO_ID=B.PEGGED_SO_ID AND A.PEGGED_SO_LINE_ID=B.PEGGED_SO_LINE_ID AND PEGGED_MO_ID IS NULL);
 

   /* --针对需求为MFG001场景对库存数据进行写入add by landor on 20180704 20180522   hide by landor on 20180710
    INSERT INTO abppmgr.SUPPLYDMDPEGGING
        (LOCATIONID,LOTLEVEL1 ,LOTLEVEL2,ITEM,SERIALNUM,DEMANDORDERID,DEMANDLINEID,DEMANDTYPE,OPERATIONSEQ,SUPPLYORDERID, SUPPLYTYPE ,QTYALLOCATED,ENTERPRISE,ENGINE_ID,SITEID,SYS_CREATED_BY)
    SELECT 
        MO.PEGGED_SO_ID || MO.PEGGED_SO_LINE_ID ||'_'||MO.LOCATION LOCATIONID
        ,MO.PEGGED_SO_ID || MO.PEGGED_SO_LINE_ID ||'_'|| MO.SITEID LOTLEVEL1 
        ,SUBSTR(MO.LOCATION,6,4) LOTLEVEL2
        ,MO.ITEM_ID ITEM
        ,MO.PEGGED_SO_ID || MO.PEGGED_SO_LINE_ID SERIALNUM
        ,SO.SO_ID||'/'||SO. SO_LINE_ID||'-'||'MFG001'  DEMANDORDERID
        ,'' DEMANDLINEID
        ,'MFG' DEMANDTYPE
        ,'10' OPERATIONSEQ    
        ,'' SUPPLYORDERID
        ,'OH' SUPPLYTYPE 
        ,MO.QUANTITY QTYALLOCATED
        ,V_ENTERPRISE ENTERPRISE
        , V_ENGINEID ENGINE_ID
        , V_SITEID SITEID
        ,'AnDan10' SYS_CREATED_BY
        FROM IN_SEMI_ONHAND_LOCKED MO,IN_SALES_ORDER SO
        WHERE  MO.PEGGED_SO_ID=SO.SO_ID AND MO.PEGGED_SO_LINE_ID=SUBSTR(SO.SO_LINE_ID,1,INSTR(SO.SO_LINE_ID,'_')-1)  --AND SO.SO_ID='5000173100'
        ;
    COMMIT;
    */

--add by zhangguipeng on 20180103 处理MO拆分程序
       FP_MO_SPLIT(EXITCODE);
--增加新字段记录厂别
    update ABPPMGR.PRODUCTIONORDERS set UDF_MO_FACTORY=substr(routingid,-4);
    update ABPPMGR.sfcheader set UDF_MO_FACTORY=substr(routingid,-4);
    commit;

    INSERT INTO ABPPMGR.PRODUCTIONORDERS_P (ENTERPRISE,
                                            ENGINE_ID,
                                            SITEID,
                                            ATTRIBUTECLASS,
                                            ATTRIBUTE,
                                            QTYORDERED,
                                            PRODUCTIONORDERID,
                                            RELATIONSHIP,
                                            VALUE,
                                            SYS_CREATED_BY,
                                            SYS_CREATION_DATE)
       SELECT ENTERPRISE,
              ENGINE_ID,
              SITEID,
              PROPERTY ATTRIBUTECLASS,
              'FACTORY 'ATTRIBUTE,
              QTYORDERED,
              PRODUCTIONORDID PRODUCTIONORDERID,
              'EQ' RELATIONSHIP,
             -- SUBSTR(PROPERTY,-4) VALUE,
             --add by zhangguipeng 20180103 工厂厂别取值更改
              CASE WHEN  UDF_MO_FACTORY LIKE '%C%' then '20'||substr(UDF_MO_FACTORY,1,2)
              else UDF_MO_FACTORY
                  end as value,
              SYS_CREATED_BY,
              SYS_CREATION_DATE
         FROM ABPPMGR.PRODUCTIONORDERS;

    COMMIT;


     ---ADD BY Landor ON 20180522 按单方案更新补充处理PRODUCTIONORDERS_P.ATTRIBUTECLASS字段值
    UPDATE ABPPMGR.PRODUCTIONORDERS_P AP SET AP.ATTRIBUTECLASS=AP.PRODUCTIONORDERID||':'||AP.VALUE;
    COMMIT;

    EXECUTE IMMEDIATE 'TRUNCATE table ABPPMGR.SUPPLYDMDPEG_P';--add by landor on 20180622 将下面删除数据改为此句

    --DELETE FROM ABPPMGR.SUPPLYDMDPEG_P P  WHERE P.SYS_CREATED_BY= V_PRONAME   AND P.SYS_CREATED_BY='AnDan'; -- HIDE BY LANDOR ON 20180622
    --COMMIT;

    --old 代码  no attribute for sourcing data  tanxiang 11/24
    insert into ABPPMGR.SUPPLYDMDPEG_P(SCENARIO_ID,ENTERPRISE, ENGINE_ID,SITEID,
                                            DEMANDORDERID, ITEM,SUPPLYORDERID, LOTLEVEL1, SERIALNUM, DEMANDLINEID, DEMANDSCHEDULEID, SUPPLYLINEID, SUPPLYSCHEDULEID, LOCATIONID, LOTLEVEL2, OPERATIONSEQ ,
                                            ATTRIBUTECLASS,
                                            ATTRIBUTE,
                                            PASSTHRU,
                                            RELATIONSHIP,
                                            SYS_CREATED_BY,
                                            SYS_CREATION_DATE)
                                 SELECT     SCENARIO_ID, ENTERPRISE,ENGINE_ID, SITEID, 
                                            DEMANDORDERID, ITEM,SUPPLYORDERID, LOTLEVEL1, replace(SERIALNUM,'/',''), DEMANDLINEID, DEMANDSCHEDULEID, SUPPLYLINEID, SUPPLYSCHEDULEID, LOCATIONID, LOTLEVEL2, OPERATIONSEQ 
                                            ,'PASS_ALL' ATTRIBUTECLASS
                                           , 'FACTORY'ATTRIBUTE
                                           , NULL PASSTHRU
                                           , 'PASS' RELATIONSHIP
                                           , V_PRONAME SYS_CREATED_BY
                                            , SYS_CREATION_DATE                      
                                            from ABPPMGR.SUPPLYDMDPEGGING s 
                                            where   not exists(select null from in_location l where S.LOCATIONID=L.LOCATION_ID) 
                                            AND DEMANDORDERID IS NULL -- 在处理按单时PAUL回复'ATTRIBUTECLASS=PASS_ALL是在DEMANDORDERID為空之下才可能发生'add by landor on 20180705
                                            ;
    COMMIT; 


    --在supplydmdpegging_p中当绑定的库存是5000164149_810,表示拷贝到到attributeclass=5000164149_810:5000164149810,attribute=ORDER_PLANNING,value=5000164149810
    --add by landor on 20180622 20180522
    insert into ABPPMGR.SUPPLYDMDPEG_P(ATTRIBUTECLASS,
                                            ATTRIBUTE,
                                            VALUE,
                                            SCENARIO_ID,ENTERPRISE, ENGINE_ID,SITEID,
                                            DEMANDORDERID, ITEM,SUPPLYORDERID, 
                                            LOTLEVEL1, 
                                            SERIALNUM, DEMANDLINEID, DEMANDSCHEDULEID, SUPPLYLINEID, SUPPLYSCHEDULEID, 
                                            LOCATIONID, LOTLEVEL2, OPERATIONSEQ ,   
                                           -- PASSTHRU,
                                            RELATIONSHIP,
                                            QTYALLOCATED,
                                            SYS_CREATED_BY,
                                            SYS_CREATION_DATE)
                                 SELECT  distinct   IVT.ATTRIBUTECLASS,
                                            'ORDER_PLANNING' ATTRIBUTE,
                                             IVT.VALUE ,
                                            SP.SCENARIO_ID,SP.ENTERPRISE, SP.ENGINE_ID,SP.SITEID,
                                            SP.DEMANDORDERID, SP.ITEM,SP.SUPPLYORDERID, 
                                            IVT.LOTLEVEL1,
                                            REPLACE(SP.SERIALNUM,'/','') SERIALNUM, SP.DEMANDLINEID, SP.DEMANDSCHEDULEID, SP.SUPPLYLINEID, SP.SUPPLYSCHEDULEID, 
                                            IVT.LOCATIONID, IVT.LOTLEVEL2, OPERATIONSEQ ,
                                           -- SP.PASSTHRU,
                                            'EQ' RELATIONSHIP,--alter by landor on 20180705
                                            SP.QTYALLOCATED, --add by landor on 20180705 需要加上库存数量
                                            'AnDan1',
                                            SP.SYS_CREATION_DATE 
                                 FROM  ABPPMGR.INVENTORYPROPERTY IVT
                                 --JOIN ABPPMGR.SUPPLYDMDPEG_P SP  ON IVT.ITEM=SP.ITEM AND IVT.VALUE=REPLACE(SP.SERIALNUM,'/','') alter by landor on 20180705
                                 JOIN ABPPMGR.SUPPLYDMDPEGGING SP  ON IVT.ITEM=SP.ITEM AND IVT.VALUE=REPLACE(SP.SERIALNUM,'/','') --add by landor on 20180705
                                 WHERE IVT.ATTRIBUTE='ORDER_PLANNING' AND LENGTH(IVT.VALUE) >10 --带SO号和行号
                                 UNION  --add by landor on 20180705
                                  SELECT  distinct   SUBSTR(IVT.ATTRIBUTECLASS,1,INSTR(IVT.ATTRIBUTECLASS,':'))||SUBSTR(IVT.LOTLEVEL1,INSTR(IVT.LOTLEVEL1,'_')+1,4) ATTRIBUTECLASS,
                                            'FACTORY' ATTRIBUTE,
                                             SUBSTR(IVT.LOTLEVEL1,INSTR(IVT.LOTLEVEL1,'_')+1,4) VALUE,
                                            SP.SCENARIO_ID,SP.ENTERPRISE, SP.ENGINE_ID,SP.SITEID,
                                            SP.DEMANDORDERID, SP.ITEM,SP.SUPPLYORDERID, 
                                            IVT.LOTLEVEL1,
                                            REPLACE(SP.SERIALNUM,'/','') SERIALNUM, SP.DEMANDLINEID, SP.DEMANDSCHEDULEID, SP.SUPPLYLINEID, SP.SUPPLYSCHEDULEID, 
                                            IVT.LOCATIONID, IVT.LOTLEVEL2, OPERATIONSEQ ,
                                            'EQ' RELATIONSHIP,
                                            SP.QTYALLOCATED, --add by landor on 20180705 需要加上库存数量
                                            'AnDan2',
                                            SP.SYS_CREATION_DATE 
                                 FROM  ABPPMGR.INVENTORYPROPERTY IVT
                                 --JOIN ABPPMGR.SUPPLYDMDPEG_P SP  ON IVT.ITEM=SP.ITEM AND IVT.VALUE=REPLACE(SP.SERIALNUM,'/','')
                                 JOIN ABPPMGR.SUPPLYDMDPEGGING SP  ON IVT.ITEM=SP.ITEM AND IVT.VALUE=REPLACE(SP.SERIALNUM,'/','')
                                 WHERE IVT.ATTRIBUTE='ORDER_PLANNING' AND LENGTH(IVT.VALUE) >10  --带SO号和行号
                                 UNION
                                  SELECT  distinct   SUBSTR(IVT.ATTRIBUTECLASS,1,INSTR(IVT.ATTRIBUTECLASS,':'))||SUBSTR(IVT.LOTLEVEL1,INSTR(IVT.LOTLEVEL1,'_')+1,4) ATTRIBUTECLASS,
                                            'FACTORY' ATTRIBUTE,
                                             SUBSTR(IVT.LOTLEVEL1,INSTR(IVT.LOTLEVEL1,'_')+1,4) VALUE,
                                            SP.SCENARIO_ID,SP.ENTERPRISE, SP.ENGINE_ID,SP.SITEID,
                                            SP.DEMANDORDERID, SP.ITEM,SP.SUPPLYORDERID, 
                                            IVT.LOTLEVEL1,
                                            REPLACE(SUBSTR(SP.SERIALNUM,1,INSTR(SP.SERIALNUM,'_')-1),'/','') SERIALNUM--此处有带计划行号,日后可加回来
                                            , SP.DEMANDLINEID, SP.DEMANDSCHEDULEID, SP.SUPPLYLINEID, SP.SUPPLYSCHEDULEID, 
                                            IVT.LOCATIONID, IVT.LOTLEVEL2, OPERATIONSEQ ,
                                            'EQ' RELATIONSHIP,
                                            SP.QTYALLOCATED,
                                            'AnDan3',
                                            SP.SYS_CREATION_DATE 
                                 FROM  ABPPMGR.INVENTORYPROPERTY IVT
                                 JOIN ABPPMGR.SUPPLYDMDPEGGING SP  ON IVT.ITEM=SP.ITEM AND IVT.VALUE= REPLACE(SUBSTR(SP.DEMANDORDERID,1,INSTR(DEMANDORDERID,'_')-1),'/','')
                                 WHERE IVT.ATTRIBUTE='ORDER_PLANNING' AND LENGTH(IVT.VALUE) >10
                                 AND SP.DEMANDORDERID LIKE '%MFG000%' --对成品MFG000场景单独出来考虑,增加库存绑定add by landor on 20180710 
                                 UNION
                                  SELECT  distinct   SUBSTR(IVT.ATTRIBUTECLASS,1,INSTR(IVT.ATTRIBUTECLASS,':'))||IVT.VALUE ATTRIBUTECLASS,
                                            'ORDER_PLANNING' ATTRIBUTE,
                                              SUBSTR(IVT.LOCATIONID,1,INSTR(IVT.LOCATIONID,'_')-1)  VALUE,
                                            SP.SCENARIO_ID,SP.ENTERPRISE, SP.ENGINE_ID,SP.SITEID,
                                            SP.DEMANDORDERID, SP.ITEM,SP.SUPPLYORDERID, 
                                            IVT.LOTLEVEL1,
                                            REPLACE(SUBSTR(SP.SERIALNUM,1,INSTR(SP.SERIALNUM,'_')-1),'/','') SERIALNUM  --此处有带计划行号,日后可加回来
                                            , SP.DEMANDLINEID, SP.DEMANDSCHEDULEID, SP.SUPPLYLINEID, SP.SUPPLYSCHEDULEID, 
                                            IVT.LOCATIONID, IVT.LOTLEVEL2, OPERATIONSEQ ,
                                            'EQ' RELATIONSHIP,
                                            SP.QTYALLOCATED,
                                            'AnDan4',
                                            SP.SYS_CREATION_DATE 
                                 FROM  ABPPMGR.INVENTORYPROPERTY IVT
                                 JOIN ABPPMGR.SUPPLYDMDPEGGING SP  ON IVT.ITEM=SP.ITEM AND IVT.VALUE= REPLACE(SUBSTR(SP.DEMANDORDERID,1,INSTR(DEMANDORDERID,'_')-1),'/','')
                                 WHERE IVT.ATTRIBUTE='ORDER_PLANNING' AND LENGTH(IVT.VALUE) >10
                                 AND SP.DEMANDORDERID LIKE '%MFG000%'; 




        --针对CRTD MO加半成品库存场景,增加MFG001数据(条件是有CRTD成品工单且半成品SO库存)add by landor on 20180710 20180522
        INSERT INTO ABPPMGR.SUPPLYDMDPEG_P(ATTRIBUTE,ATTRIBUTECLASS,DEMANDLINEID,DEMANDORDERID,ENGINE_ID,ENTERPRISE,ITEM,LOCATIONID,LOTLEVEL1,LOTLEVEL2,OPERATIONSEQ,ORIG_ENGINE_ID
                  ,QTYALLOCATED,RELATIONSHIP,SERIALNUM,SITEID,SOURCEDATE,SYNCIND,VALUE
                  ,SYS_SOURCE,SYS_CREATED_BY,SYS_CREATION_DATE,SYS_ENT_STATE )
        SELECT A.ATTRIBUTE,ATTRIBUTECLASS,DEMANDLINEID
                   ,SL.PEGGED_SO_ID || '/'||SL.PEGGED_SO_LINE_ID ||'_1-MFG001'DEMANDORDERID,ENGINE_ID,ENTERPRISE,ITEM,LOCATIONID,LOTLEVEL1,LOTLEVEL2,OPERATIONSEQ,ORIG_ENGINE_ID
                  ,QTYALLOCATED,RELATIONSHIP,SERIALNUM,A.SITEID,SOURCEDATE,SYNCIND,VALUE
                  ,SYS_SOURCE,'AnDan_MFG',SYS_CREATION_DATE,SYS_ENT_STATE 
        FROM ABPPMGR.supplydmdpeg_p A 
        JOIN IN_SEMI_ONHAND_LOCKED SL ON SUBSTR(A.ATTRIBUTECLASS,1,10)=SL.PEGGED_SO_ID AND SUBSTR(A.LOCATIONID,11,INSTR(A.LOCATIONID,'_')-11) =SL.PEGGED_SO_LINE_ID --有半成品SO库存  
        JOIN IN_MO MO ON A.DEMANDORDERID=MO.MO_ID  --需要有CRTD的成品工单
        WHERE  MO.MO_ID LIKE '0011%' 
        AND NOT EXISTS(SELECT NULL FROM ABPPMGR.supplydmdpeg_p B WHERE B.DEMANDORDERID=A.DEMANDORDERID AND A.ATTRIBUTE=B.ATTRIBUTE AND B.DEMANDORDERID LIKE '%MFG001%'); 
        COMMIT;

   /* --增加新字段记录厂别
    update ABPPMGR.PRODUCTIONORDERS set UDF_MO_FACTORY=substr(routingid,-4);
    update ABPPMGR.sfcheader set UDF_MO_FACTORY=substr(routingid,-4);
    commit;*/
    --未做绑定的工单需要给其他需求使用
    update ABPPMGR.PRODUCTIONORDERS T1 set T1.ISFEEDSLOCKED =0 
    where not exists(select null from ABPPMGR.SUPPLYDMDPEGGING sdp where sdp.supplyorderid=T1.PRODUCTIONORDID);
    commit;
    update ABPPMGR.SFCHEADER T1 set T1.ISFEEDSLOCKED =0 
    where not exists(select null from ABPPMGR.SUPPLYDMDPEGGING sdp where sdp.supplyorderid=T1.PRODUCTIONORDERID);
    commit;
  -- UPDATE IN_MO M SET M.SO_ID=NULL ,M.SO_LINE_ID=NULL,M.PEGGED_MO_ID=NULL WHERE M.MO_TYPE IN ('ZP13', 'ZP23' , 'ZP56', 'ZP66')  AND M.SO_ID=M.MO_TYPE|| '_' || M.MO_ID; 
   UPDATE IN_MO M SET M.SO_ID=NULL ,M.SO_LINE_ID=NULL,M.PEGGED_MO_ID=NULL WHERE M.MO_TYPE IN ('ZP12')  AND M.SO_ID=M.MO_TYPE|| '_' || M.MO_ID;
   --UPDATE IN_SFCHEADER M SET M.SO_ID=NULL ,M.SO_LINE_ID=NULL,M.PEGGED_MO_ID=NULL WHERE M.MO_TYPE IN ('ZP13', 'ZP23' , 'ZP56', 'ZP66')  AND M.SO_ID=M.MO_TYPE|| '_' || M.MO_ID;
   UPDATE IN_SFCHEADER M SET M.SO_ID=NULL ,M.SO_LINE_ID=NULL,M.PEGGED_MO_ID=NULL WHERE M.MO_TYPE IN ('ZP12')  AND M.SO_ID=M.MO_TYPE|| '_' || M.MO_ID;
  COMMIT;
   PKG_UTIL.CREATELOG (V_PKGName,
                       V_ProName,
                       V_Step,
                       V_Action,
                       PKG_CONSTANT.CONS_MSG_INFO,
                       'COMMIT Executed',
                       V_MainTable);


 --add by zhangguipeng on 20180103 MO拆分处理SUPPLYDMDPEG_P表
 MERGE INTO  abppmgr.SUPPLYDMDPEG_P AP
   USING(select DISTINCT DEMANDORDERID
                        ,ITEM
                        ,LOCATIONID
                        ,LOTLEVEL1
            from ABPPMGR.SUPPLYDMDPEGGING_S
         WHERE SUPPLYTYPE='OH' )AA
     ON(AP.DEMANDORDERID=AA.DEMANDORDERID
        AND AP.ITEM=AA.ITEM
        AND AA.LOCATIONID=AP.LOCATIONID)
       WHEN MATCHED THEN 
          UPDATE SET AP.ATTRIBUTECLASS=AA.LOTLEVEL1;

    --保证SUPPLYDMDPEGGING中的locationid,lotlevel1,lotlevel2,SERIALNUM都必须等于inventory中相对应的值 否则绑定会失败add by landor on 20180522 20180612 paul要求
    MERGE INTO ABPPMGR.SUPPLYDMDPEGGING SU
        USING(SELECT * FROM IN_MO WHERE SO_ID IS NOT NULL) MO
        ON(SU.DEMANDORDERID=MO.MO_ID)
     WHEN MATCHED THEN
        UPDATE SET SU.LOCATIONID=MO.SO_ID||MO.SO_LINE_ID||'_'||LOCATIONID, SU.LOTLEVEL1=MO.SO_ID||MO.SO_LINE_ID||'_'||SU.LOTLEVEL1,SERIALNUM=REPLACE(SERIALNUM,'/','') 
        WHERE  SERIALNUM IS NOT NULL;
    COMMIT;    

    MERGE INTO ABPPMGR.SUPPLYDMDPEGGING SU
        USING(SELECT * FROM IN_SFCHEADER WHERE SO_ID IS NOT NULL) MO
        ON(SU.DEMANDORDERID=MO.MO_ID)
     WHEN MATCHED THEN
        UPDATE SET SU.LOCATIONID=MO.SO_ID||MO.SO_LINE_ID||'_'||LOCATIONID, SU.LOTLEVEL1=MO.SO_ID||MO.SO_LINE_ID||'_'||SU.LOTLEVEL1 ,SERIALNUM=REPLACE(SERIALNUM,'/','') 
        WHERE  SERIALNUM IS NOT NULL;
    COMMIT;       


    --start SUPPLYDMDPEGGING  MTL
    --针对MTL无法绑定库存问题,只要寫進IN_MO_SHORT都會產生MTL的需求 add by landor on 20180522 20180626
    MERGE INTO ABPPMGR.SUPPLYDMDPEGGING S
        --USING IN_MO_SHORT M  --alter by landor on 20170702
        USING (SELECT SH.* FROM IN_MO_SHORT SH 
                JOIN (SELECT M.PEGGED_MO_ID,M.ITEM_ID,SUM(M.PLANNED_QTY) QTY FROM IN_MO M  GROUP BY M.PEGGED_MO_ID,M.ITEM_ID) A
                ON  A.PEGGED_MO_ID=SH.MO_ID AND A.ITEM_ID=SH.ITEM_ID
                WHERE SH.UDF_SHORT_QTY>=A.QTY) M --确保成品工单组件需求数量在大于IN MO里面汇总的数量add by landor on 20180702
        ON  (M.MO_ID=S.DEMANDORDERID AND M.ITEM_ID=S.ITEM) 
    WHEN MATCHED THEN
        UPDATE SET SYS_ERR_CODE='MTL_'||S.DEMANDORDERID||'_'||M.SITEID , DEMANDTYPE='SO',S.DEMANDLINEID=S.ITEM ,SYS_CREATED_BY=SYS_CREATED_BY||'_MTL';
    COMMIT;   


    UPDATE ABPPMGR.SUPPLYDMDPEGGING  SET  DEMANDORDERID=SYS_ERR_CODE,SYS_ERR_CODE='' 
            ,OPERATIONSEQ =null --add by landor on 20180628 operationseqi清空 表示需求是订单不是工单
        WHERE SYS_ERR_CODE IS NOT NULL;
    COMMIT;  
    --end SUPPLYDMDPEGGING MTL

    --start SUPPLYDMDPEG_P MTL 
   --删除MTL FACTORY数据后后面重新写入  add by landor on 20180522 20180626
    DELETE FROM ABPPMGR.SUPPLYDMDPEG_P S 
    WHERE EXISTS(SELECT NULL FROM IN_MO_SHORT M WHERE  M.MO_ID=S.DEMANDORDERID AND M.ITEM_ID=S.ITEM) 
        AND S.ATTRIBUTE='FACTORY';
    COMMIT;

    --针对MTL无法绑定库存问题,只要寫進IN_MO_SHORT都會產生MTL的需求 add by landor on 20180522 20180626
    MERGE INTO ABPPMGR.SUPPLYDMDPEG_P S
        USING IN_MO_SHORT M 
        ON  (M.MO_ID=S.DEMANDORDERID AND M.ITEM_ID=S.ITEM)
    WHEN MATCHED THEN
        UPDATE SET SYS_ERR_CODE='MTL_'||S.DEMANDORDERID||'_'||M.SITEID --用于更新DEMANDORDERID
                    ,S.DEMANDLINEID=S.ITEM
                    ,S.RELATIONSHIP='EQ'
                    ,S.QTYALLOCATED=M.UDF_SHORT_QTY  --add by landor on 20160628 在处理MTL无法吃库存时,PAUL要求加上
                    ,SYS_CREATED_BY=SYS_CREATED_BY||'_MTL'
                    WHERE S.ATTRIBUTE='ORDER_PLANNING';
    COMMIT;

    --重新写入MTL 属性为FACTORY绑定库存的数据
    --SELECT * FROM ABPPMGR.SUPPLYDMDPEG_P WHERE ITEM='000000102011001141' and SYS_ERR_CODE IS NOT NULL;
    INSERT INTO ABPPMGR.SUPPLYDMDPEG_P(ATTRIBUTE,ATTRIBUTECLASS,DEMANDLINEID,DEMANDORDERID,ENGINE_ID,ENTERPRISE,ITEM,LOCATIONID,LOTLEVEL1,LOTLEVEL2,OPERATIONSEQ,ORIG_ENGINE_ID
        ,QTYALLOCATED,RELATIONSHIP,SERIALNUM,SITEID,SOURCEDATE,SYNCIND,VALUE,SYS_SOURCE,SYS_CREATED_BY,SYS_CREATION_DATE,SYS_ENT_STATE ,SYS_ERR_CODE)
    SELECT 'FACTORY' ATTRIBUTE,SUBSTR(ATTRIBUTECLASS,1,INSTR(ATTRIBUTECLASS,':'))||SUBSTR(LOTLEVEL1,INSTR(LOTLEVEL1,'_')+1,4) ATTRIBUTECLASS
        ,DEMANDLINEID,DEMANDORDERID,ENGINE_ID,ENTERPRISE,ITEM,LOCATIONID,LOTLEVEL1,LOTLEVEL2,OPERATIONSEQ,ORIG_ENGINE_ID
        ,QTYALLOCATED,RELATIONSHIP,SERIALNUM,SITEID,SOURCEDATE,SYNCIND,SUBSTR(LOTLEVEL1,INSTR(LOTLEVEL1,'_')+1,4) VALUE
        ,SYS_SOURCE,SYS_CREATED_BY||'_CPY',SYS_CREATION_DATE,SYS_ENT_STATE ,SYS_ERR_CODE
    FROM ABPPMGR.SUPPLYDMDPEG_P WHERE  SYS_ERR_CODE IS NOT NULL;
    COMMIT;


    UPDATE ABPPMGR.SUPPLYDMDPEG_P  
        SET  DEMANDORDERID=SYS_ERR_CODE,SYS_ERR_CODE='' 
             ,OPERATIONSEQ=null --add by landor on 20180628 operationseqi清空 表示需求是订单不是工单
        WHERE SYS_ERR_CODE IS NOT NULL;
    COMMIT;
    --end SUPPLYDMDPEG_P   MTL


    --更新按单生产MFG000场景的LOCATIONID、LOTLEVEL1值 add by landor on 20180710
    MERGE INTO ABPPMGR.supplydmdpegging  S
        USING IN_SEMI_ONHAND_LOCKED SL
        ON (SL.PEGGED_SO_ID||'/'||SL.PEGGED_SO_LINE_ID=SUBSTR(S.DEMANDORDERID,1,INSTR(S.DEMANDORDERID,'_')-1)
            AND S.LOCATIONID=SL.LOCATION)
    WHEN MATCHED
        THEN
           UPDATE SET SYS_LAST_MODIFIED_BY = REPLACE(SUBSTR(S.SERIALNUM,1,INSTR(S.SERIALNUM,'_')-1),'/','')||'_'||S.LOCATIONID,  -- S.LOCATIONID 5000159589180_2022_2013
                      S.lotlevel1=REPLACE(SUBSTR(S.SERIALNUM,1,INSTR(S.SERIALNUM,'_')-1),'/','')||'_'||S.LOTLEVEL1,
                      S.SERIALNUM=REPLACE(SUBSTR(S.SERIALNUM,1,INSTR(S.SERIALNUM,'_')-1),'/','') 
           WHERE S.DEMANDORDERID LIKE '%MFG000%' ;
    COMMIT;  

    UPDATE ABPPMGR.supplydmdpegging   SET LOCATIONID=SYS_LAST_MODIFIED_BY,SYS_LAST_MODIFIED_BY =NULL WHERE SYS_LAST_MODIFIED_BY IS NOT NULL ;


     --针对成品REL工单绑定半成品库存换成MTL工单绑定半成品库存(换绑)Add by landor on 20180712
     UPDATE ABPPMGR.supplydmdpegging S 
     SET DEMANDORDERID='MTL_'||DEMANDORDERID||'_'||SUBSTR(LOTLEVEL1,INSTR(LOTLEVEL1,'_')+1,4)
        ,DEMANDLINEID=S.ITEM
        ,DEMANDTYPE='SO'
        ,OPERATIONSEQ=NULL
      WHERE DEMANDORDERID like '0011%' AND SUPPLYTYPE='OH' AND SERIALNUM IS NOT NULL AND EXISTS (SELECT NULL FROM IN_MO_SHORT MO WHERE S.DEMANDORDERID=MO.MO_ID AND S.ITEM=MO.ITEM_ID);
     COMMIT;

     --add START
    --针对成品REL工单绑定半成品MO 换成MTL工单绑定半成品MO(换绑)Add by landor on 20180714
    MERGE INTO  ABPPMGR.supplydmdpegging S
    USING  IN_MO_SHORT MO
    ON (S.DEMANDORDERID=MO.MO_ID AND S.ITEM=MO.ITEM_ID)
    WHEN MATCHED
            THEN
            UPDATE SET SYS_LAST_MODIFIED_BY ='MTL_'||DEMANDORDERID||'_'||MO.SITEID  --DEMANDORDERID
                ,DEMANDTYPE='SO'
                ,ISDEMANDLOCKED='1'
                ,DEMANDLINEID=ITEM
            where s.demandorderid like '0011%'  and SUPPLYTYPE='MFG';
    COMMIT;     

    --SELECT * FROM ABPPMGR.supplydmdpegging WHERE SYS_LAST_MODIFIED_BY IS NOT NULL;
    UPDATE ABPPMGR.supplydmdpegging SET DEMANDORDERID=SYS_LAST_MODIFIED_BY WHERE SYS_LAST_MODIFIED_BY IS NOT NULL;
    COMMIT;
   --end add  20180714
   
   --修复多计划行中数量为0的数据add by landor on 20180723
   MERGE INTO  ABPPMGR.SUPPLYDMDPEGGING A
    USING (SELECT SO_ID,SO_LINE_ID,ORDERED_QTY FROM IN_SALES_ORDER)  B
    ON (A.DEMANDORDERID=B.SO_ID  AND A.DEMANDLINEID=B.SO_LINE_ID)
    WHEN MATCHED
    THEN
        UPDATE SET A.QTYALLOCATED=B.ORDERED_QTY WHERE A.DEMANDTYPE='SO' AND A.SUPPLYTYPE='MFG' AND SUBSTR(A.DEMANDLINEID,INSTR(A.DEMANDLINEID,'_')+1,1)>1 AND A.QTYALLOCATED=0;
    COMMIT;    




COMMIT;
EXCEPTION
   --总程序异常处理部分
   WHEN OTHERS
   THEN
      BEGIN
         ROLLBACK;
         EXITCODE := 1;

         e_ErrMessage := SUBSTR (SQLERRM, 1, 1000)
                      || SUBSTR (DBMS_UTILITY.FORMAT_ERROR_BACKTRACE, 1, 2000);
         PKG_UTIL.CREATELOG (V_PKGName,
                             V_ProName,
                             V_Step,
                             V_Action,
                             PKG_CONSTANT.CONS_MSG_EXCEPTION,
                             e_ErrMessage,
                             V_MainTable);
      END;

END FP_MO;
/
View Code

 

FP_FCST

CREATE OR REPLACE PROCEDURE STG.FP_FCST (EXITCODE OUT NUMBER)
IS
   /***********************************************************************
    **  存储过程详细说明
    **  存储过程名称:  FP_FCST
    **  存储过程创建日期: 2014/06/24
    **  存储过程创建人: Kyoko Kato
    **  目的:  Populate FORECASTDETAIL from IN_SO_STOCK as follows:
    **    from W1(current) to W4 => IN_SO_STOCK netted with IN_SALES_ORDER
    **    from W5 to end         => IN_SO_STOCK
    **  输入参数:
    **  输出参数: EXITCODE: 0=Success/1=Failure
    **  返回值: 
    **  用法: 
    **
    ** 修订版本:
    **  版本号     修订时间    修订人        描述
    **  ---------  ----------  ------------  ------------------------
    **    1.0      2014/07/01  Kyoko Kato    1. 新建存储过程.
    **    2.0      2014/0826   谭湘             1. 前两周的净预测要拆分到所在周的每一天
    **    3.0     2016/02/29  伍尚锟         1. 修改为变式以VC物料冲减TMP_FCST_NETTED 表结构
    **   4.0     2018.05.22  Landor         增加按单生产业务
    **********************************************************************/


   ----------------------- 以下定义存储过程使用的变量---------------------------------
   V_PKGName     VARCHAR2 (40) := 'PKG_FP';
   V_ProName     VARCHAR2 (40) := 'FP_FCST';
   V_Action      VARCHAR2 (1);
   V_Step        VARCHAR2 (40);
   V_MainTable   VARCHAR2 (40);
   V_StartDate   DATE;
   V_EndDate     DATE;
   V_Line        VARCHAR2 (40) := 'TEMP';
   V_NUM         NUMBER := 0;
   V_Count       NUMBER := 0;
   V_Time        DATE;
   E_ErrMessage   varchar2(2000); 

   v_debug number; -- for debug
   --
   v_SqlStr      VARCHAR2(2000) ; -- for dynamic SQL
   v_NumWeekForNetting NUMBER := 4 ; -- Number of weeks to operate forecast netting
   v_NumBucketNetting  NUMBER := 3 ; -- Number of future buckets for netting 
   --
   V_ScenarioId  NUMBER  := 0 ;
   V_EngineId    NUMBER  := PKG_CONSTANT.CONS_FP_ENGINE ;
   V_Enterprise  VARCHAR2 (40) := PKG_CONSTANT.CONS_ENTERPRISE ;
   V_SiteID       VARCHAR2 (40) :=PKG_CONSTANT.CONS_SITE_ZHUHAI;
   v_Priority    NUMBER  := 20000 ; -- Max priority for Forecast
   --
   type tbl_TMP_FCST_NETTED is table of TMP_FCST_NETTED%rowtype ;
   array_TMP_FCST_NETTED  tbl_TMP_FCST_NETTED ;
   v_result_ALLOCQTY  NUMBER ;

   -- Cursor for key columns of netting group.
   -- If the combination does not have SalesOrder, no need to calculate
   CURSOR CUR1_NETTINGKEY IS
   SELECT DISTINCT PRODUCT_ID
        , LOC_ID
        , IS_BOUNDSITE
        , WK_GEO
     FROM TMP_FCST_NETTED
    WHERE nvl(SO_QTY, 0) <> 0
   ;

----------------------- 以下是存储过程的主体------------------------------------------
PROCEDURE GET_PARENT_BOM(P_PARENT_ID VARCHAR2,P_ITEM_ID VARCHAR2,P_USAGE_QTY NUMBER) IS
BEGIN
    FOR C_BOM IN (SELECT BD.ITEM_ID,substr(BH.BOM_ID,instr( BH.BOM_ID,'_')+1,4) SITE_ID, BD.USAGE_QTY*P_USAGE_QTY  USAGE_QTY
  FROM IN_BOM_DETAILS BD, IN_BOM_HEADER BH
 WHERE     TRIM(BD.BOM_ID) = TRIM(BH.BOM_ID)
       AND BH.PRODUCT_ID = P_ITEM_ID
       AND BH.MO_BOM_MARK = '0'
       AND exists( select null from in_item_site iis where IIS.ITEM_ID = BD.ITEM_ID
       AND IIS.SITE_ID= substr(BH.BOM_ID,instr( BH.BOM_ID,'_')+1,4)
       AND IIS.UDF_ITEM_CATEGORY = 'SEMI')) LOOP
    INSERT INTO TEMP_BOM_ITEM(PARENT_ID,SITE_ID,ITEM_ID,USAGE_QTY)
    VALUES(P_PARENT_ID,C_BOM.SITE_ID,C_BOM.ITEM_ID,C_BOM.USAGE_QTY);
    COMMIT;
        GET_PARENT_BOM(P_PARENT_ID,C_BOM.ITEM_ID,C_BOM.USAGE_QTY);
    END LOOP;
END;

BEGIN
   EXITCODE := 0;
    EXECUTE IMMEDIATE 'TRUNCATE TABLE TEMP_BOM_ITEM';
    EXECUTE IMMEDIATE 'TRUNCATE TABLE  TEMP_FCST_VERSION';
    DELETE FROM IN_SO_STOCK SS WHERE EXISTS( SELECT NULL FROM (
    SELECT LOC_ID,MAX(UDF_FCST_VERSION_DATE) UDF_FCST_VERSION_DATE FROM IN_SO_STOCK GROUP BY  LOC_ID) LS 
    WHERE LS.LOC_ID=SS.LOC_ID AND SS.UDF_FCST_VERSION_DATE<LS.UDF_FCST_VERSION_DATE);
  V_Step := 1;
  V_Action := PKG_CONSTANT.CONS_ACTION_DELETE ;

  V_MainTable := 'IN_SO_STOCK' ;
  PKG_UTIL.CREATELOG (V_PKGName,
                      V_ProName,
                      V_Step,
                      V_Action,
                      PKG_CONSTANT.CONS_MSG_INFO,
                      'Removed ' || sql%rowcount || ' rows for over date',
                      V_MainTable);
  COMMIT;
  FOR C_ITEM IN (SELECT TM.ITEM_ID FROM IN_ITEM TM WHERE 
  EXISTS(SELECT NULL FROM IN_SALES_ORDER SO,IN_ITEM_SITE IIS 
  WHERE SO.ITEM_ID=IIS.ITEM_ID AND IIS.ITEM_ID=TM.ITEM_ID AND SO.SITEID=IIS.SITE_ID
   AND IIS.UDF_ITEM_CATEGORY='ASS')) LOOP
    GET_PARENT_BOM(C_ITEM.ITEM_ID,C_ITEM.ITEM_ID,1);
  END LOOP;
  COMMIT;
  delete from TEMP_BOM_ITEM where rowid not in(select max(rowid) from TEMP_BOM_ITEM group by PARENT_ID, ITEM_ID, SITE_ID );
  commit;
  DBMS_STATS.GATHER_TABLE_STATS('STG','TEMP_BOM_ITEM');
  COMMIT;
    /*
   第一段逻辑描述,处理规则等
   Delete existing records from ODS
   Clear TMP table TMP_FCST_NETTED
  */
  V_Step := 1;
  V_Action := PKG_CONSTANT.CONS_ACTION_DELETE ;

  V_MainTable := 'FORECASTDETAIL_P' ;
  PKG_UTIL.CREATELOG (V_PKGName,
                      V_ProName,
                      V_Step,
                      V_Action,
                      PKG_CONSTANT.CONS_MSG_INFO,
                      'START',
                      V_MainTable);

  DELETE FROM abppmgr.FORECASTDETAIL_P ;

  /*
   第一段逻辑处理结束,记录日志
  */
   PKG_UTIL.CREATELOG (V_PKGName,
                       V_ProName,
                       V_Step,
                       V_Action,
                       PKG_CONSTANT.CONS_MSG_INFO,
                       'Record Count: '|| SQL%ROWCOUNT,
                       V_MainTable);
 commit;

  V_MainTable := 'FORECASTDETAIL' ;
  PKG_UTIL.CREATELOG (V_PKGName,
                      V_ProName,
                      V_Step,
                      V_Action,
                      PKG_CONSTANT.CONS_MSG_INFO,
                      'START',
                      V_MainTable);

  DELETE FROM abppmgr.FORECASTDETAIL ;

  /*
   第一段逻辑处理结束,记录日志
  */
   PKG_UTIL.CREATELOG (V_PKGName,
                       V_ProName,
                       V_Step,
                       V_Action,
                       PKG_CONSTANT.CONS_MSG_INFO,
                       'Record Count: '|| SQL%ROWCOUNT,
                       V_MainTable);
 commit;
  V_Action := PKG_CONSTANT.CONS_ACTION_TRUNCATE ;
  V_MainTable := 'TMP_FCST_NETTED' ;
  PKG_UTIL.CREATELOG (V_PKGName,
                      V_ProName,
                      V_Step,
                      V_Action,
                      PKG_CONSTANT.CONS_MSG_INFO,
                      'START',
                      V_MainTable);

  V_SqlStr := 'TRUNCATE TABLE ' || V_MainTable ;
  EXECUTE IMMEDIATE V_SqlStr ;

  /*
   第一段逻辑处理结束,记录日志
  */
   PKG_UTIL.CREATELOG (V_PKGName,
                       V_ProName,
                       V_Step,
                       V_Action,
                       PKG_CONSTANT.CONS_MSG_INFO,
                       'Record Count: '|| SQL%ROWCOUNT,
                       V_MainTable);

  /*
   第二段逻辑描述,处理规则等
   Copy W1 to W4 Forecast from IN_SO_STOCk to TMP table
  */                       
  V_Step := '2';
  V_Action := PKG_CONSTANT.CONS_ACTION_INSERT ;
  V_MainTable := 'TMP_FCST_NETTED' ;

    PKG_UTIL.CREATELOG (V_PKGName,
                      V_ProName,
                      V_Step,
                      V_Action,
                      PKG_CONSTANT.CONS_MSG_INFO,
                      'START',
                      V_MainTable);

  -- Get UDF_FCST_VERSION_DATE and Next Sunday of UDF_FCST_VERSION_DATE
insert into temp_fcst_version (SITE_ID, FCSTVERDATE, NETTINGSTARTDATE, NETTINGENDDATE,REQ_DATE)
 SELECT fv.loc_id site_id, fv.fcstVerDate FcstVerDate
       , trunc(fv.fcstVerDate - to_char(fcstVerDate, 'D') +8) NettingStartDate
       , trunc(fv.fcstVerDate - to_char(fcstVerDate, 'D') +8) + (7 * 4) NettingEndDate
       , REQ_DATE
    FROM (select loc_id, min(UDF_FCST_VERSION_DATE) fcstVerDate ,min(REQ_DATE) REQ_DATE  from IN_SO_STOCK group by loc_id) FV
  ; 
commit;
 
  INSERT INTO TMP_FCST_NETTED
         ( FCST_ID
         , PRODUCT_ID
         , LOC_ID
         , REQ_QTY
         , REQ_DATE
         , UDF_FCST_VERSION_DATE
         , UDF_GEO
         , UDF_SUBGEO
         , REMAININGQTY
         , IS_BOUNDSITE
         , WK_GEO
         , WK_FCST_BUCKET
         , WK_BUCKET_SEQNO
         , WK_FCST_SEQNO
         , SO_QTY
         )
  SELECT   ifcst.FCST_ID||rownum
         , ifcst.PRODUCT_ID
         , ifcst.LOC_ID
         , ifcst.REQ_QTY
         , ifcst.REQ_DATE
         , ifcst.UDF_FCST_VERSION_DATE
         , ifcst.UDF_GEO
         , ifcst.UDF_SUBGEO
         , ifcst.REQ_QTY -- REMAININGQTY
         , CASE WHEN ifcst.LOC_ID in ('2011', '2061') THEN 1 -- "Bounded site"
                ELSE 0
           END IS_BOUNDSITE
         , ifcst.UDF_GEO || ifcst.UDF_SUBGEO -- WK_GEO
         , trunc(ifcst.REQ_DATE - to_char(ifcst.REQ_DATE, 'D') +1) -- WK_FCST_BUCKET: Last Sunday (Assumption: D=1 is Sunday)
         , (trunc(ifcst.REQ_DATE - to_char(ifcst.REQ_DATE, 'D') +1) - fv.NettingStartDate)/7 +1 -- WK_BUCKET_SEQNO (W1=1)
         , row_number() over(partition by PRODUCT_ID, LOC_ID, trunc(ifcst.REQ_DATE - to_char(ifcst.REQ_DATE, 'D') +1) 
                                 order by ifcst.REQ_DATE, ifcst.FCST_ID )  WK_FCST_SEQNO
         , 0 SO_QTY
   FROM IN_SO_STOCK ifcst , temp_fcst_version fv
  WHERE ifcst.loc_id=FV.SITE_ID and  ifcst.REQ_DATE BETWEEN fv.NettingStartDate and fv.NettingEndDate -1 -- from W1 to W4
  ; 
  commit;
  /*
   第二段逻辑处理结束,记录日志
  */
   PKG_UTIL.CREATELOG (V_PKGName,
                       V_ProName,
                       V_Step,
                       V_Action,
                       PKG_CONSTANT.CONS_MSG_INFO,
                       'Record Count: '|| SQL%ROWCOUNT,
                       V_MainTable);
                       

  /*
   第三段逻辑描述,处理规则等
   Update if corresponding SO(SalesOrder) is found
   Assumption:
    - SO will be ignored in case of "SO's CREATION_DATETIME < Fcst's FCST_VERSION_DATE"
    - Only Semi-product (ITEM_ID starts with '102') is fetched
    - If a BOM has several date effective components, use one with the newest effective date
  */                       
  V_Step := '3';
  V_Action := PKG_CONSTANT.CONS_ACTION_UPDATE ;
  V_MainTable := 'TMP_FCST_NETTED' ;
    PKG_UTIL.CREATELOG (V_PKGName,
                      V_ProName,
                      V_Step,
                      V_Action,
                      PKG_CONSTANT.CONS_MSG_INFO,
                      'START',
                      V_MainTable);

  MERGE INTO TMP_FCST_NETTED tgt
  USING ( 
          SELECT iso.SITEID
               , iso.CREATION_BUCKET
              -- , iso.UDF_GEO
              -- , iso.UDF_SUBGEO
               , ibd.ITEM_ID
               , sum(iso.ORDERED_QTY * ibd.USAGE_QTY) SO_QTY
               , 1 WK_FCST_SEQNO
           FROM (select SITEID, ITEM_ID, BOMID, ORDERED_QTY, CREATION_DATETIME
--                      , trunc(CREATION_DATETIME) - to_char(CREATION_DATETIME, 'D') +1 CREATION_BUCKET --Last Sunday (Assumption: D=1 is Sunday)
                      , CASE 
                         WHEN SO.CREATION_DATETIME <fv.REQ_DATE THEN trunc(fv.REQ_DATE - to_char(fv.REQ_DATE, 'D') +1)
                         ELSE trunc(SO.CREATION_DATETIME) - to_char(SO.CREATION_DATETIME, 'D') +1 
                        END CREATION_BUCKET --Last Sunday (Assumption: D=1 is Sunday)
                      --, UDF_GEO, UDF_SUBGEO
                   from IN_SALES_ORDER SO, temp_fcst_version fv
                  where SO.CREATION_DATETIME >=trunc(fv.FcstVerDate +7- to_char(fv.FcstVerDate, 'D') +1)  and SO.SITEID=fv.site_id
                ) iso
              , (select PARENT_ID,SITE_ID, ITEM_ID, USAGE_QTY
                   from TEMP_BOM_ITEM BD
                  where exists(select null from TMP_FCST_NETTED FC WHERE BD.ITEM_ID=FC.PRODUCT_ID) -- Semi-Product
                ) ibd
          WHERE  iso.ITEM_ID = ibd.PARENT_ID and  iso.SITEID = ibd.SITE_ID
          GROUP BY ibd.ITEM_ID, iso.SITEID, iso.CREATION_BUCKET
          --, iso.UDF_GEO, iso.UDF_SUBGEO
        ) src
     ON (    tgt.WK_FCST_SEQNO = src.WK_FCST_SEQNO
         AND tgt.PRODUCT_ID = src.ITEM_ID
         AND tgt.LOC_ID     = src.SITEID
         --AND nvl(tgt.UDF_GEO, 'X_DUMMY')    = nvl(src.UDF_GEO, 'X_DUMMY')
         --AND nvl(tgt.UDF_SUBGEO, 'X_DUMMY') = nvl(src.UDF_SUBGEO, 'X_DUMMY')
         AND tgt.WK_FCST_BUCKET = src.CREATION_BUCKET
        )
   WHEN MATCHED THEN
        UPDATE SET tgt.SO_QTY = src.SO_QTY
   ;

  /*
   第三段逻辑处理结束,记录日志
  */
   PKG_UTIL.CREATELOG (V_PKGName,
                       V_ProName,
                       V_Step,
                       V_Action,
                       PKG_CONSTANT.CONS_MSG_INFO,
                       'Record Count: '|| SQL%ROWCOUNT,
                       V_MainTable);
  commit;
  /*
   第四段逻辑描述,处理规则等
   With cursor loop about Item-Site-Geo combination, 
   get list of fcst and calculate along with sort order
  */                       
  V_Step := '4';
  V_Action := PKG_CONSTANT.CONS_ACTION_UPDATE ;
  V_MainTable := 'TMP_FCST_NETTED' ;
    PKG_UTIL.CREATELOG (V_PKGName,
                      V_ProName,
                      V_Step,
                      V_Action,
                      PKG_CONSTANT.CONS_MSG_INFO,
                      'START',
                      V_MainTable);

   FOR R1 IN CUR1_NETTINGKEY LOOP
   BEGIN
    -- Initialize variables
    V_Step := '4.01';
    array_TMP_FCST_NETTED := tbl_TMP_FCST_NETTED() ;

    -- Get record list associated with the key combination
    V_Step := '4.02';
    SELECT FCST_ID
         , PRODUCT_ID
         , LOC_ID
         , REQ_QTY
         , REQ_DATE
         , UDF_FCST_VERSION_DATE
         , UDF_GEO
         , UDF_SUBGEO
         , nvl(SO_QTY, 0) SO_QTY
         , nvl(REMAININGQTY, 0) REMAININGQTY
         , IS_BOUNDSITE
         , WK_GEO
         , WK_FCST_BUCKET
         , WK_BUCKET_SEQNO
         , WK_FCST_SEQNO
         , EXT_FORDATE
         , EXT_FORQTY
         , EXT_FORQTY1
         , EXT_FORQTY2
         , EXT_FORQTY3
         , EXT_FORQTY4
         , EXT_FORTEXT1
         , EXT_FORTEXT2
         , EXT_FORTEXT3
         , REMAININGSOQTY
         , STDPD
         , KZKFG
         , ORIG_SO_QTY
      BULK COLLECT INTO array_TMP_FCST_NETTED
      FROM TMP_FCST_NETTED
     WHERE PRODUCT_ID = r1.PRODUCT_ID
       AND LOC_ID = r1.LOC_ID
       --AND nvl(WK_GEO, 'X_DUMMY') = nvl(r1.WK_GEO, 'X_DUMMY')
     ORDER BY WK_BUCKET_SEQNO, WK_FCST_SEQNO
    ;

    -- Loop1 : Week by week calucation 
    FOR cnt_StartWeekSeq IN array_TMP_FCST_NETTED(array_TMP_FCST_NETTED.first).WK_BUCKET_SEQNO  .. v_NumWeekForNetting LOOP
      -- Initialize variables
      V_Step := '4.03';
      v_result_ALLOCQTY := 0 ;

      -- Loop2: Calculate if record falls in "netting period from cnt_StartWeekSeq" 
      FOR i IN array_TMP_FCST_NETTED.first .. array_TMP_FCST_NETTED.last LOOP
        -- Exit if WK_BUCKET_SEQNO exceeds netting period
        EXIT WHEN array_TMP_FCST_NETTED(i).WK_BUCKET_SEQNO > cnt_StartWeekSeq + v_NumBucketNetting -1 ;
        IF array_TMP_FCST_NETTED(i).WK_BUCKET_SEQNO >= cnt_StartWeekSeq THEN
         BEGIN
          -- Inside of netting period. Calculate Allocated Fcst Qty
          V_Step := '4.04';

          -- Assumption: SO is set only at WK_FCST_SEQNO   = 1
          --  [AllocatedQty for cnt_StartWeekSeq] = ([Previous Result] + [RemainingFcstQty] - [SO of cnt_StartWeekSeq])
          --  Remaining Fcst Qty = AllocatedQty or 0 (cannot be negative)
          IF     array_TMP_FCST_NETTED(i).WK_BUCKET_SEQNO = cnt_StartWeekSeq
             AND array_TMP_FCST_NETTED(i).WK_FCST_SEQNO   = 1
           THEN
             v_result_ALLOCQTY := array_TMP_FCST_NETTED(i).REMAININGQTY - array_TMP_FCST_NETTED(i).SO_QTY;
             --array_TMP_FCST_NETTED(i).SO_QTY:=0;
           ELSE
             v_result_ALLOCQTY := v_result_ALLOCQTY + array_TMP_FCST_NETTED(i).REMAININGQTY;
          END IF ;
       
          IF v_result_ALLOCQTY < 0 THEN
            array_TMP_FCST_NETTED(i).REMAININGQTY := 0 ;
          ELSE
            array_TMP_FCST_NETTED(i).REMAININGQTY := v_result_ALLOCQTY ;
            exit;
          END IF ;

          CASE -- For debugging
            WHEN cnt_StartWeekSeq = 1 THEN array_TMP_FCST_NETTED(i).EXT_FORQTY1 := v_result_ALLOCQTY ;
            WHEN cnt_StartWeekSeq = 2 THEN array_TMP_FCST_NETTED(i).EXT_FORQTY2 := v_result_ALLOCQTY ;
            WHEN cnt_StartWeekSeq = 3 THEN array_TMP_FCST_NETTED(i).EXT_FORQTY3 := v_result_ALLOCQTY ;
            WHEN cnt_StartWeekSeq = 4 THEN array_TMP_FCST_NETTED(i).EXT_FORQTY4 := v_result_ALLOCQTY ;
            ELSE NULL;
          END CASE ;

         END ;
        END IF ; -- end of IF array_TMP_FCST_NETTED(i).WK_BUCKET_SEQNO >= cnt_StartWeekSeq 

      END LOOP ; -- end of Loop2: FOR i 

    END LOOP ; -- end of Loop1: FOR cnt_StartWeekSeq

    -- Update TMP_FCST_NETTED table
    V_Step := '4.05';
    FORALL k IN array_TMP_FCST_NETTED.first .. array_TMP_FCST_NETTED.last
    UPDATE TMP_FCST_NETTED
       SET REMAININGQTY = array_TMP_FCST_NETTED(k).REMAININGQTY
         , EXT_FORQTY1  = array_TMP_FCST_NETTED(k).EXT_FORQTY1
         , EXT_FORQTY2  = array_TMP_FCST_NETTED(k).EXT_FORQTY2
         , EXT_FORQTY3  = array_TMP_FCST_NETTED(k).EXT_FORQTY3
         , EXT_FORQTY4  = array_TMP_FCST_NETTED(k).EXT_FORQTY4
     WHERE FCST_ID         = array_TMP_FCST_NETTED(k).FCST_ID
       AND WK_BUCKET_SEQNO = array_TMP_FCST_NETTED(k).WK_BUCKET_SEQNO
       AND WK_FCST_SEQNO   = array_TMP_FCST_NETTED(k).WK_FCST_SEQNO
     ;
    V_Count := V_Count + SQL%ROWCOUNT ;
 
   END ;
   END LOOP ; -- end of FOR R1 IN CUR_NETTINGGROUP

  -- Reset Step number as this is outside of loop
  V_Step := '4';

  /*
   第四段逻辑处理结束,记录日志
  */
   PKG_UTIL.CREATELOG (V_PKGName,
                       V_ProName,
                       V_Step,
                       V_Action,
                       PKG_CONSTANT.CONS_MSG_INFO,
--                       'Record Count: '|| SQL%ROWCOUNT,
                       'Record Count: '|| V_Count,
                       V_MainTable);

  /*
   Execute Final Commit.
  */

COMMIT ;

   PKG_UTIL.CREATELOG (V_PKGName,
                       V_ProName,
                       V_Step,
                       V_Action,
                       PKG_CONSTANT.CONS_MSG_INFO,
                       'COMMIT Executed',
                       V_MainTable);

  /*
   第五段逻辑描述,处理规则等
   Insert record for Forecast within Netting period
  */                       
  V_Step := '5';
  V_Action := PKG_CONSTANT.CONS_ACTION_INSERT ;
  V_MainTable := 'FORECASTDETAIL' ;

    PKG_UTIL.CREATELOG (V_PKGName,
                      V_ProName,
                      V_Step,
                      V_Action,
                      PKG_CONSTANT.CONS_MSG_INFO,
                      'START',
                      V_MainTable);
--20140824 modified by 谭湘 for 前两周的净预测要拆分到所在周的每一天
  INSERT INTO abppmgr.FORECASTDETAIL
       ( ENTERPRISE
       , ENGINE_ID
       , SITEID
       , SALESLEVEL
       , SALESNAME
       , PRODUCTLEVEL
       , PRODUCTNAME
       , STOCKINGLOCATION
       , FORECASTNAME
       , EFFSTARTDATE
       , EFFENDDATE
       , QUANTITY
       , QTYUOM
       , PRIORITY
       , PROPERTY
       , NOTES
       , SYS_CREATED_BY
       )
  SELECT V_Enterprise ENTERPRISE
       , V_EngineId ENGINE_ID
       , V_SiteID
       , 1 SALESLEVEL
       , 1 SALESNAME
       , 1 PRODUCTLEVEL
       , F.PRODUCT_ID PRODUCTNAME
       , '' STOCKINGLOCATION
       , F.FCST_ID|| D.WEEKDAY ||'_'||F.LOC_ID FORECASTNAME
       , F.REQ_DATE+D.WEEKDAY EFFSTARTDATE
       , F.REQ_DATE+D.WEEKDAY EFFENDDATE
       , TRUNC(F.REMAININGQTY/5)+CASE WHEN MOD(F.REMAININGQTY,5)-D.WEEKDAY>=0 THEN 1 ELSE 0 END QUANTITY
       , TM.UDF_MEINS
       , v_Priority + 1 - DENSE_RANK() OVER ( ORDER BY F.REQ_DATE )PRIORITY
       --Seine_FP Implementation Project_Function Spec_FP_POST_PROCESS2_V_0_6.doc 处理需求属性: (不包含Souring处理过的部份)
       , F.FCST_ID||'_'||F.LOC_ID||F.PRODUCT_ID-- ||'_'|| LOC_ID 
       , F.LOC_ID
       , V_ProName||'_'||V_Step
    FROM TMP_FCST_NETTED F,(select level WEEKDAY from dual connect by level<=5) D,IN_ITEM TM, TEMP_FCST_VERSION fv
   WHERE (TRUNC(F.REMAININGQTY/5)+CASE WHEN MOD(F.REMAININGQTY,5)-D.WEEKDAY>=0 THEN 1 ELSE 0 END) > 0 and F.REQ_DATE>=fv.NettingStartDate and F.REQ_DATE<fv.NettingStartDate+14
   and TM.item_id=F.product_id and F.LOC_ID=fv.site_id
   ORDER BY F.REQ_DATE, F.FCST_ID
  ;
   
  INSERT INTO abppmgr.FORECASTDETAIL
       ( ENTERPRISE
       , ENGINE_ID
       , SITEID
       , SALESLEVEL
       , SALESNAME
       , PRODUCTLEVEL
       , PRODUCTNAME
       , STOCKINGLOCATION
       , FORECASTNAME
       , EFFSTARTDATE
       , EFFENDDATE
       , QUANTITY
       , QTYUOM
       , PRIORITY
       , PROPERTY
       , NOTES
       , SYS_CREATED_BY
       )
  SELECT V_Enterprise ENTERPRISE
       , V_EngineId ENGINE_ID
       , V_SiteID
       , 1 SALESLEVEL
       , 1 SALESNAME
       , 1 PRODUCTLEVEL
       , F.PRODUCT_ID PRODUCTNAME
       , '' STOCKINGLOCATION
       , F.FCST_ID||'_'||F.LOC_ID FORECASTNAME
       , F.REQ_DATE EFFSTARTDATE
       , F.REQ_DATE EFFENDDATE
       , F.REMAININGQTY QUANTITY
       , TM.UDF_MEINS
       , v_Priority + 1 - DENSE_RANK() OVER ( ORDER BY F.REQ_DATE )PRIORITY
       --Seine_FP Implementation Project_Function Spec_FP_POST_PROCESS2_V_0_6.doc 处理需求属性: (不包含Souring处理过的部份)
       , F.FCST_ID||'_'||F.LOC_ID||F.PRODUCT_ID-- ||'_'|| LOC_ID 
       , F.LOC_ID
       , V_ProName||'_'||V_Step
    FROM TMP_FCST_NETTED F,  IN_ITEM TM,TEMP_FCST_VERSION fv
   WHERE F.REMAININGQTY > 0 and F.REQ_DATE>=fv.NettingStartDate+14
   AND TM.ITEM_ID=F.PRODUCT_ID  and F.LOC_ID=fv.site_id
   ORDER BY F.REQ_DATE, F.FCST_ID
  ;
  /*
   第五段逻辑处理结束,记录日志
  */
   PKG_UTIL.CREATELOG (V_PKGName,
                       V_ProName,
                       V_Step,
                       V_Action,
                       PKG_CONSTANT.CONS_MSG_INFO,
                       'Record Count: '|| SQL%ROWCOUNT,
                       V_MainTable);

  /*
   第六段逻辑描述,处理规则等
   Insert record for Forecast after Netting period
  */                       
  V_Step := '6';
  V_Action := PKG_CONSTANT.CONS_ACTION_INSERT ;
  V_MainTable := 'FORECASTDETAIL' ;

    PKG_UTIL.CREATELOG (V_PKGName,
                      V_ProName,
                      V_Step,
                      V_Action,
                      PKG_CONSTANT.CONS_MSG_INFO,
                      'START',
                      V_MainTable);
  -- Re-set Priority
  SELECT min(PRIORITY) - 1 -- get next number
    INTO v_Priority
    FROM abppmgr.FORECASTDETAIL
  ;

  INSERT INTO abppmgr.FORECASTDETAIL
       ( ENTERPRISE
       , ENGINE_ID
       , SITEID
       , SALESLEVEL
       , SALESNAME
       , PRODUCTLEVEL
       , PRODUCTNAME
       , STOCKINGLOCATION
       , FORECASTNAME
       , EFFSTARTDATE
       , EFFENDDATE
       , QUANTITY
       , QTYUOM
       , PRIORITY
       , PROPERTY
       , NOTES
       , SYS_CREATED_BY
       )
  SELECT V_Enterprise ENTERPRISE
       , V_EngineId ENGINE_ID
       , V_SiteID
       , 1 SALESLEVEL
       , 1 SALESNAME
       , 1 PRODUCTLEVEL
       , S.PRODUCT_ID PRODUCTNAME
       , '' STOCKINGLOCATION
       , S.FCST_ID|| '_' || S.LOC_ID || ROWNUM FORECASTNAME
       , S.REQ_DATE EFFSTARTDATE
       , S.REQ_DATE EFFENDDATE
       , S.REQ_QTY QUANTITY
       , TM.UDF_MEINS
       , v_Priority + 1 - DENSE_RANK() OVER ( ORDER BY S.REQ_DATE )PRIORITY
       --Seine_FP Implementation Project_Function Spec_FP_POST_PROCESS2_V_0_6.doc 处理需求属性: (不包含Souring处理过的部份)
       , S.FCST_ID|| '_' || S.LOC_ID||S.PRODUCT_ID-- ||'_'|| LOC_ID 
       , S.LOC_ID
       , V_ProName||'_'||V_Step
    FROM IN_SO_STOCK S , IN_ITEM TM,TEMP_FCST_VERSION fv
   WHERE S.REQ_QTY > 0 and S.product_id=tm.item_id
     AND S.REQ_DATE >= fv.NettingEndDate and S.LOC_ID=fv.site_id
   ORDER BY S.REQ_DATE, S.FCST_ID
  ;
    COMMIT;

  /*
   第六段逻辑处理结束,记录日志
  */
   PKG_UTIL.CREATELOG (V_PKGName,
                       V_ProName,
                       V_Step,
                       V_Action,
                       PKG_CONSTANT.CONS_MSG_INFO,
                       'Record Count: '|| SQL%ROWCOUNT,
                       V_MainTable);
       --Seine_FP Implementation Project_Function Spec_FP_POST_PROCESS2_V_0_6.doc 处理需求属性: (不包含Souring处理过的部份)
    INSERT INTO ABPPMGR.FORECASTDETAIL_P (FORECASTNAME,
                                      ATTRIBUTECLASS,
                                      ATTRIBUTE,
                                      EFFSTARTDATE,
                                      ENTERPRISE,
                                      PRODUCTLEVEL,
                                      PRODUCTNAME,
                                      SALESLEVEL,
                                      SALESNAME,
                                      RELATIONSHIP,
                                      VALUE,
                                      SCENARIO_ID,
                                      ENGINE_ID)
   SELECT FORECASTNAME,
          PROPERTY ATTRIBUTECLASS,
          'FACTORY' ATTRIBUTE,
          EFFSTARTDATE,
          ENTERPRISE,
          PRODUCTLEVEL,
          PRODUCTNAME,
          SALESLEVEL,
          SALESNAME,
          'EQ' RELATIONSHIP,
          NOTES VALUE,
          SCENARIO_ID,
          ENGINE_ID
     FROM ABPPMGR.FORECASTDETAIL;
     COMMIT ;
     
  V_Step := '7';
  V_Action := PKG_CONSTANT.CONS_ACTION_INSERT ;
  V_MainTable := 'FORECASTDETAIL_P' ;
   PKG_UTIL.CREATELOG (V_PKGName,
                       V_ProName,
                       V_Step,
                       V_Action,
                       PKG_CONSTANT.CONS_MSG_INFO,
                       'Record Count: '|| SQL%ROWCOUNT,
                       V_MainTable);
  /*
   Execute Final Commit.
  */
    --add start
    --ADD BY Landor ON 20180522 按单方案更改ATTRIBUTECLASS=ATTRIBUTECLASS+value
    UPDATE ABPPMGR.FORECASTDETAIL_P AP SET ATTRIBUTECLASS=AP.ATTRIBUTECLASS||':'||AP.VALUE;

    COMMIT;

    --c.    预测需求 新增两种类型 0 和100
    INSERT INTO ABPPMGR.FORECASTDETAIL_P (FORECASTNAME,
                                      ATTRIBUTECLASS,
                                      ATTRIBUTE,
                                      EFFSTARTDATE,
                                      ENTERPRISE,
                                      PRODUCTLEVEL,
                                      PRODUCTNAME,
                                      SALESLEVEL,
                                      SALESNAME,
                                      RELATIONSHIP,
                                      VALUE,
                                      SCENARIO_ID,
                                      ENGINE_ID)
         SELECT         FORECASTNAME,
                        SUBSTR(ATTRIBUTECLASS,1,INSTR(ATTRIBUTECLASS,':')-1)||':0'ATTRIBUTECLASS,
                        'ORDER_PLANNING' ATTRIBUTE,
                        EFFSTARTDATE,
                        ENTERPRISE,
                        PRODUCTLEVEL,
                        PRODUCTNAME,
                        SALESLEVEL,
                        SALESNAME,
                        RELATIONSHIP,
                        '0'VALUE,
                        SCENARIO_ID,
                        ENGINE_ID
               FROM  ABPPMGR.FORECASTDETAIL_P
      UNION              
         SELECT         FORECASTNAME,
                            SUBSTR(ATTRIBUTECLASS,1,INSTR(ATTRIBUTECLASS,':')-1)||':100'ATTRIBUTECLASS,
                            'ORDER_PLANNING' ATTRIBUTE,
                            EFFSTARTDATE,
                            ENTERPRISE,
                            PRODUCTLEVEL,
                            PRODUCTNAME,
                            SALESLEVEL,
                            SALESNAME,
                            RELATIONSHIP,
                            '100'VALUE,
                            SCENARIO_ID,
                            ENGINE_ID
          FROM  ABPPMGR.FORECASTDETAIL_P; 
     COMMIT;
    --end add



   PKG_UTIL.CREATELOG (V_PKGName,
                       V_ProName,
                       V_Step,
                       V_Action,
                       PKG_CONSTANT.CONS_MSG_INFO,
                       'COMMIT Executed',
                       V_MainTable);


EXCEPTION
   --总程序异常处理部分
   WHEN OTHERS
   THEN
      BEGIN
         ROLLBACK;
         EXITCODE := 1;
         
         e_ErrMessage := SUBSTR (SQLERRM, 1, 1000)
                      || SUBSTR (DBMS_UTILITY.FORMAT_ERROR_BACKTRACE, 1, 2000);
         PKG_UTIL.CREATELOG (V_PKGName,
                             V_ProName,
                             V_Step,
                             V_Action,
                             PKG_CONSTANT.CONS_MSG_EXCEPTION,
                             e_ErrMessage,
                             V_MainTable);
      END;

END FP_FCST;
/
View Code

 

FP_POST_PROCESS

CREATE OR REPLACE PROCEDURE STG.FP_POST_PROCESS (EXITCODE OUT NUMBER)
IS
   /***********************************************************************
          **  存储过程名称:  FP_POST_PROCESS
         **  存储过程创建日期: 2014/7/21
         **  存储过程创建人: Tanxiang
         **  目的:  处理一些附加逻辑
         1.    检验时间:目前所有採購前置時間及在途到料時間都不包含檢驗時間,需要把檢驗時間加上去。
        2.    动态安全库存: Calendar based Safety Lead Times for Procured Parts
        3.    保税非保税处理/验货客户处理: 原材設定屬性讓IN_ITEM_SITE.PROC_TYPE是E70或F71可以使用非保稅庫存
        4.   原材没有供货商主数据:当IN_ITEM_SITE.PROC_TYPE为外购,可是在IN_SUPPLIER_ITEM没数据时需要处理
        5    工艺路线及虚拟车间不全:当MST_OPRESOURCE.WORKCENTERNAME中没有对应到数据做处理

         **  输入参数:
         **  输出参数:EXITCODE 预留接口给外部调用者,让其知道存储过程的执行结果,0.预定逻辑执行完成; 1.遇到异常,预定逻辑未执行完成
         **  返回值:
         **  用法:
         **
         ** 修订版本:
         **  版本号     修订时间    修订人        描述
         **  ---------  ----------  ------------  ------------------------
         **    1.0      2014/7/10        Tanxiang 1. 创建存储过程.
         **   2.0      2014/12/02       Huangyangxiong 2.VMI物料的采购周期由0天修改为SAP系统维护的时间
         **   3.0      2016/12/22      Landor   2.取消物化视图
         **     4.0     2018.05.22      Landor  增加按单生产业务
    **********************************************************************/
   ----------------------- 以下定义存储过程使用的变量---------------------------------
   V_PKGName          VARCHAR2 (40) := 'PKG_FP';
   V_ProName          VARCHAR2 (40) := 'FP_POST_PROCESS';
   V_Action           VARCHAR2 (1);
   V_Step             NUMBER := 0;
   V_MainTable        VARCHAR2 (40);
   E_ErrMessage       VARCHAR2 (3000);
   V_Engine           NUMBER;
   V_Enterprise       VARCHAR2 (40);
   V_SiteID           VARCHAR2 (40);
   V_ScenarioID       NUMBER;
   V_OrganizationID   VARCHAR2 (40);
   --   V_SupplierOrganization   VARCHAR2 (40);
   --   V_PurOrganizationID      VARCHAR2 (40);
   --   V_SupplierEnterprise     VARCHAR2 (40);
   --   V_CalendarName           VARCHAR2 (40);
   --   V_ProcureBucketName      VARCHAR2 (40);
   ---   V_ItemGroup              VARCHAR2 (40);
   V_LocationID       VARCHAR2 (40) := 'DUMMY_LOC';
   V_CalendarType     VARCHAR2 (40) := 'PERCENTAGE_SUPPLY';
   V_WorkcenterName   VARCHAR2 (40) := 'DUMMY_RESOURCE';
   V_CurrentTime      DATE := SYSDATE;
   V_SupplierItemCount number;
----------------------- 以下是存储过程的主体------------------------------------------

BEGIN
   EXITCODE := 0;
   V_Engine := PKG_CONSTANT.CONS_FP_ENGINE;
   V_Enterprise := PKG_CONSTANT.CONS_ENTERPRISE;
   V_SiteID := PKG_CONSTANT.CONS_SITE_ZHUHAI;
   V_ScenarioID := 0;
   V_OrganizationID := 'ORG';
   --V_SupplierOrganization := 'ORG';
   --V_PurOrganizationID := 'ORG';
   --V_SupplierEnterprise := PKG_CONSTANT.CONS_ENTERPRISE;
   --V_ProcureBucketName := 'FP_PLAN_BUCKET';
   --V_ItemGroup := 'PROC';

   V_Step := V_Step + 1;
   V_Action := PKG_CONSTANT.CONS_ACTION_UPDATE;
   V_MainTable := 'MST_APPRSUPITEM,PURCHORDLINE';
   --检验时间:目前所有採購前置時間及在途到料時間都不包含檢驗時間,需要把檢驗時間加上去
   PKG_UTIL.CREATELOG (
      V_PKGName,                                       --Put package name here
      V_ProName,                                     --Put procedure name here
      V_Step,                                       --Step seqence No. if have
      V_Action,                                       --Action 'I','D','U','T'
      PKG_CONSTANT.CONS_MSG_INFO,     --Message tppe, 'SUCCESS' or 'EXCEPTION'
      'add IN_ITEM.EXAMINE_TIME to MST_APPRSUPITEM and PURCHORDLINE', --Any generic information here
      V_MainTable);                               --Major table name processed

   MERGE INTO ABPPMGR.MST_APPRSUPITEM M
        USING IN_ITEM IM
           ON (IM.ITEM_ID = M.ITEM)
   WHEN MATCHED
   THEN
      UPDATE SET
         M.AVGLEADTIME = NVL (M.AVGLEADTIME, 0) + NVL (IM.EXAMINE_TIME, 0),
         M.SYS_LAST_MODIFIED_BY = V_ProName,
         M.SYS_LAST_MODIFIED_DATE = V_CurrentTime;

   MERGE INTO ABPPMGR.PURCHORDLINE M
        USING IN_ITEM IM
           ON (IM.ITEM_ID = M.ITEM)
   WHEN MATCHED
   THEN
      UPDATE SET
         M.SCHEDULEDDELRYDATE =
            M.SCHEDULEDDELRYDATE + NVL (IM.EXAMINE_TIME, 0),
         M.SYS_LAST_MODIFIED_BY = V_ProName,
         M.SYS_LAST_MODIFIED_DATE = V_CurrentTime;

   COMMIT;
   V_Step := V_Step + 1;
   V_Action := PKG_CONSTANT.CONS_ACTION_UPDATE;
   V_MainTable := 'MST_APPRSUPITEM,PURCHORDLINE';
   --检验时间:目前所有採購前置時間及在途到料時間都不包含檢驗時間,需要把檢驗時間加上去
   PKG_UTIL.CREATELOG (V_PKGName,                      --Put package name here
                       V_ProName,                    --Put procedure name here
                       V_Step,                      --Step seqence No. if have
                       V_Action,                      --Action 'I','D','U','T'
                       PKG_CONSTANT.CONS_MSG_INFO, --Message tppe, 'SUCCESS' or 'EXCEPTION'
                       'process IN_ITEM.EXAMINE_TIME success.', --Any generic information here
                       V_MainTable);              --Major table name processed

   V_Step := V_Step + 1;
   V_Action := PKG_CONSTANT.CONS_ACTION_UPDATE;
   V_MainTable := 'MST_APPRSUPITEM,PURCHORDLINE';
   --检验时间:目前所有採購前置時間及在途到料時間都不包含檢驗時間,需要把檢驗時間加上去
   PKG_UTIL.CREATELOG (
      V_PKGName,                                       --Put package name here
      V_ProName,                                     --Put procedure name here
      V_Step,                                       --Step seqence No. if have
      V_Action,                                       --Action 'I','D','U','T'
      PKG_CONSTANT.CONS_MSG_INFO,     --Message tppe, 'SUCCESS' or 'EXCEPTION'
      'add IN_ITEM.EXAMINE_TIME to MST_APPRSUPITEM and PURCHORDLINE', --Any generic information here
      V_MainTable);                               --Major table name processed

     DELETE FROM ABPPMGR.MST_ITEMSITELTPARM ;
    COMMIT;
    INSERT INTO ABPPMGR.MST_ITEMSITELTPARM(SCENARIO_ID,
                  ENTERPRISE,
                  ENGINE_ID,
                  SITEID,
                  ITEM,
                  ESTIMATEDTIME,
                  TIMEUOM,
                  SYS_CREATED_BY)
    SELECT V_SCENARIOID,
                  V_ENTERPRISE,
                  V_ENGINE,
                  V_SITEID,
                   IIS.ITEM_ID, 
                   MAX(IIS.ESTIMATEDTIME) ESTIMATEDTIME,
                   'DAYS' TIMEUOM,
                   V_PRONAME
       FROM IN_ITEM_SITE IIS where IIS.ESTIMATEDTIME IS NOT NULL GROUP BY IIS.ITEM_ID;
      COMMIT;
IF 1=0 THEN
--start remark 2014.10.14 动态安全库存实现方式错误,修改为写入MST_ITEMSITELTPARM
  MERGE INTO ABPPMGR.MST_CALENDARMASTER M
        USING (SELECT 'CSLT' || LTRIM (T2.ITEM_ID,'0') || '_' ||T2.SUPPLIER_ID || REPLACE(T2.LOCATION_ID,T1.SITE_ID,NULL)
                         CALENDARNAME
              from   IN_ITEM_SITE T1,
               IN_SUPPLIER_ITEM T2
         WHERE T1.SITE_ID = SUBSTR(T2.SUPPLIER_ID,1,4) AND T1.ITEM_ID = T2.ITEM_ID AND T1.ESTIMATEDTIME IS NOT NULL) IM
           ON (M.CALENDARNAME = IM.CALENDARNAME)
   WHEN MATCHED
   THEN
      UPDATE SET
         M.SYS_ENT_STATE = 'ACTIVE',
         M.SYS_LAST_MODIFIED_BY = V_ProName,
         M.SYS_LAST_MODIFIED_DATE = V_CurrentTime
   WHEN NOT MATCHED
   THEN
      INSERT     (SCENARIO_ID,
                  ENTERPRISE,
                  ENGINE_ID,
                  CALENDARNAME,
                  SYS_CREATED_BY)
          VALUES (V_ScenarioID,
                  V_ENTERPRISE,
                  V_ENGINE,
                  IM.CALENDARNAME,
                  V_ProName);

   V_Step := V_Step + 1;                                                 -- 0;
   V_Action := PKG_CONSTANT.CONS_ACTION_MERGE;
   V_MainTable := 'MST_CALENDARMASTER';
   --记录MST_CALENDARMASTER导入日志
   PKG_UTIL.CREATELOG (V_PKGName,                      --Put package name here
                       V_ProName,                    --Put procedure name here
                       V_Step,                      --Step seqence No. if have
                       V_Action,                      --Action 'I','D','U','T'
                       PKG_CONSTANT.CONS_MSG_INFO, --Message tppe, 'SUCCESS' or 'EXCEPTION'
                       'Success: ' || SQL%ROWCOUNT || ' rows imported.', --Any generic information here
                       V_MainTable);              --Major table name processed
   COMMIT;

   SELECT COUNT(*) INTO V_SUPPLIERITEMCOUNT FROM ABPPMGR.MST_CALENDARDETAIL;
   COMMIT;
   V_Step := V_Step + 1;
   V_Action := PKG_CONSTANT.CONS_ACTION_INSERT;
   V_MainTable := 'MST_CALENDARDETAIL,MST_CALBASEDATTR';
   --3.    动态安全库存: Calendar based Safety Lead Times for Procured Parts
   PKG_UTIL.CREATELOG (
      V_PKGName,                                       --Put package name here
      V_ProName,                                     --Put procedure name here
      V_Step,                                       --Step seqence No. if have
      V_Action,                                       --Action 'I','D','U','T'
      PKG_CONSTANT.CONS_MSG_INFO,     --Message tppe, 'SUCCESS' or 'EXCEPTION'
      'prepare attribute data for IN_ITEM_SITE.ESTIMATEDTIME into MST_CALENDARDETAIL, MST_CALBASEDATTR', --Any generic information here
      V_MainTable);                               --Major table name processed
   V_CalendarType := 'PROCURE_LEAD_TIME';

     DELETE FROM ABPPMGR.MST_CALBASEDATTR WHERE SYS_CREATED_BY=V_PRONAME;
    COMMIT;
    DELETE FROM ABPPMGR.MST_CALENDARDETAIL WHERE SYS_CREATED_BY=V_PRONAME;
    COMMIT;
   INSERT INTO ABPPMGR.MST_CALENDARDETAIL (SCENARIO_ID,
                                           ENTERPRISE,
                                           ENGINE_ID,
                                           CALENDARNAME,
                                           CALENDARTYPE,
                                           EFFSTARTDATE,
                                           EFFENDDATE,
                                           SYS_LAST_MODIFIED_BY,
                                           PATTERNSEQ,
                                           SHIFTNUMBER,
                                           SYS_CREATED_BY)
        SELECT V_ScenarioID,
               V_ENTERPRISE,
               V_ENGINE,
               'CSLT' || LTRIM (M.ITEM_ID,'0') || '_' ||M.SUPPLIER_ID || REPLACE(M.LOCATION_ID,IM.SITE_ID,NULL) CALENDARNAME,
               V_CalendarType,
               IM.EFFECTIVE_START_DATE,
               IM.EFFECTIVE_END_DATE,
               IM.ITEM_ID||IM.SITE_ID SYS_LAST_MODIFIED_BY,
               V_SUPPLIERITEMCOUNT+ROWNUM PATTERNSEQ,
               1 SHIFTNUMBER,
               V_ProName SYS_CREATED_BY
          FROM IN_ITEM_SITE IM,
               IN_SUPPLIER_ITEM M
         WHERE IM.SITE_ID = substr(M.SUPPLIER_ID,1,4) AND IM.ITEM_ID = M.ITEM_ID AND IM.ESTIMATEDTIME IS NOT NULL;
   COMMIT;

    INSERT INTO ABPPMGR.MST_CALBASEDATTR (SCENARIO_ID,
                                          ENTERPRISE,
                                          ENGINE_ID,
                                          ATTRIBUTE,
                                          CALENDARNAME,
                                          PATTERNSEQ,
                                          SHIFTNUMBER,
                                          VALUE,
                                          VALUEUOM,
                                          SYS_CREATED_BY)
       SELECT CD.SCENARIO_ID,
              CD.ENTERPRISE,
              CD.ENGINE_ID,
              'PROCURE_LEAD_TIME' ATTRIBUTE,
              CD.CALENDARNAME,
              CD.PATTERNSEQ,
              CD.SHIFTNUMBER,
              IM.ESTIMATEDTIME * 24 * 60 * 60,
              'SECONDS' VALUEUOM,
              V_ProName SYS_CREATED_BY
         FROM ABPPMGR.MST_CALENDARDETAIL CD, IN_ITEM_SITE IM
        WHERE CD.SYS_LAST_MODIFIED_BY = IM.ITEM_ID || IM.SITE_ID
              AND IM.ESTIMATEDTIME IS NOT NULL;


   V_Step := V_Step + 1;
   V_Action := PKG_CONSTANT.CONS_ACTION_INSERT;
   V_MainTable := 'MST_CALENDARDETAIL,MST_CALBASEDATTR';
   --3.    动态安全库存: Calendar based Safety Lead Times for Procured Parts
   PKG_UTIL.CREATELOG (V_PKGName,                      --Put package name here
                       V_ProName,                    --Put procedure name here
                       V_Step,                      --Step seqence No. if have
                       V_Action,                      --Action 'I','D','U','T'
                       PKG_CONSTANT.CONS_MSG_INFO, --Message tppe, 'SUCCESS' or 'EXCEPTION'
                       'Success: ' || SQL%ROWCOUNT || ' rows imported.', --Any generic information here
                       V_MainTable);
    COMMIT;
    DELETE FROM ABPPMGR.MST_SUPPLIERCALENDAR M WHERE M.SYS_CREATED_BY=V_PRONAME;
    COMMIT;
    INSERT INTO ABPPMGR.MST_SUPPLIERCALENDAR (SCENARIO_ID,
                                             ENTERPRISE,
                                             SITEID,
                                             ENGINE_ID,
                                             --PURORGANIZATIONID,
                                             CALENDARNAME,
                                             CALENDARTYPE,
                                             ITEM,
                                             --ITEMGROUP,
                                             SUPPLIERID,
                                             SYS_CREATED_BY)
      SELECT V_ScenarioID,
               V_ENTERPRISE,
               V_SiteID,
               V_ENGINE,
               'CSLT' || LTRIM (M.ITEM_ID,'0') || '_' ||M.SUPPLIER_ID || REPLACE(M.LOCATION_ID,IM.SITE_ID,NULL)  CALENDARNAME,
               V_CALENDARTYPE CALENDARTYPE,
               M.ITEM_ID ITEM,
              M.SUPPLIER_ID SUPPLIERID,
              V_PRONAME SYS_CREATED_BY
          FROM IN_ITEM_SITE IM,
               IN_SUPPLIER_ITEM M
         WHERE IM.SITE_ID = SUBSTR(M.SUPPLIER_ID,1,4) AND IM.ITEM_ID = M.ITEM_ID AND IM.ESTIMATEDTIME IS NOT NULL;

   V_Step := V_Step + 1;                                                 -- 0;
   V_Action := PKG_CONSTANT.CONS_ACTION_INSERT;
   V_MainTable := 'MST_SUPPLIERCALENDAR';
   --记录MST_SUPPLIERCALENDAR导入日志
   PKG_UTIL.CREATELOG (V_PKGName,                      --Put package name here
                       V_ProName,                    --Put procedure name here
                       V_Step,                      --Step seqence No. if have
                       V_Action,                      --Action 'I','D','U','T'
                       PKG_CONSTANT.CONS_MSG_INFO, --Message tppe, 'SUCCESS' or 'EXCEPTION'
                       'Success: ' || SQL%ROWCOUNT || ' rows imported.', --Any generic information here
                       V_MainTable);              --Major table name processed
   COMMIT;
end if;
--end remark 2014.10.14 动态安全库存实现方式错误,修改为写入MST_ITEMSITELTPARM

   --工艺路线及虚拟车间不全:当MST_OPRESOURCE.WORKCENTERNAME中没有对应到数据做处理
   V_Step := V_Step + 1;
   V_Action := PKG_CONSTANT.CONS_ACTION_INSERT;
   V_MainTable := 'MST_OPRESOURCE';
   PKG_UTIL.CREATELOG (V_PKGName,                      --Put package name here
                       V_ProName,                    --Put procedure name here
                       V_Step,                      --Step seqence No. if have
                       V_Action,                      --Action 'I','D','U','T'
                       PKG_CONSTANT.CONS_MSG_INFO, --Message tppe, 'SUCCESS' or 'EXCEPTION'
                       'Start: Prepare master data.', --Any generic information here
                       V_MainTable);

   MERGE INTO ABPPMGR.MST_WORKCENTERMASTER M
        USING DUAL IM
           ON (M.WORKCENTERNAME = V_WorkcenterName)
   WHEN MATCHED
   THEN
      UPDATE SET
         M.SYS_ENT_STATE = 'ACTIVE',
         M.SYS_LAST_MODIFIED_BY = V_ProName,
         M.SYS_LAST_MODIFIED_DATE = V_CurrentTime
   WHEN NOT MATCHED
   THEN
      INSERT     (SCENARIO_ID,
                  ENTERPRISE,
                  SITEID,
                  ENGINE_ID,
                  LOCATIONID,
                  WORKCENTERNAME,SYS_CREATED_BY)
          VALUES (V_ScenarioID,
                  V_Enterprise,
                  V_SiteID,
                  V_Engine,
                  V_LocationID,
                  V_WorkcenterName,
                  V_ProName);

   MERGE INTO ABPPMGR.MST_RESOURCEMASTER M
        USING DUAL IM
           ON (M.RESOURCENAME = V_WorkcenterName)
   WHEN MATCHED
   THEN
      UPDATE SET
         M.SYS_ENT_STATE = 'ACTIVE',
         M.SYS_LAST_MODIFIED_BY = V_ProName,
         M.SYS_LAST_MODIFIED_DATE = V_CurrentTime
   WHEN NOT MATCHED
   THEN
      INSERT     (SCENARIO_ID,
                  ENTERPRISE,
                  SITEID,
                  ENGINE_ID,
                  RESOURCENAME,
                  SYS_CREATED_BY)
          VALUES (V_ScenarioID,
                  V_Enterprise,
                  V_SiteID,
                  V_Engine,
                  V_WorkcenterName,
                  V_ProName);

   MERGE INTO ABPPMGR.MST_WORKCENTERDETAIL M
        USING DUAL IM
           ON (    M.SCENARIO_ID = V_ScenarioID
               AND M.ENTERPRISE = V_Enterprise
               AND M.SITEID = V_SiteID
               AND M.RESOURCENAME = V_WorkcenterName
               AND M.WORKCENTERNAME = V_WorkcenterName)
   WHEN MATCHED
   THEN
      UPDATE SET
         M.SYS_ENT_STATE = 'ACTIVE',
         M.SYS_LAST_MODIFIED_BY = V_ProName,
         M.SYS_LAST_MODIFIED_DATE = V_CurrentTime
   WHEN NOT MATCHED
   THEN
      INSERT     (SCENARIO_ID,
                  ENTERPRISE,
                  SITEID,
                  ENGINE_ID,
                  RESOURCENAME,
                  WORKCENTERNAME,
                  AGGWORKCENTERNAME,
                  AGGRESOURCENAME,
                  AGGSITEID,
                  LOCATIONID,
                  SYS_CREATED_BY)
          VALUES (V_ScenarioID,
                  V_Enterprise,
                  V_SiteID,
                  V_Engine,
                  V_WorkcenterName,
                  V_WorkcenterName,
                  V_WorkcenterName,
                  V_WorkcenterName,
                  V_SiteID,
                  V_LocationID,
                  V_ProName);

   COMMIT;

    DELETE FROM ABPPMGR.MST_OPRESOURCE WHERE SYS_CREATED_BY=V_PRONAME;
    COMMIT;
   INSERT INTO ABPPMGR.MST_OPRESOURCE (SCENARIO_ID,
                                       ENTERPRISE,
                                       SITEID,
                                       ENGINE_ID,
                                       ROUTINGID,
                                       OPERATIONSEQ,
                                       --RESOURCEGROUPNAME,
                                       --RESOURCECONFIGURATIONID,
                                       RESOURCENAME,
                                       WORKCENTERNAME,
                                       SYS_CREATED_BY)
      SELECT V_ScenarioID,
             V_Enterprise,
             V_SiteID,
             V_Engine,
             M.ROUTINGID,
             M.OPERATIONSEQ,
             --V_ResourceGroup,
             --ITEM_ID,
             V_WorkcenterName,
             V_WorkcenterName,
             V_ProName SYS_CREATED_BY
        FROM ABPPMGR.MST_ROUTINGOPERATION M 
       WHERE M.SYS_ENT_STATE='ACTIVE' AND NOT EXISTS
                    (SELECT NULL
                       FROM ABPPMGR.MST_OPRESOURCE M2
                      WHERE M.ROUTINGID = M2.ROUTINGID
                            AND M.OPERATIONSEQ = M2.OPERATIONSEQ
                            AND M2.SYS_ENT_STATE='ACTIVE');

   V_Step := V_Step + 1;
   V_Action := PKG_CONSTANT.CONS_ACTION_INSERT;
   V_MainTable := 'MST_OPRESOURCE';
   PKG_UTIL.CREATELOG (V_PKGName,                      --Put package name here
                       V_ProName,                    --Put procedure name here
                       V_Step,                      --Step seqence No. if have
                       V_Action,                      --Action 'I','D','U','T'
                       PKG_CONSTANT.CONS_MSG_INFO, --Message tppe, 'SUCCESS' or 'EXCEPTION'
                       'Success: ' || SQL%ROWCOUNT || ' rows imported.', --Any generic information here
                       V_MainTable);
   COMMIT;
     --增加订单需求属性
    UPDATE ABPPMGR.SALESORDERLINE SOL SET SOL.PROPERTY=SOL.SALESORDERID|| '_' ||LTRIM(SOL.SOLINENUM,'0')
   WHERE NOT EXISTS(SELECT NULL FROM IN_SOURCING ISRC WHERE SOL.CUSTOMERID  = ISRC.CUSTOMER_ID
     AND SOL.ITEM = ISRC.FINISH_ITEM_ID);
    COMMIT;
   DELETE FROM  ABPPMGR.SALESORDERLINE_P WHERE SYS_CREATED_BY LIKE '%' || V_PRONAME || '%';
   COMMIT;
   
   --ADD BY Landor ON 20180522 删除SYS_CREATED_BY='AnDan'
   DELETE FROM  ABPPMGR.SALESORDERLINE_P WHERE SYS_CREATED_BY='AnDan';
   COMMIT;
   
   
     INSERT INTO ABPPMGR.SALESORDERLINE_P
       ( ENTERPRISE
       , ENGINE_ID
       , SALESORDERID
       , SOLINENUM
       , ATTRIBUTECLASS
       , ATTRIBUTE
       , RELATIONSHIP
       , VALUE
       , SYS_CREATED_BY
       )
  SELECT sol.ENTERPRISE
       , sol.ENGINE_ID
       , sol.SALESORDERID
       , sol.SOLINENUM
       , sol.PROPERTY  ATTRIBUTECLASS
       , 'FACTORY' -- ATTRIBUTE
       , 'EQ' --RELATIONSHIP
       , sol.LOCATIONID -- VALUE
       , V_ProName||'_'||V_Step
    FROM abppmgr.SALESORDERLINE sol
   WHERE not exists(select null from IN_SOURCING isrc where sol.CUSTOMERID  = isrc.CUSTOMER_ID
     AND sol.ITEM = isrc.FINISH_ITEM_ID )
  ; 
  
  -- start add
    ----ADD BY Landor ON 20180522 按单1.把原来的ATTRIBUTECLASS+:+ SALESORDERLINE_P.VALUE
    UPDATE ABPPMGR.SALESORDERLINE_P  SET ATTRIBUTECLASS=ATTRIBUTECLASS || ':'|| VALUE;
     commit;
     --add by Landor on 20180522 按单业务处理
     ----a.针对MTL订单处理
    INSERT INTO ABPPMGR.SALESORDERLINE_P
           ( ENTERPRISE
           , ENGINE_ID
           , SALESORDERID
           , SOLINENUM
           , ATTRIBUTECLASS
           , ATTRIBUTE
           , RELATIONSHIP
           , VALUE
           , SYS_CREATED_BY
           )
       --新增ATTRIBUTECLASS=ATTRIBUTECLASS:0 value=0
    SELECT   ENTERPRISE
           , ENGINE_ID
           , SALESORDERID
           , SOLINENUM
           , SUBSTR(ATTRIBUTECLASS,1,INSTR(ATTRIBUTECLASS,':')-1)||':0' ATTRIBUTECLASS 
           , 'ORDER_PLANNING'    ATTRIBUTE
           , RELATIONSHIP
           , '0'  VALUE
           , 'AnDan'
       FROM ABPPMGR.SALESORDERLINE_P AP
        WHERE AP.SALESORDERID LIKE '%MTL%'
    UNION
    --新增ATTRIBUTECLASS=ATTRIBUTECLASS:100 value=100
    SELECT   ENTERPRISE
           , ENGINE_ID
           , SALESORDERID
           , SOLINENUM
           , SUBSTR(ATTRIBUTECLASS,1,INSTR(ATTRIBUTECLASS,':')-1)||':100' ATTRIBUTECLASS 
           , 'ORDER_PLANNING'    ATTRIBUTE
           , RELATIONSHIP
           , '100'  VALUE
           , 'AnDan'
       FROM ABPPMGR.SALESORDERLINE_P AP
        WHERE AP.SALESORDERID LIKE '%MTL%'
    UNION
    --如果是按单生产产生的MTL工单新增一笔ATTRIBUTECLASS=ATTRIBUTECLASS:订单号订单行项 value=订单号订单行项
    --取消使用IN_SEMI_MO_LOCKED,改为IN_MO和IN"_SFCHEADER alter by landor on 20180619
    SELECT   ENTERPRISE
           , ENGINE_ID
           , SALESORDERID
           , SOLINENUM
           , SUBSTR(ATTRIBUTECLASS,1,INSTR(ATTRIBUTECLASS,':')-1)||':'||IL.SO_ID||IL.SO_LINE_ID ATTRIBUTECLASS 
           , 'ORDER_PLANNING'    ATTRIBUTE
           , RELATIONSHIP
           , IL.SO_ID||IL.SO_LINE_ID  VALUE
           , 'AnDan'
       FROM ABPPMGR.SALESORDERLINE_P AP
       JOIN IN_SFCHEADER IL
       ON SUBSTR(AP.SALESORDERID,INSTR(AP.SALESORDERID,'_')+1,INSTR(AP.SALESORDERID,'_',1,2)-INSTR(AP.SALESORDERID,'_')-1)
              =IL.MO_ID
        WHERE AP.SALESORDERID LIKE '%MTL%' AND IL.SO_ID IS NOT NULL
     UNION
     SELECT   ENTERPRISE
           , ENGINE_ID
           , SALESORDERID
           , SOLINENUM
           , SUBSTR(ATTRIBUTECLASS,1,INSTR(ATTRIBUTECLASS,':')-1)||':'||IL.SO_ID||IL.SO_LINE_ID ATTRIBUTECLASS 
           , 'ORDER_PLANNING'    ATTRIBUTE
           , RELATIONSHIP
           , IL.SO_ID||IL.SO_LINE_ID  VALUE
           , 'AnDan'
       FROM ABPPMGR.SALESORDERLINE_P AP
       JOIN IN_MO IL
       ON SUBSTR(AP.SALESORDERID,INSTR(AP.SALESORDERID,'_')+1,INSTR(AP.SALESORDERID,'_',1,2)-INSTR(AP.SALESORDERID,'_')-1)
              =IL.MO_ID
        WHERE AP.SALESORDERID LIKE '%MTL%'   AND IL.SO_ID IS NOT NULL
        ; 
    /*取消使用IN_SEMI_MO_LOCKED,改为IN_MO和IN"_SFCHEADER alter by landor on 20180619
    SELECT   ENTERPRISE 
           , ENGINE_ID
           , SALESORDERID
           , SOLINENUM
           , SUBSTR(ATTRIBUTECLASS,1,INSTR(ATTRIBUTECLASS,':')-1)||':'||IL.PEGGED_SO_ID||IL.PEGGED_SO_LINE_ID ATTRIBUTECLASS 
           , 'ORDER_PLANNING'    ATTRIBUTE
           , RELATIONSHIP
           , IL.PEGGED_SO_ID||IL.PEGGED_SO_LINE_ID  VALUE
           , 'AnDan'
       FROM ABPPMGR.SALESORDERLINE_P AP
       JOIN IN_SEMI_MO_LOCKED IL
       ON SUBSTR(AP.SALESORDERID,INSTR(AP.SALESORDERID,'_')+1,INSTR(AP.SALESORDERID,'_',1,2)-INSTR(AP.SALESORDERID,'_')-1)
              =IL.PEGGED_MO_ID
        WHERE AP.SALESORDERID LIKE '%MTL%';   */
     COMMIT;
     

     ---- 再把原来ATTRIBUTECLASS=ATTRIBUTECLASS:100改为=ATTRIBUTECLASS:200以及value=100改为200
     UPDATE ABPPMGR.SALESORDERLINE_P AP SET ATTRIBUTECLASS=SUBSTR(ATTRIBUTECLASS,1,INSTR(ATTRIBUTECLASS,':')-1)||':200',
                                                    VALUE='200'
         WHERE EXISTS(SELECT NULL FROM IN_MO IL
          WHERE SUBSTR(AP.SALESORDERID,INSTR(AP.SALESORDERID,'_')+1, INSTR(AP.SALESORDERID,'_',1,2) -INSTR(AP.SALESORDERID,'_')-1)=IL.MO_ID)
          AND AP.SALESORDERID LIKE '%MTL%'
          AND SUBSTR(AP.ATTRIBUTECLASS,-4)=':100';   
     COMMIT;   
     
     UPDATE ABPPMGR.SALESORDERLINE_P AP SET ATTRIBUTECLASS=SUBSTR(ATTRIBUTECLASS,1,INSTR(ATTRIBUTECLASS,':')-1)||':200',
                                                    VALUE='200'
         WHERE EXISTS(SELECT NULL FROM IN_SFCHEADER IL
          WHERE SUBSTR(AP.SALESORDERID,INSTR(AP.SALESORDERID,'_')+1, INSTR(AP.SALESORDERID,'_',1,2) -INSTR(AP.SALESORDERID,'_')-1)=IL.MO_ID)
          AND AP.SALESORDERID LIKE '%MTL%'
          AND SUBSTR(AP.ATTRIBUTECLASS,-4)=':100';   
     COMMIT;    
     
     --按单生产中带SO号和行号的,同时带了:0的,需要将0删除掉,避免无法占用库存add by landor on 20180619
    DELETE FROM ABPPMGR.SALESORDERLINE_P B 
    WHERE ATTRIBUTE='ORDER_PLANNING' AND B.ATTRIBUTECLASS LIKE 'MTL%' AND LENGTH(ATTRIBUTECLASS)=36  --长度为36代表:0
    AND EXISTS(SELECT NULL FROM ABPPMGR.SALESORDERLINE_P A 
                WHERE SUBSTR(A.ATTRIBUTE,1,34)=SUBSTR(B.ATTRIBUTE,1,34) 
                AND A.ATTRIBUTE='ORDER_PLANNING'  AND A.ATTRIBUTECLASS LIKE 'MTL%'  AND LENGTH(A.ATTRIBUTECLASS)=48); --长度为48代表带上SO号行号
    COMMIT;

     /*取消使用IN_SEMI_MO_LOCKED,改为IN_MO和IN"_SFCHEADER alter by landor on 20180619 
     UPDATE ABPPMGR.SALESORDERLINE_P AP SET ATTRIBUTECLASS=SUBSTR(ATTRIBUTECLASS,1,INSTR(ATTRIBUTECLASS,':')-1)||':200',
                                                    VALUE='200'
         WHERE EXISTS(SELECT NULL FROM IN_SEMI_MO_LOCKED IL
          WHERE SUBSTR(AP.SALESORDERID,INSTR(AP.SALESORDERID,'_')+1,
                              INSTR(AP.SALESORDERID,'_',1,2)
                               -INSTR(AP.SALESORDERID,'_')-1)=IL.PEGGED_MO_ID)
          AND AP.SALESORDERID LIKE '%MTL%'
          AND SUBSTR(AP.ATTRIBUTECLASS,-4)=':100';   */
          
      
      
    --b.    非按单生产订单 不存在与INORDERPLANNING表代表非按单生产
     INSERT INTO ABPPMGR.SALESORDERLINE_P
           ( ENTERPRISE
           , ENGINE_ID
           , SALESORDERID
           , SOLINENUM
           , ATTRIBUTECLASS
           , ATTRIBUTE
           , RELATIONSHIP
           , VALUE
           , SYS_CREATED_BY
           )
    ----当INORDERPLANNING和SALESORDERLINE_P表的soid,solineid不相同 
      -- let ATTRIBUTECLASS=ATTRIBUTECLASS:0 value=0以及ATTRIBUTECLASS=ATTRIBUTECLASS:100 value=100
    SELECT DISTINCT ENTERPRISE
          ,ENGINE_ID 
          ,SALESORDERID
          ,SOLINENUM
          ,SUBSTR(AE.ATTRIBUTECLASS,1,INSTR(AE.ATTRIBUTECLASS,':')-1)||':0' ATTRIBUTECLASS
          ,'ORDER_PLANNING'        ATTRIBUTE
          ,RELATIONSHIP
          ,'0'  VALUE
          ,'AnDan'
     FROM ABPPMGR.SALESORDERLINE_P AE 
        WHERE NOT EXISTS(SELECT NULL FROM  IN_ORDER_PLANNING IP
         WHERE IP.SO_ID      = AE.SALESORDERID
           AND IP.SO_LINE_ID = AE.SOLINENUM)
           AND AE.ATTRIBUTECLASS NOT LIKE '%MTL%'
    UNION
    ----当INORDERPLANNING和SALESORDERLINE_P表的soid,solineid不相同
      -- let ATTRIBUTECLASS=ATTRIBUTECLASS:0 value=0以及ATTRIBUTECLASS=ATTRIBUTECLASS:100 value=100
    SELECT DISTINCT ENTERPRISE
          ,ENGINE_ID 
          ,SALESORDERID
          ,SOLINENUM
          ,SUBSTR(AE.ATTRIBUTECLASS,1,INSTR(AE.ATTRIBUTECLASS,':')-1)||':100' ATTRIBUTECLASS
          ,'ORDER_PLANNING'        ATTRIBUTE
          ,RELATIONSHIP
          ,'100'  VALUE
          ,'AnDan' 
     FROM ABPPMGR.SALESORDERLINE_P AE 
        WHERE NOT EXISTS(SELECT NULL FROM  IN_ORDER_PLANNING IP
         WHERE IP.SO_ID      = AE.SALESORDERID
           AND IP.SO_LINE_ID = AE.SOLINENUM)
           AND AE.ATTRIBUTECLASS NOT LIKE '%MTL%';
     COMMIT;  
      
    --d.    按单生产订单:从IN_ORDER_PLANNING找到按单生产订单,然后新增数据到SALESORDERLINE_P   1代表真验货 0为假验货
     
     INSERT INTO ABPPMGR.SALESORDERLINE_P
           ( ENTERPRISE
           , ENGINE_ID
           , SALESORDERID
           , SOLINENUM
           , ATTRIBUTECLASS
           , ATTRIBUTE
           , RELATIONSHIP
           , VALUE
           , SYS_CREATED_BY
           )
    ----新增ATTRIBUTECLASS=ATTRIBUTECLASS:订单号订单行项 当order_Planning=0,1 value=订单号订单行项
    SELECT DISTINCT ENTERPRISE
          ,ENGINE_ID 
          ,SALESORDERID
          ,SOLINENUM
          ,SUBSTR(AE.ATTRIBUTECLASS,1,INSTR(AE.ATTRIBUTECLASS,':')-1)||':'||IP.SO_ID||SUBSTR(IP.SO_LINE_ID,1,INSTR(IP.SO_LINE_ID,'_')-1) ATTRIBUTECLASS
          ,'ORDER_PLANNING'        ATTRIBUTE
          ,RELATIONSHIP
          , IP.SO_ID||SUBSTR(IP.SO_LINE_ID,1,INSTR(IP.SO_LINE_ID,'_')-1)VALUE
          ,'AnDan'
     FROM ABPPMGR.SALESORDERLINE_P AE 
       JOIN IN_ORDER_PLANNING IP
         ON IP.SO_ID      = AE.SALESORDERID
         AND IP.SO_LINE_ID = AE.SOLINENUM
         AND AE.ATTRIBUTECLASS NOT LIKE '%MTL%'
         AND IP.ORDER_PLANNING IN(0,1)
    UNION
    ----新增ATTRIBUTECLASS=ATTRIBUTECLASS:200 当order_Planning=1 value=订单号订单行项 
    SELECT DISTINCT ENTERPRISE
          ,ENGINE_ID 
          ,SALESORDERID
          ,SOLINENUM
          ,SUBSTR(AE.ATTRIBUTECLASS,1,INSTR(AE.ATTRIBUTECLASS,':')-1)||':200' ATTRIBUTECLASS
          ,'ORDER_PLANNING'        ATTRIBUTE
          ,RELATIONSHIP
          ,'200'VALUE
          ,'AnDan'
     FROM ABPPMGR.SALESORDERLINE_P AE 
       JOIN IN_ORDER_PLANNING IP
         ON IP.SO_ID      = AE.SALESORDERID
         AND IP.SO_LINE_ID = AE.SOLINENUM
         AND AE.ATTRIBUTECLASS NOT LIKE '%MTL%'
         AND IP.ORDER_PLANNING IN(1,0)
     UNION
    ----新增ATTRIBUTECLASS=ATTRIBUTECLASS:0 当order_Planning=0 value=0
    SELECT DISTINCT ENTERPRISE
          ,ENGINE_ID 
          ,SALESORDERID
          ,SOLINENUM
          ,SUBSTR(AE.ATTRIBUTECLASS,1,INSTR(AE.ATTRIBUTECLASS,':')-1)||':0' ATTRIBUTECLASS
          ,'ORDER_PLANNING'        ATTRIBUTE
          ,RELATIONSHIP
          ,'0'VALUE
          ,'AnDan'
     FROM ABPPMGR.SALESORDERLINE_P AE 
       JOIN IN_ORDER_PLANNING IP
         ON IP.SO_ID      = AE.SALESORDERID
         AND IP.SO_LINE_ID = AE.SOLINENUM
         AND AE.ATTRIBUTECLASS NOT LIKE '%MTL%'
         AND IP.ORDER_PLANNING=0;     
     COMMIT;
    --高品质客户处理,多群组增新增相应的条数 ATTRIBUTECLASS=ATTRIBUTECLASS:10000-ID VALUE=10000-ID
    INSERT INTO ABPPMGR.SALESORDERLINE_P
           ( ENTERPRISE
           , ENGINE_ID
           , SALESORDERID
           , SOLINENUM
           , ATTRIBUTECLASS
           , ATTRIBUTE
           , RELATIONSHIP
           , VALUE
           , SYS_CREATED_BY
           )
    SELECT DISTINCT  ENTERPRISE
                    ,ENGINE_ID 
                    ,SALESORDERID
                    ,SOLINENUM
                    ,SUBSTR(AE.ATTRIBUTECLASS,1,INSTR(AE.ATTRIBUTECLASS,':')-1)||':'||TO_CHAR(10000-IC.ID) ATTRIBUTECLASS --优先级
                    ,'ORDER_PLANNING'        ATTRIBUTE
                    ,RELATIONSHIP
                    ,10000-IC.ID VALUE
                    ,'AnDan'
                 FROM IN_ORDER_PLANNING IP
                  JOIN IN_STOCK_CUSTOMER IC --客户对应的群组数据表
                  ON IP.CUSTOMER_ID=IC.CUSTOMER_ID
                  JOIN ABPPMGR.SALESORDERLINE_P AE
                   ON IP.SO_ID      = AE.SALESORDERID
                  AND IP.SO_LINE_ID = AE.SOLINENUM;
     COMMIT;  
      
    --endt add

    --解决按单真验货吃非限制库存的问题,清理需求表的属性值 
    --按单真验货的ORDER_PLANNING的需求的属性值不能为0  add by landor on 20180729
    DELETE FROM ABPPMGR.SALESORDERLINE_P A  
     WHERE  ATTRIBUTE='ORDER_PLANNING' AND VALUE='0' AND 
            EXISTS (SELECT * FROM IN_ORDER_PLANNING B 
                            WHERE A.SALESORDERID=B.SO_ID AND B.SO_LINE_ID=A.SOLINENUM --按单
                            AND B.ORDER_PLANNING='1'); --真验货
    COMMIT;   
    

      V_Step := V_Step + 1;
   V_Action := PKG_CONSTANT.CONS_ACTION_INSERT;
   V_MainTable := 'SALESORDERLINE_P';
   PKG_UTIL.CREATELOG (V_PKGName,                      --Put package name here
                       V_ProName,                    --Put procedure name here
                       V_Step,                      --Step seqence No. if have
                       V_Action,                      --Action 'I','D','U','T'
                       PKG_CONSTANT.CONS_MSG_INFO, --Message tppe, 'SUCCESS' or 'EXCEPTION'
                       'Success: ' || SQL%ROWCOUNT || ' rows imported.', --Any generic information here
                       V_MainTable);
   COMMIT;
   --ALTER BY HUANGYANGXIONG ON 2014/12/02  注释掉VMI物料的采购周期为0的设定
   /*
   UPDATE ABPPMGR.MST_APPRSUPITEM M SET M.AVGLEADTIME=0
    WHERE EXISTS (SELECT NULL FROM IN_SUPPLIER_ITEM IM
    WHERE  M.ITEM=IM.ITEM_ID AND M.SUPPLIERID=IM.SUPPLIER_ID and UDF_ITEM_TYPE_ID='1');
    COMMIT;
    */
   DELETE FROM ABPPMGR.MST_APPRSUPITEM_P;--删除供应商与物料对应关系属性数据
   COMMIT;
   UPDATE ABPPMGR.MST_APPRSUPITEM M SET M.PROPERTY=M.ITEM|| '_'|| SUBSTR(M.SUPPLIERID,1,4);--更新属性数据
   COMMIT;

    INSERT INTO ABPPMGR.MST_APPRSUPITEM_P (ENGINE_ID,
                                           SUPPLIERID,
                                           ITEM,
                                           ITEMGROUP,
                                           ATTRIBUTECLASS,
                                           ENTERPRISE,
                                           PURORGANIZATIONID,
                                           SITEID,
                                           ITEMENTERPRISE,
                                           EFFSTARTDATE,
                                           SCENARIO_ID,
                                           ATTRIBUTE,
                                           RELATIONSHIP,
                                           VALUE)
       SELECT ENGINE_ID,
              SUPPLIERID,
              ITEM,
              ITEMGROUP,
              PROPERTY ATTRIBUTECLASS,
              ENTERPRISE,
              PURORGANIZATIONID,
              SITEID,
              ITEMENTERPRISE,
              EFFSTARTDATE,
              SCENARIO_ID,
              'FACTORY' ATTRIBUTE,
              'EQ' RELATIONSHIP,
              SUBSTR(SUPPLIERID,1,4) VALUE 
         FROM ABPPMGR.MST_APPRSUPITEM M 
         WHERE NOT EXISTS(SELECT NULL FROM IN_ITEM_SITE IM
          WHERE IM.ITEM_ID=M.ITEM AND IM.SITE_ID=SUBSTR(M.SUPPLIERID,1,4)
          AND  TRIM(IM.PROC_TYPE) || TRIM(IM.SP_PROC_TYPE) IN ('E70'));
    COMMIT;

    EXECUTE IMMEDIATE 'truncate table TEMP_ITEM_UOM';
    INSERT INTO TEMP_ITEM_UOM(SITE_ID,parent_id, ITEM_ID, QTY_UOM,LOC_ID)
      SELECT SUBSTR(BH.BOM_ID,INSTR(BH.BOM_ID,'_')+1,4) SITE_ID,BH.product_id,BD2.ITEM_ID
      ,MAX(BD2.USAGE_QTY_UOM) QTY_UOM,MAX(BD2.LOC_ID) LOC_ID
    FROM IN_BOM_DETAILS BD, IN_BOM_HEADER BH,IN_BOM_DETAILS BD2, IN_BOM_HEADER BH2
     where BH.BOM_ID=BD.bom_id and BH.MO_BOM_MARK='0'  and BD.IS_PHANTOM='1'
     and BH2.BOM_ID=BD2.bom_id and BH2.MO_BOM_MARK='0' and BD.item_id=BH2.PRODUCT_ID
     and SUBSTR(BH2.BOM_ID,INSTR(BH2.BOM_ID,'_')+1,4) =SUBSTR(BH.BOM_ID,INSTR(BH.BOM_ID,'_')+1,4) 
    GROUP BY  BH.product_id,SUBSTR(BH.BOM_ID,INSTR(BH.BOM_ID,'_')+1,4),BD2.ITEM_ID;
    COMMIT;
        INSERT INTO TEMP_ITEM_UOM(SITE_ID,parent_id, ITEM_ID, QTY_UOM,LOC_ID)
      SELECT SUBSTR(BH.BOM_ID,INSTR(BH.BOM_ID,'_')+1,4) SITE_ID,BH.product_id,BD2.FOLLOW_UP_MATERIAL
      ,MAX(BD2.USAGE_QTY_UOM) QTY_UOM,MAX(BD2.LOC_ID) LOC_ID
    FROM IN_BOM_DETAILS BD, IN_BOM_HEADER BH,IN_BOM_DETAILS BD2, IN_BOM_HEADER BH2
     where BH.BOM_ID=BD.bom_id and BH.MO_BOM_MARK='0'  and BD.IS_PHANTOM='1'
     and BH2.BOM_ID=BD2.bom_id and BH2.MO_BOM_MARK='0' and BD.item_id=BH2.PRODUCT_ID
     and SUBSTR(BH2.BOM_ID,INSTR(BH2.BOM_ID,'_')+1,4) =SUBSTR(BH.BOM_ID,INSTR(BH.BOM_ID,'_')+1,4) 
         and not exists (select null from TEMP_ITEM_UOM TU where TU.SITE_ID=SUBSTR(BH.BOM_ID,INSTR(BH.BOM_ID,'_')+1,4) 
    and tu.parent_id=BH.product_id and  tu.ITEM_ID=BD2.FOLLOW_UP_MATERIAL)
     and exists( select null from in_item TM where Tm.item_id=BD2.FOLLOW_UP_MATERIAL)
    GROUP BY  BH.product_id,SUBSTR(BH.BOM_ID,INSTR(BH.BOM_ID,'_')+1,4),BD2.FOLLOW_UP_MATERIAL;
    COMMIT;
    INSERT INTO TEMP_ITEM_UOM(SITE_ID,parent_id, ITEM_ID, QTY_UOM,LOC_ID)
    SELECT SUBSTR(BH.BOM_ID,INSTR(BH.BOM_ID,'_')+1,4) SITE_ID,BH.product_id,BD.FOLLOW_UP_MATERIAL
    ,MAX(BD.USAGE_QTY_UOM) QTY_UOM,MAX(BD.LOC_ID) LOC_ID
    FROM IN_BOM_DETAILS BD, IN_BOM_HEADER BH where BH.BOM_ID=BD.bom_id and BH.MO_BOM_MARK='0' 
    and not exists (select null from TEMP_ITEM_UOM TU where TU.SITE_ID=SUBSTR(BH.BOM_ID,INSTR(BH.BOM_ID,'_')+1,4) 
    and tu.parent_id=BH.product_id and  tu.ITEM_ID=BD.FOLLOW_UP_MATERIAL)
    and exists( select null from in_item TM where Tm.item_id=BD.FOLLOW_UP_MATERIAL)
    GROUP BY  BH.product_id,SUBSTR(BH.BOM_ID,INSTR(BH.BOM_ID,'_')+1,4),BD.FOLLOW_UP_MATERIAL;
        COMMIT;
    INSERT INTO TEMP_ITEM_UOM(SITE_ID,parent_id, ITEM_ID, QTY_UOM,LOC_ID)
    SELECT SUBSTR(BH.BOM_ID,INSTR(BH.BOM_ID,'_')+1,4) SITE_ID,BH.product_id,BD.ITEM_ID
    ,MAX(BD.USAGE_QTY_UOM) QTY_UOM,MAX(BD.LOC_ID) LOC_ID
    FROM IN_BOM_DETAILS BD, IN_BOM_HEADER BH where BH.BOM_ID=BD.bom_id and BH.MO_BOM_MARK='0' 
    and not exists (select null from TEMP_ITEM_UOM TU where TU.SITE_ID=SUBSTR(BH.BOM_ID,INSTR(BH.BOM_ID,'_')+1,4) 
    and tu.parent_id=BH.product_id and  tu.ITEM_ID=BD.item_id)
    GROUP BY  BH.product_id,SUBSTR(BH.BOM_ID,INSTR(BH.BOM_ID,'_')+1,4),BD.ITEM_ID;
        COMMIT;
        
   
    --MTL加工辅助件报short问题修复 add by landor on 20180809
    DELETE FROM  ABPPMGR.SALESORDERLINE_P A 
    WHERE A.ATTRIBUTECLASS LIKE 'MTL%' AND A.ATTRIBUTE='ORDER_PLANNING' 
        AND EXISTS (SELECT NULL FROM IN_ITEM B WHERE B.ITEM_ID=A.SOLINENUM
        AND FAMILY_ID IN ('102018000','402068001','402071000','402068004','402068005','402068006','402068010','402068002','502003004','502502002')) ;
    COMMIT;    
   
   

   
   
   /*    取消刷新物化视图 add by Landor on 20161222 
        
   --refresh ODS data to Materialized View
   --add by huangyangxiong on 20150430  update service lock to 'NO' status
   UPDATE ABPPMGR.SERVICE_LOCK SET IS_LOCKED = 'NO';
   
   COMMIT;
   
ABPPMGR.UPDATE_SNAPSHOT_COMPLETE();
   COMMIT;
   
   */
   
      V_Step := V_Step + 1;
   V_Action := PKG_CONSTANT.CONS_ACTION_INSERT;
   V_MainTable := 'ALL';
   PKG_UTIL.CREATELOG (V_PKGName,                      --Put package name here
                       V_ProName,                    --Put procedure name here
                       V_Step,                      --Step seqence No. if have
                       V_Action,                      --Action 'I','D','U','T'
                       PKG_CONSTANT.CONS_MSG_INFO, --Message tppe, 'SUCCESS' or 'EXCEPTION'
                       'execute procedure UPDATE_SNAPSHOT_COMPLETE compeleted.', --Any generic information here
                       V_MainTable);
EXCEPTION
   --总程序异常处理部分
   WHEN OTHERS
   THEN
      BEGIN
         ROLLBACK;
         EXITCODE := 1;
         e_ErrMessage :=
            SUBSTR (SQLERRM, 1, 1000)
            || SUBSTR (DBMS_UTILITY.FORMAT_ERROR_BACKTRACE, 1, 2000);
         PKG_UTIL.CREATELOG (V_PKGName,
                             V_ProName,
                             V_Step,
                             V_Action,
                             PKG_CONSTANT.CONS_MSG_EXCEPTION,
                             e_ErrMessage,
                             V_MainTable);
      END;
END;
/
View Code

 

FP_OUTMO_SapToFP 此存储过程用于回写后数据写入ZMO——BOM

CREATE OR REPLACE PROCEDURE STG.FP_OUTMO_SapToFP (EXITCODE OUT NUMBER)
IS
   /***********************************************************************
         **  存储过程名称:  FP_OUTMO_SapToFP
        **  存储过程创建日期:  2018/8/25
        **  存储过程创建人: Landor
        **  目的:  物料主娄据
        **  输入参数: 无
        **  输出参数:EXITCODE 预留接口给外部调用者,让其知道存储过程的执行结果,0.预定逻辑执行完成; 1.遇到异常,预定逻辑未执行完成
        **  返回值:
        **  用法:
        **
        ** 修订版本:
        **  版本号     修订时间    修订人        描述
        **  ---------  ----------  ------------  ------------------------
        **    1.0      2018/8/25   Landor  1. 新建.
   **********************************************************************/

   E_ErrMessage   VARCHAR2 (3000);
BEGIN
   EXITCODE := 0;

    --backup data
    INSERT INTO OUT_MO_WEBSERVICE_SAPTEMP(PRODUCTIONORDID,MOID,ERDAT,CPUTM,QTYORDERED,ITEM)
    SELECT  PRODUCTIONORDID,MOID,ERDAT,CPUTM,QTYORDERED ,ITEM FROM OUT_MO_WEBSERVICE_SAP;
    COMMIT;
    
   --清空IN表
   EXECUTE IMMEDIATE 'TRUNCATE TABLE OUT_MO_WEBSERVICE_SAP';

--将SAP回写MO后的日志数据写入FP,用于FP写于ZMO_BOM表
    INSERT INTO OUT_MO_WEBSERVICE_SAP(PRODUCTIONORDID,MOID,ERDAT,CPUTM,QTYORDERED,ITEM)
    SELECT MRNUM,AUFNR,ERDAT,CPUTM,MENGE,MATNR  FROM SAPSR3.ZTPO_PP003B@SAP_SEP  A WHERE A.ERDAT=TO_CHAR(SYSDATE,'YYYYMMDD');
    COMMIT;

    
    --重新将2022 2023工厂数据写进表中,用于后面数据写入ZMO_BOM中缺少此工厂数据 add by landor on 20180830
    INSERT INTO STG.TEMP_OUT_MO(PRODUCTIONORDID, SO_ID, SO_LINE_ID, PEGGED_MO_ID, ITEM, QTYORDERED, CREATION_DATE, LOC_ID, PLANNEDSTARTDATE, PLANNEDENDDATE, QTYUOM, CUSTOMER_ID, BOM_ID)
    SELECT PRODUCTIONORDID, LTRIM(SO_ID,'0') AS SO_ID, SO_LINE_ID, PEGGED_MO_ID, ITEM, QTYORDERED, CREATION_DATE, LOC_ID, PLANNEDSTARTDATE, PLANNEDENDDATE, QTYUOM, CUSTOMER_ID, BOM_ID FROM STG.OUT_MO
    WHERE OUT_MO.LOC_ID IN( '2023','2022');    
    COMMIT;


   
EXCEPTION
   --EXCEPTIONS
   WHEN OTHERS
   THEN
      BEGIN
         ROLLBACK;
         EXITCODE := 1;

         e_ErrMessage :=
               SUBSTR (SQLERRM, 1, 1000)
            || SUBSTR (DBMS_UTILITY.FORMAT_ERROR_BACKTRACE, 1, 2000);
         PKG_UTIL.CREATELOG ('FP_OUTMO_SapToFP',
                             'FP_OUTMO_SapToFP',
                             '1',
                             'INSERT',
                             PKG_CONSTANT.CONS_MSG_EXCEPTION,
                             e_ErrMessage,
                             'FP_OUTMO_SapToFP');
      END;
END;
/
View Code

 

 

V_OUT_MO_SCHEDULING

DROP VIEW STG.V_OUT_MO_SCHEDULING;

/* Formatted on 2018/8/30 17:35:18 (QP5 v5.227.12220.39754) */
CREATE OR REPLACE FORCE VIEW STG.V_OUT_MO_SCHEDULING
(
   AUFNR,
   ZARBP,
   DEPT_ID,
   MENGE,
   MEINS,
   DATUM,
   STATUS,
   SO_ID,
   SO_LINE_ID,
   SITE_ID,
   MRNUM
)
AS
   SELECT RES.MOID AUFNR,                               --RES.PEGGED_ID AUFNR,
          MO.ZARBP,
          LC.DEPT_ID,
          MO.MENGE,
          MO.MEINS,
          MO.DATUM,
          'NEW' STATUS,
          MO.SO_ID,
          MO.SO_LINE_ID,
          MO.SITE_ID,
          MO.AUFNR MRNUM
     FROM OUT_MO_SCHEDULING MO,
          OUT_MO_WEBSERVICE_SAP RES, --OUT_MO_RES RES, alter by landor on 20180822按单上线后需要更改此表
          IN_LINE_CAP LC,
          IN_ITEM TM
    WHERE     RES.PRODUCTIONORDID = MO.AUFNR         -- RES.FP_MRID = MO.AUFNR
          AND LC.FAMILY_ID = TM.FAMILY_ID
          AND MO.ZARBP = LC.LINE_ID
          AND RES.QTYORDERED = MO.MENGE
          AND TM.ITEM_ID = RES.ITEM
   UNION ALL
   SELECT IM.MO_ID AUFNR,
          MO.ZARBP,
          LC.DEPT_ID,
          MO.MENGE,
          MO.MEINS,
          MO.DATUM,
          'OLD' STATUS,
          IM.SO_ID,
          IM.SO_LINE_ID,
          IM.SITEID SITE_ID,
          NULL MRNUM
     FROM OUT_MO_SCHEDULING MO,
          IN_MO IM,
          IN_LINE_CAP LC,
          IN_ITEM TM
    WHERE     IM.MO_ID = MO.AUFNR
          AND MO.DATUM = TRUNC (SYSDATE) + 1
          AND LC.FAMILY_ID = TM.FAMILY_ID
          AND MO.ZARBP = LC.LINE_ID
          AND TM.ITEM_ID = IM.ITEM_ID;
View Code

 2018年8月28日更新

CREATE OR REPLACE PROCEDURE STG.FP_POST_PROCESS (EXITCODE OUT NUMBER)
IS
   /***********************************************************************
          **  存储过程名称:  FP_POST_PROCESS
         **  存储过程创建日期: 2014/7/21
         **  存储过程创建人: Tanxiang
         **  目的:  处理一些附加逻辑
         1.    检验时间:目前所有採購前置時間及在途到料時間都不包含檢驗時間,需要把檢驗時間加上去。
        2.    动态安全库存: Calendar based Safety Lead Times for Procured Parts
        3.    保税非保税处理/验货客户处理: 原材設定屬性讓IN_ITEM_SITE.PROC_TYPE是E70或F71可以使用非保稅庫存
        4.   原材没有供货商主数据:当IN_ITEM_SITE.PROC_TYPE为外购,可是在IN_SUPPLIER_ITEM没数据时需要处理
        5    工艺路线及虚拟车间不全:当MST_OPRESOURCE.WORKCENTERNAME中没有对应到数据做处理

         **  输入参数:
         **  输出参数:EXITCODE 预留接口给外部调用者,让其知道存储过程的执行结果,0.预定逻辑执行完成; 1.遇到异常,预定逻辑未执行完成
         **  返回值:
         **  用法:
         **
         ** 修订版本:
         **  版本号     修订时间    修订人        描述
         **  ---------  ----------  ------------  ------------------------
         **    1.0      2014/7/10        Tanxiang 1. 创建存储过程.
         **   2.0      2014/12/02       Huangyangxiong 2.VMI物料的采购周期由0天修改为SAP系统维护的时间
         **   3.0      2016/12/22      Landor   2.取消物化视图
         **     4.0     2018.05.22      Landor  增加按单生产业务
    **********************************************************************/
   ----------------------- 以下定义存储过程使用的变量---------------------------------
   V_PKGName          VARCHAR2 (40) := 'PKG_FP';
   V_ProName          VARCHAR2 (40) := 'FP_POST_PROCESS';
   V_Action           VARCHAR2 (1);
   V_Step             NUMBER := 0;
   V_MainTable        VARCHAR2 (40);
   E_ErrMessage       VARCHAR2 (3000);
   V_Engine           NUMBER;
   V_Enterprise       VARCHAR2 (40);
   V_SiteID           VARCHAR2 (40);
   V_ScenarioID       NUMBER;
   V_OrganizationID   VARCHAR2 (40);
   --   V_SupplierOrganization   VARCHAR2 (40);
   --   V_PurOrganizationID      VARCHAR2 (40);
   --   V_SupplierEnterprise     VARCHAR2 (40);
   --   V_CalendarName           VARCHAR2 (40);
   --   V_ProcureBucketName      VARCHAR2 (40);
   ---   V_ItemGroup              VARCHAR2 (40);
   V_LocationID       VARCHAR2 (40) := 'DUMMY_LOC';
   V_CalendarType     VARCHAR2 (40) := 'PERCENTAGE_SUPPLY';
   V_WorkcenterName   VARCHAR2 (40) := 'DUMMY_RESOURCE';
   V_CurrentTime      DATE := SYSDATE;
   V_SupplierItemCount number;
----------------------- 以下是存储过程的主体------------------------------------------

BEGIN
   EXITCODE := 0;
   V_Engine := PKG_CONSTANT.CONS_FP_ENGINE;
   V_Enterprise := PKG_CONSTANT.CONS_ENTERPRISE;
   V_SiteID := PKG_CONSTANT.CONS_SITE_ZHUHAI;
   V_ScenarioID := 0;
   V_OrganizationID := 'ORG';
   --V_SupplierOrganization := 'ORG';
   --V_PurOrganizationID := 'ORG';
   --V_SupplierEnterprise := PKG_CONSTANT.CONS_ENTERPRISE;
   --V_ProcureBucketName := 'FP_PLAN_BUCKET';
   --V_ItemGroup := 'PROC';

   V_Step := V_Step + 1;
   V_Action := PKG_CONSTANT.CONS_ACTION_UPDATE;
   V_MainTable := 'MST_APPRSUPITEM,PURCHORDLINE';
   --检验时间:目前所有採購前置時間及在途到料時間都不包含檢驗時間,需要把檢驗時間加上去
   PKG_UTIL.CREATELOG (
      V_PKGName,                                       --Put package name here
      V_ProName,                                     --Put procedure name here
      V_Step,                                       --Step seqence No. if have
      V_Action,                                       --Action 'I','D','U','T'
      PKG_CONSTANT.CONS_MSG_INFO,     --Message tppe, 'SUCCESS' or 'EXCEPTION'
      'add IN_ITEM.EXAMINE_TIME to MST_APPRSUPITEM and PURCHORDLINE', --Any generic information here
      V_MainTable);                               --Major table name processed

   MERGE INTO ABPPMGR.MST_APPRSUPITEM M
        USING IN_ITEM IM
           ON (IM.ITEM_ID = M.ITEM)
   WHEN MATCHED
   THEN
      UPDATE SET
         M.AVGLEADTIME = NVL (M.AVGLEADTIME, 0) + NVL (IM.EXAMINE_TIME, 0),
         M.SYS_LAST_MODIFIED_BY = V_ProName,
         M.SYS_LAST_MODIFIED_DATE = V_CurrentTime;

   MERGE INTO ABPPMGR.PURCHORDLINE M
        USING IN_ITEM IM
           ON (IM.ITEM_ID = M.ITEM)
   WHEN MATCHED
   THEN
      UPDATE SET
         M.SCHEDULEDDELRYDATE =
            M.SCHEDULEDDELRYDATE + NVL (IM.EXAMINE_TIME, 0),
         M.SYS_LAST_MODIFIED_BY = V_ProName,
         M.SYS_LAST_MODIFIED_DATE = V_CurrentTime;

   COMMIT;
   V_Step := V_Step + 1;
   V_Action := PKG_CONSTANT.CONS_ACTION_UPDATE;
   V_MainTable := 'MST_APPRSUPITEM,PURCHORDLINE';
   --检验时间:目前所有採購前置時間及在途到料時間都不包含檢驗時間,需要把檢驗時間加上去
   PKG_UTIL.CREATELOG (V_PKGName,                      --Put package name here
                       V_ProName,                    --Put procedure name here
                       V_Step,                      --Step seqence No. if have
                       V_Action,                      --Action 'I','D','U','T'
                       PKG_CONSTANT.CONS_MSG_INFO, --Message tppe, 'SUCCESS' or 'EXCEPTION'
                       'process IN_ITEM.EXAMINE_TIME success.', --Any generic information here
                       V_MainTable);              --Major table name processed

   V_Step := V_Step + 1;
   V_Action := PKG_CONSTANT.CONS_ACTION_UPDATE;
   V_MainTable := 'MST_APPRSUPITEM,PURCHORDLINE';
   --检验时间:目前所有採購前置時間及在途到料時間都不包含檢驗時間,需要把檢驗時間加上去
   PKG_UTIL.CREATELOG (
      V_PKGName,                                       --Put package name here
      V_ProName,                                     --Put procedure name here
      V_Step,                                       --Step seqence No. if have
      V_Action,                                       --Action 'I','D','U','T'
      PKG_CONSTANT.CONS_MSG_INFO,     --Message tppe, 'SUCCESS' or 'EXCEPTION'
      'add IN_ITEM.EXAMINE_TIME to MST_APPRSUPITEM and PURCHORDLINE', --Any generic information here
      V_MainTable);                               --Major table name processed

     DELETE FROM ABPPMGR.MST_ITEMSITELTPARM ;
    COMMIT;
    INSERT INTO ABPPMGR.MST_ITEMSITELTPARM(SCENARIO_ID,
                  ENTERPRISE,
                  ENGINE_ID,
                  SITEID,
                  ITEM,
                  ESTIMATEDTIME,
                  TIMEUOM,
                  SYS_CREATED_BY)
    SELECT V_SCENARIOID,
                  V_ENTERPRISE,
                  V_ENGINE,
                  V_SITEID,
                   IIS.ITEM_ID, 
                   MAX(IIS.ESTIMATEDTIME) ESTIMATEDTIME,
                   'DAYS' TIMEUOM,
                   V_PRONAME
       FROM IN_ITEM_SITE IIS where IIS.ESTIMATEDTIME IS NOT NULL GROUP BY IIS.ITEM_ID;
      COMMIT;
IF 1=0 THEN
--start remark 2014.10.14 动态安全库存实现方式错误,修改为写入MST_ITEMSITELTPARM
  MERGE INTO ABPPMGR.MST_CALENDARMASTER M
        USING (SELECT 'CSLT' || LTRIM (T2.ITEM_ID,'0') || '_' ||T2.SUPPLIER_ID || REPLACE(T2.LOCATION_ID,T1.SITE_ID,NULL)
                         CALENDARNAME
              from   IN_ITEM_SITE T1,
               IN_SUPPLIER_ITEM T2
         WHERE T1.SITE_ID = SUBSTR(T2.SUPPLIER_ID,1,4) AND T1.ITEM_ID = T2.ITEM_ID AND T1.ESTIMATEDTIME IS NOT NULL) IM
           ON (M.CALENDARNAME = IM.CALENDARNAME)
   WHEN MATCHED
   THEN
      UPDATE SET
         M.SYS_ENT_STATE = 'ACTIVE',
         M.SYS_LAST_MODIFIED_BY = V_ProName,
         M.SYS_LAST_MODIFIED_DATE = V_CurrentTime
   WHEN NOT MATCHED
   THEN
      INSERT     (SCENARIO_ID,
                  ENTERPRISE,
                  ENGINE_ID,
                  CALENDARNAME,
                  SYS_CREATED_BY)
          VALUES (V_ScenarioID,
                  V_ENTERPRISE,
                  V_ENGINE,
                  IM.CALENDARNAME,
                  V_ProName);

   V_Step := V_Step + 1;                                                 -- 0;
   V_Action := PKG_CONSTANT.CONS_ACTION_MERGE;
   V_MainTable := 'MST_CALENDARMASTER';
   --记录MST_CALENDARMASTER导入日志
   PKG_UTIL.CREATELOG (V_PKGName,                      --Put package name here
                       V_ProName,                    --Put procedure name here
                       V_Step,                      --Step seqence No. if have
                       V_Action,                      --Action 'I','D','U','T'
                       PKG_CONSTANT.CONS_MSG_INFO, --Message tppe, 'SUCCESS' or 'EXCEPTION'
                       'Success: ' || SQL%ROWCOUNT || ' rows imported.', --Any generic information here
                       V_MainTable);              --Major table name processed
   COMMIT;

   SELECT COUNT(*) INTO V_SUPPLIERITEMCOUNT FROM ABPPMGR.MST_CALENDARDETAIL;
   COMMIT;
   V_Step := V_Step + 1;
   V_Action := PKG_CONSTANT.CONS_ACTION_INSERT;
   V_MainTable := 'MST_CALENDARDETAIL,MST_CALBASEDATTR';
   --3.    动态安全库存: Calendar based Safety Lead Times for Procured Parts
   PKG_UTIL.CREATELOG (
      V_PKGName,                                       --Put package name here
      V_ProName,                                     --Put procedure name here
      V_Step,                                       --Step seqence No. if have
      V_Action,                                       --Action 'I','D','U','T'
      PKG_CONSTANT.CONS_MSG_INFO,     --Message tppe, 'SUCCESS' or 'EXCEPTION'
      'prepare attribute data for IN_ITEM_SITE.ESTIMATEDTIME into MST_CALENDARDETAIL, MST_CALBASEDATTR', --Any generic information here
      V_MainTable);                               --Major table name processed
   V_CalendarType := 'PROCURE_LEAD_TIME';

     DELETE FROM ABPPMGR.MST_CALBASEDATTR WHERE SYS_CREATED_BY=V_PRONAME;
    COMMIT;
    DELETE FROM ABPPMGR.MST_CALENDARDETAIL WHERE SYS_CREATED_BY=V_PRONAME;
    COMMIT;
   INSERT INTO ABPPMGR.MST_CALENDARDETAIL (SCENARIO_ID,
                                           ENTERPRISE,
                                           ENGINE_ID,
                                           CALENDARNAME,
                                           CALENDARTYPE,
                                           EFFSTARTDATE,
                                           EFFENDDATE,
                                           SYS_LAST_MODIFIED_BY,
                                           PATTERNSEQ,
                                           SHIFTNUMBER,
                                           SYS_CREATED_BY)
        SELECT V_ScenarioID,
               V_ENTERPRISE,
               V_ENGINE,
               'CSLT' || LTRIM (M.ITEM_ID,'0') || '_' ||M.SUPPLIER_ID || REPLACE(M.LOCATION_ID,IM.SITE_ID,NULL) CALENDARNAME,
               V_CalendarType,
               IM.EFFECTIVE_START_DATE,
               IM.EFFECTIVE_END_DATE,
               IM.ITEM_ID||IM.SITE_ID SYS_LAST_MODIFIED_BY,
               V_SUPPLIERITEMCOUNT+ROWNUM PATTERNSEQ,
               1 SHIFTNUMBER,
               V_ProName SYS_CREATED_BY
          FROM IN_ITEM_SITE IM,
               IN_SUPPLIER_ITEM M
         WHERE IM.SITE_ID = substr(M.SUPPLIER_ID,1,4) AND IM.ITEM_ID = M.ITEM_ID AND IM.ESTIMATEDTIME IS NOT NULL;
   COMMIT;

    INSERT INTO ABPPMGR.MST_CALBASEDATTR (SCENARIO_ID,
                                          ENTERPRISE,
                                          ENGINE_ID,
                                          ATTRIBUTE,
                                          CALENDARNAME,
                                          PATTERNSEQ,
                                          SHIFTNUMBER,
                                          VALUE,
                                          VALUEUOM,
                                          SYS_CREATED_BY)
       SELECT CD.SCENARIO_ID,
              CD.ENTERPRISE,
              CD.ENGINE_ID,
              'PROCURE_LEAD_TIME' ATTRIBUTE,
              CD.CALENDARNAME,
              CD.PATTERNSEQ,
              CD.SHIFTNUMBER,
              IM.ESTIMATEDTIME * 24 * 60 * 60,
              'SECONDS' VALUEUOM,
              V_ProName SYS_CREATED_BY
         FROM ABPPMGR.MST_CALENDARDETAIL CD, IN_ITEM_SITE IM
        WHERE CD.SYS_LAST_MODIFIED_BY = IM.ITEM_ID || IM.SITE_ID
              AND IM.ESTIMATEDTIME IS NOT NULL;


   V_Step := V_Step + 1;
   V_Action := PKG_CONSTANT.CONS_ACTION_INSERT;
   V_MainTable := 'MST_CALENDARDETAIL,MST_CALBASEDATTR';
   --3.    动态安全库存: Calendar based Safety Lead Times for Procured Parts
   PKG_UTIL.CREATELOG (V_PKGName,                      --Put package name here
                       V_ProName,                    --Put procedure name here
                       V_Step,                      --Step seqence No. if have
                       V_Action,                      --Action 'I','D','U','T'
                       PKG_CONSTANT.CONS_MSG_INFO, --Message tppe, 'SUCCESS' or 'EXCEPTION'
                       'Success: ' || SQL%ROWCOUNT || ' rows imported.', --Any generic information here
                       V_MainTable);
    COMMIT;
    DELETE FROM ABPPMGR.MST_SUPPLIERCALENDAR M WHERE M.SYS_CREATED_BY=V_PRONAME;
    COMMIT;
    INSERT INTO ABPPMGR.MST_SUPPLIERCALENDAR (SCENARIO_ID,
                                             ENTERPRISE,
                                             SITEID,
                                             ENGINE_ID,
                                             --PURORGANIZATIONID,
                                             CALENDARNAME,
                                             CALENDARTYPE,
                                             ITEM,
                                             --ITEMGROUP,
                                             SUPPLIERID,
                                             SYS_CREATED_BY)
      SELECT V_ScenarioID,
               V_ENTERPRISE,
               V_SiteID,
               V_ENGINE,
               'CSLT' || LTRIM (M.ITEM_ID,'0') || '_' ||M.SUPPLIER_ID || REPLACE(M.LOCATION_ID,IM.SITE_ID,NULL)  CALENDARNAME,
               V_CALENDARTYPE CALENDARTYPE,
               M.ITEM_ID ITEM,
              M.SUPPLIER_ID SUPPLIERID,
              V_PRONAME SYS_CREATED_BY
          FROM IN_ITEM_SITE IM,
               IN_SUPPLIER_ITEM M
         WHERE IM.SITE_ID = SUBSTR(M.SUPPLIER_ID,1,4) AND IM.ITEM_ID = M.ITEM_ID AND IM.ESTIMATEDTIME IS NOT NULL;

   V_Step := V_Step + 1;                                                 -- 0;
   V_Action := PKG_CONSTANT.CONS_ACTION_INSERT;
   V_MainTable := 'MST_SUPPLIERCALENDAR';
   --记录MST_SUPPLIERCALENDAR导入日志
   PKG_UTIL.CREATELOG (V_PKGName,                      --Put package name here
                       V_ProName,                    --Put procedure name here
                       V_Step,                      --Step seqence No. if have
                       V_Action,                      --Action 'I','D','U','T'
                       PKG_CONSTANT.CONS_MSG_INFO, --Message tppe, 'SUCCESS' or 'EXCEPTION'
                       'Success: ' || SQL%ROWCOUNT || ' rows imported.', --Any generic information here
                       V_MainTable);              --Major table name processed
   COMMIT;
end if;
--end remark 2014.10.14 动态安全库存实现方式错误,修改为写入MST_ITEMSITELTPARM

   --工艺路线及虚拟车间不全:当MST_OPRESOURCE.WORKCENTERNAME中没有对应到数据做处理
   V_Step := V_Step + 1;
   V_Action := PKG_CONSTANT.CONS_ACTION_INSERT;
   V_MainTable := 'MST_OPRESOURCE';
   PKG_UTIL.CREATELOG (V_PKGName,                      --Put package name here
                       V_ProName,                    --Put procedure name here
                       V_Step,                      --Step seqence No. if have
                       V_Action,                      --Action 'I','D','U','T'
                       PKG_CONSTANT.CONS_MSG_INFO, --Message tppe, 'SUCCESS' or 'EXCEPTION'
                       'Start: Prepare master data.', --Any generic information here
                       V_MainTable);

   MERGE INTO ABPPMGR.MST_WORKCENTERMASTER M
        USING DUAL IM
           ON (M.WORKCENTERNAME = V_WorkcenterName)
   WHEN MATCHED
   THEN
      UPDATE SET
         M.SYS_ENT_STATE = 'ACTIVE',
         M.SYS_LAST_MODIFIED_BY = V_ProName,
         M.SYS_LAST_MODIFIED_DATE = V_CurrentTime
   WHEN NOT MATCHED
   THEN
      INSERT     (SCENARIO_ID,
                  ENTERPRISE,
                  SITEID,
                  ENGINE_ID,
                  LOCATIONID,
                  WORKCENTERNAME,SYS_CREATED_BY)
          VALUES (V_ScenarioID,
                  V_Enterprise,
                  V_SiteID,
                  V_Engine,
                  V_LocationID,
                  V_WorkcenterName,
                  V_ProName);

   MERGE INTO ABPPMGR.MST_RESOURCEMASTER M
        USING DUAL IM
           ON (M.RESOURCENAME = V_WorkcenterName)
   WHEN MATCHED
   THEN
      UPDATE SET
         M.SYS_ENT_STATE = 'ACTIVE',
         M.SYS_LAST_MODIFIED_BY = V_ProName,
         M.SYS_LAST_MODIFIED_DATE = V_CurrentTime
   WHEN NOT MATCHED
   THEN
      INSERT     (SCENARIO_ID,
                  ENTERPRISE,
                  SITEID,
                  ENGINE_ID,
                  RESOURCENAME,
                  SYS_CREATED_BY)
          VALUES (V_ScenarioID,
                  V_Enterprise,
                  V_SiteID,
                  V_Engine,
                  V_WorkcenterName,
                  V_ProName);

   MERGE INTO ABPPMGR.MST_WORKCENTERDETAIL M
        USING DUAL IM
           ON (    M.SCENARIO_ID = V_ScenarioID
               AND M.ENTERPRISE = V_Enterprise
               AND M.SITEID = V_SiteID
               AND M.RESOURCENAME = V_WorkcenterName
               AND M.WORKCENTERNAME = V_WorkcenterName)
   WHEN MATCHED
   THEN
      UPDATE SET
         M.SYS_ENT_STATE = 'ACTIVE',
         M.SYS_LAST_MODIFIED_BY = V_ProName,
         M.SYS_LAST_MODIFIED_DATE = V_CurrentTime
   WHEN NOT MATCHED
   THEN
      INSERT     (SCENARIO_ID,
                  ENTERPRISE,
                  SITEID,
                  ENGINE_ID,
                  RESOURCENAME,
                  WORKCENTERNAME,
                  AGGWORKCENTERNAME,
                  AGGRESOURCENAME,
                  AGGSITEID,
                  LOCATIONID,
                  SYS_CREATED_BY)
          VALUES (V_ScenarioID,
                  V_Enterprise,
                  V_SiteID,
                  V_Engine,
                  V_WorkcenterName,
                  V_WorkcenterName,
                  V_WorkcenterName,
                  V_WorkcenterName,
                  V_SiteID,
                  V_LocationID,
                  V_ProName);

   COMMIT;

    DELETE FROM ABPPMGR.MST_OPRESOURCE WHERE SYS_CREATED_BY=V_PRONAME;
    COMMIT;
   INSERT INTO ABPPMGR.MST_OPRESOURCE (SCENARIO_ID,
                                       ENTERPRISE,
                                       SITEID,
                                       ENGINE_ID,
                                       ROUTINGID,
                                       OPERATIONSEQ,
                                       --RESOURCEGROUPNAME,
                                       --RESOURCECONFIGURATIONID,
                                       RESOURCENAME,
                                       WORKCENTERNAME,
                                       SYS_CREATED_BY)
      SELECT V_ScenarioID,
             V_Enterprise,
             V_SiteID,
             V_Engine,
             M.ROUTINGID,
             M.OPERATIONSEQ,
             --V_ResourceGroup,
             --ITEM_ID,
             V_WorkcenterName,
             V_WorkcenterName,
             V_ProName SYS_CREATED_BY
        FROM ABPPMGR.MST_ROUTINGOPERATION M 
       WHERE M.SYS_ENT_STATE='ACTIVE' AND NOT EXISTS
                    (SELECT NULL
                       FROM ABPPMGR.MST_OPRESOURCE M2
                      WHERE M.ROUTINGID = M2.ROUTINGID
                            AND M.OPERATIONSEQ = M2.OPERATIONSEQ
                            AND M2.SYS_ENT_STATE='ACTIVE');

   V_Step := V_Step + 1;
   V_Action := PKG_CONSTANT.CONS_ACTION_INSERT;
   V_MainTable := 'MST_OPRESOURCE';
   PKG_UTIL.CREATELOG (V_PKGName,                      --Put package name here
                       V_ProName,                    --Put procedure name here
                       V_Step,                      --Step seqence No. if have
                       V_Action,                      --Action 'I','D','U','T'
                       PKG_CONSTANT.CONS_MSG_INFO, --Message tppe, 'SUCCESS' or 'EXCEPTION'
                       'Success: ' || SQL%ROWCOUNT || ' rows imported.', --Any generic information here
                       V_MainTable);
   COMMIT;
     --增加订单需求属性
    UPDATE ABPPMGR.SALESORDERLINE SOL SET SOL.PROPERTY=SOL.SALESORDERID|| '_' ||LTRIM(SOL.SOLINENUM,'0')
   WHERE NOT EXISTS(SELECT NULL FROM IN_SOURCING ISRC WHERE SOL.CUSTOMERID  = ISRC.CUSTOMER_ID
     AND SOL.ITEM = ISRC.FINISH_ITEM_ID);
    COMMIT;
   --DELETE FROM  ABPPMGR.SALESORDERLINE_P WHERE SYS_CREATED_BY LIKE '%' || V_PRONAME || '%';
  -- COMMIT;
   
   --ADD BY Landor ON 20180522 删除SYS_CREATED_BY='AnDan'
   --DELETE FROM  ABPPMGR.SALESORDERLINE_P ;--WHERE SYS_CREATED_BY='AnDan';
   EXECUTE IMMEDIATE 'TRUNCATE TABLE ABPPMGR.SALESORDERLINE_P';
   COMMIT;
   
   
     INSERT INTO ABPPMGR.SALESORDERLINE_P
       ( ENTERPRISE
       , ENGINE_ID
       , SALESORDERID
       , SOLINENUM
       , ATTRIBUTECLASS
       , ATTRIBUTE
       , RELATIONSHIP
       , VALUE
       , SYS_CREATED_BY
       )
  SELECT sol.ENTERPRISE
       , sol.ENGINE_ID
       , sol.SALESORDERID
       , sol.SOLINENUM
       , sol.PROPERTY  ATTRIBUTECLASS
       , 'FACTORY' -- ATTRIBUTE
       , 'EQ' --RELATIONSHIP
       , sol.LOCATIONID -- VALUE
       , V_ProName||'_'||V_Step
    FROM abppmgr.SALESORDERLINE sol
   WHERE not exists(select null from IN_SOURCING isrc where sol.CUSTOMERID  = isrc.CUSTOMER_ID
     AND sol.ITEM = isrc.FINISH_ITEM_ID )
  ; 
  
  -- start add
    ----ADD BY Landor ON 20180522 按单1.把原来的ATTRIBUTECLASS+:+ SALESORDERLINE_P.VALUE
    UPDATE ABPPMGR.SALESORDERLINE_P  SET ATTRIBUTECLASS=ATTRIBUTECLASS || ':'|| VALUE;
     commit;
     --add by Landor on 20180522 按单业务处理
     ----a.针对MTL订单处理
    INSERT INTO ABPPMGR.SALESORDERLINE_P
           ( ENTERPRISE
           , ENGINE_ID
           , SALESORDERID
           , SOLINENUM
           , ATTRIBUTECLASS
           , ATTRIBUTE
           , RELATIONSHIP
           , VALUE
           , SYS_CREATED_BY
           )
       --新增ATTRIBUTECLASS=ATTRIBUTECLASS:0 value=0
    SELECT   ENTERPRISE
           , ENGINE_ID
           , SALESORDERID
           , SOLINENUM
           , SUBSTR(ATTRIBUTECLASS,1,INSTR(ATTRIBUTECLASS,':')-1)||':0' ATTRIBUTECLASS 
           , 'ORDER_PLANNING'    ATTRIBUTE
           , RELATIONSHIP
           , '0'  VALUE
           , 'AnDan1'
       FROM ABPPMGR.SALESORDERLINE_P AP
        WHERE AP.SALESORDERID LIKE '%MTL%'
    UNION
    --新增ATTRIBUTECLASS=ATTRIBUTECLASS:100 value=100  alter by landor on 20180828
        SELECT   ENTERPRISE
           , ENGINE_ID
           , AP.SALESORDERID
           , AP.SOLINENUM
           , SUBSTR(AP.ATTRIBUTECLASS,1,INSTR(AP.ATTRIBUTECLASS,':')-1)||':100' ATTRIBUTECLASS 
           , 'ORDER_PLANNING'    ATTRIBUTE
           , AP.RELATIONSHIP
           , '100'  VALUE
           , 'AnDan2' 
       FROM ABPPMGR.SALESORDERLINE_P AP
       JOIN IN_SFCHEADER S ON S.MO_ID=SUBSTR(AP.SALESORDERID,5,12)
        WHERE AP.SALESORDERID LIKE '%MTL%' 
        AND EXISTS(SELECT NULL FROM IN_ORDER_PLANNING C WHERE C.SO_ID=S.SO_ID  AND  S.SO_LINE_ID= SUBSTR(C.SO_LINE_ID,1,INSTR(C.SO_LINE_ID,'_')-1) ) 
        AND EXISTS(SELECT NULL FROM IN_ORDER_PLANNING B WHERE B.SEMI_ITEM_ID=AP.SOLINENUM)
    UNION
    --如果是按单生产产生的MTL工单新增一笔ATTRIBUTECLASS=ATTRIBUTECLASS:订单号订单行项 value=订单号订单行项
    --取消使用IN_SEMI_MO_LOCKED,改为IN_MO和IN"_SFCHEADER alter by landor on 20180619
 --取消使用IN_SEMI_MO_LOCKED,改为IN_MO和IN"_SFCHEADER alter by landor on 20180619
    SELECT   ENTERPRISE
           , ENGINE_ID
           , SALESORDERID
           , SOLINENUM
           , SUBSTR(ATTRIBUTECLASS,1,INSTR(ATTRIBUTECLASS,':')-1)||':'||IL.SO_ID||IL.SO_LINE_ID ATTRIBUTECLASS 
           , 'ORDER_PLANNING'    ATTRIBUTE
           , RELATIONSHIP
           , IL.SO_ID||IL.SO_LINE_ID  VALUE
           , 'AnDan33'
       FROM ABPPMGR.SALESORDERLINE_P AP
       JOIN IN_SFCHEADER IL ON SUBSTR(AP.SALESORDERID,5,12) =IL.MO_ID
        WHERE  EXISTS(SELECT NULL FROM IN_ORDER_PLANNING C WHERE C.SO_ID=IL.SO_ID  AND  IL.SO_LINE_ID= SUBSTR(C.SO_LINE_ID,1,INSTR(C.SO_LINE_ID,'_')-1) ) --add by landor on 20180828
        AND EXISTS(SELECT NULL FROM IN_ORDER_PLANNING B WHERE B.SEMI_ITEM_ID=AP.SOLINENUM)
        AND  AP.SALESORDERID LIKE '%MTL%' AND IL.SO_ID IS NOT NULL
     UNION
     SELECT   ENTERPRISE
           , ENGINE_ID
           , SALESORDERID
           , SOLINENUM
           , SUBSTR(ATTRIBUTECLASS,1,INSTR(ATTRIBUTECLASS,':')-1)||':'||IL.SO_ID||IL.SO_LINE_ID ATTRIBUTECLASS 
           , 'ORDER_PLANNING'    ATTRIBUTE
           , RELATIONSHIP
           , IL.SO_ID||IL.SO_LINE_ID  VALUE
           , 'AnDan4'
       FROM ABPPMGR.SALESORDERLINE_P AP
       JOIN IN_MO IL
       ON SUBSTR(AP.SALESORDERID,INSTR(AP.SALESORDERID,'_')+1,INSTR(AP.SALESORDERID,'_',1,2)-INSTR(AP.SALESORDERID,'_')-1) 
              =IL.MO_ID
        WHERE AP.SALESORDERID LIKE '%MTL%'   AND IL.SO_ID IS NOT NULL
        ; 
    /*取消使用IN_SEMI_MO_LOCKED,改为IN_MO和IN"_SFCHEADER alter by landor on 20180619
    SELECT   ENTERPRISE 
           , ENGINE_ID
           , SALESORDERID
           , SOLINENUM
           , SUBSTR(ATTRIBUTECLASS,1,INSTR(ATTRIBUTECLASS,':')-1)||':'||IL.PEGGED_SO_ID||IL.PEGGED_SO_LINE_ID ATTRIBUTECLASS 
           , 'ORDER_PLANNING'    ATTRIBUTE
           , RELATIONSHIP
           , IL.PEGGED_SO_ID||IL.PEGGED_SO_LINE_ID  VALUE
           , 'AnDan'
       FROM ABPPMGR.SALESORDERLINE_P AP
       JOIN IN_SEMI_MO_LOCKED IL
       ON SUBSTR(AP.SALESORDERID,INSTR(AP.SALESORDERID,'_')+1,INSTR(AP.SALESORDERID,'_',1,2)-INSTR(AP.SALESORDERID,'_')-1)
              =IL.PEGGED_MO_ID
        WHERE AP.SALESORDERID LIKE '%MTL%';   */
     COMMIT;
     

     ---- 再把原来ATTRIBUTECLASS=ATTRIBUTECLASS:100改为=ATTRIBUTECLASS:200以及value=100改为200
     /*UPDATE ABPPMGR.SALESORDERLINE_P AP SET ATTRIBUTECLASS=SUBSTR(ATTRIBUTECLASS,1,INSTR(ATTRIBUTECLASS,':')-1)||':200',
                                                    VALUE='200'
         WHERE EXISTS(SELECT NULL FROM IN_MO IL
          WHERE SUBSTR(AP.SALESORDERID,INSTR(AP.SALESORDERID,'_')+1, INSTR(AP.SALESORDERID,'_',1,2) -INSTR(AP.SALESORDERID,'_')-1)=IL.MO_ID)
          AND AP.SALESORDERID LIKE '%MTL%'
          AND SUBSTR(AP.ATTRIBUTECLASS,-4)=':100';   
     COMMIT;   */
     
     UPDATE ABPPMGR.SALESORDERLINE_P AP SET ATTRIBUTECLASS=SUBSTR(ATTRIBUTECLASS,1,INSTR(ATTRIBUTECLASS,':')-1)||':200',
                                                    VALUE='200'
         WHERE EXISTS(SELECT NULL FROM IN_SFCHEADER IL
          WHERE SUBSTR(AP.SALESORDERID,INSTR(AP.SALESORDERID,'_')+1, INSTR(AP.SALESORDERID,'_',1,2) -INSTR(AP.SALESORDERID,'_')-1)=IL.MO_ID)
          AND AP.SALESORDERID LIKE '%MTL%'
          AND SUBSTR(AP.ATTRIBUTECLASS,-4)=':100';   
     COMMIT;    
     
     --按单生产中带SO号和行号的,同时带了:0的,需要将0删除掉,避免无法占用库存add by landor on 20180619
    DELETE FROM ABPPMGR.SALESORDERLINE_P B 
    WHERE ATTRIBUTE='ORDER_PLANNING' AND B.ATTRIBUTECLASS LIKE 'MTL%' AND LENGTH(ATTRIBUTECLASS)=36  --长度为36代表:0
    AND EXISTS(SELECT NULL FROM ABPPMGR.SALESORDERLINE_P A 
                WHERE SUBSTR(A.ATTRIBUTE,1,34)=SUBSTR(B.ATTRIBUTE,1,34) 
                AND A.ATTRIBUTE='ORDER_PLANNING'  AND A.ATTRIBUTECLASS LIKE 'MTL%'  AND LENGTH(A.ATTRIBUTECLASS)=48); --长度为48代表带上SO号行号
    COMMIT;

     /*取消使用IN_SEMI_MO_LOCKED,改为IN_MO和IN"_SFCHEADER alter by landor on 20180619 
     UPDATE ABPPMGR.SALESORDERLINE_P AP SET ATTRIBUTECLASS=SUBSTR(ATTRIBUTECLASS,1,INSTR(ATTRIBUTECLASS,':')-1)||':200',
                                                    VALUE='200'
         WHERE EXISTS(SELECT NULL FROM IN_SEMI_MO_LOCKED IL
          WHERE SUBSTR(AP.SALESORDERID,INSTR(AP.SALESORDERID,'_')+1,
                              INSTR(AP.SALESORDERID,'_',1,2)
                               -INSTR(AP.SALESORDERID,'_')-1)=IL.PEGGED_MO_ID)
          AND AP.SALESORDERID LIKE '%MTL%'
          AND SUBSTR(AP.ATTRIBUTECLASS,-4)=':100';   */
          
      
      
    --b.    非按单生产订单 不存在与INORDERPLANNING表代表非按单生产
     INSERT INTO ABPPMGR.SALESORDERLINE_P
           ( ENTERPRISE
           , ENGINE_ID
           , SALESORDERID
           , SOLINENUM
           , ATTRIBUTECLASS
           , ATTRIBUTE
           , RELATIONSHIP
           , VALUE
           , SYS_CREATED_BY
           )
    ----当INORDERPLANNING和SALESORDERLINE_P表的soid,solineid不相同 
      -- let ATTRIBUTECLASS=ATTRIBUTECLASS:0 value=0以及ATTRIBUTECLASS=ATTRIBUTECLASS:100 value=100
    SELECT DISTINCT ENTERPRISE
          ,ENGINE_ID 
          ,SALESORDERID
          ,SOLINENUM
          ,SUBSTR(AE.ATTRIBUTECLASS,1,INSTR(AE.ATTRIBUTECLASS,':')-1)||':0' ATTRIBUTECLASS
          ,'ORDER_PLANNING'        ATTRIBUTE
          ,RELATIONSHIP
          ,'0'  VALUE
          ,'AnDan5'
     FROM ABPPMGR.SALESORDERLINE_P AE 
        WHERE NOT EXISTS(SELECT NULL FROM  IN_ORDER_PLANNING IP
         WHERE IP.SO_ID      = AE.SALESORDERID
           AND IP.SO_LINE_ID = AE.SOLINENUM)
           AND AE.ATTRIBUTECLASS NOT LIKE '%MTL%'
    UNION
    ----当INORDERPLANNING和SALESORDERLINE_P表的soid,solineid不相同
      -- let ATTRIBUTECLASS=ATTRIBUTECLASS:0 value=0以及ATTRIBUTECLASS=ATTRIBUTECLASS:100 value=100
    SELECT DISTINCT ENTERPRISE
          ,ENGINE_ID 
          ,SALESORDERID
          ,SOLINENUM
          ,SUBSTR(AE.ATTRIBUTECLASS,1,INSTR(AE.ATTRIBUTECLASS,':')-1)||':100' ATTRIBUTECLASS
          ,'ORDER_PLANNING'        ATTRIBUTE
          ,RELATIONSHIP
          ,'100'  VALUE
          ,'AnDan6' 
     FROM ABPPMGR.SALESORDERLINE_P AE 
        WHERE NOT EXISTS(SELECT NULL FROM  IN_ORDER_PLANNING IP
         WHERE IP.SO_ID      = AE.SALESORDERID
           AND IP.SO_LINE_ID = AE.SOLINENUM)
           AND AE.ATTRIBUTECLASS NOT LIKE '%MTL%';
     COMMIT;  
      
    --d.    按单生产订单:从IN_ORDER_PLANNING找到按单生产订单,然后新增数据到SALESORDERLINE_P   1代表真验货 0为假验货
     
     INSERT INTO ABPPMGR.SALESORDERLINE_P
           ( ENTERPRISE
           , ENGINE_ID
           , SALESORDERID
           , SOLINENUM
           , ATTRIBUTECLASS
           , ATTRIBUTE
           , RELATIONSHIP
           , VALUE
           , SYS_CREATED_BY
           )
    ----新增ATTRIBUTECLASS=ATTRIBUTECLASS:订单号订单行项 当order_Planning=0,1 value=订单号订单行项
    SELECT DISTINCT ENTERPRISE
          ,ENGINE_ID 
          ,SALESORDERID
          ,SOLINENUM
          ,SUBSTR(AE.ATTRIBUTECLASS,1,INSTR(AE.ATTRIBUTECLASS,':')-1)||':'||IP.SO_ID||SUBSTR(IP.SO_LINE_ID,1,INSTR(IP.SO_LINE_ID,'_')-1) ATTRIBUTECLASS
          ,'ORDER_PLANNING'        ATTRIBUTE
          ,RELATIONSHIP
          , IP.SO_ID||SUBSTR(IP.SO_LINE_ID,1,INSTR(IP.SO_LINE_ID,'_')-1)VALUE
          ,'AnDan7'
     FROM ABPPMGR.SALESORDERLINE_P AE 
       JOIN IN_ORDER_PLANNING IP
         ON IP.SO_ID      = AE.SALESORDERID
         AND IP.SO_LINE_ID = AE.SOLINENUM
         AND AE.ATTRIBUTECLASS NOT LIKE '%MTL%'
         AND IP.ORDER_PLANNING IN(0,1)
    UNION
    ----新增ATTRIBUTECLASS=ATTRIBUTECLASS:200 当order_Planning=1 value=订单号订单行项 
    SELECT DISTINCT ENTERPRISE
          ,ENGINE_ID 
          ,SALESORDERID
          ,SOLINENUM
          ,SUBSTR(AE.ATTRIBUTECLASS,1,INSTR(AE.ATTRIBUTECLASS,':')-1)||':200' ATTRIBUTECLASS
          ,'ORDER_PLANNING'        ATTRIBUTE
          ,RELATIONSHIP
          ,'200'VALUE
          ,'AnDan8'
     FROM ABPPMGR.SALESORDERLINE_P AE 
       JOIN IN_ORDER_PLANNING IP
         ON IP.SO_ID      = AE.SALESORDERID
         AND IP.SO_LINE_ID = AE.SOLINENUM
         AND AE.ATTRIBUTECLASS NOT LIKE '%MTL%'
         AND IP.ORDER_PLANNING IN(1,0)
     UNION
    ----新增ATTRIBUTECLASS=ATTRIBUTECLASS:0 当order_Planning=0 value=0
    SELECT DISTINCT ENTERPRISE
          ,ENGINE_ID 
          ,SALESORDERID
          ,SOLINENUM
          ,SUBSTR(AE.ATTRIBUTECLASS,1,INSTR(AE.ATTRIBUTECLASS,':')-1)||':0' ATTRIBUTECLASS
          ,'ORDER_PLANNING'        ATTRIBUTE
          ,RELATIONSHIP
          ,'0'VALUE
          ,'AnDan9'
     FROM ABPPMGR.SALESORDERLINE_P AE 
       JOIN IN_ORDER_PLANNING IP
         ON IP.SO_ID      = AE.SALESORDERID
         AND IP.SO_LINE_ID = AE.SOLINENUM
         AND AE.ATTRIBUTECLASS NOT LIKE '%MTL%'
         AND IP.ORDER_PLANNING=0;     
     COMMIT;
    --高品质客户处理,多群组增新增相应的条数 ATTRIBUTECLASS=ATTRIBUTECLASS:10000-ID VALUE=10000-ID
    INSERT INTO ABPPMGR.SALESORDERLINE_P
           ( ENTERPRISE
           , ENGINE_ID
           , SALESORDERID
           , SOLINENUM
           , ATTRIBUTECLASS
           , ATTRIBUTE
           , RELATIONSHIP
           , VALUE
           , SYS_CREATED_BY
           )
    SELECT DISTINCT  ENTERPRISE
                    ,ENGINE_ID 
                    ,SALESORDERID
                    ,SOLINENUM
                    ,SUBSTR(AE.ATTRIBUTECLASS,1,INSTR(AE.ATTRIBUTECLASS,':')-1)||':'||TO_CHAR(10000-IC.ID) ATTRIBUTECLASS --优先级
                    ,'ORDER_PLANNING'        ATTRIBUTE
                    ,RELATIONSHIP
                    ,10000-IC.ID VALUE
                    ,'AnDan10'
                 FROM IN_ORDER_PLANNING IP
                  JOIN IN_STOCK_CUSTOMER IC --客户对应的群组数据表
                  ON IP.CUSTOMER_ID=IC.CUSTOMER_ID
                  JOIN ABPPMGR.SALESORDERLINE_P AE
                   ON IP.SO_ID      = AE.SALESORDERID
                  AND IP.SO_LINE_ID = AE.SOLINENUM;
     COMMIT;  
      
    --endt add

    --解决按单真验货吃非限制库存的问题,清理需求表的属性值 
    --按单真验货的ORDER_PLANNING的需求的属性值不能为0  add by landor on 20180729
    DELETE FROM ABPPMGR.SALESORDERLINE_P A  
     WHERE  ATTRIBUTE='ORDER_PLANNING' AND VALUE='0' AND 
            EXISTS (SELECT * FROM IN_ORDER_PLANNING B 
                            WHERE A.SALESORDERID=B.SO_ID AND B.SO_LINE_ID=A.SOLINENUM --按单
                            AND B.ORDER_PLANNING='1'); --真验货
    COMMIT;   
    

      V_Step := V_Step + 1;
   V_Action := PKG_CONSTANT.CONS_ACTION_INSERT;
   V_MainTable := 'SALESORDERLINE_P';
   PKG_UTIL.CREATELOG (V_PKGName,                      --Put package name here
                       V_ProName,                    --Put procedure name here
                       V_Step,                      --Step seqence No. if have
                       V_Action,                      --Action 'I','D','U','T'
                       PKG_CONSTANT.CONS_MSG_INFO, --Message tppe, 'SUCCESS' or 'EXCEPTION'
                       'Success: ' || SQL%ROWCOUNT || ' rows imported.', --Any generic information here
                       V_MainTable);
   COMMIT;
   --ALTER BY HUANGYANGXIONG ON 2014/12/02  注释掉VMI物料的采购周期为0的设定
   /*
   UPDATE ABPPMGR.MST_APPRSUPITEM M SET M.AVGLEADTIME=0
    WHERE EXISTS (SELECT NULL FROM IN_SUPPLIER_ITEM IM
    WHERE  M.ITEM=IM.ITEM_ID AND M.SUPPLIERID=IM.SUPPLIER_ID and UDF_ITEM_TYPE_ID='1');
    COMMIT;
    */
   DELETE FROM ABPPMGR.MST_APPRSUPITEM_P;--删除供应商与物料对应关系属性数据
   COMMIT;
   UPDATE ABPPMGR.MST_APPRSUPITEM M SET M.PROPERTY=M.ITEM|| '_'|| SUBSTR(M.SUPPLIERID,1,4);--更新属性数据
   COMMIT;

    INSERT INTO ABPPMGR.MST_APPRSUPITEM_P (ENGINE_ID,
                                           SUPPLIERID,
                                           ITEM,
                                           ITEMGROUP,
                                           ATTRIBUTECLASS,
                                           ENTERPRISE,
                                           PURORGANIZATIONID,
                                           SITEID,
                                           ITEMENTERPRISE,
                                           EFFSTARTDATE,
                                           SCENARIO_ID,
                                           ATTRIBUTE,
                                           RELATIONSHIP,
                                           VALUE)
       SELECT ENGINE_ID,
              SUPPLIERID,
              ITEM,
              ITEMGROUP,
              PROPERTY ATTRIBUTECLASS,
              ENTERPRISE,
              PURORGANIZATIONID,
              SITEID,
              ITEMENTERPRISE,
              EFFSTARTDATE,
              SCENARIO_ID,
              'FACTORY' ATTRIBUTE,
              'EQ' RELATIONSHIP,
              SUBSTR(SUPPLIERID,1,4) VALUE 
         FROM ABPPMGR.MST_APPRSUPITEM M 
         WHERE NOT EXISTS(SELECT NULL FROM IN_ITEM_SITE IM
          WHERE IM.ITEM_ID=M.ITEM AND IM.SITE_ID=SUBSTR(M.SUPPLIERID,1,4)
          AND  TRIM(IM.PROC_TYPE) || TRIM(IM.SP_PROC_TYPE) IN ('E70'));
    COMMIT;

    EXECUTE IMMEDIATE 'truncate table TEMP_ITEM_UOM';
    INSERT INTO TEMP_ITEM_UOM(SITE_ID,parent_id, ITEM_ID, QTY_UOM,LOC_ID)
      SELECT SUBSTR(BH.BOM_ID,INSTR(BH.BOM_ID,'_')+1,4) SITE_ID,BH.product_id,BD2.ITEM_ID
      ,MAX(BD2.USAGE_QTY_UOM) QTY_UOM,MAX(BD2.LOC_ID) LOC_ID
    FROM IN_BOM_DETAILS BD, IN_BOM_HEADER BH,IN_BOM_DETAILS BD2, IN_BOM_HEADER BH2
     where BH.BOM_ID=BD.bom_id and BH.MO_BOM_MARK='0'  and BD.IS_PHANTOM='1'
     and BH2.BOM_ID=BD2.bom_id and BH2.MO_BOM_MARK='0' and BD.item_id=BH2.PRODUCT_ID
     and SUBSTR(BH2.BOM_ID,INSTR(BH2.BOM_ID,'_')+1,4) =SUBSTR(BH.BOM_ID,INSTR(BH.BOM_ID,'_')+1,4) 
    GROUP BY  BH.product_id,SUBSTR(BH.BOM_ID,INSTR(BH.BOM_ID,'_')+1,4),BD2.ITEM_ID;
    COMMIT;
        INSERT INTO TEMP_ITEM_UOM(SITE_ID,parent_id, ITEM_ID, QTY_UOM,LOC_ID)
      SELECT SUBSTR(BH.BOM_ID,INSTR(BH.BOM_ID,'_')+1,4) SITE_ID,BH.product_id,BD2.FOLLOW_UP_MATERIAL
      ,MAX(BD2.USAGE_QTY_UOM) QTY_UOM,MAX(BD2.LOC_ID) LOC_ID
    FROM IN_BOM_DETAILS BD, IN_BOM_HEADER BH,IN_BOM_DETAILS BD2, IN_BOM_HEADER BH2
     where BH.BOM_ID=BD.bom_id and BH.MO_BOM_MARK='0'  and BD.IS_PHANTOM='1'
     and BH2.BOM_ID=BD2.bom_id and BH2.MO_BOM_MARK='0' and BD.item_id=BH2.PRODUCT_ID
     and SUBSTR(BH2.BOM_ID,INSTR(BH2.BOM_ID,'_')+1,4) =SUBSTR(BH.BOM_ID,INSTR(BH.BOM_ID,'_')+1,4) 
         and not exists (select null from TEMP_ITEM_UOM TU where TU.SITE_ID=SUBSTR(BH.BOM_ID,INSTR(BH.BOM_ID,'_')+1,4) 
    and tu.parent_id=BH.product_id and  tu.ITEM_ID=BD2.FOLLOW_UP_MATERIAL)
     and exists( select null from in_item TM where Tm.item_id=BD2.FOLLOW_UP_MATERIAL)
    GROUP BY  BH.product_id,SUBSTR(BH.BOM_ID,INSTR(BH.BOM_ID,'_')+1,4),BD2.FOLLOW_UP_MATERIAL;
    COMMIT;
    INSERT INTO TEMP_ITEM_UOM(SITE_ID,parent_id, ITEM_ID, QTY_UOM,LOC_ID)
    SELECT SUBSTR(BH.BOM_ID,INSTR(BH.BOM_ID,'_')+1,4) SITE_ID,BH.product_id,BD.FOLLOW_UP_MATERIAL
    ,MAX(BD.USAGE_QTY_UOM) QTY_UOM,MAX(BD.LOC_ID) LOC_ID
    FROM IN_BOM_DETAILS BD, IN_BOM_HEADER BH where BH.BOM_ID=BD.bom_id and BH.MO_BOM_MARK='0' 
    and not exists (select null from TEMP_ITEM_UOM TU where TU.SITE_ID=SUBSTR(BH.BOM_ID,INSTR(BH.BOM_ID,'_')+1,4) 
    and tu.parent_id=BH.product_id and  tu.ITEM_ID=BD.FOLLOW_UP_MATERIAL)
    and exists( select null from in_item TM where Tm.item_id=BD.FOLLOW_UP_MATERIAL)
    GROUP BY  BH.product_id,SUBSTR(BH.BOM_ID,INSTR(BH.BOM_ID,'_')+1,4),BD.FOLLOW_UP_MATERIAL;
        COMMIT;
    INSERT INTO TEMP_ITEM_UOM(SITE_ID,parent_id, ITEM_ID, QTY_UOM,LOC_ID)
    SELECT SUBSTR(BH.BOM_ID,INSTR(BH.BOM_ID,'_')+1,4) SITE_ID,BH.product_id,BD.ITEM_ID
    ,MAX(BD.USAGE_QTY_UOM) QTY_UOM,MAX(BD.LOC_ID) LOC_ID
    FROM IN_BOM_DETAILS BD, IN_BOM_HEADER BH where BH.BOM_ID=BD.bom_id and BH.MO_BOM_MARK='0' 
    and not exists (select null from TEMP_ITEM_UOM TU where TU.SITE_ID=SUBSTR(BH.BOM_ID,INSTR(BH.BOM_ID,'_')+1,4) 
    and tu.parent_id=BH.product_id and  tu.ITEM_ID=BD.item_id)
    GROUP BY  BH.product_id,SUBSTR(BH.BOM_ID,INSTR(BH.BOM_ID,'_')+1,4),BD.ITEM_ID;
        COMMIT;
        
   
    --MTL加工辅助件报short问题修复 add by landor on 20180809
    DELETE FROM  ABPPMGR.SALESORDERLINE_P A 
    WHERE A.ATTRIBUTECLASS LIKE 'MTL%' AND A.ATTRIBUTE='ORDER_PLANNING' 
        AND EXISTS (SELECT NULL FROM IN_ITEM B WHERE B.ITEM_ID=A.SOLINENUM
        AND FAMILY_ID IN ('102018000','402068001','402071000','402068004','402068005','402068006','402068010','402068002','502003004','502502002')) ;
    COMMIT;    
   
   

   
   
   /*    取消刷新物化视图 add by Landor on 20161222 
        
   --refresh ODS data to Materialized View
   --add by huangyangxiong on 20150430  update service lock to 'NO' status
   UPDATE ABPPMGR.SERVICE_LOCK SET IS_LOCKED = 'NO';
   
   COMMIT;
   
ABPPMGR.UPDATE_SNAPSHOT_COMPLETE();
   COMMIT;
   
   */
   
      V_Step := V_Step + 1;
   V_Action := PKG_CONSTANT.CONS_ACTION_INSERT;
   V_MainTable := 'ALL';
   PKG_UTIL.CREATELOG (V_PKGName,                      --Put package name here
                       V_ProName,                    --Put procedure name here
                       V_Step,                      --Step seqence No. if have
                       V_Action,                      --Action 'I','D','U','T'
                       PKG_CONSTANT.CONS_MSG_INFO, --Message tppe, 'SUCCESS' or 'EXCEPTION'
                       'execute procedure UPDATE_SNAPSHOT_COMPLETE compeleted.', --Any generic information here
                       V_MainTable);
EXCEPTION
   --总程序异常处理部分
   WHEN OTHERS
   THEN
      BEGIN
         ROLLBACK;
         EXITCODE := 1;
         e_ErrMessage :=
            SUBSTR (SQLERRM, 1, 1000)
            || SUBSTR (DBMS_UTILITY.FORMAT_ERROR_BACKTRACE, 1, 2000);
         PKG_UTIL.CREATELOG (V_PKGName,
                             V_ProName,
                             V_Step,
                             V_Action,
                             PKG_CONSTANT.CONS_MSG_EXCEPTION,
                             e_ErrMessage,
                             V_MainTable);
      END;
END;
/
View Code

 

posted on 2018-07-03 10:20  Snowfun  阅读(222)  评论(0编辑  收藏  举报