CREATE TRIGGER TTBFRETE_OCOREN_CAB_B03I FOR TBFRETE_OCOREN_CAB
ACTIVE BEFORE INSERT POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
DECLARE VARIABLE ID_OCORRENCIA INTEGER;
DECLARE VARIABLE ID_EMPRESA INTEGER;
BEGIN

    HISTORICO = '';

    IF (NEW.ID > 0) THEN
    BEGIN
        SELECT FIRST 1 T.ID FROM TBFRETE_OCOREN_342 T WHERE T.FRETE_OCOREN_CAB_ID = NEW.ID INTO ID_OCORRENCIA;
        SELECT FIRST 1 U.CODIGO FROM TBEMPRESA U WHERE U.CNPJ = NEW.TRANSPORTADORA_CNPJ INTO ID_EMPRESA;
        HISTORICO = HISTORICO||'Ocorrencia de frete incluida: '||ID_OCORRENCIA;
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBTRANSPORTADORA', ID_EMPRESA, HISTORICO);
    END
END;

CREATE TRIGGER TTBFRETE_OCOREN_342_B02I FOR TBFRETE_OCOREN_342
ACTIVE BEFORE INSERT POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF (NEW.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||'Ocorrencia de frete incluida. ';
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBFRETE_OCOREN_342', NEW.ID, HISTORICO);
    END
END;

CREATE OR ALTER TRIGGER TTBFRETE_OCOREN_342_B01U FOR TBFRETE_OCOREN_342
ACTIVE BEFORE UPDATE POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

		HISTORICO = '';
		
		IF (OLD.ESTABELECIMENTO_CNPJ <> NEW.ESTABELECIMENTO_CNPJ) THEN
		BEGIN
			HISTORICO = HISTORICO||' Est. CNPJ: '||OLD.ESTABELECIMENTO_CNPJ||' -> '||NEW.ESTABELECIMENTO_CNPJ;
		END
		IF (OLD.DATA <> NEW.DATA or OLD.HORA <> NEW.HORA) THEN
		BEGIN
			HISTORICO = HISTORICO||', Data/Hora: '||FN_DATE_TO_STRING(OLD.DATA)||' '||FORMATTIME_EXT(OLD.HORA)||' -> '||FN_DATE_TO_STRING(NEW.DATA)||' '||FORMATTIME_EXT(NEW.HORA);
		END
		IF (OLD.NF_NUMERO <> NEW.NF_NUMERO or OLD.NF_SERIE <> NEW.NF_SERIE) THEN
		BEGIN
			HISTORICO = HISTORICO||', NF: '||OLD.NF_NUMERO||', Serie: '||OLD.NF_SERIE||' -> NF: '||NEW.NF_NUMERO||', Serie: '||NEW.NF_SERIE;
		END
		IF (OLD.OCORRENCIA_CODIGO <> NEW.OCORRENCIA_CODIGO) THEN
		BEGIN
			HISTORICO = HISTORICO||', Cód. Ocorrencia: '||OLD.OCORRENCIA_CODIGO||' -> '||NEW.OCORRENCIA_CODIGO;
		END
		IF (OLD.OBSERVACAO <> NEW.OBSERVACAO) THEN
		BEGIN
			HISTORICO = HISTORICO||', Observacao Alterada.';
		END
		IF (HISTORICO <> '') THEN
		BEGIN
			EXECUTE PROCEDURE SPI_HISTORICO('TBFRETE_OCOREN_342', NEW.ID, HISTORICO);
		END
END;

CREATE TRIGGER TTBFRETE_OCOREN_342_B01D FOR TBFRETE_OCOREN_342
ACTIVE BEFORE DELETE POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF (OLD.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||'Ocorrencia de frete excluida.';
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBFRETE_OCOREN_342', OLD.ID, HISTORICO);
    END
END;

CREATE OR ALTER TRIGGER TTBFRETE_OCOREN_CAB_B02D FOR TBFRETE_OCOREN_CAB
ACTIVE BEFORE DELETE POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
DECLARE VARIABLE ID_OCORRENCIA INTEGER;
DECLARE VARIABLE ID_EMPRESA INTEGER;
BEGIN

    HISTORICO = '';

    IF (OLD.ID > 0) THEN
    BEGIN
        SELECT FIRST 1 T.ID FROM TBFRETE_OCOREN_342 T WHERE T.FRETE_OCOREN_CAB_ID = OLD.ID INTO ID_OCORRENCIA;
        SELECT FIRST 1 U.CODIGO FROM TBEMPRESA U WHERE U.CNPJ = OLD.TRANSPORTADORA_CNPJ INTO ID_EMPRESA;
        HISTORICO = HISTORICO||'Ocorrencia de frete excluida: '||ID_OCORRENCIA;
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBTRANSPORTADORA', ID_EMPRESA, HISTORICO);
    END
END;

