<?xml version="1.0" encoding="UTF-8"?>
<!--
    $Author: LR  
    $Date: 2013-7-16 上午09:06:33  
    $Revision: 1.0  
    $Purpose: 
-->

<bm:model xmlns:bm="http://www.leaf-framework.org/schema/bm">
    <bm:operations>
        <bm:operation name="query">
            <bm:query-sql><![CDATA[
                SELECT *
  FROM (SELECT t1.invoice_hd_id,
               t1.document_number,
               TO_CHAR(t1.invoice_date, 'yyyy-mm-dd') AS invoice_date,
               t1.document_category,
               t1.contract_id,
               t1.contract_number,
               nvl(t1.contract_name,(select contract_name from con_contract 
                                         where contract_id =(select cf.contract_id 
                                         from  con_contract_Cashflow cf,acr_invoice_ln ln
                                         where ln.invoice_hd_id=t1.invoice_hd_id
                                         and cf.cashflow_id=ln.cashflow_id
                                         and rownum=1)
                                         
               ))contract_name, --modify by wuts 2019-1-22
               --decode(t1.lease_channel,'01',t1.project_name,'')  contract_name2,
               t1.project_id,
               t1.project_number,
               t1.project_name,
               t1.invoice_bp_id,
               t1.invoice_bp_code,
               t1.invoice_bp_name,
               t1.invoice_title,
               t1.invoice_number,
               t1.invoice_status,
               t1.invoice_status_desc,
               t1.currency,
               t1.business_type,
               t1.business_type_desc,
               t1.invoice_kind,
               t1.invoice_kind_desc,
               t1.document_type,
               t1.created_by_name,
               t1.created_by,
               TO_CHAR(t1.accounting_date, 'yyyy-mm-dd') AS accounting_date,
               t1.total_amount,
               t1.tax_amount,
               t1.billing_method,
               t1.lease_organization,
               t1.lease_channel,
               t1.division,
               t1.company_id,
               t1.spv_company_id
          FROM acr_invoice_hd_v t1 
         ORDER BY t1.invoice_date DESC, t1.document_number DESC) t1 #WHERE_CLAUSE#
 ORDER BY invoice_date DESC, document_number DESC
            ]]></bm:query-sql>
        </bm:operation>
        <bm:operation name="delete">
            <bm:update-sql><![CDATA[
				begin
					 ACR_INVOICE_PKG.delete_invoice(p_invoice_hd_id 	=> ${@invoice_hd_id},
					 								p_user_id 			=> ${/session/@user_id});
				end;
			]]></bm:update-sql>
        </bm:operation>
        <bm:operation name="update">
            <bm:update-sql><![CDATA[
                BEGIN
                    UPDATE
                        acr_invoice_hd a
                    SET
                        a.merge_flag = ${@merge_flag}
                    WHERE
                        a.invoice_hd_id = ${@invoice_hd_id};
                    acr_invoice_pkg.invoice_row_merge(p_invoice_hd_id => ${@invoice_hd_id}, p_user_id => ${/session/@user_id}, p_merge_flag =>${@merge_flag});
                END;
            ]]></bm:update-sql>
        </bm:operation>
    </bm:operations>
    <bm:query-fields>
        <bm:query-field name="document_number_f" queryExpression="t1.document_number &gt;= ${@document_number_f}"/>
        <bm:query-field name="document_number_t" queryExpression="t1.document_number &lt;= ${@document_number_t}"/>
        <bm:query-field name="invoice_number_f" queryExpression="t1.invoice_number &gt;= ${@invoice_number_f}"/>
        <bm:query-field name="invoice_number_t" queryExpression="t1.invoice_number &lt;= ${@invoice_number_t}"/>
        <bm:query-field name="contract_number_f" queryExpression="t1.contract_number &lt;= ${@contract_number_f}"/>
        <bm:query-field name="contract_number_t" queryExpression="t1.contract_number &lt;= ${@contract_number_t}"/>
        <bm:query-field name="contract_name" queryExpression="t1.contract_name like ${@contract_name}"/>
        <bm:query-field name="invoice_bp_name" queryExpression="t1.invoice_bp_name like ${@invoice_bp_name}"/>
        <bm:query-field name="invoice_status" queryExpression="t1.invoice_status like ${@invoice_status}"/>

        <bm:query-field name="invoice_date_f" queryExpression="to_date(t1.invoice_date,&apos;yyyy-mm-dd&apos;) &gt;= to_date(${@invoice_date_f},&apos;yyyy-mm-dd&apos;)"/>
        <bm:query-field name="invoice_date_t" queryExpression="to_date(t1.invoice_date,&apos;yyyy-mm-dd&apos;) &lt;= to_date(${@invoice_date_t},&apos;yyyy-mm-dd&apos;)"/>
        <bm:query-field name="accounting_date_f" queryExpression="to_date(t1.accounting_date,&apos;yyyy-mm-dd&apos;) &gt;= to_date(${@accounting_date_f},&apos;yyyy-mm-dd&apos;)"/>
        <bm:query-field name="accounting_date_t" queryExpression="to_date(t1.accounting_date,&apos;yyyy-mm-dd&apos;) &lt;= to_date(${@accounting_date_t},&apos;yyyy-mm-dd&apos;)"/>
        <bm:query-field name="total_amount_f" queryExpression="t1.total_amount &gt;= ${@total_amount_f}"/>
        <bm:query-field name="total_amount_t" queryExpression="t1.total_amount &lt;= ${@total_amount_t}"/>
        <bm:query-field name="invoice_bp_code_f" queryExpression="t1.invoice_bp_code &gt;= ${@invoice_bp_code_f}"/>
        <bm:query-field name="invoice_bp_code_t" queryExpression="t1.invoice_bp_code &lt;= ${@invoice_bp_code_t}"/>
        <bm:query-field name="currency" queryExpression="t1.currency = ${@currency}"/>
        <bm:query-field name="business_type" queryExpression="t1.business_type = ${@business_type}"/>
        <bm:query-field name="invoice_kind" queryExpression="t1.invoice_kind = ${@invoice_kind}"/>
        <bm:query-field name="invoice_title" queryExpression="t1.invoice_title like ${@invoice_title}"/>
        <bm:query-field name="created_by" queryExpression="t1.created_by = ${@created_by}"/>
