CREATE OR ALTER PROCEDURE SPC_REMESSA_MODELO_GRADE (
    REMESSA VARCHAR(20))
RETURNS (
    REMESSA_ID INTEGER,
    MODELO_ID INTEGER,
    MODELO_DESCRICAO VARCHAR(100),
    COR_ID INTEGER,
    COR_DESCRICAO VARCHAR(50),
    T01 NUMERIC(15,0),
    T02 NUMERIC(15,0),
    T03 NUMERIC(15,0),
    T04 NUMERIC(15,0),
    T05 NUMERIC(15,0),
    T06 NUMERIC(15,0),
    T07 NUMERIC(15,0),
    T08 NUMERIC(15,0),
    T09 NUMERIC(15,0),
    T10 NUMERIC(15,0),
    T11 NUMERIC(15,0),
    T12 NUMERIC(15,0),
    T13 NUMERIC(15,0),
    T14 NUMERIC(15,0),
    T15 NUMERIC(15,0),
    T16 NUMERIC(15,0),
    T17 NUMERIC(15,0),
    T18 NUMERIC(15,0),
    T19 NUMERIC(15,0),
    T20 NUMERIC(15,0),
    G01 VARCHAR(10),
    G02 VARCHAR(10),
    G03 VARCHAR(10),
    G04 VARCHAR(10),
    G05 VARCHAR(10),
    G06 VARCHAR(10),
    G07 VARCHAR(10),
    G08 VARCHAR(10),
    G09 VARCHAR(10),
    G10 VARCHAR(10),
    G11 VARCHAR(10),
    G12 VARCHAR(10),
    G13 VARCHAR(10),
    G14 VARCHAR(10),
    G15 VARCHAR(10),
    G16 VARCHAR(10),
    G17 VARCHAR(10),
    G18 VARCHAR(10),
    G19 VARCHAR(10),
    G20 VARCHAR(10),
    TMIN INTEGER,
    TMAX INTEGER,
    TOT NUMERIC(15,0),
    GTOT VARCHAR(10),
    TAM_MAX_MIN INTEGER)
