create or replace PROCEDURE GERA_EXPEDICAO
(P_CDFILIAL     IN EXPEDICAO.CDFILIAL%TYPE,
 P_NREXPEDICAO  IN EXPEDICAO.NREXPEDICAO%TYPE,
 P_CDEMPRESA    IN SERIENF.CDEMPRESA%TYPE     DEFAULT NULL,
 P_CDINSCESTA   IN SERIENF.CDINSCESTA%TYPE    DEFAULT NULL,
 P_CDINSCMUNI   IN SERIENF.CDINSCMUNI%TYPE    DEFAULT NULL,
 P_CDSERIE      IN SERIENF.CDSERIE%TYPE       DEFAULT NULL,
 P_CDTIPOOPER   IN TIPOOPERAC.CDTIPOOPER%TYPE DEFAULT NULL,
 P_CDSERIEENTR  IN SERIEENTR.CDSERIEENTR%TYPE DEFAULT NULL,
 P_CDTPOPERENTR IN TIPOOPERAC.CDTIPOOPER%TYPE DEFAULT NULL,
 P_CDTIPOCUSTO  IN TIPOCUSTO.CDTIPOCUSTO%TYPE DEFAULT NULL,
 P_IDSTATUSEXP OUT EXPEDICAO.IDSTATUSEXP%TYPE)
AS
   V_IDTABPRECO          PARAMPEDIDO.IDTABPRECO%TYPE;
   V_IDCALCPRECFAT       PARAMPEDIDO.IDCALCPRECFAT%TYPE;
   V_IDCUSEXP            PAREXPEDICAO.IDCUSEXP%TYPE;
   V_VRPRECO             ITEMPEDIDO.VRPREUNITPED%TYPE;
   V_VRCUSTO             ITEMPEDIDO.VRPREUNITPED%TYPE;
   V_VRFATPRECO          PRODFATOR.VRFATPRECO%TYPE;
   V_NRSEQ               ERROITEMEXP.NRSEQERROITEM%TYPE;
   V_IDOBRTPOPPED        PARAMPEDIDO.IDOBRTPOPPED%TYPE;
   V_IDDESTINO           ITEMEXPEDICAO.IDDESTINO%TYPE;
   V_CDDESTINO           ITEMEXPEDICAO.CDDESTINO%TYPE;
   V_IDUTVALIPED         PARAMPEDIDO.IDUTVALIPED%TYPE;
   V_IDCTRLESTQ          PARAMFILIAL.IDCTRLESTQ%TYPE;
   V_IDATUESTTPOP        TIPOOPERAC.IDATUESTTPOP%TYPE;
   V_IDUTILALMOX         PARAMFILIAL.IDUTILALMOX%TYPE;
   V_CDALMOXARIFE        PARAMFILIAL.CDALMOXARIFE%TYPE;
   V_CDALMOXEXP          ALMOXARIFADO.CDALMOXARIFE%TYPE;
   V_IDUTILLCESTQ        PARAMFILIAL.IDUTILLCESTQ%TYPE;
   V_CDLOCALESTOQ        PARAMFILIAL.CDLOCALESTOQ%TYPE;
   V_CDLOCALEXP          LOCALESTOQUE.CDLOCALESTOQ%TYPE;
   V_IDCNTRESTOQ         PRODFILI.IDCNTRESTOQ%TYPE;
   V_CDALMOXPRCLI        PRODFILI.CDALMOXPRCLI%TYPE;
   V_CDLOCALPRCLI        PRODFILI.CDLOCALPRCLI%TYPE;
   V_IDEMPRESANFEXP      PAREXPEDICAO.IDEMPRESANFEXP%TYPE;
   V_CDEMPRESA           EMPRESA.CDEMPRESA%TYPE;
   V_CDINSCESTA          INSCRESTAD.CDINSCESTA%TYPE;
   V_CDINSCMUNI          INSCRMUNICIP.CDINSCMUNI%TYPE;
   V_CDSERIE             SERIENF.CDSERIE%TYPE;
   V_PAREXP              INTEGER;
   V_IDPERADIEXP         PAREXPEDICAO.IDPERADIEXP%TYPE;
   V_VRPEADIEXP          PAREXPEDICAO.VRPEADIEXP%TYPE;
   V_QTMAXEXP            ITEMPEDIDO.QTITEMPED%TYPE;
   V_QTTOTAITEM          ITEMPEDIDO.QTITEMPED%TYPE;
   V_VRDESCEXP           ITEMPEDIDO.VRDESITPED%TYPE;
   V_IDGERPRODEST        PAREXPEDICAO.IDGERPRODEST%TYPE;
   V_CDPRODMOVI          PRODUTO.CDPRODUTO%TYPE;
   V_QTITEMPROD          ITEMPRODUTO.QTITEMPROD%TYPE;
   V_VRPESOLIQEXP        PRODUTO.VRPESOLIQUPR%TYPE;
   V_VRPESOBRUTOEXP      PRODUTO.VRPESOBRUTPR%TYPE;
   V_NRMAXITNF           SERIENF.NRMAXITNF%TYPE;
   V_SEQNF               NOVOCODIGO.NRSEQUENCIAL%TYPE;
   V_NRLANCTONF          NOTAFISCAL.NRLANCTONF%TYPE;
   V_SEQEST              NOVOCODIGO.NRSEQUENCIAL%TYPE;
   V_PRXLANCESTQ         LANCTOESTOQ.NRLANCESTQ%TYPE;
   V_ATUAESTOQUE         TIPOOPERAC.IDATUESTTPOP%TYPE;
   V_COUNTITEM           INTEGER;
   V_IDSITITEMPED        ITEMPEDIDO.IDSITITEMPED%TYPE;
   V_IDLANENCARGOPED     PARAMPEDIDO.IDLANENCARGOPED%TYPE;
   V_NOVANOTA            INTEGER;
   V_DTPROCESSA          PARAMFILIAL.DTPROCESSA%TYPE;
   V_DTINICIAL           DATE;
   V_DTFINAL             DATE;
   V_DTANTERIOR          DATE;
   V_ERROESTOQ           INTEGER;
   V_ITEMPA              INTEGER;
   V_IDSITPEDIDO         PEDIDO.IDSITPEDIDO%TYPE;
   V_IDAGRUPAEXP         PAREXPEDICAO.IDAGRUPAEXP%TYPE;
   V_CDQUEBRANF          CLIENTE.CDCLIENTE%TYPE;
   V_IDGERANFPEDI        CLIENTE.IDGERANFPEDI%TYPE;
   V_EXISTERESERVA       INTEGER;
   V_ERRO                INTEGER;
   V_NAOOK               INTEGER;
   V_OKNF                INTEGER;
   V_OKITEMPED           INTEGER;
   V_DTEXPEDICAO         EXPEDICAO.DTEXPEDICAO%TYPE;
   V_CDOPERADOR          OPERADOR.CDOPERADOR%TYPE;
   V_EXISTENF            INTEGER;
   V_QTNF                INTEGER;
   V_IDUTCTRLLOTE        PRODFILI.IDUTCTRLLOTE%TYPE;
   V_QTEXPEDIDA          ITEMPEDIDO.QTITEMPED%TYPE;
   V_TOTALESTOQUE        ITEMPEDIDO.QTITEMPED%TYPE;
   V_IDGERAREPLIC        PAREXPEDICAO.IDGERAREPLIC%TYPE;
   V_IDUTILLOTE          PARAMFILIAL.IDUTILLOTE%TYPE;
   V_DSNUMELOTE          PARAMFILIAL.DSNUMELOTE%TYPE;
   V_DSSEQULOTE          PARAMFILIAL.DSSEQULOTE%TYPE;
   V_IDCONMAXITEM        SERIENF.IDCONMAXITEM%TYPE;
   V_CDTIPOOPER          TIPOOPERAC.CDTIPOOPER%TYPE;
   V_CDTIPOOPERORIG      TIPOOPERAC.CDTIPOOPER%TYPE;
   V_CDFILIDEST          PEDIDO.CDFILIALPED%TYPE;
   V_IDTIPOEXP           EXPRELACITEMPPED.IDTIPOEXP%TYPE;
   V_QTRESERVTOT         ITEMPEDIDO.QTITEMPED%TYPE;
   V_QTEXPEDIRMAX        ITEMPEDIDO.QTITEMPED%TYPE;
   V_IDTPCOMPPROD        PARAMPEDIDO.IDTPCOMPPROD%TYPE;
   V_EXISTECOMPOSICAO    INTEGER;
   V_CDPRODEMIS          ITEMPRODUTO.CDPRODEMIS%TYPE;
   V_DSPRODEMIS          ITEMPRODUTO.DSPRODEMIS%TYPE;
   V_VRFATOCONV          PRODUTO.VRFATOCONV%TYPE;
   V_VRTOTAITEM          ITEMPEDIDO.VRPREUNITPED%TYPE;
   V_QTEXPTOTAL          ITEMPEDIDO.QTITEMPED%TYPE;
   V_VRTOTAITEMCOMP      ITEMPEDIDO.VRPREUNITPED%TYPE;
   V_CDPRODESTO          PRODUTO.CDPRODESTO%TYPE;
   V_VRPESOLIQUPR        PRODUTO.VRPESOLIQUPR%TYPE;
   V_VRPESOBRUTPR        PRODUTO.VRPESOBRUTPR%TYPE;
   V_NRINSJURCLIE        CLIENTE.NRINSJURCLIE%TYPE;
   V_INSCESTADOK         INTEGER;
   V_CDTPOPERPERDA       PAREXPEDICAO.CDTPOPERPERDA%TYPE;
   V_COUNTPERDAPA        INTEGER;
   V_COUNTPERDASE        INTEGER;
   V_NRORDPROD           ORDPRODUCAO.NRORDPROD%TYPE;
   V_ERROOP              VARCHAR2(01);
   V_NFSINDUSTRIALIZACAO VARCHAR2(30);
   V_NRLANCESTQ          LANCTOESTOQ.NRLANCESTQ%TYPE;
   V_IDSISTEMA           NOTAFISCAL.IDSISTEMA%TYPE;
   V_IDUTSEPPEDEXP       PAREXPEDICAO.IDUTSEPPEDEXP%TYPE;
   V_IDRETPROEXP         PAREXPEDICAO.IDRETPROEXP%TYPE;
   V_COUNT_ERROPED       INTEGER;
   V_CDFILIALREPLIC      NOTAFISCAL.CDFILIAL%TYPE;
   V_CDFILIRECDREPLIC    NOTAFISCAL.CDFILIAL%TYPE;
   V_NRLANCTONFREPLIC    NOTAFISCAL.NRLANCTONF%TYPE;
   V_DTENTRSAIDREPLIC    NOTAFISCAL.DTENTRSAID%TYPE;
   V_IDENTRSAIDOPREPLIC  NOTAFISCAL.IDENTRSAIDOP%TYPE;
   V_CDTIPOOPERREPLIC    NOTAFISCAL.CDTIPOOPER%TYPE;
   V_IDATUESTTPOPREPLIC  TIPOOPERAC.IDATUESTTPOP%TYPE;
   V_NRLANCESTQREPLIC    LANCTOESTOQ.NRLANCESTQ%TYPE;
   V_NRLANCTONFPE        NOTAFISCAL.NRLANCTONF%TYPE;
   V_CDCLIENTEREPLIC     PEDIDO.CDCLIENTE%TYPE;
   V_IDITEMCOMPPROD      ITEMEXPEDICAO.IDITEMCOMPPROD%TYPE;
   V_IDUTRESELOTE        PARAMPEDIDO.IDUTRESELOTE%TYPE;
   V_IRREGESTOQ          INTEGER;
   V_CDVENDEDOR          PEDIDO.CDVENDEDOR%TYPE;  
