CREATE OR REPLACE TRIGGER LOG_PED_ITEMPREBASE
AFTER UPDATE OR INSERT OR DELETE
ON ITEMPREBASE 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(1000);
  V_ITPESQ       VARCHAR2(40);
  V_NMOPERADOR   VARCHAR2(40);
  V_IDTIPOOPER   VARCHAR2(1);

BEGIN
/* IDTIPOOPER - '1' - Inclusao, '2' - Alteracao, '3' - Exclusao*/

   V_DSREMOTEHOST := ' ';
   V_DSSERVERRDM  := ' ';
   V_DSSERVMETHOD := ' ';
   V_DSREMOTEOPER := ' ';
   V_DESC         := ' ';
   V_ITPESQ       := ' ';
   V_NMOPERADOR   := ' ';
   V_IDTIPOOPER   := ' ';

   NMUSUARIO(V_NMOPERADOR);
   WEBTUNNING(V_DSREMOTEADDR,
              V_DSREMOTEHOST,
              V_DSSERVERRDM,
              V_DSSERVMETHOD,
              V_DSREMOTEOPER);

   IF INSERTING THEN

      V_DESC := 'Tabela de Preco: '|| :NEW.CDPRECOBASE||' '||
                'Codigo do Produto: '|| :NEW.CDPRODUTO||' '||
                'Val. Ini: '|| :NEW.DTINIVALIPB ||' '||
                'Val. Fim: '|| :NEW.DTFIMVALIPB ||' '||
                'Valor: '|| :NEW.VRITEPREBASE ||' '||
                '';

      V_IDTIPOOPER   := '1';
      V_ITPESQ     := :NEW.CDPRECOBASE;
   ELSE
      IF UPDATING THEN

         V_DESC := 'Tabela de Preco: ' ||:NEW.CDPRECOBASE ||'  ' ;
         V_DESC := V_DESC || 'Codigo do Produto: ' ||:OLD.CDPRODUTO||'  ' ;

         IF (NVL (:OLD.DTINIVALIPB,'01/01/1111') <> NVL(:NEW.DTINIVALIPB, '01/01/1111')) THEN
            V_DESC := V_DESC || 'Val. Ini: ' || :NEW.DTINIVALIPB ||'  ' ;
         END IF;
         IF (NVL (:OLD.DTFIMVALIPB,'01/01/1111') <> NVL(:NEW.DTFIMVALIPB, '01/01/1111')) THEN
            V_DESC := V_DESC || 'Val. Fim: ' || :NEW.DTFIMVALIPB ||'  ' ;
         END IF;
         IF (NVL (:OLD.VRITEPREBASE,0) <> NVL(:NEW.VRITEPREBASE, 0)) THEN
            V_DESC := V_DESC || 'Valor: ' || :NEW.VRITEPREBASE ||'  ' ;
         END IF;

         V_IDTIPOOPER   := '2';
         V_ITPESQ     := :NEW.CDPRECOBASE;
      ELSE
         IF DELETING THEN
            V_DESC := 'Tabela de Preco: '    || :OLD.CDPRECOBASE ||' '||
                      'Codigo do Produto: '  || :OLD.CDPRODUTO||'  '||
                      'Val. Ini: '           || :OLD.DTINIVALIPB ||'  '||
                      'Val. Fim: '           || :OLD.DTFIMVALIPB ||'  '||
                      'Valor: '              || :OLD.VRITEPREBASE ||'  '||
                      '';

            V_IDTIPOOPER := '3';
            V_ITPESQ     := :OLD.CDPRECOBASE;
         END IF;
      END IF;
   END IF;

   IF NOT (V_DESC IS NULL) THEN

      V_DESC := SUBSTR(V_DESC, 1, 300);

      INSERT INTO LOGOPERA ( NMTABELA, DTOPERACAO, NMRESPOPER,
                             IDTIPOOPER,   DSOPERACAO, CDPESQITEM,
                             DSRDMUSUOPER, DSENDUSUOPER, DSNMUSUOPER,
                             DSMETUSUOPER, CDORESPOPER)
                  VALUES   ('ITEMPREBASE', SYSDATE ,    NVL(V_NMOPERADOR,' '),
                             V_IDTIPOOPER,   V_DESC, V_ITPESQ,
                             V_DSSERVERRDM, V_DSREMOTEADDR,  V_DSREMOTEHOST,
                             V_DSSERVMETHOD,   V_DSREMOTEOPER);
   END IF;
END;
/