DROP FUNCTION IF EXISTS akuz.trf_resource_quota_remote_user CASCADE;

CREATE FUNCTION akuz.trf_resource_quota_remote_user()
  RETURNS trigger AS
$BODY$
BEGIN
	if exists( select* from akuz.T_RESOURCE_QUOTA q where q.ResourceQuotaID = new.ResourceQuota and q.Channel != 2 )
	then
		Raise Exception Using Message = 'Квоту на удаленные пользователи можно установить только для канала Интернет.';
		Return New;
	End if;
END;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
--ALTER FUNCTION akuz.trf_resource_quota_remote_user()  OWNER TO postgres;

CREATE TRIGGER tr_resource_quota_remote_user
	AFTER INSERT OR UPDATE OF ResourceQuota
	ON akuz.T_RESOURCE_QUOTA_REMOTE_USER
	FOR EACH ROW
	EXECUTE PROCEDURE akuz.trf_resource_quota_remote_user();
