CREATE OR REPLACE TRIGGER LOG_FIS_ITEMNOTAFISC
AFTER DELETE OR INSERT OR UPDATE
ON ITEMNOTAFISC FOR EACH ROW
DECLARE
   V_USUARIO      VARCHAR2(40);
   V_TIPO         VARCHAR2(1);
   V_ITPESQ       VARCHAR2(40);
   V_DESC         VARCHAR2(700);
   V_DSREMOTEADDR VARCHAR2(15);
   V_DSREMOTEHOST VARCHAR2(15);
   V_DSSERVERRDM  VARCHAR2(20);
   V_DSSERVMETHOD VARCHAR2(30);
   V_DSREMOTEOPER VARCHAR2(12);
   V_DTOPER       DATE;
BEGIN
   V_TIPO := '0';
   V_DESC := ''; 
   
   IF DELETING THEN
      IF :OLD.CDFILIALIT IS NOT NULL THEN
         V_DESC := V_DESC || ' CdFilialIt=' || :OLD.CDFILIALIT;
      END IF;

      IF :OLD.NRLANCTONFIT IS NOT NULL THEN
         V_DESC := V_DESC || ' NrLanctonfIt=' || :OLD.NRLANCTONFIT;
      END IF;
      
      V_TIPO   := '3';
      V_ITPESQ := :OLD.CDFILIAL||:OLD.NRLANCTONF||:OLD.NRSEQUITNOTA;
   ELSE
      IF UPDATING THEN
         IF (:OLD.CDFILIALIT    <> :NEW.CDFILIALIT) OR
            (:OLD.NRLANCTONFIT  <> :NEW.NRLANCTONFIT)  THEN
            V_TIPO   := '2';
            V_ITPESQ := :OLD.CDFILIAL||:OLD.NRLANCTONF||:OLD.NRSEQUITNOTA;
         END IF;
      ELSE
         V_TIPO   := '1';
         V_ITPESQ := :NEW.CDFILIAL||:NEW.NRLANCTONF||:NEW.NRSEQUITNOTA;
      END IF;

      IF NVL(:NEW.CDFILIALIT, '!') <> NVL(:OLD.CDFILIALIT, '!') THEN
         V_DESC := V_DESC || ' CdFilialIt=' || :NEW.CDFILIALIT;
      END IF;

      IF NVL(:NEW.NRLANCTONFIT, '!') <> NVL(:OLD.NRLANCTONFIT, '!') THEN
         V_DESC := V_DESC || ' NrLanctonfIt=' || :NEW.NRLANCTONFIT;
      END IF;
   END IF;
   
   IF V_TIPO <> '0' THEN
      NMUSUARIO(V_USUARIO);
      WEBTUNNING(V_DSREMOTEADDR, V_DSREMOTEHOST, V_DSSERVERRDM, V_DSSERVMETHOD, V_DSREMOTEOPER);
      IF V_DESC IS NOT NULL THEN
         IF V_USUARIO IS NOT NULL THEN
            V_DTOPER := SYSDATE;
            
            INSERT INTO LOGOPERA(NMTABELA, DTOPERACAO, NMRESPOPER, IDTIPOOPER, CDPESQITEM, DSOPERACAO, DSRDMUSUOPER, DSENDUSUOPER,
                                 DSNMUSUOPER, DSMETUSUOPER, CDORESPOPER)
            VALUES('PRODFORNXML', V_DTOPER, V_USUARIO, V_TIPO, V_ITPESQ, V_DESC, V_DSSERVERRDM, V_DSREMOTEADDR,
                   V_DSREMOTEHOST, V_DSSERVMETHOD, V_DSREMOTEOPER);
         END IF;
      END IF;
   END IF;
END;
/

-- VERSAO --
BEGIN ATUVEROBJ('LOG_FIS_ITEMNOTAFISC','6.05.XXX'); END;
/