CREATE OR REPLACE PROCEDURE APURAC_NF(
   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_NRVOO         IN VARCHAR2 DEFAULT 'T',
   P_IDAGRUPSERV   IN SERVPLANO.IDTIPSCBOSP%TYPE DEFAULT 'N',
   P_NRAPURACFAT   IN NOTAFISCAL.NRAPURACFAT%TYPE,
   P_GRUPPROD      IN GRUPPROD.CDGRUPPROD%TYPE DEFAULT NULL,
   P_IDGRUPTAXA    IN SERVPLANO.IDTIPSCBOSP%TYPE DEFAULT 'N',
   P_CDLOCAFATCIA  IN DELIVERY.CDLOCAFATCIA%TYPE,
   P_LISTANF       IN VARCHAR DEFAULT NULL)
AS

   V_SEQAPUR         NUMBER;

   V_CDTIPEMNF       CIAFILI.CDTIPEMNF%TYPE;

   V_INSCESTA        FILIAL.CDINSCESTA%TYPE;
   V_CDEMPRESA       FILIAL.CDEMPRESA%TYPE;
   V_CDCLIENTE       COMPANHIA.CDCLIENTE%TYPE;
   V_PAISORIG        PAIS.CDPAIS%TYPE;
   V_ESTAORIG        ESTADO.SGESTADO%TYPE;
   V_PAISDEST        PAIS.CDPAIS%TYPE;
   V_ESTADEST        ESTADO.SGESTADO%TYPE;

   V_VRDESCDEL       DELIVERY.VRDESCDEL%TYPE;
   V_VRDESCSRV       DELIVERY.VRDESCSRV%TYPE;
   V_VRDESCDESVAP    TRAB_APURAC.VRDESCDESVAP%TYPE;
   V_VRDESCFIXOAP    TRAB_APURAC.VRDESCFIXOAP%TYPE;
   V_VRDESCCONDAP    TRAB_APURAC.VRDESCCONDAP%TYPE;
   V_VRDESCDESVSEAP  TRAB_APURAC.VRDESCDESVSEAP%TYPE;
   V_VRDESCFIXOSEAP  TRAB_APURAC.VRDESCFIXOSEAP%TYPE;
   V_VRDESCCONDSEAP  TRAB_APURAC.VRDESCCONDSEAP%TYPE;

   V_VRTXAERORAP     TRAB_APURAC.VRTXAERORAP%TYPE;
   V_VRTXAERODAP     TRAB_APURAC.VRTXAERODAP%TYPE;

   V_VRSTANDBY       TRAB_APURAC.VRPUDOLSERVAP%TYPE;
   V_VRSTANDBYD      TRAB_APURAC.VRPUDOLSERVAP%TYPE;
   V_QTSTANDBY       SERVPLANO.QTSESBYUTSP%TYPE;

   V_VRINDICE        MOVINDICE.VRINDICE%TYPE;

   V_IDPRODSERVAP    TRAB_APURAC.IDPRODSERVAP%TYPE;
   V_COUNTISS        NUMBER;

   V_CDGRUPOFISC     VARCHAR2(03);

   V_VRPRUDOLSESP    SERVPLANO.VRPRUDOLSESP%TYPE;
   V_VRPRURESESP     SERVPLANO.VRPRURESESP%TYPE;

   V_NRTABPRECO      TABPRECOSE.NRTABPRECO%TYPE;
   V_NRSEQPRECO      PRECOSECIA.NRSEQPRECO%TYPE;
   V_QTPAXINICIAL    PROPRECOSE.QTPAXINICIAL%TYPE;
   V_QTPAXFINAL      PROPRECOSE.QTPAXFINAL%TYPE;
   
   V_CDTIPOOPERDEVOL VARCHAR2(2);
   V_QTPRODNF NUMBER;
   V_SEQUENCIAL VARCHAR2(20);
   V_NRLANCTONF VARCHAR2(10);
   V_NRSEQITPROD NUMBER;
   V_CDMODDOCFISC VARCHAR2(2);   
   V_CUSTOPRODUTO FLOAT;
   V_VRNOTA FLOAT;
