<?xml version="1.0" encoding="UTF-8"?>
<bm:model xmlns:bm="http://www.leaf-framework.org/schema/bm">
    <bm:fields>
        <bm:field name="invoice_hd_id"/>
        <bm:field name="document_number"/>
        <bm:field name="bp_id"/>
        <bm:field name="virtual_con_number"/>
        <bm:field name="contract_number"/>
        <bm:field name="invoice_number"/>
        <bm:field name="invoice_kind"/>
        <bm:field name="invoice_kind_n"/>
        <bm:field name="invoice_date" databaseType="DATE" datatype="java.util.Date"/>
        <bm:field name="accounting_date" databaseType="DATE" datatype="java.util.Date"/>
        <bm:field name="bp_name"/>
        <bm:field name="bp_tax_registry_num"/>
        <bm:field name="cf_item"/>
        <bm:field name="cf_item_n"/>
        <bm:field name="product_name"/>
        <bm:field name="contract_id"/>
        <bm:field name="cashflow_id"/>
        <bm:field name="due_amount" databaseType="NUMBER" datatype="java.lang.Double"/>
        <bm:field name="total_amount" databaseType="NUMBER" datatype="java.lang.Double"/>
        <bm:field name="tax_amount" databaseType="NUMBER" datatype="java.lang.Double"/>
    </bm:fields>
    <bm:operations>
        <bm:operation name="query">
            <bm:query-sql><![CDATA[
        		SELECT
        		    *
        		FROM
        		    (SELECT
        		        hd.invoice_hd_id,
        		        hd.document_number,
        		        hd.bp_id,
        		        hd.contract_id,
        		        pp.virtual_con_number,
        		        cc.contract_number,
        		        hd.invoice_number,
        		        hd.invoice_kind,
        		        (SELECT
        		            v.code_value_name
        		        FROM
        		            sys_code_values_v v
        		        WHERE
        		            v.code       = 'ACR510_INVOICE_KIND' AND
        		            v.code_value = hd.invoice_kind
        		        ) invoice_kind_n,
        		        hd.invoice_date,
        		        hd.accounting_date,
        		        hd.bp_name,
        		        hd.bp_tax_registry_num,
        		        ln.cf_item,
        		        ln.cashflow_id,
        		        (SELECT
        		            hci.description
        		        FROM
        		            hls_cashflow_item hci
        		        WHERE
        		            hci.cf_item      = ln.cf_item AND
        		            hci.enabled_flag = 'Y'
        		        ) cf_item_n,
        		        ln.product_name,
        		        ccc.due_amount,
        		        hd.total_amount,
        		        hd.tax_amount
        		    FROM
        		        acp_invoice_hd hd,
        		        acp_invoice_ln ln,
        		        prj_project pp,
        		        con_contract cc,
        		        con_contract_cashflow ccc
        		    WHERE
        		        hd.invoice_hd_id  = ln.invoice_header_id AND
        		        hd.contract_id    =cc.contract_id AND
        		        ln.cashflow_id    = ccc.cashflow_id AND
        		        cc.contract_id    = ccc.contract_id AND
        		        cc.project_id     = pp.project_id AND
        		        hd.reversed_flag  = 'N' AND
        		        hd.invoice_status = 'NEW'
        		    ) t1 #WHERE_CLAUSE#
    		]]></bm:query-sql>
        </bm:operation>
        <bm:operation name="insert">
            <bm:update-sql><![CDATA[
                BEGIN
                    sdic_invoice_pkg.acp_invoice_insert(p_contract_id =>${@contract_id},
                               p_bp_id           =>${@bp_id},
                               p_invoice_number  =>${@invoice_number},
                               p_invoice_kind    =>${@invoice_kind},
                               p_invoice_date    =>to_date(${@invoice_date},'yyyy/mm/dd'),
                               p_accounting_date =>to_date(${@accounting_date},'yyyy/mm/dd'),
                               p_cashflow_id     =>${@cashflow_id},
                               p_product_name    =>${@product_name},
                               p_total_amount    =>${@total_amount},
                               p_tax_amount      =>${@tax_amount},
                               p_company_id      =>${/session/@company_id},
                               p_user_id         =>${/session/@user_id});
                END;
            ]]></bm:update-sql>
        </bm:operation>
        <bm:operation name="update">
            <bm:update-sql><![CDATA[
                BEGIN
                    sdic_invoice_pkg.acp_invoice_update(p_invoice_hd_id   =>${@invoice_hd_id},
                    								    p_invoice_line_id =>${@invoice_line_id},
                    								    p_invoice_number  =>${@invoice_number}, 
                    								    p_invoice_kind    =>${@invoice_kind}, 
                    								    p_invoice_date    =>to_date(${@invoice_date},'yyyy/mm/dd'), 
                    								    p_accounting_date =>to_date(${@accounting_date},'yyyy/mm/dd'), 
                    								    p_product_name    =>${@product_name}, 
                    								    p_total_amount    =>${@total_amount}, 
                    								    p_tax_amount      =>${@tax_amount}, 
                    								    p_user_id         =>${/session/@user_id});
                END;
            ]]></bm:update-sql>
        </bm:operation>
        <bm:operation name="delete">
            <bm:update-sql><![CDATA[
                BEGIN
                    sdic_invoice_pkg.acp_invoice_delete(p_invoice_hd_id   =>${@invoice_hd_id}，
                                                        p_user_id =>${/session/@user_id});
                END;
            ]]></bm:update-sql>
        </bm:operation>
    </bm:operations>
    <bm:query-fields>
        <bm:query-field name="contract_id" queryExpression="t1.contract_id=${@contract_id}"/>
        <bm:query-field name="cashflow_id" queryExpression="t1.cashflow_id=${@cashflow_id}"/>
        <bm:query-field name="bp_id" queryExpression="t1.bp_id=${@bp_id}"/>
    </bm:query-fields>
</bm:model>
