--drop function if exists fw_ods.fn_fw_ods_load();

CREATE OR REPLACE FUNCTION fw_ods.fn_fw_ods_load(p_client_id int,p_function_id int,p_job_id int,p_step_id int)
 RETURNS void AS $$
declare 
	f record;
begin
-- Adding data to file fw_ods_field:

for f in select distinct file_syspk, file_mnemonic from fw_core.fw_jobctl_file_runschedule 
	where latest_runschedule_flag = '1' and end_status = 'success'  and end_status_note = 'TRX_completed'
loop

if f.file_mnemonic = 'FTDRY'
then 
	insert into fw_ods.fw_ods
		(
		-- File Identifier: Test_Mnemonic, Sheet_Mnemonic, file_syspk(id), test_instance_id, test_file_name,
		client_id,
		function_id,
		test_file_mnemonic,
		test_sheet_mnemonic,
		test_file_id,
		test_file_name,
		test_instance_id,
		test_iteration_number,
		-- Tractor Identifier: tractor make,tractor model,tractor engine hp,wheel drive type,configuration,Test insance tractor id
		tractor_make,
		tractor_model,
		mahindra_model_yn,
		test_tractor_yn,
		tractor_engine_hp,
		wheel_drive_type,
		configuration,
		test_instance_tractor_id,
		-- Test Season:
		season,
		--test specific: date & test dimensions
		test_date,
		test_date_year,
		test_date_quarter,
		test_date_month,
		test_date_dayofmonth,
		test_date_dayofweek,
		--test specific: condition
		test_condition,
		--test specific: location
		location_name,
		----test specific: Implement details(name, type and Weight_kg)
		name_of_implement,
		type_of_implement,
		implement_weight_kg,
		-- test specific measures: Speed and gear
		engine_speed_var,
		gear_used,
		-- test specific measures: fuel, area, speed,wheel-slip, productivity,rpm and depth-of-operation  
		fuel_consumption_lit_per_hr,
		fuel_consumption_lit_per_acr,
		area_covered_acr_per_hr,
		speed_kmph,
		wheel_slippage_pct,
		field_efficiency_pct,
		productivity,
		erpmdrop_straight_1stpass_low,
		erpmdrop_straight_1stpass_high,
		depth_of_cut_cm_low,
		depth_of_cut_cm_high,
		created_by,
		updated_by, 
		create_timestamp,
		update_timestamp 
		)
		select 
		-- File Identifier: Test_Mnemonic, Sheet_Mnemonic, file_syspk(id), test_instance_id, test_file_name,
		a.client_id, 
		a.function_id, 
		a.file_mnemonic,
		a.file_sheet_mnemonic,
		a.file_syspk,
		a.test_file_name,
		a.syspk,
		c.test_iteration_number,
		-- Tractor Identifier: tractor make,tractor model,tractor engine hp,wheel drive type,configuration,Test insance tractor id
		b.tractor_make,
		b.tractor_model,
		b.mahindra_model_yn,
		b.test_tractor_yn,
		b.tractor_engine_hp,
		b.wheel_drive_type,
		b.configuration,
		b.syspk,
		-- Test Season:
		a.season,
		--test specific: date & test dimensions
		a.date_of_test,
		extract( year from a.date_of_test),
		extract( quarter from a.date_of_test),
		extract( month from a.date_of_test),
		extract( day from a.date_of_test),
		extract( dow from a.date_of_test),
		--test specific: condition
		c.test_condition,
		--test specific: location
		a.test_location_name,
		----test specific: Implement details(name, type and Weight_kg)
		d.name_of_implement,
		d.Type_of_implement,
		d.implement_weight_kg,
		-- test specific measures: Speed and gear
		c.engine_rpm_set,
		c.gear_used,
		-- test specific measures: fuel, area, speed,wheel-slip, productivity,rpm and depth-of-operation  
		c.fuel_consumption_lit_per_hr,
		c.fuel_consumption_lit_per_acr,
		c.area_covered_acr_per_hr,
		c.on_load_speed_kmph,
		c.wheel_slippage_pct,
		---- test specific measures: effeciency  
		null,
		---- test specific measures: productivity,rpm and depth-of-operation  
		(c.fuel_consumption_lit_per_hr/c.area_covered_acr_per_hr),
		c.erpmdrop_straight_1stpass_low,
		c.erpmdrop_straight_1stpass_high,
		c.depth_of_cut_cm_low ,
		c.depth_of_cut_cm_high,
		current_user,
		current_user,
		current_timestamp,
		current_timestamp
		-- add user and timestamp fields
		from 
			transactional.test_instance a,
			transactional.test_instance_tractor_info b,
			transactional.field_perf_summary c,
			transactional.test_instance_implement_info d
		where  
			a.file_syspk = b.file_syspk 
			and b.file_syspk  = c.file_syspk 
			and c.file_syspk  = d.file_syspk 
			and c.tractor_model = b.tractor_model
			and a.file_mnemonic = f.file_mnemonic
			and a.file_syspk  = f.file_syspk;
		
