<?xml version="1.0" encoding="UTF-8"?>
<bm:model xmlns:bm="http://www.leaf-framework.org/schema/bm" xmlns:f="leaf.database.features">
    <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:operation name="query">
            <bm:query-sql><![CDATA[
                select * from (
                select write_off_id,
                       (select company_id from csh_transaction where transaction_id = t1.csh_transaction_id) company_id,
                       t1.csh_transaction_id transaction_id,
                       (select contract_number from con_contract where contract_id = t1.contract_id) contract_number,
                       contract_id,
                       cashflow_id write_off_cashflow_id,
                       (select a.due_date from con_contract_cashflow a where a.cashflow_id = t1.cashflow_id) due_date,
                       write_off_date,
                       write_off_type,
                       period_name write_off_period_name,
                       t1.INTERNAL_PERIOD_NUM write_off_period_num,
                       times write_off_times,
                       cf_item write_off_cf_item,
                       (select a.cf_item_desc from hls_cashflow_item_v a where a.cf_item = t1.cf_item) write_off_cf_item_desc,
                       t1.WRITE_OFF_DUE_AMOUNT write_off_amount,
                       t1.WRITE_OFF_PRINCIPAL,
                       t1.WRITE_OFF_INTEREST,
                       (select principal from con_contract_cashflow where cashflow_id = t1.cashflow_id) principal,
                       (select interest from con_contract_cashflow where cashflow_id = t1.cashflow_id) interest,
                       (select decode(cf_item, 9,
                                      CON_OVERDUE_PENALTY_PKG.CALC_PENALTY_FOR_WRITE_OFF(p_cashflow_id=>t1.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.cashflow_id) due_amount,
                       (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) bp_tenant_name
                       from csh_write_off t1) t
                       #WHERE_CLAUSE#
            ]]></bm:query-sql>
        </bm:operation>
    </bm:operations>
    <bm:fields>
        <bm:field name="company_id" databaseType="NUMBER" datatype="java.lang.Long" physicalName="COMPANY_ID"
                  prompt="CSH_WRITE_OFF.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.CONTRACT_ID"/>
        <bm:field name="write_off_cashflow_id" databaseType="NUMBER" datatype="java.lang.Long"
                  physicalName="WRITE_OFF_CASHFLOW_ID" prompt="CSH_WRITE_OFF.WRITE_OFF_CASHFLOW_ID"/>
        <bm:field name="due_date" datatype="java.util.Date" forInsert="false" forUpdate="false"/>
        <bm:field name="write_off_type" databaseType="VARCHAR2" datatype="java.lang.String"
                  physicalName="WRITE_OFF_TYPE" prompt="CSH_WRITE_OFF.WRITE_OFF_TYPE"/>
        <bm:field name="write_off_date" databaseType="DATE" datatype="java.util.Date" physicalName="WRITE_OFF_DATE"
                  prompt="CSH_WRITE_OFF.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.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.WRITE_OFF_PERIOD_NUM"/>
        <bm:field name="write_off_times" databaseType="NUMBER" datatype="java.lang.Long" physicalName="WRITE_OFF_TIMES"
                  prompt="CSH_WRITE_OFF.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.WRITE_OFF_CF_ITEM"/>
        <bm:field name="write_off_cf_item_desc" 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.WRITE_OFF_PRINCIPAL"/>-->
        <!--        <bm:field name="write_off_interest" databaseType="NUMBER" datatype="java.lang.Double" physicalName="WRITE_OFF_INTEREST" prompt="CSH_WRITE_OFF.WRITE_OFF_INTEREST"/>-->
        <!--        <bm:field name="write_off_description" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="WRITE_OFF_DESCRIPTION" prompt="CSH_WRITE_OFF.WRITE_OFF_DESCRIPTION"/>-->
        <bm:field name="write_off_id" databaseType="NUMBER" datatype="java.lang.Long" physicalName="WRITE_OFF_ID"
                  prompt="CSH_WRITE_OFF.WRITE_OFF_ID"/>
        <bm:field name="transaction_id"/>
        <bm:field name="bp_tenant_name"/>
        <bm:field name="principal"/>
        <bm:field name="interest"/>
        <bm:field name="due_amount"/>
    </bm:fields>
    <bm:query-fields>
        <bm:query-field field="transaction_id" queryOperator="="/>
    </bm:query-fields>
    <bm:data-filters>
        <bm:data-filter name="query" expression="t.contract_id is not null"/>
    </bm:data-filters>
</bm:model>
