CREATE OR REPLACE TRIGGER LOG_VND_FAMSALDOFILI
AFTER DELETE OR INSERT OR UPDATE
ON FAMSALDOFILI 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(500);
  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   := 'Filial ' || '' || NVL(:NEW.CDFILIAL,''    );
		END IF;
		IF NVL(:NEW.CDFAMILISALD ,'NULL') <> NVL(:OLD.CDFAMILISALD , 'NULL') THEN
			V_DESC := NVL(V_DESC, '') || ' ' || 'Familia '|| '' || NVL(:NEW.CDFAMILISALD,'');
		END IF;
		IF NVL(:NEW.NRORDAPRSALD ,'NULL') <> NVL(:OLD.NRORDAPRSALD ,'NULL') THEN
            V_DESC := NVL(V_DESC, '') || ' ' || 'Ordem '     || '' || NVL(:NEW.NRORDAPRSALD,''  );
		END IF;
    	IF NVL(:NEW.IDSALDNEGFAM ,'NULL') <> NVL(:OLD.IDSALDNEGFAM ,'NULL') THEN
            V_DESC := NVL(V_DESC, '') || ' ' || 'Sald Neg '     || '' || NVL(:NEW.IDSALDNEGFAM,''  );
		END IF;

		IF NVL(:NEW.VRCONSDIAFAM ,999999) <> NVL(:OLD.VRCONSDIAFAM ,999999) THEN
            V_DESC := NVL(V_DESC, '') || ' ' || 'Vr Cons Dia '  || '' || NVL(:NEW.VRCONSDIAFAM,'');
		END IF;
    	IF NVL(:NEW.IDPERMCARGACRED ,'NULL') <> NVL(:OLD.IDPERMCARGACRED ,'NULL') THEN
            V_DESC := NVL(V_DESC, '') || ' ' || 'Per Carga Cred '     || '' || NVL(:NEW.IDPERMCARGACRED,''  );
		END IF;
      V_ITPESQ := :NEW.CDFILIAL||:NEW.CDFAMILISALD;
      V_IDTIPOOPER := '2';
   ELSE
      IF INSERTING THEN
           V_DESC  := 'Filial '            || '' || NVL(:NEW.CDFILIAL,''     )  ||   
	               ' ' || 'Familia '       || '' || NVL(:NEW.CDFAMILISALD,'' )  || 
	               ' ' || 'Ordem '         || '' || NVL(:NEW.NRORDAPRSALD,'' )  || 
	               ' ' || 'Sald Neg '      || '' || NVL(:NEW.IDSALDNEGFAM,'' )  ||
                   ' ' || 'Vr Cons Dia '   || '' || NVL(:NEW.VRCONSDIAFAM,'' )  ||
	               ' ' || 'Per Carga Cred '|| '' || NVL(:NEW.IDPERMCARGACRED,'') ;
          V_ITPESQ := :NEW.CDFILIAL||:NEW.CDFAMILISALD;
          V_IDTIPOOPER := '1';
      ELSE
         IF DELETING THEN
		    V_DESC := 'Filial '            || '' || NVL(:OLD.CDFILIAL,''     )  ||   
	               ' ' || 'Familia '       || '' || NVL(:OLD.CDFAMILISALD,'' )  || 
	               ' ' || 'Ordem '         || '' || NVL(:OLD.NRORDAPRSALD,'' )  || 
	               ' ' || 'Sald Neg '      || '' || NVL(:OLD.IDSALDNEGFAM,'' )  ||
                   ' ' || 'Vr Cons Dia '   || '' || NVL(:OLD.VRCONSDIAFAM,'' )  ||
	               ' ' || 'Per Carga Cred '|| '' || NVL(:OLD.IDPERMCARGACRED,'') ;
			V_ITPESQ := :OLD.CDFILIAL||:OLD.CDFAMILISALD;
			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,
       CDFILLOGOPERVND)
   VALUES
      ('FAMSALDOFILI',
       SYSDATE,
       NVL(V_NMOPERADOR,' '),
       V_IDTIPOOPER,
       NVL(V_DESC,' '),
       V_ITPESQ,
       V_DSSERVERRDM,
       V_DSREMOTEADDR,
       V_DSREMOTEHOST,
       V_DSSERVMETHOD,
       V_DSREMOTEOPER,
       NVL(:NEW.CDFILIAL,:OLD.CDFILIAL));
END;
/

-- VERSAO --
BEGIN ATUVEROBJ('LOG_VND_FAMSALDOFILI','6.05.000'); END;
/