<?xml version="1.0" encoding="UTF-8"?>
<bm:model xmlns:bm="http://www.leaf-framework.org/schema/bm">
    <bm:operations>
        <bm:operation name="query">
            <bm:query-sql><![CDATA[
                SELECT
                    *
                FROM
                    (SELECT
                        eou.unit_id,
                        eou.unit_code,
                        eou.description_id,
                        (SELECT
                            DESCRIPTION_TEXT
                        FROM
                            fnd_descriptions
                        WHERE
                            DESCRIPTION_ID = eou.description_id AND
                            Language       = 'ZHS'
                        ) AS org_unit_description,
                        eou.company_id
                    FROM
                        EXP_ORG_UNIT eou
                    WHERE
                        eou.unit_id NOT IN
                        (SELECT
                            h.unit_id
                        FROM
                            hls_product_plan_district h
                        WHERE
                            h.product_plan_id = ${/parameter/@product_plan_id}
                        )
                    ) t1 #WHERE_CLAUSE#
            ]]></bm:query-sql>
        </bm:operation>
        <bm:operation name="update">
            <bm:update-sql><![CDATA[
                BEGIN
                    BEGIN
                        DELETE
                        FROM
                            hls_product_plan_district a
                        WHERE
                            a.product_plan_id = ${@product_plan_id};
                        FOR cus_rec          IN
                        (SELECT sr.unit_id FROM EXP_ORG_UNIT sr WHERE sr.unit_id IN (${:@unit_id})
                        )
                        LOOP
                            prj_project_pkg.update_district_ln(p_product_plan_id =>${@product_plan_id}, 
                            										   p_district_id =>cus_rec.unit_id, 
                            										   p_user_id =>${/session/@user_id});
                        END LOOP;
                    END;
                END;
            ]]></bm:update-sql>
        </bm:operation>
        <bm:operation name="delete">
            <bm:update-sql><![CDATA[
                BEGIN
                  delete from hls_product_plan_district where product_plan_id = ${@product_plan_id};
                END;
            ]]></bm:update-sql>
        </bm:operation>
    </bm:operations>
    <bm:fields>
        <bm:field name="unit_id" required="true"/>
        <bm:field name="unit_code"/>
        <bm:field name="description_id"/>
        <bm:field name="org_unit_description"/>
        <bm:field name="company_id"/>
    </bm:fields>
</bm:model>
