CREATE OR REPLACE TRIGGER LOG_COMPTPITTRAB
AFTER DELETE OR INSERT OR UPDATE
ON COMPTPITTRAB 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
      IF :OLD.CDTIPOITTRAB IS NOT NULL THEN
        V_DESC := V_DESC || 'CdTipoIt='|| :OLD.CDTIPOITTRAB;
      END IF;

      IF :OLD.CDCOMPTPITTR IS NOT NULL THEN
         V_DESC := V_DESC || 'CdCompIt='|| :OLD.CDCOMPTPITTR;
      END IF;

      IF :OLD.VRPESOCOMP IS NOT NULL THEN
         V_DESC := V_DESC || 'VrPeso='|| :OLD.VRPESOCOMP;
      END IF;

      IF :OLD.DSCOMPITTRAB IS NOT NULL THEN
         V_DESC := V_DESC || 'DsCompIt='|| :OLD.DSCOMPITTRAB;
      END IF;

      IF :OLD.DSCOMPDELPHI IS NOT NULL THEN
         V_DESC := V_DESC || 'DsCompDelphi='|| :OLD.DSCOMPDELPHI;
      END IF;

      IF :OLD.DSEXTARQCOMP IS NOT NULL THEN
         V_DESC := V_DESC || 'DsExtArq='|| :OLD.DSEXTARQCOMP;
      END IF;

      IF :OLD.IDINFNRCPITR IS NOT NULL THEN
         V_DESC := V_DESC || 'IdInfNr='|| :OLD.IDINFNRCPITR;
      END IF;
      V_TIPO := '3';
      V_ITPESQ       := :OLD.CDTIPOITTRAB||:OLD.CDCOMPTPITTR;

   ELSE
      IF UPDATING THEN
         IF (:OLD.CDTIPOITTRAB <> :NEW.CDTIPOITTRAB) OR
            (:OLD.CDCOMPTPITTR <> :NEW.CDCOMPTPITTR) OR
            (:OLD.VRPESOCOMP   <> :NEW.VRPESOCOMP) OR
            (:OLD.DSCOMPITTRAB <> :NEW.DSCOMPITTRAB) OR
            (:OLD.DSCOMPDELPHI <> :NEW.DSCOMPDELPHI) OR
            (:OLD.DSEXTARQCOMP <> :NEW.DSEXTARQCOMP) OR
            (:OLD.IDINFNRCPITR <> :NEW.IDINFNRCPITR) THEN
            V_TIPO         := '2';
            V_ITPESQ       := :OLD.CDTIPOITTRAB||:OLD.CDCOMPTPITTR;
         END IF;
      ELSE
        V_TIPO := '1';
        V_ITPESQ := :NEW.CDTIPOITTRAB||:NEW.CDCOMPTPITTR;
      END IF;

      IF :NEW.CDTIPOITTRAB IS NOT NULL THEN
        V_DESC := V_DESC || 'CdTipoIt='|| :NEW.CDTIPOITTRAB;
      END IF;

      IF :NEW.CDCOMPTPITTR IS NOT NULL THEN
         V_DESC := V_DESC || 'CdCompIt='|| :NEW.CDCOMPTPITTR;
      END IF;

      IF :NEW.VRPESOCOMP IS NOT NULL THEN
         V_DESC := V_DESC || 'VrPeso='|| :NEW.VRPESOCOMP;
      END IF;

      IF :NEW.DSCOMPITTRAB IS NOT NULL THEN
         V_DESC := V_DESC || 'DsCompIt='|| :NEW.DSCOMPITTRAB;
      END IF;

      IF :NEW.DSCOMPDELPHI IS NOT NULL THEN
         V_DESC := V_DESC || 'DsCompDelphi='|| :NEW.DSCOMPDELPHI;
      END IF;

      IF :NEW.DSEXTARQCOMP IS NOT NULL THEN
         V_DESC := V_DESC || 'DsExtArq='|| :NEW.DSEXTARQCOMP;
      END IF;

      IF :NEW.IDINFNRCPITR IS NOT NULL THEN
         V_DESC := V_DESC || 'IdInfNr='|| :NEW.IDINFNRCPITR;
      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, CDPESQITEM, DSOPERACAO, CDFILLOGOPER,
                DSRDMUSUOPER, DSENDUSUOPER, DSNMUSUOPER, DSMETUSUOPER, CDORESPOPER)
            VALUES
               ('COMPTPITTRAB',  V_DTOPER, V_USUARIO,
                V_TIPO,      V_ITPESQ, V_DESC, V_CDFILLOGOPER,
                V_DSSERVERRDM, V_DSREMOTEADDR, V_DSREMOTEHOST, V_DSSERVMETHOD, V_DSREMOTEOPER);
         END IF;
      END IF;
   END IF;
END;
/

-- VERSAO --
BEGIN ATUVEROBJ('LOG_COMPTPITTRAB','5.20.000'); END;
/

