drop function if exists staging2.fn_FTHLG_SUM_TRX ;
CREATE OR REPLACE FUNCTION staging2.fn_FTHLG_SUM_TRX(p_client_id int,p_function_id int, p_file_mnemonic text,
p_file_sheet_mnemonic text, p_file_syspk int)
 RETURNS void
 LANGUAGE plpgsql
AS $function$
   declare __test_instance_id int;
   declare __client_id int :=p_client_id;
   declare __function_id int :=p_function_id;
   declare __file_mnemonic text  :=p_file_mnemonic;
   declare __file_sheet_mnemonic text :=p_file_sheet_mnemonic;
   declare __file_syspk int :=p_file_syspk;
   declare __make text;
   declare __model text;
   declare err_state   text;
   declare err_msg     text;
   declare err_detail  text;
   declare err_hint    text;
   declare err_context text;   
   declare _error int;   
 begin

__file_syspk := p_file_syspk;

/************************************************************ 
Function Name:fn_FTHLG_SUM_TRX
Function Desc: This function populates data into ODS
File Format: FTHLG
Sheet Format: FTHLG_SUM
Creation Date: 
Updation Date:
Author: compegence team
Function Call: select staging2.fn_FTHLG_SUM_TRX();
***************************************************************/


insert into transactional.field_perf_summary_tractor_info 
(
client_id,
function_id,
tractor_model,
tractor_make,
tractor_engine_hp,
rated_rpm,
transmission_type,
wheel_drive_type,
fip_type,
steering_type,
tractor_weight_front_kg,
tractor_weight_rear_kg,
tractor_weight_total_kg,
file_syspk,
file_mnemonic,
file_sheet_mnemonic
)
select
client_id,
function_id,
tractor_model,
tractor_make ,
tractor_engine_hp::numeric,
rated_rpm::numeric,
transmission_type,
wheel_drive_type,
fip_type,
steering_type,
tractor_weight_kg_front::numeric  tractor_weight_front_kg,
tractor_weight_kg_rear::numeric tractor_weight_rear_kg,
tractor_weight_kg_total::numeric tractor_weight_total_kg,
file_syspk,
file_mnemonic,
file_sheet_mnemonic
from staging2.fthlg_sum_trac_h1_block where trx_record=1;


insert into transactional.field_perf_summary_trailer_info
(
client_id,
function_id,
trailer_type,
tire_size_and_inflation_pressure_psi,
no_of_axle,
no_of_wheels,
trailer_gross_weight_kg,
tractor_rwc_to_hitch_point_center_dist_mm,
tractor_hitch_ht_mm,
trailer_hitch_height_above_ground_level_mm,
gradient_slope_1_degree,
gradient_slope_2_degree,
tractor_hitch_type,
file_syspk,
file_mnemonic,
file_sheet_mnemonic
)
select
client_id,
function_id,
Trailer_Type,
Tire_size_and_inflation_pressure_psi,
No_Of_Axle::numeric ,
No_Of_Wheels::numeric,	
Trailer_Gross_Weight_Kg::numeric,
Tractor_Rear_Wheel_Center_to_Hitch_Point_Center_Distance_mm::numeric,
Tractor_Hitch_Height_from_Ground_mm::numeric,
Trailer_hitch_Height_above_ground_level_mm::numeric,
Gradient_Slope_1_Degree::numeric,
Gradient_Slope_2_degree::numeric,
Tractor_Hitch_Type,
file_syspk,
file_mnemonic,
file_sheet_mnemonic
from staging2.FTHLG_SUM_Trail_Type_Block where trx_record=1; 

