/* $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
                       := 'W2Reports';
                       
BEGIN
      
  xxas_com_sr_pkg.set_rest_service
    (p_service_name       => l_SERVICE
    ,p_description        => 'View W-2 reports'
    ,p_path               => 'w2'
    ,p_consumes           => 'application/xml'
    ,p_produces           => 'application/xml'
    );
    
  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'ViewW2reports'
    ,p_description        => 'Gets all W-2 reports for a person'
    ,p_path               => '{personId}'
    ,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.title "title"
                         ,xxas_us_reports_pkg.getW2URL
                           (p.assignment_action_id
                           ,'Y'
						               ,'PAYUSW2'
						               ,p.template_code
						               ,p.tax_unit_id
						               ,p.year
						               ,p.prev_action_id
						               ,p.report_type
						               ) "url"
                         )
                        ,XMLElement
                         ("meta" 
                         ,XMLElement
                           ("options"
                           ,XMLElement
                           ("height"
                           ,'30em')
                           )
                         )
                       )
                     )
                   ) "data"
          FROM  TABLE(xxas_us_reports_pkg.get_w2reports_tbl
                       ({pathParam.personId})) p
          WHERE {pathParam.personId} is not null
		  AND   xxas_com_sec_pkg.test_instance('XXAS_W2_PERM'
		                                      ,{pathParam.PersonId}) = 'Y'
          
      ]'
    );
    
  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'ViewW2reportdetail'
    ,p_description        => 'Get view details of W2 Report'
    ,p_path               => '{assignmentActionId}/{dataSourceCode}'
    ,p_methods            => 'GET'
    ,p_remove_root_ele    => true
    ,p_sql                =>
      q'[
          SELECT xxas_us_reports_pkg.get_w2_report_details_xml
		           ({pathParam.assignmentActionId}
				   ,{requestParam.taxUnitId}
				   ,{requestParam.year}
				   ,{requestParam.prevActionId}
				   ,{requestParam.reportType}) "data"
          FROM   dual
          WHERE  {requestParam.actionContextId} IS NULL		  
        ]'
    );
 
END;
/
COMMIT;
EXIT;
