ALTER TABLE TBPDV_CONFERENCIA
ADD PAGAMENTO_FORMA INTEGER
DEFAULT 0;

CREATE OR ALTER TRIGGER TBPDV_CONFERENCIA_B09U FOR TBPDV_CONFERENCIA
ACTIVE BEFORE UPDATE POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN
    HISTORICO = '';

    IF(OLD.PDV_ID IS DISTINCT FROM NEW.PDV_ID) THEN
    BEGIN
       HISTORICO = HISTORICO||' PDV:'||FN_IS_EMPTY(OLD.PDV_ID)||'->'||FN_IS_EMPTY(NEW.PDV_ID);
    END

    IF(OLD.LOCALIZACAO_ID IS DISTINCT FROM NEW.LOCALIZACAO_ID) THEN
    BEGIN
       HISTORICO = HISTORICO||' LOCALIZACAO:'||FN_IS_EMPTY(OLD.LOCALIZACAO_ID)||'->'||FN_IS_EMPTY(NEW.LOCALIZACAO_ID);
    END

    IF(OLD.CLIENTE_ID IS DISTINCT FROM NEW.CLIENTE_ID) THEN
    BEGIN
       HISTORICO = HISTORICO||' CLIENTE:'||FN_IS_EMPTY(OLD.CLIENTE_ID)||'->'||FN_IS_EMPTY(NEW.CLIENTE_ID);
    END

    IF(OLD.CLIENTE_CPF IS DISTINCT FROM NEW.CLIENTE_CPF) THEN
    BEGIN
       HISTORICO = HISTORICO||' CLIENTE CPF:'||FN_IS_EMPTY(OLD.CLIENTE_CPF)||'->'||FN_IS_EMPTY(NEW.CLIENTE_CPF);
    END

    IF(OLD.TABELA_PRECO_ID IS DISTINCT FROM NEW.TABELA_PRECO_ID) THEN
    BEGIN
       HISTORICO = HISTORICO||' TABELA PRECO:'||FN_IS_EMPTY(OLD.TABELA_PRECO_ID)||'->'||FN_IS_EMPTY(NEW.TABELA_PRECO_ID);
    END

    IF(OLD.OBSERVACAO IS DISTINCT FROM NEW.OBSERVACAO) THEN
    BEGIN
       HISTORICO = HISTORICO||' OBS.'||FN_IS_EMPTY(OLD.OBSERVACAO)||'->'||FN_IS_EMPTY(NEW.OBSERVACAO);
    END

    IF (OLD.CODIGO_PED IS DISTINCT FROM NEW.CODIGO_PED) THEN
    BEGIN
        HISTORICO = HISTORICO||' CODIGO:'||FN_IS_EMPTY(OLD.CODIGO_PED)||'->'||FN_IS_EMPTY(NEW.CODIGO_PED);
    END

    IF (OLD.CANAL_VENDAS_ID IS DISTINCT FROM NEW.CANAL_VENDAS_ID) THEN
    BEGIN
        HISTORICO = HISTORICO||' CANAL VENDAS:'||FN_IS_EMPTY(OLD.CANAL_VENDAS_ID)||'->'||FN_IS_EMPTY(NEW.CANAL_VENDAS_ID);
    END

    IF (OLD.PAGAMENTO_FORMA IS DISTINCT FROM NEW.PAGAMENTO_FORMA) THEN
    BEGIN
        HISTORICO = HISTORICO||' FORMA PAGAMENTO:'||FN_IS_EMPTY(OLD.PAGAMENTO_FORMA)||'->'||FN_IS_EMPTY(NEW.PAGAMENTO_FORMA);
    END

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