<?xml version="1.0" encoding="UTF-8"?>
<bm:model xmlns:bm="http://www.leaf-framework.org/schema/bm" xmlns:s="leaf.plugin.script" needAccessControl="false">
    <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,
                           b.bp_name,
                           b.bp_code,
                           --t.license_number,
                           TO_CHAR(t.lease_start_date, 'yyyy-mm-dd') lease_start_date,
                           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,
                           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,
                           (Select su.description
                              From sys_user su
                             Where su.user_id = t.owner_user_id) owner,
                           t.lease_times,
                           t.invoice_price,
                           t.finance_amount,
                           t.down_payment,
                           t.deposit,
                           t.lease_charge,
                           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,
                           (Select t2.description
                              From hls_division t2
                             Where t2.enabled_flag = 'Y'
                               And t2.division = t.division) division_n
                      From con_contract t, hls_bp_master b, hls_document_type_v dt
                     Where t.bp_id_tenant = b.bp_id(+)
                       And t.contract_status = 'INCEPT'
                       And t.data_class = 'NORMAL'
                       And t.division != '95'
                       And dt.document_type(+) = t.document_type
                       And Exists
                     (Select 1
                              From csh_write_off cwo, gld_write_off gwo
                             Where cwo.contract_id = t.contract_id
                               And cwo.write_off_id = gwo.csh_write_off_id
                               And nvl(cwo.reversed_flag, 'N') = 'N'
                               And nvl(gwo.reversed_flag, 'N') = 'N'
                               And cwo.cf_item = 9
                               And gwo.cf_item = 1) ) 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="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="lease_execution_date_to" queryexpression="to_char(t1.lease_execution_date,'YYYY-MM-DD') &lt;= ${@lease_execution_date_to}"/>
    </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>
