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

/************************************************************ 
Function Name:fn_IHT_HAM_ODS
Function Desc: This function populates data into ODS
File Format: IHT
Sheet Format: IHT_HAM
Creation Date: 
Updation Date:
Author: compegence team
Function Call: select mmt_staging2.fn_IHT_HAM_ODS()
***************************************************************/
delete from  mmt_ods.test_instance where test_file_sheet_format='IHT_HAM';
delete from mmt_ods.test_instance_tractor_info where test_file_sheet_format='IHT_HAM';
delete from  mmt_ods.test_instance_engine_info where test_file_sheet_format='IHT_HAM';
delete from mmt_ods.test_instance_tyre_info where test_file_sheet_format='IHT_HAM';
delete from mmt_ods.test_instance_atmospheric_info where test_file_sheet_format='IHT_HAM';
delete from mmt_ods.test_instance_atmospheric_info where test_file_sheet_format='IHT_HAM';
delete from mmt_ods.test_instance_drawbar_info where test_file_sheet_format='IHT_HAM';
delete from mmt_ods.test_instance_weight_reaction where test_file_sheet_format='IHT_HAM';
delete from mmt_ods.iht_hot_air_mapping_results where test_file_sheet_format='IHT_HAM';


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,
job_order_no,
sample_receipt_date,
test_report_no,
generation,
customer_name,
test_engineer,
test_report_date,
no_of_sample,
test_start_date,
test_end_date,
tractor_sr_no,
test_standard_ref,
test_location_name,
test_operator,
project_group,
objective_of_test,
acceptance_criteria,
test_condition
)
select 
client_id,
function_id,
file_syspk,
file_format,
sheet_mnemonic,
make,model,
Test_Request_no,
date '1899-12-30' + sample_receipt_date::int * interval '1' day as Sample_Receipt_Date,
Test_report_No,
Generation,
Customer_Name, 
Test_Engineer,
date '1899-12-30' + Test_Report_Date::int * interval '1' day as Test_Report_Date,
No_of_Sample,
date '1899-12-30' + Test_Start_Date::int * interval '1' day as Test_Start_Date,
date '1899-12-30' + Test_End_Date::int * interval '1' day as Test_End_Date,
Tractor_Sr_No,
Test_Standard_Refer,
Test_facility,
Operator_Name,
Project_Group,
Objective,
Condition
from 
 mmt_staging2.IHT_HAM_H1_block;

update mmt_ods.test_instance a
set report_prepared_by=b.prepared_by,
report_reviewed_by=b.reviewed_by,
report_approved_by=b.approved_by,
report_template_replaces=b.replaces,
report_title=b.comments,
report_template_no=b.rev1,
report_template_rev_date=b.rev2,
report_template_rev_no= b.rev3
from mmt_staging2.IHT_HAM_footer_block  b
where a.test_file_sheet_format='IHT_HAM';
 
insert into mmt_ods.test_instance_tyre_info
(
client_id,
function_id,
test_file_ref_no,
test_file_format,
test_file_sheet_format,
tractor_make,
tractor_model,
tyre_type,
tyre_make,
tyre_size,
tyre_ply_rating,
tyre_load_carrying_capacity,
tyre_pressure_kg_per_cm2,
tyre_number_of_lug,
tyre_number_of_no_load_lug_30m,
tyre_lug_height,
tyre_dynamic_rolling_radius_mm,
tyre_wheel_rim_make_and_size
)
select 
client_id,
function_id,
file_syspk,
file_format,
sheet_mnemonic,
make,model,
Tyre_Make,
Tyre_size,
Ply_Rating::numeric ,
Load_Carrying_Capacity,
pressure_kg_cm2::numeric ,
No_of_lug::numeric,
Number_of_no_load_lug_30m::numeric,				
Lug_Height::numeric,		
Dynamic_rolling_radius::numeric ,		
Wheel_rim_Make_size
from mmt_staging2.IHT_HAM_tyre_details_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,
low_idle_declared,
low_idle_observed,
high_idle_declared,
high_idle_observed,
rated_rpm,
rated_rpm_observed,
engine_to_pto_ratio_540_pto
)
select 
client_id,
function_id,
file_syspk,
file_format,
sheet_mnemonic,
make,model,
low_idle_declared,
low_idle_observed::numeric,
high_idle_declared,
high_idle_observed::numeric,
rated_rpm_declared::numeric,
rated_rpm_observed::numeric,
engine_to_pto_ratio 
from mmt_staging2.IHT_HAM_engine_rpm_block;

 insert into mmt_ods.test_instance_weight_reaction
 (
client_id,
function_id,
test_file_ref_no,
test_file_format,
test_file_sheet_format,
tractor_make,
tractor_model,
test_condition,
front_reaction_kg,
rear_reaction_kg,
total_weight_kg
)
select 
client_id,
function_id,
file_syspk,
file_format,
sheet_mnemonic,
make,model,
test_condition,
Front_weight::numeric,
Rear_weight::numeric,
Total_weight::numeric
from mmt_staging2.IHT_HAM_weight_block;


/*block */

