/* $Id: xxas_rest_timecards_upd.sql 5405 2018-04-11 11:15:35Z rsadhananthan $ */
/*============================================================================*
 |                 Copyright (c) 2015 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
                       := 'TimeCards';
                       
BEGIN
      
  xxas_com_sr_pkg.set_rest_service
    (p_service_name       => l_SERVICE
    ,p_description        => 'Time Options'
    ,p_path               => '/people/{personId}'
    ,p_consumes           => 'application/xml'
    ,p_produces           => 'application/xml'
    );
   
  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'GetTimeOptions'
    ,p_description        => 'Get a list of time options'
    ,p_methods            => 'GET'
		,p_path								=> '/time-options'
    ,p_remove_root_ele    => true
    ,p_sql                =>
      q'[
          SELECT XMLElement
                   ("data"
                   ,XMLAttributes
                     ('true' as "json:force-array"
                     ,'http://json.org/' as "xmlns:json")
										 ,XMLELEMENT("id",{pathParam.personId}) 
										 ,XMLELEMENT("self", xxas_com_rest_util_pkg.uri_from_path
												('/people/' || {pathParam.personId} || '/time-options/'))
										 ,XMLAgg
												(XMLElement
													("timeClock"
													--,XMLForest(
														,XMLElement
																("meta"
																,XMLELEMENT
																	("enabled"
																	,XMLELEMENT("desktop"
																							,decode(
																								xxas_com_sec_pkg.test_instance('XXAS_TCM_CICO',{pathParam.personId}),'Y',
																									decode(
																										xxas_com_sec_pkg.get_function_parameter('XXAS_TCM_CICO','pExcludeFromDesktop'),'N','true','false'
																									),
																									'false'
																								)
																							) 
																	,XMLELEMENT("phone"
																							,decode(
																								xxas_com_sec_pkg.test_instance('XXAS_TCM_CICO',{pathParam.personId}),'Y',
																									decode(
																										xxas_com_sec_pkg.get_function_parameter('XXAS_TCM_CICO','pExcludeFromPhone'),'N','true','false'
																									),
																									'false'
																								)
																							)
																	)
																,XMLELEMENT
																	("tips"
																	,XMLELEMENT
																		("tip"
																		,XMLELEMENT
																			("code"
																			,'XXAS_RB_TCM_CICO_NOGPS')
																		,XMLELEMENT
																			("text"
																			,xxas_com_msg_pkg.get_string('XXAS_RB_TCM_CICO_NOGPS'))
																		)
																	,XMLELEMENT
																		("tip"
																		,XMLELEMENT
																			("code"
																			,'XXAS_RB_TCM_CICO_PERIM')
																		,XMLELEMENT
																			("text"
																			,xxas_com_msg_pkg.get_string('XXAS_RB_TCM_CICO_PERIM'))
																		)
																	,XMLELEMENT
																		("tip"
																		,XMLELEMENT
																			("code"
																			,'XXAS_RB_TCM_CICO_TIP')
																		,XMLELEMENT
																			("text"
																			,xxas_com_msg_pkg.get_string('XXAS_RB_TCM_CICO_TIP'))
																		)
																	)
																,XMLELEMENT
																	("saveSearches"
																	,decode(
																			xxas_com_sec_pkg.test_instance('XXAS_TCM_SAVED_SEARCH',{pathParam.personId}),'Y','true','false')
																	)
																,XMLELEMENT
																	("actions"
																	,XMLELEMENT
																		("action"
																		,XMLELEMENT
																			("code"
																			,'IN')
																		,XMLELEMENT
																			("href"
																			,xxas_com_rest_util_pkg.uri_from_path
																				('/people/' || {pathParam.personId} || '/time-clock/')
																			)
																		,XMLELEMENT
																			("name"
																			,'Clock In')
																		)
																	,XMLELEMENT
																		("action"
																		,XMLELEMENT
																			("code"
																			,'OUT')
																		,XMLELEMENT
																			("href"
																			,xxas_com_rest_util_pkg.uri_from_path
																				('/people/' || {pathParam.personId} || '/time-clock/')
																			)
																		,XMLELEMENT
																			("name"
																			,'Clock Out')
																		)
																	,XMLELEMENT
																		("action"
																		,XMLELEMENT
																			("code"
																			,'UNDO')
																		,XMLELEMENT
																			("href"
																			,xxas_com_rest_util_pkg.uri_from_path
																				('/people/' || {pathParam.personId} || '/time-clock/')
																			)
																		,XMLELEMENT
																			("name"
																			,'Undo')
																		,XMLELEMENT
																			("timeToLive"
																			,'180') -- in seconds --
																		)
																	,XMLELEMENT
																		("action"
																		,XMLELEMENT
																			("code"
																			,'DELETE')
																		,XMLELEMENT
																			("href"
																			,xxas_com_rest_util_pkg.uri_from_path
																				('/people/' || {pathParam.personId} || '/time-clock/')
																			)
																		,XMLELEMENT
																			("name"
																			,'Delete')
																		)
																	)
																,XMLELEMENT
																	("geofencePolicy"
																	,XMLELEMENT
																		("desktop"
																		,'NONE')
																	,XMLELEMENT
																		("mobile"
																		,decode(
																					xxas_com_sec_pkg.test('XXAS_TCM_CICO_OPEN'),'Y','NONE',
																						decode(
																							xxas_com_sec_pkg.test('XXAS_TCM_CICO_REQUIREGPS'),'Y','REQUIREGPS','ENFORCEFENCE'
																							)
																			)
																		) 
																	)
															)			
															,XMLELEMENT
																("lastPunch"
																,xxas_tcm_log_entry_pkg.get_last_punch({pathParam.personId})
																)
															,XMLELEMENT
																("jobs"
																,xxas_tcm_log_entry_pkg.get_job_details({pathParam.personId})
																)
														--) as "meta"
													)
												
											)
				
					) as "data"
				FROM dual
				]'
		);
  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'Clock'
    ,p_description        => 'Records the Clock Action'
    ,p_methods            => 'POST'
    ,p_path               => '/time-clock'
    ,p_plsql              =>
      q'[
				  DECLARE
          
         
          l_resp       		xxas_http_resp_obj;
          l_num NUMBER;
          
        BEGIN
				
					IF NOT xxas_com_sec_pkg.test_instance_rtn_boolean
            ('XXAS_TCM_CICO', {pathParam.personId}) THEN
            l_resp := xxas_http_response
                        .status(xxas_http_resp_status_pkg.unauthorised)
                        .build();
					ELSE
						l_num:=xxas_tcm_log_entry_pkg.clock_action
							(p_action						=> {requestParam.action}
							,p_person_id				=> {pathParam.personId}
							,p_assignment_id		=> {requestParam.jobId}
							,p_element_type_id	=> {requestParam.paymentTypeId}
							,p_latlong					=> {requestParam.location}
							,p_remote_address		=> {headerParam.REMOTE_ADDR}
							);
							
						l_resp := xxas_com_rest_mgr_pkg.get_xml
											 ('people/' || {pathParam.personId} || '/time-options/');
											 
						{response} := l_resp;
					END IF;
				END
			]'
		);
    
xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'GetTimePaymentTypes'
    ,p_description        => 'Get a list of time payment types'
    ,p_methods            => 'GET'
		,p_path								=> '/time-payment-types/{assignmentId}'
    ,p_remove_root_ele    => true
    ,p_sql                =>
      q'[
           SELECT XMLElement
                 ("data"
                 ,XMLAttributes
                   ('true' as "json:force-array"
                   ,'http://json.org/' as "xmlns:json")
                 ,(SELECT XMLAGG(
                                      XMLELEMENT("payment",
                                        XMLFOREST(rslt.element_type_id as "id",
                                                  xxas_tcm_config_pkg.get_element_name(rslt.element_type_id)as "label"
                                        )
                                      )
																		ORDER BY nvl(to_number(eit.eei_information18), 999)	
                                    )
                              FROM  TABLE(xxas_com_elements_pkg.links_tbl
                                ({pathParam.assignmentId} , trunc(sysdate))) rslt
                              ,pay_element_type_extra_info eit
                              WHERE  eit.element_type_id = rslt.element_type_id
                              AND    eit.information_type = 'XXAS_TCM_ENTRY_MAP'
                              AND    eit.eei_information1 IS NOT NULL
                              AND    eit.eei_information1 = 'Y'
                              AND    eit.eei_information20 = 'Y'
                              AND NVL ( eit.eei_information21, 'N' ) = 'N'
                          )
                 ) "data" FROM DUAL
                 WHERE xxas_com_sec_pkg.test_instance('XXAS_CICO_PERMISSION',{pathParam.personId}) = 'Y'
				]'
		);
    
  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'GetTimePaymentTypesForOTL'
    ,p_description        => 'Get a list of time payment types for OTL'
    ,p_methods            => 'GET'
		,p_path								=> '/time-payment-types-otl/{assignmentId}'
    ,p_remove_root_ele    => true
    ,p_sql                =>
      q'[
           SELECT XMLElement
                 ("data"
                 ,XMLAttributes
                   ('true' as "json:force-array"
                   ,'http://json.org/' as "xmlns:json")
                 ,(SELECT XMLAGG(
                                      XMLELEMENT("payment",
                                        XMLFOREST(rslt.element_type_id as "id",
                                                  xxas_tcm_config_pkg.get_element_name(rslt.element_type_id)as "label"
                                        )
                                      )
                                    )
                              FROM TABLE(xxas_com_elements_pkg.links_tbl({pathParam.assignmentId}, trunc(sysdate))) rslt
                          ,TABLE(xxas_util_pkg.string_to_varchar2tbl(fnd_profile.value('XXAS_OTL_TIME_CLOCK_PAY_TYPES'))) ele
                      WHERE rslt.element_name = ele.column_value
                          )
                 ) "data" FROM DUAL
                 WHERE xxas_com_sec_pkg.test_instance('XXAS_CICO_PERMISSION',{pathParam.personId}) = 'Y'
				]'
		);
    
  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'ClockDelete'
    ,p_description        => 'Deletes the Clock Action'
    ,p_methods            => 'DELETE'
    ,p_path               => '/time-clock-delete/{pendingClockId}'
    ,p_plsql              =>
      q'[
				  DECLARE         
            l_resp       		xxas_http_resp_obj;
          BEGIN
				
					IF NOT xxas_com_sec_pkg.test_instance_rtn_boolean
            ('XXAS_CICO_PERMISSION', {pathParam.personId}) THEN
            l_resp := xxas_http_response
                        .status(xxas_http_resp_status_pkg.unauthorised)
                        .build();
					ELSE
						xxas_tcm_log_entry_pkg.delete_pending_clock
							(p_pending_clock_id				=> {pathParam.pendingClockId}
							);
							
						l_resp := xxas_http_response
                      .ok(xxas_com_msg_pkg.get_success_message_xml
                           (xxas_com_msg_type
                         (xxas_util_pkg.app, 'XXAS_COM_CHNG_SAVE', null)
                       ,'self')
                         )
                      .build();
											 
						{response} := l_resp;
					END IF;
				END
			]'
		);

  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'MissedOutClockDelete'
    ,p_description        => 'Deletes the Missed out Clock Action'
    ,p_methods            => 'DELETE'
    ,p_path               => '/time-clock-delete/missed-out/{pendingClockId}/last-punch-type/{lastPunchType}/person/{personId}'
    ,p_plsql              =>
      q'[
         DECLARE         
           l_resp     xxas_http_resp_obj;
         BEGIN

           IF NOT xxas_com_sec_pkg.test_instance_rtn_boolean
                     ('XXAS_CICO_PERMISSION', {pathParam.personId}) THEN
             l_resp := xxas_http_response
                         .status(xxas_http_resp_status_pkg.unauthorised)
                         .build();
           ELSE
             xxas_tcm_log_entry_pkg.delete_missed_out_clock
               (p_pending_clock_id => {pathParam.pendingClockId}
               ,p_last_punch_type  => {pathParam.lastPunchType});

             l_resp := xxas_http_response
                         .ok(xxas_com_msg_pkg.get_success_message_xml
                               (xxas_com_msg_type
                                  (xxas_util_pkg.app
                                  ,'XXAS_COM_CHNG_SAVE'
                                  ,null
                                  )
                               ,'self')
                            )
                         .build();

             {response} := l_resp;
					 END IF;

				 END
			]'
		);

  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'ClockOutDelete'
    ,p_description        => 'Deletes the Clock Out Action'
    ,p_methods            => 'DELETE'
    ,p_path               => '/time-clock-delete/clock-out/{pendingClockId}/last-punch-type/{lastPunchType}/log-entry-id/{logEntryId}/person/{personId}'
    ,p_plsql              =>
      q'[
         DECLARE         
           l_resp     xxas_http_resp_obj;
         BEGIN

           IF NOT xxas_com_sec_pkg.test_instance_rtn_boolean
                     ('XXAS_CICO_PERMISSION', {pathParam.personId}) THEN
             l_resp := xxas_http_response
                         .status(xxas_http_resp_status_pkg.unauthorised)
                         .build();
           ELSE
             xxas_tcm_log_entry_pkg.delete_clock_out_action
               (p_pending_clock_id => {pathParam.pendingClockId}
               ,p_last_punch_type  => {pathParam.lastPunchType}
               ,p_log_entry_id     => {pathParam.logEntryId}
               ,p_person_id        => {pathParam.personId});

             l_resp := xxas_http_response
                         .ok(xxas_com_msg_pkg.get_success_message_xml
                               (xxas_com_msg_type
                                  (xxas_util_pkg.app
                                  ,'XXAS_COM_CHNG_SAVE'
                                  ,null
                                  )
                               ,'self')
                            )
                         .build();

             {response} := l_resp;
					 END IF;

				 END
			]'
		);

END;
/
COMMIT;
EXIT;		