

CREATE OR REPLACE FUNCTION fw_ods.fn_fw_ods_load(p_client_id int,p_function_id int,p_job_id int,p_step_id int)
 RETURNS void
AS $$
 declare
 f record;
    w_job_id int := p_job_id;
    w_step_id int := p_step_id;
    w_function_id int := p_function_id;
    w_client_id int := p_client_id;
    w_file_sheet_mnemonic text := 'Sheets';

begin

for f in select distinct file_syspk, file_mnemonic
		from fw_core.fw_jobctl_file_runschedule 
		where  latest_runschedule_flag = '1' and
		end_status = 'success' and 
		end_status_note = 'TRX_completed'
		order by file_syspk 
loop
	

if f.file_mnemonic in ('FTDRY', 'FTWET', 'FTHLG')
then 
	
	perform fw_core.fn_get_function_exception('fw_ods.fn_fw_ods_field_load('||w_client_id||','||w_function_id||','''||f.file_mnemonic||''','''||w_file_sheet_mnemonic||''','||f.file_syspk||')');


	perform fw_core.fn_get_function_exception('fw_ods.fn_fw_ods_implement_details_update('||w_client_id||','||w_function_id||','''||f.file_mnemonic||''','''||w_file_sheet_mnemonic||''','||f.file_syspk||')');

	perform fw_core.fn_get_function_exception('fw_ods.fn_fw_ods_comments_load('||w_client_id||','||w_function_id||','''||f.file_mnemonic||''','''||w_file_sheet_mnemonic||''','||f.file_syspk||')');

	SET search_path To fw_ods;
	drop extension tablefunc;
	CREATE extension tablefunc;

	perform fw_core.fn_get_function_exception('fw_ods.fn_fw_ods_observations_load('||w_client_id||','||w_function_id||','''||f.file_mnemonic||''','''||w_file_sheet_mnemonic||''','||f.file_syspk||')');

	SET search_path To staging2;
	drop extension tablefunc;
	CREATE extension tablefunc;




elsif f.file_mnemonic ='PTO'  then
	perform fw_core.fn_get_function_exception('fw_ods.fn_fw_ods_PTO_load('||w_client_id||','||w_function_id||','''||f.file_mnemonic||''','''||w_file_sheet_mnemonic||''','||f.file_syspk||')');


elsif f.file_mnemonic = 'BUDNI' then 
	perform fw_core.fn_get_function_exception('fw_ods.fn_fw_ods_budni_load('||w_client_id||','||w_function_id||','''||f.file_mnemonic||''','''||w_file_sheet_mnemonic||''','||f.file_syspk||')');


elsif f.file_mnemonic in ('PTOBEN', 'PTOSTD') then 
	perform fw_core.fn_get_function_exception('fw_ods.fn_fw_ods_PTO_load('||w_client_id||','||w_function_id||','''||f.file_mnemonic||''','''||w_file_sheet_mnemonic||''','||f.file_syspk||')');



elsif f.file_mnemonic in ('IHTBT30','IHTBT50','IHTBTD','IHTCGM','IHTHAM','IHTEMT','IHTHLS','IHTNST','IHTSLL') then 
	
	perform fw_core.fn_get_function_exception('fw_ods.fn_fw_ods_IHT_load('||w_client_id||','||w_function_id||','''||f.file_mnemonic||''','''||w_file_sheet_mnemonic||''','||f.file_syspk||')');

	perform fw_core.fn_get_function_exception('fw_ods.fn_fw_ods_detailed_load('||w_client_id||','||w_function_id||','''||f.file_mnemonic||''','''||w_file_sheet_mnemonic||''','||f.file_syspk||')');




elsif f.file_mnemonic in ('DBOECD', 'DBSTD') then 
	perform fw_core.fn_get_function_exception('fw_ods.fn_fw_ods_drawbar_performance_load('||w_client_id||','||w_function_id||','''||f.file_mnemonic||''','''||w_file_sheet_mnemonic||''','||f.file_syspk||')');

	perform fw_core.fn_get_function_exception('fw_ods.fn_fw_ods_drawbar_load('||w_client_id||','||w_function_id||','''||f.file_mnemonic||''','''||w_file_sheet_mnemonic||''','||f.file_syspk||')');


end if;



end loop;


 update fw_ods.fw_ods set tractor_model=REGEXP_REPLACE(tractor_model,'\r|\n','','g');
 
 update fw_ods.fw_ods set tractor_make = m.target_value from fw_ods.fw_ods_misnomer m where trim(tractor_make) = m.source_value ;
 update fw_ods.fw_ods set tractor_model = m.target_value from fw_ods.fw_ods_misnomer m where trim(tractor_model) = m.source_value ;
 update fw_ods.fw_ods set wheel_drive_type = m.target_value from fw_ods.fw_ods_misnomer m where trim(wheel_drive_type) = m.source_value ;
 update fw_ods.fw_ods set name_of_implement = m.target_value from fw_ods.fw_ods_misnomer m where trim(name_of_implement) = m.source_value ;
 update fw_ods.fw_ods set type_of_implement = m.target_value from fw_ods.fw_ods_misnomer m where trim(type_of_implement) = m.source_value ;


update fw_ods.fw_ods set "configuration" = m."configuration",
tractor_engine_hp = m.tractor_engine_hp, wheel_drive_type=m.wheel_drive_type,platform = m.platform,tractor_make = m.tractor_make  
from fw_ods.tractor_info m where fw_ods.fw_ods.tractor_model = m.tractor_model;


update fw_ods.fw_ods_comments set tractor_model=REGEXP_REPLACE(tractor_model,'\r|\n','','g');
 
 update fw_ods.fw_ods_comments set tractor_make = m.target_value from fw_ods.fw_ods_misnomer m where trim(tractor_make) = m.source_value ;
 update fw_ods.fw_ods_comments set tractor_model = m.target_value from fw_ods.fw_ods_misnomer m where trim(tractor_model) = m.source_value ;
 update fw_ods.fw_ods_comments set wheel_drive_type = m.target_value from fw_ods.fw_ods_misnomer m where trim(wheel_drive_type) = m.source_value ;
 update fw_ods.fw_ods_comments set name_of_implement = m.target_value from fw_ods.fw_ods_misnomer m where trim(name_of_implement) = m.source_value ;
 update fw_ods.fw_ods_comments set type_of_implement = m.target_value from fw_ods.fw_ods_misnomer m where trim(type_of_implement) = m.source_value ;


update fw_ods.fw_ods_drawbar_performance set tractor_model=REGEXP_REPLACE(tractor_model,'\r|\n','','g');
 
 update fw_ods.fw_ods_drawbar_performance set tractor_make = m.target_value from fw_ods.fw_ods_misnomer m where trim(tractor_make) = m.source_value ;
 update fw_ods.fw_ods_drawbar_performance set tractor_model = m.target_value from fw_ods.fw_ods_misnomer m where trim(tractor_model) = m.source_value ;
 update fw_ods.fw_ods_drawbar_performance set wheel_drive_type = m.target_value from fw_ods.fw_ods_misnomer m where trim(wheel_drive_type) = m.source_value ;
-- update fw_ods.fw_ods_drawbar_performance set name_of_implement = m.target_value from fw_ods.fw_ods_misnomer m where trim(name_of_implement) = m.source_value ;
--update fw_ods.fw_ods_drawbar_performance set type_of_implement = m.target_value from fw_ods.fw_ods_misnomer m where trim(type_of_implement) = m.source_value ;


update fw_ods.fw_ods_drawbar_performance set "configuration" = m."configuration",
tractor_engine_hp = m.tractor_engine_hp, wheel_drive_type=m.wheel_drive_type,platform = m.platform,tractor_make = m.tractor_make  
from fw_ods.tractor_info m where fw_ods.fw_ods_drawbar_performance.tractor_model = m.tractor_model;

update fw_ods.fw_ods_comments set "configuration" = m."configuration",
tractor_engine_hp = m.tractor_engine_hp, wheel_drive_type=m.wheel_drive_type,platform = m.platform,tractor_make = m.tractor_make  
from fw_ods.tractor_info m where fw_ods.fw_ods_comments.tractor_model = m.tractor_model;


update fw_ods.fw_ods_detailed set tractor_model=REGEXP_REPLACE(tractor_model,'\r|\n','','g');
 
 update fw_ods.fw_ods_detailed set tractor_make = m.target_value from fw_ods.fw_ods_misnomer m where trim(tractor_make) = m.source_value ;
 update fw_ods.fw_ods_detailed set tractor_model = m.target_value from fw_ods.fw_ods_misnomer m where trim(tractor_model) = m.source_value ;
 update fw_ods.fw_ods_detailed set wheel_drive_type = m.target_value from fw_ods.fw_ods_misnomer m where trim(wheel_drive_type) = m.source_value ;
-- update fw_ods.fw_ods_detailed set name_of_implement = m.target_value from fw_ods.fw_ods_misnomer m where trim(name_of_implement) = m.source_value ;
 --update fw_ods.fw_ods_detailed set type_of_implement = m.target_value from fw_ods.fw_ods_misnomer m where trim(type_of_implement) = m.source_value ;


update fw_ods.fw_ods_detailed set "configuration" = m."configuration",
tractor_engine_hp = m.tractor_engine_hp, wheel_drive_type=m.wheel_drive_type,platform = m.platform,tractor_make = m.tractor_make  
from fw_ods.tractor_info m where fw_ods.fw_ods_detailed.tractor_model = m.tractor_model;



update fw_ods.fw_ods_observations set tractor_model=REGEXP_REPLACE(tractor_model,'\r|\n','','g');
 
 update fw_ods.fw_ods_observations set tractor_make = m.target_value from fw_ods.fw_ods_misnomer m where trim(tractor_make) = m.source_value ;
 update fw_ods.fw_ods_observations set tractor_model = m.target_value from fw_ods.fw_ods_misnomer m where trim(tractor_model) = m.source_value ;
 update fw_ods.fw_ods_observations set wheel_drive_type = m.target_value from fw_ods.fw_ods_misnomer m where trim(wheel_drive_type) = m.source_value ;
 update fw_ods.fw_ods_observations set name_of_implement = m.target_value from fw_ods.fw_ods_misnomer m where trim(name_of_implement) = m.source_value ;
 update fw_ods.fw_ods_observations set type_of_implement = m.target_value from fw_ods.fw_ods_misnomer m where trim(type_of_implement) = m.source_value ;


update fw_ods.fw_ods_observations set "configuration" = m."configuration",
tractor_engine_hp = m.tractor_engine_hp, wheel_drive_type=m.wheel_drive_type,platform = m.platform,tractor_make = m.tractor_make  
from fw_ods.tractor_info m where fw_ods.fw_ods_observations.tractor_model = m.tractor_model;



update fw_core.fw_jobctl_runschedule_jobstep set end_status_note = 'TRX_completed', end_status='success', end_time=now() where job_id = p_job_id and step_id = p_step_id and latest_runschedule_flag = '1';




end
$$
LANGUAGE plpgsql;


