SET TERM ^ ;

CREATE OR ALTER trigger ttbpdv_b01u for tbpdv
active before update position 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(2000);
BEGIN

    HISTORICO = '';

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

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

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

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

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

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

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

    IF ( OLD.SAT_GRAVAR_LOG <> NEW.SAT_GRAVAR_LOG) THEN
    BEGIN
        HISTORICO = HISTORICO||' SAT.GRAV.LOG:'||OLD.SAT_GRAVAR_LOG||'->'||NEW.SAT_GRAVAR_LOG;
    END

    IF ( OLD.SAT_VERSAO_XML <> NEW.SAT_VERSAO_XML) THEN
    BEGIN
        HISTORICO = HISTORICO||' SAT.VERS.XML:'||OLD.SAT_VERSAO_XML||'->'||NEW.SAT_VERSAO_XML;
    END

    IF ( OLD.SAT_COD_ATIVACAO <> NEW.SAT_COD_ATIVACAO) THEN
    BEGIN
        HISTORICO = HISTORICO||' SAT.COD.ATIV:'||OLD.SAT_COD_ATIVACAO||'->'||NEW.SAT_COD_ATIVACAO;
    END 

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

    IF ( OLD.SAT_SH_ASSINATURA <> NEW.SAT_SH_ASSINATURA) THEN
    BEGIN
        HISTORICO = HISTORICO||' SAT.SH.ASSIN:'||OLD.SAT_SH_ASSINATURA||'->'||NEW.SAT_SH_ASSINATURA;
    END 

    IF ( OLD.SAT_NUMERO_SERIE <> NEW.SAT_NUMERO_SERIE) THEN
    BEGIN
        HISTORICO = HISTORICO||' SAT.NUM.SERIE:'||OLD.SAT_NUMERO_SERIE||'->'||NEW.SAT_NUMERO_SERIE;
    END 

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

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

    IF ( OLD.CONTA_CONTABIL IS DISTINCT FROM NEW.CONTA_CONTABIL) THEN
    BEGIN
        HISTORICO = HISTORICO||' C.CONTABIL:'||COALESCE(OLD.CONTA_CONTABIL, '')||'->'||COALESCE(NEW.CONTA_CONTABIL, '');
    END

    IF ( OLD.TIPO_CLIENTE_ID IS DISTINCT FROM NEW.TIPO_CLIENTE_ID) THEN
    BEGIN
        HISTORICO = HISTORICO||' TP.CLIENTE:'||COALESCE(OLD.TIPO_CLIENTE_ID, 0)||'->'||COALESCE(NEW.TIPO_CLIENTE_ID, 0);
    END

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

    IF (OLD.ROMANEIO IS DISTINCT FROM NEW.ROMANEIO) THEN
    BEGIN
        HISTORICO = HISTORICO||' ROMANEIO:'||COALESCE(OLD.ROMANEIO, 0)||'->'||COALESCE(NEW.ROMANEIO, 0);
    END

    IF (OLD.CFE IS DISTINCT FROM NEW.CFE) THEN
    BEGIN
        HISTORICO = HISTORICO||' CFE:'||COALESCE(OLD.CFE, 0)||'->'||COALESCE(NEW.CFE, 0);
    END

    IF (OLD.NFE IS DISTINCT FROM NEW.NFE) THEN
    BEGIN
        HISTORICO = HISTORICO||' NFE:'||COALESCE(OLD.NFE, 0)||'->'||COALESCE(NEW.NFE, 0);
    END

    IF (OLD.NFCE IS DISTINCT FROM NEW.NFCE) THEN
    BEGIN
        HISTORICO = HISTORICO||' NFCE:'||COALESCE(OLD.NFCE, 0)||'->'||COALESCE(NEW.NFCE, 0);
    END
	
	IF (OLD.SERIE_NFE IS DISTINCT FROM NEW.SERIE_NFE) THEN
    BEGIN
        HISTORICO = HISTORICO||' SERIE NFE:'||COALESCE(OLD.SERIE_NFE, 0)||'->'||COALESCE(NEW.SERIE_NFE, 0);
    END
	
	IF (OLD.SERIE_NFCE IS DISTINCT FROM NEW.SERIE_NFCE) THEN
    BEGIN
        HISTORICO = HISTORICO||' SERIE NFE:'||COALESCE(OLD.SERIE_NFCE, 0)||'->'||COALESCE(NEW.SERIE_NFCE, 0);
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        HISTORICO = 'REGISTRO ALTERADO: '||HISTORICO;
        EXECUTE PROCEDURE SPI_HISTORICO('TBPDV',NEW.ID, HISTORICO);
    END
END
^

SET TERM ; ^