BEGIN
   BEGIN
      SELECT NVL(IDTABPRECO, '1'), NVL(IDCALCPRECFAT, 'E'), NVL(IDOBRTPOPPED, 'N'), NVL(IDUTVALIPED,'N'), NVL(IDLANENCARGOPED,'N'),
             NVL(IDTPCOMPPROD,'F')
        INTO V_IDTABPRECO,  V_IDCALCPRECFAT,  V_IDOBRTPOPPED,  V_IDUTVALIPED, V_IDLANENCARGOPED,
             V_IDTPCOMPPROD
        FROM PARAMPEDIDO
       WHERE CDFILIAL = P_CDFILIAL;
   EXCEPTION
      WHEN NO_DATA_FOUND THEN BEGIN
         V_IDTABPRECO      := '1';
         V_IDCALCPRECFAT   := 'E';
         V_IDOBRTPOPPED    := 'N';
         V_IDUTVALIPED     := 'N';
         V_IDLANENCARGOPED := 'N';
         V_IDTPCOMPPROD    := 'F';
      END;
   END;
   BEGIN
      SELECT NVL(IDCUSEXP, '03'),    NVL(IDEMPRESANFEXP, 'EMPRESAFILIAL') ,   NVL(IDPERADIEXP,'N'),    NVL(VRPEADIEXP,0),   NVL(IDGERPRODEST, '1'), NVL(IDAGRUPAEXP,'C'),
             NVL(IDGERAREPLIC, '1'), CDTPOPERPERDA,                           NVL(IDUTSEPPEDEXP, 'N'), NVL(IDRETPROEXP, '1') IDRETPROEXP,
             COUNT(1)
        INTO V_IDCUSEXP,             V_IDEMPRESANFEXP,                        V_IDPERADIEXP,           V_VRPEADIEXP,        V_IDGERPRODEST,         V_IDAGRUPAEXP,
             V_IDGERAREPLIC,         V_CDTPOPERPERDA,                         V_IDUTSEPPEDEXP,         V_IDRETPROEXP,
             V_PAREXP
        FROM PAREXPEDICAO
       WHERE CDFILIAL = P_CDFILIAL
      GROUP BY NVL(IDCUSEXP, '03'),  NVL(IDEMPRESANFEXP, 'EMPRESAFILIAL') ,   NVL(IDPERADIEXP,'N'),    NVL(VRPEADIEXP,0),   NVL(IDGERPRODEST, '1'), NVL(IDAGRUPAEXP, 'C'),
               NVL(IDGERAREPLIC, '1'), CDTPOPERPERDA,                         NVL(IDUTSEPPEDEXP, 'N'), NVL(IDRETPROEXP,'1');
   EXCEPTION
      WHEN NO_DATA_FOUND THEN BEGIN
         V_IDCUSEXP := '03';
         V_IDEMPRESANFEXP := 'EMPRESAFILIAL';
         V_IDPERADIEXP := 'N';
         V_VRPEADIEXP := 0;
         V_IDGERPRODEST := '1';
         V_IDAGRUPAEXP := 'C';
         V_IDGERAREPLIC := '1';
         V_CDTPOPERPERDA := NULL;
         V_IDUTSEPPEDEXP := 'N';
         V_IDRETPROEXP := '1';
         V_PAREXP := 0;
      END;
   END;
   BEGIN
      SELECT NVL(IDCTRLESTQ,'S'),   NVL(IDUTILALMOX,'N'),   CDALMOXARIFE,   NVL(IDUTILLCESTQ,'N'),   CDLOCALESTOQ,
             TRUNC(NVL(DTPROCESSA,SYSDATE))
        INTO V_IDCTRLESTQ, V_IDUTILALMOX, V_CDALMOXARIFE, V_IDUTILLCESTQ, V_CDLOCALESTOQ,
             V_DTPROCESSA
        FROM PARAMFILIAL
       WHERE CDFILIAL = P_CDFILIAL;
   EXCEPTION
      WHEN NO_DATA_FOUND THEN BEGIN
         V_IDCTRLESTQ := 'S';
         V_IDUTILALMOX := 'N';
         V_CDALMOXARIFE := NULL;
         V_IDUTILLCESTQ := 'N';
         V_CDLOCALESTOQ := NULL;
         V_DTPROCESSA := TRUNC(SYSDATE);
      END;
   END;
   V_CDEMPRESA  := NULL;
   V_CDINSCESTA := NULL;
   V_CDINSCMUNI := NULL;
   V_CDSERIE    := NULL;
   IF (V_IDEMPRESANFEXP = 'EMPRESAFILIAL') THEN
      BEGIN
         SELECT CDEMPRESA,   CDINSCESTA,   CDINSCMUNI,   CDSERIE
           INTO V_CDEMPRESA, V_CDINSCESTA, V_CDINSCMUNI, V_CDSERIE
           FROM FILIAL
          WHERE CDFILIAL = P_CDFILIAL;
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_CDEMPRESA  := NULL;
            V_CDINSCESTA := NULL;
            V_CDINSCMUNI := NULL;
            V_CDSERIE    := NULL;
      END;
   ELSE
      IF (V_IDEMPRESANFEXP = 'INFORMALANCEXP') THEN
         V_CDEMPRESA  := P_CDEMPRESA;
         V_CDINSCESTA := P_CDINSCESTA;
         V_CDINSCMUNI := P_CDINSCMUNI;
         V_CDSERIE    := P_CDSERIE;
      END IF;
   END IF;
   SELECT MAX(DTEXPEDICAO), MAX(CDOPERADOR)
     INTO V_DTEXPEDICAO, V_CDOPERADOR
     FROM EXPEDICAO
    WHERE CDFILIAL = P_CDFILIAL
      AND NREXPEDICAO = P_NREXPEDICAO;
   V_CDTIPOOPER := NULL;
   FOR C_ITEMEXP IN (SELECT CDFILIAL, NREXPEDICAO, NRPEDIDO, NRITEMPED , QTITEMEXP, IDTIPOEXP
                       FROM EXPRELACITEMPPED
                      WHERE CDFILIAL = P_CDFILIAL
                        AND NREXPEDICAO = P_NREXPEDICAO)
   LOOP
      /*LOOP NOS ITENS MARCADOS PARA EXPEDIR*/
      FOR C_ITEM IN (SELECT I.CDFILIAL, I.NRPEDIDO, P.CDTIPOOPER, P.CDFILIALPED, P.CDCLIENTE, P.DTVALIPED, P.CDVENDEDOR,
                            P.IDTPIJURTRAN, P.NRINSJURTRAN, P.CDTIPTRANS, P.IDPGFRETTRAN, P.NRPLACAVEPED,  P.SGUFVEICPED, NVL(P.IDPEDINDUSTRIA, 'N') IDPEDINDUSTRIA,
                            P.CDMODPGTO , P.IDTPFORPGTPED , P.NRDIAPGFIXPED, P.NRPARCPGTOPED, P.IDUTDIAPAGPED, P.DTENTPED, P.CDPRECOBASE,
                            I.NRITEMPED, PR.CDPRODUTO, PR.CDPRODESTO, PR.VRFATOCONV, PR.VRPESOBRUTPR, PR.VRPESOLIQUPR, I.IDSITITEMPED,
                            NVL(I.CDEMISSAOITPED, PR.CDARVPROD) AS CDPRODEMIS, NVL(I.DSEMISSAOITPED, PR.NMPRODUTO) AS DSPRODEMIS,
                            E.QTITEMEXP, I.QTITEMPED, I.QTEXPITEMPED, I.VRPREUNITPED, I.CDFATPRECO, I.VRDESITPED,
                            I.IDITEMPEDTERC
                       FROM ITEMPEDIDO I JOIN PRODUTO PR
                                           ON I.CDPRODUTO = PR.CDPRODUTO
                                         JOIN PEDIDO P
                                           ON I.CDFILIAL = P.CDFILIAL
                                          AND I.NRPEDIDO = P.NRPEDIDO
                                         JOIN EXPRELACITEMPPED E
                                           ON I.CDFILIAL = E.CDFILIAL
                                          AND I.NRPEDIDO = E.NRPEDIDO
                                          AND I.NRITEMPED = E.NRITEMPED
                      WHERE I.CDFILIAL = C_ITEMEXP.CDFILIAL
                        AND I.NRPEDIDO = C_ITEMEXP.NRPEDIDO
                        AND I.NRITEMPED = C_ITEMEXP.NRITEMPED
                        AND E.NREXPEDICAO = C_ITEMEXP.NREXPEDICAO)
      LOOP
         V_IDDESTINO    := NULL;
         V_CDDESTINO    := C_ITEM.CDCLIENTE;
         V_CDALMOXEXP   := NULL;
         V_CDLOCALEXP   := NULL;
         V_IDATUESTTPOP := NULL;
         V_IDCNTRESTOQ  := NULL;
         V_EXISTECOMPOSICAO := 0;
         IF (V_IDEMPRESANFEXP = 'EMPRESACLIENTE') THEN
            BEGIN
               SELECT CDEMPRESA,   CDINSCESTA,   CDINSCMUNI,   CDSERIE
                 INTO V_CDEMPRESA, V_CDINSCESTA, V_CDINSCMUNI, V_CDSERIE
                 FROM CLIENTE
                WHERE CDCLIENTE = C_ITEM.CDCLIENTE;
            EXCEPTION
               WHEN NO_DATA_FOUND THEN BEGIN
                  V_CDEMPRESA  := NULL;
                  V_CDINSCESTA := NULL;
                  V_CDINSCMUNI := NULL;
                  V_CDSERIE    := NULL;
               END;
            END;
         END IF;
         IF (V_IDOBRTPOPPED = 'S') THEN
            V_CDTIPOOPER := C_ITEM.CDTIPOOPER;
         ELSE
            V_CDTIPOOPER := P_CDTIPOOPER;
         END IF;
         IF (V_CDTIPOOPER IS NOT NULL) THEN
            BEGIN
               SELECT NVL(IDORIGDESTOP, 'C'), NVL(IDATUESTTPOP, 'S')
                 INTO V_IDDESTINO,  V_IDATUESTTPOP
                 FROM TIPOOPERAC
                WHERE IDENTRSAIDOP = 'S'
                  AND CDTIPOOPER = V_CDTIPOOPER;
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
                  V_IDDESTINO := 'C';
                  V_IDATUESTTPOP := 'S';
            END;
            IF (V_IDDESTINO = 'L') THEN
               V_CDDESTINO := C_ITEM.CDFILIALPED;
            END IF;
         END IF;
         IF (V_IDAGRUPAEXP = 'C') THEN
            BEGIN
               SELECT NVL(IDGERANFPEDI, '2')
                 INTO V_IDGERANFPEDI
                 FROM CLIENTE
                WHERE CDCLIENTE = C_ITEM.CDCLIENTE;
            EXCEPTION
               WHEN NO_DATA_FOUND THEN BEGIN
                  V_IDGERANFPEDI := '2';
               END;
            END;
            IF (V_IDGERANFPEDI = '1') THEN
               V_CDQUEBRANF := C_ITEM.NRPEDIDO;
            ELSE
               V_CDQUEBRANF := C_ITEM.CDCLIENTE;
            END IF;
         ELSE
            IF (C_ITEM.CDFILIALPED IS NOT NULL) THEN
               V_CDQUEBRANF := C_ITEM.CDFILIALPED;
            ELSE
               BEGIN
                  SELECT NVL(IDGERANFPEDI, '2')
                    INTO V_IDGERANFPEDI
                    FROM CLIENTE
                   WHERE CDCLIENTE = C_ITEM.CDCLIENTE;
               EXCEPTION
                  WHEN NO_DATA_FOUND THEN BEGIN
                     V_IDGERANFPEDI := '2';
                  END;
               END;
               IF (V_IDGERANFPEDI = '1') THEN
                  V_CDQUEBRANF := C_ITEM.NRPEDIDO;
               ELSE
                  V_CDQUEBRANF := C_ITEM.CDCLIENTE;
               END IF;
            END IF;
         END IF;
         V_QTMAXEXP := C_ITEM.QTITEMEXP;
         IF (V_IDPERADIEXP = 'S') THEN
            V_QTEXPEDIRMAX := (C_ITEM.QTITEMPED - C_ITEM.QTEXPITEMPED) * C_ITEM.VRFATOCONV;
            V_QTMAXEXP := ROUND(V_QTEXPEDIRMAX + ((V_QTEXPEDIRMAX*V_VRPEADIEXP)/100),5);
         END IF;
         IF (V_IDGERPRODEST = '1') THEN
            V_CDPRODMOVI := C_ITEM.CDPRODUTO;
            V_CDPRODESTO := C_ITEM.CDPRODESTO;
            V_CDPRODEMIS := C_ITEM.CDPRODEMIS;
            V_DSPRODEMIS := C_ITEM.DSPRODEMIS;
            V_VRFATOCONV := C_ITEM.VRFATOCONV;
            V_QTITEMPROD := C_ITEM.QTITEMEXP;
            V_VRPESOLIQEXP   := V_QTITEMPROD * C_ITEM.VRPESOLIQUPR;
            V_VRPESOBRUTOEXP := V_QTITEMPROD * C_ITEM.VRPESOBRUTPR;
            V_VRPRECO := C_ITEM.VRPREUNITPED;
            V_VRCUSTO := NULL;
            V_VRFATPRECO := NULL;
            V_IDITEMCOMPPROD := 'N';
            IF ((V_IDTABPRECO = '2') AND (V_IDCALCPRECFAT = 'E')) THEN
               V_VRCUSTO := (PEDCUST_COMP_PROD(C_ITEM.CDPRODUTO, C_ITEM.CDFILIAL, V_IDCUSEXP, 1, '', SYSDATE, C_ITEM.CDFATPRECO, C_ITEM.NRPEDIDO, C_ITEM.NRITEMPED) * V_VRFATOCONV);
               BEGIN
                  SELECT NVL(VRFATPRECO, 0)
                    INTO V_VRFATPRECO
                    FROM PRODFATOR
                   WHERE CDFATPRECO = C_ITEM.CDFATPRECO
                     AND CDPRODUTO  = C_ITEM.CDPRODUTO;
               EXCEPTION
                  WHEN NO_DATA_FOUND THEN
                     V_VRFATPRECO := 0;
               END;
               V_VRPRECO := V_VRCUSTO * V_VRFATPRECO;
            END IF;
            IF (C_ITEM.VRDESITPED > 0) THEN
               V_QTTOTAITEM := (C_ITEM.QTEXPITEMPED + C_ITEM.QTITEMEXP);
               IF V_QTTOTAITEM > C_ITEM.QTITEMPED THEN
                  V_QTTOTAITEM := C_ITEM.QTITEMEXP - (V_QTTOTAITEM - C_ITEM.QTITEMPED);
               ELSE
                  V_QTTOTAITEM := C_ITEM.QTITEMEXP;
               END IF;
               IF V_QTTOTAITEM = 0 THEN
                  V_QTTOTAITEM := 1;
               END IF;
               V_VRDESCEXP := C_ITEM.VRDESITPED / (C_ITEM.QTITEMPED/V_QTTOTAITEM);
            ELSE
               V_VRDESCEXP := 0;
            END IF;
         ELSE
            IF (V_IDGERPRODEST = '2') THEN
               V_CDPRODESTO := C_ITEM.CDPRODESTO;
               V_CDPRODMOVI := V_CDPRODESTO;
               V_CDPRODEMIS := C_ITEM.CDPRODEMIS;
               V_DSPRODEMIS := C_ITEM.DSPRODEMIS;
               V_VRFATOCONV := C_ITEM.VRFATOCONV;
               V_QTITEMPROD := C_ITEM.QTITEMEXP * V_VRFATOCONV;
               BEGIN
                  SELECT NVL(VRPESOLIQUPR, 0), NVL(VRPESOBRUTPR, 0)
                    INTO V_VRPESOLIQUPR, V_VRPESOBRUTPR
                    FROM PRODUTO
                   WHERE CDPRODUTO = V_CDPRODMOVI;
               EXCEPTION
                  WHEN NO_DATA_FOUND THEN BEGIN
                     V_VRPESOLIQUPR := 0;
                     V_VRPESOBRUTPR :=0;
                  END;
               END;
               V_VRPESOLIQEXP   := V_QTITEMPROD * V_VRPESOLIQUPR;
               V_VRPESOBRUTOEXP := V_QTITEMPROD * V_VRPESOBRUTPR;
               V_VRPRECO := C_ITEM.VRPREUNITPED/V_VRFATOCONV;
               V_VRCUSTO := NULL;
               V_VRFATPRECO := NULL;
               IF ((V_IDTABPRECO = '2') AND (V_IDCALCPRECFAT = 'E')) THEN
                  V_VRCUSTO := (PEDCUST_COMP_PROD(C_ITEM.CDPRODUTO, C_ITEM.CDFILIAL, V_IDCUSEXP, 1, '', SYSDATE, C_ITEM.CDFATPRECO, C_ITEM.NRPEDIDO, C_ITEM.NRITEMPED) * V_VRFATOCONV);
                  BEGIN
                     SELECT NVL(VRFATPRECO, 0)
                       INTO V_VRFATPRECO
                       FROM PRODFATOR
                      WHERE CDFATPRECO = C_ITEM.CDFATPRECO
                        AND CDPRODUTO  = C_ITEM.CDPRODUTO;
                  EXCEPTION
                     WHEN NO_DATA_FOUND THEN
                        V_VRFATPRECO := 0;
                  END;
                  V_VRPRECO := V_VRCUSTO * V_VRFATPRECO;
               END IF;
               IF (C_ITEM.VRDESITPED > 0) THEN
                  V_QTTOTAITEM := (C_ITEM.QTEXPITEMPED + C_ITEM.QTITEMEXP);
                  IF V_QTTOTAITEM > C_ITEM.QTITEMPED THEN
                     V_QTTOTAITEM := C_ITEM.QTITEMEXP - (V_QTTOTAITEM - C_ITEM.QTITEMPED);
                  ELSE
                     V_QTTOTAITEM := C_ITEM.QTITEMEXP;
                  END IF;
                  IF V_QTTOTAITEM = 0 THEN
                     V_QTTOTAITEM := 1;
                  END IF;
                  V_VRDESCEXP := C_ITEM.VRDESITPED / (C_ITEM.QTITEMPED/V_QTTOTAITEM);
               ELSE
                  V_VRDESCEXP := 0;
               END IF;
            ELSE
               IF (V_IDGERPRODEST = '3') THEN
                  V_IDITEMCOMPPROD := 'N';
                  IF (V_IDTPCOMPPROD = 'V') THEN
                     FOR C_COMPPEDIDO IN (SELECT R.CDPRODUTO, R.CDPRODCOMPED, R.QTITCOMPPROD, PR.CDARVPROD,
                                                 PR.CDPRODESTO, NVL(PR.VRFATOCONV,1) VRFATOCONV,
                                                 PR.NMPRODUTO, NVL(R.VRPREUNICOMP, 0) VRPREUNICOMP
                                            FROM COMPITEMPED R JOIN PRODUTO PR
                                                                 ON PR.CDPRODUTO = R.CDPRODCOMPED
                                           WHERE R.CDFILIAL  = C_ITEM.CDFILIAL
                                             AND R.NRPEDIDO  = C_ITEM.NRPEDIDO
                                             AND R.NRITEMPED = C_ITEM.NRITEMPED)
                     LOOP
                        V_EXISTECOMPOSICAO := 1;
                        V_IDITEMCOMPPROD := 'C';
                        V_CDPRODMOVI := C_COMPPEDIDO.CDPRODCOMPED;
                        V_CDPRODESTO := C_COMPPEDIDO.CDPRODESTO;
                        V_CDPRODEMIS := C_COMPPEDIDO.CDARVPROD;
                        V_DSPRODEMIS := C_COMPPEDIDO.NMPRODUTO;
                        V_VRFATOCONV := C_COMPPEDIDO.VRFATOCONV;
                        V_QTITEMPROD := C_COMPPEDIDO.QTITCOMPPROD * C_ITEM.QTITEMEXP;
                        BEGIN
                           SELECT NVL(VRPESOLIQUPR, 0), NVL(VRPESOBRUTPR, 0)
                             INTO V_VRPESOLIQUPR, V_VRPESOBRUTPR
                             FROM PRODUTO
                            WHERE CDPRODUTO = V_CDPRODMOVI;
                        EXCEPTION
                           WHEN NO_DATA_FOUND THEN BEGIN
                              V_VRPESOLIQUPR := 0;
                              V_VRPESOBRUTPR :=0;
                           END;
                        END;
                        V_VRPESOLIQEXP   := V_QTITEMPROD * V_VRPESOLIQUPR;
                        V_VRPESOBRUTOEXP := V_QTITEMPROD * V_VRPESOBRUTPR;
                        V_VRPRECO    := C_ITEM.VRPREUNITPED;
                        V_VRTOTAITEM := C_ITEM.QTITEMEXP + V_VRPRECO;
                        V_VRCUSTO    := NULL;
                        V_VRFATPRECO := NULL;
                        IF (V_IDTABPRECO = '1') THEN
                           IF (C_COMPPEDIDO.VRPREUNICOMP > 0) THEN
                              V_VRPRECO := C_COMPPEDIDO.VRPREUNICOMP;
                           END IF;
                        ELSE
                           BEGIN
                              SELECT NVL(VRFATPRECO, 0)
                                INTO V_VRFATPRECO
                                FROM PRODFATOR
                               WHERE CDFATPRECO = C_ITEM.CDFATPRECO
                                 AND CDPRODUTO  = V_CDPRODMOVI;
                           EXCEPTION
                              WHEN NO_DATA_FOUND THEN
                                 V_VRFATPRECO := 0;
                           END;
                           IF (V_IDTABPRECO = '2') THEN
                              V_VRCUSTO := PEDCUST_COMP_PROD(V_CDPRODMOVI, C_ITEM.CDFILIAL, V_IDCUSEXP, 1, '', SYSDATE, C_ITEM.CDFATPRECO, C_ITEM.NRPEDIDO, C_ITEM.NRITEMPED);
                              V_VRPRECO := V_VRCUSTO * V_VRFATPRECO;
                           END IF;
                        END IF;
                        IF (C_ITEM.VRDESITPED > 0) THEN
                           V_QTTOTAITEM := (C_ITEM.QTEXPITEMPED + C_ITEM.QTITEMEXP);
                           IF V_QTTOTAITEM > C_ITEM.QTITEMPED THEN
                              V_QTTOTAITEM := C_ITEM.QTITEMEXP - (V_QTTOTAITEM - C_ITEM.QTITEMPED);
                           ELSE
                              V_QTTOTAITEM := C_ITEM.QTITEMEXP;
                           END IF;
                           IF V_QTTOTAITEM = 0 THEN
                              V_QTTOTAITEM := 1;
                           END IF;
                           V_VRDESCEXP := C_ITEM.VRDESITPED / (C_ITEM.QTITEMPED/V_QTTOTAITEM);
                           V_VRDESCEXP := (V_VRDESCEXP * V_QTITEMPROD) /V_QTTOTAITEM;
                        ELSE
                           V_VRDESCEXP := 0;
                        END IF;
                        BEGIN
                           SELECT NVL(IDCNTRESTOQ,'S'),   CDALMOXPRCLI,   CDLOCALPRCLI, NVL(IDUTCTRLLOTE, 'S')
                             INTO V_IDCNTRESTOQ, V_CDALMOXPRCLI, V_CDLOCALPRCLI, V_IDUTCTRLLOTE
                             FROM PRODFILI
                            WHERE CDFILIAL  = C_ITEM.CDFILIAL
                              AND CDPRODUTO = V_CDPRODESTO;
                        EXCEPTION
                           WHEN NO_DATA_FOUND THEN BEGIN
                              V_IDCNTRESTOQ  := 'S';
                              V_CDALMOXPRCLI := NULL;
                              V_CDLOCALPRCLI := NULL;
                              V_IDUTCTRLLOTE := 'S';
                           END;
                        END;
                        IF (V_IDCTRLESTQ = 'S') AND (V_IDATUESTTPOP = 'S') THEN
                           IF (V_IDCNTRESTOQ = 'S') THEN
                              IF (V_IDUTILALMOX = 'S') THEN
                                 V_CDALMOXEXP := V_CDALMOXPRCLI;
                                 IF (V_CDALMOXEXP IS NULL) THEN
                                    V_CDALMOXEXP := V_CDALMOXARIFE;
                                 END IF;
                              ELSE
                                 V_CDALMOXEXP := ' ';
                              END IF;
                              IF (V_IDUTILLCESTQ = 'S') THEN
                                 V_CDLOCALEXP := V_CDLOCALPRCLI;
                                 IF (V_CDLOCALEXP IS NULL) THEN
                                    V_CDLOCALEXP := V_CDLOCALESTOQ;
                                 END IF;
                              ELSE
                                 V_CDLOCALEXP := ' ';
                              END IF;
                           END IF;
                        END IF;
                        SELECT NVL(MAX(NRSEQITEMEXP),0)
                          INTO V_NRSEQ
                          FROM ITEMEXPEDICAO
                         WHERE CDFILIAL = P_CDFILIAL
                           AND NREXPEDICAO = P_NREXPEDICAO
                           AND NRPEDIDO = C_ITEM.NRPEDIDO
                           AND NRITEMPED = C_ITEM.NRITEMPED;
                        V_NRSEQ := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_NRSEQ) + 1)),10,0);
                        INSERT INTO ITEMEXPEDICAO
                        (CDFILIAL, NREXPEDICAO, NRPEDIDO, CDTIPOOPER, IDATUESTTPOP, IDDESTINO, CDFILIALPED, CDCLIENTE, CDDESTINO, DTVALIPED,
                         IDTPIJURTRAN, NRINSJURTRAN, CDTIPTRANS, IDPGFRETTRAN, NRPLACAVEPED,  SGUFVEICPED,
                         CDEMPRESA,   CDINSCESTA,   CDINSCMUNI,   CDSERIE, IDPEDINDUSTRIA, CDVENDEDOR,
                         CDMODPGTO , IDTPFORPGTPED , NRDIAPGFIXPED, NRPARCPGTOPED, IDUTDIAPAGPED, IDAGRUPAEXP, IDGERANFPEDI, CDQUEBRANF,
                         NRITEMPED, CDPRODUTO, CDPRODMOVI, VRFATOCONV, IDCNTRESTOQ, IDSITITEMPED, CDPRODEMIS, DSPRODEMIS, VRPESOLIQ, VRPESOBRUTO,
                         QTITEMEXP, QTMAXEXP, QTITEMPROD, QTITEMPED, QTEXPEDIDA, IDUTILALMOX, CDALMOXEXP, IDUTILLCESTQ, CDLOCALEXP,
                         VRPRECOEXP, VRCUSTO, VRFATPRECO, VRDESITEXP, IDITEMPEDTERC, IDUTCTRLLOTE,
                         IDRESERVA, QTRESERVADA, NRLOTEEXP, NRSUBLOTEEXP, NRSEQITEMEXP, IDERRO, IDBLOQUEIAPED, IDITEMCOMPPROD)
                        VALUES
                        (C_ITEM.CDFILIAL, P_NREXPEDICAO, C_ITEM.NRPEDIDO, V_CDTIPOOPER, V_IDATUESTTPOP, V_IDDESTINO, C_ITEM.CDFILIALPED, C_ITEM.CDCLIENTE, V_CDDESTINO,C_ITEM.DTVALIPED,
                         C_ITEM.IDTPIJURTRAN, C_ITEM.NRINSJURTRAN, C_ITEM.CDTIPTRANS, C_ITEM.IDPGFRETTRAN, C_ITEM.NRPLACAVEPED,  C_ITEM.SGUFVEICPED,
                         V_CDEMPRESA, V_CDINSCESTA, V_CDINSCMUNI, V_CDSERIE, C_ITEM.IDPEDINDUSTRIA, C_ITEM.CDVENDEDOR,
                         C_ITEM.CDMODPGTO , C_ITEM.IDTPFORPGTPED , C_ITEM.NRDIAPGFIXPED, C_ITEM.NRPARCPGTOPED, C_ITEM.IDUTDIAPAGPED, V_IDAGRUPAEXP, V_IDGERANFPEDI, V_CDQUEBRANF,
                         C_ITEM.NRITEMPED,  C_ITEM.CDPRODUTO, V_CDPRODMOVI, V_VRFATOCONV, V_IDCNTRESTOQ, C_ITEM.IDSITITEMPED, V_CDPRODEMIS, V_DSPRODEMIS, V_VRPESOLIQEXP, V_VRPESOBRUTOEXP,
                         C_ITEM.QTITEMEXP, V_QTMAXEXP, V_QTITEMPROD, C_ITEM.QTITEMPED, C_ITEM.QTEXPITEMPED, V_IDUTILALMOX, V_CDALMOXEXP, V_IDUTILLCESTQ, V_CDLOCALEXP,
                         V_VRPRECO, V_VRCUSTO, V_VRFATPRECO, V_VRDESCEXP, C_ITEM.IDITEMPEDTERC, V_IDUTCTRLLOTE,
                         'N', 0, NULL, NULL, V_NRSEQ, 'N', 'N', V_IDITEMCOMPPROD);
                     END LOOP;
                  ELSE
                     IF ((V_IDTPCOMPPROD = 'F') OR (V_EXISTECOMPOSICAO = 0)) THEN
                        FOR C_ROTFABLOCAL IN (SELECT R.CDPRODUTO, R.CDSUBPRODLOC, R.QTSUBPRODLOC, PR.CDARVPROD, PR.NMPRODUTO,
                                                     PR.CDPRODESTO,  NVL(PR.VRFATOCONV,1) VRFATOCONV
                                                FROM ROTFABLOCAL R JOIN PRODUTO PR
                                                                     ON R.CDSUBPRODLOC = PR.CDPRODUTO
                                               WHERE (R.CDFILIAL     = C_ITEM.CDFILIAL)
                                                 AND (R.CDPRODUTO    = C_ITEM.CDPRODUTO)
                                                 AND ((TRUNC(SYSDATE) >= NVL(TRUNC(R.DTINIVGPRODLOC),TRUNC(SYSDATE))) AND (TRUNC(SYSDATE) <= NVL(TRUNC(R.DTFINVGPRODLOC),TRUNC(SYSDATE)))))
                        LOOP
                           V_EXISTECOMPOSICAO := 1;
                           V_IDITEMCOMPPROD := 'C';
                           V_CDPRODMOVI := C_ROTFABLOCAL.CDSUBPRODLOC;
                           V_CDPRODESTO := C_ROTFABLOCAL.CDPRODESTO;
                           V_CDPRODEMIS := C_ROTFABLOCAL.CDARVPROD;
                           V_DSPRODEMIS := C_ROTFABLOCAL.NMPRODUTO;
                           V_VRFATOCONV := C_ROTFABLOCAL.VRFATOCONV;
                           V_QTITEMPROD := C_ROTFABLOCAL.QTSUBPRODLOC * C_ITEM.QTITEMEXP;
                           BEGIN
                              SELECT NVL(VRPESOLIQUPR, 0), NVL(VRPESOBRUTPR, 0)
                                INTO V_VRPESOLIQUPR, V_VRPESOBRUTPR
                                FROM PRODUTO
                               WHERE CDPRODUTO = V_CDPRODMOVI;
                           EXCEPTION
                              WHEN NO_DATA_FOUND THEN BEGIN
                                 V_VRPESOLIQUPR := 0;
                                 V_VRPESOBRUTPR :=0;
                              END;
                           END;
                           V_VRPESOLIQEXP   := V_QTITEMPROD * V_VRPESOLIQUPR;
                           V_VRPESOBRUTOEXP := V_QTITEMPROD * V_VRPESOBRUTPR;
                           V_VRPRECO    := C_ITEM.VRPREUNITPED;
                           V_VRTOTAITEM := C_ITEM.QTITEMEXP + V_VRPRECO;
                           V_VRCUSTO    := NULL;
                           V_VRFATPRECO := NULL;
                           IF (V_IDTABPRECO = '1') THEN
                              V_VRPRECO := BUSCA_PRECO_PED(C_ITEM.CDFILIAL, C_ROTFABLOCAL.CDARVPROD, C_ITEM.CDCLIENTE, C_ITEM.CDPRECOBASE, C_ITEM.DTENTPED, C_ITEM.CDMODPGTO);
                           ELSE
                              BEGIN
                                 SELECT NVL(VRFATPRECO, 0)
                                   INTO V_VRFATPRECO
                                   FROM PRODFATOR
                                  WHERE CDFATPRECO = C_ITEM.CDFATPRECO
                                    AND CDPRODUTO  = V_CDPRODMOVI;
                              EXCEPTION
                                 WHEN NO_DATA_FOUND THEN
                                    V_VRFATPRECO := 0;
                              END;
                              V_VRCUSTO := PEDCUST_COMP_PROD(V_CDPRODMOVI, C_ITEM.CDFILIAL, V_IDCUSEXP, 1, '', SYSDATE, C_ITEM.CDFATPRECO, C_ITEM.NRPEDIDO, C_ITEM.NRITEMPED);
                              V_VRPRECO := V_VRCUSTO * V_VRFATPRECO;
                           END IF;
                           IF (C_ITEM.VRDESITPED > 0) THEN
                              V_QTTOTAITEM := (C_ITEM.QTEXPITEMPED + C_ITEM.QTITEMEXP);
                              IF V_QTTOTAITEM > C_ITEM.QTITEMPED THEN
                                 V_QTTOTAITEM := C_ITEM.QTITEMEXP - (V_QTTOTAITEM - C_ITEM.QTITEMPED);
                              ELSE
                                 V_QTTOTAITEM := C_ITEM.QTITEMEXP;
                              END IF;
                              IF V_QTTOTAITEM = 0 THEN
                                 V_QTTOTAITEM := 1;
                              END IF;
                              V_VRDESCEXP := C_ITEM.VRDESITPED / (C_ITEM.QTITEMPED/V_QTTOTAITEM);
                              V_VRDESCEXP := (V_VRDESCEXP * V_QTITEMPROD) /V_QTTOTAITEM;
                           ELSE
                              V_VRDESCEXP := 0;
                           END IF;
                           BEGIN
                              SELECT NVL(IDCNTRESTOQ,'S'),   CDALMOXPRCLI,   CDLOCALPRCLI, NVL(IDUTCTRLLOTE, 'S')
                                INTO V_IDCNTRESTOQ, V_CDALMOXPRCLI, V_CDLOCALPRCLI, V_IDUTCTRLLOTE
                                FROM PRODFILI
                               WHERE CDFILIAL  = C_ITEM.CDFILIAL
                                 AND CDPRODUTO = V_CDPRODESTO;
                           EXCEPTION
                              WHEN NO_DATA_FOUND THEN BEGIN
                                 V_IDCNTRESTOQ  := 'S';
                                 V_CDALMOXPRCLI := NULL;
                                 V_CDLOCALPRCLI := NULL;
                                 V_IDUTCTRLLOTE := 'S';
                              END;
                           END;
                           IF (V_IDCTRLESTQ = 'S') AND (V_IDATUESTTPOP = 'S') THEN
                              IF (V_IDCNTRESTOQ = 'S') THEN
                                 IF (V_IDUTILALMOX = 'S') THEN
                                    V_CDALMOXEXP := V_CDALMOXPRCLI;
                                    IF (V_CDALMOXEXP IS NULL) THEN
                                       V_CDALMOXEXP := V_CDALMOXARIFE;
                                    END IF;
                                 ELSE
                                    V_CDALMOXEXP := ' ';
                                 END IF;
                                 IF (V_IDUTILLCESTQ = 'S') THEN
                                    V_CDLOCALEXP := V_CDLOCALPRCLI;
                                    IF (V_CDLOCALEXP IS NULL) THEN
                                       V_CDLOCALEXP := V_CDLOCALESTOQ;
                                    END IF;
                                 ELSE
                                    V_CDLOCALEXP := ' ';
                                 END IF;
                              END IF;
                           END IF;
                           SELECT NVL(MAX(NRSEQITEMEXP),0)
                             INTO V_NRSEQ
                             FROM ITEMEXPEDICAO
                            WHERE CDFILIAL = P_CDFILIAL
                              AND NREXPEDICAO = P_NREXPEDICAO
                              AND NRPEDIDO = C_ITEM.NRPEDIDO
                              AND NRITEMPED = C_ITEM.NRITEMPED;
                           V_NRSEQ := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_NRSEQ) + 1)),10,0);
                           INSERT INTO ITEMEXPEDICAO
                           (CDFILIAL, NREXPEDICAO, NRPEDIDO, CDTIPOOPER, IDATUESTTPOP, IDDESTINO, CDFILIALPED, CDCLIENTE, CDDESTINO, DTVALIPED,
                            IDTPIJURTRAN, NRINSJURTRAN, CDTIPTRANS, IDPGFRETTRAN, NRPLACAVEPED,  SGUFVEICPED,
                            CDEMPRESA,   CDINSCESTA,   CDINSCMUNI,   CDSERIE, IDPEDINDUSTRIA, CDVENDEDOR,
                            CDMODPGTO , IDTPFORPGTPED , NRDIAPGFIXPED, NRPARCPGTOPED, IDUTDIAPAGPED, IDAGRUPAEXP, IDGERANFPEDI, CDQUEBRANF,
                            NRITEMPED, CDPRODUTO, CDPRODMOVI, VRFATOCONV, IDCNTRESTOQ, IDSITITEMPED, CDPRODEMIS, DSPRODEMIS, VRPESOLIQ, VRPESOBRUTO,
                            QTITEMEXP, QTMAXEXP, QTITEMPROD, QTITEMPED, QTEXPEDIDA, IDUTILALMOX, CDALMOXEXP, IDUTILLCESTQ, CDLOCALEXP,
                            VRPRECOEXP, VRCUSTO, VRFATPRECO, VRDESITEXP, IDITEMPEDTERC, IDUTCTRLLOTE,
                            IDRESERVA, QTRESERVADA, NRLOTEEXP, NRSUBLOTEEXP, NRSEQITEMEXP, IDERRO, IDBLOQUEIAPED, IDITEMCOMPPROD)
                           VALUES
                           (C_ITEM.CDFILIAL, P_NREXPEDICAO, C_ITEM.NRPEDIDO, V_CDTIPOOPER, V_IDATUESTTPOP, V_IDDESTINO, C_ITEM.CDFILIALPED, C_ITEM.CDCLIENTE, V_CDDESTINO,C_ITEM.DTVALIPED,
                            C_ITEM.IDTPIJURTRAN, C_ITEM.NRINSJURTRAN, C_ITEM.CDTIPTRANS, C_ITEM.IDPGFRETTRAN, C_ITEM.NRPLACAVEPED,  C_ITEM.SGUFVEICPED,
                            V_CDEMPRESA, V_CDINSCESTA, V_CDINSCMUNI, V_CDSERIE, C_ITEM.IDPEDINDUSTRIA, C_ITEM.CDVENDEDOR,
                            C_ITEM.CDMODPGTO , C_ITEM.IDTPFORPGTPED , C_ITEM.NRDIAPGFIXPED, C_ITEM.NRPARCPGTOPED, C_ITEM.IDUTDIAPAGPED, V_IDAGRUPAEXP, V_IDGERANFPEDI, V_CDQUEBRANF,
                            C_ITEM.NRITEMPED,  C_ITEM.CDPRODUTO, V_CDPRODMOVI, V_VRFATOCONV, V_IDCNTRESTOQ, C_ITEM.IDSITITEMPED, V_CDPRODEMIS, V_DSPRODEMIS, V_VRPESOLIQEXP, V_VRPESOBRUTOEXP,
                            C_ITEM.QTITEMEXP, V_QTMAXEXP, V_QTITEMPROD, C_ITEM.QTITEMPED, C_ITEM.QTEXPITEMPED, V_IDUTILALMOX, V_CDALMOXEXP, V_IDUTILLCESTQ, V_CDLOCALEXP,
                            V_VRPRECO, V_VRCUSTO, V_VRFATPRECO, V_VRDESCEXP, C_ITEM.IDITEMPEDTERC, V_IDUTCTRLLOTE,
                            'N', 0, NULL, NULL, V_NRSEQ, 'N', 'N', V_IDITEMCOMPPROD);
                        END LOOP;
                        IF (V_EXISTECOMPOSICAO = 0) THEN
                           FOR C_ROTFABRICA IN (SELECT R.CDPRODUTO, R.CDSUBPRODUTO, R.QTSUBPRODROT, PR.CDARVPROD, PR.NMPRODUTO,
                                                       PR.CDPRODESTO, NVL(PR.VRFATOCONV,1) VRFATOCONV
                                                  FROM ROTFABRICA R JOIN PRODUTO PR
                                                                      ON R.CDSUBPRODUTO = PR.CDPRODUTO
                                                 WHERE (R.CDPRODUTO    = C_ITEM.CDPRODUTO)
                                                   AND ((TRUNC(SYSDATE) >= NVL(TRUNC(R.DTINIVGPRODROT),TRUNC(SYSDATE))) AND (TRUNC(SYSDATE) <= NVL(TRUNC(R.DTFINVGPRODROT),TRUNC(SYSDATE)))))
                           LOOP
                              V_EXISTECOMPOSICAO := 1;
                              V_IDITEMCOMPPROD := 'C';
                              V_CDPRODMOVI := C_ROTFABRICA.CDSUBPRODUTO;
                              V_CDPRODESTO := C_ROTFABRICA.CDPRODESTO;
                              V_CDPRODEMIS := C_ROTFABRICA.CDARVPROD;
                              V_DSPRODEMIS := C_ROTFABRICA.NMPRODUTO;
                              V_VRFATOCONV := C_ROTFABRICA.VRFATOCONV;
                              V_QTITEMPROD := C_ROTFABRICA.QTSUBPRODROT * C_ITEM.QTITEMEXP;
                              BEGIN
                                 SELECT NVL(VRPESOLIQUPR, 0), NVL(VRPESOBRUTPR, 0)
                                   INTO V_VRPESOLIQUPR, V_VRPESOBRUTPR
                                   FROM PRODUTO
                                  WHERE CDPRODUTO = V_CDPRODMOVI;
                              EXCEPTION
                                 WHEN NO_DATA_FOUND THEN BEGIN
                                    V_VRPESOLIQUPR := 0;
                                    V_VRPESOBRUTPR :=0;
                                 END;
                              END;
                              V_VRPESOLIQEXP   := V_QTITEMPROD * V_VRPESOLIQUPR;
                              V_VRPESOBRUTOEXP := V_QTITEMPROD * V_VRPESOBRUTPR;
                              V_VRPRECO    := C_ITEM.VRPREUNITPED;
                              V_VRTOTAITEM := C_ITEM.QTITEMEXP + V_VRPRECO;
                              V_VRCUSTO    := NULL;
                              V_VRFATPRECO := NULL;
                              IF (V_IDTABPRECO = '1') THEN
                                 V_VRPRECO := BUSCA_PRECO_PED(C_ITEM.CDFILIAL, C_ROTFABRICA.CDARVPROD, C_ITEM.CDCLIENTE, C_ITEM.CDPRECOBASE, C_ITEM.DTENTPED, C_ITEM.CDMODPGTO);
                              ELSE
                                 BEGIN
                                    SELECT NVL(VRFATPRECO, 0)
                                      INTO V_VRFATPRECO
                                      FROM PRODFATOR
                                     WHERE CDFATPRECO = C_ITEM.CDFATPRECO
                                       AND CDPRODUTO  = V_CDPRODMOVI;
                                 EXCEPTION
                                    WHEN NO_DATA_FOUND THEN
                                       V_VRFATPRECO := 0;
                                 END;
                                 V_VRCUSTO := PEDCUST_COMP_PROD(V_CDPRODMOVI, C_ITEM.CDFILIAL, V_IDCUSEXP, 1, '', SYSDATE, C_ITEM.CDFATPRECO, C_ITEM.NRPEDIDO, C_ITEM.NRITEMPED);
                                 V_VRPRECO := V_VRCUSTO * V_VRFATPRECO;
                              END IF;
                              IF (C_ITEM.VRDESITPED > 0) THEN
                                 V_QTTOTAITEM := (C_ITEM.QTEXPITEMPED + C_ITEM.QTITEMEXP);
                                 IF V_QTTOTAITEM > C_ITEM.QTITEMPED THEN
                                    V_QTTOTAITEM := C_ITEM.QTITEMEXP - (V_QTTOTAITEM - C_ITEM.QTITEMPED);
                                 ELSE
                                    V_QTTOTAITEM := C_ITEM.QTITEMEXP;
                                 END IF;
                                 IF V_QTTOTAITEM = 0 THEN
                                    V_QTTOTAITEM := 1;
                                 END IF;
                                 V_VRDESCEXP := C_ITEM.VRDESITPED / (C_ITEM.QTITEMPED/V_QTTOTAITEM);
                                 V_VRDESCEXP := (V_VRDESCEXP * V_QTITEMPROD) /V_QTTOTAITEM;
                              ELSE
                                 V_VRDESCEXP := 0;
                              END IF;
                              BEGIN
                                 SELECT NVL(IDCNTRESTOQ,'S'),   CDALMOXPRCLI,   CDLOCALPRCLI, NVL(IDUTCTRLLOTE, 'S')
                                   INTO V_IDCNTRESTOQ, V_CDALMOXPRCLI, V_CDLOCALPRCLI, V_IDUTCTRLLOTE
                                   FROM PRODFILI
                                  WHERE CDFILIAL  = C_ITEM.CDFILIAL
                                    AND CDPRODUTO = V_CDPRODESTO;
                              EXCEPTION
                                 WHEN NO_DATA_FOUND THEN BEGIN
                                    V_IDCNTRESTOQ  := 'S';
                                    V_CDALMOXPRCLI := NULL;
                                    V_CDLOCALPRCLI := NULL;
                                    V_IDUTCTRLLOTE := 'S';
                                 END;
                              END;
                              IF (V_IDCTRLESTQ = 'S') AND (V_IDATUESTTPOP = 'S') THEN
                                 IF (V_IDCNTRESTOQ = 'S') THEN
                                    IF (V_IDUTILALMOX = 'S') THEN
                                       V_CDALMOXEXP := V_CDALMOXPRCLI;
                                       IF (V_CDALMOXEXP IS NULL) THEN
                                          V_CDALMOXEXP := V_CDALMOXARIFE;
                                       END IF;
                                    ELSE
                                       V_CDALMOXEXP := ' ';
                                    END IF;
                                    IF (V_IDUTILLCESTQ = 'S') THEN
                                       V_CDLOCALEXP := V_CDLOCALPRCLI;
                                       IF (V_CDLOCALEXP IS NULL) THEN
                                          V_CDLOCALEXP := V_CDLOCALESTOQ;
                                       END IF;
                                    ELSE
                                       V_CDLOCALEXP := ' ';
                                    END IF;
                                 END IF;
                              END IF;
                              SELECT NVL(MAX(NRSEQITEMEXP),0)
                                INTO V_NRSEQ
                                FROM ITEMEXPEDICAO
                               WHERE CDFILIAL = P_CDFILIAL
                                 AND NREXPEDICAO = P_NREXPEDICAO
                                 AND NRPEDIDO = C_ITEM.NRPEDIDO
                                 AND NRITEMPED = C_ITEM.NRITEMPED;
                              V_NRSEQ := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_NRSEQ) + 1)),10,0);
                              INSERT INTO ITEMEXPEDICAO
                              (CDFILIAL, NREXPEDICAO, NRPEDIDO, CDTIPOOPER, IDATUESTTPOP, IDDESTINO, CDFILIALPED, CDCLIENTE, CDDESTINO, DTVALIPED,
                               IDTPIJURTRAN, NRINSJURTRAN, CDTIPTRANS, IDPGFRETTRAN, NRPLACAVEPED,  SGUFVEICPED,
                               CDEMPRESA,   CDINSCESTA,   CDINSCMUNI,   CDSERIE, IDPEDINDUSTRIA, CDVENDEDOR,
                               CDMODPGTO , IDTPFORPGTPED , NRDIAPGFIXPED, NRPARCPGTOPED, IDUTDIAPAGPED, IDAGRUPAEXP, IDGERANFPEDI, CDQUEBRANF,
                               NRITEMPED, CDPRODUTO, CDPRODMOVI, VRFATOCONV, IDCNTRESTOQ, IDSITITEMPED, CDPRODEMIS, DSPRODEMIS, VRPESOLIQ, VRPESOBRUTO,
                               QTITEMEXP, QTMAXEXP, QTITEMPROD, QTITEMPED, QTEXPEDIDA, IDUTILALMOX, CDALMOXEXP, IDUTILLCESTQ, CDLOCALEXP,
                               VRPRECOEXP, VRCUSTO, VRFATPRECO, VRDESITEXP, IDITEMPEDTERC, IDUTCTRLLOTE,
                               IDRESERVA, QTRESERVADA, NRLOTEEXP, NRSUBLOTEEXP, NRSEQITEMEXP, IDERRO, IDBLOQUEIAPED, IDITEMCOMPPROD)
                              VALUES
                              (C_ITEM.CDFILIAL, P_NREXPEDICAO, C_ITEM.NRPEDIDO, V_CDTIPOOPER, V_IDATUESTTPOP, V_IDDESTINO, C_ITEM.CDFILIALPED, C_ITEM.CDCLIENTE, V_CDDESTINO,C_ITEM.DTVALIPED,
                               C_ITEM.IDTPIJURTRAN, C_ITEM.NRINSJURTRAN, C_ITEM.CDTIPTRANS, C_ITEM.IDPGFRETTRAN, C_ITEM.NRPLACAVEPED,  C_ITEM.SGUFVEICPED,
                               V_CDEMPRESA, V_CDINSCESTA, V_CDINSCMUNI, V_CDSERIE, C_ITEM.IDPEDINDUSTRIA, C_ITEM.CDVENDEDOR,
                               C_ITEM.CDMODPGTO , C_ITEM.IDTPFORPGTPED , C_ITEM.NRDIAPGFIXPED, C_ITEM.NRPARCPGTOPED, C_ITEM.IDUTDIAPAGPED, V_IDAGRUPAEXP, V_IDGERANFPEDI, V_CDQUEBRANF,
                               C_ITEM.NRITEMPED,  C_ITEM.CDPRODUTO, V_CDPRODMOVI, V_VRFATOCONV, V_IDCNTRESTOQ, C_ITEM.IDSITITEMPED, V_CDPRODEMIS, V_DSPRODEMIS, V_VRPESOLIQEXP, V_VRPESOBRUTOEXP,
                               C_ITEM.QTITEMEXP, V_QTMAXEXP, V_QTITEMPROD, C_ITEM.QTITEMPED, C_ITEM.QTEXPITEMPED, V_IDUTILALMOX, V_CDALMOXEXP, V_IDUTILLCESTQ, V_CDLOCALEXP,
                               V_VRPRECO, V_VRCUSTO, V_VRFATPRECO, V_VRDESCEXP, C_ITEM.IDITEMPEDTERC, V_IDUTCTRLLOTE,
                               'N', 0, NULL, NULL, V_NRSEQ, 'N', 'N', V_IDITEMCOMPPROD);
                           END LOOP;
                        END IF;
                     END IF;
                  END IF;
               END IF;
            END IF;
         END IF;
         V_EXISTERESERVA := 0;
         IF (V_IDGERPRODEST <> '3') THEN
            V_IDITEMCOMPPROD := 'N';
            INSERE_COMITEMEXP(C_ITEM.CDFILIAL, P_NREXPEDICAO, C_ITEM.NRPEDIDO, C_ITEM.NRITEMPED, C_ITEM.CDPRODUTO, V_QTITEMPROD, V_IDITEMCOMPPROD);
            SELECT COUNT(1)
              INTO V_EXISTERESERVA
              FROM RESERPRODPED
             WHERE CDFILIAL = C_ITEM.CDFILIAL
               AND NRPEDIDO = C_ITEM.NRPEDIDO
               AND NRITEMPED = C_ITEM.NRITEMPED;
            IF (V_EXISTERESERVA >= 1)THEN
               FOR C_RESERVA IN (SELECT NVL(CDALMOXARIFE, ' ') CDALMOXARIFE, NVL(CDLOCALESTOQ, ' ') CDLOCALESTOQ, NRLOTEESTQ, NRSUBLOTE, QTRESPRODPED
                                   FROM RESERPRODPED
                                  WHERE CDFILIAL = C_ITEM.CDFILIAL
                                    AND NRPEDIDO = C_ITEM.NRPEDIDO
                                    AND NRITEMPED = C_ITEM.NRITEMPED)
               LOOP
                  BEGIN
                     SELECT NVL(IDCNTRESTOQ,'S'),   CDALMOXPRCLI,   CDLOCALPRCLI, NVL(IDUTCTRLLOTE, 'S')
                       INTO V_IDCNTRESTOQ, V_CDALMOXPRCLI, V_CDLOCALPRCLI, V_IDUTCTRLLOTE
                       FROM PRODFILI
                      WHERE CDFILIAL  = C_ITEM.CDFILIAL
                        AND CDPRODUTO = V_CDPRODESTO;
                  EXCEPTION
                     WHEN NO_DATA_FOUND THEN BEGIN
                        V_IDCNTRESTOQ  := 'S';
                        V_CDALMOXPRCLI := NULL;
                        V_CDLOCALPRCLI := NULL;
                        V_IDUTCTRLLOTE := 'S';
                     END;
                  END;
                  IF (V_IDGERPRODEST = '2') THEN
                     V_QTITEMPROD := C_RESERVA.QTRESPRODPED;
                     V_VRPRECO    := V_VRPRECO * C_ITEM.VRFATOCONV;
                  ELSE
                     V_QTITEMPROD := C_RESERVA.QTRESPRODPED/C_ITEM.VRFATOCONV;
                  END IF;
                  V_VRPESOLIQEXP   := V_QTITEMPROD * C_ITEM.VRPESOLIQUPR;
                  V_VRPESOBRUTOEXP := V_QTITEMPROD * C_ITEM.VRPESOBRUTPR;
                  SELECT NVL(MAX(NRSEQITEMEXP),0)
                    INTO V_NRSEQ
                    FROM ITEMEXPEDICAO
                   WHERE CDFILIAL = P_CDFILIAL
                     AND NREXPEDICAO = P_NREXPEDICAO
                     AND NRPEDIDO = C_ITEM.NRPEDIDO
                     AND NRITEMPED = C_ITEM.NRITEMPED;
                  V_NRSEQ := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_NRSEQ) + 1)),10,0);
                  INSERT INTO ITEMEXPEDICAO
                  (CDFILIAL, NREXPEDICAO, NRPEDIDO, CDTIPOOPER, IDATUESTTPOP, IDDESTINO, CDFILIALPED, CDCLIENTE, CDDESTINO, DTVALIPED,
                   IDTPIJURTRAN, NRINSJURTRAN, CDTIPTRANS, IDPGFRETTRAN, NRPLACAVEPED,  SGUFVEICPED,
                   CDEMPRESA,   CDINSCESTA,   CDINSCMUNI,   CDSERIE, IDPEDINDUSTRIA, CDVENDEDOR,
                   CDMODPGTO , IDTPFORPGTPED , NRDIAPGFIXPED, NRPARCPGTOPED, IDUTDIAPAGPED, IDAGRUPAEXP, IDGERANFPEDI, CDQUEBRANF,
                   NRITEMPED, CDPRODUTO, CDPRODMOVI, VRFATOCONV, IDCNTRESTOQ, IDSITITEMPED, CDPRODEMIS, DSPRODEMIS, VRPESOLIQ, VRPESOBRUTO,
                   QTITEMEXP, QTMAXEXP, QTITEMPROD, QTITEMPED, QTEXPEDIDA, IDUTILALMOX, CDALMOXEXP, IDUTILLCESTQ, CDLOCALEXP,
                   VRPRECOEXP, VRCUSTO, VRFATPRECO, VRDESITEXP, IDITEMPEDTERC, IDUTCTRLLOTE,
                   IDRESERVA, QTRESERVADA, NRLOTEEXP, NRSUBLOTEEXP, NRSEQITEMEXP, IDERRO, IDBLOQUEIAPED, IDITEMCOMPPROD)
                  VALUES
                  (C_ITEM.CDFILIAL, P_NREXPEDICAO, C_ITEM.NRPEDIDO, V_CDTIPOOPER, V_IDATUESTTPOP, V_IDDESTINO, C_ITEM.CDFILIALPED, C_ITEM.CDCLIENTE, V_CDDESTINO,C_ITEM.DTVALIPED,
                   C_ITEM.IDTPIJURTRAN, C_ITEM.NRINSJURTRAN, C_ITEM.CDTIPTRANS, C_ITEM.IDPGFRETTRAN, C_ITEM.NRPLACAVEPED,  C_ITEM.SGUFVEICPED,
                   V_CDEMPRESA, V_CDINSCESTA, V_CDINSCMUNI, V_CDSERIE, C_ITEM.IDPEDINDUSTRIA, C_ITEM.CDVENDEDOR,
                   C_ITEM.CDMODPGTO , C_ITEM.IDTPFORPGTPED , C_ITEM.NRDIAPGFIXPED, C_ITEM.NRPARCPGTOPED, C_ITEM.IDUTDIAPAGPED, V_IDAGRUPAEXP, V_IDGERANFPEDI, V_CDQUEBRANF,
                   C_ITEM.NRITEMPED,  C_ITEM.CDPRODUTO, V_CDPRODMOVI, V_VRFATOCONV, V_IDCNTRESTOQ, C_ITEM.IDSITITEMPED, V_CDPRODEMIS, V_DSPRODEMIS, V_VRPESOLIQEXP, V_VRPESOBRUTOEXP,
                   C_ITEM.QTITEMEXP, V_QTMAXEXP, V_QTITEMPROD, C_ITEM.QTITEMPED, C_ITEM.QTEXPITEMPED, V_IDUTILALMOX, C_RESERVA.CDALMOXARIFE, V_IDUTILLCESTQ, C_RESERVA.CDLOCALESTOQ,
                   V_VRPRECO, V_VRCUSTO, V_VRFATPRECO, V_VRDESCEXP, C_ITEM.IDITEMPEDTERC, V_IDUTCTRLLOTE,
                   'S', C_RESERVA.QTRESPRODPED, C_RESERVA.NRLOTEESTQ, C_RESERVA.NRSUBLOTE, V_NRSEQ, 'N', 'N', V_IDITEMCOMPPROD);
               END LOOP;
            ELSE
               BEGIN
                  SELECT NVL(IDCNTRESTOQ,'S'),   CDALMOXPRCLI,   CDLOCALPRCLI, NVL(IDUTCTRLLOTE, 'S')
                    INTO V_IDCNTRESTOQ, V_CDALMOXPRCLI, V_CDLOCALPRCLI, V_IDUTCTRLLOTE
                    FROM PRODFILI
                   WHERE CDFILIAL  = C_ITEM.CDFILIAL
                     AND CDPRODUTO = V_CDPRODESTO;
               EXCEPTION
                  WHEN NO_DATA_FOUND THEN BEGIN
                     V_IDCNTRESTOQ  := 'S';
                     V_CDALMOXPRCLI := NULL;
                     V_CDLOCALPRCLI := NULL;
                     V_IDUTCTRLLOTE := 'S';
                  END;
               END;
               IF (V_IDCTRLESTQ = 'S') AND (V_IDATUESTTPOP = 'S') THEN
                  IF (V_IDCNTRESTOQ = 'S') THEN
                     IF (V_IDUTILALMOX = 'S') THEN
                        V_CDALMOXEXP := V_CDALMOXPRCLI;
                        IF (V_CDALMOXEXP IS NULL) THEN
                           V_CDALMOXEXP := V_CDALMOXARIFE;
                        END IF;
                     ELSE
                        V_CDALMOXEXP := ' ';
                     END IF;
                     IF (V_IDUTILLCESTQ = 'S') THEN
                        V_CDLOCALEXP := V_CDLOCALPRCLI;
                        IF (V_CDLOCALEXP IS NULL) THEN
                           V_CDLOCALEXP := V_CDLOCALESTOQ;
                        END IF;
                     ELSE
                        V_CDLOCALEXP := ' ';
                     END IF;
                  END IF;
               END IF;
               SELECT NVL(MAX(NRSEQITEMEXP),0)
                 INTO V_NRSEQ
                 FROM ITEMEXPEDICAO
                WHERE CDFILIAL = P_CDFILIAL
                  AND NREXPEDICAO = P_NREXPEDICAO
                  AND NRPEDIDO = C_ITEM.NRPEDIDO
                  AND NRITEMPED = C_ITEM.NRITEMPED;
               V_NRSEQ := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_NRSEQ) + 1)),10,0);
               INSERT INTO ITEMEXPEDICAO
               (CDFILIAL, NREXPEDICAO, NRPEDIDO, CDTIPOOPER, IDATUESTTPOP, IDDESTINO, CDFILIALPED, CDCLIENTE, CDDESTINO, DTVALIPED,
                IDTPIJURTRAN, NRINSJURTRAN, CDTIPTRANS, IDPGFRETTRAN, NRPLACAVEPED,  SGUFVEICPED,
                CDEMPRESA,   CDINSCESTA,   CDINSCMUNI,   CDSERIE, IDPEDINDUSTRIA, CDVENDEDOR,
                CDMODPGTO , IDTPFORPGTPED , NRDIAPGFIXPED, NRPARCPGTOPED, IDUTDIAPAGPED, IDAGRUPAEXP, IDGERANFPEDI, CDQUEBRANF,
                NRITEMPED, CDPRODUTO, CDPRODMOVI, VRFATOCONV, IDCNTRESTOQ, IDSITITEMPED, CDPRODEMIS, DSPRODEMIS, VRPESOLIQ, VRPESOBRUTO,
                QTITEMEXP, QTMAXEXP, QTITEMPROD, QTITEMPED, QTEXPEDIDA, IDUTILALMOX, CDALMOXEXP, IDUTILLCESTQ, CDLOCALEXP,
                VRPRECOEXP, VRCUSTO, VRFATPRECO, VRDESITEXP, IDITEMPEDTERC, IDUTCTRLLOTE,
                IDRESERVA, QTRESERVADA, NRLOTEEXP, NRSUBLOTEEXP, NRSEQITEMEXP, IDERRO, IDBLOQUEIAPED, IDITEMCOMPPROD)
               VALUES
               (C_ITEM.CDFILIAL, P_NREXPEDICAO, C_ITEM.NRPEDIDO, V_CDTIPOOPER, V_IDATUESTTPOP, V_IDDESTINO, C_ITEM.CDFILIALPED, C_ITEM.CDCLIENTE, V_CDDESTINO,C_ITEM.DTVALIPED,
                C_ITEM.IDTPIJURTRAN, C_ITEM.NRINSJURTRAN, C_ITEM.CDTIPTRANS, C_ITEM.IDPGFRETTRAN, C_ITEM.NRPLACAVEPED,  C_ITEM.SGUFVEICPED,
                V_CDEMPRESA, V_CDINSCESTA, V_CDINSCMUNI, V_CDSERIE, C_ITEM.IDPEDINDUSTRIA, C_ITEM.CDVENDEDOR,
                C_ITEM.CDMODPGTO , C_ITEM.IDTPFORPGTPED , C_ITEM.NRDIAPGFIXPED, C_ITEM.NRPARCPGTOPED, C_ITEM.IDUTDIAPAGPED, V_IDAGRUPAEXP, V_IDGERANFPEDI, V_CDQUEBRANF,
                C_ITEM.NRITEMPED,  C_ITEM.CDPRODUTO, V_CDPRODMOVI, V_VRFATOCONV, V_IDCNTRESTOQ, C_ITEM.IDSITITEMPED, V_CDPRODEMIS, V_DSPRODEMIS, V_VRPESOLIQEXP, V_VRPESOBRUTOEXP,
                C_ITEM.QTITEMEXP, V_QTMAXEXP, V_QTITEMPROD, C_ITEM.QTITEMPED, C_ITEM.QTEXPITEMPED, V_IDUTILALMOX, V_CDALMOXEXP, V_IDUTILLCESTQ, V_CDLOCALEXP,
                V_VRPRECO, V_VRCUSTO, V_VRFATPRECO, V_VRDESCEXP, C_ITEM.IDITEMPEDTERC, V_IDUTCTRLLOTE,
                'N', 0, NULL, NULL, V_NRSEQ, 'N', 'N', V_IDITEMCOMPPROD);
            END IF;
         ELSE
            IF (V_EXISTECOMPOSICAO = 0) THEN
               V_CDPRODMOVI := C_ITEM.CDPRODUTO;
               V_CDPRODESTO := C_ITEM.CDPRODESTO;
               V_CDPRODEMIS := C_ITEM.CDPRODEMIS;
               V_DSPRODEMIS := C_ITEM.DSPRODEMIS;
               V_VRFATOCONV := C_ITEM.VRFATOCONV;
               V_QTITEMPROD := C_ITEM.QTITEMEXP;
               V_VRPESOLIQEXP   := V_QTITEMPROD * C_ITEM.VRPESOLIQUPR;
               V_VRPESOBRUTOEXP := V_QTITEMPROD * C_ITEM.VRPESOBRUTPR;
               V_VRPRECO := C_ITEM.VRPREUNITPED;
               V_VRCUSTO := NULL;
               V_VRFATPRECO := NULL;
               V_IDITEMCOMPPROD := 'N';
               IF ((V_IDTABPRECO = '2') AND (V_IDCALCPRECFAT = 'E')) THEN
                  V_VRCUSTO := (PEDCUST_COMP_PROD(C_ITEM.CDPRODUTO, C_ITEM.CDFILIAL, V_IDCUSEXP, 1, '', SYSDATE, C_ITEM.CDFATPRECO, C_ITEM.NRPEDIDO, C_ITEM.NRITEMPED) * V_VRFATOCONV);
                  BEGIN
                     SELECT NVL(VRFATPRECO, 0)
                       INTO V_VRFATPRECO
                       FROM PRODFATOR
                      WHERE CDFATPRECO = C_ITEM.CDFATPRECO
                        AND CDPRODUTO  = C_ITEM.CDPRODUTO;
                  EXCEPTION
                     WHEN NO_DATA_FOUND THEN
                        V_VRFATPRECO := 0;
                  END;
                  V_VRPRECO := V_VRCUSTO * V_VRFATPRECO;
               END IF;
               IF (C_ITEM.VRDESITPED > 0) THEN
                  V_QTTOTAITEM := (C_ITEM.QTEXPITEMPED + C_ITEM.QTITEMEXP);
                  IF V_QTTOTAITEM > C_ITEM.QTITEMPED THEN
                     V_QTTOTAITEM := C_ITEM.QTITEMEXP - (V_QTTOTAITEM - C_ITEM.QTITEMPED);
                  ELSE
                     V_QTTOTAITEM := C_ITEM.QTITEMEXP;
                  END IF;
                  IF V_QTTOTAITEM = 0 THEN
                     V_QTTOTAITEM := 1;
                  END IF;
                  V_VRDESCEXP := C_ITEM.VRDESITPED / (C_ITEM.QTITEMPED/V_QTTOTAITEM);
               ELSE
                  V_VRDESCEXP := 0;
               END IF;
               BEGIN
                  SELECT NVL(IDCNTRESTOQ,'S'),   CDALMOXPRCLI,   CDLOCALPRCLI, NVL(IDUTCTRLLOTE, 'S')
                    INTO V_IDCNTRESTOQ, V_CDALMOXPRCLI, V_CDLOCALPRCLI, V_IDUTCTRLLOTE
                    FROM PRODFILI
                   WHERE CDFILIAL  = C_ITEM.CDFILIAL
                     AND CDPRODUTO = V_CDPRODESTO;
               EXCEPTION
                  WHEN NO_DATA_FOUND THEN BEGIN
                     V_IDCNTRESTOQ  := 'S';
                     V_CDALMOXPRCLI := NULL;
                     V_CDLOCALPRCLI := NULL;
                     V_IDUTCTRLLOTE := 'S';
                  END;
               END;
               IF (V_IDCTRLESTQ = 'S') AND (V_IDATUESTTPOP = 'S') THEN
                  IF (V_IDCNTRESTOQ = 'S') THEN
                     IF (V_IDUTILALMOX = 'S') THEN
                        V_CDALMOXEXP := V_CDALMOXPRCLI;
                        IF (V_CDALMOXEXP IS NULL) THEN
                           V_CDALMOXEXP := V_CDALMOXARIFE;
                        END IF;
                     ELSE
                        V_CDALMOXEXP := ' ';
                     END IF;
                     IF (V_IDUTILLCESTQ = 'S') THEN
                        V_CDLOCALEXP := V_CDLOCALPRCLI;
                        IF (V_CDLOCALEXP IS NULL) THEN
                           V_CDLOCALEXP := V_CDLOCALESTOQ;
                        END IF;
                     ELSE
                        V_CDLOCALEXP := ' ';
                     END IF;
                  END IF;
               END IF;
               SELECT NVL(MAX(NRSEQITEMEXP),0)
                 INTO V_NRSEQ
                 FROM ITEMEXPEDICAO
                WHERE CDFILIAL = P_CDFILIAL
                  AND NREXPEDICAO = P_NREXPEDICAO
                  AND NRPEDIDO = C_ITEM.NRPEDIDO
                  AND NRITEMPED = C_ITEM.NRITEMPED;
               V_NRSEQ := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_NRSEQ) + 1)),10,0);
               INSERT INTO ITEMEXPEDICAO
               (CDFILIAL, NREXPEDICAO, NRPEDIDO, CDTIPOOPER, IDATUESTTPOP, IDDESTINO, CDFILIALPED, CDCLIENTE, CDDESTINO, DTVALIPED,
                IDTPIJURTRAN, NRINSJURTRAN, CDTIPTRANS, IDPGFRETTRAN, NRPLACAVEPED,  SGUFVEICPED,
                CDEMPRESA,   CDINSCESTA,   CDINSCMUNI,   CDSERIE, IDPEDINDUSTRIA, CDVENDEDOR,
                CDMODPGTO , IDTPFORPGTPED , NRDIAPGFIXPED, NRPARCPGTOPED, IDUTDIAPAGPED, IDAGRUPAEXP, IDGERANFPEDI, CDQUEBRANF,
                NRITEMPED, CDPRODUTO, CDPRODMOVI, VRFATOCONV, IDCNTRESTOQ, IDSITITEMPED, CDPRODEMIS, DSPRODEMIS, VRPESOLIQ, VRPESOBRUTO,
                QTITEMEXP, QTMAXEXP, QTITEMPROD, QTITEMPED, QTEXPEDIDA, IDUTILALMOX, CDALMOXEXP, IDUTILLCESTQ, CDLOCALEXP,
                VRPRECOEXP, VRCUSTO, VRFATPRECO, VRDESITEXP, IDITEMPEDTERC, IDUTCTRLLOTE,
                IDRESERVA, QTRESERVADA, NRLOTEEXP, NRSUBLOTEEXP, NRSEQITEMEXP, IDERRO, IDBLOQUEIAPED, IDITEMCOMPPROD)
               VALUES
               (C_ITEM.CDFILIAL, P_NREXPEDICAO, C_ITEM.NRPEDIDO, V_CDTIPOOPER, V_IDATUESTTPOP, V_IDDESTINO, C_ITEM.CDFILIALPED, C_ITEM.CDCLIENTE, V_CDDESTINO,C_ITEM.DTVALIPED,
                C_ITEM.IDTPIJURTRAN, C_ITEM.NRINSJURTRAN, C_ITEM.CDTIPTRANS, C_ITEM.IDPGFRETTRAN, C_ITEM.NRPLACAVEPED,  C_ITEM.SGUFVEICPED,
                V_CDEMPRESA, V_CDINSCESTA, V_CDINSCMUNI, V_CDSERIE, C_ITEM.IDPEDINDUSTRIA, C_ITEM.CDVENDEDOR,
                C_ITEM.CDMODPGTO , C_ITEM.IDTPFORPGTPED , C_ITEM.NRDIAPGFIXPED, C_ITEM.NRPARCPGTOPED, C_ITEM.IDUTDIAPAGPED, V_IDAGRUPAEXP, V_IDGERANFPEDI, V_CDQUEBRANF,
                C_ITEM.NRITEMPED,  C_ITEM.CDPRODUTO, V_CDPRODMOVI, V_VRFATOCONV, V_IDCNTRESTOQ, C_ITEM.IDSITITEMPED, V_CDPRODEMIS, V_DSPRODEMIS, V_VRPESOLIQEXP, V_VRPESOBRUTOEXP,
                C_ITEM.QTITEMEXP, V_QTMAXEXP, V_QTITEMPROD, C_ITEM.QTITEMPED, C_ITEM.QTEXPITEMPED, V_IDUTILALMOX, V_CDALMOXEXP, V_IDUTILLCESTQ, V_CDLOCALEXP,
                V_VRPRECO, V_VRCUSTO, V_VRFATPRECO, V_VRDESCEXP, C_ITEM.IDITEMPEDTERC, V_IDUTCTRLLOTE,
                'N', 0, NULL, NULL, V_NRSEQ, 'N', 'N', V_IDITEMCOMPPROD);
            END IF;
         END IF;
      END LOOP;
   END LOOP;
