<?xml version="1.0" encoding="UTF-8"?>
<bm:model xmlns:bm="http://www.leaf-framework.org/schema/bm">
    <bm:operations>
        <bm:operation name="query">
            <bm:query-sql><![CDATA[
            select * from
            (
                SELECT
                    ap.abs_id,
                    ap.abs_number,
                    ap.abs_name,
                    arp.repayment_plan_id,
                    arp.abs_project_times,
                    NVL(
                    (SELECT
                        t.repayment_date
                    FROM
                        hn_abs_repayment_plan t
                    WHERE
                        t.abs_id            = ap.abs_id AND
                        t.abs_project_times = arp.abs_project_times - 1
                    )+1, ap.start_date) start_date,
                    --arp.repayment_date end_date,
                    ap.calc_date end_date,
                    arp.repayment_principal + arp.repayment_interest repayment_amount,
                    arp.repayment_principal + arp.repayment_interest -
                    (SELECT
                        SUM(hn_abs_project_pkg.get_receive_amount_1(p_contract_id => ac.contract_id, p_start_date => NVL(
                        (SELECT
                            t.repayment_date
                        FROM
                            hn_abs_repayment_plan t
                        WHERE
                            t.abs_id            = ap.abs_id AND
                            t.abs_project_times = arp.abs_project_times - 1
                        ), ap.start_date), p_calc_date => ap.calc_date))
                    FROM
                        hn_abs_contract ac
                    WHERE
                        ac.abs_id = ap.abs_id
                    ) due_amount
                FROM
                    hn_abs_project_register ap,
                    hn_abs_repayment_plan arp
                WHERE
                    ap.abs_id = arp.abs_id
                ORDER BY
                    ap.abs_number,
                    arp.abs_project_times
                    ) t1
                    #WHERE_CLAUSE#
            ]]></bm:query-sql>
        </bm:operation>
    </bm:operations>
    <!-- <bm:data-filters>
    	<bm:data-filter enforceOperations="query" expression="t1.end_date &gt;t1.start_date"/>
    </bm:data-filters> -->
    <bm:query-fields>
        <bm:query-field name="abs_number" queryExpression="t1.abs_number like &apos;%&apos;||${@abs_number}||&apos;%&apos;"/>
        <bm:query-field name="abs_name" queryExpression="t1.abs_name like &apos;%&apos;||${@abs_name}||&apos;%&apos;"/>
        <bm:query-field name="abs_project_times_from" queryExpression="t1.abs_project_times &lt;=${@abs_project_times_from}"/>
        <bm:query-field name="abs_project_times_to" queryExpression="t1.abs_project_times &gt;=${@abs_project_times_to}"/>
        <bm:query-field name="start_date_from" queryExpression="t1.start_date &lt;=to_date(${@start_date_from},&apos;YYYY-MM-DD&apos;)"/>
        <bm:query-field name="start_date_to" queryExpression="t1.start_date &gt;=to_date(${@start_date_to},&apos;YYYY-MM-DD&apos;)"/>
        <bm:query-field name="end_date_from" queryExpression="t1.end_date &lt;=to_date(${@end_date_from},&apos;YYYY-MM-DD&apos;)"/>
        <bm:query-field name="end_date_to" queryExpression="t1.end_date &gt;=to_date(${@end_date_to},&apos;YYYY-MM-DD&apos;)"/>
    </bm:query-fields>
</bm:model>
