-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-- Trigger de DELETE: tdb_sga_actas_detalle
-- BEFORE DELETE
-- Tabla: sga_actas_detalle
-- Version: 3.0.0
-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

CREATE OR REPLACE FUNCTION ftdb_sga_actas_detalle () RETURNS trigger AS 
$BODY$
DECLARE
   _estado char(1);
BEGIN
   -- No se permite borrar un registro si el acta esta cerrada. (y Anulada?)
   SELECT estado INTO _estado
      FROM sga_actas
      WHERE id_acta = OLD.id_acta;
   
   IF _estado = 'C' THEN
      raise exception 'No se puede sacar alumnos del acta porque esta Cerrada';
   ELSEIF _estado = 'B' THEN
      raise exception 'No se puede sacar alumnos del acta porque esta Anulada';
   END IF ;
   
   RETURN OLD;
END;
$BODY$
LANGUAGE 'plpgsql';


-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-- Trigger de DELETE sobre sga_actas_detalle
-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-- DROP TRIGGER tdb_sga_actas_detalle ON sga_actas_detalle RESTRICT;
CREATE TRIGGER tdb_sga_actas_detalle
       BEFORE DELETE ON sga_actas_detalle 
       FOR EACH ROW EXECUTE PROCEDURE ftdb_sga_actas_detalle();