/*VALIDA ITENS SELECIONADOS*/
   /*------------------FILIAL_N_PARAM--------------*/
   IF (V_PAREXP = 0) THEN
      UPDATE ITEMEXPEDICAO
         SET IDERRO = 'S'
       WHERE CDFILIAL = P_CDFILIAL
         AND NREXPEDICAO = P_NREXPEDICAO;
      FOR C_ERRO IN (SELECT DISTINCT CDFILIAL, NREXPEDICAO, NRPEDIDO, CDCLIENTE, CDFILIALPED,
                            CDEMPRESA, CDINSCESTA, CDINSCMUNI, CDSERIE
                         FROM ITEMEXPEDICAO
                      WHERE CDFILIAL = P_CDFILIAL
                        AND NREXPEDICAO = P_NREXPEDICAO)
      LOOP
         SELECT NVL(MAX(NRSEQERROITEM),0)
           INTO V_NRSEQ
           FROM ERROITEMEXP
          WHERE CDFILIAL = C_ERRO.CDFILIAL
            AND NREXPEDICAO = C_ERRO.NREXPEDICAO
            AND NRPEDIDO = C_ERRO.NRPEDIDO;
         V_NRSEQ := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_NRSEQ) + 1)),10,0);
         V_ERRO := 0;
         SELECT COUNT(1)
           INTO V_ERRO
           FROM ERROITEMEXP
          WHERE CDFILIAL = C_ERRO.CDFILIAL
            AND NREXPEDICAO = C_ERRO.NREXPEDICAO
            AND NRPEDIDO = C_ERRO.NRPEDIDO
            AND DSERRO = 'FILIAL_N_PARAM';
         IF (V_ERRO = 0) THEN
            INSERT INTO ERROITEMEXP
            (CDFILIAL, NREXPEDICAO, NRPEDIDO, NRITEMPED, NRSEQERROITEM, DSERRO, CDCLIENTE, CDFILIALPED, CDPRODUTO,CDPRODMOVI, QTITEMEXP, QTTOTAITEMPROD, QTTOTAESTOQ,
             CDEMPRESA, CDINSCESTA, CDINSCMUNI, CDSERIE, NRORDPROD, NRSUBORDPROD)
            VALUES
            (C_ERRO.CDFILIAL, C_ERRO.NREXPEDICAO, C_ERRO.NRPEDIDO, '', V_NRSEQ, 'FILIAL_N_PARAM', C_ERRO.NRPEDIDO, C_ERRO.CDFILIALPED, '', '', 0, 0, 0,
             C_ERRO.CDEMPRESA, C_ERRO.CDINSCESTA, C_ERRO.CDINSCMUNI, C_ERRO.CDSERIE, '', '');
         END IF;
      END LOOP;
   END IF;
   /*------------------FILIAL_N_PARAM--------------*/
   /*------------------TPOPER_N_INF--------------*/
   IF (V_IDOBRTPOPPED = 'S') THEN
      FOR C_ERRO IN (SELECT DISTINCT CDFILIAL, NREXPEDICAO, NRPEDIDO, CDCLIENTE, CDFILIALPED, CDEMPRESA, CDINSCESTA, CDINSCMUNI, CDSERIE
                       FROM ITEMEXPEDICAO
                      WHERE CDFILIAL = P_CDFILIAL
                        AND NREXPEDICAO = P_NREXPEDICAO
                        AND CDTIPOOPER IS NULL)
      LOOP
         UPDATE ITEMEXPEDICAO
            SET IDERRO = 'S'
          WHERE CDFILIAL = C_ERRO.CDFILIAL
            AND NREXPEDICAO = C_ERRO.NREXPEDICAO
            AND NRPEDIDO = C_ERRO.NRPEDIDO;
         SELECT NVL(MAX(NRSEQERROITEM),0)
           INTO V_NRSEQ
           FROM ERROITEMEXP
          WHERE CDFILIAL = C_ERRO.CDFILIAL
            AND NREXPEDICAO = C_ERRO.NREXPEDICAO
            AND NRPEDIDO = C_ERRO.NRPEDIDO;
         V_NRSEQ := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_NRSEQ) + 1)),10,0);
         V_ERRO := 0;
         SELECT COUNT(1)
           INTO V_ERRO
           FROM ERROITEMEXP
          WHERE CDFILIAL = C_ERRO.CDFILIAL
            AND NREXPEDICAO = C_ERRO.NREXPEDICAO
            AND NRPEDIDO = C_ERRO.NRPEDIDO
            AND DSERRO = 'TPOPER_N_INF';
         IF (V_ERRO = 0) THEN
            INSERT INTO ERROITEMEXP
            (CDFILIAL, NREXPEDICAO, NRPEDIDO, NRITEMPED, NRSEQERROITEM, DSERRO, CDCLIENTE, CDFILIALPED, CDPRODUTO, CDPRODMOVI, QTITEMEXP, QTTOTAITEMPROD, QTTOTAESTOQ,
             CDEMPRESA, CDINSCESTA, CDINSCMUNI, CDSERIE, NRORDPROD, NRSUBORDPROD)
            VALUES
            (C_ERRO.CDFILIAL, C_ERRO.NREXPEDICAO, C_ERRO.NRPEDIDO, '', V_NRSEQ, 'TPOPER_N_INF', C_ERRO.CDCLIENTE, C_ERRO.CDFILIALPED, '', '', 0, 0, 0,
             C_ERRO.CDEMPRESA, C_ERRO.CDINSCESTA, C_ERRO.CDINSCMUNI, C_ERRO.CDSERIE, '', '');
         END IF;
      END LOOP;
   END IF;
   /*------------------TPOPER_N_INF--------------*/
   /*------------------PEDNFILDEST--------------*/
   FOR C_ERRO IN (SELECT DISTINCT CDFILIAL, NREXPEDICAO, NRPEDIDO, CDCLIENTE, CDFILIALPED,
                                  CDEMPRESA, CDINSCESTA, CDINSCMUNI, CDSERIE
                    FROM ITEMEXPEDICAO
                   WHERE CDFILIAL = P_CDFILIAL
                     AND NREXPEDICAO = P_NREXPEDICAO
                     AND IDDESTINO = 'L'
                     AND CDFILIALPED IS NULL)
   LOOP
      UPDATE ITEMEXPEDICAO
         SET IDERRO = 'S'
       WHERE CDFILIAL = C_ERRO.CDFILIAL
         AND NREXPEDICAO = C_ERRO.NREXPEDICAO
         AND NRPEDIDO = C_ERRO.NRPEDIDO;
      SELECT NVL(MAX(NRSEQERROITEM),0)
        INTO V_NRSEQ
        FROM ERROITEMEXP
       WHERE CDFILIAL = C_ERRO.CDFILIAL
         AND NREXPEDICAO = C_ERRO.NREXPEDICAO
         AND NRPEDIDO = C_ERRO.NRPEDIDO;
      V_NRSEQ := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_NRSEQ) + 1)),10,0);
      V_ERRO := 0;
      SELECT COUNT(1)
        INTO V_ERRO
        FROM ERROITEMEXP
       WHERE CDFILIAL = C_ERRO.CDFILIAL
         AND NREXPEDICAO = C_ERRO.NREXPEDICAO
         AND NRPEDIDO = C_ERRO.NRPEDIDO
         AND DSERRO = 'PEDNFILDEST';
      IF (V_ERRO = 0) THEN
         INSERT INTO ERROITEMEXP
         (CDFILIAL, NREXPEDICAO, NRPEDIDO, NRITEMPED, NRSEQERROITEM, DSERRO, CDCLIENTE, CDFILIALPED, CDPRODUTO, CDPRODMOVI, QTITEMEXP, QTTOTAITEMPROD, QTTOTAESTOQ,
          CDEMPRESA, CDINSCESTA, CDINSCMUNI, CDSERIE, NRORDPROD, NRSUBORDPROD)
         VALUES
         (C_ERRO.CDFILIAL, C_ERRO.NREXPEDICAO, C_ERRO.NRPEDIDO, '', V_NRSEQ, 'PEDNFILDEST', C_ERRO.CDCLIENTE, C_ERRO.CDFILIALPED, '', '', 0, 0, 0,
          C_ERRO.CDEMPRESA, C_ERRO.CDINSCESTA, C_ERRO.CDINSCMUNI, C_ERRO.CDSERIE, '', '');
      END IF;
   END LOOP;
   /*------------------PEDNFILDEST--------------*/
   /*------------------FILIDEST_INATIVA--------------*/
   FOR C_ERRO IN (SELECT DISTINCT I.CDFILIAL, I.NREXPEDICAO, I.NRPEDIDO, I.CDCLIENTE ,I.CDFILIALPED,
                        I.CDEMPRESA, I.CDINSCESTA, I.CDINSCMUNI, I.CDSERIE
                       FROM ITEMEXPEDICAO I JOIN FILIAL F
                                              ON I.CDFILIALPED = F.CDFILIAL
                      WHERE I.CDFILIAL = P_CDFILIAL
                        AND I.NREXPEDICAO = P_NREXPEDICAO
                        AND F.IDCNTRRESC = 'S')
   LOOP
      UPDATE ITEMEXPEDICAO
         SET IDERRO = 'S'
       WHERE CDFILIAL = C_ERRO.CDFILIAL
         AND NREXPEDICAO = C_ERRO.NREXPEDICAO
         AND NRPEDIDO = C_ERRO.NRPEDIDO;
      SELECT NVL(MAX(NRSEQERROITEM),0)
        INTO V_NRSEQ
        FROM ERROITEMEXP
       WHERE CDFILIAL = C_ERRO.CDFILIAL
         AND NREXPEDICAO = C_ERRO.NREXPEDICAO
         AND NRPEDIDO = C_ERRO.NRPEDIDO;
      V_NRSEQ := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_NRSEQ) + 1)),10,0);
      V_ERRO := 0;
      SELECT COUNT(1)
        INTO V_ERRO
        FROM ERROITEMEXP
       WHERE CDFILIAL = C_ERRO.CDFILIAL
         AND NREXPEDICAO = C_ERRO.NREXPEDICAO
         AND NRPEDIDO = C_ERRO.NRPEDIDO
         AND DSERRO = 'FILIDEST_INATIVA';
      IF (V_ERRO = 0) THEN
         INSERT INTO ERROITEMEXP
         (CDFILIAL, NREXPEDICAO, NRPEDIDO, NRITEMPED, NRSEQERROITEM, DSERRO, CDCLIENTE, CDFILIALPED, CDPRODUTO, CDPRODMOVI, QTITEMEXP, QTTOTAITEMPROD, QTTOTAESTOQ,
          CDEMPRESA, CDINSCESTA, CDINSCMUNI, CDSERIE, NRORDPROD, NRSUBORDPROD)
         VALUES
         (C_ERRO.CDFILIAL, C_ERRO.NREXPEDICAO, C_ERRO.NRPEDIDO, '', V_NRSEQ, 'FILIDEST_INATIVA', C_ERRO.CDCLIENTE, C_ERRO.CDFILIALPED, '', '', 0, 0, 0,
          C_ERRO.CDEMPRESA, C_ERRO.CDINSCESTA, C_ERRO.CDINSCMUNI, C_ERRO.CDSERIE, '', '');
      END IF;
   END LOOP;
   /*------------------FILIDEST_INATIVA--------------*/
   /*------------------FRETE_NULO--------------*/
   FOR C_ERRO IN (SELECT DISTINCT CDFILIAL, NREXPEDICAO, NRPEDIDO, CDCLIENTE, CDFILIALPED,
                         CDEMPRESA, CDINSCESTA, CDINSCMUNI, CDSERIE
                           FROM ITEMEXPEDICAO
                          WHERE CDFILIAL = P_CDFILIAL
                            AND NREXPEDICAO = P_NREXPEDICAO
                            AND IDPGFRETTRAN IS NULL)
   LOOP
      UPDATE ITEMEXPEDICAO
         SET IDERRO = 'S'
       WHERE CDFILIAL = C_ERRO.CDFILIAL
         AND NREXPEDICAO = C_ERRO.NREXPEDICAO
         AND NRPEDIDO = C_ERRO.NRPEDIDO;
      SELECT NVL(MAX(NRSEQERROITEM),0)
        INTO V_NRSEQ
        FROM ERROITEMEXP
       WHERE CDFILIAL = C_ERRO.CDFILIAL
         AND NREXPEDICAO = C_ERRO.NREXPEDICAO
         AND NRPEDIDO = C_ERRO.NRPEDIDO;
      V_NRSEQ := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_NRSEQ) + 1)),10,0);
      V_ERRO := 0;
      SELECT COUNT(1)
        INTO V_ERRO
        FROM ERROITEMEXP
       WHERE CDFILIAL = C_ERRO.CDFILIAL
         AND NREXPEDICAO = C_ERRO.NREXPEDICAO
         AND NRPEDIDO = C_ERRO.NRPEDIDO
         AND DSERRO = 'FRETE_NULO';
      IF (V_ERRO = 0) THEN
         INSERT INTO ERROITEMEXP
         (CDFILIAL, NREXPEDICAO, NRPEDIDO, NRITEMPED, NRSEQERROITEM, DSERRO, CDCLIENTE, CDFILIALPED, CDPRODUTO, CDPRODMOVI, QTITEMEXP, QTTOTAITEMPROD, QTTOTAESTOQ,
          CDEMPRESA, CDINSCESTA, CDINSCMUNI, CDSERIE, NRORDPROD, NRSUBORDPROD)
         VALUES
         (C_ERRO.CDFILIAL, C_ERRO.NREXPEDICAO, C_ERRO.NRPEDIDO, '', V_NRSEQ, 'FRETE_NULO', C_ERRO.CDCLIENTE, C_ERRO.CDFILIALPED, '', '', 0, 0, 0,
          C_ERRO.CDEMPRESA, C_ERRO.CDINSCESTA, C_ERRO.CDINSCMUNI, C_ERRO.CDSERIE, '', '');
      END IF;
   END LOOP;
   /*------------------FRETE_NULO--------------*/
   /*------------------TRANSP_NULL--------------*/
   FOR C_ERRO IN (SELECT DISTINCT CDFILIAL, NREXPEDICAO, NRPEDIDO, CDCLIENTE, CDFILIALPED,
                                  CDEMPRESA, CDINSCESTA, CDINSCMUNI, CDSERIE
                    FROM ITEMEXPEDICAO
                   WHERE CDFILIAL = P_CDFILIAL
                     AND NREXPEDICAO = P_NREXPEDICAO
                     AND IDPGFRETTRAN NOT IN ('9', '3')
                     AND ((IDTPIJURTRAN IS NULL) OR (NRINSJURTRAN IS NULL)))
   LOOP
      UPDATE ITEMEXPEDICAO
         SET IDERRO = 'S'
       WHERE CDFILIAL = C_ERRO.CDFILIAL
         AND NREXPEDICAO = C_ERRO.NREXPEDICAO
         AND NRPEDIDO = C_ERRO.NRPEDIDO;
      SELECT NVL(MAX(NRSEQERROITEM),0)
        INTO V_NRSEQ
        FROM ERROITEMEXP
       WHERE CDFILIAL = C_ERRO.CDFILIAL
         AND NREXPEDICAO = C_ERRO.NREXPEDICAO
         AND NRPEDIDO = C_ERRO.NRPEDIDO;
      V_NRSEQ := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_NRSEQ) + 1)),10,0);
      V_ERRO := 0;
      SELECT COUNT(1)
        INTO V_ERRO
        FROM ERROITEMEXP
       WHERE CDFILIAL = C_ERRO.CDFILIAL
         AND NREXPEDICAO = C_ERRO.NREXPEDICAO
         AND NRPEDIDO = C_ERRO.NRPEDIDO
         AND DSERRO = 'TRANSP_NULL';
      IF (V_ERRO = 0) THEN
         INSERT INTO ERROITEMEXP
         (CDFILIAL, NREXPEDICAO, NRPEDIDO, NRITEMPED, NRSEQERROITEM, DSERRO, CDCLIENTE, CDFILIALPED, CDPRODUTO, CDPRODMOVI, QTITEMEXP, QTTOTAITEMPROD, QTTOTAESTOQ,
          CDEMPRESA, CDINSCESTA, CDINSCMUNI, CDSERIE, NRORDPROD, NRSUBORDPROD)
         VALUES
         (C_ERRO.CDFILIAL, C_ERRO.NREXPEDICAO, C_ERRO.NRPEDIDO, '', V_NRSEQ, 'TRANSP_NULL', C_ERRO.CDCLIENTE, C_ERRO.CDFILIALPED, '', '', 0, 0, 0,
          C_ERRO.CDEMPRESA, C_ERRO.CDINSCESTA, C_ERRO.CDINSCMUNI, C_ERRO.CDSERIE, '', '');
      END IF;
   END LOOP;
   /*------------------TRANSP_NULL--------------*/
   /*------------------VALIDACOES QUANDO CONTROLA A VALIDADE DO PEDIDO (INICIO)--------------*/
   IF (V_IDUTVALIPED = 'S') THEN
      /*------------------DTVAL_INF--------------*/
      FOR C_ERRO IN (SELECT DISTINCT CDFILIAL, NREXPEDICAO, NRPEDIDO, CDCLIENTE, CDFILIALPED,
                                     CDEMPRESA, CDINSCESTA, CDINSCMUNI, CDSERIE
                       FROM ITEMEXPEDICAO
                      WHERE CDFILIAL = P_CDFILIAL
                        AND NREXPEDICAO = P_NREXPEDICAO
                        AND (DTVALIPED IS NULL))
      LOOP
         UPDATE ITEMEXPEDICAO
            SET IDERRO = 'S'
          WHERE CDFILIAL = C_ERRO.CDFILIAL
            AND NREXPEDICAO = C_ERRO.NREXPEDICAO
            AND NRPEDIDO = C_ERRO.NRPEDIDO;
         SELECT NVL(MAX(NRSEQERROITEM),0)
           INTO V_NRSEQ
           FROM ERROITEMEXP
          WHERE CDFILIAL = C_ERRO.CDFILIAL
            AND NREXPEDICAO = C_ERRO.NREXPEDICAO
            AND NRPEDIDO = C_ERRO.NRPEDIDO;
         V_NRSEQ := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_NRSEQ) + 1)),10,0);
         V_ERRO := 0;
         SELECT COUNT(1)
           INTO V_ERRO
           FROM ERROITEMEXP
          WHERE CDFILIAL = C_ERRO.CDFILIAL
            AND NREXPEDICAO = C_ERRO.NREXPEDICAO
            AND NRPEDIDO = C_ERRO.NRPEDIDO
            AND DSERRO = 'DTVAL_INF';
         IF (V_ERRO = 0) THEN
            INSERT INTO ERROITEMEXP
            (CDFILIAL, NREXPEDICAO, NRPEDIDO, NRITEMPED, NRSEQERROITEM, DSERRO, CDCLIENTE, CDFILIALPED, CDPRODUTO, CDPRODMOVI, QTITEMEXP, QTTOTAITEMPROD, QTTOTAESTOQ,
             CDEMPRESA, CDINSCESTA, CDINSCMUNI, CDSERIE, NRORDPROD, NRSUBORDPROD)
            VALUES
            (C_ERRO.CDFILIAL, C_ERRO.NREXPEDICAO, C_ERRO.NRPEDIDO, '', V_NRSEQ, 'DTVAL_INF', C_ERRO.CDCLIENTE, C_ERRO.CDFILIALPED, '', '', 0, 0, 0,
             C_ERRO.CDEMPRESA, C_ERRO.CDINSCESTA, C_ERRO.CDINSCMUNI, C_ERRO.CDSERIE, '', '');
         END IF;
      END LOOP;
      /*------------------DTVAL_INF--------------*/
      /*------------------DTVAL_INV--------------*/
      FOR C_ERRO IN (SELECT DISTINCT CDFILIAL, NREXPEDICAO, NRPEDIDO, CDCLIENTE, CDFILIALPED,
                                     CDEMPRESA, CDINSCESTA, CDINSCMUNI, CDSERIE
                       FROM ITEMEXPEDICAO
                      WHERE CDFILIAL = P_CDFILIAL
                        AND NREXPEDICAO = P_NREXPEDICAO
                        AND (TRUNC(DTVALIPED) < TRUNC(SYSDATE)))
      LOOP
         UPDATE ITEMEXPEDICAO
            SET IDERRO = 'S'
          WHERE CDFILIAL = C_ERRO.CDFILIAL
            AND NREXPEDICAO = C_ERRO.NREXPEDICAO
            AND NRPEDIDO = C_ERRO.NRPEDIDO;
         SELECT NVL(MAX(NRSEQERROITEM),0)
           INTO V_NRSEQ
           FROM ERROITEMEXP
          WHERE CDFILIAL = C_ERRO.CDFILIAL
            AND NREXPEDICAO = C_ERRO.NREXPEDICAO
            AND NRPEDIDO = C_ERRO.NRPEDIDO;
         V_NRSEQ := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_NRSEQ) + 1)),10,0);
         V_ERRO := 0;
         SELECT COUNT(1)
           INTO V_ERRO
           FROM ERROITEMEXP
          WHERE CDFILIAL = C_ERRO.CDFILIAL
            AND NREXPEDICAO = C_ERRO.NREXPEDICAO
            AND NRPEDIDO = C_ERRO.NRPEDIDO
            AND DSERRO = 'DTVAL_INV';
         IF (V_ERRO = 0) THEN
            INSERT INTO ERROITEMEXP
            (CDFILIAL, NREXPEDICAO, NRPEDIDO, NRITEMPED, NRSEQERROITEM, DSERRO, CDCLIENTE, CDFILIALPED, CDPRODUTO, CDPRODMOVI, QTITEMEXP, QTTOTAITEMPROD, QTTOTAESTOQ,
             CDEMPRESA, CDINSCESTA, CDINSCMUNI, CDSERIE, NRORDPROD, NRSUBORDPROD)
            VALUES
            (C_ERRO.CDFILIAL, C_ERRO.NREXPEDICAO, C_ERRO.NRPEDIDO, '', V_NRSEQ, 'DTVAL_INV', C_ERRO.CDCLIENTE, C_ERRO.CDFILIALPED, '', '', 0, 0, 0,
             C_ERRO.CDEMPRESA, C_ERRO.CDINSCESTA, C_ERRO.CDINSCMUNI, C_ERRO.CDSERIE, '', '');
         END IF;
      END LOOP;
      /*------------------DTVAL_INF--------------*/
   END IF;
   /*------------------VALIDACOES QUANDO CONTROLA A VALIDADE DO PEDIDO (FIM)--------------*/
   /*------------------EMPRESA, INSR. ESTADUAL, INSCR. MUNICIPAL E S:param1RIE DEVEM SER INFORMADAS--------------*/
   FOR C_ERRO IN (SELECT DISTINCT CDFILIAL, NREXPEDICAO, NRPEDIDO, CDCLIENTE, CDFILIALPED,
                                  CDEMPRESA, CDINSCESTA, CDINSCMUNI, CDSERIE
                    FROM ITEMEXPEDICAO
                   WHERE CDFILIAL = P_CDFILIAL
                     AND NREXPEDICAO = P_NREXPEDICAO
                     AND ((CDEMPRESA IS NULL) OR (CDINSCESTA IS NULL) OR (CDINSCMUNI IS NULL) OR (CDSERIE IS NULL)))
   LOOP
      UPDATE ITEMEXPEDICAO
         SET IDERRO = 'S'
       WHERE CDFILIAL = C_ERRO.CDFILIAL
         AND NREXPEDICAO = C_ERRO.NREXPEDICAO
         AND NRPEDIDO = C_ERRO.NRPEDIDO;
      SELECT NVL(MAX(NRSEQERROITEM),0)
        INTO V_NRSEQ
        FROM ERROITEMEXP
       WHERE CDFILIAL = C_ERRO.CDFILIAL
         AND NREXPEDICAO = C_ERRO.NREXPEDICAO
         AND NRPEDIDO = C_ERRO.NRPEDIDO;
      IF (V_IDEMPRESANFEXP = 'EMPRESACLIENTE') THEN
         /*------------------SERIE_CLIENTE--------------*/
         V_NRSEQ := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_NRSEQ) + 1)),10,0);
         V_ERRO := 0;
         SELECT COUNT(1)
           INTO V_ERRO
           FROM ERROITEMEXP
          WHERE CDFILIAL = C_ERRO.CDFILIAL
            AND NREXPEDICAO = C_ERRO.NREXPEDICAO
            AND NRPEDIDO = C_ERRO.NRPEDIDO
            AND DSERRO = 'SERIE_CLIENTE';
         IF (V_ERRO = 0) THEN
            INSERT INTO ERROITEMEXP
            (CDFILIAL, NREXPEDICAO, NRPEDIDO, NRITEMPED, NRSEQERROITEM, DSERRO, CDCLIENTE, CDFILIALPED, CDPRODUTO, CDPRODMOVI, QTITEMEXP, QTTOTAITEMPROD, QTTOTAESTOQ,
             CDEMPRESA, CDINSCESTA, CDINSCMUNI, CDSERIE, NRORDPROD, NRSUBORDPROD)
            VALUES
            (C_ERRO.CDFILIAL, C_ERRO.NREXPEDICAO, C_ERRO.NRPEDIDO, '', V_NRSEQ, 'SERIE_CLIENTE', C_ERRO.CDCLIENTE, C_ERRO.CDFILIALPED, '', '', 0, 0, 0,
             C_ERRO.CDEMPRESA, C_ERRO.CDINSCESTA, C_ERRO.CDINSCMUNI, C_ERRO.CDSERIE, '', '');
         END IF;
         /*------------------SERIE_CLIENTE--------------*/
      ELSE
         IF (V_IDEMPRESANFEXP = 'EMPRESAFILIAL') THEN
            /*------------------SERIE_FILIAL--------------*/
            V_NRSEQ := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_NRSEQ) + 1)),10,0);
            V_ERRO := 0;
            SELECT COUNT(1)
              INTO V_ERRO
              FROM ERROITEMEXP
             WHERE CDFILIAL = C_ERRO.CDFILIAL
               AND NREXPEDICAO = C_ERRO.NREXPEDICAO
               AND NRPEDIDO = C_ERRO.NRPEDIDO
               AND DSERRO = 'SERIE_FILIAL';
            IF (V_ERRO = 0) THEN
               INSERT INTO ERROITEMEXP
               (CDFILIAL, NREXPEDICAO, NRPEDIDO, NRITEMPED, NRSEQERROITEM, DSERRO, CDCLIENTE, CDFILIALPED, CDPRODUTO, CDPRODMOVI, QTITEMEXP, QTTOTAITEMPROD, QTTOTAESTOQ,
                CDEMPRESA, CDINSCESTA, CDINSCMUNI, CDSERIE, NRORDPROD, NRSUBORDPROD)
               VALUES
               (C_ERRO.CDFILIAL, C_ERRO.NREXPEDICAO, C_ERRO.NRPEDIDO, '', V_NRSEQ, 'SERIE_FILIAL', C_ERRO.CDCLIENTE, C_ERRO.CDFILIALPED, '', '', 0, 0, 0,
                C_ERRO.CDEMPRESA, C_ERRO.CDINSCESTA, C_ERRO.CDINSCMUNI, C_ERRO.CDSERIE, '', '');
            END IF;
            /*------------------SERIE_FILIAL--------------*/
         ELSE
            IF (V_IDEMPRESANFEXP = 'INFORMALANCEXP') THEN
               /*------------------SERIESAIDA--------------*/
               V_NRSEQ := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_NRSEQ) + 1)),10,0);
               V_ERRO := 0;
               SELECT COUNT(1)
                 INTO V_ERRO
                 FROM ERROITEMEXP
                WHERE CDFILIAL = C_ERRO.CDFILIAL
                  AND NREXPEDICAO = C_ERRO.NREXPEDICAO
                  AND NRPEDIDO = C_ERRO.NRPEDIDO
                  AND DSERRO = 'SERIESAIDA';
               IF (V_ERRO = 0) THEN
                  INSERT INTO ERROITEMEXP
                  (CDFILIAL, NREXPEDICAO, NRPEDIDO, NRITEMPED, NRSEQERROITEM, DSERRO, CDCLIENTE, CDFILIALPED, CDPRODUTO, CDPRODMOVI, QTITEMEXP, QTTOTAITEMPROD, QTTOTAESTOQ,
                   CDEMPRESA, CDINSCESTA, CDINSCMUNI, CDSERIE, NRORDPROD, NRSUBORDPROD)
                  VALUES
                  (C_ERRO.CDFILIAL, C_ERRO.NREXPEDICAO, C_ERRO.NRPEDIDO, '', V_NRSEQ, 'SERIESAIDA', C_ERRO.CDCLIENTE, C_ERRO.CDFILIALPED, '', '', 0, 0, 0,
                   C_ERRO.CDEMPRESA, C_ERRO.CDINSCESTA, C_ERRO.CDINSCMUNI, C_ERRO.CDSERIE, '', '');
               END IF;
               /*------------------SERIESAIDA--------------*/
            END IF;
         END IF;
      END IF;
   END LOOP;
   /*------------------EMPRESA, INSR. ESTADUAL, INSCR. MUNICIPAL E S:param2RIE DEVEM SER INFORMADAS--------------*/
   /*-------INICIO VINGINSCESTA-------------*/
   FOR C_ERRO IN (SELECT DISTINCT CDFILIAL, NREXPEDICAO, NRPEDIDO, CDCLIENTE, CDFILIALPED,
                         CDEMPRESA, CDINSCESTA, CDINSCMUNI, CDSERIE
                    FROM ITEMEXPEDICAO
                   WHERE CDFILIAL = P_CDFILIAL
                     AND NREXPEDICAO = P_NREXPEDICAO
                     AND ((CDEMPRESA IS NOT NULL) OR (CDINSCESTA IS NOT NULL)))
   LOOP
      V_INSCESTADOK := 0;
      BEGIN
         SELECT COUNT(1)
            INTO V_INSCESTADOK
            FROM INSCRESTAD I
           WHERE (I.CDEMPRESA  = C_ERRO.CDEMPRESA)
             AND (I.CDINSCESTA = C_ERRO.CDINSCESTA)
             AND (NVL(V_DTEXPEDICAO, TRUNC(SYSDATE)) >= NVL(NVL(I.DTINIVIGIEST, V_DTEXPEDICAO), TRUNC(SYSDATE)))
             AND (NVL(V_DTEXPEDICAO, TRUNC(SYSDATE)) <= NVL(NVL(I.DTFIMVIGIEST, V_DTEXPEDICAO), TRUNC(SYSDATE)));
      EXCEPTION
         WHEN NO_DATA_FOUND THEN BEGIN
            V_INSCESTADOK := 0;
         END;
      END;
      IF (V_INSCESTADOK = 0) THEN
         UPDATE ITEMEXPEDICAO
            SET IDERRO = 'S'
          WHERE CDFILIAL = C_ERRO.CDFILIAL
            AND NREXPEDICAO = C_ERRO.NREXPEDICAO
            AND NRPEDIDO = C_ERRO.NRPEDIDO;
         SELECT NVL(MAX(NRSEQERROITEM),0)
           INTO V_NRSEQ
           FROM ERROITEMEXP
          WHERE CDFILIAL = C_ERRO.CDFILIAL
            AND NREXPEDICAO = C_ERRO.NREXPEDICAO
            AND NRPEDIDO = C_ERRO.NRPEDIDO;
         V_NRSEQ := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_NRSEQ) + 1)),10,0);
         V_ERRO := 0;
         SELECT COUNT(1)
           INTO V_ERRO
           FROM ERROITEMEXP
          WHERE CDFILIAL = C_ERRO.CDFILIAL
            AND NREXPEDICAO = C_ERRO.NREXPEDICAO
            AND NRPEDIDO = C_ERRO.NRPEDIDO
            AND DSERRO = 'VINGINSCESTA';
         IF (V_ERRO = 0) THEN
            INSERT INTO ERROITEMEXP
            (CDFILIAL, NREXPEDICAO, NRPEDIDO, NRITEMPED, NRSEQERROITEM, DSERRO, CDCLIENTE, CDFILIALPED, CDPRODUTO, CDPRODMOVI, QTITEMEXP, QTTOTAITEMPROD, QTTOTAESTOQ,
             CDEMPRESA, CDINSCESTA, CDINSCMUNI, CDSERIE, NRORDPROD, NRSUBORDPROD)
            VALUES
            (C_ERRO.CDFILIAL, C_ERRO.NREXPEDICAO, C_ERRO.NRPEDIDO, '', V_NRSEQ, 'VINGINSCESTA', C_ERRO.CDCLIENTE, C_ERRO.CDFILIALPED, '', '', 0, 0, 0,
             C_ERRO.CDEMPRESA, C_ERRO.CDINSCESTA, C_ERRO.CDINSCMUNI, C_ERRO.CDSERIE, '', '');
         END IF;
      END IF;
   END LOOP;
   /*-------FIM VINGINSCESTA-------------*/
   /*------------------INICIO PEDINDUSTRIA--------------*/
   FOR C_ERRO IN (SELECT DISTINCT CDFILIAL, NREXPEDICAO, NRPEDIDO, CDCLIENTE, CDFILIALPED, CDEMPRESA, CDINSCESTA, CDINSCMUNI, CDSERIE
                    FROM ITEMEXPEDICAO
                   WHERE CDFILIAL = P_CDFILIAL
                     AND NREXPEDICAO = P_NREXPEDICAO
                     AND (IDPEDINDUSTRIA = 'S'))
   LOOP
      BEGIN
         SELECT MAX(IDTIPOEXP)
           INTO V_IDTIPOEXP
           FROM EXPRELACITEMPPED
          WHERE CDFILIAL = P_CDFILIAL
            AND NREXPEDICAO = P_NREXPEDICAO;
      EXCEPTION
         WHEN NO_DATA_FOUND THEN BEGIN
            V_IDTIPOEXP := NULL;
         END;
      END;
      IF (V_IDTIPOEXP = 'ITEM') THEN
         UPDATE ITEMEXPEDICAO
            SET IDERRO = 'S'
          WHERE CDFILIAL = C_ERRO.CDFILIAL
            AND NREXPEDICAO = C_ERRO.NREXPEDICAO
            AND NRPEDIDO = C_ERRO.NRPEDIDO;
         SELECT NVL(MAX(NRSEQERROITEM),0)
           INTO V_NRSEQ
           FROM ERROITEMEXP
          WHERE CDFILIAL = C_ERRO.CDFILIAL
            AND NREXPEDICAO = C_ERRO.NREXPEDICAO
            AND NRPEDIDO = C_ERRO.NRPEDIDO;
         V_NRSEQ := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_NRSEQ) + 1)),10,0);
         V_ERRO := 0;
         SELECT COUNT(1)
           INTO V_ERRO
           FROM ERROITEMEXP
          WHERE CDFILIAL = C_ERRO.CDFILIAL
            AND NREXPEDICAO = C_ERRO.NREXPEDICAO
            AND NRPEDIDO = C_ERRO.NRPEDIDO
            AND DSERRO = 'PEDINDUSTRIA';
         IF (V_ERRO = 0) THEN
            INSERT INTO ERROITEMEXP
            (CDFILIAL, NREXPEDICAO, NRPEDIDO, NRITEMPED, NRSEQERROITEM, DSERRO, CDCLIENTE, CDFILIALPED, CDPRODUTO, CDPRODMOVI, QTITEMEXP, QTTOTAITEMPROD, QTTOTAESTOQ,
             CDEMPRESA, CDINSCESTA, CDINSCMUNI, CDSERIE, NRORDPROD, NRSUBORDPROD)
            VALUES
            (C_ERRO.CDFILIAL, C_ERRO.NREXPEDICAO, C_ERRO.NRPEDIDO, '', V_NRSEQ, 'PEDINDUSTRIA',C_ERRO.CDCLIENTE, C_ERRO.CDFILIALPED, '', '', 0, 0, 0,
             C_ERRO.CDEMPRESA, C_ERRO.CDINSCESTA, C_ERRO.CDINSCMUNI, C_ERRO.CDSERIE, '', '');
         END IF;
      /*-------FIM PEDINDUSTRIA-------------*/
      ELSE
      /*------------------INICIO PEDINDSEMOP--------------*/
         BEGIN
            SELECT MAX(O.NRORDPROD)
              INTO V_NRORDPROD
              FROM ITEMPEDIDO I JOIN ORDPRODUCAO O
                                  ON I.CDFILIAL  = O.CDFILIAL
                                 AND I.NRPEDIDO  = O.NRPEDIDO
                                 AND I.NRITEMPED = O.NRITEMPED
             WHERE I.CDFILIAL = C_ERRO.CDFILIAL
               AND I.NRPEDIDO = C_ERRO.NRPEDIDO
               AND I.IDITEMPEDTERC = 'S';
         EXCEPTION
            WHEN NO_DATA_FOUND THEN
               V_NRORDPROD := NULL;
         END;
         IF (V_NRORDPROD IS NULL) THEN
            UPDATE ITEMEXPEDICAO
               SET IDERRO = 'S'
             WHERE CDFILIAL = C_ERRO.CDFILIAL
               AND NREXPEDICAO = C_ERRO.NREXPEDICAO
               AND NRPEDIDO = C_ERRO.NRPEDIDO;
            SELECT NVL(MAX(NRSEQERROITEM),0)
              INTO V_NRSEQ
              FROM ERROITEMEXP
             WHERE CDFILIAL = C_ERRO.CDFILIAL
               AND NREXPEDICAO = C_ERRO.NREXPEDICAO
               AND NRPEDIDO = C_ERRO.NRPEDIDO;
            V_NRSEQ := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_NRSEQ) + 1)),10,0);
            V_ERRO := 0;
            SELECT COUNT(1)
              INTO V_ERRO
              FROM ERROITEMEXP
             WHERE CDFILIAL = C_ERRO.CDFILIAL
               AND NREXPEDICAO = C_ERRO.NREXPEDICAO
               AND NRPEDIDO = C_ERRO.NRPEDIDO
               AND DSERRO = 'PEDINDSEMOP';
            IF (V_ERRO = 0) THEN
               INSERT INTO ERROITEMEXP
               (CDFILIAL, NREXPEDICAO, NRPEDIDO, NRITEMPED, NRSEQERROITEM, DSERRO, CDCLIENTE, CDFILIALPED, CDPRODUTO, CDPRODMOVI, QTITEMEXP, QTTOTAITEMPROD, QTTOTAESTOQ,
                CDEMPRESA, CDINSCESTA, CDINSCMUNI, CDSERIE, NRORDPROD, NRSUBORDPROD)
               VALUES
               (C_ERRO.CDFILIAL, C_ERRO.NREXPEDICAO, C_ERRO.NRPEDIDO, '', V_NRSEQ, 'PEDINDSEMOP', C_ERRO.CDCLIENTE, C_ERRO.CDFILIALPED, '', '', 0, 0, 0,
                C_ERRO.CDEMPRESA, C_ERRO.CDINSCESTA, C_ERRO.CDINSCMUNI, C_ERRO.CDSERIE, '', '');
            END IF;
      /*-------FIM PEDINDSEMOP-------------*/
         ELSE
      /*------------------INICIO PEDINDOPNC--------------*/
            V_ERROOP := '0';
            FOR C_OP IN (SELECT O.CDFILIAL, O.NRORDPROD, O.NRSUBORDPROD, O.NRITEMPED, O.IDSITORDPROD
                           FROM ITEMPEDIDO I JOIN ORDPRODUCAO O
                                               ON I.CDFILIAL  = O.CDFILIAL
                                              AND I.NRPEDIDO  = O.NRPEDIDO
                                              AND I.NRITEMPED = O.NRITEMPED
                          WHERE I.CDFILIAL = C_ERRO.CDFILIAL
                            AND I.NRPEDIDO = C_ERRO.NRPEDIDO
                            AND I.IDITEMPEDTERC = 'S')
            LOOP
               IF ((C_OP.IDSITORDPROD <> '3') AND (C_OP.IDSITORDPROD <> '4') AND (C_OP.IDSITORDPROD <> '5')) THEN
                  UPDATE ITEMEXPEDICAO
                     SET IDERRO = 'S'
                   WHERE CDFILIAL = C_ERRO.CDFILIAL
                     AND NREXPEDICAO = C_ERRO.NREXPEDICAO
                     AND NRPEDIDO = C_ERRO.NRPEDIDO;
                  SELECT NVL(MAX(NRSEQERROITEM),0)
                    INTO V_NRSEQ
                    FROM ERROITEMEXP
                   WHERE CDFILIAL = C_ERRO.CDFILIAL
                     AND NREXPEDICAO = C_ERRO.NREXPEDICAO
                     AND NRPEDIDO = C_ERRO.NRPEDIDO;
                  V_NRSEQ := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_NRSEQ) + 1)),10,0);
                  V_ERRO := 0;
                  SELECT COUNT(1)
                    INTO V_ERRO
                    FROM ERROITEMEXP
                   WHERE CDFILIAL = C_ERRO.CDFILIAL
                     AND NREXPEDICAO = C_ERRO.NREXPEDICAO
                     AND NRPEDIDO = C_ERRO.NRPEDIDO
                     AND NRITEMPED = C_OP.NRITEMPED
                     AND DSERRO = 'PEDINDOPNC';
                  IF (V_ERRO = 0) THEN
                     INSERT INTO ERROITEMEXP
                     (CDFILIAL, NREXPEDICAO, NRPEDIDO, NRITEMPED, NRSEQERROITEM, DSERRO, CDCLIENTE, CDFILIALPED, CDPRODUTO, CDPRODMOVI, QTITEMEXP, QTTOTAITEMPROD, QTTOTAESTOQ,
                      CDEMPRESA, CDINSCESTA, CDINSCMUNI, CDSERIE, NRORDPROD, NRSUBORDPROD)
                     VALUES
                     (C_ERRO.CDFILIAL, C_ERRO.NREXPEDICAO, C_ERRO.NRPEDIDO, C_OP.NRITEMPED, V_NRSEQ, 'PEDINDOPNC', C_ERRO.CDCLIENTE, C_ERRO.CDFILIALPED, '', '', 0, 0, 0,
                      C_ERRO.CDEMPRESA, C_ERRO.CDINSCESTA, C_ERRO.CDINSCMUNI, C_ERRO.CDSERIE, C_OP.NRORDPROD, C_OP.NRSUBORDPROD);
                  END IF;
               END IF;
            END LOOP;
         END IF;
      /*------------------FIM PEDINDOPNC--------------*/
      /*------------------INICIO QTOPMENORPED--------------*/
         FOR C_QTOP IN (SELECT I.CDFILIAL, I.NRPEDIDO, I.NRITEMPED, I.QTITEMPED, O.NRORDPROD, O.NRSUBORDPROD, O.CDPRODUTO, O.QTAPRODPREOP, P.QTPRACABADO
                          FROM ITEMPEDIDO I JOIN ORDPRODUCAO O
                                              ON I.CDFILIAL = O.CDFILIAL
                                             AND I.NRPEDIDO = O.NRPEDIDO
                                             AND I.NRITEMPED = O.NRITEMPED
                                            JOIN PRODACABADO P
                                              ON O.CDFILIAL = P.CDFILIAL
                                             AND O.NRORDPROD = P.NRORDPROD
                                             AND O.NRSUBORDPROD = P.NRSUBORDPROD
                                             AND O.CDPRODUTO = P.CDSUBPRODETA
                         WHERE I.CDFILIAL = C_ERRO.CDFILIAL
                           AND I.NRPEDIDO = C_ERRO.NRPEDIDO
                           AND I.CDPRODUTO = O.CDPRODUTO
                           AND O.IDSITORDPROD = '3'
                           AND I.IDITEMPEDTERC = 'S'
                           AND ROUND(P.QTPRACABADO,5) < ROUND(I.QTITEMPED, 5))
         LOOP
            UPDATE ITEMEXPEDICAO
               SET IDERRO = 'S'
             WHERE CDFILIAL = C_ERRO.CDFILIAL
               AND NREXPEDICAO = C_ERRO.NREXPEDICAO
               AND NRPEDIDO = C_ERRO.NRPEDIDO
               AND NRITEMPED = C_QTOP.NRITEMPED;
            SELECT NVL(MAX(NRSEQERROITEM),0)
              INTO V_NRSEQ
              FROM ERROITEMEXP
             WHERE CDFILIAL = C_ERRO.CDFILIAL
               AND NREXPEDICAO = C_ERRO.NREXPEDICAO
               AND NRPEDIDO = C_ERRO.NRPEDIDO;
            V_NRSEQ := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_NRSEQ) + 1)),10,0);
            V_ERRO := 0;
            SELECT COUNT(1)
              INTO V_ERRO
              FROM ERROITEMEXP
             WHERE CDFILIAL = C_ERRO.CDFILIAL
               AND NREXPEDICAO = C_ERRO.NREXPEDICAO
               AND NRPEDIDO = C_ERRO.NRPEDIDO
               AND NRITEMPED = C_QTOP.NRITEMPED
               AND DSERRO = 'QTOPMENORPED';
            IF (V_ERRO = 0) THEN
               INSERT INTO ERROITEMEXP
               (CDFILIAL, NREXPEDICAO, NRPEDIDO, NRITEMPED, NRSEQERROITEM, DSERRO, CDCLIENTE, CDFILIALPED, CDPRODUTO, CDPRODMOVI, QTITEMEXP, QTTOTAITEMPROD, QTTOTAESTOQ,
                CDEMPRESA, CDINSCESTA, CDINSCMUNI, CDSERIE, NRORDPROD, NRSUBORDPROD)
               VALUES
               (C_ERRO.CDFILIAL, C_ERRO.NREXPEDICAO, C_ERRO.NRPEDIDO, C_QTOP.NRITEMPED, V_NRSEQ, 'QTOPMENORPED', C_ERRO.CDCLIENTE, C_ERRO.CDFILIALPED, C_QTOP.CDPRODUTO, C_QTOP.CDPRODUTO, C_QTOP.QTITEMPED, 0, 0,
                C_ERRO.CDEMPRESA, C_ERRO.CDINSCESTA, C_ERRO.CDINSCMUNI, C_ERRO.CDSERIE, C_QTOP.NRORDPROD, C_QTOP.NRSUBORDPROD);
            END IF;
         END LOOP;
         /*------------------FIM QTOPMENORPED--------------*/
         /*------------------INICIO TPOPPERDA--------------*/
         V_COUNTPERDAPA := 0;
         V_COUNTPERDASE := 0;
         BEGIN
            SELECT COUNT(1)
              INTO V_COUNTPERDAPA
              FROM TRABPRODACABPED T JOIN PERDAOP P
                                       ON T.CDFILIAL = P.CDFILIAL
                                      AND T.NRORDPROD = P.NRORDPROD
                                      AND T.NRSUBORDPROD = P.NRSUBORDPROD
             WHERE T.CDFILIAL = C_ERRO.CDFILIAL
               AND T.NRPEDIDO = C_ERRO.NRPEDIDO;
         EXCEPTION
            WHEN NO_DATA_FOUND THEN
               V_COUNTPERDAPA := 0;
         END;
         BEGIN
            SELECT COUNT(1)
              INTO V_COUNTPERDASE
              FROM TRABSEMIACABADOPED T JOIN PERDAOP P
                                          ON T.CDFILIAL = P.CDFILIAL
                                         AND T.NRORDPROD = P.NRORDPROD
                                         AND T.NRSUBORDPROD = P.NRSUBORDPROD
             WHERE T.CDFILIAL = C_ERRO.CDFILIAL
               AND T.NRPEDIDO = C_ERRO.NRPEDIDO;
         EXCEPTION
            WHEN NO_DATA_FOUND THEN
               V_COUNTPERDASE := 0;
         END;
         IF ((V_COUNTPERDAPA > 0) OR (V_COUNTPERDASE > 0)) THEN
            IF (V_CDTPOPERPERDA IS NULL) THEN
               UPDATE ITEMEXPEDICAO
                  SET IDERRO = 'S'
                WHERE CDFILIAL = C_ERRO.CDFILIAL
                  AND NREXPEDICAO = C_ERRO.NREXPEDICAO
                  AND NRPEDIDO = C_ERRO.NRPEDIDO;
               SELECT NVL(MAX(NRSEQERROITEM),0)
                 INTO V_NRSEQ
                 FROM ERROITEMEXP
                WHERE CDFILIAL = C_ERRO.CDFILIAL
                  AND NREXPEDICAO = C_ERRO.NREXPEDICAO
                  AND NRPEDIDO = C_ERRO.NRPEDIDO;
               V_NRSEQ := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_NRSEQ) + 1)),10,0);
               V_ERRO := 0;
               SELECT COUNT(1)
                 INTO V_ERRO
                 FROM ERROITEMEXP
                WHERE CDFILIAL = C_ERRO.CDFILIAL
                  AND NREXPEDICAO = C_ERRO.NREXPEDICAO
                  AND NRPEDIDO = C_ERRO.NRPEDIDO
                  AND DSERRO = 'TPOPPERDA';
               IF (V_ERRO = 0) THEN
                  INSERT INTO ERROITEMEXP
                  (CDFILIAL, NREXPEDICAO, NRPEDIDO, NRITEMPED, NRSEQERROITEM, DSERRO, CDCLIENTE, CDFILIALPED, CDPRODUTO, CDPRODMOVI, QTITEMEXP, QTTOTAITEMPROD, QTTOTAESTOQ,
                   CDEMPRESA, CDINSCESTA, CDINSCMUNI, CDSERIE, NRORDPROD, NRSUBORDPROD)
                  VALUES
                  (C_ERRO.CDFILIAL, C_ERRO.NREXPEDICAO, C_ERRO.NRPEDIDO, '', V_NRSEQ, 'TPOPPERDA', C_ERRO.CDCLIENTE, C_ERRO.CDFILIALPED, '', '', 0, 0, 0,
                   C_ERRO.CDEMPRESA, C_ERRO.CDINSCESTA, C_ERRO.CDINSCMUNI, C_ERRO.CDSERIE, '', '');
               END IF;
         /*------------------INICIO TPOPPERDA--------------*/
            ELSE
         /*------------------INICIO TPPERDAEST--------------*/
               SELECT NVL(MAX(IDATUESTTPOP), 'S')
                 INTO V_IDATUESTTPOP
                 FROM TIPOOPERAC
                WHERE IDENTRSAIDOP = 'S'
                  AND CDTIPOOPER = V_CDTPOPERPERDA;
               IF (V_IDATUESTTPOP = 'N') THEN
                  UPDATE ITEMEXPEDICAO
                     SET IDERRO = 'S'
                   WHERE CDFILIAL = C_ERRO.CDFILIAL
                     AND NREXPEDICAO = C_ERRO.NREXPEDICAO
                     AND NRPEDIDO = C_ERRO.NRPEDIDO;
                  SELECT NVL(MAX(NRSEQERROITEM),0)
                    INTO V_NRSEQ
                    FROM ERROITEMEXP
                   WHERE CDFILIAL = C_ERRO.CDFILIAL
                     AND NREXPEDICAO = C_ERRO.NREXPEDICAO
                     AND NRPEDIDO = C_ERRO.NRPEDIDO;
                  V_NRSEQ := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_NRSEQ) + 1)),10,0);
                  V_ERRO := 0;
                  SELECT COUNT(1)
                    INTO V_ERRO
                    FROM ERROITEMEXP
                   WHERE CDFILIAL = C_ERRO.CDFILIAL
                     AND NREXPEDICAO = C_ERRO.NREXPEDICAO
                     AND NRPEDIDO = C_ERRO.NRPEDIDO
                     AND DSERRO = 'TPPERDAEST';
                  IF (V_ERRO = 0) THEN
                     INSERT INTO ERROITEMEXP
                     (CDFILIAL, NREXPEDICAO, NRPEDIDO, NRITEMPED, NRSEQERROITEM, DSERRO, CDCLIENTE, CDFILIALPED, CDPRODUTO, CDPRODMOVI, QTITEMEXP, QTTOTAITEMPROD, QTTOTAESTOQ,
                      CDEMPRESA, CDINSCESTA, CDINSCMUNI, CDSERIE, NRORDPROD, NRSUBORDPROD)
                     VALUES
                     (C_ERRO.CDFILIAL, C_ERRO.NREXPEDICAO, C_ERRO.NRPEDIDO, '', V_NRSEQ, 'TPPERDAEST', C_ERRO.CDCLIENTE, C_ERRO.CDFILIALPED, '', '', 0, 0, 0,
                      C_ERRO.CDEMPRESA, C_ERRO.CDINSCESTA, C_ERRO.CDINSCMUNI, C_ERRO.CDSERIE, '', '');
                  END IF;
               END IF;
         /*------------------FIM TPPERDAEST--------------*/
            END IF;
         END IF;
      END IF;
   END LOOP;
