ALTER TABLE TBCASO_PAINEL_PROCEDIMENTO
    ADD FORMULA BLOB SUB_TYPE 1 SEGMENT SIZE 80 CHARACTER SET UTF8;

/*************************************************************************/
/*************************************************************************/

SET TERM ^ ;

CREATE OR ALTER trigger ttbcaso_painel_proc_b01u for tbcaso_painel_procedimento
active before update position 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(5000);
DECLARE VARIABLE OLD_MOTIVO VARCHAR(500);
DECLARE VARIABLE NEW_MOTIVO VARCHAR(500);
DECLARE VARIABLE OLD_TIPO VARCHAR(500);
DECLARE VARIABLE NEW_TIPO VARCHAR(500);
DECLARE VARIABLE OLD_ORIGEM VARCHAR(500);
DECLARE VARIABLE NEW_ORIGEM VARCHAR(500);
BEGIN

    HISTORICO = '';

    OLD_MOTIVO  = COALESCE((SELECT FIRST 1 FN_LPAD(A.ID, 4, '0')||' - '||A.DESCRICAO FROM TBCASO_MOTIVO A WHERE A.ID = OLD.MOTIVO_ID), '');
    NEW_MOTIVO  = COALESCE((SELECT FIRST 1 FN_LPAD(A.ID, 4, '0')||' - '||A.DESCRICAO FROM TBCASO_MOTIVO A WHERE A.ID = NEW.MOTIVO_ID), '');
    OLD_TIPO    = COALESCE((SELECT FIRST 1 FN_LPAD(B.ID, 4, '0')||' - '||B.DESCRICAO FROM TBCASO_TIPO B WHERE B.ID = OLD.TIPO_ID), '');
    NEW_TIPO    = COALESCE((SELECT FIRST 1 FN_LPAD(B.ID, 4, '0')||' - '||B.DESCRICAO FROM TBCASO_TIPO B WHERE B.ID = NEW.TIPO_ID), '');
    OLD_ORIGEM  = COALESCE((SELECT FIRST 1 FN_LPAD(C.ID, 4, '0')||' - '||C.DESCRICAO FROM TBCASO_ORIGEM C WHERE C.ID = OLD.ORIGEM_ID), '');
    NEW_ORIGEM  = COALESCE((SELECT FIRST 1 FN_LPAD(C.ID, 4, '0')||' - '||C.DESCRICAO FROM TBCASO_ORIGEM C WHERE C.ID = NEW.ORIGEM_ID), '');

    IF (OLD.TABELA <> NEW.TABELA) THEN
    BEGIN
        HISTORICO = HISTORICO||' TABELA:'||OLD.TABELA||'->'||NEW.TABELA;
    END

    IF (OLD.OPERACAO <> NEW.OPERACAO) THEN
    BEGIN
        HISTORICO = HISTORICO||' OPERACAO:'||OLD.OPERACAO||'->'||NEW.OPERACAO;
    END

    IF (OLD.MOTIVO_ID <> NEW.MOTIVO_ID) THEN
    BEGIN
        HISTORICO = HISTORICO||' OPERACAO:'||OLD_MOTIVO||'->'||NEW_MOTIVO;
    END

    IF (OLD.TIPO_ID <> NEW.TIPO_ID) THEN
    BEGIN
        HISTORICO = HISTORICO||' OPERACAO:'||OLD_TIPO||'->'||NEW_TIPO;
    END

    IF (OLD.ORIGEM_ID <> NEW.ORIGEM_ID) THEN
    BEGIN
        HISTORICO = HISTORICO||' OPERACAO:'||OLD_ORIGEM||'->'||NEW_ORIGEM;
    END

    IF (OLD.FORMULA <> NEW.FORMULA) THEN
    BEGIN
        HISTORICO = HISTORICO||' FORMULA ALTERADA!';
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        HISTORICO = 'PROCEDIMENTO '||FN_LPAD(OLD.ID, 4, '0')||' - '||NEW.TABELA||' ALTERADO:'||HISTORICO;
        EXECUTE PROCEDURE SPI_HISTORICO('TBCASO_PAINEL', OLD.PAINEL_ID, FN_ELIPSE(HISTORICO, 500));
    END
END
^

SET TERM ; ^

SET TERM ^ ;

CREATE trigger ttbcaso_painel_proc_b02u for tbcaso_painel_procedimento
active before update position 1
AS
DECLARE VARIABLE HISTORICO VARCHAR(5000);
BEGIN

    HISTORICO = '';

    IF (OLD.FORMULA <> NEW.FORMULA) THEN
    BEGIN
        HISTORICO = HISTORICO||' '||FN_ELIPSE(OLD.FORMULA, 220)||'->'||FN_ELIPSE(NEW.FORMULA, 220);
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        HISTORICO = 'FORMULA ALTERADA:'||HISTORICO;
        EXECUTE PROCEDURE SPI_HISTORICO('TBCASO_PAINEL', OLD.PAINEL_ID, FN_ELIPSE(HISTORICO, 500));
    END
END
^

SET TERM ; ^

