<?xml version="1.0" encoding="UTF-8"?>
<queryMap desc = "领导报表库存综合信息">
	<query id = "CoreSlmYdmStatisticsTask.task" desc = "获取期初重量">
		<![CDATA[
		DECLARE
	        vCount INTEGER := 0;
	        vDate VARCHAR2(6) := #vDate# ;
	        vNowDate DATE := NULL;
	        vTempDate DATE := NULL;
	        vUpDate VARCHAR2(6) := '';
        
        BEGIN
          SELECT TRUNC(SYSDATE,'DD') INTO vNowDate FROM DUAL;--当前日期
          SELECT TRUNC(SYSDATE,'MM')+1 INTO vTempDate FROM DUAL;--当前月份的第二天
          SELECT TO_CHAR(ADD_MONTHS(TRUNC(SYSDATE),-1),'YYYYMM') INTO vUpDate FROM DUAL;--上月
          IF vNowDate = vTempDate THEN
              FOR DROW1 IN (
                SELECT A1.BELONG_NAME SALE_ORG_DESC,A1.INI_NUM,A2.IN_NUM,A3.OUT_NUM,NVL(A4.ESC_NUM,0) ESC_NUM FROM 
                  (SELECT T.BELONG_NAME,
                   SUM(T.ACT_WEIGHT)INI_NUM
                    FROM YDM_ZC_INITIAL T
                   WHERE T.HANDLE_LOG = '1'
                     AND T.BAL_YEAR_MONTH = vUpDate
                     GROUP BY T.BELONG_NAME)A1,
                    
                 (SELECT T.BELONG_NAME,
                     SUM(T.ACT_WEIGHT)IN_NUM
                      FROM YDM_ZC_INLIST t
                     WHERE T.INSTOCK_TYPE_CODE IN ('800701','800702','800704','800706')
                       AND T.BAL_YEAR_MONTH = vUpDate
                       AND T.BELONG_CODE IN ('100101','100102','100103')
                       GROUP BY T.BELONG_NAME)A2,
                 
                 (SELECT T.BELONG_NAME,
                     SUM(CASE
                           WHEN T.PONDER_NO IS NOT NULL THEN
                            T.PONDER_GROSS_WT
                           ELSE
                            T.ACT_WEIGHT
                         END) OUT_NUM
                      FROM YDM_ZC_OUTLIST T
                     WHERE T.OUTSTOCK_TYPE_CODE IN ('800802', '800805', '800806', '800808')
                       AND T.BAL_YEAR_MONTH = vUpDate
                       AND T.BELONG_CODE IN ('100101','100102','100103')
                     GROUP BY T.BELONG_NAME)A3,
               
                   (SELECT T.BELONG_NAME, SUM(NVL(T.ACT_WEIGHT, 0)) AS ESC_NUM
                        FROM YDM_ZC_STOCKTAKINGLIST T
                       WHERE T.BAL_YEAR_MONTH = vUpDate
                       AND T.BELONG_CODE IN ('100101','100102','100103')
                       GROUP BY T.BELONG_NAME)A4                 
                 WHERE A1.BELONG_NAME = A2.BELONG_NAME(+)
                 AND  A1.BELONG_NAME = A3.BELONG_NAME(+)
                 AND  A1.BELONG_NAME = A4.BELONG_NAME(+)) LOOP
             
                 UPDATE RPT_SELYDM_STATISTICS SET 
                    INI_NUM = DROW1.INI_NUM,
                    IN_NUM = DROW1.IN_NUM, 
                    OUT_NUM = DROW1.OUT_NUM, 
                    ESC_NUM = DROW1.ESC_NUM
                 WHERE BAL_YEAR_MONTH = vUpDate    
                 AND SALE_ORG_DESC = DROW1.SALE_ORG_DESC;
             END LOOP;
             
          END IF;
          
          SELECT COUNT(1) INTO vCount FROM RPT_SELYDM_STATISTICS WHERE BAL_YEAR_MONTH = vDate;
          IF vCount > 0 THEN
            DELETE RPT_SELYDM_STATISTICS WHERE BAL_YEAR_MONTH = vDate;
          END IF;
        
          FOR DROW IN (
              SELECT A1.BELONG_NAME SALE_ORG_DESC,A1.INI_NUM,A2.IN_NUM,A3.OUT_NUM,NVL(A4.ESC_NUM,0) ESC_NUM,A5.STG_NUM,A6.SEL_NUM,A7.FIN_NUM1,A8.FIN_NUM2 FROM 
                (SELECT T.BELONG_NAME,
                 SUM(T.ACT_WEIGHT)INI_NUM
                  FROM YDM_ZC_INITIAL T
                 WHERE T.HANDLE_LOG = '1'
                   AND T.BAL_YEAR_MONTH = vDate
                   GROUP BY T.BELONG_NAME)A1,
                  
               (SELECT T.BELONG_NAME,
                   SUM(T.ACT_WEIGHT)IN_NUM
                    FROM YDM_ZC_INLIST t
                   WHERE T.INSTOCK_TYPE_CODE IN ('800701','800702','800704','800706')
                     AND T.BAL_YEAR_MONTH = vDate
                     AND T.BELONG_CODE IN ('100101','100102','100103')
                     GROUP BY T.BELONG_NAME)A2,
               
               (SELECT T.BELONG_NAME,
                   SUM(CASE
                         WHEN T.PONDER_NO IS NOT NULL THEN
                          T.PONDER_GROSS_WT
                         ELSE
                          T.ACT_WEIGHT
                       END) OUT_NUM
                    FROM YDM_ZC_OUTLIST T
                   WHERE T.OUTSTOCK_TYPE_CODE IN ('800802', '800805', '800806', '800808')
                     AND T.BAL_YEAR_MONTH = vDate
                     AND T.BELONG_CODE IN ('100101','100102','100103')
                   GROUP BY T.BELONG_NAME)A3,
             
                 (SELECT T.BELONG_NAME, SUM(NVL(T.ACT_WEIGHT, 0)) AS ESC_NUM
                      FROM YDM_ZC_STOCKTAKINGLIST T
                     WHERE T.BAL_YEAR_MONTH = vDate
                     AND T.BELONG_CODE IN ('100101','100102','100103')
                     GROUP BY T.BELONG_NAME)A4,        
             
             ----实物库存---若查询月份不是当前月份，则查参数月份+1的期初值为实物库存
                (SELECT T2.BELONG_NAME,
                             SUM(T2.ACT_WEIGHT) STG_NUM
                      FROM (SELECT T.BELONG_NAME,T.ACT_WEIGHT FROM MAT_ZC_M T,YDM_ZC_BUTTRESS S
                       WHERE T.MAT_NO = S.MAT_NO
                       AND T.PRODUCT_FLAG = '801402'
                       AND T.BELONG_CODE IN ('100101','100102','100103')
                       UNION ALL
                       SELECT T.BELONG_NAME,T.ACT_WEIGHT FROM MAT_ZC_M T,YDM_ZC_BUTTRESS S
                         WHERE T.MAT_NO = S.MAT_NO
                         AND T.PRODUCT_FLAG = '801403'
                         AND T.BELONG_CODE IN ('100101','100102','100103'))T2
                         GROUP BY T2.BELONG_NAME)A5,
                 
               (SELECT SUM(B.WEIGHT) SEL_NUM,B.BELONG_NAME
                     FROM (SELECT SUM(T.ACT_WEIGHT) AS WEIGHT, T.BELONG_NAME AS BELONG_NAME
                      FROM MAT_ZC_M T, YDM_ZC_BUTTRESS S
                     WHERE T.MAT_NO = S.MAT_NO
                     AND T.BELONG_CODE IN ('100101','100102','100103')
                     AND T.PRODUCT_FLAG > '801401'
                     GROUP BY T.BELONG_NAME
                    UNION ALL
                    SELECT SUM(NVL(T2.ACT_WEIGHT, 0)) AS WEIGHT, T2.SALE_ORG_DESC AS BELONG_NAME
                      FROM (SELECT T1.BILL_NO, T1.LOADVEHICLE_TYPE
                              FROM YDM_ZC_BILL_M T1 where t1.loadvehicle_type = '2' and t1.ispreclose > '0' and t1.bill_status = '3'
                             GROUP BY T1.BILL_NO, T1.LOADVEHICLE_TYPE) T6,
                           YDM_ZC_LOADVEHICLE_MAT T2
                     WHERE T6.BILL_NO = T2.BILL_NO(+)
                       AND T2.VALIDFLAG >'1'
                       AND T2.SALE_ORG IN ('100101','100102','100103')
                     GROUP BY T2.SALE_ORG_DESC
                    UNION ALL
                    SELECT SUM(-T5.WEIGHT) AS WEIGHT, T5.SALE_ORG_DESC AS BELONG_NAME
                      FROM (SELECT SUM(NVL(C.ACT_WEIGHT, 0)) AS WEIGHT, T4.SALE_ORG_DESC
                              FROM (SELECT T1.BILL_NO, T1.LOADVEHICLE_TYPE, T1.BILL_STATUS,T1.ISPRECLOSE,T1.SALE_ORG_DESC
                                      FROM YDM_ZC_BILL_M T1 WHERE T1.LOADVEHICLE_TYPE='2' AND T1.ISPRECLOSE>'0' and t1.bill_status = '3'
                                     GROUP BY T1.BILL_NO, T1.LOADVEHICLE_TYPE, T1.BILL_STATUS,T1.ISPRECLOSE,T1.SALE_ORG_DESC) T4,
                                   YDM_ZC_LOADVEHICLE_MAT T3,YDM_ZC_BILL_C C
                             WHERE T4.BILL_NO = C.BILL_NO
                             AND C.MAT_NO=T3.MAT_NO(+)       
                             AND T3.SALE_ORG IN ('100101','100102','100103')
                             GROUP BY T4.SALE_ORG_DESC) T5
                     GROUP BY T5.SALE_ORG_DESC)B GROUP BY B.BELONG_NAME)A6,
                     
               (SELECT SUM(G.WEIGHT) FIN_NUM1, G.BELONG_NAME
                    FROM (SELECT SUM(T.ACT_WEIGHT) AS WEIGHT, T.BELONG_NAME
                            FROM MAT_ZC_M T, YDM_ZC_BUTTRESS S
                           WHERE T.MAT_NO = S.MAT_NO
                           AND T.BELONG_CODE IN ('100101','100102','100103')
                           AND T.PRODUCT_FLAG > '801401'
                           GROUP BY T.BELONG_NAME
                          UNION ALL
                          SELECT SUM(NVL(T2.ACT_WEIGHT, 0)) AS WEIGHT, T2.SALE_ORG_DESC
                              FROM (SELECT T1.BILL_NO, T1.LOADVEHICLE_TYPE
                                      FROM YDM_ZC_BILL_M T1 WHERE T1.LOADVEHICLE_TYPE = '2' AND T1.REALINVOICENO1 IS NULL AND T1.ISPRECLOSE > '0' AND T1.BILL_STATUS = '3'
                                     GROUP BY T1.BILL_NO, T1.LOADVEHICLE_TYPE) T6,
                                   YDM_ZC_LOADVEHICLE_MAT T2
                             WHERE T6.BILL_NO = T2.BILL_NO
                               AND T2.VALIDFLAG > '1'
                             GROUP BY T2.SALE_ORG_DESC
                          UNION ALL
                          SELECT SUM(-T5.WEIGHT) AS WEIGHT, T5.SALE_ORG_DESC
                            FROM (SELECT SUM(NVL(C.ACT_WEIGHT, 0)) AS WEIGHT, T4.SALE_ORG_DESC
                                    FROM (SELECT T1.BILL_NO,
                                                 T1.LOADVEHICLE_TYPE,
                                                 T1.BILL_STATUS,
                                                 T1.ISPRECLOSE,
                                                 T1.SALE_ORG_DESC
                                            FROM YDM_ZC_BILL_M T1
                                           WHERE T1.LOADVEHICLE_TYPE = '2'
                                             AND T1.ISPRECLOSE > '0'
                                             AND T1.REALINVOICENO1 IS NULL
                                           GROUP BY T1.BILL_NO,
                                                    T1.LOADVEHICLE_TYPE,
                                                    T1.BILL_STATUS,
                                                    T1.ISPRECLOSE,
                                                    T1.SALE_ORG_DESC) T4,
                                         YDM_ZC_LOADVEHICLE_MAT T3,
                                         YDM_ZC_BILL_C C
                                   WHERE T4.BILL_NO = C.BILL_NO
                                     AND C.MAT_NO = T3.MAT_NO(+)
                                   GROUP BY T4.SALE_ORG_DESC) T5
                           GROUP BY T5.SALE_ORG_DESC
                          UNION ALL
                          SELECT SUM(NVL(T11.WEIGHT, 0)) WEIGHT, T11.SALE_ORG_DESC
                              FROM (SELECT T8.SALE_ORG_DESC,
                                           SUM(NVL(CASE
                                                     WHEN  NVL(T9.PONDER_GROSS_WT,0) = 0 AND T9.VALIDFLAG > '1' THEN
                                                      T9.ACT_WEIGHT
                                                     WHEN NVL(T9.PONDER_GROSS_WT,0) > 0 AND T9.VALIDFLAG > '1' THEN
                                                      T9.PONDER_GROSS_WT
                                                   END,
                                                   0)) WEIGHT        
                                      FROM (SELECT T7.BILL_NO,
                                                   T7.LOADVEHICLE_TYPE,
                                                   T7.BILL_STATUS,
                                                   T7.SALE_ORG_DESC
                                              FROM YDM_ZC_BILL_M T7
                                             WHERE T7.LOADVEHICLE_TYPE = '1'
                                               AND T7.BILL_STATUS IN ('4', '5')
                                               AND T7.REALINVOICENO1 IS NULL
                                             GROUP BY T7.BILL_NO,
                                                      T7.LOADVEHICLE_TYPE,
                                                      T7.BILL_STATUS,
                                                      T7.SALE_ORG_DESC) T8,
                                           YDM_ZC_LOADVEHICLE_MAT T9,
                                           YDM_ZC_BILL_C M
                                     WHERE T8.BILL_NO = T9.BILL_NO
                                       AND M.MAT_NO = T9.MAT_NO(+)           
                                     GROUP BY T8.SALE_ORG_DESC) T11
                             GROUP BY T11.SALE_ORG_DESC
                          UNION ALL
                          SELECT SUM(NVL(T11.WEIGHT, 0)) WEIGHT, T11.SALE_ORG_DESC
                            FROM (SELECT T8.SALE_ORG_DESC, SUM(NVL(T9.ACT_WEIGHT, 0)) AS WEIGHT
                                    FROM (SELECT T7.BILL_NO,
                                                 T7.LOADVEHICLE_TYPE,
                                                 T7.BILL_STATUS,
                                                 T7.SALE_ORG_DESC
                                            FROM YDM_ZC_BILL_M T7
                                           WHERE T7.LOADVEHICLE_TYPE = '2'
                                             AND T7.BILL_STATUS = '4'
                                             AND T7.REALINVOICENO1 IS NULL
                                           GROUP BY T7.BILL_NO,
                                                    T7.LOADVEHICLE_TYPE,
                                                    T7.BILL_STATUS,
                                                    T7.SALE_ORG_DESC) T8,
                                         YDM_ZC_LOADVEHICLE_MAT T9,
                                         YDM_ZC_BILL_C M
                                   WHERE T8.BILL_NO = T9.BILL_NO
                                     AND M.MAT_NO = T9.MAT_NO(+)
                                   GROUP BY T8.SALE_ORG_DESC) T11
                           GROUP BY T11.SALE_ORG_DESC) G
                   GROUP BY G.BELONG_NAME)A7,
                   
             (SELECT SUM(G.WEIGHT) FIN_NUM2, G.BELONG_NAME
                  FROM (SELECT SUM(T.ACT_WEIGHT) AS WEIGHT, T.BELONG_NAME
                      FROM MAT_ZC_M T, YDM_ZC_BUTTRESS S
                     WHERE T.MAT_NO = S.MAT_NO
                     AND T.BELONG_CODE IN ('100101','100102','100103')
                     AND T.PRODUCT_FLAG > '801401'
                     GROUP BY T.BELONG_NAME
                    UNION ALL
                    SELECT SUM(NVL(T2.ACT_WEIGHT, 0)) AS WEIGHT, T2.SALE_ORG_DESC
                      FROM (SELECT T1.BILL_NO, T1.LOADVEHICLE_TYPE
                              FROM YDM_ZC_BILL_M T1 WHERE T1.LOADVEHICLE_TYPE = '2' AND T1.REALINVOICENO IS NULL and t1.ispreclose > '0' and t1.bill_status = '3'
                             GROUP BY T1.BILL_NO, T1.LOADVEHICLE_TYPE) T6,
                           YDM_ZC_LOADVEHICLE_MAT T2
                     WHERE T6.BILL_NO = T2.BILL_NO
                       AND T2.VALIDFLAG > '1'
                     GROUP BY T2.SALE_ORG_DESC
                    UNION ALL
                    SELECT SUM(-T5.WEIGHT) AS WEIGHT, T5.SALE_ORG_DESC
                      FROM (SELECT SUM(NVL(C.ACT_WEIGHT, 0)) AS WEIGHT, T4.SALE_ORG_DESC
                              FROM (SELECT T1.BILL_NO,
                                           T1.LOADVEHICLE_TYPE,
                                           T1.BILL_STATUS,
                                           T1.ISPRECLOSE,
                                           T1.SALE_ORG_DESC
                                      FROM YDM_ZC_BILL_M T1
                                     WHERE T1.LOADVEHICLE_TYPE = '2'
                                       AND T1.ISPRECLOSE > '0'
                                       AND T1.REALINVOICENO IS NULL
                                     GROUP BY T1.BILL_NO,
                                              T1.LOADVEHICLE_TYPE,
                                              T1.BILL_STATUS,
                                              T1.ISPRECLOSE,
                                              T1.SALE_ORG_DESC) T4,
                                   YDM_ZC_LOADVEHICLE_MAT T3,
                                   YDM_ZC_BILL_C C
                             WHERE T4.BILL_NO = C.BILL_NO
                               AND C.MAT_NO = T3.MAT_NO(+)
                             GROUP BY T4.SALE_ORG_DESC) T5
                     GROUP BY T5.SALE_ORG_DESC
                    UNION ALL
                    SELECT SUM(NVL(T11.WEIGHT, 0)) WEIGHT, T11.SALE_ORG_DESC
                      FROM (SELECT T8.SALE_ORG_DESC,
                                   SUM(NVL(CASE
                                             WHEN NVL(T9.PONDER_GROSS_WT,0)= 0 AND T9.VALIDFLAG > '1' THEN
                                              T9.ACT_WEIGHT
                                             WHEN NVL(T9.PONDER_GROSS_WT,0)>0 AND T9.VALIDFLAG > '1' THEN
                                              T9.PONDER_GROSS_WT
                                           END,
                                           0)) WEIGHT                
                              FROM (SELECT T7.BILL_NO,
                                           T7.LOADVEHICLE_TYPE,
                                           T7.BILL_STATUS,
                                           T7.SALE_ORG_DESC
                                      FROM YDM_ZC_BILL_M T7
                                     WHERE T7.LOADVEHICLE_TYPE = '1'
                                       AND T7.BILL_STATUS IN ('4', '5')
                                       AND T7.REALINVOICENO IS NULL
                                     GROUP BY T7.BILL_NO,
                                              T7.LOADVEHICLE_TYPE,
                                              T7.BILL_STATUS,
                                              T7.SALE_ORG_DESC) T8,
                                   YDM_ZC_LOADVEHICLE_MAT T9,
                                   YDM_ZC_BILL_C M
                             WHERE T8.BILL_NO = T9.BILL_NO
                             AND M.MAT_NO = T9.MAT_NO(+)
                             GROUP BY T8.SALE_ORG_DESC) T11
                     GROUP BY T11.SALE_ORG_DESC
                    UNION ALL
                    SELECT SUM(NVL(T11.WEIGHT, 0)) WEIGHT, T11.SALE_ORG_DESC
                      FROM (SELECT T8.SALE_ORG_DESC, SUM(NVL(T9.ACT_WEIGHT, 0)) AS WEIGHT
                              FROM (SELECT T7.BILL_NO,
                                           T7.LOADVEHICLE_TYPE,
                                           T7.BILL_STATUS,
                                           T7.SALE_ORG_DESC
                                      FROM YDM_ZC_BILL_M T7
                                     WHERE T7.LOADVEHICLE_TYPE = '2'
                                       AND T7.BILL_STATUS = '4'
                                       AND T7.REALINVOICENO IS NULL
                                     GROUP BY T7.BILL_NO,
                                              T7.LOADVEHICLE_TYPE,
                                              T7.BILL_STATUS,
                                              T7.SALE_ORG_DESC) T8,
                                   YDM_ZC_LOADVEHICLE_MAT T9,
                                   YDM_ZC_BILL_C M
                             WHERE T8.BILL_NO = T9.BILL_NO
                               AND M.MAT_NO = T9.MAT_NO(+)
                             GROUP BY T8.SALE_ORG_DESC) T11
                          GROUP BY T11.SALE_ORG_DESC) G
                     GROUP BY G.BELONG_NAME)A8
               WHERE A1.BELONG_NAME = A2.BELONG_NAME(+)
               AND  A1.BELONG_NAME = A3.BELONG_NAME(+)
               AND  A1.BELONG_NAME = A4.BELONG_NAME(+)
               AND  A1.BELONG_NAME = A5.BELONG_NAME(+)
               AND  A1.BELONG_NAME = A6.BELONG_NAME(+)
               AND  A1.BELONG_NAME = A7.BELONG_NAME(+)
               AND  A1.BELONG_NAME = A8.BELONG_NAME(+) ) LOOP
           
               INSERT INTO RPT_SELYDM_STATISTICS
                  (SALE_ORG_DESC, BAL_YEAR_MONTH, INI_NUM, IN_NUM, 
                   OUT_NUM, ESC_NUM, STG_NUM, SEL_NUM, 
                   FIN_NUM1, FIN_NUM2)
                VALUES
                  (DROW.SALE_ORG_DESC, vDate, DROW.INI_NUM, DROW.IN_NUM, 
                   DROW.OUT_NUM, DROW.ESC_NUM, DROW.STG_NUM, DROW.SEL_NUM, 
                   DROW.FIN_NUM1, DROW.FIN_NUM2);
           END LOOP;
      END;  
		
		                    
		]]>
	</query>


</queryMap>