<!--
        <bm:query-field name="contract_number" queryExpression="exists     (select 1            from hls_document_flow_all_v df, con_contract cc           where df.contract_id = cc.contract_id             and df.ar_invoice_hd_id = t1.invoice_hd_id             and cc.contract_number between nvl(${@contract_number_f}, cc.contract_number) and nvl(${@contract_number_t}, cc.contract_number))"/>
-->
        <bm:query-field name="query_project_number" queryExpression="exists     (select 1           from hls_document_flow_all_v df, prj_project pp          where df.ar_invoice_hd_id = t1.invoice_hd_id            and df.project_id = pp.project_id            and pp.project_number between nvl(${@project_number_f}, pp.project_number) and nvl(${@project_number_t}, pp.project_number))"/>
        <bm:query-field name="lease_channel" queryExpression="t1.lease_channel = ${@lease_channel}"/>
    </bm:query-fields>
    <bm:data-filters>
        <bm:data-filter enforceOperations="query" expression="t1.invoice_status in( &apos;NEW&apos;,&apos;REJECT&apos;)"/>
        <bm:data-filter enforceOperations="query" expression="nvl(t1.spv_company_id,t1.company_id)=${/session/@company_id}"/>
        <bm:data-filter enforceOperations="query" expression="not exists (select 1 from acr_invoice_hd_wfl ah where ah.invoice_hd_id = t1.invoice_hd_id and ah.invoice_status = &apos;CONFIRMING&apos;)"/>
    </bm:data-filters>
</bm:model>
