/*TRIGGERS que alimentam tabela que controla quando deve calcular os dados de venda dos Dashboards*/


CREATE OR REPLACE TRIGGER LOG_ALTERA_DASHBOARD_VENDA
AFTER DELETE OR UPDATE
ON VENDA FOR EACH ROW
DECLARE
	V_CDFILIALLOG VARCHAR2(4);
	P_CDFILIAL VARCHAR2(4);
	V_DTLOGDASH DATE;
	P_DTLOGDASH DATE;
BEGIN
	V_CDFILIALLOG := NULL;
	V_DTLOGDASH := NULL;

	P_CDFILIAL := NVL(:OLD.CDFILIAL, :NEW.CDFILIAL);
	P_DTLOGDASH := NVL(:OLD.DTENTRVENDA, :NEW.DTENTRVENDA);
	BEGIN
	   SELECT CDFILIAL, DTLOGDASH
	   INTO V_CDFILIALLOG, V_DTLOGDASH
	   FROM  LOGALTERADASH
	   WHERE CDFILIAL = P_CDFILIAL
		 AND DTLOGDASH = P_DTLOGDASH;
	EXCEPTION
	   WHEN NO_DATA_FOUND THEN BEGIN
		  V_CDFILIALLOG   := NULL;
		  V_DTLOGDASH := NULL;
	   END;
	END;
	IF V_CDFILIALLOG IS NULL THEN
		INSERT INTO LOGALTERADASH (CDFILIAL, DTLOGDASH)
		VALUES (P_CDFILIAL, P_DTLOGDASH);
	END IF;
END;

/

CREATE OR REPLACE TRIGGER LOG_ALTERA_DASHBOARD_ITEMVENDA
AFTER DELETE OR INSERT OR UPDATE
ON ITEMVENDA FOR EACH ROW
DECLARE
	V_CDFILIAL VARCHAR2(4);
	V_CDFILIALLOG VARCHAR2(4);
	V_DTENTRVENDA DATE;
	V_DTLOGDASH DATE;

	P_NRSEQVENDA VARCHAR2(10);
	P_CDFILIAL VARCHAR2(4);
	P_CDCAIXA VARCHAR2(3);
BEGIN
	V_CDFILIALLOG := NULL;
	V_DTLOGDASH := NULL;

	P_NRSEQVENDA := NVL(:OLD.NRSEQVENDA, :NEW.NRSEQVENDA);
	P_CDCAIXA := NVL(:OLD.CDCAIXA, :NEW.CDCAIXA);
	P_CDFILIAL := NVL(:OLD.CDFILIAL, :NEW.CDFILIAL);

	BEGIN
		SELECT CDFILIAL, DTENTRVENDA
		INTO V_CDFILIAL, V_DTENTRVENDA
		FROM VENDA
		WHERE NRSEQVENDA = P_NRSEQVENDA
		  AND CDFILIAL = P_CDFILIAL
		  AND CDCAIXA = P_CDCAIXA;
	EXCEPTION
	   WHEN NO_DATA_FOUND THEN BEGIN
		  V_CDFILIAL   := NULL;
		  V_DTENTRVENDA := NULL;
	   END;
	END;

	BEGIN
	   SELECT CDFILIAL, DTLOGDASH
	   INTO V_CDFILIALLOG, V_DTLOGDASH
	   FROM  LOGALTERADASH
	   WHERE CDFILIAL = V_CDFILIAL
		 AND DTLOGDASH = V_DTENTRVENDA;
	EXCEPTION
	   WHEN NO_DATA_FOUND THEN BEGIN
		  V_CDFILIALLOG := NULL;
		  V_DTLOGDASH := NULL;
	   END;
	END;
	IF V_CDFILIALLOG IS NULL THEN
		INSERT INTO LOGALTERADASH (CDFILIAL, DTLOGDASH)
		VALUES (V_CDFILIAL, V_DTENTRVENDA);
	END IF;
END;

/

CREATE OR REPLACE TRIGGER LOG_ALTERA_DASHBOARD_MOVCAIXA
AFTER DELETE OR INSERT OR UPDATE
ON MOVCAIXA FOR EACH ROW
DECLARE
	V_CDFILIALLOG VARCHAR2(4);
	V_DTLOGDASH DATE;
	P_CDFILIAL VARCHAR2(4);
	P_DTMOVIMCAIXA DATE;
BEGIN
	V_CDFILIALLOG := NULL;
	V_DTLOGDASH := NULL;

	P_CDFILIAL := NVL(:OLD.CDFILIAL, :NEW.CDFILIAL);
	P_DTMOVIMCAIXA := NVL(:OLD.DTMOVIMCAIXA, :NEW.DTMOVIMCAIXA);

	BEGIN
	   SELECT CDFILIAL, DTLOGDASH
	   INTO V_CDFILIALLOG, V_DTLOGDASH
	   FROM  LOGALTERADASH
	   WHERE CDFILIAL = P_CDFILIAL
		 AND DTLOGDASH = P_DTMOVIMCAIXA;
	EXCEPTION
	   WHEN NO_DATA_FOUND THEN BEGIN
		  V_CDFILIALLOG := NULL;
		  V_DTLOGDASH := NULL;
	   END;
	END;
	IF V_CDFILIALLOG IS NULL THEN
		INSERT INTO LOGALTERADASH (CDFILIAL, DTLOGDASH)
		VALUES (P_CDFILIAL, P_DTMOVIMCAIXA);
	END IF;
