<?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
                        A.*,
                        ROWNUM RN
                    FROM
                        (SELECT
                            *
                        FROM
                            (
                select b.bp_name,
  b.bp_id,
 (select  t.description   value_name      from hls_bp_type_v t    where t.enabled_flag = 'Y'and  t.bp_type = b.bp_type) bp_type_n,
  b.bp_type,
 (select  v.code_value_name value_name   from sys_code_values_v v  where v.code = 'HLS211_BP_CLASS'  and v.code_enabled_flag='Y'  and v.code_value_enabled_flag='Y' and v.code_value = b.bp_class) bp_class_n,
  b.bp_class,
 (select distinct t.bp_category_desc bp_category_n
         from hls_bp_type_v t
        where t.enabled_flag = 'Y'
        and  t.bp_category = b.bp_category
        and exists (select 1 from hls_bp_type_assign ha where ha.bp_type = t.bp_type
        )) bp_category_n,
  (select  v.code_value_name as value_name from sys_code_values_v v where v.code = 'HLS_SYS_UNIT_PROPERTY' and v.code_enabled_flag = 'Y' and v.code_value_enabled_flag = 'Y' and v.code_value=b.legal_form) legal_form_n,
  b.legal_form,
  b.organization_code,
  b.business_license_num,
  to_char(b.founded_date,'yyyy-mm-dd') founded_date,
  b.registered_capital,
  b.corporate_code,
  b.phone,
  b.liv_province,
  (select
             p.description   as code_name
FROM fnd_province p where p.province_id = b.liv_province) liv_province_n,
  
  (select f.description as code_name
              from fnd_city f
             where f.enabled_flag = 'Y'
               and f.city_id = b.liv_city) liv_city_n,
  
  b.liv_city,
  (select 
       f.description as code_name
       
  from fnd_district f, mor_register t
 where f.enabled_flag = 'Y'
   and f.district_id = t.district_id(+)
   and f.district_id = b.liv_district) liv_district_n,
  
  b.liv_street,
  b.office_space,
  b.contact_person,
  b.taxpayer_type,
  (select v.code_value_name value_name
  from sys_code_values_v v
 where v.code = 'YES_NO'
   and v.code_enabled_flag = 'Y'
   and v.code_value_enabled_flag = 'Y' and   v.code_value=b.taxpayer_type) taxpayer_type_n,
  b.tax_registry_num,
  b.invoice_bp_address_phone_num,
  b.invoice_title,
  b.invoice_bp_bank_account,
  b.bp_code,
  b.gender,
  (select  v.code_value_name as value_name
  from sys_code_values_v v
 where v.code = 'SYS_SEX_TYPE'
   and v.code_enabled_flag = 'Y'
   and v.code_value_enabled_flag = 'Y'
   and v.code_value = b.gender
  ) gender_n,
  b.id_card_no,
  b.id_type,
  (select distinct v.code_value_name as value_name from sys_code_values_v v where v.code_value = b.id_type) id_type_n,
  to_char(b.date_of_birth,'yyyy-mm-dd')date_of_birth,
  b.marital_status,
  ( select 
    v.code_value_name as value_name
                     from sys_code_values_v v
                    where v.code = 'HLS_CUS_MARRIAGE_STATUS' 
                      and v.code_enabled_flag = 'Y'
                      and v.code_value_enabled_flag = 'Y' 
                      and v.code_value =  b.marital_status ) marital_status_n,
  b.liv_district,
  b.academic_background,
  (select  v.code_value_name as value_name from sys_code_values_v v where v.code = 'HLS211_ACADEMIC_BACKGROUND' and v.code_enabled_flag = 'Y'and v.code_value_enabled_flag ='Y' and v.code_value =  b.academic_background) academic_background_n,
  b.industry,
  b.work_unit,
  b.work_unit_phone,
  b.monthly_income,
  b.cell_phone,
  b.id_number,
  b.created_by

from hls_bp_master b

) t #WHERE_CLAUSE#
                        ORDER BY
                            t.bp_id DESC
                        )A
                    WHERE
                        ROWNUM <= NVL(${@pagesize},10) * NVL(${@offsize},1)
                    )
                WHERE
                    RN >= 1 + ((NVL(${@offsize},1)-1) * NVL(${@pagesize},10))

            ]]></bm:query-sql>
        </bm:operation>
    </bm:operations>
    <bm:fields>
        <bm:field name="bp_name"/>
        <bm:field name="bp_id"/>
        <bm:field name="id_number"/>
        <bm:field name="bp_type_n"/>
        <bm:field name="bp_type"/>
        <bm:field name="bp_class_n"/>
        <bm:field name="bp_class"/>
        <bm:field name="bp_category_n"/>
        <bm:field name="legal_form_n"/>
        <bm:field name="legal_form"/>
        <bm:field name="organization_code"/>
        <bm:field name="business_license_num"/>
        <bm:field name="founded_date"/>
        <bm:field name="registered_capital"/>
        <bm:field name="corporate_code"/>
        <bm:field name="phone"/>
        <bm:field name="liv_province"/>
        <bm:field name="liv_city"/>
        <bm:field name="liv_province_n"/>
        <bm:field name="liv_city_n"/>
        <bm:field name="liv_street"/>
        <bm:field name="office_space"/>
        <bm:field name="cell_phone"/>
        <bm:field name="taxpayer_type"/>
        <bm:field name="taxpayer_type_n"/>
        <bm:field name="tax_registry_num"/>
        <bm:field name="invoice_bp_address_phone_num"/>
        <bm:field name="invoice_title"/>
        <bm:field name="invoice_bp_bank_account"/>
        <bm:field name="bp_code"/>
        <bm:field name="gender"/>
        <bm:field name="gender_n"/>
        <bm:field name="id_card_no"/>
        <bm:field name="id_type"/>
        <bm:field name="id_type_n"/>
        <bm:field name="date_of_birth"/>
        <bm:field name="marital_status"/>
        <bm:field name="marital_status_n"/>
        <bm:field name="liv_district"/>
        <bm:field name="liv_district_n"/>
        <bm:field name="academic_background"/>
        <bm:field name="academic_background_n"/>
        <bm:field name="industry"/>
        <bm:field name="work_unit"/>
        <bm:field name="work_unit_phone"/>
        <bm:field name="monthly_income"/>
        <bm:field name="contact_person"/>
    </bm:fields>
    <bm:query-fields>
        <bm:query-field name="searchInput" queryExpression="(t.bp_name like &apos;%&apos;||${@searchInput}||&apos;%&apos; or t.bp_class_n like &apos;%&apos;||${@searchInput}||&apos;%&apos; or t.bp_type_n like &apos;%&apos;||${@searchInput}||&apos;%&apos; or t.bp_type_n like &apos;%&apos;||${@searchInput}||&apos;%&apos;)"/>
        <bm:query-field name="bp_id" queryExpression="t.bp_id=${@bp_id}"/>
        <bm:query-field name="bp_name" queryExpression="t.bp_name like &apos;%&apos;||${@bp_name}||&apos;%&apos;"/>
    </bm:query-fields>
</bm:model>
