/******************************************************************************/
/****              Generated by IBExpert 25/05/2023 07:40:55               ****/
/******************************************************************************/

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



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


CREATE GENERATOR GTBCONCILIACAO_CARTAO_ACRES;

CREATE TABLE TBCONCILIACAO_CARTAO_ACRESCIMOS (
    ID                        INTEGER NOT NULL,
    VALOR                     NUMERIC(15,4),
    DESCRICAO                 VARCHAR(300),
    DATA_PAGAMENTO            DATE,
    ADQUIRENTE_ID             INTEGER,
    MERCHANT_ID               VARCHAR(50),
    CLASSIFICACAO_FINANCEIRA  VARCHAR(20) DEFAULT ''
);




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

ALTER TABLE TBCONCILIACAO_CARTAO_ACRESCIMOS ADD CONSTRAINT PK_TBCONCILIACAO_CARTAO_ACRES PRIMARY KEY (ID);


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

CREATE INDEX ITBCONCILIACAO_CARTAO_ACRES_01 ON TBCONCILIACAO_CARTAO_ACRESCIMOS (DATA_PAGAMENTO);
CREATE INDEX ITBCONCILIACAO_CARTAO_ACRES_02 ON TBCONCILIACAO_CARTAO_ACRESCIMOS (ADQUIRENTE_ID);
CREATE INDEX ITBCONCILIACAO_CARTAO_ACRES_03 ON TBCONCILIACAO_CARTAO_ACRESCIMOS (MERCHANT_ID);


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



SET TERM ^ ;



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



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

    HISTORICO = '';

    IF(OLD.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||'ACRESCIMO ID:'||OLD.ID||', VALOR:'||OLD.VALOR||',DT.PGTO:'||FN_DATE_TO_STRING(OLD.DATA_PAGAMENTO)||' EXCLUIDO';
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBCONCILIACAO_CARTAO_ACRESCIMOS',OLD.ID, HISTORICO);
    END
END
^


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


/* Trigger: TTBCONCILIACAO_CARTAO_ACRE_BU9 */
CREATE OR ALTER TRIGGER TTBCONCILIACAO_CARTAO_ACRE_BU9 FOR TBCONCILIACAO_CARTAO_ACRESCIMOS
ACTIVE BEFORE UPDATE POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

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

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

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBCONCILIACAO_CARTAO_ACRESCIMOS',NEW.ID, HISTORICO);
    END
END
^

SET TERM ; ^



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


/* Privileges of users */
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;