<?xml version="1.0" encoding="UTF-8"?>
<!--
    $Author: lpc 9874  
    $Date: 2017/11/21 9:39:12  
    $Revision: 1.0  
    $Purpose: 
-->
<bm:model xmlns:s="leaf.plugin.script" xmlns:bm="http://www.leaf-framework.org/schema/bm">
    <bm:operations>
        <bm:operation name="query">
            <bm:query-sql><![CDATA[
                SELECT
                    cc.contract_id,
                    cc.contract_number,
                    cc.contract_name,
                    cc.bp_id_tenant,
                    (SELECT bp.bp_name FROM hls_bp_master bp WHERE bp.bp_id = cc.bp_id_tenant
                    ) bp_id_tenant_n,
                    cc.employee_id,
                    (SELECT ee.name FROM exp_employees ee WHERE ee.employee_id = cc.employee_id
                    ) employee_id_n,
                    1 times_from,
                    cc.lease_times times_to
                FROM
                    con_contract cc
                WHERE
                    cc.contract_status = 'INCEPT' AND
                    cc.data_class      = 'NORMAL' AND
                    NOT EXISTS
                    (SELECT
                        *
                    FROM
                        bgfl_tre_factoring_contract bfc
                    WHERE
                        bfc.contract_id = cc.contract_id
                    )
            ]]></bm:query-sql>
        </bm:operation>
        <bm:operation name="insert">
            <bm:update-sql><![CDATA[
                BEGIN
                    INSERT
                    INTO
                        bgfl_tre_factoring_contract
                        (
                            record_id,
                            loan_contract_id,
                            contract_id,
                            times_from,
                            times_to,
                            created_by,
                            creation_date,
                            last_updated_by,
                            last_update_date
                        )
                        VALUES
                        (
                            bgfl_tre_factoring_contract_s.nextval,
                            ${@loan_contract_id},
                            ${@contract_id},
                            1,
                            ${@times_to},
                            ${/session/@user_id},
                            SYSDATE,
                            ${/session/@user_id},
                            SYSDATE
                        );
                END;
            ]]></bm:update-sql>
        </bm:operation>
    </bm:operations>
    <bm:fields>
        <bm:field name="contract_id" databaseType="NUMBER" datatype="java.lang.Long"/>
        <bm:field name="contract_name" databaseType="VARCHAR2" datatype="java.lang.String"/>
        <bm:field name="contract_number" databaseType="VARCHAR2" datatype="java.lang.String"/>
        <bm:field name="bp_id_tenant_n" databaseType="VARCHAR2" datatype="java.lang.String"/>
        <bm:field name="employee_id_n" databaseType="VARCHAR2" datatype="java.lang.String"/>
        <bm:field name="employee_id" databaseType="NUMBER" datatype="java.lang.Long"/>
        <bm:field name="bp_id_tenant" databaseType="NUMBER" datatype="java.lang.Long"/>
        <bm:field name="times_from" databaseType="NUMBER" datatype="java.lang.Long"/>
        <bm:field name="times_to" databaseType="NUMBER" datatype="java.lang.Long"/>
    </bm:fields>
</bm:model>
