UPDATE OR INSERT INTO TBMENU (CONTROLE, GRUPO, SUBGRUPO, TIPO, URL, DESCRICAO) 
	VALUES (23440, 'RH', 'RH', '1', '', 'REGISTRO DE VERBAS FIXAS POR COLABORADOR')
	MATCHING (CONTROLE);

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

/******************************************************************************/
/****                                Tables                                ****/
/******************************************************************************/


CREATE GENERATOR GTBFOLPAG_VERBA_COLABORADOR;

CREATE TABLE TBFOLPAG_VERBA_COLABORADOR (
    ID              INTEGER NOT NULL,
    COLABORADOR_ID  INTEGER,
    VERBA_ID        INTEGER,
    STATUS          CHAR(1),
    DATA_REG        DATE DEFAULT 'NOW',
    VALOR           NUMERIC(15,4),
    OBSERVACAO      VARCHAR(200)
);




/******************************************************************************/
/****                             Primary keys                             ****/
/******************************************************************************/

ALTER TABLE TBFOLPAG_VERBA_COLABORADOR ADD CONSTRAINT PK_TBFOLPAG_VERBA_COLABORADOR PRIMARY KEY (ID);


/******************************************************************************/
/****                               Triggers                               ****/
/******************************************************************************/



SET TERM ^ ;



/******************************************************************************/
/****                         Triggers for tables                          ****/
/******************************************************************************/



/* Trigger: TTBFOLPAG_VERBA_COLAB_B01D */
CREATE OR ALTER TRIGGER TTBFOLPAG_VERBA_COLAB_B01D FOR TBFOLPAG_VERBA_COLABORADOR
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('TBFOLPAG_VERBA_COLABORADOR', OLD.ID, HISTORICO);
    END
END
^


/* Trigger: TTBFOLPAG_VERBA_COLAB_B01I */
CREATE OR ALTER TRIGGER TTBFOLPAG_VERBA_COLAB_B01I FOR TBFOLPAG_VERBA_COLABORADOR
ACTIVE BEFORE INSERT POSITION 0
AS
BEGIN
    IF (NEW.ID IS NULL) THEN
        NEW.ID = GEN_ID(GTBFOLPAG_VERBA_COLABORADOR, 1);
END
^


/* Trigger: TTBFOLPAG_VERBA_COLAB_B01U */
CREATE OR ALTER TRIGGER TTBFOLPAG_VERBA_COLAB_B01U FOR TBFOLPAG_VERBA_COLABORADOR
ACTIVE BEFORE UPDATE POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(5000);

DECLARE VARIABLE OLD_COLABORADOR VARCHAR(500);
DECLARE VARIABLE NEW_COLABORADOR VARCHAR(500);
DECLARE VARIABLE OLD_VERBA VARCHAR(500);
DECLARE VARIABLE NEW_VERBA VARCHAR(500);
BEGIN
    
    HISTORICO = '';

    OLD_COLABORADOR = COALESCE((SELECT FIRST 1 FN_LPAD(C.CODIGO, 6, '0')||' - '||C.PESSOAL_NOME FROM TBCOLABORADOR C WHERE C.CODIGO = OLD.COLABORADOR_ID), '');
    NEW_COLABORADOR = COALESCE((SELECT FIRST 1 FN_LPAD(C.CODIGO, 6, '0')||' - '||C.PESSOAL_NOME FROM TBCOLABORADOR C WHERE C.CODIGO = NEW.COLABORADOR_ID), '');
    OLD_VERBA = COALESCE((SELECT FIRST 1 FN_LPAD(V.ID, 6, '0')||' - '||V.DESCRICAO_ABREVIADA FROM TBFOLPAG_VERBA V WHERE V.ID = OLD.VERBA_ID), '');
    NEW_VERBA = COALESCE((SELECT FIRST 1 FN_LPAD(V.ID, 6, '0')||' - '||V.DESCRICAO_ABREVIADA FROM TBFOLPAG_VERBA V WHERE V.ID = NEW.VERBA_ID), '');

    IF (OLD.COLABORADOR_ID <> NEW.COLABORADOR_ID) THEN
    BEGIN
        HISTORICO = HISTORICO||' COLAB:'||OLD_COLABORADOR||'->'||NEW_COLABORADOR;
    END

    IF (OLD.VERBA_ID <> NEW.VERBA_ID) THEN
    BEGIN
        HISTORICO = HISTORICO||' VERBA:'||OLD_VERBA||'->'||NEW_VERBA;
    END

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

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

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

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

    IF (HISTORICO <> '') THEN
    BEGIN
        HISTORICO = 'REGISTRO ALTERADO:'||HISTORICO;
        EXECUTE PROCEDURE SPI_HISTORICO('TBFOLPAG_VERBA_COLABORADOR', OLD.ID, FN_ELIPSE(HISTORICO, 500));
    END
END
^


/* Trigger: TTBFOLPAG_VERBA_COLAB_B02I */
CREATE OR ALTER TRIGGER TTBFOLPAG_VERBA_COLAB_B02I FOR TBFOLPAG_VERBA_COLABORADOR
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('TBFOLPAG_VERBA_COLABORADOR', NEW.ID, HISTORICO);
    END
END
^


/* Trigger: TTBFOLPAG_VERBA_COLAB_B02U */
CREATE OR ALTER TRIGGER TTBFOLPAG_VERBA_COLAB_B02U FOR TBFOLPAG_VERBA_COLABORADOR
ACTIVE BEFORE UPDATE POSITION 1
AS
DECLARE VARIABLE HISTORICO VARCHAR(5000);
BEGIN
    
    HISTORICO = '';

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

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

SET TERM ; ^



/******************************************************************************/
/****                         Fields descriptions                          ****/
/******************************************************************************/

COMMENT ON COLUMN TBFOLPAG_VERBA_COLABORADOR.STATUS IS 
'0 - INATIVO;
1 - ATIVO;';

COMMENT ON COLUMN TBFOLPAG_VERBA_COLABORADOR.VALOR IS 
'Valor Fixo para uso no cálculo da geração da folha';