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

  HISTORICO = '';

  IF(NEW.ID > 0) THEN
  BEGIN
    HISTORICO = HISTORICO||'REGISTRO INCLUIDO';
  END

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

CREATE OR ALTER TRIGGER TTBMODELO_PRECO_B03U FOR TBMODELO_PRECO
ACTIVE BEFORE UPDATE POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(4000);
BEGIN
    HISTORICO = '';

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

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

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

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

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

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

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

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

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

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

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

    IF(OLD.PR_NF_08 <> NEW.PR_NF_08) THEN
    BEGIN
       HISTORICO = HISTORICO||' NF.T08:'||OLD.PR_NF_08||'->'||NEW.PR_NF_08;
    END
    
    IF(OLD.PR_NF_09 <> NEW.PR_NF_09) THEN
    BEGIN
       HISTORICO = HISTORICO||' NF.T09:'||OLD.PR_NF_09||'->'||NEW.PR_NF_09;
    END
    
    IF(OLD.PR_NF_10 <> NEW.PR_NF_10) THEN
    BEGIN
       HISTORICO = HISTORICO||' NF.T10:'||OLD.PR_NF_10||'->'||NEW.PR_NF_10;
    END
    
    IF(OLD.PR_NF_11 <> NEW.PR_NF_11) THEN
    BEGIN
       HISTORICO = HISTORICO||' NF.T11:'||OLD.PR_NF_11||'->'||NEW.PR_NF_11;
    END
    
    IF(OLD.PR_NF_12 <> NEW.PR_NF_12) THEN
    BEGIN
       HISTORICO = HISTORICO||' NF.T12:'||OLD.PR_NF_12||'->'||NEW.PR_NF_12;
    END
    
    IF(OLD.PR_NF_13 <> NEW.PR_NF_13) THEN
    BEGIN
       HISTORICO = HISTORICO||' NF.T13:'||OLD.PR_NF_13||'->'||NEW.PR_NF_13;
    END
    
    IF(OLD.PR_NF_14 <> NEW.PR_NF_14) THEN
    BEGIN
       HISTORICO = HISTORICO||' NF.T14:'||OLD.PR_NF_14||'->'||NEW.PR_NF_14;
    END
    
    IF(OLD.PR_NF_15 <> NEW.PR_NF_15) THEN
    BEGIN
       HISTORICO = HISTORICO||' NF.T15:'||OLD.PR_NF_15||'->'||NEW.PR_NF_15;
    END
    
    IF(OLD.PR_NF_16 <> NEW.PR_NF_16) THEN
    BEGIN
       HISTORICO = HISTORICO||' NF.T16:'||OLD.PR_NF_16||'->'||NEW.PR_NF_16;
    END
    
    IF(OLD.PR_NF_17 <> NEW.PR_NF_17) THEN
    BEGIN
       HISTORICO = HISTORICO||' NF.T17:'||OLD.PR_NF_17||'->'||NEW.PR_NF_17;
    END
    
    IF(OLD.PR_NF_18 <> NEW.PR_NF_18) THEN
    BEGIN
       HISTORICO = HISTORICO||' NF.T18:'||OLD.PR_NF_18||'->'||NEW.PR_NF_18;
    END

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

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

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

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

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

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

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

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

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

    IF(OLD.PR_CF_08 <> NEW.PR_CF_08) THEN
    BEGIN
       HISTORICO = HISTORICO||' CF.T08:'||OLD.PR_CF_08||'->'||NEW.PR_CF_08;
    END
    
    IF(OLD.PR_CF_09 <> NEW.PR_CF_09) THEN
    BEGIN
       HISTORICO = HISTORICO||' CF.T09:'||OLD.PR_CF_09||'->'||NEW.PR_CF_09;
    END
    
    IF(OLD.PR_CF_10 <> NEW.PR_CF_10) THEN
    BEGIN
       HISTORICO = HISTORICO||' CF.T10:'||OLD.PR_CF_10||'->'||NEW.PR_CF_10;
    END
    
    IF(OLD.PR_CF_11 <> NEW.PR_CF_11) THEN
    BEGIN
       HISTORICO = HISTORICO||' CF.T11:'||OLD.PR_CF_11||'->'||NEW.PR_CF_11;
    END
    
    IF(OLD.PR_CF_12 <> NEW.PR_CF_12) THEN
    BEGIN
       HISTORICO = HISTORICO||' CF.T12:'||OLD.PR_CF_12||'->'||NEW.PR_CF_12;
    END
    
    IF(OLD.PR_CF_13 <> NEW.PR_CF_13) THEN
    BEGIN
       HISTORICO = HISTORICO||' CF.T13:'||OLD.PR_CF_13||'->'||NEW.PR_CF_13;
    END
    
    IF(OLD.PR_CF_14 <> NEW.PR_CF_14) THEN
    BEGIN
       HISTORICO = HISTORICO||' CF.T14:'||OLD.PR_CF_14||'->'||NEW.PR_CF_14;
    END
    
    IF(OLD.PR_CF_15 <> NEW.PR_CF_15) THEN
    BEGIN
       HISTORICO = HISTORICO||' CF.T15:'||OLD.PR_CF_15||'->'||NEW.PR_CF_15;
    END
    
    IF(OLD.PR_CF_16 <> NEW.PR_CF_16) THEN
    BEGIN
       HISTORICO = HISTORICO||'CF.T16:'||OLD.PR_CF_16||'->'||NEW.PR_CF_16;
    END
    
    IF(OLD.PR_CF_17 <> NEW.PR_CF_17) THEN
    BEGIN
       HISTORICO = HISTORICO||' CF.T17:'||OLD.PR_CF_17||'->'||NEW.PR_CF_17;
    END
    
    IF(OLD.PR_CF_18 <> NEW.PR_CF_18) THEN
    BEGIN
       HISTORICO = HISTORICO||' CF.T18:'||OLD.PR_CF_18||'->'||NEW.PR_CF_18;
    END

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

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

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

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

  HISTORICO = '';

  IF(OLD.ID > 0) THEN
  BEGIN
    HISTORICO = HISTORICO||'REGISTRO EXCLUIDO';
  END

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



