<?xml version="1.0" encoding="UTF-8"?>
<!--
    $Author: qwm
    $Date: 2013-12-28 下午1:31:12
    $Revision: 1.0
    $Purpose:
-->
<bm:model xmlns:bm="http://www.leaf-framework.org/schema/bm" xmlns:f="leaf.database.features" alias="t1" baseTable="CON_UNEARNED_FINANCE_INCOME">
    <bm:fields>
        <bm:field name="seq" databaseType="NUMBER" datatype="java.lang.Long" expression="(row_number() over(order by t1.contract_id))"/>
        <bm:field name="record_id" databaseType="NUMBER" datatype="java.lang.Long" physicalName="RECORD_ID" prompt="CON_UNEARNED_FINANCE_INCOME.RECORD_ID"/>
        <bm:field name="company_id" databaseType="NUMBER" datatype="java.lang.Long" physicalName="COMPANY_ID" prompt="CON_UNEARNED_FINANCE_INCOME.COMPANY_ID"/>
        <bm:field name="contract_id" databaseType="NUMBER" datatype="java.lang.Long" physicalName="CONTRACT_ID" prompt="CON_UNEARNED_FINANCE_INCOME.CONTRACT_ID"/>
        <bm:field name="internal_period_num" databaseType="NUMBER" datatype="java.lang.Long" physicalName="INTERNAL_PERIOD_NUM" prompt="CON_UNEARNED_FINANCE_INCOME.INTERNAL_PERIOD_NUM"/>
        <bm:field name="cf_item" databaseType="NUMBER" datatype="java.lang.Long" physicalName="CF_ITEM" prompt="CON_UNEARNED_FINANCE_INCOME.CF_ITEM"/>
        <bm:field name="seq_in_period" databaseType="NUMBER" datatype="java.lang.Long" physicalName="SEQ_IN_PERIOD" prompt="CON_UNEARNED_FINANCE_INCOME.SEQ_IN_PERIOD"/>
        <bm:field name="unearned_finance_income" databaseType="NUMBER" datatype="java.lang.Double" physicalName="UNEARNED_FINANCE_INCOME" prompt="CON_UNEARNED_FINANCE_INCOME.UNEARNED_FINANCE_INCOME"/>
        <bm:field name="financing_cost" databaseType="NUMBER" datatype="java.lang.Long" physicalName="FINANCING_COST" prompt="CON_UNEARNED_FINANCE_INCOME.FINANCING_COST"/>
        <bm:field name="main_business_income" databaseType="NUMBER" datatype="java.lang.Long" physicalName="MAIN_BUSINESS_INCOME" prompt="CON_UNEARNED_FINANCE_INCOME.MAIN_BUSINESS_INCOME"/>
        <bm:field name="main_business_cost" databaseType="NUMBER" datatype="java.lang.Long" physicalName="MAIN_BUSINESS_COST" prompt="CON_UNEARNED_FINANCE_INCOME.MAIN_BUSINESS_COST"/>
        <bm:field name="unearned_finance_income_balanc" databaseType="NUMBER" datatype="java.lang.Long" physicalName="UNEARNED_FINANCE_INCOME_BALANC" prompt="CON_UNEARNED_FINANCE_INCOME.UNEARNED_FINANCE_INCOME_BALANC"/>
        <bm:field name="finance_income_recognized" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="FINANCE_INCOME_RECOGNIZED" prompt="CON_UNEARNED_FINANCE_INCOME.FINANCE_INCOME_RECOGNIZED"/>
        <bm:field name="weight_factor" databaseType="NUMBER" datatype="java.lang.Long" physicalName="WEIGHT_FACTOR" prompt="CON_UNEARNED_FINANCE_INCOME.WEIGHT_FACTOR"/>
        <bm:field name="total_weight" databaseType="NUMBER" datatype="java.lang.Long" physicalName="TOTAL_WEIGHT" prompt="CON_UNEARNED_FINANCE_INCOME.TOTAL_WEIGHT"/>
        <bm:field name="factor_days" databaseType="NUMBER" datatype="java.lang.Long" physicalName="FACTOR_DAYS" prompt="CON_UNEARNED_FINANCE_INCOME.FACTOR_DAYS"/>
        <bm:field name="factor_outstanding_principal" databaseType="NUMBER" datatype="java.lang.Long" physicalName="FACTOR_OUTSTANDING_PRINCIPAL" prompt="CON_UNEARNED_FINANCE_INCOME.FACTOR_OUTSTANDING_PRINCIPAL"/>
        <bm:field name="factor_interest_accrual_balanc" databaseType="NUMBER" datatype="java.lang.Long" physicalName="FACTOR_INTEREST_ACCRUAL_BALANC" prompt="CON_UNEARNED_FINANCE_INCOME.FACTOR_INTEREST_ACCRUAL_BALANC"/>
        <bm:field name="factor_reserved1" databaseType="NUMBER" datatype="java.lang.Long" physicalName="FACTOR_RESERVED1" prompt="CON_UNEARNED_FINANCE_INCOME.FACTOR_RESERVED1"/>
        <bm:field name="factor_reserved2" databaseType="NUMBER" datatype="java.lang.Long" physicalName="FACTOR_RESERVED2" prompt="CON_UNEARNED_FINANCE_INCOME.FACTOR_RESERVED2"/>
        <bm:field name="factor_reserved3" databaseType="NUMBER" datatype="java.lang.Long" physicalName="FACTOR_RESERVED3" prompt="CON_UNEARNED_FINANCE_INCOME.FACTOR_RESERVED3"/>
        <bm:field name="cashflow_id" databaseType="NUMBER" datatype="java.lang.Long" physicalName="CASHFLOW_ID" prompt="CON_UNEARNED_FINANCE_INCOME.CASHFLOW_ID"/>
        <bm:field name="times" databaseType="NUMBER" datatype="java.lang.Long" physicalName="TIMES" prompt="CON_UNEARNED_FINANCE_INCOME.TIMES"/>
        <bm:field name="cf_type" databaseType="NUMBER" datatype="java.lang.Long" physicalName="CF_TYPE" prompt="CON_UNEARNED_FINANCE_INCOME.CF_TYPE"/>
        <bm:field name="calc_start_date" databaseType="DATE" datatype="java.util.Date" physicalName="CALC_START_DATE" prompt="CON_UNEARNED_FINANCE_INCOME.CALC_START_DATE"/>
        <bm:field name="calc_end_date" databaseType="DATE" datatype="java.util.Date" physicalName="CALC_END_DATE" prompt="CON_UNEARNED_FINANCE_INCOME.CALC_END_DATE"/>
        <bm:field name="project_name" databaseType="VARCHAR2" datatype="java.lang.String" expression="(select p.project_name from prj_project p ,con_contract c where c.contract_id = t1.contract_id and p.project_id = c.project_id)" forInsert="false" forUpdate="false"/>
        <bm:field name="finance_amount" databaseType="NUMBER" datatype="java.lang.Double" expression="(select c.finance_amount from con_contract c where c.contract_id = t1.contract_id)" forInsert="false" forUpdate="false"/>
        <bm:field name="period" databaseType="VARCHAR2" datatype="java.lang.String" expression="((SELECT                                TO_CHAR(cf1.due_date + 1, &apos;yyyy/mm/dd&apos;)                            FROM                                con_contract_cashflow cf1                            WHERE                                cf1.contract_id = t1.contract_id AND                                cf1.cf_item     = t1.cf_item AND                                cf1.times       =                                (SELECT                                    cf.times - 1                                FROM                                    con_contract_cashflow cf                                WHERE                                    cf.cashflow_id = t1.cashflow_id                                ))                                || &apos;-&apos;                                ||                            (SELECT                                TO_CHAR(cf.due_date, &apos;yyyy/mm/dd&apos;)                            FROM                                con_contract_cashflow cf                            WHERE                                cf.cashflow_id = t1.cashflow_id)                            )" forInsert="false" forUpdate="false"/>
        <bm:field name="interest" databaseType="NUMBER" datatype="java.lang.Double" expression="(select c.interest from con_contract_cashflow c where c.cashflow_id = t1.cashflow_id)" forInsert="false" forUpdate="false"/>
        <bm:field name="int_rate_implicit" databaseType="NUMBER" datatype="java.lang.Double" expression="round((select c.int_rate from con_contract c where c.contract_id = t1.contract_id),4)" forInsert="false" forUpdate="false"/>
        <bm:field name="calc_period" databaseType="VARCHAR2" datatype="java.lang.String" expression="(to_char(t1.calc_start_date,&apos;yyyy/mm/dd&apos;)||&apos;-&apos;||to_char(t1.calc_end_date,&apos;yyyy/mm/dd&apos;))" forInsert="false" forUpdate="false"/>
        <bm:field name="calc_date_count" databaseType="NUMBER" datatype="java.lang.Long" expression="(t1.calc_end_date-t1.calc_start_date+1)" forInsert="false" forUpdate="false"/>
        <bm:field name="vat_unearned_finance_income" databaseType="NUMBER" datatype="java.lang.Double" expression="(t1.unearned_finance_income*(select decode(t.tax_type_id,1,1,(select c.tax_type_rate   from fnd_tax_type_codes c  where c.tax_type_id = t.tax_type_id))  from con_contract t           where t.contract_id = t1.contract_id))" forInsert="false" forUpdate="false"/>
        <bm:field name="current_month_vat_income" databaseType="NUMBER" datatype="java.lang.Double" expression="ROUND((select sum(t.unearned_finance_income*(select decode(t.tax_type_id,                        1,                        1,                        1+(select c.tax_type_rate                           from fnd_tax_type_codes c                          where c.tax_type_id = t.tax_type_id))            from con_contract t           where t.contract_id = t1.contract_id)) from con_unearned_finance_income t where t.contract_id =t1.contract_id and t.internal_period_num =t1.internal_period_num),(select c.PRECISION from gld_currency c,con_contract ct where c.currency_code = ct.currency and ct.contract_id = t1.contract_id))" forInsert="false" forUpdate="false"/>
        <bm:field name="current_month_income" databaseType="NUMBER" datatype="java.lang.Double" expression="(select sum(t.unearned_finance_income) from con_unearned_finance_income t where t.contract_id =t1.contract_id and t.internal_period_num =t1.internal_period_num)" forInsert="false" forUpdate="false"/>
        <bm:field name="contract_number" databaseType="VARCHAR2" datatype="java.lang.String" expression="(select t.contract_number from con_contract t where t.contract_id =t1.contract_id )" forInsert="false" forUpdate="false"/>
        <bm:field name="contract_name" databaseType="VARCHAR2" datatype="java.lang.String" expression="(select t.contract_name from con_contract t where t.contract_id =t1.contract_id )" forInsert="false" forUpdate="false"/>
        <bm:field name="cf_item_desc" databaseType="VARCHAR2" datatype="java.lang.String" expression="(select t.description from hls_cashflow_item t where t.cf_item =t1.cf_item )" forInsert="false" forUpdate="false"/>
        <bm:field name="loan_contract_id" databaseType="NUMBER" datatype="java.lang.Long" />
        <bm:field name="loan_contract_number" databaseType="VARCHAR2" datatype="java.lang.String" expression="(select t.loan_contract_number from tre_loan_contract t,tre_loan_con_withdrawal_plan p where p.widthdrawal_plan_id =t1.contract_id and p.loan_contract_id = t.loan_contract_id)" forInsert="false" forUpdate="false"/>
        <bm:field name="loan_amount" databaseType="VARCHAR2" datatype="java.lang.String" expression="(select p.amount from tre_loan_con_withdrawal_plan p where p.widthdrawal_plan_id =t1.contract_id )" forInsert="false" forUpdate="false"/>

        <bm:field name="full_received" databaseType="VARCHAR2" datatype="java.lang.String" expression="(select CASE WHEN nvl(ccc.write_off_flag, &apos;NOT&apos;) = &apos;FULL&apos; THEN &apos;Y&apos; ELSE &apos;N&apos; END  from con_contract_cashflow ccc where ccc.cashflow_id = t1.cashflow_id)" forInsert="false" forUpdate="false"/>

        <bm:field name="unearned_finance_income_all" databaseType="NUMBER" datatype="java.lang.Double" expression="((t1.unearned_finance_income*(select decode(t.tax_type_id,1,1,(select c.tax_type_rate   from fnd_tax_type_codes c  where c.tax_type_id = t.tax_type_id))  from con_contract t           where t.contract_id = t1.contract_id))+t1.unearned_finance_income)" forInsert="false" forUpdate="false"/>

        <bm:field name="contract_overdue_90" databaseType="VARCHAR2" datatype="java.lang.String" expression="&apos;Y&apos;" forInsert="false" forUpdate="false"/>

        <bm:field name="rental_overdue_90" databaseType="VARCHAR2" datatype="java.lang.String" expression="&apos;Y&apos;" forInsert="false" forUpdate="false"/>

        <bm:field name="overdue_days" databaseType="VARCHAR2" datatype="java.lang.String" expression="
        nvl((SELECT (add_months(to_date(${@internal_period_num},'yyyymm'),1)-1)- ccc.due_date
                 FROM con_contract_cashflow ccc
                WHERE ccc.cf_item IN (1, 200)
                  AND ccc.due_amount - nvl(ccc.received_amount, 0) > 0
                  AND ccc.cashflow_id = t1.cashflow_id),0)" forInsert="false" forUpdate="false"/>


        <!--<bm:field name="rental_overdue_90" databaseType="VARCHAR2" datatype="java.lang.String" expression="(DECODE((SELECT COUNT(*) FROM con_contract_cashflow ccc WHERE ccc.cf_item IN (1, 200) AND nvl(ccc.received_amount, 0) &lt; ccc.due_amount AND ccc.due_date + 90 &lt;= trunc(SYSDATE) AND ccc.cashflow_id = t1.cashflow_id),0,&apos;N&apos;,&apos;Y&apos;))" forInsert="false" forUpdate="false"/>-->


        <bm:field name="tre_unearned_finance_income" databaseType="NUMBER" datatype="java.lang.Double" expression="(round(t1.unearned_finance_income * 1.16,2))" forInsert="false" forUpdate="false"/>
        <bm:field name="lease_execution_date" databaseType="DATE" datatype="java.util.Date" expression="(select c.lease_execution_date from con_contract c where c.contract_id = t1.contract_id)" forInsert="false" forUpdate="false"/>
        <bm:field name="lease_end_date" databaseType="DATE" datatype="java.util.Date" expression="(select c.lease_end_date from con_contract c where c.contract_id = t1.contract_id)" forInsert="false" forUpdate="false"/>
        <bm:field name="business_type_n" databaseType="VARCHAR2" datatype="java.lang.String" expression="(SELECT hbt.description FROM con_contract c,hls_business_type hbt WHERE c.contract_id = t1.contract_id and hbt.business_type = c.business_type)" forInsert="false" forUpdate="false"/>

        <bm:field name="principal" databaseType="NUMBER" datatype="java.lang.Double" expression="(SELECT ccc.principal FROM con_contract_cashflow ccc WHERE ccc.cashflow_id = t1.cashflow_id)" forInsert="false" forUpdate="false"/>
        <bm:field name="principal_15" databaseType="NUMBER" datatype="java.lang.Double" expression="(SELECT ccc.principal FROM con_contract_cashflow ccc WHERE ccc.cashflow_id = t1.cashflow_id) * 0.15 " forInsert="false" forUpdate="false"/>
        <bm:field name="net_principal" databaseType="NUMBER" datatype="java.lang.Double" expression="(SELECT ccc.net_principal FROM con_contract_cashflow ccc WHERE ccc.cashflow_id = t1.cashflow_id)" forInsert="false" forUpdate="false"/>
        <bm:field name="net_principal_15" databaseType="NUMBER" datatype="java.lang.Double" expression="(SELECT ccc.net_principal FROM con_contract_cashflow ccc WHERE ccc.cashflow_id = t1.cashflow_id) * 0.15" forInsert="false" forUpdate="false"/>

    </bm:fields>
    <bm:features>
        <f:standard-who/>
    </bm:features>
    <bm:primary-key>
        <bm:pk-field name="record_id"/>
    </bm:primary-key>
    <bm:query-fields>
        <bm:query-field field="company_id" queryOperator="="/>
        <bm:query-field field="contract_number" queryExpression="(select t.contract_number from con_contract t where t.contract_id =t1.contract_id ) = ${@contract_number}"/>
        <bm:query-field field="rental_overdue_90" queryExpression="
        (decode((SELECT COUNT(1)
                  FROM con_contract_cashflow ccc
                 WHERE ccc.cf_item IN (1, 200)
                   AND ccc.due_amount - nvl(ccc.received_amount, 0) &gt; 0
                   AND ccc.due_date &lt;=
                       (SELECT MIN(f.due_date)
                          FROM con_contract_cashflow f
                         WHERE f.contract_id = t1.contract_id
                           AND f.cf_item IN (1, 200)
                           AND f.due_amount - nvl(ccc.received_amount, 0) &gt; 0) + 90
                           AND ccc.cashflow_id = t1.cashflow_id),0,&apos;N&apos;,&apos;Y&apos;)) =${@rental_overdue_90}"/>
        <bm:query-field field="contract_overdue_90" queryExpression="
         ((DECODE((SELECT COUNT(*)
                 FROM con_contract_cashflow ccc
                WHERE ccc.cf_item IN (1, 200)
                  AND ccc.due_amount - nvl(ccc.received_amount, 0)  &gt;= 0
                  AND  (add_months(to_date(${@internal_period_num},'yyyymm'),1)-1)- ccc.due_date BETWEEN 0 AND 89
                  AND ccc.cashflow_id = t1.cashflow_id),
               0,
               &apos;N&apos;,
               &apos;Y&apos;)))= ${@contract_overdue_90}"/>
        <bm:query-field field="loan_contract_number" queryExpression="(select t.loan_contract_number from tre_loan_contract t,tre_loan_con_withdrawal_plan p where p.widthdrawal_plan_id =t1.contract_id and p.loan_contract_id = t.loan_contract_id) = ${@loan_contract_numbers}"/>
        <bm:query-field name="buession_type" queryExpression=" ((${@buession_type} = 'LEASE'
        AND t1.cf_item in (1,3) and t1.internal_period_num &lt;= ${@internal_period_num}
        AND (select cc.data_class from con_contract cc where cc.contract_id = t1.contract_id) = 'NORMAL'
        ) OR (${@buession_type} = 'LOAN'
            AND (
            (t1.cf_item= 900
            and  t1.internal_period_num = ${@internal_period_num}
            and  not EXISTS (SELECT 1 FROM tre_loan_con_repayment_plan p WHERE EXISTS (SELECT 1 FROM tre_loan_con_repayment_dtl d
                     WHERE d.repayment_plan_id = p.repayment_plan_id AND d.repayment_amount = p.rental )
            AND p.repayment_plan_id = t1.cashflow_id)) or (t1.cf_item= 901
            and  t1.internal_period_num = ${@internal_period_num} )
        )))"/>
    </bm:query-fields>
    <bm:data-filters>
        <bm:data-filter enforceOperations="query" expression="t1.cf_item = ${@cf_item} "/>
    </bm:data-filters>
</bm:model>
