<?xml version="1.0" encoding="UTF-8"?>
<!--
    $Author: Feng  
    $Date: 2013-9-27 上午10:27:56  
    $Revision: 1.0  
    $Purpose: 
-->
<bm:model xmlns:s="leaf.plugin.script" xmlns:bm="http://www.leaf-framework.org/schema/bm" xmlns:f="leaf.database.features" alias="t1" baseTable="CON_CONTRACT_V" defaultOrderBy="lease_end_date desc" needAccessControl="false">
    <bm:fields>
        <bm:field name="contract_id" databaseType="NUMBER" datatype="java.lang.Long" physicalName="CONTRACT_ID" prompt="CON_CONTRACT.CONTRACT_ID"/>
        <bm:field name="contract_number" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="CONTRACT_NUMBER" prompt="CON_CONTRACT.CONTRACT_NUMBER"/>
        <bm:field name="business_type" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="BUSINESS_TYPE" prompt="CON_CONTRACT.BUSINESS_TYPE"/>
        <bm:field name="document_type" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="DOCUMENT_TYPE" prompt="CON_CONTRACT.DOCUMENT_TYPE"/>
        <bm:field name="document_category" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="DOCUMENT_CATEGORY" prompt="CON_CONTRACT.DOCUMENT_CATEGORY"/>
        <bm:field name="company_id" databaseType="NUMBER" datatype="java.lang.Long" physicalName="COMPANY_ID" prompt="CON_CONTRACT.COMPANY_ID"/>
        <bm:field name="lease_organization" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="LEASE_ORGANIZATION" prompt="CON_CONTRACT.LEASE_ORGANIZATION"/>
        <bm:field name="lease_channel" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="LEASE_CHANNEL" prompt="CON_CONTRACT.LEASE_CHANNEL"/>
        <bm:field name="division" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="DIVISION" prompt="CON_CONTRACT.DIVISION"/>
        <bm:field name="bp_id_tenant" databaseType="NUMBER" datatype="java.lang.Long" physicalName="BP_ID_TENANT" prompt="CON_CONTRACT.BP_ID_TENANT"/>
        <bm:field name="bp_name" expression="(select bp_name from hls_bp_master where bp_id=t1.bp_id_tenant and enabled_flag=&apos;Y&apos;)" forInsert="false" forUpdate="false"/>
        <bm:field name="bp_id_agent_level1" databaseType="NUMBER" datatype="java.lang.Long" physicalName="BP_ID_AGENT_LEVEL1" prompt="CON_CONTRACT.BP_ID_AGENT_LEVEL1"/>
        <bm:field name="owner_user_id" databaseType="NUMBER" datatype="java.lang.Long" physicalName="OWNER_USER_ID" prompt="CON_CONTRACT.OWNER_USER_ID"/>
        <bm:field name="inception_of_lease" databaseType="DATE" datatype="java.util.Date" physicalName="INCEPTION_OF_LEASE" prompt="CON_CONTRACT.INCEPTION_OF_LEASE"/>
        <bm:field name="lease_end_date" databaseType="DATE" datatype="java.util.Date" physicalName="LEASE_END_DATE" prompt="CON_CONTRACT.LEASE_END_DATE"/>
        <bm:field name="et_date" databaseType="DATE" datatype="java.util.Date" physicalName="ET_DATE" prompt="CON_CONTRACT.ET_DATE"/>
        <bm:field name="lease_item_amount" databaseType="NUMBER" datatype="java.lang.Long" physicalName="LEASE_ITEM_AMOUNT" prompt="CON_CONTRACT.LEASE_ITEM_AMOUNT"/>
        <bm:field name="contract_status" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="CONTRACT_STATUS" prompt="CON_CONTRACT.CONTRACT_STATUS"/>
        <bm:field name="contract_status_desc" databaseType="VARCHAR2" datatype="java.lang.String"  prompt="CON_CONTRACT.CONTRACT_STATUS"/>
        <!--<bm:field name="signing_user_id" databaseType="NUMBER" datatype="java.lang.Long" physicalName="SIGNING_USER_ID" prompt="CON_CONTRACT.SIGNING_USER_ID"/>-->
        <bm:field name="signing_location" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="SIGNING_LOCATION" prompt="CON_CONTRACT.SIGNING_LOCATION"/>
        <bm:field name="residual_value" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="RESIDUAL_VALUE" prompt="CON_CONTRACT.RESIDUAL_VALUE"/>
        <bm:field name="signing_date" databaseType="DATE" datatype="java.util.Date" physicalName="SIGNING_DATE" prompt="CON_CONTRACT.SIGNING_DATE"/>
        <!--<bm:field name="bp_id_vender" databaseType="NUMBER" datatype="java.lang.Long" physicalName="BP_ID_VENDER" prompt="CON_CONTRACT.BP_ID_VENDER"/>-->
        <bm:field name="down_payment" databaseType="NUMBER" datatype="java.lang.Long" physicalName="DOWN_PAYMENT"/>
        <!--<bm:field name="signing_user_id_n" expression="(select name from exp_employees where employee_id = t1.signing_user_id)" forInsert="false" forUpdate="false"/>-->
        <bm:field name="lease_channel_desc" expression="(select description from hls_lease_channel where lease_channel = t1.lease_channel)" forInsert="false" forUpdate="false"/>
        <!--<bm:field name="bp_vender" expression="(select bp_name from hls_bp_master where bp_id=t1.bp_id_vender and enabled_flag=&apos;Y&apos;)" forInsert="false" forUpdate="false"/>-->
        <bm:field name="lease_organization_desc" expression="(select description from hls_lease_organization where lease_organization = t1.lease_organization)" forInsert="false" forUpdate="false"/>
        <!--<bm:field name="owner_user_id_n" expression="(select description from sys_user where user_id = t1.created_by)" forInsert="false" forUpdate="false"/>-->
        <bm:field name="is_terminate" expression="(CASE WHEN ((select sum(ccc.due_amount) from con_contract_cashflow ccc where ccc.contract_id = t1.contract_id AND ccc.cf_status&lt;&gt;&apos;BLOCK&apos; and ccc.cf_item&lt;&gt;0) - (select sum(ccc.received_amount) from con_contract_cashflow ccc where ccc.contract_id = t1.contract_id AND ccc.cf_status&lt;&gt;&apos;BLOCK&apos; and ccc.cf_item&lt;&gt;0)) = 0 THEN &apos;是&apos; ELSE &apos;否&apos; END)" forInsert="false" forUpdate="false"/>
        <bm:field name="amount_total" expression="(select sum(ccc.due_amount) from con_contract_cashflow ccc where ccc.contract_id=t1.contract_id and ccc.cf_status&lt;&gt;&apos;BLOCK&apos; and ccc.cf_item=1)" forInsert="false" forUpdate="false"/>
        <bm:field name="amount_principal" expression="(select sum(nvl(ccc.principal,0)) from con_contract_cashflow ccc where ccc.contract_id=t1.contract_id and ccc.cf_status&lt;&gt;&apos;BLOCK&apos;)" forInsert="false" forUpdate="false"/>
        <bm:field name="amount_interest" expression="(select sum(nvl(ccc.interest,0)) from con_contract_cashflow ccc where ccc.contract_id=t1.contract_id and ccc.cf_status&lt;&gt;&apos;BLOCK&apos; and cf_item=1)" forInsert="false" forUpdate="false"/>
        <bm:field name="amount_received_interest" expression="(select sum(nvl(ccc.received_interest,0)) from con_contract_cashflow ccc where ccc.contract_id=t1.contract_id and ccc.cf_status&lt;&gt;&apos;BLOCK&apos; and cf_item=1)" forInsert="false" forUpdate="false"/>
        <bm:field name="amount_received_penalty" expression="(nvl((select sum(nvl(ccc.received_amount,0)) from con_contract_cashflow ccc where ccc.contract_id=t1.contract_id and ccc.cf_status&lt;&gt;&apos;BLOCK&apos; and ccc.cf_item=9),0))" forInsert="false" forUpdate="false"/>
        <bm:field name="bp_id_agent_desc"/>
        <bm:field name="business_type_n"/>
        <bm:field name="division_n"/>
    </bm:fields>
    <bm:features>
        <f:standard-who/>
        <s:bm-script><![CDATA[
            var cx = Packages.leaf.javascript.Context.getCurrentContext();
            Packages.leaf.plugin.script.engine.ScriptImportor.defineExternScript(cx, this, $ctx.getData(), "aut_authority_bm_validate.js");
        ]]></s:bm-script>
    </bm:features>
    <bm:operations>
        <bm:operation name="update">
        <bm:update-sql><![CDATA[
                begin
                    con_contract_pkg.change_et_date(p_contract_id =>${@contract_id} ,
                                                    p_et_date =>${@et_date} ,
                                                    p_user_id => ${session/@user_id});

                 end;
          ]]></bm:update-sql>
        </bm:operation>
    </bm:operations>
    <bm:primary-key>
        <bm:pk-field name="contract_id"/>
    </bm:primary-key>
    <bm:query-fields>
