CREATE OR REPLACE TRIGGER LOG_PLA_PRATPADR
AFTER DELETE OR INSERT OR UPDATE
ON PRATPADR 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 || 'Prato=' || :OLD.CDPRATO;
      V_DESC := V_DESC || ' Arv=' || :OLD.CDFILAUXPRAT;
      IF (NVL(:OLD.QTPADRAOPRAT, -1) <> -1) THEN
         V_DESC := V_DESC || ' QtPad=' || :OLD.QTPADRAOPRAT;
      END IF;
      IF (NVL(:OLD.CDPRODPRODUCAO, '') <> '') THEN
         V_DESC := V_DESC || ' CdProdProd=' || :OLD.CDPRODPRODUCAO;
      END IF;
      IF (NVL(:OLD.IDPRODCHILL, '') <> '') THEN
         V_DESC := V_DESC || ' ProdChill=' || :OLD.IDPRODCHILL;
      END IF;
      IF (NVL(:OLD.IDATIVO, '') <> '') THEN
         V_DESC := V_DESC || ' IdAtivo=' || :OLD.IDATIVO;
      END IF;      
      IF (NVL(:OLD.DTINATIVAUTO, '01/01/1900') <> '01/01/1900') THEN
         V_DESC := V_DESC || ' DtInativAuto=' || TO_CHAR(:OLD.DTINATIVAUTO,'DD/MM/YYYY');
      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.CDPRATO||:OLD.CDFILAUXPRAT;
   ELSE
      IF UPDATING THEN
         IF (:OLD.CDPRATO      <> :NEW.CDPRATO     ) OR
            (:OLD.CDFILAUXPRAT <> :NEW.CDFILAUXPRAT) OR
            (NVL(:OLD.QTPADRAOPRAT,    -1) <> NVL(:NEW.QTPADRAOPRAT,    -1)) OR
            (NVL(:OLD.CDPRODPRODUCAO,   '') <> NVL(:NEW.CDPRODPRODUCAO,   '')) OR
            (NVL(:OLD.IDPRODCHILL,    '') <> NVL(:NEW.IDPRODCHILL,    '')) OR
            (NVL(:OLD.DTINATIVAUTO,'01/01/1900') <> NVL(:NEW.DTINATIVAUTO,'01/01/1900')) OR                
            (NVL(:OLD.IDATIVO, '') <> NVL(:NEW.IDATIVO, '')) THEN
            V_TIPO   := '2';
            V_ITPESQ := :OLD.CDPRATO||:OLD.CDFILAUXPRAT;
         END IF;
      ELSE
         V_TIPO   := '1';
         V_ITPESQ := :NEW.CDPRATO||:NEW.CDFILAUXPRAT;
      END IF;
      IF UPDATING THEN
         IF (:OLD.CDPRATO <> :NEW.CDPRATO) THEN
            V_DESC := V_DESC || 'Prato=' || :OLD.CDPRATO || '|' || :NEW.CDPRATO;
         ELSE
            V_DESC := V_DESC || 'Prato=' || :OLD.CDPRATO;
         END IF;
      ELSE
         V_DESC := V_DESC || 'Prato=' || :NEW.CDPRATO;
      END IF;
      IF UPDATING THEN
         IF (:OLD.CDFILAUXPRAT <> :NEW.CDFILAUXPRAT) THEN
            V_DESC := V_DESC || ' Arv=' || :OLD.CDFILAUXPRAT || '|' || :NEW.CDFILAUXPRAT;
         ELSE
            V_DESC := V_DESC || ' Arv=' || :OLD.CDFILAUXPRAT;
         END IF;
      ELSE
         V_DESC := V_DESC || ' Arv=' || :NEW.CDFILAUXPRAT;
      END IF;
      IF UPDATING THEN
         IF (:OLD.IDATIVO <> :NEW.IDATIVO) THEN
            V_DESC := V_DESC || 'IdAtivo=' || :OLD.IDATIVO || '|' || :NEW.IDATIVO;
         ELSE
            V_DESC := V_DESC || 'IdAtivo=' || :OLD.IDATIVO;
         END IF;
      ELSE
         V_DESC := V_DESC || 'IdAtivo=' || :NEW.IDATIVO;
      END IF;
      IF UPDATING THEN
         IF (NVL(:OLD.DTINATIVAUTO,'01/01/1900') <> NVL(:NEW.DTINATIVAUTO,'01/01/1900')) THEN
            V_DESC := V_DESC || ' DtInativAuto=' || TO_CHAR(:OLD.DTINATIVAUTO,'DD/MM/YYYY') || '|' || TO_CHAR(:NEW.DTINATIVAUTO,'DD/MM/YYYY');
         ELSE
            V_DESC := V_DESC || ' DtInativAuto=' || TO_CHAR(:OLD.DTINATIVAUTO,'DD/MM/YYYY');
         END IF;
      ELSE
         V_DESC := V_DESC || ' DtInativAuto=' || :NEW.IDATIVO;
      END IF;        
      IF (NVL(:NEW.QTPADRAOPRAT, -1) <> -1) OR (UPDATING AND (NVL(:NEW.QTPADRAOPRAT, -1) = -1)) THEN
         IF UPDATING THEN
            IF (NVL(:OLD.QTPADRAOPRAT, -1) <> NVL(:NEW.QTPADRAOPRAT, -1)) THEN
               V_DESC := V_DESC || ' QtPad de=' || :OLD.QTPADRAOPRAT || ' para=' || :NEW.QTPADRAOPRAT;
            END IF;
         ELSE
            V_DESC := V_DESC || ' QtPad=' || :NEW.QTPADRAOPRAT;
         END IF;
      END IF;
      IF UPDATING THEN
         IF (:OLD.CDPRODPRODUCAO <> :NEW.CDPRODPRODUCAO) THEN
            V_DESC := V_DESC || ' CdProd de=' || :OLD.CDPRODPRODUCAO || ' para=' || :NEW.CDPRODPRODUCAO;
         END IF;
      ELSE
         V_DESC := V_DESC || ' CdProd=' || :NEW.CDPRODPRODUCAO;
      END IF;
      IF UPDATING THEN
         IF (:OLD.IDPRODCHILL <> :NEW.IDPRODCHILL) THEN
            V_DESC := V_DESC || ' IdProdChill de=' || :OLD.IDPRODCHILL || ' para=' || :NEW.IDPRODCHILL;
         END IF;
      ELSE
         V_DESC := V_DESC || ' IdProdChill=' || :NEW.IDPRODCHILL;
      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
               ('PRATPADR', V_DTOPER, V_USUARIO, V_TIPO, V_ITPESQ, SUBSTR(V_DESC, 1, 300),
                V_DSRDM, V_DSIP, V_DSUSUARIO, V_DSMETODO, V_DSOPERADOR, NULL);
         END IF;
      END IF;
   END IF;
END;
/