drop function if exists staging2.fn_IHTEMT_EMT_TRX;
CREATE OR REPLACE FUNCTION staging2.fn_IHTEMT_EMT_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_IHTEMT_EMT_TRX
Function Desc: This function populates data into ODS
File Format: IHTEMT
Sheet Format: IHTEMT_EMT
Creation Date: 
Updation Date:
Author: compegence team
Function Call: select staging2.fn_IHTEMT_EMT_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,
remarks,
test_purpose 
)
select 
client_id,
function_id,
file_syspk,
file_mnemonic,
file_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_Location,
Operator_Name,
Project_Group,
Objective,
Acceptance_Criteria,
Remarks,
Test_Purpose
from staging2.IHTEMT_EMT_H1_block where trx_record =1;

update transactional.test_instance a
set 
report_prepared_date=to_date(b.prepared_date,'DD-MM-YYYY'),
report_reviewed_date=to_date(b.reviewed_date,'DD-MM-YYYY'),
report_approved_date=to_date(b.approved_date,'DD-MM-YYYY'),
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.IHTEMT_EMT_footer_block b
where a.file_sheet_mnemonic ='IHTEMT_EMT' 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_static_rolling_radius_mm,
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 ,
Dynamic_rolling_radius::numeric ,		
Static_rolling_radius::numeric ,	
wheel_rim_make_size
from staging2.IHTEMT_EMT_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,
wheel_base_mm,
turning_circle_diameter_mm
)
select 
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
make,model,
wheel_base::numeric ,
tcd::numeric
from staging2.IHTEMT_EMT_wheel_block where trx_record =1;

update transactional.test_instance_tractor_info 
set mahindra_model_yn = (
case when 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;


/*block */

insert into transactional.test_instance_weight_reaction
(
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
tractor_make,
tractor_model,
test_condition,
total_weight_kg,
front_reaction_kg,
rear_reaction_kg,
left_reaction_fl_rl_kg,
right_reaction_fr_rr_kg,
front_left_reaction_kg,
front_right_reactionkg,
rear_left_reaction_kg,
rear_right_reactionkg,
distance_of_lifting_point_from_rear_axle_mm_d1
)
select 
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
make,model,
Weight_Reaction,
Total_Weight_kg::numeric,
Front_Reaction_kg::numeric ,
Rear_Reaction_kg::numeric ,
Left_Reaction_FL_RL_kg::numeric ,
Right_reaction_FR_RR_kg::numeric ,
Front_Left_Reaction_kg::numeric ,
Front_Right_Reaction_kg::numeric ,
Rear_Left_Reaction_kg::numeric ,
Rear_Right_Reaction_kg::numeric ,
Distance_of_lifting_point_from_rear_axle_mm_d1::numeric
from staging2.IHTEMT_EMT_weight_block where trx_record =1;


insert into transactional.IHT_steering_effort
(
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
tractor_make,
tractor_model,
test_condition,
test_speed_kmph,
max_angle_lh,
max_angle_rh,
max_effort_lh,
max_effort_rh
)
select 
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
make,model,
column2,
column3,
column4::numeric,
column5::numeric,
column6::numeric,
column8::numeric
from staging2.IHTEMT_emt_steering_block where trx_record=1;

insert into transactional.IHT_pedal_effort
(
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
tractor_make,
tractor_model,
parameters,
efforts_kg,
travel_mm
)
select 
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
make,model,
column2,
column3::numeric ,
column4::numeric
from staging2.IHTEMT_emt_pedal_block where trx_record=1;

insert into transactional.IHT_gear_effort
(
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
tractor_make,
tractor_model,
parameters,
efforts_kg
)
select 
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
make,model,
column5,
column8::numeric 
from staging2.IHTEMT_emt_pedal_block where trx_record=1;

insert into transactional.IHT_brake_pedal_effort 
(
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
tractor_make,
tractor_model,
srl_no,
pedal_effort_kg,
pedal_travel_lh_mm,
pedal_travel_rh_mm,
pedal_travel_latched_mm
)
select 
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
make,model,
column2::numeric,
column3::numeric,
column4::numeric ,
column5::numeric,
column6::numeric
from staging2.IHTEMT_emt_brake_pedal_block where trx_record=1;

insert into transactional.IHT_clutch_pedal_effort
(
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
tractor_make,
tractor_model,
srl_no,
pedal_effort_kg,
pedal_travel_mm
)
select 
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
make,model,
column7::numeric,
column8::numeric,
column9::numeric 
from staging2.IHTEMT_emt_brake_pedal_block where trx_record=1;


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_tractor_info
set test_instance_id=__test_instance_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_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.IHT_brake_pedal_effort
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_pedal_effort
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_steering_effort
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_gear_effort
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_clutch_pedal_effort 
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;

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

end
$function$
;

