drop function if exists staging2.fn_FTWET_SUM_TRX ;
CREATE OR REPLACE FUNCTION staging2.fn_FTWET_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_FTWET_SUM_TRX
Function Desc: This function populates data into ODS
File Format: FTWET
Sheet Format: FTWET_SUM
Creation Date: 
Updation Date:
Author: compegence team
Function Call: select staging2.fn_FTWET_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,
trim('HP' from tractor_engine_hp)::numeric,
rated_rpm::numeric,
transmission_type,
wheel_drive_type,
fip_type,
steering_type,
tractor_weight_kg_front::numeric,
tractor_weight_kg_rear::numeric,
tractor_weight_kg_total::numeric,
file_syspk,
file_mnemonic,
file_sheet_mnemonic 
from staging2.ftwet_sum_trac_h1_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;


insert into transactional.field_perf_summary_implement_info 
(
client_id,
function_id,
name_of_implement,
implement_type,
implement_size,
hitch_category,
implement_weight,
soil_type,
soil_moisture_content_pct,
soil_bulk_density_g_per_cc,
soil_cone_index,
field_condition,
season,
file_syspk,
file_mnemonic,
file_sheet_mnemonic
)
select
client_id,
function_id,
make_of_implement,
implement_type,
implement_size,
hitch_category,
implement_weight::numeric,
soil_type,
moisture_content::numeric soil_moisture_content_pct,
bulk_density_g_cc::numeric soil_bulk_density_g_per_cc,
soil_cone_index::numeric soil_cone_index,
field_condition,
season,
file_syspk,
file_mnemonic,
file_sheet_mnemonic 
from staging2.ftwet_sum_implement_block where trx_record=1;


insert into transactional.field_perf_summary_implement_info 
(
client_id,
function_id,
type_of_cage_wheel,
cage_wheel_width_mm,
cage_wheel_weight,
cage_wheel_inner_and_outer_ring_dia_mm,
cage_wheel_center_ring_dia_mm,
no_of_angles_on_cage_wheel,
file_syspk,
file_mnemonic,
file_sheet_mnemonic
)
select
client_id,
function_id,
Type_of_Cage_Wheel,
Cage_Wheel_Width_mm::numeric,
Cage_wheel_Weight::numeric,
Cage_Wheel_inner_Outer_Ring_Dia_mm::numeric cage_wheel_inner_and_outer_ring_dia_mm,
Cage_Wheel_Center_Ring_Dia_mm::numeric,
No_of_angles_on_cage_wheel::numeric,
file_syspk,
file_mnemonic,
file_sheet_mnemonic 
from staging2.FTWET_SUM_Cage_Wheel_Block where trx_record=1;


insert into transactional.field_perf_summary
(
client_id,
function_id,
test_iteration_number,
Test_Condition,
Test_Date,
Tractor_Model,
Engine_RPM_set,
PTO_RPM_set,
Gear_Used,
Nominal_Speed_KMPH,
Engine_RPM_Drop_on_straight_1st_Pass,
Engine_RPM_Drop_on_straight_2nd_Pass,
Engine_RPM_Drop_on_straight_3rd_Pass,
Engine_RPM_Drop_on_turn_1st_Pass,
Engine_RPM_Drop_on_turn_2nd_Pass,
Engine_RPM_Drop_on_turn_3rd_Pass,
No_of_passes,
depth_of_cut_cm,
fuel_consumption_lit_per_hr,
area_covered_acr_per_hr,
 fuel_consumption_lit_per_acr,
 fuel_consumption_var_lit_per_hr,
area_covered_var_acr_per_hr,
fuel_consumption_var_lit_per_acr,
Engine_Smoke_on_Load,
Engine_acceleration_smoke,
Draft_Response,
Tractor_Steer_ability,
Tractor_braking_perf,
Front_Visibility,
Implement_Accessibility,
Front_Wheel_dragging_at_turning,
Front_end_lifting_during_operation,
RPM_Recovery_Time,
Engine_Vibration,
Engine_Sound,
implement_lifting_per_lowering_response,
Pulverization_Quality,
Pulverization_Index,
file_syspk,
file_mnemonic,
file_sheet_mnemonic 
)
select
client_id,
function_id,
test_iterationnumber::numeric,
Test_Condition,
Test_Date::date,
Tractor_Model,
Engine_RPM_set::numeric,
PTO_RPM_set::numeric,
Gear_Used,
Nominal_Speed_KMPH::numeric,
Engine_RPM_Drop_on_straight_1st_Pass,
Engine_RPM_Drop_on_straight_2nd_Pass,
Engine_RPM_Drop_on_straight_3rd_Pass,
Engine_RPM_Drop_on_turn_1st_Pass,
Engine_RPM_Drop_on_turn_2nd_Pass,
Engine_RPM_Drop_on_turn_3rd_Pass,
No_of_passes::numeric,
Avg_Depth_of_cut_cm depth_of_cut_cm ,
Fuel_consumption_lit_hr::numeric fuel_consumption_lit_per_hr,
Area_covered_acr_hr::numeric area_covered_acr_per_hr,
Fuel_consumption_lit_Acr::numeric fuel_consumption_lit_per_acr,
case when fuel_consumption_lit_hr_2 ~ E'^\\d+$' then  fuel_consumption_lit_hr_2::numeric  else null end as fuel_consumption_var_lit_per_hr ,
case when area_covered_acr_hr_2 ~ E'^\\d+$' then area_covered_acr_hr_2::numeric else null  end as area_covered_var_acr_per_hr ,
case when fuel_consumption_lit_acr_2 ~ E'^\\d+$' then  fuel_consumption_lit_acr_2::numeric  else  null end as fuel_consumption_var_lit_per_acr,
Engine_Smoke_on_Load,
Engine_acceleration_smoke,
Draft_Response,
Tractor_Steer_ability,
Tractor_braking_performance tractor_braking_perf,
Front_Visibility,
Implement_Accessibility,
Front_Wheel_dragging_at_turning,
Front_end_lifting_during_operation,
RPM_Recovery_Time,
Engine_Vibration,
Engine_Sound,
Implement_Lifting_Lowering_response implement_lifting_per_lowering_response,
Pulverization_Quality,
Pulverization_Index,
file_syspk,
file_mnemonic,
file_sheet_mnemonic 
from staging2.FTWET_SUM_Test_Condition_Block where trx_record=1;




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.ftwet_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.ftwet_sum_comments_by_block b
where a.file_syspk=b.file_syspk and a.test_iteration_number =2;


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


	
update transactional.field_perf_summary 
set engine_rpm_drop_on_straight_1st_pass =replace(engine_rpm_drop_on_straight_1st_pass,' to ','-'),
engine_rpm_drop_on_straight_2nd_pass =replace(engine_rpm_drop_on_straight_2nd_pass,' to ','-'),
engine_rpm_drop_on_straight_3rd_pass =replace(engine_rpm_drop_on_straight_3rd_pass,' to ','-'),
engine_rpm_drop_on_turn_1st_pass=replace(engine_rpm_drop_on_turn_1st_pass,' to ','-'),
engine_rpm_drop_on_turn_2nd_pass=replace(engine_rpm_drop_on_turn_2nd_pass,' to ','-'),
engine_rpm_drop_on_turn_3rd_pass=replace(engine_rpm_drop_on_turn_3rd_pass,' to ','-'),
depth_of_cut_cm=replace(depth_of_cut_cm,' to ','-')
where file_mnemonic = 'FTWET';
	