/*------------------FIM PEDINDUSTRIA--------------*/
   IF (V_IDCTRLESTQ = 'S') THEN
      IF (V_IDRETPROEXP = '2') THEN
/*------------------ALMOX_NULL--------------*/
         FOR C_ERRO IN (SELECT I.CDFILIAL, I.NREXPEDICAO, I.NRPEDIDO, I.NRITEMPED, I.CDPRODUTO, P.CDPRODESTO, C.IDSUBPRUTALMOX IDUTILALMOX, C.IDSUBPRUTLCEST IDUTILLCESTQ,
                               I.CDCLIENTE, I.CDFILIALPED, I.QTITEMEXP, C.QTSUBPROD AS QTITEMPROD, I.CDEMPRESA, I.CDINSCESTA, I.CDINSCMUNI, I.CDSERIE
                          FROM ITEMEXPEDICAO I JOIN COMPITEMEXP C
                                                 ON I.CDFILIAL = C.CDFILIAL
                                                AND I.NREXPEDICAO = C.NREXPEDICAO
                                                AND I.NRPEDIDO = C.NRPEDIDO
                                                AND I.NRITEMPED = C.NRITEMPED
                                               JOIN PRODUTO P
                                                 ON C.CDSUBPROD = P.CDPRODUTO
                         WHERE I.CDFILIAL = P_CDFILIAL
                           AND I.NREXPEDICAO = P_NREXPEDICAO
                           AND I.IDATUESTTPOP = 'S'
                           AND I.IDRESERVA = 'N'
                           AND I.IDITEMCOMPPROD = 'S'
                           AND C.IDSUBPRCTRLEST = 'S'
                           AND ((V_IDUTILALMOX = 'S') AND ((C.CDSUBPRALMOX IS NULL) OR (C.CDSUBPRALMOX = ' '))))

         LOOP
            UPDATE ITEMEXPEDICAO
               SET IDERRO = 'S'
             WHERE CDFILIAL = C_ERRO.CDFILIAL
               AND NREXPEDICAO = C_ERRO.NREXPEDICAO
               AND NRPEDIDO = C_ERRO.NRPEDIDO
               AND NRITEMPED = C_ERRO.NRITEMPED;
            SELECT NVL(MAX(NRSEQERROITEM),0)
              INTO V_NRSEQ
              FROM ERROITEMEXP
             WHERE CDFILIAL = C_ERRO.CDFILIAL
               AND NREXPEDICAO = C_ERRO.NREXPEDICAO
               AND NRPEDIDO = C_ERRO.NRPEDIDO;
            V_NRSEQ := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_NRSEQ) + 1)),10,0);
            INSERT INTO ERROITEMEXP
            (CDFILIAL, NREXPEDICAO, NRPEDIDO, NRITEMPED, NRSEQERROITEM, DSERRO, CDCLIENTE, CDFILIALPED, CDPRODUTO, CDPRODMOVI, QTITEMEXP, QTTOTAITEMPROD, QTTOTAESTOQ,
             CDEMPRESA, CDINSCESTA, CDINSCMUNI, CDSERIE, NRORDPROD, NRSUBORDPROD)
            VALUES
            (C_ERRO.CDFILIAL, C_ERRO.NREXPEDICAO, C_ERRO.NRPEDIDO, C_ERRO.NRITEMPED, V_NRSEQ, 'ALMOX_NULL', C_ERRO.CDCLIENTE, C_ERRO.CDFILIALPED, C_ERRO.CDPRODUTO, C_ERRO.CDPRODESTO,  C_ERRO.QTITEMEXP, C_ERRO.QTITEMPROD, 0,
             C_ERRO.CDEMPRESA, C_ERRO.CDINSCESTA, C_ERRO.CDINSCMUNI, C_ERRO.CDSERIE, '', '');
         END LOOP;
