<?xml version="1.0" encoding="UTF-8"?>
<queryMap desc = "领导报表收发存信息">
	<query id = "CoreYdmStockTask.task" desc = "收发存信息">
		<![CDATA[
			DECLARE
				vCount INTEGER := 0;
				vDate VARCHAR2(6) := #vDate# ;
				
				BEGIN
				  SELECT COUNT(1) INTO vCount FROM RPT_YDMSTOCK_STATISTICS WHERE BAL_YEAR_MONTH = vDate;
				  IF vCount > 0 THEN
				    DELETE RPT_YDMSTOCK_STATISTICS WHERE BAL_YEAR_MONTH = vDate;
				  END IF;
				  FOR DROW IN (
				    SELECT BELONG_NAME,
				           SUM(ONDAYINWGT) ONDAYINWGT,
				           SUM(INALLWGT) INALLWGT,
				           SUM(ONDAYOUTWGT) ONDAYOUTWGT,
				           SUM(SUMWGT) SUMWGT,
				           SUM(RAILWAYWGT) RAILWAYWGT,
				           SUM(TARGETWGT) TARGETWGT,
				           SUM(ENDWGT) ENDWGT,
				           SUM(ENDWGT - BEGINWGT) INCREMENTWGT
				      FROM (SELECT T.BELONG_NAME,
				                   SUM(T.ACT_WEIGHT) ONDAYINWGT,
				                   0 INALLWGT,
				                   0 ONDAYOUTWGT,
				                   0 SUMWGT,
				                   0 RAILWAYWGT,
				                   0 TARGETWGT,
				                   0 ENDWGT,
				                   0 BEGINWGT
				              FROM YDM_ZC_INLIST T
				             WHERE TRUNC(T.INSTOCK_TIME) = TRUNC(SYSDATE)
				               AND T.BAL_YEAR_MONTH = vDate
				               AND T.INSTOCK_TYPE_CODE IN
				                   ('800701', '800702', '800704', '800706')
				               AND T.BELONG_CODE IN ('100101', '100102', '100103','100105')
				             GROUP BY T.BELONG_NAME
				            UNION ALL
				            SELECT T.BELONG_NAME,
				                   0 ONDAYINWGT,
				                   SUM(T.ACT_WEIGHT) INALLWGT,
				                   0 ONDAYOUTWGT,
				                   0 SUMWGT,
				                   0 RAILWAYWGT,
				                   0 TARGETWGT,
				                   0 ENDWGT,
				                   0 BEGINWGT
				              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','100105')
				             GROUP BY T.BELONG_NAME
				            UNION ALL
				            SELECT BELONG_NAME,
				                   0 ONDAYINWGT,
				                   0 INALLWGT,
				                   SUM(WEIGHT) ONDAYOUTWGT,
				                   0 SUMWGT,
				                   0 RAILWAYWGT,
				                   0 TARGETWGT,
				                   0 ENDWGT,
				                   0 BEGINWGT
				              FROM (SELECT T.BELONG_NAME,
				                           0 ONDAYINWGT,
				                           0 INALLWGT,
				                           CASE
				                             WHEN T.PONDER_NO IS NULL THEN
				                              NVL(T.ACT_WEIGHT, 0)
				                             ELSE
				                              NVL(T.PONDER_GROSS_WT, 0)
				                           END AS WEIGHT,
				                           0 SUMWGT,
				                           0 RAILWAYWGT,
				                           0 TARGETWGT,
				                           0 ENDWGT,
				                           0 BEGINWGT
				                      FROM YDM_ZC_OUTLIST T
				                     WHERE TRUNC(T.OUTSTOCK_TIME) = TRUNC(SYSDATE)
				                       AND T.BAL_YEAR_MONTH = vDate
				                       AND T.OUTSTOCK_TYPE_CODE IN
				                           ('800802', '800805', '800806', '800808')
				                       AND T.BELONG_CODE IN ('100101', '100102', '100103','100105'))
				             GROUP BY BELONG_NAME
				            UNION ALL
				            SELECT BELONG_NAME,
				                   0 ONDAYINWGT,
				                   0 INALLWGT,
				                   0 ONDAYOUTWGT,
				                   SUM(WEIGHT) SUMWGT,
				                   0 RAILWAYWGT,
				                   0 TARGETWGT,
				                   0 ENDWGT,
				                   0 BEGINWGT
				              FROM (SELECT T.BELONG_NAME,
				                           0 ONDAYINWGT,
				                           0 INALLWGT,
				                           0 ONDAYOUTWGT,
				                           CASE
				                             WHEN T.PONDER_NO IS NULL THEN
				                              NVL(T.ACT_WEIGHT, 0)
				                             ELSE
				                              NVL(T.PONDER_GROSS_WT, 0)
				                           END AS WEIGHT,
				                           0 RAILWAYWGT,
				                           0 TARGETWGT,
				                           0 ENDWGT,
				                           0 BEGINWGT
				                      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','100105'))
				             GROUP BY BELONG_NAME
				            UNION ALL
				            SELECT T.BELONG_NAME,
				                   0 ONDAYINWGT,
				                   0 INALLWGT,
				                   0 ONDAYOUTWGT,
				                   0 SUMWGT,
				                   SUM(T.ACT_WEIGHT) RAILWAYWGT,
				                   0 TARGETWGT,
				                   0 ENDWGT,
				                   0 BEGINWGT
				              FROM YDM_ZC_OUTLIST T, YDM_ZC_LOADVEHICLE_MAT S
				             WHERE T.MAT_NO = S.MAT_NO
				               AND S.TRANS_TPE = '110502'
				               AND T.OUTSTOCK_TYPE_CODE IN
				                   ('800802', '800805', '800806', '800808')
				               AND T.BAL_YEAR_MONTH = vDate
				               AND T.BELONG_CODE IN ('100101', '100102', '100103','100105')
				             GROUP BY T.BELONG_NAME
				            UNION ALL
				            SELECT BELONG_NAME,
				                   0 ONDAYINWGT,
				                   0 INALLWGT,
				                   0 ONDAYOUTWGT,
				                   0 SUMWGT,
				                   0 RAILWAYWGT,
				                   SUM(WEIGHT) TARGETWGT,
				                   0 ENDWGT,
				                   0 BEGINWGT
				              FROM (SELECT T.BELONG_NAME,
				                           0 ONDAYINWGT,
				                           0 INALLWGT,
				                           0 ONDAYOUTWGT,
				                           0 SUMWGT,
				                           0 RAILWAYWGT,
				                           CASE
				                             WHEN T.PONDER_NO IS NULL THEN
				                              NVL(T.ACT_WEIGHT, 0)
				                             ELSE
				                              NVL(T.PONDER_GROSS_WT, 0)
				                           END AS WEIGHT,
				                           0 ENDWGT,
				                           0 BEGINWGT
				                      FROM YDM_ZC_OUTLIST T
				                      LEFT JOIN SLM_ORDER_LINE_DELIVERY S
				                        ON T.ORD_LN_DLY_PK = S.ORD_LN_DLY_PK
				                     WHERE T.OUTSTOCK_TYPE_CODE IN
				                           ('800802', '800805', '800806', '800808')
				                       AND T.BAL_YEAR_MONTH = vDate
				                       AND T.BELONG_CODE IN ('100101', '100102', '100103','100105')
				                       AND (S.DELVRY_ADDR IN ('客户库-腾飞',
				                                              '客户库-宝仓',
				                                              '客户库-安利',
				                                              '客户库-润德',
				                                              '客户库-三圆南',
				                                              '客户库-一冶(三圆)',
				                                              '客户库-思创',
				                                              '客户库-滨城') OR
				                           T.STORAGE_NO IN ('LJZ1', 'LYD1', 'LZG1')))
				             GROUP BY BELONG_NAME
				            UNION ALL
				            SELECT T1.BELONG_NAME,
				                   0 ONDAYINWGT,
				                   0 INALLWGT,
				                   0 ONDAYOUTWGT,
				                   0 SUMWGT,
				                   0 RAILWAYWGT,
				                   0 TARGETWGT,
				                   SUM(T1.ACT_WEIGHT) ENDWGT,
				                   0 BEGINWGT
				              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 > '801401'
				                       AND T.BELONG_CODE IN ('100101', '100102', '100103','100105')) T1
				             GROUP BY T1.BELONG_NAME
				            UNION ALL
				            SELECT T.BELONG_NAME,
				                   0 ONDAYINWGT,
				                   0 INALLWGT,
				                   0 ONDAYOUTWGT,
				                   0 SUMWGT,
				                   0 RAILWAYWGT,
				                   0 TARGETWGT,
				                   0 ENDWGT,
				                   SUM(T.ACT_WEIGHT) BEGINWGT
				              FROM YDM_ZC_INITIAL T
				             WHERE T.BAL_YEAR_MONTH = vDate
				               AND T.BELONG_CODE IN ('100101', '100102', '100103','100105')
				             GROUP BY T.BELONG_NAME)
				     GROUP BY BELONG_NAME) LOOP
				   
				     INSERT INTO RPT_YDMSTOCK_STATISTICS
				         (BELONG_NAME, BAL_YEAR_MONTH, ONDAYINWGT, INALLWGT, 
				         ONDAYOUTWGT, SUMWGT, RAILWAYWGT, TARGETWGT, 
				         ENDWGT, INCREMENTWGT)
				     VALUES
				         (DROW.BELONG_NAME, vDate, DROW.ONDAYINWGT, DROW.INALLWGT, 
				         DROW.ONDAYOUTWGT, DROW.SUMWGT, DROW.RAILWAYWGT, DROW.TARGETWGT, 
				         DROW.ENDWGT, DROW.INCREMENTWGT);
				       
				   END LOOP;
				END;			
		                    
		]]>
	</query>


</queryMap>