<?xml version="1.0" encoding="UTF-8"?>
<bm:model xmlns:bm="http://www.leaf-framework.org/schema/bm" xmlns:s="leaf.plugin.script">
    <bm:operations>
        <bm:operation name="query">
            <bm:query-sql><![CDATA[
                    select * from
                    (SELECT
                            t.contract_id,
                            t.company_id,
                            t.business_type,
                            t.contract_number,
                            t.contract_name,
                            t.document_type,
                            dt.document_type_desc,
                            t.document_category,
                            dt.document_category_desc,
                            t.bp_id_tenant,
                            t.lease_execution_date,
                            (SELECT
                            scv.code_value_name
                            FROM
                            sys_code_values_v scv,
                            con_contract_lease_item ci,
                            hls_product_plan_definition hd
                            WHERE
                            scv.code                    = 'DISCOUNT_METHOD' AND
                            scv.code_value_enabled_flag = 'Y' AND
                            scv.code_value              = hd.discount_method AND
                            hd.product_plan_id          = ci.product_plan_id AND
                            ci.contract_id              = t.contract_id
                            ) discount_method_n,
                            b.bp_name,
                            b.bp_code,
                            --t.license_number,
                            TO_CHAR(t.lease_end_date, 'yyyy-mm-dd') lease_end_date,
                            (SELECT h.bp_name FROM hls_bp_master h WHERE h.bp_id = t.unit_id
                            ) AS unit_id_n,
                            t.unit_id,
                            TO_CHAR(t.inception_of_lease, 'yyyy-mm-dd') inception_of_lease,
                            t.contract_status,
                            (SELECT
                            v.code_value_name
                            FROM
                            sys_code_values_v v
                            WHERE
                            v.code       = 'CON500_CONTRACT_STATUS' AND
                            v.code_value = t.contract_status
                            ) AS status_desc,
                            (SELECT
                            DECODE(COUNT(*), 0, 'N', 'Y')
                            FROM
                            con_contract_cashflow f
                            WHERE
                            f.contract_id    = t.contract_id AND
                            f.overdue_status = 'Y'
                            ) overdue_status,
                            'ET' et_type,
                            '提前结清' et_type_dis,
                            t.early_termination_profile,
                            (SELECT
                            v.code_value_name
                            FROM
                            sys_code_values_v v
                            WHERE
                            v.code       = 'CON500_CONTRACT_STATUS' AND
                            v.code_value = t.contract_status
                            ) AS contract_status_n,
                            t.spv_company_id,
                            (SELECT
                            a.company_code
                            FROM
                            fnd_companies a
                            WHERE
                            a.company_id = t.spv_company_id
                            ) spv_company_code,
                            (SELECT
                            COUNT(*)
                            FROM
                            con_contract_cashflow h
                            WHERE
                            h.contract_id    = t.contract_id AND
                            h.write_off_flag = 'FULL' AND
                            h.cf_item        = 1 AND
                            h.times         != 0 AND
                            h.cf_direction = 'INFLOW'
                            ) received_times,
                            (SELECT su.description FROM sys_user su WHERE su.user_id = t.owner_user_id
                            ) owner,
                            t.lease_times,
                            TRIM((TO_CHAR(ROUND(t.int_rate_display, 4) * 100, '90.00')
                            || '%')) int_rate_display,
                            t.invoice_price,
                            t.finance_amount,
                            t.down_payment,
                            TRIM((TO_CHAR(ROUND(t.down_payment_ratio, 4) * 100, '90.00')
                            || '%')) down_payment_ratio,
                            t.deposit,
                            TRIM((TO_CHAR(ROUND(t.deposit_ratio, 4) * 100, '90.00')
                            || '%')) deposit_ratio,
                            t.lease_charge,
                            TRIM((TO_CHAR(ROUND(t.lease_charge_ratio, 4) * 100, '90.00')
                            || '%')) lease_charge_ratio,
                            (SELECT
                            TO_CHAR(cc.due_date, 'yyyy-mm-dd')
                            FROM
                            con_contract_cashflow cc
                            WHERE
                            cc.contract_id = t.contract_id AND
                            cc.times       = 1 AND
                            cc.cf_item     = 1
                            ) start_due_date,
                            t.bp_id_agent_level1,
                            (SELECT
                            hls.bp_name
                            FROM
                            hls_bp_master hls
                            WHERE
                            hls.bp_id = t.bp_id_agent_level1
                            ) bp_agent,
                            t.owner_user_id,
                            (SELECT
                            hbt.description
                            FROM
                            hls_business_type hbt
                            WHERE
                            hbt.enabled_flag  = 'Y' AND
                            hbt.business_type = t.business_type
                            ) business_type_n,
                            (SELECT
                            hlc.description
                            FROM
                            hls_lease_channel hlc
                            WHERE
                            hlc.lease_channel = t.lease_channel AND
                            hlc.enabled_flag  = 'Y'
                            ) lease_channel_n,
                            t.division,
                            -- add by lihh  20210323 租赁物
                            (SELECT  v.code_value_name
                            FROM sys_code_values_v v
                            where v.code = 'DIVISION'
                            and v.code_value = t.division)  division_n,
                            r.change_req_number,
                            r.change_req_id,
                            r.req_status,
                            (SELECT
                            v.code_value_name
                            FROM
                            sys_code_values_v v
                            WHERE
                            v.code       = 'CON_CHANGE_REQ_STATUS' AND
                            v.code_value = r.req_status
                            )req_status_n,
                            r.created_by,
                            (SELECT su.description FROM sys_user su WHERE su.user_id=r.created_by)req_employee_n,
                              to_char(r.termination_date,'yyyy-mm-dd')termination_date,
                              r.simulation_flag
                            FROM
                            con_contract t,
                            hls_bp_master b,
                        hls_document_type_v dt,con_contract_change_req r
                            WHERE

                            r.change_req_id = t.contract_id AND
                            r.et_flag='Y' AND
                            t.bp_id_tenant      = b.bp_id(+) AND
                            dt.document_type(+) = t.document_type
                            and nvl(r.simulation_flag,'N')='N'
                             order by DECODE(r.req_status,
                                                 'NEW',
                                                 '1',
                                                 'APPROVING',
                                                 '2',
                                                 'APPROVED',
                                                 '3',
                                                 'REJECT',
                                                 '4',
                                                 'CANCEL',
                                                 '5'),
                                          r.creation_date desc,
                                          r.change_req_number

                        ) t1 #WHERE_CLAUSE# #ORDER_BY_CLAUSE#
            ]]></bm:query-sql>
        </bm:operation>
    </bm:operations>
    <bm:query-fields>
        <bm:query-field name="contract_id" queryExpression="t1.contract_id = ${@contract_id}"/>
        <bm:query-field name="unit_id" queryExpression="t1.unit_id = ${@unit_id}"/>
        <bm:query-field name="date_from" datatype="java.lang.String" queryexpression="t1.inception_of_lease &gt;= ${@date_from}"/>
        <bm:query-field name="date_to" datatype="java.lang.String" queryexpression="t1.inception_of_lease &lt;= ${@date_to}"/>
        <bm:query-field name="lease_end_date_from" datatype="java.lang.String" queryexpression="t1.lease_end_date &gt;= ${@lease_end_date_from}"/>
        <bm:query-field name="lease_end_date_to" datatype="java.lang.String" queryexpression="t1.lease_end_date &lt;= ${@lease_end_date_to}"/>
        <bm:query-field name="change_req_number" datatype="java.lang.String" queryexpression="upper(t1.change_req_number) like &apos;%&apos; || upper(${@change_req_number}) || &apos;%&apos;"/>
        <bm:query-field name="contract_number" datatype="java.lang.String" queryexpression="upper(t1.contract_number) like &apos;%&apos; || upper(${@contract_number}) || &apos;%&apos;"/>
        <!-- <bm:query-field name="license_number" datatype="java.lang.String" queryexpression="upper(t1.license_number) like &apos;%&apos; || upper(${@license_number}) || &apos;%&apos;"/> -->
        <bm:query-field name="contract_name" datatype="java.lang.String" queryexpression="t1.contract_name like &apos;%&apos; || ${@contract_name} || &apos;%&apos;"/>
        <bm:query-field name="bp_id_tenant" queryExpression="t1.bp_id_tenant = ${@bp_id_tenant}"/>
        <bm:query-field name="bp_id_agent_level1" queryExpression="t1.bp_id_agent_level1 = ${@bp_id_agent_level1}"/>
        <bm:query-field name="bp_code" datatype="java.lang.String" queryexpression="t1.bp_code=${@bp_code}"/>
        <bm:query-field name="document_category" datatype="java.lang.String" queryexpression="t1.document_category=${@document_category}"/>
        <bm:query-field name="document_type" datatype="java.lang.String" queryexpression="t1.document_type=${@document_type}"/>
        <bm:query-field name="overdue_status" datatype="java.lang.String" queryexpression="t1.overdue_status=${@overdue_status}"/>
        <bm:query-field name="received_times" queryExpression="t1.received_times = ${@received_times}"/>
        <bm:query-field name="division" queryExpression="t1.division = ${@division}"/>
        <bm:query-field name="contract_status" queryExpression="t1.contract_status = ${@contract_status}"/>
        <bm:query-field name="lease_execution_date_from"  queryexpression="to_char(t1.lease_execution_date,'YYYY-MM-DD') &gt;= ${@lease_execution_date_from}"/>
        <bm:query-field name="termination_date_to" queryexpression="t1.termination_date&lt;= ${@termination_date_to}"/>
        <bm:query-field name="termination_date_from"  queryexpression="t1.termination_date &gt;= ${@termination_date_from}"/>
        <bm:query-field name="lease_execution_date_to" queryexpression="to_char(t1.lease_execution_date,'YYYY-MM-DD') &lt;= ${@lease_execution_date_to}"/>
        <bm:query-field name="req_employee_n" datatype="java.lang.String" queryexpression="t1.req_employee_n like &apos;%&apos; || ${@req_employee_n} || &apos;%&apos;"/>
        <bm:query-field name="req_status" queryExpression="t1.req_status = ${@req_status}"/>
    </bm:query-fields>
    <bm:features>
<!--    EXISTS-->
<!--    (SELECT-->
<!--    1-->
<!--    FROM-->
<!--    sys_user u-->
<!--    WHERE-->
<!--    u.bp_category = 'AGENT' AND-->
<!--    u.user_id     =${/session/@user_id}-->
<!--    ) AND-->
<!--    AND t.owner_user_id     = ${/session/@user_id}-->
        <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:model>
