CREATE OR REPLACE PROCEDURE ATUALIZA_ITENS_PED(
    P_FILIAL IN NOTAFISCAL.CDFILIAL%TYPE,
    P_LANCTONF IN NOTAFISCAL.NRLANCTONF%TYPE)

IS
   V_IDREPLITRANS PAREXPEDICAO.IDREPLITRANS%TYPE;
   V_IDUTRESELOTE PARAMPEDIDO.IDUTRESELOTE%TYPE;
   V_CDFILIAL     RESERPRODPED.CDFILIAL%TYPE;
   V_NRRESPRODPED NOVOCODIGO.NRSEQUENCIAL%TYPE;
   V_QTRESPRODPED RESERPRODPED.QTRESPRODPED%TYPE;
   V_CDPRODUTO    RESERPRODPED.CDPRODUTO%TYPE;
   V_CDALMOXARIFE RESERPRODPED.CDALMOXARIFE%TYPE;
   V_CDLOCALESTOQ RESERPRODPED.CDLOCALESTOQ%TYPE;
   V_NRLOTEESTQ   RESERPRODPED.NRLOTEESTQ%TYPE;
   V_NRSUBLOTE    RESERPRODPED.NRSUBLOTE%TYPE;
   V_NRPEDIDO     RESERPRODPED.NRPEDIDO%TYPE;
   V_NRITEMPED    RESERPRODPED.NRITEMPED%TYPE;
   V_IDCNTRESTOQ  PRODFILI.IDCNTRESTOQ%TYPE;
   V_COUNTNRGRADE INTEGER;
   V_NRLANCESTQ   LANCTOESTOQ.NRLANCESTQ%TYPE;
