<?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 *
                FROM (Select
                          aa.invoice_apply_id,
                          ah.invoice_hd_wfl_id,
                          al.invoice_ln_wfl_id,
                          ah.contract_id,
                          ah.project_id,
                          ah.bp_id,
                          al.cashflow_id,
                          ah.document_number,
                          cwo.write_off_id,
                          (Select c.contract_number
                           From con_contract c
                           Where c.contract_id = ah.contract_id)              contract_number,

                          (Select hb.bp_code
                           From hls_bp_master hb
                           Where hb.bp_id = ah.bp_id)                         bp_code,
                          ah.bp_name,
                          nvl(cwo.times,
                              (Select t.times
                               From con_contract_cashflow t
                               Where t.cashflow_id = al.cashflow_id))         times,

                                 (Select to_char(h.journal_date, 'yyyy-mm')
                                  From hls_journal_header h, hls_journal_detail l
                                  Where h.journal_header_id = l.journal_header_id
                                        And ((l.cashflow_id = al.cashflow_id
                                    and al.cf_item<>9) or (al.ref_n04=l.journal_line_id  and al.cf_item=9))
                                        And rownum = 1)                      journal_month,
                     (Select h.journal_num
                      From hls_journal_header h, hls_journal_detail l
                      Where h.journal_header_id = l.journal_header_id
                        And ((l.cashflow_id = al.cashflow_id
                          and al.cf_item<>9) or (al.ref_n04=l.journal_line_id  and al.cf_item=9))
                        And rownum = 1)                      journal_num,

                          al.tax_type_rate,
                          al.total_amount,
                          al.net_amount,
                          al.tax_amount,
                          nvl((Select ctr.paid_byother_flag
                               From csh_transaction ctr
                               Where ctr.transaction_id = cwo.csh_transaction_id),
                              'F')                                            paid_byother_flag,

                          (Select v.code_value_name
                           From sys_code_values_v v
                           Where v.code = 'PAID_BYOTHER_FLAG'
                                 And v.code_value =
                                     nvl((Select ctr.paid_byother_flag
                                          From csh_transaction ctr
                                          Where ctr.transaction_id = cwo.csh_transaction_id),
                                         'F'))                                paid_byother_flag_n,

                          (Select ctr.bp_bank_account_name
                           From csh_transaction ctr
                           Where ctr.transaction_id = cwo.csh_transaction_id) bp_bank_account_name,

                          (Select ctr.transaction_amount
                           From csh_transaction ctr
                           Where ctr.transaction_id = cwo.csh_transaction_id) transaction_amount,

                          cwo.write_off_due_amount,

                          (Select ctr.transaction_num
                           From csh_transaction ctr
                           Where ctr.transaction_id = cwo.csh_transaction_id) transaction_num,
                          ah.invoice_hd_id,
                          (cwo.write_off_id + al.invoice_ln_wfl_id)           file_id,
                          (Select ctr.guarantor_name_duty
                               From csh_transaction ctr
                               Where ctr.transaction_id = cwo.csh_transaction_id) guarantor_name_duty
                      From acr_invoice_apply aa,
                          acr_invoice_hd_wfl ah,
                          acr_invoice_ln_wfl al,
                          csh_write_off cwo
                      Where aa.invoice_apply_id = ah.invoice_apply_id
                            And ah.invoice_hd_wfl_id = al.invoice_hd_wfl_id
                            And al.cashflow_id = cwo.cashflow_id (+)) t1 #WHERE_CLAUSE#
                order by t1.contract_id desc, t1.times asc
                ]]></bm:query-sql>
        </bm:operation>
        <bm:operation name="update">
            <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:operations>
    <bm:query-fields>
        <bm:query-field name="contract_number" queryExpression="t1.contract_number = ${@contract_number}"/>
        <bm:query-field name="bp_code" queryExpression="t1.bp_code = ${@bp_code}"/>
        <bm:query-field name="journal_month" queryExpression="t1.journal_month = ${@journal_month}"/>
        <bm:query-field name="paid_byother_flag_n" queryExpression="t1.paid_byother_flag = ${@paid_byother_flag}"/>
    </bm:query-fields>
    <bm:data-filters>
        <bm:data-filter enforceOperations="query" expression="t1.invoice_apply_id = ${@invoice_apply_id}"/>
    </bm:data-filters>
</bm:model>