/*------------------ALMOX_NULL--------------*/
/*------------------LOCAL_NULL--------------*/
         FOR C_ERRO IN (SELECT I.CDFILIAL, I.NREXPEDICAO, I.NRPEDIDO, I.NRITEMPED, I.CDPRODUTO, P.CDPRODESTO, C.IDSUBPRUTALMOX IDUTILALMOX, C.IDSUBPRUTLCEST IDUTILLCESTQ,
                               I.CDCLIENTE, I.CDFILIALPED, I.QTITEMEXP, C.QTSUBPROD AS QTITEMPROD, I.CDEMPRESA, I.CDINSCESTA, I.CDINSCMUNI, I.CDSERIE
                          FROM ITEMEXPEDICAO I JOIN COMPITEMEXP C
                                                 ON I.CDFILIAL = C.CDFILIAL
                                                AND I.NREXPEDICAO = C.NREXPEDICAO
                                                AND I.NRPEDIDO = C.NRPEDIDO
                                                AND I.NRITEMPED = C.NRITEMPED
                                               JOIN PRODUTO P
                                                 ON C.CDSUBPROD = P.CDPRODUTO
                         WHERE I.CDFILIAL = P_CDFILIAL
                           AND I.NREXPEDICAO = P_NREXPEDICAO
                           AND I.IDATUESTTPOP = 'S'
                           AND I.IDRESERVA = 'N'
                           AND I.IDITEMCOMPPROD = 'S'
                           AND C.IDSUBPRCTRLEST = 'S'
                           AND ((V_IDUTILLCESTQ  = 'S') AND ((C.CDSUBPRLOCAL IS NULL) OR (C.CDSUBPRLOCAL = ' '))))

         LOOP
            UPDATE ITEMEXPEDICAO
               SET IDERRO = 'S'
             WHERE CDFILIAL = C_ERRO.CDFILIAL
               AND NREXPEDICAO = C_ERRO.NREXPEDICAO
               AND NRPEDIDO = C_ERRO.NRPEDIDO
               AND NRITEMPED = C_ERRO.NRITEMPED;
            SELECT NVL(MAX(NRSEQERROITEM),0)
              INTO V_NRSEQ
              FROM ERROITEMEXP
             WHERE CDFILIAL = C_ERRO.CDFILIAL
               AND NREXPEDICAO = C_ERRO.NREXPEDICAO
               AND NRPEDIDO = C_ERRO.NRPEDIDO;
            V_NRSEQ := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_NRSEQ) + 1)),10,0);
            INSERT INTO ERROITEMEXP
            (CDFILIAL, NREXPEDICAO, NRPEDIDO, NRITEMPED, NRSEQERROITEM, DSERRO, CDCLIENTE, CDFILIALPED, CDPRODUTO, CDPRODMOVI, QTITEMEXP, QTTOTAITEMPROD, QTTOTAESTOQ,
             CDEMPRESA, CDINSCESTA, CDINSCMUNI, CDSERIE, NRORDPROD, NRSUBORDPROD)
            VALUES
            (C_ERRO.CDFILIAL, C_ERRO.NREXPEDICAO, C_ERRO.NRPEDIDO, C_ERRO.NRITEMPED, V_NRSEQ, 'LOCAL_NULL', C_ERRO.CDCLIENTE, C_ERRO.CDFILIALPED, C_ERRO.CDPRODUTO, C_ERRO.CDPRODESTO,  C_ERRO.QTITEMEXP, C_ERRO.QTITEMPROD, 0,
             C_ERRO.CDEMPRESA, C_ERRO.CDINSCESTA, C_ERRO.CDINSCMUNI, C_ERRO.CDSERIE, '', '');
         END LOOP;
         /*------------------LOCAL_NULL--------------*/
         /*------------------ALMOX_NULL--------------*/
         FOR C_ERRO IN (SELECT I.CDFILIAL, I.NREXPEDICAO, I.NRPEDIDO, I.NRITEMPED, I.CDPRODUTO, I.CDPRODMOVI, P.CDPRODESTO,
                               I.CDCLIENTE, I.CDFILIALPED, I.QTITEMEXP, I.QTITEMPROD,
                               I.CDEMPRESA, I.CDINSCESTA, I.CDINSCMUNI, I.CDSERIE
                          FROM ITEMEXPEDICAO I JOIN PRODUTO P
                                                 ON I.CDPRODMOVI = P.CDPRODUTO
                         WHERE I.CDFILIAL = P_CDFILIAL
                           AND I.NREXPEDICAO = P_NREXPEDICAO
                           AND I.CDTIPOOPER IS NOT NULL
                           AND I.IDATUESTTPOP = 'S'
                           AND I.IDCNTRESTOQ = 'S'
                           AND I.IDRESERVA = 'N'
                           AND I.IDITEMCOMPPROD = 'N'
                           AND ((V_IDUTILALMOX = 'S') AND ((I.CDALMOXEXP IS NULL) OR (I.CDALMOXEXP = ' '))))
         LOOP
            UPDATE ITEMEXPEDICAO
               SET IDERRO = 'S'
             WHERE CDFILIAL = C_ERRO.CDFILIAL
               AND NREXPEDICAO = C_ERRO.NREXPEDICAO
               AND NRPEDIDO = C_ERRO.NRPEDIDO
               AND NRITEMPED = C_ERRO.NRITEMPED;
            SELECT NVL(MAX(NRSEQERROITEM),0)
              INTO V_NRSEQ
              FROM ERROITEMEXP
             WHERE CDFILIAL = C_ERRO.CDFILIAL
               AND NREXPEDICAO = C_ERRO.NREXPEDICAO
               AND NRPEDIDO = C_ERRO.NRPEDIDO;
            V_NRSEQ := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_NRSEQ) + 1)),10,0);
            INSERT INTO ERROITEMEXP
            (CDFILIAL, NREXPEDICAO, NRPEDIDO, NRITEMPED, NRSEQERROITEM, DSERRO, CDCLIENTE, CDFILIALPED, CDPRODUTO, CDPRODMOVI, QTITEMEXP, QTTOTAITEMPROD, QTTOTAESTOQ,
             CDEMPRESA, CDINSCESTA, CDINSCMUNI, CDSERIE, NRORDPROD, NRSUBORDPROD)
            VALUES
            (C_ERRO.CDFILIAL, C_ERRO.NREXPEDICAO, C_ERRO.NRPEDIDO, C_ERRO.NRITEMPED, V_NRSEQ, 'ALMOX_NULL', C_ERRO.CDCLIENTE, C_ERRO.CDFILIALPED, C_ERRO.CDPRODUTO, C_ERRO.CDPRODESTO,  C_ERRO.QTITEMEXP, C_ERRO.QTITEMPROD, 0,
             C_ERRO.CDEMPRESA, C_ERRO.CDINSCESTA, C_ERRO.CDINSCMUNI, C_ERRO.CDSERIE, '', '');
         END LOOP;
   /*------------------ALMOX_NULL--------------*/
   /*------------------LOCAL_NULL--------------*/
         FOR C_ERRO IN (SELECT I.CDFILIAL, I.NREXPEDICAO, I.NRPEDIDO, I.NRITEMPED, I.CDPRODUTO, I.CDPRODMOVI, P.CDPRODESTO,
                               I.CDCLIENTE, I.CDFILIALPED, I.QTITEMEXP, I.QTITEMPROD,
                               I.CDEMPRESA, I.CDINSCESTA, I.CDINSCMUNI, I.CDSERIE
                          FROM ITEMEXPEDICAO I JOIN PRODUTO P
                                                 ON I.CDPRODMOVI = P.CDPRODUTO
                         WHERE I.CDFILIAL = P_CDFILIAL
                           AND I.NREXPEDICAO = P_NREXPEDICAO
                           AND I.CDTIPOOPER IS NOT NULL
                           AND I.IDATUESTTPOP = 'S'
                           AND I.IDCNTRESTOQ = 'S'
                           AND I.IDRESERVA = 'N'
                           AND I.IDITEMCOMPPROD = 'N'
                           AND ((V_IDUTILLCESTQ  = 'S') AND ((I.CDLOCALEXP IS NULL) OR (I.CDLOCALEXP = ' '))))
         LOOP
            UPDATE ITEMEXPEDICAO
               SET IDERRO = 'S'
             WHERE CDFILIAL = C_ERRO.CDFILIAL
               AND NREXPEDICAO = C_ERRO.NREXPEDICAO
               AND NRPEDIDO = C_ERRO.NRPEDIDO
               AND NRITEMPED = C_ERRO.NRITEMPED;
            SELECT NVL(MAX(NRSEQERROITEM),0)
              INTO V_NRSEQ
              FROM ERROITEMEXP
             WHERE CDFILIAL = C_ERRO.CDFILIAL
               AND NREXPEDICAO = C_ERRO.NREXPEDICAO
               AND NRPEDIDO = C_ERRO.NRPEDIDO;
            V_NRSEQ := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_NRSEQ) + 1)),10,0);
            INSERT INTO ERROITEMEXP
            (CDFILIAL, NREXPEDICAO, NRPEDIDO, NRITEMPED, NRSEQERROITEM, DSERRO, CDCLIENTE, CDFILIALPED, CDPRODUTO, CDPRODMOVI, QTITEMEXP, QTTOTAITEMPROD, QTTOTAESTOQ,
             CDEMPRESA, CDINSCESTA, CDINSCMUNI, CDSERIE, NRORDPROD, NRSUBORDPROD)
            VALUES
            (C_ERRO.CDFILIAL, C_ERRO.NREXPEDICAO, C_ERRO.NRPEDIDO, C_ERRO.NRITEMPED, V_NRSEQ, 'LOCAL_NULL', C_ERRO.CDCLIENTE, C_ERRO.CDFILIALPED, C_ERRO.CDPRODUTO, C_ERRO.CDPRODESTO, C_ERRO.QTITEMEXP, C_ERRO.QTITEMPROD, 0 ,
             C_ERRO.CDEMPRESA, C_ERRO.CDINSCESTA, C_ERRO.CDINSCMUNI, C_ERRO.CDSERIE, '', '');
         END LOOP;
         /*------------------LOCAL_NULL--------------*/
      ELSE
   /*------------------ALMOX_NULL--------------*/
         FOR C_ERRO IN (SELECT I.CDFILIAL, I.NREXPEDICAO, I.NRPEDIDO, I.NRITEMPED, I.CDPRODUTO, I.CDPRODMOVI, P.CDPRODESTO,
                               I.CDCLIENTE, I.CDFILIALPED, I.QTITEMEXP, I.QTITEMPROD,
                               I.CDEMPRESA, I.CDINSCESTA, I.CDINSCMUNI, I.CDSERIE
                          FROM ITEMEXPEDICAO I JOIN PRODUTO P
                                                 ON I.CDPRODMOVI = P.CDPRODUTO
                         WHERE I.CDFILIAL = P_CDFILIAL
                           AND I.NREXPEDICAO = P_NREXPEDICAO
                           AND I.CDTIPOOPER IS NOT NULL
                           AND I.IDATUESTTPOP = 'S'
                           AND I.IDCNTRESTOQ = 'S'
                           AND I.IDRESERVA = 'N'
                           AND ((V_IDUTILALMOX = 'S') AND ((I.CDALMOXEXP IS NULL) OR (I.CDALMOXEXP = ' '))))
         LOOP
            UPDATE ITEMEXPEDICAO
               SET IDERRO = 'S'
             WHERE CDFILIAL = C_ERRO.CDFILIAL
               AND NREXPEDICAO = C_ERRO.NREXPEDICAO
               AND NRPEDIDO = C_ERRO.NRPEDIDO
               AND NRITEMPED = C_ERRO.NRITEMPED;
            SELECT NVL(MAX(NRSEQERROITEM),0)
              INTO V_NRSEQ
              FROM ERROITEMEXP
             WHERE CDFILIAL = C_ERRO.CDFILIAL
               AND NREXPEDICAO = C_ERRO.NREXPEDICAO
               AND NRPEDIDO = C_ERRO.NRPEDIDO;
            V_NRSEQ := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_NRSEQ) + 1)),10,0);
            INSERT INTO ERROITEMEXP
            (CDFILIAL, NREXPEDICAO, NRPEDIDO, NRITEMPED, NRSEQERROITEM, DSERRO, CDCLIENTE, CDFILIALPED, CDPRODUTO, CDPRODMOVI, QTITEMEXP, QTTOTAITEMPROD, QTTOTAESTOQ,
             CDEMPRESA, CDINSCESTA, CDINSCMUNI, CDSERIE, NRORDPROD, NRSUBORDPROD)
            VALUES
            (C_ERRO.CDFILIAL, C_ERRO.NREXPEDICAO, C_ERRO.NRPEDIDO, C_ERRO.NRITEMPED, V_NRSEQ, 'ALMOX_NULL', C_ERRO.CDCLIENTE, C_ERRO.CDFILIALPED, C_ERRO.CDPRODUTO, C_ERRO.CDPRODESTO,  C_ERRO.QTITEMEXP, C_ERRO.QTITEMPROD, 0,
             C_ERRO.CDEMPRESA, C_ERRO.CDINSCESTA, C_ERRO.CDINSCMUNI, C_ERRO.CDSERIE, '', '');
         END LOOP;
   /*------------------ALMOX_NULL--------------*/
   /*------------------LOCAL_NULL--------------*/
         FOR C_ERRO IN (SELECT I.CDFILIAL, I.NREXPEDICAO, I.NRPEDIDO, I.NRITEMPED, I.CDPRODUTO, I.CDPRODMOVI, P.CDPRODESTO,
                               I.CDCLIENTE, I.CDFILIALPED, I.QTITEMEXP, I.QTITEMPROD,
                               I.CDEMPRESA, I.CDINSCESTA, I.CDINSCMUNI, I.CDSERIE
                          FROM ITEMEXPEDICAO I JOIN PRODUTO P
                                                 ON I.CDPRODMOVI = P.CDPRODUTO
                         WHERE I.CDFILIAL = P_CDFILIAL
                           AND I.NREXPEDICAO = P_NREXPEDICAO
                           AND I.CDTIPOOPER IS NOT NULL
                           AND I.IDATUESTTPOP = 'S'
                           AND I.IDCNTRESTOQ = 'S'
                           AND I.IDRESERVA = 'N'
                           AND ((V_IDUTILLCESTQ  = 'S') AND ((I.CDLOCALEXP IS NULL) OR (I.CDLOCALEXP = ' '))))
         LOOP
            UPDATE ITEMEXPEDICAO
               SET IDERRO = 'S'
             WHERE CDFILIAL = C_ERRO.CDFILIAL
               AND NREXPEDICAO = C_ERRO.NREXPEDICAO
               AND NRPEDIDO = C_ERRO.NRPEDIDO
               AND NRITEMPED = C_ERRO.NRITEMPED;
            SELECT NVL(MAX(NRSEQERROITEM),0)
              INTO V_NRSEQ
              FROM ERROITEMEXP
             WHERE CDFILIAL = C_ERRO.CDFILIAL
               AND NREXPEDICAO = C_ERRO.NREXPEDICAO
               AND NRPEDIDO = C_ERRO.NRPEDIDO;
            V_NRSEQ := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_NRSEQ) + 1)),10,0);
            INSERT INTO ERROITEMEXP
            (CDFILIAL, NREXPEDICAO, NRPEDIDO, NRITEMPED, NRSEQERROITEM, DSERRO, CDCLIENTE, CDFILIALPED, CDPRODUTO, CDPRODMOVI, QTITEMEXP, QTTOTAITEMPROD, QTTOTAESTOQ,
             CDEMPRESA, CDINSCESTA, CDINSCMUNI, CDSERIE, NRORDPROD, NRSUBORDPROD)
            VALUES
            (C_ERRO.CDFILIAL, C_ERRO.NREXPEDICAO, C_ERRO.NRPEDIDO, C_ERRO.NRITEMPED, V_NRSEQ, 'LOCAL_NULL', C_ERRO.CDCLIENTE, C_ERRO.CDFILIALPED, C_ERRO.CDPRODUTO, C_ERRO.CDPRODESTO, C_ERRO.QTITEMEXP, C_ERRO.QTITEMPROD, 0 ,
             C_ERRO.CDEMPRESA, C_ERRO.CDINSCESTA, C_ERRO.CDINSCMUNI, C_ERRO.CDSERIE, '', '');
         END LOOP;
      END IF;
/*------------------LOCAL_NULL--------------*/
   END IF;
/*------------------PRECO_PROD--------------*/
   FOR C_ERRO IN (SELECT I.CDFILIAL, I.NREXPEDICAO, I.NRPEDIDO, I.NRITEMPED, I.CDPRODUTO, I.CDPRODMOVI, P.CDPRODESTO,
                         I.CDCLIENTE, I.CDFILIALPED, I.QTITEMEXP, I.QTITEMPROD,
                         I.CDEMPRESA, I.CDINSCESTA, I.CDINSCMUNI, I.CDSERIE
                    FROM ITEMEXPEDICAO I JOIN PRODUTO P
                                           ON I.CDPRODMOVI = P.CDPRODUTO
                   WHERE I.CDFILIAL = P_CDFILIAL
                     AND I.NREXPEDICAO = P_NREXPEDICAO
                     AND I.VRPRECOEXP <= 0)
   LOOP
      UPDATE ITEMEXPEDICAO
         SET IDERRO = 'S'
       WHERE CDFILIAL = C_ERRO.CDFILIAL
         AND NREXPEDICAO = C_ERRO.NREXPEDICAO
         AND NRPEDIDO = C_ERRO.NRPEDIDO
         AND NRITEMPED = C_ERRO.NRITEMPED;
      SELECT NVL(MAX(NRSEQERROITEM),0)
        INTO V_NRSEQ
        FROM ERROITEMEXP
       WHERE CDFILIAL = C_ERRO.CDFILIAL
         AND NREXPEDICAO = C_ERRO.NREXPEDICAO
         AND NRPEDIDO = C_ERRO.NRPEDIDO;
      V_NRSEQ := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_NRSEQ) + 1)),10,0);
      V_ERRO := 0;
      SELECT COUNT(1)
        INTO V_ERRO
        FROM ERROITEMEXP
       WHERE CDFILIAL = C_ERRO.CDFILIAL
         AND NREXPEDICAO = C_ERRO.NREXPEDICAO
         AND NRPEDIDO = C_ERRO.NRPEDIDO
         AND NRITEMPED = C_ERRO.NRITEMPED
         AND DSERRO = 'PRECO_PROD';
      IF (V_ERRO = 0) THEN
         INSERT INTO ERROITEMEXP
         (CDFILIAL, NREXPEDICAO, NRPEDIDO, NRITEMPED, NRSEQERROITEM, DSERRO, CDCLIENTE, CDFILIALPED, CDPRODUTO, CDPRODMOVI, QTITEMEXP, QTTOTAITEMPROD, QTTOTAESTOQ,
          CDEMPRESA, CDINSCESTA, CDINSCMUNI, CDSERIE, NRORDPROD, NRSUBORDPROD)
         VALUES
         (C_ERRO.CDFILIAL, C_ERRO.NREXPEDICAO, C_ERRO.NRPEDIDO, C_ERRO.NRITEMPED, V_NRSEQ, 'PRECO_PROD', C_ERRO.CDCLIENTE, C_ERRO.CDFILIALPED, C_ERRO.CDPRODUTO, C_ERRO.CDPRODESTO, C_ERRO.QTITEMEXP, C_ERRO.QTITEMPROD, 0,
          C_ERRO.CDEMPRESA, C_ERRO.CDINSCESTA, C_ERRO.CDINSCMUNI, C_ERRO.CDSERIE, '', '');
      END IF;
   END LOOP;
/*------------------PRECO_PROD--------------*/
   IF ((V_IDTABPRECO = '2') AND (V_IDCALCPRECFAT = 'E')) THEN
/*------------------CUSTO_PROD--------------*/
      FOR C_ERRO IN (SELECT I.CDFILIAL, I.NREXPEDICAO, I.NRPEDIDO, I.NRITEMPED, I.CDPRODUTO, I.CDPRODMOVI, P.CDPRODESTO,
                            I.CDCLIENTE, I.CDFILIALPED, I.QTITEMEXP, I.QTITEMPROD,
                            I.CDEMPRESA, I.CDINSCESTA, I.CDINSCMUNI, I.CDSERIE
                       FROM ITEMEXPEDICAO I JOIN PRODUTO P
                                              ON I.CDPRODMOVI = P.CDPRODUTO
                      WHERE I.CDFILIAL = P_CDFILIAL
                        AND I.NREXPEDICAO = P_NREXPEDICAO
                        AND I.VRCUSTO <= 0)
      LOOP
         UPDATE ITEMEXPEDICAO
            SET IDERRO = 'S'
          WHERE CDFILIAL = C_ERRO.CDFILIAL
            AND NREXPEDICAO = C_ERRO.NREXPEDICAO
            AND NRPEDIDO = C_ERRO.NRPEDIDO
            AND NRITEMPED = C_ERRO.NRITEMPED;
         SELECT NVL(MAX(NRSEQERROITEM),0)
           INTO V_NRSEQ
           FROM ERROITEMEXP
          WHERE CDFILIAL = C_ERRO.CDFILIAL
            AND NREXPEDICAO = C_ERRO.NREXPEDICAO
            AND NRPEDIDO = C_ERRO.NRPEDIDO;
         V_NRSEQ := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_NRSEQ) + 1)),10,0);
         V_ERRO := 0;
         SELECT COUNT(1)
           INTO V_ERRO
           FROM ERROITEMEXP
          WHERE CDFILIAL = C_ERRO.CDFILIAL
            AND NREXPEDICAO = C_ERRO.NREXPEDICAO
            AND NRPEDIDO = C_ERRO.NRPEDIDO
            AND NRITEMPED = C_ERRO.NRITEMPED
            AND DSERRO = 'CUSTO_PROD';
         IF (V_ERRO = 0) THEN
            INSERT INTO ERROITEMEXP
            (CDFILIAL, NREXPEDICAO, NRPEDIDO, NRITEMPED, NRSEQERROITEM, DSERRO, CDCLIENTE, CDFILIALPED, CDPRODUTO, CDPRODMOVI, QTITEMEXP, QTTOTAITEMPROD, QTTOTAESTOQ,
             CDEMPRESA, CDINSCESTA, CDINSCMUNI, CDSERIE, NRORDPROD, NRSUBORDPROD)
            VALUES
            (C_ERRO.CDFILIAL, C_ERRO.NREXPEDICAO, C_ERRO.NRPEDIDO, C_ERRO.NRITEMPED, V_NRSEQ, 'CUSTO_PROD', C_ERRO.CDCLIENTE, C_ERRO.CDFILIALPED, C_ERRO.CDPRODUTO, C_ERRO.CDPRODESTO, C_ERRO.QTITEMEXP, C_ERRO.QTITEMPROD, 0,
             C_ERRO.CDEMPRESA, C_ERRO.CDINSCESTA, C_ERRO.CDINSCMUNI, C_ERRO.CDSERIE, '' , '');
         END IF;
      END LOOP;
/*------------------CUSTO_PROD--------------*/
/*------------------VRFATPRECO--------------*/
      FOR C_ERRO IN (SELECT I.CDFILIAL, I.NREXPEDICAO, I.NRPEDIDO, I.NRITEMPED, I.CDPRODUTO, I.CDPRODMOVI, P.CDPRODESTO,
                            I.CDCLIENTE, I.CDFILIALPED, I.QTITEMEXP, I.QTITEMPROD,
                            I.CDEMPRESA, I.CDINSCESTA, I.CDINSCMUNI, I.CDSERIE
                       FROM ITEMEXPEDICAO I JOIN PRODUTO P
                                              ON I.CDPRODMOVI = P.CDPRODUTO
                      WHERE I.CDFILIAL = P_CDFILIAL
                        AND I.NREXPEDICAO = P_NREXPEDICAO
                        AND I.VRFATPRECO <= 0)
      LOOP
         UPDATE ITEMEXPEDICAO
            SET IDERRO = 'S'
          WHERE CDFILIAL = C_ERRO.CDFILIAL
            AND NREXPEDICAO = C_ERRO.NREXPEDICAO
            AND NRPEDIDO = C_ERRO.NRPEDIDO
            AND NRITEMPED = C_ERRO.NRITEMPED;
         SELECT NVL(MAX(NRSEQERROITEM),0)
           INTO V_NRSEQ
           FROM ERROITEMEXP
          WHERE CDFILIAL = C_ERRO.CDFILIAL
            AND NREXPEDICAO = C_ERRO.NREXPEDICAO
            AND NRPEDIDO = C_ERRO.NRPEDIDO;
         V_NRSEQ := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_NRSEQ) + 1)),10,0);
         V_ERRO := 0;
         SELECT COUNT(1)
           INTO V_ERRO
           FROM ERROITEMEXP
          WHERE CDFILIAL = C_ERRO.CDFILIAL
            AND NREXPEDICAO = C_ERRO.NREXPEDICAO
            AND NRPEDIDO = C_ERRO.NRPEDIDO
            AND NRITEMPED = C_ERRO.NRITEMPED
            AND DSERRO = 'VRFATPRECO';
         IF (V_ERRO = 0) THEN
            INSERT INTO ERROITEMEXP
            (CDFILIAL, NREXPEDICAO, NRPEDIDO, NRITEMPED, NRSEQERROITEM, DSERRO, CDCLIENTE, CDFILIALPED, CDPRODUTO, CDPRODMOVI, QTITEMEXP, QTTOTAITEMPROD, QTTOTAESTOQ,
             CDEMPRESA, CDINSCESTA, CDINSCMUNI, CDSERIE, NRORDPROD, NRSUBORDPROD)
            VALUES
            (C_ERRO.CDFILIAL, C_ERRO.NREXPEDICAO, C_ERRO.NRPEDIDO, C_ERRO.NRITEMPED, V_NRSEQ, 'VRFATPRECO', C_ERRO.CDCLIENTE, C_ERRO.CDFILIALPED, C_ERRO.CDPRODUTO, C_ERRO.CDPRODESTO, C_ERRO.QTITEMEXP, C_ERRO.QTITEMPROD, 0 ,
             C_ERRO.CDEMPRESA, C_ERRO.CDINSCESTA, C_ERRO.CDINSCMUNI, C_ERRO.CDSERIE, '', '');
         END IF;
      END LOOP;
/*------------------VRFATPRECO--------------*/
   END IF;
/*------------------QTDEAEXP--------------*/
   FOR C_ERRO IN (SELECT I.CDFILIAL, I.NREXPEDICAO, I.NRPEDIDO, I.NRITEMPED, I.CDPRODUTO, I.CDPRODMOVI, P.CDPRODESTO,
                         I.CDCLIENTE, I.CDFILIALPED, I.QTITEMEXP, I.QTITEMPROD,
                         I.CDEMPRESA, I.CDINSCESTA, I.CDINSCMUNI, I.CDSERIE
                    FROM ITEMEXPEDICAO I JOIN PRODUTO P
                                           ON I.CDPRODMOVI = P.CDPRODUTO
                   WHERE I.CDFILIAL = P_CDFILIAL
                     AND I.NREXPEDICAO = P_NREXPEDICAO
                     AND QTITEMEXP < 0.001)
   LOOP
      UPDATE ITEMEXPEDICAO
         SET IDERRO = 'S'
       WHERE CDFILIAL = C_ERRO.CDFILIAL
         AND NREXPEDICAO = C_ERRO.NREXPEDICAO
         AND NRPEDIDO = C_ERRO.NRPEDIDO
         AND NRITEMPED = C_ERRO.NRITEMPED;
      SELECT NVL(MAX(NRSEQERROITEM),0)
        INTO V_NRSEQ
        FROM ERROITEMEXP
       WHERE CDFILIAL = C_ERRO.CDFILIAL
         AND NREXPEDICAO = C_ERRO.NREXPEDICAO
         AND NRPEDIDO = C_ERRO.NRPEDIDO;
      V_NRSEQ := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_NRSEQ) + 1)),10,0);
      V_ERRO := 0;
      SELECT COUNT(1)
        INTO V_ERRO
        FROM ERROITEMEXP
       WHERE CDFILIAL = C_ERRO.CDFILIAL
         AND NREXPEDICAO = C_ERRO.NREXPEDICAO
         AND NRPEDIDO = C_ERRO.NRPEDIDO
         AND NRITEMPED = C_ERRO.NRITEMPED
         AND DSERRO = 'QTDEAEXP';
      IF (V_ERRO = 0) THEN
         INSERT INTO ERROITEMEXP
         (CDFILIAL, NREXPEDICAO, NRPEDIDO, NRITEMPED, NRSEQERROITEM, DSERRO, CDCLIENTE, CDFILIALPED, CDPRODUTO, CDPRODMOVI, QTITEMEXP, QTTOTAITEMPROD, QTTOTAESTOQ,
          CDEMPRESA, CDINSCESTA, CDINSCMUNI, CDSERIE, NRORDPROD, NRSUBORDPROD)
         VALUES
         (C_ERRO.CDFILIAL, C_ERRO.NREXPEDICAO, C_ERRO.NRPEDIDO, C_ERRO.NRITEMPED, V_NRSEQ, 'QTDEAEXP', C_ERRO.CDCLIENTE, C_ERRO.CDFILIALPED, C_ERRO.CDPRODUTO, C_ERRO.CDPRODESTO, C_ERRO.QTITEMEXP, C_ERRO.QTITEMPROD, 0 ,
          C_ERRO.CDEMPRESA, C_ERRO.CDINSCESTA, C_ERRO.CDINSCMUNI, C_ERRO.CDSERIE, '', '');
      END IF;
   END LOOP;
/*------------------QTDEAEXP--------------*/
/*------------------QTMAXEXP--------------*/
   FOR C_ERRO IN (SELECT I.CDFILIAL, I.NREXPEDICAO, I.NRPEDIDO, I.NRITEMPED, I.CDPRODUTO, I.CDPRODMOVI, P.CDPRODESTO,
                         I.CDCLIENTE, I.CDFILIALPED, I.QTITEMEXP, I.QTITEMPROD,
                         I.CDEMPRESA, I.CDINSCESTA, I.CDINSCMUNI, I.CDSERIE
                    FROM ITEMEXPEDICAO I JOIN PRODUTO P
                                           ON I.CDPRODMOVI = P.CDPRODUTO
                   WHERE I.CDFILIAL = P_CDFILIAL
                     AND I.NREXPEDICAO = P_NREXPEDICAO
                     AND I.QTITEMEXP > I.QTMAXEXP)
   LOOP
      UPDATE ITEMEXPEDICAO
         SET IDERRO = 'S'
       WHERE CDFILIAL = C_ERRO.CDFILIAL
         AND NREXPEDICAO = C_ERRO.NREXPEDICAO
         AND NRPEDIDO = C_ERRO.NRPEDIDO
         AND NRITEMPED = C_ERRO.NRITEMPED;
      SELECT NVL(MAX(NRSEQERROITEM),0)
        INTO V_NRSEQ
        FROM ERROITEMEXP
       WHERE CDFILIAL = C_ERRO.CDFILIAL
         AND NREXPEDICAO = C_ERRO.NREXPEDICAO
         AND NRPEDIDO = C_ERRO.NRPEDIDO;
      V_NRSEQ := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_NRSEQ) + 1)),10,0);
      V_ERRO := 0;
      SELECT COUNT(1)
        INTO V_ERRO
        FROM ERROITEMEXP
       WHERE CDFILIAL = C_ERRO.CDFILIAL
         AND NREXPEDICAO = C_ERRO.NREXPEDICAO
         AND NRPEDIDO = C_ERRO.NRPEDIDO
         AND NRITEMPED = C_ERRO.NRITEMPED
         AND DSERRO = 'QTMAXEXP';
      IF (V_ERRO = 0) THEN
         INSERT INTO ERROITEMEXP
         (CDFILIAL, NREXPEDICAO, NRPEDIDO, NRITEMPED, NRSEQERROITEM, DSERRO, CDCLIENTE, CDFILIALPED, CDPRODUTO, CDPRODMOVI, QTITEMEXP, QTTOTAITEMPROD, QTTOTAESTOQ,
          CDEMPRESA, CDINSCESTA, CDINSCMUNI, CDSERIE, NRORDPROD, NRSUBORDPROD)
         VALUES
         (C_ERRO.CDFILIAL, C_ERRO.NREXPEDICAO, C_ERRO.NRPEDIDO, C_ERRO.NRITEMPED, V_NRSEQ, 'QTMAXEXP', C_ERRO.CDCLIENTE, C_ERRO.CDFILIALPED, C_ERRO.CDPRODUTO, C_ERRO.CDPRODESTO, C_ERRO.QTITEMEXP, C_ERRO.QTITEMPROD, 0 ,
          C_ERRO.CDEMPRESA, C_ERRO.CDINSCESTA, C_ERRO.CDINSCMUNI, C_ERRO.CDSERIE, '', '');
      END IF;
   END LOOP;
/*------------------QTMAXEXP--------------*/
/*------------------ITEMALTERADO--------------*/
   FOR C_ERRO IN (SELECT IE.CDFILIAL, IE.NREXPEDICAO, IE.NRPEDIDO, IE.NRITEMPED, IE.CDPRODUTO, IE.CDPRODMOVI, P.CDPRODESTO,
                         IE.CDCLIENTE, IE.CDFILIALPED, IE.QTITEMEXP, IE.QTITEMPROD,
                         IE.CDEMPRESA, IE.CDINSCESTA, IE.CDINSCMUNI, IE.CDSERIE
                    FROM ITEMEXPEDICAO IE JOIN ITEMPEDIDO IP
                                            ON IE.CDFILIAL = IP.CDFILIAL
                                           AND IE.NRPEDIDO = IP.NRPEDIDO
                                           AND IE.NRITEMPED = IP.NRITEMPED
                                          JOIN PRODUTO P
                                            ON IE.CDPRODMOVI = P.CDPRODUTO
                   WHERE IE.CDFILIAL = P_CDFILIAL
                     AND IE.NREXPEDICAO = P_NREXPEDICAO
                     AND ((IE.IDSITITEMPED <> IP.IDSITITEMPED) OR (IE.QTEXPEDIDA > IP.QTEXPITEMPED)))
   LOOP
      UPDATE ITEMEXPEDICAO
         SET IDERRO = 'S'
       WHERE CDFILIAL = C_ERRO.CDFILIAL
         AND NREXPEDICAO = C_ERRO.NREXPEDICAO
         AND NRPEDIDO = C_ERRO.NRPEDIDO
         AND NRITEMPED = C_ERRO.NRITEMPED;
      SELECT NVL(MAX(NRSEQERROITEM),0)
        INTO V_NRSEQ
        FROM ERROITEMEXP
       WHERE CDFILIAL = C_ERRO.CDFILIAL
         AND NREXPEDICAO = C_ERRO.NREXPEDICAO
         AND NRPEDIDO = C_ERRO.NRPEDIDO;
      V_NRSEQ := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_NRSEQ) + 1)),10,0);
      V_ERRO := 0;
      SELECT COUNT(1)
        INTO V_ERRO
        FROM ERROITEMEXP
       WHERE CDFILIAL = C_ERRO.CDFILIAL
         AND NREXPEDICAO = C_ERRO.NREXPEDICAO
         AND NRPEDIDO = C_ERRO.NRPEDIDO
         AND NRITEMPED = C_ERRO.NRITEMPED
         AND DSERRO = 'ITEMALTERADO';
      IF (V_ERRO = 0) THEN
         INSERT INTO ERROITEMEXP
         (CDFILIAL, NREXPEDICAO, NRPEDIDO, NRITEMPED, NRSEQERROITEM, DSERRO, CDCLIENTE, CDFILIALPED, CDPRODUTO, CDPRODMOVI, QTITEMEXP, QTTOTAITEMPROD, QTTOTAESTOQ,
          CDEMPRESA, CDINSCESTA, CDINSCMUNI, CDSERIE, NRORDPROD, NRSUBORDPROD)
         VALUES
         (C_ERRO.CDFILIAL, C_ERRO.NREXPEDICAO, C_ERRO.NRPEDIDO, C_ERRO.NRITEMPED, V_NRSEQ, 'ITEMALTERADO', C_ERRO.CDCLIENTE, C_ERRO.CDFILIALPED, C_ERRO.CDPRODUTO, C_ERRO.CDPRODESTO, C_ERRO.QTITEMEXP, C_ERRO.QTITEMPROD, 0 ,
          C_ERRO.CDEMPRESA, C_ERRO.CDINSCESTA, C_ERRO.CDINSCMUNI, C_ERRO.CDSERIE, '', '');
      END IF;
   END LOOP;
