CREATE OR ALTER trigger ttbcolaborador_descontos_b01u for tbcolaborador_descontos
active before update position 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN
    HISTORICO = '';

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

    IF(NEW.DATA_FOLPAG <> OLD.DATA_FOLPAG) THEN
    BEGIN
        HISTORICO = HISTORICO||' DT.FOLPAG:'||FORMATDATE(OLD.DATA_FOLPAG)||'->'||FORMATDATE(NEW.DATA_FOLPAG);
    END

    IF(NEW.COLABORADOR_MATRICULA <> OLD.COLABORADOR_MATRICULA) THEN
    BEGIN
        HISTORICO = HISTORICO||' COLAB:'||FN_LPAD(OLD.COLABORADOR_MATRICULA, 7, '')||'->'||FN_LPAD(NEW.COLABORADOR_MATRICULA, 7, '');
    END  

    IF(NEW.VERBA_CODIGO <> OLD.VERBA_CODIGO) THEN
    BEGIN
        HISTORICO = HISTORICO||' VERBA:'||FN_LPAD(OLD.VERBA_CODIGO, 4, '')||'->'||FN_LPAD(NEW.VERBA_CODIGO, 4, '');
    END

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

    IF(NEW.VALOR <> OLD.VALOR) THEN
    BEGIN
        HISTORICO = HISTORICO||' VALOR:'||'R$'||CAST(OLD.VALOR AS NUMERIC(15,2))||'->'||'R$'||CAST(NEW.VALOR AS NUMERIC(15,2));
    END 

    IF(HISTORICO <> '') THEN
    BEGIN
        HISTORICO = ' REGISTRO ALTERADO:'||HISTORICO;
        EXECUTE PROCEDURE SPI_HISTORICO('TBCOLABORADOR_DESCONTOS',NEW.ID,FN_ELIPSES(HISTORICO,500));
    END
END;

