<?xml version="1.0" encoding="UTF-8"?>
<!--
    $Author: Hongquan.Dai  
    $Date: 2018-2-26 下午4:29:06  
    $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
                    pci.cdd_item,
                    pci.description,
                    pci.order_seq,
                    pci.show_seq,
                    pp.send_flag,
                    pp.paper_required,
                    pp.attachment_required,
                    pp.not_applicable,
                    pp.check_id,
                    (SELECT
                        COUNT(1)
                    FROM
                        fnd_atm_attachment_multi m
                    WHERE
                        m.table_name     = 'PRJ_CDD_ITEM_CHECK' AND
                        m.table_pk_value = TO_CHAR(pp.check_id)
                    )attach_count,
                    pci.sys_flag,
                    pci.note,
                    pci.cdd_list_id,
                    pci.cdd_item_id,
                    pci.line_type,
                    pci.chance_required_flag,
                    pci.chance_display_flag,
                    pci.project_required_flag,
                    pci.project_display_flag,
                    pci.contract_required_flag,
                    pci.contract_display_flag,
                    pci.sign_required_flag,
                    pci.sign_display_flag,
                    pci.incept_required_flag,
                    pci.incept_display_flag,
                    pci.lender_required_flag,
                    pci.lender_display_flag,
                    pci.chance_tab_group,
                    pci.project_tab_group,
                    pci.contract_tab_group,
                    pci.sign_tab_group,
                    pci.incept_tab_group,
                    pci.lender_tab_group,
                    tg.tab_group_id,
                    tg.tab_group attachment_tab_group,
                    lgt.important_flag,
                    lgt.seq_num,
                    DECODE(NVL(lgt.important_flag,'N'),'Y',1,'N',0,0) order_field,
                    NVL(pp.document_id,${@document_id}) document_id,
                    NVL(pp.document_table, ${@document_table}) document_table,
                    NVL(
                    (SELECT
                        'Y'
                    FROM
                        dual
                    WHERE
                        EXISTS
                        (SELECT
                            1
                        FROM
                            con_contract_payment_terms t,
                            con_contract_cashflow cf
                        WHERE
                            t.cashflow_id     =cf.cashflow_id AND
                            cf.contract_id    =${@document_id} AND
                            t.cdd_item_id     =pci.cdd_item_id AND
                            ${@document_table}='CON_CONTRACT'
                        )
                    ),'N') payment_terms_flag,
                    (SELECT
                        ptc.confirm_flag
                    FROM
                        con_contract_payment_t_check ptc
                    WHERE
                        ptc.cdd_item_id    = pci.cdd_item_id AND
                        ptc.contract_id    = ${@document_id} AND
                        ${@document_table} = 'CON_CONTRACT'
                    ) check_confirm_flag,
                    (SELECT
                        ptc.confirm_note
                    FROM
                        con_contract_payment_t_check ptc
                    WHERE
                        ptc.cdd_item_id    = pci.cdd_item_id AND
                        ptc.contract_id    = ${@document_id} AND
                        ${@document_table} = 'CON_CONTRACT'
                    ) check_confirm_note,
                    f.file_path,
                    pci.description
                    || '-' || f.file_name as file_name,
                    f.attachment_id,
                    (Select tab_group From prj_cdd_item_tab_group Where tab_group_id = tg.tab_group_id) AS attach_file_type,  -- add by zhuxianfei
                    (Select tab_group_name From prj_cdd_item_tab_group  Where tab_group_id = tg.tab_group_id) AS attach_file_type_n  
                FROM
                    (SELECT
                        pcf.check_id,
                        pcf.document_id,
                        pcf.document_table,
                        pck.cdd_item_id,
                        pck.send_flag,
                        pck.paper_required,
                        pck.attachment_required,
                        pck.not_applicable
                    FROM
                        prj_cdd_item_doc_ref pcf,
                        prj_cdd_item_check pck
                    WHERE
                        pcf.document_table = ${@document_table} AND
                        pcf.document_id    = ${@document_id} AND
                        pck.check_id       = pcf.check_id
                    ) pp,
                    prj_cdd_item pci,
                    prj_cdd_item_list_grp_tab lgt,
                    prj_cdd_item_tab_group tg,
                    fnd_atm_attachment_multi fm, 
                    fnd_atm_attachment f
                WHERE
                    pci.cdd_item_id  = lgt.cdd_item_id AND
                    lgt.TAB_GROUP_ID =tg.TAB_GROUP_ID AND
                    lgt.enabled_flag = 'Y' AND
                    tg.enabled_flag  = 'Y' AND
                    pci.cdd_item_id  = pp.cdd_item_id(+) AND
                    pci.cdd_list_id  = ${@cdd_list_id} AND
                    lgt.cdd_list_id  = pci.cdd_list_id AND
                    pci.enabled_flag = 'Y' AND
                    fm.attachment_id = f.attachment_id AND
                    fm.table_name = 'PRJ_CDD_ITEM_CHECK' AND
                    fm.table_pk_value = pp.check_id 
                ORDER BY
                lgt.seq_num ASC
            
            ]]></bm:query-sql>
        </bm:operation>
    </bm:operations>
    <bm:fields>
        <bm:field name="file_name" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="FILE_NAME"/>
        <bm:field name="file_path" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="FILE_PATH"/>
        <bm:field name="attachment_id" databaseType="NUMBER" datatype="java.lang.Long" physicalName="ATTACHMENT_ID"/>
    </bm:fields>
</bm:model>
