/******************************************************************************/
/****              Generated by IBExpert 14/08/2023 14:42:07               ****/
/******************************************************************************/

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



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


CREATE GENERATOR GTBDRE_FINANCEIRA_GRUPOS_CLASS;

CREATE TABLE TBDRE_FINANCEIRA_GRUPOS_CLASS (
    ID                       INTEGER,
    DRE_FINANCEIRA_ID        INTEGER,
    DRE_FINANCEIRA_GRUPO_ID  INTEGER,
    CLASSIFICACAO_FINANCEIRA VARCHAR(20)
);




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

CREATE INDEX ITBDRE_FIN_GRUPOS_CLASS_01 ON TBDRE_FINANCEIRA_GRUPOS_CLASS (ID);
CREATE INDEX ITBDRE_FIN_GRUPOS_CLASS_02 ON TBDRE_FINANCEIRA_GRUPOS_CLASS (DRE_FINANCEIRA_ID);
CREATE INDEX ITBDRE_FIN_GRUPOS_CLASS_03 ON TBDRE_FINANCEIRA_GRUPOS_CLASS (DRE_FINANCEIRA_GRUPO_ID);


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



SET TERM ^ ;



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



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

    HISTORICO = '';

    IF (OLD.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||' CLASSIFICACAO: '||FN_MASK(OLD.CLASSIFICACAO_FINANCEIRA, 'CLASS')||' - '||COALESCE((SELECT FIRST 1 CLASS.DESCRICAO FROM TBCLASSIFICACAO_CONTABIL CLASS WHERE CLASS.CODIGO = OLD.CLASSIFICACAO_FINANCEIRA),'')||' EXCLUIDA';
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        HISTORICO = 'GRUPO '||COALESCE((SELECT FIRST 1 FN_LPAD(GRUPO.ID,4,0)||' - '||GRUPO.DESCRICAO FROM TBDRE_FINANCEIRA_GRUPOS GRUPO WHERE GRUPO.ID = OLD.DRE_FINANCEIRA_GRUPO_ID),'')||' ALTERADA:'||HISTORICO;
       
        EXECUTE PROCEDURE SPI_HISTORICO('TBDRE_FINANCEIRA',OLD.DRE_FINANCEIRA_ID, HISTORICO);
    END
END
^


/* Trigger: TITBDRE_FIN_GRUPOS_CLASS_B01I */
CREATE OR ALTER TRIGGER TITBDRE_FIN_GRUPOS_CLASS_B01I FOR TBDRE_FINANCEIRA_GRUPOS_CLASS
ACTIVE BEFORE INSERT POSITION 0
as
begin
  if (new.id is null) then
    new.id = gen_id(gTBDRE_FINANCEIRA_GRUPOS_CLASS,1);
end
^


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

   HISTORICO = '';

    IF (NEW.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||' CLASSIFICACAO: '||FN_MASK(NEW.CLASSIFICACAO_FINANCEIRA, 'CLASS')||' - '||COALESCE((SELECT FIRST 1 CLASS.DESCRICAO FROM TBCLASSIFICACAO_CONTABIL CLASS WHERE CLASS.CODIGO = NEW.CLASSIFICACAO_FINANCEIRA),'')||' INCLUIDA';
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        HISTORICO = 'GRUPO '||COALESCE((SELECT FIRST 1 FN_LPAD(GRUPO.ID,4,0)||' - '||GRUPO.DESCRICAO FROM TBDRE_FINANCEIRA_GRUPOS GRUPO WHERE GRUPO.ID = NEW.DRE_FINANCEIRA_GRUPO_ID),'')||' ALTERADA:'||HISTORICO;
       
        EXECUTE PROCEDURE SPI_HISTORICO('TBDRE_FINANCEIRA',NEW.DRE_FINANCEIRA_ID, HISTORICO);
    END
END
^

SET TERM ; ^



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

COMMENT ON TABLE TBDRE_FINANCEIRA_GRUPOS_CLASS IS 
'tabela de classificacoes financeiras de grupos da DRE financeira';



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


/* Privileges of users */
GRANT ALL ON TBDRE_FINANCEIRA_GRUPOS_CLASS 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_CLASS TO RDB$ADMIN;