<?xml version="1.0" encoding="UTF-8"?>
<queryMap desc = "领导报结算统计信息">
	<query id = "CoreBalanceTask.task" desc = "结算统计信息">
		<![CDATA[
			DECLARE
				  vCount INTEGER := 0;
				  vDate VARCHAR2(6) := #vDate# ;
				BEGIN
				  SELECT COUNT(1) INTO vCount FROM RPT_BALANCE_STATISTICS WHERE BAL_YEAR_MONTH = vDate;
				  IF vCount > 0 THEN
				    DELETE RPT_BALANCE_STATISTICS WHERE BAL_YEAR_MONTH = vDate;
				  END IF;
				  FOR DROW IN (
				      SELECT B1.SALE_ORG,
				             NVL(B1.ACT_WEIGHT, 0) ACT_WEIGHT1,
				             NVL(B2.ACT_WEIGHT, 0) ACT_WEIGHT2,
				             NVL(B3.ACT_WEIGHT, 0) ACT_WEIGHT3,
				             NVL(B4.MONEY_ORG, 0) MONEY_ORG4,
				             NVL(B5.ACT_WEIGHT, 0) ACT_WEIGHT5,
				             NVL(B6.ACT_WEIGHT, 0) ACT_WEIGHT6,
				             NVL(B7.ACT_WEIGHT, 0) ACT_WEIGHT7,
				             NVL(B8.MONEY_ORG, 0) MONEY_ORG8,
				             NVL(B9.LST_MONEY, 0) QM_TOL,
				             NVL(B9.MONEY, 0) QM_CZ,
				             NVL(B10.INOUTMONEY, 0) INOUTMONEY
				        FROM ( --首次出库未结算量(吨)
				              SELECT SUM(ACT_WEIGHT) ACT_WEIGHT,
				                      DECODE(T2.SALE_ORG,
				                             '100101',
				                             '国内贸易部',
				                             '100102',
				                             '国际贸易部',
				                             '100103',
				                             '钢贸公司') SALE_ORG
				                FROM (SELECT SUM(NVL(CASE
				                                        WHEN C.PRT_FLAG = '0' AND M.VALIDFLAG > '1' THEN
				                                         M.ACT_WEIGHT
				                                        WHEN C.PRT_FLAG = '1' AND M.VALIDFLAG > '1' THEN
				                                         M.PONDER_GROSS_WT
				                                      END,
				                                      0)) ACT_WEIGHT,
				                              T1.SALE_ORG,
				                              T1.BILL_NO
				                         FROM YDM_ZC_BILL_C C,
				                              (SELECT T.BILL_NO, T.SALE_ORG
				                                 FROM YDM_ZC_BILL_M T
				                                WHERE T.BILL_STATUS > '3'
				                                  AND T.RETURN_FLAG = '0'
				                                  AND TO_CHAR(T.DELETE_TIME, 'YYYYMM') = vDate
				                                GROUP BY T.BILL_NO, T.SALE_ORG) T1,
				                              YDM_ZC_LOADVEHICLE_MAT M
				                        WHERE T1.BILL_NO = C.BILL_NO
				                          AND C.BILL_NO = M.BILL_NO(+)
				                          AND C.MAT_NO = M.MAT_NO(+)
				                          AND C.RETURN_FLAG = '0'
				                          AND (M.RETURN_FLAG = '0' OR M.RETURN_FLAG IS NULL)
				                        GROUP BY T1.SALE_ORG, T1.BILL_NO) T2,
				                      (SELECT B.ASKPLAN_ID
				                         FROM SLM_BALANCEINFO B
				                        WHERE B.BALANCESTATUS_ORG != '3'
				                          AND B.REALINVOICENO_ORG IS NULL
				                        GROUP BY B.ASKPLAN_ID) B
				               WHERE B.ASKPLAN_ID = T2.BILL_NO
				               GROUP BY T2.SALE_ORG) B1,
				             ( --首次总量
				              SELECT SUM(B.WEIGHT) ACT_WEIGHT, B.FORECORPCODE SALE_ORG
				                FROM SLM_BALANCEINFO B
				               WHERE B.BALANCESTATUS_ORG = '3'
				                 AND B.REALINVOICENO_ORG IS NOT NULL
				                 AND TO_CHAR(B.REALINVOICENO_DATE_ORG, 'YYYYMM') = vDate
				               GROUP BY B.FORECORPCODE) B2,
				             ( --首次虚结量
				              SELECT SUM(B.WEIGHT) ACT_WEIGHT,
				                      DECODE(T1.SALE_ORG,
				                             '100101',
				                             '国内贸易部',
				                             '100102',
				                             '国际贸易部',
				                             '100103',
				                             '钢贸公司') SALE_ORG
				                FROM (SELECT T.BILL_NO, T.SALE_ORG
				                         FROM YDM_ZC_BILL_M T
				                        WHERE T.BILL_STATUS < '4'
				                          AND T.LOADVEHICLE_TYPE = '2'
				                          AND T.RETURN_FLAG = '0'
				                        GROUP BY T.BILL_NO, T.SALE_ORG) T1,
				                      (SELECT SUM(B.WEIGHT) WEIGHT, B.ASKPLAN_ID
				                         FROM SLM_BALANCEINFO B
				                        WHERE B.BALANCESTATUS_ORG = '3'
				                          AND B.REALINVOICENO_ORG IS NOT NULL
				                          AND TO_CHAR(B.REALINVOICENO_DATE_ORG, 'YYYYMM') = vDate
				                        GROUP BY B.ASKPLAN_ID) B
				               WHERE B.ASKPLAN_ID = T1.BILL_NO
				               GROUP BY T1.SALE_ORG) B3,
				             ( --首次本月结算金额(元)
				              SELECT SUM(MONEY_ORG) MONEY_ORG, SALE_ORG
				                FROM (SELECT SUM(NVL(CASE
				                                        WHEN B.CONTAIN_TAX = '1' THEN
				                                         B.MONEY_ORG
				                                        WHEN B.CONTAIN_TAX = '0' THEN
				                                         B.MONEY_ORG * 1.13
				                                        ELSE
				                                         0
				                                      END,
				                                      0)) MONEY_ORG,
				                              B.FORECORPCODE SALE_ORG
				                         FROM SLM_BALANCEINFO B
				                        WHERE B.BALANCESTATUS_ORG = '3'
				                          AND B.REALINVOICENO_ORG IS NOT NULL
				                          AND TO_CHAR(B.REALINVOICENO_DATE_ORG, 'YYYYMM') = vDate
				                        GROUP BY B.FORECORPCODE
				                       UNION ALL
				                       SELECT SUM(T.MONEY) * 1.13 MONEY_ORG, T.FORECORPNAME SALE_ORG
				                         FROM SLM_BALANCE_TRANSMONEY T
				                        WHERE T.STATUS_CD_ORG = '2'
				                          AND TO_CHAR(T.INVOICEDATE_ORG, 'YYYYMM') = vDate
				                        GROUP BY T.FORECORPNAME)
				               GROUP BY SALE_ORG) B4,
				             ( --末次出库未结算量(吨)
				              SELECT SUM(ACT_WEIGHT) ACT_WEIGHT,
				                      DECODE(T2.SALE_ORG,
				                             '100101',
				                             '国内贸易部',
				                             '100102',
				                             '国际贸易部',
				                             '100103',
				                             '钢贸公司') SALE_ORG
				                FROM (SELECT SUM(NVL(CASE
				                                        WHEN C.PRT_FLAG = '0' AND M.VALIDFLAG > '1' THEN
				                                         M.ACT_WEIGHT
				                                        WHEN C.PRT_FLAG = '1' AND M.VALIDFLAG > '1' THEN
				                                         M.PONDER_GROSS_WT
				                                      END,
				                                      0)) ACT_WEIGHT,
				                              T1.SALE_ORG,
				                              T1.BILL_NO
				                         FROM YDM_ZC_BILL_C C,
				                              (SELECT T.BILL_NO, T.SALE_ORG
				                                 FROM YDM_ZC_BILL_M T
				                                WHERE T.BILL_STATUS > '3'
				                                  AND T.RETURN_FLAG = '0'
				                                  AND TO_CHAR(T.DELETE_TIME, 'YYYYMM') = vDate
				                                GROUP BY T.BILL_NO, T.SALE_ORG) T1,
				                              YDM_ZC_LOADVEHICLE_MAT M
				                        WHERE T1.BILL_NO = C.BILL_NO
				                          AND C.BILL_NO = M.BILL_NO(+)
				                          AND C.MAT_NO = M.MAT_NO(+)
				                          AND C.RETURN_FLAG = '0'
				                          AND (M.RETURN_FLAG = '0' OR M.RETURN_FLAG IS NULL)
				                        GROUP BY T1.SALE_ORG, T1.BILL_NO) T2,
				                      (SELECT B.ASKPLAN_ID
				                         FROM SLM_BALANCEINFO B
				                        WHERE B.BALANCESTATUS != '3'
				                          AND B.REALINVOICENO IS NULL
				                        GROUP BY B.ASKPLAN_ID) B
				               WHERE B.ASKPLAN_ID = T2.BILL_NO
				               GROUP BY T2.SALE_ORG) B5,
				             ( --末次总量
				              SELECT SUM(B.WEIGHT) ACT_WEIGHT, B.FORECORPCODE SALE_ORG
				                FROM SLM_BALANCEINFO B
				               WHERE B.BALANCESTATUS = '3'
				                 AND B.REALINVOICENO IS NOT NULL
				                 AND TO_CHAR(B.REALINVOICENO_DATE, 'YYYYMM') = vDate
				               GROUP BY B.FORECORPCODE) B6,
				             ( --末次虚结
				              SELECT SUM(B.WEIGHT) ACT_WEIGHT,
				                      DECODE(T1.SALE_ORG,
				                             '100101',
				                             '国内贸易部',
				                             '100102',
				                             '国际贸易部',
				                             '100103',
				                             '钢贸公司') SALE_ORG
				                FROM (SELECT T.BILL_NO, T.SALE_ORG
				                         FROM YDM_ZC_BILL_M T
				                        WHERE T.BILL_STATUS < '4'
				                          AND T.LOADVEHICLE_TYPE = '2'
				                          AND T.RETURN_FLAG = '0'
				                        GROUP BY T.BILL_NO, T.SALE_ORG) T1,
				                      (SELECT SUM(B.WEIGHT) WEIGHT, B.ASKPLAN_ID
				                         FROM SLM_BALANCEINFO B
				                        WHERE B.BALANCESTATUS = '3'
				                          AND B.REALINVOICENO IS NOT NULL
				                          AND TO_CHAR(B.REALINVOICENO_DATE, 'YYYYMM') = vDate
				                        GROUP BY B.ASKPLAN_ID) B
				               WHERE B.ASKPLAN_ID = T1.BILL_NO
				               GROUP BY T1.SALE_ORG) B7,
				             ( --末次本月结算金额(元)
				              SELECT SUM(MONEY_ORG) MONEY_ORG, SALE_ORG
				                FROM (SELECT SUM(NVL(CASE
				                                        WHEN B.CONTAIN_TAX = '1' THEN
				                                         B.MONEY
				                                        WHEN B.CONTAIN_TAX = '0' THEN
				                                         B.MONEY * 1.13
				                                        ELSE
				                                         0
				                                      END,
				                                      0)) MONEY_ORG,
				                              B.FORECORPCODE SALE_ORG
				                         FROM SLM_BALANCEINFO B
				                        WHERE B.BALANCESTATUS = '3'
				                          AND B.REALINVOICENO IS NOT NULL
				                          AND TO_CHAR(B.REALINVOICENO_DATE, 'YYYYMM') = vDate
				                        GROUP BY B.FORECORPCODE
				                       UNION ALL
				                       SELECT SUM(T.MONEY) * 1.13 MONEY_ORG, T.FORECORPNAME SALE_ORG
				                         FROM SLM_BALANCE_TRANSMONEY T
				                        WHERE T.STATUS_CD = '2'
				                          AND TO_CHAR(T.INVOICEDATE, 'YYYYMM') = vDate
				                        GROUP BY T.FORECORPNAME)
				               GROUP BY SALE_ORG) B8,
				             ( --期末应收
				              SELECT BALANCESUBJECT, (PRE_MONEY - LST_MONEY) AS MONEY, LST_MONEY
				                FROM (SELECT B.BALANCESUBJECT,
				                              SUM(NVL(B.PRE_MONEY, 0)) PRE_MONEY,
				                              F.MONEY LST_MONEY
				                         FROM SEL_FUND_RECEIVE_ACCOUNT B
				                         LEFT JOIN (SELECT SUM(S.MONEY) MONEY, S.FORECORPCODE
				                                     FROM (SELECT SUM(S.MONEY) MONEY, S.FORECORPCODE
				                                             FROM SLM_BALANCEINFO S
				                                            WHERE S.BALANCESTATUS = '3'
				                                              AND S.REALINVOICENO IS NOT NULL
				                                              AND TO_CHAR(S.REALINVOICENO_DATE_ORG,
				                                                          'YYYYMM') = vDate
				                                            GROUP BY S.FORECORPCODE
				                                           UNION ALL
				                                           SELECT SUM(B.MONEY) MONEY, B.FORECORPNAME
				                                             FROM SLM_BALANCE_TRANSMONEY B
				                                            WHERE 1 = 1
				                                              AND B.STATUS_CD = '2'
				                                              AND B.TICKETTYPE = '应收'
				                                              AND B.REALINVOICENO IS NOT NULL
				                                              AND TO_CHAR(B.INVOICEDATE, 'YYYYMM') =
				                                                  vDate
				                                            GROUP BY B.FORECORPNAME) S
				                                    GROUP BY S.FORECORPCODE) F
				                           ON B.BALANCESUBJECT = F.FORECORPCODE
				                       
				                        WHERE 1 = 1
				                          AND B.INVOICE_MONTH = vDate
				                        GROUP BY B.BALANCESUBJECT, F.MONEY)) B9,
				             ( --本月回款(元)
				              SELECT SUM(T.INOUTMONEY) INOUTMONEY, T.BALANCESUBJECT
				                FROM SEL_FUND_CUST_FUNDINOUT T
				               WHERE TO_CHAR(T.AFFRIMDATE, 'YYYYMM') = vDate
				                 AND T.ISVALID = '1'
				               GROUP BY T.BALANCESUBJECT) B10
				       WHERE B1.SALE_ORG = B2.SALE_ORG(+)
				         AND B1.SALE_ORG = B3.SALE_ORG(+)
				         AND B1.SALE_ORG = B4.SALE_ORG(+)
				         AND B1.SALE_ORG = B5.SALE_ORG(+)
				         AND B1.SALE_ORG = B6.SALE_ORG(+)
				         AND B1.SALE_ORG = B7.SALE_ORG(+)
				         AND B1.SALE_ORG = B8.SALE_ORG(+)
				         AND B1.SALE_ORG = B9.BALANCESUBJECT(+)
				         AND B1.SALE_ORG = B10.BALANCESUBJECT(+)) LOOP
				         
				         INSERT INTO RPT_BALANCE_STATISTICS
				        (SALE_ORG, BAL_YEAR_MONTH, ACT_WEIGHT1, ACT_WEIGHT2, 
				        ACT_WEIGHT3, MONEY_ORG4, ACT_WEIGHT5, ACT_WEIGHT6, 
				        ACT_WEIGHT7, MONEY_ORG8, QM_TOL, QM_CZ, 
				        INOUTMONEY)
				      VALUES
				        (DROW.SALE_ORG, vDate, DROW.ACT_WEIGHT1, DROW.ACT_WEIGHT2, 
				        DROW.ACT_WEIGHT3, DROW.MONEY_ORG4, DROW.ACT_WEIGHT5, DROW.ACT_WEIGHT6, 
				        DROW.ACT_WEIGHT7, DROW.MONEY_ORG8, DROW.QM_TOL, DROW.QM_CZ, 
				        DROW.INOUTMONEY);  
				       
				   END LOOP;
				END;
							
		                    
		]]>
	</query>


</queryMap>