CREATE TABLE TBMFE_CARTAO_BANDEIRA (
ID INTEGER NOT NULL,
DESCRICAO VARCHAR(10),
STATUS INTEGER);

ALTER TABLE TBMFE_CARTAO_BANDEIRA
ADD CONSTRAINT PK_TBMFE_CARTAO_BANDEIRA
PRIMARY KEY (ID);

COMMENT ON COLUMN TBMFE_CARTAO_BANDEIRA.STATUS IS
'0 - Inativo
1 - Ativo';

CREATE SEQUENCE GTBMFE_CARTAO_BANDEIRA;

create trigger ttbmfe_cartao_bandeira_b01i for tbmfe_cartao_bandeira
active before insert position 0
as
begin
if (new.id is null) then
new.id = gen_id(gtbmfe_cartao_bandeira,1);
end;

ALTER TABLE TBMFE_CARTAO_BANDEIRA
ALTER DESCRICAO TYPE VARCHAR(50) CHARACTER SET UTF8;

CREATE TRIGGER TTBMFE_CARTAO_BANDEIRA_B02I FOR TBMFE_CARTAO_BANDEIRA
ACTIVE BEFORE INSERT POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF ( NEW.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||'Bandeira Cartao incluida';
    END

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

CREATE TRIGGER TTBMFE_CARTAO_BANDEIRA_B01U FOR TBMFE_CARTAO_BANDEIRA
ACTIVE BEFORE UPDATE POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

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

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

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

CREATE TRIGGER TTBMFE_CARTAO_BANDEIRA_B01D FOR TBMFE_CARTAO_BANDEIRA
ACTIVE BEFORE DELETE POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF ( OLD.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||'Bandeira Cartao Excluida';
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBMFE_CARTAO_BANDEIRA',old.ID, HISTORICO);
    END
end;

