CREATE OR REPLACE PROCEDURE APURAC_LOCAL(
   P_FILIAL    IN FILIAL.CDFILIAL%TYPE,
   P_FILIPROC  IN FILIAL.CDFILIAL%TYPE,
   P_CDCIA     IN COMPANHIA.CDCIA%TYPE,
   P_DTFECHAME IN DATE,
   P_DTINIAPUR IN DATE,
   P_DTFIMAPUR IN DATE,
   P_CDSERIE   IN SERIENF.CDSERIE%TYPE,
   P_IDTPIJURTRAN IN TRANSPORT.IDTPIJURTRAN%TYPE,
   P_NRINSJURTRAN IN TRANSPORT.NRINSJURTRAN%TYPE,
   P_NRAPURACFAT  IN NOTAFISCAL.NRAPURACFAT%TYPE)

AS

   V_NRSEQITPROD   NUMBER;

   V_CDCLIENTE     COMPANHIA.CDCLIENTE%TYPE;

   V_NRLANCTONF    VARCHAR(20);

   V_NRLANCTONFAP  VARCHAR(20);

   V_VRCOTDOLAR    TRAB_APURAC.VRCOTDOLARAP%TYPE;
   V_CDLOCFATCIAP  TRAB_APURAC.CDLOCFATCIAP%TYPE;
   V_CDFISCOPER    TRAB_APURAC.CDFISCOPERAP%TYPE;
   V_IDSERVICO     TRAB_APURAC.IDSERVICOAP%TYPE;

   V_CDEMPRESA     FILIAL.CDEMPRESA%TYPE;
   V_CDINSCMUNI    FILIAL.CDINSCMUNI%TYPE;
   V_CDINSCESTA    FILIAL.CDINSCESTA%TYPE;

   V_IDTPIJURCLIE  CLIENTE.IDTPIJURCLIE%TYPE;
   V_NRINSJURCLIE  CLIENTE.NRINSJURCLIE%TYPE;

   V_NOVANOTA      VARCHAR(1);

   V_VRDESCDEL     DELIVERY.VRDESCDEL%TYPE;
   V_VRDESCSRV     DELIVERY.VRDESCSRV%TYPE;

   V_VRINDICE      MOVINDICE.VRINDICE%TYPE;

   V_VRUNITITEM    ITEMPRODUTO.VRUNITITEM%TYPE;

   V_VRTAXAAERO    ITEMPRODUTO.VRUNITITEM%TYPE;
   V_VRTAXAAEME    ITEMPRODUTO.VRUNITITEM%TYPE;

   V_IDMUNIUTCONVNFE MUNICIPIO.IDMUNIUTCONVNFE%TYPE;
   V_IDEMPUTILIZANFE PARAMEMPRESA.IDEMPUTILIZANFE%TYPE;

   V_IDPRODSERVAP  TRAB_APURAC.IDPRODSERVAP%TYPE;

   V_COUNT_SERVICO NUMBER;
   
   V_CDTIPOOPERVENDA VARCHAR2(2);
