drop function if exists staging2.fn_PTO_MPM_TRX;
CREATE OR REPLACE FUNCTION staging2.fn_PTO_MPM_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 __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 __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;
begin

/***********************************************************
Function Name:fn_PTOBEN_MPM_TRX
Function Desc: This function populates data into ODS blocks
File Format: PTOBEN
Sheet Format: PTOBEN_MPM
Creation Date: April 27 2021
Updation Date:
Author: compegence team
Function Call: select staging2.fn_PTO_MPM_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_MPM' then 'PTOBEN_MPM'
else 'PTOSTD_MPM'
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_Multipoint_Mapping 
(
client_id,function_id,file_syspk,file_mnemonic,file_sheet_mnemonic,
tractor_make ,tractor_model ,
test_condition,
engine_speed_rpm,
engine_load_pct,
sfc_gm_per_hp_hr,
fuel_consumption_ltr_per_hr
)

select client_id,function_id,file_syspk,file_mnemonic,file_sheet_mnemonic,make,model,test_condition,
c2::numeric,c3::numeric,c4::numeric,c5::numeric
from staging2.PTOBEN_MPM_performance_boost_natural_block
where trx_record=1 ; 

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_Multipoint_Mapping set test_mode = 'Normal Mode' where file_sheet_mnemonic = 'PTOBEN_MPMN' and file_syspk = __file_syspk;
update transactional.PTO_Multipoint_Mapping set test_mode = 'Boost Mode' where file_sheet_mnemonic = 'PTOBEN_MPMB' and file_syspk = __file_syspk;
update transactional.PTO_Multipoint_Mapping set test_mode = 'Eco Mode' where file_sheet_mnemonic = 'PTOBEN_MPME' and file_syspk = __file_syspk;
update transactional.PTO_Multipoint_Mapping set test_mode = 'Standard Mode' where file_sheet_mnemonic = 'PTOSTD_MPM' and file_syspk = __file_syspk;


update transactional.PTO_Multipoint_Mapping
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_multipoint_mapping 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_MPM_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_MPM_TRX', err_state, err_msg, err_detail, err_hint, err_context,'error');
return err_context;
end
$$ LANGUAGE plpgsql;

