<?xml version="1.0" encoding="UTF-8"?>
<bm:model xmlns:bm="http://www.leaf-framework.org/schema/bm">
    <bm:operations>
        <bm:operation name="update">
            <bm:update-sql><![CDATA[
                UPDATE
                    hn_payment_cond_change_hd t1
                SET
                    t1.description = ${@description}
                WHERE
                    t1.header_id = ${@header_id}
            ]]></bm:update-sql>
        </bm:operation>
        <bm:operation name="query">
            <bm:query-sql><![CDATA[
                SELECT
                    hd.description,
                    hd.note,
                    DECODE(hd.record_type, 'CONTRACT', '支付表'
                    || hd.hd_detail, hd.hd_detail) hd_detail,
                    hd.project_id,
                    hd.valid_from,
                    hd.valid_to,
                    hd.contract_id,
                    pp.bp_id_tenant,
                    hd.record_type,
                    (SELECT
                        v.code_value_name
                    FROM
                        sys_code_values_v v
                    WHERE
                        v.code       = 'PAYMENT_CON_HD_TYPE' AND
                        v.code_value = hd.record_type
                    ) record_type_n,
                    (SELECT hbm.bp_name FROM hls_bp_master hbm WHERE hbm.bp_id = pp.bp_id_tenant
                    ) bp_name,
                    pp.project_number,
                    pp.project_name,
                    pp.employee_id,
                    (SELECT ee.name FROM exp_employees ee WHERE ee.employee_id = pp.employee_id
                    ) employee_name,
                    hd.wfl_status,
                    (SELECT
                        v.code_value_name
                    FROM
                        sys_code_values_v v
                    WHERE
                        v.code       = 'CSH900_CONDITION_STATUS' AND
                        v.code_value = hd.wfl_status
                    ) wfl_status_n,
                    --ln.hd_id,
                    (case when (select t1.req_status from hn_payment_cond_change_hd t1 where t1.header_id = ln.header_id) <> 'APPROVED' then
                       ln.hd_id 
                    else
                      (select hcp.hd_id
                         from hn_con_payment_con_hd hcp 
                        where hcp.project_id = hd.project_id 
                          and hcp.hd_detail = hd.hd_detail
                          and hcp.version = hd.version
                          and hcp.data_class = 'HISTORY')
                    end) hd_id,
                    ln.change_hd_id,
                    ln.line_id
                FROM
                    hn_con_payment_con_hd hd,
                    prj_project pp,
                    hn_payment_cond_change_ln ln
                WHERE
                    pp.data_class = 'NORMAL' AND
                    hd.hd_id      = ln.change_hd_id AND
                    hd.project_id = pp.project_id AND
                    ln.header_id  = ${@header_id}
            ]]></bm:query-sql>
        </bm:operation>
    </bm:operations>
</bm:model>
