<?xml version="1.0" encoding="UTF-8"?>
<queryMap desc ="销售台帐查询">

<query id = "CoreSaleAccountQuery.InitCustomerData" desc = "初始化客户信息">

   <![CDATA[

   SELECT T.CUSTOMER_NO, T.CUSTOMER_NM
     FROM SLM_BASE_CUSTOMER T
    WHERE T.VALIDFLAG = '1'

   ]]>

   </query>

  <query id = "CoreSaleAccountQuery.QueryXS" desc = "销售公司台帐查询">

  <![CDATA[

    SELECT * FROM (SELECT B.BALANCESUBJECT,
       B.BUYERCODE,
       B.SECONDACCOUNT,
       B.INVOICE_ACCOUNT,
       K.DEPARTID,
       NVL(B.PRE_MONEY, 0)PRE_MONEY,
       NVL(INMONEY, NVL(C.INOUTMONEY, 0)) INMONEY,
       NVL(INVOICE_MONEY, F.MONEY) INVOICE_MONEY,
       NVL(LST_MONEY,
           NVL(B.PRE_MONEY, 0) - NVL(C.INOUTMONEY, 0) + NVL(F.MONEY, 0)) LST_MONEY,
       B.INVOICE_MONTH
  FROM SEL_FUND_RECEIVE_ACCOUNT B
  LEFT JOIN (SELECT DEPARTNAME,DEPARTID FROM CORE_APP_DEPARTMENT) K ON B.SECONDACCOUNT = K.DEPARTNAME
  LEFT JOIN (SELECT SUM(C.INOUTMONEY) INOUTMONEY,
                    C.BALANCESUBJECT,
                    C. BUYERCODE,
                    C.SECONDACCOUNT
               FROM (SELECT SUM(FU.INOUTMONEY) INOUTMONEY,
                            FU.BALANCESUBJECT,
                            FU.BUYERCODE,
                            FU.SECONDACCOUNT
                       FROM SEL_FUND_CUST_FUNDINOUT FU
                      WHERE FU.ISVALID = '1'
                        AND FU.INOUTTIME > TO_DATE('#TIME#', 'yyyymm')
                        AND FU.MONEY_DELIVER = '2'
                        AND BUSISEQ NOT LIKE '%-1'
                        AND FU.BALANCESUBJECT = '#SALEORG#'
                        AND FU.INOUTTIME <=
                            LAST_DAY(TO_DATE('#TIME#', 'yyyymm')) + 1
                      GROUP BY FU.BALANCESUBJECT,
                               FU.BUYERCODE,
                               FU.SECONDACCOUNT
                     
                     ) C
              GROUP BY C.BALANCESUBJECT, C.BUYERCODE, C.SECONDACCOUNT) C
    ON B.BALANCESUBJECT = C.BALANCESUBJECT
   AND B.BUYERCODE = C.BUYERCODE
   AND B.SECONDACCOUNT = C.SECONDACCOUNT
  LEFT JOIN (SELECT SUM(S.MONEY) MONEY,
                    S.BUYERCODE,
                    S.SALE_AREA_DESC,
                    S.FORECORPCODE
               FROM (SELECT SUM(S.MONEY) MONEY,
                            S.BUYERCODE,
                            S.SALE_AREA_DESC,
                            S.FORECORPCODE
                       FROM SLM_BALANCEINFO S
                      WHERE S.BALANCESTATUS = '3'
                        AND S.REALINVOICENO IS NOT NULL
                        AND S.INVOICE_MONTH = '#TIME#'
                      GROUP BY S.BUYERCODE, S.SALE_AREA_DESC, S.FORECORPCODE
                     UNION ALL
                     SELECT SUM(B.MONEY),
                            B.BUYERCODE,
                            C.SALE_AREA_DESC,
                            B.FORECORPNAME
                       FROM SLM_BALANCE_TRANSMONEY B
                       JOIN SLM_ORDER_HEAD C
                         ON B.ORD_PK = C.ORD_PK
                      WHERE 1 = 1
                        AND B.STATUS_CD = '2'
                        AND B.TICKETTYPE = '应收'
                        AND B.REALINVOICENO IS NOT NULL
                        AND B.INVOICE_MONTH = '#TIME#'
                      GROUP BY B.BUYERCODE, C.SALE_AREA_DESC, B.FORECORPNAME) S
              GROUP BY S.BUYERCODE, S.SALE_AREA_DESC, S.FORECORPCODE) F
    ON B.BALANCESUBJECT = F.FORECORPCODE
   AND B.BUYERCODE = F.BUYERCODE
   AND B.SECONDACCOUNT = F.SALE_AREA_DESC
 WHERE 1 = 1
   AND B.INVOICE_MONTH = '#TIME#'
   AND B.BALANCESUBJECT = '#SALEORG#') WHERE 1=1
    

  ]]>

  </query>
  
   <query id = "CoreSaleAccountQuery.QueryGM" desc = "钢贸公司台帐查询">

  <![CDATA[
   
   
SELECT * FROM (select b.balancesubject,
       b.buyercode,
       b.secondaccount,
       K.DEPARTID,
       b.INVOICE_ACCOUNT,
       nvl(b.pre_money, 0) pre_money,
       nvl(inmoney, nvl(c.inoutmoney, 0)) INMONEY,
       nvl(invoice_money, f.money) INVOICE_MONEY,
       nvl(lst_money,
           nvl(b.pre_money, 0) - nvl(c.inoutmoney, 0) + nvl(f.money, 0)) LST_MONEY,
       b.invoice_month
  from SEL_FUND_RECEIVE_ACCOUNT b
  LEFT JOIN (SELECT DEPARTNAME,DEPARTID FROM CORE_APP_DEPARTMENT) K ON B.SECONDACCOUNT = K.DEPARTNAME
  left join (select sum(fu.inoutmoney) inoutmoney,
                    fu.balancesubject,
                    fu.buyercode,
                    fu.secondaccount
               from sel_fund_cust_fundinout fu
              where fu.isvalid = '1'
                and money_deliver = '0'
                and fu.inouttime > to_date('#TIME#', 'yyyymm')
                and fu.inouttime <=
                    last_day(to_date('#TIME#', 'yyyymm')) + 1
              group by fu.balancesubject, fu.buyercode, fu.secondaccount) c
    on b.balancesubject = c.balancesubject
   and b.buyercode = c.buyercode
   and b.secondaccount = c.secondaccount
  left join (select sum(s.money) money,
                    s.buyercode,
                    s.sale_area_desc,
                    s.forecorpcode
               from (select sum(s.money) money,
                            s.buyercode,
                            s.sale_area_desc,
                            s.forecorpcode
                       from slm_balanceinfo s
                      where s.balancestatus = '3'
                        and s.realinvoiceno is not null
                        and s.forecorpcode = '#SALEORG#'
                        and s.invoice_month = '#TIME#'
                      group by s.buyercode, s.sale_area_desc, s.forecorpcode
                     union all
                     select sum(b.money),
                            b.buyercode,
                            c.sale_area_desc,
                            b.forecorpname
                       from slm_balance_transmoney b
                       join slm_order_head c
                         on b.ord_pk = c.ord_pk
                      where 1 = 1
                        and b.status_cd = '2'
                        and b.tickettype = '应收'
                        and b.invoice_month = '#TIME#'
                        and b.realinvoiceno is not null
                        and b.forecorpname = '#SALEORG#'
                     
                      group by b.buyercode, c.sale_area_desc, b.forecorpname) s
              group by s.buyercode, s.sale_area_desc, s.forecorpcode) f
    on b.balancesubject = f.forecorpcode
   and b.buyercode = f.buyercode
   and b.secondaccount = f.sale_area_desc
 where 1 = 1
   and b.balancesubject = '#SALEORG#'
   and b.invoice_month = '#TIME#') WHERE 1=1
   

  ]]>

  </query>



</queryMap>