/* $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 Walk-up
  --
  l_sql :=
'        SELECT /*+ cardinality(hier 5)*/ 
               hier.entity_code
              ,hier.primary_key_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_per_image_pkg.url(hier.person_id, img.last_update_date, ''MEDIUM'') image_href
              ,hier.person_id
              ,hier.parent_entity_code
              ,hier.parent_primary_key_id
              ,papf.business_group_id
              ,hier.hier_level
              ,hier.dr_count
              ,hier.total_count
              ,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_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_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_tbl(:pChartType, :pEntityCode, :pPrimaryKeyId, :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''
        ORDER BY 9, 3, 12 ';

  xxas_com_sr_pkg.seed_query_service
    (p_service_name      => 'QueryAssignmentWalkUp'
    ,p_securing_function => 'XXAS_SRV_Q_ASG_WALKUP_PERM'
    ,p_sql               => l_sql
    ,p_parameters        => xxas_com_sr_param_varray
                              (xxas_com_sr_param_type('pChartType'         , xxas_com_sr_pkg.g_VARCHAR2)
                              ,xxas_com_sr_param_type('pEntityCode'        , xxas_com_sr_pkg.g_VARCHAR2)
                              ,xxas_com_sr_param_type('pPrimaryKeyId'      , xxas_com_sr_pkg.g_NUMBER)
                              ,xxas_com_sr_param_type('pEffectiveDate'     , xxas_com_sr_pkg.g_DATE)));

END;
/
COMMIT;
EXIT;
