drop function if exists staging2.fn_BUDNI_BRK_TRX;
CREATE OR REPLACE FUNCTION staging2.fn_BUDNI_BRK_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_BRK_TRX
Function Desc: This function populates data into ODS
File Format: BUDNI
Sheet Format: BUDNI_BRK
Creation Date: 
Updation Date:
Author: compegence team
Function Call: select staging2.fn_BUDNI_BRK_TRX()
***************************************************************/
	 

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,
brake_type,
brake_free_play_lh,
brake_free_play_rh,
unballasted_max_speed_kmph,
road_ballasted_max_speed_kmph
)
select 
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
make,
model,
tractor_HP::numeric tractor_engine_hp,
Type_of_brake,
brake_free_play_lh::numeric ,
brake_free_play_rh::numeric ,
maximum_attainable_speed_kmph_unballasted::numeric ,
maximum_attainable_speed_kmph_ballasted::numeric 
from staging2.BUDNI_BRK_Spec_H1_block where trx_record =1;


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_BRK_Spec_H1_block where trx_record =1;
 
 insert into transactional.test_instance_engine_info
 (
 client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
tractor_make,
tractor_model,
rated_rpm
)
select 
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
make,model,
rated_rpm::int
from staging2.BUDNI_BRK_Spec_H1_block where trx_record =1;


/*block */

insert into transactional.budni_brake_perf_service_brake_test_results
(
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
tractor_make,
tractor_model,
brake_test_type,
brake_test_name,
speed_condition,
ballast_condition,
braking_device_control_force_n,
mean_deceleration_msec2,
stopping_dist_m
)
select 
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
make,model,
brake_test_type,
brake_test_name,
speed_condition,
ballast_condition,
Braking_device_control_force_N::numeric ,
Mean_deceleration_m_sec2::numeric ,
Stopping_distance_m ::numeric 
from  staging2.budni_brk_service_cold_ballasted_25kmph_block where trx_record =1;

insert into transactional.budni_brake_perf_service_brake_test_results
(
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
tractor_make,
tractor_model,
brake_test_type,
brake_test_name,
speed_condition,
ballast_condition,
braking_device_control_force_n,
mean_deceleration_msec2,
stopping_dist_m
)
select 
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
make,model,
brake_test_type,
brake_test_name,
speed_condition,
ballast_condition,
Braking_device_control_force_N::numeric ,
Mean_deceleration_m_sec2::numeric ,
Stopping_distance_m ::numeric 
from  staging2.budni_brk_service_cold_ballasted_maximum_block where trx_record =1;

insert into transactional.budni_brake_perf_service_brake_test_results
(
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
tractor_make,
tractor_model,
brake_test_type,
brake_test_name,
speed_condition,
ballast_condition,
braking_device_control_force_n,
mean_deceleration_msec2,
stopping_dist_m
)
select 
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
make,model,
brake_test_type,
brake_test_name,
speed_condition,
ballast_condition,
Braking_device_control_force_N::numeric ,
Mean_deceleration_m_sec2::numeric ,
Stopping_distance_m ::numeric 
from  staging2.budni_brk_service_cold_standard_25kmph_block where trx_record =1;

insert into transactional.budni_brake_perf_service_brake_test_results
(
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
tractor_make,
tractor_model,
brake_test_type,
brake_test_name,
speed_condition,
ballast_condition,
braking_device_control_force_n,
mean_deceleration_msec2,
stopping_dist_m
)
select 
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
make,model,
brake_test_type,
brake_test_name,
speed_condition,
ballast_condition,
Braking_device_control_force_N::numeric ,
Mean_deceleration_m_sec2::numeric ,
Stopping_distance_m ::numeric 
from  staging2.budni_brk_service_cold_standard_maximum_block where trx_record =1;


insert into transactional.budni_brake_perf_service_brake_test_results
(
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
tractor_make,
tractor_model,
brake_test_type,
brake_test_name,
speed_condition,
ballast_condition,
braking_device_control_force_n,
mean_deceleration_msec2,
stopping_dist_m
)
select 
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
make,model,
brake_test_type,
brake_test_name,
speed_condition,
ballast_condition,
Braking_device_control_force_N::numeric ,
Mean_deceleration_m_sec2::numeric ,
Stopping_distance_m ::numeric 
from  staging2.budni_brk_service_fade_standard_maximum_block where trx_record =1;

insert into transactional.budni_brake_perf_service_brake_test_results
(
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
tractor_make,
tractor_model,
brake_test_type,
brake_test_name,
speed_condition,
ballast_condition,
braking_device_control_force_n,
mean_deceleration_msec2,
stopping_dist_m
)
select 
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
make,model,
brake_test_type,
brake_test_name,
speed_condition,
ballast_condition,
Braking_device_control_force_N::numeric ,
Mean_deceleration_m_sec2::numeric ,
Stopping_distance_m ::numeric 
from  staging2.budni_brk_service_fade_ballasted_25kmph_block;