insert into transactional.field_perf_summary 
(
client_id,
function_id,
test_iteration_number,
test_condition,
test_date,
tractor_model,
fuel_consumption_lit_per_hr,
mileage_km_per_ltr,
avg_speed_of_travel_kmph,
total_dist_travelled_km,
gear_used_on_straight_road,
straight_road_rpm_drop,
gear_used_on_up_slope_1,
up_slope_rpm_drop_1,
gear_used_on_up_slope_2,
up_slope_rpm_drop_2,
gear_used_on_down_slope,
down_slope_rpm_shoot_up,
fuel_consumption_var_lit_per_hr,
mileage_var_km_per_ltr,
fuel_consumption_var_pct_lit_per_hr,
mileage_var_pct_km_per_ltr,
engine_smoke_on_load,
engine_acceleration_smoke,
range_gear_shifting,
speed_gear_shifting,
tractor_steer_ability,
tractor_braking_perf,
front_visibility,
implement_accessibility,
front_end_lifting_during_operation,
rpm_recovery_time,
engine_vibration,
engine_sound,
file_syspk,
file_mnemonic,
file_sheet_mnemonic
)
select
client_id,
function_id,
test_iterationnumber::numeric,
Test_Condition,
Test_Date::date,
Tractor_Model,
Fuel_consumption_Ltr_hr::numeric fuel_consumption_lit_per_hr ,
Mileage_Km_Ltr ::numeric mileage_km_per_ltr ,
Average_speed_of_travel_kmph::numeric avg_speed_of_travel_kmph ,
Total_distance_travelled_km::numeric  total_dist_travelled_km ,
Gear_used_on_Straight_Road,
Straight_road_RPM_Drop,
Gear_used_on_Up_Slope_1,
Up_Slope_RPM_Drop_1 ,
Gear_used_on_Up_Slope_2,
Up_Slope_RPM_Drop_2,
Gear_used_on_Down_Slope,
Down_Slope_RPM_Shoot_up,
fuel_consumption_lit_hr_2::numeric ,
Mileage_Km_Ltr_2::numeric ,
fuel_consumption_lit_hr_3::numeric  ,
Mileage_Km_Ltr_3::numeric,
engine_smoke_on_load,
Engine_acceleration_smoke,
Range_Gear_Shifting,
Speed_Gear_Shifting,
Tractor_Steer_ability,
Tractor_braking_performance tractor_braking_perf ,
Front_Visibility,
Implement_Accessibility,
Front_end_lifting_during_operation,
RPM_Recovery_Time,
Engine_Vibration,
Engine_Sound,
file_syspk,
file_mnemonic,
file_sheet_mnemonic
from staging2.fthlg_sum_test_condition_1_block 
where trx_record=1; 



insert into transactional.field_perf_summary 
(
client_id,
function_id,
test_iteration_number,
test_condition,
test_date,
tractor_model,
fuel_consumption_lit_per_hr,
mileage_km_per_ltr,
avg_speed_of_travel_kmph,
total_dist_travelled_km,
gear_used_on_straight_road,
straight_road_rpm_drop,
gear_used_on_up_slope_1,
up_slope_rpm_drop_1,
gear_used_on_up_slope_2,
up_slope_rpm_drop_2,
gear_used_on_down_slope,
down_slope_rpm_shoot_up,
fuel_consumption_var_lit_per_hr,
mileage_var_km_per_ltr,
fuel_consumption_var_pct_lit_per_hr,
mileage_var_pct_km_per_ltr,
engine_smoke_on_load,
engine_acceleration_smoke,
range_gear_shifting,
speed_gear_shifting,
tractor_steer_ability,
tractor_braking_perf,
front_visibility,
implement_accessibility,
front_end_lifting_during_operation,
rpm_recovery_time,
engine_vibration,
engine_sound,
file_syspk,
file_mnemonic,
file_sheet_mnemonic 
)
select
client_id,
function_id,
test_iterationnumber::numeric,
Test_Condition,
Test_Date::date,
Tractor_Model,
Fuel_consumption_Ltr_hr::numeric fuel_consumption_lit_per_hr ,
Mileage_Km_Ltr ::numeric mileage_km_per_ltr ,
Average_speed_of_travel_kmph::numeric avg_speed_of_travel_kmph ,
Total_distance_travelled_km::numeric total_dist_travelled_km ,
Gear_used_on_Straight_Road,
Straight_road_RPM_Drop,
Gear_used_on_Up_Slope_1,
Up_Slope_RPM_Drop_1 ,
Gear_used_on_Up_Slope_2,
Up_Slope_RPM_Drop_2,
Gear_used_on_Down_Slope,
Down_Slope_RPM_Shoot_up,
fuel_consumption_lit_hr_2::numeric ,
Mileage_Km_Ltr_2::numeric,
fuel_consumption_lit_hr_3::numeric  ,
Mileage_Km_Ltr_3::numeric,
engine_smoke_on_load,
Engine_acceleration_smoke,
Range_Gear_Shifting,
Speed_Gear_Shifting,
Tractor_Steer_ability,
Tractor_braking_performance tractor_braking_perf ,
Front_Visibility,
Implement_Accessibility,
Front_end_lifting_during_operation,
RPM_Recovery_Time,
Engine_Vibration,
Engine_Sound,
file_syspk,
file_mnemonic,
file_sheet_mnemonic 
from staging2.fthlg_sum_test_condition_2_block 
where trx_record=1; 



update transactional.field_perf_summary_tractor_info a
set test_instance_tractor_id = (select syspk
from transactional.test_instance_tractor_info b
where a.tractor_model=trim(b.tractor_model) and a.file_syspk =b.file_syspk
)where a.file_syspk =__file_syspk;




update transactional.field_perf_summary a
set test_engr_comments=b.test_engineer_comments_1, 
test_mgr_comments =b.test_manager_comments_1
from staging2.fthlg_sum_comments_by_block b
where a.file_syspk=b.file_syspk and a.test_iteration_number =1;

update transactional.field_perf_summary a
set test_engr_comments=b.test_engineer_comments_2, 
test_mgr_comments =b.test_manager_comments_2
from staging2.fthlg_sum_comments_by_block b
where a.file_syspk=b.file_syspk and a.test_iteration_number =2;