AS
DECLARE VARIABLE PROX_REM_MOD VARCHAR(100);
DECLARE VARIABLE Q01          NUMERIC(15,0);
DECLARE VARIABLE Q02          NUMERIC(15,0);
DECLARE VARIABLE Q03          NUMERIC(15,0);
DECLARE VARIABLE Q04          NUMERIC(15,0);
DECLARE VARIABLE Q05          NUMERIC(15,0);
DECLARE VARIABLE Q06          NUMERIC(15,0);
DECLARE VARIABLE Q07          NUMERIC(15,0);
DECLARE VARIABLE Q08          NUMERIC(15,0);
DECLARE VARIABLE Q09          NUMERIC(15,0);
DECLARE VARIABLE Q10          NUMERIC(15,0);
DECLARE VARIABLE Q11          NUMERIC(15,0);
DECLARE VARIABLE Q12          NUMERIC(15,0);
DECLARE VARIABLE Q13          NUMERIC(15,0);
DECLARE VARIABLE Q14          NUMERIC(15,0);
DECLARE VARIABLE Q15          NUMERIC(15,0);
DECLARE VARIABLE Q16          NUMERIC(15,0);
DECLARE VARIABLE Q17          NUMERIC(15,0);
DECLARE VARIABLE Q18          NUMERIC(15,0);
DECLARE VARIABLE Q19          NUMERIC(15,0);
DECLARE VARIABLE Q20          NUMERIC(15,0);
DECLARE VARIABLE QTOT         NUMERIC(15,0);
DECLARE VARIABLE GQTOT        NUMERIC(15,0); /* TOTAL GERAL */
begin

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

    FOR
        SELECT REMESSA,
               MODELO_ID, MODELO_DESCRICAO,
               COR_ID, COR_DESCRICAO,
               T01,T02,T03,T04,T05,T06,T07,T08,T09,T10,
               T11,T12,T13,T14,T15,T16,T17,T18,T19,T20,
               G01,G02,G03,G04,G05,G06,G07,G08,G09,G10,
               G11,G12,G13,G14,G15,G16,G17,G18,G19,G20,
               PROX_REM_MOD,
               TMIN,
               TMAX,
               MAX(TAM_MAX_MIN) OVER ()
          FROM (
                SELECT REMESSA,
                       MODELO_ID, MODELO_DESCRICAO,
                       COR_ID, COR_DESCRICAO,
                       T01,T02,T03,T04,T05,T06,T07,T08,T09,T10,
                       T11,T12,T13,T14,T15,T16,T17,T18,T19,T20,
                       G01,G02,G03,G04,G05,G06,G07,G08,G09,G10,
                       G11,G12,G13,G14,G15,G16,G17,G18,G19,G20,
                       PROX_REM_MOD,
                       MIN(TAM_MIN) OVER (PARTITION BY REMESSA,MODELO_ID) TMIN,
                       MAX(TAM_MAX) OVER (PARTITION BY REMESSA,MODELO_ID) TMAX,
                       TAM_MAX - TAM_MIN + 2 as TAM_MAX_MIN
                  FROM (
                        SELECT REMESSA,
                               MODELO_ID,MODELO_DESCRICAO,
                               COR_ID,COR_DESCRICAO,
                               T01,T02,T03,T04,T05,T06,T07,T08,T09,T10,
                               T11,T12,T13,T14,T15,T16,T17,T18,T19,T20,
                               G01,G02,G03,G04,G05,G06,G07,G08,G09,G10,
                               G11,G12,G13,G14,G15,G16,G17,G18,G19,G20,
                               LEAD (REM_MOD) OVER (ORDER BY REMESSA, MODELO_ID, COR_DESCRICAO) PROX_REM_MOD,
                               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 
                          FROM (
                                SELECT X.REMESSA,
                                       X.MODELO_ID,
                                       X.MODELO_DESCRICAO,
                                       X.COR_ID,
                                       X.COR_DESCRICAO,
                                       X.REMESSA||'-'||X.MODELO_ID AS REM_MOD,
                                       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
                                 FROM (
                                            SELECT L.REMESSA,
                                                   M.CODIGO AS MODELO_ID,
                                                   M.DESCRICAO AS MODELO_DESCRICAO,
                                                   C.CODIGO AS COR_ID,
                                                   C.DESCRICAO AS COR_DESCRICAO,
                                                   L.T01 AS T01,
                                                   L.T02 as T02,
                                                   L.T03 as T03,
                                                   L.T04 as T04,
                                                   L.T05 as T05,
                                                   L.T06 as T06,
                                                   L.T07 as T07,
                                                   L.T08 as T08,
                                                   L.T09 as T09,
                                                   L.T10 as T10,
                                                   L.T11 as T11,
                                                   L.T12 as T12,
                                                   L.T13 as T13,
                                                   L.T14 as T14,
                                                   L.T15 as T15,
                                                   L.T16 as T16,
                                                   L.T17 as T17,
                                                   L.T18 as T18,
                                                   L.T19 as T19,
                                                   L.T20 as 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
                                              FROM TBREMESSA A
                                             INNER JOIN TBREMESSA_ACUMULADO L ON L.REMESSA = A.NUMERO
                                             INNER JOIN TBPRODUTO P ON P.CODIGO = L.PRODUTO_CODIGO
                                             INNER JOIN TBMODELO M ON M.CODIGO = P.MODELO_CODIGO
                                             INNER JOIN TBCOR C ON C.CODIGO = P.COR_CODIGO
                                              LEFT JOIN TBGRADE G ON G.CODIGO = P.GRADE_CODIGO
                                             WHERE A.REMESSA = :REMESSA
                                         ) X
                                   GROUP BY X.REMESSA,
                                            X.MODELO_ID,
                                            X.MODELO_DESCRICAO,
                                            X.COR_ID,
                                            X.COR_DESCRICAO,
                                            G01,G02,G03,G04,G05,G06,G07,G08,G09,G10,
                                            G11,G12,G13,G14,G15,G16,G17,G18,G19,G20
                           )
                   )
           ORDER BY REMESSA, MODELO_ID, COR_DESCRICAO
        )

    INTO :REMESSA_ID,
         :MODELO_ID,:MODELO_DESCRICAO,:COR_ID,:COR_DESCRICAO,
         :T01,:T02,:T03,:T04,:T05,:T06,:T07,:T08,:T09,:T10,
         :T11,:T12,:T13,:T14,:T15,:T16,:T17,:T18,:T19,:T20,
         :G01,:G02,:G03,:G04,:G05,:G06,:G07,:G08,:G09,:G10,
         :G11,:G12,:G13,:G14,:G15,:G16,:G17,:G18,:G19,:G20,
         :PROX_REM_MOD,:TMIN,:TMAX,:TAM_MAX_MIN
     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;

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

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

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

                   IF(TOT > 0) THEN SUSPEND;

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

                           /* 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 */
                            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;
                       End
                    
                   IF ( (PROX_REM_MOD IS NULL) ) THEN
                       Begin
                           -- preenche o registro do totalizador  
                           :COR_ID = null;
                           :COR_DESCRICAO = '<b>***** TOTAL DA REMESSA: '||FN_LPAD(:REMESSA_ID,6,'0')||'<b>';
                           Tot = gqTot;

                           -- Limpa os Tamanhos e Grades
                           T01 = null; T02 = null; T03 = null; T04 = null;
                           T05 = null; T06 = null; T07 = null; T08 = null;
                           T09 = null; T10 = null; T11 = null; T12 = null;
                           T13 = null; T14 = null; T15 = null; T16 = null;
                           T17 = null; T18 = null; T19 = null; T20 = null;
                           IF(gqTOT > 0) THEN SUSPEND;
                           gqTot= 0;
                       End
                 

                End
End;

CREATE OR ALTER TRIGGER TTBESTOQUE_SALDO_B0U_01 FOR TBESTOQUE_SALDO
ACTIVE BEFORE UPDATE POSITION 0
AS
DECLARE VARIABLE Msg     VarChar(100);
DECLARE VARIABLE Msg2    VarChar(250);

