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

insert into transactional.test_instance_tractor_info 
(
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
tractor_make,
tractor_model,
tractor_engine_hp,
configuration,
fip_type,
steering_type,
transmission_type,
wheel_drive_type
)
select 
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
make,
model,
tractor_HP::int,
configuration,FIP_type,steering_type,transmission_type,wheel_drive_type
from staging2.BUDNI_PTO_Spec_H1_block;

update transactional.test_instance_tractor_info 
set mahindra_model_yn = (
case when lower(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,
date_of_test 
)
select 
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
make,model,
date
from 
 staging2.BUDNI_PTO_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,
make,model,
rated_rpm::int,
engine_to_pto_ratio
from staging2.BUDNI_PTO_Spec_H1_block where trx_record=1;


/*block */

insert into transactional.budni_pto_perf_results_summary
(
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
tractor_make,
tractor_model,
pto_category,
pto_sub_category ,
power_kw,
speed_rpm_pto ,
speed_rpm_engine ,
fuel_consumption_ltr_per_hr,
fuel_consumption_kg_per_hr,
specific_kg_per_kwh,
specific_energy_kwhl
)
select 
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
make,model,
column2,
column3,
column4::numeric,column5::numeric,column6::numeric,column7::numeric,column8::numeric,
column9::numeric,column10::numeric
from staging2.BUDNI_PTO_Perf_Report_block where trx_record=1
order by block_row_number;


/*block */
insert into transactional.budni_pto_perf_results_details
(
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
tractor_make,
tractor_model,
ambient_type,
no_load_max_engine_speed_rpm,
equi_crankshaft_torque_at_max_power_nm,
max_equi_crankshaft_torque_nm,
engine_speed_at_max_equi_crankshaft_torque_rpm,
backup_torque,
pct_smoke_level_max_light_absorption_coef_per_mtr,
range_of_atm_conditions_temp_c,
range_of_atm_conditions_pressure_kpa,
range_of_atm_conditions_rel_humidity_pct
)
select 
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
make,model,
column3,
column4::int,
column5::numeric,
column6::numeric,column7::int,column8::numeric,
column9::numeric,column10,column11,column12
from staging2.BUDNI_PTO_MMTKeyword1_block
where trx_record=1;


/* block */

insert into transactional.budni_pto_perf_results_details
(
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
tractor_make,
tractor_model,
ambient_type,
max_temp_engine_oil_c,
max_temp_coolant_water_c,
max_temp_fuel_c,
max_temp_air_intake_c,
max_temp_exhaust_gas_c,
pressure_at_max_power_intake_air_kpa,
pressure_at_max_power_exhst_gas_bfr_turbo_charger_kpa,
consumptions_lub_oil_gkwh,
consumptions_coolant_water_pct_of_total_coolant_capacity
)
select client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
make,model,
column3,
column4::numeric,column5::numeric,column6::numeric,column7::numeric,column8::numeric,column9,
column10,column11::numeric,column12::numeric
from staging2.BUDNI_PTO_MMTKeyword2_block
where trx_record=1 and column3='Natural Ambient';

insert into transactional.budni_test_observations
( 
client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
tractor_make,
tractor_model,
srl_no,
characteristic,
category_evaluative_or_non_evaluative,
requirements_as_per_is_12207_2008,
values_declared_by_the_applicant_d_rqmt,
as_observed,
whether_meets_the_requirements_yn 
)
select client_id,
function_id,
file_syspk,
file_mnemonic,
file_sheet_mnemonic,
make,model,column3,column4,column7,column8,column10,column11,column12 
from staging2.BUDNI_PTO_Summary_block
where block_row_number in (3,4) and 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_pto_perf_results_summary
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.budni_pto_perf_results_details
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.budni_test_observations
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_BUDNI_PTO_TRX', err_state, err_msg, err_detail, err_hint, err_context,'success');

end
$function$
;




