CREATE OR REPLACE PROCEDURE GRAVA_RELAT_MOVIEST_ANALI(
   P_CDFILIAL        IN ITLANCTOEST.CDFILIMOVI%TYPE,
   P_CDPRODUTO       IN ITLANCTOEST.CDPRODUTO%TYPE,
   P_CDARVPROD       IN PRODUTO.CDARVPROD%TYPE,
   P_NRLOTEESTQ      IN ITLANCTOEST.NRLOTEESTQ%TYPE,
   P_NRSUBLOTE       IN ITLANCTOEST.NRSUBLOTE%TYPE,
   P_DTLANCAMEN      IN ITLANCTOEST.DTLANCMOVI%TYPE,
   P_IDTIPOLANC      IN ITLANCTOEST.IDTIPOMOVI%TYPE,
   P_CDFILLANC       IN ITLANCTOEST.CDFILIAL%TYPE,
   P_NRLANCAMEN      IN ITLANCTOEST.NRLANCESTQ%TYPE,
   P_NRSEQULANC      IN ITLANCTOEST.NRSEQUITEM%TYPE,
   P_QTDEESTO        IN ITLANCTOEST.QTLANCTOEST%TYPE,
   P_QTLANCAMEN      IN ITLANCTOEST.QTLANCTOEST%TYPE,
   P_IDALTCMEDMV     IN ITLANCTOEST.IDALTCMEDMV%TYPE,
   P_QTLANCTOEST     IN ITLANCTOEST.QTLANCTOEST%TYPE,
   P_IDORDEMCLC      IN ITLANCTOEST.IDORDEMCLC%TYPE,
   P_QTTOTALANTE     IN ITLANCTOEST.QTLANCTOEST%TYPE,
   P_CDALMOXARIFE    IN ITLANCTOEST.CDALMOXARIFE%TYPE,
   P_CDLOCALESTOQ    IN ITLANCTOEST.CDLOCALESTOQ%TYPE,
   P_CDTIPORETIME    IN ITLANCTOEST.CDTIPORETI%TYPE,
   P_IDENTRSAIDME    IN TIPOOPERAC.IDENTRSAIDOP%TYPE,
   P_CDTIPOOPERME    IN TIPOOPERAC.CDTIPOOPER%TYPE, -- novo daqui pra baixo
   P_CUSTMED         IN ITLANCTOEST.VRLANCTOEST%TYPE,
   P_CUSTESTO        IN ITLANCTOEST.VRLANCTOEST%TYPE,
   P_CUSTBRUT        IN ITLANCTOEST.VRLANCTOEST%TYPE,
   P_CUSTMEES        IN ITLANCTOEST.VRLANCTOEST%TYPE,
   P_CUSTMEBR        IN ITLANCTOEST.VRLANCTOEST%TYPE,
   P_VRLANCTOEST     IN ITLANCTOEST.VRLANCTOEST%TYPE,
   P_VRLANCTOBRUT    IN ITLANCTOEST.VRLANCTOEST%TYPE,
   P_VRUNILANCTO     IN ITLANCTOEST.VRLANCTOEST%TYPE,
   P_VRTOTLANCTO     IN ITLANCTOEST.VRLANCTOEST%TYPE,
   P_VRTOTALANTE     IN ITLANCTOEST.VRLANCTOEST%TYPE,
   P_VRCUSTENTRME    IN ITLANCTOEST.VRLANCTOEST%TYPE,
   P_VRCUSTLOGLME    IN ITLANCTOEST.VRCUSTLOGLAN%TYPE,
   P_VRCUSTSEGLME    IN ITLANCTOEST.VRSEGULOGLAN%TYPE,
   P_VRCUSTLOGEME    IN ITLANCTOEST.VRCUSTLOGLAN%TYPE,
   P_VRCUSTSEGEME    IN ITLANCTOEST.VRSEGULOGLAN%TYPE,
   P_VRPESOBRUTME    IN PRODUTO.VRPESOBRUTPR%TYPE,
   P_NVPROD          IN PRODUTO.CDARVPROD%TYPE,
   P_NMPRODUTO       IN PRODUTO.NMPRODUTO%TYPE,
   P_SGUNIDADE       IN PRODUTO.SGUNIDADE%TYPE,
   P_NMTIPORETIME    IN TIPORETI.NMTIPORETI%TYPE)
