/*

@C:\WEB_635\SQL\END_CONV\ORACLE\4006_ESTSTPO_MOVIMENTACAO_ESTOQUE.SQL
raise_application_error(-20501, '4' , True);

*/
-- ============================================================
--   Procedure para geracao da Movimentacao de Estoque
-- ============================================================

CREATE OR REPLACE PROCEDURE MOVIMENTACAO_ESTOQUE(
   P_FILIAL       IN FILIAL.CDFILIAL%TYPE,
   P_CDALMOXARIFE IN POSESTDIA.CDALMOXARIFE%TYPE,
   P_CDLOCALESTOQ IN POSESTDIA.CDLOCALESTOQ%TYPE,
   P_NRLOTEESTQ   IN POSESTDIA.NRLOTEESTQ%TYPE,
   P_TAMNIVEL     IN INTEGER,
   P_DTINICIAL    IN DATE,
   P_DTFINAL      IN DATE,
   P_PRODINI      IN PRODUTO.CDARVPROD%TYPE,
   P_PRODFIM      IN PRODUTO.CDARVPROD%TYPE,
   P_IDLANCTO     IN INTEGER,
   P_IDCUSTO      IN INTEGER,
   P_SEQUMOVI     IN RELAT_MOVIEST.NRRELATMOVI%TYPE,
   P_MOVCONSOLID  IN VARCHAR DEFAULT 'N',
   P_CUSTOALMOX   IN VARCHAR DEFAULT 'N',
   P_DELMOVIMEN   IN VARCHAR DEFAULT 'S')

