create or replace PROCEDURE LIQUIDAR_PEDIDOITEM
(P_CDFILIAL       IN PEDIDO.CDFILIAL%TYPE,
 P_NRPEDIDO       IN PEDIDO.NRPEDIDO%TYPE,
 P_NRITEMPED      IN ITEMPEDIDO.NRITEMPED%TYPE  DEFAULT NULL,
 P_TIPOEXP        IN EXPRELACITEMPPED.IDTIPOEXP%TYPE,
 P_CDOPERADOR     IN OPERADOR.CDOPERADOR%TYPE,
 P_CDJUSTEXCHIS   IN ITEMPEDIDO.CDJUSTEXCHIS%TYPE,
 P_IDISCUSTOM     IN PEDIDO.IDSITPEDIDO%TYPE,
 P_IDRETORNOLIQ OUT PEDIDO.IDSITPEDIDO%TYPE)
IS
   V_ERRO              INTEGER;
   V_IDEVOLESTLIQMAN   PARAMPEDIDO.IDEVOLESTLIQMAN%TYPE;
   V_TITEM             PARAMPEDIDO.IDEVOLESTLIQMAN%TYPE;
   V_NRITEMPED         ITEMPEDIDO.NRITEMPED%TYPE;
   V_IDSITPEDIDO       PEDIDO.IDSITPEDIDO%TYPE;
   V_NOVOCODIGO        NOVOCODIGO.NRSEQUENCIAL%TYPE;
   V_NRLANCESTQ        LANCTOESTOQ.NRLANCESTQ%TYPE;
   V_IDCTRLESTQ        PARAMFILIAL.IDCTRLESTQ%TYPE;
   V_IDUTILALMOX       PARAMFILIAL.IDUTILALMOX%TYPE;
   V_CDALMOXARIFE      PARAMFILIAL.CDALMOXARIFE%TYPE;
   V_IDUTILLCESTQ      PARAMFILIAL.IDUTILLCESTQ%TYPE;
   V_CDLOCALESTOQ      PARAMFILIAL.CDLOCALESTOQ%TYPE;
   V_CDTIPCUSTEST      PARAMFILIAL.CDTIPCUSTEST%TYPE;
   V_DTPROCESSA        PARAMFILIAL.DTPROCESSA%TYPE;
   V_IDUTILDTPROC      PARAMGERAL.IDUTILDTPROC%TYPE;
   V_QTSALDSAID        ITEMPEDIDO.QTITEMPED%TYPE;
   V_IDCNTRESTOQ       PRODFILI.IDCNTRESTOQ%TYPE;
   V_CDALMOXPRCLI      PRODFILI.CDALMOXPRCLI%TYPE;
   V_CDLOCALPRCLI      PRODFILI.CDLOCALPRCLI%TYPE;
   V_IDUTCTRLLOTE      PRODFILI.IDUTCTRLLOTE%TYPE;
   V_CDALMOX           ALMOXARIFADO.CDALMOXARIFE%TYPE;
   V_CDLOCAL           LOCALESTOQUE.CDLOCALESTOQ%TYPE;
   V_QTTOTALESTOQ      ITEMPEDIDO.QTITEMPED%TYPE;
   V_NRSEQUITEM        ITLANCTOEST.NRSEQUITEM%TYPE;
   V_NRLOTEESTQ        LOTE.NRLOTEESTQ%TYPE;
   V_NRSUBLOTE         LOTE.NRSUBLOTE%TYPE;
   V_VRCUSTO           ITLANCTOEST.VRUNILANCTO%TYPE;
   V_QTITEM            ITEMPEDIDO.QTITEMPED%TYPE;
   V_DTINICIAL         DATE;
   V_DTFINAL           DATE;
   V_DTANTERIOR        DATE;
   V_NRSEQERROITEM     ERROLIQUIDACAO.NRSEQERROITEM%TYPE;
   V_COUNT_ITEM        INTEGER;
   V_COUNT_PEND        INTEGER;
   V_COUNT_CONF        INTEGER;
   V_COUNT_APRO        INTEGER;
   V_COUNT_EXP         INTEGER;
   V_COUNT_CANC        INTEGER;
   V_COUNT_LIQU        INTEGER;
   V_COUNT_PAAT        INTEGER;
   V_NOVASITPEDIDO     PEDIDO.IDSITPEDIDO%TYPE;

