SET TERM ^ ;

ALTER TABLE TBPERFIL
ADD ENCERRAR_POS_FATURAMENTO SMALLINT
DEFAULT 0 ^

CREATE OR ALTER TRIGGER TBPERFIL_B01U FOR TBPERFIL
ACTIVE BEFORE UPDATE POSITION 1
AS
DECLARE VARIABLE HISTORICO  VARCHAR(8000);
DECLARE VARIABLE HISTORICO2 VARCHAR(8000);
BEGIN

    HISTORICO  = '';
    HISTORICO2 = '';

    IF (OLD.NF_NATUREZA IS DISTINCT FROM NEW.NF_NATUREZA) THEN
    BEGIN
        HISTORICO2 = HISTORICO2||' NAT.NF:'||COALESCE(OLD.NF_NATUREZA,0)||'->'||COALESCE(NEW.NF_NATUREZA,0);
    END

    IF (OLD.NF_SERIE IS DISTINCT FROM NEW.NF_SERIE) THEN
    BEGIN
        HISTORICO2 = HISTORICO2||' SERIE NF:'|| COALESCE(OLD.NF_SERIE,'')||'->'||COALESCE(NEW.NF_SERIE,'');
    END

    IF (OLD.TIPO_FATURAMENTO_PEDIDO IS DISTINCT FROM NEW.TIPO_FATURAMENTO_PEDIDO) THEN
    BEGIN
        HISTORICO2 = HISTORICO2||' TP.FAT.PED:'|| COALESCE(OLD.TIPO_FATURAMENTO_PEDIDO, '')||'->'||COALESCE(NEW.TIPO_FATURAMENTO_PEDIDO, '');
    END

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

    IF ( OLD.CFOP_CODIGO IS DISTINCT FROM NEW.CFOP_CODIGO) THEN
    BEGIN
        HISTORICO2 = HISTORICO2||' CFOP:'|| COALESCE(OLD.CFOP_CODIGO,'')||'->'||COALESCE(NEW.CFOP_CODIGO,'');
    END
    
    IF ( OLD.OPERACAO_CODIGO IS DISTINCT FROM NEW.OPERACAO_CODIGO) THEN
    BEGIN
        HISTORICO2 = HISTORICO2||' OP.FISCAL:'|| COALESCE(OLD.OPERACAO_CODIGO,0)||'->'||COALESCE(NEW.OPERACAO_CODIGO,0);
    END

    IF ( OLD.TIPO_DANFE IS DISTINCT FROM NEW.TIPO_DANFE) THEN
    BEGIN
        HISTORICO2 = HISTORICO2||' TP.DANFE:'|| COALESCE(OLD.TIPO_DANFE,0)||'->'||COALESCE(NEW.TIPO_DANFE,0);
    END

    IF ( OLD.IND_PRESENCA IS DISTINCT FROM NEW.IND_PRESENCA) THEN
    BEGIN
        HISTORICO2 = HISTORICO2||' IND.PRESENC:'|| COALESCE(OLD.IND_PRESENCA,0)||'->'||COALESCE(NEW.IND_PRESENCA,0);
    END  

    IF (COALESCE(NEW.ENCERRAR_POS_FATURAMENTO,0) <> COALESCE(OLD.ENCERRAR_POS_FATURAMENTO,0)) THEN
    BEGIN
        HISTORICO2 = HISTORICO2||' ENC.POS.FAT:'||COALESCE(OLD.ENCERRAR_POS_FATURAMENTO,'0')||'->'||COALESCE(NEW.ENCERRAR_POS_FATURAMENTO,'0');
    END

    IF ( OLD.COMP_CARGA_AUTOMATICA IS DISTINCT FROM NEW.COMP_CARGA_AUTOMATICA) THEN
    BEGIN
        HISTORICO2 = HISTORICO2||' COMP.CARG.AUT.:'|| COALESCE(OLD.COMP_CARGA_AUTOMATICA,0)||'->'||COALESCE(NEW.COMP_CARGA_AUTOMATICA,0);
    END

    IF ( OLD.DESCRICAO <> NEW.DESCRICAO) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' DESC:'|| OLD.DESCRICAO||'->'||NEW.DESCRICAO FROM 1 FOR 500));
    END

    IF ( OLD.CAP_PRODUTIVA <> NEW.CAP_PRODUTIVA) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' CAP PROD:'|| OLD.CAP_PRODUTIVA||'->'||NEW.CAP_PRODUTIVA FROM 1 FOR 500));
    END

    IF ( OLD.TEMPO_FADIGA <> NEW.TEMPO_FADIGA) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' FADIGA:'|| OLD.TEMPO_FADIGA||'%'||'->'||NEW.TEMPO_FADIGA||'%' FROM 1 FOR 500));
    END

    IF ( OLD.LOCALIZACAO_ID <> NEW.LOCALIZACAO_ID) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' LOC:'|| OLD.LOCALIZACAO_ID||'->'||NEW.LOCALIZACAO_ID FROM 1 FOR 500));
    END

    IF ( OLD.TRATAMENTO_PEDIDO_MP <> NEW.TRATAMENTO_PEDIDO_MP) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' TRAT. PED.  '|| OLD.TRATAMENTO_PEDIDO_MP||'->'||NEW.TRATAMENTO_PEDIDO_MP FROM 1 FOR 500));
    END

    IF ( OLD.DESCRICAO_COMPLETA <> NEW.DESCRICAO_COMPLETA) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' DESC.COM:'|| OLD.DESCRICAO_COMPLETA||'->'||NEW.DESCRICAO_COMPLETA FROM 1 FOR 500));
    END

    IF ( OLD.QTD_MIN <> NEW.QTD_MIN) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' QTD.MIN:'|| OLD.QTD_MIN||'->'||NEW.QTD_MIN FROM 1 FOR 500));
    END

    IF ( OLD.QTD_MULT <> NEW.QTD_MULT) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' QTD.MULT:'|| OLD.QTD_MULT||'->'||NEW.QTD_MULT FROM 1 FOR 500));
    END

    IF ( OLD.AGRUPAMENTO <> NEW.AGRUPAMENTO) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' AGRUP. '|| OLD.AGRUPAMENTO||'->'||NEW.AGRUPAMENTO FROM 1 FOR 500));
    END

    IF ( OLD.EFIC_MINIMA <> NEW.EFIC_MINIMA) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' EFIO. MIN. '|| OLD.EFIC_MINIMA||'->'||NEW.EFIC_MINIMA FROM 1 FOR 500));
    END

    IF ( OLD.PONTO_REPOSICAO <> NEW.PONTO_REPOSICAO) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' PON. REP. '|| OLD.PONTO_REPOSICAO||'->'||NEW.PONTO_REPOSICAO FROM 1 FOR 500));
    END

    IF ( OLD.FAMILIA_ID <> NEW.FAMILIA_ID) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' FAMILIA: '|| OLD.FAMILIA_ID||'->'||NEW.FAMILIA_ID FROM 1 FOR 500));
    END

    IF ( OLD.COR_DIFERENCIADA <> NEW.COR_DIFERENCIADA) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' COR. DIF. '|| OLD.COR_DIFERENCIADA||'->'||NEW.COR_DIFERENCIADA FROM 1 FOR 500));
    END

    IF ( OLD.COR_SOBENCOMENDA <> NEW.COR_SOBENCOMENDA) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' COR. SOB ENC. '|| OLD.COR_SOBENCOMENDA||'->'||NEW.COR_SOBENCOMENDA FROM 1 FOR 500));
    END

    IF ( OLD.QTD_MIN_SOBENC <> NEW.QTD_MIN_SOBENC) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' QTD MIN SOB. '|| OLD.QTD_MIN_SOBENC||'->'||NEW.QTD_MIN_SOBENC FROM 1 FOR 500));
    END

    IF ( OLD.QTD_MULT_SOBENC <> NEW.QTD_MULT_SOBENC) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' QTD MULT SOB. '|| OLD.QTD_MULT_SOBENC||'->'||NEW.QTD_MULT_SOBENC FROM 1 FOR 500));
    END

    IF ( OLD.PZO1_PED_D1 <> NEW.PZO1_PED_D1) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' DOM NORMAL PROD. '|| OLD.PZO1_PED_D1||'->'||NEW.PZO1_PED_D1 FROM 1 FOR 500));
    END

    IF ( OLD.PZO1_PED_D2 <> NEW.PZO1_PED_D2) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' SEG NORMAL PROD. '|| OLD.PZO1_PED_D2||'->'||NEW.PZO1_PED_D2 FROM 1 FOR 500));
    END

     IF ( OLD.PZO1_PED_D3 <> NEW.PZO1_PED_D3) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' TER NORMAL PROD. '|| OLD.PZO1_PED_D3||'->'||NEW.PZO1_PED_D3 FROM 1 FOR 500));
    END

     IF ( OLD.PZO1_PED_D4 <> NEW.PZO1_PED_D4) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' QUA NORMAL PROD. '|| OLD.PZO1_PED_D4||'->'||NEW.PZO1_PED_D4 FROM 1 FOR 500));
    END

     IF ( OLD.PZO1_PED_D5 <> NEW.PZO1_PED_D5) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' QUI NORMAL PROD. '|| OLD.PZO1_PED_D5||'->'||NEW.PZO1_PED_D5 FROM 1 FOR 500));
    END

    IF ( OLD.PZO1_PED_D6 <> NEW.PZO1_PED_D6) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' SEX NORMAL PROD. '|| OLD.PZO1_PED_D6||'->'||NEW.PZO1_PED_D6 FROM 1 FOR 500));
    END

    IF ( OLD.PZO1_PED_D7 <> NEW.PZO1_PED_D7) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' SAB NORMAL PROD. '|| OLD.PZO1_PED_D7||'->'||NEW.PZO1_PED_D7 FROM 1 FOR 500));
    END

    IF ( OLD.PZO2_PED_D1 <> NEW.PZO2_PED_D1) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' DOM NORMAL CLI. '|| OLD.PZO2_PED_D1||'->'||NEW.PZO2_PED_D1 FROM 1 FOR 500));
    END

    IF ( OLD.PZO2_PED_D2 <> NEW.PZO2_PED_D2) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' SEG NORMAL CLI. '|| OLD.PZO2_PED_D2||'->'||NEW.PZO2_PED_D2 FROM 1 FOR 500));
    END

    IF ( OLD.PZO2_PED_D3 <> NEW.PZO2_PED_D3) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' TER NORMAL CLI. '|| OLD.PZO2_PED_D3||'->'||NEW.PZO2_PED_D3 FROM 1 FOR 500));
    END

    IF ( OLD.PZO2_PED_D4 <> NEW.PZO2_PED_D4) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' QUA NORMAL CLI. '|| OLD.PZO2_PED_D4||'->'||NEW.PZO2_PED_D4 FROM 1 FOR 500));
    END

    IF ( OLD.PZO2_PED_D5 <> NEW.PZO2_PED_D5) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' QUI NORMAL CLI. '|| OLD.PZO2_PED_D5||'->'||NEW.PZO2_PED_D5 FROM 1 FOR 500));
    END

    IF ( OLD.PZO2_PED_D6 <> NEW.PZO2_PED_D6) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' SEX NORMAL CLI. '|| OLD.PZO2_PED_D6||'->'||NEW.PZO2_PED_D6 FROM 1 FOR 500));
    END

    IF ( OLD.PZO2_PED_D7 <> NEW.PZO2_PED_D7) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' SAB NORMAL CLI. '|| OLD.PZO2_PED_D7||'->'||NEW.PZO2_PED_D7 FROM 1 FOR 500));
    END

    IF ( OLD.PZOD1_PED_D1 <> NEW.PZOD1_PED_D1) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' DOM COR DIF PROD. '|| OLD.PZOD1_PED_D1||'->'||NEW.PZOD1_PED_D1 FROM 1 FOR 500));
    END

    IF ( OLD.PZOD1_PED_D2 <> NEW.PZOD1_PED_D2) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' SEG COR DIF PROD. '|| OLD.PZOD1_PED_D2||'->'||NEW.PZOD1_PED_D2 FROM 1 FOR 500));
    END

    IF ( OLD.PZOD1_PED_D3 <> NEW.PZOD1_PED_D3) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' TER COR DIF PROD. '|| OLD.PZOD1_PED_D3||'->'||NEW.PZOD1_PED_D3 FROM 1 FOR 500));
    END

    IF ( OLD.PZOD1_PED_D4 <> NEW.PZOD1_PED_D4) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' QUA COR DIF PROD. '|| OLD.PZOD1_PED_D4||'->'||NEW.PZOD1_PED_D4 FROM 1 FOR 500));
    END

    IF ( OLD.PZOD1_PED_D5 <> NEW.PZOD1_PED_D5) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' QUI COR DIF PROD. '|| OLD.PZOD1_PED_D5||'->'||NEW.PZOD1_PED_D5 FROM 1 FOR 500));
    END

    IF ( OLD.PZOD1_PED_D6 <> NEW.PZOD1_PED_D6) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' SEX COR DIF PROD. '|| OLD.PZOD1_PED_D6||'->'||NEW.PZOD1_PED_D6 FROM 1 FOR 500));
    END

    IF ( OLD.PZOD1_PED_D7 <> NEW.PZOD1_PED_D7) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' SAB COR DIF PROD. '|| OLD.PZOD1_PED_D7||'->'||NEW.PZOD1_PED_D7 FROM 1 FOR 500));
    END

    IF ( OLD.PZOD2_PED_D1 <> NEW.PZOD2_PED_D1) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' DOM COR DIF CLI. '|| OLD.PZOD2_PED_D1||'->'||NEW.PZOD2_PED_D1 FROM 1 FOR 500));
    END

    IF ( OLD.PZOD2_PED_D2 <> NEW.PZOD2_PED_D2) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' SEG COR DIF CLI. '|| OLD.PZOD2_PED_D2||'->'||NEW.PZOD2_PED_D2 FROM 1 FOR 500));
    END

    IF ( OLD.PZOD2_PED_D3 <> NEW.PZOD2_PED_D3) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' TER COR DIF CLI. '|| OLD.PZOD2_PED_D3||'->'||NEW.PZOD2_PED_D3 FROM 1 FOR 500));
    END

    IF ( OLD.PZOD2_PED_D4 <> NEW.PZOD2_PED_D4) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' QUA COR DIF CLI. '|| OLD.PZOD2_PED_D4||'->'||NEW.PZOD2_PED_D4 FROM 1 FOR 500));
    END

    IF ( OLD.PZOD2_PED_D5 <> NEW.PZOD2_PED_D5) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' QUI COR DIF CLI. '|| OLD.PZOD2_PED_D5||'->'||NEW.PZOD2_PED_D5 FROM 1 FOR 500));
    END

    IF ( OLD.PZOD2_PED_D6 <> NEW.PZOD2_PED_D6) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' SEX COR DIF CLI. '|| OLD.PZOD2_PED_D6||'->'||NEW.PZOD2_PED_D6 FROM 1 FOR 500));
    END

    IF ( OLD.PZOD2_PED_D7 <> NEW.PZOD2_PED_D7) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' SAB COR DIF CLI. '|| OLD.PZOD2_PED_D7||'->'||NEW.PZOD2_PED_D7 FROM 1 FOR 500));
    END

    IF ( OLD.PZOS1_PED_D1 <> NEW.PZOS1_PED_D1) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' DOM SOB-ENC PROD. '|| OLD.PZOS1_PED_D1||'->'||NEW.PZOS1_PED_D1 FROM 1 FOR 500));
    END

    IF ( OLD.PZOS1_PED_D2 <> NEW.PZOS1_PED_D2) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' SEG SOB-ENC PROD. '|| OLD.PZOS1_PED_D2||'->'||NEW.PZOS1_PED_D2 FROM 1 FOR 500));
    END

    IF ( OLD.PZOS1_PED_D3 <> NEW.PZOS1_PED_D3) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' TER SOB-ENC PROD. '|| OLD.PZOS1_PED_D3||'->'||NEW.PZOS1_PED_D3 FROM 1 FOR 500));
    END

    IF ( OLD.PZOS1_PED_D4 <> NEW.PZOS1_PED_D4) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' QUA SOB-ENC PROD. '|| OLD.PZOS1_PED_D4||'->'||NEW.PZOS1_PED_D4 FROM 1 FOR 500));
    END

    IF ( OLD.PZOS1_PED_D5 <> NEW.PZOS1_PED_D5) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' QUI SOB-ENC PROD. '|| OLD.PZOS1_PED_D5||'->'||NEW.PZOS1_PED_D5 FROM 1 FOR 500));
    END

    IF ( OLD.PZOS1_PED_D6 <> NEW.PZOS1_PED_D6) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' SEX SOB-ENC PROD. '|| OLD.PZOS1_PED_D6||'->'||NEW.PZOS1_PED_D6 FROM 1 FOR 500));
    END

    IF ( OLD.PZOS1_PED_D7 <> NEW.PZOS1_PED_D7) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' SAB SOB-ENC PROD. '|| OLD.PZOS1_PED_D7||'->'||NEW.PZOS1_PED_D7 FROM 1 FOR 500));
    END

    IF ( OLD.PZOS2_PED_D1 <> NEW.PZOS2_PED_D1) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' DOM SOB-ENC CLI. '|| OLD.PZOS2_PED_D1||'->'||NEW.PZOS2_PED_D1 FROM 1 FOR 500));
    END

    IF ( OLD.PZOS2_PED_D2 <> NEW.PZOS2_PED_D2) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' SEG SOB-ENC CLI. '|| OLD.PZOS2_PED_D2||'->'||NEW.PZOS2_PED_D2 FROM 1 FOR 500));
    END

    IF ( OLD.PZOS2_PED_D3 <> NEW.PZOS2_PED_D3) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' TER SOB-ENC CLI. '|| OLD.PZOS2_PED_D3||'->'||NEW.PZOS2_PED_D3 FROM 1 FOR 500));
    END

    IF ( OLD.PZOS2_PED_D4 <> NEW.PZOS2_PED_D3) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' QUA SOB-ENC CLI. '|| OLD.PZOS2_PED_D3||'->'||NEW.PZOS2_PED_D3 FROM 1 FOR 500));
    END

    IF ( OLD.PZOS2_PED_D5 <> NEW.PZOS2_PED_D5) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' QUI SOB-ENC CLI. '|| OLD.PZOS2_PED_D5||'->'||NEW.PZOS2_PED_D5 FROM 1 FOR 500));
    END

    IF ( OLD.PZOS2_PED_D6 <> NEW.PZOS2_PED_D6) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' SEX SOB-ENC CLI. '|| OLD.PZOS2_PED_D6||'->'||NEW.PZOS2_PED_D6 FROM 1 FOR 500));
    END

    IF ( OLD.PZOS2_PED_D7 <> NEW.PZOS2_PED_D7) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' SAB SOB-ENC CLI. '|| OLD.PZOS2_PED_D7||'->'||NEW.PZOS2_PED_D7 FROM 1 FOR 500));
    END

    IF ( OLD.TRATAMENTO_PEDIDO <> NEW.TRATAMENTO_PEDIDO) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' TRAT PED. '|| OLD.TRATAMENTO_PEDIDO||'->'||NEW.TRATAMENTO_PEDIDO FROM 1 FOR 500));
    END

    IF ( OLD.AGRUPAMENTO2 <> NEW.AGRUPAMENTO2) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' AGRUP2. '|| OLD.AGRUPAMENTO2||'->'||NEW.AGRUPAMENTO2 FROM 1 FOR 500));
    END

    IF ( OLD.PZOE1_PED_D1 <> NEW.PZOE1_PED_D1) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' DOM EST MIN PROD. '|| OLD.PZOE1_PED_D1||'->'||NEW.PZOE1_PED_D1 FROM 1 FOR 500));
    END

    IF ( OLD.PZOE1_PED_D2 <> NEW.PZOE1_PED_D2) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' SEG EST MIN PROD. '|| OLD.PZOE1_PED_D2||'->'||NEW.PZOE1_PED_D2 FROM 1 FOR 500));
    END

    IF ( OLD.PZOE1_PED_D3 <> NEW.PZOE1_PED_D3) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' TER EST MIN PROD. '|| OLD.PZOE1_PED_D3||'->'||NEW.PZOE1_PED_D3 FROM 1 FOR 500));
    END

    IF ( OLD.PZOE1_PED_D4 <> NEW.PZOE1_PED_D4) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' QUA EST MIN PROD. '|| OLD.PZOE1_PED_D4||'->'||NEW.PZOE1_PED_D4 FROM 1 FOR 500));
    END

    IF ( OLD.PZOE1_PED_D5 <> NEW.PZOE1_PED_D5) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' QUI EST MIN PROD. '|| OLD.PZOE1_PED_D5||'->'||NEW.PZOE1_PED_D5 FROM 1 FOR 500));
    END

    IF ( OLD.PZOE1_PED_D6 <> NEW.PZOE1_PED_D6) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' SEX EST MIN PROD. '|| OLD.PZOE1_PED_D6||'->'||NEW.PZOE1_PED_D6 FROM 1 FOR 500));
    END

    IF ( OLD.PZOE1_PED_D7 <> NEW.PZOE1_PED_D7) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' SAB EST MIN PROD. '|| OLD.PZOE1_PED_D7||'->'||NEW.PZOE1_PED_D7 FROM 1 FOR 500));
    END

    IF ( OLD.PZOE2_PED_D1 <> NEW.PZOE2_PED_D1) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' DOM EST MIN CLI. '|| OLD.PZOE2_PED_D1||'->'||NEW.PZOE2_PED_D1 FROM 1 FOR 500));
    END

    IF ( OLD.PZOE2_PED_D2 <> NEW.PZOE2_PED_D2) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' SEG EST MIN CLI. '|| OLD.PZOE2_PED_D2||'->'||NEW.PZOE2_PED_D2 FROM 1 FOR 500));
    END

    IF ( OLD.PZOE2_PED_D3 <> NEW.PZOE2_PED_D3) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' TER EST MIN CLI. '|| OLD.PZOE2_PED_D3||'->'||NEW.PZOE2_PED_D3 FROM 1 FOR 500));
    END

    IF ( OLD.PZOE2_PED_D4 <> NEW.PZOE2_PED_D4) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' QUA EST MIN CLI. '|| OLD.PZOE2_PED_D4||'->'||NEW.PZOE2_PED_D4 FROM 1 FOR 500));
    END

    IF ( OLD.PZOE2_PED_D5 <> NEW.PZOE2_PED_D5) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' QUI EST MIN CLI. '|| OLD.PZOE2_PED_D5||'->'||NEW.PZOE2_PED_D5 FROM 1 FOR 500));
    END

    IF ( OLD.PZOE2_PED_D6 <> NEW.PZOE2_PED_D6) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' SEX EST MIN CLI. '|| OLD.PZOE2_PED_D6||'->'||NEW.PZOE2_PED_D6 FROM 1 FOR 500));
    END

    IF ( OLD.PZOE2_PED_D7 <> NEW.PZOE2_PED_D7) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' SAB EST MIN CLI. '|| OLD.PZOE2_PED_D7||'->'||NEW.PZOE2_PED_D7 FROM 1 FOR 500));
    END

    IF ( OLD.EFIC_A <> NEW.EFIC_A) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' EFIC A. '|| OLD.EFIC_A||'->'||NEW.EFIC_A FROM 1 FOR 500));
    END

    IF ( OLD.EFIC_B <> NEW.EFIC_B) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' EFIC B. '|| OLD.EFIC_B||'->'||NEW.EFIC_B FROM 1 FOR 500));
    END

    IF ( OLD.PERDA_A <> NEW.PERDA_A) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' PERDA A. '|| OLD.PERDA_A||'->'||NEW.PERDA_A FROM 1 FOR 500));
    END

    IF ( OLD.PERDA_B <> NEW.PERDA_B) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' PERDA A. '|| OLD.PERDA_B||'->'||NEW.PERDA_B FROM 1 FOR 500));
    END

    IF ( OLD.ENDERECAMENTO_SOBRA <> NEW.ENDERECAMENTO_SOBRA) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' END SOB. '|| OLD.ENDERECAMENTO_SOBRA||'->'||NEW.ENDERECAMENTO_SOBRA FROM 1 FOR 500));
    END

    IF ( OLD.TIPO_REPOSICAO <> NEW.TIPO_REPOSICAO) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' TIP REP. '|| OLD.TIPO_REPOSICAO||'->'||NEW.TIPO_REPOSICAO FROM 1 FOR 500));
    END

    IF ( OLD.COR_BASICA <> NEW.COR_BASICA) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' COR BASICA: '|| OLD.COR_BASICA||'->'||NEW.COR_BASICA FROM 1 FOR 500));
    END

    IF ( OLD.PZOI1_PED_D1 <> NEW.PZOI1_PED_D1) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' DOM EST INDIS PROD. '|| OLD.PZOI1_PED_D1||'->'||NEW.PZOI1_PED_D1 FROM 1 FOR 500));
    END

    IF ( OLD.PZOI1_PED_D2 <> NEW.PZOI1_PED_D2) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' SEG EST INDIS PROD. '|| OLD.PZOI1_PED_D2||'->'||NEW.PZOI1_PED_D2 FROM 1 FOR 500));
    END

    IF ( OLD.PZOI1_PED_D3 <> NEW.PZOI1_PED_D3) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' TER EST INDIS PROD. '|| OLD.PZOI1_PED_D3||'->'||NEW.PZOI1_PED_D3 FROM 1 FOR 500));
    END

    IF ( OLD.PZOI1_PED_D4 <> NEW.PZOI1_PED_D4) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' QUA EST INDIS PROD. '|| OLD.PZOI1_PED_D4||'->'||NEW.PZOI1_PED_D4 FROM 1 FOR 500));
    END

    IF ( OLD.PZOI1_PED_D5 <> NEW.PZOI1_PED_D5) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' QUI EST INDIS PROD. '|| OLD.PZOI1_PED_D5||'->'||NEW.PZOI1_PED_D5 FROM 1 FOR 500));
    END

    IF ( OLD.PZOI1_PED_D6 <> NEW.PZOI1_PED_D6) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' SEX EST INDIS PROD. '|| OLD.PZOI1_PED_D6||'->'||NEW.PZOI1_PED_D6 FROM 1 FOR 500));
    END

    IF ( OLD.PZOI1_PED_D7 <> NEW.PZOI1_PED_D7) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' SAB EST INDIS PROD. '|| OLD.PZOI1_PED_D7||'->'||NEW.PZOI1_PED_D7 FROM 1 FOR 500));
    END

    IF ( OLD.PZOI2_PED_D1 <> NEW.PZOI2_PED_D1) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' DOM EST INDIS CLI. '|| OLD.PZOI2_PED_D1||'->'||NEW.PZOI2_PED_D1 FROM 1 FOR 500));
    END

    IF ( OLD.PZOI2_PED_D2 <> NEW.PZOI2_PED_D2) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' SEG EST INDIS CLI. '|| OLD.PZOI2_PED_D2||'->'||NEW.PZOI2_PED_D2 FROM 1 FOR 500));
    END

    IF ( OLD.PZOI2_PED_D3 <> NEW.PZOI2_PED_D3) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' TER EST INDIS CLI. '|| OLD.PZOI2_PED_D3||'->'||NEW.PZOI2_PED_D3 FROM 1 FOR 500));
    END

    IF ( OLD.PZOI2_PED_D4 <> NEW.PZOI2_PED_D4) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' QUA EST INDIS CLI. '|| OLD.PZOI2_PED_D4||'->'||NEW.PZOI2_PED_D4 FROM 1 FOR 500));
    END

    IF ( OLD.PZOI2_PED_D5 <> NEW.PZOI2_PED_D5) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' QUI EST INDIS CLI. '|| OLD.PZOI2_PED_D5||'->'||NEW.PZOI2_PED_D5 FROM 1 FOR 500));
    END

    IF ( OLD.PZOI2_PED_D6 <> NEW.PZOI2_PED_D6) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' SEX EST INDIS CLI. '|| OLD.PZOI2_PED_D6||'->'||NEW.PZOI2_PED_D6 FROM 1 FOR 500));
    END

    IF ( OLD.PZOI2_PED_D6 <> NEW.PZOI2_PED_D6) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' SEX EST INDIS CLI. '|| OLD.PZOI2_PED_D6||'->'||NEW.PZOI2_PED_D6 FROM 1 FOR 500));
    END

    IF ( OLD.PZOI2_PED_D7 <> NEW.PZOI2_PED_D7) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' SAB EST INDIS CLI. '|| OLD.PZOI2_PED_D7||'->'||NEW.PZOI2_PED_D7 FROM 1 FOR 500));
    END

    IF ( OLD.ALOCAR_MULTIPLO <> NEW.ALOCAR_MULTIPLO) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' ALOC MULT. '|| OLD.ALOCAR_MULTIPLO||'->'||NEW.ALOCAR_MULTIPLO FROM 1 FOR 500));
    END

    IF ( OLD.TELA_ADD_PEDIDO <> NEW.TELA_ADD_PEDIDO) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' TELA ADD PEDIDO. '|| OLD.TELA_ADD_PEDIDO||'->'||NEW.TELA_ADD_PEDIDO FROM 1 FOR 500));
    END

    IF ( OLD.AMOSTRA <> NEW.AMOSTRA) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' AMOSTRA: '|| OLD.AMOSTRA||'->'||NEW.AMOSTRA FROM 1 FOR 500));
    END

    IF ( OLD.CONSIGNACAO_FITNESS <> NEW.CONSIGNACAO_FITNESS) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' CONS.FIT: '|| OLD.CONSIGNACAO_FITNESS||'->'||NEW.CONSIGNACAO_FITNESS FROM 1 FOR 500));
    END 

    IF ( OLD.TRANSFERENCIA <> NEW.TRANSFERENCIA) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' TRANSF: '|| OLD.TRANSFERENCIA||'->'||NEW.TRANSFERENCIA FROM 1 FOR 500));
    END 

    IF ( OLD.ALOCAR_PEDIDO_PENDENTE <> NEW.ALOCAR_PEDIDO_PENDENTE) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' ALOC.PEND: '|| OLD.ALOCAR_PEDIDO_PENDENTE||'->'||NEW.ALOCAR_PEDIDO_PENDENTE FROM 1 FOR 500));
    END 

    IF ( OLD.FAMILIA_PEDIDO <> NEW.FAMILIA_PEDIDO) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' FAM PED. '|| OLD.FAMILIA_PEDIDO||'->'||NEW.FAMILIA_PEDIDO FROM 1 FOR 500));
    END

    IF ( OLD.EXIGE_VINCULO_CLIENTE <> NEW.EXIGE_VINCULO_CLIENTE) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' VINC.CLIENTE: '|| OLD.EXIGE_VINCULO_CLIENTE||'->'||NEW.EXIGE_VINCULO_CLIENTE FROM 1 FOR 500));
    END

    IF ( OLD.PERMITE_INCLUIR_CLIENTE <> NEW.PERMITE_INCLUIR_CLIENTE) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' INC.CLIENTE: '|| OLD.PERMITE_INCLUIR_CLIENTE||'->'||NEW.PERMITE_INCLUIR_CLIENTE FROM 1 FOR 500));
    END                   

    IF ( OLD.ANALISE_LIMITE_CREDITO <> NEW.ANALISE_LIMITE_CREDITO) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' VERIF.CRED: '|| OLD.ANALISE_LIMITE_CREDITO||'->'||NEW.ANALISE_LIMITE_CREDITO FROM 1 FOR 500));
    END                   

    IF ( OLD.CLIENTE_REP_VINCULO  <> NEW.CLIENTE_REP_VINCULO ) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' VINC.REP: '|| OLD.CLIENTE_REP_VINCULO ||'->'||NEW.CLIENTE_REP_VINCULO  FROM 1 FOR 500));
    END

    IF ( OLD.HABILITA_CONSUMIDOR_FINAL <> NEW.HABILITA_CONSUMIDOR_FINAL) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' HAB.CONS.FINAL:'|| OLD.HABILITA_CONSUMIDOR_FINAL||'->'||NEW.HABILITA_CONSUMIDOR_FINAL FROM 1 FOR 500));
    END

    IF ( OLD.HABILITA_EAN <> NEW.HABILITA_EAN) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' HAB.EAN:'|| OLD.HABILITA_EAN||'->'||NEW.HABILITA_EAN FROM 1 FOR 500));
    END

    IF ( OLD.HABILITA_PRE_PEDIDO <> NEW.HABILITA_PRE_PEDIDO) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' HAB.PRE PED:'|| OLD.HABILITA_PRE_PEDIDO||'->'||NEW.HABILITA_PRE_PEDIDO FROM 1 FOR 500));
    END

    IF ( OLD.ORDENACAO_ITENS_NF <> NEW.ORDENACAO_ITENS_NF) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' ORDENAC.:'|| OLD.ORDENACAO_ITENS_NF||'->'||NEW.ORDENACAO_ITENS_NF FROM 1 FOR 500));
    END

    IF (OLD.TABELA_PRECO_ID <> NEW.TABELA_PRECO_ID) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' TABELA DE PRECO:'|| OLD.TABELA_PRECO_ID||'->'||NEW.TABELA_PRECO_ID FROM 1 FOR 500));
    END

    IF (OLD.PDV <> NEW.PDV) THEN
    BEGIN
        HISTORICO = TRIM(SUBSTRING(HISTORICO||' HAB.PERFIL PDV:'|| OLD.PDV||'->'||NEW.PDV FROM 1 FOR 500));
    END

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

    IF (OLD.HABILITA_SPED_NF IS DISTINCT FROM NEW.HABILITA_SPED_NF) THEN
    BEGIN
        HISTORICO = HISTORICO||' HAB.SPED NF:'|| COALESCE(OLD.HABILITA_SPED_NF, 0)||'->'||COALESCE(NEW.HABILITA_SPED_NF, 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.EXIGE_CANAL_VENDAS IS DISTINCT FROM NEW.EXIGE_CANAL_VENDAS) THEN
    BEGIN
        HISTORICO = HISTORICO||' EXIGE CANAL VENDAS:'||COALESCE(OLD.EXIGE_CANAL_VENDAS, 0)||'->'||COALESCE(NEW.EXIGE_CANAL_VENDAS, 0);
    END 

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

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

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

    IF (OLD.HABILITA_NF_REFERENCIADA IS DISTINCT FROM NEW.HABILITA_NF_REFERENCIADA) THEN
    BEGIN
        HISTORICO = HISTORICO||' HAB. NF REFER: '||COALESCE(OLD.HABILITA_NF_REFERENCIADA, '')||'->'||COALESCE(NEW.HABILITA_NF_REFERENCIADA, '');
    END

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

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

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

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

    IF (OLD.EXIGE_OBSERVACAO IS DISTINCT FROM NEW.EXIGE_OBSERVACAO) THEN
    BEGIN
        HISTORICO = HISTORICO||' EXIGE.OBS:'|| COALESCE(OLD.EXIGE_OBSERVACAO,'0')||'->'||COALESCE(NEW.EXIGE_OBSERVACAO,'0');
    END
	
	IF (OLD.EXIGE_CONDICAO_FORMA_PAGAMENTO IS DISTINCT FROM NEW.EXIGE_CONDICAO_FORMA_PAGAMENTO) THEN
    BEGIN
        HISTORICO = HISTORICO||' EXIGE.PAG:'|| COALESCE(OLD.EXIGE_CONDICAO_FORMA_PAGAMENTO,'0')||'->'||COALESCE(NEW.EXIGE_CONDICAO_FORMA_PAGAMENTO,'0');
    END
	
	IF (OLD.INCLUIR_EAN_AUTOMATICO IS DISTINCT FROM NEW.INCLUIR_EAN_AUTOMATICO) THEN
    BEGIN
        HISTORICO = HISTORICO||' INC.EAN:'|| COALESCE(OLD.INCLUIR_EAN_AUTOMATICO,'0')||'->'||COALESCE(NEW.INCLUIR_EAN_AUTOMATICO,'0');
    END
	
	IF (OLD.REINCLUIR_MODELO IS DISTINCT FROM NEW.REINCLUIR_MODELO) THEN
    BEGIN
        HISTORICO = HISTORICO||' REINC.MOD:'|| COALESCE(OLD.REINCLUIR_MODELO,'0')||'->'||COALESCE(NEW.REINCLUIR_MODELO,'0');
    END
	
	IF (OLD.DISPONIBILIDADE_FERRAMENTA IS DISTINCT FROM NEW.DISPONIBILIDADE_FERRAMENTA) THEN
    BEGIN
        HISTORICO = HISTORICO||' DISP.FER:'|| COALESCE(OLD.DISPONIBILIDADE_FERRAMENTA,'0')||'->'||COALESCE(NEW.DISPONIBILIDADE_FERRAMENTA,'0');
    END
	
	IF (OLD.EXIGE_PERFIL_SKU IS DISTINCT FROM NEW.EXIGE_PERFIL_SKU) THEN
    BEGIN
        HISTORICO = HISTORICO||' EXIGE.PERF.SKU:'|| COALESCE(OLD.EXIGE_PERFIL_SKU,'0')||'->'||COALESCE(NEW.EXIGE_PERFIL_SKU,'0');
    END
	
	IF (OLD.VISUALIZAR_QTD_MINIMA IS DISTINCT FROM NEW.VISUALIZAR_QTD_MINIMA) THEN
    BEGIN
        HISTORICO = HISTORICO||' VIS.QTD.MIN:'|| COALESCE(OLD.VISUALIZAR_QTD_MINIMA,'0')||'->'||COALESCE(NEW.VISUALIZAR_QTD_MINIMA,'0');
    END
	
	IF (OLD.VISUALIZAR_QTD_MULTIPLA IS DISTINCT FROM NEW.VISUALIZAR_QTD_MULTIPLA) THEN
    BEGIN
        HISTORICO = HISTORICO||' VIS.QTD.MUL:'|| COALESCE(OLD.VISUALIZAR_QTD_MULTIPLA,'0')||'->'||COALESCE(NEW.VISUALIZAR_QTD_MULTIPLA,'0');
    END
	
	IF (OLD.VISUALIZAR_PRONTA_ENTREGA IS DISTINCT FROM NEW.VISUALIZAR_PRONTA_ENTREGA) THEN
    BEGIN
        HISTORICO = HISTORICO||' VIS.P.E:'|| COALESCE(OLD.VISUALIZAR_PRONTA_ENTREGA,'0')||'->'||COALESCE(NEW.VISUALIZAR_PRONTA_ENTREGA,'0');
    END
	
	IF (OLD.IGNORA_CHECAGEM_PRECO IS DISTINCT FROM NEW.IGNORA_CHECAGEM_PRECO) THEN
    BEGIN
        HISTORICO = HISTORICO||' IGNORA.PRECO:'|| COALESCE(OLD.IGNORA_CHECAGEM_PRECO,'0')||'->'||COALESCE(NEW.IGNORA_CHECAGEM_PRECO,'0');
    END
	
	IF (OLD.IGNORA_CHECAGEM_DATA IS DISTINCT FROM NEW.IGNORA_CHECAGEM_DATA) THEN
    BEGIN
        HISTORICO = HISTORICO||' IGNORA.DT.PROD:'|| COALESCE(OLD.IGNORA_CHECAGEM_DATA,'0')||'->'||COALESCE(NEW.IGNORA_CHECAGEM_DATA,'0');
    END

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

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

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

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

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

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

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

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

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

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

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

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

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

    IF (OLD.TRANSF_PED_D7 IS DISTINCT FROM NEW.TRANSF_PED_D7) THEN
    BEGIN
        HISTORICO = HISTORICO||' TRANSF.PED.SAB:'|| COALESCE(OLD.TRANSF_PED_D7,'0')||'->'||COALESCE(NEW.TRANSF_PED_D7,'0');
    END
    
    IF (:HISTORICO <> '') THEN
    BEGIN
        HISTORICO = 'REGISTRO ALTERADO:'||HISTORICO;
        EXECUTE PROCEDURE SPI_HISTORICO('TBPERFIL', OLD.CONTROLE, FN_ELIPSES(:HISTORICO,500));
    END

    IF (:HISTORICO2 <> '') THEN
    BEGIN
        IF(CHAR_LENGTH(:HISTORICO) = 0)THEN
        BEGIN
           HISTORICO2 = 'REGISTRO ALTERADO:'||HISTORICO2;
        END
        EXECUTE PROCEDURE SPI_HISTORICO('TBPERFIL', OLD.CONTROLE, FN_ELIPSES(:HISTORICO2,500));
    END
END^

SET TERM ; ^