BEGIN

   SELECT MAX(CDCLIENTE), MAX(CDTIPOOPERVENDA) 
     INTO V_CDCLIENTE, V_CDTIPOOPERVENDA
   FROM COMPANHIA
   WHERE CDCIA = P_CDCIA;
   
   IF V_CDTIPOOPERVENDA IS NULL THEN
      V_CDTIPOOPERVENDA := '01';
   END IF;

   SELECT CDEMPRESA, CDINSCMUNI, CDINSCESTA
   INTO V_CDEMPRESA, V_CDINSCMUNI, V_CDINSCESTA
   FROM FILIAL
   WHERE CDFILIAL = P_FILIAL;

   SELECT IDTPIJURCLIE, NRINSJURCLIE
   INTO V_IDTPIJURCLIE, V_NRINSJURCLIE
   FROM CLIENTE
   WHERE CDCLIENTE = V_CDCLIENTE;

  SELECT NVL(MI.IDMUNIUTCONVNFE, 'N') AS IDMUNIUTCONVNFE, NVL(PE.IDEMPUTILIZANFE, 'N') IDEMPUTILIZANFE
    INTO V_IDMUNIUTCONVNFE, V_IDEMPUTILIZANFE
    FROM EMPRESA EP, MUNICIPIO MI, PARAMEMPRESA PE
   WHERE EP.CDEMPRESA = V_CDEMPRESA
     AND EP.CDEMPRESA = PE.CDEMPRESA(+)
     AND EP.CDPAIS = MI.CDPAIS
     AND EP.SGESTADO = MI.SGESTADO
     AND EP.CDMUNICIPIO = MI.CDMUNICIPIO;

   V_NRLANCTONF   := 'X';
   V_CDFISCOPER   := 'X';
   V_CDLOCFATCIAP := 'X';
   V_IDSERVICO    := 'X';
   V_VRCOTDOLAR   := -1;

   V_NRSEQITPROD := 0;

   V_NOVANOTA  := 'X';

   FOR CURS_APCLASSE IN
      (SELECT CDCIAAP, NVL(CDLOCFATCIAP, '*') CDLOCFATCIAP, CDFISCOPERAP, CDSUFXCFOPAP,
              IDSERVICOAP, CDSERVICOAP, CDPRODUTOAP, DSSERVFATUAP, VRPUREASERVAP, VRPUDOLSERVAP,
              IDTIPMANUALAP, SUM(NRQTDESERVAP) NRTOTASERVAP,  NVL(SUM(VRDESCDELAP),0) VRDESCDELAP,
              VRCOTDOLARAP, NRORDEMIS, IDPRODSERVAP, QTPROPPRODINIAP, QTPROPPRODFINAP
       FROM TRAB_APURAC
       WHERE CDCIAAP       = P_CDCIA  AND
             CDFILIALAP    = P_FILIPROC AND
             NRAPURACFATAP = P_NRAPURACFAT

       GROUP BY CDCIAAP, CDLOCFATCIAP, NRORDEMIS, VRCOTDOLARAP, IDTIPMANUALAP, CDFISCOPERAP,
                CDSUFXCFOPAP, IDSERVICOAP, CDSERVICOAP, CDPRODUTOAP, DSSERVFATUAP, VRPUREASERVAP,
                VRPUDOLSERVAP, IDPRODSERVAP, QTPROPPRODINIAP, QTPROPPRODFINAP
       ORDER BY
          CDCIAAP, CDLOCFATCIAP, CDFISCOPERAP, CDSUFXCFOPAP,
          IDSERVICOAP, CDSERVICOAP, CDPRODUTOAP, DSSERVFATUAP, VRPUREASERVAP, VRPUDOLSERVAP,
          IDTIPMANUALAP, VRCOTDOLARAP, NRORDEMIS, IDPRODSERVAP) LOOP

      /*Se A Delivery for Nacional*/
      IF CURS_APCLASSE.IDTIPMANUALAP = '1' THEN

         /************************************************************************************************************************
            Gera uma Nova Nota fiscal em um dos casos:

              1. Estiver em um novo Local de Faturamento (No faturamento por Local, uma nova NF e gerada a cada local,
                 com os servicos do mesmo)
              2. Separa as NFs de Produto e de Servicos quando a empresa utilizar NF-E (Tela Parametros de DFS) e o municipio nao deve
                 da empresa nao deve utilizar convenio de NF-E (campo "Convenio NF-E" da tela "Municipio")

           *************************************************************************************************************************/
         IF (CURS_APCLASSE.CDLOCFATCIAP   <> V_CDLOCFATCIAP)OR
            ((CURS_APCLASSE.IDPRODSERVAP <> V_IDPRODSERVAP) AND (V_IDMUNIUTCONVNFE = 'N') AND
            (V_IDEMPUTILIZANFE = 'S')) THEN

            V_NOVANOTA  := ' ';

            /* a procedure novo_codigo gera o novo sequencial e tambem
               confirma a inclusao da nota anterior, pois existe um COMMIT
               dentro da novo_codigo */

            NOVO_CODIGO('LANCNF/FILIAL' || P_FILIPROC,V_NRLANCTONF);
            V_NRLANCTONF  := SUBSTR(V_NRLANCTONF,11, 10);

            V_NRSEQITPROD := 0;

            V_CDLOCFATCIAP := CURS_APCLASSE.CDLOCFATCIAP;
            V_IDPRODSERVAP := CURS_APCLASSE.IDPRODSERVAP;

            INSERT INTO NOTAFISCAL (CDFILIAL, NRLANCTONF, CDEMPRESA,
                                    CDINSCESTA, CDINSCMUNI, CDFILIEMIT,
                                    CDFILIRECE, IDTPIJURNF, NRINSJURNF,
                                    CDFORNECED, CDCLIENTE, IDERROS, IDAVISOS,
                                    IDBLOQUEADO, IDDOCFISCAL, IDDOCFINANC,
                                    NRNOTAFISC, DTENTRSAID,
                                    VRPRODUTOS, VRNOTAFISC, DTINIAPUFISC,
                                    DTAPROVACAO, CDOPERAPRO, DTCONTABIL,
                                    NRPLACAVEIC, SGUFVEIC, IDPGTOFRETE,
                                    DSQTDEVOLU, DSESPECVOLU, DSMARCAVOLU,
                                    DSNUMEVOLU, VRPBRUTOVOLU, VRPLIQUVOLU,
                                    IDENTRSAIDOP, CDTIPOOPER, CDSERIE, IDTPIJURTRAN,
                                    NRINSJURTRAN, VRTTMOEDESTR, IDSTATUS,
                                    IDSISTEMA, IDDOCFATUR, NRAPURACFAT, IDORIGDESTNF)
            VALUES (P_FILIPROC, V_NRLANCTONF, V_CDEMPRESA,
                    V_CDINSCESTA, V_CDINSCMUNI, P_FILIAL,
                    NULL, V_IDTPIJURCLIE, V_NRINSJURCLIE,
                    NULL, V_CDCLIENTE, NULL, NULL,
                    'N', 'N', 'N',
                    ' ', P_DTFECHAME,
                    0, 0, P_DTINIAPUR,
                    NULL, NULL, NULL,
                    NULL, NULL, NULL,
                    NULL, NULL, NULL,
                    NULL, 0, 0,
                    'S', V_CDTIPOOPERVENDA, P_CDSERIE, P_IDTPIJURTRAN,
                    P_NRINSJURTRAN, 0, 'P', 'FAC', 'S', P_NRAPURACFAT, 'C');
         END IF;

         V_NRSEQITPROD := V_NRSEQITPROD + 1;

         INSERT INTO ITEMPRODUTO (CDFILIAL, NRSEQITPROD,
                                  CDPRODUTO, CDPRODEMIS,
                                  DSPRODEMIS, IDATUVRESTOQ, QTITEMPROD,
                                  VRUNITITEM, VRDESCITEM, VRTOTAITEM,
                                  VRCONTITEM, VRBRUTITEM, VRITMOEDESTR,
                                  IDCONFORITEM, NRLANCTONF, CDFISOPEITEM,
                                  CDSUFXCFOPPR,
                                  IDERROSITEM, IDAVISOSITEM,
                                  QTPROPPRODINI, QTPROPPRODFIN)
         VALUES (P_FILIPROC, LPAD(TO_CHAR(V_NRSEQITPROD),4,'0'),
                 CURS_APCLASSE.CDPRODUTOAP, CURS_APCLASSE.IDTIPMANUALAP||CURS_APCLASSE.CDSERVICOAP,
                 CURS_APCLASSE.DSSERVFATUAP, 'N', CURS_APCLASSE.NRTOTASERVAP,
                 CURS_APCLASSE.VRPUREASERVAP, 0,
                 CURS_APCLASSE.NRTOTASERVAP * CURS_APCLASSE.VRPUREASERVAP,
                 CURS_APCLASSE.NRTOTASERVAP * CURS_APCLASSE.VRPUREASERVAP,
                 CURS_APCLASSE.NRTOTASERVAP * CURS_APCLASSE.VRPUREASERVAP, 0,
                 'N', V_NRLANCTONF, CURS_APCLASSE.CDFISCOPERAP,
                 CURS_APCLASSE.CDSUFXCFOPAP, '00000','00000',
                 CURS_APCLASSE.QTPROPPRODINIAP, CURS_APCLASSE.QTPROPPRODFINAP);

         UPDATE TRAB_APURAC
         SET NRLANCTONFAP = V_NRLANCTONF
         WHERE CDCIAAP        = CURS_APCLASSE.CDCIAAP        AND
               CDFILIALAP     = P_FILIPROC                   AND
               NRAPURACFATAP  = P_NRAPURACFAT                AND
               CDSERVICOAP    = CURS_APCLASSE.CDSERVICOAP    AND
               NVL(CDLOCFATCIAP, '*')  = CURS_APCLASSE.CDLOCFATCIAP AND
               CDPRODUTOAP    = CURS_APCLASSE.CDPRODUTOAP    AND
               IDTIPMANUALAP  = CURS_APCLASSE.IDTIPMANUALAP  AND
               DSSERVFATUAP   = CURS_APCLASSE.DSSERVFATUAP   AND
               (CDFISCOPERAP  = CURS_APCLASSE.CDFISCOPERAP  OR CURS_APCLASSE.CDFISCOPERAP IS NULL) AND
               (CDSUFXCFOPAP  = CURS_APCLASSE.CDSUFXCFOPAP  OR CURS_APCLASSE.CDSUFXCFOPAP IS NULL) AND
               (VRPUREASERVAP = CURS_APCLASSE.VRPUREASERVAP OR CURS_APCLASSE.VRPUREASERVAP IS NULL)AND
               NRLANCTONFAP IS NULL;

      /*Se A Delivery for Internacional (Faturamento em Dolar)*/
      ELSIF CURS_APCLASSE.IDTIPMANUALAP = '2' THEN

         /************************************************************************************************************************
            Gera uma Nova Nota fiscal em um dos casos:

              1. Estiver em um novo Local de Faturamento (No faturamento por Local, uma nova NF e gerada a cada local,
                 com os servicos do mesmo)
              2. Separa as NFs de Produto e de Servicos quando a empresa utilizar NF-E (Tela Parametros de DFS) e o municipio nao deve
                 da empresa nao deve utilizar convenio de NF-E (campo "Convenio NF-E" da tela "Municipio")

           *************************************************************************************************************************/
         IF (CURS_APCLASSE.CDLOCFATCIAP <> V_CDLOCFATCIAP) OR
            ((CURS_APCLASSE.IDPRODSERVAP <> V_IDPRODSERVAP) AND (V_IDMUNIUTCONVNFE = 'N') AND
            (V_IDEMPUTILIZANFE = 'S')) THEN

            V_NOVANOTA  := ' ';

            /* a procedure novo_codigo gera o novo sequencial e tambem
               confirma a inclusao da nota anterior, pois existe um COMMIT
               dentro da novo_codigo */

            NOVO_CODIGO('LANCNF/FILIAL' || P_FILIPROC,V_NRLANCTONF);
            V_NRLANCTONF  := SUBSTR(V_NRLANCTONF, 11, 10);

            V_NRSEQITPROD := 0;

            V_CDFISCOPER   := CURS_APCLASSE.CDFISCOPERAP;
            V_CDLOCFATCIAP := CURS_APCLASSE.CDLOCFATCIAP;
            V_VRCOTDOLAR   := CURS_APCLASSE.VRCOTDOLARAP;
            V_IDSERVICO    := CURS_APCLASSE.IDSERVICOAP;
            V_IDPRODSERVAP := CURS_APCLASSE.IDPRODSERVAP;

            INSERT INTO NOTAFISCAL (CDFILIAL, NRLANCTONF, CDEMPRESA,
                                    CDINSCESTA, CDINSCMUNI, CDFILIEMIT,
                                    CDFILIRECE, IDTPIJURNF, NRINSJURNF,
                                    CDFORNECED, CDCLIENTE, IDERROS, IDAVISOS,
                                    IDBLOQUEADO, IDDOCFISCAL, IDDOCFINANC,
                                    NRNOTAFISC, DTENTRSAID,
                                    VRPRODUTOS, VRNOTAFISC, DTINIAPUFISC,
                                    DTAPROVACAO, CDOPERAPRO, DTCONTABIL,
                                    NRPLACAVEIC, SGUFVEIC, IDPGTOFRETE,
                                    DSQTDEVOLU, DSESPECVOLU, DSMARCAVOLU,
                                    DSNUMEVOLU, VRPBRUTOVOLU, VRPLIQUVOLU,
                                    IDENTRSAIDOP, CDTIPOOPER, CDSERIE, IDTPIJURTRAN,
                                    NRINSJURTRAN, VRTTMOEDESTR, IDSTATUS,
                                    IDSISTEMA, IDDOCFATUR, NRAPURACFAT, IDORIGDESTNF)
            VALUES (P_FILIPROC, V_NRLANCTONF, V_CDEMPRESA,
                    V_CDINSCESTA, V_CDINSCMUNI, P_FILIAL,
                    NULL, V_IDTPIJURCLIE, V_NRINSJURCLIE,
                    NULL, V_CDCLIENTE, NULL, NULL,
                    'N', 'N', 'N',
                    ' ', P_DTFECHAME,
                    0, 0, P_DTINIAPUR,
                    NULL, NULL, NULL,
                    NULL, NULL, NULL,
                    NULL, NULL, NULL,
                    NULL, 0, 0,
                    'S', V_CDTIPOOPERVENDA, P_CDSERIE, P_IDTPIJURTRAN,
                    P_NRINSJURTRAN, 0, 'P', 'FAC', 'S', P_NRAPURACFAT, 'C');
         END IF;

         V_NRSEQITPROD := V_NRSEQITPROD + 1;

         V_VRINDICE := CURS_APCLASSE.VRCOTDOLARAP;

         IF V_VRINDICE IS NULL THEN
            raise_application_error(-20501, 'IndFi_NCad', True);
         END IF;

         V_VRUNITITEM := CURS_APCLASSE.VRPUDOLSERVAP * V_VRINDICE;

         INSERT INTO ITEMPRODUTO (CDFILIAL, NRSEQITPROD,
                                  CDPRODUTO, CDPRODEMIS,
                                  DSPRODEMIS, IDATUVRESTOQ, QTITEMPROD,
                                  VRUNITITEM, VRDESCITEM,
                                  VRTOTAITEM,
                                  VRCONTITEM, VRBRUTITEM, VRITMOEDESTR,
                                  IDCONFORITEM, NRLANCTONF, CDFISOPEITEM,
                                  CDSUFXCFOPPR, IDERROSITEM, IDAVISOSITEM,
                                  QTPROPPRODINI, QTPROPPRODFIN)
         VALUES (P_FILIPROC, LPAD(TO_CHAR(V_NRSEQITPROD),4,'0'),
                 CURS_APCLASSE.CDPRODUTOAP, CURS_APCLASSE.IDTIPMANUALAP||CURS_APCLASSE.CDSERVICOAP,
                 CURS_APCLASSE.DSSERVFATUAP, 'N', CURS_APCLASSE.NRTOTASERVAP,
                 V_VRUNITITEM, 0,
                 ROUND(CURS_APCLASSE.NRTOTASERVAP * V_VRUNITITEM,2),
                 ROUND(CURS_APCLASSE.NRTOTASERVAP * V_VRUNITITEM,2),
                 ROUND(CURS_APCLASSE.NRTOTASERVAP * V_VRUNITITEM,2),
                 ROUND(CURS_APCLASSE.NRTOTASERVAP * CURS_APCLASSE.VRPUDOLSERVAP,2),
                 'N', V_NRLANCTONF, CURS_APCLASSE.CDFISCOPERAP,
                 CURS_APCLASSE.CDSUFXCFOPAP, '00000','00000',
                 CURS_APCLASSE.QTPROPPRODINIAP, CURS_APCLASSE.QTPROPPRODFINAP);

         UPDATE TRAB_APURAC
         SET NRLANCTONFAP = V_NRLANCTONF
         WHERE CDCIAAP        = CURS_APCLASSE.CDCIAAP       AND
               CDFILIALAP     = P_FILIPROC                  AND
               NRAPURACFATAP  = P_NRAPURACFAT               AND
               CDSERVICOAP    = CURS_APCLASSE.CDSERVICOAP   AND
               NVL(CDLOCFATCIAP, '*')  = CURS_APCLASSE.CDLOCFATCIAP  AND
               CDPRODUTOAP    = CURS_APCLASSE.CDPRODUTOAP   AND
               IDTIPMANUALAP  = CURS_APCLASSE.IDTIPMANUALAP AND
               DSSERVFATUAP   = CURS_APCLASSE.DSSERVFATUAP  AND
               (VRCOTDOLARAP  = CURS_APCLASSE.VRCOTDOLARAP  OR CURS_APCLASSE.VRCOTDOLARAP IS NULL)         AND
               (CDFISCOPERAP  = CURS_APCLASSE.CDFISCOPERAP  OR CURS_APCLASSE.CDFISCOPERAP IS NULL)         AND
               (CDSUFXCFOPAP  = CURS_APCLASSE.CDSUFXCFOPAP  OR CURS_APCLASSE.CDSUFXCFOPAP IS NULL)         AND
               (VRPUDOLSERVAP = CURS_APCLASSE.VRPUDOLSERVAP OR CURS_APCLASSE.VRPUDOLSERVAP IS NULL)        AND
               NRLANCTONFAP IS NULL;
      END IF;
   END LOOP;

   FOR C_TAXAAEROPORTO IN
     (SELECT CDCIAAP, NVL(CDLOCFATCIAP, '*') AS CDLOCFATCIAP, IDTIPMANUALAP, SUM(NVL(VRTXAERORAP, 0)) AS VRTXAERORAP, SUM(NVL(VRTXAERODAP, 0)) AS VRTXAERODAP
        FROM TRAB_APURAC
       WHERE CDCIAAP = P_CDCIA
         AND CDFILIALAP = P_FILIPROC
         AND NRAPURACFATAP = P_NRAPURACFAT
      GROUP BY CDCIAAP, CDLOCFATCIAP, IDTIPMANUALAP) LOOP

      /*-----------------------------------------------
         Dentre as NFs geradas durante a apuracao,
         busca a mais recente que contiver "Servicos"
         (Handling, por exemplo).
      -----------------------------------------------*/

      SELECT COUNT(1)
        INTO V_COUNT_SERVICO
        FROM TRAB_APURAC
       WHERE CDCIAAP = C_TAXAAEROPORTO.CDCIAAP
         AND CDLOCFATCIAP = C_TAXAAEROPORTO.CDLOCFATCIAP
         AND NRAPURACFATAP = P_NRAPURACFAT
         AND IDPRODSERVAP = 'S';

      /*************************************************************************
         se a variavel retornar um valor maior que zero, nao foi gerada nenhuma
         NF com servicos, portanto uma nova NF de servicos deve ser criada para
         a inclusao da taxa de aeroporto.
      **************************************************************************/
      IF (V_COUNT_SERVICO > 0) THEN

         NOVO_CODIGO('LANCNF/FILIAL' || P_FILIPROC,V_NRLANCTONF);
         V_NRLANCTONF  := SUBSTR(V_NRLANCTONF, 11, 10);

         INSERT INTO NOTAFISCAL (CDFILIAL, NRLANCTONF, CDEMPRESA,
                                 CDINSCESTA, CDINSCMUNI, CDFILIEMIT,
                                 CDFILIRECE, IDTPIJURNF, NRINSJURNF,
                                 CDFORNECED, CDCLIENTE, IDERROS, IDAVISOS,
                                 IDBLOQUEADO, IDDOCFISCAL, IDDOCFINANC,
                                 NRNOTAFISC, DTENTRSAID,
                                 VRPRODUTOS, VRNOTAFISC, DTINIAPUFISC,
                                 DTAPROVACAO, CDOPERAPRO, DTCONTABIL,
                                 NRPLACAVEIC, SGUFVEIC, IDPGTOFRETE,
                                 DSQTDEVOLU, DSESPECVOLU, DSMARCAVOLU,
                                 DSNUMEVOLU, VRPBRUTOVOLU, VRPLIQUVOLU,
                                 IDENTRSAIDOP, CDTIPOOPER, CDSERIE, IDTPIJURTRAN,
                                 NRINSJURTRAN, VRTTMOEDESTR, IDSTATUS,
                                 IDSISTEMA, IDDOCFATUR, NRAPURACFAT, IDORIGDESTNF)
         VALUES (P_FILIPROC, V_NRLANCTONF, V_CDEMPRESA,
                 V_CDINSCESTA, V_CDINSCMUNI, P_FILIAL,
                 NULL, V_IDTPIJURCLIE, V_NRINSJURCLIE,
                 NULL, V_CDCLIENTE, NULL, NULL,
                 'N', 'N', 'N',
                 ' ', P_DTFECHAME,
                 0, 0, P_DTINIAPUR,
                 NULL, NULL, NULL,
                 NULL, NULL, NULL,
                 NULL, NULL, NULL,
                 NULL, 0, 0,
                 'S', V_CDTIPOOPERVENDA, P_CDSERIE, P_IDTPIJURTRAN,
                 P_NRINSJURTRAN, 0, 'P', 'FAC', 'S', P_NRAPURACFAT, 'C');

         /* Calcula a Taxa de Aeroporto na nova Nota Fiscal de Servicos*/
         CALCULA_TAXAAEROPORTO(P_FILIAL, P_FILIPROC, V_NRLANCTONF, C_TAXAAEROPORTO.IDTIPMANUALAP, C_TAXAAEROPORTO.VRTXAERORAP, C_TAXAAEROPORTO.VRTXAERODAP);

         /*APURAC_FECH(P_FILIAL, P_FILIPROC, V_NRLANCTONF, P_CDCIA, P_DTFECHAME, P_DTINIAPUR, P_DTFIMAPUR);*/

         APURAC_DADOSADIC(P_FILIAL, P_FILIPROC, V_NRLANCTONF, P_CDCIA, 'T','T','T',NULL,'T',C_TAXAAEROPORTO.CDLOCFATCIAP,'T');

      ELSE
         SELECT MAX(NRLANCTONFAP)
          INTO V_NRLANCTONFAP
          FROM TRAB_APURAC
         WHERE CDCIAAP = C_TAXAAEROPORTO.CDCIAAP
           AND NVL(CDLOCFATCIAP, '*') = C_TAXAAEROPORTO.CDLOCFATCIAP
           AND NRAPURACFATAP = P_NRAPURACFAT
           AND IDPRODSERVAP = 'P';

         IF (V_NRLANCTONFAP  IS not NULL) THEN

            /*CALCULA_DESCONTOS(P_FILIAL, P_FILIPROC, V_NRLANCTONFAP, P_CDCIA, P_NRAPURACFAT);*/

            /* Calcula a Taxa de Aeroporto na NF de servicos atual */
            CALCULA_TAXAAEROPORTO(P_FILIAL, P_FILIPROC, V_NRLANCTONFAP, C_TAXAAEROPORTO.IDTIPMANUALAP, C_TAXAAEROPORTO.VRTXAERORAP, C_TAXAAEROPORTO.VRTXAERODAP);

            /*APURAC_FECH(P_FILIAL, P_FILIPROC, V_NRLANCTONFAP, P_CDCIA, P_DTFECHAME, P_DTINIAPUR, P_DTFIMAPUR);*/

         END IF;
      END IF;
   END LOOP;

   FOR C_CALCULANOTAPROD IN
      (SELECT NRLANCTONFAP, SUM(NVL(VRTXAERORAP, 0)) AS VRTXAERORAP, SUM(NVL(VRTXAERODAP, 0)) AS VRTXAERODAP
         FROM TRAB_APURAC
        WHERE CDCIAAP = P_CDCIA
          AND CDFILIALAP = P_FILIPROC
          AND NRAPURACFATAP = P_NRAPURACFAT
        GROUP BY NRLANCTONFAP) LOOP

      CALCULA_DESCONTOS(P_FILIAL, P_FILIPROC, C_CALCULANOTAPROD.NRLANCTONFAP, P_CDCIA, P_NRAPURACFAT);

      APURAC_FECH(P_FILIAL, P_FILIPROC, C_CALCULANOTAPROD.NRLANCTONFAP, P_CDCIA, P_DTFECHAME, P_DTINIAPUR, P_DTFIMAPUR);

   END LOOP;

END;
/