<?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" needAccessControl="false">
    <bm:operations>
        <bm:operation name="query">
            <bm:query-sql><![CDATA[
                Select
                    h.invoice_apply_id,
                    h.invoice_hd_wfl_id,
                    h.document_number,
                    h.invoice_number,
                    (Select c.contract_number
                     From con_contract c
                     Where c.contract_id = h.contract_id)          contract_number,
                   (Select c.search_term_1
                     from con_contract c
                     where c.contract_id=h.contract_id) pur_contract_number,
                     (Select p.project_number from prj_project p,prj_project_lease_item ppli,con_contract_lease_item ccli,
                     con_contract cc where p.project_id=ppli.source_project_id and ppli.project_lease_item_id=ccli.project_lease_item_id
                     and ccli.contract_id=cc.contract_id and cc.contract_id=h.contract_id) pur_number,
                    (Select c.contract_name
                     From con_contract c
                     Where c.contract_id = h.contract_id)          contract_name,
                    (Select hb.bp_code
                     From hls_bp_master hb
                     Where hb.bp_id = h.bp_id)                     bp_code,
                    h.bp_name,
                    to_char(h.invoice_date, 'yyyy-mm-dd')          invoice_date,
                    h.bp_name                                      invoice_title,
                    h.total_amount,
                    (Select u.description
                     From sys_user u, acr_invoice_hd ah
                     Where u.user_id = ah.created_by
                           And ah.invoice_hd_id = h.invoice_hd_id) created_by_n,
                    h.invoice_status,
                    (Select v.code_value_name
                     From sys_code_values_v v
                     Where v.code = 'ACR510_AR_INVOICE_STATUS'
                           And v.code_value = h.invoice_status)    invoice_status_n,
					 h.invoice_kind_type,
                    (Select v.code_value_name
                     From sys_code_values_v v
                     Where v.code = 'ACR512_INVOICE_KIND_TYPE'
                           And v.code_value = h.invoice_kind_type)    invoice_kind_type_n,
                    (Select  ahd.vat_interface_status
                     From  acr_invoice_hd ahd
                     Where  ahd.invoice_hd_id=h.invoice_hd_id) vat_interface_status,
                    (Select v.code_value_name
                     From sys_code_values_v v, acr_invoice_hd ahd
                     Where v.code = 'ACR515_VAT_INTERFACE_STATUS'
                       And v.code_value = ahd.vat_interface_status
                       and v.code_enabled_flag = 'Y'
                       and v.code_value_enabled_flag = 'Y'
                       and ahd.invoice_hd_id=h.invoice_hd_id) As vat_interface_status_n,
                    to_char(h.accounting_date, 'yyyy-mm-dd')       accounting_date,
                    h.vat_red_notice_num,
                       h.invoice_hd_id,
                    (Select he.reversed_flag
                     From acr_invoice_hd he
                     Where he.invoice_hd_id = h.invoice_hd_id)reversed_flag,
                    (Select v.code_value_name
                     From sys_code_values_v v,acr_invoice_hd he
                     Where v.code = 'ACR514_AR_INVOICE_REVERSED_FLAG'
                       And v.code_value =nvl(he.reversed_flag,'N')
                and he.invoice_hd_id = h.invoice_hd_id)reversed_flag_n,
                    NVL((Select he.claim_status
                     From acr_invoice_hd he
                     Where he.invoice_hd_id = h.invoice_hd_id),'UNCLAIMED')claim_status,
                    (select v.code_value_name
                     from sys_code_values_v v
                     where v.code = 'CLAIM_STATUS'
                       and v.code_value =  NVL((Select he.claim_status
                     From acr_invoice_hd he
                     Where he.invoice_hd_id = h.invoice_hd_id),'UNCLAIMED'))claim_status_n


                From acr_invoice_hd_wfl h #WHERE_CLAUSE#
                order by h.invoice_hd_wfl_id desc
                ]]></bm:query-sql>
        </bm:operation>
        <!--工作流提交-->
        <bm:operation name="execute">
            <bm:update-sql><![CDATA[
                begin
                    acr_invoice_wfl_pkg.acr_invoice_wfl_submit(p_invoice_apply_id   => ${@invoice_apply_id},
                                                               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
                    acr_invoice_wfl_pkg.update_vat_red_notice_num(p_invoice_hd_wfl_id   => ${@invoice_hd_wfl_id},
                                                                  p_vat_red_notice_num  => ${@vat_red_notice_num},
                                                                  p_user_id             => ${/session/@user_id});
                end;
                ]]></bm:update-sql>
        </bm:operation>
    </bm:operations>
    <bm:query-fields>
        <bm:query-field name="contract_number" queryExpression="h.contract_number = ${@contract_number}"/>
        <bm:query-field name="bp_code" queryExpression="h.bp_code = ${@bp_code}"/>
        <bm:query-field name="journal_month" queryExpression="h.journal_month = ${@journal_month}"/>
        <bm:query-field name="paid_byother_flag_n" queryExpression="h.paid_byother_flag = ${@paid_byother_flag}"/>
    </bm:query-fields>
    <bm:data-filters>
        <bm:data-filter enforceOperations="query" expression="h.invoice_apply_id = ${@invoice_apply_id}"/>
    </bm:data-filters>
</bm:model>