BEGIN
   V_ERRO := 0;
   DELETE FROM ERROLIQUIDACAO
   WHERE CDFILIAL = P_CDFILIAL
     AND NRPEDIDO = P_NRPEDIDO;
   BEGIN
      SELECT IDEVOLESTLIQMAN
        INTO V_IDEVOLESTLIQMAN
        FROM PARAMPEDIDO
       WHERE CDFILIAL = P_CDFILIAL;
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_IDEVOLESTLIQMAN := 'N';
   END;
   IF P_TIPOEXP = 'PEDIDO' THEN
      V_TITEM := 'T';
      V_NRITEMPED := NULL;
   ELSE
      V_NRITEMPED := P_NRITEMPED;
      V_TITEM := '';
   END IF;
   BEGIN
      SELECT IDSITPEDIDO
        INTO V_IDSITPEDIDO
        FROM PEDIDO
       WHERE CDFILIAL = P_CDFILIAL
         AND NRPEDIDO = P_NRPEDIDO;
   EXCEPTION
      WHEN NO_DATA_FOUND THEN BEGIN
         V_IDSITPEDIDO := '1';
      END;
   END;
   IF V_IDEVOLESTLIQMAN = 'S' THEN
      NOVO_CODIGO_NT('LANCTOEST' || P_CDFILIAL, V_NOVOCODIGO);
      V_NRLANCESTQ := SUBSTR(V_NOVOCODIGO, 11, 10);

      INSERT INTO LANCTOESTOQ
         (CDFILIAL, NRLANCESTQ, DTLANCESTQ, IDTPLANCTO, CDFILIMOVI,CDOPERADOR, DSLANCESTQ)
      VALUES
         (P_CDFILIAL, V_NRLANCESTQ, TRUNC(SYSDATE), '3', P_CDFILIAL, P_CDOPERADOR, ('Liquida'||chr(231)||chr(227)||'o Manual') );

      BEGIN
         SELECT NVL(IDCTRLESTQ,'S'),   NVL(IDUTILALMOX,'N'),   CDALMOXARIFE,   NVL(IDUTILLCESTQ,'N'),   CDLOCALESTOQ,
                NVL(CDTIPCUSTEST, '3'), NVL(TRUNC(NVL(DTPROCESSA,SYSDATE)), TRUNC(SYSDATE))
           INTO V_IDCTRLESTQ, V_IDUTILALMOX, V_CDALMOXARIFE, V_IDUTILLCESTQ, V_CDLOCALESTOQ,
                V_CDTIPCUSTEST, V_DTPROCESSA
           FROM PARAMFILIAL
          WHERE CDFILIAL = P_CDFILIAL;
      EXCEPTION
         WHEN NO_DATA_FOUND THEN BEGIN
            V_IDCTRLESTQ   := 'S';
            V_IDUTILALMOX  := 'N';
            V_CDALMOXARIFE := ' ';
            V_IDUTILLCESTQ := 'N';
            V_CDLOCALESTOQ := ' ';
            V_CDTIPCUSTEST := '3';
            V_DTPROCESSA   := SYSDATE;
         END;
      END;
      BEGIN
         SELECT NVL(IDUTILDTPROC, 'S')
           INTO V_IDUTILDTPROC
           FROM PARAMGERAL
          WHERE CDPARGERAL = '0001';
      EXCEPTION
         WHEN NO_DATA_FOUND THEN BEGIN
            V_IDUTILDTPROC := 'N';
         END;
      END;
      IF V_IDUTILDTPROC = 'N' THEN
         V_DTPROCESSA := SYSDATE;
      END IF;
   END IF;


   FOR C_ITEM IN (SELECT I.CDFILIAL, I.NRPEDIDO, I.NRITEMPED, I.CDPRODUTO, P.CDPRODESTO, P.VRFATOCONV,
                         I.QTITEMPED, I.QTEXPITEMPED,  ((I.QTITEMPED - I.QTEXPITEMPED)*VRFATOCONV) AS QTITEMPEDEXP,
                         I.IDSITITEMPED
                    FROM ITEMPEDIDO I JOIN PRODUTO P
                                        ON I.CDPRODUTO = P.CDPRODUTO
                   WHERE CDFILIAL = P_CDFILIAL
                     AND NRPEDIDO = P_NRPEDIDO
                     AND ((NRITEMPED = V_NRITEMPED) OR (V_TITEM = 'T')))
   LOOP
      IF V_IDEVOLESTLIQMAN = 'S' THEN
         V_QTITEM := C_ITEM.QTITEMPEDEXP;
         V_QTSALDSAID := C_ITEM.QTITEMPEDEXP;

         IF (V_IDCTRLESTQ = 'S') THEN
            BEGIN
               SELECT NVL(PF.IDCNTRESTOQ,'S'), PF.CDALMOXPRCLI, PF.CDLOCALPRCLI, NVL(PF.IDUTCTRLLOTE, 'S')
                 INTO V_IDCNTRESTOQ, V_CDALMOXPRCLI, V_CDLOCALPRCLI, V_IDUTCTRLLOTE
                 FROM PRODFILI PF
                WHERE PF.CDFILIAL = P_CDFILIAL
                  AND PF.CDPRODUTO = C_ITEM.CDPRODESTO;
            EXCEPTION
               WHEN NO_DATA_FOUND THEN BEGIN
                  V_IDCNTRESTOQ  := 'S';
                  V_CDALMOXPRCLI := NULL;
                  V_CDLOCALPRCLI := NULL;
                  V_IDUTCTRLLOTE := 'S';
               END;
            END;
            IF (V_IDCNTRESTOQ = 'S') THEN
               IF (V_IDUTILALMOX = 'S') THEN
                  V_CDALMOX := V_CDALMOXPRCLI;
                  IF ((V_CDALMOX IS NULL) OR (V_CDALMOX = ' ') ) THEN
                     IF ((V_CDALMOXARIFE IS NULL) OR (V_CDALMOXARIFE = ' ')) THEN
                        BEGIN
                           SELECT NVL(MAX(NRSEQERROITEM)+1, '1')
                             INTO V_NRSEQERROITEM
                             FROM ERROLIQUIDACAO
                            WHERE CDFILIAL   = C_ITEM.CDFILIAL
                              AND NRPEDIDO = C_ITEM.NRPEDIDO;
                        EXCEPTION
                           WHEN NO_DATA_FOUND THEN
                              V_NRSEQERROITEM := '0000000001';
                        END;
                        INSERT INTO ERROLIQUIDACAO
                        (CDFILIAL, NRPEDIDO, NRITEMPED, NRSEQERROITEM, CDPRODUTO, DSERRO)
                        VALUES
                        (C_ITEM.CDFILIAL, C_ITEM.NRPEDIDO, C_ITEM.NRITEMPED, V_NRSEQERROITEM, C_ITEM.CDPRODESTO, 'ALMOX_NULL');
                        V_ERRO := 1;
                     ELSE
                        V_CDALMOX := V_CDALMOXARIFE;
                     END IF;
                  END IF;
               ELSE
                  V_CDALMOX := ' ';
               END IF;
               IF (V_IDUTILLCESTQ = 'S') THEN
                  V_CDLOCAL := V_CDLOCALPRCLI;
                  IF ((V_CDLOCAL IS NULL) OR (V_CDLOCAL = ' ')) THEN
                     IF ((V_CDLOCALESTOQ IS NULL) OR (V_CDLOCALESTOQ = ' ')) THEN
                        BEGIN
                           SELECT NVL(MAX(NRSEQERROITEM)+1, '1')
                             INTO V_NRSEQERROITEM
                             FROM ERROLIQUIDACAO
                            WHERE CDFILIAL   = C_ITEM.CDFILIAL
                              AND NRPEDIDO = C_ITEM.NRPEDIDO;
                        EXCEPTION
                           WHEN NO_DATA_FOUND THEN
                              V_NRSEQERROITEM := '0000000001';
                        END;
                        INSERT INTO ERROLIQUIDACAO
                        (CDFILIAL, NRPEDIDO, NRITEMPED, NRSEQERROITEM, CDPRODUTO, DSERRO)
                        VALUES
                        (C_ITEM.CDFILIAL, C_ITEM.NRPEDIDO, C_ITEM.NRITEMPED, V_NRSEQERROITEM, C_ITEM.CDPRODESTO, 'LOCAL_NULL');
                        V_ERRO := 1;
                     ELSE
                        V_CDLOCAL := V_CDLOCALESTOQ;
                     END IF;
                  END IF;
               ELSE
                  V_CDLOCAL := ' ';
               END IF;

            END IF;
         ELSE
            V_CDALMOX := ' ';
            V_CDLOCAL := ' ';
         END IF;
         IF (V_ERRO = 0) THEN
            IF (V_IDUTCTRLLOTE = 'S') THEN
                IF (P_IDISCUSTOM = 'S') THEN
                    FOR C_LIQMANUALCUSTOM IN (SELECT 
                                         L.NRLOTEESTQ, 
                                         L.NRSUBLOTE,
                                         L.CDFILIAL,
                                         L.NRPEDIDO,
                                         L.NRITEMPED,
                                         L.CDPRODUTO,
                                         L.QTLIQITEM,
                                         L.CDALMOXARIFE,
                                         L.CDLOCALESTOQ,
                                         CUSTO_PRODUTO_LCEST(L.CDFILIAL, L.CDPRODUTO, V_CDTIPCUSTEST, SYSDATE, V_DTPROCESSA, L.CDALMOXARIFE, L.CDLOCALESTOQ, L.NRLOTEESTQ, L.NRSUBLOTE) 
                                         AS VRCUSTO
                                      FROM LIQMANUALCUSTOM L
                                      WHERE L.NRPEDIDO = P_NRPEDIDO 
                                      AND L.CDFILIAL = P_CDFILIAL 
                                      AND L.NRITEMPED = P_NRITEMPED)
                    LOOP
                       V_NRLOTEESTQ   := C_LIQMANUALCUSTOM.NRLOTEESTQ;
                       V_NRSUBLOTE    := C_LIQMANUALCUSTOM.NRSUBLOTE;
                       V_VRCUSTO      := C_LIQMANUALCUSTOM.VRCUSTO;

                        BEGIN
                            SELECT NVL(MAX(NRSEQUITEM)+1, '1')
                            INTO V_NRSEQUITEM
                            FROM ITLANCTOEST
                            WHERE CDFILIAL   = P_CDFILIAL
                            AND NRLANCESTQ = V_NRLANCESTQ;
                        EXCEPTION
                            WHEN NO_DATA_FOUND THEN
                            V_NRSEQUITEM := '0001';
                        END;

                        V_NRSEQUITEM := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_NRSEQUITEM) + 1)),4,0);

                        INSERT INTO ITLANCTOEST
                            (CDFILIAL, NRLANCESTQ, NRSEQUITEM, CDPRODUTO, QTLANCTOEST, QTTOTLANCTO,
                            CDPRODMOVI, CDFILIMOVI, CDALMOXARIFE, CDLOCALESTOQ, NRLOTEESTQ, NRSUBLOTE,
                            IDENTRSAIDOP, IDTIPOMOVI, DTLANCMOVI, VRUNILANCTO,
                            VRTOTLANCTO, VRLANCTOEST, VRLANCTOBRUT, IDORDEMCLC,DSLANCESTIT,NRPEDIDO,NRITEMPED)
                        VALUES
                            (C_ITEM.CDFILIAL, V_NRLANCESTQ, V_NRSEQUITEM, C_ITEM.CDPRODESTO, C_LIQMANUALCUSTOM.QTLIQITEM, C_LIQMANUALCUSTOM.QTLIQITEM,
                            C_ITEM.CDPRODESTO, C_ITEM.CDFILIAL, C_LIQMANUALCUSTOM.CDALMOXARIFE, C_LIQMANUALCUSTOM.CDLOCALESTOQ, V_NRLOTEESTQ, V_NRSUBLOTE,
                            'S', '4', TRUNC(SYSDATE), V_VRCUSTO,
                            (C_LIQMANUALCUSTOM.QTLIQITEM * V_VRCUSTO), V_VRCUSTO, V_VRCUSTO, '9','Liquida'||chr(231)||chr(227)||'o Manual',C_ITEM.NRPEDIDO,C_ITEM.NRITEMPED);
                    END LOOP;
                ELSE
                   FOR C_LOTE IN (SELECT R.NRLOTEESTQ, R.NRSUBLOTE, ROUND(R.QTDISPONIVEL,5) AS QTTOTALESTOQ,
                                         CUSTO_PRODUTO_LCEST(R.CDFILIAL, R.CDPRODUTO, V_CDTIPCUSTEST, SYSDATE, sysdate,
                                                             V_CDALMOX, V_CDLOCAL, R.NRLOTEESTQ, R.NRSUBLOTE) AS VRCUSTO
                                    FROM RESERVAPED R INNER JOIN LOTE L
                                                              ON R.CDFILIAL   = L.CDFILIAL
                                                             AND R.CDPRODUTO  = L.CDPRODUTO
                                                             AND R.NRLOTEESTQ = L.NRLOTEESTQ
                                                             AND R.NRSUBLOTE  = L.NRSUBLOTE
                                   WHERE (R.CDFILIAL = P_CDFILIAL)
                                     AND (R.CDPRODUTO = C_ITEM.CDPRODESTO)
                                     AND (R.CDALMOXARIFE = V_CDALMOX)
                                     AND (R.CDLOCALESTOQ = V_CDLOCAL)
                                     AND (ROUND(R.QTDISPONIVEL,5)  > 0)
                                     AND (R.NRLOTEESTQ   <> ' ')
                                     AND (NVL(L.DTVALILOTE, TO_DATE('30/12/9999', 'DD/MM/YYYY')) >= TO_DATE(SYSDATE))
                                  ORDER BY  NVL(L.DTVALILOTE, L.DTENTRLOTE), L.NRLOTEESTQ, L.NRSUBLOTE)
                   LOOP
                      V_NRLOTEESTQ := C_LOTE.NRLOTEESTQ;
                      V_NRSUBLOTE := C_LOTE.NRSUBLOTE;
                      V_VRCUSTO := C_LOTE.VRCUSTO;
                      V_QTTOTALESTOQ := C_LOTE.QTTOTALESTOQ;
                      IF (V_QTSALDSAID <= v_QTTOTALESTOQ) THEN
                         V_QTITEM := V_QTSALDSAID;
                      ELSE
                         V_QTITEM := V_QTTOTALESTOQ;
                      END IF;

                      IF (V_QTSALDSAID > 0) THEN
                         BEGIN
                            SELECT NVL(MAX(NRSEQUITEM)+1, '1')
                              INTO V_NRSEQUITEM
                              FROM ITLANCTOEST
                             WHERE CDFILIAL   = P_CDFILIAL
                               AND NRLANCESTQ = V_NRLANCESTQ;
                         EXCEPTION
                            WHEN NO_DATA_FOUND THEN
                               V_NRSEQUITEM := '0001';
                         END;
                         V_NRSEQUITEM := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_NRSEQUITEM) + 1)),4,0);
                         INSERT INTO ITLANCTOEST
                            (CDFILIAL, NRLANCESTQ, NRSEQUITEM, CDPRODUTO, QTLANCTOEST, QTTOTLANCTO,
                             CDPRODMOVI, CDFILIMOVI, CDALMOXARIFE, CDLOCALESTOQ, NRLOTEESTQ, NRSUBLOTE,
                             IDENTRSAIDOP, IDTIPOMOVI, DTLANCMOVI, VRUNILANCTO,
                             VRTOTLANCTO, VRLANCTOEST, VRLANCTOBRUT, IDORDEMCLC,DSLANCESTIT,NRPEDIDO,NRITEMPED)
                         VALUES
                            (C_ITEM.CDFILIAL, V_NRLANCESTQ, V_NRSEQUITEM, C_ITEM.CDPRODESTO, V_QTITEM, V_QTITEM,
                             C_ITEM.CDPRODESTO, C_ITEM.CDFILIAL, V_CDALMOX, V_CDLOCAL, V_NRLOTEESTQ, V_NRSUBLOTE,
                             'S', '4', TRUNC(SYSDATE), V_VRCUSTO,
                             (V_QTITEM*V_VRCUSTO), V_VRCUSTO, V_VRCUSTO, '9','Liquida'||chr(231)||chr(227)||'o Manual',C_ITEM.NRPEDIDO,C_ITEM.NRITEMPED);

                         IF (V_QTSALDSAID <= V_QTTOTALESTOQ) THEN
                            V_QTSALDSAID := 0;
                         ELSE
                            V_QTSALDSAID := V_QTSALDSAID - V_QTTOTALESTOQ;
                         END IF;

                         V_QTITEM := V_QTSALDSAID;
                      END IF;
                   END LOOP;
                   IF (V_QTSALDSAID > 0) THEN
                      BEGIN
                         SELECT NVL(MAX(NRSEQUITEM)+1, '1')
                           INTO V_NRSEQUITEM
                           FROM ITLANCTOEST
                          WHERE CDFILIAL   = P_CDFILIAL
                            AND NRLANCESTQ = V_NRLANCESTQ;
                      EXCEPTION
                         WHEN NO_DATA_FOUND THEN
                            V_NRSEQUITEM := '0001';
                      END;
                      V_NRSEQUITEM := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_NRSEQUITEM) + 1)),4,0);
                      IF (V_NRLOTEESTQ IS NULL) THEN
                          BEGIN
                             SELECT NVL(MAX(NRSEQERROITEM)+1, '1')
                               INTO V_NRSEQERROITEM
                               FROM ERROLIQUIDACAO
                              WHERE CDFILIAL   = C_ITEM.CDFILIAL
                                AND NRPEDIDO = C_ITEM.NRPEDIDO;
                          EXCEPTION
                               WHEN NO_DATA_FOUND THEN
                                  V_NRSEQERROITEM := '0000000001';
                          END;
                          
                          INSERT INTO ERROLIQUIDACAO
                            (CDFILIAL, NRPEDIDO, NRITEMPED, NRSEQERROITEM, CDPRODUTO, DSERRO)
                            VALUES
                            (C_ITEM.CDFILIAL, C_ITEM.NRPEDIDO, C_ITEM.NRITEMPED, V_NRSEQERROITEM, C_ITEM.CDPRODESTO, 'LOTE_NULL');
                            V_ERRO := 1;
                      ELSE
                          INSERT INTO ITLANCTOEST
                             (CDFILIAL, NRLANCESTQ, NRSEQUITEM, CDPRODUTO, QTLANCTOEST, QTTOTLANCTO,
                              CDPRODMOVI, CDFILIMOVI, CDALMOXARIFE, CDLOCALESTOQ, NRLOTEESTQ, NRSUBLOTE,
                              IDENTRSAIDOP, IDTIPOMOVI, DTLANCMOVI, VRUNILANCTO,
                              VRTOTLANCTO, VRLANCTOEST, VRLANCTOBRUT, IDORDEMCLC,DSLANCESTIT,NRPEDIDO,NRITEMPED)
                          VALUES
                             (C_ITEM.CDFILIAL, V_NRLANCESTQ, V_NRSEQUITEM, C_ITEM.CDPRODESTO, V_QTITEM, V_QTITEM,
                              C_ITEM.CDPRODESTO, C_ITEM.CDFILIAL, V_CDALMOX, V_CDLOCAL, V_NRLOTEESTQ, V_NRSUBLOTE,
                              'S', '4', TRUNC(SYSDATE), V_VRCUSTO,
                              (V_QTITEM*V_VRCUSTO), V_VRCUSTO, V_VRCUSTO, '9','Liquida'||chr(231)||chr(227)||'o Manual',C_ITEM.NRPEDIDO,C_ITEM.NRITEMPED);
    
                          V_QTSALDSAID := 0;
                      END IF;
                   END IF;
                END IF;
            ELSE
               V_VRCUSTO := CUSTO_PRODUTO_LCEST(C_ITEM.CDFILIAL, C_ITEM.CDPRODESTO, V_CDTIPCUSTEST, SYSDATE, V_DTPROCESSA,
                                                V_CDALMOX, V_CDLOCAL, ' ', ' ');
               BEGIN
                  SELECT NVL(MAX(NRSEQUITEM)+1, '1')
                    INTO V_NRSEQUITEM
                    FROM ITLANCTOEST
                   WHERE CDFILIAL   = P_CDFILIAL
                     AND NRLANCESTQ = V_NRLANCESTQ;
               EXCEPTION
                  WHEN NO_DATA_FOUND THEN
                     V_NRSEQUITEM := '0001';
               END;
               V_NRSEQUITEM := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_NRSEQUITEM) + 1)),4,0);
               INSERT INTO ITLANCTOEST
                  (CDFILIAL, NRLANCESTQ, NRSEQUITEM, CDPRODUTO, QTLANCTOEST, QTTOTLANCTO,
                   CDPRODMOVI, CDFILIMOVI, CDALMOXARIFE, CDLOCALESTOQ, NRLOTEESTQ, NRSUBLOTE,
                   IDENTRSAIDOP, IDTIPOMOVI, DTLANCMOVI, VRUNILANCTO,
                   VRTOTLANCTO, VRLANCTOEST, VRLANCTOBRUT, IDORDEMCLC,DSLANCESTIT,NRPEDIDO,NRITEMPED)
               VALUES
                  (C_ITEM.CDFILIAL, V_NRLANCESTQ, V_NRSEQUITEM, C_ITEM.CDPRODESTO, V_QTSALDSAID, V_QTITEM,
                   C_ITEM.CDPRODESTO, C_ITEM.CDFILIAL, V_CDALMOX, V_CDLOCAL, ' ', ' ',
                   'S', '4', TRUNC(SYSDATE), V_VRCUSTO,
                   (V_QTITEM*V_VRCUSTO), V_VRCUSTO, V_VRCUSTO, '9','Liquida'||chr(231)||chr(227)||'o Manual',C_ITEM.NRPEDIDO,C_ITEM.NRITEMPED);
            END IF;
         END IF;    
      END IF;
      IF C_ITEM.IDSITITEMPED <> 5 THEN
         UPDATE ITEMPEDIDO
            SET IDSITITEMPED = '6',
                CDOPERADOR = P_CDOPERADOR,
                CDJUSTEXCHIS = P_CDJUSTEXCHIS
          WHERE CDFILIAL = C_ITEM.CDFILIAL
            AND NRPEDIDO = C_ITEM.NRPEDIDO
            AND NRITEMPED = C_ITEM.NRITEMPED;

         DELETE FROM RESERPRODPED
         WHERE CDFILIAL = C_ITEM.CDFILIAL
           AND NRPEDIDO = C_ITEM.NRPEDIDO
           AND NRITEMPED = C_ITEM.NRITEMPED;
      END IF;
   END LOOP;
   IF V_IDEVOLESTLIQMAN = 'S' THEN
      IF SYSDATE < V_DTPROCESSA THEN
         V_DTINICIAL := SYSDATE;
      ELSE
         V_DTINICIAL := V_DTPROCESSA;
      END IF;

      IF SYSDATE > V_DTPROCESSA THEN
         V_DTFINAL := SYSDATE;
      ELSE
         V_DTFINAL := V_DTPROCESSA;
      END IF;
      BEGIN
         SELECT DTLANCESTQ
           INTO V_DTANTERIOR
           FROM LANCTOESTOQ
          WHERE CDFILIAL = P_CDFILIAL
            AND NRLANCESTQ = V_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(P_CDFILIAL, V_DTINICIAL, V_DTFINAL, P_CDFILIAL, V_NRLANCESTQ);

      FOR C_MOVILOTE IN (SELECT *
                           FROM MOVILOTE
                          WHERE NRMOVILOTE  = P_CDFILIAL||V_NRLANCESTQ
                            AND CDFILIAL   = P_CDFILIAL)
      LOOP
         V_ERRO := 1;
         FOR C_ITEMSESTOQ IN (SELECT DISTINCT I.CDFILIAL, I.NRPEDIDO, I.NRITEMPED, I.CDPRODUTO, P.CDARVPROD, P.NMPRODUTO
                                FROM ITEMPEDIDO I JOIN PRODUTO P
                                                    ON I.CDPRODUTO = P.CDPRODUTO
                               WHERE I.CDFILIAL = P_CDFILIAL
                                 AND I.NRPEDIDO = P_NRPEDIDO
                                 AND P.CDPRODESTO = C_MOVILOTE.CDPRODUTO)
         LOOP
            BEGIN
               SELECT NVL(MAX(NRSEQERROITEM)+1, '1')
                 INTO V_NRSEQERROITEM
                 FROM ERROLIQUIDACAO
                WHERE CDFILIAL   = P_CDFILIAL
                  AND NRPEDIDO = C_ITEMSESTOQ.NRPEDIDO;
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
                  V_NRSEQERROITEM := '0000000001';
            END;
            INSERT INTO ERROLIQUIDACAO
            (CDFILIAL, NRPEDIDO, NRITEMPED, NRSEQERROITEM, CDPRODUTO, DSERRO)
            VALUES
            (C_ITEMSESTOQ.CDFILIAL, C_ITEMSESTOQ.NRPEDIDO, C_ITEMSESTOQ.NRITEMPED, V_NRSEQERROITEM, C_ITEMSESTOQ.CDPRODUTO, 'IRREGESTOQ');
         END LOOP;
         DELETE FROM ITLANCTOEST
         WHERE CDFILIAL = P_CDFILIAL
           AND NRLANCESTQ = V_NRLANCESTQ;
         DELETE FROM LANCTOESTOQ
         WHERE CDFILIAL = P_CDFILIAL
           AND NRLANCESTQ = V_NRLANCESTQ;  
      END LOOP;
      DELETE MOVILOTE
      WHERE NRMOVILOTE  = P_CDFILIAL||V_NRLANCESTQ;
   END IF;
   IF (V_ERRO = 0) THEN
      BEGIN
         SELECT COUNT(1)
           INTO V_COUNT_ITEM
           FROM ITEMPEDIDO
          WHERE CDFILIAL = P_CDFILIAL
            AND NRPEDIDO = P_NRPEDIDO;
      EXCEPTION
         WHEN NO_DATA_FOUND THEN BEGIN
            V_COUNT_ITEM := 0;
         END;
      END;
      BEGIN
         SELECT COUNT(1)
           INTO V_COUNT_PEND
           FROM ITEMPEDIDO
          WHERE CDFILIAL = P_CDFILIAL
            AND NRPEDIDO = P_NRPEDIDO
            AND IDSITITEMPED = '1';
      EXCEPTION
         WHEN NO_DATA_FOUND THEN BEGIN
            V_COUNT_PEND := 0;
         END;
      END;
      BEGIN
         SELECT COUNT(1)
           INTO V_COUNT_CONF
           FROM ITEMPEDIDO
          WHERE CDFILIAL = P_CDFILIAL
            AND NRPEDIDO = P_NRPEDIDO
            AND IDSITITEMPED = '2';
      EXCEPTION
         WHEN NO_DATA_FOUND THEN BEGIN
            V_COUNT_CONF := 0;
         END;
      END;
      BEGIN
         SELECT COUNT(1)
           INTO V_COUNT_APRO
           FROM ITEMPEDIDO
          WHERE CDFILIAL = P_CDFILIAL
            AND NRPEDIDO = P_NRPEDIDO
            AND IDSITITEMPED = '3';
      EXCEPTION
         WHEN NO_DATA_FOUND THEN BEGIN
            V_COUNT_APRO := 0;
         END;
      END;
      BEGIN
         SELECT COUNT(1)
           INTO V_COUNT_EXP
           FROM ITEMPEDIDO
          WHERE CDFILIAL = P_CDFILIAL
            AND NRPEDIDO = P_NRPEDIDO
            AND IDSITITEMPED = '4';
      EXCEPTION
         WHEN NO_DATA_FOUND THEN BEGIN
            V_COUNT_EXP := 0;
         END;
      END;
      BEGIN
         SELECT COUNT(1)
           INTO V_COUNT_CANC
           FROM ITEMPEDIDO
          WHERE CDFILIAL = P_CDFILIAL
            AND NRPEDIDO = P_NRPEDIDO
            AND IDSITITEMPED = '5';
      EXCEPTION
         WHEN NO_DATA_FOUND THEN BEGIN
            V_COUNT_CANC := 0;
         END;
      END;
      BEGIN
         SELECT COUNT(1)
           INTO V_COUNT_LIQU
           FROM ITEMPEDIDO
          WHERE CDFILIAL = P_CDFILIAL
            AND NRPEDIDO = P_NRPEDIDO
            AND IDSITITEMPED = '6';
      EXCEPTION
         WHEN NO_DATA_FOUND THEN BEGIN
            V_COUNT_LIQU := 0;
         END;
      END;
      BEGIN
         SELECT COUNT(1)
           INTO V_COUNT_PAAT
           FROM ITEMPEDIDO
          WHERE CDFILIAL = P_CDFILIAL
            AND NRPEDIDO = P_NRPEDIDO
            AND IDSITITEMPED = '7';
      EXCEPTION
         WHEN NO_DATA_FOUND THEN BEGIN
            V_COUNT_PAAT := 0;
         END;
      END;
      IF (V_COUNT_ITEM = (V_COUNT_PEND+V_COUNT_CANC)) THEN
         V_NOVASITPEDIDO := '1';
      ELSE
         IF (V_COUNT_ITEM = (V_COUNT_EXP+V_COUNT_CANC)) THEN
            V_NOVASITPEDIDO := '3';
         ELSE
            IF (V_COUNT_ITEM = V_COUNT_CANC) THEN
               V_NOVASITPEDIDO := '4';
            ELSE
               IF (V_COUNT_ITEM = (V_COUNT_LIQU+V_COUNT_CANC)) THEN
                  V_NOVASITPEDIDO := '5';
               ELSE
                  IF ((V_COUNT_EXP > 0) OR (V_COUNT_PAAT > 0)) THEN
                     V_NOVASITPEDIDO := '6';
                  ELSE
                     IF (V_COUNT_LIQU > 0 ) THEN
                        V_NOVASITPEDIDO := '6';
                     ELSE
                        V_NOVASITPEDIDO := '2';
                     END IF;
                  END IF;
               END IF;
            END IF;
         END IF;
      END IF;
      UPDATE PEDIDO
         SET IDSITPEDIDO = V_NOVASITPEDIDO
       WHERE CDFILIAL = P_CDFILIAL
         AND NRPEDIDO = P_NRPEDIDO;
      P_IDRETORNOLIQ := '0';
   ELSE
      P_IDRETORNOLIQ := '1';
   END IF;
END;
/