CREATE OR REPLACE TRIGGER LOG_GEP_PROCESSO
AFTER DELETE OR UPDATE OR INSERT
ON PROCESSO 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;
BEGIN
   V_TIPO := '0';
   V_DESC := '';
   V_CDFILLOGOPER := '';

   IF DELETING THEN
      IF :OLD.CDPROCESSO IS NOT NULL THEN
         V_DESC := V_DESC ||'CdProcess= '||:OLD.CDPROCESSO;
      END IF;

      IF :OLD.NMPROCESSO IS NOT NULL THEN
         V_DESC := V_DESC ||'NmProcess= '||:OLD.NMPROCESSO;
      END IF;

      IF :OLD.CDNVPROCESSO IS NOT NULL THEN
         V_DESC := V_DESC ||'CdNvProc= '||:OLD.CDNVPROCESSO;
      END IF;

      IF :OLD.CDARVPROCESS IS NOT NULL THEN
         V_DESC := V_DESC ||'CdArvProc= '||:OLD.CDARVPROCESS;
      END IF;

      IF :OLD.QTTEMPMINATIV IS NOT NULL THEN
         V_DESC := V_DESC ||'QtTemp= '||:OLD.QTTEMPMINATIV;
      END IF;

      IF :OLD.VRFATOCPLX IS NOT NULL THEN
         V_DESC := V_DESC ||'VrFator= '||:OLD.VRFATOCPLX;
      END IF;

      IF :OLD.VRPRODMEDES IS NOT NULL THEN
         V_DESC := V_DESC ||'VrProdMes= '||:OLD.VRPRODMEDES;
      END IF;
      IF :OLD.IDGERATVCONS IS NOT NULL THEN
         V_DESC := V_DESC ||'IdGeraTV= '||:OLD.IDGERATVCONS;
      END IF;

      V_TIPO :='3';
      V_ITPESQ       := :OLD.CDPROCESSO||:OLD.CDARVPROCESS;
   ELSE
      IF UPDATING THEN
         IF (:OLD.CDPROCESSO   <> :NEW.CDPROCESSO) OR
            (:OLD.CDNVPROCESSO <> :NEW.CDNVPROCESSO) OR
            (:OLD.CDARVPROCESS <> :NEW.CDARVPROCESS) OR
            (:OLD.QTTEMPMINATIV <> :NEW.QTTEMPMINATIV) OR
            (:OLD.VRFATOCPLX <> :NEW.VRFATOCPLX) OR
            (:OLD.VRPRODMEDES <> :NEW.VRPRODMEDES) OR
            (:OLD.NMPROCESSO <> :NEW.NMPROCESSO) OR
            (:OLD.IDGERATVCONS <> :NEW.IDGERATVCONS) THEN
            V_TIPO := '2';
            V_ITPESQ       := :OLD.CDPROCESSO||:OLD.CDARVPROCESS;
         END IF;
      ELSE
         V_TIPO := '1';
         V_ITPESQ := :NEW.CDPROCESSO||:NEW.CDPROCESSO;
      END IF;


      IF :NEW.CDPROCESSO IS NOT NULL THEN
         V_DESC := V_DESC ||'CdProcess= '||:NEW.CDPROCESSO;
      END IF;
      IF :NEW.CDNVPROCESSO IS NOT NULL THEN
         V_DESC := V_DESC ||'CdNvProc= '||:NEW.CDNVPROCESSO;
      END IF;

      IF :NEW.CDARVPROCESS IS NOT NULL THEN
         V_DESC := V_DESC ||'CdArvProc= '||:NEW.CDARVPROCESS;
      END IF;

      IF :NEW.NMPROCESSO IS NOT NULL THEN
         V_DESC := V_DESC ||'NmProcess= '||:NEW.NMPROCESSO;
      END IF;

      IF :NEW.QTTEMPMINATIV IS NOT NULL THEN
         V_DESC := V_DESC ||'QtTemp= '||:NEW.QTTEMPMINATIV;
      END IF;
      IF :NEW.VRFATOCPLX IS NOT NULL THEN
         V_DESC := V_DESC ||'VrFator= '||:NEW.VRFATOCPLX;
      END IF;
      IF :NEW.VRPRODMEDES IS NOT NULL THEN
         V_DESC := V_DESC ||'VrProdMes= '||:NEW.VRPRODMEDES;
      END IF;
      IF :NEW.IDGERATVCONS IS NOT NULL THEN
         V_DESC := V_DESC ||'IdGeraTV= '||:NEW.IDGERATVCONS;
      END IF;
   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, CDFILLOGOPER)
            VALUES
               ('PROCESSO',  V_DTOPER, V_USUARIO,
                V_TIPO,      V_ITPESQ, V_DESC, V_CDFILLOGOPER);
         END IF;
      END IF;
   END IF;
END;
/