UPDATE transactional.field_perf_summary  
	SET 
--updating RPM drop straight 1st pass:
	erpmdrop_straight_1stpass_low = trim(split_part(trim(engine_rpm_drop_on_straight_1st_pass), '-', 1))::numeric,
	erpmdrop_straight_1stpass_high = case trim(split_part(trim(engine_rpm_drop_on_straight_1st_pass), '-', 2))
	when '' then null
	else trim(split_part(trim(engine_rpm_drop_on_straight_1st_pass), '-', 2))::numeric end,
--updating RPM drop straight 2nd pass:	
	erpmdrop_straight_2ndpass_low = trim(split_part(trim(engine_rpm_drop_on_straight_2nd_pass), '-', 1))::numeric,
	erpmdrop_straight_2ndpass_high = case trim(split_part(trim(engine_rpm_drop_on_straight_2nd_pass), '-', 2))
	when '' then null
	else trim(split_part(trim(engine_rpm_drop_on_straight_2nd_pass), '-', 2))::numeric end,
--updating RPM drop straight 3rd pass:
	erpmdrop_straight_3rdpass_low = trim(split_part(trim(engine_rpm_drop_on_straight_3rd_pass), '-', 1))::numeric,
	erpmdrop_straight_3rdpass_high = case trim(split_part(trim(engine_rpm_drop_on_straight_3rd_pass), '-', 2))
	when '' then null
	else trim(split_part(trim(engine_rpm_drop_on_straight_3rd_pass), '-', 2))::numeric end,
--updating RPM drop turn 1st pass:
	erpmdrop_turn_1stpass_low = trim(split_part(trim(engine_rpm_drop_on_turn_1st_pass), '-', 1))::numeric,
	erpmdrop_turn_1stpass_high = case trim(split_part(trim(engine_rpm_drop_on_turn_1st_pass), '-', 2))
	when '' then null
	else trim(split_part(trim(engine_rpm_drop_on_turn_1st_pass), '-', 2))::numeric end,
--updating RPM drop turn 2nd pass:
	erpmdrop_turn_2ndpass_low = trim(split_part(trim(engine_rpm_drop_on_turn_2nd_pass), '-', 1))::numeric,
	erpmdrop_turn_2ndpass_high = case trim(split_part(trim(engine_rpm_drop_on_turn_2nd_pass), '-', 2))
	when '' then null
	else trim(split_part(trim(engine_rpm_drop_on_turn_2nd_pass), '-', 2))::numeric end,
--updating RPM drop turn 3rd pass:
	erpmdrop_turn_3rdpass_low = trim(split_part(trim(engine_rpm_drop_on_turn_3rd_pass), '-', 1))::numeric,
	erpmdrop_turn_3rdpass_high = case trim(split_part(trim(engine_rpm_drop_on_turn_3rd_pass), '-', 2))
	when '' then null
	else trim(split_part(trim(engine_rpm_drop_on_turn_3rd_pass), '-', 2))::numeric end,
--updating depth of cut:
	depth_of_cut_cm_low = trim(split_part(trim(depth_of_cut_cm), '-', 1))::numeric,
	depth_of_cut_cm_high= case trim(split_part(trim(depth_of_cut_cm), '-', 2))
	when '' then null
	else trim('cm' from split_part(trim(depth_of_cut_cm), '-', 2))::numeric end
	where file_mnemonic = 'FTWET';


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_implement_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;

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

end
$function$
;

