CREATE OR ALTER TRIGGER TTBPDV_ADQUIRENTE_BIUD998 FOR TBPDV_ADQUIRENTE
ACTIVE BEFORE INSERT OR UPDATE OR DELETE POSITION 998
AS
BEGIN

    IF(INSERTING) THEN
    BEGIN
        EXECUTE PROCEDURE SPI_INTEGRACAO('TBPDV_ADQUIRENTE', NEW.ID, 2);
    END ELSE
    BEGIN
        EXECUTE PROCEDURE SPI_INTEGRACAO('TBPDV_ADQUIRENTE', OLD.ID, 2);
    END

END;

CREATE OR ALTER TRIGGER TTBPERFIL_CLI_DES_BIUD998 FOR TBPERFIL_CLIENTE_DESCONTO
ACTIVE BEFORE INSERT OR UPDATE OR DELETE POSITION 998
AS
BEGIN

    IF(INSERTING) THEN
    BEGIN
        IF(COALESCE(NEW.PDV_ID, 0) > 0) THEN
        BEGIN
            EXECUTE PROCEDURE SPI_INTEGRACAO('TBPERFIL_CLIENTE_DESCONTO', NEW.ID, 2);
        END
       
    END ELSE
    BEGIN
        IF(COALESCE(OLD.PDV_ID, 0) > 0) THEN
        BEGIN
            EXECUTE PROCEDURE SPI_INTEGRACAO('TBPERFIL_CLIENTE_DESCONTO', OLD.ID, 2);
        END
    END

END;

CREATE OR ALTER TRIGGER TTBCARTAO_BANDEIRA_BIUD998 FOR TBCARTAO_BANDEIRA
ACTIVE BEFORE INSERT OR UPDATE OR DELETE POSITION 998
AS
BEGIN

    IF(INSERTING) THEN
    BEGIN
        EXECUTE PROCEDURE SPI_INTEGRACAO('TBCARTAO_BANDEIRA', NEW.ID, 2);
    END ELSE
    BEGIN
        EXECUTE PROCEDURE SPI_INTEGRACAO('TBCARTAO_BANDEIRA', OLD.ID, 2);
    END

END;


CREATE OR ALTER TRIGGER TTBCFOP_BIUD998 FOR TBCFOP
ACTIVE BEFORE INSERT OR UPDATE OR DELETE POSITION 998
AS
BEGIN

    IF(INSERTING) THEN
    BEGIN
        EXECUTE PROCEDURE SPI_INTEGRACAO('TBCFOP', CAST(FN_REPLACE(NEW.CODIGO, '.', '') AS INTEGER), 2);
    END ELSE
    BEGIN
        EXECUTE PROCEDURE SPI_INTEGRACAO('TBCFOP', CAST(FN_REPLACE(OLD.CODIGO, '.', '') AS INTEGER), 2);
    END

END;

CREATE OR ALTER TRIGGER TTBPAGAMENTO_CONDICAO_BIUD998 FOR TBPAGAMENTO_CONDICAO
ACTIVE BEFORE INSERT OR UPDATE OR DELETE POSITION 998
AS
BEGIN

    IF(INSERTING) THEN
    BEGIN
        EXECUTE PROCEDURE SPI_INTEGRACAO('TBPAGAMENTO_CONDICAO', NEW.CODIGO, 2);
    END ELSE
    BEGIN
        EXECUTE PROCEDURE SPI_INTEGRACAO('TBPAGAMENTO_CONDICAO', OLD.CODIGO, 2);
    END

END;

CREATE OR ALTER TRIGGER TTBESTABELECIMENTO_BIUD998 FOR TBESTABELECIMENTO
ACTIVE BEFORE INSERT OR UPDATE OR DELETE POSITION 998
AS
BEGIN

    IF(INSERTING) THEN
    BEGIN
        EXECUTE PROCEDURE SPI_INTEGRACAO('TBESTABELECIMENTO', NEW.CODIGO, 2);
    END ELSE
    BEGIN
        EXECUTE PROCEDURE SPI_INTEGRACAO('TBESTABELECIMENTO', OLD.CODIGO, 2);
    END

END;


CREATE OR ALTER TRIGGER TTBPAGAMENTO_FORMA_BIUD998 FOR TBPAGAMENTO_FORMA
ACTIVE BEFORE INSERT OR UPDATE OR DELETE POSITION 998
AS
BEGIN
    IF(COALESCE(NEW.HABILITA_PDV, 0) = 1) THEN
    BEGIN
        IF(INSERTING) THEN
        BEGIN
            EXECUTE PROCEDURE SPI_INTEGRACAO('TBPAGAMENTO_FORMA', NEW.CODIGO, 2);
        END ELSE
        BEGIN
            EXECUTE PROCEDURE SPI_INTEGRACAO('TBPAGAMENTO_FORMA', OLD.CODIGO, 2);
        END
    END

