ALTER TABLE TBPAGAMENTO_FORMA
    ADD HABILITA_PDV INTEGER,
    ADD PDV_TIPO INTEGER;

COMMENT ON COLUMN TBPAGAMENTO_FORMA.HABILITA_PDV IS
'0 - nao
1 - sim';

COMMENT ON COLUMN TBPAGAMENTO_FORMA.PDV_TIPO IS
'1 - dinheiro
2 - tef
3 - pos';



CREATE GENERATOR GTBPDV_POS;

CREATE TABLE TBPDV_POS (
    ID            INTEGER NOT NULL,
    ADQUIRENTE_ID INTEGER,
    DESCRICAO     VARCHAR(50),
    NUMERO_SERIE  VARCHAR(50),
    STATUS        INTEGER
);


ALTER TABLE TBPDV_POS ADD CONSTRAINT PK_TBPDV_POS PRIMARY KEY (ID);


SET TERM ^ ;

/* Trigger: TTBPDV_POS_B01D */
CREATE OR ALTER TRIGGER TTBPDV_POS_B01D FOR TBPDV_POS
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('TBPDV_POS',old.ID, HISTORICO);
    END

    execute procedure SPI_INTEGRACAO('TBPDV_POS',old.ID,1);
end
^


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


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

    HISTORICO = '';

    IF ( OLD.ADQUIRENTE_ID <> NEW.ADQUIRENTE_ID) THEN
    BEGIN
        HISTORICO = HISTORICO||' ID DA ADQ.'|| OLD.ADQUIRENTE_ID||'->'||NEW.ADQUIRENTE_ID;
    END

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

    IF ( OLD.NUMERO_SERIE <> NEW.NUMERO_SERIE) THEN
    BEGIN
        HISTORICO = HISTORICO||' NR. DE SERIE:'|| OLD.NUMERO_SERIE||'->'||NEW.NUMERO_SERIE;
    END

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

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

    execute procedure SPI_INTEGRACAO('TBPDV_POS',new.ID,1);
