create or replace TRIGGER LOG_PLA_CNFCAMES
AFTER DELETE OR INSERT OR UPDATE
ON CNFCAMES FOR EACH ROW
DECLARE
  V_USUARIO    VARCHAR2(40);
  V_TIPO       VARCHAR2(1);
  V_ITPESQ     VARCHAR2(40);
  V_DESC       VARCHAR2(500);
  V_DTOPER     DATE;
  V_DSIP       VARCHAR2(50);
  V_DSUSUARIO  VARCHAR2(50);
  V_DSRDM      VARCHAR2(50);
  V_DSMETODO   VARCHAR2(250);
  V_DSOPERADOR VARCHAR2(12);
BEGIN
   V_TIPO      := '0';
   V_DESC      := '';
   WEBTUNNING(V_DSIP,
              V_DSUSUARIO,
              V_DSRDM,
              V_DSMETODO,
              V_DSOPERADOR);
              
   IF DELETING THEN
      V_DESC := V_DESC || 'Fili=' || :OLD.CDFILIAL;
      V_DESC := V_DESC || ' Serv=' || :OLD.CDSERVFILI;
      V_DESC := V_DESC || ' TipoPrat=' || :OLD.CDTIPOPRAT;
      V_DESC := V_DESC || ' Mes=' || TO_CHAR(:OLD.DTREFECNFMES,'DD/MM/YYYY');
      V_DESC := V_DESC || ' Sequ=' || :OLD.NRSEQCNFMES;
      IF (NVL(:OLD.NRORDCNFMES, -1) <> -1) THEN
         V_DESC := V_DESC || ' Ordem Visual=' || :OLD.NRORDCNFMES;
      END IF;
      V_DESC := V_DESC || ' Rdm='      || V_DSRDM;
      V_DESC := V_DESC || ' Metodo='   || V_DSMETODO;
      V_DESC := V_DESC || ' Ip='       || V_DSIP;
      V_DESC := V_DESC || ' Usuario='  || V_DSUSUARIO;
      V_DESC := V_DESC || ' Operador=' || V_DSOPERADOR;
      V_TIPO := '3';
      V_ITPESQ := :OLD.CDFILIAL||:OLD.CDSERVFILI||TO_CHAR(:OLD.DTREFECNFMES,'YYYYMMDD')||:OLD.NRSEQCNFMES;
   ELSE
      IF UPDATING THEN
         IF (:OLD.CDFILIAL     <> :NEW.CDFILIAL    ) OR
            (:OLD.CDSERVFILI   <> :NEW.CDSERVFILI  ) OR
            (:OLD.DTREFECNFMES <> :NEW.DTREFECNFMES) OR
            (:OLD.NRSEQCNFMES <> :NEW.NRSEQCNFMES) OR
            (:OLD.CDTIPOPRAT   <> :NEW.CDTIPOPRAT  ) OR
            (NVL(:OLD.NRORDCNFMES, -1) <> NVL(:NEW.NRORDCNFMES, -1)) THEN
            V_TIPO   := '2';
            V_ITPESQ := :OLD.CDFILIAL||:OLD.CDSERVFILI||TO_CHAR(:OLD.DTREFECNFMES,'YYYYMMDD')||:OLD.NRSEQCNFMES;
         END IF;
      ELSE
         V_TIPO   := '1';
         V_ITPESQ := :NEW.CDFILIAL||:NEW.CDSERVFILI||TO_CHAR(:NEW.DTREFECNFMES,'YYYYMMDD')||:NEW.NRSEQCNFMES;
      END IF;
      IF UPDATING THEN
         IF (:OLD.CDFILIAL <> :NEW.CDFILIAL) THEN
            V_DESC := V_DESC || 'Fili=' || :OLD.CDFILIAL || '|' || :NEW.CDFILIAL;
         ELSE
            V_DESC := V_DESC || 'Fili=' || :OLD.CDFILIAL;
         END IF;
      ELSE
         V_DESC := V_DESC || 'Fili=' || :NEW.CDFILIAL;
      END IF;
      IF UPDATING THEN
         IF (:OLD.CDSERVFILI <> :NEW.CDSERVFILI) THEN
            V_DESC := V_DESC || ' Serv=' || :OLD.CDSERVFILI || '|' || :NEW.CDSERVFILI;
         ELSE
            V_DESC := V_DESC || ' Serv=' || :OLD.CDSERVFILI;
         END IF;
      ELSE
         V_DESC := V_DESC || ' Card=' || :NEW.CDSERVFILI;
      END IF;
      IF UPDATING THEN
         IF (:OLD.DTREFECNFMES <> :NEW.DTREFECNFMES) THEN
            V_DESC := V_DESC || ' Mes=' || TO_CHAR(:OLD.DTREFECNFMES,'YYYYMMDD') || '|' || TO_CHAR(:NEW.DTREFECNFMES,'YYYYMMDD');
         ELSE
            V_DESC := V_DESC || ' Mes=' || TO_CHAR(:OLD.DTREFECNFMES,'YYYYMMDD');
         END IF;
      ELSE
         V_DESC := V_DESC || ' Mes=' || TO_CHAR(:NEW.DTREFECNFMES,'YYYYMMDD');
      END IF;
      
      IF UPDATING THEN
         IF (:OLD.CDTIPOPRAT <> :NEW.CDTIPOPRAT) THEN
            V_DESC := V_DESC || ' TipoPrat=' || :OLD.CDTIPOPRAT || '|' || :NEW.CDTIPOPRAT;
         ELSE
            V_DESC := V_DESC || ' TipoPrat=' || :OLD.CDTIPOPRAT;
         END IF;
      ELSE
         V_DESC := V_DESC || ' TipoPrat=' || :NEW.CDTIPOPRAT;
      END IF;

      IF UPDATING THEN
         IF (:OLD.NRSEQCNFMES <> :NEW.NRSEQCNFMES) THEN
            V_DESC := V_DESC || ' Sequ=' || :OLD.NRSEQCNFMES || '|' || :NEW.NRSEQCNFMES;
         END IF;
      ELSE
         V_DESC := V_DESC || ' Sequ=' || :NEW.NRSEQCNFMES;
      END IF;
      IF (NVL(:NEW.NRORDCNFMES, -1) <> -1) OR (UPDATING AND (NVL(:NEW.NRORDCNFMES, -1) = -1)) THEN
         IF UPDATING THEN
            IF (NVL(:OLD.NRORDCNFMES, -1) <> NVL(:NEW.NRORDCNFMES, -1)) THEN
               V_DESC := V_DESC || ' Ordem Visual=' || :OLD.NRORDCNFMES || '|' || :NEW.NRORDCNFMES;
            END IF;
         ELSE
            V_DESC := V_DESC || ' Ordem Visual=' || :NEW.NRORDCNFMES;
         END IF;
      END IF;
      V_DESC := V_DESC || ' Rdm='      || V_DSRDM;
      V_DESC := V_DESC || ' Metodo='   || V_DSMETODO;
      V_DESC := V_DESC || ' Ip='       || V_DSIP;
      V_DESC := V_DESC || ' Usuario='  || V_DSUSUARIO;
      V_DESC := V_DESC || ' Operador=' || V_DSOPERADOR;
   END IF;
   IF V_TIPO <> '0' THEN
      NMUSUARIO(V_USUARIO);
      IF NOT V_DESC IS NULL THEN
         IF NOT V_USUARIO IS NULL THEN
            V_DTOPER := SYSDATE;

            INSERT INTO LOGOPERA
               (NMTABELA,   DTOPERACAO, NMRESPOPER, IDTIPOOPER, CDPESQITEM, DSOPERACAO,
                DSRDMUSUOPER, DSENDUSUOPER, DSNMUSUOPER, DSMETUSUOPER, CDORESPOPER, CDFILLOGOPER)
            VALUES
               ('CNFCAMES', V_DTOPER, V_USUARIO, V_TIPO, V_ITPESQ, SUBSTR(V_DESC, 1, 300),
                V_DSRDM, V_DSIP, V_DSUSUARIO, V_DSMETODO, V_DSOPERADOR, NVL(:NEW.CDFILIAL, :OLD.CDFILIAL));
         END IF;
      END IF;
   END IF;
END;
/


-- VERSAO --
BEGIN ATUVEROBJ('LOG_PLA_CNFCAMES','6.49.000'); END;
/

