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

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

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

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

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

delete from mmt_ods.budni_pto_perf_results_summary  where test_file_sheet_format='BUDNI_PTO';

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

delete from mmt_ods.budni_pto_perf_results_details  where test_file_sheet_format='BUDNI_PTO';

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,
configuration,
fip_type,
steering_type,
transmission_type,
wheel_drive_type
)
select 
client_id,
function_id,
file_syspk,
file_format,
sheet_mnemonic,
make,
model,
tractor_HP::int,
configuration,FIP_type,steering_type,transmission_type,wheel_drive_type
from mmt_staging2.BUDNI_PTO_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,
make,model 
from 
 mmt_staging2.BUDNI_PTO_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,
engine_to_pto_ratio_540_pto
)
select 
client_id,
function_id,
file_syspk,
file_format,
sheet_mnemonic,
make,model,
rated_rpm::int,
engine_to_pto_ratio
from mmt_staging2.BUDNI_PTO_Spec_H1_block;


/*block */

insert into mmt_ods.budni_pto_perf_results_summary
(
client_id,
function_id,
test_file_ref_no,
test_file_format,
test_file_sheet_format,
tractor_make,
tractor_model,
pto_category,
pto_sub_category ,
power_kw,
speed_rpm_pto ,
speed_rpm_engine ,
fuel_consumption_ltr_per_hr,
fuel_consumption_kg_per_hr,
specific_kg_per_kwh,
specifc_energy_kwhl 
)
select 
client_id,
function_id,
file_syspk,
file_format,
sheet_mnemonic,
make,model,
column2,
column3,
column4::numeric,column5::numeric,column6::numeric,column7::numeric,column8::numeric,
column9::numeric,column10::numeric
from mmt_staging2.BUDNI_PTO_Perf_Report_block where ods_record=1
order by rank;


/*block */
insert into mmt_ods.budni_pto_perf_results_details
(
client_id,
function_id,
test_file_ref_no,
test_file_format,
test_file_sheet_format,
tractor_make,
tractor_model,
ambient_type,
no_load_max_engine_speed_rpm,
equi_crankshaft_torque_at_max_power_nm,
max_equi_crankshaft_torque_nm,
engine_speed_at_max_equi_crankshaft_torque_rpm,
backup_torque,
pct_smoke_level_max_light_absorption_coef_per_mtr,
range_of_atm_conditions_temp_c,
range_of_atm_conditions_pressure_kpa,
range_of_atm_conditions_rel_humidity_pct
)
select 
client_id,
function_id,
file_syspk,
file_format,
sheet_mnemonic,
make,model,
column3,
column4::int,
column5::numeric,
column6::numeric,column7::int,column8::numeric,
column9::numeric,column10,column11,column12
from mmt_staging2.BUDNI_PTO_MMTKeyword1_block
where ods_record=1;


/* block */

insert into mmt_ods.budni_pto_perf_results_details
(
client_id,
function_id,
test_file_ref_no,
test_file_format,
test_file_sheet_format,
tractor_make,
tractor_model,
ambient_type,
max_temp_engine_oil_c,
max_temp_coolant_water_c,
max_temp_fuel_c,
max_temp_air_intake_c,
max_temp_exhaust_gas_c,
pressure_at_max_power_intake_air_kpa,
pressure_at_max_power_exhst_gas_bfr_turbo_charger_kpa,
consumptions_lub_oil_gkwh,
consumptions_coolant_water_pct_of_total_coolant_capacity
)
select client_id,
function_id,
file_syspk,
file_format,
sheet_mnemonic,
make,model,
column3,
column4::numeric,column5::numeric,column6::numeric,column7::numeric,column8::numeric,column9::numeric,
column10,column11::numeric,column12::numeric
from mmt_staging2.BUDNI_PTO_MMTKeyword2_block
where ods_record=1 and column3='Natural Ambient';

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_PTO_Summary_block
where rank in (3,4) and ods_record=1
;

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

update  mmt_ods.budni_pto_perf_results_details
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;