elsif f.file_mnemonic = 'FTWET'
then
		insert into fw_ods.fw_ods
		(
		-- File Identifier: Test_Mnemonic, Sheet_Mnemonic, file_syspk(id), test_instance_id, test_file_name,
		client_id,
		function_id,
		test_file_mnemonic,
		test_sheet_mnemonic,
		test_file_id,
		test_file_name,
		test_instance_id,
		test_iteration_number,
		-- Tractor Identifier: tractor make,tractor model,tractor engine hp,wheel drive type,configuration,Test insance tractor id
		tractor_make,
		tractor_model,
		mahindra_model_yn,
		test_tractor_yn,
		tractor_engine_hp,
		wheel_drive_type,
		configuration,
		test_instance_tractor_id,
		-- Test Season:
		season,
		--test specific: date & test dimensions
		test_date,
		test_date_year,
		test_date_quarter,
		test_date_month,
		test_date_dayofmonth,
		test_date_dayofweek,
		--test specific: condition
		test_condition,
		--test specific: location
		location_name,
		----test specific: Implement details(name, type and Weight_kg)
		name_of_implement,
		type_of_implement,
		implement_weight_kg,
		-- test specific measures: Speed and gear
		engine_speed_var,
		gear_used,
		-- test specific measures: fuel, area, speed,wheel-slip, productivity,rpm and depth-of-operation  
		fuel_consumption_lit_per_hr,
		fuel_consumption_lit_per_acr,
		area_covered_acr_per_hr,
		speed_kmph,
		speed_pto_rpm,
		wheel_slippage_pct,
		no_of_passes,
		field_efficiency_pct,
		productivity,
		erpmdrop_straight_1stpass_low,
		erpmdrop_straight_1stpass_high,
		erpmdrop_straight_2ndpass_low,
		erpmdrop_straight_2ndpass_high,
		erpmdrop_straight_3rdpass_low,
		erpmdrop_straight_3rdpass_high,
		erpmdrop_turn_1stpass_low,
		erpmdrop_turn_1stpass_high,
		erpmdrop_turn_2ndpass_low,
		erpmdrop_turn_2ndpass_high,
		erpmdrop_turn_3rdpass_low,
		erpmdrop_turn_3rdpass_high,
		depth_of_cut_cm_low,
		depth_of_cut_cm_high,
		created_by,
		updated_by, 
		create_timestamp,
		update_timestamp 
		--created_by, updated_by, create_timestamp, update_timestamp 
		)  
		select 
		-- File Identifier: Test_Mnemonic, Sheet_Mnemonic, file_syspk(id), test_instance_id, test_file_name,
		a.client_id, 
		a.function_id, 
		a.file_mnemonic,
		a.file_sheet_mnemonic,
		a.file_syspk,
		a.test_file_name,
		a.syspk,
		c.test_iteration_number,
		-- Tractor Identifier: tractor make,tractor model,tractor engine hp,wheel drive type,configuration,Test insance tractor id
		b.tractor_make,
		b.tractor_model,
		b.mahindra_model_yn,
		b.test_tractor_yn,
		b.tractor_engine_hp,
		b.wheel_drive_type,
		b.configuration,
		b.syspk,
		-- Test Season:
		a.season,
		--test specific: date & test dimensions
		a.date_of_test,
		extract( year from a.date_of_test),
		extract( quarter from a.date_of_test),
		extract( month from a.date_of_test),
		extract( day from a.date_of_test),
		extract( dow from a.date_of_test),
		--test specific: condition
		c.test_condition,
		--test specific: location
		a.test_location_name,
		----test specific: Implement details(name, type and Weight_kg)
		d.name_of_implement,
		d.Type_of_implement,
		d.implement_weight_kg,
		-- test specific measures: Speed and gear
		c.engine_rpm_set,
		c.gear_used,
		-- test specific measures: fuel, area, speed,wheel-slip, productivity,rpm and depth-of-operation  
		c.fuel_consumption_lit_per_hr,
		c.fuel_consumption_lit_per_acr,
		c.area_covered_acr_per_hr,
		c.nominal_speed_kmph,
		c.pto_rpm_set,
		c.wheel_slippage_pct,
		c.no_of_passes,
		---- test specific measures: effeciency  
		null,
		---- test specific measures: productivity,rpm and depth-of-operation  
		c.fuel_consumption_lit_per_hr/area_covered_acr_per_hr,
		c.erpmdrop_straight_1stpass_low,
		c.erpmdrop_straight_1stpass_high,
		c.erpmdrop_straight_2ndpass_low,
		c.erpmdrop_straight_2ndpass_high,
		c.erpmdrop_straight_3rdpass_low,
		c.erpmdrop_straight_3rdpass_high,
		c.erpmdrop_turn_1stpass_low,
		c.erpmdrop_turn_1stpass_high,
		c.erpmdrop_turn_2ndpass_low,
		c.erpmdrop_turn_2ndpass_high,
		c.erpmdrop_turn_3rdpass_low,
		c.erpmdrop_turn_3rdpass_high,
		c.depth_of_cut_cm_low ,
		c.depth_of_cut_cm_high,
		current_user,
		current_user,
		current_timestamp,
		current_timestamp
		
		-- add user and timestamp fields
		from 
			transactional.test_instance a,
			transactional.test_instance_tractor_info b,
			transactional.field_perf_summary c,
			transactional.test_instance_implement_info d
		where  
			a.file_syspk = b.file_syspk
			and b.file_syspk = c.file_syspk
			and c.file_syspk = d.file_syspk
			and c.tractor_model = b.tractor_model
			and a.file_mnemonic = f.file_mnemonic
			and a.file_syspk  = f.file_syspk;


