drop function if exists staging2.fn_IHTHAM_HAM_TRX;
CREATE OR REPLACE FUNCTION staging2.fn_IHTHAM_HAM_TRX(p_client_id int,p_function_id int, p_file_mnemonic text,
p_file_sheet_mnemonic text, p_file_syspk int)
 RETURNS void
 LANGUAGE plpgsql
AS $function$
   declare __test_instance_id int;
   declare __client_id int :=p_client_id;
   declare __function_id int :=p_function_id;
   declare __file_mnemonic text  :=p_file_mnemonic;
   declare __file_sheet_mnemonic text :=p_file_sheet_mnemonic;
   declare __file_syspk int :=p_file_syspk;
   declare __make text;
   declare __model text;
   declare err_state   text;
   declare err_msg     text;
   declare err_detail  text;
   declare err_hint    text;
   declare err_context text;   
   declare _error int;   
   declare __test_master_id int;
   declare __test_instance_tractor_id int;
 begin
__file_syspk := p_file_syspk;

/************************************************************ 
Function Name:fn_IHTHAM_HAM_TRX
Function Desc: This function populates data into ODS
File Format: IHTHAM
Sheet Format: IHTHAM_HAM
Creation Date: 
Updation Date:
Author: compegence team
Function Call: select staging2.fn_IHTHAM_HAM_TRX()
***************************************************************/
insert into transactional.test_instance
(
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
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,
remarks
)
select 
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
make,model,
Test_Request_no,
Sample_Receipt_Date::date,
Test_report_No,
Generation,
Customer_Name, 
Test_Engineer,
Test_Report_Date::date,
No_of_Sample,
Test_Start_Date::date,
Test_End_Date::date,
Tractor_Sr_No,
Test_Standard_Refer,
Test_location,
Operator_Name,
Project_Group,
Acceptance_criteria,
Objective,
condition,
Remarks
from 
 staging2.IHTHAM_HAM_H1_block where trx_record=1;

update transactional.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 staging2.IHTHAM_HAM_footer_block  b
where a.file_sheet_mnemonic='IHTHAM_HAM' and trx_record=1;
 