/*------------------ITEMALTERADO--------------*/
/*------------------PEDBLOQ--------------*/
   FOR C_ERRO IN (SELECT IE.CDFILIAL, IE.NREXPEDICAO, IE.NRPEDIDO, IE.NRITEMPED, IE.CDPRODUTO, IE.CDPRODMOVI, P.CDPRODESTO,
                         IE.CDCLIENTE, IE.CDFILIALPED, IE.QTITEMEXP, IE.QTITEMPROD, IE.CDEMPRESA, IE.CDINSCESTA, IE.CDINSCMUNI, IE.CDSERIE
                    FROM ITEMEXPEDICAO IE JOIN ITEMPEDIDO IP
                                            ON IE.CDFILIAL = IP.CDFILIAL
                                           AND IE.NRPEDIDO = IP.NRPEDIDO
                                           AND IE.NRITEMPED = IP.NRITEMPED
                                          JOIN PRODUTO P
                                            ON IE.CDPRODMOVI = P.CDPRODUTO
                   WHERE IE.CDFILIAL = P_CDFILIAL
                     AND IE.NREXPEDICAO = P_NREXPEDICAO
                     AND IP.IDSITITEMPED IN ('1', '2', '5', '6'))
   LOOP
      UPDATE ITEMEXPEDICAO
         SET IDERRO = 'S'
       WHERE CDFILIAL = C_ERRO.CDFILIAL
         AND NREXPEDICAO = C_ERRO.NREXPEDICAO
         AND NRPEDIDO = C_ERRO.NRPEDIDO
         AND NRITEMPED = C_ERRO.NRITEMPED;
      SELECT NVL(MAX(NRSEQERROITEM),0)
        INTO V_NRSEQ
        FROM ERROITEMEXP
       WHERE CDFILIAL = C_ERRO.CDFILIAL
         AND NREXPEDICAO = C_ERRO.NREXPEDICAO
         AND NRPEDIDO = C_ERRO.NRPEDIDO;
      V_NRSEQ := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_NRSEQ) + 1)),10,0);
      V_ERRO := 0;
      SELECT COUNT(1)
        INTO V_ERRO
        FROM ERROITEMEXP
       WHERE CDFILIAL = C_ERRO.CDFILIAL
         AND NREXPEDICAO = C_ERRO.NREXPEDICAO
         AND NRPEDIDO = C_ERRO.NRPEDIDO
         AND NRITEMPED = C_ERRO.NRITEMPED
         AND DSERRO = 'PEDBLOQ';
      IF (V_ERRO = 0) THEN
         INSERT INTO ERROITEMEXP
         (CDFILIAL, NREXPEDICAO, NRPEDIDO, NRITEMPED, NRSEQERROITEM, DSERRO, CDCLIENTE, CDFILIALPED, CDPRODUTO, CDPRODMOVI, QTITEMEXP, QTTOTAITEMPROD, QTTOTAESTOQ,
          CDEMPRESA, CDINSCESTA, CDINSCMUNI, CDSERIE, NRORDPROD, NRSUBORDPROD)
         VALUES
         (C_ERRO.CDFILIAL, C_ERRO.NREXPEDICAO, C_ERRO.NRPEDIDO, C_ERRO.NRITEMPED, V_NRSEQ, 'PEDBLOQ', C_ERRO.CDCLIENTE, C_ERRO.CDFILIALPED, C_ERRO.CDPRODUTO, C_ERRO.CDPRODESTO, C_ERRO.QTITEMEXP, C_ERRO.QTITEMPROD, 0,
          C_ERRO.CDEMPRESA, C_ERRO.CDINSCESTA, C_ERRO.CDINSCMUNI, C_ERRO.CDSERIE, '', '');
      END IF;
   END LOOP;
/*------------------PEDBLOQ--------------*/
/*------------------RESERVA_DIF--------------*/
   BEGIN
      SELECT NVL(MAX(IDUTRESELOTE), 'N')
        INTO V_IDUTRESELOTE
        FROM PARAMPEDIDO
       WHERE CDFILIAL = P_CDFILIAL;
   EXCEPTION
      WHEN NO_DATA_FOUND THEN BEGIN
         V_IDUTRESELOTE := 'N';
      END;
   END;

   IF V_IDUTRESELOTE = 'S' THEN
      FOR C_ERRO IN (SELECT I.CDFILIAL, I.NREXPEDICAO, I.NRPEDIDO, I.NRITEMPED, I.CDPRODUTO, I.CDPRODMOVI, P.CDPRODESTO, (I.QTITEMEXP*I.VRFATOCONV) QTITEMEXPEST
                       FROM ITEMEXPEDICAO I JOIN PRODUTO P
                                              ON I.CDPRODMOVI = P.CDPRODUTO
                      WHERE I.CDFILIAL = P_CDFILIAL
                        AND I.NREXPEDICAO = P_NREXPEDICAO
                     GROUP BY I.CDFILIAL, I.NREXPEDICAO, I.NRPEDIDO, I.NRITEMPED, I.CDPRODUTO, I.CDPRODMOVI, P.CDPRODESTO, (I.QTITEMEXP*I.VRFATOCONV))
      LOOP
         BEGIN
            SELECT NVL(SUM(QTRESERVADA), 0)
              INTO V_QTRESERVTOT
              FROM ITEMEXPEDICAO
             WHERE CDFILIAL = P_CDFILIAL
               AND NREXPEDICAO = P_NREXPEDICAO
               AND NRPEDIDO = C_ERRO.NRPEDIDO
               AND NRITEMPED = C_ERRO.NRITEMPED
               AND IDRESERVA = 'S'
               AND (((NRLOTEEXP <> ' ') AND (IDUTCTRLLOTE = 'S')) OR ((IDUTCTRLLOTE = 'N') AND (NRLOTEEXP = ' ')));
         EXCEPTION
            WHEN NO_DATA_FOUND THEN BEGIN
               V_QTRESERVTOT := 0;
            END;
         END;
         IF (ROUND(C_ERRO.QTITEMEXPEST,5) <> ROUND(V_QTRESERVTOT,5)) THEN
            UPDATE ITEMEXPEDICAO
               SET IDERRO = 'S'
             WHERE CDFILIAL = C_ERRO.CDFILIAL
               AND NREXPEDICAO = C_ERRO.NREXPEDICAO
               AND NRPEDIDO = C_ERRO.NRPEDIDO
               AND NRITEMPED = C_ERRO.NRITEMPED;
            SELECT NVL(MAX(NRSEQERROITEM),0)
              INTO V_NRSEQ
              FROM ERROITEMEXP
             WHERE CDFILIAL = C_ERRO.CDFILIAL
               AND NREXPEDICAO = C_ERRO.NREXPEDICAO
               AND NRPEDIDO = C_ERRO.NRPEDIDO;
            V_NRSEQ := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_NRSEQ) + 1)),10,0);
            V_ERRO := 0;
            SELECT COUNT(1)
              INTO V_ERRO
              FROM ERROITEMEXP
             WHERE CDFILIAL = C_ERRO.CDFILIAL
               AND NREXPEDICAO = C_ERRO.NREXPEDICAO
               AND NRPEDIDO = C_ERRO.NRPEDIDO
               AND NRITEMPED = C_ERRO.NRITEMPED
               AND DSERRO = 'RESERVA_DIF';
            IF (V_ERRO = 0) THEN
               INSERT INTO ERROITEMEXP
               (CDFILIAL, NREXPEDICAO, NRPEDIDO, NRITEMPED, NRSEQERROITEM, DSERRO, CDPRODUTO, CDPRODMOVI,
                CDEMPRESA, CDINSCESTA, CDINSCMUNI, CDSERIE, NRORDPROD, NRSUBORDPROD)
               VALUES
               (C_ERRO.CDFILIAL, C_ERRO.NREXPEDICAO, C_ERRO.NRPEDIDO, C_ERRO.NRITEMPED, V_NRSEQ, 'RESERVA_DIF', C_ERRO.CDPRODUTO, C_ERRO.CDPRODESTO,
                '', '', '', '', '', '');
            END IF;
         END IF;
      END LOOP;
   END IF;
/*------------------RESERVA_DIF--------------*/
/*------------------ITEMSESTOQ--------------*/
   IF (V_IDRETPROEXP = '2') THEN
      V_QTEXPEDIDA := 0;
      FOR C_ERRO IN (SELECT I.CDFILIAL, I.NREXPEDICAO, I.CDPRODUTO, P.CDPRODESTO, P.VRFATOCONV, C.IDSUBPRCTRLLOTE IDUTCTRLLOTE, C.IDSUBPRUTALMOX IDUTILALMOX,
                            C.CDSUBPRALMOX CDALMOXEXP, C.CDSUBPRLOCAL CDLOCALEXP, I.CDCLIENTE, I.CDFILIALPED, SUM(C.QTSUBPROD) AS QTEXPEDIDA
                       FROM ITEMEXPEDICAO I JOIN COMPITEMEXP C
                                              ON I.CDFILIAL = C.CDFILIAL
                                             AND I.NREXPEDICAO = C.NREXPEDICAO
                                             AND I.NRPEDIDO = C.NRPEDIDO
                                             AND I.NRITEMPED = C.NRITEMPED
                                            JOIN PRODUTO P
                                              ON C.CDSUBPROD = P.CDPRODUTO
                      WHERE I.CDFILIAL = P_CDFILIAL
                        AND I.NREXPEDICAO = P_NREXPEDICAO
                        AND I.IDRESERVA = 'N'
                        AND (V_IDCTRLESTQ = 'S')
                        AND (I.IDATUESTTPOP = 'S')
                        AND I.IDITEMCOMPPROD = 'S'
                        AND (C.IDSUBPRCTRLEST = 'S')
                     GROUP BY I.CDFILIAL, I.NREXPEDICAO, I.CDPRODUTO, P.CDPRODESTO, P.VRFATOCONV, C.IDSUBPRCTRLLOTE, C.IDSUBPRUTALMOX,
                              C.CDSUBPRALMOX, C.CDSUBPRLOCAL, I.CDCLIENTE, I.CDFILIALPED)
      LOOP
         V_QTEXPEDIDA := C_ERRO.QTEXPEDIDA;
         V_TOTALESTOQUE := 0;
         FOR C_ESTOQ IN (SELECT P.CDFILIAL, P.CDALMOXARIFE, P.CDLOCALESTOQ, SUM(P.QTTOTALESTOQ) AS QTTOTALESTOQ
                           FROM POSESTATU P LEFT JOIN LOTE L
                                                   ON P.CDFILIAL = L.CDFILIAL
                                                  AND P.CDPRODUTO = L.CDPRODUTO
                                                  AND P.NRLOTEESTQ = L.NRLOTEESTQ
                                                  AND P.NRSUBLOTE = L.NRSUBLOTE
                                            JOIN LOCALESTOQUE LE
                                              ON P.CDFILIAL = LE.CDFILIAL
                                             AND P.CDALMOXARIFE = LE.CDALMOXARIFE
                                             AND P.CDLOCALESTOQ = LE.CDLOCALESTOQ
                          WHERE P.CDFILIAL = C_ERRO.CDFILIAL
                            AND P.CDPRODUTO = C_ERRO.CDPRODESTO
                            AND P.CDALMOXARIFE = C_ERRO.CDALMOXEXP
                            AND P.CDLOCALESTOQ = C_ERRO.CDLOCALEXP
                            AND ((C_ERRO.IDUTCTRLLOTE = 'S') AND (P.NRLOTEESTQ <> ' ') AND (TRUNC(L.DTVALILOTE) >= TRUNC(SYSDATE)) OR ((C_ERRO.IDUTCTRLLOTE = 'N') AND (P.NRLOTEESTQ = ' ')))
                            AND ((C_ERRO.IDUTILALMOX = 'N') OR (LE.IDDISPCONPED = 'S'))
                         GROUP BY P.CDFILIAL, P.CDALMOXARIFE, P.CDLOCALESTOQ)
         LOOP
            V_TOTALESTOQUE := V_TOTALESTOQUE + C_ESTOQ.QTTOTALESTOQ;
         END LOOP;
         IF (ROUND(V_QTEXPEDIDA, 5) > ROUND(V_TOTALESTOQUE, 5)) THEN
            FOR C_ITEMPEDSESTOQ IN (SELECT CDFILIAL, NREXPEDICAO, NRPEDIDO, NRITEMPED, CDPRODMOVI, QTITEMEXP
                                      FROM ITEMEXPEDICAO
                                     WHERE CDFILIAL = C_ERRO.CDFILIAL
                                       AND NREXPEDICAO = C_ERRO.NREXPEDICAO
                                       AND IDITEMCOMPPROD = 'S'
                                       AND CDPRODUTO = C_ERRO.CDPRODUTO)
            LOOP
               UPDATE ITEMEXPEDICAO
                  SET IDERRO = 'S'
                WHERE CDFILIAL = C_ITEMPEDSESTOQ.CDFILIAL
                  AND NREXPEDICAO = C_ITEMPEDSESTOQ.NREXPEDICAO
                  AND NRPEDIDO = C_ITEMPEDSESTOQ.NRPEDIDO
                  AND NRITEMPED = C_ITEMPEDSESTOQ.NRITEMPED;
               SELECT NVL(MAX(NRSEQERROITEM),0)
                 INTO V_NRSEQ
                 FROM ERROITEMEXP
                WHERE CDFILIAL = C_ITEMPEDSESTOQ.CDFILIAL
                  AND NREXPEDICAO = C_ITEMPEDSESTOQ.NREXPEDICAO
                  AND NRPEDIDO = C_ITEMPEDSESTOQ.NRPEDIDO;
               V_NRSEQ := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_NRSEQ) + 1)),10,0);
               V_ERRO := 0;
               INSERT INTO ERROITEMEXP
               (CDFILIAL, NREXPEDICAO, NRPEDIDO, NRITEMPED, NRSEQERROITEM, DSERRO, CDCLIENTE, CDFILIALPED, CDPRODUTO, CDPRODMOVI, QTITEMEXP, QTTOTAITEMPROD, QTTOTAESTOQ,
                CDEMPRESA, CDINSCESTA, CDINSCMUNI, CDSERIE, NRORDPROD, NRSUBORDPROD)
               VALUES
               (C_ITEMPEDSESTOQ.CDFILIAL, C_ITEMPEDSESTOQ.NREXPEDICAO, C_ITEMPEDSESTOQ.NRPEDIDO, C_ITEMPEDSESTOQ.NRITEMPED, V_NRSEQ, 'ITEMSESTOQ', C_ERRO.CDCLIENTE, C_ERRO.CDFILIALPED, C_ERRO.CDPRODUTO, C_ERRO.CDPRODESTO, C_ITEMPEDSESTOQ.QTITEMEXP, V_QTEXPEDIDA, V_TOTALESTOQUE,
                '', '', '', '', '', '');
            END LOOP;
         END IF;
      END LOOP;
      V_QTEXPEDIDA := 0;
      FOR C_ERRO IN (SELECT I.CDFILIAL, I.NREXPEDICAO, I.CDPRODUTO, I.CDPRODMOVI, P.CDPRODESTO, P.VRFATOCONV, I.IDUTCTRLLOTE, I.IDUTILALMOX,
                            I.CDALMOXEXP, I.CDLOCALEXP, CDCLIENTE, CDFILIALPED, SUM(I.QTITEMPROD) AS QTEXPEDIDA
                       FROM ITEMEXPEDICAO I JOIN PRODUTO P
                                              ON I.CDPRODMOVI = P.CDPRODUTO
                      WHERE I.CDFILIAL = P_CDFILIAL
                        AND I.NREXPEDICAO = P_NREXPEDICAO
                        AND I.IDRESERVA = 'N'
                        AND (V_IDCTRLESTQ = 'S')
                        AND (I.IDATUESTTPOP = 'S')
                        AND (I.IDCNTRESTOQ = 'S')
                        AND (I.IDITEMCOMPPROD = 'N')
                     GROUP BY I.CDFILIAL, I.NREXPEDICAO, I.CDPRODUTO, I.CDPRODMOVI, P.CDPRODESTO, P.VRFATOCONV, I.IDUTCTRLLOTE, I.IDUTILALMOX, I.CDALMOXEXP, I.CDLOCALEXP, CDCLIENTE, CDFILIALPED)
      LOOP
         V_QTEXPEDIDA := C_ERRO.QTEXPEDIDA * C_ERRO.VRFATOCONV;
         V_TOTALESTOQUE := 0;
         FOR C_ESTOQ IN (SELECT P.CDFILIAL, P.CDALMOXARIFE, P.CDLOCALESTOQ, SUM(P.QTTOTALESTOQ) AS QTTOTALESTOQ
                           FROM POSESTATU P LEFT JOIN LOTE L
                                                   ON P.CDFILIAL = L.CDFILIAL
                                                  AND P.CDPRODUTO = L.CDPRODUTO
                                                  AND P.NRLOTEESTQ = L.NRLOTEESTQ
                                                  AND P.NRSUBLOTE = L.NRSUBLOTE
                                            JOIN LOCALESTOQUE LE
                                              ON P.CDFILIAL = LE.CDFILIAL
                                             AND P.CDALMOXARIFE = LE.CDALMOXARIFE
                                             AND P.CDLOCALESTOQ = LE.CDLOCALESTOQ
                          WHERE P.CDFILIAL = C_ERRO.CDFILIAL
                            AND P.CDPRODUTO = C_ERRO.CDPRODESTO
                            AND P.CDALMOXARIFE = C_ERRO.CDALMOXEXP
                            AND P.CDLOCALESTOQ = C_ERRO.CDLOCALEXP
                            AND ((C_ERRO.IDUTCTRLLOTE = 'S') AND (P.NRLOTEESTQ <> ' ') AND (TRUNC(L.DTVALILOTE) >= TRUNC(SYSDATE)) OR ((C_ERRO.IDUTCTRLLOTE = 'N') AND (P.NRLOTEESTQ = ' ')))
                            AND ((C_ERRO.IDUTILALMOX = 'N') OR (LE.IDDISPCONPED = 'S'))
                         GROUP BY P.CDFILIAL, P.CDALMOXARIFE, P.CDLOCALESTOQ)
         LOOP
            V_TOTALESTOQUE := V_TOTALESTOQUE + C_ESTOQ.QTTOTALESTOQ;
         END LOOP;
         IF (ROUND(V_QTEXPEDIDA, 5) > ROUND(V_TOTALESTOQUE, 5)) THEN
            FOR C_ITEMPEDSESTOQ IN (SELECT CDFILIAL, NREXPEDICAO, NRPEDIDO, NRITEMPED, CDPRODMOVI, QTITEMEXP
                                      FROM ITEMEXPEDICAO
                                     WHERE CDFILIAL = C_ERRO.CDFILIAL
                                       AND NREXPEDICAO = C_ERRO.NREXPEDICAO
                                       AND IDITEMCOMPPROD = 'N'
                                       AND CDPRODMOVI = C_ERRO.CDPRODMOVI)
            LOOP
               UPDATE ITEMEXPEDICAO
                  SET IDERRO = 'S'
                WHERE CDFILIAL = C_ITEMPEDSESTOQ.CDFILIAL
                  AND NREXPEDICAO = C_ITEMPEDSESTOQ.NREXPEDICAO
                  AND NRPEDIDO = C_ITEMPEDSESTOQ.NRPEDIDO
                  AND NRITEMPED = C_ITEMPEDSESTOQ.NRITEMPED;
               SELECT NVL(MAX(NRSEQERROITEM),0)
                 INTO V_NRSEQ
                 FROM ERROITEMEXP
                WHERE CDFILIAL = C_ITEMPEDSESTOQ.CDFILIAL
                  AND NREXPEDICAO = C_ITEMPEDSESTOQ.NREXPEDICAO
                  AND NRPEDIDO = C_ITEMPEDSESTOQ.NRPEDIDO;
               V_NRSEQ := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_NRSEQ) + 1)),10,0);
               V_ERRO := 0;
               INSERT INTO ERROITEMEXP
               (CDFILIAL, NREXPEDICAO, NRPEDIDO, NRITEMPED, NRSEQERROITEM, DSERRO, CDCLIENTE, CDFILIALPED, CDPRODUTO, CDPRODMOVI, QTITEMEXP, QTTOTAITEMPROD, QTTOTAESTOQ,
                CDEMPRESA, CDINSCESTA, CDINSCMUNI, CDSERIE, NRORDPROD, NRSUBORDPROD)
               VALUES
               (C_ITEMPEDSESTOQ.CDFILIAL, C_ITEMPEDSESTOQ.NREXPEDICAO, C_ITEMPEDSESTOQ.NRPEDIDO, C_ITEMPEDSESTOQ.NRITEMPED, V_NRSEQ, 'ITEMSESTOQ', C_ERRO.CDCLIENTE, C_ERRO.CDFILIALPED, C_ERRO.CDPRODUTO, C_ERRO.CDPRODESTO, C_ITEMPEDSESTOQ.QTITEMEXP, V_QTEXPEDIDA, V_TOTALESTOQUE,
                '', '', '', '', '', '');
            END LOOP;
         END IF;
      END LOOP;
   ELSE
      V_QTEXPEDIDA := 0;
      FOR C_ERRO IN (SELECT I.CDFILIAL, I.NREXPEDICAO, I.CDPRODUTO, I.CDPRODMOVI, P.CDPRODESTO, P.VRFATOCONV, I.IDUTCTRLLOTE, I.IDUTILALMOX,
                            I.CDALMOXEXP, I.CDLOCALEXP, CDCLIENTE, CDFILIALPED, SUM(I.QTITEMPROD) AS QTEXPEDIDA
                       FROM ITEMEXPEDICAO I JOIN PRODUTO P
                                              ON I.CDPRODMOVI = P.CDPRODUTO
                      WHERE I.CDFILIAL = P_CDFILIAL
                        AND I.NREXPEDICAO = P_NREXPEDICAO
                        AND I.IDRESERVA = 'N'
                        AND (V_IDCTRLESTQ = 'S')
                        AND (IDATUESTTPOP = 'S')
                        AND (IDCNTRESTOQ = 'S')
                     GROUP BY I.CDFILIAL, I.NREXPEDICAO, I.CDPRODUTO, I.CDPRODMOVI, P.CDPRODESTO, P.VRFATOCONV, I.IDUTCTRLLOTE, I.IDUTILALMOX, I.CDALMOXEXP, I.CDLOCALEXP, CDCLIENTE, CDFILIALPED)
      LOOP
         V_QTEXPEDIDA := C_ERRO.QTEXPEDIDA * C_ERRO.VRFATOCONV;
         V_TOTALESTOQUE := 0;
         FOR C_ESTOQ IN (SELECT P.CDFILIAL, P.CDALMOXARIFE, P.CDLOCALESTOQ, SUM(P.QTTOTALESTOQ) AS QTTOTALESTOQ
                           FROM POSESTATU P LEFT JOIN LOTE L
                                                   ON P.CDFILIAL = L.CDFILIAL
                                                  AND P.CDPRODUTO = L.CDPRODUTO
                                                  AND P.NRLOTEESTQ = L.NRLOTEESTQ
                                                  AND P.NRSUBLOTE = L.NRSUBLOTE
                                            JOIN LOCALESTOQUE LE
                                              ON P.CDFILIAL = LE.CDFILIAL
                                             AND P.CDALMOXARIFE = LE.CDALMOXARIFE
                                             AND P.CDLOCALESTOQ = LE.CDLOCALESTOQ
                          WHERE P.CDFILIAL = C_ERRO.CDFILIAL
                            AND P.CDPRODUTO = C_ERRO.CDPRODESTO
                            AND P.CDALMOXARIFE = C_ERRO.CDALMOXEXP
                            AND P.CDLOCALESTOQ = C_ERRO.CDLOCALEXP
                            AND ((C_ERRO.IDUTCTRLLOTE = 'S') AND (P.NRLOTEESTQ <> ' ') AND (TRUNC(L.DTVALILOTE) >= TRUNC(SYSDATE)) OR ((C_ERRO.IDUTCTRLLOTE = 'N') AND (P.NRLOTEESTQ = ' ')))
                            AND ((C_ERRO.IDUTILALMOX = 'N') OR (LE.IDDISPCONPED = 'S'))
                         GROUP BY P.CDFILIAL, P.CDALMOXARIFE, P.CDLOCALESTOQ)
         LOOP
            V_TOTALESTOQUE := V_TOTALESTOQUE + C_ESTOQ.QTTOTALESTOQ;
         END LOOP;
         IF (ROUND(V_QTEXPEDIDA, 5) > ROUND(V_TOTALESTOQUE, 5)) THEN
            FOR C_ITEMPEDSESTOQ IN (SELECT CDFILIAL, NREXPEDICAO, NRPEDIDO, NRITEMPED, CDPRODMOVI, QTITEMEXP
                                      FROM ITEMEXPEDICAO
                                     WHERE CDFILIAL = C_ERRO.CDFILIAL
                                       AND NREXPEDICAO = C_ERRO.NREXPEDICAO
                                       AND CDPRODMOVI = C_ERRO.CDPRODMOVI)
            LOOP
               UPDATE ITEMEXPEDICAO
                  SET IDERRO = 'S'
                WHERE CDFILIAL = C_ITEMPEDSESTOQ.CDFILIAL
                  AND NREXPEDICAO = C_ITEMPEDSESTOQ.NREXPEDICAO
                  AND NRPEDIDO = C_ITEMPEDSESTOQ.NRPEDIDO
                  AND NRITEMPED = C_ITEMPEDSESTOQ.NRITEMPED;
               SELECT NVL(MAX(NRSEQERROITEM),0)
                 INTO V_NRSEQ
                 FROM ERROITEMEXP
                WHERE CDFILIAL = C_ITEMPEDSESTOQ.CDFILIAL
                  AND NREXPEDICAO = C_ITEMPEDSESTOQ.NREXPEDICAO
                  AND NRPEDIDO = C_ITEMPEDSESTOQ.NRPEDIDO;
               V_NRSEQ := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_NRSEQ) + 1)),10,0);
               V_ERRO := 0;
               INSERT INTO ERROITEMEXP
               (CDFILIAL, NREXPEDICAO, NRPEDIDO, NRITEMPED, NRSEQERROITEM, DSERRO, CDCLIENTE, CDFILIALPED, CDPRODUTO, CDPRODMOVI, QTITEMEXP, QTTOTAITEMPROD, QTTOTAESTOQ,
                CDEMPRESA, CDINSCESTA, CDINSCMUNI, CDSERIE, NRORDPROD, NRSUBORDPROD)
               VALUES
               (C_ITEMPEDSESTOQ.CDFILIAL, C_ITEMPEDSESTOQ.NREXPEDICAO, C_ITEMPEDSESTOQ.NRPEDIDO, C_ITEMPEDSESTOQ.NRITEMPED, V_NRSEQ, 'ITEMSESTOQ', C_ERRO.CDCLIENTE, C_ERRO.CDFILIALPED, C_ERRO.CDPRODUTO, C_ERRO.CDPRODESTO, C_ITEMPEDSESTOQ.QTITEMEXP, V_QTEXPEDIDA, V_TOTALESTOQUE,
                '', '', '', '', '', '');
            END LOOP;
         END IF;
      END LOOP;
   END IF;
