CREATE OR REPLACE TRIGGER LOG_VND_ROTFABLOCAL
AFTER DELETE OR INSERT OR UPDATE
ON ROTFABLOCAL FOR EACH ROW
DECLARE
  V_DSREMOTEADDR VARCHAR2(15);
  V_DSREMOTEHOST VARCHAR2(15);
  V_DSSERVERRDM  VARCHAR2(20);
  V_DSSERVMETHOD VARCHAR2(30);
  V_DSREMOTEOPER VARCHAR2(12);
  V_DESC         VARCHAR2(500);
  V_ITPESQ       VARCHAR2(60);
  V_NMOPERADOR   VARCHAR2(40);
  V_IDTIPOOPER   VARCHAR2(1);
BEGIN
   V_DESC       := '';
   V_ITPESQ     := '';
   V_NMOPERADOR := '';
   V_IDTIPOOPER := '';
   NMUSUARIO(V_NMOPERADOR);
   WEBTUNNING(V_DSREMOTEADDR,
              V_DSREMOTEHOST,
              V_DSSERVERRDM,
              V_DSSERVMETHOD,
              V_DSREMOTEOPER);

   IF UPDATING THEN
      IF NVL(:NEW.CDFILIAL ,'NULL') <> NVL(:OLD.CDFILIAL ,'NULL') THEN
			V_DESC := NVL(V_DESC,'') || ' ' || 'CDFILIAL ' || '' || NVL(:NEW.CDFILIAL,'');
		END IF;

		IF NVL(:NEW.CDPRODUTO ,'NULL') <> NVL(:OLD.CDPRODUTO ,'NULL') THEN
			V_DESC := NVL(V_DESC,'') || ' ' ||  'CDPRODUTO ' || '' || NVL(:NEW.CDPRODUTO,'');
		END IF;

      IF NVL(:NEW.CDSUBPRODLOC ,'NULL') <> NVL(:OLD.CDSUBPRODLOC ,'NULL') THEN
			V_DESC := NVL(V_DESC,'') || ' ' || 'CDSUBPRODLOC ' || '' || NVL(:NEW.CDSUBPRODLOC,'');
		END IF;

		IF NVL(:NEW.QTSUBPRODLOC ,999999) <> NVL(:OLD.QTSUBPRODLOC ,999999) THEN
			V_DESC := NVL(V_DESC,'') || ' ' || 'QTSUBPRODLOC ' || '' || NVL(:NEW.QTSUBPRODLOC,'');
		END IF;

      IF NVL(:NEW.NRHRFABPRLOC ,'NULL') <> NVL(:OLD.NRHRFABPRLOC ,'NULL') THEN
			V_DESC := NVL(V_DESC,'') || ' ' || 'NRHRFABPRLOC ' || '' || NVL(:NEW.NRHRFABPRLOC,'');
		END IF;

      IF NVL(:NEW.NRSEQPRODLOC ,'NULL') <> NVL(:OLD.NRSEQPRODLOC ,'NULL') THEN
			V_DESC := NVL(V_DESC,'') || ' ' || 'NRSEQPRODLOC ' || '' || NVL(:NEW.NRSEQPRODLOC,'');
		END IF;

      IF NVL(:NEW.IDIMPPROTLOC ,'NULL') <> NVL(:OLD.IDIMPPROTLOC ,'NULL') THEN
			V_DESC := NVL(V_DESC,'') || ' ' || 'IDIMPPROTLOC ' || '' || NVL(:NEW.IDIMPPROTLOC,'');
		END IF;

      IF NVL(:NEW.CDDEPTO ,'NULL') <> NVL(:OLD.CDDEPTO ,'NULL') THEN
			V_DESC := NVL(V_DESC,'') || ' ' || 'CDDEPTO ' || '' || NVL(:NEW.CDDEPTO,'');
		END IF;

      IF NVL(:NEW.DSAPELIDOKDS ,'NULL') <> NVL(:OLD.DSAPELIDOKDS ,'NULL') THEN
			V_DESC := NVL(V_DESC,'') || ' ' || 'DSAPELIDOKDS ' || '' || NVL(:NEW.DSAPELIDOKDS,'');
		END IF;

      IF NVL(:NEW.IDLOCGERAPEDORCAEVE ,'NULL') <> NVL(:OLD.IDLOCGERAPEDORCAEVE ,'NULL') THEN
			V_DESC := NVL(V_DESC,'') || ' ' || 'IDLOCGERAPEDORCAEVE ' || '' || NVL(:NEW.IDLOCGERAPEDORCAEVE,'');
		END IF;

      IF NVL(:NEW.CDPRODIMPORT ,'NULL') <> NVL(:OLD.CDPRODIMPORT ,'NULL') THEN
			V_DESC := NVL(V_DESC,'') || ' ' || 'CDPRODIMPORT ' || '' || NVL(:NEW.CDPRODIMPORT,'');
		END IF;
      V_ITPESQ := :OLD.CDPRODUTO;
      V_IDTIPOOPER := '2';
   ELSE
      IF INSERTING THEN
         V_DESC := NVL(V_DESC,'') || ' ' || 'CDFILIAL ' || '' || NVL(:NEW.CDFILIAL,'');
         V_DESC := NVL(V_DESC,'') || ' ' || 'CDPRODUTO ' || '' || NVL(:NEW.CDPRODUTO,'');
         V_DESC := NVL(V_DESC,'') || ' ' || 'CDSUBPRODLOC ' || '' || NVL(:NEW.CDSUBPRODLOC,'');
         V_DESC := NVL(V_DESC,'') || ' ' || 'QTSUBPRODLOC ' || '' || NVL(:NEW.QTSUBPRODLOC,'');
         V_DESC := NVL(V_DESC,'') || ' ' || 'NRHRFABPRLOC ' || '' || NVL(:NEW.NRHRFABPRLOC,'');
         V_DESC := NVL(V_DESC,'') || ' ' || 'NRSEQPRODLOC ' || '' || NVL(:NEW.NRSEQPRODLOC,'');
         V_DESC := NVL(V_DESC,'') || ' ' || 'IDIMPPROTLOC ' || '' || NVL(:NEW.IDIMPPROTLOC,'');
         V_DESC := NVL(V_DESC,'') || ' ' || 'CDDEPTO ' || '' || NVL(:NEW.CDDEPTO,'');
         V_DESC := NVL(V_DESC,'') || ' ' || 'DSAPELIDOKDS ' || '' || NVL(:NEW.DSAPELIDOKDS,'');
         V_DESC := NVL(V_DESC,'') || ' ' || 'IDLOCGERAPEDORCAEVE ' || '' || NVL(:NEW.IDLOCGERAPEDORCAEVE,'');
         V_DESC := NVL(V_DESC,'') || ' ' || 'CDPRODIMPORT ' || '' || NVL(:NEW.CDPRODIMPORT,'');
         V_ITPESQ := :NEW.CDPRODUTO;
         V_IDTIPOOPER := '1';
      ELSE
         IF DELETING THEN
            V_DESC := NVL(V_DESC,'') || ' ' || 'CDFILIAL ' || '' || NVL(:OLD.CDFILIAL,'');
            V_DESC := NVL(V_DESC,'') || ' ' || 'CDPRODUTO ' || '' || NVL(:OLD.CDPRODUTO,'');
            V_DESC := NVL(V_DESC,'') || ' ' || 'CDSUBPRODLOC ' || '' || NVL(:OLD.CDSUBPRODLOC,'');
            V_DESC := NVL(V_DESC,'') || ' ' || 'QTSUBPRODLOC ' || '' || NVL(:OLD.QTSUBPRODLOC,'');
            V_DESC := NVL(V_DESC,'') || ' ' || 'NRHRFABPRLOC ' || '' || NVL(:OLD.NRHRFABPRLOC,'');
            V_DESC := NVL(V_DESC,'') || ' ' || 'NRSEQPRODLOC ' || '' || NVL(:OLD.NRSEQPRODLOC,'');
            V_DESC := NVL(V_DESC,'') || ' ' || 'IDIMPPROTLOC ' || '' || NVL(:OLD.IDIMPPROTLOC,'');
            V_DESC := NVL(V_DESC,'') || ' ' || 'CDDEPTO ' || '' || NVL(:OLD.CDDEPTO,'');
            V_DESC := NVL(V_DESC,'') || ' ' || 'DSAPELIDOKDS ' || '' || NVL(:OLD.DSAPELIDOKDS,'');
            V_DESC := NVL(V_DESC,'') || ' ' || 'IDLOCGERAPEDORCAEVE ' || '' || NVL(:OLD.IDLOCGERAPEDORCAEVE,'');
            V_DESC := NVL(V_DESC,'') || ' ' || 'CDPRODIMPORT ' || '' || NVL(:OLD.CDPRODIMPORT,'');
            V_ITPESQ :=  NVL(:OLD.CDFILIAL,'')||:OLD.CDPRODUTO;
            V_IDTIPOOPER := '3';
         END IF;
      END IF;
   END IF;
   V_DESC := SUBSTR(V_DESC, 1, 300);
   V_ITPESQ := SUBSTR(V_ITPESQ, 1, 40);
   INSERT INTO LOGOPERAVND
      (NMTABELAVND,
       DTOPERACAOVND,
       NMRESPOPERVND,
       IDTIPOOPERVND,
       DSOPERACAOVND,
       CDPESQITEMVND,
       DSRDMUSUOPERVND,
       DSENDUSUOPERVND,
       DSNMUSUOPERVND,
       DSMETUSUOPERVND,
       CDORESPOPERVND,
       CDFILLOGOPERVND)
   VALUES
      ('ROTFABLOCAL',
       SYSDATE,
       NVL(V_NMOPERADOR,' '),
       V_IDTIPOOPER,
       NVL(V_DESC,' '),
       V_ITPESQ,
       V_DSSERVERRDM,
       V_DSREMOTEADDR,
       V_DSREMOTEHOST,
       V_DSSERVMETHOD,
       V_DSREMOTEOPER,
       NVL(:NEW.CDFILIAL, :OLD.CDFILIAL));
END;
/