drop function if exists staging2.fn_PTO_GVG_TRX;
CREATE OR REPLACE FUNCTION staging2.fn_PTO_GVG_TRX(p_client_id int,p_function_id int, p_file_mnemonic text,
p_file_sheet_mnemonic text, p_file_syspk int)
   RETURNS text AS $$
   declare __make text;
   declare __model text;
   declare __test_instance_id int;
   declare __test_instance_tractor_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 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;
 begin

SET search_path TO staging2;
	 
/************************************************************
Function Name:fn_PTOBEN_GVG_TRX
Function Desc: This function populates data into ODS blocks
File Format: PTOBEN
Sheet Format: PTOBEN_GVG
Creation Date: April 27 2021
Updation Date:
Author: compegence team
Function Call: select staging2.fn_PTO_GVG_TRX(907)
***************************************************************/
select tractor_model into __model from transactional.test_instance_tractor_info where file_syspk =__file_syspk;
select tractor_make into __make from transactional.test_instance_tractor_info where file_syspk =__file_syspk;
select syspk from transactional.test_master into __test_master_id where test_type ='PTO';


insert into transactional.test_instance
(
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic ,
tractor_make,
tractor_model
)
select 
client_id,
function_id,
file_syspk,
file_mnemonic,
case when __file_sheet_mnemonic = 'PTOBEN_GVG' then 'PTOBEN_GVG'
else 'PTOSTD_GVG'
end,
make,
model
from staging2.ptoben_prf_tractor_specs_block where trx_record=1;

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;


insert into transactional.PTO_Perf_Governing_Graph
(client_id,
function_id,
file_syspk,
tractor_make,
tractor_model,
test_condition,
test_type,
engine_speed_rpm,
torque_kg_m,
power_hp,
sfc_gm_per_hp_hr,
fuelling_mm3_per_stroke_per_cyl,
file_mnemonic,
file_sheet_mnemonic)
select 
client_id,
function_id,
file_syspk,
make,
model,
test_condition ,
c1,
c2::numeric,
c3::numeric,
c4::numeric,
c5::numeric,
c6::numeric,
file_mnemonic,
file_sheet_mnemonic
from staging2.PTOBEN_GVG_performance_governing_trails_block
where trx_record=1
union
select 
client_id,
function_id,
file_syspk,
make,
model,
test_condition ,
c7_1,
c7::numeric,
c8::numeric,
c9::numeric,
c10::numeric,
c11::numeric,
file_mnemonic,
file_sheet_mnemonic
from staging2.PTOBEN_GVG_performance_governing_trails_block
where trx_record=1
and (c7 is not null and c8 is not null and c9 is not null and c10 is not null and c11 is not null)
union 
select 
client_id,
function_id,
file_syspk,
make,
model,
test_condition ,
c12_1,
c12::numeric,
c13::numeric,
c14::numeric,
c15::numeric,
c16::numeric,
file_mnemonic,
file_sheet_mnemonic
from staging2.PTOBEN_GVG_performance_governing_trails_block
where trx_record=1
and (c12 is not null and c13 is not null and c14 is not null and c15 is not null and c16 is not null)
union 
select 
client_id,
function_id,
file_syspk,
make,
model,
test_condition ,
c17_1,
c17::numeric,
c18::numeric,
c19::numeric,
c20::numeric,
c21::numeric,
file_mnemonic,
file_sheet_mnemonic
from staging2.PTOBEN_GVG_performance_governing_trails_block
where trx_record=1
and (c17 is not null and c18 is not null and c19 is not null and c20 is not null and c21 is not null)
union 
select 
client_id,
function_id,
file_syspk,
model,
make,
test_condition ,
c22_1,
c22::numeric,
c23::numeric,
c24::numeric,
c25::numeric,
c26::numeric,
file_mnemonic,
file_sheet_mnemonic
from staging2.PTOBEN_GVG_performance_governing_trails_block
where trx_record=1
and (c22 is not null and c23 is not null and c24 is not null and c25 is not null and c26 is not null)
union 
select 
client_id,
function_id,
file_syspk,
model,
make,
test_condition ,
c27_1,
c27::numeric,
c28::numeric,
c29::numeric,
c30::numeric,
c31::numeric,
file_mnemonic,
file_sheet_mnemonic
from staging2.PTOBEN_GVG_performance_governing_trails_block
where trx_record=1
and (c27 is not null and c28 is not null and c29 is not null and c30 is not null and c32 is not null)
union 
select 
client_id,
function_id,
file_syspk,
model,
make,
test_condition ,
c32_1,
c32::numeric,
c33::numeric,
c34::numeric,
c35::numeric,
c36::numeric,
file_mnemonic,
file_sheet_mnemonic
from staging2.PTOBEN_GVG_performance_governing_trails_block
where trx_record=1
and (c32 is not null and c33 is not null and c34 is not null and c35 is not null and c36 is not null);

update transactional.PTO_Perf_Governing_Graph set test_mode = 'Normal Mode' where file_sheet_mnemonic = 'PTOBEN_GVGN' and file_syspk = __file_syspk;
update transactional.PTO_Perf_Governing_Graph set test_mode = 'Boost Mode' where file_sheet_mnemonic = 'PTOBEN_GVGB' and file_syspk = __file_syspk;
update transactional.PTO_Perf_Governing_Graph set test_mode = 'Eco Mode' where file_sheet_mnemonic = 'PTOBEN_GVGE' and file_syspk = __file_syspk;
update transactional.PTO_Perf_Governing_Graph set test_mode = 'Standard Mode' where file_sheet_mnemonic = 'PTOSTD_GVG' and file_syspk = __file_syspk;


select syspk into __test_instance_id from transactional.test_instance where file_syspk =__file_syspk;
select syspk into __test_instance_tractor_id from transactional.test_instance_tractor_info where file_syspk =__file_syspk;


update transactional.PTO_Perf_Governing_Graph
set test_instance_id=__test_instance_id,
test_instance_tractor_id  = __test_instance_tractor_id
where file_syspk=__file_syspk;

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

update transactional.test_instance a 
set (test_location_name,test_engineer,date_of_test) = (select place,test_engineer,TO_DATE(test_date, 'DD/MM/YYYY') from staging2.PTOBEN_PRF_Key_Performance_Parameters_block where file_syspk = __file_syspk and test_condition='observed')
where file_syspk = __file_syspk;

UPDATE transactional.pto_perf_governing_graph a set test_mode = CASE  WHEN a.test_condition like '%Normal%' THEN 'Normal'  
    										WHEN a.test_condition like '%Eco%' THEN 'Eco'
    										WHEN a.test_condition like '%Boost%' THEN 'Boost' 
    										WHEN a.test_condition like '%Standard%' THEN 'Standard' 
    										ELSE NULL  
    										end;  

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

EXCEPTION when OTHERS then 
	
GET STACKED DIAGNOSTICS 
       err_state   = returned_sqlstate,
       err_msg     = message_text,
       err_detail  = pg_exception_detail,
       err_hint    = pg_exception_hint,
       err_context = pg_exception_context;

   	   perform  fw_core.fn_insert_db_error ( __client_id, __function_id, 1001, 'Compegence', __file_syspk ,__file_mnemonic,__file_sheet_mnemonic ,null,'trx', 'fn_PTO_GVG_trx', err_state, err_msg, err_detail, err_hint, err_context,'error');
return err_context;
end
$$ LANGUAGE plpgsql;
