CREATE OR REPLACE TRIGGER DTULTATU_CLIENFILIAL
BEFORE INSERT OR UPDATE OR DELETE
ON CLIENFILIAL FOR EACH ROW
BEGIN
   IF INSERTING THEN
      :NEW.DTULTATUCLIF := SYSDATE;
      /*
	  UPDATE EXTRATOCONS
      SET    DTULTATUEXT = SYSDATE
      WHERE  CDCLIENTE = :NEW.CDCLIENTE;
	  Retirado devido erro na baixa de EXTRATOCONS e SALDOCONs no cliente Madero.	  
	  */
      UPDATE CLIENTE
      SET    DTULTATUCLIE = SYSDATE
      WHERE  CDCLIENTE = :NEW.CDCLIENTE;
	  /*
	  UPDATE CONSUMIDOR
      SET    DTULTATUCSUM = SYSDATE
      WHERE  CDCLIENTE = :NEW.CDCLIENTE;
	  Retirado devido erro na baixa de EXTRATOCONS e SALDOCONs no cliente Madero.
	  */	  
   END IF;
   IF UPDATING THEN
      :NEW.DTULTATUCLIF := SYSDATE;
      IF (NVL(:OLD.CDCFILTABPRE,0) <> NVL(:NEW.CDCFILTABPRE,0)) OR
         (NVL(:OLD.CDTABEPREC,0)   <> NVL(:NEW.CDTABEPREC,0))   THEN
         UPDATE TABEVEND SET DTULTATUTBVE = SYSDATE  
          WHERE CDFILIAL   = :NEW.CDCFILTABPRE
            AND CDTABEPREC = :NEW.CDTABEPREC;
         UPDATE TABEPREC SET DTULTATUTBPR = SYSDATE  
          WHERE CDFILIAL = :NEW.CDCFILTABPRE
            AND CDTABEPREC = :NEW.CDTABEPREC;
         UPDATE ITEMPRECO SET DTULTATUITPR = SYSDATE 
          WHERE CDFILIAL = :NEW.CDCFILTABPRE
            AND CDTABEPREC = :NEW.CDTABEPREC;
         UPDATE ITEMPRECODIA SET DTULTATUITPD = SYSDATE 
          WHERE CDFILIAL = :NEW.CDCFILTABPRE
            AND CDTABEPREC = :NEW.CDTABEPREC;
      END IF;      
   END IF;
   IF DELETING THEN
      INSERT INTO DELETECACHE (NMTABLECACHE, CDFILICACHE, DSKEYCACHE, DTULTATUCACH)
                       VALUES ('CLIENFILIAL', :OLD.CDFILIAL, 'CDCLIENTE='||:OLD.CDCLIENTE||';CDFILIAL='||:OLD.CDFILIAL||';', SYSDATE);
   END IF;
END;
/
CREATE OR REPLACE TRIGGER DTULTATU_CLIENFILIAL
BEFORE INSERT OR UPDATE OR DELETE
ON CLIENFILIAL FOR EACH ROW
BEGIN
   IF INSERTING THEN
      :NEW.DTULTATUCLIF := SYSDATE;
      /*
	  UPDATE EXTRATOCONS
      SET    DTULTATUEXT = SYSDATE
      WHERE  CDCLIENTE = :NEW.CDCLIENTE;
	  Retirado devido erro na baixa de EXTRATOCONS e SALDOCONs no cliente Madero.	  
	  */
      UPDATE CLIENTE
      SET    DTULTATUCLIE = SYSDATE
      WHERE  CDCLIENTE = :NEW.CDCLIENTE;
	  /*
	  UPDATE CONSUMIDOR
      SET    DTULTATUCSUM = SYSDATE
      WHERE  CDCLIENTE = :NEW.CDCLIENTE;
	  Retirado devido erro na baixa de EXTRATOCONS e SALDOCONs no cliente Madero.
	  */	  
   END IF;
   IF UPDATING THEN
      :NEW.DTULTATUCLIF := SYSDATE;
      IF (NVL(:OLD.CDCFILTABPRE,0) <> NVL(:NEW.CDCFILTABPRE,0)) OR
         (NVL(:OLD.CDTABEPREC,0)   <> NVL(:NEW.CDTABEPREC,0))   THEN
         UPDATE TABEVEND SET DTULTATUTBVE = SYSDATE  
          WHERE CDFILIAL   = :NEW.CDCFILTABPRE
            AND CDTABEPREC = :NEW.CDTABEPREC;
         UPDATE TABEPREC SET DTULTATUTBPR = SYSDATE  
          WHERE CDFILIAL = :NEW.CDCFILTABPRE
            AND CDTABEPREC = :NEW.CDTABEPREC;
         UPDATE ITEMPRECO SET DTULTATUITPR = SYSDATE 
          WHERE CDFILIAL = :NEW.CDCFILTABPRE
            AND CDTABEPREC = :NEW.CDTABEPREC;
         UPDATE ITEMPRECODIA SET DTULTATUITPD = SYSDATE 
          WHERE CDFILIAL = :NEW.CDCFILTABPRE
            AND CDTABEPREC = :NEW.CDTABEPREC;
      END IF;      
   END IF;
   IF DELETING THEN
      INSERT INTO DELETECACHE (NMTABLECACHE, CDFILICACHE, DSKEYCACHE, DTULTATUCACH)
                       VALUES ('CLIENFILIAL', :OLD.CDFILIAL, 'CDCLIENTE='||:OLD.CDCLIENTE||';CDFILIAL='||:OLD.CDFILIAL||';', SYSDATE);
   END IF;
END;