end
^


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

    HISTORICO = '';

    execute procedure SPI_INTEGRACAO('TBPDV_POS',new.ID,1);

    IF ( NEW.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||'REGISTRO INCLUIDO';
    END

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

SET TERM ; ^


COMMENT ON COLUMN TBPDV_POS.STATUS IS 
'1 - ativo
0 - inativo';



CREATE GENERATOR GTBPDV_ADQUIRENTE;

CREATE TABLE TBPDV_ADQUIRENTE (
    ID                INTEGER NOT NULL,
    EMPRESA_ID        INTEGER,
    MERCHANTID        VARCHAR(50),
    CHAVE_REQUISICAO  VARCHAR(50),
    STATUS            INTEGER
);


ALTER TABLE TBPDV_ADQUIRENTE ADD CONSTRAINT PK_TBPDV_ADQUIRENTE PRIMARY KEY (ID);


SET TERM ^ ;


/* Trigger: TTBPDV_ADQUIRENTE_B01D */
CREATE OR ALTER TRIGGER TTBPDV_ADQUIRENTE_B01D FOR TBPDV_ADQUIRENTE
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('TBPDV_ADQUIRENTE',old.ID, HISTORICO);
    END

    execute procedure SPI_INTEGRACAO('TBPDV_ADQUIRENTE',old.ID,1);
end
^


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


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

    HISTORICO = '';

    IF ( OLD.MERCHANTID <> NEW.MERCHANTID) THEN
    BEGIN
        HISTORICO = HISTORICO||' ID MERCH.'|| OLD.MERCHANTID||'->'||NEW.MERCHANTID;
    END

     IF ( OLD.CHAVE_REQUISICAO <> NEW.CHAVE_REQUISICAO) THEN
    BEGIN
        HISTORICO = HISTORICO||' CHAVE DE REQ.'|| OLD.CHAVE_REQUISICAO||'->'||NEW.CHAVE_REQUISICAO;
    END

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

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

    execute procedure SPI_INTEGRACAO('TBPDV_ADQUIRENTE',new.ID,1);
end
^


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

    HISTORICO = '';

    execute procedure SPI_INTEGRACAO('TBPDV_ADQUIRENTE',new.ID,1);

    IF ( NEW.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||'REGISTRO INCLUIDO';
    END

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

SET TERM ; ^


COMMENT ON COLUMN TBPDV_ADQUIRENTE.STATUS IS 
'1 - ativo
0 - inativo';


CREATE GENERATOR GTBCARTAO_BANDEIRA;

CREATE TABLE TBCARTAO_BANDEIRA (
    ID         INTEGER NOT NULL,
    DESCRICAO  VARCHAR(50),
    STATUS     INTEGER
);

ALTER TABLE TBCARTAO_BANDEIRA ADD CONSTRAINT PK_TBCARTAO_BANDEIRA PRIMARY KEY (ID);


SET TERM ^ ;


/* Trigger: TBCARTAO_BANDEIRA_BU0 */
CREATE OR ALTER TRIGGER TBCARTAO_BANDEIRA_BU0 FOR TBCARTAO_BANDEIRA
ACTIVE BEFORE UPDATE POSITION 0
AS
begin
  execute procedure SPI_INTEGRACAO('TBCARTAO_BANDEIRA',new.ID,1);
end
^


/* Trigger: TTBCARTAO_BANDEIRA_B01D */
CREATE OR ALTER TRIGGER TTBCARTAO_BANDEIRA_B01D FOR TBCARTAO_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('TBCARTAO_BANDEIRA',old.ID, HISTORICO);
    END
end
^


/* Trigger: TTBCARTAO_BANDEIRA_B01I */
CREATE OR ALTER TRIGGER TTBCARTAO_BANDEIRA_B01I FOR TBCARTAO_BANDEIRA
ACTIVE BEFORE INSERT POSITION 0
as
begin
  if (new.id is null) then
    new.id = gen_id(gTBCARTAO_BANDEIRA,1);

  execute procedure SPI_INTEGRACAO('TBCARTAO_BANDEIRA',new.ID,1);
end
^


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


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

    HISTORICO = '';

    IF ( OLD.DESCRICAO <> NEW.DESCRICAO) THEN
    BEGIN
        HISTORICO = HISTORICO||' Descr.: '|| 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('TBCARTAO_BANDEIRA',NEW.ID, HISTORICO);
    END
end
^

SET TERM ; ^

ALTER TABLE TBPAGAMENTO_FORMA
    ADD PDV_SUB_TIPO INTEGER;

COMMENT ON COLUMN TBPAGAMENTO_FORMA.PDV_SUB_TIPO IS
'0 - NAO DEFINIDO
1 - CREDITO
2 - DEBITO';


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

INSERT INTO TBCARTAO_BANDEIRA (DESCRICAO, STATUS)
                           VALUES ('VISA', 1);
INSERT INTO TBCARTAO_BANDEIRA (DESCRICAO, STATUS)
                           VALUES ('MASTER', 1);
INSERT INTO TBCARTAO_BANDEIRA (DESCRICAO, STATUS)
                           VALUES ('ELO', 1);
INSERT INTO TBCARTAO_BANDEIRA (DESCRICAO, STATUS)
                           VALUES ('AMEX', 1);
INSERT INTO TBCARTAO_BANDEIRA (DESCRICAO, STATUS)
                           VALUES ('CABAL', 1);
INSERT INTO TBCARTAO_BANDEIRA (DESCRICAO, STATUS)
                           VALUES ('DINERS', 1);
INSERT INTO TBCARTAO_BANDEIRA (DESCRICAO, STATUS)
                           VALUES ('HIPER', 1);
						
						
INSERT INTO TBPDV_ADQUIRENTE (EMPRESA_ID, MERCHANTID, CHAVE_REQUISICAO, STATUS)
                      VALUES (10625, '10', '26359854-5698-1365-9856-965478231456', 1);
INSERT INTO TBPDV_ADQUIRENTE (EMPRESA_ID, MERCHANTID, CHAVE_REQUISICAO, STATUS)
                      VALUES (10624, '10', '26359854-5698-1365-9856-965478231456', 1);
					  
INSERT INTO TBPDV_POS (ADQUIRENTE_ID, DESCRICAO, NUMERO_SERIE, STATUS)
               VALUES (1, 'MAQUINA CIELO', '00000000000', 1);
INSERT INTO TBPDV_POS (ADQUIRENTE_ID, DESCRICAO, NUMERO_SERIE, STATUS)
               VALUES (1, 'MAQUINA REDE', '00000000000', 1);					  