<?xml version="1.0" encoding="UTF-8"?>
<!--
    $Author: Guoxuezhao  
    $Date: 2013-5-28 下午1:19:35  
    $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_BANK_CREDIT_CONTRACT" needAccessControl="false">
    <bm:fields>
        <bm:field name="credit_contract_id" databaseType="NUMBER" datatype="java.lang.Long" physicalName="CREDIT_CONTRACT_ID" prompt="TRE_BANK_CREDIT_CONTRACT.CREDIT_CONTRACT_ID"/>
        <bm:field name="credit_class" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="CREDIT_CLASS" prompt="TRE_BANK_CREDIT_CONTRACT.CREDIT_CLASS"/>
        <bm:field name="credit_limit_type" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="CREDIT_LIMIT_TYPE" prompt="TRE_BANK_CREDIT_CONTRACT.CREDIT_LIMIT_TYPE"/>
        <bm:field name="bp_id" databaseType="NUMBER" datatype="java.lang.Long" physicalName="BP_ID" prompt="TRE_BANK_CREDIT_CONTRACT.BANK_ID"/>
        <!-- <bm:field name="bank_branch_name" databaseType="VARCHAR2" datatype="java.lang.String" forInsert="false" forUpdate="false"/> -->
        <bm:field name="credit_contract_number" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="CREDIT_CONTRACT_NUMBER" prompt="TRE_BANK_CREDIT_CONTRACT.CREDIT_CONTRACT_NUMBER"/>
        <bm:field name="credit_amount" databaseType="NUMBER" datatype="java.lang.Double" physicalName="CREDIT_AMOUNT" prompt="TRE_BANK_CREDIT_CONTRACT.CREDIT_AMOUNT"/>
        <bm:field name="available_amount" databaseType="NUMBER" datatype="java.lang.Double" forInsert="false" forUpdate="false"/>
        <bm:field name="currency" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="CURRENCY" prompt="TRE_BANK_CREDIT_CONTRACT.CURRENCY"/>
        <!-- <bm:field name="currency_name" databaseType="VARCHAR2" datatype="java.lang.String" forInsert="false" forUpdate="false"/> -->
        <bm:field name="credit_date_from" databaseType="DATE" datatype="java.util.Date" physicalName="CREDIT_DATE_FROM" prompt="TRE_BANK_CREDIT_CONTRACT.CREDIT_DATE_FROM"/>
        <bm:field name="credit_date_to" databaseType="DATE" datatype="java.util.Date" physicalName="CREDIT_DATE_TO" prompt="TRE_BANK_CREDIT_CONTRACT.CREDIT_DATE_TO"/>
        <bm:field name="notes" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="NOTES" prompt="TRE_BANK_CREDIT_CONTRACT.NOTES"/>
        <bm:field name="credit_limit_category" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="CREDIT_LIMIT_CATEGORY"/>
        <bm:field name="business_term" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="BUSINESS_TERM" prompt="TRE_BANK_CREDIT_CONTRACT.BUSINESS_TERM"/>
        <bm:field name="purpose" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="PURPOSE" prompt="TRE_BANK_CREDIT_CONTRACT.PURPOSE"/>
        <bm:field name="director_resolution" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="DIRECTOR_RESOLUTION" prompt="TRE_BANK_CREDIT_CONTRACT.DIRECTOR_RESOLUTION"/>
        <!-- <bm:field name="ref_v01" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="REF_V01" prompt="TRE_BANK_CREDIT_CONTRACT.REF_V01"/>
        <bm:field name="ref_v02" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="REF_V02" prompt="TRE_BANK_CREDIT_CONTRACT.REF_V02"/>
        <bm:field name="ref_v03" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="REF_V03" prompt="TRE_BANK_CREDIT_CONTRACT.REF_V03"/>
        <bm:field name="ref_v04" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="REF_V04" prompt="TRE_BANK_CREDIT_CONTRACT.REF_V04"/>
        <bm:field name="ref_v05" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="REF_V05" prompt="TRE_BANK_CREDIT_CONTRACT.REF_V05"/>
        <bm:field name="ref_n01" databaseType="NUMBER" datatype="java.lang.Long" physicalName="REF_N01" prompt="TRE_BANK_CREDIT_CONTRACT.REF_N01"/>
        <bm:field name="ref_n02" databaseType="NUMBER" datatype="java.lang.Long" physicalName="REF_N02" prompt="TRE_BANK_CREDIT_CONTRACT.REF_N02"/>
        <bm:field name="ref_n03" databaseType="NUMBER" datatype="java.lang.Long" physicalName="REF_N03" prompt="TRE_BANK_CREDIT_CONTRACT.REF_N03"/>
        <bm:field name="ref_n04" databaseType="NUMBER" datatype="java.lang.Long" physicalName="REF_N04" prompt="TRE_BANK_CREDIT_CONTRACT.REF_N04"/>
        <bm:field name="ref_n05" databaseType="NUMBER" datatype="java.lang.Long" physicalName="REF_N05" prompt="TRE_BANK_CREDIT_CONTRACT.REF_N05"/>
        <bm:field name="ref_d01" databaseType="DATE" datatype="java.util.Date" physicalName="REF_D01" prompt="TRE_BANK_CREDIT_CONTRACT.REF_D01"/>
        <bm:field name="ref_d02" databaseType="DATE" datatype="java.util.Date" physicalName="REF_D02" prompt="TRE_BANK_CREDIT_CONTRACT.REF_D02"/>
        <bm:field name="ref_d03" databaseType="DATE" datatype="java.util.Date" physicalName="REF_D03" prompt="TRE_BANK_CREDIT_CONTRACT.REF_D03"/>
        <bm:field name="ref_d04" databaseType="DATE" datatype="java.util.Date" physicalName="REF_D04" prompt="TRE_BANK_CREDIT_CONTRACT.REF_D04"/>
        <bm:field name="ref_d05" databaseType="DATE" datatype="java.util.Date" physicalName="REF_D05" prompt="TRE_BANK_CREDIT_CONTRACT.REF_D05"/> -->
        <!-- <bm:field name="bank_id" forInsert="false" forUpdate="false"/>
        <bm:field name="bank_full_name" forInsert="false" forUpdate="false"/> -->
        <bm:field name="credit_class_desc" forInsert="false" forUpdate="false"/>
        <bm:field name="credit_limit_type_desc" forInsert="false" forUpdate="false"/>
        <bm:field name="credit_limit_category_desc" forInsert="false" forUpdate="false"/>
        <bm:field name="bp_id_desc" forInsert="false" forUpdate="false"/>
        <bm:field name="loan_amount" forInsert="false" forUpdate="false"/>
        <bm:field name="repayment_principal" forInsert="false" forUpdate="false"/>
    </bm:fields>
    <bm:operations>
        <bm:operation name="query">
            <bm:query-sql><![CDATA[
                SELECT
                    *
                FROM
                    (SELECT
                        t1.credit_contract_id,
                        t1.credit_class,
                        (SELECT
                            scv.code_value_name
                        FROM
                            sys_code_values_v scv
                        WHERE
                            scv.code       = 'CREDIT_CONTRACT_CREDIT_CLASS' AND
                            scv.code_value = t1.credit_class
                        ) credit_class_desc,
                        t1.credit_limit_type,
                        (SELECT
                            scv.code_value_name
                        FROM
                            sys_code_values_v scv
                        WHERE
                            scv.code       = 'TRE501_CREDIT_LIMIT_TYPE' AND
                            scv.code_value = t1.credit_limit_type
                        ) credit_limit_type_desc,
                        t1.bp_id,
                        (SELECT h.bp_name FROM hls_bp_master h WHERE h.bp_id = t1.bp_id
                        ) bp_id_desc,
                        t1.credit_contract_number,
                        t1.credit_amount,
                        --tre_bank_credit_contract_pkg.get_available_amount(t1.credit_contract_id) AS available_amount,
                        t1.credit_amount -
                        (SELECT
                            nvl(SUM(l.loan_amount),0)
                        FROM
                            tre_loan_contract_v l
                        WHERE
                            l.loan_con_bp          = t1.bp_id AND
                            l.loan_contract_status = 'COMPLETED' and
                            l.bank_credit_contract_id = t1.credit_contract_id
                        ) available_amount,
                        t1.currency,
                        t1.credit_date_from,
                        t1.credit_date_to,
                        t1.notes,
                        t1.credit_limit_category,
                        t1.business_term,
                        t1.purpose,
                        t1.director_resolution,
                        (SELECT
                            scv.code_value_name
                        FROM
                            sys_code_values_v scv
                        WHERE
                            scv.code       = 'CREDIT_CONTRACT_CREDIT_CATEGORY' AND
                            scv.code_value = t1.credit_limit_category
                        ) credit_limit_category_desc,
                        (SELECT
                            SUM(l.loan_amount)
                        FROM
                            tre_loan_contract_v l
                        WHERE
                            l.loan_con_bp= t1.bp_id
                        ) loan_amount,
                        (SELECT
                            SUM(l.repayment_principal)
                        FROM
                            tre_loan_contract_v l
                        WHERE
                            l.loan_con_bp= t1.bp_id
                        ) repayment_principal
                    FROM
                        tre_bank_credit_contract t1
                    ORDER BY
                        t1.credit_contract_id DESC
                    ) t #WHERE_CLAUSE#
            ]]></bm:query-sql>
        </bm:operation>
    </bm:operations>
    <bm:features>
        <ns2:sequence-pk/>
        <f:standard-who/>
    </bm:features>
    <bm:primary-key>
        <bm:pk-field name="credit_contract_id"/>
    </bm:primary-key>
    <bm:query-fields>
        <bm:query-field field="credit_class" queryOperator="="/>
        <bm:query-field field="credit_limit_type" queryOperator="="/>
        <bm:query-field field="bp_id" queryOperator="="/>
        <bm:query-field field="credit_contract_number" queryOperator="like"/>
        <bm:query-field field="currency" queryOperator="="/>
        <!-- <bm:query-field field="bank_id" queryOperator="="/> -->
        <bm:query-field field="credit_date_from" queryExpression="credit_date_from &gt;= ${@credit_date_from}"/>
        <bm:query-field field="credit_date_to" queryExpression="credit_date_to &lt;= ${@credit_date_to}"/>
    </bm:query-fields>
</bm:model>
