/* $Id$ */
/*============================================================================*
 |                 Copyright (c) 2010 Applaud Solutions UK Ltd                |
 |                             All rights reserved                            |
 *===========================================================================*/

SET VERIFY OFF
SET FEEDBACK OFF
SET SCAN OFF
WHENEVER OSERROR  EXIT FAILURE ROLLBACK
WHENEVER SQLERROR EXIT FAILURE ROLLBACK

DECLARE

  --
  -- Local constants and variables
  --
  l_sql varchar2(32000);

BEGIN

  --
  -- Register Query Assignment DRs
  --
  l_sql :=
'       SELECT * from 
        (SELECT /*+ cardinality(hier 10)*/ 
               null group_name
              ,hier.primary_key_id assignment_id
              ,hier.person_id
              ,xxas_util_pkg.get_formatted_name
                                  (papf.full_name
                                  ,papf.global_name
                                  ,papf.local_name
                                  ,papf.first_name
                                  ,papf.last_name
                                  ,papf.known_as) short_name
			  ,xxas_util_pkg.get_initials(papf.first_name, papf.last_name, papf.known_as)	initials
			  ,fnd_date.date_to_canonical(img.last_update_date) image_lud
              ,xxas_per_image_pkg.url(hier.person_id, img.last_update_date, ''MEDIUM'') image_href
              ,papf.business_group_id
              ,xxas_com_asg_pkg.get_job_title(hier.primary_key_id, :pEffectiveDate) job_title
              ,xxas_com_asg_pkg.get_worker_type(hier.primary_key_id, :pEffectiveDate) worker_type
              ,xxas_com_asg_pkg.get_location(hier.primary_key_id, :pEffectiveDate) location
              ,xxas_com_asg_pkg.get_latitude(hier.primary_key_id, :pEffectiveDate) latitude
              ,xxas_com_asg_pkg.get_longtitude(hier.primary_key_id, :pEffectiveDate) longitude
              ,xxas_com_avail_pkg.get_free_busy(hier.primary_key_id) free_busy
              ,xxas_com_avail_pkg.get_color(hier.primary_key_id) avail_color
              ,xxas_com_avail_pkg.get_bg_color(hier.primary_key_id) bg_color
              ,xxas_com_avail_pkg.get_availability(hier.primary_key_id) avail
              ,xxas_com_avail_pkg.get_description(hier.primary_key_id) avail_desc
              ,xxas_com_avail_pkg.get_update_datetime(hier.primary_key_id) avail_update
              ,xxas_com_avail_pkg.get_icon(hier.primary_key_id) avail_icon
        FROM   TABLE(xxas_com_hier_pkg.org_chart_drs_tbl(''ASG'', ''ASG'', :pAssignmentId, :pEffectiveDate)) hier
              ,per_all_people_f papf
              ,xxas_per_thumbnails img
        WHERE  hier.person_id = papf.person_id(+)
        AND    hier.person_id = img.person_id(+)
        AND    :pEffectiveDate BETWEEN
               papf.effective_start_date(+) AND papf.effective_end_date(+)
        AND    hier.entity_code = ''ASG''
		AND    (:pAsgsFilterFlag = ''N'' OR NVL(xxas_com_sec_pkg.test_instance(''XXAS_DIR_REPORTS_ASGS_PERM'',hier.primary_key_id),''Y'') = ''Y'' ) 
        ORDER BY 1, papf.last_name, 4, 7)
        where rownum &lt;= :pRestrictDRsCount ';

  xxas_com_sr_pkg.seed_query_service
    (p_service_name      => 'QueryAssignmentDRs'
    ,p_securing_function => 'XXAS_SRV_Q_ASG_DRS_PERM'
    ,p_sql               => l_sql
    ,p_parameters        => xxas_com_sr_param_varray
                              (xxas_com_sr_param_type('pAssignmentId'      , xxas_com_sr_pkg.g_NUMBER)
                              ,xxas_com_sr_param_type('pEffectiveDate'     , xxas_com_sr_pkg.g_DATE)
							                ,xxas_com_sr_param_type('pAsgsFilterFlag'    , xxas_com_sr_pkg.g_VARCHAR2)
                              ,xxas_com_sr_param_type('pRestrictDRsCount'  , xxas_com_sr_pkg.g_NUMBER)
                              ));

END;
/
COMMIT;
EXIT;