<!--        <bm:query-field field="contract_number" queryExpression="contract_number like upper(&apos;%&apos;||${@contract_number}||&apos;%&apos;)"/>-->
        <bm:query-field field="owner_user_id" queryOperator="="/>
        <bm:query-field field="contract_number" queryOperator="="/>
        <bm:query-field field="bp_id_tenant" queryOperator="="/>
<!--        <bm:query-field field="lease_organization" queryOperator="="/>-->
        <bm:query-field field="contract_status" queryOperator="="/>
        <bm:query-field field="bp_id_agent_desc" queryExpression="bp_id_agent_desc like upper(&apos;%&apos;||${@bp_id_agent_desc}||&apos;%&apos;)"/>
    </bm:query-fields>
    <bm:data-filters>
        <bm:field name="is_terminate" expression="((t1.contract_status not in('REPURING') and (SELECT ccc.write_off_flag from con_contract_cashflow ccc WHERE ccc.contract_id=t1.contract_id AND ccc.cf_item=8 AND ccc.cf_status=&apos;RELEASE&apos;)=&apos;FULL&apos;)or(t1.contract_status='REPURING'))"/>
        <bm:data-filter enforceOperations="query" expression="t1.contract_status in (&apos;INCEPT&apos;,&apos;ETING&apos;,&apos;REPURING&apos;,&apos;TERMINATING&apos;) "/>
        <!--         <bm:data-filter enforceOperations="query" expression="t1.lease_end_date &lt;= sysdate"/> -->
        <bm:data-filter enforceOperations="query" expression="t1.data_class = &apos;NORMAL&apos;"/>
