<?xml version="1.0" encoding="UTF-8"?>
<!--
    $Author: lpc 9874
    $Date: 
    $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.description,
                    'Y' AS lend_flag,
                    ylrl.quantity,
                    ylrl.use_to,
                    ylrl.comments,
                    ylrl.lend_line_id,
                    ylrl.cdd_item_id
                FROM
                    document_lend_record_ln ylrl,
                    prj_cdd_item pci
                WHERE
                    ylrl.cdd_item_id    = pci.cdd_item_id AND
                    ylrl.lend_header_id = ${@lend_header_id}
                UNION ALL
                SELECT
                    tt.description,
                    tt.lend_flag,
                    '' AS quantity,
                    '' AS use_to,
                    '' AS comments,
                    '' AS lend_line_id,
                    tt.cdd_item_id
                FROM
                    (SELECT
                        (SELECT
                            p.document_table
                        FROM
                            prj_cdd_item_doc_ref p
                        WHERE
                            p.check_id        = pcc.check_id AND
                            p.document_id     = t.contract_id AND
                            p.document_table IN ('CON_CONTRACT_CREDIT', 'CON_CONTRACT')
                        ) document_table,
                        t.contract_id document_id,
                        t.document_category,
                        pci.cdd_item_id,
                        pci.cdd_list_id,
                        pci.cdd_item,
                        pci.description,
                        pci.enabled_flag,
                        pci.note,
                        NVL(pci.sys_flag, 'N') sys_flag,
                        pcc.paper_required,
                        pcc.attachment_required,
                        pcc.not_applicable,
                        pcc.check_id,
                        pci.order_seq,
                        pci.sign_required_flag,
                        hls_sys_upload_pkg.get_atm_file_href(p_table_pk_value => pcc.check_id, p_source_type => 'PRJ_CDD_ITEM_CHECK', p_user_id => ${/session/@user_id}) AS file_name,
                        pci.lend_flag
                    FROM
                        prj_cdd_item pci,
                        prj_cdd_item_check pcc,
                        con_contract t
                    WHERE
                        pci.cdd_item_id  = pcc.cdd_item_id(+) AND
                        t.cdd_list_id    = pci.cdd_list_id AND
                        pci.cdd_list_id  = t.cdd_list_id AND
                        pci.enabled_flag = 'Y' AND
                        t.contract_id    = ${@contract_id}
                    ) tt
                WHERE
                    NOT EXISTS
                    (SELECT
                        1
                    FROM
                        document_lend_record_ln l,
                        document_lend_record_hd h
                    WHERE
                        l.lend_header_id = h.lend_header_id AND
                        l.cdd_item_id    = tt.cdd_item_id AND
                        l.contract_id    = tt.document_id AND
                        h.real_date     IS NULL
                    )
            ]]></bm:query-sql>
        </bm:operation>
    </bm:operations>
</bm:model>
