<?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
                    DECODE(tmp.ln_count, 1, tmp.contract_name, tmp.contract_name
                    || '等') contract_name,
                    DECODE(tmp.ln_count, 1, tmp.employee_name, tmp.employee_name
                    || '等') employee_name,
                    DECODE(tmp.ln_count, 1, tmp.lease_organization_n, tmp.lease_organization_n
                    || '等') lease_organization_n,
                    DECODE(tmp.ln_count, 1, tmp.lease_organization_n, tmp.lease_organization_n
                    || '等') lease_organization_n,
                    TO_CHAR(tmp.req_date,'yyyy-mm-dd') req_date,
                    DECODE(tmp.ln_count, 1, tmp.bp_name, tmp.bp_name
                    || '等') bp_name,
                    DECODE(tmp.ln_count, 1, tmp.cf_type_name, tmp.cf_type_name
                    || '等') cf_type_name,
                    DECODE(tmp.ln_count, 1, tmp.cf_item_name, tmp.cf_item_name
                    || '等') cf_item_name,
                    TO_CHAR(tmp.sum_payment_amount, 'fm99,999,999,990.00') sum_payment_amount,
                    TO_CHAR(tmp.currrent_payamount, 'fm99,999,999,990.00') currrent_payamount,
                    DECODE(tmp.ln_count, 1, TO_CHAR(tmp.due_date, 'yyyy-mm-dd'), TO_CHAR(tmp.due_date, 'yyyy-mm-dd')
                    || '等') due_date,
                    DECODE(tmp.ln_count, 1, TO_CHAR(tmp.apply_pay_date, 'yyyy-mm-dd'), TO_CHAR(tmp.apply_pay_date, 'yyyy-mm-dd')
                    || '等') apply_pay_date
                FROM
                    (SELECT
                        c.contract_name,
                        c.employee_name,
                        c.lease_organization_n,
                        cph.req_date,
                        (SELECT m.bp_name FROM hls_bp_master m WHERE m.bp_id = cpl.bp_id
                        ) bp_name,
                        f.cf_type_name,
                        f.cf_item_name,
                        by_hls_wx_core_pkg.sum_payment_cf_amount(cph.payment_req_id) sum_payment_amount,
                        (SELECT
                            NVL(SUM(rl.amount), 0)
                        FROM
                            csh_payment_req_ln rl
                        WHERE
                            rl.payment_req_id = cph.payment_req_id
                        ) currrent_payamount,
                        f.due_date,
                        cpl.apply_pay_date,
                        (SELECT
                            COUNT(*)
                        FROM
                            csh_payment_req_ln rl
                        WHERE
                            rl.payment_req_id = cph.payment_req_id
                        ) ln_count
                    FROM
                        csh_payment_req_hd cph,
                        csh_payment_req_ln cpl,
                        con_contract_cashflow_v f,
                        con_contract_v c
                    WHERE
                        cph.payment_req_id    = cpl.payment_req_id AND
                        cpl.ref_doc_line_id   = f.cashflow_id AND
                        cpl.ref_doc_id        = f.contract_id AND
                        f.contract_id         = c.contract_id AND
                        cph.document_category = ${@document_category} AND
                        cph.payment_req_id    = ${@document_id}
                    ORDER BY
                        cpl.payment_req_ln_id
                    ) tmp
                WHERE
                    rownum = 1
            ]]></bm:query-sql>
        </bm:operation>
    </bm:operations>
</bm:model>