DECLARE VARIABLE SALDO   Numeric(15,5);
DECLARE VARIABLE SALDO01 Numeric(15,5); DECLARE VARIABLE SALDO02 Numeric(15,5);
DECLARE VARIABLE SALDO03 Numeric(15,5); DECLARE VARIABLE SALDO04 Numeric(15,5);
DECLARE VARIABLE SALDO05 Numeric(15,5); DECLARE VARIABLE SALDO06 Numeric(15,5);
DECLARE VARIABLE SALDO07 Numeric(15,5); DECLARE VARIABLE SALDO08 Numeric(15,5);
DECLARE VARIABLE SALDO09 Numeric(15,5); DECLARE VARIABLE SALDO10 Numeric(15,5);
DECLARE VARIABLE SALDO11 Numeric(15,5); DECLARE VARIABLE SALDO12 Numeric(15,5);
DECLARE VARIABLE SALDO13 Numeric(15,5); DECLARE VARIABLE SALDO14 Numeric(15,5);
DECLARE VARIABLE SALDO15 Numeric(15,5); DECLARE VARIABLE SALDO16 Numeric(15,5);
DECLARE VARIABLE SALDO17 Numeric(15,5); DECLARE VARIABLE SALDO18 Numeric(15,5);
DECLARE VARIABLE SALDO19 Numeric(15,5); DECLARE VARIABLE SALDO20 Numeric(15,5);

DECLARE VARIABLE SALDODE Numeric(15,5); DECLARE VARIABLE SALDOEM Numeric(15,5);

DECLARE VARIABLE ALOC   Numeric(15,4);
DECLARE VARIABLE ALOC01 Numeric(15,4); DECLARE VARIABLE ALOC02 Numeric(15,4);
DECLARE VARIABLE ALOC03 Numeric(15,4); DECLARE VARIABLE ALOC04 Numeric(15,4);
DECLARE VARIABLE ALOC05 Numeric(15,4); DECLARE VARIABLE ALOC06 Numeric(15,4);
DECLARE VARIABLE ALOC07 Numeric(15,4); DECLARE VARIABLE ALOC08 Numeric(15,4);
DECLARE VARIABLE ALOC09 Numeric(15,4); DECLARE VARIABLE ALOC10 Numeric(15,4);
DECLARE VARIABLE ALOC11 Numeric(15,4); DECLARE VARIABLE ALOC12 Numeric(15,4);
DECLARE VARIABLE ALOC13 Numeric(15,4); DECLARE VARIABLE ALOC14 Numeric(15,4);
DECLARE VARIABLE ALOC15 Numeric(15,4); DECLARE VARIABLE ALOC16 Numeric(15,4);
DECLARE VARIABLE ALOC17 Numeric(15,4); DECLARE VARIABLE ALOC18 Numeric(15,4);
DECLARE VARIABLE ALOC19 Numeric(15,4); DECLARE VARIABLE ALOC20 Numeric(15,4);

declare variable G01 VARCHAR(10);
declare variable G02 VARCHAR(10);
declare variable G03 VARCHAR(10);
declare variable G04 VARCHAR(10);
declare variable G05 VARCHAR(10);
declare variable G06 VARCHAR(10);
declare variable G07 VARCHAR(10);
declare variable G08 VARCHAR(10);
declare variable G09 VARCHAR(10);
declare variable G10 VARCHAR(10);
declare variable G11 VARCHAR(10);
declare variable G12 VARCHAR(10);
declare variable G13 VARCHAR(10);
declare variable G14 VARCHAR(10);
declare variable G15 VARCHAR(10);
declare variable G16 VARCHAR(10);
declare variable G17 VARCHAR(10);
declare variable G18 VARCHAR(10);
declare variable G19 VARCHAR(10);
declare variable G20 VARCHAR(10);

