/******************************************************************************/
/****              Generated by IBExpert 14/08/2023 09:49:27               ****/
/******************************************************************************/

/******************************************************************************/
/****     Following SET SQL DIALECT is just for the Database Comparer      ****/
/******************************************************************************/
SET SQL DIALECT 3;



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


CREATE GENERATOR GTBDRE_FINANCEIRA_GRUPOS;

CREATE TABLE TBDRE_FINANCEIRA_GRUPOS (
    ID                  INTEGER,
    DESCRICAO           VARCHAR(200),
    ORDEM               INTEGER,
    DRE_FINANCEIRA_ID   INTEGER,
    STATUS              INTEGER,
    COR                 VARCHAR(20),
    TIPO_GRUPO          INTEGER DEFAULT 1,
    COR_VALOR_POSITIVO  VARCHAR(20),
    COR_VALOR_NEGATIVO  VARCHAR(20) 
);




/******************************************************************************/
/****                               Indices                                ****/
/******************************************************************************/

CREATE INDEX ITBDRE_FINANCEIRA_GRUPOS_01 ON TBDRE_FINANCEIRA_GRUPOS (ID);
CREATE INDEX ITBDRE_FINANCEIRA_GRUPOS_02 ON TBDRE_FINANCEIRA_GRUPOS (DRE_FINANCEIRA_ID);
CREATE INDEX ITBDRE_FINANCEIRA_GRUPOS_03 ON TBDRE_FINANCEIRA_GRUPOS (STATUS);


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



SET TERM ^ ;



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



/* Trigger: TBDRE_FINANCEIRA_GRUPOS_BD0 */
CREATE OR ALTER TRIGGER TBDRE_FINANCEIRA_GRUPOS_BD0 FOR TBDRE_FINANCEIRA_GRUPOS
ACTIVE BEFORE DELETE POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF (OLD.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||' GRUPO: '||FN_LPAD(OLD.ID, 4, 0)||' - '||COALESCE(OLD.DESCRICAO,'')||' EXCLUIDO';
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBDRE_FINANCEIRA',old.DRE_FINANCEIRA_ID, HISTORICO);
    END
END
^


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

/* Trigger: TTBDRE_FINANCEIRA_GRUPOS_B01U */
CREATE OR ALTER TRIGGER TTBDRE_FINANCEIRA_GRUPOS_B01U FOR TBDRE_FINANCEIRA_GRUPOS
ACTIVE BEFORE UPDATE POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN
    
    HISTORICO = '';

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

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

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

    IF (HISTORICO <> '') THEN
    BEGIN
        HISTORICO = 'GRUPO: '||FN_LPAD(OLD.ID,4,0)||' - '||COALESCE(OLD.DESCRICAO, '')||' ALTERADO: '||HISTORICO;

        EXECUTE PROCEDURE SPI_HISTORICO('TBDRE_FINANCEIRA',NEW.DRE_FINANCEIRA_ID, HISTORICO);
    END
END
^


/* Trigger: TTBDRE_FINANCEIRA_GRUPOS_B02I */
CREATE OR ALTER TRIGGER TTBDRE_FINANCEIRA_GRUPOS_B02I FOR TBDRE_FINANCEIRA_GRUPOS
ACTIVE BEFORE INSERT POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF ( NEW.id > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||' GRUPO: '||FN_LPAD(NEW.ID, 4, 0)||' - '||COALESCE(NEW.DESCRICAO,'')||' INCLUIDO';
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBDRE_FINANCEIRA',NEW.DRE_FINANCEIRA_ID, HISTORICO);
    END
end
^

SET TERM ; ^



/******************************************************************************/
/****                             Descriptions                             ****/
/******************************************************************************/

COMMENT ON TABLE TBDRE_FINANCEIRA_GRUPOS IS 
'tabela de grupos da DRE financeira';



/******************************************************************************/
/****                              Privileges                              ****/
/******************************************************************************/


/* Privileges of users */
GRANT ALL ON TBDRE_FINANCEIRA_GRUPOS TO GC;
GRANT SELECT ON MON$ATTACHMENTS TO PUBLIC;
GRANT SELECT ON MON$CALL_STACK TO PUBLIC;
GRANT SELECT ON MON$CONTEXT_VARIABLES TO PUBLIC;
GRANT SELECT ON MON$DATABASE TO PUBLIC;
GRANT SELECT ON MON$IO_STATS TO PUBLIC;
GRANT SELECT ON MON$MEMORY_USAGE TO PUBLIC;
GRANT SELECT ON MON$RECORD_STATS TO PUBLIC;
GRANT SELECT ON MON$STATEMENTS TO PUBLIC;
GRANT SELECT ON MON$TABLE_STATS TO PUBLIC;
GRANT SELECT ON MON$TRANSACTIONS TO PUBLIC;

/* Privileges of roles */
GRANT ALL ON TBDRE_FINANCEIRA_GRUPOS TO RDB$ADMIN;