create or alter procedure SPC_PEDIDO_GRADE (
    DATA1 date,
    DATA2 date,
    PEDIDO1 integer,
    PEDIDO2 integer,
    SITUACAO integer)
returns (
    PEDIDO integer,
    MODELO_ID integer,
    MODELO_DESCRICAO varchar(100),
    COR_ID integer,
    COR_DESCRICAO varchar(50),
    T01 numeric(15,0),
    T02 numeric(15,0),
    T03 numeric(15,0),
    T04 numeric(15,0),
    T05 numeric(15,0),
    T06 numeric(15,0),
    T07 numeric(15,0),
    T08 numeric(15,0),
    T09 numeric(15,0),
    T10 numeric(15,0),
    T11 numeric(15,0),
    T12 numeric(15,0),
    T13 numeric(15,0),
    T14 numeric(15,0),
    T15 numeric(15,0),
    T16 numeric(15,0),
    T17 numeric(15,0),
    T18 numeric(15,0),
    T19 numeric(15,0),
    T20 numeric(15,0),
    G01 varchar(10),
    G02 varchar(10),
    G03 varchar(10),
    G04 varchar(10),
    G05 varchar(10),
    G06 varchar(10),
    G07 varchar(10),
    G08 varchar(10),
    G09 varchar(10),
    G10 varchar(10),
    G11 varchar(10),
    G12 varchar(10),
    G13 varchar(10),
    G14 varchar(10),
    G15 varchar(10),
    G16 varchar(10),
    G17 varchar(10),
    G18 varchar(10),
    G19 varchar(10),
    G20 varchar(10),
    TMIN integer,
    TMAX integer,
    TOT numeric(15,0),
    GTOT varchar(10),
    TAM_MAX_MIN integer,
    VALOR_UNITARIO numeric(15,5),
    VALOR_SUBTOTAL numeric(15,2),
    VALOR_DESCONTO numeric(15,2),
    VALOR_TOTAL numeric(15,2))
