/* Tabela TIPORECE: TABELA DE TIPO DE RECEBIMENTO*/
/* Descrição: A trigger insere registros na tabela LOGOPERAVND a partir de alterações na tabela TIPORECEFILIAL */
/* Objetivo: Gerar um log que vai registrar as alterações na TIPORECEFILIAL,
             utilizando a tabela de Logs de Operações do VND*/
CREATE OR REPLACE TRIGGER LOG_VND_TIPORECEFILIAL
AFTER DELETE OR INSERT OR UPDATE
ON TIPORECEFILIAL 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(3000);
  V_ITPESQ       VARCHAR2(40);
  V_NMOPERADOR   VARCHAR2(40);
  V_IDTIPOOPER   VARCHAR2(1);
BEGIN
/* IDTIPOOPER - '1' - Inclusão, '2' - Alteração, '3' - Exclusão*/
   V_DESC       := '';
   V_ITPESQ     := '';
   V_NMOPERADOR := '';
   V_IDTIPOOPER := '';
   NMUSUARIO(V_NMOPERADOR);
   WEBTUNNING(V_DSREMOTEADDR,
              V_DSREMOTEHOST,
              V_DSSERVERRDM,
              V_DSSERVMETHOD,
              V_DSREMOTEOPER);
   IF UPDATING THEN
      IF NVL(:NEW.CDFILIAL  ,'NULL') <> NVL(:OLD.CDFILIAL  ,'NULL') THEN 
         V_DESC := 'CDFILIAL ' || ' ' || NVL(:NEW.CDFILIAL ,'' );
      END IF;
      IF NVL(:NEW.CDTIPORECE ,'NULL') <> NVL(:OLD.CDTIPORECE ,'NULL') THEN
         V_DESC := NVL(V_DESC, '') || ' ' || 'CDTIPORECE ' || ' ' || NVL(:NEW.CDTIPORECE,'' );
      END IF;
      IF NVL(:NEW.VRTAXAADMFIL ,999999) <> NVL(:OLD.VRTAXAADMFIL ,999999) THEN
         V_DESC := NVL(V_DESC, '') || ' ' || 'VRTAXAADMFIL ' || ' ' || NVL(:NEW.VRTAXAADMFIL,'' );
      END IF;
      IF NVL(:NEW.CDFORMPGTO ,'NULL') <> NVL(:OLD.CDFORMPGTO ,'NULL') THEN
         V_DESC := NVL(V_DESC, '') || ' ' || 'CDFORMPGTO ' || ' ' || NVL(:NEW.CDFORMPGTO,'' );
      END IF;    
      IF NVL(:NEW.NRDIASADMIFIL ,'NULL') <> NVL(:OLD.NRDIASADMIFIL ,'NULL') THEN
         V_DESC := NVL(V_DESC, '') || ' ' || 'NRDIASADMIFIL ' || ' ' || NVL(:NEW.NRDIASADMIFIL,'' );
      END IF;
      IF NVL(:NEW.CDREDUCONT ,'NULL') <> NVL(:OLD.CDREDUCONT ,'NULL') THEN
         V_DESC := NVL(V_DESC, '') || ' ' || 'CDREDUCONT ' || ' ' || NVL(:NEW.CDREDUCONT,'' );
      END IF;
      IF NVL(:NEW.CDTPBAIXAREC ,'NULL') <> NVL(:OLD.CDTPBAIXAREC ,'NULL') THEN
         V_DESC := NVL(V_DESC, '') || ' ' || 'CDTPBAIXAREC ' || ' ' || NVL(:NEW.CDTPBAIXAREC,'' );
      END IF;
      IF NVL(:NEW.IDGERAFINFIL ,'NULL') <> NVL(:OLD.IDGERAFINFIL ,'NULL') THEN
         V_DESC := NVL(V_DESC, '') || ' ' || 'IDGERAFINFIL ' || ' ' || NVL(:NEW.IDGERAFINFIL,'' );
      END IF;
      IF NVL(:NEW.IDCONSVRDEBCONS ,'NULL') <> NVL(:OLD.IDCONSVRDEBCONS ,'NULL') THEN
         V_DESC := NVL(V_DESC, '') || ' ' || 'IDCONSVRDEBCONS ' || ' ' || NVL(:NEW.IDCONSVRDEBCONS,'' );
      END IF;
      IF NVL(:NEW.IDGERABAIXAUTO ,'NULL') <> NVL(:OLD.IDGERABAIXAUTO ,'NULL') THEN
         V_DESC := NVL(V_DESC, '') || ' ' || 'IDGERABAIXAUTO ' || ' ' || NVL(:NEW.IDGERABAIXAUTO,'' );
      END IF;
      V_ITPESQ := :NEW.CDTIPORECE;
      V_IDTIPOOPER := '2';
   ELSE
      IF INSERTING THEN
      IF :NEW.CDFILIAL  IS NOT NULL  THEN 
         V_DESC := 'CDFILIAL ' || ' ' || NVL(:NEW.CDFILIAL ,'' );
      END IF;
      IF :NEW.CDTIPORECE IS NOT NULL THEN
         V_DESC := NVL(V_DESC, '') || ' ' || 'CDTIPORECE ' || ' ' || NVL(:NEW.CDTIPORECE,'' );
      END IF;
      IF :NEW.VRTAXAADMFIL IS NOT NULL THEN
         V_DESC := NVL(V_DESC, '') || ' ' || 'VRTAXAADMFIL ' || ' ' || NVL(:NEW.VRTAXAADMFIL,'' );
      END IF;
      IF :NEW.CDFORMPGTO IS NOT NULL THEN
         V_DESC := NVL(V_DESC, '') || ' ' || 'CDFORMPGTO ' || ' ' || NVL(:NEW.CDFORMPGTO,'' );
      END IF;
      IF :NEW.NRDIASADMIFIL IS NOT NULL THEN
         V_DESC := NVL(V_DESC, '') || ' ' || 'NRDIASADMIFIL ' || ' ' || NVL(:NEW.NRDIASADMIFIL,'' );
      END IF;
      IF :NEW.CDREDUCONT IS NOT NULL THEN
         V_DESC := NVL(V_DESC, '') || ' ' || 'CDREDUCONT ' || ' ' || NVL(:NEW.CDREDUCONT,'' );
      END IF;
      IF :NEW.CDTPBAIXAREC IS NOT NULL THEN
         V_DESC := NVL(V_DESC, '') || ' ' || 'CDTPBAIXAREC ' || ' ' || NVL(:NEW.CDTPBAIXAREC,'' );
      END IF;
      IF :NEW.IDGERAFINFIL IS NOT NULL THEN
         V_DESC := NVL(V_DESC, '') || ' ' || 'IDGERAFINFIL ' || ' ' || NVL(:NEW.IDGERAFINFIL,'' );
      END IF;
      IF :NEW.IDCONSVRDEBCONS IS NOT NULL THEN
         V_DESC := NVL(V_DESC, '') || ' ' || 'IDCONSVRDEBCONS ' || ' ' || NVL(:NEW.IDCONSVRDEBCONS,'' );
      END IF;
      IF :NEW.IDGERABAIXAUTO IS NOT NULL THEN
         V_DESC := NVL(V_DESC, '') || ' ' || 'IDGERABAIXAUTO ' || ' ' || NVL(:NEW.IDGERABAIXAUTO,'' );
      END IF;
      V_ITPESQ := :NEW.CDTIPORECE;
         V_IDTIPOOPER := '1';
      ELSE
      IF DELETING THEN
      IF :OLD.CDFILIAL  IS NOT NULL  THEN 
         V_DESC := 'CDFILIAL ' || ' ' || NVL(:OLD.CDFILIAL ,'' );
      END IF;     
      IF :OLD.CDTIPORECE IS NOT NULL THEN
         V_DESC := NVL(V_DESC, '') || ' ' || 'CDTIPORECE ' || ' ' || NVL(:OLD.CDTIPORECE,'' );
      END IF;
      IF :OLD.VRTAXAADMFIL IS NOT NULL THEN
         V_DESC := NVL(V_DESC, '') || ' ' || 'VRTAXAADMFIL ' || ' ' || NVL(:OLD.VRTAXAADMFIL,'' );
      END IF;
      IF :OLD.CDFORMPGTO IS NOT NULL THEN
         V_DESC := NVL(V_DESC, '') || ' ' || 'CDFORMPGTO ' || ' ' || NVL(:OLD.CDFORMPGTO,'' );
      END IF;
      IF :OLD.NRDIASADMIFIL IS NOT NULL THEN
         V_DESC := NVL(V_DESC, '') || ' ' || 'NRDIASADMIFIL ' || ' ' || NVL(:OLD.NRDIASADMIFIL,'' );
      END IF;
      IF :OLD.CDREDUCONT IS NOT NULL THEN
         V_DESC := NVL(V_DESC, '') || ' ' || 'CDREDUCONT ' || ' ' || NVL(:OLD.CDREDUCONT,'' );
      END IF;
      IF :OLD.CDTPBAIXAREC IS NOT NULL THEN
         V_DESC := NVL(V_DESC, '') || ' ' || 'CDTPBAIXAREC ' || ' ' || NVL(:OLD.CDTPBAIXAREC,'' );
      END IF;
      IF :OLD.IDGERAFINFIL IS NOT NULL THEN
         V_DESC := NVL(V_DESC, '') || ' ' || 'IDGERAFINFIL ' || ' ' || NVL(:OLD.IDGERAFINFIL,'' );
      END IF;
      IF :OLD.IDCONSVRDEBCONS IS NOT NULL THEN
         V_DESC := NVL(V_DESC, '') || ' ' || 'IDCONSVRDEBCONS ' || ' ' || NVL(:OLD.IDCONSVRDEBCONS,'' );
      END IF;
      IF :OLD.IDGERABAIXAUTO IS NOT NULL THEN
         V_DESC := NVL(V_DESC, '') || ' ' || 'IDGERABAIXAUTO ' || ' ' || NVL(:OLD.IDGERABAIXAUTO,'' );
      END IF;         
      V_ITPESQ := :OLD.CDTIPORECE;
            V_IDTIPOOPER := '3';
         END IF;
      END IF;
   END IF;
   V_DESC := SUBSTR(V_DESC, 1, 300);
   INSERT INTO LOGOPERAVND
      (NMTABELAVND,
       DTOPERACAOVND,
       NMRESPOPERVND,
       IDTIPOOPERVND,
       DSOPERACAOVND,
       CDPESQITEMVND,
       DSRDMUSUOPERVND,
       DSENDUSUOPERVND,
       DSNMUSUOPERVND,
       DSMETUSUOPERVND,
       CDORESPOPERVND)
   VALUES
      ('TIPORECEFILIAL',
       SYSDATE, 
       NVL(V_NMOPERADOR,' '),
       V_IDTIPOOPER,
       NVL(V_DESC,' '),
       V_ITPESQ,
       V_DSSERVERRDM,
       V_DSREMOTEADDR,
       V_DSREMOTEHOST,
       V_DSSERVMETHOD,
       V_DSREMOTEOPER);
END;
/

-- VERSAO --
BEGIN ATUVEROBJ('LOG_VND_TIPORECEFILIAL','5.20.001'); END;
/