Begin
For
Select
IIF(OLD.SALDO    >NEW.SALDO,    OLD.SALDO    -NEW.SALDO,0),
IIF(OLD.T01_SALDO>NEW.T01_SALDO,OLD.T01_SALDO-NEW.T01_SALDO,0),
IIF(OLD.T02_SALDO>NEW.T02_SALDO,OLD.T02_SALDO-NEW.T02_SALDO,0),
IIF(OLD.T03_SALDO>NEW.T03_SALDO,OLD.T03_SALDO-NEW.T03_SALDO,0),
IIF(OLD.T04_SALDO>NEW.T04_SALDO,OLD.T04_SALDO-NEW.T04_SALDO,0),
IIF(OLD.T05_SALDO>NEW.T05_SALDO,OLD.T05_SALDO-NEW.T05_SALDO,0),
IIF(OLD.T06_SALDO>NEW.T06_SALDO,OLD.T06_SALDO-NEW.T06_SALDO,0),
IIF(OLD.T07_SALDO>NEW.T07_SALDO,OLD.T07_SALDO-NEW.T07_SALDO,0),
IIF(OLD.T08_SALDO>NEW.T08_SALDO,OLD.T08_SALDO-NEW.T08_SALDO,0),
IIF(OLD.T09_SALDO>NEW.T09_SALDO,OLD.T09_SALDO-NEW.T09_SALDO,0),
IIF(OLD.T10_SALDO>NEW.T10_SALDO,OLD.T10_SALDO-NEW.T10_SALDO,0),
IIF(OLD.T11_SALDO>NEW.T11_SALDO,OLD.T11_SALDO-NEW.T11_SALDO,0),
IIF(OLD.T12_SALDO>NEW.T12_SALDO,OLD.T12_SALDO-NEW.T12_SALDO,0),
IIF(OLD.T13_SALDO>NEW.T13_SALDO,OLD.T13_SALDO-NEW.T13_SALDO,0),
IIF(OLD.T14_SALDO>NEW.T14_SALDO,OLD.T14_SALDO-NEW.T14_SALDO,0),
IIF(OLD.T15_SALDO>NEW.T15_SALDO,OLD.T15_SALDO-NEW.T15_SALDO,0),
IIF(OLD.T16_SALDO>NEW.T16_SALDO,OLD.T16_SALDO-NEW.T16_SALDO,0),
IIF(OLD.T17_SALDO>NEW.T17_SALDO,OLD.T17_SALDO-NEW.T17_SALDO,0),
IIF(OLD.T18_SALDO>NEW.T18_SALDO,OLD.T18_SALDO-NEW.T18_SALDO,0),
IIF(OLD.T19_SALDO>NEW.T19_SALDO,OLD.T19_SALDO-NEW.T19_SALDO,0),
IIF(OLD.T20_SALDO>NEW.T20_SALDO,OLD.T20_SALDO-NEW.T20_SALDO,0),

IIF(OLD.Saldo_DeTerceiro>NEW.Saldo_DeTerceiro,OLD.Saldo_DeTerceiro-NEW.Saldo_DeTerceiro,0),
IIF(OLD.Saldo_EmTerceiro>NEW.Saldo_EmTerceiro,OLD.Saldo_EmTerceiro-NEW.Saldo_EmTerceiro,0),

