CREATE OR REPLACE TRIGGER DTULTATU_ITMENUCONFTE
BEFORE INSERT OR UPDATE OR DELETE
ON ITMENUCONFTE FOR EACH ROW
DECLARE
   V_NRCONFTELA VARCHAR2(05);
   V_CDFILIAL VARCHAR2(04);
   V_DTINIVIGENCIA VARCHAR2(10);
   V_CDPRODUTO VARCHAR2(20);
   V_COUNT NUMBER;
   V_DSCHAVE VARCHAR2(100);
BEGIN
   IF UPDATING OR INSERTING THEN
      :NEW.DTULTATUITCT := SYSDATE;
      V_NRCONFTELA := NVL(:NEW.NRCONFTELA, :OLD.NRCONFTELA);
      V_CDFILIAL := NVL(:NEW.CDFILIAL, :OLD.CDFILIAL);
      V_DTINIVIGENCIA := TO_CHAR(NVL(:NEW.DTINIVIGENCIA, :OLD.DTINIVIGENCIA), 'DD/MM/YYYY');
      V_CDPRODUTO := NVL(:NEW.CDIDENTBUTON, :OLD.CDIDENTBUTON);
      V_DSCHAVE := V_CDFILIAL||'|'||V_NRCONFTELA||'|'||V_DTINIVIGENCIA||'|'||V_CDPRODUTO;
      V_COUNT := 0;
      BEGIN
         SELECT COUNT(*)
           INTO V_COUNT
           FROM CTRLATUALTAB
          WHERE NMTABELA = 'ITMENUCONFTE'
            AND DSCHAVE = V_DSCHAVE;
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_COUNT := 0;
      END;
      IF V_COUNT = 0 THEN
         INSERT INTO CTRLATUALTAB
            (NMTABELA, DSCHAVE, DTULTATU)
         VALUES
            ('ITMENUCONFTE', V_DSCHAVE, SYSDATE);
      ELSE
         UPDATE CTRLATUALTAB
            SET DTULTATU = SYSDATE
          WHERE NMTABELA = 'ITMENUCONFTE'
            AND DSCHAVE = V_DSCHAVE;
      END IF;
   END IF;

   IF INSERTING THEN
      UPDATE PRODOCORRE
      SET DTULTATUPOCO = SYSDATE
      WHERE CDPRODUTO  = :NEW.CDIDENTBUTON;

      UPDATE PRODLOJA
      SET DTULTATUPLOJ = SYSDATE
      WHERE CDPRODUTO  = :NEW.CDIDENTBUTON;

      IF :NEW.IDTPBUTTON = '5' THEN
         UPDATE TIPORECE
            SET DTULTATUTIPR = SYSDATE
          WHERE CDTIPORECE = :NEW.CDIDENTBUTON;
      END IF;
   END IF;
   IF UPDATING THEN
      IF :NEW.IDTPBUTTON = '5' THEN
         UPDATE TIPORECE
            SET DTULTATUTIPR = SYSDATE
          WHERE CDTIPORECE = :NEW.CDIDENTBUTON;
      END IF;
   END IF;
   IF DELETING THEN
      INSERT INTO DELETECACHE (NMTABLECACHE, CDFILICACHE, DSKEYCACHE, DTULTATUCACH)
                       VALUES ('ITMENUCONFTE',:OLD.CDFILIAL, 'CDFILIAL='||:OLD.CDFILIAL||';NRCONFTELA='||:OLD.NRCONFTELA||';IDTPBUTTON='||:OLD.IDTPBUTTON||
                                                             ';NRBUTTON='||:OLD.NRBUTTON||';NRPGCONFTELA='||:OLD.NRPGCONFTELA||';IDTPBUTONAUX='||:OLD.IDTPBUTONAUX||
                                                             ';NRBUTTONAUX='||:OLD.NRBUTTONAUX||';NRPGCONFTAUX='||:OLD.NRPGCONFTAUX||';', SYSDATE);
   END IF;
END;
/

ALTER TABLE ITMENUCONFTE MODIFY DTULTATUITCT DEFAULT SYSDATE
/


-- VERSAO --
BEGIN ATUVEROBJ('DTULTATU_ITMENUCONFTE','6.35.041'); END;
/

