CREATE OR REPLACE TRIGGER LOG_FAI_MINEFETMES
AFTER DELETE OR INSERT OR UPDATE
ON MINEFETMES FOR EACH ROW
DECLARE
  V_USUARIO   VARCHAR2(40);
  V_TIPO      VARCHAR2(1);
  V_ITPESQ    VARCHAR2(40);
  V_DESC      VARCHAR2(300);
  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 := '';

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

   IF DELETING THEN
      IF :OLD.CDFILIAL IS NOT NULL THEN
         V_DESC := V_DESC || 'Filial=' || :OLD.CDFILIAL;
      END IF;

      IF :OLD.CDSERVFILI IS NOT NULL THEN
         V_DESC := V_DESC || 'Servico=' || :OLD.CDSERVFILI;
      END IF;

      IF :OLD.NRDIABASE IS NOT NULL THEN
         V_DESC := V_DESC || 'DiaBase=' || TO_CHAR(:OLD.NRDIABASE);
      END IF;

      IF :OLD.QTMINEFET IS NOT NULL THEN
         V_DESC := V_DESC || 'QtMinEfetivosMes=' || TO_CHAR(:OLD.QTMINEFET);
      END IF;

      IF :OLD.DSMINEFET IS NOT NULL THEN
         V_DESC := V_DESC || 'DescMinEfet=' || :OLD.DSMINEFET;
      END IF;

      IF :OLD.IDTERCEIROS IS NOT NULL THEN
         V_DESC := V_DESC || 'ConsideraTerceiro=' || :OLD.IDTERCEIROS;
      END IF;
      V_TIPO := '3';
      V_ITPESQ := :OLD.CDFILIAL;
   ELSE
      IF UPDATING THEN
         IF (NVL(:OLD.CDSERVFILI,'!')  <> NVL(:NEW.CDSERVFILI,'!'))  OR
            (NVL(:OLD.NRDIABASE,0)     <> NVL(:NEW.NRDIABASE,0))     OR
            (NVL(:OLD.QTMINEFET,0)     <> NVL(:NEW.QTMINEFET,0))     OR
            (NVL(:OLD.DSMINEFET,'!')   <> NVL(:NEW.DSMINEFET,'!'))   OR
            (NVL(:OLD.IDTERCEIROS,'!') <> NVL(:NEW.IDTERCEIROS,'!')) THEN
            V_TIPO   := '2';
            V_ITPESQ := :OLD.CDFILIAL;


            IF :OLD.CDSERVFILI <> :NEW.CDSERVFILI THEN
               V_DESC := V_DESC || 'Servico=' || :OLD.CDSERVFILI;
            END IF;

            IF :OLD.NRDIABASE <> :NEW.NRDIABASE THEN
               V_DESC := V_DESC || 'DiaBase=' || TO_CHAR(:OLD.NRDIABASE);
            END IF;

            IF :OLD.QTMINEFET <> :NEW.QTMINEFET THEN
               V_DESC := V_DESC || 'QtMinEfetivosMes=' || TO_CHAR(:OLD.QTMINEFET);
            END IF;

            IF :OLD.DSMINEFET <> :NEW.DSMINEFET THEN
               V_DESC := V_DESC || 'DescMinEfet=' || :OLD.DSMINEFET;
            END IF;

            IF :OLD.IDTERCEIROS <> :NEW.IDTERCEIROS THEN
               V_DESC := V_DESC || 'ConsideraTerceiro=' || :OLD.IDTERCEIROS;
            END IF;
         END IF;
      ELSE
         V_TIPO   := '1';
         V_ITPESQ := :NEW.CDFILIAL;
         V_DESC := V_DESC ||'Filial=' ||:NEW.CDFILIAL;
         V_DESC := V_DESC ||'Servico=' || :NEW.CDSERVFILI;
         V_DESC := V_DESC ||'DiaBase=' ||TO_CHAR(:NEW.NRDIABASE);
         V_DESC := V_DESC ||'QtMinEfetivosMes=' ||TO_CHAR(:NEW.QTMINEFET);
         V_DESC := V_DESC ||'DescMinEfet=' ||:NEW.DSMINEFET;
         V_DESC := V_DESC ||'ConsideraTerceiro=' ||:NEW.IDTERCEIROS;
      END IF;
   END IF;

   IF V_TIPO <> '0' THEN
      IF NOT V_DESC IS NULL THEN
         NMUSUARIO(V_USUARIO);
         IF NOT V_USUARIO IS NULL THEN
            V_DTOPER := SYSDATE;

            INSERT INTO LOGOPERA
               (NMTABELA,   DTOPERACAO, NMRESPOPER, IDTIPOOPER, CDPESQITEM, DSOPERACAO,
                DSRDMUSUOPER, DSENDUSUOPER, DSNMUSUOPER, DSMETUSUOPER, CDORESPOPER)
            VALUES
               ('MINEFETMES',  V_DTOPER, V_USUARIO,
                V_TIPO,         V_ITPESQ, SUBSTR(V_DESC, 1, 300),V_DSREMOTEADDR, V_DSREMOTEHOST, V_DSSERVERRDM,
                V_DSSERVMETHOD, V_DSREMOTEOPER);
         END IF;
      END IF;
   END IF;
END;
/
-- VERSAO --
BEGIN ATUVEROBJ('LOG_FAI_MINEFETMES','5.65.000'); END;
/