update transactional.field_perf_summary a
set test_engr_comments=b.test_engineer_comments_3, 
test_mgr_comments =b.test_manager_comments_3
from staging2.fthlg_sum_comments_by_block b
where a.file_syspk=b.file_syspk and a.test_iteration_number =3;

update transactional.field_perf_summary a
set test_engr_comments=b.test_engineer_comments_4, 
test_mgr_comments =b.test_manager_comments_4
from staging2.fthlg_sum_comments_by_block b
where a.file_syspk=b.file_syspk and a.test_iteration_number =4;




select syspk into __test_instance_id from transactional.test_instance where file_syspk =__file_syspk;

update transactional.field_perf_summary
set test_instance_id=__test_instance_id
where file_syspk=__file_syspk;

update transactional.field_perf_summary_implement_info
set test_instance_id=__test_instance_id
where file_syspk=__file_syspk;

update transactional.field_perf_summary_tractor_info
set test_instance_id=__test_instance_id
where file_syspk=__file_syspk;

update transactional.field_perf_summary_trailer_info
set test_instance_id=__test_instance_id
where file_syspk=__file_syspk;

update transactional.field_perf_summary a
set test_instance_tractor_id = (select syspk
from transactional.test_instance_tractor_info b
where a.tractor_model=b.tractor_model and a.file_syspk =b.file_syspk
)where test_iteration_number =1 and a.file_syspk =__file_syspk;

update transactional.field_perf_summary a
set test_instance_tractor_id = (select syspk
from transactional.test_instance_tractor_info b
where a.tractor_model=b.tractor_model and a.file_syspk =b.file_syspk
)where test_iteration_number =2 and a.file_syspk =__file_syspk;

update transactional.field_perf_summary a
set test_instance_tractor_id = (select syspk
from transactional.test_instance_tractor_info b
where a.tractor_model=b.tractor_model and a.file_syspk =b.file_syspk
)where test_iteration_number =3 and a.file_syspk =__file_syspk;


update transactional.field_perf_summary a
set test_instance_tractor_id = (select syspk
from transactional.test_instance_tractor_info b
where a.tractor_model=b.tractor_model and a.file_syspk =b.file_syspk
)where test_iteration_number =4 and a.file_syspk =__file_syspk;


update transactional.field_perf_summary 
set straight_road_rpm_drop =replace(straight_road_rpm_drop,' to ','-'),
up_slope_rpm_drop_1 =replace(up_slope_rpm_drop_1,' to ','-'),
up_slope_rpm_drop_2 =replace(up_slope_rpm_drop_2,' to ','-'),
down_slope_rpm_shoot_up=replace(down_slope_rpm_shoot_up,' to ','-')
where file_mnemonic = 'FTHLG';



UPDATE transactional.field_perf_summary  
	SET 
--updating RPM drop straight:
	erpmdrop_straight_1stpass_low = trim(split_part(trim(straight_road_rpm_drop), '-', 1))::numeric,
	erpmdrop_straight_1stpass_high = case trim(split_part(trim(straight_road_rpm_drop), '-', 2))
	when '' then null
	else trim(split_part(trim(straight_road_rpm_drop), '-', 2))::numeric end,
--updating RPM drop up slop 1st pass:
	erpmdrop_turn_1stpass_low = trim(split_part(trim(up_slope_rpm_drop_1), '-', 1))::numeric,
	erpmdrop_turn_1stpass_high = case trim(split_part(trim(up_slope_rpm_drop_1), '-', 2))
	when '' then null
	else trim(split_part(trim(up_slope_rpm_drop_1), '-', 2))::numeric end,
--updating RPM drop up slop 2nd pass:
	erpmdrop_turn_2ndpass_low = trim(split_part(trim(up_slope_rpm_drop_2), '-', 1))::numeric,
	erpmdrop_turn_2ndpass_high = case trim(split_part(trim(up_slope_rpm_drop_2), '-', 2))
	when '' then null
	else trim(split_part(trim(up_slope_rpm_drop_2), '-', 2))::numeric end,
--updating down slope rpm shoot up :
	erpmdrop_turn_3rdpass_low = trim(split_part(trim(down_slope_rpm_shoot_up), '-', 1))::numeric,
	erpmdrop_turn_3rdpass_high = case trim(split_part(trim(down_slope_rpm_shoot_up), '-', 2))
	when '' then null
	else trim(split_part(trim(down_slope_rpm_shoot_up), '-', 2))::numeric end
	where file_mnemonic = 'FTHLG';



perform  fw_core.fn_insert_db_error ( __client_id, __function_id, 1001, 'Compegence', __file_syspk ,__file_mnemonic,__file_sheet_mnemonic ,null,'trx', 'fn_FTHLG_SUM_TRX', err_state, err_msg, err_detail, err_hint, err_context,'success');

end
$function$
;


