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

    HISTORICO = '';

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

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

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

    HISTORICO = '';

    IF ( OLD.ID_POS <> NEW.ID_POS) THEN
    BEGIN
        HISTORICO = HISTORICO||'ID da POS:'|| OLD.ID_POS||'->'||NEW.ID_POS;
    END

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

    IF ( OLD.CNPJ <> NEW.CNPJ) THEN
    BEGIN
        HISTORICO = HISTORICO||',Numero de CNPJ:'|| OLD.CNPJ||'->'||NEW.CNPJ;
    END

    IF ( OLD.MERCHANTID <> NEW.MERCHANTID) THEN
    BEGIN
        HISTORICO = HISTORICO||',Numero de Merchant:'|| OLD.MERCHANTID||'->'||NEW.MERCHANTID;
    END

     IF ( OLD.CHAVE_REQUISICAO <> NEW.CHAVE_REQUISICAO) THEN
    BEGIN
        HISTORICO = HISTORICO||',Chave de Requisicao:'|| 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('TBADQUIRENTE',NEW.ID, HISTORICO);
    END
end;

CREATE TRIGGER TTBADQUIRENTE_B01D FOR TBADQUIRENTE
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('TBADQUIRENTE',old.ID, HISTORICO);
    END
end;
