drop function if exists staging2.fn_BUDNI_DBP_TRX ;
CREATE OR REPLACE FUNCTION staging2.fn_BUDNI_DBP_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_BUDNI_DBP_TRX
Function Desc: This function populates data into ODS
File Format: BUDNI
Sheet Format: BUDNI_DBP
Creation Date: 
Updation Date:
Author: compegence team
Function Call: select staging2.fn_BUDNI_DBP_TRX()
***************************************************************/

 insert into transactional.test_instance_tractor_info
 (
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
tractor_make,
tractor_model,
configuration,
Transmission_type,
 Wheel_Drive_Type,
 Steering_type,
 hitch_height_mm,
 Ballast_condition
 )
 select 
 client_id,
function_id,
file_syspk ,
file_mnemonic,
file_sheet_mnemonic,
 column3,
 column4,
  column7,
  column8,
  column9,
  column11,
  column13::int,
  column14
 from staging2.BUDNI_DBP_Spec_H1_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;

 insert into transactional.test_instance
 (
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
tractor_make,
tractor_model,
type_of_road
 )
 select 
 client_id,
function_id,
file_syspk ,
file_mnemonic,
file_sheet_mnemonic,
 column3,
 column4,
 column15
 from staging2.BUDNI_DBP_Spec_H1_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,
Rated_RPM,
engine_to_pto_ratio_540_pto
 )
 select 
 client_id,
function_id,
file_syspk ,
file_mnemonic,
file_sheet_mnemonic,
 column3,
 column4,
 column6::int,
 column12
 from staging2.BUDNI_DBP_Spec_H1_Block where trx_record =1 ;


insert into transactional.budni_drawbar_perf_results
(
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
tractor_make,
tractor_model,
test_condtion,
gear_used,
travel_speed_km_per_hr,
drawbar_power_kw,
drawbar_pull_kn,
engine_speed_rpm,
wheel_slippage_pct ,
fuel_consumption_kg_per_kwh,
fuel_consumption_ltr_per_hr ,
specific_energy_kwh_per_ltr, 
atmosp_conditions_temp_c,
atmosp_conditions_pressure_kpa,
atmosp_conditions_rh_pct,
temp_fuel_c,
temp_trans_oil_c,
temp_coolant_c,
temp_engine_oil_c
)
select 
client_id,function_id,file_syspk,file_mnemonic,file_sheet_mnemonic,make,model,
column2,column3,column4::float,column5::float,column6::float,column7::float,
column8::float,column9::float,column10::float,column11::float,column12,column13,
column14,column15,column16,column17,column18
from  staging2.BUDNI_DBP_Drawbar_Perf_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 ='Budni';
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_tractor_info
set test_instance_id=__test_instance_id,
tractor_model =__model,
tractor_make=__make
where file_syspk=__file_syspk;


update transactional.budni_drawbar_perf_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_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;


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 b
set date_of_test = (select a.date_of_test from  transactional.test_instance a where a.date_Of_test is not null and a.file_syspk = __file_syspk and  a.file_mnemonic = 'BUDNI' and file_sheet_mnemonic='BUDNI_PTO')::date
where 
b.date_Of_test is null 
and b.file_syspk = __file_syspk
and  b.file_mnemonic = 'BUDNI';


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

end
$function$
;




 