END;

CREATE OR ALTER TRIGGER TTBPDV_POS_BIUD998 FOR TBPDV_POS
ACTIVE BEFORE INSERT OR UPDATE OR DELETE POSITION 998
AS
BEGIN

    IF(INSERTING) THEN
    BEGIN
        EXECUTE PROCEDURE SPI_INTEGRACAO('TBPDV_POS', NEW.ID, 2);
    END ELSE
    BEGIN
        EXECUTE PROCEDURE SPI_INTEGRACAO('TBPDV_POS', OLD.ID, 2);
    END

END;

CREATE OR ALTER TRIGGER TTBREPRESENTANTE_BIUD998 FOR TBREPRESENTANTE
ACTIVE BEFORE INSERT OR UPDATE OR DELETE POSITION 998
AS
BEGIN

    IF(INSERTING) THEN
    BEGIN
        EXECUTE PROCEDURE SPI_INTEGRACAO('TBREPRESENTANTE', NEW.CODIGO, 2);
    END ELSE
    BEGIN
        EXECUTE PROCEDURE SPI_INTEGRACAO('TBREPRESENTANTE', OLD.CODIGO, 2);
    END

END;

CREATE OR ALTER TRIGGER TTBTABELA_PRECO_BIUD998 FOR TBTABELA_PRECO
ACTIVE BEFORE INSERT OR UPDATE OR DELETE POSITION 998
AS
BEGIN
    IF(COALESCE(NEW.HABILITA_PDV, 0) = 1) THEN
    BEGIN
        IF(INSERTING) THEN
        BEGIN
            EXECUTE PROCEDURE SPI_INTEGRACAO('TBTABELA_PRECO', NEW.ID, 2);
        END ELSE
        BEGIN
            EXECUTE PROCEDURE SPI_INTEGRACAO('TBTABELA_PRECO', OLD.ID, 2);
        END
    END

END;

CREATE OR ALTER TRIGGER TTBREP_VEND_BIUD998 FOR TBREPRESENTANTE_VENDEDOR
ACTIVE BEFORE INSERT OR UPDATE OR DELETE POSITION 998
AS
BEGIN

    IF(INSERTING) THEN
    BEGIN
        EXECUTE PROCEDURE SPI_INTEGRACAO('TBREPRESENTANTE_VENDEDOR', NEW.CODIGO, 2);
    END ELSE
    BEGIN
        EXECUTE PROCEDURE SPI_INTEGRACAO('TBREPRESENTANTE_VENDEDOR', OLD.CODIGO, 2);
    END

END;

CREATE OR ALTER TRIGGER TTBCIDADE_BIUD998 FOR TBCIDADE
ACTIVE BEFORE INSERT OR UPDATE OR DELETE POSITION 998
AS
BEGIN

    IF(INSERTING) THEN
    BEGIN
        EXECUTE PROCEDURE SPI_INTEGRACAO('TBCIDADE', NEW.ID, 2);
    END ELSE
    BEGIN
        EXECUTE PROCEDURE SPI_INTEGRACAO('TBCIDADE', OLD.ID, 2);
    END

END;

CREATE OR ALTER TRIGGER TTBPDV_CAIXA_BIUD998 FOR TBPDV_CAIXA
ACTIVE BEFORE INSERT OR UPDATE OR DELETE POSITION 998
AS
BEGIN

    IF(INSERTING) THEN
    BEGIN
        EXECUTE PROCEDURE SPI_INTEGRACAO('TBPDV_CAIXA', NEW.ID, 2);
    END ELSE
    BEGIN
        EXECUTE PROCEDURE SPI_INTEGRACAO('TBPDV_CAIXA', OLD.ID, 2);
    END

END;

CREATE OR ALTER TRIGGER TTBPDV_OPERADOR_BIUD998 FOR TBPDV_OPERADOR
ACTIVE BEFORE INSERT OR UPDATE OR DELETE POSITION 998
AS
BEGIN

    IF(INSERTING) THEN
    BEGIN
        EXECUTE PROCEDURE SPI_INTEGRACAO('TBPDV_OPERADOR', NEW.ID, 2);
    END ELSE
    BEGIN
        EXECUTE PROCEDURE SPI_INTEGRACAO('TBPDV_OPERADOR', OLD.ID, 2);
    END

END;

CREATE OR ALTER TRIGGER TTBPDV_BIUD998 FOR TBPDV
ACTIVE BEFORE INSERT OR UPDATE OR DELETE POSITION 998
AS
BEGIN

    IF(INSERTING) THEN
    BEGIN
        EXECUTE PROCEDURE SPI_INTEGRACAO('TBPDV', NEW.ID, 2);
    END ELSE
    BEGIN
        EXECUTE PROCEDURE SPI_INTEGRACAO('TBPDV', OLD.ID, 2);
    END

