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

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

delete from  mmt_ods.test_instance where test_file_sheet_format='BUDNI_FLD';
	
delete from  mmt_ods.test_instance_engine_info where test_file_sheet_format='BUDNI_FLD';

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

delete from  mmt_ods.test_instance_implement_info where test_file_sheet_format='BUDNI_FLD';

delete from mmt_ods.budni_field_perf_results where test_file_sheet_format='BUDNI_FLD';

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

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,
transmission_type,
wheel_drive_type,
fip_type,
steering_type,
ballast_condition
)
select 
client_id,
function_id,
file_syspk,
file_format,
sheet_mnemonic,
make,
model,
tractor_HP::int tractor_engine_hp,
configuration,
transmission_type,
wheel_drive_type,
FIP_type,
steering_type,
Ballast_Condition
from mmt_staging2.budni_fld_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_fld_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 engine_to_pto_ratio_540_pto
from mmt_staging2.budni_fld_Spec_H1_block;

insert into mmt_ods.test_instance_implement_info
(
client_id,
function_id,
test_file_ref_no,
test_file_format,
test_file_sheet_format,
name_of_implement,
make_of_implement,
type_of_implement,
no_of_disc_blades_bottoms,
type_of_disc_blades_bottoms,
size_of_bottoms_blades_mm,
size_of_bottoms_blades2_mm,
size_of_bottoms_blades3_mm, 
spacing_of_bottoms_flanges_mm,
span_for_mounted_implement_mm,
mast_height_for_mounted_implement_mm,
length_mm,
width_mm,
height_mm,
gross_mass_kg
)
select 
client_id,
function_id,
file_syspk,
file_format,
sheet_mnemonic,
items,
Make,
type,
No_of_Disc_blades,
Type_of_Disc_blades,
Size_of_bottoms_blades_1_mm::numeric,
Size_of_bottoms_blades_2_mm::numeric,
Size_of_bottoms_blades_3_mm::numeric,
Spacing_of_bottoms_flanges_mm::numeric,
Lower_hitch_point_span_mm::numeric,
Mast_height_mm,
Overall_dimensions_mm_length::numeric,
Overall_dimensions_mm_Width::numeric,
Overall_dimensions_mm_Height::numeric,
Gross_mass_kg::numeric
from mmt_staging2.BUDNI_FLD_Impl_Test_Block
order by rank;



insert into mmt_ods.test_instance_implement_info
(
client_id,
function_id,
test_file_ref_no,
test_file_format,
test_file_sheet_format,
name_of_implement,
type_of_implement,
cage_wheel_dia_mm,
cage_wheel_width_mm,
no_and_types_of_lugs,
size_of_angle_section_mm,
size_of_angle_section2_mm,
size_of_angle_section3_mm,
length_of_lugs_mm,
spacing_of_lugs_mm,
cage_wheel_weight_kg 
)
select 
client_id,
function_id,
file_syspk,
file_format,
sheet_mnemonic,
Items,
type,
Dia_mm::numeric,
Width_mm::numeric,
No_and_types_of_lugs,
Size_of_angle_section_1_mm::numeric,
Size_of_angle_section_2_mm::numeric,
Size_of_angle_section_3_mm::numeric,
Length_of_lugs_mm::numeric,
Spacing_of_lugs_mm::numeric,
Weight_of_each_cage_wheels_kg::numeric
from mmt_staging2.BUDNI_FLD_Cage_Wheel_Block
order by rank;



insert into mmt_ods.budni_field_perf_results
(
client_id,
function_id,
test_file_ref_no,
test_file_format,
test_file_sheet_format,
tractor_make,
tractor_model,
operation_type,
type_of_soil,
avg_soil_moisture_pct_,
bulk_density_of_soil_g_per_cc,
cone_index_kgf_per_sq_cm,
gear_used,
avg_speed_of_operation_kmph,
avg_travel_reduction_pct,
avg_depth_of_puddle_cm,
avg_working_width_cm,
area_covered_ha_per_hr,
fuel_consumption_ltr_per_hr,
fuel_consumption_ltr_per_ha,
avg_draft_of_implement_kn 
)
select 
client_id,
function_id,
file_syspk,
file_format,
sheet_mnemonic,
make,
model,
Parameter_operation,
Type_of_soil,
Av_soil_moisture_Av_depth_of_standing_water_cm,
Bulk_density_of_soil_g_cc,
Cone_index_kgf_sq_cm_Puddling_index,
Gear_used,
Av_speed_of_operation_kmph,
Av_wheel_slip_Av_Travel_reduction,
Av_depth_of_cut_cm_Av_Depth_of_puddle_cm,
Av_working_width_cm,
Area_covered_ha_h,
Fuel_consumption_l_h,
Fuel_consumption_l_ha,
Av_draft_of_implement_kN
from mmt_staging2.BUDNI_FLD_Sum_Perf_Test_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,
column5,
column6,
column7,
column8,
column9
from mmt_staging2.budni_fld_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_implement_info
set test_instance_id=__test_instance_id
where test_file_ref_no=__file_syspk;

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