<!--        <bm:data-filter enforceOperations="query" expression=" not exists (select 1 from con_contract_cashflow ccw  where ccw.contract_id = t1.contract_id and ccw.cf_item=1 and ccw.cf_status=&apos;RELEASE&apos; and nvl(ccw.output_tax_create_je_flag,'N')='N')"/>-->
        <bm:data-filter enforceOperations="query" expression=" not exists (select 1 from con_contract_cashflow ccw , con_unearned_finance_income cf  where ccw.contract_id = t1.contract_id and ccw.cf_item=1 and cf.cashflow_id(+)=ccw.cashflow_id and ccw.cf_status=&apos;RELEASE&apos; and nvl(cf.create_je_flag,'N')='N'  and nvl(ccw.output_tax_create_je_flag,'N')='N')"/>
<!--        <bm:data-filter enforceOperations="query" expression="t1.contract_id = ${@contract_id}"/>-->
        <!--<bm:data-filter enforceOperations="query" expression=" NOT EXISTS (SELECT 1                 FROM con_contract_cashflow                WHERE contract_id = t1.contract_id                      AND cf_item IN (1,8,52)  AND (write_off_flag IN (&apos;PARTIAL&apos;, &apos;NOT&apos;) OR received_amount != due_amount))"/>-->
        <!--<bm:data-filter enforceOperations="query" expression="exists (select 1 from con_contract where contract_id = t1.contract_id and created_by = ${/session/@user_id})"/>-->
    </bm:data-filters>
</bm:model>
