



drop function if exists staging2.fn_FTDRY_SUM_TRX ;
CREATE OR REPLACE FUNCTION staging2.fn_FTDRY_SUM_TRX(p_client_id int,p_function_id int, p_file_mnemonic text,
p_file_sheet_mnemonic text, p_files_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 __files_syspk int :=p_files_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

__files_syspk := p_files_syspk;

/************************************************************ 
Function Name:fn_FTDRY_SUM_TRX
Function Desc: This function populates data into ODS
File Format: FTDRY
Sheet Format: FTDRY_SUM
Creation Date: 
Updation Date:
Author: compegence team
Function Call: select staging2.fn_FTDRY_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_kg_rear::numeric,
tractor_weight_kg_total::numeric,
file_syspk,
file_mnemonic,
file_sheet_mnemonic 
from staging2.ftdry_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 =__files_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,
trim('kg' from implement_weight)::numeric,
soil_type,
moisture_content::text soil_moisture_content_pct,
bulk_density_g_cc::numeric soil_bulk_density_g_per_cc,
trim(' Kpa' from soil_cone_index)::numeric soil_cone_index,
field_condition,
season,
file_syspk,
file_mnemonic,
file_sheet_mnemonic 
from staging2.ftdry_sum_implement_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_turn_1st_pass,
depth_of_cut_cm,
no_load_speed_kmph,
on_load_speed_kmph,
wheel_slippage_pct,
fuel_consumption_lit_per_hr,
area_covered_acr_per_hr,
fuel_consumption_lit_per_acr,
fuel_consumption_var_pct_lit_per_hr,
area_covered_var_pct_acr_per_hr,
fuel_consumption_var_pct_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_index,
pulverization_quality,
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 engine_rpm_drop_on_straight_1st_pass ,
engine_rpm_drop_on_turn engine_rpm_drop_on_turn_1st_pass,
depth_of_cut_cm,
no_load_speed_kmph::numeric,
on_load_speed_kmph::numeric,
wheel_slippage_::numeric wheel_slippage_pct,
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 ,
fuel_consumption_lit_hr_2::numeric  ,
area_covered_acr_hr_2::numeric  ,
fuel_consumption_lit_acr_2::numeric ,
engine_smoke_on_load,
engine_acceleration_smoke ,
draft_response,
tractor_steer_ability,
tractor_braking_performance,
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_index,
pulverization_quality,
file_syspk,
file_mnemonic,
file_sheet_mnemonic 
from staging2.ftdry_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,
engine_rpm_set,
pto_rpm_set,
gear_used,
nominal_speed_kmph,
engine_rpm_drop_on_straight_1st_pass,
engine_rpm_drop_on_turn_1st_pass,
depth_of_cut_cm,
no_load_speed_kmph,
on_load_speed_kmph,
wheel_slippage_pct,
fuel_consumption_lit_per_hr,
area_covered_acr_per_hr,
fuel_consumption_lit_per_acr,
fuel_consumption_var_pct_lit_per_hr,
area_covered_var_pct_acr_per_hr,
fuel_consumption_var_pct_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_index,
pulverization_quality,
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 engine_rpm_drop_on_straight_1st_pass ,
engine_rpm_drop_on_turn engine_rpm_drop_on_turn_1st_pass,
depth_of_cut_cm,
no_load_speed_kmph::numeric,
on_load_speed_kmph::numeric,
wheel_slippage_::numeric wheel_slippage_pct,
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 ,
fuel_consumption_lit_hr_2::numeric ,
area_covered_acr_hr_2::numeric ,
fuel_consumption_lit_acr_2::numeric ,
engine_smoke_on_load,
engine_acceleration_smoke ,
draft_response,
tractor_steer_ability,
tractor_braking_performance,
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_index,
pulverization_quality,
file_syspk,
file_mnemonic,
file_sheet_mnemonic 
from staging2.ftdry_sum_test_condition_2_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.ftdry_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.ftdry_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.ftdry_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.ftdry_sum_comments_by_block b
where a.file_syspk=b.file_syspk and a.test_iteration_number =4;





update transactional.field_perf_summary 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 test_iteration_number =1 and a.file_syspk =__files_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 =__files_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 =__files_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 =__files_syspk;


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

update transactional.field_perf_summary
set test_instance_id=__test_instance_id
where file_syspk=__files_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_turn_1st_pass=replace(engine_rpm_drop_on_turn_1st_pass,' to ','-'),
depth_of_cut_cm=replace(depth_of_cut_cm,' to ','-')
where file_mnemonic = 'FTDRY';

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

	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 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 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 = 'FTDRY';

update transactional.field_perf_summary_tractor_info
set test_instance_id=__test_instance_id
where file_syspk=__files_syspk;
perform  fw_core.fn_insert_db_error ( __client_id, __function_id, 1001, 'Compegence', __files_syspk ,__file_mnemonic,__file_sheet_mnemonic ,null,'trx', 'fn_FTDRY_SUM_TRX', err_state, err_msg, err_detail, err_hint, err_context,'success');

end
$function$
;
