/*
@C:\WEB_501\SQL\END_CONV\ORACLE\02FU_ESTTRGO_LOG_EST_ITREQUESTO.SQL
*/
CREATE OR REPLACE TRIGGER LOG_EST_ITREQUESTO
AFTER DELETE OR INSERT OR UPDATE
ON ITREQUESTO FOR EACH ROW
DECLARE
  V_USUARIO      VARCHAR2(40);
  V_TIPO         VARCHAR2(1);
  V_ITPESQ       VARCHAR2(40);
  V_DESC         VARCHAR2(300);
  V_CDFILLOGOPER VARCHAR2(4);
  V_DTOPER       DATE;
  V_DSREMOTEADDR VARCHAR2(15);
  V_DSREMOTEHOST VARCHAR2(15);
  V_DSSERVERRDM  VARCHAR2(20);
  V_DSSERVMETHOD VARCHAR2(30);
  V_DSREMOTEOPER VARCHAR2(12);

BEGIN
   V_TIPO := '0';
   V_DESC := '';
   V_CDFILLOGOPER := '';
   IF DELETING THEN

      V_DESC := V_DESC || 'Filial='        || :OLD.CDFILIAL;
      V_DESC := V_DESC || ' NrRequesicao=' || :OLD.NRREQUESTO;
      V_DESC := V_DESC || ' Produto='      || :OLD.CDPRODUTO;
      V_DESC := V_DESC || ' Qtde Req='     || :OLD.QTREQUESTO;
      V_DESC := V_DESC || ' Qtde Dev='     || :OLD.QTDEVOREQU;
      V_DESC := V_DESC || ' Situacao='     || :OLD.IDSTITREQU;
      V_DESC := V_DESC || ' FilLanc='      || :OLD.CDFILILANC;
      V_DESC := V_DESC || ' Lancamento='   || :OLD.NRLANCESTQ;
      V_DESC := V_DESC || ' NrSeqIt='      || :OLD.NRSEQUITEM;
      V_DESC := V_DESC || ' Lote='         || :OLD.NRLOTEESTQ;
      V_DESC := V_DESC || ' SubLote='      || :OLD.NRSUBLOTE;
      V_DESC := V_DESC || ' Descricao='    || :OLD.DSREQUIAPROV;

      V_TIPO         := '3';
      V_ITPESQ       := :OLD.CDFILIAL||:OLD.NRREQUESTO||:OLD.CDPRODUTO;
   ELSE
      IF UPDATING THEN
         IF (NVL(:OLD.QTREQUESTO,-1)    <> NVL(:NEW.QTREQUESTO, -1))   OR
            (NVL(:OLD.QTDEVOREQU,-1)    <> NVL(:NEW.QTDEVOREQU, -1))   OR
            (NVL(:OLD.IDSTITREQU,'#')   <> NVL(:NEW.IDSTITREQU,'#'))   OR
            (NVL(:OLD.CDFILILANC,'#')   <> NVL(:NEW.CDFILILANC,'#'))   OR
            (NVL(:OLD.NRLANCESTQ,'#')   <> NVL(:NEW.NRLANCESTQ,'#'))   OR
            (NVL(:OLD.NRSEQUITEM,'#')   <> NVL(:NEW.NRSEQUITEM,'#'))   OR
            (NVL(:OLD.NRLOTEESTQ,'#')   <> NVL(:NEW.NRLOTEESTQ,'#'))   OR
            (NVL(:OLD.NRSUBLOTE,'#')    <> NVL(:NEW.NRSUBLOTE,'#'))    OR
            (NVL(:OLD.DSREQUIAPROV,'#') <> NVL(:NEW.DSREQUIAPROV,'#')) THEN
            V_TIPO         := '2';
            V_ITPESQ       := :OLD.CDFILIAL||:OLD.NRREQUESTO||:OLD.CDPRODUTO;
         END IF;
      ELSE
         V_TIPO         := '1';
         V_ITPESQ       := :NEW.CDFILIAL||:NEW.NRREQUESTO||:NEW.CDPRODUTO;
      END IF;

      IF (NVL(:OLD.QTREQUESTO,-1)    <> NVL(:NEW.QTREQUESTO, -1)) THEN
         V_DESC := V_DESC || ' Qtde Req='     || :NEW.QTREQUESTO;
      END IF;
      IF (NVL(:OLD.QTDEVOREQU,-1)    <> NVL(:NEW.QTDEVOREQU, -1)) THEN
         V_DESC := V_DESC || ' Qtde Dev='     || :NEW.QTDEVOREQU;
      END IF;
      IF (NVL(:OLD.IDSTITREQU,'#')   <> NVL(:NEW.IDSTITREQU,'#')) THEN
         V_DESC := V_DESC || ' Situacao='     || :NEW.IDSTITREQU;
      END IF;
      IF (NVL(:OLD.CDFILILANC,'#')   <> NVL(:NEW.CDFILILANC,'#')) THEN
         V_DESC := V_DESC || ' FilLanc='      || :NEW.CDFILILANC;
      END IF;
      IF (NVL(:OLD.NRLANCESTQ,'#')   <> NVL(:NEW.NRLANCESTQ,'#')) THEN
         V_DESC := V_DESC || ' Lancamento='   || :NEW.NRLANCESTQ;
      END IF;
      IF (NVL(:OLD.NRSEQUITEM,'#')   <> NVL(:NEW.NRSEQUITEM,'#')) THEN
         V_DESC := V_DESC || ' NrSeqIt='      || :NEW.NRSEQUITEM;
      END IF;
      IF (NVL(:OLD.NRLOTEESTQ,'#')   <> NVL(:NEW.NRLOTEESTQ,'#')) THEN
         V_DESC := V_DESC || ' Lote='         || :NEW.NRLOTEESTQ;
      END IF;
      IF (NVL(:OLD.NRSUBLOTE,'#')    <> NVL(:NEW.NRSUBLOTE,'#')) THEN
         V_DESC := V_DESC || ' SubLote='      || :NEW.NRSUBLOTE;
      END IF;
      IF (NVL(:OLD.DSREQUIAPROV,'#') <> NVL(:NEW.DSREQUIAPROV,'#')) THEN
         V_DESC := V_DESC || ' Descricao='    || :NEW.DSREQUIAPROV;
      END IF;
   END IF;
   IF V_TIPO <> '0' THEN
      NMUSUARIO(V_USUARIO);

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

      IF NOT V_DESC IS NULL THEN
         IF NOT V_USUARIO IS NULL THEN
            V_DTOPER := SYSDATE;
            INSERT INTO LOGOPERA
                 (NMTABELA,     DTOPERACAO,   NMRESPOPER, IDTIPOOPER,    DSOPERACAO, CDPESQITEM,
                  DSRDMUSUOPER, DSENDUSUOPER, DSNMUSUOPER, DSMETUSUOPER, CDORESPOPER)
              VALUES
                 ('ITREQUESTO', SYSDATE, NVL(V_USUARIO,' '), V_TIPO, V_DESC,
                  V_ITPESQ, V_DSSERVERRDM, V_DSREMOTEADDR, V_DSREMOTEHOST, V_DSSERVMETHOD,
                  V_DSREMOTEOPER);
         END IF;
      END IF;
   END IF;
END;
/

-- VERSAO --
BEGIN ATUVEROBJ('LOG_EST_ITREQUESTO','5.01.017'); END;
/
