segment1 AS g1_vendor_no,
vendor_name AS g1_vendor_name,
party_name AS g1_contact_name,
primary_phone_country_code AS g1_cnt_cntry,
primary_phone_area_code g1_cnt_area,
primary_phone_number g1_cnt_phone,
vendor_site_code AS g1_vendor_site_code,
(SELECT MAX(start_time) last_logged_on FROM apps.fnd_logins log WHERE log.user_id = user_id) AS g1_last_login,
email_address AS g1_email_address,
vendor_type_lookup_code AS g1_vendor_type_lookup_code,
decode(sign(nvl(end_date_active, SYSDATE + 1) - SYSDATE), 1, 'A', 0, 'A', 'I') AS g1_vendor_status,
web_user_id AS g1_web_user_id,
decode(web_user_id, NULL, 'N', 'Y') AS g1_isp_portal,
end_date_active AS g1_end_date_active,
creation_date AS g1_vnd_creation_date
FROM (SELECT DISTINCT asu.party_id,
asu.segment1,
asu.vendor_name,
hpc.party_name,
hpr.primary_phone_country_code,
hpr.primary_phone_area_code,
hpr.primary_phone_number,
assa.vendor_site_code,
assa.vendor_site_id,
asco.vendor_contact_id,
usr.user_id,
usr.email_address,
asu.end_date_active,
aw.web_user_id,
asu.creation_date,
asu.vendor_type_lookup_code
FROM hz_relationships hr,
ap_suppliers asu,
ap_supplier_sites_all assa,
ap_supplier_contacts asco,
hz_org_contacts hoc,
hz_parties hpc,
hz_parties hpr,
hz_contact_points hpcp,
apps.ak_web_user_sec_attr_values aw,
apps.fnd_user usr
WHERE hoc.party_relationship_id = hr.relationship_id
AND hr.subject_id = asu.party_id
AND hr.relationship_code = 'CONTACT'
AND hr.object_table_name = 'HZ_PARTIES'
AND asu.vendor_id = assa.vendor_id
AND hr.object_id = hpc.party_id
AND hr.party_id = hpr.party_id
AND asco.relationship_id(+) = hoc.party_relationship_id
AND assa.party_site_id(+) = asco.org_party_site_id
AND hpr.party_type = 'PARTY_RELATIONSHIP'
AND hpr.party_id = hpcp.owner_table_id
AND hpcp.owner_table_name = 'HZ_PARTIES'
AND aw.number_value(+) = hpc.party_id
AND usr.user_id(+) = aw.web_user_id
AND aw.attribute_code(+) = 'ICX_CUSTOMER_CONTACT_ID'
AND aw.attribute_application_id(+) = 178
AND SYSDATE <= nvl(hr.end_date, SYSDATE + 1)
--AND asu.segment1 = '10002'
UNION ALL
/*non site contacts*/
SELECT DISTINCT asu.party_id,
asu.segment1 supp_num,
asu.vendor_name,
hpc.party_name contact_name,
hpr.primary_phone_country_code cnt_cntry,
hpr.primary_phone_area_code cnt_area,
hpr.primary_phone_number cnt_phone,
NULL, --,assa.vendor_site_code
NULL, --,assa.vendor_site_id
asco.vendor_contact_id,
usr.user_id,
usr.email_address,
asu.end_date_active,
aw.web_user_id,
asu.creation_date,
asu.vendor_type_lookup_code
FROM hz_relationships hr,
ap_suppliers asu,
--,ap_supplier_sites_all assa
ap_supplier_contacts asco,
hz_org_contacts hoc,
hz_parties hpc,
hz_parties hpr,
hz_contact_points hpcp,
apps.ak_web_user_sec_attr_values aw,
apps.fnd_user usr
WHERE hoc.party_relationship_id = hr.relationship_id
AND hr.subject_id = asu.party_id
AND hr.relationship_code = 'CONTACT'
AND hr.object_table_name = 'HZ_PARTIES'
--AND asu.vendor_id = assa.vendor_id
AND hr.object_id = hpc.party_id
AND hr.party_id = hpr.party_id
AND asco.relationship_id(+) = hoc.party_relationship_id
--AND assa.party_site_id (+) = asco.org_party_site_id
AND hpr.party_type = 'PARTY_RELATIONSHIP'
AND hpr.party_id = hpcp.owner_table_id
AND hpcp.owner_table_name = 'HZ_PARTIES'
AND NOT EXISTS (SELECT 1
FROM ap_supplier_contacts asco1
WHERE asco.vendor_contact_id = asco1.vendor_contact_id
AND asco.org_party_site_id IS NOT NULL)
--AND asu.segment1 = '10002'
AND aw.number_value(+) = hpc.party_id
AND usr.user_id(+) = aw.web_user_id
AND aw.attribute_code(+) = 'ICX_CUSTOMER_CONTACT_ID'
AND aw.attribute_application_id(+) = 178
AND SYSDATE <= nvl(hr.end_date, SYSDATE + 1))
:p_where_vnd_contact_list = 1
&p_where_active_inactive
ORDER BY g1_vendor_no