IIF(OLD.SALDO    >NEW.SALDO,    Coalesce((Select First 1 A.Quantidade From TbEstoque_Saldo_Alocacao A Where A.Estabelecimento_Id = Old.Estabelecimento_Codigo and A.Localizacao_Id = Old.Localizacao_Codigo and A.Produto_Id = Old.Produto_Codigo and A.Tamanho = 00),0),0),
IIF(OLD.T01_SALDO>NEW.T01_SALDO,Coalesce((Select First 1 A.Quantidade From TbEstoque_Saldo_Alocacao A Where A.Estabelecimento_Id = Old.Estabelecimento_Codigo and A.Localizacao_Id = Old.Localizacao_Codigo and A.Produto_Id = Old.Produto_Codigo and A.Tamanho = 01),0),0),
IIF(OLD.T02_SALDO>NEW.T02_SALDO,Coalesce((Select First 1 A.Quantidade From TbEstoque_Saldo_Alocacao A Where A.Estabelecimento_Id = Old.Estabelecimento_Codigo and A.Localizacao_Id = Old.Localizacao_Codigo and A.Produto_Id = Old.Produto_Codigo and A.Tamanho = 02),0),0),
IIF(OLD.T03_SALDO>NEW.T03_SALDO,Coalesce((Select First 1 A.Quantidade From TbEstoque_Saldo_Alocacao A Where A.Estabelecimento_Id = Old.Estabelecimento_Codigo and A.Localizacao_Id = Old.Localizacao_Codigo and A.Produto_Id = Old.Produto_Codigo and A.Tamanho = 03),0),0),
IIF(OLD.T04_SALDO>NEW.T04_SALDO,Coalesce((Select First 1 A.Quantidade From TbEstoque_Saldo_Alocacao A Where A.Estabelecimento_Id = Old.Estabelecimento_Codigo and A.Localizacao_Id = Old.Localizacao_Codigo and A.Produto_Id = Old.Produto_Codigo and A.Tamanho = 04),0),0),
IIF(OLD.T05_SALDO>NEW.T05_SALDO,Coalesce((Select First 1 A.Quantidade From TbEstoque_Saldo_Alocacao A Where A.Estabelecimento_Id = Old.Estabelecimento_Codigo and A.Localizacao_Id = Old.Localizacao_Codigo and A.Produto_Id = Old.Produto_Codigo and A.Tamanho = 05),0),0),
IIF(OLD.T06_SALDO>NEW.T06_SALDO,Coalesce((Select First 1 A.Quantidade From TbEstoque_Saldo_Alocacao A Where A.Estabelecimento_Id = Old.Estabelecimento_Codigo and A.Localizacao_Id = Old.Localizacao_Codigo and A.Produto_Id = Old.Produto_Codigo and A.Tamanho = 06),0),0),
IIF(OLD.T07_SALDO>NEW.T07_SALDO,Coalesce((Select First 1 A.Quantidade From TbEstoque_Saldo_Alocacao A Where A.Estabelecimento_Id = Old.Estabelecimento_Codigo and A.Localizacao_Id = Old.Localizacao_Codigo and A.Produto_Id = Old.Produto_Codigo and A.Tamanho = 07),0),0),
IIF(OLD.T08_SALDO>NEW.T08_SALDO,Coalesce((Select First 1 A.Quantidade From TbEstoque_Saldo_Alocacao A Where A.Estabelecimento_Id = Old.Estabelecimento_Codigo and A.Localizacao_Id = Old.Localizacao_Codigo and A.Produto_Id = Old.Produto_Codigo and A.Tamanho = 08),0),0),
IIF(OLD.T09_SALDO>NEW.T09_SALDO,Coalesce((Select First 1 A.Quantidade From TbEstoque_Saldo_Alocacao A Where A.Estabelecimento_Id = Old.Estabelecimento_Codigo and A.Localizacao_Id = Old.Localizacao_Codigo and A.Produto_Id = Old.Produto_Codigo and A.Tamanho = 09),0),0),
IIF(OLD.T10_SALDO>NEW.T10_SALDO,Coalesce((Select First 1 A.Quantidade From TbEstoque_Saldo_Alocacao A Where A.Estabelecimento_Id = Old.Estabelecimento_Codigo and A.Localizacao_Id = Old.Localizacao_Codigo and A.Produto_Id = Old.Produto_Codigo and A.Tamanho = 10),0),0),
IIF(OLD.T11_SALDO>NEW.T11_SALDO,Coalesce((Select First 1 A.Quantidade From TbEstoque_Saldo_Alocacao A Where A.Estabelecimento_Id = Old.Estabelecimento_Codigo and A.Localizacao_Id = Old.Localizacao_Codigo and A.Produto_Id = Old.Produto_Codigo and A.Tamanho = 11),0),0),
IIF(OLD.T12_SALDO>NEW.T12_SALDO,Coalesce((Select First 1 A.Quantidade From TbEstoque_Saldo_Alocacao A Where A.Estabelecimento_Id = Old.Estabelecimento_Codigo and A.Localizacao_Id = Old.Localizacao_Codigo and A.Produto_Id = Old.Produto_Codigo and A.Tamanho = 12),0),0),
IIF(OLD.T13_SALDO>NEW.T13_SALDO,Coalesce((Select First 1 A.Quantidade From TbEstoque_Saldo_Alocacao A Where A.Estabelecimento_Id = Old.Estabelecimento_Codigo and A.Localizacao_Id = Old.Localizacao_Codigo and A.Produto_Id = Old.Produto_Codigo and A.Tamanho = 13),0),0),
IIF(OLD.T14_SALDO>NEW.T14_SALDO,Coalesce((Select First 1 A.Quantidade From TbEstoque_Saldo_Alocacao A Where A.Estabelecimento_Id = Old.Estabelecimento_Codigo and A.Localizacao_Id = Old.Localizacao_Codigo and A.Produto_Id = Old.Produto_Codigo and A.Tamanho = 14),0),0),
IIF(OLD.T15_SALDO>NEW.T15_SALDO,Coalesce((Select First 1 A.Quantidade From TbEstoque_Saldo_Alocacao A Where A.Estabelecimento_Id = Old.Estabelecimento_Codigo and A.Localizacao_Id = Old.Localizacao_Codigo and A.Produto_Id = Old.Produto_Codigo and A.Tamanho = 15),0),0),
IIF(OLD.T16_SALDO>NEW.T16_SALDO,Coalesce((Select First 1 A.Quantidade From TbEstoque_Saldo_Alocacao A Where A.Estabelecimento_Id = Old.Estabelecimento_Codigo and A.Localizacao_Id = Old.Localizacao_Codigo and A.Produto_Id = Old.Produto_Codigo and A.Tamanho = 16),0),0),
IIF(OLD.T17_SALDO>NEW.T17_SALDO,Coalesce((Select First 1 A.Quantidade From TbEstoque_Saldo_Alocacao A Where A.Estabelecimento_Id = Old.Estabelecimento_Codigo and A.Localizacao_Id = Old.Localizacao_Codigo and A.Produto_Id = Old.Produto_Codigo and A.Tamanho = 17),0),0),
IIF(OLD.T18_SALDO>NEW.T18_SALDO,Coalesce((Select First 1 A.Quantidade From TbEstoque_Saldo_Alocacao A Where A.Estabelecimento_Id = Old.Estabelecimento_Codigo and A.Localizacao_Id = Old.Localizacao_Codigo and A.Produto_Id = Old.Produto_Codigo and A.Tamanho = 18),0),0),
IIF(OLD.T19_SALDO>NEW.T19_SALDO,Coalesce((Select First 1 A.Quantidade From TbEstoque_Saldo_Alocacao A Where A.Estabelecimento_Id = Old.Estabelecimento_Codigo and A.Localizacao_Id = Old.Localizacao_Codigo and A.Produto_Id = Old.Produto_Codigo and A.Tamanho = 19),0),0),
IIF(OLD.T20_SALDO>NEW.T20_SALDO,Coalesce((Select First 1 A.Quantidade From TbEstoque_Saldo_Alocacao A Where A.Estabelecimento_Id = Old.Estabelecimento_Codigo and A.Localizacao_Id = Old.Localizacao_Codigo and A.Produto_Id = Old.Produto_Codigo and A.Tamanho = 20),0),0)