elsif f.file_mnemonic = 'FTHLG'
then
		insert into fw_ods.fw_ods
		(
		-- File Identifier: Test_Mnemonic, Sheet_Mnemonic, file_syspk(id), test_instance_id, test_file_name,
		client_id,
		function_id,
		test_file_mnemonic,
		test_sheet_mnemonic,
		test_file_id,
		test_file_name,
		test_instance_id,
		-- Tractor Identifier: tractor make,tractor model,tractor engine hp,wheel drive type,configuration,Test insance tractor id
		tractor_make,
		tractor_model,
		mahindra_model_yn,
		test_tractor_yn,
		tractor_engine_hp,
		wheel_drive_type,
		configuration,
		test_instance_tractor_id,
		--test specific: date & test dimensions
		test_date,
		test_date_year,
		test_date_quarter,
		test_date_month,
		test_date_dayofmonth,
		test_date_dayofweek,
		--test specific: condition
		test_condition,
		test_iteration_number, 
		--test specific: location
		location_name,
		----test specific: Implement details(name, type and Weight_kg)
		name_of_implement,
		type_of_implement,
		implement_weight_kg,
		-- test specific measures: Speed and gear
		-- test specific measures: fuel, area, speed,wheel-slip, productivity,rpm and depth-of-operation  
		fuel_consumption_lit_per_hr,
		gear_used,
		gear_user_up_slope_1,
		gear_user_up_slope_2,
		gear_user_down_slope,
		speed_kmph,
		mileage_kmpl,
		total_dist_travelled_km,
		erpmdrop_straight_1stpass_low,
		erpmdrop_straight_1stpass_high,
		erpmdrop_turn_1stpass_low,
		erpmdrop_turn_1stpass_high,
		erpmdrop_turn_2ndpass_low,
		erpmdrop_turn_2ndpass_high,
		erpmdrop_turn_3rdpass_low,
		erpmdrop_turn_3rdpass_high,
		created_by,
		updated_by, 
		create_timestamp,
		update_timestamp 
		--created_by, updated_by, create_timestamp, update_timestamp 
		)  
		select   
		-- File Identifier: Test_Mnemonic, Sheet_Mnemonic, file_syspk(id), test_instance_id, test_file_name,
		a.client_id, 
		a.function_id, 
		a.file_mnemonic,
		a.file_sheet_mnemonic,
		a.file_syspk,
		a.test_file_name,
		a.syspk,
		-- Tractor Identifier: tractor make,tractor model,tractor engine hp,wheel drive type,configuration,Test insance tractor id
		b.tractor_make,
		b.tractor_model,
		b.mahindra_model_yn,
		b.test_tractor_yn,
		b.tractor_engine_hp,
		b.wheel_drive_type,
		b.configuration,
		b.syspk,
		--test specific: date & test dimensions
		a.date_of_test,
		extract( year from a.date_of_test),
		extract( quarter from a.date_of_test),
		extract( month from a.date_of_test),
		extract( day from a.date_of_test),
		extract( dow from a.date_of_test),
		--test specific: condition
		c.test_condition,
		c.test_iteration_number,
		--test specific: location
		a.test_location_name,
		----test specific: Implement details(name, type and Weight_kg)
		'Trailer',
		d.trailer_type,
		d.trailer_gross_weight_kg,
		-- test specific measures: Speed and gear
		-- test specific measures: fuel, area, speed,wheel-slip, productivity,rpm and depth-of-operation  
		c.fuel_consumption_lit_per_hr,
		c. gear_used_on_straight_road,
		c.gear_used_on_up_slope_1,
		c.gear_used_on_up_slope_2,
		c.gear_used_on_down_slope,
		c.avg_speed_of_travel_kmph,
		c.Mileage_Km_per_Ltr,
		c.total_dist_travelled_km,
		c.erpmdrop_straight_1stpass_low,
		c.erpmdrop_straight_1stpass_high,
		c.erpmdrop_turn_1stpass_low,
		c.erpmdrop_turn_1stpass_high,
		c.erpmdrop_turn_2ndpass_low,
		c.erpmdrop_turn_2ndpass_high,
		c.erpmdrop_turn_3rdpass_low,
		c.erpmdrop_turn_3rdpass_high,
		current_user,
		current_user,
		current_timestamp,
		current_timestamp
		-- add user and timestamp fields
		from 
			transactional.test_instance a,
			transactional.test_instance_tractor_info b,
			transactional.field_perf_summary c,
			transactional.field_perf_summary_trailer_info d
		where  
			a.file_syspk = b.file_syspk
			and b.file_syspk = c.file_syspk
			and c.file_syspk = d.file_syspk
			and c.tractor_model = b.tractor_model
			and a.file_mnemonic = f.file_mnemonic
			and a.file_syspk  = f.file_syspk;

		