/*------------------ITEMSESTOQ--------------*/
   IF (V_IDGERAREPLIC = '2') THEN
      FOR C_ERRO IN (SELECT I.CDFILIAL, I.NREXPEDICAO, I.NRPEDIDO, I.NRITEMPED, I.CDCLIENTE, I.CDFILIALPED, I.CDPRODUTO, I.CDPRODMOVI, P.CDPRODESTO,
                            I.CDEMPRESA, I.CDINSCESTA, I.CDINSCMUNI, I.CDSERIE
                       FROM ITEMEXPEDICAO I JOIN PRODUTO P
                                              ON I.CDPRODMOVI = P.CDPRODUTO
                      WHERE I.CDFILIAL = P_CDFILIAL
                        AND I.NREXPEDICAO = P_NREXPEDICAO)
      LOOP
      /*------------------SERIEENTR--------------*/
         IF (P_CDSERIEENTR IS NULL) THEN
            UPDATE ITEMEXPEDICAO
               SET IDERRO = 'S'
             WHERE CDFILIAL = C_ERRO.CDFILIAL
               AND NREXPEDICAO = C_ERRO.NREXPEDICAO
               AND NRPEDIDO = C_ERRO.NRPEDIDO
               AND NRITEMPED = C_ERRO.NRITEMPED;
            SELECT NVL(MAX(NRSEQERROITEM),0)
              INTO V_NRSEQ
              FROM ERROITEMEXP
             WHERE CDFILIAL = C_ERRO.CDFILIAL
               AND NREXPEDICAO = C_ERRO.NREXPEDICAO
               AND NRPEDIDO = C_ERRO.NRPEDIDO;
            V_NRSEQ := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_NRSEQ) + 1)),10,0);
            V_ERRO := 0;
            SELECT COUNT(1)
              INTO V_ERRO
              FROM ERROITEMEXP
             WHERE CDFILIAL = C_ERRO.CDFILIAL
               AND NREXPEDICAO = C_ERRO.NREXPEDICAO
               AND NRPEDIDO = C_ERRO.NRPEDIDO
               AND DSERRO = 'SERIEENTR';
            IF (V_ERRO = 0) THEN
               INSERT INTO ERROITEMEXP
               (CDFILIAL, NREXPEDICAO, NRPEDIDO, NRITEMPED, NRSEQERROITEM, DSERRO, CDCLIENTE, CDFILIALPED, CDPRODUTO, CDPRODMOVI, QTITEMEXP, QTTOTAITEMPROD, QTTOTAESTOQ ,
                CDEMPRESA, CDINSCESTA, CDINSCMUNI, CDSERIE, NRORDPROD, NRSUBORDPROD)
               VALUES
               (C_ERRO.CDFILIAL, C_ERRO.NREXPEDICAO, C_ERRO.NRPEDIDO, '', V_NRSEQ, 'SERIEENTR', C_ERRO.CDCLIENTE, C_ERRO.CDFILIALPED, '', ' ', 0, 0, 0,
                C_ERRO.CDEMPRESA, C_ERRO.CDINSCESTA, C_ERRO.CDINSCMUNI, C_ERRO.CDSERIE, '', '');
            END IF;
         END IF;
      /*------------------SERIEENTR--------------*/
      /*------------------TIPOOPERENTR--------------*/
   		IF (P_CDTPOPERENTR IS NULL) THEN
			   UPDATE ITEMEXPEDICAO
               SET IDERRO = 'S'
             WHERE CDFILIAL = C_ERRO.CDFILIAL
               AND NREXPEDICAO = C_ERRO.NREXPEDICAO
               AND NRPEDIDO = C_ERRO.NRPEDIDO
               AND NRITEMPED = C_ERRO.NRITEMPED;
            SELECT NVL(MAX(NRSEQERROITEM),0)
              INTO V_NRSEQ
              FROM ERROITEMEXP
             WHERE CDFILIAL = C_ERRO.CDFILIAL
               AND NREXPEDICAO = C_ERRO.NREXPEDICAO
               AND NRPEDIDO = C_ERRO.NRPEDIDO;
            V_NRSEQ := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_NRSEQ) + 1)),10,0);
            V_ERRO := 0;
            SELECT COUNT(1)
              INTO V_ERRO
              FROM ERROITEMEXP
             WHERE CDFILIAL = C_ERRO.CDFILIAL
               AND NREXPEDICAO = C_ERRO.NREXPEDICAO
               AND NRPEDIDO = C_ERRO.NRPEDIDO
               AND DSERRO = 'TIPOOPERENTR';
            IF (V_ERRO = 0) THEN
               INSERT INTO ERROITEMEXP
               (CDFILIAL, NREXPEDICAO, NRPEDIDO, NRITEMPED, NRSEQERROITEM, DSERRO, CDCLIENTE, CDFILIALPED, CDPRODUTO, CDPRODMOVI, QTITEMEXP, QTTOTAITEMPROD, QTTOTAESTOQ,
                CDEMPRESA, CDINSCESTA, CDINSCMUNI, CDSERIE, NRORDPROD, NRSUBORDPROD)
               VALUES
               (C_ERRO.CDFILIAL, C_ERRO.NREXPEDICAO, C_ERRO.NRPEDIDO, '', V_NRSEQ, 'TIPOOPERENTR', C_ERRO.CDCLIENTE, C_ERRO.CDFILIALPED, '', '', 0, 0, 0,
                C_ERRO.CDEMPRESA, C_ERRO.CDINSCESTA, C_ERRO.CDINSCMUNI, C_ERRO.CDSERIE, '', '');
            END IF;
	   	END IF;
      /*------------------TIPOOPERENTR--------------*/
      /*------------------ALMOXENTR--------------*/
         V_CDFILIDEST := C_ERRO.CDFILIALPED;
         IF (V_CDFILIDEST IS NULL) THEN
            BEGIN
               SELECT NRINSJURCLIE
                 INTO V_NRINSJURCLIE
                 FROM CLIENTE
                WHERE CDCLIENTE = C_ERRO.CDCLIENTE;
            EXCEPTION
               WHEN NO_DATA_FOUND THEN BEGIN
                  V_NRINSJURCLIE := NULL;
                END;
            END;
            BEGIN
               SELECT CDFILIAL
                 INTO V_CDFILIDEST
                 FROM FILIAL
                WHERE NRINSJURFILI = V_NRINSJURCLIE;
            EXCEPTION
               WHEN NO_DATA_FOUND THEN BEGIN
                  V_CDFILIDEST := NULL;
                END;
            END;
         END IF;
         IF (V_CDFILIDEST IS NOT NULL) THEN
            BEGIN
               SELECT IDUTILALMOX,   IDUTILLCESTQ,   IDUTILLOTE,   CDALMOXARIFE,   CDLOCALESTOQ,   DSNUMELOTE,   DSSEQULOTE
                 INTO V_IDUTILALMOX, V_IDUTILLCESTQ, V_IDUTILLOTE, V_CDALMOXARIFE, V_CDLOCALESTOQ, V_DSNUMELOTE, V_DSSEQULOTE
                 FROM PARAMFILIAL
                WHERE CDFILIAL = V_CDFILIDEST;
            EXCEPTION
               WHEN NO_DATA_FOUND THEN BEGIN
                  V_IDUTILALMOX  := 'N';
                  V_IDUTILLCESTQ := 'N';
                  V_IDUTILLOTE   := 'N';
                  V_CDALMOXARIFE := NULL;
                  V_CDLOCALESTOQ := NULL;
                  V_DSNUMELOTE   := NULL;
                  V_DSSEQULOTE   := NULL;
               END;
            END;
            IF (V_IDUTILALMOX = 'S') THEN
               BEGIN
                  SELECT CDALMOXPRCLI,   CDLOCALPRCLI
                    INTO V_CDALMOXPRCLI, V_CDLOCALPRCLI
                    FROM PRODFILI PF
                   WHERE PF.CDPRODUTO = C_ERRO.CDPRODESTO
                     AND PF.CDFILIAL  = V_CDFILIDEST;
               EXCEPTION
                  WHEN NO_DATA_FOUND THEN
                     V_CDALMOXPRCLI := NULL;
                     V_CDLOCALPRCLI := NULL;
               END;
               IF (V_CDALMOXPRCLI IS NULL) THEN
                  IF (V_CDALMOXARIFE IS NULL) OR (V_CDALMOXARIFE = ' ') THEN
                     UPDATE ITEMEXPEDICAO
                        SET IDERRO = 'S'
                      WHERE CDFILIAL = C_ERRO.CDFILIAL
                        AND NREXPEDICAO = C_ERRO.NREXPEDICAO
                        AND NRPEDIDO = C_ERRO.NRPEDIDO
                        AND NRITEMPED = C_ERRO.NRITEMPED;
                     SELECT NVL(MAX(NRSEQERROITEM),0)
                       INTO V_NRSEQ
                       FROM ERROITEMEXP
                      WHERE CDFILIAL = C_ERRO.CDFILIAL
                        AND NREXPEDICAO = C_ERRO.NREXPEDICAO
                        AND NRPEDIDO = C_ERRO.NRPEDIDO;
                     V_NRSEQ := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_NRSEQ) + 1)),10,0);
                     V_ERRO := 0;
                     SELECT COUNT(1)
                       INTO V_ERRO
                       FROM ERROITEMEXP
                      WHERE CDFILIAL = C_ERRO.CDFILIAL
                        AND NREXPEDICAO = C_ERRO.NREXPEDICAO
                        AND NRPEDIDO = C_ERRO.NRPEDIDO
                        AND NRITEMPED = C_ERRO.NRITEMPED
                        AND DSERRO = 'ALMOXENTR';
                     IF (V_ERRO = 0) THEN
                        INSERT INTO ERROITEMEXP
                        (CDFILIAL, NREXPEDICAO, NRPEDIDO, NRITEMPED, NRSEQERROITEM, DSERRO, CDCLIENTE, CDFILIALPED, CDPRODUTO, CDPRODMOVI, QTITEMEXP, QTTOTAITEMPROD, QTTOTAESTOQ,
                         CDEMPRESA, CDINSCESTA, CDINSCMUNI, CDSERIE, NRORDPROD, NRSUBORDPROD)
                        VALUES
                        (C_ERRO.CDFILIAL, C_ERRO.NREXPEDICAO, C_ERRO.NRPEDIDO, C_ERRO.NRITEMPED, V_NRSEQ, 'ALMOXENTR', C_ERRO.CDCLIENTE, V_CDFILIDEST, C_ERRO.CDPRODUTO, C_ERRO.CDPRODESTO, 0, 0, 0,
                         C_ERRO.CDEMPRESA, C_ERRO.CDINSCESTA, C_ERRO.CDINSCMUNI, C_ERRO.CDSERIE, '', '');
                     END IF;
                  END IF;
               END IF;
         /*------------------ALMOXENTR--------------*/
         /*------------------ALMOXLCESTENTR--------------*/
               IF (V_IDUTILLCESTQ = 'S') THEN
                  IF (V_CDLOCALPRCLI IS NULL) THEN
                     IF (V_CDALMOXARIFE <> V_CDALMOXPRCLI) THEN
                        IF NOT(V_CDLOCALESTOQ IS NULL) THEN
                           UPDATE ITEMEXPEDICAO
                              SET IDERRO = 'S'
                            WHERE CDFILIAL = C_ERRO.CDFILIAL
                              AND NREXPEDICAO = C_ERRO.NREXPEDICAO
                              AND NRPEDIDO = C_ERRO.NRPEDIDO
                              AND NRITEMPED = C_ERRO.NRITEMPED;
                           SELECT NVL(MAX(NRSEQERROITEM),0)
                             INTO V_NRSEQ
                             FROM ERROITEMEXP
                            WHERE CDFILIAL = C_ERRO.CDFILIAL
                              AND NREXPEDICAO = C_ERRO.NREXPEDICAO
                              AND NRPEDIDO = C_ERRO.NRPEDIDO;
                           V_NRSEQ := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_NRSEQ) + 1)),10,0);
                           V_ERRO := 0;
                           SELECT COUNT(1)
                             INTO V_ERRO
                             FROM ERROITEMEXP
                            WHERE CDFILIAL = C_ERRO.CDFILIAL
                              AND NREXPEDICAO = C_ERRO.NREXPEDICAO
                              AND NRPEDIDO = C_ERRO.NRPEDIDO
                              AND NRITEMPED = C_ERRO.NRITEMPED
                              AND DSERRO = 'ALMOXLCESTENTR';
                           IF (V_ERRO = 0) THEN
                              INSERT INTO ERROITEMEXP
                              (CDFILIAL, NREXPEDICAO, NRPEDIDO, NRITEMPED, NRSEQERROITEM, DSERRO, CDCLIENTE, CDFILIALPED, CDPRODUTO, CDPRODMOVI, QTITEMEXP, QTTOTAITEMPROD, QTTOTAESTOQ,
                               CDEMPRESA, CDINSCESTA, CDINSCMUNI, CDSERIE, NRORDPROD, NRSUBORDPROD)
                              VALUES
                              (C_ERRO.CDFILIAL, C_ERRO.NREXPEDICAO, C_ERRO.NRPEDIDO, C_ERRO.NRITEMPED, V_NRSEQ, 'ALMOXLCESTENTR', C_ERRO.CDCLIENTE, V_CDFILIDEST, C_ERRO.CDPRODUTO, C_ERRO.CDPRODESTO, 0, 0, 0,
                               C_ERRO.CDEMPRESA, C_ERRO.CDINSCESTA, C_ERRO.CDINSCMUNI, C_ERRO.CDSERIE, '', '');
                           END IF;
                        END IF;
                     END IF;
                  END IF;
               END IF;
         /*------------------ALMOXLCESTENTR--------------*/
         /*------------------LCESTENTR--------------*/
               IF (V_IDUTILLCESTQ = 'S') THEN
                  IF (V_CDLOCALPRCLI IS NULL) THEN
                     IF (V_CDLOCALESTOQ IS NULL) OR (V_CDLOCALESTOQ = ' ') THEN
                        UPDATE ITEMEXPEDICAO
                           SET IDERRO = 'S'
                         WHERE CDFILIAL = C_ERRO.CDFILIAL
                           AND NREXPEDICAO = C_ERRO.NREXPEDICAO
                           AND NRPEDIDO = C_ERRO.NRPEDIDO
                           AND NRITEMPED = C_ERRO.NRITEMPED;
                        SELECT NVL(MAX(NRSEQERROITEM),0)
                          INTO V_NRSEQ
                          FROM ERROITEMEXP
                         WHERE CDFILIAL = C_ERRO.CDFILIAL
                           AND NREXPEDICAO = C_ERRO.NREXPEDICAO
                           AND NRPEDIDO = C_ERRO.NRPEDIDO;
                        V_NRSEQ := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_NRSEQ) + 1)),10,0);
                        V_ERRO := 0;
                        SELECT COUNT(1)
                          INTO V_ERRO
                          FROM ERROITEMEXP
                         WHERE CDFILIAL = C_ERRO.CDFILIAL
                           AND NREXPEDICAO = C_ERRO.NREXPEDICAO
                           AND NRPEDIDO = C_ERRO.NRPEDIDO
                           AND NRITEMPED = C_ERRO.NRITEMPED
                           AND DSERRO = 'LCESTENTR';
                        IF (V_ERRO = 0) THEN
                           INSERT INTO ERROITEMEXP
                           (CDFILIAL, NREXPEDICAO, NRPEDIDO, NRITEMPED, NRSEQERROITEM, DSERRO, CDCLIENTE, CDFILIALPED, CDPRODUTO, CDPRODMOVI, QTITEMEXP, QTTOTAITEMPROD, QTTOTAESTOQ ,
                            CDEMPRESA, CDINSCESTA, CDINSCMUNI, CDSERIE, NRORDPROD, NRSUBORDPROD)
                           VALUES
                           (C_ERRO.CDFILIAL, C_ERRO.NREXPEDICAO, C_ERRO.NRPEDIDO, C_ERRO.NRITEMPED, V_NRSEQ, 'LCESTENTR', C_ERRO.CDCLIENTE, V_CDFILIDEST, C_ERRO.CDPRODUTO, C_ERRO.CDPRODESTO, 0, 0, 0,
                            C_ERRO.CDEMPRESA, C_ERRO.CDINSCESTA, C_ERRO.CDINSCMUNI, C_ERRO.CDSERIE, '', '');
                        END IF;
                     END IF;
                  END IF;
               END IF;
         /*------------------LCESTENTR--------------*/
            END IF;
         /*------------------LOTEENTR--------------*/
            IF (V_IDUTILLOTE = 'S')  THEN
               IF ((V_DSNUMELOTE IS NULL) OR (V_DSSEQULOTE IS NULL)) THEN
                  UPDATE ITEMEXPEDICAO
                     SET IDERRO = 'S'
                   WHERE CDFILIAL = C_ERRO.CDFILIAL
                     AND NREXPEDICAO = C_ERRO.NREXPEDICAO
                     AND NRPEDIDO = C_ERRO.NRPEDIDO
                     AND NRITEMPED = C_ERRO.NRITEMPED;
                  SELECT NVL(MAX(NRSEQERROITEM),0)
                    INTO V_NRSEQ
                    FROM ERROITEMEXP
                   WHERE CDFILIAL = C_ERRO.CDFILIAL
                     AND NREXPEDICAO = C_ERRO.NREXPEDICAO
                     AND NRPEDIDO = C_ERRO.NRPEDIDO;
                  V_NRSEQ := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_NRSEQ) + 1)),10,0);
                  V_ERRO := 0;
                  SELECT COUNT(1)
                    INTO V_ERRO
                    FROM ERROITEMEXP
                   WHERE CDFILIAL = C_ERRO.CDFILIAL
                     AND NREXPEDICAO = C_ERRO.NREXPEDICAO
                     AND NRPEDIDO = C_ERRO.NRPEDIDO
                     AND NRITEMPED = C_ERRO.NRITEMPED
                     AND DSERRO = 'LOTEENTR';
                  IF (V_ERRO = 0) THEN
                     INSERT INTO ERROITEMEXP
                     (CDFILIAL, NREXPEDICAO, NRPEDIDO, NRITEMPED, NRSEQERROITEM, DSERRO, CDCLIENTE, CDFILIALPED, CDPRODUTO, CDPRODMOVI, QTITEMEXP, QTTOTAITEMPROD, QTTOTAESTOQ,
                      CDEMPRESA, CDINSCESTA, CDINSCMUNI, CDSERIE, NRORDPROD, NRSUBORDPROD)
                     VALUES
                     (C_ERRO.CDFILIAL, C_ERRO.NREXPEDICAO, C_ERRO.NRPEDIDO, C_ERRO.NRITEMPED, V_NRSEQ, 'LOTEENTR', C_ERRO.CDCLIENTE, V_CDFILIDEST, C_ERRO.CDPRODUTO, C_ERRO.CDPRODESTO, 0, 0, 0,
                      C_ERRO.CDEMPRESA, C_ERRO.CDINSCESTA, C_ERRO.CDINSCMUNI, C_ERRO.CDSERIE, '', '');
                  END IF;
               END IF;
            END IF;
         END IF;
      /*------------------LOTEENTR--------------*/
      END LOOP;
      /*------------------PEDNFILCLIE--------------*/
      /*FOR C_ERRO IN (SELECT DISTINCT CDFILIAL, NREXPEDICAO, NRPEDIDO, CDFILIALPED, CDCLIENTE, CDEMPRESA, CDINSCESTA, CDINSCMUNI, CDSERIE
                       FROM ITEMEXPEDICAO
                      WHERE CDFILIAL = P_CDFILIAL
                        AND NREXPEDICAO = P_NREXPEDICAO
                        AND IDDESTINO = 'C'
                        AND CDFILIALPED IS NULL)
      LOOP
         BEGIN
            SELECT NRINSJURCLIE
              INTO V_NRINSJURCLIE
              FROM CLIENTE
             WHERE CDCLIENTE = C_ERRO.CDCLIENTE;
         EXCEPTION
            WHEN NO_DATA_FOUND THEN BEGIN
               V_NRINSJURCLIE := NULL;
             END;
         END;
         BEGIN
            SELECT CDFILIAL
              INTO V_CDFILIDEST
              FROM FILIAL
             WHERE NRINSJURFILI = V_NRINSJURCLIE;
         EXCEPTION
            WHEN NO_DATA_FOUND THEN BEGIN
               V_CDFILIDEST := NULL;
             END;
         END;
         IF (V_CDFILIDEST IS NULL) THEN
            UPDATE ITEMEXPEDICAO
               SET IDERRO = 'S'
             WHERE CDFILIAL = C_ERRO.CDFILIAL
               AND NREXPEDICAO = C_ERRO.NREXPEDICAO
               AND NRPEDIDO = C_ERRO.NRPEDIDO;
            SELECT NVL(MAX(NRSEQERROITEM),0)
              INTO V_NRSEQ
              FROM ERROITEMEXP
             WHERE CDFILIAL = C_ERRO.CDFILIAL
               AND NREXPEDICAO = C_ERRO.NREXPEDICAO
               AND NRPEDIDO = C_ERRO.NRPEDIDO;
            V_NRSEQ := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_NRSEQ) + 1)),10,0);
            V_ERRO := 0;
            SELECT COUNT(1)
              INTO V_ERRO
              FROM ERROITEMEXP
             WHERE CDFILIAL = C_ERRO.CDFILIAL
               AND NREXPEDICAO = C_ERRO.NREXPEDICAO
               AND NRPEDIDO = C_ERRO.NRPEDIDO
               AND DSERRO = 'PEDNFILCLIE';
            IF (V_ERRO = 0) THEN
               INSERT INTO ERROITEMEXP
               (CDFILIAL, NREXPEDICAO, NRPEDIDO, NRITEMPED, NRSEQERROITEM, DSERRO, CDCLIENTE, CDFILIALPED, CDPRODUTO, CDPRODMOVI, QTITEMEXP, QTTOTAITEMPROD, QTTOTAESTOQ,
                CDEMPRESA, CDINSCESTA, CDINSCMUNI, CDSERIE, NRORDPROD, NRSUBORDPROD)
               VALUES
               (C_ERRO.CDFILIAL, C_ERRO.NREXPEDICAO, C_ERRO.NRPEDIDO, '', V_NRSEQ, 'PEDNFILCLIE', C_ERRO.CDCLIENTE, C_ERRO.CDFILIALPED, '', '', 0, 0, 0,
                C_ERRO.CDEMPRESA, C_ERRO.CDINSCESTA, C_ERRO.CDINSCMUNI, C_ERRO.CDSERIE, '', '');
            END IF;
         END IF;
      END LOOP;*/
     /*------------------PEDNFILCLIE--------------*/
  END IF;
/*------------------LOTE_VENCIDO--------------*/
   FOR C_ERRO IN (SELECT I.CDFILIAL, I.NREXPEDICAO, I.NRPEDIDO, I.NRITEMPED, I.CDCLIENTE, I.CDFILIALPED, I.CDPRODUTO, I.CDPRODMOVI, P.CDPRODESTO, I.NRLOTEEXP, I.NRSUBLOTEEXP,
                         I.CDEMPRESA, I.CDINSCESTA, I.CDINSCMUNI, I.CDSERIE
                    FROM ITEMEXPEDICAO I LEFT JOIN LOTE L
                                                ON I.CDFILIAL = L.CDFILIAL
                                               AND I.CDPRODMOVI = L.CDPRODUTO
                                               AND I.NRLOTEEXP = L.NRLOTEESTQ
                                               AND I.NRSUBLOTEEXP = L.NRSUBLOTE
                                              JOIN PRODUTO P
                                                ON I.CDPRODMOVI = P.CDPRODESTO
                   WHERE I.CDFILIAL = P_CDFILIAL
                     AND I.NREXPEDICAO = P_NREXPEDICAO
                     AND I.IDRESERVA = 'S'
                     AND ((NVL(I.IDUTCTRLLOTE, 'S') = 'S') AND (TRUNC(L.DTVALILOTE) <= TRUNC(SYSDATE))))
   LOOP
      UPDATE ITEMEXPEDICAO
         SET IDERRO = 'S'
       WHERE CDFILIAL = C_ERRO.CDFILIAL
         AND NREXPEDICAO = C_ERRO.NREXPEDICAO
         AND NRPEDIDO = C_ERRO.NRPEDIDO
         AND NRITEMPED = C_ERRO.NRITEMPED;
      SELECT NVL(MAX(NRSEQERROITEM),0)
        INTO V_NRSEQ
        FROM ERROITEMEXP
       WHERE CDFILIAL = C_ERRO.CDFILIAL
         AND NREXPEDICAO = C_ERRO.NREXPEDICAO
         AND NRPEDIDO = C_ERRO.NRPEDIDO;
      V_NRSEQ := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_NRSEQ) + 1)),10,0);
      V_ERRO := 0;
      SELECT COUNT(1)
        INTO V_ERRO
        FROM ERROITEMEXP
       WHERE CDFILIAL = C_ERRO.CDFILIAL
         AND NREXPEDICAO = C_ERRO.NREXPEDICAO
         AND NRPEDIDO = C_ERRO.NRPEDIDO
         AND NRITEMPED = C_ERRO.NRITEMPED
         AND DSERRO = 'LOTE_VENCIDO';
      IF (V_ERRO = 0) THEN
         INSERT INTO ERROITEMEXP
         (CDFILIAL, NREXPEDICAO, NRPEDIDO, NRITEMPED, NRSEQERROITEM, DSERRO, CDCLIENTE, CDFILIALPED, CDPRODUTO, CDPRODMOVI,QTITEMEXP, QTTOTAITEMPROD, QTTOTAESTOQ,
          CDEMPRESA, CDINSCESTA, CDINSCMUNI, CDSERIE, NRORDPROD, NRSUBORDPROD, NRLOTEESTQ, NRSUBLOTE)
         VALUES
         (C_ERRO.CDFILIAL, C_ERRO.NREXPEDICAO, C_ERRO.NRPEDIDO, C_ERRO.NRITEMPED, V_NRSEQ, 'LOTE_VENCIDO', C_ERRO.CDCLIENTE, C_ERRO.CDFILIALPED, C_ERRO.CDPRODUTO, C_ERRO.CDPRODESTO, 0, 0, 0,
          C_ERRO.CDEMPRESA, C_ERRO.CDINSCESTA, C_ERRO.CDINSCMUNI, C_ERRO.CDSERIE, '', '', C_ERRO.NRLOTEEXP, C_ERRO.NRSUBLOTEEXP);
      END IF;
   END LOOP;
