SET TERM ^ ;

CREATE OR ALTER procedure SPC_PRE_PEDIDO_GRADE (
    DATA1 date,
    DATA2 date,
    PDV_CONFERENCIA_ID_1 integer,
    PDV_CONFERENCIA_ID_2 integer,
    SITUACAO integer,
    EXIBE_ALOCADO integer = 0,
    EXIBE_FATURADOS integer = 0,
    EXIBE_ENCERRADOS integer = 0)
returns (
    PDV_CONFERENCIA_ID integer,
    MODELO_ID integer,
    MODELO_DESCRICAO varchar(100),
    COR_ID integer,
    COR_DESCRICAO varchar(50),
    T00 numeric(15,0),
    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),
    G00 varchar(10),
    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_ORIGINAL 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 Q00 numeric(15,0);
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 U00 integer;
declare variable U01 integer;
declare variable U02 integer;
declare variable U03 integer;
declare variable U04 integer;
declare variable U05 integer;
declare variable U06 integer;
declare variable U07 integer;
declare variable U08 integer;
declare variable U09 integer;
declare variable U10 integer;
declare variable U11 integer;
declare variable U12 integer;
declare variable U13 integer;
declare variable U14 integer;
declare variable U15 integer;
declare variable U16 integer;
declare variable U17 integer;
declare variable U18 integer;
declare variable U19 integer;
declare variable U20 integer;
declare variable QTOT numeric(15,0);
declare variable GQTOT numeric(15,0); /* TOTAL DO PRE PEDIDO */
declare variable VR_SUBTOT numeric(15,2); /* VR SUB TOTAL DO PRE PEDIDO */
declare variable VR_TOTDESC numeric(15,2); /* VR TOTAL DO DESCONTO DO PRE PEDIDO */
declare variable VR_TOTGERAL numeric(15,2); /* VR TOTAL DO PRE PEDIDO */
declare variable VR_SUBGTOT numeric(15,2); /* VR SUB TOTAL DO PRE PEDIDO */
declare variable VR_TOTGDESC numeric(15,2); /* VR TOTAL DO DESCONTO DO PRE PEDIDO */
declare variable VR_TOTGGERAL numeric(15,2); /* VR TOTAL DO PRE PEDIDO */
BEGIN

    Q00 = 0; 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;
    
    
    U00 = 0; U01 = 0; U02 = 0; U03 = 0; U04 = 0;
    U05 = 0; U06 = 0; U07 = 0; U08 = 0;
    U09 = 0; U10 = 0; U11 = 0; U12 = 0;
    U13 = 0; U14 = 0; U15 = 0; U16 = 0;
    U17 = 0; U18 = 0; U19 = 0; U20 = 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 PDV_CONFERENCIA_ID,
               MODELO_ID,
               MODELO_DESCRICAO,
               COR_ID,
               COR_DESCRICAO,
               T00,T01,T02,T03,T04,T05,T06,T07,T08,T09,T10,
               T11,T12,T13,T14,T15,T16,T17,T18,T19,T20,
               G00,G01,G02,G03,G04,G05,G06,G07,G08,G09,G10,
               G11,G12,G13,G14,G15,G16,G17,G18,G19,G20,               
               U01,U02,U03,U04,U05,U06,U07,U08,U09,U10,
               U11,U12,U13,U14,U15,U16,U17,U18,U19,U20,
               PROX_PEDMOD,
               PROX_PED,
               TMIN,
               TMAX,
               MAX(TAM_MAX_MIN) OVER (),
               VALOR_UNITARIO,
               VALOR_ORIGINAL,
               VALOR_SUBTOTAL,
               VALOR_DESCONTO
          FROM (
                SELECT PDV_CONFERENCIA_ID,
                       MODELO_ID, MODELO_DESCRICAO,
                       COR_ID, COR_DESCRICAO,
                       T00,T01,T02,T03,T04,T05,T06,T07,T08,T09,T10,
                       T11,T12,T13,T14,T15,T16,T17,T18,T19,T20,
                       G00,G01,G02,G03,G04,G05,G06,G07,G08,G09,G10,
                       G11,G12,G13,G14,G15,G16,G17,G18,G19,G20,
                       U01,U02,U03,U04,U05,U06,U07,U08,U09,U10,
                       U11,U12,U13,U14,U15,U16,U17,U18,U19,U20,
                       PROX_PEDMOD,
                       PROX_PED,
                       MIN(TAM_MIN) OVER (PARTITION BY PDV_CONFERENCIA_ID,MODELO_ID) TMIN,
                       MAX(TAM_MAX) OVER (PARTITION BY PDV_CONFERENCIA_ID,MODELO_ID) TMAX,
                       TAM_MAX - TAM_MIN + 2 AS TAM_MAX_MIN,
                       VALOR_UNITARIO,
                       VALOR_ORIGINAL,
                       VALOR_SUBTOTAL,
                       VALOR_DESCONTO
                  FROM (
                        SELECT PDV_CONFERENCIA_ID,
                               MODELO_ID,
                               MODELO_DESCRICAO,
                               COR_ID,
                               COR_DESCRICAO,
                               T00,T01,T02,T03,T04,T05,T06,T07,T08,T09,T10,
                               T11,T12,T13,T14,T15,T16,T17,T18,T19,T20,
                               G00,G01,G02,G03,G04,G05,G06,G07,G08,G09,G10,
                               G11,G12,G13,G14,G15,G16,G17,G18,G19,G20,
                               U01,U02,U03,U04,U05,U06,U07,U08,U09,U10,
                               U11,U12,U13,U14,U15,U16,U17,U18,U19,U20,
                               LEAD (PRE_PEDIDO_MODELO) OVER (ORDER BY PDV_CONFERENCIA_ID, MODELO_ID, COR_DESCRICAO) PROX_PEDMOD,
                               LEAD (PDV_CONFERENCIA_ID) OVER (ORDER BY PDV_CONFERENCIA_ID, MODELO_ID, COR_DESCRICAO, VALOR_UNITARIO) PROX_PED,
                               IIF(T00>0,1,  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,  IIF(T01>0,1,  0)))))))))))))))))))) TAM_MAX,
                               VALOR_UNITARIO,
                               VALOR_ORIGINAL,
                               VALOR_SUBTOTAL,
                               VALOR_DESCONTO
                          FROM (
                                SELECT X.PDV_CONFERENCIA_ID||'-'||X.MODELO_ID/*||'-'||VALOR_UNITARIO*/ AS PRE_PEDIDO_MODELO,
                                       X.PDV_CONFERENCIA_ID,
                                       X.MODELO_ID, X.MODELO_DESCRICAO,
                                       X.COR_ID, X.COR_DESCRICAO,
                                       SUM(T00) T00,
                                       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,
                                       
                                       SUM(U00) U00,
                                       SUM(U01) U01,SUM(U02) U02,SUM(U03) U03,SUM(U04) U04,
                                       SUM(U05) U05,SUM(U06) U06,SUM(U07) U07,SUM(U08) U08,
                                       SUM(U09) U09,SUM(U10) U10,SUM(U11) U11,SUM(U12) U12,
                                       SUM(U13) U13,SUM(U14) U14,SUM(U15) U15,SUM(U16) U16,
                                       SUM(U17) U17,SUM(U18) U18,SUM(U19) U19,SUM(U20) U20,

                                       G00,G01,G02,G03,G04,G05,G06,G07,G08,G09,G10,
                                       G11,G12,G13,G14,G15,G16,G17,G18,G19,G20,
                                       VALOR_UNITARIO,
                                       VALOR_ORIGINAL,
                                       SUM(VALOR_SUBTOTAL) VALOR_SUBTOTAL,
                                       SUM(VALOR_DESCONTO) VALOR_DESCONTO
                                 FROM (
                                          SELECT P.ID PDV_CONFERENCIA_ID, I.ID PRE_PEDIDO_ITEM_ID,
                                                 PR.MODELO_CODIGO AS MODELO_ID,
                                                 M.DESCRICAO MODELO_DESCRICAO,
                                                 C.CODIGO AS COR_ID, C.DESCRICAO COR_DESCRICAO,
                                                 0 AS T00,
                                                 IIF(I.TAMANHO = 1, IIF( :SITUACAO <> 0,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(:EXIBE_ALOCADO = 1, I.QTD_ESTOQUE , IIF(:EXIBE_ALOCADO = 2, S.QUANTIDADE-I.QTD_ESTOQUE , 0))))),I.QUANTIDADE) , 0) T01,
                                                 IIF(I.TAMANHO = 2, IIF( :SITUACAO <> 0,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(:EXIBE_ALOCADO = 1, I.QTD_ESTOQUE , IIF(:EXIBE_ALOCADO = 2, S.QUANTIDADE-I.QTD_ESTOQUE , 0))))),I.QUANTIDADE) , 0) T02,
                                                 IIF(I.TAMANHO = 3, IIF( :SITUACAO <> 0,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(:EXIBE_ALOCADO = 1, I.QTD_ESTOQUE , IIF(:EXIBE_ALOCADO = 2, S.QUANTIDADE-I.QTD_ESTOQUE , 0))))),I.QUANTIDADE) , 0) T03,
                                                 IIF(I.TAMANHO = 4, IIF( :SITUACAO <> 0,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(:EXIBE_ALOCADO = 1, I.QTD_ESTOQUE , IIF(:EXIBE_ALOCADO = 2, S.QUANTIDADE-I.QTD_ESTOQUE , 0))))),I.QUANTIDADE) , 0) T04,
                                                 IIF(I.TAMANHO = 5, IIF( :SITUACAO <> 0,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(:EXIBE_ALOCADO = 1, I.QTD_ESTOQUE , IIF(:EXIBE_ALOCADO = 2, S.QUANTIDADE-I.QTD_ESTOQUE , 0))))),I.QUANTIDADE) , 0) T05,
                                                 IIF(I.TAMANHO = 6, IIF( :SITUACAO <> 0,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(:EXIBE_ALOCADO = 1, I.QTD_ESTOQUE , IIF(:EXIBE_ALOCADO = 2, S.QUANTIDADE-I.QTD_ESTOQUE , 0))))),I.QUANTIDADE) , 0) T06,
                                                 IIF(I.TAMANHO = 7, IIF( :SITUACAO <> 0,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(:EXIBE_ALOCADO = 1, I.QTD_ESTOQUE , IIF(:EXIBE_ALOCADO = 2, S.QUANTIDADE-I.QTD_ESTOQUE , 0))))),I.QUANTIDADE) , 0) T07,
                                                 IIF(I.TAMANHO = 8, IIF( :SITUACAO <> 0,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(:EXIBE_ALOCADO = 1, I.QTD_ESTOQUE , IIF(:EXIBE_ALOCADO = 2, S.QUANTIDADE-I.QTD_ESTOQUE , 0))))),I.QUANTIDADE) , 0) T08,
                                                 IIF(I.TAMANHO = 9, IIF( :SITUACAO <> 0,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(:EXIBE_ALOCADO = 1, I.QTD_ESTOQUE , IIF(:EXIBE_ALOCADO = 2, S.QUANTIDADE-I.QTD_ESTOQUE , 0))))),I.QUANTIDADE) , 0) T09,
                                                 IIF(I.TAMANHO =10, IIF( :SITUACAO <> 0,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(:EXIBE_ALOCADO = 1, I.QTD_ESTOQUE , IIF(:EXIBE_ALOCADO = 2, S.QUANTIDADE-I.QTD_ESTOQUE , 0))))),I.QUANTIDADE) , 0) T10,
                                                 IIF(I.TAMANHO =11, IIF( :SITUACAO <> 0,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(:EXIBE_ALOCADO = 1, I.QTD_ESTOQUE , IIF(:EXIBE_ALOCADO = 2, S.QUANTIDADE-I.QTD_ESTOQUE , 0))))),I.QUANTIDADE) , 0) T11,
                                                 IIF(I.TAMANHO =12, IIF( :SITUACAO <> 0,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(:EXIBE_ALOCADO = 1, I.QTD_ESTOQUE , IIF(:EXIBE_ALOCADO = 2, S.QUANTIDADE-I.QTD_ESTOQUE , 0))))),I.QUANTIDADE) , 0) T12,
                                                 IIF(I.TAMANHO =13, IIF( :SITUACAO <> 0,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(:EXIBE_ALOCADO = 1, I.QTD_ESTOQUE , IIF(:EXIBE_ALOCADO = 2, S.QUANTIDADE-I.QTD_ESTOQUE , 0))))),I.QUANTIDADE) , 0) T13,
                                                 IIF(I.TAMANHO =14, IIF( :SITUACAO <> 0,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(:EXIBE_ALOCADO = 1, I.QTD_ESTOQUE , IIF(:EXIBE_ALOCADO = 2, S.QUANTIDADE-I.QTD_ESTOQUE , 0))))),I.QUANTIDADE) , 0) T14,
                                                 IIF(I.TAMANHO =15, IIF( :SITUACAO <> 0,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(:EXIBE_ALOCADO = 1, I.QTD_ESTOQUE , IIF(:EXIBE_ALOCADO = 2, S.QUANTIDADE-I.QTD_ESTOQUE , 0))))),I.QUANTIDADE) , 0) T15,
                                                 IIF(I.TAMANHO =16, IIF( :SITUACAO <> 0,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(:EXIBE_ALOCADO = 1, I.QTD_ESTOQUE , IIF(:EXIBE_ALOCADO = 2, S.QUANTIDADE-I.QTD_ESTOQUE , 0))))),I.QUANTIDADE) , 0) T16,
                                                 IIF(I.TAMANHO =17, IIF( :SITUACAO <> 0,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(:EXIBE_ALOCADO = 1, I.QTD_ESTOQUE , IIF(:EXIBE_ALOCADO = 2, S.QUANTIDADE-I.QTD_ESTOQUE , 0))))),I.QUANTIDADE) , 0) T17,
                                                 IIF(I.TAMANHO =18, IIF( :SITUACAO <> 0,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(:EXIBE_ALOCADO = 1, I.QTD_ESTOQUE , IIF(:EXIBE_ALOCADO = 2, S.QUANTIDADE-I.QTD_ESTOQUE , 0))))),I.QUANTIDADE) , 0) T18,
                                                 IIF(I.TAMANHO =19, IIF( :SITUACAO <> 0,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(:EXIBE_ALOCADO = 1, I.QTD_ESTOQUE , IIF(:EXIBE_ALOCADO = 2, S.QUANTIDADE-I.QTD_ESTOQUE , 0))))),I.QUANTIDADE) , 0) T19,
                                                 IIF(I.TAMANHO =20, IIF( :SITUACAO <> 0,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(:EXIBE_ALOCADO = 1, I.QTD_ESTOQUE , IIF(:EXIBE_ALOCADO = 2, S.QUANTIDADE-I.QTD_ESTOQUE , 0))))),I.QUANTIDADE) , 0) T20,
                                                
                                                 IIF(I.TAMANHO = 0,1,0) AS U00,
                                                 IIF(I.TAMANHO = 1,1,0) AS U01,
                                                 IIF(I.TAMANHO = 2,1,0) AS U02,
                                                 IIF(I.TAMANHO = 3,1,0) AS U03,
                                                 IIF(I.TAMANHO = 4,1,0) AS U04,
                                                 IIF(I.TAMANHO = 5,1,0) AS U05,
                                                 IIF(I.TAMANHO = 6,1,0) AS U06,
                                                 IIF(I.TAMANHO = 7,1,0) AS U07,
                                                 IIF(I.TAMANHO = 8,1,0) AS U08,
                                                 IIF(I.TAMANHO = 9,1,0) AS U09,
                                                 IIF(I.TAMANHO =10,1,0) AS U10,
                                                 IIF(I.TAMANHO =11,1,0) AS U11,
                                                 IIF(I.TAMANHO =12,1,0) AS U12,
                                                 IIF(I.TAMANHO =13,1,0) AS U13,
                                                 IIF(I.TAMANHO =14,1,0) AS U14,
                                                 IIF(I.TAMANHO =15,1,0) AS U15,
                                                 IIF(I.TAMANHO =16,1,0) AS U16,
                                                 IIF(I.TAMANHO =17,1,0) AS U17,
                                                 IIF(I.TAMANHO =18,1,0) AS U18,
                                                 IIF(I.TAMANHO =19,1,0) AS U19,
                                                 IIF(I.TAMANHO =20,1,0) AS U20,

                                                 '' AS G00,
                                                 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_UNITARIO,
                                                 COALESCE(I.VALOR_ORIGINAL, 0) AS VALOR_ORIGINAL,
                                                 IIF( :SITUACAO <> 0,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(:EXIBE_ALOCADO = 1, I.QTD_ESTOQUE , IIF(:EXIBE_ALOCADO = 2, S.QUANTIDADE-I.QTD_ESTOQUE , 0))))),I.QUANTIDADE) * I.VALOR_UNITARIO AS VALOR_SUBTOTAL,

                                                 I.VALOR_DESCONTO / I.QUANTIDADE *
                                                 IIF( :SITUACAO <> 0,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(:EXIBE_ALOCADO = 1, I.QTD_ESTOQUE , IIF(:EXIBE_ALOCADO = 2, S.QUANTIDADE-I.QTD_ESTOQUE , 0))))),I.QUANTIDADE) AS VALOR_DESCONTO

                                            FROM TBPDV_CONFERENCIA P, TBPDV_CONFERENCIA_ITEM I, TBPDV_CONFERENCIA_ITEM_SALDO S,
                                                 TBGRADE G, TBPRODUTO PR, TBMODELO M, TBCOR C
                                           WHERE P.ID = I.PDV_CONFERENCIA_ID
                                             AND I.ID = S.PDV_CONFERENCIA_ITEM_ID
                                             AND I.PRODUTO_ID = PR.CODIGO
                                             AND M.GRADE_CODIGO = G.CODIGO
                                             AND C.CODIGO = PR.COR_CODIGO
                                             AND M.CODIGO = PR.MODELO_CODIGO
                                             AND I.TAMANHO > 0
                                             AND IIF(:EXIBE_ENCERRADOS = 1, I.SITUACAO = 4, TRUE)
                                             --AND P.DATA BETWEEN :DATA1 AND :DATA2
                                             AND P.ID BETWEEN :PDV_CONFERENCIA_ID_1 AND :PDV_CONFERENCIA_ID_2
                                             UNION ALL

                                             SELECT P.ID PDV_CONFERENCIA_ID, I.ID PRE_PEDIDO_ITEM_ID,
                                                 PR.MODELO_CODIGO AS MODELO_ID,
                                                 M.DESCRICAO MODELO_DESCRICAO,
                                                 C.CODIGO AS COR_ID, C.DESCRICAO COR_DESCRICAO,
                                                 
                                                  IIF(I.TAMANHO = 0, IIF( :SITUACAO <> 0,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(:EXIBE_ALOCADO = 1, I.QTD_ESTOQUE , IIF(:EXIBE_ALOCADO = 2, S.QUANTIDADE-I.QTD_ESTOQUE , 0))))),I.QUANTIDADE) , 0) AS T00,
                                                  0 AS T01,
                                                  0 AS T02,
                                                  0 AS T03,
                                                  0 AS T04,
                                                  0 AS T05,
                                                  0 AS T06,
                                                  0 AS T07,
                                                  0 AS T08,
                                                  0 AS T09,
                                                  0 AS T10,
                                                  0 AS T11,
                                                  0 AS T12,
                                                  0 AS T13,
                                                  0 AS T14,
                                                  0 AS T15,
                                                  0 AS T16,
                                                  0 AS T17,
                                                  0 AS T18,
                                                  0 AS T19,
                                                  0 AS T20,
                                                
                                                  1 AS U00,
                                                  0 AS U01,
                                                  0 AS U02,
                                                  0 AS U03,
                                                  0 AS U04,
                                                  0 AS U05,
                                                  0 AS U06,
                                                  0 AS U07,
                                                  0 AS U08,
                                                  0 AS U09,
                                                  0 AS U10,
                                                  0 AS U11,
                                                  0 AS U12,
                                                  0 AS U13,
                                                  0 AS U14,
                                                  0 AS U15,
                                                  0 AS U16,
                                                  0 AS U17,
                                                  0 AS U18,
                                                  0 AS U19,
                                                  0 AS U20,

                                                 '' AS G00,
                                                 '' AS G01, '' AS G02,
                                                 '' AS G03, '' AS G04,
                                                 '' AS G05, '' AS G06,
                                                 '' AS G07, '' AS G08,
                                                 '' AS G09, '' AS G10,
                                                 '' AS G11, '' AS G12,
                                                 '' AS G13, '' AS G14,
                                                 '' AS G15, '' AS G16,
                                                 '' AS G17, '' AS G18,
                                                 '' AS G19, '' AS G20,

                                                 I.VALOR_UNITARIO,
                                                 COALESCE(I.VALOR_ORIGINAL, 0) AS VALOR_ORIGINAL,
                                                 IIF( :SITUACAO <> 0,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(:EXIBE_ALOCADO = 1, I.QTD_ESTOQUE , IIF(:EXIBE_ALOCADO = 2, S.QUANTIDADE-I.QTD_ESTOQUE , 0))))),I.QUANTIDADE) * I.VALOR_UNITARIO AS VALOR_SUBTOTAL,

                                                 I.VALOR_DESCONTO / I.QUANTIDADE *
                                                 IIF( :SITUACAO <> 0,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(:EXIBE_ALOCADO = 1, I.QTD_ESTOQUE , IIF(:EXIBE_ALOCADO = 2, S.QUANTIDADE-I.QTD_ESTOQUE , 0))))),I.QUANTIDADE) AS VALOR_DESCONTO

                                            FROM TBPDV_CONFERENCIA P, TBPDV_CONFERENCIA_ITEM I, TBPDV_CONFERENCIA_ITEM_SALDO S,
                                                 TBPRODUTO PR, TBMODELO M, TBCOR C
                                           WHERE P.ID = I.PDV_CONFERENCIA_ID
                                             AND I.ID = S.PDV_CONFERENCIA_ITEM_ID
                                             AND I.PRODUTO_ID = PR.CODIGO
                                             AND C.CODIGO = PR.COR_CODIGO
                                             AND M.CODIGO = PR.MODELO_CODIGO
                                             AND I.TAMANHO = 0
                                             AND IIF(:EXIBE_ENCERRADOS = 1, I.SITUACAO = 4, TRUE)
                                             --AND P.DATA BETWEEN :DATA1 AND :DATA2
                                             AND P.ID BETWEEN :PDV_CONFERENCIA_ID_1 AND :PDV_CONFERENCIA_ID_2

                                         ) X
                                   GROUP BY X.PDV_CONFERENCIA_ID,
                                            X.MODELO_ID, X.MODELO_DESCRICAO,
                                            X.COR_ID, X.COR_DESCRICAO,
                                            G00,G01,G02,G03,G04,G05,G06,G07,G08,G09,G10,
                                            G11,G12,G13,G14,G15,G16,G17,G18,G19,G20,
                                            VALOR_UNITARIO, VALOR_ORIGINAL
                           )
                   )
           ORDER BY PDV_CONFERENCIA_ID, MODELO_ID, COR_DESCRICAO
        )

    INTO :PDV_CONFERENCIA_ID,:MODELO_ID,:MODELO_DESCRICAO,:COR_ID,:COR_DESCRICAO,
         :T00,:T01,:T02,:T03,:T04,:T05,:T06,:T07,:T08,:T09,:T10,
         :T11,:T12,:T13,:T14,:T15,:T16,:T17,:T18,:T19,:T20,
         :G00,:G01,:G02,:G03,:G04,:G05,:G06,:G07,:G08,:G09,:G10,
         :G11,:G12,:G13,:G14,:G15,:G16,:G17,:G18,:G19,:G20,
         :U01,:U02,:U03,:U04,:U05,:U06,:U07,:U08,:U09,:U10,
         :U11,:U12,:U13,:U14,:U15,:U16,:U17,:U18,:U19,:U20,
         :PROX_PEDMOD,:PROX_PED,:TMIN,:TMAX,:TAM_MAX_MIN,
         :VALOR_UNITARIO, :VALOR_ORIGINAL, :VALOR_SUBTOTAL, :VALOR_DESCONTO
     DO BEGIN

             GTOT = 'TOTAL';
             TOT  = T00+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 AND :EXIBE_FATURADOS = 0) 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
                      
                   IF (TMIN = 20 AND :EXIBE_FATURADOS = 1) THEN
                       BEGIN
                        G01 = IIF(U00 > 0,G01,'');
                        G01 = IIF(U01 > 0,G01,''); G02 = IIF(U02 > 0,G02,''); G03 = IIF(U03 > 0,G03,''); G04 = IIF(U04 > 0,G04,'');
                        G05 = IIF(U05 > 0,G05,''); G06 = IIF(U06 > 0,G06,''); G07 = IIF(U07 > 0,G07,''); G08 = IIF(U08 > 0,G08,'');
                        G09 = IIF(U09 > 0,G09,''); G10 = IIF(U10 > 0,G10,''); G11 = IIF(U11 > 0,G11,''); G12 = IIF(U12 > 0,G12,'');
                        G13 = IIF(U13 > 0,G13,''); G14 = IIF(U14 > 0,G14,''); G15 = IIF(U15 > 0,G15,''); G16 = IIF(U16 > 0,G16,'');
                        G17 = IIF(U17 > 0,G17,''); G18 = IIF(U18 > 0,G18,''); G19 = IIF(U19 > 0,G19,''); G20 = IIF(U20 > 0,G20,'');

                        IF (U02 > 0) THEN
                            BEGIN
                            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 (U03 > 0) THEN
                            BEGIN
                            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 (U04 > 0) THEN
                            BEGIN
                            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 (U05 > 0) THEN
                            BEGIN
                            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 (U06 > 0) THEN
                            BEGIN
                            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 (U07 > 0) THEN
                            BEGIN
                            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 (U08 > 0) THEN
                            BEGIN
                            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 (U09 > 0) THEN
                            BEGIN
                            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 (U10 > 0) THEN
                            BEGIN
                            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 (U11 > 0) THEN
                            BEGIN
                            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 (U12 > 0) THEN
                            BEGIN
                            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 (U13 > 0) THEN
                            BEGIN
                            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 (U14 > 0) THEN
                            BEGIN
                            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 (U15 > 0) THEN
                            BEGIN
                            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 (U16 > 0) THEN
                            BEGIN
                            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 (U17 > 0) THEN
                            BEGIN
                            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 (U18 > 0) THEN
                            BEGIN
                            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 (U19 > 0) THEN
                            BEGIN
                            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 (U20 > 0) THEN
                            BEGIN
                            G01 = G20;  G02 = '';  G03 = '';    G04 = ''; G05 = '';    G06 = '';  G07 = '';    G08 = ''; G09 = '';    G10 = '';  G11 = '';    G12 = ''; G13 = '';    G14 = '';  G15 = '';    G16 = ''; G17 = '';    G18 = '';  G19 = '';    G20 = '';
                            END
                      END
                   /* CALCULA OS TOTALIZADORES */
                   /* TOTAIS DO MODELO */
                   Q00 = Q00 + :T00;
                   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= Q00+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(:EXIBE_FATURADOS = 1)THEN
                   BEGIN
                      IF(TOT = 0) THEN
                      BEGIN
                           T00 = NULL; 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;
                      SUSPEND;
                      END
                   END

                   IF ( (PDV_CONFERENCIA_ID||'-'||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 = 'TOTAL';
                         T00 = Q00; 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_ORIGINAL = 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
                         

                         IF(:EXIBE_FATURADOS = 1)THEN
                         BEGIN
                           IF(TOT = 0) THEN
                           BEGIN
                             
                             T00 = NULL;T01 = NULL; T02 = NULL; T03 = NULL; T04 = 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; T05 = NULL;
                             GQTOT = GQTOT + TOT;
                            SUSPEND;
                           END
                         END


                         Q00 = 0;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 ( (PDV_CONFERENCIA_ID <> PROX_PED) OR (PROX_PED IS NULL)) THEN
                       BEGIN
                           /* PREENCHE O REGISTRO DO TOTALIZADOR DO PRE-PEDIDO */
                           :COR_ID = NULL;
                           :COR_DESCRICAO = 'TOTAL DO PRE-PEDIDO';
                           TOT = GQTOT;
                           :VALOR_UNITARIO = NULL;
                           :VALOR_ORIGINAL = NULL;
                           :VALOR_SUBTOTAL = VR_SUBGTOT;
                           :VALOR_DESCONTO = VR_TOTGDESC;
                           :VALOR_TOTAL    = VR_TOTGGERAL;

                           /* LIMPA OS TAMANHOS E GRADES */
                           T00 = NULL;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
^

SET TERM ; ^