elsif f.file_mnemonic like 'IHT%'
then
		insert into fw_ods.fw_ods(
		-- File Identifier: Test_Mnemonic, Sheet_Mnemonic, file_syspk(id), test_instance_id, test_file_name,
		client_id,
		function_id,
		test_file_mnemonic,
		test_sheet_mnemonic,
		test_file_id,
		test_file_name,
		test_instance_id,
		-- Tractor Identifier: tractor make,tractor model,tractor engine hp,wheel drive type,configuration,Test insance tractor id
		tractor_make,
		tractor_model,
		mahindra_model_yn,
		test_tractor_yn,
		tractor_engine_hp,
		wheel_drive_type,
		configuration,
		test_instance_tractor_id,
		--test specific: date & test dimensions
		test_date,
		test_date_year,
		test_date_quarter,
		test_date_month,
		test_date_dayofmonth,
		test_date_dayofweek,
		--test specific: location and test purpose
		test_condition,
		test_purpose,
		location_name,
		--Test Specific:
		engine_rpm_type,
		lifting_time_in_secs,
		lowering_time_secs,
		created_by,
		updated_by, 
		create_timestamp,
		update_timestamp 
		)
		select 
		-- File Identifier: Test_Mnemonic, Sheet_Mnemonic, file_syspk(id), test_instance_id, test_file_name,
		a.client_id, 
		a.function_id, 
		a.file_mnemonic,
		a.file_sheet_mnemonic,
		a.file_syspk,
		a.test_file_name,
		a.syspk,
		-- Tractor Identifier: tractor make,tractor model,tractor engine hp,wheel drive type,configuration,Test insance tractor id
		b.tractor_make,
		b.tractor_model,
		b.mahindra_model_yn,
		b.test_tractor_yn,
		b.tractor_engine_hp,
		b.wheel_drive_type,
		b.configuration,
		b.syspk,
		--test specific: date & test dimensions
		a.date_of_test,
		extract( year from a.date_of_test),
		extract( quarter from a.date_of_test),
		extract( month from a.date_of_test),
		extract( day from a.date_of_test),
		extract( dow from a.date_of_test),
		--test specific: location
		c.TPL_load,
		a.test_purpose,
		a.test_location_name,
		-- Test Specific:
		c.engine_rpm_type,
		c.lifting_time_sec,
		c.lowering_time_sec,
		current_user,
		current_user,
		current_timestamp,
		current_timestamp
		from 
			transactional.test_instance a,
			transactional.test_instance_tractor_info b,
			transactional.IHT_hyd_lift_sensitivity c
		where  
			a.file_syspk = b.file_syspk
			and b.file_syspk = c.file_syspk
			and c.tractor_model = b.tractor_model
			and a.test_file_format in ('IHTHLS')
			and a.file_syspk  = f.file_syspk;	
		
		-- Insert data to fw_ods_iht: for Noise:
		insert into fw_ods.fw_ods(
		-- File Identifier: Test_Mnemonic, Sheet_Mnemonic, file_syspk(id), test_instance_id, test_file_name,
		client_id,
		function_id,
		test_file_mnemonic,
		test_sheet_mnemonic,
		test_file_id,
		test_file_name,
		test_instance_id,
		-- Tractor Identifier: tractor make,tractor model,tractor engine hp,wheel drive type,configuration,Test insance tractor id
		tractor_make,
		tractor_model,
		mahindra_model_yn,
		test_tractor_yn,
		tractor_engine_hp,
		wheel_drive_type,
		configuration,
		test_instance_tractor_id,
		--test specific: date & test dimensions
		test_date,
		test_date_year,
		test_date_quarter,
		test_date_month,
		test_date_dayofmonth,
		test_date_dayofweek,
		--test specific: location and test purpose
		test_mode,
		test_type,
		test_purpose,
		location_name,
		--Test Specific:
		engine_speed_var,
		gear_used,
		speed_kmph,
		load_kg,
		noise_dba,
		created_by,
		updated_by, 
		create_timestamp,
		update_timestamp 
		)
		select  
		-- File Identifier: Test_Mnemonic, Sheet_Mnemonic, file_syspk(id), test_instance_id, test_file_name,
		a.client_id, 
		a.function_id, 
		a.file_mnemonic,
		a.file_sheet_mnemonic,
		a.file_syspk,
		a.test_file_name,
		a.syspk,
		-- Tractor Identifier: tractor make,tractor model,tractor engine hp,wheel drive type,configuration,Test insance tractor id
		b.tractor_make,
		b.tractor_model,
		b.mahindra_model_yn,
		b.test_tractor_yn,
		b.tractor_engine_hp,
		b.wheel_drive_type,
		b.configuration,
		b.syspk,
		--test specific: date & test dimensions
		a.date_of_test,
		extract( year from a.date_of_test),
		extract( quarter from a.date_of_test),
		extract( month from a.date_of_test),
		extract( day from a.date_of_test),
		extract( dow from a.date_of_test),
		--test specific: location
		c.test_mode,
		c.test_type,
		a.test_purpose,
		a.test_location_name,
		-- Test Specific:
		c.speed_rpm,
		c.gear,
		c.speed_kmph,
		c.load_kg,
		(( c.Noise_Level_1_dB_A + c.Noise_Level_2_dB_A +c.Noise_Level_3_dB_A )/3),
		current_user,
		current_user,
		current_timestamp,
		current_timestamp
		
		from 
			transactional.test_instance a,
			transactional.test_instance_tractor_info b,
			transactional.iht_noise_measurement_results c
		where  
			a.file_syspk = b.file_syspk
			and b.file_syspk = c.file_syspk
			and c.tractor_model = b.tractor_model
			and a.test_file_format in ('IHTNST')
			and a.file_syspk  = f.file_syspk;
		
		
		-- Insert data to fw_ods_iht: for Speed Lag:
		insert into fw_ods.fw_ods(
		-- File Identifier: Test_Mnemonic, Sheet_Mnemonic, file_syspk(id), test_instance_id, test_file_name,
		client_id,
		function_id,
		test_file_mnemonic,
		test_sheet_mnemonic,
		test_file_id,
		test_file_name,
		test_instance_id,
		-- Tractor Identifier: tractor make,tractor model,tractor engine hp,wheel drive type,configuration,Test insance tractor id
		tractor_make,
		tractor_model,
		mahindra_model_yn,
		test_tractor_yn,
		tractor_engine_hp,
		wheel_drive_type,
		configuration,
		test_instance_tractor_id,
		--test specific: date & test dimensions
		test_date,
		test_date_year,
		test_date_quarter,
		test_date_month,
		test_date_dayofmonth,
		test_date_dayofweek,
		--test specific: location and test purpose
		test_condition,
		test_purpose,
		location_name,
		--Test Specific:
		gear_used,
		gear_forward_reverse,
		speed_on_high_rpm_kmph,
		speed_on_low_rpm_kmph,
		speed_on_rated_rpm_kmph,
		created_by,
		updated_by, 
		create_timestamp,
		update_timestamp 
		)
		select  
		-- File Identifier: Test_Mnemonic, Sheet_Mnemonic, file_syspk(id), test_instance_id, test_file_name,
		a.client_id, 
		a.function_id, 
		a.test_file_format,
		a.test_file_sheet_format,
		a.file_syspk,
		a.test_file_name,
		a.syspk,
		-- Tractor Identifier: tractor make,tractor model,tractor engine hp,wheel drive type,configuration,Test insance tractor id
		b.tractor_make,
		b.tractor_model,
		b.mahindra_model_yn,
		b.test_tractor_yn,
		b.tractor_engine_hp,
		b.wheel_drive_type,
		b.configuration,
		b.syspk,
		--test specific: date & test dimensions
		a.date_of_test,
		extract( year from a.date_of_test),
		extract( quarter from a.date_of_test),
		extract( month from a.date_of_test),
		extract( day from a.date_of_test),
		extract( dow from a.date_of_test),
		--test specific: location
		c.test_condition,
		a.test_purpose,
		a.test_location_name,
		-- Test Specific:
		c.gear,
		c.test_condition,
		c.high_2500_rpm,
		c.low_1000_rpm,
		c.rated_2300_rpm,
		current_user,
		current_user,
		current_timestamp,
		current_timestamp
		from 
			transactional.test_instance a,
			transactional.test_instance_tractor_info b,
			transactional.iht_gear_max_speed c
		where  
			a.file_syspk = b.file_syspk
			and b.file_syspk = c.file_syspk
			and c.tractor_model = b.tractor_model
			and a.test_file_format in ('IHTSLL')
			and a.file_syspk  = f.file_syspk;
		
		
		-- Insert data to fw_ods_iht: for Brake:
		insert into fw_ods.fw_ods(
		-- File Identifier: Test_Mnemonic, Sheet_Mnemonic, file_syspk(id), test_instance_id, test_file_name,
		client_id,
		function_id,
		test_file_mnemonic,
		test_sheet_mnemonic,
		test_file_id,
		test_file_name,
		test_instance_id,
		-- Tractor Identifier: tractor make,tractor model,tractor engine hp,wheel drive type,configuration,Test insance tractor id
		tractor_make,
		tractor_model,
		mahindra_model_yn,
		test_tractor_yn,
		tractor_engine_hp,
		wheel_drive_type,
		configuration,
		test_instance_tractor_id,
		--test specific: date & test dimensions
		test_date,
		test_date_year,
		test_date_quarter,
		test_date_month,
		test_date_dayofmonth,
		test_date_dayofweek,
		--test specific: location and test purpose
		test_mode,
		test_condition,
		location_name,
		--Test Specific:
		effort_type,
		speed_kmph,
		effort_kg,
		required_stopping_distance_m,
		actual_stopping_distance_m,
		decceleartoin_m_per_s2 ,
		created_by,
		updated_by, 
		create_timestamp,
		update_timestamp 
		)
		select 
		-- File Identifier: Test_Mnemonic, Sheet_Mnemonic, file_syspk(id), test_instance_id, test_file_name,
		a.client_id, 
		a.function_id, 
		a.test_file_format,
		a.test_file_sheet_format,
		a.file_syspk,
		a.test_file_name,
		a.syspk,
		-- Tractor Identifier: tractor make,tractor model,tractor engine hp,wheel drive type,configuration,Test insance tractor id
		b.tractor_make,
		b.tractor_model,
		b.mahindra_model_yn,
		b.test_tractor_yn,
		b.tractor_engine_hp,
		b.wheel_drive_type,
		b.configuration,
		b.syspk,
		--test specific: date & test dimensions
		a.date_of_test,
		extract( year from a.date_of_test),
		extract( quarter from a.date_of_test),
		extract( month from a.date_of_test),
		extract( day from a.date_of_test),
		extract( dow from a.date_of_test),
		--test specific: location
		c.test_mode, 
		c.test_condition,
		a.test_location_name,
		-- Test Specific:
		'Brake',
		c.trigger_speed_km_per_h,
		c.brake_control_input_force_kg,
		c.required_corrected_stopping_distance_m,
		c.actual_stopping_distance_m,
		c.deceleration_m_s2_v2_2s,
		current_user,
		current_user,
		current_timestamp,
		current_timestamp
		from 
			transactional.test_instance a,
			transactional.test_instance_tractor_info b,
			transactional.iht_brake_perf_test_results c
		where  
			a.file_syspk = b.file_syspk
			and b.file_syspk = c.file_syspk
			and c.tractor_model = b.tractor_model
			and a.test_file_format in ('IHTBT50', 'IHTBT30', 'IHTBTD')
			and a.file_syspk  = f.file_syspk;
		
		
		-- data for Pedal Effort: from transactional.iht_pedal_effort
		insert into fw_ods.fw_ods(
		-- File Identifier: Test_Mnemonic, Sheet_Mnemonic, file_syspk(id), test_instance_id, test_file_name,
		client_id,
		function_id,
		test_file_mnemonic,
		test_sheet_mnemonic,
		test_file_id,
		test_file_name,
		test_instance_id,
		-- Tractor Identifier: tractor make,tractor model,tractor engine hp,wheel drive type,configuration,Test insance tractor id
		tractor_make,
		tractor_model,
		mahindra_model_yn,
		test_tractor_yn,
		tractor_engine_hp,
		wheel_drive_type,
		configuration,
		test_instance_tractor_id,
		--test specific: date & test dimensions
		test_date,
		test_date_year,
		test_date_quarter,
		test_date_month,
		test_date_dayofmonth,
		test_date_dayofweek,
		--test specific: location and test purpose
		test_purpose,
		location_name,
		--Test Specific:
		gear_pedal_effort_parameters,
		effort_type,
		effort_kg,
		brake_pedal_travel_lh_in_mm,
		brake_pedal_travel_rh_in_mm,
		brake_pedal_travel_latched_in_mm,
		pedal_travel_mm,
		created_by,
		updated_by, 
		create_timestamp,
		update_timestamp 
		)
		select 
		-- File Identifier: Test_Mnemonic, Sheet_Mnemonic, file_syspk(id), test_instance_id, test_file_name,
		a.client_id, 
		a.function_id, 
		a.test_file_format,
		a.test_file_sheet_format,
		a.file_syspk,
		a.test_file_name,
		a.syspk,
		-- Tractor Identifier: tractor make,tractor model,tractor engine hp,wheel drive type,configuration,Test insance tractor id
		b.tractor_make,
		b.tractor_model,
		b.mahindra_model_yn,
		b.test_tractor_yn,
		b.tractor_engine_hp,
		b.wheel_drive_type,
		b.configuration,
		b.syspk,
		--test specific: date & test dimensions
		a.date_of_test,
		extract( year from a.date_of_test),
		extract( quarter from a.date_of_test),
		extract( month from a.date_of_test),
		extract( day from a.date_of_test),
		extract( dow from a.date_of_test),
		--test specific: location
		a.test_purpose,
		a.test_location_name,
		-- Test Specific:
		c.parameters,
		'pedal',
		c.efforts_kg,
		null,
		null,
		null,
		c.travel_mm,
		current_user,
		current_user,
		current_timestamp,
		current_timestamp
		from 
			transactional.test_instance a,
			transactional.test_instance_tractor_info b,
			transactional.iht_pedal_effort c
		where  
			a.file_syspk = b.file_syspk
			and b.file_syspk = c.file_syspk
			and c.tractor_model = b.tractor_model
			and a.test_file_format in ('IHTEMT')
			and a.file_syspk  = f.file_syspk;
		
		
		-- Data for Gear Effort from transactional.iht_gear_effort 
		insert into fw_ods.fw_ods(
		-- File Identifier: Test_Mnemonic, Sheet_Mnemonic, file_syspk(id), test_instance_id, test_file_name,
		client_id,
		function_id,
		test_file_mnemonic,
		test_sheet_mnemonic,
		test_file_id,
		test_file_name,
		test_instance_id,
		-- Tractor Identifier: tractor make,tractor model,tractor engine hp,wheel drive type,configuration,Test insance tractor id
		tractor_make,
		tractor_model,
		mahindra_model_yn,
		test_tractor_yn,
		tractor_engine_hp,
		wheel_drive_type,
		configuration,
		test_instance_tractor_id,
		--test specific: date & test dimensions
		test_date,
		test_date_year,
		test_date_quarter,
		test_date_month,
		test_date_dayofmonth,
		test_date_dayofweek,
		--test specific: location
		test_purpose,
		location_name,
		--Test Specific:
		gear_pedal_effort_parameters,
		effort_type,
		effort_kg,
		brake_pedal_travel_lh_in_mm,
		brake_pedal_travel_rh_in_mm,
		brake_pedal_travel_latched_in_mm,
		pedal_travel_mm,
		created_by,
		updated_by, 
		create_timestamp,
		update_timestamp 
		)
		select 
		-- File Identifier: Test_Mnemonic, Sheet_Mnemonic, file_syspk(id), test_instance_id, test_file_name,
		a.client_id, 
		a.function_id, 
		a.test_file_format,
		a.test_file_sheet_format,
		a.file_syspk,
		a.test_file_name,
		a.syspk,
		-- Tractor Identifier: tractor make,tractor model,tractor engine hp,wheel drive type,configuration,Test insance tractor id
		b.tractor_make,
		b.tractor_model,
		b.mahindra_model_yn,
		b.test_tractor_yn,
		b.tractor_engine_hp,
		b.wheel_drive_type,
		b.configuration,
		b.syspk,
		--test specific: date & test dimensions
		a.date_of_test,
		extract( year from a.date_of_test),
		extract( quarter from a.date_of_test),
		extract( month from a.date_of_test),
		extract( day from a.date_of_test),
		extract( dow from a.date_of_test),
		--test specific: location
		a.test_purpose,
		a.test_location_name,
		-- Test Specific:
		c.parameters,
		'Gear',
		c.efforts_kg,
		null,
		null,
		null,
		null,
		current_user,
		current_user,
		current_timestamp,
		current_timestamp
		from 
			transactional.test_instance a,
			transactional.test_instance_tractor_info b,
			transactional.iht_gear_effort c
		where  
			a.file_syspk = b.file_syspk
			and b.file_syspk = c.file_syspk
			and c.tractor_model = b.tractor_model
			and a.test_file_format in ('IHTEMT')
			and a.file_syspk  = f.file_syspk;
		
		
		
		-- Data for Break Pedal Effor: from table transactional.iht_brake_pedal_effort 
		insert into fw_ods.fw_ods(
		-- File Identifier: Test_Mnemonic, Sheet_Mnemonic, file_syspk(id), test_instance_id, test_file_name,
		client_id,
		function_id,
		test_file_mnemonic,
		test_sheet_mnemonic,
		test_file_id,
		test_file_name,
		test_instance_id,
		-- Tractor Identifier: tractor make,tractor model,tractor engine hp,wheel drive type,configuration,Test insance tractor id
		tractor_make,
		tractor_model,
		mahindra_model_yn,
		test_tractor_yn,
		tractor_engine_hp,
		wheel_drive_type,
		configuration,
		test_instance_tractor_id,
		--test specific: date & test dimensions
		test_date,
		test_date_year,
		test_date_quarter,
		test_date_month,
		test_date_dayofmonth,
		test_date_dayofweek,
		--test specific: location
		test_purpose,
		location_name,
		--Test Specific:
		gear_pedal_effort_parameters,
		effort_type,
		effort_kg,
		brake_pedal_travel_lh_in_mm,
		brake_pedal_travel_rh_in_mm,
		brake_pedal_travel_latched_in_mm,
		pedal_travel_mm,
		created_by,
		updated_by, 
		create_timestamp,
		update_timestamp 
		)
		select 
		-- File Identifier: Test_Mnemonic, Sheet_Mnemonic, file_syspk(id), test_instance_id, test_file_name,
		a.client_id, 
		a.function_id, 
		a.test_file_format,
		a.test_file_sheet_format,
		a.file_syspk,
		a.test_file_name,
		a.syspk,
		-- Tractor Identifier: tractor make,tractor model,tractor engine hp,wheel drive type,configuration,Test insance tractor id
		b.tractor_make,
		b.tractor_model,
		b.mahindra_model_yn,
		b.test_tractor_yn,
		b.tractor_engine_hp,
		b.wheel_drive_type,
		b.configuration,
		b.syspk,
		--test specific: date & test dimensions
		a.date_of_test,
		extract( year from a.date_of_test),
		extract( quarter from a.date_of_test),
		extract( month from a.date_of_test),
		extract( day from a.date_of_test),
		extract( dow from a.date_of_test),
		--test specific: location
		a.test_purpose,
		a.test_location_name,
		-- Test Specific:
		null,
		'Brake Pedal',
		c.pedal_effort_kg,
		pedal_travel_rh_mm,
		pedal_travel_lh_mm,
		pedal_travel_latched_mm,
		null,
		current_user,
		current_user,
		current_timestamp,
		current_timestamp
		from 
			transactional.test_instance a,
			transactional.test_instance_tractor_info b,
			transactional.iht_brake_pedal_effort c
		where  
			a.file_syspk = b.file_syspk
			and b.file_syspk = c.file_syspk
			and c.tractor_model = b.tractor_model
			and a.test_file_format in ('IHTEMT')
			and a.file_syspk  = f.file_syspk;
		
		--Data for Cluth PedalEffort: from transactional.iht_clutch_pedal_effort 
		insert into fw_ods.fw_ods(
		-- File Identifier: Test_Mnemonic, Sheet_Mnemonic, file_syspk(id), test_instance_id, test_file_name,
		client_id,
		function_id,
		test_file_mnemonic,
		test_sheet_mnemonic,
		test_file_id,
		test_file_name,
		test_instance_id,
		-- Tractor Identifier: tractor make,tractor model,tractor engine hp,wheel drive type,configuration,Test insance tractor id
		tractor_make,
		tractor_model,
		mahindra_model_yn,
		test_tractor_yn,
		tractor_engine_hp,
		wheel_drive_type,
		configuration,
		test_instance_tractor_id,
		--test specific: date & test dimensions
		test_date,
		test_date_year,
		test_date_quarter,
		test_date_month,
		test_date_dayofmonth,
		test_date_dayofweek,
		--test specific: location
		test_purpose,
		location_name,
		--Test Specific:
		gear_pedal_effort_parameters,
		effort_type,
		effort_kg,
		brake_pedal_travel_lh_in_mm,
		brake_pedal_travel_rh_in_mm,
		brake_pedal_travel_latched_in_mm,
		pedal_travel_mm,
		created_by,
		updated_by, 
		create_timestamp,
		update_timestamp 
		)
		select 
		-- File Identifier: Test_Mnemonic, Sheet_Mnemonic, file_syspk(id), test_instance_id, test_file_name,
		a.client_id, 
		a.function_id, 
		a.test_file_format,
		a.test_file_sheet_format,
		a.file_syspk,
		a.test_file_name,
		a.syspk,
		-- Tractor Identifier: tractor make,tractor model,tractor engine hp,wheel drive type,configuration,Test insance tractor id
		b.tractor_make,
		b.tractor_model,
		b.mahindra_model_yn,
		b.test_tractor_yn,
		b.tractor_engine_hp,
		b.wheel_drive_type,
		b.configuration,
		b.syspk,
		--test specific: date & test dimensions
		a.date_of_test,
		extract( year from a.date_of_test),
		extract( quarter from a.date_of_test),
		extract( month from a.date_of_test),
		extract( day from a.date_of_test),
		extract( dow from a.date_of_test),
		--test specific: location
		a.test_purpose,
		a.test_location_name,
		-- Test Specific:
		null,
		'Clutch Pedal',
		c.pedal_effort_kg,
		null,
		null,
		null,
		c.pedal_travel_mm,
		current_user,
		current_user,
		current_timestamp,
		current_timestamp
		from 
			transactional.test_instance a,
			transactional.test_instance_tractor_info b,
			transactional.iht_clutch_pedal_effort c
		where  
			a.file_syspk = b.file_syspk
			and b.file_syspk = c.file_syspk
			and c.tractor_model = b.tractor_model
			and a.test_file_format in ('IHTEMT')
			and a.file_syspk  = f.file_syspk;
	end if; 			
 end loop;

update fw_core.fw_jobctl_runschedule_jobstep set end_status_note = 'trx_load_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;