/*------------------LOTE_VENCIDO--------------*/
/*-------------------------FAZ A EXPEDICAO DOS ITENS SEM ERRO--------------------------*/
   V_IDSISTEMA   := 'EXP';
   IF (V_IDUTSEPPEDEXP = 'S') THEN
      V_IDSISTEMA   := 'SEP';
      V_ATUAESTOQUE := 'N';
   END IF;
   /*--------- EXPEDE OS PEDIDOS DE INDUSTRIALIZACAO -------------------*/
   FOR C_PEDINDUSTRIA IN (SELECT DISTINCT CDFILIAL, NREXPEDICAO, NRPEDIDO, CDEMPRESA, CDTIPOOPER, CDSERIE, IDTPIJURTRAN, NRINSJURTRAN,
                                 NRPLACAVEPED, SGUFVEICPED, IDPGFRETTRAN, IDATUESTTPOP
                            FROM ITEMEXPEDICAO
                           WHERE CDFILIAL = P_CDFILIAL
                             AND NREXPEDICAO = P_NREXPEDICAO
                             AND IDERRO = 'N'
                             AND (IDPEDINDUSTRIA = 'S'))
   LOOP
      V_COUNT_ERROPED := 0;
      SELECT COUNT(1)
        INTO V_COUNT_ERROPED
        FROM ITEMEXPEDICAO
       WHERE CDFILIAL = C_PEDINDUSTRIA.CDFILIAL
         AND NRPEDIDO = C_PEDINDUSTRIA.NRPEDIDO
         AND NREXPEDICAO = P_NREXPEDICAO
         AND IDERRO = 'S';
      IF (V_COUNT_ERROPED = 0) THEN
         GERAR_EXP_INDUSTRIALIZACAO(C_PEDINDUSTRIA.CDFILIAL,     C_PEDINDUSTRIA.NRPEDIDO,     V_CDOPERADOR,
                                    C_PEDINDUSTRIA.CDEMPRESA,    C_PEDINDUSTRIA.CDTIPOOPER,   C_PEDINDUSTRIA.CDSERIE,
                                    C_PEDINDUSTRIA.IDTPIJURTRAN, C_PEDINDUSTRIA.NRINSJURTRAN, C_PEDINDUSTRIA.NRPLACAVEPED,
                                    C_PEDINDUSTRIA.SGUFVEICPED,  C_PEDINDUSTRIA.IDPGFRETTRAN, V_IDCUSEXP,
                                    V_NFSINDUSTRIALIZACAO,       C_PEDINDUSTRIA.NREXPEDICAO);
         V_NRLANCTONF := SUBSTR(V_NFSINDUSTRIALIZACAO,11,10);
         BEGIN
            SELECT MAX(NRLANCESTQ)
              INTO V_NRLANCESTQ
              FROM LANCTOESTOQ
             WHERE CDFILIAL = C_PEDINDUSTRIA.CDFILIAL
               AND NRLANCTONF = V_NRLANCTONF;
         EXCEPTION
            WHEN NO_DATA_FOUND THEN BEGIN
               V_NRLANCESTQ := NULL;
             END;
         END;
         V_ATUAESTOQUE := 'N';
         IF ((V_IDCTRLESTQ = 'S') AND (C_PEDINDUSTRIA.IDATUESTTPOP = 'S') AND (V_IDSISTEMA = 'EXP')) THEN
            V_ATUAESTOQUE := 'S';
         END IF;
         V_EXISTENF := 0;
         SELECT COUNT(1)
           INTO V_EXISTENF
           FROM NFRELACITEMPED
          WHERE CDFILIAL = C_PEDINDUSTRIA.CDFILIAL
            AND NREXPEDICAO = C_PEDINDUSTRIA.NREXPEDICAO
            AND NRPEDIDO = C_PEDINDUSTRIA.NRPEDIDO
            AND NRLANCTONF = V_NRLANCTONF;
         IF (V_EXISTENF = 0) THEN
            INSERT INTO NFRELACITEMPED
            (CDFILIAL, NREXPEDICAO, NRPEDIDO, NRITEMPED, NRLANCTONF , NRLANCESTQ, IDATUAESTOQUE, IDERRO)
            SELECT CDFILIAL, NREXPEDICAO, NRPEDIDO, NRITEMPED, V_NRLANCTONF, V_NRLANCESTQ, V_ATUAESTOQUE, 'N'
              FROM ITEMEXPEDICAO
             WHERE CDFILIAL = C_PEDINDUSTRIA.CDFILIAL
               AND NREXPEDICAO = C_PEDINDUSTRIA.NREXPEDICAO
               AND NRPEDIDO = C_PEDINDUSTRIA.NRPEDIDO;
         END IF;
         V_NRLANCTONFPE := SUBSTR(V_NFSINDUSTRIALIZACAO,21,10);
         IF (V_NRLANCTONFPE IS NOT NULL) THEN
            V_EXISTENF := 0;
            SELECT COUNT(1)
              INTO V_EXISTENF
              FROM NFRELACITEMPED
             WHERE CDFILIAL = C_PEDINDUSTRIA.CDFILIAL
               AND NREXPEDICAO = C_PEDINDUSTRIA.NREXPEDICAO
               AND NRPEDIDO = C_PEDINDUSTRIA.NRPEDIDO
               AND NRLANCTONF = V_NRLANCTONFPE;
            IF (V_EXISTENF = 0) THEN
               INSERT INTO NFRELACITEMPED
               (CDFILIAL, NREXPEDICAO, NRPEDIDO, NRITEMPED, NRLANCTONF , IDERRO)
               SELECT CDFILIAL, NREXPEDICAO, NRPEDIDO, NRITEMPED, V_NRLANCTONFPE, 'N'
                 FROM ITEMEXPEDICAO
                WHERE CDFILIAL = C_PEDINDUSTRIA.CDFILIAL
                  AND NREXPEDICAO = C_PEDINDUSTRIA.NREXPEDICAO
                  AND NRPEDIDO = C_PEDINDUSTRIA.NRPEDIDO;
            END IF;
         END IF;
      ELSE
         UPDATE ITEMEXPEDICAO
            SET IDBLOQUEIAPED = 'S'
          WHERE CDFILIAL = C_PEDINDUSTRIA.CDFILIAL
            AND NRPEDIDO = C_PEDINDUSTRIA.NRPEDIDO;
      END IF;
   END LOOP;
   /*--------- EXPEDE OS PEDIDOS N:param3O INDUSTRIALIZACAO -------------------*/
   BEGIN
      SELECT MAX(IDTIPOEXP)
        INTO V_IDTIPOEXP
        FROM EXPRELACITEMPPED
       WHERE CDFILIAL = P_CDFILIAL
         AND NREXPEDICAO = P_NREXPEDICAO;
   EXCEPTION
      WHEN NO_DATA_FOUND THEN BEGIN
         V_IDTIPOEXP := NULL;
      END;
   END;
   IF (V_IDTIPOEXP = 'PEDIDO') THEN
      FOR C_BLOQUEIAPEDIDO IN (SELECT DISTINCT CDFILIAL, NRPEDIDO
                                 FROM ITEMEXPEDICAO
                                WHERE CDFILIAL = P_CDFILIAL
                                  AND NREXPEDICAO = P_NREXPEDICAO
                                  AND IDERRO = 'S'
                                  AND IDPEDINDUSTRIA = 'N')
      LOOP
         UPDATE ITEMEXPEDICAO
            SET IDBLOQUEIAPED = 'S'
          WHERE CDFILIAL = C_BLOQUEIAPEDIDO.CDFILIAL
            AND NRPEDIDO = C_BLOQUEIAPEDIDO.NRPEDIDO;
      END LOOP;
   END IF;
   FOR C_AGRUPAITENS IN (SELECT CDFILIAL, NREXPEDICAO, CDEMPRESA, CDINSCMUNI, CDINSCESTA, CDSERIE, CDQUEBRANF, IDTPIJURTRAN, NRINSJURTRAN, CDTIPTRANS, IDPGFRETTRAN, NRPLACAVEPED, SGUFVEICPED, CDMODPGTO, CDTIPOOPER, IDATUESTTPOP, CDVENDEDOR, COUNT(1)
                           FROM ITEMEXPEDICAO
                          WHERE CDFILIAL = P_CDFILIAL
                            AND NREXPEDICAO = P_NREXPEDICAO
                            AND IDERRO = 'N'
                            AND IDPEDINDUSTRIA = 'N'
                            AND IDBLOQUEIAPED = 'N'
                         GROUP BY CDFILIAL, NREXPEDICAO, CDEMPRESA, CDINSCMUNI, CDINSCESTA, CDSERIE, CDQUEBRANF, IDTPIJURTRAN, NRINSJURTRAN, CDTIPTRANS, IDPGFRETTRAN, NRPLACAVEPED, SGUFVEICPED, CDMODPGTO , CDTIPOOPER, IDATUESTTPOP, CDVENDEDOR)
   LOOP
      BEGIN
         SELECT NVL(NRMAXITNF, 10), NVL(IDCONMAXITEM, 'S')
           INTO V_NRMAXITNF, V_IDCONMAXITEM
           FROM SERIENF
          WHERE CDEMPRESA = C_AGRUPAITENS.CDEMPRESA
            AND CDINSCMUNI = C_AGRUPAITENS.CDINSCMUNI
            AND CDINSCESTA = C_AGRUPAITENS.CDINSCESTA
            AND CDSERIE = C_AGRUPAITENS.CDSERIE;
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_NRMAXITNF := 10;
            V_IDCONMAXITEM := 'S';
      END;
      V_NOVANOTA := '1';
      FOR C_ITEMNF IN (SELECT CDFILIAL, NREXPEDICAO, NRPEDIDO, NRITEMPED, CDPRODMOVI, CDPRODEMIS, DSPRODEMIS, CDDESTINO,
                              IDTPIJURTRAN, NRINSJURTRAN, CDTIPTRANS, IDPGFRETTRAN, NRPLACAVEPED,  SGUFVEICPED,
                              CDEMPRESA,   CDINSCESTA,   CDINSCMUNI,   CDSERIE, QTITEMPROD, CDALMOXEXP, CDLOCALEXP,
                              NRLOTEEXP, NRSUBLOTEEXP, VRPRECOEXP, VRDESITEXP, IDCNTRESTOQ, QTEXPEDIDA, QTITEMPED
                         FROM ITEMEXPEDICAO
                        WHERE CDFILIAL = C_AGRUPAITENS.CDFILIAL
                          AND NREXPEDICAO = C_AGRUPAITENS.NREXPEDICAO
                          AND CDEMPRESA = C_AGRUPAITENS.CDEMPRESA
                          AND CDINSCMUNI = C_AGRUPAITENS.CDINSCMUNI
                          AND CDINSCESTA = C_AGRUPAITENS.CDINSCESTA
                          AND NVL(CDSERIE, 'A') = NVL(C_AGRUPAITENS.CDSERIE, 'A')
                          AND NVL(CDQUEBRANF, 'A') = NVL(C_AGRUPAITENS.CDQUEBRANF, 'A')
                          AND NVL(IDTPIJURTRAN, 'A') = NVL(C_AGRUPAITENS.IDTPIJURTRAN, 'A')
                          AND NVL(NRINSJURTRAN, 'A') = NVL(C_AGRUPAITENS.NRINSJURTRAN, 'A')
                          AND NVL(CDTIPTRANS, 'A') = NVL(C_AGRUPAITENS.CDTIPTRANS, 'A')
                          AND IDPGFRETTRAN = C_AGRUPAITENS.IDPGFRETTRAN
                          AND CDMODPGTO = C_AGRUPAITENS.CDMODPGTO
                          AND CDTIPOOPER = C_AGRUPAITENS.CDTIPOOPER
                          AND CDVENDEDOR = C_AGRUPAITENS.CDVENDEDOR
                          AND IDERRO = 'N'
                          AND IDBLOQUEIAPED = 'N'
                       ORDER BY CDDESTINO, NRPEDIDO, NRITEMPED)
      LOOP
         IF (V_NOVANOTA = '1') THEN
            NOVO_CODIGO('LANCNF/FILIAL' || C_AGRUPAITENS.CDFILIAL,V_SEQNF);
            V_NRLANCTONF := SUBSTR(V_SEQNF,11,10);
            NOVO_CODIGO('LANCTOEST'|| C_AGRUPAITENS.CDFILIAL,V_SEQEST);
            V_PRXLANCESTQ := SUBSTR(V_SEQEST,11,10);
            V_ATUAESTOQUE := 'N';
            IF ((V_IDCTRLESTQ = 'S') AND (C_AGRUPAITENS.IDATUESTTPOP = 'S') AND (V_IDSISTEMA = 'EXP')) THEN
               V_ATUAESTOQUE := 'S';
            END IF;
            GERA_NOTAFISCAL_CORPO(C_AGRUPAITENS.CDFILIAL,   C_AGRUPAITENS.CDFILIAL,      C_AGRUPAITENS.CDEMPRESA,    C_AGRUPAITENS.CDTIPOOPER,   C_AGRUPAITENS.CDSERIE,     C_ITEMNF.CDDESTINO,
                                  V_DTEXPEDICAO,  V_IDSISTEMA,    V_NRLANCTONF,   V_PRXLANCESTQ,  V_ATUAESTOQUE, NULL,
                                  V_NRLANCTONF,  C_AGRUPAITENS.IDTPIJURTRAN, C_AGRUPAITENS.NRINSJURTRAN, C_AGRUPAITENS.NRPLACAVEPED, C_AGRUPAITENS.SGUFVEICPED, C_AGRUPAITENS.IDPGFRETTRAN,
                                  NULL,          NULL,           NULL,           NULL,           NULL,         NULL,
                                  V_PRXLANCESTQ, NULL,           V_DTEXPEDICAO,   NULL,           NULL,         NULL);
            V_NOVANOTA := '0';
            V_COUNTITEM := 0;
         END IF;
         GERA_NOTAFISCAL_PRODUTO(C_ITEMNF.CDFILIAL, V_NRLANCTONF,       C_ITEMNF.CDPRODMOVI,    C_ITEMNF.CDPRODEMIS, C_ITEMNF.DSPRODEMIS , C_ITEMNF.QTITEMPROD,
                                 C_ITEMNF.VRPRECOEXP,    C_ITEMNF.VRDESITEXP, C_ITEMNF.CDFILIAL,     C_ITEMNF.NRPEDIDO,         C_ITEMNF.NRITEMPED,   NULL,
                                 NULL,       NULL,       C_ITEMNF.CDALMOXEXP, C_ITEMNF.CDLOCALEXP,     C_ITEMNF.NRLOTEEXP,                C_ITEMNF.NRSUBLOTEEXP,
                                 'S',        NULL,               NULL,           NULL,               NULL,                NULL,
                                  NULL,       'N',       C_ITEMNF.IDCNTRESTOQ,  NULL,                  NULL,                NULL,
                                  NULL);
         V_COUNTITEM := V_COUNTITEM + 1;
         IF (C_ITEMNF.QTEXPEDIDA + C_ITEMNF.QTITEMPROD >= C_ITEMNF.QTITEMPED) THEN
            V_IDSITITEMPED := '4';
         ELSE
            V_IDSITITEMPED := '7';
         END IF;
         UPDATE ITEMPEDIDO
            SET IDSITITEMPED = V_IDSITITEMPED,
                QTEXPITEMPED = QTEXPITEMPED + C_ITEMNF.QTITEMPROD,
                CDOPERADOR   = V_CDOPERADOR
          WHERE CDFILIAL  = C_ITEMNF.CDFILIAL
            AND NRPEDIDO  = C_ITEMNF.NRPEDIDO
            AND NRITEMPED = C_ITEMNF.NRITEMPED;
         V_EXISTENF := 0;
         SELECT COUNT(1)
           INTO V_EXISTENF
           FROM NFRELACITEMPED
          WHERE CDFILIAL = C_ITEMNF.CDFILIAL
            AND NREXPEDICAO = C_ITEMNF.NREXPEDICAO
            AND NRPEDIDO = C_ITEMNF.NRPEDIDO
            AND NRITEMPED = C_ITEMNF.NRITEMPED
            AND NRLANCTONF = V_NRLANCTONF;
         IF (V_EXISTENF = 0) THEN
            INSERT INTO NFRELACITEMPED
            (CDFILIAL, NREXPEDICAO, NRPEDIDO, NRITEMPED, NRLANCTONF , NRLANCESTQ, IDATUAESTOQUE, IDERRO)
            VALUES
            (C_ITEMNF.CDFILIAL, C_ITEMNF.NREXPEDICAO, C_ITEMNF.NRPEDIDO, C_ITEMNF.NRITEMPED, V_NRLANCTONF, V_PRXLANCESTQ, V_ATUAESTOQUE, 'N');
         END IF;

         IF ((V_COUNTITEM = V_NRMAXITNF) AND (V_IDCONMAXITEM = 'S')) THEN
            V_NOVANOTA := '1';
         END IF;
      END LOOP;
   END LOOP;
   FOR C_NF IN (SELECT DISTINCT CDFILIAL, NREXPEDICAO, NRLANCTONF, NRLANCESTQ, IDATUAESTOQUE
                  FROM NFRELACITEMPED N
                 WHERE CDFILIAL = P_CDFILIAL
                   AND NREXPEDICAO = P_NREXPEDICAO)
   LOOP
      FOR C_EXCLUIRESERVA IN (SELECT DISTINCT CDFILIAL, NREXPEDICAO, NRPEDIDO, NRITEMPED
                                FROM NFRELACITEMPED
                               WHERE CDFILIAL = C_NF.CDFILIAL
                                 AND NREXPEDICAO = C_NF.NREXPEDICAO
                                 AND NRLANCTONF = C_NF.NRLANCTONF)
      LOOP
         DELETE FROM RESERPRODPED
         WHERE CDFILIAL = C_EXCLUIRESERVA.CDFILIAL
           AND NRPEDIDO = C_EXCLUIRESERVA.NRPEDIDO
           AND NRITEMPED = C_EXCLUIRESERVA.NRITEMPED;
      END LOOP;
      FOR C_PEDIDO IN (SELECT DISTINCT CDFILIAL, NRPEDIDO
                         FROM NFRELACITEMPED
                        WHERE CDFILIAL = C_NF.CDFILIAL
                          AND NREXPEDICAO = C_NF.NREXPEDICAO
                          AND NRLANCTONF = C_NF.NRLANCTONF)
      LOOP
         IF (V_IDLANENCARGOPED = 'S') THEN
            FOR C_ENCARGOS IN (SELECT EP.CDENCARGO, EP.VRENCARGOPED
                                 FROM ENCARGOPED EP
                                WHERE EP.CDFILIAL  = C_PEDIDO.CDFILIAL
                                  AND EP.NRPEDIDO  = C_PEDIDO.NRPEDIDO)
            LOOP
               IF (C_ENCARGOS.VRENCARGOPED > 0) THEN

                  GERA_NOTAFISCAL_ENCARGO(C_NF.CDFILIAL, C_NF.NRLANCTONF,C_ENCARGOS.CDENCARGO, C_ENCARGOS.VRENCARGOPED);
               END IF;
            END LOOP;
         END IF;
         FOR C_DADOSADIC IN (SELECT NRDADOADICPED, CDMENSAGEM, DSDADOADICPED
                               FROM DADOSADICPED
                              WHERE CDFILIAL = C_PEDIDO.CDFILIAL
                                AND NRPEDIDO = C_PEDIDO.NRPEDIDO)
         LOOP
            GERA_NOTAFISCAL_DADOSADIC (C_NF.CDFILIAL, C_NF.NRLANCTONF, NULL, NULL, C_DADOSADIC.DSDADOADICPED, C_DADOSADIC.CDMENSAGEM);
         END LOOP;
      END LOOP;
      GERA_NOTAFISCAL_FECHAMENTO(C_NF.CDFILIAL, C_NF.NRLANCTONF, V_DTEXPEDICAO, C_NF.NRLANCESTQ, 'S', V_CDOPERADOR, 'N', C_NF.IDATUAESTOQUE);
      V_ERROESTOQ := 0;
      IF (C_NF.IDATUAESTOQUE = 'S') THEN
         IF V_DTEXPEDICAO < V_DTPROCESSA THEN
            V_DTINICIAL := V_DTEXPEDICAO;
         ELSE
            V_DTINICIAL := V_DTEXPEDICAO;
         END IF;
         IF V_DTEXPEDICAO > V_DTPROCESSA THEN
            V_DTFINAL := V_DTEXPEDICAO;
         ELSE
            V_DTFINAL := V_DTPROCESSA;
         END IF;
         BEGIN
            SELECT MAX(DTLANCESTQ)
              INTO V_DTANTERIOR
              FROM LANCTOESTOQ
             WHERE CDFILIAL = C_NF.CDFILIAL
               AND NRLANCESTQ = C_NF.NRLANCESTQ;
         EXCEPTION
            WHEN NO_DATA_FOUND THEN BEGIN
               V_DTANTERIOR  := NULL;
            END;
         END;
         IF (V_DTANTERIOR IS NOT NULL) THEN
            IF(V_DTANTERIOR < V_DTINICIAL) THEN
               V_DTINICIAL := V_DTANTERIOR;
            END IF;
            IF(V_DTANTERIOR > V_DTFINAL) THEN
               V_DTFINAL := V_DTANTERIOR;
            END IF;
         END IF;
         ATUALIZA_ESTOQUE(C_NF.CDFILIAL, V_DTINICIAL, V_DTFINAL, C_NF.CDFILIAL, C_NF.NRLANCESTQ);
         V_IRREGESTOQ := 0;
         SELECT COUNT(1)
           INTO V_IRREGESTOQ
           FROM MOVILOTE
          WHERE NRMOVILOTE  = C_NF.CDFILIAL||C_NF.NRLANCESTQ;
         IF V_IRREGESTOQ > 0 THEN
            FOR C_MOVILOTE IN (SELECT CDFILIAL, CDALMOXARIFE, CDLOCALESTOQ, NRLOTEESTQ, NRSUBLOTE, CDPRODUTO
                                 FROM MOVILOTE
                                WHERE NRMOVILOTE  = C_NF.CDFILIAL||C_NF.NRLANCESTQ)
            LOOP
               V_ERROESTOQ := 1;
               IF (V_IDRETPROEXP = '2') THEN
                  FOR C_ITEMSESTOQ IN (SELECT NF.CDFILIAL, NF.NREXPEDICAO, NF.NRPEDIDO, NF.NRITEMPED,
                                              IE.CDCLIENTE, IE.CDFILIALPED, IE.CDPRODUTO, P.CDPRODESTO AS CDPRODMOVI, IE.QTITEMEXP, IC.QTSUBPROD AS QTITEMPROD,
                                              CDSUBPRALMOX AS CDALMOXEXP, CDSUBPRLOCAL AS CDLOCALEXP, IDSUBPRCTRLLOTE AS IDUTCTRLLOTE, IDSUBPRUTALMOX AS IDUTILALMOX
                                         FROM NFRELACITEMPED NF JOIN ITEMEXPEDICAO IE
                                                                  ON NF.CDFILIAL = IE.CDFILIAL
                                                                 AND NF.NREXPEDICAO = IE.NREXPEDICAO
                                                                 AND NF.NRPEDIDO = IE.NRPEDIDO
                                                                 AND NF.NRITEMPED = IE.NRITEMPED
                                                                JOIN COMPITEMEXP IC
                                                                  ON NF.CDFILIAL = IC.CDFILIAL
                                                                 AND NF.NREXPEDICAO = IC.NREXPEDICAO
                                                                 AND NF.NRPEDIDO = IC.NRPEDIDO
                                                                 AND NF.NRITEMPED = IC.NRITEMPED
                                                                JOIN PRODUTO P
                                                                  ON IC.CDSUBPROD = P.CDPRODUTO
                                        WHERE NF.CDFILIAL = C_NF.CDFILIAL
                                          AND NF.NREXPEDICAO = C_NF.NREXPEDICAO
                                          AND NF.NRLANCTONF = C_NF.NRLANCTONF
                                          AND IE.IDITEMCOMPPROD = 'S'
                                          AND P.CDPRODESTO = C_MOVILOTE.CDPRODUTO
                                       UNION ALL
                                       SELECT NF.CDFILIAL, NF.NREXPEDICAO, NF.NRPEDIDO, NF.NRITEMPED,
                                              IE.CDCLIENTE, IE.CDFILIALPED, IE.CDPRODUTO, IE.CDPRODMOVI, IE.QTITEMEXP, IE.QTITEMPROD,
                                              IE.CDALMOXEXP, IE.CDLOCALEXP, IE.IDUTCTRLLOTE, IE.IDUTILALMOX
                                         FROM NFRELACITEMPED NF JOIN ITEMEXPEDICAO IE
                                                                  ON NF.CDFILIAL = IE.CDFILIAL
                                                                 AND NF.NREXPEDICAO = IE.NREXPEDICAO
                                                                 AND NF.NRPEDIDO = IE.NRPEDIDO
                                                                 AND NF.NRITEMPED = IE.NRITEMPED
                                        WHERE NF.CDFILIAL = C_NF.CDFILIAL
                                          AND NF.NREXPEDICAO = C_NF.NREXPEDICAO
                                          AND NF.NRLANCTONF = C_NF.NRLANCTONF
                                          AND IE.IDITEMCOMPPROD = 'N'
                                          AND IE.CDPRODMOVI = C_MOVILOTE.CDPRODUTO)

                  LOOP
                     V_TOTALESTOQUE := 0;
                     FOR C_ESTOQ IN (SELECT P.CDFILIAL, P.CDALMOXARIFE, P.CDLOCALESTOQ, SUM(P.QTTOTALESTOQ) AS QTTOTALESTOQ
                                       FROM POSESTATU P LEFT JOIN LOTE L
                                                               ON P.CDFILIAL = L.CDFILIAL
                                                              AND P.CDPRODUTO = L.CDPRODUTO
                                                              AND P.NRLOTEESTQ = L.NRLOTEESTQ
                                                              AND P.NRSUBLOTE = L.NRSUBLOTE
                                                        JOIN LOCALESTOQUE LE
                                                          ON P.CDFILIAL = LE.CDFILIAL
                                                         AND P.CDALMOXARIFE = LE.CDALMOXARIFE
                                                         AND P.CDLOCALESTOQ = LE.CDLOCALESTOQ
                                      WHERE P.CDFILIAL = C_ITEMSESTOQ.CDFILIAL
                                        AND P.CDPRODUTO = C_ITEMSESTOQ.CDPRODMOVI
                                        AND P.CDALMOXARIFE = C_ITEMSESTOQ.CDALMOXEXP
                                        AND P.CDLOCALESTOQ = C_ITEMSESTOQ.CDLOCALEXP
                                        AND ((C_ITEMSESTOQ.IDUTCTRLLOTE = 'S') AND (TRUNC(L.DTVALILOTE) >= TRUNC(SYSDATE)) OR ((C_ITEMSESTOQ.IDUTCTRLLOTE = 'N')))
                                        AND ((C_ITEMSESTOQ.IDUTILALMOX = 'N') OR (LE.IDDISPCONPED = 'S'))
                                     GROUP BY P.CDFILIAL, P.CDALMOXARIFE, P.CDLOCALESTOQ)
                     LOOP
                        V_TOTALESTOQUE := V_TOTALESTOQUE + C_ESTOQ.QTTOTALESTOQ;
                     END LOOP;
                     UPDATE ITEMEXPEDICAO
                        SET IDERRO = 'S'
                      WHERE CDFILIAL = C_ITEMSESTOQ.CDFILIAL
                        AND NREXPEDICAO = C_ITEMSESTOQ.NREXPEDICAO
                        AND NRPEDIDO = C_ITEMSESTOQ.NRPEDIDO
                        AND NRITEMPED = C_ITEMSESTOQ.NRITEMPED;
                     SELECT NVL(MAX(NRSEQERROITEM),0)
                       INTO V_NRSEQ
                       FROM ERROITEMEXP
                      WHERE CDFILIAL = C_ITEMSESTOQ.CDFILIAL
                        AND NREXPEDICAO = C_ITEMSESTOQ.NREXPEDICAO
                        AND NRPEDIDO = C_ITEMSESTOQ.NRPEDIDO;
                     V_NRSEQ := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_NRSEQ) + 1)),10,0);
                     SELECT COUNT(1)
                       INTO V_ERRO
                       FROM ERROITEMEXP
                      WHERE CDFILIAL = C_ITEMSESTOQ.CDFILIAL
                        AND NREXPEDICAO = C_ITEMSESTOQ.NREXPEDICAO
                        AND NRPEDIDO = C_ITEMSESTOQ.NRPEDIDO
                        AND NRITEMPED = C_ITEMSESTOQ.NRITEMPED
                        AND DSERRO = 'IRREGESTOQ';
                     IF (V_ERRO = 0) THEN
                        INSERT INTO ERROITEMEXP
                        (CDFILIAL, NREXPEDICAO, NRPEDIDO, NRITEMPED, NRSEQERROITEM, DSERRO, CDCLIENTE, CDFILIALPED, CDPRODUTO, CDPRODMOVI,  QTITEMEXP, QTTOTAITEMPROD, QTTOTAESTOQ)
                        VALUES
                        (C_ITEMSESTOQ.CDFILIAL, C_ITEMSESTOQ.NREXPEDICAO, C_ITEMSESTOQ.NRPEDIDO, C_ITEMSESTOQ.NRITEMPED, V_NRSEQ, 'IRREGESTOQ', C_ITEMSESTOQ.CDCLIENTE, C_ITEMSESTOQ.CDFILIALPED, C_ITEMSESTOQ.CDPRODUTO, C_ITEMSESTOQ.CDPRODMOVI, C_ITEMSESTOQ.QTITEMEXP, C_ITEMSESTOQ.QTITEMPROD, V_TOTALESTOQUE);
                     END IF;
                  END LOOP;
               ELSE
                  FOR C_ITEMSESTOQ IN (SELECT NF.CDFILIAL, NF.NREXPEDICAO, NF.NRPEDIDO, NF.NRITEMPED,
                                              IE.CDCLIENTE, IE.CDFILIALPED, IE.CDPRODUTO, IE.CDPRODMOVI, IE.QTITEMEXP, IE.QTITEMPROD,
                                              IE.CDALMOXEXP, IE.CDLOCALEXP, IE.IDUTCTRLLOTE, IE.IDUTILALMOX
                                         FROM NFRELACITEMPED NF JOIN ITEMEXPEDICAO IE
                                                                  ON NF.CDFILIAL = IE.CDFILIAL
                                                                 AND NF.NREXPEDICAO = IE.NREXPEDICAO
                                                                 AND NF.NRPEDIDO = IE.NRPEDIDO
                                                                 AND NF.NRITEMPED = IE.NRITEMPED
                                        WHERE NF.CDFILIAL = C_NF.CDFILIAL
                                          AND NF.NREXPEDICAO = C_NF.NREXPEDICAO
                                          AND NF.NRLANCTONF = C_NF.NRLANCTONF
                                          AND IE.CDPRODMOVI = C_MOVILOTE.CDPRODUTO)
                  LOOP
                     V_TOTALESTOQUE := 0;
                     FOR C_ESTOQ IN (SELECT P.CDFILIAL, P.CDALMOXARIFE, P.CDLOCALESTOQ, SUM(P.QTTOTALESTOQ) AS QTTOTALESTOQ
                                       FROM POSESTATU P LEFT JOIN LOTE L
                                                               ON P.CDFILIAL = L.CDFILIAL
                                                              AND P.CDPRODUTO = L.CDPRODUTO
                                                              AND P.NRLOTEESTQ = L.NRLOTEESTQ
                                                              AND P.NRSUBLOTE = L.NRSUBLOTE
                                                        JOIN LOCALESTOQUE LE
                                                          ON P.CDFILIAL = LE.CDFILIAL
                                                         AND P.CDALMOXARIFE = LE.CDALMOXARIFE
                                                         AND P.CDLOCALESTOQ = LE.CDLOCALESTOQ
                                      WHERE P.CDFILIAL = C_ITEMSESTOQ.CDFILIAL
                                        AND P.CDPRODUTO = C_ITEMSESTOQ.CDPRODMOVI
                                        AND P.CDALMOXARIFE = C_ITEMSESTOQ.CDALMOXEXP
                                        AND P.CDLOCALESTOQ = C_ITEMSESTOQ.CDLOCALEXP
                                        AND ((C_ITEMSESTOQ.IDUTCTRLLOTE = 'S') AND (TRUNC(L.DTVALILOTE) >= TRUNC(SYSDATE)) OR ((C_ITEMSESTOQ.IDUTCTRLLOTE = 'N')))
                                        AND ((C_ITEMSESTOQ.IDUTILALMOX = 'N') OR (LE.IDDISPCONPED = 'S'))
                                     GROUP BY P.CDFILIAL, P.CDALMOXARIFE, P.CDLOCALESTOQ)
                     LOOP
                        V_TOTALESTOQUE := V_TOTALESTOQUE + C_ESTOQ.QTTOTALESTOQ;
                     END LOOP;
                     UPDATE ITEMEXPEDICAO
                        SET IDERRO = 'S'
                      WHERE CDFILIAL = C_ITEMSESTOQ.CDFILIAL
                        AND NREXPEDICAO = C_ITEMSESTOQ.NREXPEDICAO
                        AND NRPEDIDO = C_ITEMSESTOQ.NRPEDIDO
                        AND NRITEMPED = C_ITEMSESTOQ.NRITEMPED;
                     SELECT NVL(MAX(NRSEQERROITEM),0)
                       INTO V_NRSEQ
                       FROM ERROITEMEXP
                      WHERE CDFILIAL = C_ITEMSESTOQ.CDFILIAL
                        AND NREXPEDICAO = C_ITEMSESTOQ.NREXPEDICAO
                        AND NRPEDIDO = C_ITEMSESTOQ.NRPEDIDO;
                     V_NRSEQ := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_NRSEQ) + 1)),10,0);
                     SELECT COUNT(1)
                       INTO V_ERRO
                       FROM ERROITEMEXP
                      WHERE CDFILIAL = C_ITEMSESTOQ.CDFILIAL
                        AND NREXPEDICAO = C_ITEMSESTOQ.NREXPEDICAO
                        AND NRPEDIDO = C_ITEMSESTOQ.NRPEDIDO
                        AND NRITEMPED = C_ITEMSESTOQ.NRITEMPED
                        AND DSERRO = 'IRREGESTOQ';
                     IF (V_ERRO = 0) THEN
                        INSERT INTO ERROITEMEXP
                        (CDFILIAL, NREXPEDICAO, NRPEDIDO, NRITEMPED, NRSEQERROITEM, DSERRO, CDCLIENTE, CDFILIALPED, CDPRODUTO, CDPRODMOVI,  QTITEMEXP, QTTOTAITEMPROD, QTTOTAESTOQ)
                        VALUES
                        (C_ITEMSESTOQ.CDFILIAL, C_ITEMSESTOQ.NREXPEDICAO, C_ITEMSESTOQ.NRPEDIDO, C_ITEMSESTOQ.NRITEMPED, V_NRSEQ, 'IRREGESTOQ', C_ITEMSESTOQ.CDCLIENTE, C_ITEMSESTOQ.CDFILIALPED, C_ITEMSESTOQ.CDPRODUTO, C_ITEMSESTOQ.CDPRODMOVI, C_ITEMSESTOQ.QTITEMEXP, C_ITEMSESTOQ.QTITEMPROD, V_TOTALESTOQUE);
                     END IF;
                  END LOOP;
               END IF;
               UPDATE NFRELACITEMPED
                  SET IDERRO = 'S'
                WHERE CDFILIAL = C_NF.CDFILIAL
                  AND NREXPEDICAO = C_NF.NREXPEDICAO
                  AND NRLANCTONF = C_NF.NRLANCTONF;

               UPDATE ITLANCTOEST
                  SET IDEVOLESTOQ = 'N'
                WHERE CDFILIAL = C_NF.CDFILIAL
                  AND NRLANCESTQ = C_NF.NRLANCESTQ
                  AND CDPRODUTO = C_MOVILOTE.CDPRODUTO
                  AND CDALMOXARIFE = C_MOVILOTE.CDALMOXARIFE
                  AND CDLOCALESTOQ = C_MOVILOTE.CDLOCALESTOQ
                  AND NRLOTEESTQ  = C_MOVILOTE.NRLOTEESTQ
                  AND NRSUBLOTE = C_MOVILOTE.NRSUBLOTE;

            END LOOP;

            ATUALIZA_ESTOQUE(C_NF.CDFILIAL, V_DTINICIAL, V_DTFINAL, C_NF.CDFILIAL, C_NF.NRLANCESTQ);
            DELETA_NF(C_NF.CDFILIAL, C_NF.NRLANCTONF);
            DELETE FROM MOVILOTE
            WHERE NRMOVILOTE  = C_NF.CDFILIAL||C_NF.NRLANCESTQ;
         END IF;
      END IF;
      IF (V_ERROESTOQ = 0) THEN
         FOR C_PEDIDO IN (SELECT DISTINCT CDFILIAL, NREXPEDICAO, NRPEDIDO
                            FROM NFRELACITEMPED R
                           WHERE CDFILIAL = C_NF.CDFILIAL
                             AND NREXPEDICAO = C_NF.NREXPEDICAO
                             AND NRLANCTONF = C_NF.NRLANCTONF)
         LOOP
            SELECT COUNT(1)
              INTO V_ITEMPA
              FROM ITEMPEDIDO IT
             WHERE IT.CDFILIAL = C_PEDIDO.CDFILIAL
               AND IT.NRPEDIDO = C_PEDIDO.NRPEDIDO
               AND IT.IDSITITEMPED IN ('1','2','3','7');
            IF (V_ITEMPA > 0) THEN
               V_IDSITPEDIDO := '6';
            ELSE
               V_IDSITPEDIDO := '3';
            END IF;
            UPDATE PEDIDO
               SET IDSITPEDIDO = V_IDSITPEDIDO
             WHERE CDFILIAL = C_PEDIDO.CDFILIAL
               AND NRPEDIDO = C_PEDIDO.NRPEDIDO;
         END LOOP;
         BEGIN
            SELECT MAX(P.CDVENDEDOR)
              INTO V_CDVENDEDOR
              FROM NFRELACITEMPED R JOIN PEDIDO P
                                      ON R.CDFILIAL = P.CDFILIAL
                                     AND R.NRPEDIDO = P.NRPEDIDO
             WHERE R.CDFILIAL = C_NF.CDFILIAL
               AND R.NREXPEDICAO = C_NF.NREXPEDICAO
               AND R.NRLANCTONF = C_NF.NRLANCTONF;
         EXCEPTION
            WHEN NO_DATA_FOUND THEN BEGIN
               V_CDVENDEDOR  := NULL;
            END;
         END;

         UPDATE NOTAFISCAL
            SET NREXPEDICAO = C_NF.NREXPEDICAO,
                CDVENDEDOR = V_CDVENDEDOR
          WHERE CDFILIAL = C_NF.CDFILIAL
            AND NRLANCTONF = C_NF.NRLANCTONF;
         /*--------------------------REPLICACAO------------------------*/
         IF (V_IDGERAREPLIC = '2') THEN
            IF ((P_CDSERIEENTR IS NOT NULL) AND (P_CDTPOPERENTR IS NOT NULL)) THEN
               BEGIN
                  SELECT MAX(P.CDFILIALPED), MAX(P.CDCLIENTE)
                    INTO V_CDFILIDEST, V_CDCLIENTEREPLIC
                    FROM ITEMPRODUTO I JOIN PEDIDO P
                                         ON I.CDFILIALPED = P.CDFILIAL
                                        AND I.NRPEDIDO = P.NRPEDIDO
                   WHERE I.CDFILIAL = C_NF.CDFILIAL
                     AND I.NRLANCTONF = C_NF.NRLANCTONF;
               EXCEPTION
                  WHEN NO_DATA_FOUND THEN BEGIN
                     V_CDFILIDEST  := NULL;
                     V_CDCLIENTEREPLIC := NULL;
                  END;
               END;
               IF (V_CDFILIDEST IS NULL) THEN
                  BEGIN
                     SELECT NRINSJURCLIE
                       INTO V_NRINSJURCLIE
                       FROM CLIENTE
                      WHERE CDCLIENTE = V_CDCLIENTEREPLIC;
                  EXCEPTION
                     WHEN NO_DATA_FOUND THEN BEGIN
                        V_NRINSJURCLIE := NULL;
                      END;
                  END;
                  BEGIN
                     SELECT CDFILIAL
                       INTO V_CDFILIDEST
                       FROM FILIAL
                      WHERE NRINSJURFILI = V_NRINSJURCLIE;
                  EXCEPTION
                     WHEN NO_DATA_FOUND THEN BEGIN
                        V_CDFILIDEST := NULL;
                      END;
                  END;
               END IF;
               IF (V_CDFILIDEST IS NOT NULL) THEN
                  BEGIN
                     SELECT CDTIPOOPER
                       INTO V_CDTIPOOPERORIG
                       FROM NOTAFISCAL
                      WHERE CDFILIAL = C_NF.CDFILIAL
                        AND NRLANCTONF = C_NF.NRLANCTONF;
                  EXCEPTION
                     WHEN NO_DATA_FOUND THEN BEGIN
                        V_CDTIPOOPERORIG  := NULL;
                     END;
                  END;
                  GERAR_EXP_REPLICACAO(C_NF.CDFILIAL, C_NF.NRLANCTONF, V_CDTIPOOPERORIG, V_CDFILIDEST, P_CDTPOPERENTR, P_CDSERIEENTR, NULL);
                  V_ERROESTOQ := 0;
                  BEGIN
                     SELECT MAX(CDFILIAL), MAX(CDFILIRECE), MAX(NRLANCTONF), MAX(DTENTRSAID), MAX(IDENTRSAIDOP), MAX(CDTIPOOPER)
                       INTO V_CDFILIALREPLIC, V_CDFILIRECDREPLIC, V_NRLANCTONFREPLIC, V_DTENTRSAIDREPLIC, V_IDENTRSAIDOPREPLIC, V_CDTIPOOPERREPLIC
                       FROM NOTAFISCAL
                      WHERE IDDOCREPLIC = C_NF.CDFILIAL||C_NF.NRLANCTONF;
                  EXCEPTION
                     WHEN NO_DATA_FOUND THEN BEGIN
                        V_CDFILIALREPLIC := NULL;
                        V_CDFILIRECDREPLIC  := NULL;
                        V_NRLANCTONFREPLIC := NULL;
                        V_DTENTRSAIDREPLIC := NULL;
                        V_IDENTRSAIDOPREPLIC := NULL;
                        V_CDTIPOOPERREPLIC := NULL;
                     END;
                  END;
                  IF (V_NRLANCTONFREPLIC IS NOT NULL) THEN
                     BEGIN
                        SELECT NVL(IDCTRLESTQ,'S'),TRUNC(NVL(DTPROCESSA,SYSDATE))
                          INTO V_IDCTRLESTQ, V_DTPROCESSA
                          FROM PARAMFILIAL
                         WHERE CDFILIAL = V_CDFILIRECDREPLIC;
                     EXCEPTION
                        WHEN NO_DATA_FOUND THEN BEGIN
                           V_IDCTRLESTQ := 'S';
                           V_DTPROCESSA := TRUNC(SYSDATE);
                        END;
                     END;
                     IF (V_IDCTRLESTQ = 'S') THEN
                        BEGIN
                           SELECT NVL(IDATUESTTPOP, 'S')
                             INTO V_IDATUESTTPOPREPLIC
                             FROM TIPOOPERAC
                            WHERE IDENTRSAIDOP = V_IDENTRSAIDOPREPLIC
                              AND CDTIPOOPER = V_CDTIPOOPERREPLIC;
                        EXCEPTION
                           WHEN NO_DATA_FOUND THEN
                              V_IDATUESTTPOPREPLIC := 'S';
                        END;
                        IF (V_IDATUESTTPOPREPLIC = 'S') THEN
                           IF V_DTENTRSAIDREPLIC < V_DTPROCESSA THEN
                              V_DTINICIAL := V_DTENTRSAIDREPLIC;
                           ELSE
                              V_DTINICIAL := V_DTENTRSAIDREPLIC;
                           END IF;
                           IF V_DTENTRSAIDREPLIC > V_DTPROCESSA THEN
                              V_DTFINAL := V_DTENTRSAIDREPLIC;
                           ELSE
                              V_DTFINAL := V_DTPROCESSA;
                           END IF;
                           BEGIN
                              SELECT MAX(NRLANCESTQ)
                                INTO V_NRLANCESTQREPLIC
                                FROM LANCTOESTOQ
                               WHERE CDFILIAL = V_CDFILIALREPLIC
                                 AND NRLANCTONF = V_NRLANCTONFREPLIC;
                           EXCEPTION
                              WHEN NO_DATA_FOUND THEN BEGIN
                                 V_NRLANCESTQREPLIC := NULL;
                              END;
                           END;
                           BEGIN
                              SELECT MAX(DTLANCESTQ)
                                INTO V_DTANTERIOR
                                FROM LANCTOESTOQ
                               WHERE CDFILIAL = V_CDFILIALREPLIC
                                 AND NRLANCESTQ = V_NRLANCESTQREPLIC;
                           EXCEPTION
                              WHEN NO_DATA_FOUND THEN BEGIN
                                 V_DTANTERIOR  := NULL;
                              END;
                           END;
                           IF (V_DTANTERIOR IS NOT NULL) THEN
                              IF(V_DTANTERIOR < V_DTINICIAL) THEN
                                 V_DTINICIAL := V_DTANTERIOR;
                              END IF;
                              IF(V_DTANTERIOR > V_DTFINAL) THEN
                                 V_DTFINAL := V_DTANTERIOR;
                              END IF;
                           END IF;
                           ATUALIZA_ESTOQUE(V_CDFILIALREPLIC, V_DTINICIAL, V_DTFINAL, V_CDFILIALREPLIC, V_NRLANCESTQREPLIC);
                           V_IRREGESTOQ := 0;
                           SELECT COUNT(1)
                             INTO V_IRREGESTOQ
                             FROM MOVILOTE
                            WHERE NRMOVILOTE  = V_CDFILIALREPLIC||V_NRLANCESTQREPLIC;
                           IF V_IRREGESTOQ > 0 THEN
                              FOR C_MOVILOTE IN (SELECT CDFILIAL, CDALMOXARIFE, CDLOCALESTOQ, NRLOTEESTQ, NRSUBLOTE, CDPRODUTO
                                                   FROM MOVILOTE
                                                  WHERE NRMOVILOTE  = V_CDFILIALREPLIC||V_NRLANCESTQREPLIC)
                              LOOP
                                 V_ERROESTOQ := 1;
                                 FOR C_ITEMSESTOQ IN (SELECT NF.CDFILIAL, NF.NREXPEDICAO, NF.NRPEDIDO, NF.NRITEMPED,
                                                             IE.CDCLIENTE, IE.CDFILIALPED, IE.CDPRODUTO, IE.CDPRODMOVI, IE.QTITEMEXP, IE.QTITEMPROD,
                                                             IE.CDALMOXEXP, IE.CDLOCALEXP, IE.IDUTCTRLLOTE, IE.IDUTILALMOX
                                                        FROM NFRELACITEMPED NF JOIN ITEMEXPEDICAO IE
                                                                                 ON NF.CDFILIAL = IE.CDFILIAL
                                                                                AND NF.NREXPEDICAO = IE.NREXPEDICAO
                                                                                AND NF.NRPEDIDO = IE.NRPEDIDO
                                                                                AND NF.NRITEMPED = IE.NRITEMPED
                                                       WHERE NF.CDFILIAL = C_NF.CDFILIAL
                                                         AND NF.NREXPEDICAO = C_NF.NREXPEDICAO
                                                         AND NF.NRLANCTONF = C_NF.NRLANCTONF
                                                         AND IE.CDPRODMOVI = C_MOVILOTE.CDPRODUTO)
                                 LOOP
                                     UPDATE ITEMEXPEDICAO
                                       SET IDERRO = 'S'
                                     WHERE CDFILIAL = C_ITEMSESTOQ.CDFILIAL
                                       AND NREXPEDICAO = C_ITEMSESTOQ.NREXPEDICAO
                                       AND NRPEDIDO = C_ITEMSESTOQ.NRPEDIDO
                                       AND NRITEMPED = C_ITEMSESTOQ.NRITEMPED;
                                    SELECT NVL(MAX(NRSEQERROITEM),0)
                                      INTO V_NRSEQ
                                      FROM ERROITEMEXP
                                     WHERE CDFILIAL = C_ITEMSESTOQ.CDFILIAL
                                       AND NREXPEDICAO = C_ITEMSESTOQ.NREXPEDICAO
                                       AND NRPEDIDO = C_ITEMSESTOQ.NRPEDIDO;
                                    V_NRSEQ := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_NRSEQ) + 1)),10,0);
                                    SELECT COUNT(1)
                                      INTO V_ERRO
                                      FROM ERROITEMEXP
                                     WHERE CDFILIAL = C_ITEMSESTOQ.CDFILIAL
                                       AND NREXPEDICAO = C_ITEMSESTOQ.NREXPEDICAO
                                       AND NRPEDIDO = C_ITEMSESTOQ.NRPEDIDO
                                       AND NRITEMPED = C_ITEMSESTOQ.NRITEMPED
                                       AND DSERRO = 'REPLICIRREGESTOQ';
                                    IF (V_ERRO = 0) THEN
                                       INSERT INTO ERROITEMEXP
                                       (CDFILIAL, NREXPEDICAO, NRPEDIDO, NRITEMPED, NRSEQERROITEM, DSERRO, CDCLIENTE, CDFILIALPED, CDPRODUTO, CDPRODMOVI,  QTITEMEXP, QTTOTAITEMPROD, QTTOTAESTOQ)
                                       VALUES
                                       (C_ITEMSESTOQ.CDFILIAL, C_ITEMSESTOQ.NREXPEDICAO, C_ITEMSESTOQ.NRPEDIDO, C_ITEMSESTOQ.NRITEMPED, V_NRSEQ, 'REPLICIRREGESTOQ', C_ITEMSESTOQ.CDCLIENTE, C_ITEMSESTOQ.CDFILIALPED, C_ITEMSESTOQ.CDPRODUTO, C_ITEMSESTOQ.CDPRODMOVI, C_ITEMSESTOQ.QTITEMEXP, C_ITEMSESTOQ.QTITEMPROD, 0);
                                    END IF;
                                 END LOOP;
                                 UPDATE NFRELACITEMPED
                                    SET IDERRO = 'S'
                                  WHERE CDFILIAL = C_NF.CDFILIAL
                                    AND NREXPEDICAO = C_NF.NREXPEDICAO
                                    AND NRLANCTONF = C_NF.NRLANCTONF;

                                 UPDATE ITLANCTOEST
                                    SET IDEVOLESTOQ = 'N'
                                  WHERE CDFILIAL = V_CDFILIALREPLIC
                                    AND NRLANCESTQ = V_NRLANCESTQREPLIC
                                    AND CDPRODUTO = C_MOVILOTE.CDPRODUTO
                                    AND CDALMOXARIFE = C_MOVILOTE.CDALMOXARIFE
                                    AND CDLOCALESTOQ = C_MOVILOTE.CDLOCALESTOQ
                                    AND NRLOTEESTQ  = C_MOVILOTE.NRLOTEESTQ
                                    AND NRSUBLOTE = C_MOVILOTE.NRSUBLOTE;

                              END LOOP;
                              DELETA_NF(C_NF.CDFILIAL, C_NF.NRLANCTONF);

                              ATUALIZA_ESTOQUE(V_CDFILIALREPLIC, V_DTINICIAL, V_DTFINAL, V_CDFILIALREPLIC, V_NRLANCESTQREPLIC);
                              DELETA_NF(V_CDFILIALREPLIC, V_NRLANCTONFREPLIC);
                              
                              DELETE FROM MOVILOTE
                              WHERE NRMOVILOTE  = V_CDFILIALREPLIC||V_NRLANCESTQREPLIC;
                           END IF;
                        END IF;
                     END IF;
                     IF (V_ERROESTOQ = 0) THEN
                        UPDATE NFRELACITEMPED
                           SET NRLANCTONFREPLIC = V_NRLANCTONFREPLIC
                         WHERE CDFILIAL = C_NF.CDFILIAL
                           AND NREXPEDICAO = C_NF.NREXPEDICAO
                           AND NRLANCTONF = C_NF.NRLANCTONF;
                     END IF;
                  END IF;
               END IF;
            END IF;
         END IF;
      END IF;
   END LOOP;
   FOR C_RELACNF IN (SELECT N.CDFILIAL, N.NREXPEDICAO, N.NRPEDIDO, N.NRITEMPED, N.NRLANCTONF, I.CDPRODUTO
                       FROM NFRELACITEMPED N JOIN ITEMPEDIDO I
                                               ON N.CDFILIAL = I.CDFILIAL
                                              AND N.NRPEDIDO = I.NRPEDIDO
                                              AND N.NRITEMPED = I.NRITEMPED
                      WHERE N.CDFILIAL = P_CDFILIAL
                        AND N.NREXPEDICAO = P_NREXPEDICAO
                        AND N.IDERRO = 'N')
   LOOP
      INSERT INTO ITEMPEDIDONF (CDFILIAL, NRPEDIDO, NRITEMPED, NREXPEDICAO,NRLANCTONF,CDPRODUTO,DTINCLUSAO)
                        VALUES (C_RELACNF.CDFILIAL, C_RELACNF.NRPEDIDO, C_RELACNF.NRITEMPED,C_RELACNF.NREXPEDICAO, C_RELACNF.NRLANCTONF, C_RELACNF.CDPRODUTO, SYSDATE);
   END LOOP;
   V_QTNF := 0;
   SELECT COUNT(1)
     INTO V_QTNF
     FROM NFRELACITEMPED
    WHERE CDFILIAL = P_CDFILIAL
      AND NREXPEDICAO = P_NREXPEDICAO;
   V_NAOOK := 0;
   SELECT COUNT(1)
     INTO V_NAOOK
     FROM NFRELACITEMPED
    WHERE CDFILIAL = P_CDFILIAL
      AND NREXPEDICAO = P_NREXPEDICAO
      AND IDERRO = 'S';
   IF (V_NAOOK = V_QTNF) THEN
      P_IDSTATUSEXP := 'NAO_OK';
   ELSE
      V_OKNF := 0;
      SELECT COUNT(1)
        INTO V_OKNF
        FROM NFRELACITEMPED
       WHERE CDFILIAL = P_CDFILIAL
         AND NREXPEDICAO = P_NREXPEDICAO
         AND IDERRO = 'S';
      V_OKITEMPED := 0;
      SELECT COUNT(1)
        INTO V_OKITEMPED
        FROM ITEMEXPEDICAO
       WHERE CDFILIAL = P_CDFILIAL
         AND NREXPEDICAO = P_NREXPEDICAO
         AND IDERRO = 'S';
      IF ((V_OKNF = 0) AND (V_OKITEMPED = 0)) THEN
         P_IDSTATUSEXP := 'OK_0';
      ELSE
         P_IDSTATUSEXP := 'OK_1';
      END IF;
   END IF;
   UPDATE EXPEDICAO
      SET IDSTATUSEXP = P_IDSTATUSEXP
    WHERE CDFILIAL = P_CDFILIAL
      AND NREXPEDICAO = P_NREXPEDICAO;
END;
/
