-- idea is to prevent deletion of coitems if there is 


CREATE OR REPLACE FUNCTION apopen_trigger_change() RETURNS trigger 
AS $BODY$
DECLARE
     v_tmp_id INT;
BEGIN
    
    IF NEW.apopen_paid > NEW.apopen_amount THEN
        RAISE EXCEPTION 'Attempted to set apopen paid to more than the amount due';
    END IF;
    
    
   RETURN NEW;

END;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
  
ALTER FUNCTION  apopen_trigger_change()
  OWNER TO admin;


GRANT ALL ON FUNCTION apopen_trigger_change() TO xtrole;

CREATE TRIGGER _apopen_trigger_change
    BEFORE INSERT OR UPDATE  ON  apopen
        FOR EACH ROW EXECUTE PROCEDURE apopen_trigger_change();
        