drop function if exists mmt_staging2.fn_BUDNI_HDL_ODS;
CREATE OR REPLACE FUNCTION mmt_staging2.fn_BUDNI_HDL_ODS()
   RETURNS void AS $$
    declare __test_instance_id int;
    declare __file_syspk int;
 begin

/************************************************************ 
Function Name:fn_BUDNI_HDL_ODS
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 mmt_staging2.fn_BUDNI_HDL_ODS()
***************************************************************/

delete from  mmt_ods.test_instance 
where test_file_sheet_format='BUDNI_HDL';

delete from  mmt_ods.test_instance_engine_info 
where test_file_sheet_format='BUDNI_HDL';

delete from  mmt_ods.test_instance_tractor_info
where test_file_sheet_format='BUDNI_HDL';

delete from  mmt_ods.test_instance_tractor_info_misc
where test_file_sheet_format='BUDNI_HDL';

delete from mmt_ods.budni_hydraulic_maint_of_lift_load_results 
where test_file_sheet_format='BUDNI_HDL';

delete from mmt_ods.budni_hydraulic_power_test_results
where test_file_sheet_format='BUDNI_HDL';

delete from mmt_ods.budni_hydraulic_lifting_capacity_test_results 
where test_file_sheet_format='BUDNI_HDL';

delete from mmt_ods.budni_test_observations 
where test_file_sheet_format='BUDNI_HDL';


insert into mmt_ods.test_instance_tractor_info 
(
client_id,
function_id,
test_file_ref_no,
test_file_format,
test_file_sheet_format,
tractor_make,
tractor_model,
tractor_engine_hp,
steering_type
)
select 
client_id,
function_id,
file_syspk,
file_format,
sheet_mnemonic,
tractor_make,
tractor_model,
tractor_HP::int,
steering_type
from mmt_staging2.BUDNI_HDL_Spec_H1_block;

insert into mmt_ods.test_instance
(
client_id,
function_id,
test_file_ref_no,
test_file_format,
test_file_sheet_format,
tractor_make,
tractor_model
)
select 
client_id,
function_id,
file_syspk,
file_format,
sheet_mnemonic,
tractor_make,
tractor_model
from 
 mmt_staging2.BUDNI_HDL_Spec_H1_block;
 
 insert into mmt_ods.test_instance_engine_info
 (
 client_id,
function_id,
test_file_ref_no,
test_file_format,
test_file_sheet_format,
tractor_make,
tractor_model,
rated_rpm
)
select 
client_id,
function_id,
file_syspk,
file_format,
sheet_mnemonic,
tractor_make,
tractor_model,
rated_rpm::int
from mmt_staging2.BUDNI_HDL_Spec_H1_block;

insert into mmt_ods.test_instance_tractor_info_misc
 (
 client_id,
function_id,
test_file_ref_no,
test_file_format,
test_file_sheet_format,
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_format,
sheet_mnemonic,
tractor_make,
tractor_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 mmt_staging2.BUDNI_HDL_Spec_H1_block;


/*block */

insert into mmt_ods.budni_hydraulic_power_test_results
(
client_id,
function_id,
test_file_ref_no,
test_file_format,
test_file_sheet_format,
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,
sustained_pressure_of_the_open_relief_valve_mpa,
pressure_at_max_hydraulic_power_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_format,
sheet_mnemonic,
make,model,
Pump_delivery_rate_at_minimum_pressure_and_rated_engine_speed_l_min::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 mmt_staging2.budni_hdl_power_test_block  where ods_record=1
order by rank;


insert into mmt_ods.budni_hydraulic_maint_of_lift_load_results
(
client_id,
function_id,
test_file_ref_no,
test_file_format,
test_file_sheet_format,
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_format,
sheet_mnemonic,
make,
model,
Force_applied_at_the_frame_kN::numeric,
Temperature_of_hydraulic_fluid_at_the_start_of_test_C::numeric,
Elapsed_time_minute::numeric,
Cumulative_drop_in_height_of_lift_mm::numeric
from mmt_staging2.budni_hdl_maintenance_of_lift_load_block;

/*block */
insert into mmt_ods.budni_hydraulic_lifting_capacity_test_results
(
client_id,
function_id,
test_file_ref_no,
test_file_format,
test_file_sheet_format,
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_format,
sheet_mnemonic,
make,
model,
column5,
column6::numeric,
column7::numeric,
column8::numeric,
column9::numeric,
column10::numeric,
column11::numeric
from mmt_staging2.budni_hdl_Lifting_Capacity_Test_Block
where ods_record=1;


/* block */


insert into mmt_ods.budni_test_observations
( 
client_id,
function_id,
test_file_ref_no,
test_file_format,
test_file_sheet_format,
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_format,
sheet_mnemonic,
make,model,
column3,column4,column7,column8,column10,column11,column12 
from mmt_staging2.budni_hdl_test_obs_summary_block
where ods_record=1
;


select file_syspk into __file_syspk from mmt_staging2.BUDNI_FLD_Spec_H1_block;
select syspk into __test_instance_id from mmt_ods.test_instance where test_file_ref_no =__file_syspk;


update mmt_ods.test_instance_engine_info
set test_instance_id=__test_instance_id
where test_file_ref_no=__file_syspk;


update mmt_ods.test_instance_tractor_info
set test_instance_id=__test_instance_id
where test_file_ref_no=__file_syspk;


update mmt_ods.test_instance_tractor_info_misc
set test_instance_id=__test_instance_id
where test_file_ref_no=__file_syspk;

update mmt_ods.budni_hydraulic_maint_of_lift_load_results
set test_instance_id=__test_instance_id
where test_file_ref_no=__file_syspk;

update mmt_ods.budni_hydraulic_power_test_results
set test_instance_id=__test_instance_id
where test_file_ref_no=__file_syspk;

update mmt_ods.budni_hydraulic_lifting_capacity_test_results
set test_instance_id=__test_instance_id
where test_file_ref_no=__file_syspk;


update mmt_ods.budni_test_observations
set test_instance_id=__test_instance_id
where test_file_ref_no=__file_syspk;



end
$$ LANGUAGE plpgsql;




