SET TERM ^ ;

CREATE OR ALTER TRIGGER TTBPDV_CONFERENCIA_B02U FOR TBPDV_CONFERENCIA
ACTIVE BEFORE UPDATE POSITION 2
AS
BEGIN   
    IF(OLD.ESTABELECIMENTO_ID IS DISTINCT FROM NEW.ESTABELECIMENTO_ID
    OR OLD.PDV_ID IS DISTINCT FROM NEW.PDV_ID
    OR OLD.CLIENTE_ID IS DISTINCT FROM NEW.CLIENTE_ID
    OR OLD.TABELA_PRECO_ID IS DISTINCT FROM NEW.TABELA_PRECO_ID
    OR OLD.LOCALIZACAO_ID IS DISTINCT FROM NEW.LOCALIZACAO_ID
    OR OLD.REPRESENTANTE_ID IS DISTINCT FROM NEW.REPRESENTANTE_ID
    OR OLD.CORRETOR_ID IS DISTINCT FROM NEW.CORRETOR_ID
    OR OLD.VENDEDOR_ID IS DISTINCT FROM NEW.VENDEDOR_ID
    OR OLD.COLABORADOR_ID IS DISTINCT FROM NEW.COLABORADOR_ID
    ) THEN
    BEGIN
        NEW.VERSAO_ALTERACAO = NEW.VERSAO_ALTERACAO + 1;
    END

    IF(OLD.CLIENTE_ID IS DISTINCT FROM NEW.CLIENTE_ID) THEN
    BEGIN
        UPDATE TBPDV_DEVOLUCAO_TROCA   TROCA SET TROCA.PRE_PEDIDO_ID = 0 WHERE TROCA.PRE_PEDIDO_ID = NEW.ID;

        UPDATE TBPDV_CONFERENCIA_TROCA TROCA SET TROCA.PRE_PEDIDO_ID = 0 WHERE TROCA.PRE_PEDIDO_ID = NEW.ID;
    END
END^

SET TERM ; ^

