CREATE TRIGGER TTBMODELO_B05I FOR TBMODELO
ACTIVE BEFORE INSERT POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN
    HISTORICO = '';
    IF(NEW.CODIGO > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||' Modelo incluído';
    END
    
    IF(HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBMODELO',NEW.CODIGO,HISTORICO);
    END
END;

CREATE TRIGGER TTBMODELO_B06D FOR TBMODELO
ACTIVE BEFORE DELETE POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN
    HISTORICO = '';
    IF(OLD.CODIGO > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||' Modelo excluído';
    END
    
    IF(HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBMODELO',OLD.CODIGO,HISTORICO);
    END
END;

CREATE TRIGGER TTBMODELO_COMPOSICAO_B01I FOR TBMODELO_COMPOSICAO
ACTIVE BEFORE INSERT POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF (NEW.COMPOSICAO_ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||'Composicao ID:'||FN_LPAD(NEW.COMPOSICAO_ID,4,'0')||' incluida. ';
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBMODELO', NEW.MODELO_ID, HISTORICO);
    END
END;

CREATE OR ALTER TRIGGER TTBMODELO_COMPOSICAO_B01U FOR TBMODELO_COMPOSICAO
ACTIVE BEFORE UPDATE POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(8000);
BEGIN
  
     HISTORICO = '';
    
    IF (OLD.PERCENTUAL <> NEW.PERCENTUAL) THEN
    BEGIN
        HISTORICO = HISTORICO||' Perc.(%): '||OLD.PERCENTUAL||' -> '||NEW.PERCENTUAL;
    END
    IF (OLD.OBSERVACAO <> NEW.OBSERVACAO) THEN
    BEGIN
        HISTORICO = HISTORICO||', Observ.: '||OLD.OBSERVACAO||' -> '||NEW.OBSERVACAO;
    END

    IF (HISTORICO <> '') THEN
    BEGIN
       HISTORICO = 'Composicao ID: '||FN_LPAD(NEW.COMPOSICAO_ID,4,'0')||', Alterada: '||HISTORICO;
       EXECUTE PROCEDURE SPI_HISTORICO('TBMODELO', NEW.MODELO_ID, HISTORICO);
    END
END;

CREATE TRIGGER TTBMODELO_COMPOSICAO_B01D FOR TBMODELO_COMPOSICAO
ACTIVE BEFORE DELETE POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF (OLD.COMPOSICAO_ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||'Composicao ID:'||FN_LPAD(OLD.COMPOSICAO_ID,4,'0')||' excluida. ';
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBMODELO', OLD.MODELO_ID, HISTORICO);
    END
END;

CREATE TRIGGER TTBEAN_B02I FOR TBEAN
ACTIVE BEFORE INSERT POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF (NEW.MODELO_ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||'Cód. EAN incluído na Cor/Tamanho:'||
        (SELECT FIRST 1 A.DESCRICAO FROM TBCOR A WHERE A.CODIGO = NEW.COR_ID)
        ||' / '||NEW.TAMANHO||': '||NEW.PREFIXO||NEW.CODIGO;
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBMODELO', NEW.MODELO_ID, HISTORICO);
    END
END;

CREATE OR ALTER TRIGGER TTBEAN_B02U FOR TBEAN
ACTIVE BEFORE UPDATE POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF (NEW.MODELO_ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||'Cód. EAN alterado na Cor/Tamanho:'||
        (SELECT FIRST 1 A.DESCRICAO FROM TBCOR A WHERE A.CODIGO = NEW.COR_ID)
        ||' / '||NEW.TAMANHO||': '||OLD.PREFIXO||OLD.CODIGO||' -> '||NEW.PREFIXO||NEW.CODIGO;
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBMODELO', NEW.MODELO_ID, HISTORICO);
    END
END;

CREATE OR ALTER TRIGGER TTBEAN_B01D FOR TBEAN
ACTIVE BEFORE DELETE POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF (OLD.MODELO_ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||'Cód. EAN excluído na Cor/Tamanho:'||
        (SELECT FIRST 1 A.DESCRICAO FROM TBCOR A WHERE A.CODIGO = OLD.COR_ID)
        ||' / '||OLD.TAMANHO||': '||OLD.PREFIXO||OLD.CODIGO;
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBMODELO', OLD.MODELO_ID, HISTORICO);
    END
END;

/*CREATE TRIGGER TTBMODELO_CONSUMO_B06D FOR TBMODELO_CONSUMO
ACTIVE BEFORE DELETE POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF (OLD.MODELO_CODIGO > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||'Produto de Consumo da Ficha Téc.2 excluído: '||OLD.CONSUMO_PRODUTO_CODIGO;
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBMODELO', OLD.MODELO_CODIGO, HISTORICO);
    END
END;*/

CREATE OR ALTER TRIGGER TTBMODELO_FICHA_TECNICA_B02U FOR TBMODELO_FICHA_TECNICA
ACTIVE BEFORE INSERT OR UPDATE POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(8000);
BEGIN
  
     HISTORICO = '';
    IF(INSERTING) THEN
    BEGIN
        IF (NEW.OBSERVACAO IS NOT NULL AND NEW.OBSERVACAO <> '') THEN
        BEGIN
            HISTORICO = HISTORICO||' Obs.: '||''||' -> '||NEW.OBSERVACAO;
        END
    
        if (0 <> NEW.QUANTIDADE or 0 <> NEW.T01 or 0 <> NEW.T02 or 0 <> NEW.T03
            or 0 <> NEW.T04 or 0 <> NEW.T05 or 0 <> NEW.T06 or 0 <> NEW.T07
            or 0 <> NEW.T08 or 0 <> NEW.T09 or 0 <> NEW.T10 or 0 <> NEW.T11
            or 0 <> NEW.T12 or 0 <> NEW.T13 or 0 <> NEW.T14 or 0 <> NEW.T15
            or 0 <> NEW.T16 or 0 <> NEW.T17 or 0 <> NEW.T18 or 0 <> NEW.T19
            or 0 <> NEW.T20) THEN
        BEGIN
            HISTORICO = HISTORICO||'[Valores]';
        END
        IF (HISTORICO <> '') THEN
        BEGIN
            HISTORICO = 'Registro da Ficha Téc(1) alterado '||HISTORICO;
        --HISTORICO = 'Registro da Ficha Téc(1) alterado '||FN_LPAD(NEW.TIPO_CODIGO,4,'0')||', Alterada: '||HISTORICO;
           EXECUTE PROCEDURE SPI_HISTORICO('TBMODELO', NEW.MODELO_CODIGO, HISTORICO);
        END
    END
    ELSE
    BEGIN
        IF (OLD.OBSERVACAO <> NEW.OBSERVACAO) THEN
        BEGIN
            HISTORICO = HISTORICO||' Obs.: '||OLD.OBSERVACAO||' -> '||NEW.OBSERVACAO;
        END
    
        if (OLD.QUANTIDADE <> NEW.QUANTIDADE or OLD.T01 <> NEW.T01 or OLD.T02 <> NEW.T02 or OLD.T03 <> NEW.T03
            or OLD.T04 <> NEW.T04 or OLD.T05 <> NEW.T05 or OLD.T06 <> NEW.T06 or OLD.T07 <> NEW.T07
            or OLD.T08 <> NEW.T08 or OLD.T09 <> NEW.T09 or OLD.T10 <> NEW.T10 or OLD.T11 <> NEW.T11
            or OLD.T12 <> NEW.T12 or OLD.T13 <> NEW.T13 or OLD.T14 <> NEW.T14 or OLD.T15 <> NEW.T15
            or OLD.T16 <> NEW.T16 or OLD.T17 <> NEW.T17 or OLD.T18 <> NEW.T18 or OLD.T19 <> NEW.T19
            or OLD.T20 <> NEW.T20) THEN
        BEGIN
            HISTORICO = HISTORICO||'[Valores]';
        END
        IF (HISTORICO <> '') THEN
        BEGIN
            HISTORICO = 'Registro da Ficha Téc(1) alterado '||HISTORICO;
        --HISTORICO = 'Registro da Ficha Téc(1) alterado '||FN_LPAD(NEW.TIPO_CODIGO,4,'0')||', Alterada: '||HISTORICO;
           EXECUTE PROCEDURE SPI_HISTORICO('TBMODELO', NEW.MODELO_CODIGO, HISTORICO);
        END
    END
END;	

CREATE TRIGGER TTBMODELO_SETOR_B02I FOR TBMODELO_SETOR
ACTIVE BEFORE INSERT POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF (NEW.MODELO_CODIGO > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||'Roteiro de produção incluído '||FN_LPAD(NEW.ORDEM,3,'0')||'-'||(SELECT FIRST 1 A.DESCRICAO FROM TBSETOR A WHERE A.CODIGO = NEW.SETOR_CODIGO);
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBMODELO', NEW.MODELO_CODIGO, HISTORICO);
    END
END;

CREATE TRIGGER TTBMODELO_FLUXO_B02I FOR TBMODELO_FLUXO
ACTIVE BEFORE INSERT POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF (NEW.MODELO_ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||'Fluxo de produção incluído: '||FN_LPAD(NEW.FLUXO_ID,5,'0');
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBMODELO', NEW.MODELO_ID, HISTORICO);
    END
END;

CREATE TRIGGER TTBMODELO_FLUXO_B02U FOR TBMODELO_FLUXO
ACTIVE BEFORE UPDATE POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF (NEW.ORDEM <> OLD.ORDEM) THEN
    BEGIN
        HISTORICO = HISTORICO||'Ordem do fluxo de produção alterada! ';
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBMODELO', NEW.MODELO_ID, HISTORICO);
    END
END;

CREATE TRIGGER TTBMODELO_FLUXO_B02D FOR TBMODELO_FLUXO
ACTIVE BEFORE DELETE POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF (OLD.MODELO_ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||'Fluxo de produção excluído: '||FN_LPAD(OLD.FLUXO_ID,5,'0')||' - '||(SELECT FIRST 1 A.DESCRICAO FROM TBFLUXO_PRODUCAO A WHERE A.ID = OLD.FLUXO_ID);
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBMODELO', OLD.MODELO_ID, HISTORICO);
    END
END;

CREATE OR ALTER TRIGGER TTBMODELO_FLUXO_PRODUCAO_B05IU FOR TBMODELO_FLUXO_PRODUCAO
ACTIVE BEFORE INSERT OR UPDATE POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(3000);
BEGIN

    HISTORICO = '';
    IF(INSERTING) THEN
    BEGIN
        IF((SELECT FIRST 1 C.MODELO_ID FROM TBMODELO_FLUXO_PRODUCAO C WHERE C.MODELO_ID = NEW.MODELO_ID
            AND C.FLUXO_ID = NEW.FLUXO_ID AND C.COR_ID = NEW.COR_ID) IS NOT NULL ) THEN
           BEGIN
             HISTORICO = HISTORICO||'Tempos do Fluxo alterados: '||IIF(NEW.COR_ID > 0,(select first 1 a.descricao from TBCOR a where a.CODIGO = new.COR_ID),'*** PADRÃO ***')
             ||' - '||new.FLUXO_ID||' '||(SELECT FIRST 1 b.descricao from TBFLUXO_PRODUCAO b where b.ID = new.FLUXO_ID);
           END
           ELSE
           BEGIN
             HISTORICO = HISTORICO||'Fluxo de produção vinculado: '||IIF(NEW.COR_ID > 0,(select first 1 a.descricao from TBCOR a where a.CODIGO = new.COR_ID),'*** PADRÃO ***')
             ||' - '||NEW.FLUXO_ID||' '||(select first 1 b.descricao FROM TBFLUXO_PRODUCAO B WHERE B.ID = NEW.FLUXO_ID);
           END
    END
    ELSE
    BEGIN
       IF (NEW.TEMPO <> OLD.TEMPO) THEN
        BEGIN
            HISTORICO = HISTORICO||'Tempos do Fluxo alterados: '||IIF(NEW.COR_ID > 0,(select first 1 a.descricao from TBCOR a where a.CODIGO = new.COR_ID),'*** PADRÃO ***')
            ||' - '||new.FLUXO_ID||' '||(select first 1 b.descricao from TBFLUXO_PRODUCAO b where b.ID = new.FLUXO_ID);
        END
       ELSE
       BEGIN
            HISTORICO = HISTORICO||'Tempos do Fluxo alterados: '||IIF(NEW.COR_ID > 0,(select first 1 a.descricao from TBCOR a where a.CODIGO = new.COR_ID),'*** PADRÃO ***')
            ||' - '||new.FLUXO_ID||' '||(select first 1 b.descricao from TBFLUXO_PRODUCAO b where b.ID = new.FLUXO_ID);
       END
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBMODELO', NEW.MODELO_ID, HISTORICO);
    END
END;

CREATE TRIGGER TTBMODELO_FLUXO_PRODUCAO_B03D FOR TBMODELO_FLUXO_PRODUCAO
ACTIVE BEFORE DELETE POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF (OLD.MODELO_ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||'Fluxo excluído do modelo/cor: '||IIF(OLD.COR_ID > 0,(select first 1 a.descricao from TBCOR A where A.CODIGO = OLD.COR_ID),'*** PADRÃO ***')||' - '||OLD.FLUXO_ID||' - '||(SELECT FIRST 1 B.DESCRICAO FROM TBFLUXO_PRODUCAO B WHERE B.ID = OLD.FLUXO_ID);
    END
    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBMODELO', OLD.MODELO_ID, HISTORICO);
    END
END;

CREATE TRIGGER TTBMODELO_PADRAO_B01I FOR TBMODELO_PADRAO
ACTIVE BEFORE INSERT OR UPDATE POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(3000);
BEGIN

    HISTORICO = '';
    IF(INSERTING) THEN
    BEGIN
        HISTORICO = HISTORICO||'Cor e tamanho padrão definido com sucesso! '||NEW.COR_ID||'/'||NEW.TAMANHO;
    END
    ELSE
    BEGIN
        IF(NEW.COR_ID <> OLD.COR_ID OR NEW.TAMANHO <> OLD.TAMANHO) THEN
        BEGIN
            HISTORICO = HISTORICO||'Cor e tamanho padrão definido com sucesso! '||NEW.COR_ID||'/'||NEW.TAMANHO;
        END
    END
    
    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBMODELO', NEW.MODELO_ID, HISTORICO);
    END
END;

CREATE OR ALTER TRIGGER TTMODELO_B9U FOR TBMODELO
ACTIVE BEFORE UPDATE POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(5000);
BEGIN

   HISTORICO = '';

   IF (OLD.TIPOPRODUTO_CODIGO <> NEW.TIPOPRODUTO_CODIGO) THEN
       BEGIN
       HISTORICO = HISTORICO||' TIPO:'||OLD.TIPOPRODUTO_CODIGO||'->'||NEW.TIPOPRODUTO_CODIGO;
       END
   IF (OLD.FAMILIA_CODIGO <> NEW.FAMILIA_CODIGO) THEN
       BEGIN
       HISTORICO = HISTORICO||' FAM:'||OLD.FAMILIA_CODIGO||'->'||NEW.FAMILIA_CODIGO;
       END
   IF (OLD.LINHA_CODIGO <> NEW.LINHA_CODIGO) THEN
       BEGIN
       HISTORICO = HISTORICO||' LIN:'||OLD.LINHA_CODIGO||'->'||NEW.LINHA_CODIGO;
       END
   IF (OLD.UNIDADEMEDIDA_SIGLA <> NEW.UNIDADEMEDIDA_SIGLA) THEN
       BEGIN
       HISTORICO = HISTORICO||' UM:'||OLD.UNIDADEMEDIDA_SIGLA||'->'||NEW.UNIDADEMEDIDA_SIGLA;
       END
   IF (OLD.DESCRICAO <> NEW.DESCRICAO) THEN
       BEGIN
       HISTORICO = HISTORICO||' DESC:'||OLD.DESCRICAO||'->'||NEW.DESCRICAO;
       END
   IF (OLD.DESCRICAO_NF <> NEW.DESCRICAO_NF) THEN
       BEGIN
       HISTORICO = HISTORICO||' DESC.CPL:'||OLD.DESCRICAO_NF||'->'||NEW.DESCRICAO_NF;
       END       
   IF (OLD.DESCRICAO_COMERCIAL <> NEW.DESCRICAO_COMERCIAL) THEN
       BEGIN
       HISTORICO = HISTORICO||' DESC.COM:'||OLD.DESCRICAO_COMERCIAL||'->'||NEW.DESCRICAO_COMERCIAL;
       END
   IF (OLD.MATRIZ_CODIGO <> NEW.MATRIZ_CODIGO) THEN
       BEGIN
       HISTORICO = HISTORICO||' MTZ:'||OLD.MATRIZ_CODIGO||'->'||NEW.MATRIZ_CODIGO;
       END
   IF (OLD.GRADE_CODIGO <> NEW.GRADE_CODIGO) THEN
       BEGIN
       HISTORICO = HISTORICO||' GRD:'||OLD.GRADE_CODIGO||'->'||NEW.GRADE_CODIGO;
       END
   IF (OLD.CLASSIFICACAO <> NEW.CLASSIFICACAO) THEN
       BEGIN
       HISTORICO = HISTORICO||' CLASS:'||OLD.CLASSIFICACAO||'->'||NEW.CLASSIFICACAO;
       END
   IF (OLD.COMPRA_PRODUZ <> NEW.COMPRA_PRODUZ) THEN
       BEGIN
       HISTORICO = HISTORICO||' FORM.ENTR.EST:'||OLD.COMPRA_PRODUZ||'->'||NEW.COMPRA_PRODUZ;
       END
   IF (OLD.DENSIDADE <> NEW.DENSIDADE) THEN
       BEGIN
       HISTORICO = HISTORICO||' DENS:'||OLD.DENSIDADE||'->'||NEW.DENSIDADE;
       END
   IF (OLD.ESPESSURA<> NEW.ESPESSURA) THEN
       BEGIN
       HISTORICO = HISTORICO||' ESP:'||OLD.ESPESSURA||'->'||NEW.ESPESSURA;
       END
   IF (OLD.TIPO_TAG <> NEW.TIPO_TAG) THEN
       BEGIN
       HISTORICO = HISTORICO||' TP.TAG:'||OLD.TIPO_TAG||'->'||NEW.TIPO_TAG;
       END
   IF (OLD.ACRESCIMO <> NEW.ACRESCIMO) THEN
       BEGIN
       HISTORICO = HISTORICO||' ACRESC:'||OLD.ACRESCIMO||'->'||NEW.ACRESCIMO;
       END
   IF (OLD.PRIORIDADE <> NEW.PRIORIDADE) THEN
       BEGIN
       HISTORICO = HISTORICO||' PRIOR.PROD:'||OLD.PRIORIDADE||'->'||NEW.PRIORIDADE;
       END
   IF (OLD.HORAS_BLOQUEIO <> NEW.HORAS_BLOQUEIO) THEN
       BEGIN
       HISTORICO = HISTORICO||' HR.BLQ:'||OLD.HORAS_BLOQUEIO||'->'||NEW.HORAS_BLOQUEIO;
       END
   IF (OLD.GERAR_REMESSA <> NEW.GERAR_REMESSA) THEN
       BEGIN
       HISTORICO = HISTORICO||' HAB.GER.REM:'||OLD.GERAR_REMESSA||'->'||NEW.GERAR_REMESSA;
       END 
   IF (OLD.HABILITA_ECOMMERCE <> NEW.HABILITA_ECOMMERCE) THEN
       BEGIN
       HISTORICO = HISTORICO||' HAB.ECOM:'||OLD.HABILITA_ECOMMERCE||'->'||NEW.HABILITA_ECOMMERCE;
       END
   IF (OLD.CATEGORIA_ID <> NEW.CATEGORIA_ID) THEN
       BEGIN
       HISTORICO = HISTORICO||' CAT:'||OLD.CATEGORIA_ID||'->'||NEW.CATEGORIA_ID;
       END
   IF (OLD.MODELO_PAI <> NEW.MODELO_PAI) THEN
       BEGIN
       HISTORICO = HISTORICO||' MODELO PAI:'||OLD.MODELO_PAI||'->'||NEW.MODELO_PAI;
       END

   IF (OLD.T01 <> NEW.T01 OR OLD.T01 IS NULL AND NEW.T01 IS NOT NULL) THEN BEGIN HISTORICO = HISTORICO||' T01:'||OLD.T01||'->'||NEW.T01; END
   IF (OLD.T02 <> NEW.T02 OR OLD.T02 IS NULL AND NEW.T02 IS NOT NULL) THEN BEGIN HISTORICO = HISTORICO||' T02:'||OLD.T02||'->'||NEW.T02; END
   IF (OLD.T03 <> NEW.T03 OR OLD.T03 IS NULL AND NEW.T03 IS NOT NULL) THEN BEGIN HISTORICO = HISTORICO||' T03:'||OLD.T03||'->'||NEW.T03; END
   IF (OLD.T04 <> NEW.T04 OR OLD.T04 IS NULL AND NEW.T04 IS NOT NULL) THEN BEGIN HISTORICO = HISTORICO||' T04:'||OLD.T04||'->'||NEW.T04; END
   IF (OLD.T05 <> NEW.T05 OR OLD.T05 IS NULL AND NEW.T05 IS NOT NULL) THEN BEGIN HISTORICO = HISTORICO||' T05:'||OLD.T05||'->'||NEW.T05; END
   IF (OLD.T06 <> NEW.T06 OR OLD.T06 IS NULL AND NEW.T06 IS NOT NULL) THEN BEGIN HISTORICO = HISTORICO||' T06:'||OLD.T06||'->'||NEW.T06; END
   IF (OLD.T07 <> NEW.T07 OR OLD.T07 IS NULL AND NEW.T07 IS NOT NULL) THEN BEGIN HISTORICO = HISTORICO||' T07:'||OLD.T07||'->'||NEW.T07; END
   IF (OLD.T08 <> NEW.T08 OR OLD.T08 IS NULL AND NEW.T08 IS NOT NULL) THEN BEGIN HISTORICO = HISTORICO||' T08:'||OLD.T08||'->'||NEW.T08; END
   IF (OLD.T09 <> NEW.T09 OR OLD.T09 IS NULL AND NEW.T09 IS NOT NULL) THEN BEGIN HISTORICO = HISTORICO||' T09:'||OLD.T09||'->'||NEW.T09; END
   IF (OLD.T10 <> NEW.T10 OR OLD.T10 IS NULL AND NEW.T10 IS NOT NULL) THEN BEGIN HISTORICO = HISTORICO||' T10:'||OLD.T10||'->'||NEW.T10; END
   IF (OLD.T11 <> NEW.T11 OR OLD.T11 IS NULL AND NEW.T11 IS NOT NULL) THEN BEGIN HISTORICO = HISTORICO||' T11:'||OLD.T11||'->'||NEW.T11; END
   IF (OLD.T12 <> NEW.T12 OR OLD.T12 IS NULL AND NEW.T12 IS NOT NULL) THEN BEGIN HISTORICO = HISTORICO||' T12:'||OLD.T12||'->'||NEW.T12; END
   IF (OLD.T13 <> NEW.T13 OR OLD.T13 IS NULL AND NEW.T13 IS NOT NULL) THEN BEGIN HISTORICO = HISTORICO||' T13:'||OLD.T13||'->'||NEW.T13; END
   IF (OLD.T14 <> NEW.T14 OR OLD.T14 IS NULL AND NEW.T14 IS NOT NULL) THEN BEGIN HISTORICO = HISTORICO||' T14:'||OLD.T14||'->'||NEW.T14; END
   IF (OLD.T15 <> NEW.T15 OR OLD.T15 IS NULL AND NEW.T15 IS NOT NULL) THEN BEGIN HISTORICO = HISTORICO||' T15:'||OLD.T15||'->'||NEW.T15; END
   IF (OLD.T16 <> NEW.T16 OR OLD.T16 IS NULL AND NEW.T16 IS NOT NULL) THEN BEGIN HISTORICO = HISTORICO||' T16:'||OLD.T16||'->'||NEW.T16; END
   IF (OLD.T17 <> NEW.T17 OR OLD.T17 IS NULL AND NEW.T17 IS NOT NULL) THEN BEGIN HISTORICO = HISTORICO||' T17:'||OLD.T17||'->'||NEW.T17; END
   IF (OLD.T18 <> NEW.T18 OR OLD.T18 IS NULL AND NEW.T18 IS NOT NULL) THEN BEGIN HISTORICO = HISTORICO||' T18:'||OLD.T18||'->'||NEW.T18; END
   IF (OLD.T19 <> NEW.T19 OR OLD.T19 IS NULL AND NEW.T19 IS NOT NULL) THEN BEGIN HISTORICO = HISTORICO||' T19:'||OLD.T19||'->'||NEW.T19; END
   IF (OLD.T20 <> NEW.T20 OR OLD.T20 IS NULL AND NEW.T20 IS NOT NULL) THEN BEGIN HISTORICO = HISTORICO||' T20:'||OLD.T20||'->'||NEW.T20; END

   IF (OLD.STATUS <> NEW.STATUS) THEN
       BEGIN
       HISTORICO = HISTORICO||' STATUS:'||OLD.STATUS||'->'||NEW.STATUS;
       IF (NEW.STATUS = '0') THEN
           BEGIN
           NEW.DATA_INATIVACAO = CURRENT_DATE;
           END
       IF (NEW.STATUS = '1') THEN
           BEGIN
           NEW.DATA_ATIVACAO = CURRENT_DATE;
           END
       END

   IF (HISTORICO <> '') THEN
       BEGIN
       INSERT INTO TBHISTORICO (
       TABELA, TABELA_ID, USUARIO, IP, HISTORICO) VALUES (
      'TBMODELO', OLD.CODIGO,
      (SELECT FIRST 1 MON$USER FROM MON$ATTACHMENTS WHERE MON$ATTACHMENT_ID = CURRENT_CONNECTION),
      (SELECT FIRST 1 MON$REMOTE_ADDRESS FROM MON$ATTACHMENTS WHERE MON$ATTACHMENT_ID = CURRENT_CONNECTION),
       FN_ELIPSES(:HISTORICO,500));
       END
END;


CREATE INDEX ITBCATEGORIA_LACLOFIT_02
ON TBCATEGORIA_LACLOFIT (ID_ECOMMERCE);

CREATE INDEX ITBMODELO_14
ON TBMODELO (CODIGO,MODELO_PAI);

CREATE OR ALTER TRIGGER TTBMODELO_FICHA_TECNICA_B02U FOR TBMODELO_FICHA_TECNICA
ACTIVE BEFORE INSERT OR UPDATE POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(8000);
BEGIN
  
    HISTORICO = '';
    IF(INSERTING) THEN
    BEGIN
        IF(NEW.OBSERVACAO IS NOT NULL AND NEW.OBSERVACAO <> '') THEN
        BEGIN
            HISTORICO = HISTORICO||' OBS: '||''||' -> '||NEW.OBSERVACAO;
        END

        IF (0 <> NEW.QUANTIDADE) THEN
        BEGIN
            HISTORICO = HISTORICO||' QTD:'||0||'->'||NEW.QUANTIDADE;
        END
        
        if(0 <> NEW.T01) THEN
        BEGIN
            HISTORICO = HISTORICO||' T01:'||0||'->'||NEW.T01;
        END
        if(0 <> NEW.T02) THEN
        BEGIN
            HISTORICO = HISTORICO||' T02:'||0||'->'||NEW.T02;
        END
        if(0 <> NEW.T03) THEN
        BEGIN
            HISTORICO = HISTORICO||' T03:'||0||'->'||NEW.T03;
        END
        if(0 <> NEW.T04) THEN
        BEGIN
            HISTORICO = HISTORICO||' T04:'||0||'->'||NEW.T04;
        END
        if(0 <> NEW.T05) THEN
        BEGIN
            HISTORICO = HISTORICO||' T05:'||0||'->'||NEW.T05;
        END
        if(0 <> NEW.T06) THEN
        BEGIN
            HISTORICO = HISTORICO||' T06:'||0||'->'||NEW.T06;
        END
        if(0 <> NEW.T07) THEN
        BEGIN
            HISTORICO = HISTORICO||' T07:'||0||'->'||NEW.T07;
        END
        if(0 <> NEW.T08) THEN
        BEGIN
            HISTORICO = HISTORICO||' T08:'||0||'->'||NEW.T08;
        END
        if(0 <> NEW.T09) THEN
        BEGIN
            HISTORICO = HISTORICO||' T09:'||0||'->'||NEW.T09;
        END
        if(0 <> NEW.T10) THEN
        BEGIN
            HISTORICO = HISTORICO||' T10:'||0||'->'||NEW.T10;
        END
        if(0 <> NEW.T11) THEN
        BEGIN
            HISTORICO = HISTORICO||' T11:'||0||'->'||NEW.T11;
        END
        if(0 <> NEW.T12) THEN
        BEGIN
            HISTORICO = HISTORICO||' T12:'||0||'->'||NEW.T12;
        END
        if(0 <> NEW.T13) THEN
        BEGIN
            HISTORICO = HISTORICO||' T13:'||0||'->'||NEW.T13;
        END
        if(0 <> NEW.T14) THEN
        BEGIN
            HISTORICO = HISTORICO||' T14:'||0||'->'||NEW.T14;
        END
        if(0 <> NEW.T15) THEN
        BEGIN
            HISTORICO = HISTORICO||' T15:'||0||'->'||NEW.T15;
        END
        if(0 <> NEW.T16) THEN
        BEGIN
            HISTORICO = HISTORICO||' T16:'||0||'->'||NEW.T16;
        END
        if(0 <> NEW.T17) THEN
        BEGIN
            HISTORICO = HISTORICO||' T17:'||0||'->'||NEW.T17;
        END
        if(0 <> NEW.T18) THEN
        BEGIN
            HISTORICO = HISTORICO||' T18:'||0||'->'||NEW.T18;
        END
        if(0 <> NEW.T19) THEN
        BEGIN
            HISTORICO = HISTORICO||' T19:'||0||'->'||NEW.T19;
        END
        if(0 <> NEW.T20) THEN
        BEGIN
            HISTORICO = HISTORICO||' T20:'||0||'->'||NEW.T20;
        END

        IF (HISTORICO <> '') THEN
        BEGIN
           HISTORICO = 'CONS.PARAMETRIZÁVEL ID:'||NEW.TIPO_CODIGO||' ALTERADO'||HISTORICO;
        --HISTORICO = 'Registro da Ficha Téc(1) alterado '||FN_LPAD(NEW.TIPO_CODIGO,4,'0')||', Alterada: '||HISTORICO;
           EXECUTE PROCEDURE SPI_HISTORICO('TBMODELO', NEW.MODELO_CODIGO, HISTORICO);
        END
    END
    ELSE
    BEGIN
        IF (OLD.OBSERVACAO <> NEW.OBSERVACAO) THEN
        BEGIN
            HISTORICO = HISTORICO||' OBS: '||OLD.OBSERVACAO||' -> '||NEW.OBSERVACAO;
        END
        
        IF (OLD.QUANTIDADE <> NEW.QUANTIDADE) THEN
        BEGIN
            HISTORICO = HISTORICO||' QTD:'||OLD.QUANTIDADE||'->'||NEW.QUANTIDADE;
        END
        
        if(OLD.T01 <> NEW.T01) THEN
        BEGIN
            HISTORICO = HISTORICO||' T01:'||OLD.T01||'->'||NEW.T01;
        END
        if(OLD.T02 <> NEW.T02) THEN
        BEGIN
            HISTORICO = HISTORICO||' T02:'||OLD.T02||'->'||NEW.T02;
        END
        if(OLD.T03 <> NEW.T03) THEN
        BEGIN
            HISTORICO = HISTORICO||' T03:'||OLD.T03||'->'||NEW.T03;
        END
        if(OLD.T04 <> NEW.T04) THEN
        BEGIN
            HISTORICO = HISTORICO||' T04:'||OLD.T04||'->'||NEW.T04;
        END
        if(OLD.T05 <> NEW.T05) THEN
        BEGIN
            HISTORICO = HISTORICO||' T05:'||OLD.T05||'->'||NEW.T05;
        END
        if(OLD.T06 <> NEW.T06) THEN
        BEGIN
            HISTORICO = HISTORICO||' T06:'||OLD.T06||'->'||NEW.T06;
        END
        if(OLD.T07 <> NEW.T07) THEN
        BEGIN
            HISTORICO = HISTORICO||' T07:'||OLD.T07||'->'||NEW.T07;
        END
        if(OLD.T08 <> NEW.T08) THEN
        BEGIN
            HISTORICO = HISTORICO||' T08:'||OLD.T08||'->'||NEW.T08;
        END
        if(OLD.T09 <> NEW.T09) THEN
        BEGIN
            HISTORICO = HISTORICO||' T09:'||OLD.T09||'->'||NEW.T09;
        END
        if(OLD.T10 <> NEW.T10) THEN
        BEGIN
            HISTORICO = HISTORICO||' T10:'||OLD.T10||'->'||NEW.T10;
        END
        if(OLD.T11 <> NEW.T11) THEN
        BEGIN
            HISTORICO = HISTORICO||' T11:'||OLD.T11||'->'||NEW.T11;
        END
        if(OLD.T12 <> NEW.T12) THEN
        BEGIN
            HISTORICO = HISTORICO||' T12:'||OLD.T12||'->'||NEW.T12;
        END
        if(OLD.T13 <> NEW.T13) THEN
        BEGIN
            HISTORICO = HISTORICO||' T13:'||OLD.T13||'->'||NEW.T13;
        END
        if(OLD.T14 <> NEW.T14) THEN
        BEGIN
            HISTORICO = HISTORICO||' T14:'||OLD.T14||'->'||NEW.T14;
        END
        if(OLD.T15 <> NEW.T15) THEN
        BEGIN
            HISTORICO = HISTORICO||' T15:'||OLD.T15||'->'||NEW.T15;
        END
        if(OLD.T16 <> NEW.T16) THEN
        BEGIN
            HISTORICO = HISTORICO||' T16:'||OLD.T16||'->'||NEW.T16;
        END
        if(OLD.T17 <> NEW.T17) THEN
        BEGIN
            HISTORICO = HISTORICO||' T17:'||OLD.T17||'->'||NEW.T17;
        END
        if(OLD.T18 <> NEW.T18) THEN
        BEGIN
            HISTORICO = HISTORICO||' T18:'||OLD.T18||'->'||NEW.T18;
        END
        if(OLD.T19 <> NEW.T19) THEN
        BEGIN
            HISTORICO = HISTORICO||' T19:'||OLD.T19||'->'||NEW.T19;
        END
        
        if(OLD.T20 <> NEW.T20) THEN
        BEGIN
            HISTORICO = HISTORICO||' T20:'||OLD.T20||'->'||NEW.T20;
        END

        IF (HISTORICO <> '') THEN
        BEGIN
           HISTORICO = 'CONS.PARAMETRIZÁVEL ID:'||NEW.TIPO_CODIGO||' ALTERADO'||HISTORICO;
        --HISTORICO = 'Registro da Ficha Téc(1) alterado '||FN_LPAD(NEW.TIPO_CODIGO,4,'0')||', Alterada: '||HISTORICO;
           EXECUTE PROCEDURE SPI_HISTORICO('TBMODELO', NEW.MODELO_CODIGO, HISTORICO);
        END
    END
END;

CREATE OR ALTER TRIGGER TTBMODELO_FLUXO_PRODUCAO_B05IU FOR TBMODELO_FLUXO_PRODUCAO
ACTIVE BEFORE INSERT OR UPDATE POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(3000);
BEGIN

    HISTORICO = '';
    IF(INSERTING) THEN
    BEGIN
       IF((SELECT FIRST 1 C.MODELO_ID FROM TBMODELO_FLUXO_PRODUCAO C WHERE C.MODELO_ID = NEW.MODELO_ID
       AND C.FLUXO_ID = NEW.FLUXO_ID AND C.COR_ID = NEW.COR_ID) IS NOT NULL) THEN
       BEGIN
            IF(NEW.TAMANHO = 0 AND NEW.TEMPO > 0)THEN
                BEGIN HISTORICO = HISTORICO||' T00(min):'||0||'->'||NEW.TEMPO;
            END
            IF(NEW.TAMANHO = 1 AND NEW.TEMPO > 0)THEN
                BEGIN HISTORICO = HISTORICO||' T01(min):'||0||'->'||NEW.TEMPO;
            END
            IF(NEW.TAMANHO = 2 AND NEW.TEMPO > 0)THEN
                BEGIN HISTORICO = HISTORICO||' T02(min):'||0||'->'||NEW.TEMPO;
            END
            IF(NEW.TAMANHO = 3 AND NEW.TEMPO > 0)THEN
                BEGIN HISTORICO = HISTORICO||' T03(min):'||0||'->'||NEW.TEMPO;
            END
            IF(NEW.TAMANHO = 4 AND NEW.TEMPO > 0)THEN
                BEGIN HISTORICO = HISTORICO||' T04(min):'||0||'->'||NEW.TEMPO;
            END
            IF(NEW.TAMANHO = 5 AND NEW.TEMPO > 0)THEN
                BEGIN HISTORICO = HISTORICO||' T05(min):'||0||'->'||NEW.TEMPO;
            END
            IF(NEW.TAMANHO = 6 AND NEW.TEMPO > 0)THEN
                BEGIN HISTORICO = HISTORICO||' T06(min):'||0||'->'||NEW.TEMPO;
            END
            IF(NEW.TAMANHO = 7 AND NEW.TEMPO > 0)THEN
                BEGIN HISTORICO = HISTORICO||' T07(min):'||0||'->'||NEW.TEMPO;
            END
            IF(NEW.TAMANHO = 8 AND NEW.TEMPO > 0)THEN
                BEGIN HISTORICO = HISTORICO||' T08(min):'||0||'->'||NEW.TEMPO;
            END
            IF(NEW.TAMANHO = 9 AND NEW.TEMPO > 0)THEN
                BEGIN HISTORICO = HISTORICO||' T09(min):'||0||'->'||NEW.TEMPO;
            END
            IF(NEW.TAMANHO = 10 AND NEW.TEMPO > 0)THEN
                BEGIN HISTORICO = HISTORICO||' T10(min):'||0||'->'||NEW.TEMPO;
            END
            IF(NEW.TAMANHO = 11 AND NEW.TEMPO > 0)THEN
                BEGIN HISTORICO = HISTORICO||' T11(min):'||0||'->'||NEW.TEMPO;
            END
            IF(NEW.TAMANHO = 12 AND NEW.TEMPO > 0)THEN
                BEGIN HISTORICO = HISTORICO||' T12(min):'||0||'->'||NEW.TEMPO;
            END
            IF(NEW.TAMANHO = 13 AND NEW.TEMPO > 0)THEN
                BEGIN HISTORICO = HISTORICO||' T13(min):'||0||'->'||NEW.TEMPO;
            END
            IF(NEW.TAMANHO = 14 AND NEW.TEMPO > 0)THEN
                BEGIN HISTORICO = HISTORICO||' T14(min):'||0||'->'||NEW.TEMPO;
            END
            IF(NEW.TAMANHO = 15 AND NEW.TEMPO > 0)THEN
                BEGIN HISTORICO = HISTORICO||' T15(min):'||0||'->'||NEW.TEMPO;
            END
            IF(NEW.TAMANHO = 16 AND NEW.TEMPO > 0)THEN
                BEGIN HISTORICO = HISTORICO||' T16(min):'||0||'->'||NEW.TEMPO;
            END
            IF(NEW.TAMANHO = 17 AND NEW.TEMPO > 0)THEN
                BEGIN HISTORICO = HISTORICO||' T17(min):'||0||'->'||NEW.TEMPO;
            END
            IF(NEW.TAMANHO = 18 AND NEW.TEMPO > 0)THEN
                BEGIN HISTORICO = HISTORICO||' T18(min):'||0||'->'||NEW.TEMPO;
            END
            IF(NEW.TAMANHO = 19 AND NEW.TEMPO > 0)THEN
                BEGIN HISTORICO = HISTORICO||' T19(min):'||0||'->'||NEW.TEMPO;
            END
            IF(NEW.TAMANHO = 20 AND NEW.TEMPO > 0)THEN
                BEGIN HISTORICO = HISTORICO||' T20(min):'||0||'->'||NEW.TEMPO;
            END
            IF(NEW.TAMANHO = 0 AND NEW.TM > 0)THEN
                BEGIN HISTORICO = HISTORICO||' T00(seg):'||0||'->'||NEW.TM;
            END
            IF(NEW.TAMANHO = 1 AND NEW.TM > 0)THEN
                BEGIN HISTORICO = HISTORICO||' T01(seg):'||0||'->'||NEW.TM;
            END
            IF(NEW.TAMANHO = 2 AND NEW.TM > 0)THEN
                BEGIN HISTORICO = HISTORICO||' T02(seg):'||0||'->'||NEW.TM;
            END
            IF(NEW.TAMANHO = 3 AND NEW.TM > 0)THEN
                BEGIN HISTORICO = HISTORICO||' T03(seg):'||0||'->'||NEW.TM;
            END
            IF(NEW.TAMANHO = 4 AND NEW.TM > 0)THEN
                BEGIN HISTORICO = HISTORICO||' T04(seg):'||0||'->'||NEW.TM;
            END
            IF(NEW.TAMANHO = 5 AND NEW.TM > 0)THEN
                BEGIN HISTORICO = HISTORICO||' T05(seg):'||0||'->'||NEW.TM;
            END
            IF(NEW.TAMANHO = 6 AND NEW.TM > 0)THEN
                BEGIN HISTORICO = HISTORICO||' T06(seg):'||0||'->'||NEW.TM;
            END
            IF(NEW.TAMANHO = 7 AND NEW.TM > 0)THEN
                BEGIN HISTORICO = HISTORICO||' T07(seg):'||0||'->'||NEW.TM;
            END
            IF(NEW.TAMANHO = 8 AND NEW.TM > 0)THEN
                BEGIN HISTORICO = HISTORICO||' T08(seg):'||0||'->'||NEW.TM;
            END
            IF(NEW.TAMANHO = 9 AND NEW.TM > 0)THEN
                BEGIN HISTORICO = HISTORICO||' T09(seg):'||0||'->'||NEW.TM;
            END
            IF(NEW.TAMANHO = 10 AND NEW.TM > 0)THEN
                BEGIN HISTORICO = HISTORICO||' T10(seg):'||0||'->'||NEW.TM;
            END
            IF(NEW.TAMANHO = 11 AND NEW.TM > 0)THEN
                BEGIN HISTORICO = HISTORICO||' T11(seg):'||0||'->'||NEW.TM;
            END
            IF(NEW.TAMANHO = 12 AND NEW.TM > 0)THEN
                BEGIN HISTORICO = HISTORICO||' T12(seg):'||0||'->'||NEW.TM;
            END
            IF(NEW.TAMANHO = 13 AND NEW.TM > 0)THEN
                BEGIN HISTORICO = HISTORICO||' T13(seg):'||0||'->'||NEW.TM;
            END
            IF(NEW.TAMANHO = 14 AND NEW.TM > 0)THEN
                BEGIN HISTORICO = HISTORICO||' T14(seg):'||0||'->'||NEW.TM;
            END
            IF(NEW.TAMANHO = 15 AND NEW.TM > 0)THEN
                BEGIN HISTORICO = HISTORICO||' T15(seg):'||0||'->'||NEW.TM;
            END
            IF(NEW.TAMANHO = 16 AND NEW.TM > 0)THEN
                BEGIN HISTORICO = HISTORICO||' T16(seg):'||0||'->'||NEW.TM;
            END
            IF(NEW.TAMANHO = 17 AND NEW.TM > 0)THEN
                BEGIN HISTORICO = HISTORICO||' T17(seg):'||0||'->'||NEW.TM;
            END
            IF(NEW.TAMANHO = 18 AND NEW.TM > 0)THEN
                BEGIN HISTORICO = HISTORICO||' T18(seg):'||0||'->'||NEW.TM;
            END
            IF(NEW.TAMANHO = 19 AND NEW.TM > 0)THEN
                BEGIN HISTORICO = HISTORICO||' T19(seg):'||0||'->'||NEW.TM;
            END
            IF(NEW.TAMANHO = 20 AND NEW.TM > 0)THEN
                BEGIN HISTORICO = HISTORICO||' T20(seg):'||0||'->'||NEW.TM;
            END
            IF (HISTORICO <> '') THEN
            BEGIN
               HISTORICO = ' FLUXO DE PRODUCAO:'||new.FLUXO_ID||', COR: '||IIF(NEW.COR_ID > 0,(select first 1 NEW.COR_ID||' - '||a.descricao from TBCOR a where a.CODIGO = new.COR_ID),'*** PADRÃO ***')||' ALTERADO '||HISTORICO;
               EXECUTE PROCEDURE SPI_HISTORICO('TBMODELO', NEW.MODELO_ID, HISTORICO);
            END
       END
       ELSE
       BEGIN
           HISTORICO = HISTORICO||' FLUXO DE PRODUCAO:'||new.FLUXO_ID||', COR: '||IIF(NEW.COR_ID > 0,(select first 1 NEW.COR_ID||' - '||a.descricao from TBCOR a where a.CODIGO = new.COR_ID),'*** PADRÃO ***')||' INCLUIDA.';
           EXECUTE PROCEDURE SPI_HISTORICO('TBMODELO', NEW.MODELO_ID, HISTORICO);
       END
    END

    IF(UPDATING) THEN
    BEGIN
        IF(NEW.TAMANHO = 0 AND OLD.TEMPO <> NEW.TEMPO)THEN
            BEGIN HISTORICO = HISTORICO||' T00(min):'||OLD.TEMPO||'->'||NEW.TEMPO;
        END
        IF(NEW.TAMANHO = 1 AND OLD.TEMPO <> NEW.TEMPO)THEN
            BEGIN HISTORICO = HISTORICO||' T01(min):'||OLD.TEMPO||'->'||NEW.TEMPO;
        END
        IF(NEW.TAMANHO = 2 AND OLD.TEMPO <> NEW.TEMPO)THEN
            BEGIN HISTORICO = HISTORICO||' T02(min):'||OLD.TEMPO||'->'||NEW.TEMPO;
        END
        IF(NEW.TAMANHO = 3 AND OLD.TEMPO <> NEW.TEMPO)THEN
            BEGIN HISTORICO = HISTORICO||' T03(min):'||OLD.TEMPO||'->'||NEW.TEMPO;
        END
        IF(NEW.TAMANHO = 4 AND OLD.TEMPO <> NEW.TEMPO)THEN
            BEGIN HISTORICO = HISTORICO||' T04(min):'||OLD.TEMPO||'->'||NEW.TEMPO;
        END
        IF(NEW.TAMANHO = 5 AND OLD.TEMPO <> NEW.TEMPO)THEN
            BEGIN HISTORICO = HISTORICO||' T05(min):'||OLD.TEMPO||'->'||NEW.TEMPO;
        END
        IF(NEW.TAMANHO = 6 AND OLD.TEMPO <> NEW.TEMPO)THEN
            BEGIN HISTORICO = HISTORICO||' T06(min):'||OLD.TEMPO||'->'||NEW.TEMPO;
        END
        IF(NEW.TAMANHO = 7 AND OLD.TEMPO <> NEW.TEMPO)THEN
            BEGIN HISTORICO = HISTORICO||' T07(min):'||OLD.TEMPO||'->'||NEW.TEMPO;
        END
        IF(NEW.TAMANHO = 8 AND OLD.TEMPO <> NEW.TEMPO)THEN
            BEGIN HISTORICO = HISTORICO||' T08(min):'||OLD.TEMPO||'->'||NEW.TEMPO;
        END
        IF(NEW.TAMANHO = 9 AND OLD.TEMPO <> NEW.TEMPO)THEN
            BEGIN HISTORICO = HISTORICO||' T09(min):'||OLD.TEMPO||'->'||NEW.TEMPO;
        END
        IF(NEW.TAMANHO = 10 AND OLD.TEMPO <> NEW.TEMPO)THEN
            BEGIN HISTORICO = HISTORICO||' T10(min):'||OLD.TEMPO||'->'||NEW.TEMPO;
        END
        IF(NEW.TAMANHO = 11 AND OLD.TEMPO <> NEW.TEMPO)THEN
            BEGIN HISTORICO = HISTORICO||' T11(min):'||OLD.TEMPO||'->'||NEW.TEMPO;
        END
        IF(NEW.TAMANHO = 12 AND OLD.TEMPO <> NEW.TEMPO)THEN
            BEGIN HISTORICO = HISTORICO||' T12(min):'||OLD.TEMPO||'->'||NEW.TEMPO;
        END
        IF(NEW.TAMANHO = 13 AND OLD.TEMPO <> NEW.TEMPO)THEN
            BEGIN HISTORICO = HISTORICO||' T13(min):'||OLD.TEMPO||'->'||NEW.TEMPO;
        END
        IF(NEW.TAMANHO = 14 AND OLD.TEMPO <> NEW.TEMPO)THEN
            BEGIN HISTORICO = HISTORICO||' T14(min):'||OLD.TEMPO||'->'||NEW.TEMPO;
        END
        IF(NEW.TAMANHO = 15 AND OLD.TEMPO <> NEW.TEMPO)THEN
            BEGIN HISTORICO = HISTORICO||' T15(min):'||OLD.TEMPO||'->'||NEW.TEMPO;
        END
        IF(NEW.TAMANHO = 16 AND OLD.TEMPO <> NEW.TEMPO)THEN
            BEGIN HISTORICO = HISTORICO||' T16(min):'||OLD.TEMPO||'->'||NEW.TEMPO;
        END
        IF(NEW.TAMANHO = 17 AND OLD.TEMPO <> NEW.TEMPO)THEN
            BEGIN HISTORICO = HISTORICO||' T17(min):'||OLD.TEMPO||'->'||NEW.TEMPO;
        END
        IF(NEW.TAMANHO = 18 AND OLD.TEMPO <> NEW.TEMPO)THEN
            BEGIN HISTORICO = HISTORICO||' T18(min):'||OLD.TEMPO||'->'||NEW.TEMPO;
        END
        IF(NEW.TAMANHO = 19 AND OLD.TEMPO <> NEW.TEMPO)THEN
            BEGIN HISTORICO = HISTORICO||' T19(min):'||OLD.TEMPO||'->'||NEW.TEMPO;
        END
        IF(NEW.TAMANHO = 20 AND OLD.TEMPO <> NEW.TEMPO)THEN
            BEGIN HISTORICO = HISTORICO||' T20(min):'||OLD.TEMPO||'->'||NEW.TEMPO;
        END
    
        IF(NEW.TAMANHO = 0 AND OLD.TM <> NEW.TM)THEN
        BEGIN
            HISTORICO = HISTORICO||' T00(seg):'||OLD.TM||'->'||NEW.TM;
        END
        IF(NEW.TAMANHO = 1 AND OLD.TM <> NEW.TM)THEN
            BEGIN HISTORICO = HISTORICO||' T01(seg):'||OLD.TM||'->'||NEW.TM;
        END
        IF(NEW.TAMANHO = 2 AND OLD.TM <> NEW.TM)THEN
            BEGIN HISTORICO = HISTORICO||' T02(seg):'||OLD.TM||'->'||NEW.TM;
        END
        IF(NEW.TAMANHO = 3 AND OLD.TM <> NEW.TM)THEN
            BEGIN HISTORICO = HISTORICO||' T03(seg):'||OLD.TM||'->'||NEW.TM;
        END
        IF(NEW.TAMANHO = 4 AND OLD.TM <> NEW.TM)THEN
            BEGIN HISTORICO = HISTORICO||' T04(seg):'||OLD.TM||'->'||NEW.TM;
        END
        IF(NEW.TAMANHO = 5 AND OLD.TM <> NEW.TM)THEN
            BEGIN HISTORICO = HISTORICO||' T05(seg):'||OLD.TM||'->'||NEW.TM;
        END
        IF(NEW.TAMANHO = 6 AND OLD.TM <> NEW.TM)THEN
            BEGIN HISTORICO = HISTORICO||' T06(seg):'||OLD.TM||'->'||NEW.TM;
        END
        IF(NEW.TAMANHO = 7 AND OLD.TM <> NEW.TM)THEN
            BEGIN HISTORICO = HISTORICO||' T07(seg):'||OLD.TM||'->'||NEW.TM;
        END
        IF(NEW.TAMANHO = 8 AND OLD.TM <> NEW.TM)THEN
            BEGIN HISTORICO = HISTORICO||' T08(seg):'||OLD.TM||'->'||NEW.TM;
        END
        IF(NEW.TAMANHO = 9 AND OLD.TM <> NEW.TM)THEN
            BEGIN HISTORICO = HISTORICO||' T09(seg):'||OLD.TM||'->'||NEW.TM;
        END
        IF(NEW.TAMANHO = 10 AND OLD.TM <> NEW.TM)THEN
            BEGIN HISTORICO = HISTORICO||' T10(seg):'||OLD.TM||'->'||NEW.TM;
        END
        IF(NEW.TAMANHO = 11 AND OLD.TM <> NEW.TM)THEN
            BEGIN HISTORICO = HISTORICO||' T11(seg):'||OLD.TM||'->'||NEW.TM;
        END
        IF(NEW.TAMANHO = 12 AND OLD.TM <> NEW.TM)THEN
            BEGIN HISTORICO = HISTORICO||' T12(seg):'||OLD.TM||'->'||NEW.TM;
        END
        IF(NEW.TAMANHO = 13 AND OLD.TM <> NEW.TM)THEN
            BEGIN HISTORICO = HISTORICO||' T13(seg):'||OLD.TM||'->'||NEW.TM;
        END
        IF(NEW.TAMANHO = 14 AND OLD.TM <> NEW.TM)THEN
            BEGIN HISTORICO = HISTORICO||' T14(seg):'||OLD.TM||'->'||NEW.TM;
        END
        IF(NEW.TAMANHO = 15 AND OLD.TM <> NEW.TM)THEN
            BEGIN HISTORICO = HISTORICO||' T15(seg):'||OLD.TM||'->'||NEW.TM;
        END
        IF(NEW.TAMANHO = 16 AND OLD.TM <> NEW.TM)THEN
            BEGIN HISTORICO = HISTORICO||' T16(seg):'||OLD.TM||'->'||NEW.TM;
        END
        IF(NEW.TAMANHO = 17 AND OLD.TM <> NEW.TM)THEN
            BEGIN HISTORICO = HISTORICO||' T17(seg):'||OLD.TM||'->'||NEW.TM;
        END
        IF(NEW.TAMANHO = 18 AND OLD.TM <> NEW.TM)THEN
            BEGIN HISTORICO = HISTORICO||' T18(seg):'||OLD.TM||'->'||NEW.TM;
        END
        IF(NEW.TAMANHO = 19 AND OLD.TM <> NEW.TM)THEN
            BEGIN HISTORICO = HISTORICO||' T19(seg):'||OLD.TM||'->'||NEW.TM;
        END
        IF(NEW.TAMANHO = 20 AND OLD.TM <> NEW.TM)THEN
            BEGIN HISTORICO = HISTORICO||' T20(seg):'||OLD.TM||'->'||NEW.TM;
        END
        IF(OLD.ORDEM <> NEW.ORDEM)THEN
            BEGIN HISTORICO = HISTORICO||' ORDEM:'||OLD.ORDEM||'->'||NEW.ORDEM;
        END
        IF (HISTORICO <> '') THEN
        BEGIN
           HISTORICO = ' FLUXO DE PRODUCAO:'||new.FLUXO_ID||', COR: '||IIF(NEW.COR_ID > 0,(select first 1 NEW.COR_ID||' - '||a.descricao from TBCOR a where a.CODIGO = new.COR_ID),'*** PADRÃO ***')||' ALTERADO '||HISTORICO;
           EXECUTE PROCEDURE SPI_HISTORICO('TBMODELO', NEW.MODELO_ID, HISTORICO);
        END
    END
END;

CREATE OR ALTER TRIGGER TBMODELO_FLUXO_PRODUCAO_B1I FOR TBMODELO_FLUXO_PRODUCAO
INACTIVE BEFORE INSERT POSITION 1
AS
begin
  if (New.Tempo = 0) then
      Begin
      --EXCEPTION "..." 'ATENDEU A CONDICAO 1! NOVO TEMPO: '||NEW.TEMPO;
      New.Tempo = Coalesce((Select FP.Tempo From TbFluxo_Producao FP Where FP.Id = New.FLuxo_Id),0.0000);
      End
end;

CREATE OR ALTER TRIGGER TTBMODELO_FLUXO_PRODUCAO_B03D FOR TBMODELO_FLUXO_PRODUCAO
ACTIVE BEFORE DELETE POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF(OLD.MODELO_ID > 0) THEN
    BEGIN
       HISTORICO = ' FLUXO DE PRODUCAO:'||old.FLUXO_ID||', COR: '||IIF(old.COR_ID > 0,(select first 1 old.COR_ID||' - '||a.descricao from TBCOR a where a.CODIGO = old.COR_ID),'*** PADRÃO ***')||' EXCLUIDO.';
    END
    IF(HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBMODELO', OLD.MODELO_ID, HISTORICO);
    END
END;

CREATE OR ALTER TRIGGER TTBMODELO_EMBALAGEM_B9U FOR TBMODELO_EMBALAGEM
ACTIVE BEFORE UPDATE POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
DECLARE VARIABLE Grade Integer;
DECLARE VARIABLE G01 VarChar(10); DECLARE VARIABLE G02 VarChar(10);
DECLARE VARIABLE G03 VarChar(10); DECLARE VARIABLE G04 VarChar(10);
DECLARE VARIABLE G05 VarChar(10); DECLARE VARIABLE G06 VarChar(10);
DECLARE VARIABLE G07 VarChar(10); DECLARE VARIABLE G08 VarChar(10);
DECLARE VARIABLE G09 VarChar(10); DECLARE VARIABLE G10 VarChar(10);
DECLARE VARIABLE G11 VarChar(10); DECLARE VARIABLE G12 VarChar(10);
DECLARE VARIABLE G13 VarChar(10); DECLARE VARIABLE G14 VarChar(10);
DECLARE VARIABLE G15 VarChar(10); DECLARE VARIABLE G16 VarChar(10);
DECLARE VARIABLE G17 VarChar(10); DECLARE VARIABLE G18 VarChar(10);
DECLARE VARIABLE G19 VarChar(10); DECLARE VARIABLE G20 VarChar(10);
begin

  For
  Select Coalesce(M.Grade_Codigo,0),
         Coalesce(G.T01,''), Coalesce(G.T02,''), Coalesce(G.T03,''), Coalesce(G.T04,''),
         Coalesce(G.T05,''), Coalesce(G.T06,''), Coalesce(G.T07,''), Coalesce(G.T08,''),
         Coalesce(G.T09,''), Coalesce(G.T10,''), Coalesce(G.T11,''), Coalesce(G.T12,''),
         Coalesce(G.T13,''), Coalesce(G.T14,''), Coalesce(G.T15,''), Coalesce(G.T16,''),
         Coalesce(G.T17,''), Coalesce(G.T18,''), Coalesce(G.T19,''), Coalesce(G.T20,'')
    From TbModelo M
   Left join TbGrade G On G.Codigo = M.Grade_Codigo
   Where M.Codigo = Old.Modelo_Codigo
   Into :Grade,:G01,:G02,:G03,:G04,:G05,:G06,:G07,:G08,:G09,
     :G10,:G11,:G12,:G13,:G14,:G15,:G16,:G17,:G18,:G19,:G20
   DO

   HISTORICO = 'EMBALAGEM - ';

   if (:Grade = 0) then
       Begin
       if (OLD.QUANTIDADE <> NEW.QUANTIDADE) then
           BEGIN
           HISTORICO = HISTORICO||' QTD: '||Trunc(OLD.QUANTIDADE)||'->'||Trunc(NEW.QUANTIDADE);
           END
       End

   if (:Grade > 0) then
       Begin

       if (OLD.QUANTIDADE <> NEW.QUANTIDADE) then
           BEGIN
           HISTORICO = HISTORICO||' QTD: '||Trunc(OLD.QUANTIDADE)||'->'||Trunc(NEW.QUANTIDADE);
           END


      if (OLD.T01 <> NEW.T01) then
           BEGIN
           HISTORICO = HISTORICO||' ['||:G01||': '||Trunc(OLD.T01)||'->'||Trunc(NEW.T01)||']';
           END
    
       if (OLD.T02 <> NEW.T02) then
           BEGIN
           HISTORICO = HISTORICO||' ['||:G02||': '||Trunc(OLD.T02)||'->'||Trunc(NEW.T02)||']';
           END
    
       if (OLD.T03 <> NEW.T03) then
           BEGIN
           HISTORICO = HISTORICO||' ['||:G03||': '||Trunc(OLD.T03)||'->'||Trunc(NEW.T03)||']';
           END
    
       if (OLD.T04 <> NEW.T04) then
           BEGIN
           HISTORICO = HISTORICO||' ['||:G04||': '||Trunc(OLD.T04)||'->'||Trunc(NEW.T04)||']';
           END
    
       if (OLD.T05 <> NEW.T05) then
           BEGIN
           HISTORICO = HISTORICO||' ['||:G05||': '||Trunc(OLD.T05)||'->'||Trunc(NEW.T05)||']';
           END
    
       if (OLD.T06 <> NEW.T06) then
           BEGIN
           HISTORICO = HISTORICO||' ['||:G06||': '||Trunc(OLD.T06)||'->'||Trunc(NEW.T06)||']';
           END
    
       if (OLD.T07 <> NEW.T07) then
           BEGIN
           HISTORICO = HISTORICO||' ['||:G07||': '||Trunc(OLD.T07)||'->'||Trunc(NEW.T07)||']';
           END
    
       if (OLD.T08 <> NEW.T08) then
           BEGIN
           HISTORICO = HISTORICO||' ['||:G08||': '||Trunc(OLD.T08)||'->'||Trunc(NEW.T08)||']';
           END
    
       if (OLD.T09 <> NEW.T09) then
           BEGIN
           HISTORICO = HISTORICO||' ['||:G09||': '||Trunc(OLD.T09)||'->'||Trunc(NEW.T09)||']';
           END
    
       if (OLD.T10 <> NEW.T10) then
           BEGIN
           HISTORICO = HISTORICO||' ['||:G10||': '||Trunc(OLD.T10)||'->'||Trunc(NEW.T10)||']';
           END
    
       if (OLD.T11 <> NEW.T11) then
           BEGIN
           HISTORICO = HISTORICO||' ['||:G11||': '||Trunc(OLD.T11)||'->'||Trunc(NEW.T11)||']';
           END
    
       if (OLD.T12 <> NEW.T12) then
           BEGIN
           HISTORICO = HISTORICO||' ['||:G12||': '||Trunc(OLD.T12)||'->'||Trunc(NEW.T12)||']';
           END
    
       if (OLD.T13 <> NEW.T13) then
           BEGIN
           HISTORICO = HISTORICO||' ['||:G13||': '||Trunc(OLD.T13)||'->'||Trunc(NEW.T13)||']';
           END
    
       if (OLD.T14 <> NEW.T14) then
           BEGIN
           HISTORICO = HISTORICO||' ['||:G14||': '||Trunc(OLD.T14)||'->'||Trunc(NEW.T14)||']';
           END
    
       if (OLD.T15 <> NEW.T15) then
           BEGIN
           HISTORICO = HISTORICO||' ['||:G15||': '||Trunc(OLD.T15)||'->'||Trunc(NEW.T15)||']';
           END
    
       if (OLD.T16 <> NEW.T16) then
           BEGIN
           HISTORICO = HISTORICO||' ['||:G16||': '||Trunc(OLD.T16)||'->'||Trunc(NEW.T16)||']';
           END
    
       if (OLD.T17 <> NEW.T17) then
           BEGIN
           HISTORICO = HISTORICO||' ['||:G17||': '||Trunc(OLD.T17)||'->'||Trunc(NEW.T17)||']';
           END
    
       if (OLD.T18 <> NEW.T18) then
           BEGIN
           HISTORICO = HISTORICO||' ['||:G18||': '||Trunc(OLD.T18)||'->'||Trunc(NEW.T18)||']';
           END
    
       if (OLD.T19 <> NEW.T19) then
           BEGIN
           HISTORICO = HISTORICO||' ['||:G19||': '||Trunc(OLD.T19)||'->'||Trunc(NEW.T19)||']';
           END
    
       if (OLD.T20 <> NEW.T20) then
           BEGIN
           HISTORICO = HISTORICO||' ['||:G20||': '||Trunc(OLD.T20)||'->'||Trunc(NEW.T20)||']';
           END

       End

   if (HISTORICO <> 'EMBALAGEM - ') Then
       Begin
       INSERT INTO TBHISTORICO (
       TABELA, TABELA_ID, USUARIO, IP, HISTORICO) VALUES (
      'TBMODELO', OLD.MODELO_CODIGO,
      (Select First 1 MON$USER from mon$attachments where mon$attachment_id = current_connection),
      (Select First 1 MON$REMOTE_ADDRESS from mon$attachments where mon$attachment_id = current_connection),
       :Historico);
       End

End;

CREATE TRIGGER TTBMODELO_B08UI FOR TBMODELO
ACTIVE BEFORE INSERT OR UPDATE POSITION 3
AS
begin
  IF((NEW.T01 = '0' OR NEW.T01 IS NULL) AND (NEW.T02 = '0' OR NEW.T02 IS NULL) AND
    (NEW.T03 = '0' OR NEW.T03 IS NULL) AND (NEW.T04 = '0' OR NEW.T04 IS NULL) AND
    (NEW.T05 = '0' OR NEW.T05 IS NULL) AND (NEW.T06 = '0' OR NEW.T06 IS NULL) AND
    (NEW.T07 = '0' OR NEW.T07 IS NULL) AND (NEW.T08 = '0' OR NEW.T08 IS NULL) AND
    (NEW.T09 = '0' OR NEW.T09 IS NULL) AND (NEW.T10 = '0' OR NEW.T10 IS NULL) AND
    (NEW.T11 = '0' OR NEW.T11 IS NULL) AND (NEW.T12 = '0' OR NEW.T12 IS NULL) AND
    (NEW.T13 = '0' OR NEW.T13 IS NULL) AND (NEW.T14 = '0' OR NEW.T14 IS NULL) AND
    (NEW.T15 = '0' OR NEW.T15 IS NULL) AND (NEW.T16 = '0' OR NEW.T16 IS NULL) AND
    (NEW.T17 = '0' OR NEW.T17 IS NULL) AND (NEW.T18 = '0' OR NEW.T18 IS NULL) AND
    (NEW.T19 = '0' OR NEW.T19 IS NULL) AND (NEW.T20 = '0' OR NEW.T20 IS NULL)
    AND NEW.GRADE_CODIGO > 0) THEN
    
    BEGIN
       EXCEPTION "..." 'GRADE DO MODELO REQUER AO MENOS 1 TAMANHO ATIVO OPERACAO CANCELADA!';
    END
end;

CREATE OR ALTER TRIGGER TTBMODELO_SETOR_B01U FOR TBMODELO_SETOR
ACTIVE BEFORE UPDATE POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN
    HISTORICO = '';
    IF(OLD.ORDEM <> NEW.ORDEM) THEN
    BEGIN
        HISTORICO = HISTORICO||' ORD:'||OLD.ORDEM||'->'||NEW.ORDEM;
    END

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

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

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

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

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

    IF(OLD.OBSERVACAO <> NEW.OBSERVACAO) THEN
    BEGIN
       HISTORICO = HISTORICO||' OBS:'||OLD.OBSERVACAO||'->'||NEW.OBSERVACAO;
    END
    
    IF(HISTORICO <> '') THEN
    BEGIN
        HISTORICO = 'ROT.PROD. ORDEM: '||FN_LPAD(OLD.ORDEM,3,'0')||', UP: '||FN_LPAD(OLD.SETOR_CODIGO,4,0)||' ALTERADO'||HISTORICO;
        EXECUTE PROCEDURE SPI_HISTORICO('TBMODELO',NEW.MODELO_CODIGO,HISTORICO);
    END
END;

CREATE TRIGGER TTBMODELO_SETOR_B02D FOR TBMODELO_SETOR
ACTIVE BEFORE DELETE POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF (OLD.MODELO_CODIGO > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||'ROT.PROD. ORDEM: '||FN_LPAD(OLD.ORDEM,3,'0')||', UP: '||FN_LPAD(OLD.SETOR_CODIGO,4,0)||' EXCLUIDO';
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBMODELO', OLD.MODELO_CODIGO, HISTORICO);
    END
END;

CREATE TRIGGER TTBMODELO_FLUXO_B03I FOR TBMODELO_FLUXO
ACTIVE BEFORE INSERT POSITION 0
AS
declare variable fluxo integer;
begin
  if(new.fluxo_id > 0)then
  begin
     fluxo = coalesce((select first 1 a.fluxo_id from tbmodelo_fluxo a where a.MODELO_ID = new.modelo_id and a.FLUXO_ID = new.fluxo_id),0);
     if(fluxo > 0)then
     begin
       exception "..." 'FLUXO ID:'||FN_LPAD(FLUXO,4,0)||' JÁ INCLUÍDO PARA ESTE MODELO!, OPERAÇÃO CANCELADA!';
     end
  end
end;

CREATE OR ALTER TRIGGER TTBMODELO_FICHA_TECNICA_B02U FOR TBMODELO_FICHA_TECNICA
ACTIVE BEFORE INSERT OR UPDATE POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(8000);
DECLARE VARIABLE Grade Integer;
DECLARE VARIABLE G01 VarChar(10); DECLARE VARIABLE G02 VarChar(10);
DECLARE VARIABLE G03 VarChar(10); DECLARE VARIABLE G04 VarChar(10);
DECLARE VARIABLE G05 VarChar(10); DECLARE VARIABLE G06 VarChar(10);
DECLARE VARIABLE G07 VarChar(10); DECLARE VARIABLE G08 VarChar(10);
DECLARE VARIABLE G09 VarChar(10); DECLARE VARIABLE G10 VarChar(10);
DECLARE VARIABLE G11 VarChar(10); DECLARE VARIABLE G12 VarChar(10);
DECLARE VARIABLE G13 VarChar(10); DECLARE VARIABLE G14 VarChar(10);
DECLARE VARIABLE G15 VarChar(10); DECLARE VARIABLE G16 VarChar(10);
DECLARE VARIABLE G17 VarChar(10); DECLARE VARIABLE G18 VarChar(10);
DECLARE VARIABLE G19 VarChar(10); DECLARE VARIABLE G20 VarChar(10);
BEGIN
  
    HISTORICO = '';
    IF(INSERTING) THEN
    BEGIN
        IF(NEW.OBSERVACAO IS NOT NULL AND NEW.OBSERVACAO <> '') THEN
        BEGIN
            HISTORICO = HISTORICO||' OBS: '||''||' -> '||NEW.OBSERVACAO;
        END

        IF (0 <> NEW.QUANTIDADE) THEN
        BEGIN
            HISTORICO = HISTORICO||' QTD:'||0||'->'||NEW.QUANTIDADE;
        END
        
        For
            SELECT FIRST 1 Coalesce(G.CODIGO,0),
                 Coalesce(G.T01,'T01'), Coalesce(G.T02,'T02'), Coalesce(G.T03,'T03'), Coalesce(G.T04,'T03'),
                 Coalesce(G.T05,'T05'), Coalesce(G.T06,'T06'), Coalesce(G.T07,'T07'), Coalesce(G.T08,'T07'),
                 Coalesce(G.T09,'T09'), Coalesce(G.T10,'T10'), Coalesce(G.T11,'T11'), Coalesce(G.T12,'T11'),
                 Coalesce(G.T13,'T13'), Coalesce(G.T14,'T14'), Coalesce(G.T15,'T15'), Coalesce(G.T16,'T15'),
                 Coalesce(G.T17,'T17'), Coalesce(G.T18,'T18'), Coalesce(G.T19,'T19'), Coalesce(G.T20,'T19')
        From TbGrade G WHERE G.Codigo = coalesce((select first 1 z.grade_codigo from tbmodelo z where z.codigo = new.MODELO_CODIGO),0)
        Into :Grade,:G01,:G02,:G03,:G04,:G05,:G06,:G07,:G08,:G09,
         :G10,:G11,:G12,:G13,:G14,:G15,:G16,:G17,:G18,:G19,:G20
        DO
        BEGIN
        IF(0 <> NEW.T01) THEN BEGIN HISTORICO = HISTORICO||' ['||:G01||':'||0||'->'||NEW.T01||']'; END
        IF(0 <> NEW.T02) THEN BEGIN HISTORICO = HISTORICO||' ['||:G02||':'||0||'->'||NEW.T02||']'; END
        IF(0 <> NEW.T03) THEN BEGIN HISTORICO = HISTORICO||' ['||:G03||':'||0||'->'||NEW.T03||']'; END
        IF(0 <> NEW.T04) THEN BEGIN HISTORICO = HISTORICO||' ['||:G04||':'||0||'->'||NEW.T04||']'; END
        IF(0 <> NEW.T05) THEN BEGIN HISTORICO = HISTORICO||' ['||:G05||':'||0||'->'||NEW.T05||']'; END
        IF(0 <> NEW.T06) THEN BEGIN HISTORICO = HISTORICO||' ['||:G06||':'||0||'->'||NEW.T06||']'; END
        IF(0 <> NEW.T07) THEN BEGIN HISTORICO = HISTORICO||' ['||:G07||':'||0||'->'||NEW.T07||']'; END
        IF(0 <> NEW.T08) THEN BEGIN HISTORICO = HISTORICO||' ['||:G08||':'||0||'->'||NEW.T08||']'; END
        IF(0 <> NEW.T09) THEN BEGIN HISTORICO = HISTORICO||' ['||:G09||':'||0||'->'||NEW.T09||']'; END
        IF(0 <> NEW.T10) THEN BEGIN HISTORICO = HISTORICO||' ['||:G10||':'||0||'->'||NEW.T10||']'; END
        IF(0 <> NEW.T11) THEN BEGIN HISTORICO = HISTORICO||' ['||:G11||':'||0||'->'||NEW.T11||']'; END
        IF(0 <> NEW.T12) THEN BEGIN HISTORICO = HISTORICO||' ['||:G12||':'||0||'->'||NEW.T12||']'; END
        IF(0 <> NEW.T13) THEN BEGIN HISTORICO = HISTORICO||' ['||:G13||':'||0||'->'||NEW.T13||']'; END
        IF(0 <> NEW.T14) THEN BEGIN HISTORICO = HISTORICO||' ['||:G14||':'||0||'->'||NEW.T14||']'; END
        IF(0 <> NEW.T15) THEN BEGIN HISTORICO = HISTORICO||' ['||:G15||':'||0||'->'||NEW.T15||']'; END
        IF(0 <> NEW.T16) THEN BEGIN HISTORICO = HISTORICO||' ['||:G16||':'||0||'->'||NEW.T16||']'; END
        IF(0 <> NEW.T17) THEN BEGIN HISTORICO = HISTORICO||' ['||:G17||':'||0||'->'||NEW.T17||']'; END
        IF(0 <> NEW.T18) THEN BEGIN HISTORICO = HISTORICO||' ['||:G18||':'||0||'->'||NEW.T18||']'; END
        IF(0 <> NEW.T19) THEN BEGIN HISTORICO = HISTORICO||' ['||:G19||':'||0||'->'||NEW.T19||']'; END
        IF(0 <> NEW.T20) THEN BEGIN HISTORICO = HISTORICO||' ['||:G20||':'||0||'->'||NEW.T20||']'; END
        END

        IF (HISTORICO <> '') THEN
        BEGIN
           HISTORICO = 'CONS.PARAM. ID:'||NEW.TIPO_CODIGO||' ALTERADO'||FN_ELIPSES(HISTORICO,500);
        --HISTORICO = 'Registro da Ficha Téc(1) alterado '||FN_LPAD(NEW.TIPO_CODIGO,4,'0')||', Alterada: '||HISTORICO;
           EXECUTE PROCEDURE SPI_HISTORICO('TBMODELO', NEW.MODELO_CODIGO, HISTORICO);
        END
    END
    ELSE
    BEGIN
        IF (OLD.OBSERVACAO <> NEW.OBSERVACAO) THEN
        BEGIN
            HISTORICO = HISTORICO||' OBS: '||OLD.OBSERVACAO||' -> '||NEW.OBSERVACAO;
        END
        
        IF (OLD.QUANTIDADE <> NEW.QUANTIDADE) THEN
        BEGIN
            HISTORICO = HISTORICO||' QTD:'||OLD.QUANTIDADE||'->'||NEW.QUANTIDADE;
        END
        
        For
        SELECT FIRST 1 Coalesce(G.CODIGO,0),
                 Coalesce(G.T01,'T01'), Coalesce(G.T02,'T02'), Coalesce(G.T03,'T03'), Coalesce(G.T04,'T03'),
                 Coalesce(G.T05,'T05'), Coalesce(G.T06,'T06'), Coalesce(G.T07,'T07'), Coalesce(G.T08,'T07'),
                 Coalesce(G.T09,'T09'), Coalesce(G.T10,'T10'), Coalesce(G.T11,'T11'), Coalesce(G.T12,'T11'),
                 Coalesce(G.T13,'T13'), Coalesce(G.T14,'T14'), Coalesce(G.T15,'T15'), Coalesce(G.T16,'T15'),
                 Coalesce(G.T17,'T17'), Coalesce(G.T18,'T18'), Coalesce(G.T19,'T19'), Coalesce(G.T20,'T19')
        From TbGrade G WHERE G.Codigo = coalesce((select first 1 z.grade_codigo from tbmodelo z where z.codigo = new.MODELO_CODIGO),0)
        Into :Grade,:G01,:G02,:G03,:G04,:G05,:G06,:G07,:G08,:G09,
         :G10,:G11,:G12,:G13,:G14,:G15,:G16,:G17,:G18,:G19,:G20
        DO
        BEGIN
        IF(OLD.T01 <> NEW.T01) THEN BEGIN HISTORICO = HISTORICO||' ['||:G01||':'||OLD.T01||'->'||NEW.T01||']'; END
        IF(OLD.T02 <> NEW.T02) THEN BEGIN HISTORICO = HISTORICO||' ['||:G02||':'||OLD.T02||'->'||NEW.T02||']'; END
        IF(OLD.T03 <> NEW.T03) THEN BEGIN HISTORICO = HISTORICO||' ['||:G03||':'||OLD.T03||'->'||NEW.T03||']'; END
        IF(OLD.T04 <> NEW.T04) THEN BEGIN HISTORICO = HISTORICO||' ['||:G04||':'||OLD.T04||'->'||NEW.T04||']'; END
        IF(OLD.T05 <> NEW.T05) THEN BEGIN HISTORICO = HISTORICO||' ['||:G05||':'||OLD.T05||'->'||NEW.T05||']'; END
        IF(OLD.T06 <> NEW.T06) THEN BEGIN HISTORICO = HISTORICO||' ['||:G06||':'||OLD.T06||'->'||NEW.T06||']'; END
        IF(OLD.T07 <> NEW.T07) THEN BEGIN HISTORICO = HISTORICO||' ['||:G07||':'||OLD.T07||'->'||NEW.T07||']'; END
        IF(OLD.T08 <> NEW.T08) THEN BEGIN HISTORICO = HISTORICO||' ['||:G08||':'||OLD.T08||'->'||NEW.T08||']'; END
        IF(OLD.T09 <> NEW.T09) THEN BEGIN HISTORICO = HISTORICO||' ['||:G09||':'||OLD.T09||'->'||NEW.T09||']'; END
        IF(OLD.T10 <> NEW.T10) THEN BEGIN HISTORICO = HISTORICO||' ['||:G10||':'||OLD.T10||'->'||NEW.T10||']'; END
        IF(OLD.T11 <> NEW.T11) THEN BEGIN HISTORICO = HISTORICO||' ['||:G11||':'||OLD.T11||'->'||NEW.T11||']'; END
        IF(OLD.T12 <> NEW.T12) THEN BEGIN HISTORICO = HISTORICO||' ['||:G12||':'||OLD.T12||'->'||NEW.T12||']'; END
        IF(OLD.T13 <> NEW.T13) THEN BEGIN HISTORICO = HISTORICO||' ['||:G13||':'||OLD.T13||'->'||NEW.T13||']'; END
        IF(OLD.T14 <> NEW.T14) THEN BEGIN HISTORICO = HISTORICO||' ['||:G14||':'||OLD.T14||'->'||NEW.T14||']'; END
        IF(OLD.T15 <> NEW.T15) THEN BEGIN HISTORICO = HISTORICO||' ['||:G15||':'||OLD.T15||'->'||NEW.T15||']'; END
        IF(OLD.T16 <> NEW.T16) THEN BEGIN HISTORICO = HISTORICO||' ['||:G16||':'||OLD.T16||'->'||NEW.T16||']'; END
        IF(OLD.T17 <> NEW.T17) THEN BEGIN HISTORICO = HISTORICO||' ['||:G17||':'||OLD.T17||'->'||NEW.T17||']'; END
        IF(OLD.T18 <> NEW.T18) THEN BEGIN HISTORICO = HISTORICO||' ['||:G18||':'||OLD.T18||'->'||NEW.T18||']'; END
        IF(OLD.T19 <> NEW.T19) THEN BEGIN HISTORICO = HISTORICO||' ['||:G19||':'||OLD.T19||'->'||NEW.T19||']'; END
        IF(OLD.T20 <> NEW.T20) THEN BEGIN HISTORICO = HISTORICO||' ['||:G20||':'||OLD.T20||'->'||NEW.T20||']'; END
        END


        IF (HISTORICO <> '') THEN
        BEGIN
           HISTORICO = 'CONS.PARAM. ID:'||NEW.TIPO_CODIGO||' ALTERADO'||FN_ELIPSES(HISTORICO,500);
        --HISTORICO = 'Registro da Ficha Téc(1) alterado '||FN_LPAD(NEW.TIPO_CODIGO,4,'0')||', Alterada: '||HISTORICO;
           EXECUTE PROCEDURE SPI_HISTORICO('TBMODELO', NEW.MODELO_CODIGO, HISTORICO);
        END
    END
END;

CREATE OR ALTER TRIGGER TTBMODELO_FLUXO_PRODUCAO_B05IU FOR TBMODELO_FLUXO_PRODUCAO
ACTIVE BEFORE INSERT OR UPDATE POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(3000);
DECLARE VARIABLE Grade Integer;
DECLARE VARIABLE G01 VarChar(10); DECLARE VARIABLE G02 VarChar(10);
DECLARE VARIABLE G03 VarChar(10); DECLARE VARIABLE G04 VarChar(10);
DECLARE VARIABLE G05 VarChar(10); DECLARE VARIABLE G06 VarChar(10);
DECLARE VARIABLE G07 VarChar(10); DECLARE VARIABLE G08 VarChar(10);
DECLARE VARIABLE G09 VarChar(10); DECLARE VARIABLE G10 VarChar(10);
DECLARE VARIABLE G11 VarChar(10); DECLARE VARIABLE G12 VarChar(10);
DECLARE VARIABLE G13 VarChar(10); DECLARE VARIABLE G14 VarChar(10);
DECLARE VARIABLE G15 VarChar(10); DECLARE VARIABLE G16 VarChar(10);
DECLARE VARIABLE G17 VarChar(10); DECLARE VARIABLE G18 VarChar(10);
DECLARE VARIABLE G19 VarChar(10); DECLARE VARIABLE G20 VarChar(10);
BEGIN

    HISTORICO = '';
    IF(INSERTING) THEN
    BEGIN
       IF((SELECT FIRST 1 C.MODELO_ID FROM TBMODELO_FLUXO_PRODUCAO C WHERE C.MODELO_ID = NEW.MODELO_ID
       AND C.FLUXO_ID = NEW.FLUXO_ID AND C.COR_ID = NEW.COR_ID) IS NOT NULL) THEN
       BEGIN
            IF(NEW.TAMANHO = 0 AND NEW.TEMPO > 0)THEN
                BEGIN HISTORICO = HISTORICO||' T00(min):'||0||'->'||NEW.TEMPO;
            END
            IF(NEW.TAMANHO = 0 AND NEW.TM > 0)THEN
                BEGIN HISTORICO = HISTORICO||' T00(seg):'||0||'->'||NEW.TEMPO;
            END
            For
            Select FIRST 1 Coalesce(G.CODIGO,0),
                 Coalesce(G.T01,'T01'), Coalesce(G.T02,'T02'), Coalesce(G.T03,'T03'), Coalesce(G.T04,'T03'),
                 Coalesce(G.T05,'T05'), Coalesce(G.T06,'T06'), Coalesce(G.T07,'T07'), Coalesce(G.T08,'T07'),
                 Coalesce(G.T09,'T09'), Coalesce(G.T10,'T10'), Coalesce(G.T11,'T11'), Coalesce(G.T12,'T11'),
                 Coalesce(G.T13,'T13'), Coalesce(G.T14,'T14'), Coalesce(G.T15,'T15'), Coalesce(G.T16,'T15'),
                 Coalesce(G.T17,'T17'), Coalesce(G.T18,'T18'), Coalesce(G.T19,'T19'), Coalesce(G.T20,'T19')
            From TbGrade G WHERE G.Codigo = coalesce((select first 1 z.grade_codigo from tbmodelo z where z.codigo = new.MODELO_ID),0)
            Into :Grade,:G01,:G02,:G03,:G04,:G05,:G06,:G07,:G08,:G09,
             :G10,:G11,:G12,:G13,:G14,:G15,:G16,:G17,:G18,:G19,:G20
            DO
            BEGIN
            IF(NEW.TAMANHO = 1 AND NEW.TEMPO > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G01||'(min):'||0||'->'||NEW.TEMPO||']'; END
            IF(NEW.TAMANHO = 2 AND NEW.TEMPO > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G02||'(min):'||0||'->'||NEW.TEMPO||']'; END
            IF(NEW.TAMANHO = 3 AND NEW.TEMPO > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G03||'(min):'||0||'->'||NEW.TEMPO||']'; END
            IF(NEW.TAMANHO = 4 AND NEW.TEMPO > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G04||'(min):'||0||'->'||NEW.TEMPO||']'; END
            IF(NEW.TAMANHO = 5 AND NEW.TEMPO > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G05||'(min):'||0||'->'||NEW.TEMPO||']'; END
            IF(NEW.TAMANHO = 6 AND NEW.TEMPO > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G06||'(min):'||0||'->'||NEW.TEMPO||']'; END
            IF(NEW.TAMANHO = 7 AND NEW.TEMPO > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G07||'(min):'||0||'->'||NEW.TEMPO||']'; END
            IF(NEW.TAMANHO = 8 AND NEW.TEMPO > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G08||'(min):'||0||'->'||NEW.TEMPO||']'; END
            IF(NEW.TAMANHO = 9 AND NEW.TEMPO > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G09||'(min):'||0||'->'||NEW.TEMPO||']'; END
            IF(NEW.TAMANHO = 10 AND NEW.TEMPO > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G10||'(min):'||0||'->'||NEW.TEMPO||']'; END
            IF(NEW.TAMANHO = 11 AND NEW.TEMPO > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G11||'(min):'||0||'->'||NEW.TEMPO||']'; END
            IF(NEW.TAMANHO = 12 AND NEW.TEMPO > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G12||'(min):'||0||'->'||NEW.TEMPO||']'; END
            IF(NEW.TAMANHO = 13 AND NEW.TEMPO > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G13||'(min):'||0||'->'||NEW.TEMPO||']'; END
            IF(NEW.TAMANHO = 14 AND NEW.TEMPO > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G14||'(min):'||0||'->'||NEW.TEMPO||']'; END
            IF(NEW.TAMANHO = 15 AND NEW.TEMPO > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G15||'(min):'||0||'->'||NEW.TEMPO||']'; END
            IF(NEW.TAMANHO = 16 AND NEW.TEMPO > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G16||'(min):'||0||'->'||NEW.TEMPO||']'; END
            IF(NEW.TAMANHO = 17 AND NEW.TEMPO > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G17||'(min):'||0||'->'||NEW.TEMPO||']'; END
            IF(NEW.TAMANHO = 18 AND NEW.TEMPO > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G18||'(min):'||0||'->'||NEW.TEMPO||']'; END
            IF(NEW.TAMANHO = 19 AND NEW.TEMPO > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G19||'(min):'||0||'->'||NEW.TEMPO||']'; END
            IF(NEW.TAMANHO = 20 AND NEW.TEMPO > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G20||'(min):'||0||'->'||NEW.TEMPO||']'; END
            
            IF(NEW.TAMANHO = 1 AND NEW.TM > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G01||'(seg):'||0||'->'||NEW.TM||']'; END
            IF(NEW.TAMANHO = 2 AND NEW.TM > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G02||'(seg):'||0||'->'||NEW.TM||']'; END
            IF(NEW.TAMANHO = 3 AND NEW.TM > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G03||'(seg):'||0||'->'||NEW.TM||']'; END
            IF(NEW.TAMANHO = 4 AND NEW.TM > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G04||'(seg):'||0||'->'||NEW.TM||']'; END
            IF(NEW.TAMANHO = 5 AND NEW.TM > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G05||'(seg):'||0||'->'||NEW.TM||']'; END
            IF(NEW.TAMANHO = 6 AND NEW.TM > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G06||'(seg):'||0||'->'||NEW.TM||']'; END
            IF(NEW.TAMANHO = 7 AND NEW.TM > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G07||'(seg):'||0||'->'||NEW.TM||']'; END
            IF(NEW.TAMANHO = 8 AND NEW.TM > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G08||'(seg):'||0||'->'||NEW.TM||']'; END
            IF(NEW.TAMANHO = 9 AND NEW.TM > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G09||'(seg):'||0||'->'||NEW.TM||']'; END
            IF(NEW.TAMANHO = 10 AND NEW.TM > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G10||'(seg):'||0||'->'||NEW.TM||']'; END
            IF(NEW.TAMANHO = 11 AND NEW.TM > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G11||'(seg):'||0||'->'||NEW.TM||']'; END
            IF(NEW.TAMANHO = 12 AND NEW.TM > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G12||'(seg):'||0||'->'||NEW.TM||']'; END
            IF(NEW.TAMANHO = 13 AND NEW.TM > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G13||'(seg):'||0||'->'||NEW.TM||']'; END
            IF(NEW.TAMANHO = 14 AND NEW.TM > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G14||'(seg):'||0||'->'||NEW.TM||']'; END
            IF(NEW.TAMANHO = 15 AND NEW.TM > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G15||'(seg):'||0||'->'||NEW.TM||']'; END
            IF(NEW.TAMANHO = 16 AND NEW.TM > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G16||'(seg):'||0||'->'||NEW.TM||']'; END
            IF(NEW.TAMANHO = 17 AND NEW.TM > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G17||'(seg):'||0||'->'||NEW.TM||']'; END
            IF(NEW.TAMANHO = 18 AND NEW.TM > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G18||'(seg):'||0||'->'||NEW.TM||']'; END
            IF(NEW.TAMANHO = 19 AND NEW.TM > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G19||'(seg):'||0||'->'||NEW.TM||']'; END
            IF(NEW.TAMANHO = 20 AND NEW.TM > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G20||'(seg):'||0||'->'||NEW.TM||']'; END
            END

            IF (HISTORICO <> '') THEN
            BEGIN
               HISTORICO = ' FLUXO DE PRODUCAO:'||new.FLUXO_ID||', COR: '||IIF(NEW.COR_ID > 0,(select first 1 NEW.COR_ID||' - '||a.descricao from TBCOR a where a.CODIGO = new.COR_ID),'*** PADRÃO ***')||' ALTERADO '||HISTORICO;
               EXECUTE PROCEDURE SPI_HISTORICO('TBMODELO', NEW.MODELO_ID, HISTORICO);
            END
       END
       ELSE
       BEGIN
           HISTORICO = HISTORICO||' FLUXO DE PRODUCAO:'||new.FLUXO_ID||', COR: '||IIF(NEW.COR_ID > 0,(select first 1 NEW.COR_ID||' - '||a.descricao from TBCOR a where a.CODIGO = new.COR_ID),'*** PADRÃO ***')||' INCLUIDA.';
           EXECUTE PROCEDURE SPI_HISTORICO('TBMODELO', NEW.MODELO_ID, HISTORICO);
       END
    END

    IF(UPDATING) THEN
    BEGIN
        IF(NEW.TAMANHO = 0 AND OLD.TEMPO <> NEW.TEMPO)THEN
            BEGIN HISTORICO = HISTORICO||' T00(min):'||OLD.TEMPO||'->'||NEW.TEMPO;
        END
        IF(NEW.TAMANHO = 0 AND OLD.TM <> NEW.TM)THEN
            BEGIN HISTORICO = HISTORICO||' T00(seg):'||OLD.TM||'->'||NEW.TM;
        END
       For
        Select FIRST 1 Coalesce(G.CODIGO,0),
             Coalesce(G.T01,'T01'), Coalesce(G.T02,'T02'), Coalesce(G.T03,'T03'), Coalesce(G.T04,'T03'),
             Coalesce(G.T05,'T05'), Coalesce(G.T06,'T06'), Coalesce(G.T07,'T07'), Coalesce(G.T08,'T07'),
             Coalesce(G.T09,'T09'), Coalesce(G.T10,'T10'), Coalesce(G.T11,'T11'), Coalesce(G.T12,'T11'),
             Coalesce(G.T13,'T13'), Coalesce(G.T14,'T14'), Coalesce(G.T15,'T15'), Coalesce(G.T16,'T15'),
             Coalesce(G.T17,'T17'), Coalesce(G.T18,'T18'), Coalesce(G.T19,'T19'), Coalesce(G.T20,'T19')
        From TbGrade G WHERE G.Codigo =  coalesce((select first 1 z.grade_codigo from tbmodelo z where z.codigo = new.MODELO_ID),0)
        Into :Grade,:G01,:G02,:G03,:G04,:G05,:G06,:G07,:G08,:G09,
         :G10,:G11,:G12,:G13,:G14,:G15,:G16,:G17,:G18,:G19,:G20
        DO
        BEGIN
        IF(NEW.TAMANHO = 1 AND NEW.TEMPO <> OLD.TEMPO) THEN BEGIN HISTORICO = HISTORICO||' ['||:G01||'(min):'||OLD.TEMPO||'->'||NEW.TEMPO||']'; END
        IF(NEW.TAMANHO = 2 AND NEW.TEMPO <> OLD.TEMPO) THEN BEGIN HISTORICO = HISTORICO||' ['||:G02||'(min):'||OLD.TEMPO||'->'||NEW.TEMPO||']'; END
        IF(NEW.TAMANHO = 3 AND NEW.TEMPO <> OLD.TEMPO) THEN BEGIN HISTORICO = HISTORICO||' ['||:G03||'(min):'||OLD.TEMPO||'->'||NEW.TEMPO||']'; END
        IF(NEW.TAMANHO = 4 AND NEW.TEMPO <> OLD.TEMPO) THEN BEGIN HISTORICO = HISTORICO||' ['||:G04||'(min):'||OLD.TEMPO||'->'||NEW.TEMPO||']'; END
        IF(NEW.TAMANHO = 5 AND NEW.TEMPO <> OLD.TEMPO) THEN BEGIN HISTORICO = HISTORICO||' ['||:G05||'(min):'||OLD.TEMPO||'->'||NEW.TEMPO||']'; END
        IF(NEW.TAMANHO = 6 AND NEW.TEMPO <> OLD.TEMPO) THEN BEGIN HISTORICO = HISTORICO||' ['||:G06||'(min):'||OLD.TEMPO||'->'||NEW.TEMPO||']'; END
        IF(NEW.TAMANHO = 7 AND NEW.TEMPO <> OLD.TEMPO) THEN BEGIN HISTORICO = HISTORICO||' ['||:G07||'(min):'||OLD.TEMPO||'->'||NEW.TEMPO||']'; END
        IF(NEW.TAMANHO = 8 AND NEW.TEMPO <> OLD.TEMPO) THEN BEGIN HISTORICO = HISTORICO||' ['||:G08||'(min):'||OLD.TEMPO||'->'||NEW.TEMPO||']'; END
        IF(NEW.TAMANHO = 9 AND NEW.TEMPO <> OLD.TEMPO) THEN BEGIN HISTORICO = HISTORICO||' ['||:G09||'(min):'||OLD.TEMPO||'->'||NEW.TEMPO||']'; END
        IF(NEW.TAMANHO = 10 AND NEW.TEMPO <> OLD.TEMPO) THEN BEGIN HISTORICO = HISTORICO||' ['||:G10||'(min):'||OLD.TEMPO||'->'||NEW.TEMPO||']'; END
        IF(NEW.TAMANHO = 11 AND NEW.TEMPO <> OLD.TEMPO) THEN BEGIN HISTORICO = HISTORICO||' ['||:G11||'(min):'||OLD.TEMPO||'->'||NEW.TEMPO||']'; END
        IF(NEW.TAMANHO = 12 AND NEW.TEMPO <> OLD.TEMPO) THEN BEGIN HISTORICO = HISTORICO||' ['||:G12||'(min):'||OLD.TEMPO||'->'||NEW.TEMPO||']'; END
        IF(NEW.TAMANHO = 13 AND NEW.TEMPO <> OLD.TEMPO) THEN BEGIN HISTORICO = HISTORICO||' ['||:G13||'(min):'||OLD.TEMPO||'->'||NEW.TEMPO||']'; END
        IF(NEW.TAMANHO = 14 AND NEW.TEMPO <> OLD.TEMPO) THEN BEGIN HISTORICO = HISTORICO||' ['||:G14||'(min):'||OLD.TEMPO||'->'||NEW.TEMPO||']'; END
        IF(NEW.TAMANHO = 15 AND NEW.TEMPO <> OLD.TEMPO) THEN BEGIN HISTORICO = HISTORICO||' ['||:G15||'(min):'||OLD.TEMPO||'->'||NEW.TEMPO||']'; END
        IF(NEW.TAMANHO = 16 AND NEW.TEMPO <> OLD.TEMPO) THEN BEGIN HISTORICO = HISTORICO||' ['||:G16||'(min):'||OLD.TEMPO||'->'||NEW.TEMPO||']'; END
        IF(NEW.TAMANHO = 17 AND NEW.TEMPO <> OLD.TEMPO) THEN BEGIN HISTORICO = HISTORICO||' ['||:G17||'(min):'||OLD.TEMPO||'->'||NEW.TEMPO||']'; END
        IF(NEW.TAMANHO = 18 AND NEW.TEMPO <> OLD.TEMPO) THEN BEGIN HISTORICO = HISTORICO||' ['||:G18||'(min):'||OLD.TEMPO||'->'||NEW.TEMPO||']'; END
        IF(NEW.TAMANHO = 19 AND NEW.TEMPO <> OLD.TEMPO) THEN BEGIN HISTORICO = HISTORICO||' ['||:G19||'(min):'||OLD.TEMPO||'->'||NEW.TEMPO||']'; END
        IF(NEW.TAMANHO = 20 AND NEW.TEMPO <> OLD.TEMPO) THEN BEGIN HISTORICO = HISTORICO||' ['||:G20||'(min):'||OLD.TEMPO||'->'||NEW.TEMPO||']'; END

        IF(NEW.TAMANHO = 1 AND NEW.TM <> OLD.TM) THEN BEGIN HISTORICO = HISTORICO||' ['||:G01||'(seg):'||OLD.TM||'->'||NEW.TM||']'; END
        IF(NEW.TAMANHO = 2 AND NEW.TM <> OLD.TM) THEN BEGIN HISTORICO = HISTORICO||' ['||:G02||'(seg):'||OLD.TM||'->'||NEW.TM||']'; END
        IF(NEW.TAMANHO = 3 AND NEW.TM <> OLD.TM) THEN BEGIN HISTORICO = HISTORICO||' ['||:G03||'(seg):'||OLD.TM||'->'||NEW.TM||']'; END
        IF(NEW.TAMANHO = 4 AND NEW.TM <> OLD.TM) THEN BEGIN HISTORICO = HISTORICO||' ['||:G04||'(seg):'||OLD.TM||'->'||NEW.TM||']'; END
        IF(NEW.TAMANHO = 5 AND NEW.TM <> OLD.TM) THEN BEGIN HISTORICO = HISTORICO||' ['||:G05||'(seg):'||OLD.TM||'->'||NEW.TM||']'; END
        IF(NEW.TAMANHO = 6 AND NEW.TM <> OLD.TM) THEN BEGIN HISTORICO = HISTORICO||' ['||:G06||'(seg):'||OLD.TM||'->'||NEW.TM||']'; END
        IF(NEW.TAMANHO = 7 AND NEW.TM <> OLD.TM) THEN BEGIN HISTORICO = HISTORICO||' ['||:G07||'(seg):'||OLD.TM||'->'||NEW.TM||']'; END
        IF(NEW.TAMANHO = 8 AND NEW.TM <> OLD.TM) THEN BEGIN HISTORICO = HISTORICO||' ['||:G08||'(seg):'||OLD.TM||'->'||NEW.TM||']'; END
        IF(NEW.TAMANHO = 9 AND NEW.TM <> OLD.TM) THEN BEGIN HISTORICO = HISTORICO||' ['||:G09||'(seg):'||OLD.TM||'->'||NEW.TM||']'; END
        IF(NEW.TAMANHO = 10 AND NEW.TM <> OLD.TM) THEN BEGIN HISTORICO = HISTORICO||' ['||:G10||'(seg):'||OLD.TM||'->'||NEW.TM||']'; END
        IF(NEW.TAMANHO = 11 AND NEW.TM <> OLD.TM) THEN BEGIN HISTORICO = HISTORICO||' ['||:G11||'(seg):'||OLD.TM||'->'||NEW.TM||']'; END
        IF(NEW.TAMANHO = 12 AND NEW.TM <> OLD.TM) THEN BEGIN HISTORICO = HISTORICO||' ['||:G12||'(seg):'||OLD.TM||'->'||NEW.TM||']'; END
        IF(NEW.TAMANHO = 13 AND NEW.TM <> OLD.TM) THEN BEGIN HISTORICO = HISTORICO||' ['||:G13||'(seg):'||OLD.TM||'->'||NEW.TM||']'; END
        IF(NEW.TAMANHO = 14 AND NEW.TM <> OLD.TM) THEN BEGIN HISTORICO = HISTORICO||' ['||:G14||'(seg):'||OLD.TM||'->'||NEW.TM||']'; END
        IF(NEW.TAMANHO = 15 AND NEW.TM <> OLD.TM) THEN BEGIN HISTORICO = HISTORICO||' ['||:G15||'(seg):'||OLD.TM||'->'||NEW.TM||']'; END
        IF(NEW.TAMANHO = 16 AND NEW.TM <> OLD.TM) THEN BEGIN HISTORICO = HISTORICO||' ['||:G16||'(seg):'||OLD.TM||'->'||NEW.TM||']'; END
        IF(NEW.TAMANHO = 17 AND NEW.TM <> OLD.TM) THEN BEGIN HISTORICO = HISTORICO||' ['||:G17||'(seg):'||OLD.TM||'->'||NEW.TM||']'; END
        IF(NEW.TAMANHO = 18 AND NEW.TM <> OLD.TM) THEN BEGIN HISTORICO = HISTORICO||' ['||:G18||'(seg):'||OLD.TM||'->'||NEW.TM||']'; END
        IF(NEW.TAMANHO = 19 AND NEW.TM <> OLD.TM) THEN BEGIN HISTORICO = HISTORICO||' ['||:G19||'(seg):'||OLD.TM||'->'||NEW.TM||']'; END
        IF(NEW.TAMANHO = 20 AND NEW.TM <> OLD.TM) THEN BEGIN HISTORICO = HISTORICO||' ['||:G20||'(seg):'||OLD.TM||'->'||NEW.TM||']'; END
        END
        IF(HISTORICO <> '') THEN
        BEGIN
           HISTORICO = ' FLUXO DE PRODUCAO:'||new.FLUXO_ID||', COR: '||IIF(NEW.COR_ID > 0,(select first 1 NEW.COR_ID||' - '||a.descricao from TBCOR a where a.CODIGO = new.COR_ID),'*** PADRÃO ***')||' ALTERADO '||HISTORICO;
           EXECUTE PROCEDURE SPI_HISTORICO('TBMODELO', NEW.MODELO_ID, HISTORICO);
        END
    END


END;

CREATE OR ALTER TRIGGER TTBMODELO_FLUXO_PRODUCAO_B03D FOR TBMODELO_FLUXO_PRODUCAO
ACTIVE BEFORE DELETE POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(8000);
DECLARE VARIABLE Grade Integer;
DECLARE VARIABLE G01 VarChar(10); DECLARE VARIABLE G02 VarChar(10);
DECLARE VARIABLE G03 VarChar(10); DECLARE VARIABLE G04 VarChar(10);
DECLARE VARIABLE G05 VarChar(10); DECLARE VARIABLE G06 VarChar(10);
DECLARE VARIABLE G07 VarChar(10); DECLARE VARIABLE G08 VarChar(10);
DECLARE VARIABLE G09 VarChar(10); DECLARE VARIABLE G10 VarChar(10);
DECLARE VARIABLE G11 VarChar(10); DECLARE VARIABLE G12 VarChar(10);
DECLARE VARIABLE G13 VarChar(10); DECLARE VARIABLE G14 VarChar(10);
DECLARE VARIABLE G15 VarChar(10); DECLARE VARIABLE G16 VarChar(10);
DECLARE VARIABLE G17 VarChar(10); DECLARE VARIABLE G18 VarChar(10);
DECLARE VARIABLE G19 VarChar(10); DECLARE VARIABLE G20 VarChar(10);
BEGIN

    HISTORICO = '';

    IF(OLD.MODELO_ID > 0) THEN
    BEGIN
        IF(OLD.TAMANHO =  0 AND OLD.TEMPO IS NOT NULL)THEN BEGIN HISTORICO = HISTORICO||' T00(min)'; END
        IF(OLD.TAMANHO =  0 AND OLD.TM IS NOT NULL)THEN BEGIN HISTORICO = HISTORICO||' T00(seg)'; END
        For
            SELECT FIRST 1 Coalesce(G.CODIGO,0),
                 Coalesce(G.T01,'T01'), Coalesce(G.T02,'T02'), Coalesce(G.T03,'T03'), Coalesce(G.T04,'T03'),
                 Coalesce(G.T05,'T05'), Coalesce(G.T06,'T06'), Coalesce(G.T07,'T07'), Coalesce(G.T08,'T07'),
                 Coalesce(G.T09,'T09'), Coalesce(G.T10,'T10'), Coalesce(G.T11,'T11'), Coalesce(G.T12,'T11'),
                 Coalesce(G.T13,'T13'), Coalesce(G.T14,'T14'), Coalesce(G.T15,'T15'), Coalesce(G.T16,'T15'),
                 Coalesce(G.T17,'T17'), Coalesce(G.T18,'T18'), Coalesce(G.T19,'T19'), Coalesce(G.T20,'T19')
        From TbGrade G WHERE G.Codigo = coalesce((select first 1 z.grade_codigo from tbmodelo z where z.codigo = old.MODELO_ID),0)
        Into :Grade,:G01,:G02,:G03,:G04,:G05,:G06,:G07,:G08,:G09,
         :G10,:G11,:G12,:G13,:G14,:G15,:G16,:G17,:G18,:G19,:G20
        DO
        BEGIN
        IF(OLD.TAMANHO = 1 AND OLD.TEMPO IS NOT NULL) THEN BEGIN HISTORICO = HISTORICO||' '||:G01||'(min)'; END
        IF(OLD.TAMANHO = 2 AND OLD.TEMPO IS NOT NULL) THEN BEGIN HISTORICO = HISTORICO||' '||:G02||'(min)'; END
        IF(OLD.TAMANHO = 3 AND OLD.TEMPO IS NOT NULL) THEN BEGIN HISTORICO = HISTORICO||' '||:G03||'(min)'; END
        IF(OLD.TAMANHO = 4 AND OLD.TEMPO IS NOT NULL) THEN BEGIN HISTORICO = HISTORICO||' '||:G04||'(min)'; END
        IF(OLD.TAMANHO = 5 AND OLD.TEMPO IS NOT NULL) THEN BEGIN HISTORICO = HISTORICO||' '||:G05||'(min)'; END
        IF(OLD.TAMANHO = 6 AND OLD.TEMPO IS NOT NULL) THEN BEGIN HISTORICO = HISTORICO||' '||:G06||'(min)'; END
        IF(OLD.TAMANHO = 7 AND OLD.TEMPO IS NOT NULL) THEN BEGIN HISTORICO = HISTORICO||' '||:G07||'(min)'; END
        IF(OLD.TAMANHO = 8 AND OLD.TEMPO IS NOT NULL) THEN BEGIN HISTORICO = HISTORICO||' '||:G08||'(min)'; END
        IF(OLD.TAMANHO = 9 AND OLD.TEMPO IS NOT NULL) THEN BEGIN HISTORICO = HISTORICO||' '||:G09||'(min)'; END
        IF(OLD.TAMANHO = 10 AND OLD.TEMPO IS NOT NULL) THEN BEGIN HISTORICO = HISTORICO||' '||:G10||'(min)'; END
        IF(OLD.TAMANHO = 11 AND OLD.TEMPO IS NOT NULL) THEN BEGIN HISTORICO = HISTORICO||' '||:G11||'(min)'; END
        IF(OLD.TAMANHO = 12 AND OLD.TEMPO IS NOT NULL) THEN BEGIN HISTORICO = HISTORICO||' '||:G12||'(min)'; END
        IF(OLD.TAMANHO = 13 AND OLD.TEMPO IS NOT NULL) THEN BEGIN HISTORICO = HISTORICO||' '||:G13||'(min)'; END
        IF(OLD.TAMANHO = 14 AND OLD.TEMPO IS NOT NULL) THEN BEGIN HISTORICO = HISTORICO||' '||:G14||'(min)'; END
        IF(OLD.TAMANHO = 15 AND OLD.TEMPO IS NOT NULL) THEN BEGIN HISTORICO = HISTORICO||' '||:G15||'(min)'; END
        IF(OLD.TAMANHO = 16 AND OLD.TEMPO IS NOT NULL) THEN BEGIN HISTORICO = HISTORICO||' '||:G16||'(min)'; END
        IF(OLD.TAMANHO = 17 AND OLD.TEMPO IS NOT NULL) THEN BEGIN HISTORICO = HISTORICO||' '||:G17||'(min)'; END
        IF(OLD.TAMANHO = 18 AND OLD.TEMPO IS NOT NULL) THEN BEGIN HISTORICO = HISTORICO||' '||:G18||'(min)'; END
        IF(OLD.TAMANHO = 19 AND OLD.TEMPO IS NOT NULL) THEN BEGIN HISTORICO = HISTORICO||' '||:G19||'(min)'; END
        IF(OLD.TAMANHO = 20 AND OLD.TEMPO IS NOT NULL) THEN BEGIN HISTORICO = HISTORICO||' '||:G20||'(min)'; END
        
        IF(OLD.TAMANHO = 1 AND OLD.TM IS NOT NULL) THEN BEGIN HISTORICO = HISTORICO||' '||:G01||'(seg)'; END
        IF(OLD.TAMANHO = 2 AND OLD.TM IS NOT NULL) THEN BEGIN HISTORICO = HISTORICO||' '||:G02||'(seg)'; END
        IF(OLD.TAMANHO = 3 AND OLD.TM IS NOT NULL) THEN BEGIN HISTORICO = HISTORICO||' '||:G03||'(seg)'; END
        IF(OLD.TAMANHO = 4 AND OLD.TM IS NOT NULL) THEN BEGIN HISTORICO = HISTORICO||' '||:G04||'(seg)'; END
        IF(OLD.TAMANHO = 5 AND OLD.TM IS NOT NULL) THEN BEGIN HISTORICO = HISTORICO||' '||:G05||'(seg)'; END
        IF(OLD.TAMANHO = 6 AND OLD.TM IS NOT NULL) THEN BEGIN HISTORICO = HISTORICO||' '||:G06||'(seg)'; END
        IF(OLD.TAMANHO = 7 AND OLD.TM IS NOT NULL) THEN BEGIN HISTORICO = HISTORICO||' '||:G07||'(seg)'; END
        IF(OLD.TAMANHO = 8 AND OLD.TM IS NOT NULL) THEN BEGIN HISTORICO = HISTORICO||' '||:G08||'(seg)'; END
        IF(OLD.TAMANHO = 9 AND OLD.TM IS NOT NULL) THEN BEGIN HISTORICO = HISTORICO||' '||:G09||'(seg)'; END
        IF(OLD.TAMANHO = 10 AND OLD.TM IS NOT NULL) THEN BEGIN HISTORICO = HISTORICO||' '||:G10||'(seg)'; END
        IF(OLD.TAMANHO = 11 AND OLD.TM IS NOT NULL) THEN BEGIN HISTORICO = HISTORICO||' '||:G11||'(seg)'; END
        IF(OLD.TAMANHO = 12 AND OLD.TM IS NOT NULL) THEN BEGIN HISTORICO = HISTORICO||' '||:G12||'(seg)'; END
        IF(OLD.TAMANHO = 13 AND OLD.TM IS NOT NULL) THEN BEGIN HISTORICO = HISTORICO||' '||:G13||'(seg)'; END
        IF(OLD.TAMANHO = 14 AND OLD.TM IS NOT NULL) THEN BEGIN HISTORICO = HISTORICO||' '||:G14||'(seg)'; END
        IF(OLD.TAMANHO = 15 AND OLD.TM IS NOT NULL) THEN BEGIN HISTORICO = HISTORICO||' '||:G15||'(seg)'; END
        IF(OLD.TAMANHO = 16 AND OLD.TM IS NOT NULL) THEN BEGIN HISTORICO = HISTORICO||' '||:G16||'(seg)'; END
        IF(OLD.TAMANHO = 17 AND OLD.TM IS NOT NULL) THEN BEGIN HISTORICO = HISTORICO||' '||:G17||'(seg)'; END
        IF(OLD.TAMANHO = 18 AND OLD.TM IS NOT NULL) THEN BEGIN HISTORICO = HISTORICO||' '||:G18||'(seg)'; END
        IF(OLD.TAMANHO = 19 AND OLD.TM IS NOT NULL) THEN BEGIN HISTORICO = HISTORICO||' '||:G19||'(seg)'; END
        IF(OLD.TAMANHO = 20 AND OLD.TM IS NOT NULL) THEN BEGIN HISTORICO = HISTORICO||' '||:G20||'(seg)'; END
        END
       HISTORICO = ' FLUXO DE PRODUCAO:'||IIF(old.FLUXO_ID > 0,(select first 1 old.fluxo_id||' - '||b.descricao from TBFLUXO_PRODUCAO b where b.ID = old.FLUXO_ID),old.FLUXO_ID)||', COR: '||IIF(old.COR_ID > 0,(select first 1 old.COR_ID||' - '||a.descricao from TBCOR a where a.CODIGO = old.COR_ID),'*** PADRÃO ***')||HISTORICO||' EXCLUIDO(S).';
    END
    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBMODELO', OLD.MODELO_ID, HISTORICO);
    END
END;

CREATE OR ALTER TRIGGER TTBMODELO_FICHA_TECNICA_B02U FOR TBMODELO_FICHA_TECNICA
ACTIVE BEFORE INSERT OR UPDATE POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(8000);
DECLARE VARIABLE Grade Integer;
DECLARE VARIABLE G01 VarChar(10); DECLARE VARIABLE G02 VarChar(10);
DECLARE VARIABLE G03 VarChar(10); DECLARE VARIABLE G04 VarChar(10);
DECLARE VARIABLE G05 VarChar(10); DECLARE VARIABLE G06 VarChar(10);
DECLARE VARIABLE G07 VarChar(10); DECLARE VARIABLE G08 VarChar(10);
DECLARE VARIABLE G09 VarChar(10); DECLARE VARIABLE G10 VarChar(10);
DECLARE VARIABLE G11 VarChar(10); DECLARE VARIABLE G12 VarChar(10);
DECLARE VARIABLE G13 VarChar(10); DECLARE VARIABLE G14 VarChar(10);
DECLARE VARIABLE G15 VarChar(10); DECLARE VARIABLE G16 VarChar(10);
DECLARE VARIABLE G17 VarChar(10); DECLARE VARIABLE G18 VarChar(10);
DECLARE VARIABLE G19 VarChar(10); DECLARE VARIABLE G20 VarChar(10);
BEGIN
  
    HISTORICO = '';
    IF(INSERTING) THEN
    BEGIN
        IF(NEW.OBSERVACAO IS NOT NULL AND NEW.OBSERVACAO <> '') THEN
        BEGIN
            HISTORICO = HISTORICO||' OBS: '||''||' -> '||NEW.OBSERVACAO;
        END

        IF (0 <> NEW.QUANTIDADE) THEN
        BEGIN
            HISTORICO = HISTORICO||' QTD:'||0||'->'||NEW.QUANTIDADE;
        END
        
        For
            SELECT FIRST 1 Coalesce(G.CODIGO,0),
                 Coalesce(G.T01,'T01'), Coalesce(G.T02,'T02'), Coalesce(G.T03,'T03'), Coalesce(G.T04,'T03'),
                 Coalesce(G.T05,'T05'), Coalesce(G.T06,'T06'), Coalesce(G.T07,'T07'), Coalesce(G.T08,'T07'),
                 Coalesce(G.T09,'T09'), Coalesce(G.T10,'T10'), Coalesce(G.T11,'T11'), Coalesce(G.T12,'T11'),
                 Coalesce(G.T13,'T13'), Coalesce(G.T14,'T14'), Coalesce(G.T15,'T15'), Coalesce(G.T16,'T15'),
                 Coalesce(G.T17,'T17'), Coalesce(G.T18,'T18'), Coalesce(G.T19,'T19'), Coalesce(G.T20,'T19')
        From TbGrade G WHERE G.Codigo = coalesce((select first 1 z.grade_codigo from tbmodelo z where z.codigo = new.MODELO_CODIGO),0)
        Into :Grade,:G01,:G02,:G03,:G04,:G05,:G06,:G07,:G08,:G09,
         :G10,:G11,:G12,:G13,:G14,:G15,:G16,:G17,:G18,:G19,:G20
        DO
        BEGIN
        IF(0 <> NEW.T01) THEN BEGIN HISTORICO = HISTORICO||' ['||:G01||':'||0||'->'||NEW.T01||']'; END
        IF(0 <> NEW.T02) THEN BEGIN HISTORICO = HISTORICO||' ['||:G02||':'||0||'->'||NEW.T02||']'; END
        IF(0 <> NEW.T03) THEN BEGIN HISTORICO = HISTORICO||' ['||:G03||':'||0||'->'||NEW.T03||']'; END
        IF(0 <> NEW.T04) THEN BEGIN HISTORICO = HISTORICO||' ['||:G04||':'||0||'->'||NEW.T04||']'; END
        IF(0 <> NEW.T05) THEN BEGIN HISTORICO = HISTORICO||' ['||:G05||':'||0||'->'||NEW.T05||']'; END
        IF(0 <> NEW.T06) THEN BEGIN HISTORICO = HISTORICO||' ['||:G06||':'||0||'->'||NEW.T06||']'; END
        IF(0 <> NEW.T07) THEN BEGIN HISTORICO = HISTORICO||' ['||:G07||':'||0||'->'||NEW.T07||']'; END
        IF(0 <> NEW.T08) THEN BEGIN HISTORICO = HISTORICO||' ['||:G08||':'||0||'->'||NEW.T08||']'; END
        IF(0 <> NEW.T09) THEN BEGIN HISTORICO = HISTORICO||' ['||:G09||':'||0||'->'||NEW.T09||']'; END
        IF(0 <> NEW.T10) THEN BEGIN HISTORICO = HISTORICO||' ['||:G10||':'||0||'->'||NEW.T10||']'; END
        IF(0 <> NEW.T11) THEN BEGIN HISTORICO = HISTORICO||' ['||:G11||':'||0||'->'||NEW.T11||']'; END
        IF(0 <> NEW.T12) THEN BEGIN HISTORICO = HISTORICO||' ['||:G12||':'||0||'->'||NEW.T12||']'; END
        IF(0 <> NEW.T13) THEN BEGIN HISTORICO = HISTORICO||' ['||:G13||':'||0||'->'||NEW.T13||']'; END
        IF(0 <> NEW.T14) THEN BEGIN HISTORICO = HISTORICO||' ['||:G14||':'||0||'->'||NEW.T14||']'; END
        IF(0 <> NEW.T15) THEN BEGIN HISTORICO = HISTORICO||' ['||:G15||':'||0||'->'||NEW.T15||']'; END
        IF(0 <> NEW.T16) THEN BEGIN HISTORICO = HISTORICO||' ['||:G16||':'||0||'->'||NEW.T16||']'; END
        IF(0 <> NEW.T17) THEN BEGIN HISTORICO = HISTORICO||' ['||:G17||':'||0||'->'||NEW.T17||']'; END
        IF(0 <> NEW.T18) THEN BEGIN HISTORICO = HISTORICO||' ['||:G18||':'||0||'->'||NEW.T18||']'; END
        IF(0 <> NEW.T19) THEN BEGIN HISTORICO = HISTORICO||' ['||:G19||':'||0||'->'||NEW.T19||']'; END
        IF(0 <> NEW.T20) THEN BEGIN HISTORICO = HISTORICO||' ['||:G20||':'||0||'->'||NEW.T20||']'; END
        END

        IF (HISTORICO <> '') THEN
        BEGIN
           HISTORICO = 'CONS.PARAM. ID:'||NEW.TIPO_CODIGO||' ALTERADO'||HISTORICO;
        --HISTORICO = 'Registro da Ficha Téc(1) alterado '||FN_LPAD(NEW.TIPO_CODIGO,4,'0')||', Alterada: '||HISTORICO;
           EXECUTE PROCEDURE SPI_HISTORICO('TBMODELO', NEW.MODELO_CODIGO,FN_ELIPSES(HISTORICO,500));
        END
    END
    ELSE
    BEGIN
        IF (OLD.OBSERVACAO <> NEW.OBSERVACAO) THEN
        BEGIN
            HISTORICO = HISTORICO||' OBS: '||OLD.OBSERVACAO||' -> '||NEW.OBSERVACAO;
        END
        
        IF (OLD.QUANTIDADE <> NEW.QUANTIDADE) THEN
        BEGIN
            HISTORICO = HISTORICO||' QTD:'||OLD.QUANTIDADE||'->'||NEW.QUANTIDADE;
        END
        
        For
        SELECT FIRST 1 Coalesce(G.CODIGO,0),
                 Coalesce(G.T01,'T01'), Coalesce(G.T02,'T02'), Coalesce(G.T03,'T03'), Coalesce(G.T04,'T03'),
                 Coalesce(G.T05,'T05'), Coalesce(G.T06,'T06'), Coalesce(G.T07,'T07'), Coalesce(G.T08,'T07'),
                 Coalesce(G.T09,'T09'), Coalesce(G.T10,'T10'), Coalesce(G.T11,'T11'), Coalesce(G.T12,'T11'),
                 Coalesce(G.T13,'T13'), Coalesce(G.T14,'T14'), Coalesce(G.T15,'T15'), Coalesce(G.T16,'T15'),
                 Coalesce(G.T17,'T17'), Coalesce(G.T18,'T18'), Coalesce(G.T19,'T19'), Coalesce(G.T20,'T19')
        From TbGrade G WHERE G.Codigo = coalesce((select first 1 z.grade_codigo from tbmodelo z where z.codigo = new.MODELO_CODIGO),0)
        Into :Grade,:G01,:G02,:G03,:G04,:G05,:G06,:G07,:G08,:G09,
         :G10,:G11,:G12,:G13,:G14,:G15,:G16,:G17,:G18,:G19,:G20
        DO
        BEGIN
        IF(OLD.T01 <> NEW.T01) THEN BEGIN HISTORICO = HISTORICO||' ['||:G01||':'||OLD.T01||'->'||NEW.T01||']'; END
        IF(OLD.T02 <> NEW.T02) THEN BEGIN HISTORICO = HISTORICO||' ['||:G02||':'||OLD.T02||'->'||NEW.T02||']'; END
        IF(OLD.T03 <> NEW.T03) THEN BEGIN HISTORICO = HISTORICO||' ['||:G03||':'||OLD.T03||'->'||NEW.T03||']'; END
        IF(OLD.T04 <> NEW.T04) THEN BEGIN HISTORICO = HISTORICO||' ['||:G04||':'||OLD.T04||'->'||NEW.T04||']'; END
        IF(OLD.T05 <> NEW.T05) THEN BEGIN HISTORICO = HISTORICO||' ['||:G05||':'||OLD.T05||'->'||NEW.T05||']'; END
        IF(OLD.T06 <> NEW.T06) THEN BEGIN HISTORICO = HISTORICO||' ['||:G06||':'||OLD.T06||'->'||NEW.T06||']'; END
        IF(OLD.T07 <> NEW.T07) THEN BEGIN HISTORICO = HISTORICO||' ['||:G07||':'||OLD.T07||'->'||NEW.T07||']'; END
        IF(OLD.T08 <> NEW.T08) THEN BEGIN HISTORICO = HISTORICO||' ['||:G08||':'||OLD.T08||'->'||NEW.T08||']'; END
        IF(OLD.T09 <> NEW.T09) THEN BEGIN HISTORICO = HISTORICO||' ['||:G09||':'||OLD.T09||'->'||NEW.T09||']'; END
        IF(OLD.T10 <> NEW.T10) THEN BEGIN HISTORICO = HISTORICO||' ['||:G10||':'||OLD.T10||'->'||NEW.T10||']'; END
        IF(OLD.T11 <> NEW.T11) THEN BEGIN HISTORICO = HISTORICO||' ['||:G11||':'||OLD.T11||'->'||NEW.T11||']'; END
        IF(OLD.T12 <> NEW.T12) THEN BEGIN HISTORICO = HISTORICO||' ['||:G12||':'||OLD.T12||'->'||NEW.T12||']'; END
        IF(OLD.T13 <> NEW.T13) THEN BEGIN HISTORICO = HISTORICO||' ['||:G13||':'||OLD.T13||'->'||NEW.T13||']'; END
        IF(OLD.T14 <> NEW.T14) THEN BEGIN HISTORICO = HISTORICO||' ['||:G14||':'||OLD.T14||'->'||NEW.T14||']'; END
        IF(OLD.T15 <> NEW.T15) THEN BEGIN HISTORICO = HISTORICO||' ['||:G15||':'||OLD.T15||'->'||NEW.T15||']'; END
        IF(OLD.T16 <> NEW.T16) THEN BEGIN HISTORICO = HISTORICO||' ['||:G16||':'||OLD.T16||'->'||NEW.T16||']'; END
        IF(OLD.T17 <> NEW.T17) THEN BEGIN HISTORICO = HISTORICO||' ['||:G17||':'||OLD.T17||'->'||NEW.T17||']'; END
        IF(OLD.T18 <> NEW.T18) THEN BEGIN HISTORICO = HISTORICO||' ['||:G18||':'||OLD.T18||'->'||NEW.T18||']'; END
        IF(OLD.T19 <> NEW.T19) THEN BEGIN HISTORICO = HISTORICO||' ['||:G19||':'||OLD.T19||'->'||NEW.T19||']'; END
        IF(OLD.T20 <> NEW.T20) THEN BEGIN HISTORICO = HISTORICO||' ['||:G20||':'||OLD.T20||'->'||NEW.T20||']'; END
        END


        IF (HISTORICO <> '') THEN
        BEGIN
           HISTORICO = 'CONS.PARAM. ID:'||NEW.TIPO_CODIGO||' ALTERADO'||HISTORICO;
        --HISTORICO = 'Registro da Ficha Téc(1) alterado '||FN_LPAD(NEW.TIPO_CODIGO,4,'0')||', Alterada: '||HISTORICO;
           EXECUTE PROCEDURE SPI_HISTORICO('TBMODELO', NEW.MODELO_CODIGO, FN_ELIPSES(HISTORICO,500));
        END
    END
END;

CREATE OR ALTER TRIGGER TTBMODELO_FLUXO_PRODUCAO_B05IU FOR TBMODELO_FLUXO_PRODUCAO
ACTIVE BEFORE INSERT OR UPDATE POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(3000);
DECLARE VARIABLE Grade Integer;
DECLARE VARIABLE G01 VarChar(10); DECLARE VARIABLE G02 VarChar(10);
DECLARE VARIABLE G03 VarChar(10); DECLARE VARIABLE G04 VarChar(10);
DECLARE VARIABLE G05 VarChar(10); DECLARE VARIABLE G06 VarChar(10);
DECLARE VARIABLE G07 VarChar(10); DECLARE VARIABLE G08 VarChar(10);
DECLARE VARIABLE G09 VarChar(10); DECLARE VARIABLE G10 VarChar(10);
DECLARE VARIABLE G11 VarChar(10); DECLARE VARIABLE G12 VarChar(10);
DECLARE VARIABLE G13 VarChar(10); DECLARE VARIABLE G14 VarChar(10);
DECLARE VARIABLE G15 VarChar(10); DECLARE VARIABLE G16 VarChar(10);
DECLARE VARIABLE G17 VarChar(10); DECLARE VARIABLE G18 VarChar(10);
DECLARE VARIABLE G19 VarChar(10); DECLARE VARIABLE G20 VarChar(10);
BEGIN

    HISTORICO = '';
    IF(INSERTING) THEN
    BEGIN
       IF((SELECT FIRST 1 C.MODELO_ID FROM TBMODELO_FLUXO_PRODUCAO C WHERE C.MODELO_ID = NEW.MODELO_ID
       AND C.FLUXO_ID = NEW.FLUXO_ID AND C.COR_ID = NEW.COR_ID) IS NOT NULL) THEN
       BEGIN
            IF(NEW.TAMANHO = 0 AND NEW.TEMPO > 0)THEN
                BEGIN HISTORICO = HISTORICO||' T00(min):'||0||'->'||NEW.TEMPO;
            END
            IF(NEW.TAMANHO = 0 AND NEW.TM > 0)THEN
                BEGIN HISTORICO = HISTORICO||' T00(seg):'||0||'->'||NEW.TEMPO;
            END
            For
            Select FIRST 1 Coalesce(G.CODIGO,0),
                 Coalesce(G.T01,'T01'), Coalesce(G.T02,'T02'), Coalesce(G.T03,'T03'), Coalesce(G.T04,'T03'),
                 Coalesce(G.T05,'T05'), Coalesce(G.T06,'T06'), Coalesce(G.T07,'T07'), Coalesce(G.T08,'T07'),
                 Coalesce(G.T09,'T09'), Coalesce(G.T10,'T10'), Coalesce(G.T11,'T11'), Coalesce(G.T12,'T11'),
                 Coalesce(G.T13,'T13'), Coalesce(G.T14,'T14'), Coalesce(G.T15,'T15'), Coalesce(G.T16,'T15'),
                 Coalesce(G.T17,'T17'), Coalesce(G.T18,'T18'), Coalesce(G.T19,'T19'), Coalesce(G.T20,'T19')
            From TbGrade G WHERE G.Codigo = coalesce((select first 1 z.grade_codigo from tbmodelo z where z.codigo = new.MODELO_ID),0)
            Into :Grade,:G01,:G02,:G03,:G04,:G05,:G06,:G07,:G08,:G09,
             :G10,:G11,:G12,:G13,:G14,:G15,:G16,:G17,:G18,:G19,:G20
            DO
            BEGIN
            IF(NEW.TAMANHO = 1 AND NEW.TEMPO > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G01||'(min):'||0||'->'||NEW.TEMPO||']'; END
            IF(NEW.TAMANHO = 2 AND NEW.TEMPO > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G02||'(min):'||0||'->'||NEW.TEMPO||']'; END
            IF(NEW.TAMANHO = 3 AND NEW.TEMPO > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G03||'(min):'||0||'->'||NEW.TEMPO||']'; END
            IF(NEW.TAMANHO = 4 AND NEW.TEMPO > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G04||'(min):'||0||'->'||NEW.TEMPO||']'; END
            IF(NEW.TAMANHO = 5 AND NEW.TEMPO > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G05||'(min):'||0||'->'||NEW.TEMPO||']'; END
            IF(NEW.TAMANHO = 6 AND NEW.TEMPO > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G06||'(min):'||0||'->'||NEW.TEMPO||']'; END
            IF(NEW.TAMANHO = 7 AND NEW.TEMPO > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G07||'(min):'||0||'->'||NEW.TEMPO||']'; END
            IF(NEW.TAMANHO = 8 AND NEW.TEMPO > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G08||'(min):'||0||'->'||NEW.TEMPO||']'; END
            IF(NEW.TAMANHO = 9 AND NEW.TEMPO > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G09||'(min):'||0||'->'||NEW.TEMPO||']'; END
            IF(NEW.TAMANHO = 10 AND NEW.TEMPO > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G10||'(min):'||0||'->'||NEW.TEMPO||']'; END
            IF(NEW.TAMANHO = 11 AND NEW.TEMPO > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G11||'(min):'||0||'->'||NEW.TEMPO||']'; END
            IF(NEW.TAMANHO = 12 AND NEW.TEMPO > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G12||'(min):'||0||'->'||NEW.TEMPO||']'; END
            IF(NEW.TAMANHO = 13 AND NEW.TEMPO > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G13||'(min):'||0||'->'||NEW.TEMPO||']'; END
            IF(NEW.TAMANHO = 14 AND NEW.TEMPO > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G14||'(min):'||0||'->'||NEW.TEMPO||']'; END
            IF(NEW.TAMANHO = 15 AND NEW.TEMPO > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G15||'(min):'||0||'->'||NEW.TEMPO||']'; END
            IF(NEW.TAMANHO = 16 AND NEW.TEMPO > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G16||'(min):'||0||'->'||NEW.TEMPO||']'; END
            IF(NEW.TAMANHO = 17 AND NEW.TEMPO > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G17||'(min):'||0||'->'||NEW.TEMPO||']'; END
            IF(NEW.TAMANHO = 18 AND NEW.TEMPO > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G18||'(min):'||0||'->'||NEW.TEMPO||']'; END
            IF(NEW.TAMANHO = 19 AND NEW.TEMPO > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G19||'(min):'||0||'->'||NEW.TEMPO||']'; END
            IF(NEW.TAMANHO = 20 AND NEW.TEMPO > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G20||'(min):'||0||'->'||NEW.TEMPO||']'; END
            
            IF(NEW.TAMANHO = 1 AND NEW.TM > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G01||'(seg):'||0||'->'||NEW.TM||']'; END
            IF(NEW.TAMANHO = 2 AND NEW.TM > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G02||'(seg):'||0||'->'||NEW.TM||']'; END
            IF(NEW.TAMANHO = 3 AND NEW.TM > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G03||'(seg):'||0||'->'||NEW.TM||']'; END
            IF(NEW.TAMANHO = 4 AND NEW.TM > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G04||'(seg):'||0||'->'||NEW.TM||']'; END
            IF(NEW.TAMANHO = 5 AND NEW.TM > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G05||'(seg):'||0||'->'||NEW.TM||']'; END
            IF(NEW.TAMANHO = 6 AND NEW.TM > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G06||'(seg):'||0||'->'||NEW.TM||']'; END
            IF(NEW.TAMANHO = 7 AND NEW.TM > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G07||'(seg):'||0||'->'||NEW.TM||']'; END
            IF(NEW.TAMANHO = 8 AND NEW.TM > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G08||'(seg):'||0||'->'||NEW.TM||']'; END
            IF(NEW.TAMANHO = 9 AND NEW.TM > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G09||'(seg):'||0||'->'||NEW.TM||']'; END
            IF(NEW.TAMANHO = 10 AND NEW.TM > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G10||'(seg):'||0||'->'||NEW.TM||']'; END
            IF(NEW.TAMANHO = 11 AND NEW.TM > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G11||'(seg):'||0||'->'||NEW.TM||']'; END
            IF(NEW.TAMANHO = 12 AND NEW.TM > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G12||'(seg):'||0||'->'||NEW.TM||']'; END
            IF(NEW.TAMANHO = 13 AND NEW.TM > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G13||'(seg):'||0||'->'||NEW.TM||']'; END
            IF(NEW.TAMANHO = 14 AND NEW.TM > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G14||'(seg):'||0||'->'||NEW.TM||']'; END
            IF(NEW.TAMANHO = 15 AND NEW.TM > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G15||'(seg):'||0||'->'||NEW.TM||']'; END
            IF(NEW.TAMANHO = 16 AND NEW.TM > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G16||'(seg):'||0||'->'||NEW.TM||']'; END
            IF(NEW.TAMANHO = 17 AND NEW.TM > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G17||'(seg):'||0||'->'||NEW.TM||']'; END
            IF(NEW.TAMANHO = 18 AND NEW.TM > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G18||'(seg):'||0||'->'||NEW.TM||']'; END
            IF(NEW.TAMANHO = 19 AND NEW.TM > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G19||'(seg):'||0||'->'||NEW.TM||']'; END
            IF(NEW.TAMANHO = 20 AND NEW.TM > 0) THEN BEGIN HISTORICO = HISTORICO||' ['||:G20||'(seg):'||0||'->'||NEW.TM||']'; END
            END

            IF (HISTORICO <> '') THEN
            BEGIN
               HISTORICO = ' FLUXO DE PRODUCAO:'||new.FLUXO_ID||', COR: '||IIF(NEW.COR_ID > 0,(select first 1 NEW.COR_ID||' - '||a.descricao from TBCOR a where a.CODIGO = new.COR_ID),'*** PADRÃO ***')||' ALTERADO '||HISTORICO;
               EXECUTE PROCEDURE SPI_HISTORICO('TBMODELO', NEW.MODELO_ID, HISTORICO);
            END
       END
       --ELSE
       --BEGIN
          -- HISTORICO = HISTORICO||' FLUXO DE PRODUCAO:'||new.FLUXO_ID||', COR: '||IIF(NEW.COR_ID > 0,(select first 1 NEW.COR_ID||' - '||a.descricao from TBCOR a where a.CODIGO = new.COR_ID),'*** PADRÃO ***')||' INCLUIDA.';
          -- EXECUTE PROCEDURE SPI_HISTORICO('TBMODELO', NEW.MODELO_ID, HISTORICO);
       --END
    END

    IF(UPDATING) THEN
    BEGIN
        IF(NEW.TAMANHO = 0 AND OLD.TEMPO <> NEW.TEMPO)THEN
            BEGIN HISTORICO = HISTORICO||' T00(min):'||OLD.TEMPO||'->'||NEW.TEMPO;
        END
        IF(NEW.TAMANHO = 0 AND OLD.TM <> NEW.TM)THEN
            BEGIN HISTORICO = HISTORICO||' T00(seg):'||OLD.TM||'->'||NEW.TM;
        END
       For
        Select FIRST 1 Coalesce(G.CODIGO,0),
             Coalesce(G.T01,'T01'), Coalesce(G.T02,'T02'), Coalesce(G.T03,'T03'), Coalesce(G.T04,'T03'),
             Coalesce(G.T05,'T05'), Coalesce(G.T06,'T06'), Coalesce(G.T07,'T07'), Coalesce(G.T08,'T07'),
             Coalesce(G.T09,'T09'), Coalesce(G.T10,'T10'), Coalesce(G.T11,'T11'), Coalesce(G.T12,'T11'),
             Coalesce(G.T13,'T13'), Coalesce(G.T14,'T14'), Coalesce(G.T15,'T15'), Coalesce(G.T16,'T15'),
             Coalesce(G.T17,'T17'), Coalesce(G.T18,'T18'), Coalesce(G.T19,'T19'), Coalesce(G.T20,'T19')
        From TbGrade G WHERE G.Codigo =  coalesce((select first 1 z.grade_codigo from tbmodelo z where z.codigo = new.MODELO_ID),0)
        Into :Grade,:G01,:G02,:G03,:G04,:G05,:G06,:G07,:G08,:G09,
         :G10,:G11,:G12,:G13,:G14,:G15,:G16,:G17,:G18,:G19,:G20
        DO
        BEGIN
        IF(NEW.TAMANHO = 1 AND NEW.TEMPO <> OLD.TEMPO) THEN BEGIN HISTORICO = HISTORICO||' ['||:G01||'(min):'||OLD.TEMPO||'->'||NEW.TEMPO||']'; END
        IF(NEW.TAMANHO = 2 AND NEW.TEMPO <> OLD.TEMPO) THEN BEGIN HISTORICO = HISTORICO||' ['||:G02||'(min):'||OLD.TEMPO||'->'||NEW.TEMPO||']'; END
        IF(NEW.TAMANHO = 3 AND NEW.TEMPO <> OLD.TEMPO) THEN BEGIN HISTORICO = HISTORICO||' ['||:G03||'(min):'||OLD.TEMPO||'->'||NEW.TEMPO||']'; END
        IF(NEW.TAMANHO = 4 AND NEW.TEMPO <> OLD.TEMPO) THEN BEGIN HISTORICO = HISTORICO||' ['||:G04||'(min):'||OLD.TEMPO||'->'||NEW.TEMPO||']'; END
        IF(NEW.TAMANHO = 5 AND NEW.TEMPO <> OLD.TEMPO) THEN BEGIN HISTORICO = HISTORICO||' ['||:G05||'(min):'||OLD.TEMPO||'->'||NEW.TEMPO||']'; END
        IF(NEW.TAMANHO = 6 AND NEW.TEMPO <> OLD.TEMPO) THEN BEGIN HISTORICO = HISTORICO||' ['||:G06||'(min):'||OLD.TEMPO||'->'||NEW.TEMPO||']'; END
        IF(NEW.TAMANHO = 7 AND NEW.TEMPO <> OLD.TEMPO) THEN BEGIN HISTORICO = HISTORICO||' ['||:G07||'(min):'||OLD.TEMPO||'->'||NEW.TEMPO||']'; END
        IF(NEW.TAMANHO = 8 AND NEW.TEMPO <> OLD.TEMPO) THEN BEGIN HISTORICO = HISTORICO||' ['||:G08||'(min):'||OLD.TEMPO||'->'||NEW.TEMPO||']'; END
        IF(NEW.TAMANHO = 9 AND NEW.TEMPO <> OLD.TEMPO) THEN BEGIN HISTORICO = HISTORICO||' ['||:G09||'(min):'||OLD.TEMPO||'->'||NEW.TEMPO||']'; END
        IF(NEW.TAMANHO = 10 AND NEW.TEMPO <> OLD.TEMPO) THEN BEGIN HISTORICO = HISTORICO||' ['||:G10||'(min):'||OLD.TEMPO||'->'||NEW.TEMPO||']'; END
        IF(NEW.TAMANHO = 11 AND NEW.TEMPO <> OLD.TEMPO) THEN BEGIN HISTORICO = HISTORICO||' ['||:G11||'(min):'||OLD.TEMPO||'->'||NEW.TEMPO||']'; END
        IF(NEW.TAMANHO = 12 AND NEW.TEMPO <> OLD.TEMPO) THEN BEGIN HISTORICO = HISTORICO||' ['||:G12||'(min):'||OLD.TEMPO||'->'||NEW.TEMPO||']'; END
        IF(NEW.TAMANHO = 13 AND NEW.TEMPO <> OLD.TEMPO) THEN BEGIN HISTORICO = HISTORICO||' ['||:G13||'(min):'||OLD.TEMPO||'->'||NEW.TEMPO||']'; END
        IF(NEW.TAMANHO = 14 AND NEW.TEMPO <> OLD.TEMPO) THEN BEGIN HISTORICO = HISTORICO||' ['||:G14||'(min):'||OLD.TEMPO||'->'||NEW.TEMPO||']'; END
        IF(NEW.TAMANHO = 15 AND NEW.TEMPO <> OLD.TEMPO) THEN BEGIN HISTORICO = HISTORICO||' ['||:G15||'(min):'||OLD.TEMPO||'->'||NEW.TEMPO||']'; END
        IF(NEW.TAMANHO = 16 AND NEW.TEMPO <> OLD.TEMPO) THEN BEGIN HISTORICO = HISTORICO||' ['||:G16||'(min):'||OLD.TEMPO||'->'||NEW.TEMPO||']'; END
        IF(NEW.TAMANHO = 17 AND NEW.TEMPO <> OLD.TEMPO) THEN BEGIN HISTORICO = HISTORICO||' ['||:G17||'(min):'||OLD.TEMPO||'->'||NEW.TEMPO||']'; END
        IF(NEW.TAMANHO = 18 AND NEW.TEMPO <> OLD.TEMPO) THEN BEGIN HISTORICO = HISTORICO||' ['||:G18||'(min):'||OLD.TEMPO||'->'||NEW.TEMPO||']'; END
        IF(NEW.TAMANHO = 19 AND NEW.TEMPO <> OLD.TEMPO) THEN BEGIN HISTORICO = HISTORICO||' ['||:G19||'(min):'||OLD.TEMPO||'->'||NEW.TEMPO||']'; END
        IF(NEW.TAMANHO = 20 AND NEW.TEMPO <> OLD.TEMPO) THEN BEGIN HISTORICO = HISTORICO||' ['||:G20||'(min):'||OLD.TEMPO||'->'||NEW.TEMPO||']'; END

        IF(NEW.TAMANHO = 1 AND NEW.TM <> OLD.TM) THEN BEGIN HISTORICO = HISTORICO||' ['||:G01||'(seg):'||OLD.TM||'->'||NEW.TM||']'; END
        IF(NEW.TAMANHO = 2 AND NEW.TM <> OLD.TM) THEN BEGIN HISTORICO = HISTORICO||' ['||:G02||'(seg):'||OLD.TM||'->'||NEW.TM||']'; END
        IF(NEW.TAMANHO = 3 AND NEW.TM <> OLD.TM) THEN BEGIN HISTORICO = HISTORICO||' ['||:G03||'(seg):'||OLD.TM||'->'||NEW.TM||']'; END
        IF(NEW.TAMANHO = 4 AND NEW.TM <> OLD.TM) THEN BEGIN HISTORICO = HISTORICO||' ['||:G04||'(seg):'||OLD.TM||'->'||NEW.TM||']'; END
        IF(NEW.TAMANHO = 5 AND NEW.TM <> OLD.TM) THEN BEGIN HISTORICO = HISTORICO||' ['||:G05||'(seg):'||OLD.TM||'->'||NEW.TM||']'; END
        IF(NEW.TAMANHO = 6 AND NEW.TM <> OLD.TM) THEN BEGIN HISTORICO = HISTORICO||' ['||:G06||'(seg):'||OLD.TM||'->'||NEW.TM||']'; END
        IF(NEW.TAMANHO = 7 AND NEW.TM <> OLD.TM) THEN BEGIN HISTORICO = HISTORICO||' ['||:G07||'(seg):'||OLD.TM||'->'||NEW.TM||']'; END
        IF(NEW.TAMANHO = 8 AND NEW.TM <> OLD.TM) THEN BEGIN HISTORICO = HISTORICO||' ['||:G08||'(seg):'||OLD.TM||'->'||NEW.TM||']'; END
        IF(NEW.TAMANHO = 9 AND NEW.TM <> OLD.TM) THEN BEGIN HISTORICO = HISTORICO||' ['||:G09||'(seg):'||OLD.TM||'->'||NEW.TM||']'; END
        IF(NEW.TAMANHO = 10 AND NEW.TM <> OLD.TM) THEN BEGIN HISTORICO = HISTORICO||' ['||:G10||'(seg):'||OLD.TM||'->'||NEW.TM||']'; END
        IF(NEW.TAMANHO = 11 AND NEW.TM <> OLD.TM) THEN BEGIN HISTORICO = HISTORICO||' ['||:G11||'(seg):'||OLD.TM||'->'||NEW.TM||']'; END
        IF(NEW.TAMANHO = 12 AND NEW.TM <> OLD.TM) THEN BEGIN HISTORICO = HISTORICO||' ['||:G12||'(seg):'||OLD.TM||'->'||NEW.TM||']'; END
        IF(NEW.TAMANHO = 13 AND NEW.TM <> OLD.TM) THEN BEGIN HISTORICO = HISTORICO||' ['||:G13||'(seg):'||OLD.TM||'->'||NEW.TM||']'; END
        IF(NEW.TAMANHO = 14 AND NEW.TM <> OLD.TM) THEN BEGIN HISTORICO = HISTORICO||' ['||:G14||'(seg):'||OLD.TM||'->'||NEW.TM||']'; END
        IF(NEW.TAMANHO = 15 AND NEW.TM <> OLD.TM) THEN BEGIN HISTORICO = HISTORICO||' ['||:G15||'(seg):'||OLD.TM||'->'||NEW.TM||']'; END
        IF(NEW.TAMANHO = 16 AND NEW.TM <> OLD.TM) THEN BEGIN HISTORICO = HISTORICO||' ['||:G16||'(seg):'||OLD.TM||'->'||NEW.TM||']'; END
        IF(NEW.TAMANHO = 17 AND NEW.TM <> OLD.TM) THEN BEGIN HISTORICO = HISTORICO||' ['||:G17||'(seg):'||OLD.TM||'->'||NEW.TM||']'; END
        IF(NEW.TAMANHO = 18 AND NEW.TM <> OLD.TM) THEN BEGIN HISTORICO = HISTORICO||' ['||:G18||'(seg):'||OLD.TM||'->'||NEW.TM||']'; END
        IF(NEW.TAMANHO = 19 AND NEW.TM <> OLD.TM) THEN BEGIN HISTORICO = HISTORICO||' ['||:G19||'(seg):'||OLD.TM||'->'||NEW.TM||']'; END
        IF(NEW.TAMANHO = 20 AND NEW.TM <> OLD.TM) THEN BEGIN HISTORICO = HISTORICO||' ['||:G20||'(seg):'||OLD.TM||'->'||NEW.TM||']'; END
        END
        IF(HISTORICO <> '') THEN
        BEGIN
           HISTORICO = ' FLUXO DE PRODUCAO:'||new.FLUXO_ID||', COR: '||IIF(NEW.COR_ID > 0,(select first 1 NEW.COR_ID||' - '||a.descricao from TBCOR a where a.CODIGO = new.COR_ID),'*** PADRÃO ***')||' ALTERADO '||HISTORICO;
           EXECUTE PROCEDURE SPI_HISTORICO('TBMODELO', NEW.MODELO_ID, HISTORICO);
        END
    END


END;

CREATE OR ALTER TRIGGER TTBMODELO_PADRAO_B01I FOR TBMODELO_PADRAO
ACTIVE BEFORE INSERT OR UPDATE POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(3000);
DECLARE VARIABLE TAM VARCHAR(3000);
DECLARE VARIABLE OLD_TAM VARCHAR(3000);
BEGIN

    TAM = COALESCE((SELECT FIRST 1 A.TAMANHO_DESCRICAO FROM(select TAMANHO_DESCRICAO from SP_GRADE_MODELO(NEW.MODELO_ID,'M') WHERE TAMANHO = NEW.TAMANHO)A),'T'||FN_LPAD(NEW.TAMANHO,2,0));

    HISTORICO = '';
    IF(INSERTING) THEN
    BEGIN
       HISTORICO = HISTORICO||'COR/TAM. PADRAO DEFINIDOS, COR: '||NEW.COR_ID||',TAM: '||TAM;
    END
    ELSE
    BEGIN
        OLD_TAM = COALESCE((SELECT FIRST 1 A.TAMANHO_DESCRICAO FROM(select TAMANHO_DESCRICAO from SP_GRADE_MODELO(NEW.MODELO_ID,'M') WHERE TAMANHO = OLD.TAMANHO)A ),'T'||FN_LPAD(OLD.TAMANHO,2,0));
        IF(NEW.COR_ID <> OLD.COR_ID) THEN
        BEGIN
            HISTORICO = HISTORICO||' COR: '||OLD.COR_ID||' -> '||NEW.COR_ID;
        END
        IF(NEW.TAMANHO <> OLD.TAMANHO) THEN
        BEGIN
            HISTORICO = HISTORICO||',TAM: '||OLD_TAM||' -> '||TAM;
        END
    END
    
    IF(HISTORICO <> '') THEN
    BEGIN
        IF(UPDATING)THEN
        BEGIN
            HISTORICO = 'COR/TAM. PADRAO ALTERADOS. '||HISTORICO;
        END
        EXECUTE PROCEDURE SPI_HISTORICO('TBMODELO', NEW.MODELO_ID, HISTORICO);
    END
END;

CREATE OR ALTER TRIGGER TTBMODELO_B05I FOR TBMODELO
ACTIVE BEFORE INSERT POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN
    HISTORICO = '';
    IF(NEW.CODIGO > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||' MODELO INCLUIDO';
    END
    
    IF(HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBMODELO',NEW.CODIGO,HISTORICO);
    END
END;

CREATE OR ALTER TRIGGER TTBMODELO_B06D FOR TBMODELO
ACTIVE BEFORE DELETE POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN
    HISTORICO = '';
    IF(OLD.CODIGO > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||' MODELO EXCLUIDO';
    END
    
    IF(HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBMODELO',OLD.CODIGO,HISTORICO);
    END
END;

CREATE OR ALTER TRIGGER TTBMODELO_SETOR_B02I FOR TBMODELO_SETOR
ACTIVE BEFORE INSERT POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF (NEW.MODELO_CODIGO > 0) THEN
    BEGIN
      HISTORICO = HISTORICO||'ROT.PROD. ORDEM: '||FN_LPAD(NEW.ORDEM,3,'0')||', UP: '||FN_LPAD(NEW.SETOR_CODIGO,4,0)||' INCLUIDO';
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBMODELO', NEW.MODELO_CODIGO, HISTORICO);
    END
END;

CREATE OR ALTER TRIGGER TTBMODELO_FLUXO_B02U FOR TBMODELO_FLUXO
ACTIVE BEFORE UPDATE POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

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

    IF (HISTORICO <> '') THEN
    BEGIN
        HISTORICO = ' FLUXO PROD: '||FN_LPAD(NEW.FLUXO_ID,4,0)||' ALTERADO'||HISTORICO;
        EXECUTE PROCEDURE SPI_HISTORICO('TBMODELO', NEW.MODELO_ID, HISTORICO);
    END
END;

CREATE OR ALTER TRIGGER TTBMODELO_FLUXO_B02D FOR TBMODELO_FLUXO
ACTIVE BEFORE DELETE POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF (OLD.MODELO_ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||'FLUXO PROD: '||FN_LPAD(OLD.FLUXO_ID,4,'0')||' EXCLUÍDO.';
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBMODELO', OLD.MODELO_ID, HISTORICO);
    END
END;

CREATE OR ALTER TRIGGER TTBMODELO_FLUXO_B02I FOR TBMODELO_FLUXO
ACTIVE BEFORE INSERT POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF (NEW.MODELO_ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||'FLUXO PROD: '||FN_LPAD(NEW.FLUXO_ID,4,'0')||' INCLUÍDO.';
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBMODELO', NEW.MODELO_ID, HISTORICO);
    END
END;