/* $Id$ */
/*============================================================================*
 |                 Copyright (c) 2017 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_SERVICE CONSTANT xxas_com_service_repository.service_name%TYPE
                       := 'JobAccrualPlans';
                       
BEGIN
      
  xxas_com_sr_pkg.set_rest_service
    (p_service_name       => l_SERVICE
    ,p_description        => 'Manage an assignment''s accrual plans' 
    ,p_path               => 'jobs/{assignmentId}/accrual-plans'    
    ,p_consumes           => 'application/xml'
    ,p_produces           => 'application/xml'
    );
 
   xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'GetAssignmentAccrualPlans'
    ,p_description        => 'Gets an assignment''s accrual plans'
    ,p_methods            => 'GET'
    ,p_remove_root_ele    => true
    ,p_sql                =>
      q'[
        SELECT XMLElement
                 ("data"
                 ,XMLAttributes
                   ('true' as "json:force-array"
                   ,'http://json.org/' as "xmlns:json")
                 ,XMLAgg
                   (XMLElement
                     ("plan"
                     ,XMLForest
                       (pap.accrual_plan_id "id"
                       ,pap.accrual_plan_name "value"
                       ,pap.accrual_plan_name "label")
                     )
                   )
                 ) "data"
        FROM   per_all_assignments_f rslt
              ,pay_element_entries_f pee
              ,pay_accrual_plans_v pap
        WHERE  rslt.assignment_id = {pathParam.assignmentId}
        AND    {authFunctionPredicate.XXAS_DT_ACCRUAL_PLAN_VIEW}
        AND    rslt.assignment_id = pee.assignment_id
        AND    nvl(to_date({requestParam.effectiveDate}, 'YYYY-MM-DD')
                  ,trunc(sysdate)) BETWEEN
               rslt.effective_start_date AND rslt.effective_end_date
        AND    nvl(to_date({requestParam.effectiveDate}, 'YYYY-MM-DD')
                  ,trunc(sysdate)) BETWEEN
               pee.effective_start_date AND pee.effective_end_date
        AND    pee.element_type_id = pap.accrual_plan_element_type_id
        ORDER BY pap.accrual_plan_name
        ]'
    ); 

   xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'GetAssignmentAbsTypeLinkedAccrualPlans'
    ,p_description        => 'Gets an assignment''s Absece type Linked Accrual Plans'
    ,p_methods            => 'GET'
    ,p_path               => 'absence-type/{absenceTypeId}'
    ,p_remove_root_ele    => true
    ,p_sql                =>
      q'[
        SELECT XMLElement
                 ("data"
                 ,XMLAttributes
                   ('true' as "json:force-array"
                   ,'http://json.org/' as "xmlns:json")
                 ,XMLAgg
                   (XMLElement
                     ("plan"
                     ,XMLForest
                       (pap.accrual_plan_id "id"
                       ,pap.accrual_plan_name "value"
                       ,pap.accrual_plan_name "label")
                     )
                   )
                 ) "data"
        FROM   per_all_assignments_f        rslt
              ,pay_element_entries_f        pee
              ,pay_accrual_plans_v          pap
              ,per_absence_attendance_types paat
        WHERE  rslt.assignment_id     = {pathParam.assignmentId}
        AND    {authFunctionPredicate.XXAS_DT_ACCRUAL_PLAN_VIEW}
        AND    rslt.assignment_id     = pee.assignment_id
        AND    fnd_date.canonical_to_date({requestParam.effectiveDate}) BETWEEN rslt.effective_start_date 
                                                                        AND     rslt.effective_end_date
        AND    fnd_date.canonical_to_date({requestParam.effectiveDate}) BETWEEN pee.effective_start_date 
                                                                        AND     pee.effective_end_date
        AND    pee.element_type_id    = pap.accrual_plan_element_type_id
        AND    pap.business_group_id  = paat.business_group_id
        AND    paat.absence_attendance_type_id = {pathParam.absenceTypeId}
        AND    pap.pto_input_value_id = paat.input_value_id
        AND    fnd_date.canonical_to_date({requestParam.effectiveDate}) BETWEEN paat.date_effective 
                                                                        AND     nvl(paat.date_end,fnd_date.canonical_to_date({requestParam.effectiveDate}))
        ORDER BY pap.accrual_plan_name
        ]'
    );

END;
/
COMMIT;
EXIT;
