CREATE OR REPLACE PROCEDURE GRAVA_PAGAMENTO_ELETRONICO(
   P_NRSEQUMAPAPU    IN MAPAAPURAC.NRSEQUMAPAPU%TYPE,
   P_DTINICIAL       IN MAPAAPURAC.DTINIPERIODO%TYPE,
   P_DTFINAL         IN MAPAAPURAC.DTINIPERIODO%TYPE,
   P_CDEMPRESA       IN MAPAAPURAC.CDEMPRESA%TYPE,
   P_CDINSCESTA       IN MAPAAPURAC.CDINSCESTA%TYPE)
AS
   V_TOTALBOL    FLOAT;
   V_VRPROPOR    FLOAT;
   V_VROUTROS    FLOAT;
   V_VRICMS      FLOAT;
   V_VRISS       FLOAT;
   V_NOVOCODIGO  VARCHAR2(20);
BEGIN
    DELETE FROM PAGELETRONICO
    WHERE CDINSCESTA = P_CDINSCESTA
      AND DTINIAPUR = P_DTINICIAL
      AND DTFIMAPUR = P_DTFINAL;

  /*LANCAMENTOS DIRETO NO REC*/
  FOR C_RECGARANTIDO IN (SELECT --SUM(VRTPRECBOLR) OVER(PARTITION BY TB.CDFILIAL, TB.NRLANBOLREC, B.NRORIGBOLREC) AS TOTALRECPARCELAS,
                                TB.CDFILIAL AS CDFILIBOLREC, TB.NRLANBOLREC, B.NRORIGBOLREC,
                                TP.CDTIPORECE, N.CDFILIAL, N.NRLANCTONF, N.DTEMISSAO, N.VRNOTAFISC, B.VRBOLREC,
                                NVL(AD.CDADMINCART, TP.NRCNPJPAG) AS NRCNPJPAG,  NVL(AD.CDADMINCARTTRANS,TP.NRCNPJPAGTRANS) AS NRCNPJPAGTRANS,
                                ROUND((TB.VRTPRECBOLR * B.VRBOLREC) / VRFINALBREC,2) AS VROUTROS
                           FROM NOTAFISCAL N, CONTARECEBER C, BOLETRECEBER B, TPRECBOLETO TB, TIPORECE TP, ADMINCARTFIL AD
                          WHERE N.IDSISTEMA = 'REC'
                            AND N.CDEMPRESA      = P_CDEMPRESA
                            AND N.CDINSCESTA     = P_CDINSCESTA
                            AND N.CDFILIAL       = C.CDFILIAL
                            AND N.NRLANCTONF     = C.NRLANCTONF
                            AND C.CDFILIBOLREC   = B.CDFILIAL
                            AND C.NRLANBOLREC    = B.NRLANBOLREC
                            AND TB.CDFILIAL      = B.CDFILIAL
                            AND TB.NRLANBOLREC   = B.NRLANBOLREC
                            AND TP.CDTIPORECE    = TB.CDTIPORECE
                            AND TP.IDMEIOPAGELE  = 'S'
                            AND TP.IDRECEGARANTIDO = 'S'
                            AND N.DTEMISSAO BETWEEN P_DTINICIAL AND P_DTFINAL
                            AND AD.CDTIPORECE(+)    = TB.CDTIPORECE
                            AND AD.CDFILIAL(+)      = TB.CDFILIAL) LOOP

        NOVO_CODIGO_NT('PAGELETRONICO', V_NOVOCODIGO);
        V_NOVOCODIGO := SUBSTR(V_NOVOCODIGO,11,10);

       INSERT INTO PAGELETRONICO
          (NRSEQUENCIAL, NRSEQUMAPAPU, CDEMPRESA, CDINSCESTA, IDORIGEM, CDFILIAL, NRLANCTONF, CDFILIBOLREC, NRLANBOLREC, DTEMISSAO, CDTIPORECE,
           DTPAGAMENTO, VRNOTAFISC, VRRECEBIMENTO, VRICMS, VRISS, VROUTROS, NRCNPJPAG, NRCNPJPAGTRANS, DTINIAPUR, DTFIMAPUR, IDRECEGARANTIDO)
       VALUES
          (V_NOVOCODIGO, P_NRSEQUMAPAPU, P_CDEMPRESA, P_CDINSCESTA, 'REC', C_RECGARANTIDO.CDFILIAL, C_RECGARANTIDO.NRLANCTONF,C_RECGARANTIDO.CDFILIBOLREC, C_RECGARANTIDO.NRLANBOLREC, C_RECGARANTIDO.DTEMISSAO, C_RECGARANTIDO.CDTIPORECE,
           NULL, C_RECGARANTIDO.VRNOTAFISC, C_RECGARANTIDO.VRBOLREC, 0, 0, C_RECGARANTIDO.VROUTROS, C_RECGARANTIDO.NRCNPJPAG, C_RECGARANTIDO.NRCNPJPAGTRANS, P_DTINICIAL, P_DTFINAL,'S');
  END LOOP;

  FOR C_RECSEMGARANTIA IN (SELECT --SUM(VRTPRECBOLR) OVER(PARTITION BY TB.CDFILIAL, TB.NRLANBOLREC, B.NRORIGBOLREC) AS TOTALRECPARCELAS,
                                TB.CDFILIAL AS CDFILIBOLREC, TB.NRLANBOLREC, B.NRORIGBOLREC,
                                TP.CDTIPORECE, N.CDFILIAL, N.NRLANCTONF, N.DTEMISSAO, C.DTMOVFLUXO, N.VRNOTAFISC, B.VRBOLREC,
                                NVL(AD.CDADMINCART, TP.NRCNPJPAG) AS NRCNPJPAG,  NVL(AD.CDADMINCARTTRANS,TP.NRCNPJPAGTRANS) AS NRCNPJPAGTRANS,
                                ROUND((TB.VRTPRECBOLR * B.VRBOLREC) / VRFINALBREC,2) AS VROUTROS
                           FROM NOTAFISCAL N, CONTARECEBER C, BOLETRECEBER B, TPRECBOLETO TB, TIPORECE TP, ADMINCARTFIL AD
                          WHERE N.IDSISTEMA = 'REC'
                            AND N.CDEMPRESA      = P_CDEMPRESA
                            AND N.CDINSCESTA     = P_CDINSCESTA
                            AND N.CDFILIAL       = C.CDFILIAL
                            AND N.NRLANCTONF     = C.NRLANCTONF
                            AND C.CDFILIBOLREC   = B.CDFILIAL
                            AND C.NRLANBOLREC    = B.NRLANBOLREC
                            AND TB.CDFILIAL      = B.CDFILIAL
                            AND TB.NRLANBOLREC   = B.NRLANBOLREC
                            AND TP.CDTIPORECE    = TB.CDTIPORECE
                            AND TP.IDMEIOPAGELE  = 'S'
                            AND NVL(TP.IDRECEGARANTIDO,'N') <> 'S'
                            AND C.IDSTATREC = 'B'
                            AND C.DTMOVFLUXO BETWEEN P_DTINICIAL AND P_DTFINAL
                            AND AD.CDTIPORECE(+)    = TB.CDTIPORECE
                            AND AD.CDFILIAL(+)      = TB.CDFILIAL) LOOP
       NOVO_CODIGO_NT('PAGELETRONICO', V_NOVOCODIGO);
       V_NOVOCODIGO := SUBSTR(V_NOVOCODIGO,11,10);

       INSERT INTO PAGELETRONICO
          (NRSEQUENCIAL, NRSEQUMAPAPU, CDEMPRESA, CDINSCESTA, IDORIGEM, CDFILIAL, NRLANCTONF, CDFILIBOLREC, NRLANBOLREC, DTEMISSAO, CDTIPORECE,
           DTPAGAMENTO, VRNOTAFISC, VRRECEBIMENTO, VRICMS, VRISS, VROUTROS, NRCNPJPAG, NRCNPJPAGTRANS, DTINIAPUR, DTFIMAPUR, IDRECEGARANTIDO)
       VALUES
          (V_NOVOCODIGO, P_NRSEQUMAPAPU, P_CDEMPRESA, P_CDINSCESTA, 'REC', C_RECSEMGARANTIA.CDFILIAL, C_RECSEMGARANTIA.NRLANCTONF,C_RECSEMGARANTIA.CDFILIBOLREC, C_RECSEMGARANTIA.NRLANBOLREC, C_RECSEMGARANTIA.DTEMISSAO, C_RECSEMGARANTIA.CDTIPORECE,
           C_RECSEMGARANTIA.DTMOVFLUXO, C_RECSEMGARANTIA.VRNOTAFISC, C_RECSEMGARANTIA.VRBOLREC, 0, 0, C_RECSEMGARANTIA.VROUTROS, C_RECSEMGARANTIA.NRCNPJPAG, C_RECSEMGARANTIA.NRCNPJPAGTRANS, P_DTINICIAL, P_DTFINAL,'N');
  END LOOP;

  /*NOTAS*/
  FOR C_NFGARANTIDO IN (SELECT N.CDFILIAL, N.NRLANCTONF, B.CDFILIAL AS CDFILIBOLREC, B.NRLANBOLREC, N.VRNOTAFISC,
                               TB.VRTPRECBOLR,
                               CASE WHEN C.VRORIGREC = 0 THEN 1 ELSE C.VRORIGREC END AS VRORIGREC,
                               CASE WHEN B.VRFINALBREC = 0 THEN 1 ELSE B.VRFINALBREC END AS VRFINALBREC,
                               N.IDSISTEMA, TB.CDTIPORECE, N.NRNOTAFISC, N.DTEMISSAO,  NVL(AD.CDADMINCART, TR.NRCNPJPAG) AS NRCNPJPAG,  NVL(AD.CDADMINCARTTRANS,TR.NRCNPJPAGTRANS) AS NRCNPJPAGTRANS
                               -- SUM(I.VRIMPOSTONF) OVER(PARTITION BY TB.CDTIPORECE) AS TOTALIMPOSTO,
                               --SUM(TB.VRTPRECBOLR) OVER(PARTITION BY I.CDIMPOSTO) AS TOTALRECEBIDO
                           FROM NOTAFISCAL N, CONTARECEBER C, BOLETRECEBER B, TPRECBOLETO TB, TIPORECE TR, ADMINCARTFIL AD
                          WHERE TB.CDFILIAL        = C.CDFILIBOLREC
                            AND TB.NRLANBOLREC     = C.NRLANBOLREC
                            AND C.CDFILIAL         = N.CDFILIAL
                            AND C.NRLANCTONF       = N.NRLANCTONF
                            AND B.CDFILIAL         = TB.CDFILIAL
                            AND B.NRLANBOLREC      = TB.NRLANBOLREC
                            AND TR.CDTIPORECE      = TB.CDTIPORECE
                            AND TR.IDMEIOPAGELE    = 'S'
                            AND TR.IDRECEGARANTIDO = 'S'
                            AND N.CDEMPRESA        = P_CDEMPRESA
                            AND N.CDINSCESTA       = P_CDINSCESTA
                            AND N.IDENTRSAIDOP     = 'S'
                            AND N.IDSISTEMA NOT IN ('REC', 'FIS', 'TMP', 'CNC')
                            AND N.DTEMISSAO BETWEEN P_DTINICIAL AND P_DTFINAL
                            AND AD.CDTIPORECE(+)    = TB.CDTIPORECE
                            AND AD.CDFILIAL(+)      = TB.CDFILIAL) LOOP

    V_VRICMS := 0;
    V_VRISS  := 0;
    V_VRPROPOR := 0;

    SELECT VALOR_IMPOSTO_NFE(C_NFGARANTIDO.CDFILIAL, C_NFGARANTIDO.NRLANCTONF,'00','C'),
           VALOR_IMPOSTO_NFE(C_NFGARANTIDO.CDFILIAL, C_NFGARANTIDO.NRLANCTONF,'31','C')
      INTO V_VRICMS, V_VRISS
      FROM DUAL;

   /* IF  VRNOTAFISC - TOTAL_ICMS - TOTAL_ISS  > 0 THEN VROUTROS :=  VALORNF - VALORICMS - VALORISS ELSE VROUTROS := 0 ;    */
    IF ROUND(C_NFGARANTIDO.VRNOTAFISC - V_VRICMS - V_VRISS,2) > 0 THEN
       V_VROUTROS := ROUND(C_NFGARANTIDO.VRNOTAFISC - V_VRICMS - V_VRISS,2);
    ELSE
       V_VROUTROS := 0;
    END IF;

    /*  prop RECEBIMENTO =  ROUND(vr. recebimento * vr. original DA CONTA / vr. final DO BOLETO,2)  */
    V_VRPROPOR := ROUND(C_NFGARANTIDO.VRTPRECBOLR * C_NFGARANTIDO.VRORIGREC / C_NFGARANTIDO.VRFINALBREC,2);

    IF V_VRICMS > 0 THEN
       V_VRICMS := ROUND((V_VRICMS * V_VRPROPOR) / C_NFGARANTIDO.VRORIGREC,2);
    END IF;

    IF V_VRISS > 0 THEN
       V_VRISS := ROUND((V_VRISS * V_VRPROPOR) / C_NFGARANTIDO.VRORIGREC,2);
    END IF;

    IF V_VROUTROS > 0 THEN
       V_VROUTROS := ROUND((V_VROUTROS * V_VRPROPOR) / C_NFGARANTIDO.VRORIGREC,2);
    END IF;

    NOVO_CODIGO_NT('PAGELETRONICO', V_NOVOCODIGO);
    V_NOVOCODIGO := SUBSTR(V_NOVOCODIGO,11,10);

    INSERT INTO PAGELETRONICO
       (NRSEQUENCIAL, NRSEQUMAPAPU, CDEMPRESA, CDINSCESTA, IDORIGEM, CDFILIAL, NRLANCTONF, CDFILIBOLREC, NRLANBOLREC, DTEMISSAO, CDTIPORECE,
        DTPAGAMENTO, VRNOTAFISC, VRRECEBIMENTO, VRICMS, VRISS, VROUTROS, NRCNPJPAG, NRCNPJPAGTRANS, DTINIAPUR, DTFIMAPUR, IDRECEGARANTIDO)
    VALUES
       (V_NOVOCODIGO, P_NRSEQUMAPAPU, P_CDEMPRESA, P_CDINSCESTA, C_NFGARANTIDO.IDSISTEMA, C_NFGARANTIDO.CDFILIAL, C_NFGARANTIDO.NRLANCTONF, C_NFGARANTIDO.CDFILIBOLREC, C_NFGARANTIDO.NRLANBOLREC, C_NFGARANTIDO.DTEMISSAO, C_NFGARANTIDO.CDTIPORECE,
        NULL, C_NFGARANTIDO.VRNOTAFISC, V_VRPROPOR, V_VRICMS, V_VRISS, V_VROUTROS, C_NFGARANTIDO.NRCNPJPAG, C_NFGARANTIDO.NRCNPJPAGTRANS, P_DTINICIAL, P_DTFINAL, 'S');
  END LOOP;

  FOR C_NFSEMGARANTIA IN (SELECT N.CDFILIAL, N.NRLANCTONF, B.CDFILIAL AS CDFILIBOLREC, B.NRLANBOLREC, N.VRNOTAFISC,
                                 TB.VRTPRECBOLR, C.DTMOVFLUXO,
                                 CASE WHEN C.VRORIGREC = 0 THEN 1 ELSE C.VRORIGREC END AS VRORIGREC,
                                 CASE WHEN B.VRFINALBREC = 0 THEN 1 ELSE B.VRFINALBREC END AS VRFINALBREC,
                                 N.IDSISTEMA, TB.CDTIPORECE, N.NRNOTAFISC, N.DTEMISSAO,  NVL(AD.CDADMINCART, TR.NRCNPJPAG) AS NRCNPJPAG,  NVL(AD.CDADMINCARTTRANS,TR.NRCNPJPAGTRANS) AS NRCNPJPAGTRANS
                         FROM NOTAFISCAL N,  CONTARECEBER C, BOLETRECEBER B, TPRECBOLETO TB, TIPORECE TR, ADMINCARTFIL AD
                        WHERE TB.CDFILIAL        = C.CDFILIBOLREC
                          AND TB.NRLANBOLREC     = C.NRLANBOLREC
                          AND C.CDFILIAL         = N.CDFILIAL
                          AND C.NRLANCTONF       = N.NRLANCTONF
                          AND B.CDFILIAL         = TB.CDFILIAL
                          AND B.NRLANBOLREC      = TB.NRLANBOLREC
                          AND TR.CDTIPORECE      = TB.CDTIPORECE
                          AND TR.IDMEIOPAGELE    = 'S'
                          AND NVL(TR.IDRECEGARANTIDO,'N') <> 'S'
                          AND N.CDEMPRESA        = P_CDEMPRESA
                          AND N.CDINSCESTA       = P_CDINSCESTA
                          AND N.IDENTRSAIDOP     = 'S'
                          AND N.IDSISTEMA NOT IN ('REC', 'FIS', 'TMP', 'CNC')
                          AND C.DTMOVFLUXO BETWEEN P_DTINICIAL AND P_DTFINAL
                          AND C.IDSTATREC = 'B'
                          AND AD.CDTIPORECE(+)    = TB.CDTIPORECE
                          AND AD.CDFILIAL(+)      = TB.CDFILIAL) LOOP

    V_VRICMS := 0;
    V_VRISS  := 0;
    V_VRPROPOR := 0;

    SELECT VALOR_IMPOSTO_NFE(C_NFSEMGARANTIA.CDFILIAL, C_NFSEMGARANTIA.NRLANCTONF,'00','C'),
           VALOR_IMPOSTO_NFE(C_NFSEMGARANTIA.CDFILIAL, C_NFSEMGARANTIA.NRLANCTONF,'31','C')
      INTO V_VRICMS, V_VRISS
      FROM DUAL;

   /* IF  VRNOTAFISC - TOTAL_ICMS - TOTAL_ISS  > 0 THEN VROUTROS :=  VALORNF - VALORICMS - VALORISS ELSE VROUTROS := 0 ;  */
    IF ROUND(C_NFSEMGARANTIA.VRNOTAFISC - V_VRICMS - V_VRISS,2) > 0 THEN
       V_VROUTROS := ROUND(C_NFSEMGARANTIA.VRNOTAFISC - V_VRICMS - V_VRISS,2);
    ELSE
       V_VROUTROS := 0;
    END IF;

    /*  prop RECEBIMENTO =  ROUND(vr. recebimento * vr. original DA CONTA / vr. final DO BOLETO,2)  */
    V_VRPROPOR := ROUND(C_NFSEMGARANTIA.VRTPRECBOLR * C_NFSEMGARANTIA.VRORIGREC / C_NFSEMGARANTIA.VRFINALBREC,2);

    IF V_VRICMS > 0 THEN
       V_VRICMS := ROUND((V_VRICMS * V_VRPROPOR) / C_NFSEMGARANTIA.VRORIGREC,2);
    END IF;

    IF V_VRISS > 0 THEN
       V_VRISS := ROUND((V_VRISS * V_VRPROPOR) / C_NFSEMGARANTIA.VRORIGREC,2);
    END IF;

    IF V_VROUTROS > 0 THEN
       V_VROUTROS := ROUND((V_VROUTROS * V_VRPROPOR) / C_NFSEMGARANTIA.VRORIGREC,2);
    END IF;

    NOVO_CODIGO_NT('PAGELETRONICO', V_NOVOCODIGO);
    V_NOVOCODIGO := SUBSTR(V_NOVOCODIGO,11,10);

    INSERT INTO PAGELETRONICO
       (NRSEQUENCIAL, NRSEQUMAPAPU, CDEMPRESA, CDINSCESTA, IDORIGEM, CDFILIAL, NRLANCTONF, CDFILIBOLREC, NRLANBOLREC, DTEMISSAO, CDTIPORECE,
        DTPAGAMENTO, VRNOTAFISC, VRRECEBIMENTO, VRICMS, VRISS, VROUTROS, NRCNPJPAG, NRCNPJPAGTRANS, DTINIAPUR, DTFIMAPUR, IDRECEGARANTIDO)
    VALUES
       (V_NOVOCODIGO, P_NRSEQUMAPAPU, P_CDEMPRESA, P_CDINSCESTA, C_NFSEMGARANTIA.IDSISTEMA, C_NFSEMGARANTIA.CDFILIAL, C_NFSEMGARANTIA.NRLANCTONF, C_NFSEMGARANTIA.CDFILIBOLREC, C_NFSEMGARANTIA.NRLANBOLREC, C_NFSEMGARANTIA.DTEMISSAO, C_NFSEMGARANTIA.CDTIPORECE,
        C_NFSEMGARANTIA.DTMOVFLUXO, C_NFSEMGARANTIA.VRNOTAFISC, V_VRPROPOR, V_VRICMS, V_VRISS, V_VROUTROS, C_NFSEMGARANTIA.NRCNPJPAG, C_NFSEMGARANTIA.NRCNPJPAGTRANS, P_DTINICIAL, P_DTFINAL, 'S');
  END LOOP;

  /*NOTAS DO FOS/CNC* REFERENTE AO FISCAL*/
  FOR C_ECFGARANTIDO IN (SELECT NVL(AD.CDADMINCART, TP.NRCNPJPAG) AS NRCNPJPAG,  NVL(AD.CDADMINCARTTRANS,TP.NRCNPJPAGTRANS) AS NRCNPJPAGTRANS, TP.CDTIPORECE, VRMOVVEND AS VRICMS,
                                NF.CDFILIAL, NF.NRLANCTONF, NF.DTEMISSAO, NF.VRNOTAFISC
                            FROM NOTAFISCAL NF, TIPORECE TP, ADMINCARTFIL AD,
                                (SELECT MV.CDFILIALNFFIS, MV.NRLANCTONFFIS, MV.CDTIPORECE,
                                       SUM(TRUNC(DECODE(MV.IDTIPOMOVIVE,'T',VRMOVIVEND,DECODE(MV.IDTIPOMOVIVE, 'E',VRMOVIVEND, DECODE(MV.IDTIPOMOVIVE, 'S',-VRMOVIVEND,0))),2)) AS VRMOVVEND
                                   FROM MOVCAIXA MV
                                  WHERE DTMOVIMCAIXA BETWEEN P_DTINICIAL AND P_DTFINAL
                                  GROUP BY MV.CDFILIALNFFIS, MV.NRLANCTONFFIS, MV.CDTIPORECE) MV
                           WHERE MV.CDTIPORECE = TP.CDTIPORECE
                             AND NF.DTEMISSAO BETWEEN P_DTINICIAL AND P_DTFINAL
                             AND NF.CDEMPRESA     = P_CDEMPRESA
                             AND NF.CDINSCESTA    = P_CDINSCESTA
                             AND MV.CDFILIALNFFIS = NF.CDFILIAL
                             AND MV.NRLANCTONFFIS = NF.NRLANCTONF
                             AND TP.IDMEIOPAGELE  = 'S'
                             AND TP.IDRECEGARANTIDO = 'S'
                             AND AD.CDTIPORECE(+)   = MV.CDTIPORECE
                             AND AD.CDFILIAL(+)     = MV.CDFILIALNFFIS) LOOP

     NOVO_CODIGO_NT('PAGELETRONICO', V_NOVOCODIGO);
     V_NOVOCODIGO := SUBSTR(V_NOVOCODIGO,11,10);

     INSERT INTO PAGELETRONICO
        (NRSEQUENCIAL, NRSEQUMAPAPU,CDEMPRESA, CDINSCESTA, IDORIGEM, CDFILIAL, NRLANCTONF, CDFILIBOLREC, NRLANBOLREC, DTEMISSAO, CDTIPORECE,
         DTPAGAMENTO, VRNOTAFISC, VRRECEBIMENTO, VRICMS, VRISS, VROUTROS, NRCNPJPAG, NRCNPJPAGTRANS, DTINIAPUR, DTFIMAPUR, IDRECEGARANTIDO)
     VALUES
        (V_NOVOCODIGO, P_NRSEQUMAPAPU, P_CDEMPRESA, P_CDINSCESTA, 'CFE', C_ECFGARANTIDO.CDFILIAL, C_ECFGARANTIDO.NRLANCTONF, NULL, NULL, C_ECFGARANTIDO.DTEMISSAO, C_ECFGARANTIDO.CDTIPORECE,
         NULL, C_ECFGARANTIDO.VRNOTAFISC, C_ECFGARANTIDO.VRICMS, C_ECFGARANTIDO.VRICMS, 0, 0, C_ECFGARANTIDO.NRCNPJPAG, C_ECFGARANTIDO.NRCNPJPAGTRANS, P_DTINICIAL, P_DTFINAL,'S');
  END LOOP;

  FOR C_ECFSEMGARANTIA IN (SELECT NVL(AD.CDADMINCART, TP.NRCNPJPAG) AS NRCNPJPAG, NVL(AD.CDADMINCARTTRANS,TP.NRCNPJPAGTRANS) AS NRCNPJPAGTRANS, TP.CDTIPORECE, VRMOVVEND AS VRICMS,
                                  NF.CDFILIAL, NF.NRLANCTONF, NF.DTEMISSAO, NF.VRNOTAFISC, C.DTMOVFLUXO
                             FROM NOTAFISCAL NF, TIPORECE TP, CONTARECEBER C, ADMINCARTFIL AD,
                                  (SELECT MV.CDFILIALNF, MV.NRLANCTONF, MV.CDTIPORECE,
                                         SUM(TRUNC(DECODE(MV.IDTIPOMOVIVE,'T',VRMOVIVEND,DECODE(MV.IDTIPOMOVIVE, 'E',VRMOVIVEND, DECODE(MV.IDTIPOMOVIVE, 'S',-VRMOVIVEND,0))),2)) AS VRMOVVEND
                                   FROM MOVCAIXA MV
                                  WHERE DTMOVIMCAIXA BETWEEN P_DTINICIAL AND P_DTFINAL
                                  GROUP BY MV.CDFILIALNF, MV.NRLANCTONF, MV.CDTIPORECE) MV
                            WHERE MV.CDTIPORECE = TP.CDTIPORECE
                              AND C.DTMOVFLUXO BETWEEN P_DTINICIAL AND P_DTFINAL
                              AND NF.CDEMPRESA     = P_CDEMPRESA
                              AND NF.CDINSCESTA    = P_CDINSCESTA
                              AND MV.CDFILIALNF    = NF.CDFILIAL
                              AND MV.NRLANCTONF    = NF.NRLANCTONF
                              AND TP.IDMEIOPAGELE  = 'S'
                              AND NVL(TP.IDRECEGARANTIDO,'N') <> 'S'
                              AND C.CDFILIAL       = NF.CDFILIAL
                              AND C.NRLANCTONF     = NF.NRLANCTONF
                              AND C.IDSTATREC      = 'B'
                              AND AD.CDTIPORECE(+) = MV.CDTIPORECE
                              AND AD.CDFILIAL(+)   = MV.CDFILIALNF) LOOP

     NOVO_CODIGO_NT('PAGELETRONICO', V_NOVOCODIGO);
     V_NOVOCODIGO := SUBSTR(V_NOVOCODIGO,11,10);

     INSERT INTO PAGELETRONICO
        (NRSEQUENCIAL, NRSEQUMAPAPU, CDEMPRESA, CDINSCESTA, IDORIGEM, CDFILIAL, NRLANCTONF, CDFILIBOLREC, NRLANBOLREC, DTEMISSAO, CDTIPORECE,
         DTPAGAMENTO, VRNOTAFISC, VRRECEBIMENTO, VRICMS, VRISS, VROUTROS, NRCNPJPAG, NRCNPJPAGTRANS, DTINIAPUR, DTFIMAPUR, IDRECEGARANTIDO)
     VALUES
        (V_NOVOCODIGO, P_NRSEQUMAPAPU, P_CDEMPRESA, P_CDINSCESTA, 'CFE', C_ECFSEMGARANTIA.CDFILIAL, C_ECFSEMGARANTIA.NRLANCTONF, NULL, NULL, C_ECFSEMGARANTIA.DTEMISSAO, C_ECFSEMGARANTIA.CDTIPORECE,
         C_ECFSEMGARANTIA.DTMOVFLUXO, C_ECFSEMGARANTIA.VRNOTAFISC, C_ECFSEMGARANTIA.VRICMS, C_ECFSEMGARANTIA.VRICMS, 0, 0, C_ECFSEMGARANTIA.NRCNPJPAG, C_ECFSEMGARANTIA.NRCNPJPAGTRANS, P_DTINICIAL, P_DTFINAL, 'N');
   END LOOP;
END;
/
