CREATE TRIGGER TTBFOLPAG_INSS_B02I FOR TBFOLPAG_INSS
ACTIVE BEFORE INSERT POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN
    HISTORICO = '';
    IF(NEW.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||' Reg. Incluído.';
    END
    
    IF(HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBFOLPAG_INSS',NEW.ID,HISTORICO);
    END
END;

CREATE OR ALTER TRIGGER TTBFOLPAG_INSS_B01U FOR TBFOLPAG_INSS
ACTIVE BEFORE UPDATE POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
DECLARE VARIABLE INTZOLD VARCHAR(10);
DECLARE VARIABLE INTZNEW VARCHAR(10);
BEGIN
    HISTORICO = '';
    IF(OLD.ENCARGO_SALARIO <> NEW.ENCARGO_SALARIO) THEN
    BEGIN
        HISTORICO = HISTORICO||' Enc.Sal: '||FN_LPAD(OLD.ENCARGO_SALARIO,4,'0')||' -> '||FN_LPAD(NEW.ENCARGO_SALARIO,4,'0');
        --HISTORICO = HISTORICO||' Enc.Sal: '||coalesce((select first 1 T.CONTA||' - '||T.DESCRICAO FROM TBFOLPAG_ENCARGO T WHERE T.CONTA = OLD.CONTACONTABIL),'')
        --||' ->'||coalesce((SELECT FIRST 1 T.CONTA||' - '||T.DESCRICAO FROM TBFOLPAG_ENCARGO T WHERE T.CONTA = OLD.CONTACONTABIL),'');
    END

    IF(OLD.ENC_SAL_BS_FERIAS <> NEW.ENC_SAL_BS_FERIAS) THEN
    BEGIN
       HISTORICO = HISTORICO||' Enc.Sal.Bs.Ferias: '||FN_LPAD(OLD.ENC_SAL_BS_FERIAS,4,'0')||' -> '||FN_LPAD(NEW.ENC_SAL_BS_FERIAS,4,'0');
    END

    IF(OLD.ENC_SAL_BS_DECIMO <> NEW.ENC_SAL_BS_DECIMO)THEN
    BEGIN
        HISTORICO = HISTORICO||' Enc.Sal.Bs.Dec: '||FN_LPAD(OLD.ENC_SAL_BS_DECIMO,4,'0')||' -> '||FN_LPAD(NEW.ENC_SAL_BS_DECIMO,4,'0');
    END

    IF(OLD.ENC_INSS_FERIAS <> NEW.ENC_INSS_FERIAS)THEN
    BEGIN
        HISTORICO = HISTORICO||' Enc.INSS.Ferias: '||FN_LPAD(OLD.ENC_INSS_FERIAS,4,'0')||' -> '||FN_LPAD(NEW.ENC_INSS_FERIAS,4,'0');
    END 

    IF(OLD.ENC_INSS_DECIMO <> NEW.ENC_INSS_DECIMO)THEN
    BEGIN
        HISTORICO = HISTORICO||' Enc.INSS.Dec: '||FN_LPAD(OLD.ENC_INSS_DECIMO,4,'0')||' -> '||FN_LPAD(NEW.ENC_INSS_DECIMO,4,'0');
    END 

    IF(OLD.ENC_FGTS_DECIMO <> NEW.ENC_FGTS_DECIMO)THEN
    BEGIN
        HISTORICO = HISTORICO||' Enc.FGTS.Dec: '||FN_LPAD(OLD.ENC_FGTS_DECIMO,4,'0')||' -> '||FN_LPAD(NEW.ENC_FGTS_DECIMO,4,'0');
    END

    IF(OLD.TETO <> NEW.TETO)THEN
    BEGIN
        INTZNEW = CASE WHEN NEW.TETO = '1' THEN 'Sim'
                    WHEN NEW.TETO = '0' THEN 'Não'
                    ELSE ''
                    END;
        INTZOLD = CASE WHEN OLD.TETO = '1' THEN 'Sim'
                    WHEN OLD.TETO = '0' THEN 'Não'
                    ELSE ''
                    END;

        HISTORICO = HISTORICO||' Teto: '||INTZOLD||' -> '||INTZNEW;
    END

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

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

    IF(OLD.PERCENTUAL <> NEW.PERCENTUAL)THEN
    BEGIN
        HISTORICO = HISTORICO||' Perc: '||OLD.PERCENTUAL||' -> '||NEW.PERCENTUAL;
    END
    
    IF(OLD.DATA_INICIAL <> NEW.DATA_INICIAL)THEN
    BEGIN
        HISTORICO = HISTORICO||' Dt.Inicial: '||FORMATDATE(OLD.DATA_INICIAL)||' -> '||FORMATDATE(NEW.DATA_INICIAL);
    END   
    
    IF(OLD.DATA_FINAL <> NEW.DATA_FINAL)THEN
    BEGIN
        HISTORICO = HISTORICO||' Dt.Final: '||FORMATDATE(OLD.DATA_FINAL)||' -> '||FORMATDATE(NEW.DATA_FINAL);
    END
    
    IF(HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBFOLPAG_INSS',NEW.ID,HISTORICO);
    END
END;

CREATE TRIGGER TTBFOLPAG_INSS_B01D FOR TBFOLPAG_INSS
ACTIVE BEFORE DELETE POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN
    HISTORICO = '';
    IF(OLD.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||' Reg. Excluído.';
    END
    
    IF(HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBFOLPAG_INSS',OLD.ID,HISTORICO);
    END
END;



