/* $Id$ */
/*============================================================================*
 |                 Copyright (c) 2016 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
                       := 'AbsenceEvent';

BEGIN

  xxas_com_sr_pkg.set_rest_service
    (p_service_name       => l_SERVICE
    ,p_description        => 'AbsenceEvent'
    ,p_consumes           => 'application/xml'
    ,p_produces           => 'application/xml'
    ,p_path               => 'absence-event');

  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'GetAbsenceEventReasons'
    ,p_description        => 'Get the Absence Event Reasons'
    ,p_path               => 'reasons'
    ,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
                   ("AbsenceEventReason"
                   ,XMLForest
                     (rslt.abs_attendance_reason_id "id"
                     ,rslt.meaning                  "value"
                     ,rslt.meaning                  "label")
                   )
                 )
               ) "data"
         FROM  (SELECT DISTINCT paar.abs_attendance_reason_id
                               ,hl.meaning
                FROM   hr_lookups hl
                      ,per_abs_attendance_reasons paar
                      ,per_all_people_f papf
                WHERE hl.lookup_type                  = 'ABSENCE_REASON'
                AND   paar.business_group_id          = papf.business_group_id
                AND   papf.person_id                  = {requestParam.personId}
                AND   trunc(sysdate) BETWEEN papf.effective_start_date
                                     AND     papf.effective_end_date
                AND   paar.name                       = hl.lookup_code
                AND   paar.absence_attendance_type_id = {requestParam.absAttTypeId}
                AND   trunc(sysdate) BETWEEN nvl(hl.start_date_active,trunc(sysdate))
                                     AND     nvl(hl.end_date_active,trunc(sysdate))) rslt

      ]'
    );

  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'AbsenceEntryForm'
    ,p_description        => 'Get Absence Entry Form'
    ,p_methods            => 'GET'
    ,p_remove_root_ele    => true
    ,p_path               => 'entry-form'
    ,p_sql                =>
      q'[
          SELECT xxas_absence_event_pkg.entry_form
                   ({requestParam.personId}
                   ,{requestParam.startTime}
                   ,{requestParam.endTime}
                   ,{requestParam.sourceBlock}
                   ,{requestParam.savedSearchId}
                   ,{requestParam.managerId}
                   ,{requestParam.defAbsTypeFlag}) "data"
          FROM   dual
      ]'
    );

  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'AbsenceEntryView'
    ,p_description        => 'Get Absence Entry View'
    ,p_methods            => 'GET'
    ,p_remove_root_ele    => true
    ,p_path               => 'entry-view/{personId}/abs/{absAttendanceId}/transaction/{transAndStepId}'
    ,p_sql                =>
      q'[
          SELECT xxas_absence_event_pkg.entry_view
                   ({pathParam.personId}
                   ,{pathParam.absAttendanceId}
                   ,{pathParam.transAndStepId}
                   ,{requestParam.sourceBlock}
                   ,{requestParam.savedSearchId}
                   ,{requestParam.managerId}) "data"
          FROM   dual
      ]'
    );

  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'AbsenceEntryEdit'
    ,p_description        => 'Get Absence Entry Edit'
    ,p_methods            => 'GET'
    ,p_remove_root_ele    => true
    ,p_path               => 'entry-edit/{personId}/abs/{absAttendanceId}/transaction/{transAndStepId}'
    ,p_sql                =>
      q'[
          SELECT xxas_absence_event_pkg.entry_edit
                   ({pathParam.personId}
                   ,{pathParam.absAttendanceId}
                   ,{pathParam.transAndStepId}
                   ,{requestParam.times}
                   ,{requestParam.sourceBlock}
                   ,{requestParam.savedSearchId}
                   ,{requestParam.managerId}) "data"
          FROM   dual
      ]'
    );

  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'AbsenceEntryReview'
    ,p_description        => 'Get Absence Entry Review'
    ,p_methods            => 'GET'
    ,p_remove_root_ele    => true
    ,p_path               => 'entry-review/{personId}/abs/{absAttendanceId}/transaction/{transAndStepId}'
    ,p_sql                =>
      q'[
          SELECT xxas_absence_event_pkg.entry_review
                   ({pathParam.personId}
                   ,{pathParam.absAttendanceId}
                   ,{pathParam.transAndStepId}
                   ,{requestParam.sourceBlock}
                   ,{requestParam.savedSearchId}
                   ,{requestParam.managerId}) "data"
          FROM   dual
      ]'
    );

  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'DeleteAbsenceEntry'
    ,p_description        => 'Deletes a Absence Entry'
    ,p_path               => 'entry/{personId}/abs/{absAttendanceId}'
    ,p_methods            => 'DELETE'
    ,p_plsql              =>
      q'[
        DECLARE
          l_resp          xxas_http_resp_obj;
          l_response_xml  XMLType;
          l_function_id   number;
          l_status              varchar2(10);
          l_transaction_id      number;
          l_transaction_step_id number;
          l_test varchar2(1000);
          l_test_value varchar2(10);
          l_abs_attendance_id number;
          l_person_id  number;
          
          cursor csr_get_transaction
          is
          select status
                ,transaction_id 
          from hr_api_transactions
          where TRANSACTION_REF_ID = l_abs_attendance_id
          and transaction_ref_table = 'PER_ABSENCE_ATTENDANCES'
          order by transaction_id desc;
		  
		      cursor csr_get_trans_step(p_trans_id IN NUMBER)
		      is
  		    select transaction_step_id
          from hr_api_transaction_steps
          where transaction_id = p_trans_id
          and api_name = 'HR_PERSON_ABSENCE_SWI.PROCESS_API';

        BEGIN

          -- Check access to delete the absence entry

          l_abs_attendance_id := {pathParam.absAttendanceId};
          l_person_id := {pathParam.personId};
          
          open csr_get_transaction;
          fetch csr_get_transaction into l_status, l_transaction_id;
          close csr_get_transaction;
          
          IF l_status = 'RI' OR l_status = 'RO' THEN

             open csr_get_trans_step(l_transaction_id);
      			 fetch csr_get_trans_step into l_transaction_step_id;
			       close csr_get_trans_step;

             IF xxas_com_sec_pkg.test_instance('XXAS_DT_ABSENCE_DELETE', to_char(l_person_id), to_char(l_abs_attendance_id), to_char(l_transaction_id), to_char(l_transaction_step_id)) = 'Y' THEN

                l_function_id  := xxas_cal_util_pkg.page_slug_func_id('absence-edit');
                l_response_xml := xxas_absence_event_pkg.delete_rfc_absence_event
                                  (l_abs_attendance_id
                                  ,l_function_id
                                  ,replace({requestParam.sourceBlock},'_','$')
                                  ,{requestParam.savedSearchId}
                                  ,l_person_id
                                  ,{requestParam.managerId}
                                  ,l_transaction_id
                                  ,l_transaction_step_id);
              END IF;
          ELSE
              IF xxas_com_sec_pkg.test_instance('XXAS_DT_ABSENCE_DELETE',to_char(l_person_id),to_char(l_abs_attendance_id)) = 'Y' THEN
                 l_function_id  := xxas_cal_util_pkg.page_slug_func_id('absence-edit');
                 l_response_xml := xxas_absence_event_pkg.delete_absence_event
                                   (l_abs_attendance_id
                                   ,l_function_id
                                   ,replace({requestParam.sourceBlock},'_','$')
                                   ,{requestParam.savedSearchId}
                                   ,null
                                   ,{requestParam.managerId});

              END IF;
          END IF; 

          l_resp := xxas_http_response
                      .ok(l_response_xml)
                      .build();

          {response} := l_resp;
          
         END
      ]'
    );

END;
/
COMMIT;
EXIT;
