<?xml version="1.0" encoding="UTF-8"?>
<!--
    $Author: Guoxuezhao  
    $Date: 2013-5-31 下午2:14:45  
    $Revision: 1.0  
    $Purpose: 已提本金明细表
-->
<bm:model xmlns:ns2="leaf.database.local.oracle" xmlns:f="leaf.database.features" xmlns:bm="http://www.leaf-framework.org/schema/bm" alias="t1" baseTable="TRE_LOAN_CONTRACT_WITHDRAW" needAccessControl="false">
    <bm:fields>
        <bm:field name="withdraw_id" databaseType="NUMBER" datatype="java.lang.Long" physicalName="WITHDRAW_ID" prompt="TRE_LOAN_CONTRACT_WITHDRAW.WITHDRAW_ID"/>
        <bm:field name="loan_contract_id" databaseType="NUMBER" datatype="java.lang.Long" physicalName="LOAN_CONTRACT_ID" prompt="TRE_LOAN_CONTRACT_WITHDRAW.LOAN_CONTRACT_ID"/>
        <bm:field name="withdraw_number" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="WITHDRAW_NUMBER" prompt="TRE_LOAN_CONTRACT_WITHDRAW.WITHDRAW_NUMBER"/>
        <bm:field name="withdraw_date" databaseType="DATE" datatype="java.util.Date" physicalName="WITHDRAW_DATE" prompt="TRE_LOAN_CONTRACT_WITHDRAW.WITHDRAW_DATE"/>
        <bm:field name="period_name" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="PERIOD_NAME" prompt="TRE_LOAN_CONTRACT_WITHDRAW.PERIOD_NAME"/>
        <bm:field name="internal_period_num" databaseType="NUMBER" datatype="java.lang.Double" physicalName="INTERNAL_PERIOD_NUM" prompt="TRE_LOAN_CONTRACT_WITHDRAW.INTERNAL_PERIOD_NUM"/>
        <bm:field name="withdraw_amount" databaseType="NUMBER" datatype="java.lang.Double" physicalName="WITHDRAW_AMOUNT" prompt="TRE_LOAN_CONTRACT_WITHDRAW.WITHDRAW_AMOUNT"/>
        <bm:field name="repayment_date" databaseType="DATE" datatype="java.util.Date" physicalName="REPAYMENT_DATE" prompt="TRE_LOAN_CONTRACT_WITHDRAW.REPAYMENT_DATE"/>
        <bm:field name="int_rate" databaseType="NUMBER" datatype="java.lang.Double" physicalName="INT_RATE" prompt="TRE_LOAN_CONTRACT_WITHDRAW.INT_RATE"/>
        <bm:field name="currency" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="CURRENCY" prompt="TRE_LOAN_CONTRACT_WITHDRAW.CURRENCY"/>
        <bm:field name="exchange_rate_type" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="EXCHANGE_RATE_TYPE" prompt="TRE_LOAN_CONTRACT_WITHDRAW.EXCHANGE_RATE_TYPE"/>
        <bm:field name="exchange_rate_type_desc" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="EXCHANGE_RATE_TYPE_DESC" prompt="TRE_LOAN_CONTRACT_WITHDRAW.EXCHANGE_RATE_TYPE_DESC"/>
        <bm:field name="exchange_rate" databaseType="NUMBER" datatype="java.lang.Double" physicalName="EXCHANGE_RATE" prompt="TRE_LOAN_CONTRACT_WITHDRAW.EXCHANGE_RATE"/>
        <bm:field name="repurchase_date" databaseType="DATE" datatype="java.util.Date" physicalName="REPURCHASE_DATE" prompt="TRE_LOAN_CONTRACT_WITHDRAW.REPURCHASE_DATE"/>
        <bm:field name="repurchase_amount" databaseType="NUMBER" datatype="java.lang.Long" physicalName="REPURCHASE_AMOUNT" prompt="TRE_LOAN_CONTRACT_WITHDRAW.REPURCHASE_AMOUNT"/>
        <bm:field name="status" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="STATUS" prompt="TRE_LOAN_CONTRACT_WITHDRAW.STATUS"/>
        <bm:field name="status_name" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="STATUS_NAME" prompt="TRE_LOAN_CONTRACT_WITHDRAW.STATUS"/>
        <bm:field name="reviewed_by_name" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="REVIEWED_BY_NAME" prompt="TRE_LOAN_CONTRACT_WITHDRAW.REVIEWED_BY"/>
        <bm:field name="reviewed_by" databaseType="NUMBER" datatype="java.lang.Long" physicalName="REVIEWED_BY" prompt="TRE_LOAN_CONTRACT_WITHDRAW.REVIEWED_BY"/>
        <bm:field name="contract_id" databaseType="NUMBER" datatype="java.lang.Long" physicalName="CONTRACT_ID" prompt="TRE_LOAN_CONTRACT_WITHDRAW.CONTRACT_ID"/>
        <bm:field name="contract_number" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="CONTRACT_NUMBER" prompt="TRE_LOAN_CONTRACT_WITHDRAW.CONTRACT_NUMBER"/>
        <bm:field name="contract_name" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="CONTRACT_NAME" prompt="TRE_LOAN_CONTRACT_WITHDRAW.CONTRACT_NAME"/>
        <bm:field name="vat_rate_of_interest" databaseType="NUMBER" datatype="java.lang.Double" physicalName="VAT_RATE_OF_INTEREST" prompt="TRE_LOAN_CONTRACT_WITHDRAW.VAT_RATE_OF_INTEREST"/>
        <bm:field name="note" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="NOTE" prompt="TRE_LOAN_CONTRACT_WITHDRAW.NOTE"/>
        <bm:field name="currency_name" databaseType="VARCHAR2" datatype="java.lang.String" forInsert="false" forUpdate="false"/>
        <bm:field name="reversed_flag" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="REVERSED_FLAG" prompt="TRE_LOAN_CONTRACT_WITHDRAW.REVERSED_FLAG"/>
        <bm:field name="reversed_date" databaseType="DATE" datatype="java.util.Date" physicalName="REVERSED_DATE" prompt="TRE_LOAN_CONTRACT_WITHDRAW.REVERSED_DATE"/>
        <bm:field name="reversed_withdraw_id" databaseType="NUMBER" datatype="java.lang.Long" physicalName="REVERSED_WITHDRAW_ID" prompt="TRE_LOAN_CONTRACT_WITHDRAW.REVERSED_WITHDRAW_ID"/>
        <bm:field name="create_je_flag" forInsert="false" forUpdate="false"/>
        <bm:field name="signing_num" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="SIGNING_NUM" prompt="TRE_LOAN_CONTRACT_WITHDRAW.SIGNING_NUM"/>
        <bm:field name="signing_date" databaseType="DATE" datatype="java.util.Date" physicalName="SIGNING_DATE" prompt="TRE_LOAN_CONTRACT_WITHDRAW.SIGNING_DATE"/>
    </bm:fields>
    <bm:operations>
        <bm:operation name="query">
            <bm:query-sql><![CDATA[
                SELECT
                    lcw.withdraw_id,
                    lcw.loan_contract_id,
                    lcw.withdraw_number,
                    lcw.withdraw_date,
                    lcw.period_name,
                    lcw.internal_period_num,
                    lcw.withdraw_amount,
                    lcw.repayment_date,
                    lcw.int_rate,
                    lcw.currency,
                    lcw.currency_name,
                    lcw.exchange_rate_type,
                    lcw.exchange_rate_type_desc,
                    lcw.exchange_rate,
                    lcw.repurchase_date,
                    lcw.repurchase_amount,
                    lcw.status,
                    lcw.status_name,
                    lcw.contract_id,
                    lcw.contract_number,
                    lcw.contract_name,
                    lcw.vat_rate_of_interest,
                    lcw.note,
                    lcw.signing_date,
                    lcw.signing_num,
                    lcw.reviewed_by,
                    lcw.reviewed_by_name,
                    lcw.reversed_flag,
                    lcw.reversed_date,
                    lcw.reversed_withdraw_id,
                    (SELECT
                        t.create_je_flag
                    FROM
                        csh_transaction t
                    WHERE
                        t.source_doc_category = 'LOAN_WITHDRAW' AND
                        t.source_doc_id       = lcw.withdraw_id AND
                        t.reversed_flag       = 'N'
                    ) create_je_flag
                FROM
                    tre_loan_contract_withdraw_v lcw
                    
                    #WHERE_CLAUSE#
                ORDER BY
                    lcw.withdraw_id
            ]]></bm:query-sql>
        </bm:operation>
        <bm:operation name="insert">
            <bm:update-sql><![CDATA[
        		begin
    				tre_loan_contract_pkg.detail_check_after_review(p_loan_contract_id => ${@loan_contract_id},
                                                   				    p_user_id          => ${/session/@user_id});
        			tre_loan_con_withdraw_pkg.insert_loan_con_withdraw(p_withdraw_id      =>${@withdraw_id},
        			                                                 p_loan_contract_id   =>${@loan_contract_id},
								                                     p_withdraw_number    =>${@withdraw_number},
								                                     p_withdraw_date      =>to_date(${@withdraw_date},'yyyy-mm-dd'),
								                                     p_period_name        =>${@period_name},
								                                     p_internal_period_num=>${@internal_period_num},
								                                     p_withdraw_amount    =>${@withdraw_amount},
								                                     p_repayment_date     =>to_date(${@repayment_date},'yyyy-mm-dd'),
								                                     p_int_rate           =>${@int_rate},
								                                     p_currency           =>${@currency},
								                                     p_exchange_rate_type =>${@exchange_rate_type},
								                                     p_exchange_rate      =>${@exchange_rate},
								                                     p_contract_id        =>${@contract_id},
								                                     p_note               =>${@note},
								                                     p_signing_date       =>to_date(${@signing_date},'yyyy-mm-dd'),
								                                     p_signing_num        =>${@signing_num},
								                                     p_user_id            =>${/session/@user_id});
        		
        		end;
        	]]></bm:update-sql>
            <bm:parameters>
                <bm:parameter name="withdraw_id" dataType="java.lang.Long" input="false" output="true" outputPath="@withdraw_id"/>
            </bm:parameters>
        </bm:operation>
        <bm:operation name="update">
            <bm:query-sql><![CDATA[
        		begin
        			tre_loan_contract_pkg.detail_check_after_review(p_loan_contract_id => ${@loan_contract_id},
                                                   				    p_user_id          => ${/session/@user_id});
        			tre_loan_con_withdraw_pkg.update_loan_con_withdraw(p_withdraw_id        =>${@withdraw_id},
								                                     p_loan_contract_id   =>${@loan_contract_id},
								                                     p_withdraw_number    =>${@withdraw_number},
								                                     p_withdraw_date      =>to_date(${@withdraw_date},'yyyy-mm-dd'),
								                                     p_period_name        =>${@period_name},
								                                     p_internal_period_num=>${@internal_period_num},
								                                     p_withdraw_amount    =>${@withdraw_amount},
								                                     p_repayment_date     =>to_date(${@repayment_date},'yyyy-mm-dd'),
								                                     p_int_rate           =>${@int_rate},
								                                     p_currency           =>${@currency},
								                                     p_exchange_rate_type =>${@exchange_rate_type},
								                                     p_exchange_rate      =>${@exchange_rate},
								                                     p_contract_id        =>${@contract_id},
								                                     p_note               =>${@note},
								                                     p_signing_date       =>to_date(${@signing_date},'yyyy-mm-dd'),
								                                     p_signing_num        =>${@signing_num},
								                                     p_user_id            =>${/session/@user_id},
								                                     p_company_id         =>${/session/@company_id});
        		end;
        	]]></bm:query-sql>
        </bm:operation>
        <bm:operation name="delete">
            <bm:update-sql><![CDATA[
        		begin
        			tre_loan_contract_pkg.detail_check_after_review(p_loan_contract_id => ${@loan_contract_id},
                                                   				   p_user_id          => ${/session/@user_id});
        			tre_loan_con_withdraw_pkg.del_loan_con_withdraw(p_withdraw_id =>${@withdraw_id},
																	p_user_id 	=>${/session/@user_id});
        		end;
        	]]></bm:update-sql>
        </bm:operation>
    </bm:operations>
    <bm:features>
        <ns2:sequence-pk/>
        <f:standard-who/>
    </bm:features>
    <bm:primary-key>
        <bm:pk-field name="withdraw_id"/>
    </bm:primary-key>
    <bm:query-fields>
        <!-- <bm:query-field field="withdraw_number" queryOperator="like"/> -->
        <bm:query-field field="reversed_flag" queryOperator="="/>
        <bm:query-field field="withdraw_id" queryOperator="="/>
        <bm:query-field field="status" queryOperator="="/>
        <bm:query-field name="withdraw_date_from" datatype="java.sql.Date" queryExpression="lcw.withdraw_date &gt;= ${@withdraw_date_from}"/>
        <bm:query-field name="withdraw_date_to" datatype="java.sql.Date" queryExpression="lcw.withdraw_date &lt;= ${@withdraw_date_to}"/>
    </bm:query-fields>
    <bm:data-filters>
        <bm:data-filter enforceOperations="query" expression="lcw.loan_contract_id=${@loan_contract_id} and lcw.status=&apos;NEW&apos; and not exists (select 1 from tre_loan_contract_withdraw tw, tre_con_withdraw_interface te where tw.withdraw_id =lcw.withdraw_id and tw.withdraw_interface_id = te.withdraw_interface_id)"/>
    </bm:data-filters>
</bm:model>