BEGIN
   V_SEQAPUR := 1;

   SELECT NVL(MAX(F.IDTIPEMNF),SUBSTR(MAX(C.CDTIPEMNF),3,4)), MAX(CO.CDCLIENTE), MAX(CO.CDTIPOOPERDEVOL)
   INTO V_CDTIPEMNF, V_CDCLIENTE, V_CDTIPOOPERDEVOL
   FROM CIAFILI C, COMPANHIA CO, FORMEMISNF F
   WHERE (C.CDFILIAL  = P_FILIAL) AND
         (C.CDCIA     = P_CDCIA)  AND
         (CO.CDCIA    = P_CDCIA)  AND
         (C.CDTIPEMNF = F.CDTIPEMNF (+));

   DELETE INCITEMP
   WHERE NRINCITEMP = P_FILIPROC || P_NRAPURACFAT;

   CONSULTA_GRUPOFISCAL('C', V_CDCLIENTE, '', 'S', '01', V_CDGRUPOFISC);

   IF (V_CDTIPEMNF IS NOT NULL) THEN
      SELECT MAX(CDINSCESTA), MAX(CDEMPRESA)
      INTO V_INSCESTA, V_CDEMPRESA
      FROM FILIAL
      WHERE CDFILIAL = P_FILIAL;

      SELECT MAX(CDPAIS), MAX(SGESTADO)
      INTO V_PAISORIG, V_ESTAORIG
      FROM INSCRESTAD
      WHERE (CDEMPRESA = V_CDEMPRESA AND CDINSCESTA = V_INSCESTA);

      SELECT MAX(CDPAIS), MAX(SGESTADO)
      INTO V_PAISDEST, V_ESTADEST
      FROM ENDECLIE
      WHERE (CDCLIENTE = V_CDCLIENTE) AND
            (IDTPENDECLIE = 'P');


      DELETE FROM TRAB_APURAC
      WHERE CDCIAAP       = P_CDCIA    AND
            CDFILIALAP    = P_FILIPROC AND
            NRAPURACFATAP = P_NRAPURACFAT;


      IF (P_GRUPPROD IS NULL) THEN
         FOR CURS_APURAC IN
            (SELECT CD.CDFILIAL, CD.CDCIA, CD.NRDELIVERY, CD.DTEMISDEL,
                    ROUND(NVL(MOVINDICE.VRINDICE,1), 5) VRINDICE, CD.VRDESCDEL, CD.VRDESCSRV,
                    SERVICOCIA.CDARVSERV, SERVPLANO.QTSERFATSP, SERVICOCIA.CDSERVICO,
                    NVL(SERVPLANO.VRPRECMANUTSESP, SERVPLANO.VRPRUDOLSESP) VRPRUDOLSESP, NVL(SERVPLANO.VRPRECMANUTSESP, SERVPLANO.VRPRURESESP) VRPRURESESP,
                    SERVICOCIA.CDPRODUTO, PRODUTO.CDCLASSE,
                    SERVICOCIA.IDSERVICO, SERVICOCIA.DSSERVICO,
                    SERVICOCIA.IDTIPMANUAL, PR.CDARVPROD,
                    SERVPLANO.NRVOO, SERVPLANO.CDTRECHO, PLANOVOO.CDEQUIP,
                    SERVPLANO.DTHRPLAVOOPV, SERVPLANO.QTSESBYPRSP,
                    SERVPLANO.QTSESBYUTSP,  SERVPLANO.VRPRUSBYSP,
                    SERVPLANO.VRDESCRESESP, SERVPLANO.VRDESCDOSESP, SERVPLANO.CDAGRDECO,
                    CD.VRPERSBYUT,          CD.VRPERSBYNUT,
                    SERVPLANO.VRDESCONDRSP, SERVPLANO.VRDESCONDDSP,
                    SERVPLANO.VRDESFIXORSP, SERVPLANO.VRDESFIXODSP,
                    SERVPLANO.VRDESDESVRSP, SERVPLANO.VRDESDESVDSP,
                    CD.VRTXAEROR, CD.VRTXAEROD, CD.VRBACATX, DELIVERY.CDLOCAFATCIA,
                    DELIVERY.CDLOCALDELIV, DELIVERY.NRDELEMISS,
                    SERVPLANO.QTATUAPRODSP
            FROM COMPANHIA_DELIVERY CD, SERVPLANO, SERVICOCIA, PLANOVOO, MOVINDICE, PRODUTO, PRODUTO PR, DELIVERY
            WHERE (CD.CDFILIAL           = P_FILIAL)                 AND
                  (CD.CDCIA              = P_CDCIA)                  AND
                  (CD.DTEMISDEL BETWEEN P_DTINIAPUR AND P_DTFIMAPUR) AND
                  (SERVPLANO.CDFILIAL    = P_FILIAL)                 AND
                  (SERVPLANO.IDSITSERSP  = '2')                      AND
                  (SERVPLANO.NRDELIVERY  = CD.NRDELIVERY)            AND
                  (SERVPLANO.NRLANCTONF  IS NULL)                    AND
                  ((P_NRVOO = 'T') OR (INSTR(P_NRVOO, '_'||PLANOVOO.NRVOO) > 0)) AND
                  ((P_CDLOCAFATCIA = 'T') OR (INSTR(P_CDLOCAFATCIA, '_'||DELIVERY.CDLOCAFATCIA) > 0)) AND
                  (PRODUTO.CDPRODUTO     = SERVPLANO.CDSERVICO)    AND
                  (SERVICOCIA.CDSERVICO  = SERVPLANO.CDSERVICO)    AND
                  (PLANOVOO.CDFILIAL     = P_FILIAL)               AND
                  (PLANOVOO.CDCIA        = SERVPLANO.CDCIA)        AND
                  (PLANOVOO.NRVOO        = SERVPLANO.NRVOO)        AND
                  (PLANOVOO.CDTRECHO     = SERVPLANO.CDTRECHO)     AND
                  (PLANOVOO.DTHRPLAVOOPV = SERVPLANO.DTHRPLAVOOPV) AND
                  (DELIVERY.CDFILIAL     = P_FILIAL)               AND
                  (DELIVERY.CDCIA        = SERVPLANO.CDCIA)        AND
                  (DELIVERY.NRVOO        = SERVPLANO.NRVOO)        AND
                  (DELIVERY.CDTRECHO     = SERVPLANO.CDTRECHO)     AND
                  (DELIVERY.DTHRPLAVOOPV = SERVPLANO.DTHRPLAVOOPV) AND
                  (DELIVERY.NRDELIVERY   = SERVPLANO.NRDELIVERY  ) AND
                  (SERVICOCIA.CDPRODUTO  IS NOT NULL)              AND
                  (SERVICOCIA.IDFATSE  <> '2')                     AND
                  (SERVICOCIA.CDPRODUTO  = PR.CDPRODUTO   )        AND
                  (MOVINDICE.CDTPINDICE(+) = CD.CDTPINDICE)        AND
                  (MOVINDICE.CDINDICE(+)   = CD.CDINDICE)          AND
                  (MOVINDICE.IDPERVALOR(+) = '2')                  AND
                  (MOVINDICE.DTREFIND(+)   = CD.DTEMISDEL)
            ORDER BY CD.CDCIA, CD.NRDELIVERY, SERVPLANO.CDSERVICO) LOOP


            /* Para Garvar a proporcao do servico de acordo com o preco*/
            BEGIN
               SELECT NRTABPRECO
               INTO V_NRTABPRECO
               FROM TABPRECOSE
               WHERE (CDFILIAL = P_FILIAL) AND
                     (CDCIA    = P_CDCIA   ) AND
                     (CURS_APURAC.DTEMISDEL BETWEEN DTINIVIGTAB AND DTFIMVIGTAB);
            EXCEPTION
               WHEN No_Data_Found THEN
                  V_NRTABPRECO := NULL;
            END;

            IF (V_NRTABPRECO IS NOT NULL) THEN
               BEGIN
                  SELECT MIN(NRSEQPRECO)
                  INTO V_NRSEQPRECO
                  FROM PRECOSECIA
                  WHERE (CDFILIAL   = P_FILIAL  ) AND
                        (CDCIA      = P_CDCIA     ) AND
                        (NRTABPRECO = V_NRTABPRECO) AND
                        (CDSERVICO  = CURS_APURAC.CDSERVICO ) AND
                        (((DTINIVIGSE <= CURS_APURAC.DTEMISDEL) OR
                        (DTINIVIGSE IS NULL)) AND
                        ((DTFIMVIGSE >= CURS_APURAC.DTEMISDEL) OR
                         (DTFIMVIGSE IS NULL)));
               EXCEPTION
                  WHEN NO_DATA_FOUND THEN
                     V_NRSEQPRECO := NULL;
               END;
               IF (V_NRSEQPRECO IS NOT NULL) THEN
                  BEGIN
                     SELECT QTPAXINICIAL, QTPAXFINAL
                     INTO V_QTPAXINICIAL, V_QTPAXFINAL
                     FROM PROPRECOSE
                     WHERE (CDFILIAL   = P_FILIAL           ) AND
                           (CDCIA      = P_CDCIA              ) AND
                           (NRTABPRECO = V_NRTABPRECO         ) AND
                           (CDSERVICO  = CURS_APURAC.CDSERVICO) AND
                           (NRSEQPRECO = V_NRSEQPRECO         ) AND
                           (CURS_APURAC.QTATUAPRODSP BETWEEN QTPAXINICIAL AND QTPAXFINAL);
                  EXCEPTION
                     WHEN NO_DATA_FOUND THEN
                        V_QTPAXINICIAL := NULL;
                        V_QTPAXFINAL   := NULL;
                  END;
               END IF;
            END IF;

            CONSULTA_CFOP('S', V_CDEMPRESA, CURS_APURAC.CDPRODUTO, V_INSCESTA, '01',
                          V_PAISORIG, V_ESTAORIG, V_PAISDEST, V_ESTADEST,
                          P_FILIPROC, P_NRAPURACFAT, V_CDGRUPOFISC, P_DTFIMAPUR);

            --Consulta a incidencia de Impostos no item
            CONSULTA_INCIDENCIA('S', V_CDEMPRESA, CURS_APURAC.CDARVPROD,
                                V_INSCESTA, '01',
                                P_FILIPROC, P_NRAPURACFAT, V_CDGRUPOFISC);
            

            SELECT COUNT(*)
            INTO V_COUNTISS
            FROM INCITEMP
            WHERE CDIMPOTEMP = '31' AND
                  NRINCITEMP = P_FILIPROC || P_NRAPURACFAT;


            IF V_COUNTISS > 0 THEN
               V_IDPRODSERVAP := 'S';
            ELSE
               V_IDPRODSERVAP := 'P';
            END IF;

            DELETE INCITEMP
            WHERE NRINCITEMP = P_FILIPROC || P_NRAPURACFAT;

            V_VRDESCDESVAP   := 0;
            V_VRDESCFIXOAP   := 0;
            V_VRDESCCONDAP   := 0;
            V_VRDESCSRV      := 0;
            V_VRDESCDESVSEAP := 0;
            V_VRDESCFIXOSEAP := 0;
            V_VRDESCCONDSEAP := 0;
            V_VRDESCDEL      := 0;

            IF (CURS_APURAC.VRTXAEROR = 0) AND (CURS_APURAC.VRTXAEROD = 0) THEN
               V_VRTXAERORAP := 0;
               V_VRTXAERODAP := 0;
            ELSE
               IF CURS_APURAC.IDTIPMANUAL = '2' THEN
                  V_VRTXAERORAP := (CURS_APURAC.VRTXAEROD * ((CURS_APURAC.QTSERFATSP * CURS_APURAC.VRPRUDOLSESP)/CURS_APURAC.VRBACATX)) * (CURS_APURAC.VRINDICE);
                  V_VRTXAERODAP := (CURS_APURAC.VRTXAEROD * ((CURS_APURAC.QTSERFATSP * CURS_APURAC.VRPRUDOLSESP)/CURS_APURAC.VRBACATX));
               ELSE
                  V_VRTXAERORAP := (CURS_APURAC.VRTXAEROR * ((CURS_APURAC.QTSERFATSP * CURS_APURAC.VRPRURESESP)/CURS_APURAC.VRBACATX));
                  V_VRTXAERODAP := (CURS_APURAC.VRTXAEROR * ((CURS_APURAC.QTSERFATSP * CURS_APURAC.VRPRURESESP)/CURS_APURAC.VRBACATX)) / (CURS_APURAC.VRINDICE);
               END IF;
            END IF;

            IF V_IDPRODSERVAP = 'P' THEN
               IF CURS_APURAC.IDTIPMANUAL = '2' THEN
                  V_VRINDICE  := CURS_APURAC.VRINDICE;
                  V_VRDESCDESVAP := (CURS_APURAC.VRDESDESVDSP * V_VRINDICE);
                  V_VRDESCFIXOAP := (CURS_APURAC.VRDESFIXODSP * V_VRINDICE);
                  V_VRDESCCONDAP := (CURS_APURAC.VRDESCONDDSP * V_VRINDICE);
                  V_VRDESCDEL    := (CURS_APURAC.VRDESCDOSESP * V_VRINDICE);
               ELSE
                  V_VRDESCDESVAP := CURS_APURAC.VRDESDESVRSP;
                  V_VRDESCFIXOAP := CURS_APURAC.VRDESFIXORSP;
                  V_VRDESCCONDAP := CURS_APURAC.VRDESCONDRSP;
                  V_VRDESCDEL    := CURS_APURAC.VRDESCRESESP;
                  V_VRINDICE     := 1;
               END IF;
            ELSE
               IF CURS_APURAC.IDTIPMANUAL = '2' THEN
                  V_VRINDICE       := CURS_APURAC.VRINDICE;
                  V_VRDESCDESVSEAP := (CURS_APURAC.VRDESDESVDSP * V_VRINDICE);
                  V_VRDESCFIXOSEAP := (CURS_APURAC.VRDESFIXODSP * V_VRINDICE);
                  V_VRDESCCONDSEAP := (CURS_APURAC.VRDESCONDDSP * V_VRINDICE);
                  V_VRDESCSRV      := (CURS_APURAC.VRDESCDOSESP * V_VRINDICE);
               ELSE
                  V_VRDESCDESVSEAP := CURS_APURAC.VRDESDESVRSP;
                  V_VRDESCFIXOSEAP := CURS_APURAC.VRDESFIXORSP;
                  V_VRDESCCONDSEAP := CURS_APURAC.VRDESCONDRSP;
                  V_VRDESCSRV      := CURS_APURAC.VRDESCRESESP ;
                  V_VRINDICE       := 1;
               END IF;
            END IF;


            INSERT INTO TRAB_APURAC
               (CDCIAAP, CDFILIALAP, NRSEQAPURACAP, NRLANCTONFAP,
                NRDELIVERYAP, DTEMISDELAP,
                CDSERVICOAP, IDSERVICOAP, DSSERVFATUAP,
                CDPRODUTOAP, CDCLASSEAP,
                NRVOOAP, CDTRECHOAP, DTHRPLAVOOPVAP,
                CDEQUIPAP, NRQTDESERVAP, VRPUDOLSERVAP,
                VRPUREASERVAP, VRCOTDOLARAP,
                CDFISCOPERAP, CDSUFXCFOPAP, DSCDFISCOPERAP,
                VRDESCDELAP, VRDESCSRVAP, IDTIPMANUALAP,
                IDPRODSERVAP, NRAPURACFATAP, CDFILISERVAP,
                VRDESCDESVAP, VRDESCFIXOAP, VRDESCCONDAP,
                VRDESCDESVSEAP, VRDESCFIXOSEAP, VRDESCCONDSEAP,
                VRTXAERORAP, VRTXAERODAP, CDLOCFATCIAP, CDAGRDECOAP,
                CDLOCALDELIVAP, NRDELEMISSAP, QTPROPPRODINIAP, QTPROPPRODFINAP)
            VALUES
               (CURS_APURAC.CDCIA, P_FILIPROC, LPAD(TO_CHAR(V_SEQAPUR),10,'0'), NULL,
                CURS_APURAC.NRDELIVERY, CURS_APURAC.DTEMISDEL,
                CURS_APURAC.CDARVSERV, CURS_APURAC.IDSERVICO, CURS_APURAC.DSSERVICO,
                CURS_APURAC.CDPRODUTO, CURS_APURAC.CDCLASSE,
                CURS_APURAC.NRVOO, CURS_APURAC.CDTRECHO, CURS_APURAC.DTHRPLAVOOPV,
                CURS_APURAC.CDEQUIP, CURS_APURAC.QTSERFATSP, CURS_APURAC.VRPRUDOLSESP,
                CURS_APURAC.VRPRURESESP, V_VRINDICE,
                '***', '***', '***',
                V_VRDESCDEL, V_VRDESCSRV, CURS_APURAC.IDTIPMANUAL,
                V_IDPRODSERVAP, P_NRAPURACFAT, CURS_APURAC.CDFILIAL,
                V_VRDESCDESVAP, V_VRDESCFIXOAP, V_VRDESCCONDAP,
                V_VRDESCDESVSEAP, V_VRDESCFIXOSEAP, V_VRDESCCONDSEAP,
                V_VRTXAERORAP, V_VRTXAERODAP, CURS_APURAC.CDLOCAFATCIA, CURS_APURAC.CDAGRDECO,
                CURS_APURAC.CDLOCALDELIV, CURS_APURAC.NRDELEMISS, V_QTPAXINICIAL, V_QTPAXFINAL);

            V_VRDESCDEL    := 0;
            V_VRDESCSRV    := 0;
            V_VRDESCDESVAP := 0;
            V_VRDESCFIXOAP := 0;
            V_VRDESCCONDAP := 0;

            IF CURS_APURAC.QTSESBYPRSP > 0 THEN
               IF CURS_APURAC.QTSESBYUTSP > 0 THEN
                  IF (CURS_APURAC.VRPERSBYUT = 100) AND
                     (((CURS_APURAC.IDTIPMANUAL = '1') AND (CURS_APURAC.VRPRUSBYSP = CURS_APURAC.VRPRURESESP)) OR
                      ((CURS_APURAC.IDTIPMANUAL = '2') AND (CURS_APURAC.VRPRUSBYSP = CURS_APURAC.VRPRUDOLSESP))) THEN

                     IF (CURS_APURAC.VRTXAEROR = 0) AND (CURS_APURAC.VRTXAEROD = 0) THEN
                        V_VRTXAERORAP := 0;
                        V_VRTXAERODAP := 0;
                     ELSE
                        IF CURS_APURAC.IDTIPMANUAL = '2' THEN
                           V_VRTXAERORAP := (CURS_APURAC.VRTXAEROD * (((CURS_APURAC.QTSERFATSP + CURS_APURAC.QTSESBYUTSP) * CURS_APURAC.VRPRUDOLSESP)/CURS_APURAC.VRBACATX)) * (CURS_APURAC.VRINDICE);
                           V_VRTXAERODAP := (CURS_APURAC.VRTXAEROD * (((CURS_APURAC.QTSERFATSP + CURS_APURAC.QTSESBYUTSP) * CURS_APURAC.VRPRUDOLSESP)/CURS_APURAC.VRBACATX));
                        ELSE
                           V_VRTXAERORAP := (CURS_APURAC.VRTXAEROR * (((CURS_APURAC.QTSERFATSP + CURS_APURAC.QTSESBYUTSP) * CURS_APURAC.VRPRURESESP)/CURS_APURAC.VRBACATX));
                           V_VRTXAERODAP := (CURS_APURAC.VRTXAEROR * (((CURS_APURAC.QTSERFATSP + CURS_APURAC.QTSESBYUTSP) * CURS_APURAC.VRPRURESESP)/CURS_APURAC.VRBACATX)) / (CURS_APURAC.VRINDICE);
                        END IF;
                     END IF;

                     UPDATE TRAB_APURAC
                     SET NRQTDESERVAP  = NRQTDESERVAP + CURS_APURAC.QTSESBYUTSP,
                         VRTXAERORAP   = V_VRTXAERORAP,
                         VRTXAERODAP   = V_VRTXAERODAP
                     WHERE CDCIAAP       = CURS_APURAC.CDCIA AND
                           CDFILIALAP    = P_FILIPROC        AND
                           NRAPURACFATAP = P_NRAPURACFAT     AND
                           NRSEQAPURACAP = LPAD(TO_CHAR(V_SEQAPUR),10,'0');
                  ELSE
                     IF CURS_APURAC.VRPERSBYUT <> 0 THEN
                        V_SEQAPUR   := V_SEQAPUR + 1;
                        V_VRSTANDBY := ROUND((CURS_APURAC.VRPRUSBYSP * (CURS_APURAC.VRPERSBYUT/100)),2);

                        IF (CURS_APURAC.IDTIPMANUAL = '2') THEN
                           V_VRSTANDBYD := V_VRSTANDBY;
                           V_VRSTANDBY  := V_VRSTANDBY * V_VRINDICE;
                        ELSE
                           V_VRSTANDBYD := V_VRSTANDBY / V_VRINDICE;
                        END IF;

                        IF (CURS_APURAC.VRTXAEROR = 0) AND (CURS_APURAC.VRTXAEROD = 0) THEN
                           V_VRTXAERORAP := 0;
                           V_VRTXAERODAP := 0;
                        ELSE
                           IF CURS_APURAC.IDTIPMANUAL = '2' THEN
                              V_VRTXAERORAP := (CURS_APURAC.VRTXAEROD * ((CURS_APURAC.QTSESBYUTSP * V_VRSTANDBYD)/CURS_APURAC.VRBACATX)) * (CURS_APURAC.VRINDICE);
                              V_VRTXAERODAP := (CURS_APURAC.VRTXAEROD * ((CURS_APURAC.QTSESBYUTSP * V_VRSTANDBYD)/CURS_APURAC.VRBACATX));
                           ELSE
                              V_VRTXAERORAP := (CURS_APURAC.VRTXAEROR * ((CURS_APURAC.QTSESBYUTSP * V_VRSTANDBY)/CURS_APURAC.VRBACATX));
                              V_VRTXAERODAP := (CURS_APURAC.VRTXAEROR * ((CURS_APURAC.QTSESBYUTSP * V_VRSTANDBY)/CURS_APURAC.VRBACATX)) / (CURS_APURAC.VRINDICE);
                           END IF;
                        END IF;

                        INSERT INTO TRAB_APURAC
                           (CDCIAAP, CDFILIALAP, NRSEQAPURACAP, NRLANCTONFAP,
                            NRDELIVERYAP, DTEMISDELAP,
                            CDSERVICOAP, IDSERVICOAP, DSSERVFATUAP,
                            CDPRODUTOAP, CDCLASSEAP,
                            NRVOOAP, CDTRECHOAP, DTHRPLAVOOPVAP,
                            CDEQUIPAP, NRQTDESERVAP, VRPUDOLSERVAP,
                            VRPUREASERVAP, VRCOTDOLARAP,
                            CDFISCOPERAP, CDSUFXCFOPAP, DSCDFISCOPERAP,
                            VRDESCDELAP, VRDESCSRVAP, IDTIPMANUALAP,
                            IDPRODSERVAP, NRAPURACFATAP,
                            VRTXAERORAP, VRTXAERODAP, CDLOCFATCIAP, CDAGRDECOAP,
                            CDLOCALDELIVAP, NRDELEMISSAP, QTPROPPRODINIAP, QTPROPPRODFINAP)
                        VALUES
                           (CURS_APURAC.CDCIA, P_FILIPROC, LPAD(TO_CHAR(V_SEQAPUR),10,'0'), NULL,
                            CURS_APURAC.NRDELIVERY, CURS_APURAC.DTEMISDEL,
                            CURS_APURAC.CDARVSERV, CURS_APURAC.IDSERVICO, 'STAND BY',
                            CURS_APURAC.CDPRODUTO, CURS_APURAC.CDCLASSE,
                            CURS_APURAC.NRVOO, CURS_APURAC.CDTRECHO, CURS_APURAC.DTHRPLAVOOPV,
                            CURS_APURAC.CDEQUIP, CURS_APURAC.QTSESBYUTSP, V_VRSTANDBYD,
                            V_VRSTANDBY, V_VRINDICE,
                            '***', '***', '***', V_VRDESCDEL, V_VRDESCSRV, CURS_APURAC.IDTIPMANUAL,
                            V_IDPRODSERVAP, P_NRAPURACFAT, V_VRTXAERORAP, V_VRTXAERODAP,
                            CURS_APURAC.CDLOCAFATCIA, CURS_APURAC.CDAGRDECO,
                            CURS_APURAC.CDLOCALDELIV, CURS_APURAC.NRDELEMISS, V_QTPAXINICIAL, V_QTPAXFINAL);
                     END IF;
                  END IF;
               END IF;

               IF CURS_APURAC.VRPERSBYNUT <> 0 THEN
                  V_SEQAPUR   := V_SEQAPUR + 1;
                  V_QTSTANDBY := CURS_APURAC.QTSESBYPRSP - CURS_APURAC.QTSESBYUTSP;
                  V_VRSTANDBY := ROUND((CURS_APURAC.VRPRUSBYSP * (CURS_APURAC.VRPERSBYNUT/100)),2);

                  IF (CURS_APURAC.IDTIPMANUAL = '2') THEN
                     V_VRSTANDBYD := V_VRSTANDBY;
                     V_VRSTANDBY  := V_VRSTANDBY * V_VRINDICE;
                  ELSE
                     V_VRSTANDBYD := V_VRSTANDBY / V_VRINDICE;
                  END IF;

                  IF (CURS_APURAC.VRTXAEROR = 0) AND (CURS_APURAC.VRTXAEROD = 0) THEN
                     V_VRTXAERORAP := 0;
                     V_VRTXAERODAP := 0;
                  ELSE
                     IF CURS_APURAC.IDTIPMANUAL = '2' THEN
                        V_VRTXAERORAP := (CURS_APURAC.VRTXAEROD * ((V_QTSTANDBY * V_VRSTANDBYD)/CURS_APURAC.VRBACATX)) * (CURS_APURAC.VRINDICE);
                        V_VRTXAERODAP := (CURS_APURAC.VRTXAEROD * ((V_QTSTANDBY * V_VRSTANDBYD)/CURS_APURAC.VRBACATX));
                     ELSE
                        V_VRTXAERORAP := (CURS_APURAC.VRTXAEROR * ((V_QTSTANDBY * V_VRSTANDBY)/CURS_APURAC.VRBACATX));
                        V_VRTXAERODAP := (CURS_APURAC.VRTXAEROR * ((V_QTSTANDBY * V_VRSTANDBY)/CURS_APURAC.VRBACATX)) / (CURS_APURAC.VRINDICE);
                     END IF;
                  END IF;

                  IF V_QTSTANDBY > 0 THEN
                     INSERT INTO TRAB_APURAC
                        (CDCIAAP, CDFILIALAP, NRSEQAPURACAP, NRLANCTONFAP,
                         NRDELIVERYAP, DTEMISDELAP,
                         CDSERVICOAP, IDSERVICOAP, DSSERVFATUAP,
                         CDPRODUTOAP, CDCLASSEAP,
                         NRVOOAP, CDTRECHOAP, DTHRPLAVOOPVAP,
                         CDEQUIPAP, NRQTDESERVAP, VRPUDOLSERVAP,
                         VRPUREASERVAP, VRCOTDOLARAP,
                         CDFISCOPERAP, CDSUFXCFOPAP, DSCDFISCOPERAP,
                         VRDESCDELAP, VRDESCSRVAP, IDTIPMANUALAP,
                         IDPRODSERVAP, NRAPURACFATAP,
                         VRTXAERORAP, VRTXAERODAP, CDLOCFATCIAP, CDAGRDECOAP,
                         CDLOCALDELIVAP, NRDELEMISSAP, QTPROPPRODINIAP, QTPROPPRODFINAP)
                     VALUES
                        (CURS_APURAC.CDCIA, P_FILIPROC, LPAD(TO_CHAR(V_SEQAPUR),10,'0'), NULL,
                         CURS_APURAC.NRDELIVERY, CURS_APURAC.DTEMISDEL,
                         CURS_APURAC.CDARVSERV, CURS_APURAC.IDSERVICO, 'STAND BY',
                         CURS_APURAC.CDPRODUTO, CURS_APURAC.CDCLASSE,
                         CURS_APURAC.NRVOO, CURS_APURAC.CDTRECHO, CURS_APURAC.DTHRPLAVOOPV,
                         CURS_APURAC.CDEQUIP, V_QTSTANDBY, V_VRSTANDBYD,
                         V_VRSTANDBY, V_VRINDICE,
                         '***', '***', '***', V_VRDESCDEL, V_VRDESCSRV, CURS_APURAC.IDTIPMANUAL,
                         V_IDPRODSERVAP, P_NRAPURACFAT, V_VRTXAERORAP, V_VRTXAERODAP,
                         CURS_APURAC.CDLOCAFATCIA, CURS_APURAC.CDAGRDECO,
                         CURS_APURAC.CDLOCALDELIV, CURS_APURAC.NRDELEMISS,
                         V_QTPAXINICIAL, V_QTPAXFINAL);
                  END IF;
               END IF;
            END IF;


            DELETE FROM CFOPTEMP
            WHERE NRCFOPTEMP = P_FILIPROC || P_NRAPURACFAT;

            V_SEQAPUR := V_SEQAPUR + 1;
         END LOOP;
      ELSE
         FOR CURS_APURAC IN
            (SELECT D.CDFILIAL, C.CDCIA, D.NRDELIVERY, D.DTEMISDEL,
                    NVL(MOVINDICE.VRINDICE,1 ) VRINDICE, D.VRDESCDEL, D.VRDESCSRV,
                    SERVICOCIA.CDARVSERV, SERVPLANO.QTSERFATSP,  SERVICOCIA.CDSERVICO,
                    NVL(SERVPLANO.VRPRECMANUTSESP, SERVPLANO.VRPRUDOLSESP) VRPRUDOLSESP, ROUND(NVL(SERVPLANO.VRPRECMANUTSESP, SERVPLANO.VRPRURESESP),2) VRPRURESESP,
                    SERVICOCIA.CDPRODUTO, PRODUTO.CDCLASSE,
                    SERVICOCIA.IDSERVICO, SERVICOCIA.DSSERVICO,
                    SERVICOCIA.IDTIPMANUAL,
                    SERVPLANO.NRVOO, SERVPLANO.CDTRECHO, PLANOVOO.CDEQUIP,
                    SERVPLANO.DTHRPLAVOOPV, SERVPLANO.QTSESBYPRSP,
                    SERVPLANO.QTSESBYUTSP,  SERVPLANO.VRPRUSBYSP,
                    SERVPLANO.VRDESCRESESP, SERVPLANO.VRDESCDOSESP, SERVPLANO.CDAGRDECO,
                    CF.VRPERSBYUT, CF.VRPERSBYNUT,
                    SERVPLANO.VRDESCONDRSP, SERVPLANO.VRDESCONDDSP,
                    SERVPLANO.VRDESFIXORSP, SERVPLANO.VRDESFIXODSP,
                    SERVPLANO.VRDESDESVRSP, SERVPLANO.VRDESDESVDSP,
                    D.VRTXAEROR, D.VRTXAEROD, D.VRBACATX, D.CDLOCAFATCIA,
                    D.CDLOCALDELIV, D.NRDELEMISS,
                    SERVPLANO.QTATUAPRODSP
            FROM COMPANHIA C, DELIVERY D, CIAFILI CF, SERVPLANO, SERVICOCIA, PLANOVOO, MOVINDICE, PRODUTO,
                 PRSUBGRPROD AN, SUBGRUPPROD SG, GRUPPROD GR
            WHERE (C.CDCIA              = D.CDCIA)                               AND
                  (CF.CDFILIAL          = D.CDFILIAL)                            AND
                  (CF.CDCIA             = D.CDCIA)                               AND
                  (D.CDFILIAL           = P_FILIAL)                              AND
                  (C.CDCIA              = P_CDCIA)                               AND
                  (D.DTEMISDEL BETWEEN P_DTINIAPUR AND P_DTFIMAPUR)              AND
                  (SERVPLANO.CDFILIAL    = P_FILIAL)                             AND
                  (SERVPLANO.IDSITSERSP  = '2')                                  AND
                  (SERVPLANO.NRDELIVERY  = D.NRDELIVERY)                         AND
                  (SERVPLANO.NRLANCTONF  IS NULL)                                AND
                  ((P_NRVOO = 'T') OR (INSTR(P_NRVOO, '_'||PLANOVOO.NRVOO) > 0)) AND
                  ((P_CDLOCAFATCIA = 'T') OR (INSTR(P_CDLOCAFATCIA, '_'||D.CDLOCAFATCIA) > 0)) AND
                  (PRODUTO.CDPRODUTO     = SERVPLANO.CDSERVICO)                  AND
                  (SERVICOCIA.CDSERVICO  = SERVPLANO.CDSERVICO)                  AND
                  (GR.CDGRUPPROD         = P_GRUPPROD      )                     AND
                  (GR.CDGRUPPROD         = SG.CDGRUPPROD)                        AND
                  (SG.CDSUBGRPROD    	  = AN.CDSUBGRPROD)                      AND
                  (SG.CDGRUPPROD      	  = AN.CDGRUPPROD)                       AND
                  (AN.CDFILISUBGR	     = 'P')                              AND
                  (PRODUTO.CDPRODUTO	  = AN.CDPRODUTO)                        AND
                  (PLANOVOO.CDFILIAL     = P_FILIAL)                             AND
                  (PLANOVOO.CDCIA        = SERVPLANO.CDCIA)                      AND
                  (PLANOVOO.NRVOO        = SERVPLANO.NRVOO)                      AND
                  (PLANOVOO.CDTRECHO     = SERVPLANO.CDTRECHO)                   AND
                  (PLANOVOO.DTHRPLAVOOPV = SERVPLANO.DTHRPLAVOOPV)               AND
                  (SERVICOCIA.CDPRODUTO  IS NOT NULL)                            AND
                  (SERVICOCIA.IDFATSE  <> '2')                                   AND
                  (MOVINDICE.CDTPINDICE(+) = CF.CDTPINDICE)                      AND
                  (MOVINDICE.CDINDICE(+)   = CF.CDINDICE)                        AND
                  (MOVINDICE.IDPERVALOR(+) = '2')                                AND
                  (MOVINDICE.DTREFIND      = D.DTEMISDEL(+))
            ORDER BY C.CDCIA, D.NRDELIVERY, SERVPLANO.CDSERVICO)  LOOP

            /*Para Garvar a proporcao do servico de acordo com o preco */
            BEGIN
               SELECT NRTABPRECO
               INTO V_NRTABPRECO
               FROM TABPRECOSE
               WHERE (CDFILIAL = P_FILIAL) AND
                     (CDCIA    = P_CDCIA   ) AND
                     (CURS_APURAC.DTEMISDEL BETWEEN DTINIVIGTAB AND DTFIMVIGTAB);
            EXCEPTION
               WHEN No_Data_Found THEN
                  V_NRTABPRECO := NULL;
            END;

            IF (V_NRTABPRECO IS NOT NULL) THEN
               BEGIN
                  SELECT MIN(NRSEQPRECO)
                  INTO V_NRSEQPRECO
                  FROM PRECOSECIA
                  WHERE (CDFILIAL   = P_FILIAL  ) AND
                        (CDCIA      = P_CDCIA     ) AND
                        (NRTABPRECO = V_NRTABPRECO) AND
                        (CDSERVICO  = CURS_APURAC.CDSERVICO ) AND
                        (((DTINIVIGSE <= CURS_APURAC.DTEMISDEL) OR
                        (DTINIVIGSE IS NULL)) AND
                        ((DTFIMVIGSE >= CURS_APURAC.DTEMISDEL) OR
                         (DTFIMVIGSE IS NULL)));
               EXCEPTION
                  WHEN NO_DATA_FOUND THEN
                     V_NRSEQPRECO := NULL;
               END;
               IF (V_NRSEQPRECO IS NOT NULL) THEN
                  BEGIN
                     SELECT QTPAXINICIAL, QTPAXFINAL
                     INTO V_QTPAXINICIAL, V_QTPAXFINAL
                     FROM PROPRECOSE
                     WHERE (CDFILIAL   = P_FILIAL           ) AND
                           (CDCIA      = P_CDCIA              ) AND
                           (NRTABPRECO = V_NRTABPRECO         ) AND
                           (CDSERVICO  = CURS_APURAC.CDSERVICO) AND
                           (NRSEQPRECO = V_NRSEQPRECO         ) AND
                           (CURS_APURAC.QTATUAPRODSP BETWEEN QTPAXINICIAL AND QTPAXFINAL);
                  EXCEPTION
                     WHEN NO_DATA_FOUND THEN
                        V_QTPAXINICIAL := NULL;
                        V_QTPAXFINAL   := NULL;
                  END;
               END IF;
            END IF;

            CONSULTA_CFOP('S', V_CDEMPRESA, CURS_APURAC.CDPRODUTO, V_INSCESTA, '01',
                          V_PAISORIG, V_ESTAORIG, V_PAISDEST, V_ESTADEST,
                          P_FILIPROC, P_NRAPURACFAT, V_CDGRUPOFISC, P_DTFIMAPUR);



            CONSULTA_INCIDENCIA('S', V_CDEMPRESA, CURS_APURAC.CDPRODUTO,
                                V_INSCESTA, '01',
                                P_FILIPROC, P_NRAPURACFAT, V_CDGRUPOFISC);

            SELECT COUNT(*)
            INTO V_COUNTISS
            FROM INCITEMP
            WHERE CDIMPOTEMP = '31' AND
                  NRINCITEMP = P_FILIPROC || P_NRAPURACFAT;

            IF V_COUNTISS > 0 THEN
               V_IDPRODSERVAP := 'S';
            ELSE
               V_IDPRODSERVAP := 'P';
            END IF;

            DELETE INCITEMP
            WHERE NRINCITEMP = P_FILIPROC || P_NRAPURACFAT;


            V_VRDESCDESVAP   := 0;
            V_VRDESCFIXOAP   := 0;
            V_VRDESCCONDAP   := 0;
            V_VRDESCSRV      := 0;
            V_VRDESCDESVSEAP := 0;
            V_VRDESCFIXOSEAP := 0;
            V_VRDESCCONDSEAP := 0;
            V_VRDESCDEL      := 0;


            IF (CURS_APURAC.VRTXAEROR = 0) AND (CURS_APURAC.VRTXAEROD = 0) THEN
               V_VRTXAERORAP := 0;
               V_VRTXAERODAP := 0;
            ELSE
               IF CURS_APURAC.IDTIPMANUAL = '2' THEN
                  V_VRTXAERORAP := (CURS_APURAC.VRTXAEROD * ((CURS_APURAC.QTSERFATSP * CURS_APURAC.VRPRUDOLSESP)/CURS_APURAC.VRBACATX)) * (CURS_APURAC.VRINDICE);
                  V_VRTXAERODAP := (CURS_APURAC.VRTXAEROD * ((CURS_APURAC.QTSERFATSP * CURS_APURAC.VRPRUDOLSESP)/CURS_APURAC.VRBACATX));
               ELSE
                  V_VRTXAERORAP := (CURS_APURAC.VRTXAEROR * ((CURS_APURAC.QTSERFATSP * CURS_APURAC.VRPRURESESP)/CURS_APURAC.VRBACATX));
                  V_VRTXAERODAP := (CURS_APURAC.VRTXAEROR * ((CURS_APURAC.QTSERFATSP * CURS_APURAC.VRPRURESESP)/CURS_APURAC.VRBACATX)) / (CURS_APURAC.VRINDICE);
               END IF;
            END IF;

            IF V_IDPRODSERVAP = 'P' THEN
               IF CURS_APURAC.IDTIPMANUAL = '2' THEN
                  V_VRINDICE     := CURS_APURAC.VRINDICE;
                  V_VRDESCDESVAP := (CURS_APURAC.VRDESDESVDSP * V_VRINDICE);
                  V_VRDESCFIXOAP := (CURS_APURAC.VRDESFIXODSP * V_VRINDICE);
                  V_VRDESCCONDAP := (CURS_APURAC.VRDESCONDDSP * V_VRINDICE);
                  V_VRDESCDEL    := (CURS_APURAC.VRDESCDOSESP * V_VRINDICE);
               ELSE
                  V_VRDESCDESVAP := CURS_APURAC.VRDESDESVRSP;
                  V_VRDESCFIXOAP := CURS_APURAC.VRDESFIXORSP;
                  V_VRDESCCONDAP := CURS_APURAC.VRDESCONDRSP;
                  V_VRDESCDEL    := CURS_APURAC.VRDESCRESESP;
                  V_VRINDICE     := 1;
               END IF;
            ELSE
               IF CURS_APURAC.IDTIPMANUAL = '2' THEN
                  V_VRINDICE       := CURS_APURAC.VRINDICE;
                  V_VRDESCDESVSEAP := (CURS_APURAC.VRDESDESVDSP * V_VRINDICE);
                  V_VRDESCFIXOSEAP := (CURS_APURAC.VRDESFIXODSP * V_VRINDICE);
                  V_VRDESCCONDSEAP := (CURS_APURAC.VRDESCONDDSP * V_VRINDICE);
                  V_VRDESCSRV      := (CURS_APURAC.VRDESCDOSESP * V_VRINDICE);
               ELSE
                  V_VRDESCDESVSEAP := CURS_APURAC.VRDESDESVRSP;
                  V_VRDESCFIXOSEAP := CURS_APURAC.VRDESFIXORSP;
                  V_VRDESCCONDSEAP := CURS_APURAC.VRDESCONDRSP;
               	  V_VRDESCSRV      := CURS_APURAC.VRDESCRESESP ;
                  V_VRINDICE       := 1;
               END IF;
            END IF;


            INSERT INTO TRAB_APURAC
               (CDCIAAP, CDFILIALAP, NRSEQAPURACAP, NRLANCTONFAP,
                NRDELIVERYAP, DTEMISDELAP,
                CDSERVICOAP, IDSERVICOAP, DSSERVFATUAP,
                CDPRODUTOAP, CDCLASSEAP,
                NRVOOAP, CDTRECHOAP, DTHRPLAVOOPVAP,
                CDEQUIPAP, NRQTDESERVAP, VRPUDOLSERVAP,
                VRPUREASERVAP, VRCOTDOLARAP,
                CDFISCOPERAP, CDSUFXCFOPAP, DSCDFISCOPERAP,
                VRDESCDELAP, VRDESCSRVAP, IDTIPMANUALAP,
                IDPRODSERVAP, NRAPURACFATAP, CDFILISERVAP,
                VRDESCDESVAP, VRDESCFIXOAP, VRDESCCONDAP,
                VRDESCDESVSEAP, VRDESCFIXOSEAP, VRDESCCONDSEAP,
                VRTXAERORAP, VRTXAERODAP, CDLOCFATCIAP, CDAGRDECOAP,
                CDLOCALDELIVAP, NRDELEMISSAP, QTPROPPRODINIAP, QTPROPPRODFINAP)
            VALUES
               (CURS_APURAC.CDCIA, P_FILIPROC, LPAD(TO_CHAR(V_SEQAPUR),10,'0'), NULL,
                CURS_APURAC.NRDELIVERY, CURS_APURAC.DTEMISDEL,
                CURS_APURAC.CDARVSERV, CURS_APURAC.IDSERVICO, CURS_APURAC.DSSERVICO,
                CURS_APURAC.CDPRODUTO, CURS_APURAC.CDCLASSE,
                CURS_APURAC.NRVOO, CURS_APURAC.CDTRECHO, CURS_APURAC.DTHRPLAVOOPV,
                CURS_APURAC.CDEQUIP, CURS_APURAC.QTSERFATSP, CURS_APURAC.VRPRUDOLSESP,
                CURS_APURAC.VRPRURESESP, V_VRINDICE,
                '***', '***', '***',
                V_VRDESCDEL, V_VRDESCSRV, CURS_APURAC.IDTIPMANUAL,
                V_IDPRODSERVAP, P_NRAPURACFAT, CURS_APURAC.CDFILIAL,
                V_VRDESCDESVAP, V_VRDESCFIXOAP, V_VRDESCCONDAP,
                V_VRDESCDESVSEAP, V_VRDESCFIXOSEAP, V_VRDESCCONDSEAP,
                V_VRTXAERORAP, V_VRTXAERODAP, CURS_APURAC.CDLOCAFATCIA, CURS_APURAC.CDAGRDECO,
                CURS_APURAC.CDLOCALDELIV, CURS_APURAC.NRDELEMISS, V_QTPAXINICIAL, V_QTPAXFINAL);

            V_VRDESCDEL    := 0;
            V_VRDESCSRV    := 0;
            V_VRDESCDESVAP := 0;
            V_VRDESCFIXOAP := 0;
            V_VRDESCCONDAP := 0;

            IF CURS_APURAC.QTSESBYPRSP > 0 THEN
               IF CURS_APURAC.QTSESBYUTSP > 0 THEN
                  IF (CURS_APURAC.VRPERSBYUT = 100) AND
                     (((CURS_APURAC.IDTIPMANUAL = '1') AND (CURS_APURAC.VRPRUSBYSP = CURS_APURAC.VRPRURESESP)) OR
                      ((CURS_APURAC.IDTIPMANUAL = '2') AND (CURS_APURAC.VRPRUSBYSP = CURS_APURAC.VRPRUDOLSESP))) THEN

                     IF (CURS_APURAC.VRTXAEROR = 0) AND (CURS_APURAC.VRTXAEROD = 0) THEN
                        V_VRTXAERORAP := 0;
                        V_VRTXAERODAP := 0;
                     ELSE
                        IF CURS_APURAC.IDTIPMANUAL = '2' THEN
                           V_VRTXAERORAP := (CURS_APURAC.VRTXAEROD * (((CURS_APURAC.QTSERFATSP + CURS_APURAC.QTSESBYUTSP) * CURS_APURAC.VRPRUDOLSESP)/CURS_APURAC.VRBACATX)) * (CURS_APURAC.VRINDICE);
                           V_VRTXAERODAP := (CURS_APURAC.VRTXAEROD * (((CURS_APURAC.QTSERFATSP + CURS_APURAC.QTSESBYUTSP) * CURS_APURAC.VRPRUDOLSESP)/CURS_APURAC.VRBACATX));
                        ELSE
                           V_VRTXAERORAP := (CURS_APURAC.VRTXAEROR * (((CURS_APURAC.QTSERFATSP + CURS_APURAC.QTSESBYUTSP) * CURS_APURAC.VRPRURESESP)/CURS_APURAC.VRBACATX));
                           V_VRTXAERODAP := (CURS_APURAC.VRTXAEROR * (((CURS_APURAC.QTSERFATSP + CURS_APURAC.QTSESBYUTSP) * CURS_APURAC.VRPRURESESP)/CURS_APURAC.VRBACATX)) / (CURS_APURAC.VRINDICE);
                        END IF;
                     END IF;

                     UPDATE TRAB_APURAC
                     SET NRQTDESERVAP  = NRQTDESERVAP + CURS_APURAC.QTSESBYUTSP,
                         VRTXAERORAP   = V_VRTXAERORAP,
                         VRTXAERODAP   = V_VRTXAERODAP
                     WHERE CDCIAAP       = CURS_APURAC.CDCIA AND
                           CDFILIALAP    = P_FILIPROC        AND
                           NRAPURACFATAP = P_NRAPURACFAT     AND
                           NRSEQAPURACAP = LPAD(TO_CHAR(V_SEQAPUR),10,'0');
                  ELSE
                     IF CURS_APURAC.VRPERSBYUT <> 0 THEN
                        V_SEQAPUR   := V_SEQAPUR + 1;
                        V_VRSTANDBY := ROUND((CURS_APURAC.VRPRUSBYSP * (CURS_APURAC.VRPERSBYUT/100)),2);

                        IF (CURS_APURAC.IDTIPMANUAL = '2') THEN
                           V_VRSTANDBYD := V_VRSTANDBY;
                           V_VRSTANDBY  := V_VRSTANDBY * V_VRINDICE;
                        ELSE
                           V_VRSTANDBYD := V_VRSTANDBY / V_VRINDICE;
                        END IF;

                        IF (CURS_APURAC.VRTXAEROR = 0) AND (CURS_APURAC.VRTXAEROD = 0) THEN
                           V_VRTXAERORAP := 0;
                           V_VRTXAERODAP := 0;
                        ELSE
                           IF CURS_APURAC.IDTIPMANUAL = '2' THEN
                              V_VRTXAERORAP := (CURS_APURAC.VRTXAEROD * ((CURS_APURAC.QTSESBYUTSP * V_VRSTANDBYD)/CURS_APURAC.VRBACATX)) * (CURS_APURAC.VRINDICE);
                              V_VRTXAERODAP := (CURS_APURAC.VRTXAEROD * ((CURS_APURAC.QTSESBYUTSP * V_VRSTANDBYD)/CURS_APURAC.VRBACATX));
                           ELSE
                              V_VRTXAERORAP := (CURS_APURAC.VRTXAEROR * ((CURS_APURAC.QTSESBYUTSP * V_VRSTANDBY)/CURS_APURAC.VRBACATX));
                              V_VRTXAERODAP := (CURS_APURAC.VRTXAEROR * ((CURS_APURAC.QTSESBYUTSP * V_VRSTANDBY)/CURS_APURAC.VRBACATX)) / (CURS_APURAC.VRINDICE);
                           END IF;
                        END IF;

                        INSERT INTO TRAB_APURAC
                           (CDCIAAP, CDFILIALAP, NRSEQAPURACAP, NRLANCTONFAP,
                            NRDELIVERYAP, DTEMISDELAP,
                            CDSERVICOAP, IDSERVICOAP, DSSERVFATUAP,
                            CDPRODUTOAP, CDCLASSEAP,
                            NRVOOAP, CDTRECHOAP, DTHRPLAVOOPVAP,
                            CDEQUIPAP, NRQTDESERVAP, VRPUDOLSERVAP,
                            VRPUREASERVAP, VRCOTDOLARAP,
                            CDFISCOPERAP, CDSUFXCFOPAP, DSCDFISCOPERAP,
                            VRDESCDELAP, VRDESCSRVAP, IDTIPMANUALAP,
                            IDPRODSERVAP, NRAPURACFATAP,
                            VRTXAERORAP, VRTXAERODAP, CDLOCFATCIAP, CDAGRDECOAP,
                            CDLOCALDELIVAP, NRDELEMISSAP, QTPROPPRODINIAP, QTPROPPRODFINAP)
                        VALUES
                           (CURS_APURAC.CDCIA, P_FILIPROC, LPAD(TO_CHAR(V_SEQAPUR),10,'0'), NULL,
                            CURS_APURAC.NRDELIVERY, CURS_APURAC.DTEMISDEL,
                            CURS_APURAC.CDARVSERV, CURS_APURAC.IDSERVICO, 'STAND BY',
                            CURS_APURAC.CDPRODUTO, CURS_APURAC.CDCLASSE,
                            CURS_APURAC.NRVOO, CURS_APURAC.CDTRECHO, CURS_APURAC.DTHRPLAVOOPV,
                            CURS_APURAC.CDEQUIP, CURS_APURAC.QTSESBYUTSP, V_VRSTANDBYD,
                            V_VRSTANDBY, V_VRINDICE,
                            '***', '***', '***', V_VRDESCDEL, V_VRDESCSRV, CURS_APURAC.IDTIPMANUAL,
                            V_IDPRODSERVAP, P_NRAPURACFAT, V_VRTXAERORAP, V_VRTXAERODAP,
                            CURS_APURAC.CDLOCAFATCIA, CURS_APURAC.CDAGRDECO,
                            CURS_APURAC.CDLOCALDELIV, CURS_APURAC.NRDELEMISS, V_QTPAXINICIAL, V_QTPAXFINAL);
                     END IF;
                  END IF;
               END IF;

               IF CURS_APURAC.VRPERSBYNUT <> 0 THEN
                  V_SEQAPUR   := V_SEQAPUR + 1;
                  V_QTSTANDBY := CURS_APURAC.QTSESBYPRSP - CURS_APURAC.QTSESBYUTSP;
                  V_VRSTANDBY := ROUND((CURS_APURAC.VRPRUSBYSP * (CURS_APURAC.VRPERSBYNUT/100)),2);

                  IF (CURS_APURAC.IDTIPMANUAL = '2') THEN
                     V_VRSTANDBYD := V_VRSTANDBY;
                     V_VRSTANDBY  := V_VRSTANDBY * V_VRINDICE;
                  ELSE
                     V_VRSTANDBYD := V_VRSTANDBY / V_VRINDICE;
                  END IF;

                  IF (CURS_APURAC.VRTXAEROR = 0) AND (CURS_APURAC.VRTXAEROD = 0) THEN
                     V_VRTXAERORAP := 0;
                     V_VRTXAERODAP := 0;
                  ELSE
                     IF CURS_APURAC.IDTIPMANUAL = '2' THEN
                        V_VRTXAERORAP := (CURS_APURAC.VRTXAEROD * ((V_QTSTANDBY * V_VRSTANDBYD)/CURS_APURAC.VRBACATX)) * (CURS_APURAC.VRINDICE);
                        V_VRTXAERODAP := (CURS_APURAC.VRTXAEROD * ((V_QTSTANDBY * V_VRSTANDBYD)/CURS_APURAC.VRBACATX));
                     ELSE
                        V_VRTXAERORAP := (CURS_APURAC.VRTXAEROR * ((V_QTSTANDBY * V_VRSTANDBY)/CURS_APURAC.VRBACATX));
                        V_VRTXAERODAP := (CURS_APURAC.VRTXAEROR * ((V_QTSTANDBY * V_VRSTANDBY)/CURS_APURAC.VRBACATX)) / (CURS_APURAC.VRINDICE);
                     END IF;
                  END IF;

                  IF V_QTSTANDBY > 0 THEN
                     INSERT INTO TRAB_APURAC
                        (CDCIAAP, CDFILIALAP, NRSEQAPURACAP, NRLANCTONFAP,
                         NRDELIVERYAP, DTEMISDELAP,
                         CDSERVICOAP, IDSERVICOAP, DSSERVFATUAP,
                         CDPRODUTOAP, CDCLASSEAP,
                         NRVOOAP, CDTRECHOAP, DTHRPLAVOOPVAP,
                         CDEQUIPAP, NRQTDESERVAP, VRPUDOLSERVAP,
                         VRPUREASERVAP, VRCOTDOLARAP,
                         CDFISCOPERAP, CDSUFXCFOPAP, DSCDFISCOPERAP,
                         VRDESCDELAP, VRDESCSRVAP, IDTIPMANUALAP,
                         IDPRODSERVAP, NRAPURACFATAP,
                         VRTXAERORAP, VRTXAERODAP, CDLOCFATCIAP, CDAGRDECOAP,
                         CDLOCALDELIVAP, NRDELEMISSAP)
                     VALUES
                        (CURS_APURAC.CDCIA, P_FILIPROC, LPAD(TO_CHAR(V_SEQAPUR),10,'0'), NULL,
                         CURS_APURAC.NRDELIVERY, CURS_APURAC.DTEMISDEL,
                         CURS_APURAC.CDARVSERV, CURS_APURAC.IDSERVICO, 'STAND BY',
                         CURS_APURAC.CDPRODUTO, CURS_APURAC.CDCLASSE,
                         CURS_APURAC.NRVOO, CURS_APURAC.CDTRECHO, CURS_APURAC.DTHRPLAVOOPV,
                         CURS_APURAC.CDEQUIP, V_QTSTANDBY, V_VRSTANDBYD,
                         V_VRSTANDBY, V_VRINDICE,
                         '***', '***', '***', V_VRDESCDEL, V_VRDESCSRV, CURS_APURAC.IDTIPMANUAL,
                         V_IDPRODSERVAP, P_NRAPURACFAT, V_VRTXAERORAP, V_VRTXAERODAP,
                         CURS_APURAC.CDLOCAFATCIA, CURS_APURAC.CDAGRDECO,
                         CURS_APURAC.CDLOCALDELIV, CURS_APURAC.NRDELEMISS);
                  END IF;
               END IF;
            END IF;


            DELETE FROM CFOPTEMP
            WHERE NRCFOPTEMP = P_FILIPROC || P_NRAPURACFAT;

            V_SEQAPUR := V_SEQAPUR + 1;
         END LOOP;
      END IF;

     
      IF (V_CDTIPEMNF = '01') THEN
         APURAC_CLASSE(P_FILIAL, P_FILIPROC, P_CDCIA, P_DTFECHAME, P_DTINIAPUR, P_DTFIMAPUR, P_CDSERIE, P_IDTPIJURTRAN, P_NRINSJURTRAN, P_NRAPURACFAT);
      END IF;

      IF (V_CDTIPEMNF = '02') THEN
         APURAC_VOOCLASSE(P_FILIAL, P_FILIPROC, P_CDCIA, P_DTFECHAME, P_DTINIAPUR, P_DTFIMAPUR, P_CDSERIE, P_IDTPIJURTRAN, P_NRINSJURTRAN, P_NRVOO, P_IDAGRUPSERV, P_NRAPURACFAT);
      END IF;

      IF (V_CDTIPEMNF = '03') THEN
        APURAC_VOO(P_FILIAL, P_FILIPROC, P_CDCIA, P_DTFECHAME, P_DTINIAPUR, P_DTFIMAPUR, P_CDSERIE, P_IDTPIJURTRAN, P_NRINSJURTRAN, P_NRVOO, P_IDAGRUPSERV, P_NRAPURACFAT);
      END IF;

      IF (V_CDTIPEMNF = '04') THEN
         APURAC_DELIVERY(P_FILIAL, P_FILIPROC, P_CDCIA, P_DTFECHAME, P_DTINIAPUR, P_DTFIMAPUR, P_CDSERIE, P_IDTPIJURTRAN, P_NRINSJURTRAN, P_NRAPURACFAT);
      END IF;

      IF (V_CDTIPEMNF = '05') THEN
         APURAC_DELIVCLASSE(P_FILIAL, P_FILIPROC, P_CDCIA, P_DTFECHAME, P_DTINIAPUR, P_DTFIMAPUR, P_CDSERIE, P_IDTPIJURTRAN, P_NRINSJURTRAN, P_NRAPURACFAT);
      END IF;

      IF (V_CDTIPEMNF = '06') THEN
         APURAC_PERIODO(P_FILIAL, P_FILIPROC, P_CDCIA, P_DTFECHAME, P_DTINIAPUR, P_DTFIMAPUR, P_CDSERIE, P_IDTPIJURTRAN, P_NRINSJURTRAN, P_NRAPURACFAT);
      END IF;

      IF (V_CDTIPEMNF = '07') THEN
         APURAC_EQUIP(P_FILIAL, P_FILIPROC, P_CDCIA, P_DTFECHAME, P_DTINIAPUR, P_DTFIMAPUR, P_CDSERIE, P_IDTPIJURTRAN, P_NRINSJURTRAN, P_NRAPURACFAT);
      END IF;

      IF (V_CDTIPEMNF = '08') THEN
         APURAC_EQUIPCLASSE(P_FILIAL, P_FILIPROC, P_CDCIA, P_DTFECHAME, P_DTINIAPUR, P_DTFIMAPUR, P_CDSERIE, P_IDTPIJURTRAN, P_NRINSJURTRAN, P_NRAPURACFAT);
      END IF;

      IF (V_CDTIPEMNF = '09') THEN
         APURAC_EQUIPCLASSE(P_FILIAL, P_FILIPROC, P_CDCIA, P_DTFECHAME, P_DTINIAPUR, P_DTFIMAPUR, P_CDSERIE, P_IDTPIJURTRAN, P_NRINSJURTRAN, P_NRAPURACFAT, P_GRUPPROD, P_IDGRUPTAXA, P_LISTANF);
      END IF;

      IF (V_CDTIPEMNF = '10') THEN
         APURAC_LOCAL(P_FILIAL, P_FILIPROC, P_CDCIA, P_DTFECHAME, P_DTINIAPUR, P_DTFIMAPUR, P_CDSERIE, P_IDTPIJURTRAN, P_NRINSJURTRAN, P_NRAPURACFAT);
      END IF;

      IF (V_CDTIPEMNF = '11') THEN
         APURAC_AGRUPAMENTODEL(P_FILIAL, P_FILIPROC, P_CDCIA, P_DTFECHAME, P_DTINIAPUR, P_DTFIMAPUR, P_CDSERIE, P_IDTPIJURTRAN, P_NRINSJURTRAN, P_NRAPURACFAT);
      END IF;
      
      /* Agrupamento de Delivery/Periodo (Voo) */
      IF (V_CDTIPEMNF = '12') THEN
         APURAC_AGRUPPERIODOVOO(P_FILIAL, P_FILIPROC, P_CDCIA, P_DTFECHAME, P_DTINIAPUR, P_DTFIMAPUR, P_CDSERIE, P_IDTPIJURTRAN, P_NRINSJURTRAN, P_NRAPURACFAT, P_NRVOO);
      END IF;

      UPDATE CIAFILI
      SET DTULTFECH = P_DTFIMAPUR
      WHERE CDFILIAL = P_FILIAL AND
            CDCIA    = P_CDCIA;
      
      /* Gera devolucao somente se o parametro de tipo de operacao de devolucao estiver parametrizado */
      IF V_CDTIPOOPERDEVOL IS NOT NULL THEN
         /* *****************************************
         Gera nota fiscal (ou notas fiscais) de devolucao com os servicos indicados
         como devolucao no cadastro de servico do comissaria.
         ***************************************** */
         V_QTPRODNF := 0;
         V_NRSEQITPROD := 0;
         /* Busca os servicos a faturar, percorrendo-os um a um */
         FOR C_SERV_DEVOL IN
         (
         SELECT F.CDEMPRESA, F.CDINSCMUNI, F.CDINSCESTA,
                NVL(SR.NRMAXITNF, 10) AS NRMAXITNF, NVL(SR.NRMAXSERVNF, 5), SR.IDCONMAXITEM,
                NVL(M.IDMUNIUTCONVNFE, 'N') AS IDMUNIUTCONVNFE, NVL(PE.IDEMPUTILIZANFE, 'N') IDEMPUTILIZANFE,
                MAX(NVL(NVL(S.VRPRECMANUTSESP, S.VRPRUDOLSESP), 0.0)) VRPRUDOLSESP, 
                MAX(NVL(NVL(S.VRPRECMANUTSESP, S.VRPRURESESP), 0.0)) VRPRURESESP,
                CL.CDCLIENTE, CL.IDTPIJURCLIE, CL.NRINSJURCLIE,
                S.CDSERVICO, PF.CDARVPROD, SC.CDPRODUTO, SC.IDTIPMANUAL, SC.DSSERVICO, SC.CDARVSERV,
                SUM(NVL(S.QTSERFATSP, 0.0)) AS QTSERFATSP,
                MAX(NVL(CUSTO_PRODUTO(P_FILIAL, SC.CDPRODUTO, NVL(PF.CDTIPCUSTEST, '03'), P_DTFECHAME, SYSDATE), 0.0)) AS VRPRODUTO
           FROM SERVPLANO S
                INNER JOIN COMPANHIA C ON C.CDCIA = S.CDCIA
                INNER JOIN SERVICOCIA SC ON SC.CDSERVICO = S.CDSERVICO AND
                                            SC.CDCIA = S.CDCIA
                INNER JOIN PRODUTO PF ON PF.CDPRODUTO = SC.CDPRODUTO
                INNER JOIN CLIENTE CL ON CL.CDCLIENTE = C.CDCLIENTE
                INNER JOIN PRODUTO P ON P.CDPRODUTO = S.CDSERVICO
                INNER JOIN FILIAL F ON F.CDFILIAL = S.CDFILIAL
                LEFT JOIN SERIENF SR ON SR.CDEMPRESA = F.CDEMPRESA AND
                                        SR.CDINSCESTA = F.CDINSCESTA AND
                                        SR.CDINSCMUNI = F.CDINSCMUNI AND
                                        SR.CDSERIE = P_CDSERIE
                LEFT JOIN EMPRESA E ON E.CDEMPRESA = F.CDEMPRESA
                LEFT JOIN PARAMEMPRESA PE ON PE.CDEMPRESA = E.CDEMPRESA
                LEFT JOIN MUNICIPIO M ON M.CDPAIS = E.CDPAIS AND
                                         M.SGESTADO = E.SGESTADO AND
                                         M.CDMUNICIPIO = E.CDMUNICIPIO
                LEFT JOIN PARAMFILIAL PF ON PF.CDFILIAL = S.CDFILIAL
          WHERE NVL(P.IDFATVENDADEVOL, 'NFVENDA') = 'NFDEVOLUCAO'            
            AND SC.IDFATSE  <> '2'
            AND S.CDFILIAL = P_FILIAL
            AND S.CDCIA = P_CDCIA
            AND S.DTHRPLAVOOPV BETWEEN P_DTINIAPUR AND P_DTFIMAPUR
            AND S.NRLANCTONF IS NULL
            AND((P_NRVOO = 'T') OR (INSTR(P_NRVOO, '_'||S.NRVOO) > 0))
          GROUP BY F.CDEMPRESA, F.CDINSCMUNI, F.CDINSCESTA,
                   NVL(SR.NRMAXITNF, 10), NVL(SR.NRMAXSERVNF, 5), SR.IDCONMAXITEM,
                   NVL(M.IDMUNIUTCONVNFE, 'N'), NVL(PE.IDEMPUTILIZANFE, 'N'),
                   CL.CDCLIENTE, CL.IDTPIJURCLIE, CL.NRINSJURCLIE,
                   S.CDSERVICO, PF.CDARVPROD, SC.CDPRODUTO, SC.IDTIPMANUAL, SC.DSSERVICO, SC.CDARVSERV
          ORDER BY SC.IDTIPMANUAL
         ) LOOP
            /* ***********************************************************************************************************************
            Gera uma Nova Nota fiscal se nao tiver lancado nota ou tiver alcancado o maximo de Produtos em uma NF, 
            e controlar quantidade maxima de Itens na NF                 
            *************************************************************************************************************************/
            IF (V_NRLANCTONF IS NULL) OR 
               ((V_QTPRODNF = C_SERV_DEVOL.NRMAXITNF) AND (C_SERV_DEVOL.IDCONMAXITEM = 'S')) THEN
               /* *****************************/
               /* Fecha nota fiscal anterior */
               /* *****************************/
               
               IF (V_NRLANCTONF IS NOT NULL) THEN
                  /* Encargos */
                  INSERT INTO ITEMENCARGO
                  (CDFILIAL, NRLANCTONF, CDENCARGO, VRITEMENCA)
                  (SELECT P_FILIPROC, V_NRLANCTONF, CDENCARGO, 0 AS VRITEMENCA
                   FROM ENCARGO
                   MINUS
                   SELECT CDFILIAL, NRLANCTONF, CDENCARGO, 0 AS VRITEMENCA
                   FROM ITEMENCARGO
                   WHERE CDFILIAL   = P_FILIPROC AND
                         NRLANCTONF = V_NRLANCTONF);
                         
                  /* impostos */
                  IMPOSTO_SAIDA(C_SERV_DEVOL.CDEMPRESA, C_SERV_DEVOL.CDINSCESTA,
                             V_CDTIPOOPERDEVOL, C_SERV_DEVOL.CDCLIENTE,
                             P_FILIPROC, V_NRLANCTONF,
                             P_DTFECHAME, V_VRNOTA, NULL);
                  
                  APURAC_VENC(P_FILIPROC, C_SERV_DEVOL.CDCLIENTE, V_NRLANCTONF);
               END IF;               
               /*------------------------------------------------------ 
                  Busca o Modelo da Nota Fiscal
                  O modelo selecionado sera o Modelo de menor codigo
                  cadastrado para o tipo de operacao da NF, na tela
                  "Modelo de Documento Fiscal", no sistema "Fiscal"
               ------------------------------------------------------*/              
               SELECT MIN(CDMODDOCFISC) CDMODDOCFISC
               INTO V_CDMODDOCFISC
               FROM MODDOCFISCTPOP
               WHERE CDTIPOOPER   = V_CDTIPOOPERDEVOL
               AND IDENTRSAIDOP = 'S';
               /* *****************************
               Nova nota fiscal
               *****************************/
               NOVO_CODIGO_SC('LANCNF/FILIAL' || P_FILIPROC,V_SEQUENCIAL);
               V_NRLANCTONF  := SUBSTR(V_SEQUENCIAL,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, CDMODDOCFISC)
                VALUES (P_FILIPROC, V_NRLANCTONF, C_SERV_DEVOL.CDEMPRESA,
                        C_SERV_DEVOL.CDINSCESTA, C_SERV_DEVOL.CDINSCMUNI, P_FILIAL,
                        NULL, C_SERV_DEVOL.IDTPIJURCLIE, C_SERV_DEVOL.NRINSJURCLIE,
                        NULL, C_SERV_DEVOL.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_CDTIPOOPERDEVOL, P_CDSERIE, P_IDTPIJURTRAN,
                        P_NRINSJURTRAN, 0, 'P', 'FAC', 'S', P_NRAPURACFAT, 'C', V_CDMODDOCFISC);
               
               V_QTPRODNF := 0;
               V_NRSEQITPROD := 0;
            END IF;
            V_NRSEQITPROD := V_NRSEQITPROD + 1;               
            
            IF C_SERV_DEVOL.VRPRODUTO <= 0 THEN
               IF C_SERV_DEVOL.IDTIPMANUAL = 2  THEN
                  V_CUSTOPRODUTO := C_SERV_DEVOL.VRPRUDOLSESP;
               ELSE
                  V_CUSTOPRODUTO := C_SERV_DEVOL.VRPRURESESP;
               END IF;
            ELSE
               V_CUSTOPRODUTO := ROUND(C_SERV_DEVOL.VRPRODUTO, 3);
            END IF;
            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'),
                     C_SERV_DEVOL.CDPRODUTO, C_SERV_DEVOL.CDARVPROD,
                     C_SERV_DEVOL.DSSERVICO, 'N', ROUND(C_SERV_DEVOL.QTSERFATSP, 3),
                     V_CUSTOPRODUTO, 0,
                     ROUND(C_SERV_DEVOL.QTSERFATSP * V_CUSTOPRODUTO, 3),
                     ROUND(C_SERV_DEVOL.QTSERFATSP * V_CUSTOPRODUTO, 3),
                     ROUND(C_SERV_DEVOL.QTSERFATSP * V_CUSTOPRODUTO, 3), 
                     ROUND(C_SERV_DEVOL.QTSERFATSP * C_SERV_DEVOL.VRPRUDOLSESP, 3),
                     'N', V_NRLANCTONF, '***',
                     '***', '00000','00000',
                     ROUND(C_SERV_DEVOL.QTSERFATSP, 3), ROUND(C_SERV_DEVOL.QTSERFATSP, 3));
                     
            /* Atualiza o servico de devolucao nos planos de voo, e modifica o plano de voo para atendido */
            UPDATE SERVPLANO
               SET IDSITSERSP = '2', 
                   CDFILIALNF = P_FILIPROC,
                   NRLANCTONF = V_NRLANCTONF
             WHERE CDSERVICO = C_SERV_DEVOL.CDSERVICO
               AND CDFILIAL = P_FILIAL
               AND CDCIA = P_CDCIA
               AND DTHRPLAVOOPV BETWEEN P_DTINIAPUR AND P_DTFIMAPUR
               AND((P_NRVOO = 'T') OR (INSTR(P_NRVOO, '_'||NRVOO) > 0))
               AND EXISTS (SELECT 1 FROM PRODUTO 
                            WHERE PRODUTO.CDPRODUTO = SERVPLANO.CDSERVICO 
                              AND NVL(PRODUTO.IDFATVENDADEVOL, 'NFVENDA') = 'NFDEVOLUCAO')
               AND EXISTS (SELECT 1 FROM SERVICOCIA
                            WHERE SERVICOCIA.CDCIA = SERVPLANO.CDCIA
                              AND SERVICOCIA.CDSERVICO = SERVPLANO.CDSERVICO
                              AND SERVICOCIA.IDFATSE  <> '2');
         END LOOP;
         
         /* *****************************/
         /* Fecha nota fiscal anterior */
         /* *****************************/
         
         IF (V_NRLANCTONF IS NOT NULL) THEN
            /* Encargos */
            INSERT INTO ITEMENCARGO
            (CDFILIAL, NRLANCTONF, CDENCARGO, VRITEMENCA)
            (SELECT P_FILIPROC, V_NRLANCTONF, CDENCARGO, 0 AS VRITEMENCA
             FROM ENCARGO
             MINUS
             SELECT CDFILIAL, NRLANCTONF, CDENCARGO, 0 AS VRITEMENCA
             FROM ITEMENCARGO
             WHERE CDFILIAL   = P_FILIPROC AND
                   NRLANCTONF = V_NRLANCTONF);
                  
            /* impostos */
            IMPOSTO_SAIDA(V_CDEMPRESA, V_INSCESTA,
                       V_CDTIPOOPERDEVOL, V_CDCLIENTE,
                       P_FILIPROC, V_NRLANCTONF,
                       P_DTFECHAME, V_VRNOTA, NULL);
            
            APURAC_VENC(P_FILIPROC, V_CDCLIENTE, V_NRLANCTONF);
         END IF;

         UPDATE PLANOVOO            
            SET IDSITABASPV = '2'
          WHERE CDFILIAL = P_FILIAL
            AND CDCIA = P_CDCIA
            AND DTHRPLAVOOPV BETWEEN P_DTINIAPUR AND P_DTFIMAPUR
            AND ((P_NRVOO = 'T') OR (INSTR(P_NRVOO, '_'||NRVOO) > 0))
            AND NOT EXISTS 
            (
            SELECT 1 FROM SERVPLANO
              WHERE SERVPLANO.CDFILIAL = PLANOVOO.CDFILIAL
                AND SERVPLANO.CDCIA = PLANOVOO.CDCIA
                AND SERVPLANO.NRVOO = PLANOVOO.NRVOO
                AND SERVPLANO.CDTRECHO = PLANOVOO.CDTRECHO
                AND SERVPLANO.DTHRPLAVOOPV  = PLANOVOO.DTHRPLAVOOPV
                AND SERVPLANO.IDSITSERSP = '1'
            );          
         
      END IF;
      
      COMMIT;
   ELSE
      raise_application_error(-20501,'TpFt_N_Cad', True);
   END IF;
END;
/