CREATE OR REPLACE TRIGGER LOG_VND_ITEMVENDA
AFTER DELETE OR UPDATE
ON ITEMVENDA FOR EACH ROW
DECLARE
	V_NMOPERADOR   VARCHAR2(40);
	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_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.CDFILIALEST,'NULL') <> NVL(:OLD.CDFILIALEST,'NULL') THEN 
			V_DESC := 'Filial Lanc. ' || ' ' || NVL(:NEW.CDFILIALEST,'');
		END IF;
		IF NVL(:NEW.NRLANCESTQ,'NULL') <> NVL(:OLD.NRLANCESTQ,'NULL') THEN
			V_DESC := NVL(V_DESC, '') || ' ' || 'Lanc. Estoque ' || ' ' || NVL(:NEW.NRLANCESTQ,'');
		END IF;
		
		V_ITPESQ := :NEW.CDFILIAL||:NEW.CDCAIXA||:NEW.NRSEQVENDA||:NEW.NRSEQUITVEND;
		V_IDTIPOOPER := '2';
	ELSE
		IF DELETING THEN
			IF :OLD.CDFILIALEST IS NOT NULL THEN 
				V_DESC := 'Filial Lanc. ' || ' ' || NVL(:OLD.CDFILIALEST,'');
			END IF;
			IF :OLD.NRLANCESTQ IS NOT NULL THEN
				V_DESC := NVL(V_DESC, '') || ' ' || 'Lanc. Estoque ' || ' ' || NVL(:OLD.NRLANCESTQ,'');
			END IF;
		
			V_ITPESQ := :OLD.CDFILIAL||:OLD.CDCAIXA||:OLD.NRSEQVENDA||:OLD.NRSEQUITVEND;
			V_IDTIPOOPER := '3';
		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
		('ITEMVENDA',
		 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_ITEMVENDA','6.06.000'); END;
/