CREATE OR REPLACE PROCEDURE GERA_FECHAMENTO_NFENTRADA
   (P_CDFILIAL   IN NOTAFISCAL.CDFILIAL%TYPE,
    P_NRLANCTONF IN NOTAFISCAL.NRLANCTONF%TYPE,
    P_DATAPROC   IN DATE                        DEFAULT NULL,
    P_NRLANCESTQ IN LANCTOESTOQ.NRLANCESTQ%TYPE DEFAULT NULL,
    P_CDOPERADOR IN LANCTOESTOQ.CDOPERADOR%TYPE DEFAULT NULL)

AS

   V_CDEMPRESA     VARCHAR2(02);
   V_CDINSCESTA    VARCHAR2(16);
   V_CDTIPOOPER    VARCHAR2(02);
   V_CDCLIENTE     VARCHAR2(21);
   V_CDFORNECED    VARCHAR2(21);
   V_CDFILIRECE    VARCHAR2(04);
   V_CLIENTENF     VARCHAR2(21);
   V_ORIGDESTNF    VARCHAR2(01);
   V_CDFILIEMIT    VARCHAR2(04);
   V_IDGERAVENC    VARCHAR2(01);
   V_IDUTILVENC    VARCHAR2(01);

   V_DTINICIAL     DATE;
   V_DTFINAL       DATE;
   V_DTENTRSAID    DATE;
   V_VRNOTAFISC    NUMBER;
   V_VRNOTAPAINEL  NUMBER;
   V_VRLIQUIDO     NUMBER;
   V_DIFERENCA     NUMBER;
   V_VRIMPOSTOICST NUMBER;
   V_CDENCARGOST   VARCHAR2(02);
   V_IDSTATUS      VARCHAR2(01);
   V_IDPERLANESTAPNF VARCHAR2(01);
   V_IDNFGERADAVIAXML VARCHAR2(01);

   V_NRLANCTO      VARCHAR2(10);
   P_RESULTADO      VARCHAR2(50);
