<?xml version="1.0" encoding="UTF-8"?>
<!--
    $Author: leaf Generator  
    $Date: Wed Oct 09 11:00:38 CST 2019  
    $Revision: 1.0  
    $Purpose: 
-->
<bm:model xmlns:bm="http://www.leaf-framework.org/schema/bm" xmlns:e="leaf.service.exception"
          xmlns:o="leaf.database.local.oracle" xmlns:f="leaf.database.features" alias="t1"
          baseTable="HLS_EBANK_BATCH_LN">
    <bm:fields>
        <bm:field name="ln_id" databaseType="NUMBER" datatype="java.lang.Long" physicalName="LN_ID"
                  prompt="HLS_EBANK_BATCH_LN.LN_ID"/>
        <bm:field name="hd_id" databaseType="NUMBER" datatype="java.lang.Long" physicalName="HD_ID"
                  prompt="HLS_EBANK_BATCH_LN.HD_ID"/>
        <bm:field name="batch_id" databaseType="NUMBER" datatype="java.lang.Long" physicalName="BATCH_ID"
                  prompt="HLS_EBANK_BATCH_LN.BATCH_ID"/>
        <bm:field name="group_id" databaseType="NUMBER" datatype="java.lang.Long" physicalName="GROUP_ID"
                  prompt="HLS_EBANK_BATCH_LN.GROUP_ID"/>
        <bm:field name="cashflow_id" databaseType="NUMBER" datatype="java.lang.Long" physicalName="CASHFLOW_ID"
                  prompt="HLS_EBANK_BATCH_LN.CASHFLOW_ID"/>
        <bm:field name="amount" databaseType="NUMBER" datatype="java.lang.Double" physicalName="AMOUNT"
                  prompt="HLS_EBANK_BATCH_LN.AMOUNT"/>
        <bm:field name="manual_amount" databaseType="NUMBER" datatype="java.lang.Double" physicalName="MANUAL_AMOUNT"
                  prompt="HLS_EBANK_BATCH_LN.MANUAL_AMOUNT"/>
        <bm:field name="write_off_flag" databaseType="VARCHAR2" datatype="java.lang.String"
                  physicalName="WRITE_OFF_FLAG" prompt="HLS_EBANK_BATCH_LN.WRITE_OFF_FLAG"/>
        <bm:field name="description" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="DESCRIPTION"
                  prompt="HLS_EBANK_BATCH_LN.DESCRIPTION"/>
        <bm:field name="contract_number" forInsert="false" forceUpdate="false" expression="(select contract_number from con_contract c,con_contract_cashflow cf where c.contract_id = cf.contract_id and cf.cashflow_id = t1.cashflow_id)"/>
        <bm:field name="cf_item_desc" forInsert="false" forceUpdate="false" expression="(select description from hls_cashflow_item hi,con_contract_cashflow cc where hi.cf_item = cc.cf_item and cc.cashflow_id = t1.cashflow_id)"/>
        <bm:field name="bp_tenant_name" forInsert="false" forceUpdate="false" expression="(select h.bp_name from hls_bp_master h,con_contract c,con_contract_cashflow cf where c.contract_id = cf.contract_id and cf.cashflow_id = t1.cashflow_id and c.bp_id_tenant = h.bp_id)"/>
        <bm:field name="times" forceUpdate="false" forInsert="false" expression="(select times from con_contract_cashflow where cashflow_id = t1.cashflow_id)"/>
        <bm:field name="due_date" forceUpdate="false" forInsert="false" expression="(select to_char(due_date,'yyyy-mm-dd') from con_contract_cashflow where cashflow_id = t1.cashflow_id)"/>
<!--        <bm:field name="due_amount" forceUpdate="false" forInsert="false" expression="(select due_amount - nvl(received_amount,0) from con_contract_cashflow where cashflow_id = t1.cashflow_id)"/>-->
        <bm:field name="due_amount" forceUpdate="false" forInsert="false" expression="((select decode(cf_item, 9,
                                        CON_OVERDUE_PENALTY_PKG.CALC_PENALTY_FOR_WRITE_OFF(p_cashflow_id=> cashflow_id,
                                                                                           p_calc_date=>(select due_date from hls_ebank_batch where batch_id = t1.batch_id),
                                                                                           p_user_id=>${/session/@user_id},
                                                                                           p_penalty_flag=>'Y'),
                                        due_amount)
                          from con_contract_cashflow
                          where cashflow_id = t1.cashflow_id) - nvl((select sum(CSH_WRITE_OFF_AMOUNT)
                                                                  from csh_write_off
                                                                  where cashflow_id = t1.cashflow_id and reversed_flag = 'N'
                                                                    and write_off_date &lt; (select due_date from hls_ebank_batch where batch_id = t1.batch_id)),0))"/>
    </bm:fields>
    <bm:query-fields>
        <bm:query-field name="batch_id" queryExpression="t1.batch_id = ${@batch_id}"/>
    </bm:query-fields>
    <bm:primary-key>
        <bm:pk-field name="ln_id"/>
    </bm:primary-key>
    <bm:features>
        <f:standard-who xmlns:f="leaf.database.features"/>
        <o:sequence-pk sequenceName="hls_ebank_batch_ln_s"/>
    </bm:features>
    <bm:operations>
        <bm:operation name="insert">
            <bm:update-sql><![CDATA[
            begin
                hls_ebank_batch_pkg.insert_hls_ebank_batch_ln_new(p_ln_id => hls_ebank_batch_ln_s.nextval,
                                                              p_hd_id => '',
                                                              p_manual_amount => ${@manual_amount},
                                                              p_batch_id => ${../../@batch_id},
                                                              p_cashflow_id => ${@cashflow_id},
                                                              p_bank_account_num => ${@bank_account_num},
                                                              p_bp_id => ${@bp_id},
                                                              p_user_id => ${/session/@user_id});
            end;
            ]]></bm:update-sql>
            <bm:parameters>
                <bm:parameter name="p_ln_id" dataType="java.lang.Long" input="false" output="true"
                              outputPath="/parameter/@p_ln_id"/>
            </bm:parameters>
        </bm:operation>
        <bm:operation name="execute">
            <bm:update-sql><![CDATA[
            begin
                hls_ebank_batch_pkg.contract_merge_by_rules(p_batch_id => ${@batch_id},
                                                            p_user_id => ${/session/@user_id});
            end;
            ]]></bm:update-sql>
            <bm:parameters>
                <bm:parameter name="p_ln_id" dataType="java.lang.Long" input="false" output="true"
                              outputPath="/parameter/@p_ln_id"/>
            </bm:parameters>
        </bm:operation>
    </bm:operations>
</bm:model>