END;

/

CREATE OR REPLACE TRIGGER LOG_ALTERA_DASHBOARD_MOVLOJA
AFTER DELETE OR INSERT OR UPDATE
ON MOVLOJA FOR EACH ROW
DECLARE
	V_CDFILIALLOG VARCHAR2(4);
	P_CDFILIAL VARCHAR2(4);
	V_DTLOGDASH DATE;
	P_DTLOGDASH DATE;
BEGIN
	V_CDFILIALLOG := NULL;
	V_DTLOGDASH := NULL;
	P_CDFILIAL := NVL(:OLD.CDFILIAL, :NEW.CDFILIAL);
	P_DTLOGDASH := NVL(:OLD.DTMOVIMENTO, :NEW.DTMOVIMENTO);

	BEGIN
	   SELECT CDFILIAL, DTLOGDASH
	   INTO V_CDFILIALLOG, V_DTLOGDASH
	   FROM LOGALTERADASH
	   WHERE CDFILIAL = P_CDFILIAL
		 AND DTLOGDASH = P_DTLOGDASH;
	EXCEPTION
	   WHEN NO_DATA_FOUND THEN BEGIN
		  V_CDFILIALLOG := NULL;
		  V_DTLOGDASH := NULL;
	   END;
	END;
	IF V_CDFILIALLOG IS NULL THEN
		INSERT INTO LOGALTERADASH (CDFILIAL, DTLOGDASH)
		VALUES (P_CDFILIAL, P_DTLOGDASH);
	END IF;
END;

/

CREATE OR REPLACE TRIGGER LOG_ALTERA_DASHBOARD_TURCAIXA
AFTER DELETE OR INSERT OR UPDATE
ON TURCAIXA FOR EACH ROW
DECLARE
	V_CDFILIALLOG VARCHAR2(4);
	P_CDFILIAL    VARCHAR2(4);
	V_DTLOGDASH DATE;
	P_DTLOGDASH DATE;
BEGIN
	V_CDFILIALLOG := NULL;
	V_DTLOGDASH := NULL;

	P_CDFILIAL := NVL(:OLD.CDFILIAL, :NEW.CDFILIAL);
	P_DTLOGDASH := NVL(:OLD.DTMOVTURCAIX, :NEW.DTMOVTURCAIX);

	BEGIN
	   SELECT CDFILIAL, DTLOGDASH
	   INTO V_CDFILIALLOG, V_DTLOGDASH
	   FROM  LOGALTERADASH
	   WHERE CDFILIAL = P_CDFILIAL
		 AND DTLOGDASH = P_DTLOGDASH;
	EXCEPTION
	   WHEN NO_DATA_FOUND THEN BEGIN
		  V_CDFILIALLOG := NULL;
		  V_DTLOGDASH := NULL;
	   END;
	END;
	IF V_CDFILIALLOG IS NULL THEN
		INSERT INTO LOGALTERADASH (CDFILIAL, DTLOGDASH)
		VALUES (P_CDFILIAL, P_DTLOGDASH);
	END IF;
END;
/

CREATE OR REPLACE TRIGGER LOG_ALTERA_DASHBOARD_REDUCAOZ
AFTER DELETE OR INSERT OR UPDATE
ON REDUCAOZ FOR EACH ROW
DECLARE
	V_CDFILIALLOG VARCHAR2(4);
	V_DTLOGDASH DATE;
	P_CDFILIAL VARCHAR2(4);
	P_DTLOGDASH DATE;
BEGIN
	V_CDFILIALLOG := NULL;
	V_DTLOGDASH := NULL;
	P_CDFILIAL := NVL(:OLD.CDFILIAL, :NEW.CDFILIAL);
	P_DTLOGDASH := NVL(:OLD.DTREDUCAOZ, :NEW.DTREDUCAOZ);

	BEGIN
	   SELECT CDFILIAL, DTLOGDASH
	   INTO V_CDFILIALLOG, V_DTLOGDASH
	   FROM  LOGALTERADASH
	   WHERE CDFILIAL = P_CDFILIAL
		 AND DTLOGDASH = P_DTLOGDASH;
	EXCEPTION
	   WHEN NO_DATA_FOUND THEN BEGIN
		  V_CDFILIALLOG := NULL;
		  V_DTLOGDASH := NULL;
	   END;
	END;
	IF V_CDFILIALLOG IS NULL THEN
		INSERT INTO LOGALTERADASH (CDFILIAL, DTLOGDASH)
		VALUES (P_CDFILIAL, P_DTLOGDASH);
	END IF;
END;