do $$
declare exportServiceID uuid;
begin
select ExportRecordID into exportServiceID 
from IES.T_EXPORT_RECORD where ProcessName = 'UnloadActsReportPeriod' limit 1;

insert into IES.T_EXPORT_RECORD_SERVICE_ROLES
(ExportRecordServiceRolesID, ExportRecordService, Roles)
select gen_random_uuid(),  exportServiceID, tr.RoleID
from VCLib.T_BASE_FEATURE_SETTINGS tbfs
join VCLib.T_ROLE_CLIENT_MODULES trcm on tbfs.RoleClientModules = trcm.RoleClientModuleID
join VCLib.T_ROLES tr on tr.RoleID = trcm.RoleID
join VCLib.T_CLIENT_MODULE_FEATURE tcmf on tcmf.ClientModuleFeatureID = tbfs.ClientModuleFeature
where tcmf.Name = 'UnloadActsReportPeriod';

select ExportRecordID into exportServiceID 
from IES.T_EXPORT_RECORD where ProcessName = 'UnloadSMOActsRB' limit 1;

insert into IES.T_EXPORT_RECORD_SERVICE_ROLES
(ExportRecordServiceRolesID, ExportRecordService, Roles)
select gen_random_uuid(),  exportServiceID, tr.RoleID
from VCLib.T_BASE_FEATURE_SETTINGS tbfs
join VCLib.T_ROLE_CLIENT_MODULES trcm on tbfs.RoleClientModules = trcm.RoleClientModuleID
join VCLib.T_ROLES tr on tr.RoleID = trcm.RoleID
join VCLib.T_CLIENT_MODULE_FEATURE tcmf on tcmf.ClientModuleFeatureID = tbfs.ClientModuleFeature
where tcmf.Name = 'UnloadSMOActsRBPeriod';
end$$;


delete from VCLib.T_BASE_FEATURE_SETTINGS bfs
	using VCLib.T_CLIENT_MODULE_FEATURE cmf, VCLib.T_CLIENT_MODULES cm
	where bfs.ClientModuleFeature = cmf.ClientModuleFeatureID
		and	cmf.ClientModules = cm.ClientModuleId
		and cm.ModuleType = 'ActsUnloadClientModule' 
		and cm."schema" = 'IES'
		and cmf.Name in ('UnloadActsReportPeriod', 'UnloadSMOActsRBPeriod');

delete from VCLib.T_CLIENT_MODULE_FEATURE cmf
	using VCLib.T_CLIENT_MODULES cm
	where cmf.ClientModules = cm.ClientModuleID
		and cm.ModuleType = 'ActsUnloadClientModule' 
		and cm."schema" = 'IES'
		and cmf.Name in ('UnloadActsReportPeriod', 'UnloadSMOActsRBPeriod');