IS
   V_CDALMOXLCEST VARCHAR2(12);
   V_CDPRODUTO    VARCHAR2(20);
   V_NRLOTEESTQ   VARCHAR2(20);
   V_NRSUBLOTE    VARCHAR2(04);
   V_INSERT       VARCHAR2(01);
   V_CDPRODESTO   VARCHAR2(10);
   V_CDARVPROD    VARCHAR2(20);
   V_UNIDADE      VARCHAR2(10);
   V_NMPRODUTO    VARCHAR2(50);
   V_NVPROD       VARCHAR2(20);

   V_SGUNIDADE    VARCHAR2(10);
   V_COUNT        NUMBER;
   V_QTATUAL      NUMBER;
   V_CUSTMEES     NUMBER;
   V_VRATUAL      NUMBER;
   V_QTMOVI       NUMBER;
   V_CUSTMED      NUMBER;
   V_CUSTANT      NUMBER;
   V_CUSTZERO     NUMBER;
   V_QTDEANT      NUMBER;
   V_QTMOVANT     NUMBER;
   V_VRMOVANT     NUMBER;
   V_VRMOVSEGANT  NUMBER;
   V_VRMOVLOGANT  NUMBER;
   V_VRQTDETOTAL  NUMBER;
   V_VRSOMSATUAN  NUMBER;
   V_VRQTDESATUAN NUMBER;

   V_QTDEAJUSTE   NUMBER;
   V_VLRESTOAJU   NUMBER;
   V_VLRBRUTAJU   NUMBER;
   V_VRQTANT      NUMBER;
   V_VRESTANT     NUMBER;
   V_DIFCUSTSAI   NUMBER;
   V_ULTCUSTENT   NUMBER;
   V_VRANTEST     NUMBER;
   V_VRCUSTLOGANT NUMBER;
   V_VRCUSTSEGANT NUMBER;
   V_QTPOSINI     NUMBER;

   V_TIPCUSTEST   NUMBER;
   V_CUSTESTOQ    NUMBER;
   V_DATAPROC     DATE;
   V_TAMARV       INTEGER;
   V_VRCUSTLOGEME NUMBER;
   V_VRCUSTSEGEME NUMBER;
   V_VRPESOBRUTME NUMBER;
   V_VRATUEST     NUMBER;
   w_N            CHAR(1) := 'N';
   w_S            CHAR(1) := 'S';
   V_VRDIFCUSSAID NUMBER;


   V_CDFILISUBGR     VARCHAR2(04);
   V_CDGRUPPRMOVEST  VARCHAR2(10);

   V_CUSTTOTENTR   NUMBER;
   V_CUSTOMEDIO    NUMBER;
   V_CUSTOANTCONS  NUMBER;

   V_CDALMOXARIFE  VARCHAR2(06);
   V_CDLOCALESTOQ  VARCHAR2(06);

   V_COMANDO      VARCHAR2(4000);
   V_TIPO         VARCHAR2(6) := 'BINARY';

   CURSOR C_POSESTDIA_GRUP IS
      SELECT A.CDFILIAL,
             A.CDPRODUTO,
             NVL(P_NRLOTEESTQ, 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,
             P.CDSUBGRPROD AS NVPROD
        FROM POSESTDIA A,
             (SELECT X.CDPRODUTO, X.CDSUBGRPROD, B.VRPESOBRUTPR
                FROM (SELECT CDPRODUTO, VRPESOBRUTPR
                       FROM PRODUTO
                      WHERE CDARVPROD BETWEEN P_PRODINI AND P_PRODFIM
                        AND CDNVPRODUTO = (SELECT MAX(CDNVPRODUTO)
                                             FROM PRODUTO))B, PRSUBGRPROD X
               WHERE X.CDFILISUBGR = V_CDFILISUBGR
                 AND X.CDGRUPPROD = V_CDGRUPPRMOVEST
                 AND X.CDPRODUTO = B.CDPRODUTO) P
       WHERE (A.CDFILIAL = P_FILIAL)
         AND (A.CDALMOXARIFE = V_CDALMOXARIFE)
         AND (A.CDLOCALESTOQ = V_CDLOCALESTOQ)
         AND (A.CDPRODUTO = P.CDPRODUTO)
         AND ((A.NRLOTEESTQ = P_NRLOTEESTQ) OR (P_NRLOTEESTQ IS NULL))
         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_POSESTCONS_GRUP IS
      SELECT A.CDFILIAL,
             P.CDARVPROD AS CDPRODUTO,
             ' ' AS NRLOTEESTQ,
             ' ' AS NRSUBLOTE,
             A.DTPOSESTCON AS DTLANCAMEN,
             '-' AS IDTIPOLANC,
             '----' AS CDFILLANC,
             '----------' AS NRLANCAMEN,
             '0001' AS NRSEQULANC,
             A.QTESTDIACON AS QTDEESTO,
             DECODE(A.QTESTDIACON,0,0,DECODE(P_IDCUSTO,1,A.VRESTDIACON,A.VRESTBRUTCON)/A.QTESTDIACON) AS CUSTMEDI,
             DECODE(A.QTESTDIACON,0,VRESTANTCON,A.VRESTDIACON) AS CUSTESTO,
             A.QTESTDIACON AS QTLANCAMEN,
             DECODE(A.QTESTDIACON,0,VRBRUTANTCON,VRESTBRUTCON) AS CUSTBRUT,
             DECODE(A.QTESTDIACON,0,DECODE(QTESTANTCON,0,0,VRESTANTCON/QTESTANTCON),A.VRESTDIACON/A.QTESTDIACON) AS CUSTMEES,
             DECODE(A.QTESTDIACON,0,DECODE(QTESTANTCON,0,0,VRBRUTANTCON/QTESTANTCON),VRESTBRUTCON/A.QTESTDIACON) AS CUSTMEBR,
             ' ' AS IDALTCMEDMV,
             0.0 AS QTLANCTOEST,
             0.0 AS VRLANCTOEST,
             0.0 AS VRLANCTOBRUT,
             '-' AS IDORDEMCLC,
             P.CDSUBGRPROD AS NVPROD,
             P.NMPRODUTO,
             P.SGUNIDADE,
             SUBSTR(P_SEQUMOVI,1,10) AS NRRELATMOVI,
             0.0 AS VRUNILANCTO,
             0.0 AS VRTOTLANCTO,
             0.0 AS VRTOTALANTE,
             0.0 AS QTTOTALANTE,
             ' ' AS CDALMOXARIFE,
             ' ' AS CDLOCALESTOQ,
             0.0 AS VRCUSTENTRME,
             '--' AS CDTIPORETIME,
             '------------------------------' AS NMTIPORETIME,
             '-' AS IDENTRSAIDME,
             '--' AS CDTIPOOPERME,
             NVL(VRCLOGCON,0) AS VRCUSTLOGLME,
             NVL(VRSEGUCON,0) AS VRCUSTSEGLME,
             NVL(VRPESOBRUTPR,0) AS VRPESOBRUTME,
             DECODE(A.QTESTDIACON,0,VRCLOGCONANT,A.VRCLOGCON) AS CUSTLOGESTO,
             DECODE(A.QTESTDIACON,0, VRSEGUCONANT,A.VRSEGUCON) AS CUSTSEGESTO
        FROM POSESTCONS A,
             (SELECT X.CDPRODUTO, X.CDSUBGRPROD, B.VRPESOBRUTPR, B.CDARVPROD,
                     B.NMPRODUTO, B.SGUNIDADE
                FROM (SELECT CDARVPROD, CDPRODUTO, NMPRODUTO, SGUNIDADE, VRPESOBRUTPR
                        FROM PRODUTO
                       WHERE CDARVPROD BETWEEN P_PRODINI AND P_PRODFIM
                         AND CDNVPRODUTO = (SELECT MAX(CDNVPRODUTO)
                                              FROM PRODUTO)) B, PRSUBGRPROD X
               WHERE X.CDFILISUBGR = V_CDFILISUBGR
                 AND X.CDGRUPPROD = V_CDGRUPPRMOVEST
                 AND X.CDPRODUTO = B.CDPRODUTO) P
       WHERE (A.CDFILIAL = P_FILIAL)
         AND (A.CDPRODUTO = P.CDPRODUTO)
         AND (A.DTPOSESTCON = (SELECT MAX(P.DTPOSESTCON)
                                 FROM POSESTCONS P
                                WHERE P.CDFILIAL    = A.CDFILIAL
                                  AND P.CDPRODUTO   = A.CDPRODUTO
                                  AND P.DTPOSESTCON <= P_DTINICIAL));

   CURSOR C_POSESTALMOX_GRUP IS
      SELECT A.CDFILIAL,
             P.CDARVPROD AS CDPRODUTO,
             DECODE(NVL(P_MOVCONSOLID,w_N), w_S, ' ', NVL(P_NRLOTEESTQ, A.NRLOTEESTQ)) AS NRLOTEESTQ,
             DECODE(NVL(P_MOVCONSOLID,w_N), w_S, ' ', A.NRSUBLOTE) AS NRSUBLOTE,
             DECODE(NVL(P_MOVCONSOLID,w_N), w_S,P_DTINICIAL,A.DTPOSIESTQ) AS DTLANCAMEN,
             '-' AS IDTIPOLANC,
             '----' AS CDFILLANC,
             '----------' AS NRLANCAMEN,
             '0001' AS NRSEQULANC,
             SUM(A.QTESTOQDIA) AS QTDEESTO,
             DECODE(SUM(A.QTESTOQDIA),0,0,DECODE(P_IDCUSTO,1,SUM(A.VRESTOQDIA),SUM(A.VRESTOQBRUT))/SUM(A.QTESTOQDIA)) AS CUSTMEDI,
             DECODE(SUM(A.QTESTOQDIA),0,SUM(VRESTOQANT),SUM(A.VRESTOQDIA)) AS CUSTESTO,
             SUM(A.QTESTOQDIA) AS QTLANCAMEN,
             DECODE(SUM(A.QTESTOQDIA),0,SUM(VRBRUTOANT),SUM(A.VRESTOQBRUT)) AS CUSTBRUT,
             DECODE(SUM(A.QTESTOQDIA),0,DECODE(SUM(QTESTOQANT),0,0,SUM(VRESTOQANT)/SUM(QTESTOQANT)),SUM(A.VRESTOQDIA)/SUM(A.QTESTOQDIA)) AS CUSTMEES,
             DECODE(SUM(A.QTESTOQDIA),0,DECODE(SUM(QTESTOQANT),0,0,SUM(VRBRUTOANT)/SUM(QTESTOQANT)),SUM(A.VRESTOQBRUT)/SUM(A.QTESTOQDIA)) AS CUSTMEBR,
             ' ' AS IDALTCMEDMV,
             0.0 AS QTLANCTOEST,
             0.0 AS VRLANCTOEST,
             0.0 AS VRLANCTOBRUT,
             '-' AS IDORDEMCLC,
             P.CDSUBGRPROD AS NVPROD,
             P.NMPRODUTO,
             P.SGUNIDADE,
             SUBSTR(P_SEQUMOVI,1,10) AS NRRELATMOVI,
             0.0 AS VRUNILANCTO,
             0.0 AS VRTOTLANCTO,
             0.0 AS VRTOTALANTE,
             0.0 AS QTTOTALANTE,
             DECODE(NVL(P_MOVCONSOLID,w_N), w_S, ' ', A.CDALMOXARIFE) AS CDALMOXARIFE,
             DECODE(NVL(P_MOVCONSOLID,w_N), w_S, ' ', A.CDLOCALESTOQ) AS CDLOCALESTOQ,
             0.0 AS VRCUSTENTRME,
             '--' AS CDTIPORETIME,
             '------------------------------' AS NMTIPORETIME,
             '-' AS IDENTRSAIDME,
             '--' AS CDTIPOOPERME,
             0 AS VRCUSTLOGLME,
             0 AS VRCUSTSEGLME,
             MAX(NVL(VRPESOBRUTPR,0)) AS VRPESOBRUTME,
             SUM(NVL(A.VRCLOGDIA,0)) AS CUSTLOGESTO,
             SUM(NVL(A.VRSEGUDIA,0)) AS CUSTSEGESTO
        FROM POSESTDIA A,
             (SELECT X.CDPRODUTO, X.CDSUBGRPROD, B.VRPESOBRUTPR, B.CDARVPROD,
                     B.NMPRODUTO, B.SGUNIDADE
                FROM (SELECT CDARVPROD, CDPRODUTO, NMPRODUTO, SGUNIDADE, VRPESOBRUTPR
                        FROM PRODUTO
                       WHERE CDARVPROD BETWEEN P_PRODINI AND P_PRODFIM
                         AND CDNVPRODUTO = (SELECT MAX(CDNVPRODUTO)
                                              FROM PRODUTO)) B, PRSUBGRPROD X
               WHERE X.CDFILISUBGR = V_CDFILISUBGR
                 AND X.CDGRUPPROD = V_CDGRUPPRMOVEST
                 AND X.CDPRODUTO = B.CDPRODUTO) P
       WHERE (A.CDFILIAL = P_FILIAL)
         AND (A.CDALMOXARIFE = V_CDALMOXARIFE)
         AND (A.CDLOCALESTOQ = V_CDLOCALESTOQ)
         AND (A.CDPRODUTO = P.CDPRODUTO)
         AND ((A.NRLOTEESTQ = P_NRLOTEESTQ) OR (P_NRLOTEESTQ IS NULL))
         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)
       GROUP BY A.CDFILIAL, P.CDARVPROD, P.CDSUBGRPROD,
                DECODE(NVL(P_MOVCONSOLID,w_N), w_S, ' ', NVL(P_NRLOTEESTQ, A.NRLOTEESTQ)),
                DECODE(NVL(P_MOVCONSOLID,w_N), w_S, ' ', A.NRSUBLOTE),
                DECODE(NVL(P_MOVCONSOLID,w_N), w_S, ' ', A.CDALMOXARIFE),
                DECODE(NVL(P_MOVCONSOLID,w_N), w_S, ' ', A.CDLOCALESTOQ),
                DECODE(NVL(P_MOVCONSOLID,w_N), w_S,P_DTINICIAL,A.DTPOSIESTQ),
                SUBSTR(P.CDARVPROD,1, 1), P.NMPRODUTO, P.SGUNIDADE, VRPESOBRUTPR;

    /* Cursor referente a posicao de estoque por almoxarifado/grupo consolidado sem localizacao */
   CURSOR C_POSESTALMOX_GRUP_SEM IS
      SELECT A.CDFILIAL,
             P.CDARVPROD AS CDPRODUTO,
             DECODE(NVL(P_MOVCONSOLID,w_N), w_S, ' ', NVL(P_NRLOTEESTQ, A.NRLOTEESTQ)) AS NRLOTEESTQ,
             DECODE(NVL(P_MOVCONSOLID,w_N), w_S, ' ', A.NRSUBLOTE) AS NRSUBLOTE,
             DECODE(NVL(P_MOVCONSOLID,w_N), w_S,P_DTINICIAL,A.DTPOSIESTQ) AS DTLANCAMEN,
             '-' AS IDTIPOLANC,
             '----' AS CDFILLANC,
             '----------' AS NRLANCAMEN,
             '0001' AS NRSEQULANC,
             SUM(A.QTESTOQDIA) AS QTDEESTO,
             DECODE(SUM(A.QTESTOQDIA),0,0,DECODE(P_IDCUSTO,1,SUM(A.VRESTOQDIA),SUM(A.VRESTOQBRUT))/SUM(A.QTESTOQDIA)) AS CUSTMEDI,
             DECODE(SUM(A.QTESTOQDIA),0,SUM(VRESTOQANT),SUM(A.VRESTOQDIA)) AS CUSTESTO,
             SUM(A.QTESTOQDIA) AS QTLANCAMEN,
             DECODE(SUM(A.QTESTOQDIA),0,SUM(VRBRUTOANT),SUM(A.VRESTOQBRUT)) AS CUSTBRUT,
             DECODE(SUM(A.QTESTOQDIA),0,DECODE(SUM(QTESTOQANT),0,0,SUM(VRESTOQANT)/SUM(QTESTOQANT)),SUM(A.VRESTOQDIA)/SUM(A.QTESTOQDIA)) AS CUSTMEES,
             DECODE(SUM(A.QTESTOQDIA),0,DECODE(SUM(QTESTOQANT),0,0,SUM(VRBRUTOANT)/SUM(QTESTOQANT)),SUM(A.VRESTOQBRUT)/SUM(A.QTESTOQDIA)) AS CUSTMEBR,
             ' ' AS IDALTCMEDMV,
             0.0 AS QTLANCTOEST,
             0.0 AS VRLANCTOEST,
             0.0 AS VRLANCTOBRUT,
             '-' AS IDORDEMCLC,
             P.CDSUBGRPROD AS NVPROD,
             P.NMPRODUTO,
             P.SGUNIDADE,
             SUBSTR(P_SEQUMOVI,1,10) AS NRRELATMOVI,
             0.0 AS VRUNILANCTO,
             0.0 AS VRTOTLANCTO,
             0.0 AS VRTOTALANTE,
             0.0 AS QTTOTALANTE,
             DECODE(NVL(P_MOVCONSOLID,w_N), w_S, ' ', A.CDALMOXARIFE) AS CDALMOXARIFE,
             DECODE(NVL(P_MOVCONSOLID,w_N), w_S, ' ', A.CDLOCALESTOQ) AS CDLOCALESTOQ,
             0.0 AS VRCUSTENTRME,
             '--' AS CDTIPORETIME,
             '------------------------------' AS NMTIPORETIME,
             '-' AS IDENTRSAIDME,
             '--' AS CDTIPOOPERME,
             0 AS VRCUSTLOGLME,
             0 AS VRCUSTSEGLME,
             MAX(NVL(VRPESOBRUTPR,0)) AS VRPESOBRUTME,
             SUM(NVL(A.VRCLOGDIA,0)) AS CUSTLOGESTO,
             SUM(NVL(A.VRSEGUDIA,0)) AS CUSTSEGESTO
        FROM POSESTDIA A,
             (SELECT X.CDPRODUTO, X.CDSUBGRPROD, B.VRPESOBRUTPR, B.CDARVPROD,
                     B.NMPRODUTO, B.SGUNIDADE
                FROM (SELECT CDARVPROD, CDPRODUTO, NMPRODUTO, SGUNIDADE, VRPESOBRUTPR
                        FROM PRODUTO
                       WHERE CDARVPROD BETWEEN P_PRODINI AND P_PRODFIM
                         AND CDNVPRODUTO = (SELECT MAX(CDNVPRODUTO)
                                              FROM PRODUTO)) B, PRSUBGRPROD X
               WHERE X.CDFILISUBGR = V_CDFILISUBGR
                 AND X.CDGRUPPROD = V_CDGRUPPRMOVEST
                 AND X.CDPRODUTO = B.CDPRODUTO) P
       WHERE (A.CDFILIAL = P_FILIAL)
         AND (A.CDALMOXARIFE = V_CDALMOXARIFE)
         AND ((A.CDLOCALESTOQ = P_CDLOCALESTOQ) OR (P_CDLOCALESTOQ IS NULL))
         AND (A.CDPRODUTO = P.CDPRODUTO)
         AND ((A.NRLOTEESTQ = P_NRLOTEESTQ) OR (P_NRLOTEESTQ IS NULL))
         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)
       GROUP BY A.CDFILIAL, P.CDARVPROD, P.CDSUBGRPROD,
                DECODE(NVL(P_MOVCONSOLID,w_N), w_S, ' ', NVL(P_NRLOTEESTQ, A.NRLOTEESTQ)),
                DECODE(NVL(P_MOVCONSOLID,w_N), w_S, ' ', A.NRSUBLOTE),
                DECODE(NVL(P_MOVCONSOLID,w_N), w_S, ' ', A.CDALMOXARIFE),
                DECODE(NVL(P_MOVCONSOLID,w_N), w_S, ' ', A.CDLOCALESTOQ),
                DECODE(NVL(P_MOVCONSOLID,w_N), w_S,P_DTINICIAL,A.DTPOSIESTQ),
                SUBSTR(P.CDARVPROD,1, 1), P.NMPRODUTO, P.SGUNIDADE, VRPESOBRUTPR;

   CURSOR C_ITLANCTOEST_GRUP IS
      SELECT D.CDFILIMOVI AS CDFILIAL,
             E.CDARVPROD 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.CDSUBGRPROD AS NVPROD,
             E.NMPRODUTO,
             E.SGUNIDADE,
             SUBSTR(P_SEQUMOVI,1,10) AS NRRELATMOVI,
             NVL(D.VRUNILANCTO,0) AS VRUNILANCTO,
             NVL(D.VRTOTLANCTO,0) AS VRTOTLANCTO,
             0.0 AS VRTOTALANTE,
             0.0 AS QTTOTALANTE,
             D.CDALMOXARIFE,
             D.CDLOCALESTOQ,
             (D.VRLANCTOEST * D.QTLANCTOEST) AS VRCUSTENTRME,
             NVL(D.CDTIPORETI  ,'--') AS CDTIPORETIME,
             NVL(T.NMTIPORETI  ,'-') AS NMTIPORETIME,
             NVL(D.IDENTRSAIDOP,'-') AS IDENTRSAIDME,
             NVL(L.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
        FROM PRODFILI C,
             (SELECT X.CDPRODUTO, X.CDSUBGRPROD, B.VRPESOBRUTPR, B.CDARVPROD,
                     B.NMPRODUTO, B.SGUNIDADE
                FROM (SELECT CDARVPROD, CDPRODUTO, NMPRODUTO, SGUNIDADE, VRPESOBRUTPR
                        FROM PRODUTO
                       WHERE CDARVPROD BETWEEN P_PRODINI AND P_PRODFIM
                         AND CDNVPRODUTO = (SELECT MAX(CDNVPRODUTO)
                                              FROM PRODUTO)) B, PRSUBGRPROD X
               WHERE X.CDFILISUBGR = V_CDFILISUBGR
                 AND X.CDGRUPPROD = V_CDGRUPPRMOVEST
                 AND X.CDPRODUTO = B.CDPRODUTO) E,
             ITLANCTOEST D, TIPORETI T, LANCTOESTOQ L
       WHERE (D.CDFILIMOVI = P_FILIAL)
         AND (D.CDALMOXARIFE = V_CDALMOXARIFE)
         AND (D.CDLOCALESTOQ = V_CDLOCALESTOQ)
         AND ((D.NRLOTEESTQ = P_NRLOTEESTQ) OR (P_NRLOTEESTQ IS NULL))
         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(+))
         AND (D.CDFILIAL = L.CDFILIAL)
         AND (D.NRLANCESTQ = L.NRLANCESTQ);

   CURSOR C_IMPLANTACAO_GRUP IS
      SELECT D.CDFILIMOVI AS CDFILIAL,
             E.CDARVPROD 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.CDSUBGRPROD AS NVPROD,
             E.NMPRODUTO,
             E.SGUNIDADE,
             SUBSTR(P_SEQUMOVI,1,10) AS NRRELATMOVI,
             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
        FROM PRODFILI C,
             (SELECT X.CDPRODUTO, X.CDSUBGRPROD, B.CDARVPROD, B.NMPRODUTO, B.SGUNIDADE, B.VRPESOBRUTPR
                FROM (SELECT CDPRODUTO, CDARVPROD, NMPRODUTO, SGUNIDADE, VRPESOBRUTPR
                       FROM PRODUTO
                      WHERE CDARVPROD BETWEEN P_PRODINI AND P_PRODFIM
                        AND CDNVPRODUTO = (SELECT MAX(CDNVPRODUTO)
                                             FROM PRODUTO))B, PRSUBGRPROD X
               WHERE X.CDFILISUBGR = V_CDFILISUBGR
                 AND X.CDGRUPPROD = V_CDGRUPPRMOVEST
                 AND X.CDPRODUTO = B.CDPRODUTO) E,
             ITLANCTOEST D, TIPORETI T
       WHERE (D.CDFILIMOVI = P_FILIAL)
         AND (D.CDALMOXARIFE = V_CDALMOXARIFE)
         AND (D.CDLOCALESTOQ = V_CDLOCALESTOQ)
         AND ((D.NRLOTEESTQ = P_NRLOTEESTQ) OR (P_NRLOTEESTQ IS NULL))
         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, E.CDSUBGRPROD, 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;

   /* Cursor referente a movimentacao por grupo consolidado sem almoxarifado e/ou localizacao */
   CURSOR C_ITLANCTOEST_GRUP_SEM IS
      SELECT D.CDFILIMOVI AS CDFILIAL,
             E.CDARVPROD 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.CDSUBGRPROD AS NVPROD,
             E.NMPRODUTO,
             E.SGUNIDADE,
             SUBSTR(P_SEQUMOVI,1,10) AS NRRELATMOVI,
             NVL(D.VRUNILANCTO,0) AS VRUNILANCTO,
             NVL(D.VRTOTLANCTO,0) AS VRTOTLANCTO,
             0.0 AS VRTOTALANTE,
             0.0 AS QTTOTALANTE,
             D.CDALMOXARIFE,
             D.CDLOCALESTOQ,
             (D.VRLANCTOEST * D.QTLANCTOEST) AS VRCUSTENTRME,
             NVL(D.CDTIPORETI  ,'--') AS CDTIPORETIME,
             NVL(T.NMTIPORETI  ,'-') AS NMTIPORETIME,
             NVL(D.IDENTRSAIDOP,'-') AS IDENTRSAIDME,
             NVL(L.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
        FROM PRODFILI C,
             (SELECT X.CDPRODUTO, X.CDSUBGRPROD, B.VRPESOBRUTPR, B.CDARVPROD,
                     B.NMPRODUTO, B.SGUNIDADE
                FROM (SELECT CDARVPROD, CDPRODUTO, NMPRODUTO, SGUNIDADE, VRPESOBRUTPR
                        FROM PRODUTO
                       WHERE CDARVPROD BETWEEN P_PRODINI AND P_PRODFIM
                         AND CDNVPRODUTO = (SELECT MAX(CDNVPRODUTO)
                                              FROM PRODUTO)) B, PRSUBGRPROD X
               WHERE X.CDFILISUBGR = V_CDFILISUBGR
                 AND X.CDGRUPPROD = V_CDGRUPPRMOVEST
                 AND X.CDPRODUTO = B.CDPRODUTO) E,
             ITLANCTOEST D, TIPORETI T, LANCTOESTOQ L
       WHERE (D.CDFILIMOVI = P_FILIAL)
         AND ((D.CDALMOXARIFE = P_CDALMOXARIFE) OR (P_CDALMOXARIFE IS NULL))
         AND ((D.CDLOCALESTOQ = P_CDLOCALESTOQ) OR (P_CDLOCALESTOQ IS NULL))
         AND ((D.NRLOTEESTQ = P_NRLOTEESTQ) OR (P_NRLOTEESTQ IS NULL))
         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(+))
         AND (D.CDFILIAL = L.CDFILIAL)
         AND (D.NRLANCESTQ = L.NRLANCESTQ);

   /* Cursor referente a implantacao por grupo consolidado sem almoxarifado e/ou localizacao */
   CURSOR C_IMPLANTACAO_GRUP_SEM IS
      SELECT D.CDFILIMOVI AS CDFILIAL,
             E.CDARVPROD 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.CDSUBGRPROD AS NVPROD,
             E.NMPRODUTO,
             E.SGUNIDADE,
             SUBSTR(P_SEQUMOVI,1,10) AS NRRELATMOVI,
             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
        FROM PRODFILI C,
             (SELECT X.CDPRODUTO, X.CDSUBGRPROD, B.CDARVPROD, B.NMPRODUTO, B.SGUNIDADE, B.VRPESOBRUTPR
                FROM (SELECT CDPRODUTO, CDARVPROD, NMPRODUTO, SGUNIDADE, VRPESOBRUTPR
                       FROM PRODUTO
                      WHERE CDARVPROD BETWEEN P_PRODINI AND P_PRODFIM
                        AND CDNVPRODUTO = (SELECT MAX(CDNVPRODUTO)
                                             FROM PRODUTO))B, PRSUBGRPROD X
               WHERE X.CDFILISUBGR = V_CDFILISUBGR
                 AND X.CDGRUPPROD = V_CDGRUPPRMOVEST
                 AND X.CDPRODUTO = B.CDPRODUTO) E,
             ITLANCTOEST D, TIPORETI T
       WHERE (D.CDFILIMOVI = P_FILIAL)
         AND ((D.CDALMOXARIFE = P_CDALMOXARIFE) OR (P_CDALMOXARIFE IS NULL))
         AND ((D.CDLOCALESTOQ = P_CDLOCALESTOQ) OR (P_CDLOCALESTOQ IS NULL))
         AND ((D.NRLOTEESTQ = P_NRLOTEESTQ) OR (P_NRLOTEESTQ IS NULL))
         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, E.CDSUBGRPROD, 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;


   CURSOR C_POSESTDIA IS
      SELECT A.CDFILIAL,
             A.CDPRODUTO,
             NVL(P_NRLOTEESTQ, 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
        FROM POSESTDIA A,
             (SELECT CDPRODUTO, VRPESOBRUTPR
                FROM PRODUTO
               WHERE CDARVPROD BETWEEN P_PRODINI AND P_PRODFIM
                 AND CDNVPRODUTO = (SELECT MAX(CDNVPRODUTO)
                                      FROM PRODUTO)) C
       WHERE (A.CDFILIAL = P_FILIAL)
         AND (A.CDALMOXARIFE = V_CDALMOXARIFE)
         AND (A.CDLOCALESTOQ = V_CDLOCALESTOQ)
         AND (A.CDPRODUTO = C.CDPRODUTO)
         AND ((A.NRLOTEESTQ = P_NRLOTEESTQ) OR (P_NRLOTEESTQ IS NULL))
         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_POSESTCONS IS
      SELECT A.CDFILIAL,
             B.CDARVPROD AS CDPRODUTO,
             ' ' AS NRLOTEESTQ,
             ' ' AS NRSUBLOTE,
             A.DTPOSESTCON AS DTLANCAMEN,
             '-' AS IDTIPOLANC,
             '----' AS CDFILLANC,
             '----------' AS NRLANCAMEN,
             '0001' AS NRSEQULANC,
             A.QTESTDIACON AS QTDEESTO,
             DECODE(A.QTESTDIACON,0,0,DECODE(P_IDCUSTO,1,A.VRESTDIACON,A.VRESTBRUTCON)/A.QTESTDIACON) AS CUSTMEDI,
             DECODE(A.QTESTDIACON,0,VRESTANTCON,A.VRESTDIACON) AS CUSTESTO,
             A.QTESTDIACON AS QTLANCAMEN,
             DECODE(A.QTESTDIACON,0,VRBRUTANTCON,VRESTBRUTCON) AS CUSTBRUT,
             DECODE(A.QTESTDIACON,0,DECODE(QTESTANTCON,0,0,VRESTANTCON/QTESTANTCON),A.VRESTDIACON/A.QTESTDIACON) AS CUSTMEES,
             DECODE(A.QTESTDIACON,0,DECODE(QTESTANTCON,0,0,VRBRUTANTCON/QTESTANTCON),VRESTBRUTCON/A.QTESTDIACON) AS CUSTMEBR,
             ' ' AS IDALTCMEDMV,
             0.0 AS QTLANCTOEST,
             0.0 AS VRLANCTOEST,
             0.0 AS VRLANCTOBRUT,
             '-' AS IDORDEMCLC,
             SUBSTR(B.CDARVPROD,1, P_TAMNIVEL) AS NVPROD,
             B.NMPRODUTO,
             B.SGUNIDADE,
             SUBSTR(P_SEQUMOVI,1,10) AS NRRELATMOVI,
             0.0 AS VRUNILANCTO,
             0.0 AS VRTOTLANCTO,
             0.0 AS VRTOTALANTE,
             0.0 AS QTTOTALANTE,
             ' ' AS CDALMOXARIFE,
             ' ' AS CDLOCALESTOQ,
             0.0 AS VRCUSTENTRME,
             '--' AS CDTIPORETIME,
             '------------------------------' AS NMTIPORETIME,
             '-' AS IDENTRSAIDME,
             '--' AS CDTIPOOPERME,
             NVL(VRCLOGCON,0) AS VRCUSTLOGLME,
             NVL(VRSEGUCON,0) AS VRCUSTSEGLME,
             NVL(VRPESOBRUTPR,0) AS VRPESOBRUTME,
             DECODE(A.QTESTDIACON,0,VRCLOGCONANT,A.VRCLOGCON) AS CUSTLOGESTO,
             DECODE(A.QTESTDIACON,0, VRSEGUCONANT,A.VRSEGUCON) AS CUSTSEGESTO
        FROM POSESTCONS A,
             (SELECT CDPRODUTO, CDARVPROD, NMPRODUTO, SGUNIDADE, VRPESOBRUTPR
                FROM PRODUTO
               WHERE CDARVPROD BETWEEN P_PRODINI AND P_PRODFIM
                 AND CDNVPRODUTO = (SELECT MAX(CDNVPRODUTO)
                                       FROM PRODUTO)) B
       WHERE (A.CDFILIAL = P_FILIAL)
         AND (A.CDPRODUTO = B.CDPRODUTO)
         AND (A.DTPOSESTCON = (SELECT MAX(P.DTPOSESTCON)
                                 FROM POSESTCONS P
                                WHERE P.CDFILIAL    = A.CDFILIAL
                                  AND P.CDPRODUTO   = A.CDPRODUTO
                                  AND P.DTPOSESTCON <= P_DTINICIAL));

   CURSOR C_POSESTALMOX IS
      SELECT A.CDFILIAL,
             B.CDARVPROD AS CDPRODUTO,
             DECODE(NVL(P_MOVCONSOLID,w_N), w_S, ' ', NVL(P_NRLOTEESTQ, A.NRLOTEESTQ)) AS NRLOTEESTQ,
             DECODE(NVL(P_MOVCONSOLID,w_N), w_S, ' ', A.NRSUBLOTE) AS NRSUBLOTE,
             DECODE(NVL(P_MOVCONSOLID,w_N), w_S,P_DTINICIAL,A.DTPOSIESTQ) AS DTLANCAMEN,
             '-' AS IDTIPOLANC,
             '----' AS CDFILLANC,
             '----------' AS NRLANCAMEN,
             '0001' AS NRSEQULANC,
             SUM(A.QTESTOQDIA) AS QTDEESTO,
             DECODE(SUM(A.QTESTOQDIA),0,0,DECODE(P_IDCUSTO,1,SUM(A.VRESTOQDIA),SUM(A.VRESTOQBRUT))/SUM(A.QTESTOQDIA)) AS CUSTMEDI,
             DECODE(SUM(A.QTESTOQDIA),0,SUM(VRESTOQANT),SUM(A.VRESTOQDIA)) AS CUSTESTO,
             SUM(A.QTESTOQDIA) AS QTLANCAMEN,
             DECODE(SUM(A.QTESTOQDIA),0,SUM(VRBRUTOANT),SUM(A.VRESTOQBRUT)) AS CUSTBRUT,
             DECODE(SUM(A.QTESTOQDIA),0,DECODE(SUM(QTESTOQANT),0,0,SUM(VRESTOQANT)/SUM(QTESTOQANT)),SUM(A.VRESTOQDIA)/SUM(A.QTESTOQDIA)) AS CUSTMEES,
             DECODE(SUM(A.QTESTOQDIA),0,DECODE(SUM(QTESTOQANT),0,0,SUM(VRBRUTOANT)/SUM(QTESTOQANT)),SUM(A.VRESTOQBRUT)/SUM(A.QTESTOQDIA)) AS CUSTMEBR,
             ' ' AS IDALTCMEDMV,
             0.0 AS QTLANCTOEST,
             0.0 AS VRLANCTOEST,
             0.0 AS VRLANCTOBRUT,
             '-' AS IDORDEMCLC,
             SUBSTR(B.CDARVPROD,1, P_TAMNIVEL) AS NVPROD,
             B.NMPRODUTO,
             B.SGUNIDADE,
             SUBSTR(P_SEQUMOVI,1,10) AS NRRELATMOVI,
             0.0 AS VRUNILANCTO,
             0.0 AS VRTOTLANCTO,
             0.0 AS VRTOTALANTE,
             0.0 AS QTTOTALANTE,
             DECODE(NVL(P_MOVCONSOLID,w_N), w_S, ' ', A.CDALMOXARIFE) AS CDALMOXARIFE,
             DECODE(NVL(P_MOVCONSOLID,w_N), w_S, ' ', A.CDLOCALESTOQ) AS CDLOCALESTOQ,
             0.0 AS VRCUSTENTRME,
             '--' AS CDTIPORETIME,
             '------------------------------' AS NMTIPORETIME,
             '-' AS IDENTRSAIDME,
             '--' AS CDTIPOOPERME,
             0 AS VRCUSTLOGLME,
             0 AS VRCUSTSEGLME,
             MAX(NVL(VRPESOBRUTPR,0)) AS VRPESOBRUTME,
             SUM(NVL(A.VRCLOGDIA,0)) AS CUSTLOGESTO,
             SUM(NVL(A.VRSEGUDIA,0)) AS CUSTSEGESTO
        FROM POSESTDIA A,
             (SELECT CDPRODUTO, CDARVPROD, NMPRODUTO, SGUNIDADE, VRPESOBRUTPR
                FROM PRODUTO
               WHERE CDARVPROD BETWEEN P_PRODINI AND P_PRODFIM
                 AND CDNVPRODUTO = (SELECT MAX(CDNVPRODUTO)
                                       FROM PRODUTO)) B
        WHERE (A.CDFILIAL = P_FILIAL)
         AND (A.CDALMOXARIFE = V_CDALMOXARIFE)
         AND (A.CDLOCALESTOQ = V_CDLOCALESTOQ)
         AND ((A.NRLOTEESTQ = P_NRLOTEESTQ) OR (P_NRLOTEESTQ IS NULL))
         AND (A.CDPRODUTO = B.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))
       GROUP BY A.CDFILIAL, B.CDARVPROD,
                DECODE(NVL(P_MOVCONSOLID,w_N), w_S, ' ', NVL(P_NRLOTEESTQ, A.NRLOTEESTQ)),
                DECODE(NVL(P_MOVCONSOLID,w_N), w_S, ' ', A.NRSUBLOTE),
                DECODE(NVL(P_MOVCONSOLID,w_N), w_S, ' ', A.CDALMOXARIFE),
                DECODE(NVL(P_MOVCONSOLID,w_N), w_S, ' ', A.CDLOCALESTOQ),
                DECODE(NVL(P_MOVCONSOLID,w_N), w_S,P_DTINICIAL,A.DTPOSIESTQ),
                SUBSTR(B.CDARVPROD,1, 1), B.NMPRODUTO, B.SGUNIDADE, VRPESOBRUTPR;

    /* Cursor referente a posicao de estoque por almoxarifado consolidado sem almoxarifado e/ou localizacao */
   CURSOR C_POSESTALMOX_SEM IS
      SELECT A.CDFILIAL,
             B.CDARVPROD AS CDPRODUTO,
             DECODE(NVL(P_MOVCONSOLID,w_N), w_S, ' ', NVL(P_NRLOTEESTQ, A.NRLOTEESTQ)) AS NRLOTEESTQ,
             DECODE(NVL(P_MOVCONSOLID,w_N), w_S, ' ', A.NRSUBLOTE) AS NRSUBLOTE,
             DECODE(NVL(P_MOVCONSOLID,w_N), w_S,P_DTINICIAL,A.DTPOSIESTQ) AS DTLANCAMEN,
             '-' AS IDTIPOLANC,
             '----' AS CDFILLANC,
             '----------' AS NRLANCAMEN,
             '0001' AS NRSEQULANC,
             SUM(A.QTESTOQDIA) AS QTDEESTO,
             DECODE(SUM(A.QTESTOQDIA),0,0,DECODE(P_IDCUSTO,1,SUM(A.VRESTOQDIA),SUM(A.VRESTOQBRUT))/SUM(A.QTESTOQDIA)) AS CUSTMEDI,
             DECODE(SUM(A.QTESTOQDIA),0,SUM(VRESTOQANT),SUM(A.VRESTOQDIA)) AS CUSTESTO,
             SUM(A.QTESTOQDIA) AS QTLANCAMEN,
             DECODE(SUM(A.QTESTOQDIA),0,SUM(VRBRUTOANT),SUM(A.VRESTOQBRUT)) AS CUSTBRUT,
             DECODE(SUM(A.QTESTOQDIA),0,DECODE(SUM(QTESTOQANT),0,0,SUM(VRESTOQANT)/SUM(QTESTOQANT)),SUM(A.VRESTOQDIA)/SUM(A.QTESTOQDIA)) AS CUSTMEES,
             DECODE(SUM(A.QTESTOQDIA),0,DECODE(SUM(QTESTOQANT),0,0,SUM(VRBRUTOANT)/SUM(QTESTOQANT)),SUM(A.VRESTOQBRUT)/SUM(A.QTESTOQDIA)) AS CUSTMEBR,
             ' ' AS IDALTCMEDMV,
             0.0 AS QTLANCTOEST,
             0.0 AS VRLANCTOEST,
             0.0 AS VRLANCTOBRUT,
             '-' AS IDORDEMCLC,
             SUBSTR(B.CDARVPROD,1, P_TAMNIVEL) AS NVPROD,
             B.NMPRODUTO,
             B.SGUNIDADE,
             SUBSTR(P_SEQUMOVI,1,10) AS NRRELATMOVI,
             0.0 AS VRUNILANCTO,
             0.0 AS VRTOTLANCTO,
             0.0 AS VRTOTALANTE,
             0.0 AS QTTOTALANTE,
             DECODE(NVL(P_MOVCONSOLID,w_N), w_S, ' ', A.CDALMOXARIFE) AS CDALMOXARIFE,
             DECODE(NVL(P_MOVCONSOLID,w_N), w_S, ' ', A.CDLOCALESTOQ) AS CDLOCALESTOQ,
             0.0 AS VRCUSTENTRME,
             '--' AS CDTIPORETIME,
             '------------------------------' AS NMTIPORETIME,
             '-' AS IDENTRSAIDME,
             '--' AS CDTIPOOPERME,
             0 AS VRCUSTLOGLME,
             0 AS VRCUSTSEGLME,
             MAX(NVL(VRPESOBRUTPR,0)) AS VRPESOBRUTME,
             SUM(NVL(A.VRCLOGDIA,0)) AS CUSTLOGESTO,
             SUM(NVL(A.VRSEGUDIA,0)) AS CUSTSEGESTO
        FROM POSESTDIA A,
             (SELECT CDPRODUTO, CDARVPROD, NMPRODUTO, SGUNIDADE, VRPESOBRUTPR
                FROM PRODUTO
               WHERE CDARVPROD BETWEEN P_PRODINI AND P_PRODFIM
                 AND CDNVPRODUTO = (SELECT MAX(CDNVPRODUTO)
                                       FROM PRODUTO)) B
        WHERE (A.CDFILIAL = P_FILIAL)
         AND (A.CDALMOXARIFE = V_CDALMOXARIFE)
         AND ((A.CDLOCALESTOQ = P_CDLOCALESTOQ) OR (P_CDLOCALESTOQ IS NULL))
         AND ((A.NRLOTEESTQ = P_NRLOTEESTQ) OR (P_NRLOTEESTQ IS NULL))
         AND (A.CDPRODUTO = B.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))
       GROUP BY A.CDFILIAL, B.CDARVPROD,
                DECODE(NVL(P_MOVCONSOLID,w_N), w_S, ' ', NVL(P_NRLOTEESTQ, A.NRLOTEESTQ)),
                DECODE(NVL(P_MOVCONSOLID,w_N), w_S, ' ', A.NRSUBLOTE),
                DECODE(NVL(P_MOVCONSOLID,w_N), w_S, ' ', A.CDALMOXARIFE),
                DECODE(NVL(P_MOVCONSOLID,w_N), w_S, ' ', A.CDLOCALESTOQ),
                DECODE(NVL(P_MOVCONSOLID,w_N), w_S,P_DTINICIAL,A.DTPOSIESTQ),
                SUBSTR(B.CDARVPROD,1, 1), B.NMPRODUTO, B.SGUNIDADE, VRPESOBRUTPR;

   CURSOR C_ITLANCTOEST IS
      SELECT D.CDFILIMOVI AS CDFILIAL,
             E.CDARVPROD 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,
             SUBSTR(E.CDARVPROD,1, P_TAMNIVEL) AS NVPROD,
             E.NMPRODUTO,
             E.SGUNIDADE,
             SUBSTR(P_SEQUMOVI,1,10) AS NRRELATMOVI,
             NVL(D.VRUNILANCTO,0) AS VRUNILANCTO,
             NVL(D.VRTOTLANCTO,0) AS VRTOTLANCTO,
             0.0 AS VRTOTALANTE,
             0.0 AS QTTOTALANTE,
             D.CDALMOXARIFE,
             D.CDLOCALESTOQ,
             (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
        FROM PRODFILI C,
             (SELECT CDPRODUTO, CDARVPROD, NMPRODUTO, SGUNIDADE, VRPESOBRUTPR
                FROM PRODUTO
               WHERE CDARVPROD BETWEEN P_PRODINI AND P_PRODFIM
                 AND CDNVPRODUTO = (SELECT MAX(CDNVPRODUTO)
                                      FROM PRODUTO)) E,
             ITLANCTOEST D, TIPORETI T
       WHERE (D.CDFILIMOVI = P_FILIAL)
         AND (D.CDALMOXARIFE = V_CDALMOXARIFE)
         AND (D.CDLOCALESTOQ = V_CDLOCALESTOQ)
         AND ((D.NRLOTEESTQ = P_NRLOTEESTQ) OR (P_NRLOTEESTQ IS NULL))
         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 referente as movimentacoes de estoque consolidada sem almoxarifado e/ou localizacao */
   CURSOR C_ITLANCTOEST_SEM IS
      SELECT D.CDFILIMOVI AS CDFILIAL,
             E.CDARVPROD 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,
             SUBSTR(E.CDARVPROD,1, P_TAMNIVEL) AS NVPROD,
             E.NMPRODUTO,
             E.SGUNIDADE,
             SUBSTR(P_SEQUMOVI,1,10) AS NRRELATMOVI,
             NVL(D.VRUNILANCTO,0) AS VRUNILANCTO,
             NVL(D.VRTOTLANCTO,0) AS VRTOTLANCTO,
             0.0 AS VRTOTALANTE,
             0.0 AS QTTOTALANTE,
             D.CDALMOXARIFE,
             D.CDLOCALESTOQ,
             (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
        FROM PRODFILI C,
             (SELECT CDPRODUTO, CDARVPROD, NMPRODUTO, SGUNIDADE, VRPESOBRUTPR
                FROM PRODUTO
               WHERE CDARVPROD BETWEEN P_PRODINI AND P_PRODFIM
                 AND CDNVPRODUTO = (SELECT MAX(CDNVPRODUTO)
                                      FROM PRODUTO)) E,
             ITLANCTOEST D, TIPORETI T
       WHERE (D.CDFILIMOVI = P_FILIAL)
         AND ((D.CDALMOXARIFE = P_CDALMOXARIFE) OR (P_CDALMOXARIFE IS NULL))
         AND ((D.CDLOCALESTOQ = P_CDLOCALESTOQ) OR (P_CDLOCALESTOQ IS NULL))
         AND ((D.NRLOTEESTQ = P_NRLOTEESTQ) OR (P_NRLOTEESTQ IS NULL))
         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,
             E.CDARVPROD 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,
             SUBSTR(E.CDARVPROD,1, P_TAMNIVEL) AS NVPROD,
             E.NMPRODUTO,
             E.SGUNIDADE,
             SUBSTR(P_SEQUMOVI,1,10) AS NRRELATMOVI,
             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
        FROM PRODFILI C, ITLANCTOEST D, TIPORETI T,
             (SELECT CDPRODUTO, CDARVPROD, NMPRODUTO, SGUNIDADE, VRPESOBRUTPR
                FROM PRODUTO
               WHERE CDARVPROD BETWEEN P_PRODINI AND P_PRODFIM
                 AND CDNVPRODUTO = (SELECT MAX(CDNVPRODUTO)
                                      FROM PRODUTO)) E
       WHERE (D.CDFILIMOVI = P_FILIAL)
         AND (D.CDALMOXARIFE = V_CDALMOXARIFE)
         AND (D.CDLOCALESTOQ = V_CDLOCALESTOQ)
         AND ((D.NRLOTEESTQ = P_NRLOTEESTQ) OR (P_NRLOTEESTQ IS NULL))
         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;

   /* Cursor referente a implantacao consolidado sem almoxarifado e/ou localizacao */
   CURSOR C_IMPLANTACAO_SEM IS
      SELECT D.CDFILIMOVI AS CDFILIAL,
             E.CDARVPROD 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,
             SUBSTR(E.CDARVPROD,1, P_TAMNIVEL) AS NVPROD,
             E.NMPRODUTO,
             E.SGUNIDADE,
             SUBSTR(P_SEQUMOVI,1,10) AS NRRELATMOVI,
             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
        FROM PRODFILI C, ITLANCTOEST D, TIPORETI T,
             (SELECT CDPRODUTO, CDARVPROD, NMPRODUTO, SGUNIDADE, VRPESOBRUTPR
                FROM PRODUTO
               WHERE CDARVPROD BETWEEN P_PRODINI AND P_PRODFIM
                 AND CDNVPRODUTO = (SELECT MAX(CDNVPRODUTO)
                                      FROM PRODUTO)) E
       WHERE (D.CDFILIMOVI = P_FILIAL)
         AND ((D.CDALMOXARIFE = P_CDALMOXARIFE) OR (P_CDALMOXARIFE IS NULL))
         AND ((D.CDLOCALESTOQ = P_CDLOCALESTOQ) OR (P_CDLOCALESTOQ IS NULL))
         AND ((D.NRLOTEESTQ = P_NRLOTEESTQ) OR (P_NRLOTEESTQ IS NULL))
         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;

BEGIN
   V_COMANDO := NULL;
   V_COMANDO := 'ALTER SESSION SET NLS_SORT = '||CHR(39)||V_TIPO||CHR(39);
   EXECUTE IMMEDIATE V_COMANDO;

   IF P_CDALMOXARIFE IS NULL THEN
      V_CDALMOXARIFE := ' ';
   ELSE
      V_CDALMOXARIFE := P_CDALMOXARIFE;
   END IF;

   IF P_CDLOCALESTOQ IS NULL THEN
      V_CDLOCALESTOQ := ' ';
   ELSE
      V_CDLOCALESTOQ := P_CDLOCALESTOQ;
   END IF;

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

/*Verifica se filial tem sub-grupo especifico*/
   FOR C_FILGRUPO IN(SELECT DISTINCT CDFILISUBGR
                       FROM PRSUBGRPROD
                      WHERE CDGRUPPROD = V_CDGRUPPRMOVEST
                      ORDER BY CDFILISUBGR ASC)LOOP

          IF C_FILGRUPO.CDFILISUBGR = P_FILIAL THEN
             V_CDFILISUBGR := P_FILIAL;
             EXIT;
          ELSE
             V_CDFILISUBGR := 'P';
          END IF;
   END LOOP;

---------------------------------------------------------------------------------
-- DELETA OS REGISTROS DAS TABELA TEMPORARIAS                                  --
---------------------------------------------------------------------------------
   IF (NVL(P_DELMOVIMEN, W_s) = w_S) THEN
      DELETE FROM RELAT_MOVIEST
      WHERE NRRELATMOVI = P_SEQUMOVI;

      DELETE FROM RELAT_DESCMOVI
      WHERE NRRELATDESC = P_SEQUMOVI;

      DELETE FROM RELAT_TOTAMOVI
      WHERE NRRELATTOTA = P_SEQUMOVI;

      DELETE FROM RELAT_ANALMOVI
      WHERE NRRELATANAL = P_SEQUMOVI;
   END IF;

---------------------------------------------------------------------------------
-- INSERE PRODUTOS NA TABELA RELAT_MOVIEST DA POSIESTDIA E ITLANCTOEST         --
---------------------------------------------------------------------------------
   IF NVL(P_MOVCONSOLID,w_N) = w_N THEN
      IF V_CDGRUPPRMOVEST IS NULL THEN
         FOR C_MOVIEST IN C_POSESTDIA LOOP
            BEGIN
               SELECT P.CDARVPROD, P.SGUNIDADE, P.NMPRODUTO,
                      SUBSTR(P.CDARVPROD,1, P_TAMNIVEL)
               INTO V_CDARVPROD, V_UNIDADE, V_NMPRODUTO, V_NVPROD
               FROM  PRODUTO  P
               WHERE P.CDPRODUTO = C_MOVIEST.CDPRODUTO;
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
                  V_CDARVPROD := NULL; V_UNIDADE := NULL; V_NMPRODUTO := NULL; V_NVPROD := NULL;
            END;

            GRAVA_RELAT_MOVIEST(C_MOVIEST.CDFILIAL,     V_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.CUSTMEDI,     C_MOVIEST.CUSTESTO,     C_MOVIEST.QTLANCAMEN,   C_MOVIEST.CUSTBRUT,     C_MOVIEST.CUSTMEES,
                                C_MOVIEST.CUSTMEBR,     C_MOVIEST.IDALTCMEDMV,  C_MOVIEST.QTLANCTOEST,  C_MOVIEST.VRLANCTOEST,  C_MOVIEST.VRLANCTOBRUT,
                                C_MOVIEST.IDORDEMCLC,   V_NVPROD,       V_NMPRODUTO,            V_UNIDADE,              P_SEQUMOVI,
                                C_MOVIEST.VRUNILANCTO,  C_MOVIEST.VRTOTLANCTO,  C_MOVIEST.VRTOTALANTE,  C_MOVIEST.QTTOTALANTE,  C_MOVIEST.CDALMOXARIFE,
                                C_MOVIEST.CDLOCALESTOQ, C_MOVIEST.VRCUSTENTRME, C_MOVIEST.CDTIPORETIME, C_MOVIEST.NMTIPORETIME, C_MOVIEST.IDENTRSAIDME,
                                C_MOVIEST.CDTIPOOPERME, C_MOVIEST.VRCUSTLOGLME, C_MOVIEST.VRCUSTSEGLME, C_MOVIEST.CUSTLOGESTO,  C_MOVIEST.CUSTSEGESTO,
                                C_MOVIEST.VRPESOBRUTME);

         END LOOP;
      ELSE
         FOR C_MOVIEST IN C_POSESTDIA_GRUP LOOP
            BEGIN
               SELECT P.CDARVPROD, P.SGUNIDADE, P.NMPRODUTO
               INTO V_CDARVPROD, V_UNIDADE, V_NMPRODUTO
               FROM  PRODUTO  P
               WHERE P.CDPRODUTO = C_MOVIEST.CDPRODUTO;
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
                  V_CDARVPROD := NULL; V_UNIDADE := NULL; V_NMPRODUTO := NULL; V_NVPROD := NULL;
            END;

            GRAVA_RELAT_MOVIEST(C_MOVIEST.CDFILIAL,     V_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.CUSTMEDI,     C_MOVIEST.CUSTESTO,     C_MOVIEST.QTLANCAMEN,   C_MOVIEST.CUSTBRUT,     C_MOVIEST.CUSTMEES,
                                C_MOVIEST.CUSTMEBR,     C_MOVIEST.IDALTCMEDMV,  C_MOVIEST.QTLANCTOEST,  C_MOVIEST.VRLANCTOEST,  C_MOVIEST.VRLANCTOBRUT,
                                C_MOVIEST.IDORDEMCLC,   C_MOVIEST.NVPROD,       V_NMPRODUTO,            V_UNIDADE,              P_SEQUMOVI,
                                C_MOVIEST.VRUNILANCTO,  C_MOVIEST.VRTOTLANCTO,  C_MOVIEST.VRTOTALANTE,  C_MOVIEST.QTTOTALANTE,  C_MOVIEST.CDALMOXARIFE,
                                C_MOVIEST.CDLOCALESTOQ, C_MOVIEST.VRCUSTENTRME, C_MOVIEST.CDTIPORETIME, C_MOVIEST.NMTIPORETIME, C_MOVIEST.IDENTRSAIDME,
                                C_MOVIEST.CDTIPOOPERME, C_MOVIEST.VRCUSTLOGLME, C_MOVIEST.VRCUSTSEGLME, C_MOVIEST.CUSTLOGESTO,  C_MOVIEST.CUSTSEGESTO,
                                C_MOVIEST.VRPESOBRUTME);

         END LOOP;
      END IF;
   ELSE
      IF P_CDALMOXARIFE IS NULL THEN
         IF V_CDGRUPPRMOVEST IS NULL THEN
            FOR C_MOVIEST IN C_POSESTCONS LOOP
               GRAVA_RELAT_MOVIEST(C_MOVIEST.CDFILIAL,     C_MOVIEST.CDPRODUTO,    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.CUSTMEDI,     C_MOVIEST.CUSTESTO,     C_MOVIEST.QTLANCAMEN,   C_MOVIEST.CUSTBRUT,     C_MOVIEST.CUSTMEES,
                                   C_MOVIEST.CUSTMEBR,     C_MOVIEST.IDALTCMEDMV,  C_MOVIEST.QTLANCTOEST,  C_MOVIEST.VRLANCTOEST,  C_MOVIEST.VRLANCTOBRUT,
                                   C_MOVIEST.IDORDEMCLC,   C_MOVIEST.NVPROD,       C_MOVIEST.NMPRODUTO,    C_MOVIEST.SGUNIDADE,    P_SEQUMOVI,
                                   C_MOVIEST.VRUNILANCTO,  C_MOVIEST.VRTOTLANCTO,  C_MOVIEST.VRTOTALANTE,  C_MOVIEST.QTTOTALANTE,  C_MOVIEST.CDALMOXARIFE,
                                   C_MOVIEST.CDLOCALESTOQ, C_MOVIEST.VRCUSTENTRME, C_MOVIEST.CDTIPORETIME, C_MOVIEST.NMTIPORETIME, C_MOVIEST.IDENTRSAIDME,
                                   C_MOVIEST.CDTIPOOPERME, C_MOVIEST.VRCUSTLOGLME, C_MOVIEST.VRCUSTSEGLME, C_MOVIEST.CUSTLOGESTO, C_MOVIEST.CUSTSEGESTO,
                                   C_MOVIEST.VRPESOBRUTME);
            END LOOP;
         ELSE
            FOR C_MOVIEST IN C_POSESTCONS_GRUP LOOP
               GRAVA_RELAT_MOVIEST(C_MOVIEST.CDFILIAL,     C_MOVIEST.CDPRODUTO,    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.CUSTMEDI,     C_MOVIEST.CUSTESTO,     C_MOVIEST.QTLANCAMEN,   C_MOVIEST.CUSTBRUT,     C_MOVIEST.CUSTMEES,
                                   C_MOVIEST.CUSTMEBR,     C_MOVIEST.IDALTCMEDMV,  C_MOVIEST.QTLANCTOEST,  C_MOVIEST.VRLANCTOEST,  C_MOVIEST.VRLANCTOBRUT,
                                   C_MOVIEST.IDORDEMCLC,   C_MOVIEST.NVPROD,       C_MOVIEST.NMPRODUTO,    C_MOVIEST.SGUNIDADE,    P_SEQUMOVI,
                                   C_MOVIEST.VRUNILANCTO,  C_MOVIEST.VRTOTLANCTO,  C_MOVIEST.VRTOTALANTE,  C_MOVIEST.QTTOTALANTE,  C_MOVIEST.CDALMOXARIFE,
                                   C_MOVIEST.CDLOCALESTOQ, C_MOVIEST.VRCUSTENTRME, C_MOVIEST.CDTIPORETIME, C_MOVIEST.NMTIPORETIME, C_MOVIEST.IDENTRSAIDME,
                                   C_MOVIEST.CDTIPOOPERME, C_MOVIEST.VRCUSTLOGLME, C_MOVIEST.VRCUSTSEGLME, C_MOVIEST.CUSTLOGESTO, C_MOVIEST.CUSTSEGESTO,
                                   C_MOVIEST.VRPESOBRUTME);
            END LOOP;
         END IF;
      ELSE
         IF V_CDGRUPPRMOVEST IS NULL THEN
            IF (P_CDLOCALESTOQ IS NULL) THEN
               FOR C_MOVIEST IN C_POSESTALMOX_SEM LOOP
                  GRAVA_RELAT_MOVIEST(C_MOVIEST.CDFILIAL,     C_MOVIEST.CDPRODUTO,    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.CUSTMEDI,     C_MOVIEST.CUSTESTO,     C_MOVIEST.QTLANCAMEN,   C_MOVIEST.CUSTBRUT,     C_MOVIEST.CUSTMEES,
                                      C_MOVIEST.CUSTMEBR,     C_MOVIEST.IDALTCMEDMV,  C_MOVIEST.QTLANCTOEST,  C_MOVIEST.VRLANCTOEST,  C_MOVIEST.VRLANCTOBRUT,
                                      C_MOVIEST.IDORDEMCLC,   C_MOVIEST.NVPROD,       C_MOVIEST.NMPRODUTO,    C_MOVIEST.SGUNIDADE,    P_SEQUMOVI,
                                      C_MOVIEST.VRUNILANCTO,  C_MOVIEST.VRTOTLANCTO,  C_MOVIEST.VRTOTALANTE,  C_MOVIEST.QTTOTALANTE,  C_MOVIEST.CDALMOXARIFE,
                                      C_MOVIEST.CDLOCALESTOQ, C_MOVIEST.VRCUSTENTRME, C_MOVIEST.CDTIPORETIME, C_MOVIEST.NMTIPORETIME, C_MOVIEST.IDENTRSAIDME,
                                      C_MOVIEST.CDTIPOOPERME, C_MOVIEST.VRCUSTLOGLME, C_MOVIEST.VRCUSTSEGLME, C_MOVIEST.CUSTLOGESTO,  C_MOVIEST.CUSTSEGESTO,
                                      C_MOVIEST.VRPESOBRUTME);
               END LOOP;
            ELSE
               FOR C_MOVIEST IN C_POSESTALMOX LOOP
                  GRAVA_RELAT_MOVIEST(C_MOVIEST.CDFILIAL,     C_MOVIEST.CDPRODUTO,    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.CUSTMEDI,     C_MOVIEST.CUSTESTO,     C_MOVIEST.QTLANCAMEN,   C_MOVIEST.CUSTBRUT,     C_MOVIEST.CUSTMEES,
                                      C_MOVIEST.CUSTMEBR,     C_MOVIEST.IDALTCMEDMV,  C_MOVIEST.QTLANCTOEST,  C_MOVIEST.VRLANCTOEST,  C_MOVIEST.VRLANCTOBRUT,
                                      C_MOVIEST.IDORDEMCLC,   C_MOVIEST.NVPROD,       C_MOVIEST.NMPRODUTO,    C_MOVIEST.SGUNIDADE,    P_SEQUMOVI,
                                      C_MOVIEST.VRUNILANCTO,  C_MOVIEST.VRTOTLANCTO,  C_MOVIEST.VRTOTALANTE,  C_MOVIEST.QTTOTALANTE,  C_MOVIEST.CDALMOXARIFE,
                                      C_MOVIEST.CDLOCALESTOQ, C_MOVIEST.VRCUSTENTRME, C_MOVIEST.CDTIPORETIME, C_MOVIEST.NMTIPORETIME, C_MOVIEST.IDENTRSAIDME,
                                      C_MOVIEST.CDTIPOOPERME, C_MOVIEST.VRCUSTLOGLME, C_MOVIEST.VRCUSTSEGLME, C_MOVIEST.CUSTLOGESTO,  C_MOVIEST.CUSTSEGESTO,
                                      C_MOVIEST.VRPESOBRUTME);
               END LOOP;
            END IF;
         ELSE
            IF (P_CDLOCALESTOQ IS NULL) THEN
               FOR C_MOVIEST IN C_POSESTALMOX_GRUP_SEM LOOP
                  GRAVA_RELAT_MOVIEST(C_MOVIEST.CDFILIAL,     C_MOVIEST.CDPRODUTO,    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.CUSTMEDI,     C_MOVIEST.CUSTESTO,     C_MOVIEST.QTLANCAMEN,   C_MOVIEST.CUSTBRUT,     C_MOVIEST.CUSTMEES,
                                      C_MOVIEST.CUSTMEBR,     C_MOVIEST.IDALTCMEDMV,  C_MOVIEST.QTLANCTOEST,  C_MOVIEST.VRLANCTOEST,  C_MOVIEST.VRLANCTOBRUT,
                                      C_MOVIEST.IDORDEMCLC,   C_MOVIEST.NVPROD,       C_MOVIEST.NMPRODUTO,    C_MOVIEST.SGUNIDADE,    P_SEQUMOVI,
                                      C_MOVIEST.VRUNILANCTO,  C_MOVIEST.VRTOTLANCTO,  C_MOVIEST.VRTOTALANTE,  C_MOVIEST.QTTOTALANTE,  C_MOVIEST.CDALMOXARIFE,
                                      C_MOVIEST.CDLOCALESTOQ, C_MOVIEST.VRCUSTENTRME, C_MOVIEST.CDTIPORETIME, C_MOVIEST.NMTIPORETIME, C_MOVIEST.IDENTRSAIDME,
                                      C_MOVIEST.CDTIPOOPERME, C_MOVIEST.VRCUSTLOGLME, C_MOVIEST.VRCUSTSEGLME, C_MOVIEST.CUSTLOGESTO,  C_MOVIEST.CUSTSEGESTO,
                                      C_MOVIEST.VRPESOBRUTME);
               END LOOP;
            ELSE
               FOR C_MOVIEST IN C_POSESTALMOX_GRUP LOOP
                  GRAVA_RELAT_MOVIEST(C_MOVIEST.CDFILIAL,     C_MOVIEST.CDPRODUTO,    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.CUSTMEDI,     C_MOVIEST.CUSTESTO,     C_MOVIEST.QTLANCAMEN,   C_MOVIEST.CUSTBRUT,     C_MOVIEST.CUSTMEES,
                                      C_MOVIEST.CUSTMEBR,     C_MOVIEST.IDALTCMEDMV,  C_MOVIEST.QTLANCTOEST,  C_MOVIEST.VRLANCTOEST,  C_MOVIEST.VRLANCTOBRUT,
                                      C_MOVIEST.IDORDEMCLC,   C_MOVIEST.NVPROD,       C_MOVIEST.NMPRODUTO,    C_MOVIEST.SGUNIDADE,    P_SEQUMOVI,
                                      C_MOVIEST.VRUNILANCTO,  C_MOVIEST.VRTOTLANCTO,  C_MOVIEST.VRTOTALANTE,  C_MOVIEST.QTTOTALANTE,  C_MOVIEST.CDALMOXARIFE,
                                      C_MOVIEST.CDLOCALESTOQ, C_MOVIEST.VRCUSTENTRME, C_MOVIEST.CDTIPORETIME, C_MOVIEST.NMTIPORETIME, C_MOVIEST.IDENTRSAIDME,
                                      C_MOVIEST.CDTIPOOPERME, C_MOVIEST.VRCUSTLOGLME, C_MOVIEST.VRCUSTSEGLME, C_MOVIEST.CUSTLOGESTO,  C_MOVIEST.CUSTSEGESTO,
                                      C_MOVIEST.VRPESOBRUTME);
               END LOOP;
            END IF;
         END IF;
      END IF;
   END IF;

   IF V_CDGRUPPRMOVEST IS NULL THEN
      IF NVL(P_MOVCONSOLID,w_N) = w_N THEN
         FOR C_MOVIEST IN C_ITLANCTOEST LOOP
            IF (V_TIPCUSTEST = 3) OR (V_TIPCUSTEST = 42) THEN
               V_CUSTTOTENTR := C_MOVIEST.VRCUSTENTRME;
            ELSE
               BEGIN
                  SELECT CDPRODESTO
                  INTO V_CDPRODESTO
                  FROM PRODUTO
                  WHERE CDARVPROD = C_MOVIEST.CDPRODUTO;
               EXCEPTION
                  WHEN NO_DATA_FOUND THEN
                     V_CDPRODESTO := NULL;
               END;
               V_CUSTTOTENTR := CUSTO_PRODUTO_LCEST(C_MOVIEST.CDFILIAL, V_CDPRODESTO, 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(C_MOVIEST.CDFILIAL,     C_MOVIEST.CDPRODUTO,    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.CUSTMEDI,     C_MOVIEST.CUSTESTO,     C_MOVIEST.QTLANCAMEN,   C_MOVIEST.CUSTBRUT,     C_MOVIEST.CUSTMEES,
                                C_MOVIEST.CUSTMEBR,     C_MOVIEST.IDALTCMEDMV,  C_MOVIEST.QTLANCTOEST,  C_MOVIEST.VRLANCTOEST,  C_MOVIEST.VRLANCTOBRUT,
                                C_MOVIEST.IDORDEMCLC,   C_MOVIEST.NVPROD,       C_MOVIEST.NMPRODUTO,    C_MOVIEST.SGUNIDADE,    P_SEQUMOVI,
                                C_MOVIEST.VRUNILANCTO,  C_MOVIEST.VRTOTLANCTO,  C_MOVIEST.VRTOTALANTE,  C_MOVIEST.QTTOTALANTE,  C_MOVIEST.CDALMOXARIFE,
                                C_MOVIEST.CDLOCALESTOQ, V_CUSTTOTENTR,          C_MOVIEST.CDTIPORETIME, C_MOVIEST.NMTIPORETIME, C_MOVIEST.IDENTRSAIDME,
                                C_MOVIEST.CDTIPOOPERME, C_MOVIEST.VRCUSTLOGLME, C_MOVIEST.VRCUSTSEGLME, C_MOVIEST.CUSTLOGESTO, C_MOVIEST.CUSTSEGESTO,
                                C_MOVIEST.VRPESOBRUTME);
         END LOOP;
      ELSE
         FOR C_MOVIEST IN C_ITLANCTOEST_SEM LOOP
            IF (V_TIPCUSTEST = 3) OR (V_TIPCUSTEST = 42) THEN
               V_CUSTTOTENTR := C_MOVIEST.VRCUSTENTRME;
            ELSE
               BEGIN
                  SELECT CDPRODESTO
                  INTO V_CDPRODESTO
                  FROM PRODUTO
                  WHERE CDARVPROD = C_MOVIEST.CDPRODUTO;
               EXCEPTION
                  WHEN NO_DATA_FOUND THEN
                     V_CDPRODESTO := NULL;
               END;
               V_CUSTTOTENTR := CUSTO_PRODUTO_LCEST(C_MOVIEST.CDFILIAL, V_CDPRODESTO, 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(C_MOVIEST.CDFILIAL,     C_MOVIEST.CDPRODUTO,    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.CUSTMEDI,     C_MOVIEST.CUSTESTO,     C_MOVIEST.QTLANCAMEN,   C_MOVIEST.CUSTBRUT,     C_MOVIEST.CUSTMEES,
                                C_MOVIEST.CUSTMEBR,     C_MOVIEST.IDALTCMEDMV,  C_MOVIEST.QTLANCTOEST,  C_MOVIEST.VRLANCTOEST,  C_MOVIEST.VRLANCTOBRUT,
                                C_MOVIEST.IDORDEMCLC,   C_MOVIEST.NVPROD,       C_MOVIEST.NMPRODUTO,    C_MOVIEST.SGUNIDADE,    P_SEQUMOVI,
                                C_MOVIEST.VRUNILANCTO,  C_MOVIEST.VRTOTLANCTO,  C_MOVIEST.VRTOTALANTE,  C_MOVIEST.QTTOTALANTE,  C_MOVIEST.CDALMOXARIFE,
                                C_MOVIEST.CDLOCALESTOQ, V_CUSTTOTENTR,          C_MOVIEST.CDTIPORETIME, C_MOVIEST.NMTIPORETIME, C_MOVIEST.IDENTRSAIDME,
                                C_MOVIEST.CDTIPOOPERME, C_MOVIEST.VRCUSTLOGLME, C_MOVIEST.VRCUSTSEGLME, C_MOVIEST.CUSTLOGESTO, C_MOVIEST.CUSTSEGESTO,
                                C_MOVIEST.VRPESOBRUTME);
         END LOOP;
      END IF;

      IF NVL(P_MOVCONSOLID,w_N) = w_N THEN
         FOR C_MOVIEST IN C_IMPLANTACAO LOOP
            GRAVA_RELAT_MOVIEST(C_MOVIEST.CDFILIAL,    C_MOVIEST.CDPRODUTO,    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.CUSTMEDI,     C_MOVIEST.CUSTESTO,     C_MOVIEST.QTLANCAMEN,   C_MOVIEST.CUSTBRUT,     C_MOVIEST.CUSTMEES,
                                C_MOVIEST.CUSTMEBR,     C_MOVIEST.IDALTCMEDMV,  C_MOVIEST.QTLANCTOEST,  C_MOVIEST.VRLANCTOEST,  C_MOVIEST.VRLANCTOBRUT,
                                C_MOVIEST.IDORDEMCLC,   C_MOVIEST.NVPROD,       C_MOVIEST.NMPRODUTO,    C_MOVIEST.SGUNIDADE,    P_SEQUMOVI,
                                C_MOVIEST.VRUNILANCTO,  C_MOVIEST.VRTOTLANCTO,  C_MOVIEST.VRTOTALANTE,  C_MOVIEST.QTTOTALANTE,  C_MOVIEST.CDALMOXARIFE,
                                C_MOVIEST.CDLOCALESTOQ, C_MOVIEST.VRCUSTENTRME, C_MOVIEST.CDTIPORETIME, C_MOVIEST.NMTIPORETIME, C_MOVIEST.IDENTRSAIDME,
                                C_MOVIEST.CDTIPOOPERME, C_MOVIEST.VRCUSTLOGLME, C_MOVIEST.VRCUSTSEGLME, C_MOVIEST.CUSTLOGESTO, C_MOVIEST.CUSTSEGESTO,
                                C_MOVIEST.VRPESOBRUTME);
         END LOOP;
      ELSE
         FOR C_MOVIEST IN C_IMPLANTACAO_SEM LOOP
            GRAVA_RELAT_MOVIEST(C_MOVIEST.CDFILIAL,    C_MOVIEST.CDPRODUTO,    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.CUSTMEDI,     C_MOVIEST.CUSTESTO,     C_MOVIEST.QTLANCAMEN,   C_MOVIEST.CUSTBRUT,     C_MOVIEST.CUSTMEES,
                                C_MOVIEST.CUSTMEBR,     C_MOVIEST.IDALTCMEDMV,  C_MOVIEST.QTLANCTOEST,  C_MOVIEST.VRLANCTOEST,  C_MOVIEST.VRLANCTOBRUT,
                                C_MOVIEST.IDORDEMCLC,   C_MOVIEST.NVPROD,       C_MOVIEST.NMPRODUTO,    C_MOVIEST.SGUNIDADE,    P_SEQUMOVI,
                                C_MOVIEST.VRUNILANCTO,  C_MOVIEST.VRTOTLANCTO,  C_MOVIEST.VRTOTALANTE,  C_MOVIEST.QTTOTALANTE,  C_MOVIEST.CDALMOXARIFE,
                                C_MOVIEST.CDLOCALESTOQ, C_MOVIEST.VRCUSTENTRME, C_MOVIEST.CDTIPORETIME, C_MOVIEST.NMTIPORETIME, C_MOVIEST.IDENTRSAIDME,
                                C_MOVIEST.CDTIPOOPERME, C_MOVIEST.VRCUSTLOGLME, C_MOVIEST.VRCUSTSEGLME, C_MOVIEST.CUSTLOGESTO, C_MOVIEST.CUSTSEGESTO,
                                C_MOVIEST.VRPESOBRUTME);
         END LOOP;
      END IF;
   ELSE
      IF NVL(P_MOVCONSOLID,w_N) = w_N THEN
         FOR C_MOVIEST IN C_ITLANCTOEST_GRUP LOOP
            IF (V_TIPCUSTEST = 3) OR  (V_TIPCUSTEST = 42) THEN
               V_CUSTTOTENTR := C_MOVIEST.VRCUSTENTRME;
            ELSE
               BEGIN
                  SELECT CDPRODESTO
                  INTO V_CDPRODESTO
                  FROM PRODUTO
                  WHERE CDARVPROD = C_MOVIEST.CDPRODUTO;
               EXCEPTION
                  WHEN NO_DATA_FOUND THEN
                     V_CDPRODESTO := NULL;
               END;
               V_CUSTTOTENTR := CUSTO_PRODUTO_LCEST(C_MOVIEST.CDFILIAL, V_CDPRODESTO, 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(C_MOVIEST.CDFILIAL,     C_MOVIEST.CDPRODUTO,    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.CUSTMEDI,     C_MOVIEST.CUSTESTO,     C_MOVIEST.QTLANCAMEN,   C_MOVIEST.CUSTBRUT,     C_MOVIEST.CUSTMEES,
                                C_MOVIEST.CUSTMEBR,     C_MOVIEST.IDALTCMEDMV,  C_MOVIEST.QTLANCTOEST,  C_MOVIEST.VRLANCTOEST,  C_MOVIEST.VRLANCTOBRUT,
                                C_MOVIEST.IDORDEMCLC,   C_MOVIEST.NVPROD,       C_MOVIEST.NMPRODUTO,    C_MOVIEST.SGUNIDADE,    P_SEQUMOVI,
                                C_MOVIEST.VRUNILANCTO,  C_MOVIEST.VRTOTLANCTO,  C_MOVIEST.VRTOTALANTE,  C_MOVIEST.QTTOTALANTE,  C_MOVIEST.CDALMOXARIFE,
                                C_MOVIEST.CDLOCALESTOQ, V_CUSTTOTENTR,          C_MOVIEST.CDTIPORETIME, C_MOVIEST.NMTIPORETIME, C_MOVIEST.IDENTRSAIDME,
                                C_MOVIEST.CDTIPOOPERME, C_MOVIEST.VRCUSTLOGLME, C_MOVIEST.VRCUSTSEGLME, C_MOVIEST.CUSTLOGESTO, C_MOVIEST.CUSTSEGESTO,
                                C_MOVIEST.VRPESOBRUTME);
         END LOOP;
      ELSE
         FOR C_MOVIEST IN C_ITLANCTOEST_GRUP_SEM LOOP
            IF (V_TIPCUSTEST = 3) OR  (V_TIPCUSTEST = 42) THEN
               V_CUSTTOTENTR := C_MOVIEST.VRCUSTENTRME;
            ELSE
               BEGIN
                  SELECT CDPRODESTO
                  INTO V_CDPRODESTO
                  FROM PRODUTO
                  WHERE CDARVPROD = C_MOVIEST.CDPRODUTO;
               EXCEPTION
                  WHEN NO_DATA_FOUND THEN
                     V_CDPRODESTO := NULL;
               END;
               V_CUSTTOTENTR := CUSTO_PRODUTO_LCEST(C_MOVIEST.CDFILIAL, V_CDPRODESTO, 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(C_MOVIEST.CDFILIAL,     C_MOVIEST.CDPRODUTO,    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.CUSTMEDI,     C_MOVIEST.CUSTESTO,     C_MOVIEST.QTLANCAMEN,   C_MOVIEST.CUSTBRUT,     C_MOVIEST.CUSTMEES,
                                C_MOVIEST.CUSTMEBR,     C_MOVIEST.IDALTCMEDMV,  C_MOVIEST.QTLANCTOEST,  C_MOVIEST.VRLANCTOEST,  C_MOVIEST.VRLANCTOBRUT,
                                C_MOVIEST.IDORDEMCLC,   C_MOVIEST.NVPROD,       C_MOVIEST.NMPRODUTO,    C_MOVIEST.SGUNIDADE,    P_SEQUMOVI,
                                C_MOVIEST.VRUNILANCTO,  C_MOVIEST.VRTOTLANCTO,  C_MOVIEST.VRTOTALANTE,  C_MOVIEST.QTTOTALANTE,  C_MOVIEST.CDALMOXARIFE,
                                C_MOVIEST.CDLOCALESTOQ, V_CUSTTOTENTR,          C_MOVIEST.CDTIPORETIME, C_MOVIEST.NMTIPORETIME, C_MOVIEST.IDENTRSAIDME,
                                C_MOVIEST.CDTIPOOPERME, C_MOVIEST.VRCUSTLOGLME, C_MOVIEST.VRCUSTSEGLME, C_MOVIEST.CUSTLOGESTO, C_MOVIEST.CUSTSEGESTO,
                                C_MOVIEST.VRPESOBRUTME);
         END LOOP;
      END IF;

      IF NVL(P_MOVCONSOLID,w_N) = w_N THEN
         FOR C_MOVIEST IN C_IMPLANTACAO_GRUP LOOP
            GRAVA_RELAT_MOVIEST(C_MOVIEST.CDFILIAL,    C_MOVIEST.CDPRODUTO,    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.CUSTMEDI,     C_MOVIEST.CUSTESTO,     C_MOVIEST.QTLANCAMEN,   C_MOVIEST.CUSTBRUT,     C_MOVIEST.CUSTMEES,
                                C_MOVIEST.CUSTMEBR,     C_MOVIEST.IDALTCMEDMV,  C_MOVIEST.QTLANCTOEST,  C_MOVIEST.VRLANCTOEST,  C_MOVIEST.VRLANCTOBRUT,
                                C_MOVIEST.IDORDEMCLC,   C_MOVIEST.NVPROD,       C_MOVIEST.NMPRODUTO,    C_MOVIEST.SGUNIDADE,    P_SEQUMOVI,
                                C_MOVIEST.VRUNILANCTO,  C_MOVIEST.VRTOTLANCTO,  C_MOVIEST.VRTOTALANTE,  C_MOVIEST.QTTOTALANTE,  C_MOVIEST.CDALMOXARIFE,
                                C_MOVIEST.CDLOCALESTOQ, C_MOVIEST.VRCUSTENTRME, C_MOVIEST.CDTIPORETIME, C_MOVIEST.NMTIPORETIME, C_MOVIEST.IDENTRSAIDME,
                                C_MOVIEST.CDTIPOOPERME, C_MOVIEST.VRCUSTLOGLME, C_MOVIEST.VRCUSTSEGLME, C_MOVIEST.CUSTLOGESTO, C_MOVIEST.CUSTSEGESTO,
                                C_MOVIEST.VRPESOBRUTME);
         END LOOP;
      ELSE
         FOR C_MOVIEST IN C_IMPLANTACAO_GRUP_SEM LOOP
            GRAVA_RELAT_MOVIEST(C_MOVIEST.CDFILIAL,    C_MOVIEST.CDPRODUTO,    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.CUSTMEDI,     C_MOVIEST.CUSTESTO,     C_MOVIEST.QTLANCAMEN,   C_MOVIEST.CUSTBRUT,     C_MOVIEST.CUSTMEES,
                                C_MOVIEST.CUSTMEBR,     C_MOVIEST.IDALTCMEDMV,  C_MOVIEST.QTLANCTOEST,  C_MOVIEST.VRLANCTOEST,  C_MOVIEST.VRLANCTOBRUT,
                                C_MOVIEST.IDORDEMCLC,   C_MOVIEST.NVPROD,       C_MOVIEST.NMPRODUTO,    C_MOVIEST.SGUNIDADE,    P_SEQUMOVI,
                                C_MOVIEST.VRUNILANCTO,  C_MOVIEST.VRTOTLANCTO,  C_MOVIEST.VRTOTALANTE,  C_MOVIEST.QTTOTALANTE,  C_MOVIEST.CDALMOXARIFE,
                                C_MOVIEST.CDLOCALESTOQ, C_MOVIEST.VRCUSTENTRME, C_MOVIEST.CDTIPORETIME, C_MOVIEST.NMTIPORETIME, C_MOVIEST.IDENTRSAIDME,
                                C_MOVIEST.CDTIPOOPERME, C_MOVIEST.VRCUSTLOGLME, C_MOVIEST.VRCUSTSEGLME, C_MOVIEST.CUSTLOGESTO, C_MOVIEST.CUSTSEGESTO,
                                C_MOVIEST.VRPESOBRUTME);
         END LOOP;
      END IF;
   END IF;

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

   FOR C_MOVIMENTA IN(
      SELECT R.*, P.CDPRODUTO
        FROM RELAT_MOVIEST R, PRODUTO P
       WHERE R.NRRELATMOVI   = P_SEQUMOVI
         AND R.CDPRODMOVIEST = P.CDARVPROD
         AND R.CDFILIMOVIEST = P_FILIAL
       ORDER BY DECODE(P_CUSTOALMOX,w_S,CDALMOXARIRM,P_CUSTOALMOX),
                DECODE(P_CUSTOALMOX,w_S,CDLOCALESTRM,P_CUSTOALMOX),
                CDPRODMOVIEST, DECODE(NVL(NVL(P_MOVCONSOLID,w_N),w_N),w_N,NRLOTEESTQME,NULL), DECODE(NVL(NVL(P_MOVCONSOLID,w_N),w_N),w_N,NRSUBLOTEME,NULL),
                DTLANCMOVIEST, NVL(IDORDEMCALC,IDTIPOMOVIEST), DECODE(NVL(NVL(P_MOVCONSOLID,w_N),w_N),w_S,NRLOTEESTQME,NULL),
                DECODE(NVL(NVL(P_MOVCONSOLID,w_N),w_N),w_S,NRSUBLOTEME,NULL), NRLANCMOVIEST, NRSEQUMOVIEST) LOOP
      V_DIFCUSTSAI :=  0;
      IF (NVL(P_MOVCONSOLID,w_N) = w_N)  THEN
         IF (C_MOVIMENTA.IDTIPOMOVIEST <> '-')          AND
            ((V_CDPRODUTO  <> C_MOVIMENTA.CDPRODMOVIEST) OR
             (V_NRLOTEESTQ <> C_MOVIMENTA.NRLOTEESTQME)  OR
             (V_NRSUBLOTE  <> C_MOVIMENTA.NRSUBLOTEME)   OR
             ((V_CDALMOXLCEST <> C_MOVIMENTA.CDALMOXARIRM||C_MOVIMENTA.CDLOCALESTRM) AND (P_CUSTOALMOX = w_S)))THEN

            GRAVA_RELAT_MOVIEST(P_FILIAL, C_MOVIMENTA.CDPRODMOVIEST, C_MOVIMENTA.NRLOTEESTQME, C_MOVIMENTA.NRSUBLOTEME,
                                P_DTINICIAL, '-', '----', '------', '0001', 0, 0, 0, 0, 0, 0, 0,
                                ' ', 0, 0, 0, '-', C_MOVIMENTA.CDTOTMOVIEST, C_MOVIMENTA.NMPRODMOVIEST,
                                C_MOVIMENTA.SGUNIDMOVI, P_SEQUMOVI, 0, 0, 0, 0,
                                C_MOVIMENTA.CDALMOXARIRM, C_MOVIMENTA.CDLOCALESTRM, 0, NULL, NULL, NULL, NULL,
                                0,0,0,0,0);

            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;
      ELSE
         IF (C_MOVIMENTA.IDTIPOMOVIEST <> '-')           AND
            (V_CDPRODUTO  <> C_MOVIMENTA.CDPRODMOVIEST) THEN

            GRAVA_RELAT_MOVIEST(P_FILIAL, C_MOVIMENTA.CDPRODMOVIEST, C_MOVIMENTA.NRLOTEESTQME, C_MOVIMENTA.NRSUBLOTEME,
                                P_DTINICIAL, '-', '----', '------', '0001', 0, 0, 0, 0, 0, 0, 0,
                                ' ', 0, 0, 0, '-', C_MOVIMENTA.CDTOTMOVIEST, C_MOVIMENTA.NMPRODMOVIEST,
                                C_MOVIMENTA.SGUNIDMOVI, P_SEQUMOVI, 0, 0, 0, 0,
                                C_MOVIMENTA.CDALMOXARIRM, C_MOVIMENTA.CDLOCALESTRM, 0, NULL, NULL, NULL, NULL,
                                C_MOVIMENTA.VRCUSTLOGLME,
                                C_MOVIMENTA.VRCUSTSEGLME,
                                C_MOVIMENTA.VRCUSTLOGEME,
                                C_MOVIMENTA.VRCUSTSEGEME,
                                C_MOVIMENTA.VRPESOBRUTME);

            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;
         IF (C_MOVIMENTA.IDTIPOMOVIEST <> '-')          AND
            ((V_CDPRODUTO  <> C_MOVIMENTA.CDPRODMOVIEST) OR
             (V_NRLOTEESTQ <> C_MOVIMENTA.NRLOTEESTQME)  OR
             (V_NRSUBLOTE  <> C_MOVIMENTA.NRSUBLOTEME)   OR
             ((V_CDALMOXLCEST <> C_MOVIMENTA.CDALMOXARIRM||C_MOVIMENTA.CDLOCALESTRM) AND (P_CUSTOALMOX = w_S)))THEN

             V_QTDEANT      := 0;
             V_VRCUSTLOGEME := 0;
             V_VRCUSTSEGEME := 0;
             V_ULTCUSTENT   := 0;
         END IF;
      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 (NVL(P_MOVCONSOLID,w_N) = w_N)  THEN
            IF C_MOVIMENTA.IDTIPOMOVIEST = '-' THEN
               V_CUSTESTOQ := CUSTO_PRODUTO_LCEST(P_FILIAL, 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(P_FILIAL, V_CDPRODESTO, V_TIPCUSTEST, C_MOVIMENTA.DTLANCMOVIEST, V_DATAPROC,
                                                   C_MOVIMENTA.CDALMOXARIRM,C_MOVIMENTA.CDLOCALESTRM, C_MOVIMENTA.NRLOTEESTQME, C_MOVIMENTA.NRSUBLOTEME);
             END IF;
         ELSE
            IF C_MOVIMENTA.IDTIPOMOVIEST = '-' THEN
               V_CUSTESTOQ := CUSTO_PRODUTO(P_FILIAL, V_CDPRODESTO, V_TIPCUSTEST, C_MOVIMENTA.DTLANCMOVIEST - 1, V_DATAPROC);
            ELSE
               V_CUSTESTOQ := CUSTO_PRODUTO(P_FILIAL, V_CDPRODESTO, V_TIPCUSTEST, C_MOVIMENTA.DTLANCMOVIEST, V_DATAPROC);
            END IF;
         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;

         IF NVL(P_MOVCONSOLID,w_N) = w_S THEN
            IF V_QTATUAL <> 0 THEN
               V_CUSTOANTCONS := V_VRATUAL/V_QTATUAL;
            END IF;
            CALCULA_SALDO_ANTERIOR(C_MOVIMENTA.CDFILLANCMEST, C_MOVIMENTA.NRLANCMOVIEST,
                                   C_MOVIMENTA.CDFILIMOVIEST, C_MOVIMENTA.CDPRODUTO,
                                   C_MOVIMENTA.CDALMOXARIRM, C_MOVIMENTA.CDLOCALESTRM,
                                   C_MOVIMENTA.NRLOTEESTQME, C_MOVIMENTA.NRSUBLOTEME,
                                   C_MOVIMENTA.DTLANCMOVIEST, V_QTDEAJUSTE, V_VLRESTOAJU,
                                   V_VLRBRUTAJU, V_VRQTANT, V_VRESTANT, w_S);

            V_QTMOVI  := C_MOVIMENTA.QTLANCMOVIEST - V_QTDEAJUSTE;

            V_VRCUSTLOGEME := 0;
            V_VRCUSTSEGEME := 0;

            V_QTATUAL := V_QTATUAL + C_MOVIMENTA.QTLANCMOVIEST - V_QTDEAJUSTE;

            IF P_IDCUSTO = 1 THEN
               IF V_TIPCUSTEST <> 3 THEN
                  V_VRATUAL := V_VRATUAL + C_MOVIMENTA.VRLANCMOVIEST - (V_VLRESTOAJU * V_QTDEAJUSTE);
               ELSE
                  IF V_QTDEAJUSTE <> 0 THEN
                     V_VRATUAL := V_VRATUAL + C_MOVIMENTA.VRLANCMOVIEST - V_VLRESTOAJU;
                  ELSE
                     V_VRATUAL := V_VRATUAL + C_MOVIMENTA.VRLANCMOVIEST;
                  END IF;
               END IF;

               IF C_MOVIMENTA.QTLANCMOVIEST <> 0 THEN
                  IF V_TIPCUSTEST <> 3 THEN
                     V_CUSTMED := C_MOVIMENTA.VRLANCMOVIEST/C_MOVIMENTA.QTLANCMOVIEST;
                  ELSE
                     V_CUSTMED := CUSTO_PRODUTO_LCEST(P_FILIAL, V_CDPRODESTO, V_TIPCUSTEST, C_MOVIMENTA.DTLANCMOVIEST,  V_DATAPROC,
                                                     C_MOVIMENTA.CDALMOXARIRM, C_MOVIMENTA.CDLOCALESTRM, C_MOVIMENTA.NRLOTEESTQME, C_MOVIMENTA.NRSUBLOTEME);
                  END IF;
                  IF V_QTATUAL = 0 THEN
                     V_DIFCUSTSAI :=  V_CUSTOANTCONS;
                  END IF;
               ELSE
                  V_CUSTMED := 0;
               END IF;
            ELSIF P_IDCUSTO = 2 THEN
               IF V_TIPCUSTEST <> 3 THEN
                  V_VRATUAL := V_VRATUAL + C_MOVIMENTA.VRBRUTMOVIEST - (V_VLRBRUTAJU * V_QTDEAJUSTE);
               ELSE
                  IF V_QTDEAJUSTE <> 0 THEN
                     V_VRATUAL := V_VRATUAL + C_MOVIMENTA.VRBRUTMOVIEST - V_VLRBRUTAJU;
                  ELSE
                     V_VRATUAL := V_VRATUAL + C_MOVIMENTA.VRBRUTMOVIEST;
                  END IF;
               END IF;
               IF C_MOVIMENTA.QTLANCMOVIEST <> 0 THEN
                  V_CUSTMED := C_MOVIMENTA.VRBRUTMOVIEST/C_MOVIMENTA.QTLANCMOVIEST;
               ELSE
                  V_CUSTMED := 0;
               END IF;
            END IF;
         ELSE
            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(P_FILIAL, 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;
         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;

         IF NVL(P_MOVCONSOLID,w_N) = w_S THEN
            V_CUSTOANTCONS := V_VRATUAL;
            CALCULA_SALDO_ANTERIOR(C_MOVIMENTA.CDFILLANCMEST, C_MOVIMENTA.NRLANCMOVIEST,
                                   C_MOVIMENTA.CDFILIMOVIEST, C_MOVIMENTA.CDPRODUTO,
                                   C_MOVIMENTA.CDALMOXARIRM, C_MOVIMENTA.CDLOCALESTRM,
                                   C_MOVIMENTA.NRLOTEESTQME, C_MOVIMENTA.NRSUBLOTEME,
                                   C_MOVIMENTA.DTLANCMOVIEST, V_QTDEAJUSTE, V_VLRESTOAJU,
                                   V_VLRBRUTAJU, V_VRQTANT, V_VRESTANT, w_S);

            V_QTMOVI := (C_MOVIMENTA.QTLANCMOVIEST - V_QTDEAJUSTE);


            IF V_QTMOVI < 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;
            END IF;

            IF C_MOVIMENTA.IDALTCMEDMOV = w_S THEN
               IF V_QTMOVI <> 0 THEN
                  IF P_IDCUSTO = 1 THEN
                     IF V_VLRESTOAJU <> 0 THEN
                        V_CUSTMED := CUSTO_PRODUTO_LCEST(C_MOVIMENTA.CDFILLANCMEST, C_MOVIMENTA.CDPRODUTO, V_TIPCUSTEST, C_MOVIMENTA.DTLANCMOVIEST, V_DATAPROC,
                                              C_MOVIMENTA.CDALMOXARIRM,C_MOVIMENTA.CDLOCALESTRM, C_MOVIMENTA.NRLOTEESTQME, C_MOVIMENTA.NRSUBLOTEME);
                        --V_CUSTMED := V_VLRESTOAJU/(V_QTDEAJUSTE + V_QTMOVI);
                     ELSE
                        V_CUSTMED := 0;
                     END IF;
                  ELSE
                     IF V_VLRBRUTAJU <> 0 THEN
                        V_CUSTMED := V_VLRBRUTAJU/(V_QTDEAJUSTE + V_QTMOVI);
                     ELSE
                        V_CUSTMED := 0;
                     END IF;
                  END IF;
               ELSE
                  V_CUSTMED := 0;
               END IF;
            ELSE
               IF V_QTDEAJUSTE <> 0 THEN
                  IF V_TIPCUSTEST <> 3 THEN
                     V_CUSTMED := CUSTO_PRODUTO_LCEST(C_MOVIMENTA.CDFILLANCMEST, C_MOVIMENTA.CDPRODUTO, V_TIPCUSTEST, C_MOVIMENTA.DTLANCMOVIEST, V_DATAPROC,
                                              C_MOVIMENTA.CDALMOXARIRM,C_MOVIMENTA.CDLOCALESTRM, C_MOVIMENTA.NRLOTEESTQME, C_MOVIMENTA.NRSUBLOTEME);
                     --V_CUSTMED := V_VLRESTOAJU;
                  ELSE
                     IF P_IDCUSTO = 1 THEN
                        V_CUSTMED := V_VLRESTOAJU/V_QTDEAJUSTE;
                     ELSE
                        V_CUSTMED := V_VLRBRUTAJU/V_QTDEAJUSTE;
                     END IF;
                  END IF;
               ELSE
                  IF V_VLRESTOAJU <> 0 THEN
                     IF V_TIPCUSTEST <> 3 THEN
                        V_CUSTMED := V_VLRESTOAJU;
                     ELSE
                        IF V_VRQTANT <> 0 THEN
                           IF P_IDCUSTO = 1 THEN
                              V_CUSTMED := V_VLRESTOAJU/V_VRQTANT;
                           ELSE
                              V_CUSTMED := V_VLRBRUTAJU/V_VRQTANT;
                           END IF;
                        END IF;
                     END IF;
                  END IF;
               END IF;
            END IF;
            V_QTATUAL := (V_QTATUAL + V_QTMOVI);
            V_VRATUAL := V_VRATUAL + (V_CUSTMED * V_QTMOVI);
            IF V_QTATUAL = 0 THEN
               V_DIFCUSTSAI := abs(V_CUSTMED*V_QTMOVI) - V_CUSTOANTCONS;
            END IF;
         ELSE
            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;

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

         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(P_FILIAL, 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_QTATUAL := V_QTATUAL + V_QTMOVI;

           IF (V_TIPCUSTEST <> 3) AND (V_TIPCUSTEST <> 4) AND (V_TIPCUSTEST <> 42) 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;

           V_VRATUAL := V_VRATUAL + C_MOVIMENTA.VRCUSTLOGLME + C_MOVIMENTA.VRCUSTSEGLME;
           IF (V_TIPCUSTEST <> 3) AND (V_TIPCUSTEST <> 4) AND (V_TIPCUSTEST <> 42) 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 NVL(P_MOVCONSOLID,w_N) = w_S THEN
            IF V_QTATUAL <> 0 THEN
               V_CUSTOANTCONS := V_VRATUAL /V_QTATUAL;
            END IF;
            CALCULA_SALDO_ANTERIOR(C_MOVIMENTA.CDFILLANCMEST, C_MOVIMENTA.NRLANCMOVIEST,
                                   C_MOVIMENTA.CDFILIMOVIEST, C_MOVIMENTA.CDPRODUTO,
                                   C_MOVIMENTA.CDALMOXARIRM, C_MOVIMENTA.CDLOCALESTRM,
                                   C_MOVIMENTA.NRLOTEESTQME, C_MOVIMENTA.NRSUBLOTEME,
                                   C_MOVIMENTA.DTLANCMOVIEST, V_QTDEAJUSTE, V_VLRESTOAJU,
                                   V_VLRBRUTAJU, V_VRQTANT, V_VRESTANT, w_S);

            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;
                V_CUSTESTOQ := CUSTO_PRODUTO_LCEST(P_FILIAL, V_CDPRODESTO, V_TIPCUSTEST, C_MOVIMENTA.DTLANCMOVIEST, V_DATAPROC,
                                                  C_MOVIMENTA.CDALMOXARIRM,C_MOVIMENTA.CDLOCALESTRM, C_MOVIMENTA.NRLOTEESTQME, C_MOVIMENTA.NRSUBLOTEME);


            IF V_QTDEAJUSTE <> 0 THEN
               IF V_TIPCUSTEST <> 3 THEN
                  V_CUSTMED := V_CUSTESTOQ;
                  V_VRATUAL := V_VRATUAL - (V_QTMOVI * V_CUSTMED);
               ELSE
                  IF P_IDCUSTO = 1 THEN
                     V_VRATUAL := V_VRATUAL - (V_QTMOVI * (V_VLRESTOAJU/V_QTDEAJUSTE));
                     V_CUSTMED := V_VLRESTOAJU/V_QTDEAJUSTE;
                  ELSIF P_IDCUSTO = 2 THEN
                     V_VRATUAL := V_VRATUAL - (V_QTMOVI * (V_VLRBRUTAJU/V_QTDEAJUSTE));
                     V_CUSTMED := V_VLRBRUTAJU/V_QTDEAJUSTE;
                  END IF;
               END IF;
               V_QTATUAL := V_QTATUAL - V_QTMOVI;
               IF V_QTATUAL = 0 THEN
                  V_DIFCUSTSAI :=  V_CUSTOANTCONS;
               END IF;
            ELSE
               IF (V_TIPCUSTEST <> 3) OR (V_VLRESTOAJU = 0) THEN
                  V_CUSTMED := V_CUSTESTOQ;
               ELSE
                  IF V_VRQTANT <> 0 THEN
                     IF P_IDCUSTO = 1 THEN
                        V_CUSTMED := V_VLRESTOAJU/V_VRQTANT;
                     ELSE
                        V_CUSTMED := V_VLRBRUTAJU/V_VRQTANT;
                     END IF;
                  END IF;
               END IF;

               V_VRATUAL := V_VRATUAL - (V_CUSTMED * V_QTMOVI);
               V_QTATUAL := V_QTATUAL - V_QTMOVI;
            END IF;
         ELSE
            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_QTATUAL - V_QTMOVI) < 0) AND (V_QTATUAL <= 0) THEN
               V_CUSTMED := V_CUSTZERO;
            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;
         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 NVL(P_MOVCONSOLID,w_N) = w_S THEN
            IF V_QTATUAL <> 0 THEN
               V_CUSTOANTCONS := V_VRATUAL/V_QTATUAL;
            END IF;
            CALCULA_SALDO_ANTERIOR(C_MOVIMENTA.CDFILLANCMEST, C_MOVIMENTA.NRLANCMOVIEST,
                                   C_MOVIMENTA.CDFILIMOVIEST, C_MOVIMENTA.CDPRODUTO,
                                   C_MOVIMENTA.CDALMOXARIRM, C_MOVIMENTA.CDLOCALESTRM,
                                   C_MOVIMENTA.NRLOTEESTQME, C_MOVIMENTA.NRSUBLOTEME,
                                   C_MOVIMENTA.DTLANCMOVIEST, V_QTDEAJUSTE, V_VLRESTOAJU,
                                   V_VLRBRUTAJU, V_VRQTANT, V_VRESTANT, w_S);

            V_CUSTESTOQ := CUSTO_PRODUTO(P_FILIAL, V_CDPRODESTO, V_TIPCUSTEST, C_MOVIMENTA.DTLANCMOVIEST, V_DATAPROC);
--            CUSTO_PRODUTO_LCEST(P_FILIAL, V_CDPRODESTO, V_TIPCUSTEST, C_MOVIMENTA.DTLANCMOVIEST, V_DATAPROC,
--                                              C_MOVIMENTA.CDALMOXARIRM,C_MOVIMENTA.CDLOCALESTRM, C_MOVIMENTA.NRLOTEESTQME, C_MOVIMENTA.NRSUBLOTEME);

            IF (V_QTMOVI <> 0) OR (C_MOVIMENTA.CDTIPOOPERME = '03')  THEN
               IF V_QTDEAJUSTE <> 0 THEN
                  IF V_TIPCUSTEST <> 3 THEN
                     V_CUSTMED := V_CUSTESTOQ;
                     V_VRATUAL := V_VRATUAL - (V_QTMOVI * V_CUSTMED);
                  ELSE
                     IF P_IDCUSTO = 1 THEN
                        V_VRATUAL := V_VRATUAL - (V_QTMOVI * (V_VLRESTOAJU/V_QTDEAJUSTE));
                        V_CUSTMED := V_VLRESTOAJU/V_QTDEAJUSTE;
                     ELSIF P_IDCUSTO = 2 THEN
                        V_VRATUAL := V_VRATUAL - (V_QTMOVI * (V_VLRBRUTAJU/V_QTDEAJUSTE));
                        V_CUSTMED := V_VLRBRUTAJU/V_QTDEAJUSTE;
                     END IF;
                  END IF;
                  V_QTATUAL := V_QTATUAL - V_QTMOVI;
                  IF V_QTATUAL = 0 THEN
                     V_DIFCUSTSAI :=  V_CUSTOANTCONS;
                  END IF;
               ELSE
                  V_VRANTEST := V_VRATUAL;
                  IF V_VLRESTOAJU <> 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 P_IDCUSTO = 1 THEN
                        V_CUSTMED := V_VLRESTOAJU;
                     ELSE
                        V_CUSTMED := V_VLRBRUTAJU;
                     END IF;
                  ELSE
                     V_CUSTMED := V_CUSTESTOQ;
                  END IF;

                  V_VRATUAL := V_VRATUAL - (V_CUSTMED * V_QTMOVI);
                  V_QTATUAL := V_QTATUAL - V_QTMOVI;
               END IF;
            ELSE           V_CUSTMED := 0;
            END IF;
         ELSE
            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_QTATUAL - V_QTMOVI) < 0) AND (V_QTATUAL <= 0) THEN
               V_CUSTMED := V_CUSTZERO;
            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;
         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 NVL(P_MOVCONSOLID,w_N) = w_S THEN
            CALCULA_SALDO_ANTERIOR(C_MOVIMENTA.CDFILLANCMEST, C_MOVIMENTA.NRLANCMOVIEST,
                                   C_MOVIMENTA.CDFILIMOVIEST, C_MOVIMENTA.CDPRODUTO,
                                   C_MOVIMENTA.CDALMOXARIRM, C_MOVIMENTA.CDLOCALESTRM,
                                   C_MOVIMENTA.NRLOTEESTQME, C_MOVIMENTA.NRSUBLOTEME,
                                   C_MOVIMENTA.DTLANCMOVIEST, V_QTDEAJUSTE, V_VLRESTOAJU,
                                   V_VLRBRUTAJU, V_VRQTANT, V_VRESTANT, w_S);
            IF P_IDCUSTO = 1 THEN
               V_CUSTMED :=  V_VLRESTOAJU;
            ELSE
               V_CUSTMED := V_VLRBRUTAJU;
            END IF;
         ELSE
            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(P_FILIAL, 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(P_FILIAL, 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;
         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(P_FILIAL, 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
      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 NRRELATMOVI   = P_SEQUMOVI                AND
            CDFILIMOVIEST = P_FILIAL                  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;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
---------------------------------------------------------------------------------                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
-- ATUALIZA TABELA DE DESCRICAO DOS LANCAMENTOS                                --                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
---------------------------------------------------------------------------------                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
      IF (NVL(P_DELMOVIMEN, W_s) = w_S) THEN
         IF C_MOVIMENTA.IDTIPOMOVIEST <> '-' THEN
            IF P_IDLANCTO = 1 THEN                                           --GERAL
               SELECT COUNT(NRLANCDESCMOVI) INTO V_COUNT
               FROM RELAT_DESCMOVI
               WHERE CDFILLANCDMOVI = C_MOVIMENTA.CDFILLANCMEST AND
                     NRLANCDESCMOVI = C_MOVIMENTA.NRLANCMOVIEST AND
                     CDPRODDESCMOVI = ' '                       AND
                     NRRELATDESC    = P_SEQUMOVI;

               IF V_COUNT = 0 THEN
                  INSERT INTO RELAT_DESCMOVI
                  (DTLANCDESCMOVI, IDTIPODESCMOVI, CDFILLANCDMOVI, NRLANCDESCMOVI, CDPRODDESCMOVI,
                   NRRELATDESC)
                  VALUES
                  (C_MOVIMENTA.DTLANCMOVIEST, C_MOVIMENTA.IDTIPOMOVIEST,
                   C_MOVIMENTA.CDFILLANCMEST, C_MOVIMENTA.NRLANCMOVIEST, ' ', P_SEQUMOVI);
               END IF;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
            ELSIF P_IDLANCTO = 2 THEN                           --QUEBRA POR PRODUTO
               SELECT COUNT(NRLANCDESCMOVI) INTO V_COUNT                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
               FROM RELAT_DESCMOVI                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
               WHERE CDFILLANCDMOVI = C_MOVIMENTA.CDFILLANCMEST AND                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
                     NRLANCDESCMOVI = C_MOVIMENTA.NRLANCMOVIEST AND                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
                     CDPRODDESCMOVI = C_MOVIMENTA.CDPRODMOVIEST AND                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
                     NRRELATDESC    = P_SEQUMOVI;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        

               IF V_COUNT = 0 THEN                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
                  INSERT INTO RELAT_DESCMOVI
                  (DTLANCDESCMOVI, IDTIPODESCMOVI, CDFILLANCDMOVI, NRLANCDESCMOVI,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
                   CDPRODDESCMOVI, NMPRODDESCMOVI, NRRELATDESC)
                  VALUES                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
                  (C_MOVIMENTA.DTLANCMOVIEST, C_MOVIMENTA.IDTIPOMOVIEST, C_MOVIMENTA.CDFILLANCMEST,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
                   C_MOVIMENTA.NRLANCMOVIEST, C_MOVIMENTA.CDPRODMOVIEST, C_MOVIMENTA.NMPRODMOVIEST, P_SEQUMOVI);
               END IF;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
            END IF;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
         END IF;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
      END IF;

      V_CUSTANT      := V_VRATUAL;
      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';
   IF  (NVL(P_DELMOVIMEN, W_s) = w_S) THEN
      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
          WHERE --DECODE(IDTIPOMOVIEST,'-', 1,QTLANCAMENTO)  <> 0  AND
                NOT(IDTIPOMOVIEST IN ('0','1','2')) AND
                NRRELATMOVI  =  P_SEQUMOVI
          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
          WHERE IDTIPOMOVIEST IN ('0','1') AND
                NRRELATMOVI  =  P_SEQUMOVI
          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
          WHERE IDTIPOMOVIEST = '2'         AND --Entradas no estoque
                NRRELATMOVI   =  P_SEQUMOVI AND
                ((P_IDCUSTO     = '1') AND (V_TIPCUSTEST <> 4) AND (V_TIPCUSTEST <> 43))        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
          WHERE IDTIPOMOVIEST = '2'         AND --Entradas no estoque
                NRRELATMOVI   = P_SEQUMOVI  AND
                ((P_IDCUSTO     = '2') OR (V_TIPCUSTEST = 4) OR (V_TIPCUSTEST = 43))         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
          WHERE IDTIPOMOVIEST = '2'         AND --Entradas no estoque
                NRRELATMOVI   = P_SEQUMOVI  AND
                QTLANCAMENTO  = 0
          GROUP BY CDTOTMOVIEST, IDTIPOMOVIEST, CDTIPORETIME, NMTIPORETIME, IDENTRSAIDME, CDTIPOOPERME)


         ORDER BY IDTIPOMOVIEST

         ) LOOP

         SELECT COUNT(*) INTO V_COUNT
         FROM RELAT_TOTAMOVI
         WHERE CDPRODTOTAMOVI = C_TOTALMOVI.CDTOTMOVIEST AND
               NRRELATTOTA    = P_SEQUMOVI;

         V_SGUNIDADE := 'X';

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

         FOR C_UNIDADE IN (SELECT SGUNIDMOVI
                           FROM RELAT_MOVIEST
                           WHERE NRRELATMOVI  = P_SEQUMOVI AND
                                 CDTOTMOVIEST = C_TOTALMOVI.CDTOTMOVIEST
                           GROUP BY SGUNIDMOVI) LOOP

            IF V_SGUNIDADE = 'X' THEN
               V_SGUNIDADE := C_UNIDADE.SGUNIDMOVI;
            ELSE
               IF V_SGUNIDADE <> C_UNIDADE.SGUNIDMOVI THEN
                  IF (P_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 = P_TAMNIVEL) THEN
            V_VRQTDETOTAL := C_TOTALMOVI.VRQTDETOTAL;
         ELSE
            V_VRQTDETOTAL := 0;
         END IF;

         IF V_COUNT = 0 THEN
            INSERT INTO RELAT_TOTAMOVI
               (IDTIPOTOTAMOVI, CDPRODTOTAMOVI, VRSOMPINI, VRSOMAINV, VRSOMINVE,
                VRSOMENTR, VRSOMRETI, VRSOMSAID, VRSOMDFOR, VRSOMSATU, NRRELATTOTA,
                VRQTDEPINIME, VRQTDEAINVME, VRQTDEINVEME, VRQTDEENTRME, VRQTDERETIME,
                VRQTDESAIDME, VRQTDEDFORME, VRQTDESATUME, VRSOMCUSTLOG, VRSOMCUSTSEG,
                VRSOMDIFSAID)
            VALUES
               ('P', C_TOTALMOVI.CDTOTMOVIEST, 0, 0, 0, 0, 0, 0, 0, 0, P_SEQUMOVI,
                0, 0, 0, 0, 0, 0, 0, 0, 0,0,0);
         END IF;

         IF V_INSERT = '1' THEN

            V_INSERT := '2';

            INSERT INTO RELAT_ANALMOVI
               (NRRELATANAL, CDTIPORETIAN, NMTIPORETIAN, IDENTRSAIDAN, CDTIPOOPERAN,
                IDSALDOESTAN, VRSALDOESTAN, QTSALDOESTAN)
            VALUES
               (P_SEQUMOVI, '-', '-', '-', '-', 'SA', 0, 0);

            INSERT INTO RELAT_ANALMOVI
               (NRRELATANAL, CDTIPORETIAN, NMTIPORETIAN, IDENTRSAIDAN, CDTIPOOPERAN,
                IDSALDOESTAN, VRSALDOESTAN, QTSALDOESTAN)
            VALUES
               (P_SEQUMOVI, '-', '-','-', '-', 'SI', 0, 0);

            INSERT INTO RELAT_ANALMOVI
               (NRRELATANAL, CDTIPORETIAN, NMTIPORETIAN, IDENTRSAIDAN, CDTIPOOPERAN,
                IDSALDOESTAN, VRSALDOESTAN, QTSALDOESTAN)
            VALUES
               (P_SEQUMOVI, '-', '-', '-', '-', 'SJ', 0, 0);

            INSERT INTO RELAT_ANALMOVI
               (NRRELATANAL, CDTIPORETIAN, NMTIPORETIAN, IDENTRSAIDAN, CDTIPOOPERAN,
                IDSALDOESTAN, VRSALDOESTAN, QTSALDOESTAN)
            (SELECT NRRELATMOVI, '-', '-', NVL(IDENTRSAIDME,'-'), NVL(CDTIPOOPERME,'-'),'SE', 0, 0
              FROM RELAT_MOVIEST
              WHERE IDTIPOMOVIEST   =  '2'       AND
                    NRRELATMOVI     =  P_SEQUMOVI
              GROUP BY NRRELATMOVI,  NVL(IDENTRSAIDME,'-'), NVL(CDTIPOOPERME,'-'));

              INSERT INTO RELAT_ANALMOVI
               (NRRELATANAL, CDTIPORETIAN, NMTIPORETIAN, IDENTRSAIDAN, CDTIPOOPERAN,
                IDSALDOESTAN, VRSALDOESTAN, QTSALDOESTAN)
               VALUES
               (P_SEQUMOVI, '-', '-', '-', '-', 'SP', 0, 0);

            INSERT INTO RELAT_ANALMOVI
               (NRRELATANAL, CDTIPORETIAN, NMTIPORETIAN, IDENTRSAIDAN, CDTIPOOPERAN,
                IDSALDOESTAN, VRSALDOESTAN, QTSALDOESTAN)
            (SELECT NRRELATMOVI, NVL(CDTIPORETIME,'-'), NVL(NMTIPORETIME,'-'),
                    '-', '-', 'SR', 0, 0
              FROM RELAT_MOVIEST
              WHERE IDTIPOMOVIEST = '3' AND --Retiradas de estoque
                    NRRELATMOVI  =  P_SEQUMOVI
              GROUP BY NRRELATMOVI, NVL(CDTIPORETIME,'-'), NVL(NMTIPORETIME,'-'));


            INSERT INTO RELAT_ANALMOVI
               (NRRELATANAL, CDTIPORETIAN, NMTIPORETIAN, IDENTRSAIDAN, CDTIPOOPERAN,
                IDSALDOESTAN, VRSALDOESTAN, QTSALDOESTAN)
            (SELECT NRRELATMOVI, '-', '-', NVL(IDENTRSAIDME,'-'), NVL(CDTIPOOPERME,'-'), 'SS', 0, 0
              FROM RELAT_MOVIEST
              WHERE (IDTIPOMOVIEST = '4' OR  IDTIPOMOVIEST = '5') AND -- 4>>Saidas de estoque - 5>>Devolucao ao fornecedor
                     NRRELATMOVI  =  P_SEQUMOVI
              GROUP BY NRRELATMOVI, NVL(IDENTRSAIDME,'-'), NVL(CDTIPOOPERME,'-'));

            INSERT INTO RELAT_ANALMOVI
               (NRRELATANAL, CDTIPORETIAN, NMTIPORETIAN,  IDENTRSAIDAN, CDTIPOOPERAN,
                IDSALDOESTAN, VRSALDOESTAN, QTSALDOESTAN)
            VALUES
               (P_SEQUMOVI, '-', '-', '-', '-', 'ST',0, 0);

            INSERT INTO RELAT_ANALMOVI
               (NRRELATANAL, CDTIPORETIAN, NMTIPORETIAN, IDENTRSAIDAN, CDTIPOOPERAN,
                IDSALDOESTAN, VRSALDOESTAN, QTSALDOESTAN)
            VALUES
               (P_SEQUMOVI, '-', '-', '-', '-', 'SL', 0, 0);

            INSERT INTO RELAT_ANALMOVI
               (NRRELATANAL, CDTIPORETIAN, NMTIPORETIAN, IDENTRSAIDAN, CDTIPOOPERAN,
                IDSALDOESTAN, VRSALDOESTAN, QTSALDOESTAN)
            VALUES
               (P_SEQUMOVI, '-', '-', '-', '-', 'SG', 0, 0);

         END IF;

         IF (C_TOTALMOVI.IDTIPOMOVIEST = '-') AND (C_TOTALMOVI.VRCUSTTOTAL <> 0)  THEN
            UPDATE RELAT_TOTAMOVI
            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
                  NRRELATTOTA    = P_SEQUMOVI;
         END IF;
         IF (C_TOTALMOVI.IDTIPOMOVIEST = '0') THEN --Implantacao de estoque
            UPDATE RELAT_TOTAMOVI
            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
                  NRRELATTOTA    = P_SEQUMOVI;
         END IF;

         IF (C_TOTALMOVI.IDTIPOMOVIEST = '1') AND (C_TOTALMOVI.VRCUSTTOTAL <> 0) THEN -- IDTIPOMOVIEST = '1' >> Ajuste de inventario
            UPDATE RELAT_TOTAMOVI
            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
                  NRRELATTOTA    = P_SEQUMOVI;
         END IF;

         IF (C_TOTALMOVI.IDTIPOMOVIEST = '2') THEN --IDTIPOMOVIEST = '2' >> Entradas no estoque
            UPDATE RELAT_TOTAMOVI
            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
                  NRRELATTOTA    = P_SEQUMOVI;
         END IF;

         IF (C_TOTALMOVI.IDTIPOMOVIEST = '3') AND (C_TOTALMOVI.VRCUSTTOTAL <> 0) THEN --IDTIPOMOVIEST = '3' >> Retiradas de estoque
            UPDATE RELAT_TOTAMOVI
            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
                  NRRELATTOTA    = P_SEQUMOVI;

         END IF;

         IF (C_TOTALMOVI.IDTIPOMOVIEST = '4') AND (C_TOTALMOVI.VRCUSTTOTAL <> 0) THEN --IDTIPOMOVIEST = '4' >> Saidas de estoque
            UPDATE RELAT_TOTAMOVI
            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
                  NRRELATTOTA    = P_SEQUMOVI;
         END IF;

         IF (C_TOTALMOVI.IDTIPOMOVIEST = '5') AND (C_TOTALMOVI.VRCUSTTOTAL <> 0) THEN --IDTIPOMOVIEST = '5' >> Devolucao ao fornecedor
            UPDATE RELAT_TOTAMOVI
            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
                  NRRELATTOTA    = P_SEQUMOVI;
         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
            SET VRSALDOESTAN     = VRSALDOESTAN + C_TOTALMOVI.VRCUSTTOTAL,
                QTSALDOESTAN     = QTSALDOESTAN + V_VRQTDETOTAL,
                IDSALDOESTAN     = 'SA'
            WHERE NRRELATANAL    = P_SEQUMOVI    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
            SET VRSALDOESTAN     = VRSALDOESTAN + C_TOTALMOVI.VRCUSTTOTAL,
                QTSALDOESTAN     = QTSALDOESTAN + V_VRQTDETOTAL,
                IDSALDOESTAN     = 'SI'
            WHERE NRRELATANAL    = P_SEQUMOVI    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
            SET VRSALDOESTAN     = VRSALDOESTAN + C_TOTALMOVI.VRCUSTTOTAL,
                QTSALDOESTAN     = QTSALDOESTAN + V_VRQTDETOTAL,
                IDSALDOESTAN     = 'SJ'
            WHERE NRRELATANAL    = P_SEQUMOVI    AND
                  IDSALDOESTAN  = 'SJ';

            UPDATE RELAT_ANALMOVI
               SET VRSALDOESTAN   = VRSALDOESTAN + C_TOTALMOVI.VRDIFCUSSAID
             WHERE NRRELATANAL  = P_SEQUMOVI 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
            SET VRSALDOESTAN     = VRSALDOESTAN + C_TOTALMOVI.VRCUSTTOTAL,
                QTSALDOESTAN     = QTSALDOESTAN + V_VRQTDETOTAL,
                IDSALDOESTAN     = 'SE'
            WHERE NRRELATANAL    = P_SEQUMOVI               AND
                  IDSALDOESTAN   = 'SE'                     AND
                  IDENTRSAIDAN   = C_TOTALMOVI.IDENTRSAIDME AND
                  CDTIPOOPERAN   = C_TOTALMOVI.CDTIPOOPERME;

             UPDATE RELAT_ANALMOVI
             SET VRSALDOESTAN   = VRSALDOESTAN + C_TOTALMOVI.VRDIFCUSSAID
             WHERE NRRELATANAL  = P_SEQUMOVI 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
            SET VRSALDOESTAN     = VRSALDOESTAN + C_TOTALMOVI.VRCUSTTOTAL,
                QTSALDOESTAN     = QTSALDOESTAN + V_VRQTDETOTAL,
                IDSALDOESTAN     = 'SR'
            WHERE NRRELATANAL    = P_SEQUMOVI               AND
                  IDSALDOESTAN   = 'SR'                     AND
                  CDTIPORETIAN   = C_TOTALMOVI.CDTIPORETIME;

            UPDATE RELAT_ANALMOVI
            SET VRSALDOESTAN   = VRSALDOESTAN + C_TOTALMOVI.VRDIFCUSSAID
            WHERE NRRELATANAL  = P_SEQUMOVI 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
            SET VRSALDOESTAN     = VRSALDOESTAN + C_TOTALMOVI.VRCUSTTOTAL,
                QTSALDOESTAN     = QTSALDOESTAN + V_VRQTDETOTAL,
                IDSALDOESTAN     = 'SS'
            WHERE NRRELATANAL    = P_SEQUMOVI               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
               SET VRSALDOESTAN   = VRSALDOESTAN + C_TOTALMOVI.VRDIFCUSSAID
               WHERE NRRELATANAL  = P_SEQUMOVI AND
                     IDSALDOESTAN = 'SP';

                V_VRSOMSATUAN    := V_VRSOMSATUAN  - C_TOTALMOVI.VRCUSTTOTAL + C_TOTALMOVI.VRDIFCUSSAID;
            ELSE
               UPDATE RELAT_ANALMOVI
               SET VRSALDOESTAN   = VRSALDOESTAN
               WHERE NRRELATANAL  = P_SEQUMOVI 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
         SET VRSALDOESTAN   = VRSALDOESTAN + V_VRSOMSATUAN,
             QTSALDOESTAN   = QTSALDOESTAN + V_VRQTDESATUAN,
             IDSALDOESTAN   = 'ST'
         WHERE NRRELATANAL  = P_SEQUMOVI AND
               IDSALDOESTAN = 'ST';

         -- Custo de Logistica
         IF ((C_TOTALMOVI.IDTIPOMOVIEST = '-') AND (C_TOTALMOVI.VRCUSTTOTAL = 0)) OR
            (C_TOTALMOVI.IDTIPOMOVIEST <> '-') THEN
            UPDATE RELAT_ANALMOVI
            SET VRSALDOESTAN   = VRSALDOESTAN + C_TOTALMOVI.VRCUSTLOGLME
            WHERE NRRELATANAL  = P_SEQUMOVI AND
                  IDSALDOESTAN = 'SL';
            -- Custo de Seguro
            UPDATE RELAT_ANALMOVI
            SET VRSALDOESTAN   = VRSALDOESTAN + C_TOTALMOVI.VRCUSTSEGLME
            WHERE NRRELATANAL  = P_SEQUMOVI 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
                         WHERE NRRELATTOTA    = P_SEQUMOVI) LOOP
         FOR C_DIFERENCA IN (SELECT CDPRODMOVIEST, DTLANCMOVIEST
                             FROM RELAT_MOVIEST
                             WHERE QTLANCAMENTO   <> 0                         AND
                                   IDTIPOMOVIEST  = '5'                        AND --Devolucao ao fornecedor
                                   CDTOTMOVIEST   = C_DEVOLUCAO.CDPRODTOTAMOVI AND
                                   QTDEMOVIEST    = 0                          AND
                                   NRRELATMOVI    =  P_SEQUMOVI
                             GROUP BY CDPRODMOVIEST, DTLANCMOVIEST) LOOP
            SELECT SUM(DECODE(VRDIFCUSTEME,0,0,(VRCUSTMEDI*QTLANCAMENTO)-(VRDIFCUSTEME*QTLANCAMENTO)))  AS VRDIFCUSSAID
            INTO  V_VRDIFCUSSAID
            FROM RELAT_MOVIEST
            WHERE QTLANCAMENTO   <> 0                         AND
                  IDTIPOMOVIEST  = '5'                        AND ----Devolucao ao fornecedor
                  CDTOTMOVIEST   = C_DEVOLUCAO.CDPRODTOTAMOVI AND
                  DTLANCMOVIEST  = C_DIFERENCA.DTLANCMOVIEST   AND
                  NRRELATMOVI    =  P_SEQUMOVI                AND
                  CDPRODMOVIEST  = C_DIFERENCA.CDPRODMOVIEST;

            UPDATE RELAT_TOTAMOVI
            SET VRSOMSATU    = VRSOMSATU + V_VRDIFCUSSAID,
                VRSOMDIFSAID = NVL(VRSOMDIFSAID,0) + V_VRDIFCUSSAID
            WHERE CDPRODTOTAMOVI = C_DEVOLUCAO.CDPRODTOTAMOVI AND
                  NRRELATTOTA    = P_SEQUMOVI;

            UPDATE RELAT_ANALMOVI
            SET VRSALDOESTAN   = VRSALDOESTAN + V_VRDIFCUSSAID
            WHERE NRRELATANAL  = P_SEQUMOVI AND
                  IDSALDOESTAN = 'SP';

         UPDATE RELAT_ANALMOVI
         SET VRSALDOESTAN   = VRSALDOESTAN + V_VRDIFCUSSAID
         WHERE NRRELATANAL  = P_SEQUMOVI AND
               IDSALDOESTAN = 'ST';
         END LOOP;
      END LOOP;

      INSERT INTO RELAT_TOTAMOVI
         (IDTIPOTOTAMOVI, CDPRODTOTAMOVI, VRSOMPINI, VRSOMAINV, VRSOMINVE,
          VRSOMENTR, VRSOMRETI, VRSOMSAID, VRSOMDFOR, VRSOMSATU, NRRELATTOTA,
          VRSOMCUSTLOG, VRSOMCUSTSEG)
         (SELECT 'T', 'TOTALGERAL', SUM(VRSOMPINI),
                 SUM(VRSOMAINV), SUM(VRSOMINVE), SUM(VRSOMENTR),
                 SUM(VRSOMRETI), SUM(VRSOMSAID), SUM(VRSOMDFOR),
                 SUM(VRSOMSATU), P_SEQUMOVI AS NRRELATTOTA,
                 SUM(VRSOMCUSTLOG),  SUM(VRSOMCUSTSEG)
          FROM RELAT_TOTAMOVI
          WHERE NRRELATTOTA = P_SEQUMOVI);
    END IF;
END;
/