create or replace PROCEDURE MOVIMENTACAO_ESTOQUE_ANALI(
   P_IDCUSTO      IN NUMBER DEFAULT 1)

IS
   V_CDALMOXLCEST VARCHAR2(12);
   V_CDPRODUTO    VARCHAR2(20);
   V_CDARVPROD    VARCHAR2(20);
   V_NRLOTEESTQ   VARCHAR2(20);
   V_NRSUBLOTE    VARCHAR2(04);
   V_NMPRODUTO    VARCHAR2(50);
   V_NVPROD       VARCHAR2(20);
   V_UNIDADE      VARCHAR2(10);
   V_CDPRODESTO   VARCHAR2(10);
   V_SGUNIDADE    VARCHAR2(10);
   V_TIPCUSTEST   NUMBER;
   V_DATAPROC     DATE;
   V_TAMARV       INTEGER;
   V_TAMNIVEL     INTEGER;

   V_QTATUAL      NUMBER;
   V_QTMOVI       NUMBER;
   V_QTDEANT      NUMBER;
   V_QTMOVANT     NUMBER;

   V_QTDEAJUSTE   NUMBER;
   V_QTPOSINI     NUMBER;

   w_N            CHAR(1) := 'N';
   w_S            CHAR(1) := 'S';
   V_INSERT       VARCHAR2(01);

   V_CDALMOXARIFE  VARCHAR2(06);
   V_CDLOCALESTOQ  VARCHAR2(06);
   V_CDFILIAL      VARCHAR2(04);
   V_CDGRUPPRMOVEST  VARCHAR2(10);

   V_VRPESOBRUTME FLOAT(126);
   V_DIFCUSTSAI   NUMBER;
   V_VRANTEST     NUMBER;
   V_VRATUAL      NUMBER;
   V_CUSTMED      NUMBER;
   V_CUSTANT      NUMBER;
   V_CUSTZERO     NUMBER;
   V_VRCUSTLOGEME NUMBER;
   V_VRCUSTSEGEME NUMBER;
   V_ULTCUSTENT   NUMBER;
   V_CUSTESTOQ    NUMBER;
   V_VRCUSTLOGANT NUMBER;
   V_VRCUSTSEGANT NUMBER;
   V_VRMOVSEGANT  NUMBER;
   V_VRMOVLOGANT  NUMBER;
   V_CUSTMEES     NUMBER;
   V_VRMOVANT     NUMBER;
   V_VLRESTOAJU   NUMBER;
   V_VLRBRUTAJU   NUMBER;
   V_VRATUEST     NUMBER;
   V_CUSTOMEDIO   NUMBER;
   V_COUNT        NUMBER;
   V_VRQTDETOTAL  NUMBER;
   V_VRSOMSATUAN  NUMBER;
   V_VRQTDESATUAN NUMBER;
   V_VRDIFCUSSAID NUMBER;
   V_CUSTTOTENTR  NUMBER;


   V_NRMESANTEMOVIEST    NUMBER;
   V_DTMOVIESTINI        DATE;
   V_DTMOVIESTFIN        DATE;
   V_HRAGEMOVIEST        VARCHAR2(5);
   P_DTINICIAL           DATE;
   P_DTFINAL             DATE;
   V_COMANDO             VARCHAR2(4000);
   V_TIPO                VARCHAR2(6) := 'BINARY';

   V_IDPERMESTNEG  VARCHAR2(01);
   V_VRCUSTTOTNEG  NUMBER;
   V_QTANTENTR     NUMBER;

   CURSOR C_POSESTDIA IS
      SELECT A.CDFILIAL,
             A.CDPRODUTO,
             A.NRLOTEESTQ AS NRLOTEESTQ,
             A.NRSUBLOTE,
             A.DTPOSIESTQ AS DTLANCAMEN,
             '-' AS IDTIPOLANC,
             '----' AS CDFILLANC,
             '----------' AS NRLANCAMEN,
             '0001' AS NRSEQULANC,
             A.QTESTOQDIA AS QTDEESTO,
             DECODE(A.QTESTOQDIA,0,0,DECODE(P_IDCUSTO,1,A.VRESTOQDIA,A.VRESTOQBRUT)/A.QTESTOQDIA) AS CUSTMEDI,
             DECODE(A.QTESTOQDIA,0,VRESTOQANT,A.VRESTOQDIA) AS CUSTESTO,
             A.QTESTOQDIA AS QTLANCAMEN,
             DECODE(A.QTESTOQDIA,0,VRBRUTOANT,A.VRESTOQBRUT) AS CUSTBRUT,
             DECODE(A.QTESTOQDIA,0,DECODE(QTESTOQANT,0,0,VRESTOQANT/QTESTOQANT),A.VRESTOQDIA/A.QTESTOQDIA) AS CUSTMEES,
             DECODE(A.QTESTOQDIA,0,DECODE(QTESTOQANT,0,0,VRBRUTOANT/QTESTOQANT),A.VRESTOQBRUT/A.QTESTOQDIA) AS CUSTMEBR,
             ' ' AS IDALTCMEDMV,
             0.0 AS QTLANCTOEST,
             0.0 AS VRLANCTOEST,
             0.0 AS VRLANCTOBRUT,
             '-' AS IDORDEMCLC,
             0.0 AS VRUNILANCTO,
             0.0 AS VRTOTLANCTO,
             0.0 AS VRTOTALANTE,
             0.0 AS QTTOTALANTE,
             A.CDALMOXARIFE AS CDALMOXARIFE,
             A.CDLOCALESTOQ AS CDLOCALESTOQ,
             0.0 AS VRCUSTENTRME,
             '--' AS CDTIPORETIME,
             '------------------------------' AS NMTIPORETIME,
             '-' AS IDENTRSAIDME,
             '--' AS CDTIPOOPERME,
             0 AS VRCUSTLOGLME,
             0 AS VRCUSTSEGLME,
             NVL(VRPESOBRUTPR,0) AS VRPESOBRUTME,
             A.VRCLOGDIA AS CUSTLOGESTO,
             A.VRSEGUDIA AS CUSTSEGESTO,
             CDARVPROD,
             SGUNIDADE,
             NMPRODUTO,
             NVPROD
        FROM POSESTDIA A,
             (SELECT CDPRODUTO, VRPESOBRUTPR, CDARVPROD, SGUNIDADE, NMPRODUTO,
               SUBSTR(CDARVPROD,1, V_TAMARV) as NVPROD
                FROM PRODUTO
               WHERE CDNVPRODUTO = (SELECT MAX(CDNVPRODUTO)
                                      FROM PRODUTO)) C
       WHERE (A.CDFILIAL = V_CDFILIAL)
         AND (A.CDALMOXARIFE = V_CDALMOXARIFE)
         AND (A.CDLOCALESTOQ = V_CDLOCALESTOQ)
         AND (A.CDPRODUTO = C.CDPRODUTO)
         AND A.DTPOSIESTQ = (SELECT MAX(P.DTPOSIESTQ)
                               FROM POSESTDIA P
                              WHERE P.CDFILIAL     = A.CDFILIAL
                                AND P.CDPRODUTO    = A.CDPRODUTO
                                AND P.CDALMOXARIFE = A.CDALMOXARIFE
                                AND P.CDLOCALESTOQ = A.CDLOCALESTOQ
                                AND P.NRLOTEESTQ   = A.NRLOTEESTQ
                                AND P.NRSUBLOTE    = A.NRSUBLOTE
                                AND P.DTPOSIESTQ   <= P_DTINICIAL);

   CURSOR C_ITLANCTOEST IS
      SELECT D.CDFILIMOVI AS CDFILIAL,
             D.CDPRODMOVI AS CDPRODUTO,
             D.NRLOTEESTQ,
             D.NRSUBLOTE,
             D.DTLANCMOVI AS DTLANCAMEN,
             D.IDTIPOMOVI AS IDTIPOLANC,
             D.CDFILIAL AS CDFILLANC,
             D.NRLANCESTQ AS NRLANCAMEN,
             D.NRSEQUITEM AS NRSEQULANC,
             0.0 AS QTDEESTO,
             0.0 AS CUSTMEDI,
             0.0 AS CUSTESTO,
             0.0 AS QTLANCAMEN,
             0.0 AS CUSTBRUT,
             0.0 AS CUSTMEES,
             0.0 AS CUSTMEBR,
             D.IDALTCMEDMV,
             D.QTLANCTOEST,
             NVL(D.VRLANCTOEST ,0) AS VRLANCTOEST,
             NVL(D.VRLANCTOBRUT,0) AS VRLANCTOBRUT,
             D.IDORDEMCLC,
             E.NMPRODUTO,
             E.SGUNIDADE,
             NVL(D.VRUNILANCTO,0) AS VRUNILANCTO,
             NVL(D.VRTOTLANCTO,0) AS VRTOTLANCTO,
             0.0 AS VRTOTALANTE,
             0.0 AS QTTOTALANTE,
             D.CDALMOXARIFE,
             D.CDLOCALESTOQ,
             NVL(VRENTRESTNEG, (D.VRLANCTOEST * D.QTLANCTOEST)) AS VRCUSTENTRME,
             NVL(D.CDTIPORETI  ,'--') AS CDTIPORETIME,
             NVL(T.NMTIPORETI  ,'-') AS NMTIPORETIME,
             NVL(D.IDENTRSAIDOP,'-') AS IDENTRSAIDME,
             NVL(D.CDTIPOOPER  ,'--') AS CDTIPOOPERME,
             NVL(VRCUSTLOGLAN,0) AS VRCUSTLOGLME,
             NVL(VRSEGULOGLAN,0) AS VRCUSTSEGLME,
             NVL(VRPESOBRUTPR,0) AS VRPESOBRUTME,
             0.0 AS CUSTLOGESTO,
             0.0 AS CUSTSEGESTO,
             CDARVPROD,
             NVPROD
        FROM PRODFILI C,
             (SELECT CDPRODUTO, VRPESOBRUTPR, CDARVPROD, SGUNIDADE, NMPRODUTO,
               SUBSTR(CDARVPROD,1, V_TAMARV) as NVPROD
                FROM PRODUTO
               WHERE CDNVPRODUTO = (SELECT MAX(CDNVPRODUTO)
                                      FROM PRODUTO)) E,
             ITLANCTOEST D, TIPORETI T
       WHERE (D.CDFILIMOVI = V_CDFILIAL)
         AND (D.CDALMOXARIFE = V_CDALMOXARIFE)
         AND (D.CDLOCALESTOQ = V_CDLOCALESTOQ)
         AND (D.CDPRODMOVI = E.CDPRODUTO)
         AND (NVL(IDEVOLESTOQ,w_S) = w_S)
         AND (D.CDFILIMOVI = C.CDFILIAL(+))
         AND (D.CDPRODMOVI = C.CDPRODUTO(+))
         AND (NVL(C.IDCNTRESTOQ,w_S) = w_S)
         AND (D.DTLANCMOVI BETWEEN P_DTINICIAL AND P_DTFINAL)
         AND (D.IDTIPOMOVI <> '0')
         AND (D.CDTIPORETI = T.CDTIPORETI(+));

   CURSOR C_IMPLANTACAO IS
      SELECT D.CDFILIMOVI AS CDFILIAL,
             D.CDPRODMOVI AS CDPRODUTO,
             D.NRLOTEESTQ,
             D.NRSUBLOTE,
             D.DTLANCMOVI AS DTLANCAMEN,
             D.IDTIPOMOVI AS IDTIPOLANC,
             D.CDFILIAL AS CDFILLANC,
             D.NRLANCESTQ AS NRLANCAMEN,
             '0000' AS NRSEQULANC,
             0.0 AS QTDEESTO,
             0.0 AS CUSTMEDI,
             0.0 AS CUSTESTO,
             0.0 AS QTLANCAMEN,
             0.0 AS CUSTBRUT,
             0.0 AS CUSTMEES,
             0.0 AS CUSTMEBR,
             D.IDALTCMEDMV,
             SUM(QTLANCTOEST) AS QTLANCTOEST,
             SUM(NVL(VRLANCTOEST,0)) AS VRLANCTOEST,
             SUM(NVL(VRLANCTOBRUT,0)) AS VRLANCTOBRUT,
             D.IDORDEMCLC,
             E.NMPRODUTO,
             E.SGUNIDADE,
             NVL(SUM(VRTOTLANCTO),0) / DECODE(SUM(QTLANCTOEST),0,1,SUM(QTLANCTOEST)) AS VRUNILANCTO,
             NVL(SUM(VRTOTLANCTO),0) AS VRTOTLANCTO,
             0.0 AS VRTOTALANTE,
             0.0 AS QTTOTALANTE,
             D.CDALMOXARIFE,
             D.CDLOCALESTOQ,
             0.0 AS VRCUSTENTRME,
             NVL(D.CDTIPORETI  ,'--') AS CDTIPORETIME,
             NVL(T.NMTIPORETI  ,'-') AS NMTIPORETIME,
             NVL(D.IDENTRSAIDOP,'-') AS IDENTRSAIDME,
             NVL(D.CDTIPOOPER  ,'--') AS CDTIPOOPERME,
             0.0 AS VRCUSTLOGLME,
             0.0 AS VRCUSTSEGLME,
             NVL(VRPESOBRUTPR,0) AS VRPESOBRUTME,
             0.0 AS CUSTLOGESTO,
             0.0 AS CUSTSEGESTO,
             CDARVPROD,
             NVPROD
        FROM PRODFILI C, ITLANCTOEST D, TIPORETI T,
             (SELECT CDPRODUTO, VRPESOBRUTPR, CDARVPROD, SGUNIDADE, NMPRODUTO,
               SUBSTR(CDARVPROD,1, V_TAMARV) as NVPROD
                FROM PRODUTO
               WHERE CDNVPRODUTO = (SELECT MAX(CDNVPRODUTO)
                                      FROM PRODUTO)) E
       WHERE (D.CDFILIMOVI = V_CDFILIAL)
         AND (D.CDALMOXARIFE = V_CDALMOXARIFE)
         AND (D.CDLOCALESTOQ = V_CDLOCALESTOQ)
         AND (D.CDPRODMOVI = E.CDPRODUTO)
         AND (NVL(IDEVOLESTOQ,w_S) = w_S)
         AND (D.CDFILIMOVI = C.CDFILIAL(+))
         AND (D.CDPRODMOVI = C.CDPRODUTO(+))
         AND (NVL(C.IDCNTRESTOQ,w_S) = w_S)
         AND (D.DTLANCMOVI BETWEEN P_DTINICIAL AND P_DTFINAL)
         AND (D.IDTIPOMOVI = '0') --Implantacao de estoque
         AND (D.CDTIPORETI = T.CDTIPORETI(+))
       GROUP BY D.CDFILIMOVI, E.CDARVPROD, D.NRLOTEESTQ, D.NRSUBLOTE, D.DTLANCMOVI,
                D.IDTIPOMOVI, D.CDFILIAL, D.NRLANCESTQ, D.IDALTCMEDMV, D.IDORDEMCLC, D.CDPRODMOVI,
                E.NMPRODUTO, E.SGUNIDADE, D.CDALMOXARIFE, D.CDLOCALESTOQ, D.CDTIPORETI,
                T.NMTIPORETI, D.IDENTRSAIDOP, D.CDTIPOOPER, VRPESOBRUTPR, CDARVPROD, NVPROD;