as
declare variable PROX_PEDMOD varchar(100);
declare variable PROX_PED integer;
declare variable Q01 numeric(15,0);
declare variable Q02 numeric(15,0);
declare variable Q03 numeric(15,0);
declare variable Q04 numeric(15,0);
declare variable Q05 numeric(15,0);
declare variable Q06 numeric(15,0);
declare variable Q07 numeric(15,0);
declare variable Q08 numeric(15,0);
declare variable Q09 numeric(15,0);
declare variable Q10 numeric(15,0);
declare variable Q11 numeric(15,0);
declare variable Q12 numeric(15,0);
declare variable Q13 numeric(15,0);
declare variable Q14 numeric(15,0);
declare variable Q15 numeric(15,0);
declare variable Q16 numeric(15,0);
declare variable Q17 numeric(15,0);
declare variable Q18 numeric(15,0);
declare variable Q19 numeric(15,0);
declare variable Q20 numeric(15,0);
declare variable QTOT numeric(15,0);
declare variable GQTOT numeric(15,0); /* TOTAL DO PEDIDO */
declare variable VR_SUBTOT numeric(15,2); /* VR SUB TOTAL DO PEDIDO */
declare variable VR_TOTDESC numeric(15,2); /* VR TOTAL DO DESCONTO DO PEDIDO */
declare variable VR_TOTGERAL numeric(15,2); /* VR TOTAL DO PEDIDO */
declare variable VR_SUBGTOT numeric(15,2); /* VR SUB TOTAL DO PEDIDO */
declare variable VR_TOTGDESC numeric(15,2); /* VR TOTAL DO DESCONTO DO PEDIDO */
declare variable VR_TOTGGERAL numeric(15,2); /* VR TOTAL DO PEDIDO */
begin

    q01 = 0; q02 = 0; q03 = 0; q04 = 0;
    q05 = 0; q06 = 0; q07 = 0; q08 = 0;
    q09 = 0; q10 = 0; q11 = 0; q12 = 0;
    q13 = 0; q14 = 0; q15 = 0; q16 = 0;
    q17 = 0; q18 = 0; q19 = 0; q20 = 0;
    qTot= 0;
    gqTot= 0;

    vr_subTOT = 0;
    vr_totDESC = 0;
    vr_totGERAL = 0;

    vr_subGTOT = 0;
    vr_totGDESC = 0;
    vr_totGGERAL = 0;

    FOR
        SELECT PEDIDO,
               MODELO_ID, MODELO_DESCRICAO,
               COR_ID, COR_DESCRICAO,
               T01,T02,T03,T04,T05,T06,T07,T08,T09,T10,
               T11,T12,T13,T14,T15,T16,T17,T18,T19,T20,
               G01,G02,G03,G04,G05,G06,G07,G08,G09,G10,
               G11,G12,G13,G14,G15,G16,G17,G18,G19,G20,
               PROX_PEDMOD,
               PROX_PED,
               TMIN,
               TMAX,
               MAX(TAM_MAX_MIN) OVER (),
               VALOR_UNITARIO,
               VALOR_SUBTOTAL,
               VALOR_DESCONTO
          FROM (
                SELECT PEDIDO,
                       MODELO_ID, MODELO_DESCRICAO,
                       COR_ID, COR_DESCRICAO,
                       T01,T02,T03,T04,T05,T06,T07,T08,T09,T10,
                       T11,T12,T13,T14,T15,T16,T17,T18,T19,T20,
                       G01,G02,G03,G04,G05,G06,G07,G08,G09,G10,
                       G11,G12,G13,G14,G15,G16,G17,G18,G19,G20,
                       PROX_PEDMOD,
                       PROX_PED,
                       MIN(TAM_MIN) OVER (PARTITION BY PEDIDO,MODELO_ID) TMIN,
                       MAX(TAM_MAX) OVER (PARTITION BY PEDIDO,MODELO_ID) TMAX,
                       TAM_MAX - TAM_MIN + 2 as TAM_MAX_MIN,
                       VALOR_UNITARIO,
                       VALOR_SUBTOTAL,
                       VALOR_DESCONTO
                  FROM (
                        SELECT PEDIDO,
                               MODELO_ID, MODELO_DESCRICAO,
                               COR_ID, COR_DESCRICAO,
                               T01,T02,T03,T04,T05,T06,T07,T08,T09,T10,
                               T11,T12,T13,T14,T15,T16,T17,T18,T19,T20,
                               G01,G02,G03,G04,G05,G06,G07,G08,G09,G10,
                               G11,G12,G13,G14,G15,G16,G17,G18,G19,G20,
                               LEAD (PEDIDO_MODELO) OVER (ORDER BY PEDIDO, MODELO_ID, COR_DESCRICAO, VALOR_UNITARIO) PROX_PEDMOD,
                               LEAD (PEDIDO) OVER (ORDER BY PEDIDO, MODELO_ID, COR_DESCRICAO, VALOR_UNITARIO) PROX_PED,
                               IIF(T01>0,1,  IIF(T02>0,2,    IIF(T03>0,3,  IIF(T04>0,4,
                               IIF(T05>0,5,  IIF(T06>0,6,    IIF(T07>0,7,  IIF(T08>0,8,
                               IIF(T09>0,9,  IIF(T10>0,10,   IIF(T11>0,11, IIF(T12>0,12,
                               IIF(T13>0,13, IIF(T14>0,14,   IIF(T15>0,15, IIF(T16>0,16,
                               IIF(T17>0,17, IIF(T18>0,18,   IIF(T19>0,19, 20))))))))))))))))))) TAM_MIN,
                               IIF(T20>0,20, IIF(T19>0,19,   IIF(T18>0,18, IIF(T17>0,17,
                               IIF(T16>0,16, IIF(T15>0,15,   IIF(T14>0,14, IIF(T13>0,13,
                               IIF(T12>0,12, IIF(T11>0,11,   IIF(T10>0,10, IIF(T09>0,9,
                               IIF(T08>0,8,  IIF(T07>0,7,    IIF(T06>0,6,  IIF(T05>0,5,
                               IIF(T04>0,4,  IIF(T03>0,3,    IIF(T02>0,2,  1))))))))))))))))))) TAM_MAX,
                               VALOR_UNITARIO,
                               VALOR_SUBTOTAL,
                               VALOR_DESCONTO
                          FROM (
                                SELECT X.PEDIDO||'-'||X.MODELO_ID||'-'||VALOR_UNITARIO AS PEDIDO_MODELO,
                                       X.PEDIDO,
                                       X.MODELO_ID, X.MODELO_DESCRICAO,
                                       X.COR_ID, X.COR_DESCRICAO,
                                       SUM(T01) T01,SUM(T02) T02,SUM(T03) T03,SUM(T04) T04,
                                       SUM(T05) T05,SUM(T06) T06,SUM(T07) T07,SUM(T08) T08,
                                       SUM(T09) T09,SUM(T10) T10,SUM(T11) T11,SUM(T12) T12,
                                       SUM(T13) T13,SUM(T14) T14,SUM(T15) T15,SUM(T16) T16,
                                       SUM(T17) T17,SUM(T18) T18,SUM(T19) T19,SUM(T20) T20,
                                       G01,G02,G03,G04,G05,G06,G07,G08,G09,G10,
                                       G11,G12,G13,G14,G15,G16,G17,G18,G19,G20,
                                       VALOR_UNITARIO,
                                       SUM(VALOR_SUBTOTAL) VALOR_SUBTOTAL,
                                       SUM(VALOR_DESCONTO) VALOR_DESCONTO
                                 FROM (
                                          SELECT P.PEDIDO, I.CONTROLE PEDIDO_ITEM_ID,
                                                 I.MODELO_CODIGO AS MODELO_ID,
                                                 M.DESCRICAO MODELO_DESCRICAO,
                                                 I.COR_ID, C.DESCRICAO COR_DESCRICAO,
                                                 IIF(I.TAMANHO = 1, IIF( :SITUACAO = 1 and I.SITUACAO < 3 , I.QUANTIDADE ,
                                                                    IIF( :SITUACAO = 1 and I.SITUACAO = 3 , I.QUANTIDADE - S.QUANTIDADE ,
                                                                    IIF( :SITUACAO = 2 and I.SITUACAO < 3 , S.QUANTIDADE ,
                                                                    IIF( :SITUACAO = 3 and I.SITUACAO = 1 , I.QTD_EST    ,0)))) , 0) T01,
                                                 IIF(I.TAMANHO = 2, IIF( :SITUACAO = 1 and I.SITUACAO < 3 , I.QUANTIDADE ,
                                                                    IIF( :SITUACAO = 1 and I.SITUACAO = 3 , I.QUANTIDADE - S.QUANTIDADE ,
                                                                    IIF( :SITUACAO = 2 and I.SITUACAO < 3 , S.QUANTIDADE ,
                                                                    IIF( :SITUACAO = 3 and I.SITUACAO = 1 , I.QTD_EST    ,0)))) , 0) T02,
                                                 IIF(I.TAMANHO = 3, IIF( :SITUACAO = 1 and I.SITUACAO < 3 , I.QUANTIDADE ,
                                                                    IIF( :SITUACAO = 1 and I.SITUACAO = 3 , I.QUANTIDADE - S.QUANTIDADE ,
                                                                    IIF( :SITUACAO = 2 and I.SITUACAO < 3 , S.QUANTIDADE ,
                                                                    IIF( :SITUACAO = 3 and I.SITUACAO = 1 , I.QTD_EST    , 0)))) , 0) T03,
                                                 IIF(I.TAMANHO = 4, IIF( :SITUACAO = 1 and I.SITUACAO < 3 , I.QUANTIDADE ,
                                                                    IIF( :SITUACAO = 1 and I.SITUACAO = 3 , I.QUANTIDADE - S.QUANTIDADE ,
                                                                    IIF( :SITUACAO = 2 and I.SITUACAO < 3 , S.QUANTIDADE ,
                                                                    IIF( :SITUACAO = 3 and I.SITUACAO = 1 , I.QTD_EST    , 0)))) , 0) T04,
                                                 IIF(I.TAMANHO = 5, IIF( :SITUACAO = 1 and I.SITUACAO < 3 , I.QUANTIDADE ,
                                                                    IIF( :SITUACAO = 1 and I.SITUACAO = 3 , I.QUANTIDADE - S.QUANTIDADE ,
                                                                    IIF( :SITUACAO = 2 and I.SITUACAO < 3 , S.QUANTIDADE ,
                                                                    IIF( :SITUACAO = 3 and I.SITUACAO = 1 , I.QTD_EST    , 0)))) , 0) T05,
                                                 IIF(I.TAMANHO = 6, IIF( :SITUACAO = 1 and I.SITUACAO < 3 , I.QUANTIDADE ,
                                                                    IIF( :SITUACAO = 1 and I.SITUACAO = 3 , I.QUANTIDADE - S.QUANTIDADE ,
                                                                    IIF( :SITUACAO = 2 and I.SITUACAO < 3 , S.QUANTIDADE ,
                                                                    IIF( :SITUACAO = 3 and I.SITUACAO = 1 , I.QTD_EST    , 0)))) , 0) T06,
                                                 IIF(I.TAMANHO = 7, IIF( :SITUACAO = 1 and I.SITUACAO < 3 , I.QUANTIDADE ,
                                                                    IIF( :SITUACAO = 1 and I.SITUACAO = 3 , I.QUANTIDADE - S.QUANTIDADE ,
                                                                    IIF( :SITUACAO = 2 and I.SITUACAO < 3 , S.QUANTIDADE ,
                                                                    IIF( :SITUACAO = 3 and I.SITUACAO = 1 , I.QTD_EST    , 0)))) , 0) T07,
                                                 IIF(I.TAMANHO = 8, IIF( :SITUACAO = 1 and I.SITUACAO < 3 , I.QUANTIDADE ,
                                                                    IIF( :SITUACAO = 1 and I.SITUACAO = 3 , I.QUANTIDADE - S.QUANTIDADE ,
                                                                    IIF( :SITUACAO = 2 and I.SITUACAO < 3 , S.QUANTIDADE ,
                                                                    IIF( :SITUACAO = 3 and I.SITUACAO = 1 , I.QTD_EST    , 0)))) , 0) T08,
                                                 IIF(I.TAMANHO = 9, IIF( :SITUACAO = 1 and I.SITUACAO < 3 , I.QUANTIDADE ,
                                                                    IIF( :SITUACAO = 1 and I.SITUACAO = 3 , I.QUANTIDADE - S.QUANTIDADE ,
                                                                    IIF( :SITUACAO = 2 and I.SITUACAO < 3 , S.QUANTIDADE ,
                                                                    IIF( :SITUACAO = 3 and I.SITUACAO = 1 , I.QTD_EST    , 0)))) , 0) T09,
                                                 IIF(I.TAMANHO =10, IIF( :SITUACAO = 1 and I.SITUACAO < 3 , I.QUANTIDADE ,
                                                                    IIF( :SITUACAO = 1 and I.SITUACAO = 3 , I.QUANTIDADE - S.QUANTIDADE ,
                                                                    IIF( :SITUACAO = 2 and I.SITUACAO < 3 , S.QUANTIDADE ,
                                                                    IIF( :SITUACAO = 3 and I.SITUACAO = 1 , I.QTD_EST    , 0)))) , 0) T10,
                                                 IIF(I.TAMANHO =11, IIF( :SITUACAO = 1 and I.SITUACAO < 3 , I.QUANTIDADE ,
                                                                    IIF( :SITUACAO = 1 and I.SITUACAO = 3 , I.QUANTIDADE - S.QUANTIDADE ,
                                                                    IIF( :SITUACAO = 2 and I.SITUACAO < 3 , S.QUANTIDADE ,
                                                                    IIF( :SITUACAO = 3 and I.SITUACAO = 1 , I.QTD_EST    , 0)))) , 0) T11,
                                                 IIF(I.TAMANHO =12, IIF( :SITUACAO = 1 and I.SITUACAO < 3 , I.QUANTIDADE ,
                                                                    IIF( :SITUACAO = 1 and I.SITUACAO = 3 , I.QUANTIDADE - S.QUANTIDADE ,
                                                                    IIF( :SITUACAO = 2 and I.SITUACAO < 3 , S.QUANTIDADE ,
                                                                    IIF( :SITUACAO = 3 and I.SITUACAO = 1 , I.QTD_EST    , 0)))) , 0) T12,
                                                 IIF(I.TAMANHO =13, IIF( :SITUACAO = 1 and I.SITUACAO < 3 , I.QUANTIDADE ,
                                                                    IIF( :SITUACAO = 1 and I.SITUACAO = 3 , I.QUANTIDADE - S.QUANTIDADE ,
                                                                    IIF( :SITUACAO = 2 and I.SITUACAO < 3 , S.QUANTIDADE ,
                                                                    IIF( :SITUACAO = 3 and I.SITUACAO = 1 , I.QTD_EST    , 0)))) , 0) T13,
                                                 IIF(I.TAMANHO =14, IIF( :SITUACAO = 1 and I.SITUACAO < 3 , I.QUANTIDADE ,
                                                                    IIF( :SITUACAO = 1 and I.SITUACAO = 3 , I.QUANTIDADE - S.QUANTIDADE ,
                                                                    IIF( :SITUACAO = 2 and I.SITUACAO < 3 , S.QUANTIDADE ,
                                                                    IIF( :SITUACAO = 3 and I.SITUACAO = 1 , I.QTD_EST    , 0)))) , 0) T14,
                                                 IIF(I.TAMANHO =15, IIF( :SITUACAO = 1 and I.SITUACAO < 3 , I.QUANTIDADE ,
                                                                    IIF( :SITUACAO = 1 and I.SITUACAO = 3 , I.QUANTIDADE - S.QUANTIDADE ,
                                                                    IIF( :SITUACAO = 2 and I.SITUACAO < 3 , S.QUANTIDADE ,
                                                                    IIF( :SITUACAO = 3 and I.SITUACAO = 1 , I.QTD_EST    , 0)))) , 0) T15,
                                                 IIF(I.TAMANHO =16, IIF( :SITUACAO = 1 and I.SITUACAO < 3 , I.QUANTIDADE ,
                                                                    IIF( :SITUACAO = 1 and I.SITUACAO = 3 , I.QUANTIDADE - S.QUANTIDADE ,
                                                                    IIF( :SITUACAO = 2 and I.SITUACAO < 3 , S.QUANTIDADE ,
                                                                    IIF( :SITUACAO = 3 and I.SITUACAO = 1 , I.QTD_EST    , 0)))) , 0) T16,
                                                 IIF(I.TAMANHO =17, IIF( :SITUACAO = 1 and I.SITUACAO < 3 , I.QUANTIDADE ,
                                                                    IIF( :SITUACAO = 1 and I.SITUACAO = 3 , I.QUANTIDADE - S.QUANTIDADE ,
                                                                    IIF( :SITUACAO = 2 and I.SITUACAO < 3 , S.QUANTIDADE ,
                                                                    IIF( :SITUACAO = 3 and I.SITUACAO = 1 , I.QTD_EST    , 0)))) , 0) T17,
                                                 IIF(I.TAMANHO =18, IIF( :SITUACAO = 1 and I.SITUACAO < 3 , I.QUANTIDADE ,
                                                                    IIF( :SITUACAO = 1 and I.SITUACAO = 3 , I.QUANTIDADE - S.QUANTIDADE ,
                                                                    IIF( :SITUACAO = 2 and I.SITUACAO < 3 , S.QUANTIDADE ,
                                                                    IIF( :SITUACAO = 3 and I.SITUACAO = 1 , I.QTD_EST    , 0)))) , 0) T18,
                                                 IIF(I.TAMANHO =19, IIF( :SITUACAO = 1 and I.SITUACAO < 3 , I.QUANTIDADE ,
                                                                    IIF( :SITUACAO = 1 and I.SITUACAO = 3 , I.QUANTIDADE - S.QUANTIDADE ,
                                                                    IIF( :SITUACAO = 2 and I.SITUACAO < 3 , S.QUANTIDADE ,
                                                                    IIF( :SITUACAO = 3 and I.SITUACAO = 1 , I.QTD_EST    , 0)))) , 0) T19,
                                                 IIF(I.TAMANHO =20, IIF( :SITUACAO = 1 and I.SITUACAO < 3 , I.QUANTIDADE ,
                                                                    IIF( :SITUACAO = 1 and I.SITUACAO = 3 , I.QUANTIDADE - S.QUANTIDADE ,
                                                                    IIF( :SITUACAO = 2 and I.SITUACAO < 3 , S.QUANTIDADE ,
                                                                    IIF( :SITUACAO = 3 and I.SITUACAO = 1 , I.QTD_EST    , 0)))) , 0) T20,
                                                 G.T01 AS G01, G.T02 AS G02,
                                                 G.T03 AS G03, G.T04 AS G04,
                                                 G.T05 AS G05, G.T06 AS G06,
                                                 G.T07 AS G07, G.T08 AS G08,
                                                 G.T09 AS G09, G.T10 AS G10,
                                                 G.T11 AS G11, G.T12 AS G12,
                                                 G.T13 AS G13, G.T14 AS G14,
                                                 G.T15 AS G15, G.T16 AS G16,
                                                 G.T17 AS G17, G.T18 AS G18,
                                                 G.T19 AS G19, G.T20 AS G20,

                                                 I.VALOR AS VALOR_UNITARIO,
                                                 IIF( :SITUACAO = 1 and I.SITUACAO < 3 , I.QUANTIDADE ,
                                                 IIF( :SITUACAO = 1 and I.SITUACAO = 3 , I.QUANTIDADE - S.QUANTIDADE ,
                                                 IIF( :SITUACAO = 2 and I.SITUACAO < 3 , S.QUANTIDADE,
                                                 IIF( :SITUACAO = 3 and I.SITUACAO = 1 , I.QTD_EST   , 0.00)))) * I.VALOR AS VALOR_SUBTOTAL,

                                                 I.VALOR_DESCONTO / I.QUANTIDADE *
                                                 IIF( :SITUACAO = 1 and I.SITUACAO < 3 , I.QUANTIDADE ,
                                                 IIF( :SITUACAO = 1 and I.SITUACAO = 3 , I.QUANTIDADE - S.QUANTIDADE ,
                                                 IIF( :SITUACAO = 2 and I.SITUACAO < 3 , S.QUANTIDADE,
                                                 IIF( :SITUACAO = 3 and I.SITUACAO = 1 , I.QTD_EST   , 0.00)))) AS VALOR_DESCONTO

                                            FROM TBPEDIDO P, TBPEDIDO_ITEM I, TBPEDIDO_ITEM_SALDO S,
                                                 TBGRADE G, TBCOR C, TBMODELO M
                                           WHERE P.PEDIDO = I.PEDIDO
                                             AND I.CONTROLE = S.PEDIDO_ITEM_CONTROLE
                                             AND M.GRADE_CODIGO = G.CODIGO
                                             AND C.CODIGO = I.COR_ID
                                             AND M.CODIGO = I.MODELO_CODIGO
                                             AND P.DATA BETWEEN :DATA1 AND :DATA2
                                             AND P.PEDIDO BETWEEN :PEDIDO1 AND :PEDIDO2
                                         ) X
                                   GROUP BY X.PEDIDO,
                                            X.MODELO_ID, X.MODELO_DESCRICAO,
                                            X.COR_ID, X.COR_DESCRICAO,
                                            G01,G02,G03,G04,G05,G06,G07,G08,G09,G10,
                                            G11,G12,G13,G14,G15,G16,G17,G18,G19,G20,
                                            VALOR_UNITARIO
                           )
                       ORDER BY PEDIDO, MODELO_ID, COR_DESCRICAO, VALOR_UNITARIO
                   )
           ORDER BY PEDIDO, MODELO_ID, COR_DESCRICAO, VALOR_UNITARIO
        )
    ORDER BY PEDIDO, MODELO_ID, COR_DESCRICAO, VALOR_UNITARIO

    INTO :PEDIDO,:MODELO_ID,:MODELO_DESCRICAO,:COR_ID,:COR_DESCRICAO,
         :T01,:T02,:T03,:T04,:T05,:T06,:T07,:T08,:T09,:T10,
         :T11,:T12,:T13,:T14,:T15,:T16,:T17,:T18,:T19,:T20,
         :G01,:G02,:G03,:G04,:G05,:G06,:G07,:G08,:G09,:G10,
         :G11,:G12,:G13,:G14,:G15,:G16,:G17,:G18,:G19,:G20,
         :PROX_PEDMOD,:PROX_PED,:TMIN,:TMAX,:TAM_MAX_MIN,
         :VALOR_UNITARIO, :VALOR_SUBTOTAL, :VALOR_DESCONTO
     Do Begin

             gTot = 'Total';
             Tot  = t01+t02+t03+t04+t05+t06+t07+t08+t09+t10+
                    t11+t12+t13+t14+t15+t16+t17+t18+t19+t20;

             /* Totais do Modelo */
             VALOR_TOTAL = VALOR_SUBTOTAL - VALOR_DESCONTO;
             vr_subTOT = vr_subTOT + VALOR_SUBTOTAL;
             vr_totDESC = vr_totDESC + VALOR_DESCONTO;
             vr_totGERAL = vr_totGERAL + VALOR_TOTAL;
             /* Totais do Pedido */
             vr_subGTOT = vr_subGTOT + VALOR_SUBTOTAL;
             vr_totGDESC = vr_totGDESC + VALOR_DESCONTO;
             vr_totGGERAL = vr_totGGERAL + VALOR_TOTAL;

                   /* "Puxa" para as primeiras posições os valores > 0 */
                   if (TMIN = 2) Then
                       Begin
                       T01 = T02;  T02 = T03; T03 = T04;  T04 = T05;
                       T05 = T06;  T06 = T07; T07 = T08;  T08 = T09;
                       T09 = T10;  T10 = T11; T11 = T12;  T12 = T13;
                       T13 = T14;  T14 = T15; T15 = T16;  T16 = T17;
                       T17 = T18;  T18 = T19; T19 = T20;  T20 = 0;
                       G01 = G02;  G02 = G03; G03 = G04;  G04 = G05;
                       G05 = G06;  G06 = G07; G07 = G08;  G08 = G09;
                       G09 = G10;  G10 = G11; G11 = G12;  G12 = G13;
                       G13 = G14;  G14 = G15; G15 = G16;  G16 = G17;
                       G17 = G18;  G18 = G19; G19 = G20;  G20 = '';
                       End else
                   if (TMIN = 3) Then
                       Begin
                       T01 = T03; T02 = T04; T03 = T05; T04 = T06;
                       T05 = T07; T06 = T08; T07 = T09; T08 = T10;
                       T09 = T11; T10 = T12; T11 = T13; T12 = T14;
                       T13 = T15; T14 = T16; T15 = T17; T16 = T18;
                       T17 = T19; T18 = T20; T19 = 0;   T20 = 0;
                       G01 = G03; G02 = G04; G03 = G05; G04 = G06;
                       G05 = G07; G06 = G08; G07 = G09; G08 = G10;
                       G09 = G11; G10 = G12; G11 = G13; G12 = G14;
                       G13 = G15; G14 = G16; G15 = G17; G16 = G18;
                       G17 = G19; G18 = G20; G19 = '';   G20 = '';
                       End else
                   if (TMIN = 4) Then
                       Begin
                       T01 = T04; T02 = T05; T03 = T06; T04 = T07;
                       T05 = T08; T06 = T09; T07 = T10; T08 = T11;
                       T09 = T12; T10 = T13; T11 = T14; T12 = T15;
                       T13 = T16; T14 = T17; T15 = T18; T16 = T19;
                       T17 = T20; T18 = 0;   T19 = 0;   T20 = 0;
                       G01 = G04; G02 = G05; G03 = G06; G04 = G07;
                       G05 = G08; G06 = G09; G07 = G10; G08 = G11;
                       G09 = G12; G10 = G13; G11 = G14; G12 = G15;
                       G13 = G16; G14 = G17; G15 = G18; G16 = G19;
                       G17 = G20; G18 = '';   G19 = '';   G20 = '';
                       End else
                   if (TMIN = 5) Then
                       Begin
                       T01 = T05; T02 = T06; T03 = T07; T04 = T08;
                       T05 = T09; T06 = T10; T07 = T11; T08 = T12;
                       T09 = T13; T10 = T14; T11 = T15; T12 = T16;
                       T13 = T17; T14 = T18; T15 = T19; T16 = T20;
                       T17 = 0;   T18 = 0;   T19 = 0;   T20 = 0;
                       G01 = G05; G02 = G06; G03 = G07; G04 = G08;
                       G05 = G09; G06 = G10; G07 = G11; G08 = G12;
                       G09 = G13; G10 = G14; G11 = G15; G12 = G16;
                       G13 = G17; G14 = G18; G15 = G19; G16 = G20;
                       G17 = '';   G18 = '';   G19 = '';   G20 = '';
                       End else
                   if (TMIN = 6) Then
                       Begin
                       T01 = T06; T02 = T07; T03 = T08; T04 = T09;
                       T05 = T10; T06 = T11; T07 = T12; T08 = T13;
                       T09 = T14; T10 = T15; T11 = T16; T12 = T17;
                       T13 = T18; T14 = T19; T15 = T20; T16 = 0;
                       T17 = 0;   T18 = 0;   T19 = 0;   T20 = 0;
                       G01 = G06; G02 = G07; G03 = G08; G04 = G09;
                       G05 = G10; G06 = G11; G07 = G12; G08 = G13;
                       G09 = G14; G10 = G15; G11 = G16; G12 = G17;
                       G13 = G18; G14 = G19; G15 = G20; G16 = '';
                       G17 = '';   G18 = '';   G19 = '';   G20 = '';
                       End else
                   if (TMIN = 7) Then
                       Begin
                       T01 = T07; T02 = T08; T03 = T09; T04 = T10;
                       T05 = T11; T06 = T12; T07 = T13; T08 = T14;
                       T09 = T15; T10 = T16; T11 = T17; T12 = T18;
                       T13 = T19; T14 = T20; T15 = 0;   T16 = 0;
                       T17 = 0;   T18 = 0;   T19 = 0;   T20 = 0;
                       G01 = G07; G02 = G08; G03 = G09; G04 = G10;
                       G05 = G11; G06 = G12; G07 = G13; G08 = G14;
                       G09 = G15; G10 = G16; G11 = G17; G12 = G18;
                       G13 = G19; G14 = G20; G15 = '';   G16 = '';
                       G17 = '';   G18 = '';   G19 = '';   G20 = '';
                       End else
                   if (TMIN = 8) Then
                       Begin
                       T01 = T08; T02 = T09; T03 = T10; T04 = T11;
                       T05 = T12; T06 = T13; T07 = T14; T08 = T15;
                       T09 = T16; T10 = T17; T11 = T18; T12 = T19;
                       T13 = T20; T14 = 0;   T15 = 0;   T16 = 0;
                       T17 = 0;   T18 = 0;   T19 = 0;   T20 = 0;
                       G01 = G08; G02 = G09; G03 = G10; G04 = G11;
                       G05 = G12; G06 = G13; G07 = G14; G08 = G15;
                       G09 = G16; G10 = G17; G11 = G18; G12 = G19;
                       G13 = G20; G14 = '';   G15 = '';   G16 = '';
                       G17 = '';   G18 = '';   G19 = '';   G20 = '';
                       End else
                   if (TMIN = 9) Then
                       Begin
                       T01 = T09; T02 = T10; T03 = T11; T04 = T12;
                       T05 = T13; T06 = T14; T07 = T15; T08 = T16;
                       T09 = T17; T10 = T18; T11 = T19; T12 = T20;
                       T13 = 0;   T14 = 0;   T15 = 0;   T16 = 0;
                       T17 = 0;   T18 = 0;   T19 = 0;   T20 = 0;
                       G01 = G09; G02 = G10; G03 = G11; G04 = G12;
                       G05 = G13; G06 = G14; G07 = G15; G08 = G16;
                       G09 = G17; G10 = G18; G11 = G19; G12 = G20;
                       G13 = '';   G14 = '';   G15 = '';   G16 = '';
                       G17 = '';   G18 = '';   G19 = '';   G20 = '';
                       End else
                   if (TMIN =10) Then
                       Begin
                       T01 = T10; T02 = T11; T03 = T12; T04 = T13;
                       T05 = T14; T06 = T15; T07 = T16; T08 = T17;
                       T09 = T18; T10 = T19; T11 = T20; T12 = 0;
                       T13 = 0;   T14 = 0;   T15 = 0;   T16 = 0;
                       T17 = 0;   T18 = 0;   T19 = 0;   T20 = 0;
                       G01 = G10; G02 = G11; G03 = G12; G04 = G13;
                       G05 = G14; G06 = G15; G07 = G16; G08 = G17;
                       G09 = G18; G10 = G19; G11 = G20; G12 = '';
                       G13 = '';   G14 = '';   G15 = '';   G16 = '';
                       G17 = '';   G18 = '';   G19 = '';   G20 = '';
                       End else
                   if (TMIN =11) Then
                       Begin
                       T01 = T11; T02 = T12; T03 = T13; T04 = T14;
                       T05 = T15; T06 = T16; T07 = T17; T08 = T18;
                       T09 = T19; T10 = T20; T11 = 0;   T12 = 0;
                       T13 = 0;   T14 = 0;   T15 = 0;   T16 = 0;
                       T17 = 0;   T18 = 0;   T19 = 0;   T20 = 0;
                       G01 = G11; G02 = G12; G03 = G13; G04 = G14;
                       G05 = G15; G06 = G16; G07 = G17; G08 = G18;
                       G09 = G19; G10 = G20; G11 = '';   G12 = '';
                       G13 = '';   G14 = '';   G15 = '';   G16 = '';
                       G17 = '';   G18 = '';   G19 = '';   G20 = '';
                       End else
                   if (TMIN =12) Then
                       Begin
                       T01 = T12; T02 = T13; T03 = T14; T04 = T15;
                       T05 = T16; T06 = T17; T07 = T18; T08 = T19;
                       T09 = T20; T10 = 0;   T11 = 0;   T12 = 0;
                       T13 = 0;   T14 = 0;   T15 = 0;   T16 = 0;
                       T17 = 0;   T18 = 0;   T19 = 0;   T20 = 0;
                       G01 = G12; G02 = G13; G03 = G14; G04 = G15;
                       G05 = G16; G06 = G17; G07 = G18; G08 = G19;
                       G09 = G20; G10 = '';   G11 = '';   G12 = '';
                       G13 = '';   G14 = '';   G15 = '';   G16 = '';
                       G17 = '';   G18 = '';   G19 = '';   G20 = '';
                       End else
                   if (TMIN =13) Then
                       Begin
                       T01 = T13; T02 = T14; T03 = T15; T04 = T16;
                       T05 = T17; T06 = T18; T07 = T19; T08 = T20;
                       T09 = 0;   T10 = 0;   T11 = 0;   T12 = 0;
                       T13 = 0;   T14 = 0;   T15 = 0;   T16 = 0;
                       T17 = 0;   T18 = 0;   T19 = 0;   T20 = 0;
                       G01 = G13; G02 = G14; G03 = G15; G04 = G16;
                       G05 = G17; G06 = G18; G07 = G19; G08 = G20;
                       G09 = '';   G10 = '';   G11 = '';   G12 = '';
                       G13 = '';   G14 = '';   G15 = '';   G16 = '';
                       G17 = '';   G18 = '';   G19 = '';   G20 = '';
                       End else
                   if (TMIN =14) Then
                       Begin
                       T01 = T14; T02 = T15; T03 = T16; T04 = T17;
                       T05 = T18; T06 = T19; T07 = T20; T08 = 0;
                       T09 = 0;   T10 = 0;   T11 = 0;   T12 = 0;
                       T13 = 0;   T14 = 0;   T15 = 0;   T16 = 0;
                       T17 = 0;   T18 = 0;   T19 = 0;   T20 = 0;
                       G01 = G14; G02 = G15; G03 = G16; G04 = G17;
                       G05 = G18; G06 = G19; G07 = G20; G08 = '';
                       G09 = '';   G10 = '';   G11 = '';   G12 = '';
                       G13 = '';   G14 = '';   G15 = '';   G16 = '';
                       G17 = '';   G18 = '';   G19 = '';   G20 = '';
                       End else
                   if (TMIN =15) Then
                       Begin
                       T01 = T15; T02 = T16; T03 = T17; T04 = T18;
                       T05 = T19; T06 = T20; T07 = 0;   T08 = 0;
                       T09 = 0;   T10 = 0;   T11 = 0;   T12 = 0;
                       T13 = 0;   T14 = 0;   T15 = 0;   T16 = 0;
                       T17 = 0;   T18 = 0;   T19 = 0;   T20 = 0;
                       G01 = G15; G02 = G16; G03 = G17; G04 = G18;
                       G05 = G19; G06 = G20; G07 = '';   G08 = '';
                       G09 = '';   G10 = '';   G11 = '';   G12 = '';
                       G13 = '';   G14 = '';   G15 = '';   G16 = '';
                       G17 = '';   G18 = '';   G19 = '';   G20 = '';
                       End else
                   if (TMIN =16) Then
                       Begin
                       T01 = T16; T02 = T17; T03 = T18; T04 = T19;
                       T05 = T20; T06 = 0;   T07 = 0;   T08 = 0;
                       T09 = 0;   T10 = 0;   T11 = 0;   T12 = 0;
                       T13 = 0;   T14 = 0;   T15 = 0;   T16 = 0;
                       T17 = 0;   T18 = 0;   T19 = 0;   T20 = 0;
                       G01 = G16; G02 = G17; G03 = G18; G04 = G19;
                       G05 = G20; G06 = '';   G07 = '';   G08 = '';
                       G09 = '';   G10 = '';   G11 = '';   G12 = '';
                       G13 = '';   G14 = '';   G15 = '';   G16 = '';
                       G17 = '';   G18 = '';   G19 = '';   G20 = '';
                       End else
                   if (TMIN =17) Then
                       Begin
                       T01 = T17; T02 = T18; T03 = T19; T04 = T20;
                       T05 = 0;   T06 = 0;   T07 = 0;   T08 = 0;
                       T09 = 0;   T10 = 0;   T11 = 0;   T12 = 0;
                       T13 = 0;   T14 = 0;   T15 = 0;   T16 = 0;
                       T17 = 0;   T18 = 0;   T19 = 0;   T20 = 0;
                       G01 = G17; G02 = G18; G03 = G19; G04 = G20;
                       G05 = '';   G06 = '';   G07 = '';   G08 = '';
                       G09 = '';   G10 = '';   G11 = '';   G12 = '';
                       G13 = '';   G14 = '';   G15 = '';   G16 = '';
                       G17 = '';   G18 = '';   G19 = '';   G20 = '';
                       End else
                   if (TMIN =18) Then
                       Begin
                       T01 = T18; T02 = T19; T03 = T20; T04 = 0;
                       T05 = 0;   T06 = 0;   T07 = 0;   T08 = 0;
                       T09 = 0;   T10 = 0;   T11 = 0;   T12 = 0;
                       T13 = 0;   T14 = 0;   T15 = 0;   T16 = 0;
                       T17 = 0;   T18 = 0;   T19 = 0;   T20 = 0;
                       G01 = G18; G02 = G19; G03 = G20; G04 = '';
                       G05 = '';   G06 = '';   G07 = '';   G08 = '';
                       G09 = '';   G10 = '';   G11 = '';   G12 = '';
                       G13 = '';   G14 = '';   G15 = '';   G16 = '';
                       G17 = '';   G18 = '';   G19 = '';   G20 = '';
                       End else
                   if (TMIN =19) Then
                       Begin
                       T01 = T19; T02 = T20;  T03 = 0;   T04 = 0;
                       T05 = 0;   T06 = 0;    T07 = 0;   T08 = 0;
                       T09 = 0;   T10 = 0;    T11 = 0;   T12 = 0;
                       T13 = 0;   T14 = 0;    T15 = 0;   T16 = 0;
                       T17 = 0;   T18 = 0;    T19 = 0;   T20 = 0;
                       G01 = G19; G02 = G20;  G03 = '';   G04 = '';
                       G05 = '';   G06 = '';    G07 = '';   G08 = '';
                       G09 = '';   G10 = '';    G11 = '';   G12 = '';
                       G13 = '';   G14 = '';    G15 = '';   G16 = '';
                       G17 = '';   G18 = '';    G19 = '';   G20 = '';
                       End else
                   if (TMIN =20) Then
                       Begin
                       T01 = T20;  T02 = 0;  T03 = 0;    T04 = 0;
                       T05 = 0;    T06 = 0;  T07 = 0;    T08 = 0;
                       T09 = 0;    T10 = 0;  T11 = 0;    T12 = 0;
                       T13 = 0;    T14 = 0;  T15 = 0;    T16 = 0;
                       T17 = 0;    T18 = 0;  T19 = 0;    T20 = 0;
                       G01 = G20;  G02 = '';  G03 = '';    G04 = '';
                       G05 = '';    G06 = '';  G07 = '';    G08 = '';
                       G09 = '';    G10 = '';  G11 = '';    G12 = '';
                       G13 = '';    G14 = '';  G15 = '';    G16 = '';
                       G17 = '';    G18 = '';  G19 = '';    G20 = '';
                       end

                   /* Calcula os Totalizadores */
                   /* Totais do Modelo */
                   q01 = q01 + :t01;
                   q02 = q02 + :t02;
                   q03 = q03 + :t03;
                   q04 = q04 + :t04;
                   q05 = q05 + :t05;
                   q06 = q06 + :t06;
                   q07 = q07 + :t07;
                   q08 = q08 + :t08;
                   q09 = q09 + :t09;
                   q10 = q10 + :t10;
                   q11 = q11 + :t11;
                   q12 = q12 + :t12;
                   q13 = q13 + :t13;
                   q14 = q14 + :t14;
                   q15 = q15 + :t15;
                   q16 = q16 + :t16;
                   q17 = q17 + :t17;
                   q18 = q18 + :t18;
                   q19 = q19 + :t19;
                   q20 = q20 + :t20;
                   qTot= q01+q02+q03+q04+q05+q06+q07+q08+q09+q10+
                         q11+q12+q13+q14+q15+q16+q17+q18+q19+q20;

                   /* Limpa os Tamanhos e Grades das últimas posições sem salores */
                   if (TMAX - TMIN + 1 = 1) Then
                       Begin
                       T02 = null; G02 = '';  
                       T03 = null; G03 = '';  T04 = null; G04 = '';
                       T05 = null; G05 = '';  T06 = null; G06 = '';
                       T07 = null; G07 = '';  T08 = null; G08 = '';
                       T09 = null; G09 = '';  T10 = null; G10 = '';
                       T11 = null; G11 = '';  T12 = null; G12 = '';
                       T13 = null; G13 = '';  T14 = null; G14 = '';
                       T15 = null; G15 = '';  T16 = null; G16 = '';
                       T17 = null; G17 = '';  T18 = null; G18 = '';
                       T19 = null; G19 = '';  T20 = null; G20 = '';
                       end else
                   if (TMAX - TMIN + 1 = 2) Then
                       Begin
                       T03 = null; G03 = '';  T04 = null; G04 = '';
                       T05 = null; G05 = '';  T06 = null; G06 = '';
                       T07 = null; G07 = '';  T08 = null; G08 = '';
                       T09 = null; G09 = '';  T10 = null; G10 = '';
                       T11 = null; G11 = '';  T12 = null; G12 = '';
                       T13 = null; G13 = '';  T14 = null; G14 = '';
                       T15 = null; G15 = '';  T16 = null; G16 = '';
                       T17 = null; G17 = '';  T18 = null; G18 = '';
                       T19 = null; G19 = '';  T20 = null; G20 = '';
                       end else
                   if (TMAX - TMIN + 1 = 3) Then
                       Begin
                       T04 = null; G04 = '';
                       T05 = null; G05 = '';  T06 = null; G06 = '';
                       T07 = null; G07 = '';  T08 = null; G08 = '';
                       T09 = null; G09 = '';  T10 = null; G10 = '';
                       T11 = null; G11 = '';  T12 = null; G12 = '';
                       T13 = null; G13 = '';  T14 = null; G14 = '';
                       T15 = null; G15 = '';  T16 = null; G16 = '';
                       T17 = null; G17 = '';  T18 = null; G18 = '';
                       T19 = null; G19 = '';  T20 = null; G20 = '';
                       end else
                   if (TMAX - TMIN + 1 = 4) Then
                       Begin
                       T05 = null; G05 = '';  T06 = null; G06 = '';
                       T07 = null; G07 = '';  T08 = null; G08 = '';
                       T09 = null; G09 = '';  T10 = null; G10 = '';
                       T11 = null; G11 = '';  T12 = null; G12 = '';
                       T13 = null; G13 = '';  T14 = null; G14 = '';
                       T15 = null; G15 = '';  T16 = null; G16 = '';
                       T17 = null; G17 = '';  T18 = null; G18 = '';
                       T19 = null; G19 = '';  T20 = null; G20 = '';
                       end else
                   if (TMAX - TMIN + 1 = 5) Then
                       Begin
                       T06 = null; G06 = '';
                       T07 = null; G07 = '';  T08 = null; G08 = '';
                       T09 = null; G09 = '';  T10 = null; G10 = '';
                       T11 = null; G11 = '';  T12 = null; G12 = '';
                       T13 = null; G13 = '';  T14 = null; G14 = '';
                       T15 = null; G15 = '';  T16 = null; G16 = '';
                       T17 = null; G17 = '';  T18 = null; G18 = '';
                       T19 = null; G19 = '';  T20 = null; G20 = '';
                       end else
                   if (TMAX - TMIN + 1 = 6) Then
                       Begin
                       T07 = null; G07 = '';  T08 = null; G08 = '';
                       T09 = null; G09 = '';  T10 = null; G10 = '';
                       T11 = null; G11 = '';  T12 = null; G12 = '';
                       T13 = null; G13 = '';  T14 = null; G14 = '';
                       T15 = null; G15 = '';  T16 = null; G16 = '';
                       T17 = null; G17 = '';  T18 = null; G18 = '';
                       T19 = null; G19 = '';  T20 = null; G20 = '';
                       end else
                   if (TMAX - TMIN + 1 = 7) Then
                       Begin
                       T08 = null; G08 = '';
                       T09 = null; G09 = '';  T10 = null; G10 = '';
                       T11 = null; G11 = '';  T12 = null; G12 = '';
                       T13 = null; G13 = '';  T14 = null; G14 = '';
                       T15 = null; G15 = '';  T16 = null; G16 = '';
                       T17 = null; G17 = '';  T18 = null; G18 = '';
                       T19 = null; G19 = '';  T20 = null; G20 = '';
                       end else
                   if (TMAX - TMIN + 1 = 8) Then
                       Begin
                       T09 = null; G09 = '';  T10 = null; G10 = '';
                       T11 = null; G11 = '';  T12 = null; G12 = '';
                       T13 = null; G13 = '';  T14 = null; G14 = '';
                       T15 = null; G15 = '';  T16 = null; G16 = '';
                       T17 = null; G17 = '';  T18 = null; G18 = '';
                       T19 = null; G19 = '';  T20 = null; G20 = '';
                       end else
                   if (TMAX - TMIN + 1 = 9) Then
                       Begin
                       T10 = null; G10 = '';
                       T11 = null; G11 = '';  T12 = null; G12 = '';
                       T13 = null; G13 = '';  T14 = null; G14 = '';
                       T15 = null; G15 = '';  T16 = null; G16 = '';
                       T17 = null; G17 = '';  T18 = null; G18 = '';
                       T19 = null; G19 = '';  T20 = null; G20 = '';
                       end else
                   if (TMAX - TMIN + 1 =10) Then
                       Begin
                       T11 = null; G11 = '';  T12 = null; G12 = '';
                       T13 = null; G13 = '';  T14 = null; G14 = '';
                       T15 = null; G15 = '';  T16 = null; G16 = '';
                       T17 = null; G17 = '';  T18 = null; G18 = '';
                       T19 = null; G19 = '';  T20 = null; G20 = '';
                       end else
                   if (TMAX - TMIN + 1 =11) Then
                       Begin
                       T12 = null; G12 = '';
                       T13 = null; G13 = '';  T14 = null; G14 = '';
                       T15 = null; G15 = '';  T16 = null; G16 = '';
                       T17 = null; G17 = '';  T18 = null; G18 = '';
                       T19 = null; G19 = '';  T20 = null; G20 = '';
                       end else
                   if (TMAX - TMIN + 1 =12) Then
                       Begin
                       T13 = null; G13 = '';  T14 = null; G14 = '';
                       T15 = null; G15 = '';  T16 = null; G16 = '';
                       T17 = null; G17 = '';  T18 = null; G18 = '';
                       T19 = null; G19 = '';  T20 = null; G20 = '';
                       end else
                   if (TMAX - TMIN + 1 =13) Then
                       Begin
                       T14 = null; G14 = '';
                       T15 = null; G15 = '';  T16 = null; G16 = '';
                       T17 = null; G17 = '';  T18 = null; G18 = '';
                       T19 = null; G19 = '';  T20 = null; G20 = '';
                       end else
                   if (TMAX - TMIN + 1 =14) Then
                       Begin
                       T15 = null; G15 = '';  T16 = null; G16 = '';
                       T17 = null; G17 = '';  T18 = null; G18 = '';
                       T19 = null; G19 = '';  T20 = null; G20 = '';
                       end else
                   if (TMAX - TMIN + 1 =15) Then
                       Begin
                       T16 = null; G16 = '';
                       T17 = null; G17 = '';  T18 = null; G18 = '';
                       T19 = null; G19 = '';  T20 = null; G20 = '';
                       end else
                   if (TMAX - TMIN + 1 =16) Then
                       Begin
                       T17 = null; G17 = '';  T18 = null; G18 = '';
                       T19 = null; G19 = '';  T20 = null; G20 = '';
                       end else
                   if (TMAX - TMIN + 1 =17) Then
                       Begin
                       T18 = null; G18 = '';
                       T19 = null; G19 = '';  T20 = null; G20 = '';
                       end else
                   if (TMAX - TMIN + 1 =18) Then
                       Begin
                       T19 = null; G19 = '';  T20 = null; G20 = '';
                       end else
                   if (TMAX - TMIN + 1 =19) Then
                       Begin
                       T20 = null; G20 = '';
                       end

                   IF(TOT > 0) THEN SUSPEND;

                   IF ( (PEDIDO||'-'||MODELO_ID||'-'||VALOR_UNITARIO <> PROX_PEDMOD) OR (PROX_PEDMOD IS NULL) ) THEN
                       Begin
                         /* preenche o registro do totalizador no pedido por modelo */
                         :COR_ID = null;
                         :COR_DESCRICAO = '<b>TOTAL<b>';
                         t01 = q01; t02 = q02; t03 = q03; t04 = q04;
                         t05 = q05; t06 = q06; t07 = q07; t08 = q08;
                         t09 = q09; t10 = q10; t11 = q11; t12 = q12;
                         t13 = q13; t14 = q14; t15 = q15; t16 = q16;
                         t17 = q17; t18 = q18; t19 = q19; t20 = q20;
                         Tot = qTot;
                         :VALOR_UNITARIO = null;
                         :VALOR_SUBTOTAL = vr_subTOT;
                         :VALOR_DESCONTO = vr_totDESC;
                         :VALOR_TOTAL    = vr_totGERAL;


                           /* Limpa os Tamanhos das últimas posições sem salores */
                           if (TMAX - TMIN + 1 = 1) Then
                               Begin
                               T02 = null; T03 = null; T04 = null; T05 = null;
                               T06 = null; T07 = null; T08 = null; T09 = null;
                               T10 = null; T11 = null; T12 = null; T13 = null;
                               T14 = null; T15 = null; T16 = null; T17 = null;
                               T18 = null; T19 = null; T20 = null;
                               end else
                           if (TMAX - TMIN + 1 = 2) Then
                               Begin
                               T03 = null; T04 = null; T05 = null; T06 = null;
                               T07 = null; T08 = null; T09 = null; T10 = null;
                               T11 = null; T12 = null; T13 = null; T14 = null;
                               T15 = null; T16 = null; T17 = null; T18 = null;
                               T19 = null; T20 = null;
                               end else
                           if (TMAX - TMIN + 1 = 3) Then
                               Begin
                               T04 = null;
                               T05 = null; T06 = null; T07 = null; T08 = null;
                               T09 = null; T10 = null; T11 = null; T12 = null;
                               T13 = null; T14 = null; T15 = null; T16 = null;
                               T17 = null; T18 = null; T19 = null; T20 = null;
                               end else
                           if (TMAX - TMIN + 1 = 4) Then
                               Begin
                               T05 = null; T06 = null; T07 = null; T08 = null;
                               T09 = null; T10 = null; T11 = null; T12 = null;
                               T13 = null; T14 = null; T15 = null; T16 = null;
                               T17 = null; T18 = null; T19 = null; T20 = null;
                               end else
                           if (TMAX - TMIN + 1 = 5) Then
                               Begin
                               T06 = null; T07 = null; T08 = null; T09 = null;
                               T10 = null; T11 = null; T12 = null; T13 = null;
                               T14 = null; T15 = null; T16 = null; T17 = null;
                               T18 = null; T19 = null; T20 = null;
                               end else
                           if (TMAX - TMIN + 1 = 6) Then
                               Begin
                               T07 = null; T08 = null; T09 = null; T10 = null;
                               T11 = null; T12 = null; T13 = null; T14 = null;
                               T15 = null; T16 = null; T17 = null; T18 = null;
                               T19 = null; T20 = null;
                               end else
                           if (TMAX - TMIN + 1 = 7) Then
                               Begin
                               T08 = null;
                               T09 = null; T10 = null; T11 = null; T12 = null;
                               T13 = null; T14 = null; T15 = null; T16 = null;
                               T17 = null; T18 = null; T19 = null; T20 = null;
                               end else
                           if (TMAX - TMIN + 1 = 8) Then
                               Begin
                               T09 = null; T10 = null; T11 = null; T12 = null;
                               T13 = null; T14 = null; T15 = null; T16 = null;
                               T17 = null; T18 = null; T19 = null; T20 = null;
                               end else
                           if (TMAX - TMIN + 1 = 9) Then
                               Begin
                               T10 = null; T11 = null; T12 = null; T13 = null;
                               T14 = null; T15 = null; T16 = null; T17 = null;
                               T18 = null; T19 = null; T20 = null;
                               end else
                           if (TMAX - TMIN + 1 =10) Then
                               Begin
                               T11 = null; T12 = null; T13 = null; T14 = null;
                               T15 = null; T16 = null; T17 = null; T18 = null;
                               T19 = null; T20 = null;
                               end else
                           if (TMAX - TMIN + 1 =11) Then
                               Begin
                               T12 = null; T13 = null; T14 = null; T15 = null;
                               T16 = null; T17 = null; T18 = null; T19 = null;
                               T20 = null;
                               end else
                           if (TMAX - TMIN + 1 =12) Then
                               Begin
                               T13 = null; T14 = null; T15 = null; T16 = null;
                               T17 = null; T18 = null; T19 = null; T20 = null;
                               end else
                           if (TMAX - TMIN + 1 =13) Then
                               Begin
                               T14 = null; T15 = null; T16 = null; T17 = null;
                               T18 = null; T19 = null; T20 = null;
                               end else
                           if (TMAX - TMIN + 1 =14) Then
                               Begin
                               T15 = null; T16 = null; T17 = null; T18 = null;
                               T19 = null; T20 = null;
                               end else
                           if (TMAX - TMIN + 1 =15) Then
                               Begin
                               T16 = null; T17 = null; T18 = null; T19 = null;
                               T20 = null;
                               end else
                           if (TMAX - TMIN + 1 =16) Then
                               Begin
                               T17 = null; T18 = null; T19 = null; T20 = null;
                               end else
                           if (TMAX - TMIN + 1 =17) Then
                               Begin
                               T18 = null; T19 = null; T20 = null;
                               end else
                           if (TMAX - TMIN + 1 =18) Then
                               Begin
                               T19 = null; T20 = null;
                               end else
                           if (TMAX - TMIN + 1 =19) Then
                               Begin
                               T20 = null;
                               end

                         IF(TOT > 0) THEN
                            Begin
                            /* Total do Pedido */
                            gqTot = gqTot + Tot;
                            SUSPEND;
                            End

                         q01 = 0; q02 = 0; q03 = 0; q04 = 0;
                         q05 = 0; q06 = 0; q07 = 0; q08 = 0;
                         q09 = 0; q10 = 0; q11 = 0; q12 = 0;
                         q13 = 0; q14 = 0; q15 = 0; q16 = 0;
                         q17 = 0; q18 = 0; q19 = 0; q20 = 0;
                         qTot= 0;
                         vr_subTOT = 0;
                         vr_totDESC = 0;
                         vr_totGERAL = 0;

                       End

                   IF ( (PEDIDO <> PROX_PED) OR (PROX_PED IS NULL) ) THEN
                       Begin
                           /* preenche o registro do totalizador do pedido */
                           :COR_ID = null;
                           :COR_DESCRICAO = '<b>***** TOTAL DO PEDIDO *****<b>';
                           Tot = gqTot;
                           :VALOR_UNITARIO = null;
                           :VALOR_SUBTOTAL = vr_subGTOT;
                           :VALOR_DESCONTO = vr_totGDESC;
                           :VALOR_TOTAL    = vr_totGGERAL;

                           /* Limpa os Tamanhos e Grades */
                           T01 = null; T02 = null; T03 = null; T04 = null;
                           T05 = null; T06 = null; T07 = null; T08 = null;
                           T09 = null; T10 = null; T11 = null; T12 = null;
                           T13 = null; T14 = null; T15 = null; T16 = null;
                           T17 = null; T18 = null; T19 = null; T20 = null;

                           IF(gqTOT > 0) THEN SUSPEND;
                           gqTot= 0;
                           vr_subGTOT = 0;
                           vr_totGDESC = 0;
                           vr_totGGERAL = 0;
                       End


                End
End;