END;

CREATE OR ALTER TRIGGER TTBUSUARIO_BIUD998 FOR TBUSUARIO
ACTIVE BEFORE INSERT OR UPDATE OR DELETE POSITION 998
AS
BEGIN
    IF(COALESCE(NEW.PDV, 0) = 1) THEN
    BEGIN
        IF(INSERTING) THEN
        BEGIN
            EXECUTE PROCEDURE SPI_INTEGRACAO('TBUSUARIO', NEW.CODIGO, 2);
        END ELSE
        BEGIN
            EXECUTE PROCEDURE SPI_INTEGRACAO('TBUSUARIO', OLD.CODIGO, 2);
        END
    END

END;

CREATE OR ALTER TRIGGER TTBPDV_DEVOLUCAO_TROCA_BIUD998 FOR TBPDV_DEVOLUCAO_TROCA
ACTIVE BEFORE INSERT OR UPDATE OR DELETE POSITION 998
AS
BEGIN

    IF(INSERTING) THEN
    BEGIN
        EXECUTE PROCEDURE SPI_INTEGRACAO('TBPDV_DEVOLUCAO_TROCA', NEW.ID, 2);
    END ELSE
    BEGIN
        EXECUTE PROCEDURE SPI_INTEGRACAO('TBPDV_DEVOLUCAO_TROCA', OLD.ID, 2);
    END

END;

CREATE OR ALTER TRIGGER TTBPDV_MOVIMENTO_BIUD998 FOR TBPDV_MOVIMENTO
ACTIVE BEFORE INSERT OR UPDATE OR DELETE POSITION 998
AS
BEGIN

    IF(INSERTING) THEN
    BEGIN
        EXECUTE PROCEDURE SPI_INTEGRACAO('TBPDV_MOVIMENTO', NEW.ID, 2);
    END ELSE
    BEGIN
        EXECUTE PROCEDURE SPI_INTEGRACAO('TBPDV_MOVIMENTO', OLD.ID, 2);
    END

END;

CREATE OR ALTER TRIGGER TTBPDV_MOVIMENTO_CAIXA_BIUD998 FOR TBPDV_MOVIMENTO_CAIXA
ACTIVE BEFORE INSERT OR UPDATE OR DELETE POSITION 998
AS
BEGIN

    IF(INSERTING) THEN
    BEGIN
        EXECUTE PROCEDURE SPI_INTEGRACAO('TBPDV_MOVIMENTO_CAIXA', NEW.ID, 2);
    END ELSE
    BEGIN
        EXECUTE PROCEDURE SPI_INTEGRACAO('TBPDV_MOVIMENTO_CAIXA', OLD.ID, 2);
    END

END;

CREATE OR ALTER TRIGGER TTBPDV_MOV_CAI_DET_BIUD998 FOR TBPDV_MOVIMENTO_CAIXA_DET
ACTIVE BEFORE INSERT OR UPDATE OR DELETE POSITION 998
AS
BEGIN

    IF(INSERTING) THEN
    BEGIN
        EXECUTE PROCEDURE SPI_INTEGRACAO('TBPDV_MOVIMENTO_CAIXA_DET', NEW.ID, 2);
    END ELSE
    BEGIN
        EXECUTE PROCEDURE SPI_INTEGRACAO('TBPDV_MOVIMENTO_CAIXA_DET', OLD.ID, 2);
    END

END;

CREATE OR ALTER TRIGGER TTBPDV_MOV_DET_BIUD998 FOR TBPDV_MOVIMENTO_DETALHE
ACTIVE BEFORE INSERT OR UPDATE OR DELETE POSITION 998
AS
BEGIN

    IF(INSERTING) THEN
    BEGIN
        EXECUTE PROCEDURE SPI_INTEGRACAO('TBPDV_MOVIMENTO_DETALHE', NEW.ID, 2);
    END ELSE
    BEGIN
        EXECUTE PROCEDURE SPI_INTEGRACAO('TBPDV_MOVIMENTO_DETALHE', OLD.ID, 2);
    END

END;

CREATE OR ALTER TRIGGER TTBPDV_MOV_CAI_ABERT_BIUD998 FOR TBPDV_MOV_CAIXA_ABERTURA
ACTIVE BEFORE INSERT OR UPDATE OR DELETE POSITION 998
AS
BEGIN

    IF(INSERTING) THEN
    BEGIN
        EXECUTE PROCEDURE SPI_INTEGRACAO('TBPDV_MOV_CAIXA_ABERTURA', NEW.ID, 2);
    END ELSE
    BEGIN
        EXECUTE PROCEDURE SPI_INTEGRACAO('TBPDV_MOV_CAIXA_ABERTURA', OLD.ID, 2);
    END

END;
