drop function if exists fw_core.fn_run_trx_rollback;
CREATE OR REPLACE FUNCTION fw_core.fn_run_trx_rollback(p_client_id int,p_function_id int,p_job_id int,p_step_id int)
   RETURNS void AS $$
   declare 
   f record;
   begin
	
	for f in select file_syspk,file_mnemonic
		from fw_core.fw_jobctl_file_runschedule where latest_runschedule_flag = '1' and end_status = 'error' 
		and end_status_note in ('stg2_failed','TRX_failed')
		order by file_syspk
	loop
	if  f.file_mnemonic = 'FTDRY' then
   	
		delete from transactional.test_instance where file_syspk =  f.file_syspk;
		delete from transactional.test_instance_engine_info where file_syspk = f.file_syspk;
		delete from transactional.test_instance_implement_info where file_syspk = f.file_syspk;
		delete from transactional.test_instance_tractor_info where file_syspk = f.file_syspk;
		delete from transactional.test_instance_trailer_info where file_syspk = f.file_syspk;
		delete from transactional.test_instance_tyre_info where file_syspk = f.file_syspk;
		delete from transactional.field_perf_summary where file_syspk = f.file_syspk;
		delete from transactional.field_perf_summary_implement_info  where file_syspk = f.file_syspk;
		delete from  transactional.field_perf_summary_tractor_info  where file_syspk = f.file_syspk;

   	elsif  f.file_mnemonic = 'FTWET' then
   		delete from transactional.test_instance where file_syspk = f.file_syspk;
		delete from transactional.test_instance_engine_info where file_syspk = f.file_syspk;
		delete from transactional.test_instance_implement_info where file_syspk = f.file_syspk;
		delete from transactional.test_instance_tractor_info where file_syspk = f.file_syspk;
		delete from transactional.test_instance_trailer_info where file_syspk = f.file_syspk;
		delete from transactional.test_instance_tyre_info where file_syspk = f.file_syspk;
		delete from transactional.field_perf_summary where file_syspk = f.file_syspk;
		delete from transactional.field_perf_summary_implement_info  where file_syspk = f.file_syspk;
		delete from  transactional.field_perf_summary_tractor_info  where file_syspk = f.file_syspk;
    elsif  f.file_mnemonic = 'FTHLG' then
   		delete from transactional.test_instance where file_syspk = f.file_syspk;
		delete from transactional.test_instance_engine_info where file_syspk = f.file_syspk;
		delete from transactional.test_instance_implement_info where file_syspk = f.file_syspk;
		delete from transactional.test_instance_tractor_info where file_syspk = f.file_syspk;
		delete from transactional.test_instance_trailer_info where file_syspk = f.file_syspk;
		delete from transactional.test_instance_tyre_info where file_syspk = f.file_syspk;
		delete from transactional.field_perf_summary where file_syspk = f.file_syspk;
		delete from transactional.field_perf_summary_implement_info  where file_syspk = f.file_syspk;
		delete from  transactional.field_perf_summary_tractor_info  where file_syspk = f.file_syspk;
		delete from transactional.field_perf_summary_trailer_info where file_syspk = f.file_syspk;
    elsif  f.file_mnemonic = 'DBSTD' then
		delete from transactional.test_instance where file_syspk = f.file_syspk;
		delete from transactional.test_instance_engine_info where file_syspk = f.file_syspk;
		delete from transactional.test_instance_tractor_info where file_syspk = f.file_syspk;
		delete from transactional.test_instance_drawbar_info where file_syspk = f.file_syspk;
		delete from transactional.test_instance_tyre_info where file_syspk = f.file_syspk;
		delete from transactional.drawbar_perf_results_summary where file_syspk = f.file_syspk;
		delete from transactional.drawbar_perf_results where file_syspk = f.file_syspk;
		delete from transactional.test_instance_equipment_info where file_syspk = f.file_syspk;
		delete from transactional.test_instance_measurement_uncertainty where file_syspk = f.file_syspk;
		delete from transactional.test_instance_tractor_info_misc where file_syspk = f.file_syspk;
		delete from transactional.test_instance_gear_speed_chart where file_syspk = f.file_syspk;
		delete from transactional.Test_instance_wheels_info where file_syspk = f.file_syspk;
		delete from transactional.Test_instance_fuel_lubricant_info where file_syspk = f.file_syspk;
		delete from transactional.drawbar_perf_observations where file_syspk = f.file_syspk;
    elsif  f.file_mnemonic = 'DBOECD' then
    	delete from transactional.test_instance where file_syspk = f.file_syspk;
		delete from transactional.test_instance_engine_info where file_syspk = f.file_syspk;
		delete from transactional.test_instance_tractor_info where file_syspk = f.file_syspk;
		delete from transactional.test_instance_drawbar_info where file_syspk = f.file_syspk;
		delete from transactional.test_instance_tyre_info where file_syspk = f.file_syspk;
		delete from transactional.drawbar_perf_results_summary where file_syspk = f.file_syspk;
		delete from transactional.drawbar_perf_results where file_syspk = f.file_syspk;
		delete from transactional.drawbar_perf_oecd_results where file_syspk = f.file_syspk;
		delete from transactional.test_instance_equipment_info where file_syspk = f.file_syspk;
		delete from transactional.test_instance_measurement_uncertainty where file_syspk = f.file_syspk;
		delete from transactional.test_instance_tractor_info_misc where file_syspk = f.file_syspk;
		delete from transactional.test_instance_gear_speed_chart where file_syspk = f.file_syspk;
		delete from transactional.Test_instance_wheels_info where file_syspk = f.file_syspk;
		delete from transactional.Test_instance_fuel_lubricant_info where file_syspk = f.file_syspk;
		delete from transactional.drawbar_perf_observations where file_syspk = f.file_syspk;
    elsif f.file_mnemonic = 'PTOBEN' then
    	delete from transactional.pto_engine_oil_consumption where file_syspk = f.file_syspk;
		delete from transactional.pto_key_perf_parameters where file_syspk = f.file_syspk;
		delete from transactional.pto_multipoint_mapping where file_syspk = f.file_syspk;
		delete from transactional.pto_perf_governing_graph where file_syspk = f.file_syspk;
		delete from transactional.pto_perf_results where file_syspk = f.file_syspk;
		delete from transactional.pto_smoke_test where file_syspk = f.file_syspk;
		delete from transactional.test_instance where file_syspk = f.file_syspk;
		delete from transactional.test_instance_tractor_info where file_syspk = f.file_syspk;
    elsif f.file_mnemonic = 'PTOSTD' then
    	delete from transactional.pto_engine_oil_consumption where file_syspk = f.file_syspk;
		delete from transactional.pto_key_perf_parameters where file_syspk = f.file_syspk;
		delete from transactional.pto_multipoint_mapping where file_syspk = f.file_syspk;
		delete from transactional.pto_perf_governing_graph where file_syspk = f.file_syspk;
		delete from transactional.pto_perf_results where file_syspk = f.file_syspk;
		delete from transactional.pto_smoke_test where file_syspk = f.file_syspk;
		delete from transactional.test_instance where file_syspk = f.file_syspk;
		delete from transactional.test_instance_tractor_info where file_syspk = f.file_syspk;
    elsif f.file_mnemonic = 'BUDNI' then
    	delete from  transactional.test_instance where file_syspk = f.file_syspk;
		delete from  transactional.test_instance_engine_info where file_syspk = f.file_syspk;
 		delete from transactional.test_instance_tractor_info where file_syspk = f.file_syspk;
		delete from transactional.budni_air_cleaner_perf_results where file_syspk = f.file_syspk;
		delete from transactional.budni_test_observations where file_syspk = f.file_syspk;
		delete from transactional.budni_brake_perf_parking_brake_test_results where file_syspk = f.file_syspk;
		delete from transactional.budni_brake_perf_service_brake_test_results where file_syspk = f.file_syspk;
		delete from transactional.budni_drawbar_perf_results  where file_syspk = f.file_syspk;
		delete from transactional.test_instance_implement_info where file_syspk = f.file_syspk;
		delete from transactional.budni_field_perf_results where file_syspk = f.file_syspk;
		delete from  transactional.test_instance_tractor_info_misc where file_syspk = f.file_syspk;
		delete from transactional.budni_hydraulic_maint_of_lift_load_results where file_syspk = f.file_syspk;
		delete from transactional.budni_hydraulic_power_test_results where file_syspk = f.file_syspk;
		delete from transactional.budni_hydraulic_lifting_capacity_test_results where file_syspk = f.file_syspk;
		delete from transactional.budni_centre_of_gravity_location  where file_syspk = f.file_syspk;
		delete from transactional.budni_noise_atmospheric_conditions where file_syspk = f.file_syspk;	
		delete from transactional.budni_noise_measurement_results where file_syspk = f.file_syspk;
		delete from transactional.budni_pto_perf_results_summary  where file_syspk = f.file_syspk;
		delete from transactional.budni_pto_perf_results_details  where file_syspk = f.file_syspk;
		delete from transactional.budni_vibration_measurement where file_syspk = f.file_syspk;
    elsif f.file_mnemonic = 'IHT' then
    	delete from transactional.test_instance where file_syspk = f.file_syspk;
		delete from transactional.test_instance_engine_info where file_syspk = f.file_syspk;
		delete from transactional.test_instance_tyre_info where file_syspk = f.file_syspk;
		delete from transactional.test_instance_atmospheric_info where file_syspk = f.file_syspk;
		delete from transactional.iht_noise_measurement_results where file_syspk = f.file_syspk;
		delete from transactional.test_instance_tractor_info where file_syspk = f.file_syspk;
		delete from transactional.test_instance_drawbar_info where file_syspk = f.file_syspk;
		delete from  transactional.test_instance_weight_reaction where file_syspk = f.file_syspk;
		delete from transactional.iht_brake_perf_test_condition  where file_syspk = f.file_syspk;
		delete from transactional.iht_brake_perf_test_results where file_syspk = f.file_syspk;
		delete from transactional.iht_cgm_lifting_angle_load_details where file_syspk = f.file_syspk;
		delete from transactional.iht_cgm_results where file_syspk = f.file_syspk;
		delete from transactional.iht_steering_effort where file_syspk = f.file_syspk;
		delete from transactional.iht_pedal_effort where file_syspk = f.file_syspk;
		delete from  transactional.iht_clutch_pedal_effort where file_syspk = f.file_syspk;
		delete from transactional.iht_brake_pedal_effort where file_syspk = f.file_syspk;
		delete from transactional.iht_gear_effort where file_syspk = f.file_syspk;
		delete from transactional.iht_hyd_lift_sensitivity where file_syspk = f.file_syspk;
		delete from transactional.iht_noise_measurement_test where file_syspk = f.file_syspk;
		delete from transactional.iht_gear_max_speed where file_syspk = f.file_syspk;
		delete from transactional.iht_speed_lead_lag_measurement where file_syspk = f.file_syspk;
		delete from transactional.iht_steering_effort_raw_data where file_syspk = f.file_syspk;
		delete from transactional.iht_hot_air_mapping_results where file_syspk = f.file_syspk;
	else
		delete from transactional.test_instance where file_syspk = f.file_syspk;
		delete from transactional.test_instance_engine_info where file_syspk = f.file_syspk;
		delete from transactional.test_instance_tyre_info where file_syspk = f.file_syspk;
		delete from transactional.test_instance_atmospheric_info where file_syspk = f.file_syspk;
		delete from transactional.iht_noise_measurement_results where file_syspk = f.file_syspk;
		delete from transactional.test_instance_tractor_info where file_syspk = f.file_syspk;
		delete from transactional.test_instance_drawbar_info where file_syspk = f.file_syspk;
		delete from  transactional.test_instance_weight_reaction where file_syspk = f.file_syspk;
		delete from transactional.iht_brake_perf_test_condition  where file_syspk = f.file_syspk;
		delete from transactional.iht_brake_perf_test_results where file_syspk = f.file_syspk;
		delete from transactional.iht_cgm_lifting_angle_load_details where file_syspk = f.file_syspk;
		delete from transactional.iht_cgm_results where file_syspk = f.file_syspk;
		delete from transactional.iht_steering_effort where file_syspk = f.file_syspk;
		delete from transactional.iht_pedal_effort where file_syspk = f.file_syspk;
		delete from  transactional.iht_clutch_pedal_effort where file_syspk = f.file_syspk;
		delete from transactional.iht_brake_pedal_effort where file_syspk = f.file_syspk;
		delete from transactional.iht_gear_effort where file_syspk = f.file_syspk;
		delete from transactional.iht_hyd_lift_sensitivity where file_syspk = f.file_syspk;
		delete from transactional.iht_noise_measurement_test where file_syspk = f.file_syspk;
		delete from transactional.iht_gear_max_speed where file_syspk = f.file_syspk;
		delete from transactional.iht_speed_lead_lag_measurement where file_syspk = f.file_syspk;
		delete from transactional.iht_steering_effort_raw_data where file_syspk = f.file_syspk;
		delete from transactional.iht_hot_air_mapping_results where file_syspk = f.file_syspk;
	end if;
end loop;

update fw_core.fw_jobctl_runschedule_jobstep set end_status_note = 'rollback completed', end_status='success', end_time=now() where job_id = p_job_id and step_id = p_step_id and latest_runschedule_flag = '1';

end;
$$  LANGUAGE plpgsql;