CREATE OR REPLACE TRIGGER LOG_VND_TABEVENDGER
AFTER DELETE OR INSERT OR UPDATE
ON TABEVENDGER 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(40);
  V_NMOPERADOR   VARCHAR2(40);
  V_IDTIPOOPER   VARCHAR2(1);
BEGIN
/* IDTIPOOPER - '1' - Inclusao, '2' - Alteracao, '3' - Exclusao*/
   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.CDTABEPREC ,'NULL') <> NVL(:OLD.CDTABEPREC ,'NULL') THEN
			V_DESC := NVL(V_DESC,'') || ' ' || 'Tab Preco ' || '' || NVL(:NEW.CDTABEPREC,'');
		END IF;
		IF NVL(:NEW.DTINIVGPREC ,TO_DATE('01/01/0001','DD/MM/YYYY')) <> NVL(:OLD.DTINIVGPREC ,TO_DATE('01/01/0001','DD/MM/YYYY')) THEN
			V_DESC := NVL(V_DESC,'') || ' ' || 'Dt Ini Vg Preco ' || '' || NVL(:NEW.DTINIVGPREC,'');
		END IF;
		IF NVL(:NEW.DTFINVGPREC ,TO_DATE('01/01/0001','DD/MM/YYYY')) <> NVL(:OLD.DTFINVGPREC ,TO_DATE('01/01/0001','DD/MM/YYYY')) THEN
			V_DESC := NVL(V_DESC,'') || ' ' || 'Dt Fin Vg Preco ' || '' || NVL(:NEW.DTFINVGPREC,'');
		END IF;
		IF NVL(:NEW.DTREAJUSTE ,TO_DATE('01/01/0001','DD/MM/YYYY')) <> NVL(:OLD.DTREAJUSTE ,TO_DATE('01/01/0001','DD/MM/YYYY')) THEN
			V_DESC := NVL(V_DESC,'') || ' ' || 'Data Reajuste ' || '' || NVL(:NEW.DTREAJUSTE,'');
		END IF;
		IF NVL(:NEW.VRREAJUSTE ,999999) <> NVL(:OLD.VRREAJUSTE ,999999) THEN
			V_DESC := NVL(V_DESC,'') || ' ' || 'Vr Reajuste ' || '' || NVL(:NEW.VRREAJUSTE,'');
		END IF;
		IF NVL(:NEW.DTULTATU ,TO_DATE('01/01/0001','DD/MM/YYYY')) <> NVL(:OLD.DTULTATU ,TO_DATE('01/01/0001','DD/MM/YYYY')) THEN 
			V_DESC := NVL(V_DESC,'') || ' ' || 'Dt Ult Atu Tab Venda ' || '' || NVL(:NEW.DTULTATU,'');
		END IF;
		IF NVL(:NEW.IDSITVGPREC ,'NULL') <> NVL(:OLD.IDSITVGPREC ,'NULL') THEN
			V_DESC := NVL(V_DESC,'') || ' ' || 'Id Sit Vg Preco ' || '' || NVL(:NEW.IDSITVGPREC,'');
		END IF;
      V_ITPESQ := :NEW.CDTABEPREC||:NEW.DTINIVGPREC;
      V_IDTIPOOPER := '2';
   ELSE
      IF INSERTING THEN
      V_DESC   := 'Tab Preco  '           || '' || NVL(:NEW.CDTABEPREC,''  )  || ' ' ||
                  'Dt Ini Vg Preco '      || '' || NVL(:NEW.DTINIVGPREC,'' )  || ' ' ||
                  'Dt Fin Vg Preco  '     || '' || NVL(:NEW.DTFINVGPREC,'' )  || ' ' ||
                  'Data Reajuste  '       || '' || NVL(:NEW.DTREAJUSTE,''  )  || ' ' ||
                  'Vr Reajuste '          || '' || NVL(:NEW.VRREAJUSTE,''  )  || ' ' ||
                  'Dt Ult Atu Tab Venda ' || '' || NVL(:NEW.DTULTATU,'')  || ' ' ||
                  'Id Sit Vg Preco '      || '' || NVL(:NEW.IDSITVGPREC,'');
      V_ITPESQ := :NEW.CDTABEPREC||:NEW.DTINIVGPREC;
         V_IDTIPOOPER := '1';
      ELSE
         IF DELETING THEN
      V_DESC   := 'Tab Preco  '           || '' || NVL(:OLD.CDTABEPREC,''  )  || ' ' ||
                  'Dt Ini Vg Preco '      || '' || NVL(:OLD.DTINIVGPREC,'' )  || ' ' ||
                  'Dt Fin Vg Preco  '     || '' || NVL(:OLD.DTFINVGPREC,'' )  || ' ' ||
                  'Data Reajuste  '       || '' || NVL(:OLD.DTREAJUSTE,''  )  || ' ' ||
                  'Vr Reajuste '          || '' || NVL(:OLD.VRREAJUSTE,''  )  || ' ' ||
                  'Dt Ult Atu Tab Venda ' || '' || NVL(:OLD.DTULTATU,'')  || ' ' ||
                  'Id Sit Vg Preco '      || '' || NVL(:OLD.IDSITVGPREC,'');
            V_ITPESQ := :OLD.CDTABEPREC||:OLD.DTINIVGPREC;
            V_IDTIPOOPER := '3';
         END IF;
      END IF;
   END IF;
   V_DESC := SUBSTR(V_DESC, 1, 300);
   INSERT INTO LOGOPERAVND
      (NMTABELAVND,
       DTOPERACAOVND,
       NMRESPOPERVND,
       IDTIPOOPERVND,
       DSOPERACAOVND,
       CDPESQITEMVND,
       DSRDMUSUOPERVND,
       DSENDUSUOPERVND,
       DSNMUSUOPERVND,
       DSMETUSUOPERVND,
       CDORESPOPERVND,
       CDFILLOGOPERVND)
   VALUES
      ('TABEVENDGER',
       SYSDATE,
       NVL(V_NMOPERADOR,' '),
       V_IDTIPOOPER,
       NVL(V_DESC,' '),
       V_ITPESQ,
       V_DSSERVERRDM,
       V_DSREMOTEADDR,
       V_DSREMOTEHOST,
       V_DSSERVMETHOD,
       V_DSREMOTEOPER,
       NULL);
END;
/