insert into mmt_ods.test_instance_atmospheric_info
(
client_id,
function_id,
test_file_ref_no,
test_file_format,
test_file_sheet_format,
tractor_make,
tractor_model,
ambient_temp_c,
humidity_pct,
wind_velocity_kmph,
date,
start_time,
end_time
)
select 
client_id,
function_id,
file_syspk,
file_format,
sheet_mnemonic,
make,model,
Ambient_temp_C,
humidity,
wind_velocity,
date,
start_time,
end_time
from mmt_staging2.IHT_HAM_atmos_cond_block;

insert into mmt_ods.test_instance_drawbar_info 
(
client_id,
function_id,
test_file_ref_no,
test_file_format,
test_file_sheet_format,
tractor_make,
tractor_model,
test_condition,
required_pull_in_kg,
required_power_in_hp,
actual_pull_in_kg,
actual_power_in_hp,
calculated_hitch_height_mm,
actual_hitch_height_mm
)
select 
client_id,
function_id,
file_syspk,
file_format,
sheet_mnemonic,
make,model,
test_condition,	
Required_pull_in_kg::numeric ,	
Required_Power_in_hp::numeric ,		
Actual_pull_in_kg::numeric ,	
actual_power_in_hp::numeric ,	
Calculated_hitch_heigh_mm::numeric ,		
Actual_hitch_height_mm::numeric 
from mmt_staging2.iht_ham_drawbar_block;

insert into mmt_ods.iht_hot_air_mapping_results
(
client_id,
function_id,
test_file_ref_no,
test_file_format,
test_file_sheet_format,
tractor_make,
tractor_model,
gear,
load_kg,
speed_kmph,
speed_rpm,
ambient_measured_temp_c,
head_measured_temp_c,
head_roa_c,
chest_measured_temp_c,
chest_roa_c,
lh_leg_measured_temp_c,
lh_leg_roa_c,
rh_leg_measured_temp_c,
rh_leg_roa_c,
lh_hand_measured_temp_c,
lh_hand_roa_c,
rh_hand_measured_temp_c,
rh_hand_roa_c,
hood_gas_spring_rh_side_measured_temp_c,
hood_gas_spring_rh_side_roa_c,
hood_foam_top_measured_temp_c,
hood_foam_top_roa_c,
hood_foam_middle_measured_temp_c,
hood_foam_middle_roa_c,
hood_foam_bottom_measured_temp_c,
hood_foam_bottom_roa_c,
outside_hood_top_measured_temp_c,
outside_hood_top_roa_c,
outside_hood_middle_measured_temp_c,
outside_hood_middle_roa_c,
outside_hood_bottom_measured_temp_c,
outside_hood_bottom_roa_c,
time_of_test,
time_diff_h_min
)
select 
client_id,
function_id,
file_syspk,
file_format,
sheet_mnemonic,
make,model,
column7::numeric,column8::numeric ,column9::numeric ,
column10::numeric ,column11::numeric ,column12::numeric ,
column13::numeric ,column14::numeric ,column15::numeric ,
column16::numeric ,column17::numeric ,column18::numeric ,
column19::numeric ,column20::numeric ,column21::numeric ,
column22::numeric ,column23::numeric ,column24::numeric ,
column25::numeric ,column26::numeric ,column27::numeric ,
column28::numeric ,column29::numeric ,column30::numeric ,
column31::numeric ,column32::numeric ,column33::numeric ,
column34 ,column36::numeric
from mmt_staging2.iht_ham_results_block ihrb where ods_record=1;


select to_char(to_timestamp((-0.74861111111109402)* 60),'HH12:MIPM');

select to_char(to_timestamp((4.8611111111110938)* 60),'HH12:MI');

select file_syspk into __file_syspk from mmt_staging2.IHT_HAM_H1_block;
select syspk into __test_instance_id from mmt_ods.test_instance where test_file_ref_no =__file_syspk;
select tractor_model into __model from mmt_ods.test_instance where test_file_ref_no =__file_syspk;
select tractor_make into __make 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,
tractor_model =__model,
tractor_make=__make
where test_file_ref_no=__file_syspk;


update mmt_ods.test_instance_tyre_info 
set test_instance_id=__test_instance_id,
tractor_model =__model,
tractor_make=__make
where test_file_ref_no=__file_syspk;

update mmt_ods.test_instance_tractor_info 
set test_instance_id=__test_instance_id,
tractor_model =__model,
tractor_make=__make
where test_file_ref_no=__file_syspk;

update mmt_ods.test_instance_weight_reaction
set test_instance_id=__test_instance_id,
tractor_model =__model,
tractor_make=__make
where test_file_ref_no=__file_syspk;

update mmt_ods.test_instance_atmospheric_info 
set test_instance_id=__test_instance_id,
tractor_model =__model,
tractor_make=__make
where test_file_ref_no=__file_syspk;

update mmt_ods.test_instance_drawbar_info 
set test_instance_id=__test_instance_id,
tractor_model =__model,
tractor_make=__make
where test_file_ref_no=__file_syspk;

update mmt_ods.iht_hot_air_mapping_results
set test_instance_id=__test_instance_id,
tractor_model =__model,
tractor_make=__make
where test_file_ref_no=__file_syspk;


end
$$ LANGUAGE plpgsql;