--SPC_MODELO_ALL_TAMANHOS

CREATE OR ALTER PROCEDURE SPC_MODELO_ALL_TAMANHOS (
    MODELO_ID INTEGER)
RETURNS (
    MODELO_DESCRICAO VARCHAR(150),
    TAMANHO INTEGER,
    TAMANHO_CODIGO_STR VARCHAR(2),
    TAMANHO_DESCRICAO VARCHAR(100),
    STATUS CHAR(1),
    GRADE_ID INTEGER)
AS
BEGIN
    FOR
        SELECT M.DESCRICAO, 01, '01', M.GRADE_CODIGO, M.T01 AS STATUS FROM TBMODELO M WHERE M.GRADE_CODIGO > 0 AND M.CODIGO = :MODELO_ID UNION
        SELECT M.DESCRICAO, 02, '02', M.GRADE_CODIGO, M.T02 AS STATUS FROM TBMODELO M WHERE M.GRADE_CODIGO > 0 AND M.CODIGO = :MODELO_ID UNION
        SELECT M.DESCRICAO, 03, '03', M.GRADE_CODIGO, M.T03 AS STATUS FROM TBMODELO M WHERE M.GRADE_CODIGO > 0 AND M.CODIGO = :MODELO_ID UNION
        SELECT M.DESCRICAO, 04, '04', M.GRADE_CODIGO, M.T04 AS STATUS FROM TBMODELO M WHERE M.GRADE_CODIGO > 0 AND M.CODIGO = :MODELO_ID UNION
        SELECT M.DESCRICAO, 05, '05', M.GRADE_CODIGO, M.T05 AS STATUS FROM TBMODELO M WHERE M.GRADE_CODIGO > 0 AND M.CODIGO = :MODELO_ID UNION
        SELECT M.DESCRICAO, 06, '06', M.GRADE_CODIGO, M.T06 AS STATUS FROM TBMODELO M WHERE M.GRADE_CODIGO > 0 AND M.CODIGO = :MODELO_ID UNION
        SELECT M.DESCRICAO, 07, '07', M.GRADE_CODIGO, M.T07 AS STATUS FROM TBMODELO M WHERE M.GRADE_CODIGO > 0 AND M.CODIGO = :MODELO_ID UNION
        SELECT M.DESCRICAO, 08, '08', M.GRADE_CODIGO, M.T08 AS STATUS FROM TBMODELO M WHERE M.GRADE_CODIGO > 0 AND M.CODIGO = :MODELO_ID UNION
        SELECT M.DESCRICAO, 09, '09', M.GRADE_CODIGO, M.T09 AS STATUS FROM TBMODELO M WHERE M.GRADE_CODIGO > 0 AND M.CODIGO = :MODELO_ID UNION
        SELECT M.DESCRICAO, 10, '10', M.GRADE_CODIGO, M.T10 AS STATUS FROM TBMODELO M WHERE M.GRADE_CODIGO > 0 AND M.CODIGO = :MODELO_ID UNION
        SELECT M.DESCRICAO, 11, '11', M.GRADE_CODIGO, M.T11 AS STATUS FROM TBMODELO M WHERE M.GRADE_CODIGO > 0 AND M.CODIGO = :MODELO_ID UNION
        SELECT M.DESCRICAO, 12, '12', M.GRADE_CODIGO, M.T12 AS STATUS FROM TBMODELO M WHERE M.GRADE_CODIGO > 0 AND M.CODIGO = :MODELO_ID UNION
        SELECT M.DESCRICAO, 13, '13', M.GRADE_CODIGO, M.T13 AS STATUS FROM TBMODELO M WHERE M.GRADE_CODIGO > 0 AND M.CODIGO = :MODELO_ID UNION
        SELECT M.DESCRICAO, 14, '14', M.GRADE_CODIGO, M.T14 AS STATUS FROM TBMODELO M WHERE M.GRADE_CODIGO > 0 AND M.CODIGO = :MODELO_ID UNION
        SELECT M.DESCRICAO, 15, '15', M.GRADE_CODIGO, M.T15 AS STATUS FROM TBMODELO M WHERE M.GRADE_CODIGO > 0 AND M.CODIGO = :MODELO_ID UNION
        SELECT M.DESCRICAO, 16, '16', M.GRADE_CODIGO, M.T16 AS STATUS FROM TBMODELO M WHERE M.GRADE_CODIGO > 0 AND M.CODIGO = :MODELO_ID UNION
        SELECT M.DESCRICAO, 17, '17', M.GRADE_CODIGO, M.T17 AS STATUS FROM TBMODELO M WHERE M.GRADE_CODIGO > 0 AND M.CODIGO = :MODELO_ID UNION
        SELECT M.DESCRICAO, 18, '18', M.GRADE_CODIGO, M.T18 AS STATUS FROM TBMODELO M WHERE M.GRADE_CODIGO > 0 AND M.CODIGO = :MODELO_ID UNION
        SELECT M.DESCRICAO, 19, '19', M.GRADE_CODIGO, M.T19 AS STATUS FROM TBMODELO M WHERE M.GRADE_CODIGO > 0 AND M.CODIGO = :MODELO_ID UNION
        SELECT M.DESCRICAO, 20, '20', M.GRADE_CODIGO, M.T20 AS STATUS FROM TBMODELO M WHERE M.GRADE_CODIGO > 0 AND M.CODIGO = :MODELO_ID
    INTO
        MODELO_DESCRICAO,
        TAMANHO,
        TAMANHO_CODIGO_STR,
        GRADE_ID,
        STATUS
    DO
    BEGIN
        TAMANHO_DESCRICAO = FN_TAMANHO_GRADE(:GRADE_ID,:TAMANHO);
        SUSPEND;
    END
END;