update transactional.budni_brake_perf_service_brake_test_results
set max_dev_of_tractor_from_its_org_course_m =column3,
abnormal_vibration=column5,
the_brakes_were_heated_by =column7
from staging2.budni_brk_other_observations_Block where trx_record =1;

insert into transactional.budni_brake_perf_parking_brake_test_results
(
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
tractor_make,
tractor_model,
test_condition 
)
select
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
make,
model,
particulars 
from staging2.budni_brk_parking_barke_test_block where trx_record =1;

update transactional.budni_brake_perf_parking_brake_test_results a
set braking_device_control_force_n_facing_up = (
select Braking_device_control_force::numeric 
from staging2.budni_brk_parking_barke_test_block b
where facing='Facing up' and particulars='18 percent slope' and a.file_syspk=__file_syspk) 
where test_condition='18 percent slope' and a.file_syspk =__file_syspk;

update transactional.budni_brake_perf_parking_brake_test_results a
set braking_device_control_force_n_facing_down = (
select Braking_device_control_force::numeric 
from staging2.budni_brk_parking_barke_test_block b
where facing='Facing Down' and particulars='18 percent slope' and a.file_syspk=__file_syspk)
where test_condition='18 percent slope'  and a.file_syspk =__file_syspk ;

update transactional.budni_brake_perf_parking_brake_test_results a
set efficacy_of_parking_brake_facing_down = (
select Efficacy_of_parking_brake
from staging2.budni_brk_parking_barke_test_block b
where facing='Facing Down' and particulars='18 percent slope' and a.file_syspk=__file_syspk)
where test_condition='18 percent slope'  and a.file_syspk =__file_syspk;

update transactional.budni_brake_perf_parking_brake_test_results a
set efficacy_of_parking_brake_facing_up = (
select Efficacy_of_parking_brake
from staging2.budni_brk_parking_barke_test_block b
where facing='Facing up' and particulars='18 percent slope' and a.file_syspk=__file_syspk)
where test_condition='18 percent slope' and a.file_syspk =__file_syspk;

update transactional.budni_brake_perf_parking_brake_test_results a
set braking_device_control_force_n_facing_up = (
select Braking_device_control_force::numeric 
from staging2.budni_brk_parking_barke_test_block b
where facing='Facing up' and particulars='12 percent slope with trailer of 2.55 tonnes.'  and a.file_syspk=__file_syspk)
where test_condition='12 percent slope with trailer of 2.55 tonnes.' and a.file_syspk =__file_syspk;

update transactional.budni_brake_perf_parking_brake_test_results a
set braking_device_control_force_n_facing_down = (
select Braking_device_control_force::numeric 
from staging2.budni_brk_parking_barke_test_block b
where facing='Facing Down' and particulars='12 percent slope with trailer of 2.55 tonnes.' and a.file_syspk=__file_syspk)
where test_condition='12 percent slope with trailer of 2.55 tonnes.' and a.file_syspk =__file_syspk;

update transactional.budni_brake_perf_parking_brake_test_results a
set efficacy_of_parking_brake_facing_down = (
select Efficacy_of_parking_brake 
from staging2.budni_brk_parking_barke_test_block b
where facing='Facing Down' and particulars='12 percent slope with trailer of 2.55 tonnes.' and a.file_syspk=__file_syspk)
where test_condition='12 percent slope with trailer of 2.55 tonnes.' and a.file_syspk =__file_syspk ;

update transactional.budni_brake_perf_parking_brake_test_results a
set efficacy_of_parking_brake_facing_up = (
select Efficacy_of_parking_brake
from staging2.budni_brk_parking_barke_test_block
where facing='Facing up' and particulars='12 percent slope with trailer of 2.55 tonnes.'  and a.file_syspk=__file_syspk)
where test_condition='12 percent slope with trailer of 2.55 tonnes.' and a.file_syspk =__file_syspk;

delete from transactional.budni_brake_perf_parking_brake_test_results a
using transactional.budni_brake_perf_parking_brake_test_results b
where a.syspk < b.syspk  and a.test_condition =b.test_condition  and a.file_syspk =b.file_syspk;


insert into transactional.budni_test_observations
(
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
tractor_make,
tractor_model,
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,
column4,
column5,
column6,
column7,
column8
from staging2.BUDNI_BRK_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_brake_perf_parking_brake_test_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_brake_perf_service_brake_test_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_BRK_TRX', err_state, err_msg, err_detail, err_hint, err_context,'success');

end
$function$
;