BEGIN
   V_VRIMPOSTOICST := 0;
   
   SELECT MAX(N.CDEMPRESA),    MAX(N.CDINSCESTA),   MAX(N.CDTIPOOPER),
          MAX(N.CDCLIENTE),    MAX(N.CDFORNECED),   MAX(N.CDFILIRECE),
          MAX(N.DTENTRSAID),   MAX(N.IDORIGDESTNF), MAX(N.CDFILIEMIT),
          NVL(MAX(T.IDGERAVENC), 'S'),
          NVL(MAX(T.IDUTILVENC), 'S'),
          NVL(MAX(IDSTATUS), 'P'),
          MAX(VRNOTAFISC),
          MAX(IDNFGERADAVIAXML)
   INTO   V_CDEMPRESA,    V_CDINSCESTA,   V_CDTIPOOPER,
          V_CDCLIENTE,    V_CDFORNECED,   V_CDFILIRECE,
          V_DTENTRSAID,   V_ORIGDESTNF,   V_CDFILIEMIT,
          V_IDGERAVENC,   V_IDUTILVENC,   V_IDSTATUS,
          V_VRNOTAPAINEL, V_IDNFGERADAVIAXML
   FROM NOTAFISCAL N, TIPOOPERAC T
   WHERE N.CDFILIAL     = P_CDFILIAL     AND
         N.NRLANCTONF   = P_NRLANCTONF   AND
         T.IDENTRSAIDOP = N.IDENTRSAIDOP AND
         T.CDTIPOOPER   = N.CDTIPOOPER;

   IF V_ORIGDESTNF = 'C' THEN
      V_CLIENTENF := V_CDCLIENTE;
   ELSIF V_ORIGDESTNF = 'L' THEN
      V_CLIENTENF := V_CDFILIRECE;
   ELSE
      V_CLIENTENF := V_CDFORNECED;
   END IF;

   INSERT INTO ITEMENCARGO
      (CDFILIAL, NRLANCTONF, CDENCARGO, VRITEMENCA)
      (SELECT P_CDFILIAL, P_NRLANCTONF, CDENCARGO, 0
         FROM ENCARGO
        WHERE CDENCARGO NOT IN (SELECT CDENCARGO
                                  FROM ITEMENCARGO
                                 WHERE CDFILIAL   = P_CDFILIAL
                                   AND NRLANCTONF = P_NRLANCTONF));

   BEGIN
      SELECT IE.CDENCARGOST
        INTO V_CDENCARGOST
        FROM NOTAFISCAL NF INNER JOIN INSCRESTAD IE ON NF.CDEMPRESA  = IE.CDEMPRESA
                                                   AND NF.CDINSCESTA = IE.CDINSCESTA
       WHERE NF.CDFILIAL   = P_CDFILIAL
         AND NF.NRLANCTONF = P_NRLANCTONF
         AND IE.IDSUGREGSTENTR = 'N';
   EXCEPTION
      WHEN NO_DATA_FOUND THEN BEGIN
         V_CDENCARGOST  := NULL;
      END;
   END;

   IF V_CDENCARGOST IS NOT NULL THEN
      BEGIN
         SELECT NVL(SUM(I.VRIMPOSTOIT),0)
           INTO V_VRIMPOSTOICST
           FROM ITEMPROIMPOS I INNER JOIN IMPOSTO M ON I.CDIMPOSTO = M.CDIMPOSTO
          WHERE I.CDFILIAL||I.NRLANCTONF = (SELECT DSNOTAFISORIGOL
                                              FROM NOTAFISCAL
                                             WHERE CDFILIAL   = P_CDFILIAL
                                               AND NRLANCTONF = P_NRLANCTONF)
            AND M.CDIMPOSTONFE = '80';
      EXCEPTION
         WHEN NO_DATA_FOUND THEN BEGIN
            V_VRIMPOSTOICST  := 0;
         END;
      END;

      UPDATE ITEMENCARGO
         SET VRITEMENCA = VRITEMENCA + V_VRIMPOSTOICST
       WHERE CDFILIAL   = P_CDFILIAL
         AND NRLANCTONF = P_NRLANCTONF
         AND CDENCARGO  = V_CDENCARGOST;
   END IF;

   IMPOSTO_ENTRADA(V_CDEMPRESA, V_CDINSCESTA, V_CDTIPOOPER, V_CLIENTENF,
                   P_CDFILIAL,  P_NRLANCTONF, V_DTENTRSAID, V_VRNOTAFISC,
                   P_NRLANCESTQ, 'E', P_CDOPERADOR);

   GRAVA_IMPOSTO(P_CDFILIAL,  P_NRLANCTONF);

   SELECT MAX(NRLANCTONF)
     INTO V_NRLANCTO
     FROM CONTAPAGAR
    WHERE CDFILIAL   = P_CDFILIAL
      AND NRLANCTONF = P_NRLANCTONF;

   IF (V_IDSTATUS IN ('I', 'L')) OR (V_IDNFGERADAVIAXML = 'S') THEN
      V_VRNOTAFISC := V_VRNOTAPAINEL;
   END IF;
   
   SELECT NVL(SUM(VRTOTAITEM - VRDESCITEM), 0) AS VRLIQUIDO
   INTO V_VRLIQUIDO
   FROM ITEMPRODUTO
   WHERE CDFILIAL   = P_CDFILIAL AND
         NRLANCTONF = P_NRLANCTONF;

   UPDATE NOTAFISCAL
   SET VRNOTAFISC = V_VRNOTAFISC,
       VRPRODUTOS = V_VRLIQUIDO
   WHERE
      CDFILIAL   = P_CDFILIAL AND
      NRLANCTONF = P_NRLANCTONF;

   IF (V_IDGERAVENC = 'S') AND (V_IDUTILVENC = 'S') AND (V_NRLANCTO IS NULL) THEN
      GERA_DADOS_FINANCEIRO(P_CDFILIAL, P_NRLANCTONF, P_CDFILIAL, 'L', P_RESULTADO);
   END IF;

   IF P_NRLANCESTQ IS NOT NULL THEN

      CUSTO_LIQUIDO(P_CDFILIAL, P_NRLANCESTQ, V_VRLIQUIDO, 0, 'N');

      IF V_DTENTRSAID < P_DATAPROC THEN
         V_DTINICIAL := V_DTENTRSAID;
      ELSE
         V_DTINICIAL := P_DATAPROC;
      END IF;

      IF V_DTENTRSAID > P_DATAPROC THEN
         V_DTFINAL := V_DTENTRSAID;
      ELSE
         V_DTFINAL := P_DATAPROC;
      END IF;

      BEGIN
         SELECT NVL(IDPERLANESTAPNF, 'N') AS IDPERLANESTAPNF
           INTO V_IDPERLANESTAPNF
           FROM PARAMGERAL;
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_IDPERLANESTAPNF := 'N';
      END;

      IF (V_IDSTATUS = 'I') OR (V_IDSTATUS = 'L') OR (V_IDSTATUS = 'D') OR (V_IDPERLANESTAPNF = 'S') THEN
         UPDATE ITLANCTOEST
            SET IDEVOLESTOQ  = 'N'
          WHERE CDFILIAL     = P_CDFILIAL
            AND NRLANCESTQ   = P_NRLANCESTQ;
      END IF;
      ATUALIZA_ESTOQUE(V_CDFILIRECE, V_DTINICIAL, V_DTFINAL, P_CDFILIAL, P_NRLANCESTQ);
   END IF;
END;
/

