drop function if exists staging2.fn_IHTNST_NST_TRX;
CREATE OR REPLACE FUNCTION staging2.fn_IHTNST_NST_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_IHTNST_NST_TRX
Function Desc: This function populates data into ODS
File Format: IHTNST
Sheet Format: IHTNST_NST
Creation Date: 
Updation Date:
Author: compegence team
Function Call: select staging2.fn_IHTNST_NST_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,
test_condition,
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_facility,
Operator_Name,
Project_Group,
Objective,
condition,
Test_Purpose
from 
 staging2.IHTNST_NST_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.IHTNST_nst_footer_block  b
where a.file_sheet_mnemonic='IHTNST_NST' 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_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,
wheel_rim_make_size
from staging2.IHTNST_nst_tyre_details_block where trx_record=1;


 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.IHTNST_nst_engine_rpm_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,
weight_reactions_front_kg,
tractor_weight_front_observed_kg,
tractor_weight_front_remark,
weight_reactions_rear_kg,
tractor_weight_rear_observed_kg,
tractor_weight_rear_remark,
tractor_weight_total_kg,
tractor_weight_total_observed_kg,
tractor_weight_total_remark
)
select 
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
make,model,
front_weight_declared::numeric ,
front_weight_observed::numeric,
front_weight_remark,
rear_weight_declared::numeric,
rear_weight_observed::numeric,
rear_weight_remark,
total_weight_declared::numeric,
total_weight_observed::numeric,
total_weight_remark
from staging2.IHTNST_nst_weight_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_atmospheric_info
(
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
tractor_make,
tractor_model,
ambient_temp_c,
humidity_pct,
pressure_kpa,
background_noise_dba,
wind_velocity_kmph
)
select 
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
make,model,
ambient_temp_c::numeric,
humidity::numeric,
pressure_kpa::numeric,
background_noise::numeric,
wind_velocity
from staging2.IHTNST_nst_atmos_cond_block where trx_record=1;

insert into transactional.IHT_noise_measurement_results
(
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
tractor_make,
tractor_model,
test_type,
test_mode,
gear,
noise_level_1_db_a,
noise_level_2_db_a,
noise_level_3_db_a
)
select 
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
make,model,
column1,
column2,
column3,
column4::numeric,
column5::numeric,
column6::numeric
from staging2.IHTNST_nst_stand_noise_block where trx_record=1;

update transactional.IHT_noise_measurement_results 
set test_condition = (select column4 
from staging2.IHTNST_nst_stand_noise_block
where block_row_number=3) where file_syspk = __file_syspk;

insert into transactional.IHT_noise_measurement_test 
(
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
tractor_make,
tractor_model,
test_condition,
test_type,
test_mode
)
select
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
tractor_make,
tractor_model,
test_condition,
test_type,
test_mode
from transactional.IHT_noise_measurement_results where test_type='BY STANDER’S NOISE';

update transactional.IHT_noise_measurement_test
set acceptance_criteria=b.acceptance_criteria,
remarks =b.remarks 
from staging2.IHTNST_nst_stand_noise_block  b
where b.column1 ='BY STANDER’S NOISE' and test_type='BY STANDER’S NOISE' and trx_record=1;

insert into transactional.IHT_noise_measurement_results
(
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
tractor_make,
tractor_model,
test_type,
test_mode,
gear,
noise_level_1_db_a,
noise_level_2_db_a,
noise_level_3_db_a
)
select 
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
make,model,
column1,
column2,
column3,
column7::numeric,
column8::numeric,
column9::numeric
from staging2.IHTNST_nst_stand_noise_block where trx_record=1;

update transactional.IHT_noise_measurement_results 
set test_condition = (select column7 
from staging2.IHTNST_nst_stand_noise_block
where block_row_number=3) where test_condition is null;

insert into transactional.IHT_noise_measurement_test 
(
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
tractor_make,
tractor_model,
test_condition,
test_type,
test_mode
)
select
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
tractor_make,
tractor_model,
test_condition,
test_type,
test_mode
from transactional.IHT_noise_measurement_results where test_type='BY STANDER’S NOISE';

update transactional.IHT_noise_measurement_test
set acceptance_criteria=b.acceptance_criteria,
remarks =b.remarks 
from staging2.IHTNST_nst_stand_noise_block  b
where b.column1 ='BY STANDER’S NOISE' and test_type='BY STANDER’S NOISE' and trx_record=1;

insert into transactional.IHT_noise_measurement_results
(
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
tractor_make,
tractor_model,
test_type,
test_mode,
gear,
load_kg,
speed_kmph,
speed_rpm,
noise_level_1_db_a,
noise_level_2_db_a,
noise_level_3_db_a
)
select 
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
make,model,
column1,
column2,
column3,
column4::numeric,
column5::numeric,
column6::numeric,
column7::numeric,
column8::numeric,
column9::numeric
from staging2.IHTNST_nst_oel_noise_block where trx_record=1;

update transactional.IHT_noise_measurement_test
set acceptance_criteria=b.acceptance_criteria,
remarks =b.remarks 
from staging2.IHTNST_nst_oel_noise_block b 
where b.column1 ='OEL Noise without Load' and test_type='OEL Noise without Load';

insert into transactional.IHT_noise_measurement_results
(
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
tractor_make,
tractor_model,
test_type,
test_mode,
gear,
load_kg,
speed_kmph,
speed_rpm,
noise_level_1_db_a,
noise_level_2_db_a,
noise_level_3_db_a
)
select 
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
make,model,
column1,
column2,
column3,
column4::numeric,
column5::numeric,
column6::numeric,
column7::numeric,
column8::numeric,
column9::numeric
from staging2.IHTNST_nst_oel_noise_load_block where trx_record=1;

update transactional.IHT_noise_measurement_test
set acceptance_criteria=b.acceptance_criteria,
remarks =b.remarks 
from staging2.IHTNST_nst_oel_noise_load_block b 
where b.column1 ='OEL Noise with Load' and test_type='OEL Noise with Load';

insert into transactional.IHT_noise_measurement_test 
(
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
tractor_make,
tractor_model,
test_condition,
test_type,
test_mode
)
select
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
tractor_make,
tractor_model,
test_condition,
test_type,
test_mode
from transactional.IHT_noise_measurement_results where test_type ='OEL Noise without Load' ;

insert into transactional.IHT_noise_measurement_test 
(
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
tractor_make,
tractor_model,
test_condition,
test_type,
test_mode
)
select
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
tractor_make,
tractor_model,
test_condition,
test_type,
test_mode
from transactional.IHT_noise_measurement_results where test_type ='OEL Noise with Load';
  
delete from transactional.IHT_noise_measurement_test a
using transactional.IHT_noise_measurement_test b
where (a.syspk < b.syspk and 
a.test_condition is not null and
a.test_condition =b.test_condition and 
a.test_mode =b.test_mode and a.test_type=b.test_type) or 
(a.syspk < b.syspk and a.test_condition is null and a.test_mode =b.test_mode and a.test_type=b.test_type);

update transactional.IHT_noise_measurement_results a
set noise_measurement_id =(select syspk 
from transactional.IHT_noise_measurement_test b
where (a.test_condition =b.test_condition and a.test_condition is not null and
a.test_mode =b.test_mode and a.test_type=b.test_type) or
(a.test_condition is null and a.test_mode =b.test_mode and a.test_type=b.test_type));


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_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.IHT_noise_measurement_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.IHT_noise_measurement_test 
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_IHTNST_NST_TRX', err_state, err_msg, err_detail, err_hint, err_context,'success');

end
$function$
;




