drop function if exists staging2.fn_budni_hlg_TRX;
CREATE OR REPLACE FUNCTION staging2.fn_budni_hlg_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;   
   declare __test_master_id int;
   declare __test_instance_tractor_id int;
 begin
__file_syspk := p_file_syspk;

/************************************************************ 
Function Name:fn_BUDNI_HLG_TRX
Function Desc: This function populates data into ODS
File Format: BUDNI
Sheet Format: BUDNI_HLG
Creation Date: 
Updation Date:
Author: compegence team
Function Call: select staging2.fn_BUDNI_HLG_TRX()
***************************************************************/

/*select file_syspk into __file_syspk from staging2.BUDNI_HLG_Spec_H1_block; */

insert into transactional.test_instance_tractor_info 
(
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
tractor_make,
tractor_model,
tractor_engine_hp,
configuration,
transmission_type,
wheel_drive_type,
fip_type,
steering_type,
ballast_condition
)
select 
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
make,
model,
tractor_HP::int tractor_engine_hp,
configuration,
transmission_type,
wheel_drive_type,
FIP_type,
steering_type,
Ballast_Condition
from staging2.budni_hlg_Spec_H1_block;

update transactional.test_instance_tractor_info 
set mahindra_model_yn = (
case when tractor_make  like 'Mahindra%' then 'Y' else 'N' end
) where file_syspk =__file_syspk;

update transactional.test_instance_tractor_info a
set test_tractor_yn ='Y' where syspk in 
(select min(syspk) from transactional.test_instance_tractor_info b
where b.file_syspk =a.file_syspk)
and a.file_syspk =__file_syspk;

insert into transactional.test_instance
(
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
tractor_make,
tractor_model,
type_of_road 
)
select 
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
make,model,
type_of_track
from 
 staging2.budni_hlg_Spec_H1_block;
 
 insert into transactional.test_instance_engine_info
 (
 client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
tractor_make,
tractor_model,
rated_rpm,
engine_to_pto_ratio_540_pto
)
select 
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
make,model,
rated_rpm::int,
engine_to_pto_ratio engine_to_pto_ratio_540_pto
from staging2.budni_hlg_Spec_H1_block;


/*block */

insert into transactional.budni_haulage_perf_results
(
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
tractor_make,
tractor_model,
trailer_type,
gross_mass_of_trailer_tonne,
height_of_trailer_hitch_above_ground_level_mm,
gear_used_negotiating_slopes_up_to_8pct,
avg_travel_speed_kmph,
avg_fuel_consumption_ltr_per_hr,
avg_fuel_consumption_ml_per_km_per_tonne,
avg_dist_traveled_per_litre_of_fuel_consumption_km,
effectiveness_of_brakes,
maneuverability_of_tractor_trailer_combination
)
select 
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
make,model,
type_of_trailer,
Gross_mass_of_trailer_tonne::numeric,
Height_of_trailer_hitch_above_ground_level_mm::numeric,
Gear_used_during_the_test_for_negotiating_slopes_up_to_8,	
Average_travel_speed_kmph,
l_h,
ml_km_tonne,
Average_distance_traveled_per_litre_of_fuel_consumption_km,	
Effectiveness_of_brakes,
Maneuverability_of_tractor_trailer_combination
from  staging2.BUDNI_HLG_Perf_Test_block where trx_record=1
order by block_row_number;



insert into transactional.budni_test_observations
(
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
tractor_make,
tractor_model,
sequence_number , 
characteristic,
category_evaluative_or_non_evaluative,
requirements_as_per_is_12207_2008,
values_declared_by_the_applicant_d_rqmt,
as_observed,
whether_meets_the_requirements_yn
)
select 
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
make,model,
column3::numeric ,
column4,
column5,
column6,
column7,
column8,
column9
from staging2.budni_hlg_test_obs_summary_block where trx_record=1;


select syspk into __test_instance_id from transactional.test_instance where file_syspk =__file_syspk;
select tractor_model into __model from transactional.test_instance where file_syspk =__file_syspk;
select tractor_make into __make from transactional.test_instance where file_syspk =__file_syspk;
select syspk  from transactional.test_master into __test_master_id where test_type ='BUDNI';
select syspk into __test_instance_tractor_id  from transactional.test_instance_tractor_info where file_syspk =__file_syspk;


update transactional.test_instance 
set test_master_id =__test_master_id,
test_tractor_id =__test_instance_tractor_id
where file_syspk=__file_syspk;

update transactional.test_instance_engine_info
set test_instance_id=__test_instance_id,
test_instance_tractor_id = __test_instance_tractor_id,
tractor_model =__model,
tractor_make=__make
where file_syspk=__file_syspk;


update transactional.test_instance_tractor_info
set test_instance_id=__test_instance_id,
tractor_model =__model,
tractor_make=__make
where file_syspk=__file_syspk;



update transactional.budni_haulage_perf_results
set test_instance_id=__test_instance_id,
test_instance_tractor_id = __test_instance_tractor_id,
tractor_model =__model,
tractor_make=__make
where file_syspk=__file_syspk;


update transactional.budni_test_observations
set test_instance_id=__test_instance_id,
test_instance_tractor_id = __test_instance_tractor_id,
tractor_model =__model,
tractor_make=__make
where 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_budni_hlg_TRX', err_state, err_msg, err_detail, err_hint, err_context,'success');

end
$function$
;




