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

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

delete from mmt_ods.test_instance_engine_info where test_file_ref_no=261;
delete from mmt_ods.test_instance_tractor_info  where test_file_ref_no=261;
delete from mmt_ods.budni_drawbar_perf_results  where test_file_ref_no=261;
	 
 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,
configuration,
Transmission_type,
 Wheel_Drive_Type,
 Steering_type,
 hitch_height_mm,
 Ballast_condition
 )
 select 
 client_id,
function_id,
file_syspk ,
file_format,
sheet_mnemonic,
 column3,
 column4,
  column7,
  column8,
  column9,
  column11,
  column13::int,
  column14
 from mmt_staging2.BUDNI_DBP_Spec_H1_Block where ods_record =1 ;

 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,
type_of_road
 )
 select 
 client_id,
function_id,
file_syspk ,
file_format,
sheet_mnemonic,
 column3,
 column4,
 column15
 from mmt_staging2.BUDNI_DBP_Spec_H1_Block where ods_record =1 ;

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,
engine_to_pto_ratio_540_pto
 )
 select 
 client_id,
function_id,
file_syspk ,
file_format,
sheet_mnemonic,
 column3,
 column4,
 column6::int,
 column12
 from mmt_staging2.BUDNI_DBP_Spec_H1_Block where ods_record =1 ;


insert into mmt_ods.budni_drawbar_perf_results
(
client_id,
function_id,
test_file_ref_no,
test_file_format,
test_file_sheet_format,
tractor_make,
tractor_model,
test_condtion,
gear_used,
travel_speed_km_per_hr,
drawbar_power_kw,
drawbar_pull_kn,
engine_speed_rpm,
wheel_slippage_pct ,
fuel_consumption_kg_per_kwh,
fuel_consumption_ltr_per_hr ,
specific_energy_kwh_per_ltr, 
atmosp_conditions_pressure_kpa,
atmosp_conditions_rh_pct,
atmosp_conditions_temp_c,
temp_trans_oil_c,
temp_coolant_c,
temp_fuel_c,
temp_engine_oil_c
)
select 
client_id,function_id,file_syspk,file_format,sheet_mnemonic,make,model,
column2,column3,column4::float,column5::float,column6::float,column7::float,
column8::float,column9::float,column10::float,column11::float,column12,column13,
column14,column15,column16,column17,column18
from  mmt_staging2.BUDNI_DBP_Drawbar_Perf_Block where ods_record = 1;


select file_syspk into __file_syspk from mmt_staging2.BUDNI_DBP_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.budni_drawbar_perf_results
set test_instance_id=__test_instance_id
where test_file_ref_no=__file_syspk;


end
$$ LANGUAGE plpgsql;




 
