CREATE trigger ttbcolaborador_credito_b02i for tbcolaborador_credito
active before insert position 1
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF ( NEW.id > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||'REGISTRO INCLUIDO';
    END

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



CREATE trigger ttbcolaborador_credito_b01d for tbcolaborador_credito
active before delete position 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF ( old.id > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||'REGISTRO EXCLUIDO';
    END

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



CREATE OR ALTER trigger ttbcolaborador_credito_b01u for tbcolaborador_credito
active before update position 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF (OLD.COLABORADOR_ID IS distinct FROM NEW.COLABORADOR_ID) THEN
       BEGIN
       HISTORICO = HISTORICO||' COLAB:'||COALESCE(OLD.COLABORADOR_ID, 0)||'->'||COALESCE(NEW.COLABORADOR_ID, 0);
       END

    IF (OLD.VALOR_CREDITO <> NEW.VALOR_CREDITO) THEN
       BEGIN
       HISTORICO = HISTORICO||' VR.CREDITO:'||FN_REPLACE(OLD.VALOR_CREDITO, '.', ',')||'->'||FN_REPLACE(NEW.VALOR_CREDITO, '.', ',');
       END
       
    IF (OLD.SALDO <> NEW.SALDO) THEN
       BEGIN
       HISTORICO = HISTORICO||' SALDO:'||FN_REPLACE(OLD.SALDO, '.', ',')||'->'||FN_REPLACE(NEW.SALDO, '.', ',');
       END
    
    IF (HISTORICO <> '') THEN
    BEGIN
        HISTORICO = ' REGISTRO ALTERADO:'||HISTORICO;
        EXECUTE PROCEDURE SPI_HISTORICO('TBCOLABORADOR_CREDITO', NEW.ID, HISTORICO);
    END
END;

