<?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
                    t1.*
                FROM
                    (SELECT
                        hd.bp_credit_hd_id,
                        hd.bp_id,
                        bp.bp_code,
                        bp.bp_name,
                        bp.bp_type,
                        bp.bp_category,
                        (SELECT
                            v.code_value_name
                        FROM
                            sys_code_values_v v
                        WHERE
                            v.code              = 'BP_CATEGORY' AND
                            v.code_enabled_flag = 'Y' AND
                            v.code_value        = bp.bp_category
                        ) bp_category_n,
                        (SELECT
                            SUM(NVL(l.credit_total_amount, 0))
                        FROM
                            hls_bp_master_credit_ln l
                        WHERE
                            l.bp_credit_hd_id          = hd.bp_credit_hd_id AND
                            TRUNC(l.credit_date_from) <= Sysdate AND
                            l.credit_date_to          >= TRUNC(Sysdate) AND
                            l.enable_flag              = 'Y'
                        ) credit_total_amount,
                        (SELECT
                            SUM(NVL(l.open_amount, 0))
                        FROM
                            hls_bp_master_credit_ln l
                        WHERE
                            l.bp_credit_hd_id          = hd.bp_credit_hd_id AND
                            TRUNC(l.credit_date_from) <= Sysdate AND
                            l.credit_date_to          >= TRUNC(Sysdate) AND
                            l.enable_flag              = 'Y'
                        ) open_amount,
                        hls_bp_master_credit_pkg.calc_used_amount_bp(p_bp_credit_hd_id => hd.bp_credit_hd_id,p_user_id => ${/session/@user_id}) AS used_amount,
                        (
                        (SELECT
                            SUM(NVL(l.open_amount, 0))
                        FROM
                            hls_bp_master_credit_ln l
                        WHERE
                            l.bp_credit_hd_id          = hd.bp_credit_hd_id AND
                            TRUNC(l.credit_date_from) <= Sysdate AND
                            l.credit_date_to          >= TRUNC(Sysdate) AND
                            l.enable_flag              = 'Y'
                        ) -(hls_bp_master_credit_pkg.calc_used_amount_bp(p_bp_credit_hd_id => hd.bp_credit_hd_id,p_user_id => ${/session/@user_id}))) last_amount
                    FROM
                        hls_bp_master_credit_hd hd,
                        hls_bp_master bp
                    WHERE
                        hd.bp_id        = bp.bp_id AND
                        bp.enabled_flag = 'Y'
                    ) t1 #WHERE_CLAUSE#
            ]]></bm:query-sql>
        </bm:operation>
        <bm:operation name="update">
            <bm:update-sql><![CDATA[
                BEGIN
                    hls_bp_master_credit_pkg.hls_credit_hd_save(p_bp_credit_hd_id => ${@bp_credit_hd_id},
                    										    p_bp_id => ${@bp_id},
                    										    p_user_id => ${/session/@user_id});
                END ;
            ]]></bm:update-sql>
        </bm:operation>
    </bm:operations>
    <bm:fields>
        <bm:field name="bp_credit_hd_id" databaseType="NUMBER" datatype="java.lang.Long" physicalName="BP_CREDIT_HD_ID" required="true"/>
        <bm:field name="bp_id" databaseType="NUMBER" datatype="java.lang.Long" physicalName="BP_ID" required="true"/>
        <bm:field name="bp_code" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="BP_CODE"/>
        <bm:field name="bp_name" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="BP_NAME"/>
        <bm:field name="bp_type" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="BP_TYPE"/>
        <bm:field name="credit_total_amount" databaseType="NUMBER" datatype="java.lang.Long" physicalName="CREDIT_TOTAL_AMOUNT"/>
        <bm:field name="open_amount" databaseType="NUMBER" datatype="java.lang.Long" physicalName="OPEN_AMOUNT"/>
        <bm:field name="used_amount" databaseType="NUMBER" datatype="java.lang.Long" physicalName="USED_AMOUNT"/>
        <bm:field name="last_amount" databaseType="NUMBER" datatype="java.lang.Long" physicalName="LAST_AMOUNT"/>
        <bm:field name="bp_category"/>
        <bm:field name="bp_category_n"/>
    </bm:fields>
    <bm:query-fields>
        <bm:query-field field="bp_code" queryExpression="t1.bp_code like upper(&apos;%&apos;||${@bp_code}||&apos;%&apos;)"/>
        <bm:query-field field="bp_name" queryOperator="like"/>
        <bm:query-field field="bp_category_n" queryOperator="like"/>
    </bm:query-fields>
</bm:model>
