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

BEGIN

  xxas_com_sr_pkg.set_rest_service
    (p_service_name       => l_SERVICE
    ,p_description        => 'View and transact vacancies'
	,p_path               => '/vac/{vacancyId}'
    ,p_consumes           => 'application/xml'
    ,p_produces           => 'application/xml'
    );
    
  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'GetVacancyOffers'
    ,p_description        => 'Get a list of offers for a vacancy'
    ,p_path               => 'offersAccepted'
    ,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 ('people' "type",
								   v.vacancy_id "id",
								   v.person_name "name",
								   (CASE WHEN v.proposed_start_date is null THEN
								   'Offer accepted'
								   ELSE
								   'Offer accepted, proposed start ' || to_char(v.proposed_start_date,'DD/MM/YYYY')
								   END )"title",
								   xxas_com_rest_util_pkg.uri_from_app_route
										('/applicant-detail/' || v.assignment_id || '_' || {pathParam.vacancyId} || '?' ||
											xxas_rslt_cache_pkg.get_offer_details_block_id('applicant-detail') ) "href",
								    v.vacancy_id "ObjectId",
								   'modal' "target",
								   'large' "modalWidth"
								   ,XMLElement
									("image"
									,XMLElement
									("self"
										,xxas_com_rest_util_pkg.uri_from_path
										(v.image_href))
									,CASE WHEN v.image_href IS NULL THEN
									XMLForest
									(xxas_util_pkg.get_initials(v.first_name, v.last_name, v.known_as) "initials"
									)
									END
									) "linked"
						          )	 
							   )
					ORDER BY v.last_name, v.person_name	
				) 
				)  "data" 
		FROM  (select va.vacancy_id, va.person_name, vo.person_id ,vo.status_meaning , vo.proposed_start_date , va.image_href, va.first_name, va.last_name, va.known_as, va.assignment_id
			    FROM   xxas_vacancy_applicants_v va
					 ,xxas_vacancy_offers_v vo
				WHERE  va.vacancy_id = {pathParam.vacancyId}
				AND    vo.vacancy_id = {pathParam.vacancyId}
				AND    vo.applicant_assignment_id = va.assignment_id
				AND    xxas_com_sec_pkg.test_instance('XXAS_DATA_VAC_VIEW', va.vacancy_id) = 'Y') v
      ]'
    );

	xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'GetVacancyOffersAccepted'
    ,p_description        => 'Get a list of offers for a vacancy'
    ,p_path               => 'offersReady'
    ,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 ('people' "type",
								   v.vacancy_id "id",
								   v.person_name "name",
								   'Offer ready' "title",
								   xxas_com_rest_util_pkg.uri_from_app_route
										('/applicant-detail/' || v.assignment_id || '_' || {pathParam.vacancyId} )  "href",
								    v.vacancy_id "ObjectId",
								   'modal' "target",
								   'large' "modalWidth"
								   )
						,XMLELEMENT("linked"
							,XMLElement
								("actions"
         						 ,XMLAttributes
        						   ('true' as "json:force-array"
         						    ,'http://json.org/' as "xmlns:json")
        						  ,XMLElement
         						   ("action"
       							     ,XMLForest
        						      ('offer' "id"
         						       ,'Offer' "title"
         							   ,'false'   "bulkActionEnabled"
          							   ,'POST' "operation"
									   ,xxas_com_rest_util_pkg.uri_from_path(
						                  'vacancies/' || v.vacancy_id || '/offers/create?applicantId=' || v.assignment_id
										  ) "href"
									   )
         							   ,XMLElement
          							    ("meta"
          							    ,XMLElement
                							("options"
               							 ,XMLForest
             							     ('balanced' "color") )
             							 )
									)
        						)
							,XMLElement
								("image"
									,XMLElement
									("self"
										,xxas_com_rest_util_pkg.uri_from_path
										(v.image_href))
									,CASE WHEN v.image_href IS NULL THEN
									XMLForest
									(xxas_util_pkg.get_initials(v.first_name, v.last_name, v.known_as) "initials"
									)
									END
								)
							)"linked"	 
							   )
					ORDER BY v.last_name, v.person_name	
				) 
				)  "data" 
		FROM  (select va.vacancy_id, va.person_name,va.person_id,va.image_href, va.first_name, va.last_name, va.known_as, va.assignment_id
			    FROM   xxas_vacancy_applicants_v va
				WHERE  va.vacancy_id = {pathParam.vacancyId}
				AND    xxas_com_sec_pkg.test_instance('XXAS_DATA_VAC_VIEW', va.vacancy_id) = 'Y'
				AND    decode(va.has_offer, 'N'
                         ,decode(xxas_com_sec_pkg.person_on_rec_team_edit_flag(va.vacancy_id,'Y','Y'),'Y',
                                 xxas_com_sec_pkg.test_instance('XXAS_APL_CREATE_OFFER_PERM', va.assignment_status_type_id),'N')
                          ) = 'Y') v
      ]'
    );
    
END;
/
COMMIT;
EXIT;
