SELECT distinct (select distinct ORIG_SYSTEM from HZ_ORIG_SYS_REFERENCES where
orig_system_reference=hzp.orig_system_reference and rownum=1) ORIG_SYSTEM
, hzp.orig_system_reference party_orig_system_reference
, busineesunit.bu_name businees_unit
,hzp.party_name customer_name
,hca.account_number accountnumber
,party_site_number
,hcsu.status site_status
,hcsu.site_use_code addresstype
, location
, a5.address1
, a5.address2
, a5.address3
, a5.address4
, a5.city
, a5.postal_code
, a5.state
, a5.country
--,primary_flag
, cpc.name profile_class_name
, col.name collector_name
, cp.credit_balance_statements
, cp.credit_hold
, cp.dunning_letters send_dunning_letters
, cp.send_statements send_statements
, term.name standard_terms
, cp.credit_currency_code
, hzp1.person_first_name contactname
,hcp.email_address
,hcp_phone.phone_number
FROM hz_cust_accounts hca
,hz_parties hzp
,hz_party_sites hzs
,hz_cust_acct_sites_all hcas
,hz_cust_account_roles hcar
,hz_party_site_uses hps1
,hz_relationships hr
,hz_parties hzp1
,hz_contact_points hcp
,hz_cust_site_uses_all hcsu,
hz_contact_points hcp_phone,
(
SELECT DISTINCT
b.bu_name,a.set_id
FROM fnd_setid_assignments a
INNER JOIN fun_all_business_units_v b
ON a.determinant_value = b.bu_id
WHERE a.reference_group_name = 'HZ_CUSTOMER_ACCOUNT_SITE'
) busineesunit,
hz_locations a5
, hz_cust_profile_classes cpc
, hz_customer_profiles_f cp
, ar_collectors col
, ra_terms term
, ar_statement_cycles cyc
-- hz_parties hp2
WHERE hzp.party_id = hca.party_id
AND hzp.party_id = hzs.party_id
AND hzs.party_id = hca.party_id
AND hcas.party_site_id = hzs.party_site_id
AND hcas.cust_account_id = hca.cust_account_id
AND hps1.party_site_id = hzs.party_site_id
AND hcsu.cust_acct_site_id = hcas.cust_acct_site_id
AND hcar.cust_acct_site_id = hcas.cust_acct_site_id
AND hcar.relationship_id = hr.relationship_id
--AND hzp.PREFERRED_CONTACT_PERSON_ID(+) = hp2.party_id
--AND CONTACT_POINT_PURPOSE = 'BUSINESS'
--AND hca.account_number='MH100055'
AND hzp.party_id = hr.subject_id
AND hr.object_id = hzp1.party_id
AND hcp.owner_table_id (+)= hzp1.party_id
AND hr.relationship_id = hcp.relationship_id(+)
and hcp.contact_point_type(+) ='EMAIL'
and hcp.STATUS(+)='A'
AND hcp_phone.owner_table_id (+)= hzp1.party_id
AND hr.relationship_id = hcp_phone.relationship_id(+)
and hcp_phone.contact_point_type(+) ='PHONE'
and hcp_phone.STATUS(+)='A'
and busineesunit.set_id=hcas.set_id
AND hzs.location_id = a5.location_id
AND cp.profile_class_id = cpc.profile_class_id (+)
AND hca.cust_account_id = cp.cust_account_id
AND cp.collector_id = col.collector_id
AND cp.standard_terms = term.term_id (+)
AND cp.statement_cycle_id = cyc.statement_cycle_id (+)
AND cp.site_use_id IS NULL
and hzp.created_by =nvl(:p_created_by, hzp.created_by)
and busineesunit.bu_name =nvl(:p_Business_Unit,busineesunit.bu_name)
--PH.SITE.57AND NVL(hcp.PRIMARY_FLAG,'Y')='Y'
--and hca.account_number = 'PH.ACCOUNT-10'
--and hzp.party_name ='ELI LILLY (PHILIPPINES) Inc..PHLAT'