IS
   V_NRNOTAFISC VARCHAR2(20);
BEGIN
   IF P_IDTIPOLANC IN ('2','4','5') THEN
      BEGIN
         SELECT N.NRNOTAFISC
           INTO V_NRNOTAFISC
           FROM LANCTOESTOQ L, NOTAFISCAL N
          WHERE L.CDFILIAL   = P_CDFILLANC
            AND L.NRLANCESTQ = P_NRLANCAMEN
            AND N.CDFILIAL   = L.CDFILIAL
            AND N.NRLANCTONF = L.NRLANCTONF;
      EXCEPTION
         WHEN NO_DATA_FOUND THEN BEGIN
            V_NRNOTAFISC := NULL;
         END;
      END;    
   END IF;

   INSERT INTO RELAT_MOVIEST_ANALI
      (CDFILIMOVIEST, CDPRODUTO,     CDPRODMOVIEST, NRLOTEESTQME,  NRSUBLOTEME,
       DTLANCMOVIEST, IDTIPOMOVIEST, CDFILLANCMEST, NRLANCMOVIEST, NRSEQUMOVIEST,
       QTDEMOVIEST,   QTLANCAMENTO,  IDALTCMEDMOV,  QTLANCMOVIEST, IDORDEMCALC,
       QTTOTALANT,    CDALMOXARIRM,  CDLOCALESTRM,  CDTIPORETIME,
       IDENTRSAIDME,  CDTIPOOPERME,  VRCUSTMEDI,    VRCUSTMOVIEST, VRCUSTBRUT,
       VRCUSTMEES,    VRCUSTMEBR,    VRLANCMOVIEST, VRBRUTMOVIEST, VRUNILANCEST,
       VRTOTLANCEST,  VRTOTALANT,    VRCUSTENTRME,  VRCUSTLOGLME,  VRCUSTSEGLME,
       VRCUSTLOGEME,  VRCUSTSEGEME,  VRPESOBRUTME,  CDTOTMOVIEST,  NMPRODMOVIEST,
       SGUNIDMOVI,    NMTIPORETIME,  NRNOTAFISCEST)
   VALUES
      (P_CDFILIAL,     P_CDPRODUTO,    P_CDARVPROD,    P_NRLOTEESTQ,   P_NRSUBLOTE,
       P_DTLANCAMEN,   P_IDTIPOLANC,   P_CDFILLANC,    P_NRLANCAMEN,   P_NRSEQULANC,
       P_QTDEESTO,     P_QTLANCAMEN,   P_IDALTCMEDMV,  P_QTLANCTOEST,  P_IDORDEMCLC,
       P_QTTOTALANTE,  P_CDALMOXARIFE, P_CDLOCALESTOQ, P_CDTIPORETIME,
       P_IDENTRSAIDME, P_CDTIPOOPERME, P_CUSTMED,      P_CUSTESTO,     P_CUSTBRUT,
       P_CUSTMEES,     P_CUSTMEBR,     P_VRLANCTOEST,  P_VRLANCTOBRUT, P_VRUNILANCTO,
       P_VRTOTLANCTO,  P_VRTOTALANTE,  P_VRCUSTENTRME, P_VRCUSTLOGLME, P_VRCUSTSEGLME,
       P_VRCUSTLOGEME, P_VRCUSTSEGEME, P_VRPESOBRUTME, P_NVPROD,       P_NMPRODUTO,
       P_SGUNIDADE,    P_NMTIPORETIME, V_NRNOTAFISC);
END;
/