drop function if exists staging2.fn_BUDNI_HDL_TRX;
CREATE OR REPLACE FUNCTION staging2.fn_BUDNI_HDL_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_HDL_TRX
Function Desc: This function populates data into ODS
File Format: BUDNI
Sheet Format: BUDNI_HDL
Creation Date: 
Updation Date:
Author: compegence team
Function Call: select staging2.fn_BUDNI_HDL_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,
steering_type
)
select 
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
make,
model,
tractor_HP::int,
steering_type
from staging2.BUDNI_HDL_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
)
select 
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
make,
model
from 
 staging2.BUDNI_HDL_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
)
select 
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
make,
model,
rated_rpm::int
from staging2.BUDNI_HDL_Spec_H1_block;

insert into transactional.test_instance_tractor_info_misc
 (
 client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
tractor_make,
tractor_model,
hydraulic_type,
pump_speed_at_rated_engine_speed_rpm,
oil,
lift_rod_type_lh,
lift_rod_type_rh,
lift_rod_length_lh,
lift_rod_length_rh,
lower_link_length_lh,
lower_link_length_rh,
pump_hp
)
select 
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
make,
model,
hydraulic_type,
Pump_speed_at_rated_engine_speed_rpm::numeric,	
Oil,
Lift_Rod_type_LH,			
Lift_Rod_type_RH,
Lift_Rod_Length_LH::numeric,
Lift_Rod_Length_RH::numeric,
Lower_link_Length_LH::numeric,
Lower_link_Length_RH::numeric,
Pump_HP::numeric 
from staging2.BUDNI_HDL_Spec_H1_block;


/*block */

insert into transactional.budni_hydraulic_power_test_results
(
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
tractor_make,
tractor_model,
pump_dlvry_rate_at_min_pressure_engine_speed_l_per_min,
max_hydraulic_power_kw,
pump_dlvry_rate_at_max_hydraulic_power_lmin,
pressure_at_max_hydraulic_power_mpa,
sustained_pressure_of_the_open_relief_valve_mpa,
tapping_point_relief_valve_test,
tapping_point_pump_perf_test,
tapping_point_temperature_of_hydraulic_fluid_c
)
select 
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
make,model,
Pump_delivery_rate_at_min_pressure_and_rated_engine::numeric,
Maximum_hydraulic_power_kW::numeric,
 Pump_delivery_rate_at_maximum_hydraulic_power_l_min::numeric,
 Pressure_at_maximum_hydraulic_power_MPa::numeric,
 Sustained_pressure_of_the_open_relief_valve_MPa::numeric,
tapping_point_relief_valve_test,
tapping_point_Pump_performance_test,
tapping_point_Temperature_of_hydraulic_fluid_C 
from staging2.budni_hdl_power_test_block  where trx_record=1
order by block_row_number;


insert into transactional.budni_hydraulic_maint_of_lift_load_results
(
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
tractor_make,
tractor_model,
force_applied_at_the_frame_kn,
temp_of_hydraulic_fluid_at_the_start_of_test_c,
elapsed_time_min,
cumulative_drop_in_height_of_lift_mm
)
select
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
make,
model,
Force_applied_at_the_frame_kN::numeric,
Temperature_of_hydraulic_fluid_at_the_start_of_test_C,
Elapsed_time_minute::numeric,
Cumulative_drop_in_height_of_lift_mm::numeric
from staging2.budni_hdl_maintenance_of_lift_load_block;

/*block */
insert into transactional.budni_hydraulic_lifting_capacity_test_results
(
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
tractor_make,
tractor_model,
test_condition,
height_of_lower_hitch_pt_above_ground_in_down_position_mm,
vertical_move_ment_with_lifting_forces_mm,
max_force_exerted_through_full_range_kn,
corresponding_pressure_mpa,
moment_about_rear_axle_kn_m,
max_tilt_angle_of_mast_from_vertical_degrees
)
select 
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
make,
model,
column5,
column6::numeric,
column7::numeric,
column8::numeric,
column9::numeric,
column10::numeric,
column11::numeric
from staging2.budni_hdl_Lifting_Capacity_Test_Block
where trx_record=1;


/* block */


insert into transactional.budni_test_observations
( 
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
tractor_make,
tractor_model,
srl_no,
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,column7,column8,column10,column11,column12 
from staging2.budni_hdl_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.test_instance_tractor_info_misc
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_hydraulic_maint_of_lift_load_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_hydraulic_power_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_hydraulic_lifting_capacity_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;



update transactional.test_instance 
set test_type = a.test_type,
test_name= a.test_name
from fw_core.test_file_sheet_format_master a 
where transactional.test_instance.file_sheet_mnemonic = a.file_sheet_mnemonic
and transactional.test_instance.file_syspk=__file_syspk;

update transactional.test_instance
set test_file_name =(select a.file_name from staging1.staging_generic_table a where file_syspk = __file_syspk limit 1)
where file_syspk = __file_syspk;


update transactional.test_instance b
set date_of_test = (select a.date_of_test from  transactional.test_instance a where a.date_Of_test is not null and a.file_syspk = __file_syspk and  a.file_mnemonic = 'BUDNI' and file_sheet_mnemonic='BUDNI_PTO')::date
where 
b.date_Of_test is null 
and b.file_syspk = __file_syspk
and  b.file_mnemonic = 'BUDNI';



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

end
$function$
;




