CREATE OR REPLACE TRIGGER LOG_OBSHISTFILIAL
AFTER DELETE OR INSERT OR UPDATE
ON OBSHISTFILIAL FOR EACH ROW
DECLARE
  V_USUARIO VARCHAR2(40);
  V_TIPO VARCHAR2(1);
  V_CDPESQITEM VARCHAR2(80);
  V_DESC VARCHAR2(4000);
  V_DSREMOTEADDR VARCHAR2(15);
  V_DSREMOTEHOST VARCHAR2(15);
  V_DSSERVERRDM  VARCHAR2(20);
  V_DSSERVMETHOD VARCHAR2(30);
  V_DSREMOTEOPER VARCHAR2(12);
BEGIN
   WEBTUNNING(V_DSREMOTEADDR,
              V_DSREMOTEHOST,
              V_DSSERVERRDM,
              V_DSSERVMETHOD,
              V_DSREMOTEOPER);

   IF DELETING THEN
      V_CDPESQITEM := :OLD.CDFILIAL || TO_CHAR(:OLD.DTHISTFIL, 'DDMMYYYYHH24MISS') || :OLD.CDOPERADOR || :OLD.CDDESCOBSFILIAL;
      V_TIPO := '3';      
   ELSE
      IF UPDATING THEN
         V_CDPESQITEM := :OLD.CDFILIAL || TO_CHAR(:OLD.DTHISTFIL, 'DDMMYYYYHH24MISS') || :OLD.CDOPERADOR || :OLD.CDDESCOBSFILIAL;
         V_TIPO := '2';
      ELSE
         V_CDPESQITEM := :NEW.CDFILIAL || TO_CHAR(:NEW.DTHISTFIL, 'DDMMYYYYHH24MISS') || :NEW.CDOPERADOR || :NEW.CDDESCOBSFILIAL;
         V_TIPO := '1';
      END IF;
   END IF;
   V_DESC := '-';
      
   NMUSUARIO(V_USUARIO);
   IF NOT V_USUARIO IS NULL THEN
      INSERT INTO LOGOPERA
      (NMTABELA, DTOPERACAO, NMRESPOPER, IDTIPOOPER, CDPESQITEM, DSOPERACAO,
      DSRDMUSUOPER, DSENDUSUOPER, DSNMUSUOPER, DSMETUSUOPER, CDORESPOPER)
      VALUES
      ('OBSHISTFILIAL', SYSDATE, V_USUARIO, V_TIPO, V_CDPESQITEM, V_DESC,
      V_DSREMOTEADDR, V_DSREMOTEHOST, V_DSSERVERRDM, V_DSSERVMETHOD, V_DSREMOTEOPER);
   END IF;
END;
/