<?xml version="1.0" encoding="UTF-8"?>
<!--
    $Author: DJ  
    $Date: 2013-7-4 下午5:06:06  
    $Revision: 1.0  
    $Purpose: 
-->
<bm:model xmlns:bm="http://www.leaf-framework.org/schema/bm">
    <bm:fields>
        <bm:field name="record_id"/>
        <bm:field name="cashflow_id"/>
        <bm:field name="product_name" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="PRODUCT_NAME" prompt="ACR.PRODUCT_NAME"/>
        <bm:field name="project_id" databaseType="NUMBER" datatype="java.lang.Long" physicalName="PROJECT_ID"/>
        <bm:field name="project_number" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="PROJECT_NUMBER" prompt="HLS.PROJECT_NUMBER"/>
        <bm:field name="project_name" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="PROJECT_NAME" prompt="HLS.PROJECT_NAME"/>
        <bm:field name="contract_id" databaseType="NUMBER" datatype="java.lang.Long" physicalName="CONTRACT_ID"/>
        <bm:field name="contract_number" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="CONTRACT_NUMBER" prompt="HLS.CONTRACT_NUMBER"/>
        <bm:field name="contract_name" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="CONTRACT_NAME" prompt="HLS.CONTRACT_NAME"/>
        <bm:field name="invoice_kind" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="INVOICE_KIND"/>
        <bm:field name="invoice_kind_desc" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="INVOICE_KIND_DESC" prompt="ACR.INVOICE_TYPE"/>
        <bm:field name="due_amount" databaseType="NUMBER" datatype="java.lang.Double" physicalName="DUE_AMOUNT" prompt="ACR.DUE_AMOUNT"/>
        <bm:field name="principal" databaseType="NUMBER" datatype="java.lang.Double" physicalName="PRINCIPAL" prompt="应收本金"/>
        <bm:field name="interest" databaseType="NUMBER" datatype="java.lang.Double" physicalName="INTEREST" prompt="应收利息"/>
        <bm:field name="cf_billing_amount" databaseType="NUMBER" datatype="java.lang.Double" physicalName="CF_BILLING_AMOUNT" prompt="ACR.BILLING_AMOUNT"/>
        <bm:field name="received_amount" databaseType="NUMBER" datatype="java.lang.Double" physicalName="RECEIVED_AMOUNT" prompt="ACR.RECEIVED_AMOUNT"/>
        <bm:field name="billing_amount" databaseType="NUMBER" datatype="java.lang.Double" physicalName="BILLING_AMOUNT" prompt="ACR.THIS_BILLING_AMOUNT"/>
        <bm:field name="times" databaseType="NUMBER" datatype="java.lang.Long" physicalName="TIMES" prompt="HLS.PERIOD_NUMBER"/>
        <bm:field name="last_received_date" databaseType="DATE" datatype="java.util.Date" physicalName="LAST_RECEIVED_DATE" prompt="ACR.RECEIVED_DATE"/>
        <bm:field name="cf_item" databaseType="NUMBER" datatype="java.lang.Long" physicalName="CF_ITEM" prompt="HLS.RECEIVE_PROJECT"/>
        <bm:field name="cf_item_desc" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="CF_ITEM_DESC" prompt="HLS.RECEIVE_PROJECT"/>
        <bm:field name="currency" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="CURRENCY" prompt="HLS.CURRENCY"/>
        <bm:field name="currency_desc" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="CURRENCY_DESC" prompt="HLS.CURRENCY"/>
        <bm:field name="exchange_rate" databaseType="NUMBER" datatype="java.lang.Double" physicalName="EXCHANGE_RATE" prompt="HLS.EXCHANGE_RATE"/>
        <bm:field name="exchange_rate_type" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="EXCHANGE_RATE_TYPE" prompt="HLS.EXCHANGE_RATE_TYPE"/>
        <bm:field name="exchange_rate_type_desc" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="EXCHANGE_RATE_TYPE_DESC" prompt="HLS.EXCHANGE_RATE_TYPE_DESC"/>
        <bm:field name="billing_object_name" databaseType="VARCHAR2" datatype="java.lang.String"/>
        <bm:field name="contract_status" databaseType="VARCHAR2" datatype="java.lang.String"/>
        <bm:field name="cf_status" databaseType="VARCHAR2" datatype="java.lang.String"/>
		<bm:field name="invoice_kind_type" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="INVOICE_KIND_TYPE"/>
		<bm:field name="invoice_kind_type_n" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="INVOICE_KIND_TYPE_N" prompt="发票种类"/>
        <bm:field name="rl_flag"/>
		<bm:field name="full_elec_invoice_flag"/>
		<bm:field name="full_elec_invoice_flag_n"/>
        <bm:field name="business_type" />
    </bm:fields>
    <bm:operations>
        <bm:operation name="query">
            <bm:query-sql><![CDATA[
            	select t.cashflow_id,
				       t.record_id,
				       t.billing_object,
				       (select c.code_value_name
				          from sys_code_values_v c
				         where c.code = 'ACR510_BILL_DETAIL_METHOD'
				           and c.code_value = t.billing_object) as billing_object_desc,
				         (select m.bp_name
          from hls_bp_master_s_v m
         where m.bp_id =t.bp_id) billing_object_name,

				       t.due_amount,
				       /*-- t.billing_amount,
				       (case when cf.cf_item= 1 and  cf.cf_status='BLOCK' then
				        t.received_amount-nvl(t.cf_billing_amount,0)
				       else
                       t.due_amount-nvl(t.cf_billing_amount,0)
                       end )billing_amount,*/
                       t.billing_amount,
				       t.cf_billing_amount,
				       t.received_amount,
				       t.contract_id,
				       decode(t.cf_item, 101, 0, cf.principal) principal,
                       cf.interest,
				       con.contract_number,
				       con.contract_name,
				       con.project_id,
				       con.project_number,
				       con.project_name,
				       t.invoice_kind,
				       (select v.code_value_name
				          from sys_code_values_v v
				         where v.code = 'ACR510_INVOICE_KIND'
				           and v.code_value = t.invoice_kind) as invoice_kind_desc,
				       t.currency,
				       (select c.currency_name
						  from gld_currency_vl c
						 where c.currency_code = t.currency) as currency_desc,
				       1 as exchange_rate,
				       'MANUAL' as exchange_rate_type,
				       (select v.rate_type_desc
				          from gld_exchange_rate_type_v v
				         where v.rate_type_code = 'MANUAL') as exchange_rate_type_desc,
				       cf.times,
				       cf.last_received_date,
				       t.cf_item,
				       (select i.description
				          from hls_cashflow_item i
				         where i.cf_item = t.cf_item) as cf_item_desc,
					   decode(con.contract_number,'HLL23120001','租赁费',decode(t.cf_item,250,'设备款','融资租赁费')) as product_name,
				       con.contract_status contract_status,
				       cf.cf_status cf_status,
				       t.invoice_kind_type,
				       (select v.code_value_name
						from sys_code_values_v v
						where v.code = 'ACR512_INVOICE_KIND_TYPE'
						and v.code_value = t.invoice_kind_type) invoice_kind_type_n
                   , case when acr_invoice_pkg.check_rl_cf_flag(con.contract_id,cf.cf_item,cf.times)= 'Y' and
                     acr_invoice_pkg.check_rl_flag(con.contract_id) = 'Y'
                     then
                     'Y'
                      when acr_invoice_pkg.check_rl_cf_flag(con.contract_id,cf.cf_item,cf.times)= 'Y' and
                     acr_invoice_pkg.check_rl_flag(con.contract_id) = 'N'
                     then
                     'N'
                     else
                     ''
                    end rl_flag,
                    con.business_type,
					 t.full_elec_invoice_flag,
					(select v.code_value_name value_name from sys_code_values_v v where v.code = 'YES_NO' and v.code_value = t.full_elec_invoice_flag) full_elec_invoice_flag_n

				  from acr_invoice_create_tmp      t,
				       con_contract_v              con,
				       con_contract_cashflow_all_v cf,
                       hls_bp_master t1
				 where t.session_id = ${/session/@session_id}
				   and t.contract_id = con.contract_id
				   and t.cashflow_id = cf.cashflow_id
				   and t1.bp_id = t.bp_id
                   order by con.bp_id_agent_level1,t1.bp_id,con.contract_number,cf.times
			]]></bm:query-sql>
        </bm:operation>

        <bm:operation name="update">
            <bm:update-sql><![CDATA[
				begin
				  ACR_INVOICE_PKG.invoiceTmp_update(p_record_id				=> ${@record_id},
						                            p_invoice_kind          => ${@invoice_kind},
													p_invoice_kind_type     => ${@invoice_kind_type},
						                            p_billing_amount  		=> ${@billing_amount},
						                            p_product_name          => ${@product_name},
						                            p_user_id			  	=> ${/session/@user_id});
				end;
			]]></bm:update-sql>
        </bm:operation>
        <bm:operation name="execute">
            <bm:update-sql><![CDATA[
				begin
					ACR_INVOICE_PKG.create_invoice(p_session_id           => ${/session/@session_id},
						                           p_company_id           => ${@company_id},
						                           p_group_billing_method => ${@group_billing_method},
						                           p_invoice_date         => to_date(${@invoice_date},'yyyy-mm-dd'),
						                           p_accounting_date      => to_date(${@accounting_date},'yyyy-mm-dd'),
						                           p_user_id			  => ${/session/@user_id},
						                           p_role_id			  => ${/session/@role_id});
				end;
			]]></bm:update-sql>
        </bm:operation>
    </bm:operations>
</bm:model>
