create or replace TRIGGER LOG_LOTEQRCODE_EXP
BEFORE DELETE OR INSERT ON LOTEQRCODE 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_NMOPERADOR   VARCHAR2(40);
V_DSTIPOOPER   VARCHAR2(20);
V_USUARIO      VARCHAR2(40);
V_ITPESQ       VARCHAR2(80);
V_DESC         VARCHAR2(700);

V_DTOPER       DATE          := SYSDATE;
V_LISTA_RDM    VARCHAR2(200) := 'rdmLeitorQrcode_rdmLancExpPed';

BEGIN

   CASE WHEN INSERTING THEN V_DSTIPOOPER := 'INSERTING';
        WHEN DELETING  THEN V_DSTIPOOPER := 'DELETEING';
       ELSE V_DSTIPOOPER := 'UPDATEING';
   END CASE; 

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

   --Raise_Application_Error (-20343, 'EXCESSAO LOG_RESERPRODPED_EXP V_LISTA_RDM:'||V_LISTA_RDM||'  V_DSSERVERRDM:'||V_DSSERVERRDM);            

   IF (INSTR(V_LISTA_RDM, REPLACE(V_DSSERVERRDM, ' ', '')) > 0) THEN 

      IF INSERTING THEN
        --Raise_Application_Error (-20343, 'EXCESSAO LOG_RESERPRODPED_EXP V_LISTA_RDM:'||V_LISTA_RDM||'  V_DSSERVERRDM:'||V_DSSERVERRDM); 
         V_ITPESQ := :NEW.CDFILIAL||:NEW.NRPEDIDO||:NEW.NRITEMPED||:NEW.CDALMOXARIFE||:NEW.CDLOCALESTOQ||:NEW.NRLOTEESTQ||:NEW.NRSUBLOTE;

         V_DESC   := 'CDFILIAL:'||:NEW.CDFILIAL||' NRPEDIDO:'||:NEW.NRPEDIDO||' NRITEMPED:'||:NEW.NRITEMPED||' CDALMOXARIFE:'||:NEW.CDALMOXARIFE||' CDLOCALESTOQ:'||:NEW.CDLOCALESTOQ||' NRLOTEESTQ:'||:NEW.NRLOTEESTQ||' NRSUBLOTE:'||:NEW.NRSUBLOTE||' QTVERIFICADA:'||:NEW.QTVERIFICADA;

         INSERT INTO LOGOPERAEXP(NMTABELA,   DTOPERACAO, NMRESPOPER,
                                 DSTIPOOPER, CDPESQITEM, DSOPERACAO, CDPESQ_EXP,
                                 DSRDMUSUOPER, DSENDUSUOPER, DSNMUSUOPER, DSMETUSUOPER, CDORESPOPER)
                           VALUES('LOTEQRCODE',  V_DTOPER, NVL(V_NMOPERADOR,'XXX'),
                                   V_DSTIPOOPER,   V_ITPESQ, V_DESC, (:NEW.CDFILIAL||:NEW.NRPEDIDO||:NEW.NRITEMPED),
                                   V_DSSERVERRDM, V_DSREMOTEADDR, V_DSREMOTEHOST, V_DSSERVMETHOD, V_DSREMOTEOPER);
      END IF;

      IF DELETING THEN

         V_ITPESQ := :OLD.CDFILIAL||:OLD.NRPEDIDO||:OLD.NRITEMPED||:OLD.CDALMOXARIFE||:OLD.CDLOCALESTOQ||:OLD.NRLOTEESTQ||:OLD.NRSUBLOTE;

         V_DESC   := 'CDFILIAL:'||:OLD.CDFILIAL||' NRPEDIDO:'||:OLD.NRPEDIDO||' NRITEMPED:'||:OLD.NRITEMPED||' CDALMOXARIFE:'||:OLD.CDALMOXARIFE||' CDLOCALESTOQ:'||:OLD.CDLOCALESTOQ||' NRLOTEESTQ:'||:OLD.NRLOTEESTQ||' NRSUBLOTE:'||:OLD.NRSUBLOTE||' QTVERIFICADA:'||:OLD.QTVERIFICADA;

         INSERT INTO LOGOPERAEXP(NMTABELA,   DTOPERACAO, NMRESPOPER,
                                 DSTIPOOPER, CDPESQITEM, DSOPERACAO, CDPESQ_EXP,
                                 DSRDMUSUOPER, DSENDUSUOPER, DSNMUSUOPER, DSMETUSUOPER, CDORESPOPER)
                           VALUES('LOTEQRCODE',  V_DTOPER, NVL(V_USUARIO,'XXX'),
                                   V_DSTIPOOPER,   V_ITPESQ, V_DESC, (:OLD.CDFILIAL||:OLD.NRPEDIDO||:OLD.NRITEMPED),
                                   V_DSSERVERRDM, V_DSREMOTEADDR, V_DSREMOTEHOST, V_DSSERVMETHOD, V_DSREMOTEOPER);    

      END IF;

   END IF;

END;
/