<?xml version="1.0" encoding="UTF-8"?>
<bm:model xmlns:s="leaf.plugin.script" xmlns:bm="http://www.leaf-framework.org/schema/bm">
    <bm:operations>
        <bm:operation name="query">
            <bm:query-sql><![CDATA[
                SELECT
                    *
                FROM
                    (SELECT
                        pp.project_id,
                        pp.virtual_con_number document_number,
                        pp.virtual_con_name document_name,
                        'CONTRACT' document_category,
                        '租赁合同' document_category_desc,
                        pp.finance_amount,
                        pp.inception_of_lease,
                        pp.bp_id_tenant,
                        pp.bp_id_tenant_n bp_name,
                        (SELECT
                            NVL(SUM(tfr.reserve_amount), 0)
                        FROM
                            tre_funds_reservation tfr
                        WHERE
                            tfr.project_id = pp.project_id AND
                            tfr.status     = 'RESERVED'
                        ) reserve_amount
                    FROM
                        prj_project_virtual_con_lv pp
                    WHERE
                        pp.content_finish_flag = 'Y' AND
                        pp.content_wfl_status  = 'APPROVED'
                    UNION
                    SELECT
                        pp.project_id,
                        pp.project_number document_number,
                        pp.project_name document_name,
                        'PROJECT' document_category,
                        '项目' document_category_desc,
                        pp.finance_amount,
                        pp.inception_of_lease,
                        pp.bp_id_tenant,
                        pp.bp_name,
                        (SELECT
                            NVL(SUM(tfr.reserve_amount), 0)
                        FROM
                            tre_funds_reservation tfr
                        WHERE
                            tfr.project_id = pp.project_id AND
                            tfr.status     = 'RESERVED'
                        ) reserve_amount
                    FROM
                        prj_project_lv pp
                    WHERE
                        pp.data_class = 'NORMAL' AND
                        NOT EXISTS
                        (SELECT 1 FROM prj_project pp1 WHERE pp1.ref_project_id = pp.project_id
                        )
                    ) t1 #WHERE_CLAUSE#
                ORDER BY
                    document_number DESC
            ]]></bm:query-sql>
        </bm:operation>
    </bm:operations>
    <bm:features>
        <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:query-fields>
        <bm:query-field name="document_number" queryExpression="upper(t1.document_number) like &apos;%&apos; || upper(${@document_number}) || &apos;%&apos;"/>
        <bm:query-field name="document_name" queryExpression="upper(t1.document_name) like &apos;%&apos; || upper(${@document_name}) || &apos;%&apos;"/>
        <bm:query-field name="bp_name" queryExpression="upper(t1.bp_name) like &apos;%&apos; || upper(${@bp_name}) || &apos;%&apos;"/>
        <bm:query-field name="contract_status" queryExpression="t1.contract_status = ${@contract_status}"/>
        <bm:query-field name="reserve_status" queryExpression="t1.reservation_status=${@reserve_status}"/>
        <bm:query-field name="inception_of_lease_from" queryExpression="t1.inception_of_lease &gt;=to_date(${@inception_of_lease_from},&apos;yyyy-mm-dd&apos;)"/>
        <bm:query-field name="inception_of_lease_to" queryExpression="t1.inception_of_lease &lt;=to_date(${@inception_of_lease_to},&apos;yyyy-mm-dd&apos;)"/>
        <bm:query-field name="lease_start_date_from" queryExpression="t1.lease_start_date &gt;=to_date(${@lease_start_date_from},&apos;yyyy-mm-dd&apos;)"/>
        <bm:query-field name="lease_start_date_to" queryExpression="t1.lease_start_date &lt;=to_date(${@lease_start_date_to},&apos;yyyy-mm-dd&apos;)"/>
        <bm:query-field name="reserve_amount_from" queryExpression="t1.reserve_amount &gt;=${@reserve_amount_from}"/>
        <bm:query-field name="reserve_amount_to" queryExpression="t1.reserve_amount &lt;=${@reserve_amount_to}"/>
        <bm:query-field name="write_off_amount_from" queryExpression="t1.write_off_amount &gt;=${@write_off_amount_from}"/>
        <bm:query-field name="write_off_amount_to" queryExpression="t1.write_off_amount &lt;=${@write_off_amount_to}"/>
        <bm:query-field name="bp_code" queryExpression="bp_code=${@bp_code}"/>
        <bm:query-field name="finance_amount_from" queryExpression="t1.finance_amount &gt;=${@finance_amount_from}"/>
        <bm:query-field name="finance_amount_to" queryExpression="t1.finance_amount &lt;=${@finance_amount_to}"/>
        <bm:query-field name="document_category_desc" queryExpression="t1.document_category=${@document_category}"/>
    </bm:query-fields>
</bm:model>
