<?xml version="1.0" encoding="UTF-8"?>
<bm:model xmlns:bm="http://www.leaf-framework.org/schema/bm" xmlns:f="leaf.database.features" alias="t1" baseTable="CSH_WRITE_OFF_INTERFACE" defaultOrderBy="contract_number asc,write_off_times asc,write_off_cf_item asc">
    <bm:operations>
        <bm:operation name="delete">
            <bm:update-sql><![CDATA[
        begin
        csh_transaction_interface_pkg.delete_write_off_interface(
        p_write_off_interface_id=>${@write_off_interface_id},
        p_user_id => ${/session/@user_id}
        );
        end;
        ]]></bm:update-sql>
        </bm:operation>
        <bm:operation name="insert">
            <bm:update-sql><![CDATA[
begin
  -- Call the procedure
  csh_transaction_interface_pkg.insert_write_off_interface(p_write_off_interface_id => ${@write_off_interface_id},
                                                           p_record_type => ${@record_type},
                                                           p_transaction_id => ${@trx_interface_id},
                                                           p_company_id => ${@company_id},
                                                           p_contract_number => ${@contract_number},
                                                           p_contract_id => ${@contract_id},
                                                           p_write_off_type => ${@write_off_type},
                                                           p_write_off_date => to_date(${@write_off_date},'yyyy-mm-dd'),
                                                           p_write_off_period_name => ${@write_off_period_name},
                                                           p_write_off_period_num => ${@write_off_period_num},
                                                           p_write_off_amount => ${@write_off_amount},
                                                           p_write_off_bp_id => ${@write_off_bp_id},
                                                           p_batch_id => ${@batch_id},
                                                           p_write_off_cashflow_id => ${@write_off_cashflow_id},
                                                           p_write_off_times => ${@write_off_times},
                                                           p_write_off_cf_item => ${@write_off_cf_item},
                                                           p_write_off_principal => ${@write_off_principal},
                                                           p_write_off_interest => ${@write_off_interest},
                                                           p_write_off_description => ${@write_off_description},
                                                           p_post_flag => ${@post_flag},
                                                           p_write_off_classification => ${@write_off_classification},
                                                           p_journal_date => to_date(${@journal_date},'yyyy-mm-dd'),
                                                           p_user_id => ${/session/@user_id});
end;
        ]]></bm:update-sql>
        </bm:operation>
        <bm:operation name="update">
            <bm:update-sql><![CDATA[
begin
  -- Call the procedure
  csh_transaction_interface_pkg.insert_write_off_interface(p_write_off_interface_id => ${@write_off_interface_id},
                                                           p_record_type => ${@record_type},
                                                           p_transaction_id => ${@trx_interface_id},
                                                           p_company_id => ${@company_id},
                                                           p_contract_number => ${@contract_number},
                                                           p_contract_id => ${@contract_id},
                                                           p_write_off_type => ${@write_off_type},
                                                           p_write_off_date => to_date(${@write_off_date},'yyyy-mm-dd'),
                                                           p_write_off_period_name => ${@write_off_period_name},
                                                           p_write_off_period_num => ${@write_off_period_num},
                                                           p_write_off_amount => ${@write_off_amount},
                                                           p_write_off_bp_id => ${@write_off_bp_id},
                                                           p_batch_id => ${@batch_id},
                                                           p_write_off_cashflow_id => ${@write_off_cashflow_id},
                                                           p_write_off_times => ${@write_off_times},
                                                           p_write_off_cf_item => ${@write_off_cf_item},
                                                           p_write_off_principal => ${@write_off_principal},
                                                           p_write_off_interest => ${@write_off_interest},
                                                           p_write_off_description => ${@write_off_description},
                                                           p_post_flag => ${@post_flag},
                                                           p_write_off_classification => ${@write_off_classification},
                                                           p_journal_date => to_date(${@journal_date},'yyyy-mm-dd'),
                                                           p_user_id => ${/session/@user_id});
end;
        ]]></bm:update-sql>
        </bm:operation>
    </bm:operations>
    <bm:fields>
        <bm:field name="write_off_interface_id" databaseType="NUMBER" datatype="java.lang.Long" insertExpression="csh_write_off_interface_s.nextval" physicalName="WRITE_OFF_INTERFACE_ID" prompt="CSH_WRITE_OFF_INTERFACE.WRITE_OFF_INTERFACE_ID"/>
        <bm:field name="batch_id" databaseType="NUMBER" datatype="java.lang.Long" physicalName="BATCH_ID" prompt="CSH_WRITE_OFF_INTERFACE.BATCH_ID"/>
        <bm:field name="trx_interface_id" databaseType="NUMBER" datatype="java.lang.Long" physicalName="TRX_INTERFACE_ID" prompt="CSH_WRITE_OFF_INTERFACE.TRX_INTERFACE_ID"/>
        <!-- <bm:field name="trx_number" databaseType="NUMBER" datatype="java.lang.Long" physicalName="TRX_NUMBER" prompt="CSH_WRITE_OFF_INTERFACE.TRX_NUMBER"/> -->
        <bm:field name="company_id" databaseType="NUMBER" datatype="java.lang.Long" physicalName="COMPANY_ID" prompt="CSH_WRITE_OFF_INTERFACE.COMPANY_ID"/>
        <bm:field name="contract_number" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="CONTRACT_NUMBER" prompt="合同编号" />
        <bm:field name="contract_id" databaseType="NUMBER" datatype="java.lang.Long" physicalName="CONTRACT_ID" prompt="CSH_WRITE_OFF_INTERFACE.CONTRACT_ID"/>
        <bm:field name="write_off_cashflow_id" databaseType="NUMBER" datatype="java.lang.Long" physicalName="WRITE_OFF_CASHFLOW_ID" prompt="CSH_WRITE_OFF_INTERFACE.WRITE_OFF_CASHFLOW_ID"/>
        <bm:field name="due_date" datatype="java.util.Date" expression="(select a.due_date from con_contract_cashflow a where a.cashflow_id = t1.write_off_cashflow_id)" forInsert="false" forUpdate="false"/>
        <bm:field name="write_off_type" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="WRITE_OFF_TYPE" prompt="CSH_WRITE_OFF_INTERFACE.WRITE_OFF_TYPE"/>
        <bm:field name="write_off_type_desc" Expression="(select v.code_value_name from sys_code_values_v v where v.code_value = t1.write_off_type and v.code = &apos;CSH513_WRITE_OFF_TYPE&apos;)" forInsert="false" forUpdate="false"/>
        <bm:field name="write_off_date" databaseType="DATE" datatype="java.util.Date" physicalName="WRITE_OFF_DATE" prompt="CSH_WRITE_OFF_INTERFACE.WRITE_OFF_DATE"/>
        <bm:field name="write_off_period_name" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="WRITE_OFF_PERIOD_NAME" prompt="CSH_WRITE_OFF_INTERFACE.WRITE_OFF_PERIOD_NAME"/>
        <bm:field name="write_off_period_num" databaseType="NUMBER" datatype="java.lang.Long" physicalName="WRITE_OFF_PERIOD_NUM" prompt="CSH_WRITE_OFF_INTERFACE.WRITE_OFF_PERIOD_NUM"/>
        <bm:field name="write_off_times" databaseType="NUMBER" datatype="java.lang.Long" physicalName="WRITE_OFF_TIMES" prompt="CSH_WRITE_OFF_INTERFACE.WRITE_OFF_TIMES"/>
        <bm:field name="write_off_cf_item" databaseType="NUMBER" datatype="java.lang.Long" physicalName="WRITE_OFF_CF_ITEM" prompt="CSH_WRITE_OFF_INTERFACE.WRITE_OFF_CF_ITEM"/>
        <bm:field name="write_off_cf_item_desc" expression="(select a.cf_item_desc from hls_cashflow_item_v a where a.cf_item = t1.write_off_cf_item)" forInsert="false" forUpdate="false"/>
        <bm:field name="write_off_amount" databaseType="NUMBER" datatype="java.lang.Double" physicalName="WRITE_OFF_AMOUNT" prompt="核销金额"/>
        <bm:field name="write_off_principal" databaseType="NUMBER" datatype="java.lang.Double" physicalName="WRITE_OFF_PRINCIPAL" prompt="CSH_WRITE_OFF_INTERFACE.WRITE_OFF_PRINCIPAL"/>
        <bm:field name="write_off_interest" databaseType="NUMBER" datatype="java.lang.Double" physicalName="WRITE_OFF_INTEREST" prompt="CSH_WRITE_OFF_INTERFACE.WRITE_OFF_INTEREST"/>
        <bm:field name="write_off_description" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="WRITE_OFF_DESCRIPTION" prompt="CSH_WRITE_OFF_INTERFACE.WRITE_OFF_DESCRIPTION"/>
        <bm:field name="check_flag" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="CHECK_FLAG" prompt="CSH_WRITE_OFF_INTERFACE.CHECK_FLAG"/>
        <bm:field name="post_flag" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="POST_FLAG" prompt="CSH_WRITE_OFF_INTERFACE.POST_FLAG"/>
        <bm:field name="write_off_id" databaseType="NUMBER" datatype="java.lang.Long" physicalName="WRITE_OFF_ID" prompt="CSH_WRITE_OFF_INTERFACE.WRITE_OFF_ID"/>
        <bm:field name="record_type" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="RECORD_TYPE" prompt="CSH_WRITE_OFF_INTERFACE.RECORD_TYPE"/>
        <bm:field name="write_off_bp_id" databaseType="NUMBER" datatype="java.lang.Long" physicalName="WRITE_OFF_BP_ID" prompt="CSH_WRITE_OFF_INTERFACE.WRITE_OFF_BP_ID"/>
        <bm:field name="write_off_bp_name" Expression="(select a.bp_name from hls_bp_master a where a.bp_id = t1.write_off_bp_id)" forInsert="false" forUpdate="false"/>
        <bm:field name="contract_status_desc" Expression="(select a.contract_status_n  from con_contract_lv a where a.contract_id=t1.contract_id)" forInsert="false" forUpdate="false"/>
        <bm:field name="csh_bank_slip_num" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="CSH_BANK_SLIP_NUM" prompt="CSH_WRITE_OFF_INTERFACE.CSH_BANK_SLIP_NUM"/>
        <bm:field name="error_message" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="ERROR_MESSAGE" prompt="CSH_WRITE_OFF_INTERFACE.ERROR_MESSAGE"/>
        <bm:field name="trx_number" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="TRX_NUMBER" prompt="事务编号"/>
        <bm:field name="left_amount" expression="(select decode(cf_item,1,ca.due_amount - nvl(ca.received_amount,0)) from con_contract_cashflow ca where ca.cashflow_id = t1.write_off_cashflow_id)" forInsert="false" forUpdate="false"/>
        <bm:field name="unreceived_amount" expression="(select ca.due_amount - nvl(ca.received_amount,0) from con_contract_cashflow ca where ca.cashflow_id = t1.write_off_cashflow_id)" forInsert="false" forUpdate="false"/>
        <bm:field name="left_principal" expression="(select ca.principal - nvl(ca.received_principal,0) from con_contract_cashflow ca where ca.cashflow_id = t1.write_off_cashflow_id)" forInsert="false" forUpdate="false"/>
        <bm:field name="left_interest" expression="(select ca.interest - nvl(ca.received_interest,0) from con_contract_cashflow ca where ca.cashflow_id = t1.write_off_cashflow_id)" forInsert="false" forUpdate="false"/>
        <bm:field name="unreceived_penalty" expression="(select ca.due_amount - nvl(ca.received_amount,0) from con_contract_cashflow ca where ca.contract_id = t1.contract_id and ca.times = t1.write_off_times and cf_item = 9 and cf_status = 'RELEASE')" forInsert="false" forUpdate="false"/>
        <bm:field name="agent_id" databaseType="NUMBER" datatype="java.lang.Long"/>
        <bm:field name="agent_id_desc" expression="(select hbm.bp_name from hls_bp_master hbm where hbm.bp_id = t1.agent_id)" forUpdate="false" forInsert="false"/>
        <bm:field name="write_off_classification" lookupField="write_off_classification_desc" lookupCode="DS_CSH_WRITE_OFF_CLASSIFICATION" databaseType="VARCHAR2" datatype="java.lang.String" />
        <bm:field name="write_off_classification_desc" forInsert="false" forUpdate="false"/>
        <bm:field name="bp_tenant_name" expression="(select bp_name from hls_bp_master h,con_contract c where h.bp_id = c.bp_id_tenant and c.contract_id = t1.contract_id)" forInsert="false" forUpdate="false"/>
        <bm:field name="journal_date" databaseType="DATE" datatype="java.util.Date"/>
        <bm:field name="principal" expression="(select principal from con_contract_cashflow where cashflow_id = t1.write_off_cashflow_id)" />
        <bm:field name="interest" expression="(select interest from con_contract_cashflow where cashflow_id = t1.write_off_cashflow_id)" />
        <bm:field name="due_amount" expression="(select decode(cf_item, 9,
                                      CON_OVERDUE_PENALTY_PKG.CALC_PENALTY_FOR_WRITE_OFF(p_cashflow_id=> cashflow_id,
                                                                                         p_calc_date=>to_date(${@transaction_date}, 'yyyy-mm-dd'),
                                                                                         p_user_id=>${/session/@user_id},
                                                                                         p_penalty_flag=>'Y'),
                                      due_amount) from con_contract_cashflow where cashflow_id = t1.write_off_cashflow_id)" />
        <bm:field name="payment_deduction" expression="(select payment_deduction from con_contract c where c.contract_id = t1.contract_id)" forInsert="false" forUpdate="false"/>
    </bm:fields>
    <bm:features>
        <f:standard-who/>
    </bm:features>
    <bm:primary-key>
        <bm:pk-field name="write_off_interface_id"/>
    </bm:primary-key>
    <bm:query-fields>
        <bm:query-field field="batch_id" queryOperator="="/>
        <bm:query-field field="trx_interface_id" queryOperator="="/>
    </bm:query-fields>
</bm:model>
