CREATE OR REPLACE TRIGGER LOG_SUP_ITMAPURPREC
AFTER INSERT OR UPDATE OR DELETE
ON ITMAPURPREC 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(1500);
  V_CDFILLOGOPER VARCHAR2(4);
  V_ITPESQ       VARCHAR2(80);
  V_NMOPERADOR   VARCHAR2(40);
  V_IDTIPOOPER   VARCHAR2(1);
  V_IDTIPOOPERACAO VARCHAR2(30);
  V_PRXLOGITMAPURPREC NUMBER;

BEGIN
   /* IDTIPOOPER - '1' - Inclusao, '2' - Alteracao, '3' - Exclusao*/
   V_DESC         := '';
   V_ITPESQ       := '';
   V_NMOPERADOR   := '';
   V_IDTIPOOPER   := '';
   V_CDFILLOGOPER := '';
   NMUSUARIO(V_NMOPERADOR);
   WEBTUNNING(V_DSREMOTEADDR,
              V_DSREMOTEHOST,
              V_DSSERVERRDM,
              V_DSSERVMETHOD,
              V_DSREMOTEOPER);

   IF UPDATING THEN
      IF (NVL(:OLD.QTSOLACOMP, '') <> NVL(:NEW.QTSOLACOMP, '')) THEN
         V_DESC := V_DESC || 'Qtde. Soli.: ' ||''||NVL(:NEW.QTSOLACOMP, '')||'   ';
      END IF;
      IF (NVL(:OLD.QTACOMP, '') <> NVL(:NEW.QTACOMP, '')) THEN
         V_DESC := V_DESC || 'Qt. CMP.' ||''||NVL(:NEW.QTACOMP, '')||'   ';
      END IF;
      IF (NVL(:OLD.IDITMSELAPUR, '') <> NVL(:NEW.IDITMSELAPUR, '')) THEN
         V_DESC := V_DESC || 'Id. Sel.: ' ||''||NVL(:NEW.IDITMSELAPUR, '')||'   ';
      END IF;
      V_ITPESQ       := SUBSTR(:NEW.CDCENTITMAP|| :NEW.NRSEQITMAP|| :NEW.CDREGITMAP|| :NEW.CDFORNITMAP|| :NEW.NRCOLEITMAP|| TO_CHAR(:NEW.DTENTRAPUT, 'DD/MM/YYYY')||
                        :NEW.CDFILIAP|| :NEW.CDPRODITMAP|| :NEW.CDMARCITMAP, 1, 80);
      V_CDFILLOGOPER := :NEW.CDCENTITMAP;
      V_IDTIPOOPER   := '2';
      V_IDTIPOOPERACAO := 'ALTERACAO';
   ELSE
      IF INSERTING THEN
         V_DESC   :=       'Qtde. Soli.: '            ||''  || NVL(:NEW.QTSOLACOMP, '')||'  '  ||
                           'Qt. CMP.:'              ||''  || NVL(:NEW.QTACOMP, '')  ||'  '  ||
                           'Id. Sel.: '             ||''  || NVL(:NEW.IDITMSELAPUR, '');

         V_ITPESQ       := SUBSTR(:NEW.CDCENTITMAP|| :NEW.NRSEQITMAP|| :NEW.CDREGITMAP|| :NEW.CDFORNITMAP|| :NEW.NRCOLEITMAP|| TO_CHAR(:NEW.DTENTRAPUT, 'DD/MM/YYYY')||
                           :NEW.CDFILIAP|| :NEW.CDPRODITMAP|| :NEW.CDMARCITMAP, 1, 80);
         V_CDFILLOGOPER := :NEW.CDCENTITMAP;
         V_IDTIPOOPER   := '1';
         V_IDTIPOOPERACAO := 'INCLUSAO';
      ELSE
         IF DELETING THEN
            V_DESC  :=     'Qtde. Soli.: '            ||''  || NVL(:OLD.QTSOLACOMP, '')||'  '  ||
                           'Qt. CMP.'              ||''  || NVL(:OLD.QTACOMP, '')  ||'  '  ||
                           'Id. Sel.: '             ||''  || NVL(:OLD.IDITMSELAPUR, '');

            V_ITPESQ         := SUBSTR(:OLD.CDCENTITMAP|| :OLD.NRSEQITMAP|| :OLD.CDREGITMAP|| :OLD.CDFORNITMAP|| :OLD.NRCOLEITMAP|| TO_CHAR(:OLD.DTENTRAPUT, 'DD/MM/YYYY')||
                                :OLD.CDFILIAP|| :OLD.CDPRODITMAP|| :OLD.CDMARCITMAP, 1, 80);
            V_CDFILLOGOPER   := :OLD.CDCENTITMAP;
            V_IDTIPOOPER     := '3';
            V_IDTIPOOPERACAO := 'EXCLUSAO';
         END IF;
      END IF;
   END IF;
   IF (V_DESC IS NOT NULL)   THEN
      V_DESC := SUBSTR(V_DESC, 1, 400);
      INSERT INTO LOGOPERA
         (NMTABELA,
          DTOPERACAO,
          NMRESPOPER,
          IDTIPOOPER,
          DSOPERACAO,
          CDPESQITEM,
          DSRDMUSUOPER,
          DSENDUSUOPER,
          DSNMUSUOPER,
          DSMETUSUOPER,
          CDORESPOPER,
          CDFILLOGOPER)
      VALUES
         ('ITMAPURPREC',
          SYSDATE,
          NVL(V_NMOPERADOR,' '),
          V_IDTIPOOPER,
          V_DESC,
          V_ITPESQ,
          V_DSSERVERRDM,
          V_DSREMOTEADDR,
          V_DSREMOTEHOST,
          V_DSSERVMETHOD,
          V_DSREMOTEOPER,
          V_CDFILLOGOPER);
   END IF;
   INSERT
     INTO LOGITMAPURPREC (CDCENTITMAP, NRSEQITMAP, CDREGITMAP,
                          CDFORNITMAP, NRCOLEITMAP,  DTENTRAPUT, CDFILIAP,
                          CDPRODITMAP, CDMARCITMAP,  QTSOLACOMP,  QTACOMP,
                          DTOPERACAO, IDTIPOOPERACAO, IDITMSELAPUR)
   VALUES (NVL(:NEW.CDCENTITMAP, :OLD.CDCENTITMAP),
           NVL(:NEW.NRSEQITMAP, :OLD.NRSEQITMAP), NVL(:NEW.CDREGITMAP, :OLD.CDREGITMAP),
           NVL(:NEW.CDFORNITMAP, :OLD.CDFORNITMAP), NVL(:NEW.NRCOLEITMAP, :OLD.NRCOLEITMAP),
           NVL(:NEW.DTENTRAPUT, :OLD.DTENTRAPUT), NVL(:NEW.CDFILIAP, :OLD.CDFILIAP),
           NVL(:NEW.CDPRODITMAP, :OLD.CDPRODITMAP), NVL(:NEW.CDMARCITMAP, :OLD.CDMARCITMAP),
           NVL(:NEW.QTSOLACOMP, :OLD.QTSOLACOMP), NVL(:NEW.QTACOMP, :OLD.QTACOMP), SYSDATE,
           V_IDTIPOOPERACAO, NVL(:NEW.IDITMSELAPUR, :OLD.IDITMSELAPUR));
END;
/