BEGIN
   ----------------------------------------------------------------------------------
   -- ALTERA SESSAO PARA ORDENAR NUMEROS ANTES DE LETRAS                           --
   ----------------------------------------------------------------------------------
   V_COMANDO := NULL;
   V_COMANDO := 'ALTER SESSION SET NLS_SORT = '||CHR(39)||V_TIPO||CHR(39);
   EXECUTE IMMEDIATE V_COMANDO;
   ----------------------------------------------------------------------------------
   -- CONFERE PARAMETRIZA!O PARA PEGAR DATAS E GERAR O RELATORIO                  --
   ----------------------------------------------------------------------------------

   BEGIN
      SELECT NRMESANTEMOVIEST, DTMOVIESTINI, DTMOVIESTFIN, HRAGEMOVIEST
        INTO V_NRMESANTEMOVIEST, V_DTMOVIESTINI, V_DTMOVIESTFIN , V_HRAGEMOVIEST
        FROM PARAMESTOQUE;
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_NRMESANTEMOVIEST  := NULL;
         V_DTMOVIESTINI      := NULL;
         V_DTMOVIESTFIN      := NULL;
         GOTO PROCEDURE_END;
      WHEN OTHERS THEN
         GOTO PROCEDURE_END;
   END;

   IF((V_NRMESANTEMOVIEST IS NULL) AND (V_DTMOVIESTINI IS NULL) AND (V_DTMOVIESTFIN IS NULL)) THEN
      GOTO PROCEDURE_END;
   END IF;

   IF (V_NRMESANTEMOVIEST IS NOT NULL) THEN
      P_DTFINAL   := TRUNC(SYSDATE);
      P_DTINICIAL := TRUNC(TO_DATE('01/'||TO_CHAR(ADD_MONTHS(TRUNC(SYSDATE), - V_NRMESANTEMOVIEST), 'MM/YY'), 'DD/MM/YY'));
   ELSE
      P_DTFINAL   := TRUNC(V_DTMOVIESTFIN);
      P_DTINICIAL := TRUNC(V_DTMOVIESTINI);
   END IF;

   ---------------------------------------------------------------------------------
   -- DELETA OS REGISTROS DAS TABELA TEMPORARIAS                                  --
   ---------------------------------------------------------------------------------

   DELETE FROM RELAT_MOVIEST_ANALI
    WHERE CDFILIMOVIEST IN (SELECT CDFILIAL
                              FROM FILIAL)
      AND DTLANCMOVIEST BETWEEN P_DTINICIAL AND P_DTFINAL;

   DELETE FROM RELAT_ANALMOVI_ANALI
    WHERE CDFILIAL IN (SELECT CDFILIAL
                         FROM FILIAL);
   DELETE FROM RELAT_TOTAMOVI_ANALI
    WHERE CDFILIAL IN (SELECT CDFILIAL
                         FROM FILIAL);

   FOR C_FILIAIS IN (SELECT DISTINCT F.CDFILIAL, L.CDALMOXARIFE, L.CDLOCALESTOQ
                       FROM FILIAL F, LOCALESTOQUE L
                      WHERE L.CDFILIAL   = F.CDFILIAL
                      ORDER BY F.CDFILIAL, L.CDALMOXARIFE, L.CDLOCALESTOQ)
   LOOP
      V_CDALMOXARIFE := C_FILIAIS.CDALMOXARIFE;
      V_CDLOCALESTOQ := C_FILIAIS.CDLOCALESTOQ;
      V_CDFILIAL     := C_FILIAIS.CDFILIAL;

      ---------------------------------------------------------------------------------
      -- Preenche Variaveis de tipo de custo e data processamento                                  --
      ---------------------------------------------------------------------------------

      BEGIN
         SELECT NVL(TO_NUMBER(P.CDTIPCUSTEST),3), DECODE(PG.IDUTILDTPROC, 'S', NVL(P.DTPROCESSA, SYSDATE), SYSDATE), P.CDGRUPPRMOVEST, NVL(IDPERMESTNEG,'N')
           INTO V_TIPCUSTEST, V_DATAPROC, V_CDGRUPPRMOVEST, V_IDPERMESTNEG
           FROM PARAMFILIAL P, PARAMGERAL PG
          WHERE P.CDFILIAL = V_CDFILIAL
            AND CDPARGERAL = '0001';
      EXCEPTION
         WHEN NO_DATA_FOUND THEN BEGIN
            V_TIPCUSTEST := 3;
            V_DATAPROC   := SYSDATE;
            V_CDGRUPPRMOVEST := NULL;
            V_IDPERMESTNEG := 'N';
         END;
      END;

      SELECT LENGTH(MAX(CDARVPROD))
        INTO V_TAMARV
        FROM PRODUTO;

      V_TAMNIVEL := V_TAMARV;
      ---------------------------------------------------------------------------------
      -- INSERE PRODUTOS NA TABELA RELAT_MOVIEST_ANALI DA POSIESTDIA E ITLANCTOEST    --
      ---------------------------------------------------------------------------------

      FOR C_MOVIEST IN C_POSESTDIA LOOP
         DELETE FROM RELAT_MOVIEST_ANALI R
          WHERE R.CDFILIMOVIEST = C_MOVIEST.CDFILIAL
            AND R.CDPRODMOVIEST = C_MOVIEST.CDARVPROD
            AND R.DTLANCMOVIEST BETWEEN C_MOVIEST.DTLANCAMEN AND P_DTFINAL
            AND R.CDALMOXARIRM = C_MOVIEST.CDALMOXARIFE
            AND R.CDLOCALESTRM = C_MOVIEST.CDLOCALESTOQ;

         GRAVA_RELAT_MOVIEST_ANALI(C_MOVIEST.CDFILIAL,    C_MOVIEST.CDPRODUTO,    C_MOVIEST.CDARVPROD,    C_MOVIEST.NRLOTEESTQ,   C_MOVIEST.NRSUBLOTE,
                                  P_DTINICIAL,            C_MOVIEST.IDTIPOLANC,   C_MOVIEST.CDFILLANC,    C_MOVIEST.NRLANCAMEN,   C_MOVIEST.NRSEQULANC,
                                  C_MOVIEST.QTDEESTO,     C_MOVIEST.QTLANCAMEN,   C_MOVIEST.IDALTCMEDMV,  C_MOVIEST.QTLANCTOEST,  C_MOVIEST.IDORDEMCLC,
                                  C_MOVIEST.QTTOTALANTE,  C_MOVIEST.CDALMOXARIFE, C_MOVIEST.CDLOCALESTOQ, C_MOVIEST.CDTIPORETIME, C_MOVIEST.IDENTRSAIDME,
                                  C_MOVIEST.CDTIPOOPERME, C_MOVIEST.CUSTMEDI,     C_MOVIEST.CUSTESTO,     C_MOVIEST.CUSTBRUT,     C_MOVIEST.CUSTMEES,
                                  C_MOVIEST.CUSTMEBR,     C_MOVIEST.VRLANCTOEST,  C_MOVIEST.VRLANCTOBRUT, C_MOVIEST.VRUNILANCTO,  C_MOVIEST.VRTOTLANCTO,
                                  C_MOVIEST.VRTOTALANTE,  C_MOVIEST.VRCUSTENTRME, C_MOVIEST.VRCUSTLOGLME, C_MOVIEST.VRCUSTSEGLME, C_MOVIEST.CUSTLOGESTO,
                                  C_MOVIEST.CUSTSEGESTO,  C_MOVIEST.VRPESOBRUTME, C_MOVIEST.NVPROD,       C_MOVIEST.NMPRODUTO,    C_MOVIEST.SGUNIDADE,
                                  C_MOVIEST.NMTIPORETIME);
      END LOOP;

      FOR C_MOVIEST IN C_ITLANCTOEST LOOP

         IF (V_TIPCUSTEST = 3) OR (V_TIPCUSTEST = 42) THEN
               V_CUSTTOTENTR := C_MOVIEST.VRCUSTENTRME;
         ELSE
            V_CUSTTOTENTR := CUSTO_PRODUTO_LCEST(C_MOVIEST.CDFILIAL, C_MOVIEST.CDPRODUTO, V_TIPCUSTEST, C_MOVIEST.DTLANCAMEN, V_DATAPROC,
                                                C_MOVIEST.CDALMOXARIFE, C_MOVIEST.CDLOCALESTOQ, C_MOVIEST.NRLOTEESTQ, C_MOVIEST.NRSUBLOTE) * C_MOVIEST.QTLANCTOEST;
         END IF;

         GRAVA_RELAT_MOVIEST_ANALI(C_MOVIEST.CDFILIAL,    C_MOVIEST.CDPRODUTO,    C_MOVIEST.CDARVPROD,    C_MOVIEST.NRLOTEESTQ,   C_MOVIEST.NRSUBLOTE,
                                   C_MOVIEST.DTLANCAMEN,   C_MOVIEST.IDTIPOLANC,   C_MOVIEST.CDFILLANC,    C_MOVIEST.NRLANCAMEN,   C_MOVIEST.NRSEQULANC,
                                   C_MOVIEST.QTDEESTO,     C_MOVIEST.QTLANCAMEN,   C_MOVIEST.IDALTCMEDMV,  C_MOVIEST.QTLANCTOEST,  C_MOVIEST.IDORDEMCLC,
                                   C_MOVIEST.QTTOTALANTE,  C_MOVIEST.CDALMOXARIFE, C_MOVIEST.CDLOCALESTOQ, C_MOVIEST.CDTIPORETIME, C_MOVIEST.IDENTRSAIDME,
                                   C_MOVIEST.CDTIPOOPERME, C_MOVIEST.CUSTMEDI,     C_MOVIEST.CUSTESTO,     C_MOVIEST.CUSTBRUT,     C_MOVIEST.CUSTMEES,
                                   C_MOVIEST.CUSTMEBR,     C_MOVIEST.VRLANCTOEST,  C_MOVIEST.VRLANCTOBRUT, C_MOVIEST.VRUNILANCTO,  C_MOVIEST.VRTOTLANCTO,
                                   C_MOVIEST.VRTOTALANTE,  V_CUSTTOTENTR,          C_MOVIEST.VRCUSTLOGLME, C_MOVIEST.VRCUSTSEGLME, C_MOVIEST.CUSTLOGESTO,
                                   C_MOVIEST.CUSTSEGESTO,  C_MOVIEST.VRPESOBRUTME, C_MOVIEST.NVPROD,       C_MOVIEST.NMPRODUTO,    C_MOVIEST.SGUNIDADE,
                                   C_MOVIEST.NMTIPORETIME);
      END LOOP;

      FOR C_MOVIEST IN C_IMPLANTACAO LOOP
         GRAVA_RELAT_MOVIEST_ANALI(C_MOVIEST.CDFILIAL,    C_MOVIEST.CDPRODUTO,    C_MOVIEST.CDARVPROD,    C_MOVIEST.NRLOTEESTQ,   C_MOVIEST.NRSUBLOTE,
                                   C_MOVIEST.DTLANCAMEN,   C_MOVIEST.IDTIPOLANC,   C_MOVIEST.CDFILLANC,    C_MOVIEST.NRLANCAMEN,   C_MOVIEST.NRSEQULANC,
                                   C_MOVIEST.QTDEESTO,     C_MOVIEST.QTLANCAMEN,   C_MOVIEST.IDALTCMEDMV,  C_MOVIEST.QTLANCTOEST,  C_MOVIEST.IDORDEMCLC,
                                   C_MOVIEST.QTTOTALANTE,  C_MOVIEST.CDALMOXARIFE, C_MOVIEST.CDLOCALESTOQ, C_MOVIEST.CDTIPORETIME, C_MOVIEST.IDENTRSAIDME,
                                   C_MOVIEST.CDTIPOOPERME, C_MOVIEST.CUSTMEDI,     C_MOVIEST.CUSTESTO,     C_MOVIEST.CUSTBRUT,     C_MOVIEST.CUSTMEES,
                                   C_MOVIEST.CUSTMEBR,     C_MOVIEST.VRLANCTOEST,  C_MOVIEST.VRLANCTOBRUT, C_MOVIEST.VRUNILANCTO,  C_MOVIEST.VRTOTLANCTO,
                                   C_MOVIEST.VRTOTALANTE,  C_MOVIEST.VRCUSTENTRME, C_MOVIEST.VRCUSTLOGLME, C_MOVIEST.VRCUSTSEGLME, C_MOVIEST.CUSTLOGESTO,
                                   C_MOVIEST.CUSTSEGESTO,  C_MOVIEST.VRPESOBRUTME, C_MOVIEST.NVPROD,       C_MOVIEST.NMPRODUTO,    C_MOVIEST.SGUNIDADE,
                                   C_MOVIEST.NMTIPORETIME);
      END LOOP;

      ---------------------------------------------------------------------------------
      -- INSERE POSICAO INICIAL PARA PRODUTOS                                        --
      ---------------------------------------------------------------------------------
      V_CDPRODUTO    := ' ';
      V_NRLOTEESTQ   := 'X';
      V_NRSUBLOTE    := 'X';
      V_CDALMOXLCEST := ' ';
      V_DIFCUSTSAI   := 0;
      V_VRANTEST     := 0;

      DBMS_OUTPUT.PUT_LINE('C_movimenta');
      FOR C_MOVIMENTA IN(SELECT R.*
                           FROM RELAT_MOVIEST_ANALI R
                          WHERE R.CDFILIMOVIEST = V_CDFILIAL
                            AND R.CDALMOXARIRM  = V_CDALMOXARIFE
                            AND R.CDLOCALESTRM  = V_CDLOCALESTOQ
                            AND R.DTLANCMOVIEST BETWEEN P_DTINICIAL AND P_DTFINAL
                          ORDER BY CDPRODMOVIEST, NRLOTEESTQME, NRSUBLOTEME,
                                   DTLANCMOVIEST, NVL(IDORDEMCALC,IDTIPOMOVIEST),
                                   NRLANCMOVIEST, NRSEQUMOVIEST)
      LOOP
         V_DIFCUSTSAI :=  0;
         IF (C_MOVIMENTA.IDTIPOMOVIEST <> '-')          AND
            ((V_CDPRODUTO  <> C_MOVIMENTA.CDPRODMOVIEST) OR
             (V_NRLOTEESTQ <> C_MOVIMENTA.NRLOTEESTQME)  OR
             (V_NRSUBLOTE  <> C_MOVIMENTA.NRSUBLOTEME)) THEN

            GRAVA_RELAT_MOVIEST_ANALI (V_CDFILIAL,               C_MOVIMENTA.CDPRODUTO,    C_MOVIMENTA.CDPRODMOVIEST, C_MOVIMENTA.NRLOTEESTQME,  C_MOVIMENTA.NRSUBLOTEME,
                                     P_DTINICIAL,               '-',                      C_MOVIMENTA.CDFILIMOVIEST, '------',                  '0001',
                                     0,                         0,                        ' ',                       0,                         '-',
                                     0,                         C_MOVIMENTA.CDALMOXARIRM, C_MOVIMENTA.CDLOCALESTRM,  NULL,                      NULL,
                                     NULL,                      0,                        0,                         0,                         0,
                                     0,                         0,                        0,                         0,                         0,
                                     0,                         0,                        C_MOVIMENTA.VRCUSTLOGLME,  C_MOVIMENTA.VRCUSTSEGLME,  C_MOVIMENTA.VRCUSTLOGEME,
                                     C_MOVIMENTA.VRCUSTSEGEME,  C_MOVIMENTA.VRPESOBRUTME, C_MOVIMENTA.CDTOTMOVIEST,  C_MOVIMENTA.NMPRODMOVIEST, C_MOVIMENTA.SGUNIDMOVI,
                                     NULL);
            V_VRATUAL  := 0;
            V_QTATUAL  := 0;
            V_QTMOVI   := 0;
            V_CUSTMED  := 0;
            V_CUSTANT  := 0;
            V_QTDEANT  := 0;
            V_CUSTZERO := 0;
            V_VRCUSTLOGEME := 0;
            V_VRCUSTSEGEME := 0;
            V_ULTCUSTENT   := 0;
         END IF;

         ---------------------------------------------------------------------------------
         -- ALTERA OS VALORES DE ESTOQUE PARA OS PRODUTOS                               --
         ---------------------------------------------------------------------------------
         BEGIN
            SELECT CDPRODESTO
            INTO V_CDPRODESTO
            FROM PRODUTO
            WHERE CDPRODUTO = C_MOVIMENTA.CDPRODUTO;
         EXCEPTION
            WHEN NO_DATA_FOUND THEN
               V_CDPRODESTO := NULL;
         END;

         IF V_TIPCUSTEST <> 3 THEN
               IF C_MOVIMENTA.IDTIPOMOVIEST = '-' THEN
                  V_CUSTESTOQ := CUSTO_PRODUTO_LCEST(V_CDFILIAL, V_CDPRODESTO, V_TIPCUSTEST, C_MOVIMENTA.DTLANCMOVIEST - 1, V_DATAPROC,
                                                     C_MOVIMENTA.CDALMOXARIRM, C_MOVIMENTA.CDLOCALESTRM, C_MOVIMENTA.NRLOTEESTQME, C_MOVIMENTA.NRSUBLOTEME);

                ELSE
                   V_CUSTESTOQ := CUSTO_PRODUTO_LCEST(V_CDFILIAL, V_CDPRODESTO, V_TIPCUSTEST, C_MOVIMENTA.DTLANCMOVIEST, V_DATAPROC,
                                                      C_MOVIMENTA.CDALMOXARIRM,C_MOVIMENTA.CDLOCALESTRM, C_MOVIMENTA.NRLOTEESTQME, C_MOVIMENTA.NRSUBLOTEME);
                END IF;
         END IF;

         IF (V_CUSTESTOQ IS NULL) THEN
            V_CUSTESTOQ := 0;
         END IF;

         IF C_MOVIMENTA.IDTIPOMOVIEST = '-' THEN

            V_QTATUAL  := C_MOVIMENTA.QTDEMOVIEST;
            V_QTMOVI   := C_MOVIMENTA.QTLANCAMENTO;
            V_QTDEANT  := V_QTATUAL;
            V_QTMOVANT := V_QTATUAL;

            V_QTPOSINI := V_QTATUAL;

            V_VRCUSTLOGEME := C_MOVIMENTA.VRCUSTLOGEME;
            V_VRCUSTSEGEME := C_MOVIMENTA.VRCUSTSEGEME;

            V_VRCUSTLOGANT := V_VRCUSTLOGEME;
            V_VRCUSTSEGANT := V_VRCUSTSEGEME;

            V_VRMOVSEGANT := V_VRCUSTLOGEME;
            V_VRMOVLOGANT := V_VRCUSTSEGEME;

            IF V_TIPCUSTEST <> 3 THEN
               V_CUSTMED   := V_CUSTESTOQ;
               V_CUSTMEES  := V_CUSTESTOQ;
               V_CUSTANT   := V_VRATUAL;

               IF (C_MOVIMENTA.QTDEMOVIEST <> 0) THEN
                  V_VRATUAL := V_CUSTESTOQ * C_MOVIMENTA.QTDEMOVIEST;
               ELSE
                  V_VRATUAL := V_CUSTESTOQ;
               END IF;
            ELSE
               IF P_IDCUSTO = 1 THEN
                  V_VRATUAL  := C_MOVIMENTA.VRCUSTMOVIEST;
                  V_CUSTMED  := C_MOVIMENTA.VRCUSTMEDI;
                  V_CUSTMEES := C_MOVIMENTA.VRCUSTMEES;
                  V_CUSTANT  := V_VRATUAL;
               ELSIF P_IDCUSTO = 2 THEN
                  V_VRATUAL  := C_MOVIMENTA.VRCUSTBRUT;
                  V_CUSTMED  := C_MOVIMENTA.VRCUSTMEDI;
                  V_CUSTMEES := C_MOVIMENTA.VRCUSTMEBR;
                  V_CUSTANT  := V_VRATUAL;
               END IF;
               V_VRMOVANT  := V_VRATUAL;
            END IF;
            IF V_QTATUAL > 0 THEN
               V_ULTCUSTENT  := V_VRATUAL/ V_QTATUAL;
            END IF;

         ELSIF C_MOVIMENTA.IDTIPOMOVIEST = '0' THEN --Implantacao de estoque
            V_QTDEAJUSTE := 0;
            V_VLRESTOAJU := 0;
            V_VLRBRUTAJU := 0;

            V_VRCUSTLOGANT := V_VRCUSTLOGEME;
            V_VRCUSTSEGANT := V_VRCUSTSEGEME;

            IF V_QTATUAL > 0 THEN
               V_QTMOVANT := V_QTATUAL;
            END IF;

            IF V_VRATUAL > 0 THEN
               V_VRMOVANT  := V_VRATUAL;
            END IF;

           V_QTMOVI  := C_MOVIMENTA.QTLANCMOVIEST;

           V_VRCUSTLOGEME := 0;
           V_VRCUSTSEGEME := 0;

           V_QTATUAL := C_MOVIMENTA.QTLANCMOVIEST;

           IF V_QTATUAL <> 0 THEN
              IF V_TIPCUSTEST = 3 THEN
                 IF P_IDCUSTO = 1 THEN
                    V_VRATUAL := C_MOVIMENTA.VRLANCMOVIEST;
                 ELSIF P_IDCUSTO = 2 THEN
                    V_VRATUAL := C_MOVIMENTA.VRBRUTMOVIEST;
                 END IF;
                 V_CUSTMED := V_VRATUAL/V_QTATUAL;
              ELSE
                 V_CUSTMED := CUSTO_PRODUTO_LCEST(V_CDFILIAL, V_CDPRODESTO, V_TIPCUSTEST, C_MOVIMENTA.DTLANCMOVIEST,  V_DATAPROC,
                                                  C_MOVIMENTA.CDALMOXARIRM, C_MOVIMENTA.CDLOCALESTRM, C_MOVIMENTA.NRLOTEESTQME, C_MOVIMENTA.NRSUBLOTEME);
              V_VRATUAL := V_CUSTMED * C_MOVIMENTA.QTLANCMOVIEST;
              END IF;
           ELSE
              V_CUSTMED := 0;
           END IF;

           V_ULTCUSTENT := V_CUSTMED;

         ELSIF C_MOVIMENTA.IDTIPOMOVIEST = '1' THEN --Ajuste de inventario

            V_QTDEAJUSTE := 0;
            V_VLRESTOAJU := 0;
            V_VLRBRUTAJU := 0;
            V_VRCUSTLOGANT := V_VRCUSTLOGEME;
            V_VRCUSTSEGANT := V_VRCUSTSEGEME;

            IF V_QTATUAL > 0 THEN
               V_QTMOVANT := V_QTATUAL;
            END IF;

            IF V_VRATUAL > 0 THEN
               V_VRMOVANT  := V_VRATUAL;
            END IF;

            V_QTMOVI := C_MOVIMENTA.QTLANCMOVIEST - V_QTATUAL;

             IF V_QTMOVI < 0 THEN
                IF V_QTATUAL > 0 THEN
                   V_VRCUSTLOGEME := V_VRCUSTLOGEME - ((V_VRCUSTLOGEME/V_QTATUAL) * ABS(V_QTMOVI));
                   V_VRCUSTSEGEME := V_VRCUSTSEGEME - ((V_VRCUSTSEGEME/V_QTATUAL) * ABS(V_QTMOVI));
                END IF;
             ELSE
                IF V_QTMOVI > 0 THEN
                   IF V_QTATUAL > 0 THEN
                      V_VRCUSTLOGEME := V_VRCUSTLOGEME + ((V_VRCUSTLOGEME/V_QTATUAL) * ABS(V_QTMOVI));
                      V_VRCUSTSEGEME := V_VRCUSTSEGEME + ((V_VRCUSTSEGEME/V_QTATUAL) * ABS(V_QTMOVI));
                   END IF;
                END IF;
             END IF;


             IF C_MOVIMENTA.IDALTCMEDMOV = w_S THEN
                IF C_MOVIMENTA.QTLANCMOVIEST <> 0 THEN
                   IF V_VRATUAL <> 0 THEN
                      V_CUSTMED := V_VRATUAL/C_MOVIMENTA.QTLANCMOVIEST;
                   ELSE
                      V_CUSTMED := C_MOVIMENTA.VRTOTALANT/C_MOVIMENTA.QTLANCMOVIEST;
                   END IF;
                ELSE
                   V_CUSTMED := 0;

                END IF;
             ELSE
                IF V_QTATUAL > 0 THEN
                   IF V_VRATUAL <> 0 THEN
                      V_CUSTMED := V_VRATUAL/V_QTATUAL;
                   ELSE
                     V_CUSTMED := V_CUSTZERO;
                      IF (V_VRCUSTLOGEME + V_VRCUSTSEGEME) = 0 THEN
                         IF V_QTMOVANT > 0 THEN
                            V_CUSTMED :=  (((C_MOVIMENTA.QTLANCMOVIEST*V_CUSTZERO) - V_VRMOVANT)- (V_VRMOVLOGANT + V_VRMOVSEGANT))/V_QTMOVANT;
                         END IF;
                      END IF;
                   END IF;
                ELSE

                   V_CUSTMED := V_CUSTZERO;

                   /*Se quantidade zero e CL zero, busca o custo medio sem logistica*/
                   IF ((V_VRCUSTLOGEME + V_VRCUSTSEGEME) = 0) and ((V_VRMOVLOGANT + V_VRMOVSEGANT)> 0)  THEN
                         IF V_QTMOVANT > 0 THEN
                            V_CUSTMED := (V_VRMOVANT - (V_VRMOVLOGANT + V_VRMOVSEGANT))/V_QTMOVANT;
                         END IF;
                   END IF;

                   IF (V_QTMOVANT - V_QTMOVI) > 0 THEN
                      IF V_QTMOVANT > 0 THEN
                         V_VRCUSTLOGEME := V_VRCUSTLOGEME - ((V_VRCUSTLOGEME/V_QTMOVANT) * ABS(V_QTMOVI));
                         V_VRCUSTSEGEME := V_VRCUSTSEGEME - ((V_VRCUSTSEGEME/V_QTMOVANT) * ABS(V_QTMOVI));
                      END IF;
                   ELSE
                      IF (V_QTMOVANT - V_QTMOVI) < 0 THEN
                         IF V_QTMOVANT > 0 THEN
                            V_VRCUSTLOGEME := V_VRCUSTLOGEME + ((V_VRCUSTLOGEME/V_QTMOVANT) * ABS(V_QTMOVI));
                            V_VRCUSTSEGEME := V_VRCUSTSEGEME + ((V_VRCUSTSEGEME/V_QTMOVANT) * ABS(V_QTMOVI));
                         END IF;
                      END IF;
                   END IF;
                END IF;
             END IF;

             --calcula valor do estoque considerando custo negativo
            IF (V_IDPERMESTNEG = 'S') THEN
                IF (V_TIPCUSTEST = 3) THEN
                   V_CUSTMED := ROUND(CUSTO_PRODUTO_LCEST(V_CDFILIAL, V_CDPRODESTO, V_TIPCUSTEST, C_MOVIMENTA.DTLANCMOVIEST,  V_DATAPROC,
                                      C_MOVIMENTA.CDALMOXARIRM, C_MOVIMENTA.CDLOCALESTRM, C_MOVIMENTA.NRLOTEESTQME, C_MOVIMENTA.NRSUBLOTEME),5);
                ELSE
                   V_CUSTMED := ROUND(CUSTO_PRODUTO_LCEST(V_CDFILIAL, V_CDPRODESTO, V_TIPCUSTEST, C_MOVIMENTA.DTLANCMOVIEST,  V_DATAPROC,
                                      C_MOVIMENTA.CDALMOXARIRM, C_MOVIMENTA.CDLOCALESTRM, C_MOVIMENTA.NRLOTEESTQME, C_MOVIMENTA.NRSUBLOTEME),5);
                END IF;
             END IF;

             V_QTATUAL := C_MOVIMENTA.QTLANCMOVIEST;
             V_VRATUAL := V_CUSTMED * V_QTATUAL;
             V_ULTCUSTENT := V_CUSTMED;

            IF ((C_MOVIMENTA.QTLANCMOVIEST > 0 ) AND (V_QTMOVI >  0)) OR (V_QTMOVANT = 0)  THEN
               IF (V_VRCUSTLOGEME+ V_VRCUSTSEGEME) <= (V_VRCUSTSEGANT+V_VRCUSTLOGANT) THEN
                  V_DIFCUSTSAI := (V_VRCUSTSEGANT+V_VRCUSTLOGANT)*(-1);
               END IF;
            END IF;

         ELSIF C_MOVIMENTA.IDTIPOMOVIEST = '2' THEN --Entradas no estoque
            V_VRCUSTLOGANT := V_VRCUSTLOGEME;
            V_VRCUSTSEGANT := V_VRCUSTSEGEME;

            V_QTMOVI  := C_MOVIMENTA.QTLANCMOVIEST;
            V_QTMOVANT := V_QTATUAL;
           IF (C_MOVIMENTA.QTLANCMOVIEST <> 0)  OR (C_MOVIMENTA.CDTIPOOPERME = '03') THEN
              IF V_VRATUAL > 0 THEN
                 V_VRMOVANT  := V_VRATUAL;
              END IF;

              IF (V_TIPCUSTEST <> 3) and (V_TIPCUSTEST <> 4) AND
                 (V_TIPCUSTEST <> 42) and (V_TIPCUSTEST <> 43) THEN
                 V_CUSTMED := CUSTO_PRODUTO_LCEST(V_CDFILIAL, V_CDPRODESTO, V_TIPCUSTEST, C_MOVIMENTA.DTLANCMOVIEST,  V_DATAPROC,
                                                  C_MOVIMENTA.CDALMOXARIRM, C_MOVIMENTA.CDLOCALESTRM, C_MOVIMENTA.NRLOTEESTQME, C_MOVIMENTA.NRSUBLOTEME);
              ELSE
                 IF (P_IDCUSTO = 1) and (V_TIPCUSTEST <> 4) and (V_TIPCUSTEST <> 43)  THEN
                    V_CUSTMED := C_MOVIMENTA.VRLANCMOVIEST;
                 ELSIF (P_IDCUSTO = 2 ) or (V_TIPCUSTEST = 4) or (V_TIPCUSTEST = 43)  THEN
                    V_CUSTMED := C_MOVIMENTA.VRBRUTMOVIEST;
                 END IF;
              END IF;

              IF (V_QTATUAL = 0)  THEN
                 V_VRATUAL := V_VRATUAL + NVL(V_VRCUSTLOGEME,0) + NVL(V_VRCUSTSEGEME,0);
              END IF;

              V_VRCUSTLOGEME := V_VRCUSTLOGEME + C_MOVIMENTA.VRCUSTLOGLME;
              V_VRCUSTSEGEME := V_VRCUSTSEGEME + C_MOVIMENTA.VRCUSTSEGLME;

              V_VRATUEST := V_QTATUAL * V_CUSTMED;
              V_VRANTEST := V_VRATUAL;

              V_QTANTENTR := V_QTATUAL;
              V_QTATUAL := V_QTATUAL + V_QTMOVI;

              --calcula valor do estoque considerando custo negativo
              IF (V_IDPERMESTNEG = 'S') AND
                 (V_QTANTENTR < 0)      THEN
                  IF (V_TIPCUSTEST = 3) THEN
                     V_VRCUSTTOTNEG := ROUND(CUSTO_PRODUTO_LCEST(V_CDFILIAL, V_CDPRODESTO, V_TIPCUSTEST, C_MOVIMENTA.DTLANCMOVIEST,  V_DATAPROC,
                                                  C_MOVIMENTA.CDALMOXARIRM, C_MOVIMENTA.CDLOCALESTRM, C_MOVIMENTA.NRLOTEESTQME, C_MOVIMENTA.NRSUBLOTEME),5);
                  ELSE
                     V_VRCUSTTOTNEG := ROUND(CUSTO_PRODUTO_LCEST(V_CDFILIAL, V_CDPRODESTO, V_TIPCUSTEST, C_MOVIMENTA.DTLANCMOVIEST,  V_DATAPROC,
                                                  C_MOVIMENTA.CDALMOXARIRM, C_MOVIMENTA.CDLOCALESTRM, C_MOVIMENTA.NRLOTEESTQME, C_MOVIMENTA.NRSUBLOTEME),5);
                  END IF;

                  V_VRATUAL := V_QTATUAL * V_VRCUSTTOTNEG;
              ELSE
                 IF (V_TIPCUSTEST <> 3) AND (V_TIPCUSTEST <> 4) THEN
                    IF V_QTMOVI <> 0 THEN
                       IF V_CUSTMED > 0 THEN
                         V_VRATUAL := V_QTATUAL * V_CUSTMED;
                       END IF;
                    END IF;
                 ELSE
                    IF V_QTMOVI = 0 THEN
                       V_VRATUAL := V_VRATUAL + V_CUSTMED;
                    ELSE
                       IF V_CUSTMED > 0 THEN
                          V_VRATUAL := V_VRATUAL + (V_QTMOVI * V_CUSTMED);
                       END IF;
                    END IF;
                 END IF;
              END IF;

              V_VRATUAL := V_VRATUAL + C_MOVIMENTA.VRCUSTLOGLME + C_MOVIMENTA.VRCUSTSEGLME;
              IF (V_TIPCUSTEST <> 3) AND (V_TIPCUSTEST <> 4) THEN
                  IF V_QTMOVI > 0 THEN

                     V_DIFCUSTSAI := (V_VRATUEST - V_VRANTEST);
                  END IF;
               END IF;
              IF V_QTATUAL > 0 THEN
                 V_ULTCUSTENT := V_VRATUAL/V_QTATUAL;
              END IF;
           ELSE
             V_CUSTMED := 0;
           END IF;
         ELSIF C_MOVIMENTA.IDTIPOMOVIEST = '3' THEN --Retiradas de estoque

            V_VRCUSTLOGANT := V_VRCUSTLOGEME;
            V_VRCUSTSEGANT := V_VRCUSTSEGEME;

            V_QTMOVI   := C_MOVIMENTA.QTLANCMOVIEST;
            V_QTMOVANT := V_QTATUAL;
            IF V_VRATUAL > 0 THEN
               V_VRMOVANT  := V_VRATUAL;
            END IF;

            /*Para calculo do valor sem logistica no ajuste*/

            IF (V_VRCUSTLOGEME+V_VRCUSTSEGEME) > 0 THEN
               V_VRMOVSEGANT := V_VRCUSTLOGEME;
               V_VRMOVLOGANT := V_VRCUSTSEGEME;
            END IF;

            IF V_QTATUAL <> 0 THEN
               V_VRCUSTLOGEME := V_VRCUSTLOGEME - ((V_VRCUSTLOGEME/V_QTATUAL) * V_QTMOVI);
               V_VRCUSTSEGEME := V_VRCUSTSEGEME - ((V_VRCUSTSEGEME/V_QTATUAL) * V_QTMOVI);

               IF V_TIPCUSTEST <> 3 THEN
                  V_CUSTMED := V_CUSTESTOQ;
               ELSE
                  V_CUSTMED := V_VRATUAL/V_QTATUAL;
               END IF;
            ELSE
               V_CUSTMED := 0;
            END IF;

            IF (V_IDPERMESTNEG <> 'S') THEN
                  IF ((V_QTATUAL - V_QTMOVI) < 0) AND (V_QTATUAL <= 0) THEN
                     V_CUSTMED := V_CUSTZERO;
                  END IF;
            END IF;

            V_VRANTEST := V_VRATUAL;
            V_VRATUAL := (V_QTATUAL - V_QTMOVI)*V_CUSTMED;

            V_QTATUAL := V_QTATUAL - V_QTMOVI;

            IF ROUND(V_ULTCUSTENT,8) <> ROUND(V_CUSTMED,8) THEN
               IF V_QTMOVI > 0 THEN
                  V_DIFCUSTSAI := (V_VRANTEST - V_VRATUAL)/V_QTMOVI;
               END IF;
            END IF;

         ELSIF C_MOVIMENTA.IDTIPOMOVIEST = '4' THEN --Saidas de estoque
            V_VRCUSTLOGANT := V_VRCUSTLOGEME;
            V_VRCUSTSEGANT := V_VRCUSTSEGEME;

            V_QTMOVI     := C_MOVIMENTA.QTLANCMOVIEST;
            V_QTMOVANT   := V_QTATUAL;

            /*Para calculo do valor sem logistica no ajuste*/

            IF (V_VRCUSTLOGEME+V_VRCUSTSEGEME) > 0 THEN
               V_VRMOVSEGANT := V_VRCUSTLOGEME;
               V_VRMOVLOGANT := V_VRCUSTSEGEME;
            END IF;

            IF V_VRATUAL > 0 THEN
               V_VRMOVANT  := V_VRATUAL;
            END IF;

            IF V_QTATUAL <> 0 THEN
               IF (V_QTATUAL <> 0) THEN
                  V_VRCUSTLOGEME := V_VRCUSTLOGEME - ((V_VRCUSTLOGEME/V_QTATUAL) * V_QTMOVI);
                  V_VRCUSTSEGEME := V_VRCUSTSEGEME - ((V_VRCUSTSEGEME/V_QTATUAL) * V_QTMOVI);
               END IF;

               IF (V_TIPCUSTEST <> 3) THEN
                  V_CUSTMED := V_CUSTESTOQ;
               ELSE
                  V_CUSTMED := V_VRATUAL/V_QTATUAL;
               END IF;
            ELSE
               V_CUSTMED := 0;
            END IF;

            IF (V_IDPERMESTNEG <> 'S') THEN
               IF ((V_QTATUAL - V_QTMOVI) < 0) AND (V_QTATUAL <= 0) THEN
                  V_CUSTMED := V_CUSTZERO;
               END IF;
            END IF;

            V_VRANTEST := V_VRATUAL;
            V_QTATUAL  := V_QTATUAL - V_QTMOVI;

            V_VRATUAL  := V_QTATUAL * V_CUSTMED;

            IF V_ULTCUSTENT <> V_CUSTMED THEN
               IF V_QTMOVI > 0 THEN
                  V_DIFCUSTSAI := (V_VRANTEST - V_VRATUAL)/V_QTMOVI;
               END IF;
               V_ULTCUSTENT   := V_CUSTMED;
            END IF;

         ELSIF C_MOVIMENTA.IDTIPOMOVIEST = '5' THEN --Devolucao ao fornecedor
            IF V_QTATUAL <> 0 THEN
               V_CUSTOMEDIO   := V_VRATUAL/V_QTATUAL;
            END IF;
            V_VRCUSTLOGANT := V_VRCUSTLOGEME;
            V_VRCUSTSEGANT := V_VRCUSTSEGEME;

            V_VRANTEST := V_VRATUAL;

            V_QTMOVI  := C_MOVIMENTA.QTLANCMOVIEST;

            IF (V_QTATUAL - V_QTMOVI) <> 0 THEN
               IF P_IDCUSTO = 1 THEN
                  IF (V_VRATUAL - (V_QTMOVI * C_MOVIMENTA.VRLANCMOVIEST)) >= 0 THEN
                     V_CUSTMED := C_MOVIMENTA.VRLANCMOVIEST;
                  ELSE
                     IF (V_VRATUAL - (V_QTMOVI * V_CUSTMED)) < 0 THEN
                        IF V_QTATUAL <> 0 THEN
                           V_CUSTMED := V_VRATUAL / V_QTATUAL;
                        ELSE
                           V_CUSTMED := CUSTO_PRODUTO_LCEST(V_CDFILIAL, V_CDPRODESTO, V_TIPCUSTEST, C_MOVIMENTA.DTLANCMOVIEST, V_DATAPROC,
                                                  C_MOVIMENTA.CDALMOXARIRM,C_MOVIMENTA.CDLOCALESTRM, C_MOVIMENTA.NRLOTEESTQME, C_MOVIMENTA.NRSUBLOTEME);
                        END IF;
                     END IF;
                  END IF;
               ELSIF P_IDCUSTO = 2 THEN
                  IF (V_VRATUAL - (V_QTMOVI * C_MOVIMENTA.VRBRUTMOVIEST)) >= 0 THEN
                    V_CUSTMED := C_MOVIMENTA.VRBRUTMOVIEST;
                  ELSE
                     IF (V_VRATUAL - (V_QTMOVI * V_CUSTMED)) < 0 THEN
                        IF V_QTATUAL <> 0 THEN
                           V_CUSTMED := V_VRATUAL / V_QTATUAL;
                        ELSE
                           V_CUSTMED := CUSTO_PRODUTO_LCEST(V_CDFILIAL, V_CDPRODESTO, V_TIPCUSTEST, C_MOVIMENTA.DTLANCMOVIEST, V_DATAPROC,
                                                  C_MOVIMENTA.CDALMOXARIRM,C_MOVIMENTA.CDLOCALESTRM, C_MOVIMENTA.NRLOTEESTQME, C_MOVIMENTA.NRSUBLOTEME);
                        END IF;
                     END IF;
                  END IF;
               END IF;
            END IF;

            V_QTATUAL := V_QTATUAL - V_QTMOVI;
            /*Se o custo da devolucao for diferente do custo medio no dia,
             guarda o valor do custo medio para depois calcular a diferenca*/
            IF (V_ULTCUSTENT <> V_CUSTMED) AND ((V_VRATUAL - (V_QTMOVI * C_MOVIMENTA.VRLANCMOVIEST)) >= 0)  THEN
               IF V_QTMOVI > 0 THEN
                  V_DIFCUSTSAI := V_ULTCUSTENT;
               END IF;
            END IF;

            IF(C_MOVIMENTA.VRLANCMOVIEST*V_QTMOVI > V_VRATUAL) THEN
               V_CUSTMED := V_CUSTOMEDIO;
            ELSE
               IF  P_IDCUSTO = 2 THEN
                  V_CUSTMED :=  C_MOVIMENTA.VRBRUTMOVIEST;
               ELSE
                  V_CUSTMED := C_MOVIMENTA.VRLANCMOVIEST;
               END IF;
            END IF;

            IF V_QTMOVI = 0 THEN
               V_VRATUAL := V_VRATUAL - V_CUSTMED;
            ELSE
               V_VRATUAL := V_VRATUAL - (V_QTMOVI * V_CUSTMED);
            END IF;

            IF (V_QTATUAL = 0) AND (V_VRATUAL < 0) THEN
               IF V_QTMOVI <> 0 THEN
                  V_CUSTMED := V_CUSTMED + (V_VRATUAL/V_QTMOVI);
               END IF;
            END IF;
         END IF;

         IF V_QTATUAL = 0 THEN
            IF V_CUSTMEES <> 0 THEN
               V_CUSTZERO := V_CUSTMEES;
            END IF;

            IF V_CUSTZERO = 0 THEN
               V_CUSTZERO := C_MOVIMENTA.VRCUSTMEES;
            END IF;
         END IF;

         IF C_MOVIMENTA.IDTIPOMOVIEST <> '-' THEN
            IF V_QTATUAL = 0 THEN
               V_CUSTMEES := 0;
            ELSE
               IF V_TIPCUSTEST IN (3,4) THEN
                   IF V_QTATUAL <> 0 THEN
                      V_CUSTMEES := V_VRATUAL/V_QTATUAL;
                   END IF;
               ELSE
                   V_CUSTMEES :=  CUSTO_PRODUTO_LCEST(V_CDFILIAL, V_CDPRODESTO, V_TIPCUSTEST, C_MOVIMENTA.DTLANCMOVIEST, V_DATAPROC,
                                                    C_MOVIMENTA.CDALMOXARIRM,C_MOVIMENTA.CDLOCALESTRM, C_MOVIMENTA.NRLOTEESTQME, C_MOVIMENTA.NRSUBLOTEME);
               END IF;
            END IF;
         END IF;

         IF V_QTATUAL =  0 THEN
            V_VRATUAL := 0;
         END IF;

         IF (V_QTATUAL <> 0) THEN
            V_CUSTZERO := V_CUSTMED;
         END IF;

         UPDATE RELAT_MOVIEST_ANALI
         SET QTLANCAMENTO  = V_QTMOVI,
             VRCUSTMEDI    = V_CUSTMED,
             VRCUSTMOVIEST = V_VRATUAL,
             QTDEMOVIEST   = V_QTATUAL,
             VRCUSTMEES    = V_CUSTMEES,
             VRDIFECUST    = DECODE(C_MOVIMENTA.IDTIPOMOVIEST,'1', V_QTMOVI * V_CUSTMED,V_VRATUAL - V_CUSTANT),
             VRTOTALANT    = V_CUSTANT,
             QTTOTALANT    = V_QTDEANT,
             VRCUSTLOGEME  = V_VRCUSTLOGEME,
             VRCUSTSEGEME  = V_VRCUSTSEGEME,
             VRDIFCUSTEME  = NVL(V_DIFCUSTSAI,0)
         WHERE CDFILIMOVIEST = V_CDFILIAL                  AND
               NRLANCMOVIEST = C_MOVIMENTA.NRLANCMOVIEST AND
               NRSEQUMOVIEST = C_MOVIMENTA.NRSEQUMOVIEST AND
               CDPRODMOVIEST = C_MOVIMENTA.CDPRODMOVIEST AND
               NRLOTEESTQME  = C_MOVIMENTA.NRLOTEESTQME  AND
               NRSUBLOTEME   = C_MOVIMENTA.NRSUBLOTEME   AND
               DTLANCMOVIEST = C_MOVIMENTA.DTLANCMOVIEST AND
               CDALMOXARIRM  = C_MOVIMENTA.CDALMOXARIRM  AND
               CDLOCALESTRM  = C_MOVIMENTA.CDLOCALESTRM  AND
               CDFILLANCMEST = C_MOVIMENTA.CDFILLANCMEST AND
               IDTIPOMOVIEST = C_MOVIMENTA.IDTIPOMOVIEST;

               ---APOS ATUALIZA TABELA DE DESCRICRICAO (Nao contido nesta procedure)
            V_QTDEANT      := V_QTATUAL;
            V_CDPRODUTO    := C_MOVIMENTA.CDPRODMOVIEST;
            V_NRLOTEESTQ   := C_MOVIMENTA.NRLOTEESTQME;
            V_NRSUBLOTE    := C_MOVIMENTA.NRSUBLOTEME;
            V_CDALMOXLCEST := C_MOVIMENTA.CDALMOXARIRM||C_MOVIMENTA.CDLOCALESTRM;
      END LOOP;

      --------------------------------------------------------------------------------
      -- ATUALIZA TABELA DE TOTALIZACAO                                             --
      --------------------------------------------------------------------------------
      V_INSERT := '1';

      FOR C_TOTALMOVI IN(
         (SELECT CDTOTMOVIEST, IDTIPOMOVIEST,
                 SUM(QTLANCAMENTO * VRCUSTMEDI) +
                 SUM(DECODE(QTLANCAMENTO, 0,DECODE(IDTIPOMOVIEST , '-',NVL(VRCUSTLOGEME,0)+NVL(VRCUSTSEGEME,0),0),0))  AS VRCUSTTOTAL,
                 SUM(QTLANCAMENTO) AS VRQTDETOTAL,
                 CDTIPORETIME, NMTIPORETIME, IDENTRSAIDME, CDTIPOOPERME,
                 SUM(DECODE(IDTIPOMOVIEST,'-', VRCUSTLOGEME,0))  AS VRCUSTLOGLME,
                 SUM(DECODE(IDTIPOMOVIEST,'-', VRCUSTSEGEME,0))  AS VRCUSTSEGLME,
                 SUM(VRDIFCUSTEME) VRDIFCUSTEME,
                 SUM(DECODE(VRDIFCUSTEME,0,0,(VRCUSTMEDI*QTLANCAMENTO)-(VRDIFCUSTEME*QTLANCAMENTO)))  AS VRDIFCUSSAID
          FROM RELAT_MOVIEST_ANALI
          WHERE NOT(IDTIPOMOVIEST IN ('0','1','2'))
            AND CDFILIMOVIEST = V_CDFILIAL
            AND CDALMOXARIRM  = V_CDALMOXARIFE
            AND CDLOCALESTRM  = V_CDLOCALESTOQ
            AND DTLANCMOVIEST BETWEEN P_DTINICIAL AND P_DTFINAL
          GROUP BY CDTOTMOVIEST, IDTIPOMOVIEST, CDTIPORETIME, NMTIPORETIME, IDENTRSAIDME, CDTIPOOPERME)
         UNION ALL
         (SELECT CDTOTMOVIEST, IDTIPOMOVIEST,
                 SUM(VRDIFECUST)   AS VRCUSTTOTAL,
                 SUM(QTLANCAMENTO) AS VRQTDETOTAL,
                 CDTIPORETIME, NMTIPORETIME, IDENTRSAIDME, CDTIPOOPERME,
                 0 AS VRCUSTLOGLME,
                 0 AS VRCUSTSEGLME,
                 SUM(VRDIFCUSTEME) VRDIFCUSTEME,
                 SUM(VRDIFCUSTEME)  AS VRDIFCUSSAID
          FROM RELAT_MOVIEST_ANALI
          WHERE IDTIPOMOVIEST IN ('0','1')
            AND CDFILIMOVIEST = V_CDFILIAL
            AND CDALMOXARIRM  = V_CDALMOXARIFE
            AND CDLOCALESTRM  = V_CDLOCALESTOQ
            AND DTLANCMOVIEST BETWEEN P_DTINICIAL AND P_DTFINAL
          GROUP BY CDTOTMOVIEST, IDTIPOMOVIEST, CDTIPORETIME, NMTIPORETIME, IDENTRSAIDME, CDTIPOOPERME)
         UNION ALL

         (SELECT CDTOTMOVIEST, IDTIPOMOVIEST,
                 SUM(VRCUSTENTRME)  AS VRCUSTTOTAL,
                 SUM(QTLANCAMENTO) AS VRQTDETOTAL,
                 CDTIPORETIME, NMTIPORETIME, IDENTRSAIDME, CDTIPOOPERME,
                 SUM(VRCUSTLOGLME) AS VRCUSTLOGLME,
                 SUM(VRCUSTSEGLME) AS VRCUSTSEGLME,
                 SUM(VRDIFCUSTEME) VRDIFCUSTEME,
                 sum(VRDIFCUSTEME) AS VRDIFCUSSAID
          FROM RELAT_MOVIEST_ANALI
          WHERE IDTIPOMOVIEST = '2'          --Entradas no estoque
            AND CDFILIMOVIEST = V_CDFILIAL
            AND CDALMOXARIRM  = V_CDALMOXARIFE
            AND CDLOCALESTRM  = V_CDLOCALESTOQ
            AND ((P_IDCUSTO = '1') AND (V_TIPCUSTEST <> 4) AND (V_TIPCUSTEST <> 43))
            AND DTLANCMOVIEST BETWEEN P_DTINICIAL AND P_DTFINAL
            AND QTLANCAMENTO  > 0
          GROUP BY CDTOTMOVIEST, IDTIPOMOVIEST, CDTIPORETIME, NMTIPORETIME, IDENTRSAIDME, CDTIPOOPERME

          UNION ALL

          SELECT CDTOTMOVIEST, IDTIPOMOVIEST,
                 SUM(DECODE(V_TIPCUSTEST,3,QTLANCMOVIEST * VRBRUTMOVIEST,
                     DECODE(V_TIPCUSTEST,4,QTLANCMOVIEST * VRBRUTMOVIEST,
                     DECODE(V_TIPCUSTEST,43,QTLANCMOVIEST * VRBRUTMOVIEST,VRCUSTENTRME)))) AS VRCUSTTOTAL,
                 SUM(QTLANCAMENTO) AS VRQTDETOTAL,
                 CDTIPORETIME, NMTIPORETIME, IDENTRSAIDME, CDTIPOOPERME,
                 SUM(VRCUSTLOGLME) AS VRCUSTLOGLME,
                 SUM(VRCUSTSEGLME) AS VRCUSTSEGLME,
                 SUM(VRDIFCUSTEME) VRDIFCUSTEME,
                 SUM(DECODE(V_TIPCUSTEST,3,0,VRDIFCUSTEME)) AS VRDIFCUSSAID
          FROM RELAT_MOVIEST_ANALI
          WHERE IDTIPOMOVIEST = '2'          --Entradas no estoque
            AND CDFILIMOVIEST = V_CDFILIAL
            AND CDALMOXARIRM  = V_CDALMOXARIFE
            AND CDLOCALESTRM  = V_CDLOCALESTOQ
            AND ((P_IDCUSTO = '2') OR (V_TIPCUSTEST = 4) OR (V_TIPCUSTEST = 43))
            AND DTLANCMOVIEST BETWEEN P_DTINICIAL AND P_DTFINAL
            AND QTLANCAMENTO  > 0
          GROUP BY CDTOTMOVIEST, IDTIPOMOVIEST, CDTIPORETIME, NMTIPORETIME, IDENTRSAIDME, CDTIPOOPERME

          UNION ALL

          SELECT CDTOTMOVIEST, IDTIPOMOVIEST,
                 SUM(DECODE(CDTIPOOPERME, '03', VRCUSTMEDI, 0)) AS VRCUSTTOTAL,
                 SUM(QTLANCAMENTO) AS VRQTDETOTAL,
                 CDTIPORETIME, NMTIPORETIME, IDENTRSAIDME, CDTIPOOPERME,
                 SUM(VRCUSTLOGLME) AS VRCUSTLOGLME,
                 SUM(VRCUSTSEGLME) AS VRCUSTSEGLME,
                 SUM(VRDIFCUSTEME) VRDIFCUSTEME,
                 0 AS VRDIFCUSSAID
          FROM RELAT_MOVIEST_ANALI
          WHERE IDTIPOMOVIEST = '2'          --Entradas no estoque
            AND CDFILIMOVIEST = V_CDFILIAL
            AND CDALMOXARIRM  = V_CDALMOXARIFE
            AND CDLOCALESTRM  = V_CDLOCALESTOQ
            AND DTLANCMOVIEST BETWEEN P_DTINICIAL AND P_DTFINAL
            AND QTLANCAMENTO  = 0
          GROUP BY CDTOTMOVIEST, IDTIPOMOVIEST, CDTIPORETIME, NMTIPORETIME, IDENTRSAIDME, CDTIPOOPERME)
         ORDER BY IDTIPOMOVIEST)

      LOOP

         SELECT COUNT(*)
           INTO V_COUNT
           FROM RELAT_TOTAMOVI_ANALI
          WHERE CDPRODTOTAMOVI = C_TOTALMOVI.CDTOTMOVIEST
            AND CDFILIAL  =  V_CDFILIAL
            AND CDALMOXARIFE = V_CDALMOXARIFE
            AND CDLOCALESTOQ = V_CDLOCALESTOQ;

         V_SGUNIDADE := 'X';

         FOR C_UNIDADE IN (SELECT SGUNIDMOVI
                             FROM RELAT_MOVIEST_ANALI
                            WHERE CDFILIMOVIEST = V_CDFILIAL
                              AND CDALMOXARIRM  = V_CDALMOXARIFE
                              AND CDLOCALESTRM  = V_CDLOCALESTOQ
                              AND CDTOTMOVIEST  = C_TOTALMOVI.CDTOTMOVIEST
                              AND DTLANCMOVIEST BETWEEN P_DTINICIAL AND P_DTFINAL
                            GROUP BY SGUNIDMOVI) LOOP

            IF V_SGUNIDADE = 'X' THEN
               V_SGUNIDADE := C_UNIDADE.SGUNIDMOVI;
            ELSE
               IF V_SGUNIDADE <> C_UNIDADE.SGUNIDMOVI THEN
                  IF (V_TAMNIVEL <> V_TAMARV) THEN
                     V_SGUNIDADE := ' ';
                  END IF;
               END IF;
            END IF;
         END LOOP;

         IF V_SGUNIDADE = 'X' THEN
            V_SGUNIDADE := ' ';
         END IF;

         IF (V_SGUNIDADE <> ' ') OR (V_TAMARV = V_TAMNIVEL) THEN
            V_VRQTDETOTAL := C_TOTALMOVI.VRQTDETOTAL;
         ELSE
            V_VRQTDETOTAL := 0;
         END IF;

         IF V_COUNT = 0 THEN
            INSERT INTO RELAT_TOTAMOVI_ANALI
               (CDFILIAL, IDTIPOTOTAMOVI, CDPRODTOTAMOVI, VRSOMPINI, VRSOMAINV, VRSOMINVE,
                VRSOMENTR, VRSOMRETI, VRSOMSAID, VRSOMDFOR, VRSOMSATU,
                VRQTDEPINIME, VRQTDEAINVME, VRQTDEINVEME, VRQTDEENTRME, VRQTDERETIME,
                VRQTDESAIDME, VRQTDEDFORME, VRQTDESATUME, VRSOMCUSTLOG, VRSOMCUSTSEG,
                VRSOMDIFSAID, CDALMOXARIFE, CDLOCALESTOQ)
            VALUES
               (V_CDFILIAL, 'P', C_TOTALMOVI.CDTOTMOVIEST, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0,0,0, V_CDALMOXARIFE, V_CDLOCALESTOQ);
         END IF;

         IF V_INSERT = '1' THEN

            V_INSERT := '2';

            INSERT INTO RELAT_ANALMOVI_ANALI
               (CDTIPORETIAN, NMTIPORETIAN, IDENTRSAIDAN, CDTIPOOPERAN,
                IDSALDOESTAN, VRSALDOESTAN, QTSALDOESTAN, CDFILIAL, CDALMOXARIFE, CDLOCALESTOQ)
            VALUES
               ('-', '-', '-', '-', 'SA', 0, 0, V_CDFILIAL, V_CDALMOXARIFE, V_CDLOCALESTOQ);

            INSERT INTO RELAT_ANALMOVI_ANALI
               (CDTIPORETIAN, NMTIPORETIAN, IDENTRSAIDAN, CDTIPOOPERAN,
                IDSALDOESTAN, VRSALDOESTAN, QTSALDOESTAN, CDFILIAL, CDALMOXARIFE, CDLOCALESTOQ)
            VALUES
               ('-', '-','-', '-', 'SI', 0, 0, V_CDFILIAL, V_CDALMOXARIFE, V_CDLOCALESTOQ);

            INSERT INTO RELAT_ANALMOVI_ANALI
               (CDTIPORETIAN, NMTIPORETIAN, IDENTRSAIDAN, CDTIPOOPERAN,
                IDSALDOESTAN, VRSALDOESTAN, QTSALDOESTAN, CDFILIAL, CDALMOXARIFE, CDLOCALESTOQ)
            VALUES
               ('-', '-', '-', '-', 'SJ', 0, 0, V_CDFILIAL, V_CDALMOXARIFE, V_CDLOCALESTOQ);

            INSERT INTO RELAT_ANALMOVI_ANALI
               (CDTIPORETIAN, NMTIPORETIAN, IDENTRSAIDAN, CDTIPOOPERAN,
                IDSALDOESTAN, VRSALDOESTAN, QTSALDOESTAN, CDFILIAL, CDALMOXARIFE, CDLOCALESTOQ)
            (SELECT '-', '-', NVL(IDENTRSAIDME,'-'), NVL(CDTIPOOPERME,'-'),'SE', 0, 0, V_CDFILIAL, V_CDALMOXARIFE, V_CDLOCALESTOQ
               FROM RELAT_MOVIEST_ANALI
              WHERE IDTIPOMOVIEST = '2'
                AND CDFILIMOVIEST = V_CDFILIAL
                AND CDALMOXARIRM  = V_CDALMOXARIFE
                AND CDLOCALESTRM  = V_CDLOCALESTOQ
                AND DTLANCMOVIEST BETWEEN P_DTINICIAL AND P_DTFINAL
              GROUP BY CDFILIMOVIEST,  NVL(IDENTRSAIDME,'-'), NVL(CDTIPOOPERME,'-'));

              INSERT INTO RELAT_ANALMOVI_ANALI
               (CDTIPORETIAN, NMTIPORETIAN, IDENTRSAIDAN, CDTIPOOPERAN,
                IDSALDOESTAN, VRSALDOESTAN, QTSALDOESTAN, CDFILIAL, CDALMOXARIFE, CDLOCALESTOQ)
               VALUES
               ('-', '-', '-', '-', 'SP', 0, 0, V_CDFILIAL, V_CDALMOXARIFE, V_CDLOCALESTOQ);

            INSERT INTO RELAT_ANALMOVI_ANALI
               (CDTIPORETIAN, NMTIPORETIAN, IDENTRSAIDAN, CDTIPOOPERAN,
                IDSALDOESTAN, VRSALDOESTAN, QTSALDOESTAN, CDFILIAL, CDALMOXARIFE, CDLOCALESTOQ)
            (SELECT NVL(CDTIPORETIME,'-'), NVL(NMTIPORETIME,'-'),
                    '-', '-', 'SR', 0, 0, V_CDFILIAL, V_CDALMOXARIFE, V_CDLOCALESTOQ
              FROM RELAT_MOVIEST_ANALI
              WHERE IDTIPOMOVIEST = '3'  --Retiradas de estoque
                AND CDFILIMOVIEST = V_CDFILIAL
                AND CDALMOXARIRM  = V_CDALMOXARIFE
                AND CDLOCALESTRM  = V_CDLOCALESTOQ
                AND DTLANCMOVIEST BETWEEN P_DTINICIAL AND P_DTFINAL
              GROUP BY CDFILIMOVIEST, NVL(CDTIPORETIME,'-'), NVL(NMTIPORETIME,'-'));


            INSERT INTO RELAT_ANALMOVI_ANALI
               (CDTIPORETIAN, NMTIPORETIAN, IDENTRSAIDAN, CDTIPOOPERAN,
                IDSALDOESTAN, VRSALDOESTAN, QTSALDOESTAN, CDFILIAL, CDALMOXARIFE, CDLOCALESTOQ)
            (SELECT '-', '-', NVL(IDENTRSAIDME,'-'), NVL(CDTIPOOPERME,'-'), 'SS', 0, 0, V_CDFILIAL, V_CDALMOXARIFE, V_CDLOCALESTOQ
              FROM RELAT_MOVIEST_ANALI
              WHERE (IDTIPOMOVIEST = '4' OR  IDTIPOMOVIEST = '5')  -- 4>>Saidas de estoque - 5>>Devolucao ao fornecedor
                AND CDFILIMOVIEST = V_CDFILIAL
                AND CDALMOXARIRM  = V_CDALMOXARIFE
                AND CDLOCALESTRM  = V_CDLOCALESTOQ
                AND DTLANCMOVIEST BETWEEN P_DTINICIAL AND P_DTFINAL
              GROUP BY CDFILIMOVIEST, NVL(IDENTRSAIDME,'-'), NVL(CDTIPOOPERME,'-'));

            INSERT INTO RELAT_ANALMOVI_ANALI
               (CDTIPORETIAN, NMTIPORETIAN,  IDENTRSAIDAN, CDTIPOOPERAN,
                IDSALDOESTAN, VRSALDOESTAN, QTSALDOESTAN, CDFILIAL, CDALMOXARIFE, CDLOCALESTOQ)
            VALUES
               ('-', '-', '-', '-', 'ST',0, 0, V_CDFILIAL, V_CDALMOXARIFE, V_CDLOCALESTOQ);

            INSERT INTO RELAT_ANALMOVI_ANALI
               (CDTIPORETIAN, NMTIPORETIAN, IDENTRSAIDAN, CDTIPOOPERAN,
                IDSALDOESTAN, VRSALDOESTAN, QTSALDOESTAN, CDFILIAL, CDALMOXARIFE, CDLOCALESTOQ)
            VALUES
               ('-', '-', '-', '-', 'SL', 0, 0, V_CDFILIAL, V_CDALMOXARIFE, V_CDLOCALESTOQ);

            INSERT INTO RELAT_ANALMOVI_ANALI
               (CDTIPORETIAN, NMTIPORETIAN, IDENTRSAIDAN, CDTIPOOPERAN,
                IDSALDOESTAN, VRSALDOESTAN, QTSALDOESTAN, CDFILIAL, CDALMOXARIFE, CDLOCALESTOQ)
            VALUES
               ('-', '-', '-', '-', 'SG', 0, 0, V_CDFILIAL, V_CDALMOXARIFE, V_CDLOCALESTOQ);

         END IF;

         IF (C_TOTALMOVI.IDTIPOMOVIEST = '-') AND (C_TOTALMOVI.VRCUSTTOTAL <> 0)  THEN
            UPDATE RELAT_TOTAMOVI_ANALI
            SET VRSOMPINI    = VRSOMPINI + C_TOTALMOVI.VRCUSTTOTAL,
                VRSOMSATU    = VRSOMSATU + C_TOTALMOVI.VRCUSTTOTAL,
                VRQTDEPINIME = VRQTDEPINIME + V_VRQTDETOTAL,
                VRQTDESATUME = VRQTDESATUME + V_VRQTDETOTAL,
                SGUNIDTOTAME = V_SGUNIDADE
            WHERE CDPRODTOTAMOVI = C_TOTALMOVI.CDTOTMOVIEST AND
                  CDFILIAL       = V_CDFILIAL AND
                  CDALMOXARIFE   = V_CDALMOXARIFE AND
                  CDLOCALESTOQ   = V_CDLOCALESTOQ;
         END IF;
         IF (C_TOTALMOVI.IDTIPOMOVIEST = '0') THEN --Implantacao de estoque
            UPDATE RELAT_TOTAMOVI_ANALI
            SET VRSOMINVE    = VRSOMINVE + C_TOTALMOVI.VRCUSTTOTAL,
                VRSOMSATU    = VRSOMSATU + C_TOTALMOVI.VRCUSTTOTAL,
                VRQTDEINVEME = VRQTDEINVEME + V_VRQTDETOTAL,
                VRQTDESATUME = VRQTDESATUME + V_VRQTDETOTAL,
                SGUNIDTOTAME = V_SGUNIDADE
            WHERE CDPRODTOTAMOVI = C_TOTALMOVI.CDTOTMOVIEST AND
                  CDFILIAL       = V_CDFILIAL AND
                  CDALMOXARIFE   = V_CDALMOXARIFE AND
                  CDLOCALESTOQ   = V_CDLOCALESTOQ;
         END IF;

         IF (C_TOTALMOVI.IDTIPOMOVIEST = '1') AND (C_TOTALMOVI.VRCUSTTOTAL <> 0) THEN -- IDTIPOMOVIEST = '1' >> Ajuste de inventario
            UPDATE RELAT_TOTAMOVI_ANALI
            SET VRSOMAINV    = VRSOMAINV + C_TOTALMOVI.VRCUSTTOTAL,
                VRSOMSATU    = VRSOMSATU + C_TOTALMOVI.VRCUSTTOTAL + C_TOTALMOVI.VRDIFCUSSAID,
                VRQTDEAINVME = VRQTDEAINVME + V_VRQTDETOTAL,
                VRQTDESATUME = VRQTDESATUME + V_VRQTDETOTAL,
                SGUNIDTOTAME = V_SGUNIDADE,
                VRSOMDIFSAID = VRSOMDIFSAID + C_TOTALMOVI.VRDIFCUSSAID
            WHERE CDPRODTOTAMOVI = C_TOTALMOVI.CDTOTMOVIEST AND
                  CDFILIAL       = V_CDFILIAL AND
                  CDALMOXARIFE   = V_CDALMOXARIFE AND
                  CDLOCALESTOQ   = V_CDLOCALESTOQ;
         END IF;

         IF (C_TOTALMOVI.IDTIPOMOVIEST = '2') THEN --IDTIPOMOVIEST = '2' >> Entradas no estoque
            UPDATE RELAT_TOTAMOVI_ANALI
            SET VRSOMENTR    = VRSOMENTR + C_TOTALMOVI.VRCUSTTOTAL,
                VRSOMSATU    = VRSOMSATU + C_TOTALMOVI.VRCUSTTOTAL + C_TOTALMOVI.VRCUSTLOGLME+C_TOTALMOVI.VRCUSTSEGLME + C_TOTALMOVI.VRDIFCUSSAID,
                VRQTDEENTRME = VRQTDEENTRME + V_VRQTDETOTAL,
                VRQTDESATUME = VRQTDESATUME + V_VRQTDETOTAL,
                SGUNIDTOTAME = V_SGUNIDADE,
                VRSOMCUSTLOG = VRSOMCUSTLOG + C_TOTALMOVI.VRCUSTLOGLME,
                VRSOMCUSTSEG = VRSOMCUSTSEG + C_TOTALMOVI.VRCUSTSEGLME,
                VRSOMDIFSAID = VRSOMDIFSAID + C_TOTALMOVI.VRDIFCUSSAID
            WHERE CDPRODTOTAMOVI = C_TOTALMOVI.CDTOTMOVIEST AND
                  CDFILIAL       = V_CDFILIAL AND
                  CDALMOXARIFE   = V_CDALMOXARIFE AND
                  CDLOCALESTOQ   = V_CDLOCALESTOQ;
         END IF;

         IF (C_TOTALMOVI.IDTIPOMOVIEST = '3') AND (C_TOTALMOVI.VRCUSTTOTAL <> 0) THEN --IDTIPOMOVIEST = '3' >> Retiradas de estoque
            UPDATE RELAT_TOTAMOVI_ANALI
            SET VRSOMRETI    = VRSOMRETI + C_TOTALMOVI.VRCUSTTOTAL,
                VRSOMSATU    = (VRSOMSATU - C_TOTALMOVI.VRCUSTTOTAL)+ C_TOTALMOVI.VRDIFCUSSAID,
                VRQTDERETIME = VRQTDERETIME + V_VRQTDETOTAL,
                VRQTDESATUME = VRQTDESATUME - V_VRQTDETOTAL,
                SGUNIDTOTAME = V_SGUNIDADE,
                VRSOMDIFSAID = VRSOMDIFSAID + C_TOTALMOVI.VRDIFCUSSAID
            WHERE CDPRODTOTAMOVI = C_TOTALMOVI.CDTOTMOVIEST AND
                  CDFILIAL       = V_CDFILIAL AND
                  CDALMOXARIFE   = V_CDALMOXARIFE AND
                  CDLOCALESTOQ   = V_CDLOCALESTOQ;

         END IF;

         IF (C_TOTALMOVI.IDTIPOMOVIEST = '4') AND (C_TOTALMOVI.VRCUSTTOTAL <> 0) THEN --IDTIPOMOVIEST = '4' >> Saidas de estoque
            UPDATE RELAT_TOTAMOVI_ANALI
            SET VRSOMSAID    = VRSOMSAID + C_TOTALMOVI.VRCUSTTOTAL,
                VRSOMSATU    = (VRSOMSATU - C_TOTALMOVI.VRCUSTTOTAL)+C_TOTALMOVI.VRCUSTLOGLME+
                                C_TOTALMOVI.VRCUSTSEGLME + C_TOTALMOVI.VRDIFCUSSAID,
                VRSOMCUSTLOG = VRSOMCUSTLOG + C_TOTALMOVI.VRCUSTLOGLME,
                VRQTDESAIDME = VRQTDESAIDME + V_VRQTDETOTAL,
                VRQTDESATUME = VRQTDESATUME - V_VRQTDETOTAL,
                SGUNIDTOTAME = V_SGUNIDADE,
                VRSOMDIFSAID = VRSOMDIFSAID + C_TOTALMOVI.VRDIFCUSSAID
            WHERE CDPRODTOTAMOVI = C_TOTALMOVI.CDTOTMOVIEST AND
                  CDFILIAL       = V_CDFILIAL AND
                  CDALMOXARIFE   = V_CDALMOXARIFE AND
                  CDLOCALESTOQ   = V_CDLOCALESTOQ;
         END IF;

         IF (C_TOTALMOVI.IDTIPOMOVIEST = '5') AND (C_TOTALMOVI.VRCUSTTOTAL <> 0) THEN --IDTIPOMOVIEST = '5' >> Devolucao ao fornecedor
            UPDATE RELAT_TOTAMOVI_ANALI
            SET VRSOMDFOR    = VRSOMDFOR + C_TOTALMOVI.VRCUSTTOTAL,
                VRSOMSATU    = VRSOMSATU - C_TOTALMOVI.VRCUSTTOTAL,
                VRQTDEDFORME = VRQTDEDFORME + V_VRQTDETOTAL,
                VRQTDESATUME = VRQTDESATUME - V_VRQTDETOTAL,
                SGUNIDTOTAME = V_SGUNIDADE,
                VRSOMDIFSAID = NVL(VRSOMDIFSAID,0)
            WHERE CDPRODTOTAMOVI = C_TOTALMOVI.CDTOTMOVIEST AND
                  CDFILIAL       = V_CDFILIAL AND
                  CDALMOXARIFE   = V_CDALMOXARIFE AND
                  CDLOCALESTOQ   = V_CDLOCALESTOQ;
         END IF;

         --- Inserir total moviventacao (Dados Analiticos) ---

         V_VRSOMSATUAN  := 0;
         V_VRQTDESATUAN := 0;

         -- Saldo Anterior

         IF (C_TOTALMOVI.IDTIPOMOVIEST = '-') AND (C_TOTALMOVI.VRCUSTTOTAL <> 0) THEN
            UPDATE RELAT_ANALMOVI_ANALI
            SET VRSALDOESTAN     = VRSALDOESTAN + C_TOTALMOVI.VRCUSTTOTAL,
                QTSALDOESTAN     = QTSALDOESTAN + V_VRQTDETOTAL,
                IDSALDOESTAN     = 'SA'
            WHERE CDFILIAL     = V_CDFILIAL    AND
                  CDALMOXARIFE = V_CDALMOXARIFE AND
                  CDLOCALESTOQ = V_CDLOCALESTOQ AND
                  IDSALDOESTAN = 'SA';

             V_VRSOMSATUAN    := V_VRSOMSATUAN   + C_TOTALMOVI.VRCUSTTOTAL ;
             V_VRQTDESATUAN   := V_VRQTDESATUAN + V_VRQTDETOTAL;

         END IF;

         -- Saldo Implantacao

         IF (C_TOTALMOVI.IDTIPOMOVIEST = '0') THEN -- IDTIPOMOVIEST = '0' >> Implantacao de estoque
            UPDATE RELAT_ANALMOVI_ANALI
            SET VRSALDOESTAN     = VRSALDOESTAN + C_TOTALMOVI.VRCUSTTOTAL,
                QTSALDOESTAN     = QTSALDOESTAN + V_VRQTDETOTAL,
                IDSALDOESTAN     = 'SI'
            WHERE CDFILIAL     = V_CDFILIAL    AND
                  CDALMOXARIFE = V_CDALMOXARIFE AND
                  CDLOCALESTOQ = V_CDLOCALESTOQ AND
                  IDSALDOESTAN = 'SI';

             V_VRSOMSATUAN    := V_VRSOMSATUAN  + C_TOTALMOVI.VRCUSTTOTAL;
             V_VRQTDESATUAN   := V_VRQTDESATUAN + V_VRQTDETOTAL;
         END IF;

         -- Saldo Ajuste

         IF (C_TOTALMOVI.IDTIPOMOVIEST = '1') AND (C_TOTALMOVI.VRCUSTTOTAL <> 0) THEN --IDTIPOMOVIEST = '1' >> Ajuste de inventario
            UPDATE RELAT_ANALMOVI_ANALI
            SET VRSALDOESTAN     = VRSALDOESTAN + C_TOTALMOVI.VRCUSTTOTAL,
                QTSALDOESTAN     = QTSALDOESTAN + V_VRQTDETOTAL,
                IDSALDOESTAN     = 'SJ'
            WHERE CDFILIAL     = V_CDFILIAL    AND
                  CDALMOXARIFE = V_CDALMOXARIFE AND
                  CDLOCALESTOQ = V_CDLOCALESTOQ AND
                  IDSALDOESTAN = 'SJ';

            UPDATE RELAT_ANALMOVI_ANALI
               SET VRSALDOESTAN   = VRSALDOESTAN + C_TOTALMOVI.VRDIFCUSSAID
             WHERE CDFILIAL     = V_CDFILIAL AND
                   CDALMOXARIFE = V_CDALMOXARIFE AND
                   CDLOCALESTOQ = V_CDLOCALESTOQ AND
                   IDSALDOESTAN = 'SP';

             V_VRSOMSATUAN    := V_VRSOMSATUAN  + C_TOTALMOVI.VRCUSTTOTAL+ C_TOTALMOVI.VRDIFCUSSAID;
             V_VRQTDESATUAN   := V_VRQTDESATUAN + V_VRQTDETOTAL;
         END IF;

         -- Saldo Entrada

         IF (C_TOTALMOVI.IDTIPOMOVIEST = '2') THEN -- IDTIPOMOVIEST = '2' >> Entradas no estoque
            UPDATE RELAT_ANALMOVI_ANALI
            SET VRSALDOESTAN     = VRSALDOESTAN + C_TOTALMOVI.VRCUSTTOTAL,
                QTSALDOESTAN     = QTSALDOESTAN + V_VRQTDETOTAL,
                IDSALDOESTAN     = 'SE'
            WHERE CDFILIAL       = V_CDFILIAL               AND
                  CDALMOXARIFE   = V_CDALMOXARIFE AND
                  CDLOCALESTOQ   = V_CDLOCALESTOQ AND
                  IDSALDOESTAN   = 'SE'                     AND
                  IDENTRSAIDAN   = C_TOTALMOVI.IDENTRSAIDME AND
                  CDTIPOOPERAN   = C_TOTALMOVI.CDTIPOOPERME;

             UPDATE RELAT_ANALMOVI_ANALI
             SET VRSALDOESTAN  = VRSALDOESTAN + C_TOTALMOVI.VRDIFCUSSAID
             WHERE CDFILIAL    = V_CDFILIAL AND
                  CDALMOXARIFE = V_CDALMOXARIFE AND
                  CDLOCALESTOQ = V_CDLOCALESTOQ AND
                  IDSALDOESTAN = 'SP';

             V_VRSOMSATUAN    := V_VRSOMSATUAN  + C_TOTALMOVI.VRCUSTTOTAL + C_TOTALMOVI.VRCUSTLOGLME + C_TOTALMOVI.VRCUSTSEGLME + C_TOTALMOVI.VRDIFCUSSAID;
             V_VRQTDESATUAN   := V_VRQTDESATUAN + V_VRQTDETOTAL;
         END IF;

         -- Saldo Retirada

         IF (C_TOTALMOVI.IDTIPOMOVIEST = '3') AND (C_TOTALMOVI.VRCUSTTOTAL <> 0) THEN --IDTIPOMOVIEST = '3'Retiradas de estoque
            UPDATE RELAT_ANALMOVI_ANALI
            SET VRSALDOESTAN     = VRSALDOESTAN + C_TOTALMOVI.VRCUSTTOTAL,
                QTSALDOESTAN     = QTSALDOESTAN + V_VRQTDETOTAL,
                IDSALDOESTAN     = 'SR'
            WHERE CDFILIAL       = V_CDFILIAL               AND
                  CDALMOXARIFE   = V_CDALMOXARIFE AND
                  CDLOCALESTOQ   = V_CDLOCALESTOQ AND
                  IDSALDOESTAN   = 'SR'                     AND
                  CDTIPORETIAN   = C_TOTALMOVI.CDTIPORETIME;

            UPDATE RELAT_ANALMOVI_ANALI
            SET VRSALDOESTAN   = VRSALDOESTAN + C_TOTALMOVI.VRDIFCUSSAID
            WHERE CDFILIAL     = V_CDFILIAL AND
                  CDALMOXARIFE = V_CDALMOXARIFE AND
                  CDLOCALESTOQ = V_CDLOCALESTOQ AND
                  IDSALDOESTAN = 'SP';

            V_VRSOMSATUAN    := V_VRSOMSATUAN  - C_TOTALMOVI.VRCUSTTOTAL + C_TOTALMOVI.VRDIFCUSSAID;
            V_VRQTDESATUAN   := V_VRQTDESATUAN - V_VRQTDETOTAL;
         END IF;

         -- Saldo Saida

         IF (C_TOTALMOVI.IDTIPOMOVIEST = '4' OR C_TOTALMOVI.IDTIPOMOVIEST = '5') AND (C_TOTALMOVI.VRCUSTTOTAL <> 0) THEN --IDTIPOMOVIEST = '4' >> Saidas de estoque // IDTIPOMOVIEST = '5' >> Devolucao ao fornecedor
            UPDATE RELAT_ANALMOVI_ANALI
            SET VRSALDOESTAN     = VRSALDOESTAN + C_TOTALMOVI.VRCUSTTOTAL,
                QTSALDOESTAN     = QTSALDOESTAN + V_VRQTDETOTAL,
                IDSALDOESTAN     = 'SS'
            WHERE CDFILIAL       = V_CDFILIAL               AND
                  CDALMOXARIFE   = V_CDALMOXARIFE AND
                  CDLOCALESTOQ   = V_CDLOCALESTOQ AND
                  IDSALDOESTAN   = 'SS'                     AND
                  IDENTRSAIDAN   = C_TOTALMOVI.IDENTRSAIDME AND
                  CDTIPOOPERAN   = C_TOTALMOVI.CDTIPOOPERME;
            IF C_TOTALMOVI.IDTIPOMOVIEST = '4' THEN --IDTIPOMOVIEST = '4' >> Saidas de estoque
               UPDATE RELAT_ANALMOVI_ANALI
               SET VRSALDOESTAN   = VRSALDOESTAN + C_TOTALMOVI.VRDIFCUSSAID
               WHERE CDFILIAL     = V_CDFILIAL AND
                     CDALMOXARIFE = V_CDALMOXARIFE AND
                     CDLOCALESTOQ = V_CDLOCALESTOQ AND
                     IDSALDOESTAN = 'SP';

                V_VRSOMSATUAN    := V_VRSOMSATUAN  - C_TOTALMOVI.VRCUSTTOTAL + C_TOTALMOVI.VRDIFCUSSAID;
            ELSE
               UPDATE RELAT_ANALMOVI_ANALI
               SET VRSALDOESTAN   = VRSALDOESTAN
               WHERE CDFILIAL     = V_CDFILIAL AND
                     CDALMOXARIFE = V_CDALMOXARIFE AND
                     CDLOCALESTOQ = V_CDLOCALESTOQ AND
                     IDSALDOESTAN = 'SP';

                V_VRSOMSATUAN    := V_VRSOMSATUAN  - C_TOTALMOVI.VRCUSTTOTAL;
            END IF;
            V_VRQTDESATUAN   := V_VRQTDESATUAN - V_VRQTDETOTAL;
         END IF;

         -- Saldo Atual

         UPDATE RELAT_ANALMOVI_ANALI
         SET VRSALDOESTAN   = VRSALDOESTAN + V_VRSOMSATUAN,
             QTSALDOESTAN   = QTSALDOESTAN + V_VRQTDESATUAN,
             IDSALDOESTAN   = 'ST'
         WHERE CDFILIAL     = V_CDFILIAL AND
               CDALMOXARIFE = V_CDALMOXARIFE AND
               CDLOCALESTOQ = V_CDLOCALESTOQ AND
               IDSALDOESTAN = 'ST';

         -- Custo de Logistica
         IF ((C_TOTALMOVI.IDTIPOMOVIEST = '-') AND (C_TOTALMOVI.VRCUSTTOTAL = 0)) OR
            (C_TOTALMOVI.IDTIPOMOVIEST <> '-') THEN
            UPDATE RELAT_ANALMOVI_ANALI
            SET VRSALDOESTAN   = VRSALDOESTAN + C_TOTALMOVI.VRCUSTLOGLME
            WHERE CDFILIAL     = V_CDFILIAL AND
                  CDALMOXARIFE = V_CDALMOXARIFE AND
                  CDLOCALESTOQ = V_CDLOCALESTOQ AND
                  IDSALDOESTAN = 'SL';
            -- Custo de Seguro
            UPDATE RELAT_ANALMOVI_ANALI
            SET VRSALDOESTAN   = VRSALDOESTAN + C_TOTALMOVI.VRCUSTSEGLME
            WHERE CDFILIAL     = V_CDFILIAL AND
                  CDALMOXARIFE = V_CDALMOXARIFE AND
                  CDLOCALESTOQ = V_CDLOCALESTOQ AND
                  IDSALDOESTAN = 'SG';
         END IF;
      END LOOP;

      /*Quando o saldo de um produto e zerado por NF's de devolucao ocorre um divergencia na evolucao do custo,
       segue abaixo tratamento para igualar a totalizacao do saldo em estoque qdo houver NF's de devolucao com
       custo diferente do custo medio do dia.
      */
      FOR C_DEVOLUCAO IN(SELECT CDPRODTOTAMOVI
                         FROM RELAT_TOTAMOVI_ANALI
                         WHERE CDFILIAL     = V_CDFILIAL
                           AND CDALMOXARIFE = V_CDALMOXARIFE
                           AND CDLOCALESTOQ = V_CDLOCALESTOQ) LOOP
         FOR C_DIFERENCA IN (SELECT CDPRODMOVIEST, DTLANCMOVIEST
                             FROM RELAT_MOVIEST_ANALI
                             WHERE QTLANCAMENTO   <> 0                         AND
                                   IDTIPOMOVIEST  = '5'                        AND --Devolucao ao fornecedor
                                   CDTOTMOVIEST   = C_DEVOLUCAO.CDPRODTOTAMOVI AND
                                   QTDEMOVIEST    = 0                          AND
                                   CDFILIMOVIEST  = V_CDFILIAL                 AND
                                   CDALMOXARIRM   = V_CDALMOXARIFE             AND
                                   CDLOCALESTRM   = V_CDLOCALESTOQ             AND
                                   DTLANCMOVIEST BETWEEN P_DTINICIAL AND P_DTFINAL
                             GROUP BY CDPRODMOVIEST, DTLANCMOVIEST) LOOP
            SELECT SUM(DECODE(VRDIFCUSTEME,0,0,(VRCUSTMEDI*QTLANCAMENTO)-(VRDIFCUSTEME*QTLANCAMENTO)))  AS VRDIFCUSSAID
            INTO  V_VRDIFCUSSAID
            FROM RELAT_MOVIEST_ANALI
            WHERE QTLANCAMENTO   <> 0                         AND
                  IDTIPOMOVIEST  = '5'                        AND ----Devolucao ao fornecedor
                  CDTOTMOVIEST   = C_DEVOLUCAO.CDPRODTOTAMOVI AND
                  DTLANCMOVIEST  = C_DIFERENCA.DTLANCMOVIEST  AND
                  CDFILIMOVIEST  = V_CDFILIAL                 AND
                  CDALMOXARIRM   = V_CDALMOXARIFE             AND
                  CDLOCALESTRM   = V_CDLOCALESTOQ             AND
                  DTLANCMOVIEST BETWEEN P_DTINICIAL AND P_DTFINAL AND
                  CDPRODMOVIEST  = C_DIFERENCA.CDPRODMOVIEST;

            UPDATE RELAT_TOTAMOVI_ANALI
            SET VRSOMSATU    = VRSOMSATU + V_VRDIFCUSSAID,
                VRSOMDIFSAID = NVL(VRSOMDIFSAID,0) + V_VRDIFCUSSAID
            WHERE CDPRODTOTAMOVI = C_DEVOLUCAO.CDPRODTOTAMOVI AND
                  CDFILIAL       = V_CDFILIAL AND
                  CDALMOXARIFE   = V_CDALMOXARIFE AND
                  CDLOCALESTOQ   = V_CDLOCALESTOQ;

            UPDATE RELAT_ANALMOVI_ANALI
            SET VRSALDOESTAN   = VRSALDOESTAN + V_VRDIFCUSSAID
            WHERE CDFILIAL     = V_CDFILIAL AND
                  CDALMOXARIFE = V_CDALMOXARIFE AND
                  CDLOCALESTOQ = V_CDLOCALESTOQ AND
                  IDSALDOESTAN = 'SP';

         UPDATE RELAT_ANALMOVI_ANALI
         SET VRSALDOESTAN   = VRSALDOESTAN + V_VRDIFCUSSAID
         WHERE CDFILIAL     = V_CDFILIAL AND
               CDALMOXARIFE = V_CDALMOXARIFE AND
               CDLOCALESTOQ = V_CDLOCALESTOQ AND
               IDSALDOESTAN = 'ST';
         END LOOP;
      END LOOP;

      INSERT INTO RELAT_TOTAMOVI_ANALI
         (CDFILIAL, CDALMOXARIFE, CDLOCALESTOQ, IDTIPOTOTAMOVI, CDPRODTOTAMOVI, VRSOMPINI, VRSOMAINV, VRSOMINVE,
          VRSOMENTR, VRSOMRETI, VRSOMSAID, VRSOMDFOR, VRSOMSATU,
          VRSOMCUSTLOG, VRSOMCUSTSEG)
         (SELECT V_CDFILIAL, V_CDALMOXARIFE, V_CDLOCALESTOQ, 'T', 'TOTALGERAL', SUM(VRSOMPINI),
                 SUM(VRSOMAINV), SUM(VRSOMINVE), SUM(VRSOMENTR),
                 SUM(VRSOMRETI), SUM(VRSOMSAID), SUM(VRSOMDFOR),
                 SUM(VRSOMSATU),
                 SUM(VRSOMCUSTLOG),  SUM(VRSOMCUSTSEG)
          FROM RELAT_TOTAMOVI_ANALI
          WHERE CDFILIAL     = V_CDFILIAL AND
                CDALMOXARIFE = V_CDALMOXARIFE AND
                CDLOCALESTOQ = V_CDLOCALESTOQ);

      COMMIT;
   END LOOP;
   <<PROCEDURE_END>>
   COMMIT;
END;
/