insert into transactional.test_instance_tyre_info
(
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
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_mnemonic,
file_sheet_mnemonic,
make,model,
tyre_details,
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 staging2.IHTHAM_HAM_tyre_details_block where trx_record=1;

insert into transactional.test_instance_tractor_info
(
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
tractor_make,
tractor_model
)
select 
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
make,model
from 
 staging2.IHTHAM_HAM_H1_block where trx_record=1;

update transactional.test_instance_tractor_info 
set mahindra_model_yn = (
case when lower(tractor_make)  like 'mahindra%' then 'Y' else 'N' end
) where file_syspk =__file_syspk;

update transactional.test_instance_tractor_info a
set test_tractor_yn ='Y' where syspk in 
(select min(syspk) from transactional.test_instance_tractor_info b
where b.file_syspk =a.file_syspk)
and a.file_syspk =__file_syspk;

 insert into transactional.test_instance_engine_info
 (
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
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_mnemonic,
file_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 staging2.IHTHAM_HAM_engine_rpm_block where trx_record=1;

 insert into transactional.test_instance_weight_reaction
 (
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
tractor_make,
tractor_model,
test_condition,
front_reaction_kg,
rear_reaction_kg,
total_weight_kg
)
select 
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
make,model,
test_condition,
Front_weight::numeric,
Rear_weight::numeric,
Total_weight::numeric
from staging2.IHTHAM_HAM_weight_block where trx_record=1;


/*block */

insert into transactional.test_instance_atmospheric_info
(
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
tractor_make,
tractor_model,
ambient_temp_c,
humidity_pct,
wind_velocity_kmph,
date_of_test,
start_time,
end_time
)
select 
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
make,model,
Ambient_temp_C::numeric ,
humidity::numeric ,
wind_velocity,
date::date,
start_time::time,
end_time::time
from staging2.IHTHAM_HAM_atmos_cond_block where trx_record=1;

insert into transactional.test_instance_drawbar_info 
(
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
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_mnemonic,
file_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 staging2.IHTHAM_ham_drawbar_block where trx_record=1;

insert into transactional.IHT_hot_air_mapping_results
(
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
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,
rh_leg_measured_temp_c,
rh_leg_roa_c,
lh_leg_measured_temp_c,
lh_leg_roa_c,
rh_hand_measured_temp_c,
rh_hand_roa_c,
lh_hand_measured_temp_c,
lh_hand_roa_c,
time_of_test,
towards_main_security_gate_yn,
time_diff_h_min
)
select 
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
make,model,
column3,
column4::numeric,
column5::numeric,
column6::numeric,
column7::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::time ,column23 ,column24::time
from staging2.IHTHAM_ham_results_block ihrb where trx_record=1 order by block_row_number;



select syspk into __test_instance_id from transactional.test_instance where file_syspk =__file_syspk;
select tractor_model into __model from transactional.test_instance where file_syspk =__file_syspk;
select tractor_make into __make from transactional.test_instance where file_syspk =__file_syspk;
select syspk  from transactional.test_master into __test_master_id where test_type ='In House';
select syspk into __test_instance_tractor_id  from transactional.test_instance_tractor_info where file_syspk =__file_syspk;


update transactional.test_instance 
set test_master_id =__test_master_id,
test_tractor_id =__test_instance_tractor_id
where file_syspk=__file_syspk;

update transactional.test_instance_engine_info
set test_instance_id=__test_instance_id,
test_instance_tractor_id = __test_instance_tractor_id,
tractor_model =__model,
tractor_make=__make
where file_syspk=__file_syspk;


update transactional.test_instance_tyre_info 
set test_instance_id=__test_instance_id,
test_instance_tractor_id = __test_instance_tractor_id,
tractor_model =__model,
tractor_make=__make
where file_syspk=__file_syspk;

update transactional.test_instance_tractor_info 
set test_instance_id=__test_instance_id,
tractor_model =__model,
tractor_make=__make
where file_syspk=__file_syspk;

update transactional.test_instance_weight_reaction
set test_instance_id=__test_instance_id,
test_instance_tractor_id = __test_instance_tractor_id,
tractor_model =__model,
tractor_make=__make
where file_syspk=__file_syspk;

update transactional.test_instance_atmospheric_info 
set test_instance_id=__test_instance_id,
test_instance_tractor_id = __test_instance_tractor_id,
tractor_model =__model,
tractor_make=__make
where file_syspk=__file_syspk;

update transactional.test_instance_drawbar_info 
set test_instance_id=__test_instance_id,
test_instance_tractor_id = __test_instance_tractor_id,
tractor_model =__model,
tractor_make=__make
where file_syspk=__file_syspk;

update transactional.IHT_hot_air_mapping_results
set test_instance_id=__test_instance_id,
test_instance_tractor_id = __test_instance_tractor_id,
tractor_model =__model,
tractor_make=__make
where file_syspk=__file_syspk;

update transactional.test_instance
set test_file_name =(select a.file_name from staging1.staging_generic_table a where file_syspk = __file_syspk limit 1)
where file_syspk = __file_syspk;


update transactional.test_instance 
set test_type = a.test_type,
test_name= a.test_name
from fw_core.test_file_sheet_format_master a 
where transactional.test_instance.file_sheet_mnemonic = a.file_sheet_mnemonic
and transactional.test_instance.file_syspk=__file_syspk;


perform  fw_core.fn_insert_db_error ( __client_id, __function_id, 1001, 'Compegence', __file_syspk ,__file_mnemonic,__file_sheet_mnemonic ,null,'trx', 'fn_IHTHAM_HAM_TRX', err_state, err_msg, err_detail, err_hint, err_context,'success');

end
$function$
;


