SET TERM ^ ;

CREATE OR ALTER procedure SPC_COMANDA_GRADE (
    DATA1 date,
    DATA2 date,
    PEDIDO1 integer not null = 0,
    PEDIDO2 integer not null = 0,
    SITUACAO integer = 1,
    COMANDA_ID2 integer = 0)
returns (
    PEDIDO integer,
    MODELO_ID integer,
    MODELO_DESCRICAO varchar(100),
    COR_ID integer,
    COR_DESCRICAO varchar(50),
    COMANDA_ID integer,
    COMANDA 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 PROX_COMANDA varchar(50);
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 C01 numeric(15,0);
declare variable C02 numeric(15,0);
declare variable C03 numeric(15,0);
declare variable C04 numeric(15,0);
declare variable C05 numeric(15,0);
declare variable C06 numeric(15,0);
declare variable C07 numeric(15,0);
declare variable C08 numeric(15,0);
declare variable C09 numeric(15,0);
declare variable C10 numeric(15,0);
declare variable C11 numeric(15,0);
declare variable C12 numeric(15,0);
declare variable C13 numeric(15,0);
declare variable C14 numeric(15,0);
declare variable C15 numeric(15,0);
declare variable C16 numeric(15,0);
declare variable C17 numeric(15,0);
declare variable C18 numeric(15,0);
declare variable C19 numeric(15,0);
declare variable C20 numeric(15,0);
declare variable QTOT numeric(15,0); /* TOTAL DO MODELO */
declare variable CQTOT numeric(15,0); /* TOTAL DA COMANDA */
declare variable GQTOT numeric(15,0); /* TOTAL DO PEDIDO */
declare variable VR_SUBTOT numeric(15,2); /* VR SUB TOTAL DO MODELO */
declare variable VR_TOTDESC numeric(15,2); /* VR TOTAL DO DESCONTO DO MODELO */
declare variable VR_TOTGERAL numeric(15,2); /* VR TOTAL DO MODELO */
declare variable VR_SUBCTOT numeric(15,2); /* VR SUB TOTAL DA COMANDA */
declare variable VR_TOTCDESC numeric(15,2); /* VR TOTAL DO DESCONTO DA COMANDA */
declare variable VR_TOTCGERAL numeric(15,2); /* VR TOTAL DA COMANDA */
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

    IF (:DATA1 IS NULL) THEN
    BEGIN
       :DATA1 = '01.01.2000';
    END

    IF (:DATA2 IS NULL) THEN
    BEGIN
       :DATA2 = '31.12.2999';
    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;

    C01 = 0; C02 = 0; C03 = 0; C04 = 0; C05 = 0;
    C06 = 0; C07 = 0; C08 = 0; C09 = 0; C10 = 0;
    C11 = 0; C12 = 0; C13 = 0; C14 = 0; C15 = 0;
    C16 = 0; C17 = 0; C18 = 0; C19 = 0; C20 = 0;

    QTOT  = 0;
    CQTOT = 0;
    GQTOT = 0;

    VR_SUBTOT   = 0;
    VR_TOTDESC  = 0;
    VR_TOTGERAL = 0;

    VR_SUBCTOT   = 0;
    VR_TOTCDESC  = 0;
    VR_TOTCGERAL = 0;

    VR_SUBGTOT   = 0;
    VR_TOTGDESC  = 0;
    VR_TOTGGERAL = 0;

    FOR
        SELECT
            PEDIDO,
            MODELO_ID,
            MODELO_DESCRICAO,
            COR_ID,
            COR_DESCRICAO,
            COMANDA_ID,
            COMANDA,
        
            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,
            PROX_COMANDA,
        
            TMIN,
            TMAX,
            MAX(TAM_MAX_MIN) OVER (),
        
            VALOR_UNITARIO,
            VALOR_SUBTOTAL,
            VALOR_DESCONTO
        
        FROM (
            SELECT
                PEDIDO,
                MODELO_ID,
                MODELO_DESCRICAO,
                COR_ID,
                COR_DESCRICAO,
                COMANDA_ID,
                COMANDA,
        
                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,
                PROX_COMANDA,
        
                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,
                    COMANDA_ID,
                    COMANDA,
        
                    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, COMANDA_ID, MODELO_ID, COR_DESCRICAO) PROX_PEDMOD,
                    LEAD (PEDIDO) OVER (ORDER BY PEDIDO, COMANDA_ID, MODELO_ID, COR_DESCRICAO, VALOR_UNITARIO) PROX_PED,
                    LEAD (COMANDA) OVER (ORDER BY PEDIDO, COMANDA_ID, MODELO_ID, COR_DESCRICAO) PROX_COMANDA,
        
                    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,
                        X.COMANDA_ID,
                        X.COMANDA,
        
                        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,
        
                        MIN(VALOR_UNITARIO) 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,
                            A.ESTOQUE_DETALHE_ID AS COMANDA_ID,
                            COALESCE((SELECT FIRST 1 FN_LPAD(A2.PEDIDO, 8, '0')||'-'||FN_LPAD(A2.SEQUENCIA, 3, '0') FROM TBESTOQUE_DETALHE A2 WHERE A2.ID = A.ESTOQUE_DETALHE_ID), '') AS COMANDA,
        
                            IIF(A.TAMANHO = 1 , IIF(:SITUACAO <> 0, IIF(:SITUACAO = 1, A.QUANTIDADE, IIF(:SITUACAO = 2, A.SALDO, IIF(:SITUACAO = 3, I.QUANTIDADE, 0))), A.QUANTIDADE), 0) T01,
                            IIF(A.TAMANHO = 2 , IIF(:SITUACAO <> 0, IIF(:SITUACAO = 1, A.QUANTIDADE, IIF(:SITUACAO = 2, A.SALDO, IIF(:SITUACAO = 3, I.QUANTIDADE, 0))), A.QUANTIDADE), 0) T02,
                            IIF(A.TAMANHO = 3 , IIF(:SITUACAO <> 0, IIF(:SITUACAO = 1, A.QUANTIDADE, IIF(:SITUACAO = 2, A.SALDO, IIF(:SITUACAO = 3, I.QUANTIDADE, 0))), A.QUANTIDADE), 0) T03,
                            IIF(A.TAMANHO = 4 , IIF(:SITUACAO <> 0, IIF(:SITUACAO = 1, A.QUANTIDADE, IIF(:SITUACAO = 2, A.SALDO, IIF(:SITUACAO = 3, I.QUANTIDADE, 0))), A.QUANTIDADE), 0) T04,
                            IIF(A.TAMANHO = 5 , IIF(:SITUACAO <> 0, IIF(:SITUACAO = 1, A.QUANTIDADE, IIF(:SITUACAO = 2, A.SALDO, IIF(:SITUACAO = 3, I.QUANTIDADE, 0))), A.QUANTIDADE), 0) T05,
                            IIF(A.TAMANHO = 6 , IIF(:SITUACAO <> 0, IIF(:SITUACAO = 1, A.QUANTIDADE, IIF(:SITUACAO = 2, A.SALDO, IIF(:SITUACAO = 3, I.QUANTIDADE, 0))), A.QUANTIDADE), 0) T06,
                            IIF(A.TAMANHO = 7 , IIF(:SITUACAO <> 0, IIF(:SITUACAO = 1, A.QUANTIDADE, IIF(:SITUACAO = 2, A.SALDO, IIF(:SITUACAO = 3, I.QUANTIDADE, 0))), A.QUANTIDADE), 0) T07,
                            IIF(A.TAMANHO = 8 , IIF(:SITUACAO <> 0, IIF(:SITUACAO = 1, A.QUANTIDADE, IIF(:SITUACAO = 2, A.SALDO, IIF(:SITUACAO = 3, I.QUANTIDADE, 0))), A.QUANTIDADE), 0) T08,
                            IIF(A.TAMANHO = 9 , IIF(:SITUACAO <> 0, IIF(:SITUACAO = 1, A.QUANTIDADE, IIF(:SITUACAO = 2, A.SALDO, IIF(:SITUACAO = 3, I.QUANTIDADE, 0))), A.QUANTIDADE), 0) T09,
                            IIF(A.TAMANHO = 10, IIF(:SITUACAO <> 0, IIF(:SITUACAO = 1, A.QUANTIDADE, IIF(:SITUACAO = 2, A.SALDO, IIF(:SITUACAO = 3, I.QUANTIDADE, 0))), A.QUANTIDADE), 0) T10,
                            IIF(A.TAMANHO = 11, IIF(:SITUACAO <> 0, IIF(:SITUACAO = 1, A.QUANTIDADE, IIF(:SITUACAO = 2, A.SALDO, IIF(:SITUACAO = 3, I.QUANTIDADE, 0))), A.QUANTIDADE), 0) T11,
                            IIF(A.TAMANHO = 12, IIF(:SITUACAO <> 0, IIF(:SITUACAO = 1, A.QUANTIDADE, IIF(:SITUACAO = 2, A.SALDO, IIF(:SITUACAO = 3, I.QUANTIDADE, 0))), A.QUANTIDADE), 0) T12,
                            IIF(A.TAMANHO = 13, IIF(:SITUACAO <> 0, IIF(:SITUACAO = 1, A.QUANTIDADE, IIF(:SITUACAO = 2, A.SALDO, IIF(:SITUACAO = 3, I.QUANTIDADE, 0))), A.QUANTIDADE), 0) T13,
                            IIF(A.TAMANHO = 14, IIF(:SITUACAO <> 0, IIF(:SITUACAO = 1, A.QUANTIDADE, IIF(:SITUACAO = 2, A.SALDO, IIF(:SITUACAO = 3, I.QUANTIDADE, 0))), A.QUANTIDADE), 0) T14,
                            IIF(A.TAMANHO = 15, IIF(:SITUACAO <> 0, IIF(:SITUACAO = 1, A.QUANTIDADE, IIF(:SITUACAO = 2, A.SALDO, IIF(:SITUACAO = 3, I.QUANTIDADE, 0))), A.QUANTIDADE), 0) T15,
                            IIF(A.TAMANHO = 16, IIF(:SITUACAO <> 0, IIF(:SITUACAO = 1, A.QUANTIDADE, IIF(:SITUACAO = 2, A.SALDO, IIF(:SITUACAO = 3, I.QUANTIDADE, 0))), A.QUANTIDADE), 0) T16,
                            IIF(A.TAMANHO = 17, IIF(:SITUACAO <> 0, IIF(:SITUACAO = 1, A.QUANTIDADE, IIF(:SITUACAO = 2, A.SALDO, IIF(:SITUACAO = 3, I.QUANTIDADE, 0))), A.QUANTIDADE), 0) T17,
                            IIF(A.TAMANHO = 18, IIF(:SITUACAO <> 0, IIF(:SITUACAO = 1, A.QUANTIDADE, IIF(:SITUACAO = 2, A.SALDO, IIF(:SITUACAO = 3, I.QUANTIDADE, 0))), A.QUANTIDADE), 0) T18,
                            IIF(A.TAMANHO = 19, IIF(:SITUACAO <> 0, IIF(:SITUACAO = 1, A.QUANTIDADE, IIF(:SITUACAO = 2, A.SALDO, IIF(:SITUACAO = 3, I.QUANTIDADE, 0))), A.QUANTIDADE), 0) T19,
                            IIF(A.TAMANHO = 20, IIF(:SITUACAO <> 0, IIF(:SITUACAO = 1, A.QUANTIDADE, IIF(:SITUACAO = 2, A.SALDO, IIF(:SITUACAO = 3, I.QUANTIDADE, 0))), A.QUANTIDADE), 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 <> 0,
                                IIF(:SITUACAO = 1, A.QUANTIDADE,
                                IIF(:SITUACAO = 2, A.SALDO,
                                IIF(:SITUACAO = 3, I.QUANTIDADE, 0.00)))
                                , A.QUANTIDADE) * I.VALOR AS VALOR_SUBTOTAL,
                            
                            I.VALOR_DESCONTO / I.QUANTIDADE *
                            IIF(:SITUACAO <> 0,
                                IIF(:SITUACAO = 1, A.QUANTIDADE,
                                IIF(:SITUACAO = 2, A.SALDO,
                                IIF(:SITUACAO = 3, I.QUANTIDADE, 0.00)))
                                , I.QUANTIDADE) AS VALOR_DESCONTO
        
                        FROM TBPEDIDO P, TBPEDIDO_ITEM I, TBPEDIDO_ITEM_SALDO S, TBGRADE G, TBCOR C, TBMODELO M, TBESTOQUE_DETALHE_ALOCACAO A
                        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 A.TAB_ITEM_ID = I.CONTROLE
                        AND A.TABELA_ID = P.PEDIDO
                        AND A.TIPO = 'P'
                        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,
                        X.COMANDA_ID,
                        X.COMANDA,
        
                        G01, G02, G03, G04, G05,
                        G06, G07, G08, G09, G10,
                        G11, G12, G13, G14, G15,
                        G16, G17, G18, G19, G20
        
                    HAVING X.COMANDA_ID > 0 AND (IIF(:COMANDA_ID2 > 0, X.COMANDA_ID = :COMANDA_ID2, 1 = 1))
                )
            )
        
            ORDER BY
                PEDIDO,
                COMANDA_ID,
                MODELO_ID,
                COR_DESCRICAO
        
        )

    INTO
        :PEDIDO,
        :MODELO_ID,
        :MODELO_DESCRICAO,
        :COR_ID,
        :COR_DESCRICAO,
        :COMANDA_ID,
        :COMANDA,

        :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,
        :PROX_COMANDA,

        :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 DA COMANDA */
        VR_SUBCTOT   = VR_SUBCTOT   + VALOR_SUBTOTAL;
        VR_TOTCDESC  = VR_TOTCDESC  + VALOR_DESCONTO;
        VR_TOTCGERAL = VR_TOTCGERAL + 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 posicoes 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;

        /* Totais da Comanda */
        C01 = C01 + :T01;
        C02 = C02 + :T02;
        C03 = C03 + :T03;
        C04 = C04 + :T04;
        C05 = C05 + :T05;
        C06 = C06 + :T06;
        C07 = C07 + :T07;
        C08 = C08 + :T08;
        C09 = C09 + :T09;
        C10 = C10 + :T10;
        C11 = C11 + :T11;
        C12 = C12 + :T12;
        C13 = C13 + :T13;
        C14 = C14 + :T14;
        C15 = C15 + :T15;
        C16 = C16 + :T16;
        C17 = C17 + :T17;
        C18 = C18 + :T18;
        C19 = C19 + :T19;
        C20 = C20 + :T20;
        CQTOT= C01 + C02 + C03 + C04 + C05 + C06 + C07 + C08 + C09 + C10 + C11 + C12 + C13 + C14 + C15 + C16 + C17 + C18 + C19 + C20;
        
        /* Limpa os Tamanhos e Grades das ultimas posicoes sem valores */
        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
        BEGIN
            SUSPEND;
        END

        IF ((PEDIDO||'-'||MODELO_ID/*||'-'||VALOR_UNITARIO*/ <> PROX_PEDMOD) OR (PROX_PEDMOD IS NULL)) THEN
        BEGIN
            GTOT = 'MOD. TOTAL';

            /* preenche o registro do totalizador no pedido por modelo */
            :COR_ID = NULL;
            :COR_DESCRICAO = 'TOTAL';
            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 ultimas posicoes sem valores */
            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 Modelo */
                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 ((COMANDA <> PROX_COMANDA) OR (PROX_COMANDA IS NULL)) THEN
        BEGIN
            GTOT = 'COM. TOTAL';

            /* preenche o registro do totalizador no pedido por comanda */
            :MODELO_ID = NULL;
            :MODELO_DESCRICAO = NULL;
            :COR_ID = NULL;
            :COR_DESCRICAO = 'TOTAL DA COMANDA';
            T01 = C01; T02 = C02; T03 = C03; T04 = C04;
            T05 = C05; T06 = C06; T07 = C07; T08 = C08;
            T09 = C09; T10 = C10; T11 = C11; T12 = C12;
            T13 = C13; T14 = C14; T15 = C15; T16 = C16;
            T17 = C17; T18 = C18; T19 = C19; T20 = C20;
            TOT = CQTOT;
            :VALOR_UNITARIO = NULL;
            :VALOR_SUBTOTAL = VR_SUBCTOT;
            :VALOR_DESCONTO = VR_TOTCDESC;
            :VALOR_TOTAL    = VR_TOTCGERAL;


            /* Limpa os Tamanhos das ultimas posicoes sem salores */
            IF (TAM_MAX_MIN = 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 (TAM_MAX_MIN = 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 (TAM_MAX_MIN = 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 (TAM_MAX_MIN = 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 (TAM_MAX_MIN = 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 (TAM_MAX_MIN = 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 (TAM_MAX_MIN = 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 (TAM_MAX_MIN = 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 (TAM_MAX_MIN = 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 (TAM_MAX_MIN = 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 (TAM_MAX_MIN = 11) THEN
            BEGIN
                T12 = NULL; T13 = NULL; T14 = NULL; T15 = NULL;
                T16 = NULL; T17 = NULL; T18 = NULL; T19 = NULL;
                T20 = NULL;
            END ELSE IF (TAM_MAX_MIN = 12) THEN
            BEGIN
                T13 = NULL; T14 = NULL; T15 = NULL; T16 = NULL;
                T17 = NULL; T18 = NULL; T19 = NULL; T20 = NULL;
            END ELSE IF (TAM_MAX_MIN = 13) THEN
            BEGIN
                T14 = NULL; T15 = NULL; T16 = NULL; T17 = NULL;
                T18 = NULL; T19 = NULL; T20 = NULL;
            END ELSE IF (TAM_MAX_MIN = 14) THEN
            BEGIN
                T15 = NULL; T16 = NULL; T17 = NULL; T18 = NULL;
                T19 = NULL; T20 = NULL;
            END ELSE IF (TAM_MAX_MIN = 15) THEN
            BEGIN
                T16 = NULL; T17 = NULL; T18 = NULL; T19 = NULL;
                T20 = NULL;
            END ELSE IF (TAM_MAX_MIN = 16) THEN
            BEGIN
                T17 = NULL; T18 = NULL; T19 = NULL; T20 = NULL;
            END ELSE IF (TAM_MAX_MIN = 17) THEN
            BEGIN
                T18 = NULL; T19 = NULL; T20 = NULL;
            END ELSE IF (TAM_MAX_MIN = 18) THEN
            BEGIN
                T19 = NULL; T20 = NULL;
            END ELSE IF (TAM_MAX_MIN = 19) THEN
            BEGIN
                T20 = NULL;
            END
            
            IF (TOT > 0) THEN
            BEGIN
                /* Total da Comanda */
                CQTOT = CQTOT + TOT;
                SUSPEND;
            END
            
            C01 = 0; C02 = 0; C03 = 0; C04 = 0;
            C05 = 0; C06 = 0; C07 = 0; C08 = 0;
            C09 = 0; C10 = 0; C11 = 0; C12 = 0;
            C13 = 0; C14 = 0; C15 = 0; C16 = 0;
            C17 = 0; C18 = 0; C19 = 0; C20 = 0;
            CQTOT= 0;
            VR_SUBCTOT = 0;
            VR_TOTCDESC = 0;
            VR_TOTCGERAL = 0;
            
        END

        IF ((PEDIDO <> PROX_PED) OR (PROX_PED IS NULL)) THEN
        BEGIN
            GTOT = 'PED. TOTAL';

            /* preenche o registro do totalizador do pedido */
            :MODELO_ID = NULL;
            :MODELO_DESCRICAO = NULL;
            :COR_ID = NULL;
            :COR_DESCRICAO = 'TOTAL DO PEDIDO';
            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
            BEGIN
                SUSPEND;
            END

            GQTOT = 0;
            VR_SUBGTOT = 0;
            VR_TOTGDESC = 0;
            VR_TOTGGERAL = 0;
        END
        
        
    END
END
^

SET TERM ; ^