BEGIN
   /*LISTA OS ITENS DA NF */
   FOR C_ITENNF IN (SELECT IT.CDFILIALPED,  IT.NRPEDIDO,     IT.NRITEMPED,   EX.NRGRADEDISTR, EX.CDVIATURA,
                           IT.CDALMOXARIFE, IT.CDLOCALESTOQ, IT.NRLOTEESTQ,  IT.NRSUBLOTE, IT.QTITEMPROD,
                           PR.VRFATOCONV, PR.CDPRODESTO, IT.IDATUESTPROD, IT.CDPRODUTO, NF.NREXPEDICAO
                      FROM ITEMPRODUTO IT, NOTAFISCAL NF, EXPEDICAO EX, PRODUTO PR
                     WHERE IT.CDFILIAL    = P_FILIAL
                       AND IT.NRLANCTONF  = P_LANCTONF
                       AND IT.CDFILIAL    = NF.CDFILIAL
                       AND IT.NRLANCTONF  = NF.NRLANCTONF
                       AND NF.CDFILIAL    = EX.CDFILIAL
                       AND NF.NREXPEDICAO = EX.NREXPEDICAO
                       AND IT.CDPRODUTO  = PR.CDPRODUTO

   ) LOOP
      /* APAGA DA GRADE */
      SELECT MAX(IDREPLITRANS)
        INTO V_IDREPLITRANS
        FROM PAREXPEDICAO
       WHERE CDFILIAL = P_FILIAL;
      IF (V_IDREPLITRANS = '3') THEN
         DELETE FROM ITGRADEDISTR
         WHERE CDFILIAL = C_ITENNF.CDFILIALPED
           AND NRGRADEDISTR = C_ITENNF.NRGRADEDISTR
           AND NRPEDIDO = C_ITENNF.NRPEDIDO
           AND NRITEMPED = C_ITENNF.NRITEMPED
           AND CDVIATURA = C_ITENNF.CDVIATURA;

         SELECT COUNT(NRGRADEDISTR)
           INTO V_COUNTNRGRADE
           FROM ITGRADEDISTR
          WHERE CDFILIAL = C_ITENNF.CDFILIALPED
            AND NRGRADEDISTR = C_ITENNF.NRGRADEDISTR;

         IF V_COUNTNRGRADE = 0 THEN
            DELETE FROM GRADEPEDIDO
            WHERE NRPEDIDO = C_ITENNF.NRPEDIDO
              AND CDFILIAL = C_ITENNF.CDFILIALPED;
         END IF;

      END IF;
      /*ATUALIZA RESERVA */
      SELECT MAX(IDUTRESELOTE)
        INTO V_IDUTRESELOTE
        FROM PARAMPEDIDO
       WHERE CDFILIAL = C_ITENNF.CDFILIALPED;

      IF V_IDUTRESELOTE = 'S' THEN
         SELECT MAX(NRLANCESTQ)
           INTO V_NRLANCESTQ
           FROM LANCTOESTOQ
          WHERE CDFILIAL    = P_FILIAL
            AND NRLANCTONF  = P_LANCTONF;

         IF (V_NRLANCESTQ IS NOT NULL) THEN
            SELECT MAX(CDFILIAL), MAX(NRRESPRODPED), MAX(QTRESPRODPED), MAX(CDPRODUTO),
                   MAX(CDALMOXARIFE), MAX(CDLOCALESTOQ), MAX(NRLOTEESTQ), MAX(NRSUBLOTE),
                   MAX(NRPEDIDO), MAX(NRITEMPED)
              INTO V_CDFILIAL, V_NRRESPRODPED, V_QTRESPRODPED, V_CDPRODUTO,
                   V_CDALMOXARIFE, V_CDLOCALESTOQ, V_NRLOTEESTQ, V_NRSUBLOTE,
                   V_NRPEDIDO, V_NRITEMPED
              FROM RESERPRODPED
             WHERE CDFILIAL    = C_ITENNF.CDFILIALPED
               AND NRPEDIDO  = C_ITENNF.NRPEDIDO
               AND NRITEMPED = C_ITENNF.NRITEMPED
               AND CDALMOXARIFE = C_ITENNF.CDALMOXARIFE
               AND CDLOCALESTOQ = C_ITENNF.CDLOCALESTOQ
               AND NRLOTEESTQ   = C_ITENNF.NRLOTEESTQ
               AND NRSUBLOTE    = C_ITENNF.NRSUBLOTE;
            IF V_NRRESPRODPED IS NOT NULL THEN
               UPDATE RESERPRODPED
                  SET QTRESPRODPED = V_QTRESPRODPED + (C_ITENNF.QTITEMPROD * C_ITENNF.VRFATOCONV),
                      QTRESPRODARR = V_QTRESPRODPED
                WHERE CDFILIAL = V_CDFILIAL
                  AND NRRESPRODPED = V_NRRESPRODPED
                  AND CDPRODUTO = V_CDPRODUTO
                  AND CDALMOXARIFE = V_CDALMOXARIFE
                  AND CDLOCALESTOQ = V_CDLOCALESTOQ
                  AND NRLOTEESTQ = V_NRLOTEESTQ
                  AND NRSUBLOTE = V_NRSUBLOTE
                  AND NRPEDIDO = V_NRPEDIDO
                  AND NRITEMPED = V_NRITEMPED;
            ELSE
               SELECT MAX(IDCNTRESTOQ)
                 INTO V_IDCNTRESTOQ
                 FROM PRODFILI
                WHERE (CDFILIAL = C_ITENNF.CDFILIALPED)
                  AND (CDPRODUTO = C_ITENNF.CDPRODUTO);
               IF V_IDCNTRESTOQ = 'S' THEN
                  NOVO_CODIGO('CDRESPEDIDO'||C_ITENNF.CDFILIALPED, V_NRRESPRODPED);
                  V_NRRESPRODPED := SUBSTR(V_NRRESPRODPED,11,10);

                  insert into RESERPRODPED
                 (CDFILIAL, NRRESPRODPED, CDPRODUTO, CDALMOXARIFE, CDLOCALESTOQ, NRLOTEESTQ, NRSUBLOTE,
                  QTRESPRODPED, NRPEDIDO, NRITEMPED, QTRESPRODARR)
                  VALUES
                 (C_ITENNF.CDFILIALPED, V_NRRESPRODPED, C_ITENNF.CDPRODESTO, C_ITENNF.CDALMOXARIFE, C_ITENNF.CDLOCALESTOQ, C_ITENNF.NRLOTEESTQ, C_ITENNF.NRSUBLOTE,
                  (C_ITENNF.QTITEMPROD * C_ITENNF.VRFATOCONV), C_ITENNF.NRPEDIDO, C_ITENNF.NRITEMPED, (C_ITENNF.QTITEMPROD * C_ITENNF.VRFATOCONV));

               END IF;
            END IF;
         END IF;
      END IF;

   END LOOP;
END;
/
