create or replace TRIGGER LOG_ITEMPRODUTO_EXP
BEFORE INSERT ON ITEMPRODUTO 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(40);
V_DESC             VARCHAR2(700);
V_CDPESQ_EXP       VARCHAR2(80);
V_IDDOCREPLIC      VARCHAR2(14);
V_IDORIGDESTNF     VARCHAR2(1);
V_IDENTRSAIDOP     VARCHAR2(1);
V_DESC_COMPL       VARCHAR2(15);
V_QTRESERVADA      FLOAT;
V_IDBLOQINCOSGERNF VARCHAR2(1);

V_DTOPER           DATE           := SYSDATE;
V_LISTA_RDM        VARCHAR2(200)  := 'rdmLancExpPed_rdmLancExp';
V_EXCEPTION        VARCHAR2(5000) := '';

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_ITEMPRODUTO_EXP V_LISTA_RDM:'||V_LISTA_RDM||'  V_DSSERVERRDM:'||V_DSSERVERRDM);            

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

      IF INSERTING THEN

         V_ITPESQ := :NEW.CDFILIAL||:NEW.NRLANCTONF||:NEW.NRSEQITPROD;
         
         BEGIN
            SELECT IDORIGDESTNF, IDENTRSAIDOP, IDDOCREPLIC
              INTO V_IDORIGDESTNF, V_IDENTRSAIDOP, V_IDDOCREPLIC
              FROM NOTAFISCAL
             WHERE CDFILIAL = :NEW.CDFILIAL
               AND NRLANCTONF = :NEW.NRLANCTONF;
         EXCEPTION
            WHEN NO_DATA_FOUND THEN
               V_IDDOCREPLIC := '';
         END;
         
         CASE WHEN (V_IDORIGDESTNF = 'L' AND V_IDENTRSAIDOP = 'E') THEN V_DESC_COMPL := 'TRANSF_ENTRADA';
              WHEN (V_IDORIGDESTNF = 'L' AND V_IDENTRSAIDOP = 'S') THEN V_DESC_COMPL := 'TRANSF_SAIDA';
              WHEN (V_IDORIGDESTNF = 'C' AND V_IDENTRSAIDOP = 'S') THEN V_DESC_COMPL := 'VENDA';
             ELSE V_DESC_COMPL := 'OUTROS';
         END CASE;  
      
         IF V_IDORIGDESTNF = 'L' AND V_IDENTRSAIDOP = 'E'  THEN            
            V_CDPESQ_EXP := 'REPLICACAO';
            V_DESC   := 'TIPO:'||V_DESC_COMPL||' DOCREPLIC:'||V_IDDOCREPLIC||' CDFILIAL:'||:NEW.CDFILIAL||' NRLANCTONF:'||:NEW.NRLANCTONF||' CDPRODUTO:'||:NEW.CDPRODUTO||' CDPRODEMIS:'||:NEW.CDPRODEMIS||' DSPRODEMIS:'||:NEW.DSPRODEMIS||' QTITEMPROD:'||:NEW.QTITEMPROD||' VRUNITITEM:'||:NEW.VRUNITITEM;
         ELSE
            V_CDPESQ_EXP := :NEW.CDFILIALPED||:NEW.NRPEDIDO||:NEW.NRITEMPED;
            V_DESC   := 'TIPO:'||V_DESC_COMPL||' CDFILIAL:'||:NEW.CDFILIAL||' NRLANCTONF:'||:NEW.NRLANCTONF||' CDPRODUTO:'||:NEW.CDPRODUTO||' CDPRODEMIS:'||:NEW.CDPRODEMIS||' DSPRODEMIS:'||:NEW.DSPRODEMIS||' CDFILIALPED:'||:NEW.CDFILIALPED||' NRPEDIDO:'||:NEW.NRPEDIDO||' NRITEMPED:'||:NEW.NRITEMPED||' QTITEMPROD:'||:NEW.QTITEMPROD||' VRUNITITEM:'||:NEW.VRUNITITEM;
         END IF;
         
         INSERT INTO LOGOPERAEXP(NMTABELA,   DTOPERACAO, NMRESPOPER,
                                 DSTIPOOPER, CDPESQITEM, DSOPERACAO, CDPESQ_EXP,
                                 DSRDMUSUOPER, DSENDUSUOPER, DSNMUSUOPER, DSMETUSUOPER, CDORESPOPER)
                           VALUES('ITEMPRODUTO',  V_DTOPER, NVL(V_NMOPERADOR,'XXX'),
                                   V_DSTIPOOPER,   V_ITPESQ, V_DESC, V_CDPESQ_EXP,
                                   V_DSSERVERRDM, V_DSREMOTEADDR, V_DSREMOTEHOST, V_DSSERVMETHOD, V_DSREMOTEOPER);
      END IF;
      
      BEGIN
         SELECT IDBLOQINCOSGERNF
           INTO V_IDBLOQINCOSGERNF
           FROM PAREXPEDICAO
          WHERE CDFILIAL = :NEW.CDFILIAL;
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_IDBLOQINCOSGERNF := 'N';
      END;
      
      BEGIN
         SELECT SUM(QTRESPRODPED)
           INTO V_QTRESERVADA
           FROM RESERPRODPED 
          WHERE CDFILIAL   = :NEW.CDFILIAL 
            AND NRPEDIDO   = :NEW.NRPEDIDO
            AND NRITEMPED  = :NEW.NRITEMPED
            AND NRLOTEESTQ = :NEW.NRLOTEESTQ; 
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_QTRESERVADA := '0';
      END;

      IF V_IDBLOQINCOSGERNF = 'S' THEN
      
         IF :NEW.QTITEMPROD <> V_QTRESERVADA THEN
      
            V_EXCEPTION := CHR(13)||'******TRATAMENTO DE EXCESSAO CUSTOMIZADO******'||CHR(13)||
                                     '<<EXCESSAO>> '||'PARAMETRO (Bloqueia Expedicao Com Divergencias entre Reserva X NF) NA TELA PARAMETRO DE EXPEDICAO'||CHR(13)||
                                     '<<MOTIVO>> '   ||'DIVERGENCIA NF X RESERVA'||CHR(13)||
                                     '<<QUANT NF>> ' ||'QUANTIDADE NA NF:'||:NEW.QTITEMPROD||CHR(13)||
                                     '<<QUANT RES>> '||'QUANTIDADE RESERVADA:'||V_QTRESERVADA||CHR(13)||
                                     '<<DADOS PED>> '||'FILIAL:'||:NEW.CDFILIAL||' PEDIDO:'||:NEW.NRPEDIDO||' NRITEMPED:'||:NEW.NRITEMPED||' NRLOTEESTQ:'||:NEW.NRLOTEESTQ||CHR(13)||
                                     '<<DADOS ADICIONAIS>> '||'DADOS COMPLETOS ESTAO ARMAZENADOS NA TABELA LOGOPERAEXP'||CHR(13)||
                                     '***********************************************'||CHR(13)||CHR(13);      
            
            Raise_Application_Error (-20343, V_EXCEPTION); 
         END IF;
      END IF;

   END IF;

END;
/