CREATE OR REPLACE TRIGGER LOG_VND_GRUPROMOCPROD
AFTER DELETE OR INSERT OR UPDATE
ON GRUPROMOCPROD FOR EACH ROW
DECLARE
   V_DSREMOTEADDR VARCHAR2(15);
   V_DSREMOTEHOST VARCHAR2(15);
   V_DSSERVERRDM  VARCHAR2(20);
   V_DSSERVMETHOD VARCHAR2(30);
   V_DSREMOTEOPER VARCHAR2(12);
   V_DESC         VARCHAR2(4000);
   V_ITPESQ       VARCHAR2(40);
   V_NMOPERADOR   VARCHAR2(40);
   V_IDTIPOOPER   VARCHAR2(1);
BEGIN
   V_DESC       := '';
   V_ITPESQ     := '';
   V_NMOPERADOR := '';
   V_IDTIPOOPER := '';

   NMUSUARIO(V_NMOPERADOR);
   WEBTUNNING(V_DSREMOTEADDR,
              V_DSREMOTEHOST,
              V_DSSERVERRDM,
              V_DSSERVMETHOD,
              V_DSREMOTEOPER);

   IF UPDATING THEN
      IF NVL(:NEW.CDPRODPROMOCAO ,'NULL') <> NVL(:OLD.CDPRODPROMOCAO ,'NULL') THEN
         V_DESC := NVL(V_DESC,'') || ' ' || 'CDPRODPROMOCAO ' || NVL(:NEW.CDPRODPROMOCAO,'');
      END IF;

      IF NVL(:NEW.CDGRUPROMOC ,'NULL') <> NVL(:OLD.CDGRUPROMOC ,'NULL') THEN
         V_DESC := NVL(V_DESC,'') || ' ' || 'CDGRUPROMOC ' || NVL(:NEW.CDGRUPROMOC,'');
      END IF;

      IF NVL(:NEW.QTPRGRUPPROMOC, 999999) <> NVL(:OLD.QTPRGRUPPROMOC, 999999) THEN
         V_DESC := NVL(V_DESC,'') || ' ' || 'QTPRGRUPPROMOC ' || NVL(:NEW.QTPRGRUPPROMOC,'');
      END IF;

      IF NVL(:NEW.NRORDPROMOGRUP, 999999) <> NVL(:OLD.NRORDPROMOGRUP, 999999) THEN
         V_DESC := NVL(V_DESC,'') || ' ' || 'NRORDPROMOGRUP ' || NVL(:NEW.NRORDPROMOGRUP,'');
      END IF;

      IF NVL(:NEW.CDGRUPMUTEX ,'NULL') <> NVL(:OLD.CDGRUPMUTEX ,'NULL') THEN
         V_DESC := NVL(V_DESC,'') || ' ' || 'CDGRUPMUTEX ' || NVL(:NEW.CDGRUPMUTEX,'');
      END IF;

      IF NVL(:NEW.QTPRGRUPROMIN, 999999) <> NVL(:OLD.QTPRGRUPROMIN, 999999) THEN
         V_DESC := NVL(V_DESC,'') || ' ' || 'QTPRGRUPROMIN ' || NVL(:NEW.QTPRGRUPROMIN,'');
      END IF;

      IF NVL(:NEW.IDAPLICADESC ,'NULL') <> NVL(:OLD.IDAPLICADESC ,'NULL') THEN
         V_DESC := NVL(V_DESC,'') || ' ' || 'IDAPLICADESC ' || NVL(:NEW.IDAPLICADESC,'');
      END IF;

      IF NVL(:NEW.IDIMPGRPROMO ,'NULL') <> NVL(:OLD.IDIMPGRPROMO ,'NULL') THEN
         V_DESC := NVL(V_DESC,'') || ' ' || 'IDIMPGRPROMO ' || NVL(:NEW.IDIMPGRPROMO,'');
      END IF;

      IF NVL(:NEW.DTINIVGGRPROMO,TO_DATE('01/01/0001','DD/MM/YYYY')) <> NVL(:OLD.DTINIVGGRPROMO,TO_DATE('01/01/0001','DD/MM/YYYY'))  THEN
         V_DESC := NVL(V_DESC, '') || ' ' || 'DTINIVGGRPROMO ' || NVL(:NEW.DTINIVGGRPROMO,'');
      END IF;

      IF NVL(:NEW.DTFINVGGRPROMO, TO_DATE('01/01/0001','DD/MM/YYYY')) <> NVL(:OLD.DTFINVGGRPROMO, TO_DATE('01/01/0001','DD/MM/YYYY'))  THEN
         V_DESC := NVL(V_DESC, '') || ' ' || 'DTFINVGGRPROMO ' || NVL(:NEW.DTFINVGGRPROMO,'');
      END IF;

      IF NVL(:NEW.NRSEQGRPROMO ,'NULL') <> NVL(:OLD.NRSEQGRPROMO ,'NULL') THEN
         V_DESC := NVL(V_DESC,'') || ' ' || 'NRSEQGRPROMO ' || NVL(:NEW.NRSEQGRPROMO,'');
      END IF;

      V_ITPESQ := :NEW.CDPRODPROMOCAO||:NEW.CDGRUPROMOC||:NEW.NRSEQGRPROMO;
      V_IDTIPOOPER := '2';

   ELSIF INSERTING THEN
      V_DESC := 'CDPRODPROMOCAO ' || NVL(:NEW.CDPRODPROMOCAO,'');
      V_DESC := NVL(V_DESC,'') || ' ' || 'CDGRUPROMOC ' || NVL(:NEW.CDGRUPROMOC,'');
      V_DESC := NVL(V_DESC,'') || ' ' || 'QTPRGRUPPROMOC ' || NVL(:NEW.QTPRGRUPPROMOC,'');
      V_DESC := NVL(V_DESC,'') || ' ' || 'NRORDPROMOGRUP ' || NVL(:NEW.NRORDPROMOGRUP,'');
      V_DESC := NVL(V_DESC,'') || ' ' || 'CDGRUPMUTEX ' || NVL(:NEW.CDGRUPMUTEX,'');
      V_DESC := NVL(V_DESC,'') || ' ' || 'QTPRGRUPROMIN ' || NVL(:NEW.QTPRGRUPROMIN,'');
      V_DESC := NVL(V_DESC,'') || ' ' || 'IDAPLICADESC ' || NVL(:NEW.IDAPLICADESC,'');
      V_DESC := NVL(V_DESC,'') || ' ' || 'IDIMPGRPROMO ' || NVL(:NEW.IDIMPGRPROMO,'');
      V_DESC := NVL(V_DESC,'') || ' ' || 'DTINIVGGRPROMO ' || NVL(:NEW.DTINIVGGRPROMO,'');
      V_DESC := NVL(V_DESC,'') || ' ' || 'DTFINVGGRPROMO ' || NVL(:NEW.DTFINVGGRPROMO,'');
      V_DESC := NVL(V_DESC,'') || ' ' || 'NRSEQGRPROMO ' || NVL(:NEW.NRSEQGRPROMO,'');

      V_ITPESQ := :NEW.CDPRODPROMOCAO||:NEW.CDGRUPROMOC||:NEW.NRSEQGRPROMO;
      V_IDTIPOOPER := '1';

   ELSIF DELETING THEN
      V_DESC := 'CDPRODPROMOCAO ' || NVL(:OLD.CDPRODPROMOCAO,'');
      V_DESC := NVL(V_DESC,'') || ' ' || 'CDGRUPROMOC ' || NVL(:OLD.CDGRUPROMOC,'');
      V_DESC := NVL(V_DESC,'') || ' ' || 'QTPRGRUPPROMOC ' || NVL(:OLD.QTPRGRUPPROMOC,'');
      V_DESC := NVL(V_DESC,'') || ' ' || 'NRORDPROMOGRUP ' || NVL(:OLD.NRORDPROMOGRUP,'');
      V_DESC := NVL(V_DESC,'') || ' ' || 'CDGRUPMUTEX ' || NVL(:OLD.CDGRUPMUTEX,'');
      V_DESC := NVL(V_DESC,'') || ' ' || 'QTPRGRUPROMIN ' || NVL(:OLD.QTPRGRUPROMIN,'');
      V_DESC := NVL(V_DESC,'') || ' ' || 'IDAPLICADESC ' || NVL(:OLD.IDAPLICADESC,'');
      V_DESC := NVL(V_DESC,'') || ' ' || 'IDIMPGRPROMO ' || NVL(:OLD.IDIMPGRPROMO,'');
      V_DESC := NVL(V_DESC,'') || ' ' || 'DTINIVGGRPROMO ' || NVL(:OLD.DTINIVGGRPROMO,'');
      V_DESC := NVL(V_DESC,'') || ' ' || 'DTFINVGGRPROMO ' || NVL(:OLD.DTFINVGGRPROMO,'');
      V_DESC := NVL(V_DESC,'') || ' ' || 'NRSEQGRPROMO ' || NVL(:OLD.NRSEQGRPROMO,'');

      V_ITPESQ := :OLD.CDPRODPROMOCAO||:OLD.CDGRUPROMOC||:OLD.NRSEQGRPROMO;
      V_IDTIPOOPER := '3';
   END IF;

   V_DESC := SUBSTR(V_DESC, 1, 4000);

   INSERT INTO LOGOPERAVND
      (NMTABELAVND,
       DTOPERACAOVND,
       NMRESPOPERVND,
       IDTIPOOPERVND,
       DSOPERACAOVND,
       CDPESQITEMVND,
       DSRDMUSUOPERVND,
       DSENDUSUOPERVND,
       DSNMUSUOPERVND,
       DSMETUSUOPERVND,
       CDORESPOPERVND,
       CDFILLOGOPERVND)
   VALUES
      ('GRUPROMOCPROD',
       SYSDATE,
       NVL(V_NMOPERADOR,' '),
       V_IDTIPOOPER,
       NVL(V_DESC,' '),
       V_ITPESQ,
       V_DSSERVERRDM,
       V_DSREMOTEADDR,
       V_DSREMOTEHOST,
       V_DSSERVMETHOD,
       V_DSREMOTEOPER,
       '');
END;
/

-- VERSAO --
BEGIN ATUVEROBJ('LOG_VND_GRUPROMOCPROD','6.39.011'); END;
/
