CREATE OR REPLACE FUNCTION transactions.check_cash_balance_trigger()
RETURNS trigger
LANGUAGE plpgsql
AS $function$
DECLARE cash_balance DECIMAL(24, 4);
BEGIN
IF(NEW.cash_repository_id IS NOT NULL) THEN
IF(TG_OP='UPDATE') THEN
IF (OLD.amount_in_currency != NEW.amount_in_currency) OR (OLD.amount_in_local_currency != NEW.amount_in_local_currency) THEN
RAISE EXCEPTION 'Acess is denied. You cannot update the "transaction_details" table.';
END IF;
END IF;
IF(TG_OP='INSERT') THEN
IF(NEW.tran_type = 'Cr' AND NEW.cash_repository_id IS NOT NULL) THEN
cash_balance := transactions.get_cash_repository_balance(NEW.cash_repository_id, NEW.currency_code);
IF(cash_balance < NEW.amount_in_currency) THEN
RAISE EXCEPTION 'Acess is denied. Posting this transaction would produce a negative cash balance.';
END IF;
END IF;
END IF;
END IF;
RETURN NEW;
END
$function$