/* $Id$ */
/*============================================================================*
 |                 Copyright (c) 2018 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
                       := 'P60Reports';
                       
BEGIN
      
  xxas_com_sr_pkg.set_rest_service
    (p_service_name       => l_SERVICE
    ,p_description        => 'View P60 reports'
    ,p_path               => 'assignments/{assignmentId}/'
    ,p_consumes           => 'application/xml'
    ,p_produces           => 'application/xml'
    );
    
  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'ViewP60reports'
    ,p_description        => 'Gets all p60 reports for an assignment'
    ,p_path               => 'p60reports'
    ,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
                       ("item"
                       ,XMLForest
                         ('pdf' "documentType"
                         ,p.summary_text "title"
                         ,xxas_gb_reports_pkg.p60report_url
                           ({pathParam.assignmentId}
                           ,p.p60_report_id
                           ,p.template
                           ,'Y'
                           ,p.rest_api_path
                           ,p.report_type) "url"
                         )
                        ,XMLElement
                         ("meta" 
                         ,XMLElement
                           ("options"
                           ,XMLElement
                           ("height"
                           ,'30em')
                           )
                         )
                       )
                     )
                   ) "data"
          FROM  TABLE(xxas_gb_reports_pkg.get_p60reports_tbl
                       ({pathParam.assignmentId})) p
          WHERE {pathParam.assignmentId} is not null
          AND   xxas_com_sec_pkg.test_instance
                  ('XXAS_DT_GB_P60_REPORT'
                  ,to_number({pathParam.assignmentId})
                  ,null
                  ,null
                  ,null
                  ,null) = 'Y'
      ]'
    );
    
  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'ViewP60report'
    ,p_description        => 'Get view details of P60 Report'
    ,p_path               => 'p60reports/{p60reportId}/{templateName}'
    ,p_methods            => 'GET'
    ,p_remove_root_ele    => true
    ,p_sql                =>
      q'[
          SELECT xxas_gb_reports_pkg.get_p60_report_details_xml
                   ({pathParam.assignmentId}
                   ,{pathParam.p60reportId}
                   ,{pathParam.templateName}
                   ,{requestParam.reportType})  "data"
           FROM dual
           WHERE {pathParam.assignmentId} is not null
           AND   xxas_com_sec_pkg.test_instance
                   ('XXAS_DT_GB_P60_REPORT'
                   ,to_number({pathParam.assignmentId})
                   ,null
                   ,null
                   ,null
                   ,null) = 'Y'
      ]'
    );

END;
/
COMMIT;
EXIT;