FROM RDB$DATABASE

Into :SALDO,
     :SALDO01,:SALDO02,:SALDO03,:SALDO04,
     :SALDO05,:SALDO06,:SALDO07,:SALDO08,
     :SALDO09,:SALDO10,:SALDO11,:SALDO12,
     :SALDO13,:SALDO14,:SALDO15,:SALDO16,
     :SALDO17,:SALDO18,:SALDO19,:SALDO20,
     :SALDODE,:SALDOEM,
     :ALOC,
     :ALOC01,:ALOC02,:ALOC03,:ALOC04,
     :ALOC05,:ALOC06,:ALOC07,:ALOC08,
     :ALOC09,:ALOC10,:ALOC11,:ALOC12,
     :ALOC13,:ALOC14,:ALOC15,:ALOC16,
     :ALOC17,:ALOC18,:ALOC19,:ALOC20
Do
begin

  Msg = 'SALDO NAO PODE FICAR NEGATIVO:'||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||
        ' ESTABELECIMENTO: '||OLD.Estabelecimento_Codigo||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||
        ' LOCALIZACAO: '||OLD.Localizacao_Codigo||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||
        ' PRODUTO: '||OLD.PRODUTO_CODIGO;
--  Msg2= '. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  . . . . . . . . . . . . . . . . . . . . . . ';

  FOR SELECT G.T01,G.T02,G.T03,G.T04,G.T05,G.T06,G.T07,G.T08,G.T09,G.T10,
             G.T11,G.T12,G.T13,G.T14,G.T15,G.T16,G.T17,G.T18,G.T19,G.T20
  FROM TBPRODUTO P, TBGRADE G
  WHERE P.GRADE_CODIGO = G.CODIGO
  AND P.CODIGO = :OLD.PRODUTO_CODIGO
  INTO :G01,:G02,:G03,:G04,:G05,:G06,:G07,:G08,:G09,:G10,
       :G11,:G12,:G13,:G14,:G15,:G16,:G17,:G18,:G19,:G20 
    DO begin
       end

  if (NEW.T01_SALDO-:ALOC01 < 0) then  Exception  "..." :Msg||' TAM.'||:G01||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' SALDO FISICO: '||FN_NUMBER_TRIM(OLD.T01_SALDO)||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' QUANT.ALOCADO: '||FN_NUMBER_TRIM(:ALOC01)||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' QUANT.DISPONIVEL: '||FN_NUMBER_TRIM(CAST(OLD.T01_SALDO-:ALOC01 AS VARCHAR(20)))||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' QUANT.A BAIXAR: '||FN_NUMBER_TRIM(:SALDO01);
  if (NEW.T02_SALDO-:ALOC02 < 0) then  Exception  "..." :Msg||' TAM.'||:G02||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' SALDO FISICO: '||FN_NUMBER_TRIM(OLD.T02_SALDO)||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' QUANT.ALOCADO: '||FN_NUMBER_TRIM(:ALOC02)||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' QUANT.DISPONIVEL: '||FN_NUMBER_TRIM(CAST(OLD.T02_SALDO-:ALOC02 AS VARCHAR(20)))||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' QUANT.A BAIXAR: '||FN_NUMBER_TRIM(:SALDO02);
  if (NEW.T03_SALDO-:ALOC03 < 0) then  Exception  "..." :Msg||' TAM.'||:G03||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' SALDO FISICO: '||FN_NUMBER_TRIM(OLD.T03_SALDO)||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' QUANT.ALOCADO: '||FN_NUMBER_TRIM(:ALOC03)||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' QUANT.DISPONIVEL: '||FN_NUMBER_TRIM(CAST(OLD.T03_SALDO-:ALOC03 AS VARCHAR(20)))||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' QUANT.A BAIXAR: '||FN_NUMBER_TRIM(:SALDO03);
  if (NEW.T04_SALDO-:ALOC04 < 0) then  Exception  "..." :Msg||' TAM.'||:G04||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' SALDO FISICO: '||FN_NUMBER_TRIM(OLD.T04_SALDO)||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' QUANT.ALOCADO: '||FN_NUMBER_TRIM(:ALOC04)||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' QUANT.DISPONIVEL: '||FN_NUMBER_TRIM(CAST(OLD.T04_SALDO-:ALOC04 AS VARCHAR(20)))||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' QUANT.A BAIXAR: '||FN_NUMBER_TRIM(:SALDO04);
  if (NEW.T05_SALDO-:ALOC05 < 0) then  Exception  "..." :Msg||' TAM.'||:G05||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' SALDO FISICO: '||FN_NUMBER_TRIM(OLD.T05_SALDO)||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' QUANT.ALOCADO: '||FN_NUMBER_TRIM(:ALOC05)||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' QUANT.DISPONIVEL: '||FN_NUMBER_TRIM(CAST(OLD.T05_SALDO-:ALOC05 AS VARCHAR(20)))||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' QUANT.A BAIXAR: '||FN_NUMBER_TRIM(:SALDO05);
  if (NEW.T06_SALDO-:ALOC06 < 0) then  Exception  "..." :Msg||' TAM.'||:G06||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' SALDO FISICO: '||FN_NUMBER_TRIM(OLD.T06_SALDO)||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' QUANT.ALOCADO: '||FN_NUMBER_TRIM(:ALOC06)||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' QUANT.DISPONIVEL: '||FN_NUMBER_TRIM(CAST(OLD.T06_SALDO-:ALOC06 AS VARCHAR(20)))||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' QUANT.A BAIXAR: '||FN_NUMBER_TRIM(:SALDO06);
  if (NEW.T07_SALDO-:ALOC07 < 0) then  Exception  "..." :Msg||' TAM.'||:G07||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' SALDO FISICO: '||FN_NUMBER_TRIM(OLD.T07_SALDO)||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' QUANT.ALOCADO: '||FN_NUMBER_TRIM(:ALOC07)||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' QUANT.DISPONIVEL: '||FN_NUMBER_TRIM(CAST(OLD.T07_SALDO-:ALOC07 AS VARCHAR(20)))||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' QUANT.A BAIXAR: '||FN_NUMBER_TRIM(:SALDO07);
  if (NEW.T08_SALDO-:ALOC08 < 0) then  Exception  "..." :Msg||' TAM.'||:G08||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' SALDO FISICO: '||FN_NUMBER_TRIM(OLD.T08_SALDO)||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' QUANT.ALOCADO: '||FN_NUMBER_TRIM(:ALOC08)||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' QUANT.DISPONIVEL: '||FN_NUMBER_TRIM(CAST(OLD.T08_SALDO-:ALOC08 AS VARCHAR(20)))||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' QUANT.A BAIXAR: '||FN_NUMBER_TRIM(:SALDO08);
  if (NEW.T09_SALDO-:ALOC09 < 0) then  Exception  "..." :Msg||' TAM.'||:G09||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' SALDO FISICO: '||FN_NUMBER_TRIM(OLD.T09_SALDO)||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' QUANT.ALOCADO: '||FN_NUMBER_TRIM(:ALOC09)||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' QUANT.DISPONIVEL: '||FN_NUMBER_TRIM(CAST(OLD.T09_SALDO-:ALOC09 AS VARCHAR(20)))||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' QUANT.A BAIXAR: '||FN_NUMBER_TRIM(:SALDO09);
  if (NEW.T10_SALDO-:ALOC10 < 0) then  Exception  "..." :Msg||' TAM.'||:G10||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' SALDO FISICO: '||FN_NUMBER_TRIM(OLD.T10_SALDO)||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' QUANT.ALOCADO: '||FN_NUMBER_TRIM(:ALOC10)||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' QUANT.DISPONIVEL: '||FN_NUMBER_TRIM(CAST(OLD.T10_SALDO-:ALOC10 AS VARCHAR(20)))||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' QUANT.A BAIXAR: '||FN_NUMBER_TRIM(:SALDO10);
                                                                    
  if (NEW.T11_SALDO-:ALOC11 < 0) then  Exception  "..." :Msg||' TAM.'||:G11||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' SALDO FISICO: '||FN_NUMBER_TRIM(OLD.T11_SALDO)||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' QUANT.ALOCADO: '||FN_NUMBER_TRIM(:ALOC11)||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' QUANT.DISPONIVEL: '||FN_NUMBER_TRIM(CAST(OLD.T11_SALDO-:ALOC11 AS VARCHAR(20)))||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' QUANT.A BAIXAR: '||FN_NUMBER_TRIM(:SALDO11);
  if (NEW.T12_SALDO-:ALOC12 < 0) then  Exception  "..." :Msg||' TAM.'||:G12||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' SALDO FISICO: '||FN_NUMBER_TRIM(OLD.T12_SALDO)||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' QUANT.ALOCADO: '||FN_NUMBER_TRIM(:ALOC12)||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' QUANT.DISPONIVEL: '||FN_NUMBER_TRIM(CAST(OLD.T12_SALDO-:ALOC12 AS VARCHAR(20)))||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' QUANT.A BAIXAR: '||FN_NUMBER_TRIM(:SALDO12);
  if (NEW.T13_SALDO-:ALOC13 < 0) then  Exception  "..." :Msg||' TAM.'||:G13||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' SALDO FISICO: '||FN_NUMBER_TRIM(OLD.T13_SALDO)||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' QUANT.ALOCADO: '||FN_NUMBER_TRIM(:ALOC13)||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' QUANT.DISPONIVEL: '||FN_NUMBER_TRIM(CAST(OLD.T13_SALDO-:ALOC13 AS VARCHAR(20)))||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' QUANT.A BAIXAR: '||FN_NUMBER_TRIM(:SALDO13);
  if (NEW.T14_SALDO-:ALOC14 < 0) then  Exception  "..." :Msg||' TAM.'||:G14||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' SALDO FISICO: '||FN_NUMBER_TRIM(OLD.T14_SALDO)||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' QUANT.ALOCADO: '||FN_NUMBER_TRIM(:ALOC14)||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' QUANT.DISPONIVEL: '||FN_NUMBER_TRIM(CAST(OLD.T14_SALDO-:ALOC14 AS VARCHAR(20)))||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' QUANT.A BAIXAR: '||FN_NUMBER_TRIM(:SALDO14);
  if (NEW.T15_SALDO-:ALOC15 < 0) then  Exception  "..." :Msg||' TAM.'||:G15||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' SALDO FISICO: '||FN_NUMBER_TRIM(OLD.T15_SALDO)||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' QUANT.ALOCADO: '||FN_NUMBER_TRIM(:ALOC15)||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' QUANT.DISPONIVEL: '||FN_NUMBER_TRIM(CAST(OLD.T15_SALDO-:ALOC15 AS VARCHAR(20)))||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' QUANT.A BAIXAR: '||FN_NUMBER_TRIM(:SALDO15);
  if (NEW.T16_SALDO-:ALOC16 < 0) then  Exception  "..." :Msg||' TAM.'||:G16||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' SALDO FISICO: '||FN_NUMBER_TRIM(OLD.T16_SALDO)||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' QUANT.ALOCADO: '||FN_NUMBER_TRIM(:ALOC16)||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' QUANT.DISPONIVEL: '||FN_NUMBER_TRIM(CAST(OLD.T16_SALDO-:ALOC16 AS VARCHAR(20)))||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' QUANT.A BAIXAR: '||FN_NUMBER_TRIM(:SALDO16);
  if (NEW.T17_SALDO-:ALOC17 < 0) then  Exception  "..." :Msg||' TAM.'||:G17||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' SALDO FISICO: '||FN_NUMBER_TRIM(OLD.T17_SALDO)||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' QUANT.ALOCADO: '||FN_NUMBER_TRIM(:ALOC17)||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' QUANT.DISPONIVEL: '||FN_NUMBER_TRIM(CAST(OLD.T17_SALDO-:ALOC17 AS VARCHAR(20)))||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' QUANT.A BAIXAR: '||FN_NUMBER_TRIM(:SALDO17);
  if (NEW.T18_SALDO-:ALOC18 < 0) then  Exception  "..." :Msg||' TAM.'||:G18||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' SALDO FISICO: '||FN_NUMBER_TRIM(OLD.T18_SALDO)||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' QUANT.ALOCADO: '||FN_NUMBER_TRIM(:ALOC18)||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' QUANT.DISPONIVEL: '||FN_NUMBER_TRIM(CAST(OLD.T18_SALDO-:ALOC18 AS VARCHAR(20)))||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' QUANT.A BAIXAR: '||FN_NUMBER_TRIM(:SALDO18);
  if (NEW.T19_SALDO-:ALOC19 < 0) then  Exception  "..." :Msg||' TAM.'||:G19||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' SALDO FISICO: '||FN_NUMBER_TRIM(OLD.T19_SALDO)||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' QUANT.ALOCADO: '||FN_NUMBER_TRIM(:ALOC19)||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' QUANT.DISPONIVEL: '||FN_NUMBER_TRIM(CAST(OLD.T19_SALDO-:ALOC19 AS VARCHAR(20)))||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' QUANT.A BAIXAR: '||FN_NUMBER_TRIM(:SALDO19);
  if (NEW.T20_SALDO-:ALOC20 < 0) then  Exception  "..." :Msg||' TAM.'||:G20||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' SALDO FISICO: '||FN_NUMBER_TRIM(OLD.T20_SALDO)||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' QUANT.ALOCADO: '||FN_NUMBER_TRIM(:ALOC20)||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' QUANT.DISPONIVEL: '||FN_NUMBER_TRIM(CAST(OLD.T20_SALDO-:ALOC20 AS VARCHAR(20)))||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' QUANT.A BAIXAR: '||FN_NUMBER_TRIM(:SALDO20);

  if (NEW.SALDO    -:ALOC   < 0) then  Exception  "..." :Msg||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' SALDO FISICO: '||FN_NUMBER_TRIM(OLD.SALDO)||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' QUANT.ALOCADO: '||FN_NUMBER_TRIM(:ALOC)||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' QUANT.DISPONIVEL: '||FN_NUMBER_TRIM(CAST(OLD.SALDO-:ALOC AS VARCHAR(20)))||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' QUANT. A BAIXAR: '||FN_NUMBER_TRIM(:SALDO);

  if (NEW.SALDO_DETERCEIRO < 0) then Exception "..." Msg||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' QUANT.DISPONIVEL '||FN_NUMBER_TRIM(OLD.SALDO_DETERCEIRO)||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||'   QUANT.DE TERC.A BAIXAR: '||FN_NUMBER_TRIM(:SALDODE);
  if (NEW.SALDO_EMTERCEIRO < 0) then Exception "..." Msg||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||' QUANT.DISPONIVEL '||FN_NUMBER_TRIM(OLD.SALDO_EMTERCEIRO)||ASCII_CHAR(13)||ASCII_CHAR(10)||'<br>'||'   QUANT.EM TERC.A BAIXAR: '||FN_NUMBER_TRIM(:SALDOEM);
end

End;