CREATE OR REPLACE PROCEDURE GERA_CISP(
   P_CDEMPRESA    IN EMPRESA.CDEMPRESA%TYPE,
   P_CDASSOCIADO  IN VARCHAR2,
   P_NRSEQARQMAG  IN NOTAFISCAL.NRLANCTONF%TYPE)
AS
   V_PCDUCM      VARCHAR2(20);
   V_PCVULC      VARCHAR2(20);
   V_PCVMAC      VARCHAR2(20);
   V_PCVRMAC     FLOAT;
   V_PCVRMACANT  FLOAT;
   V_PCVVRSAT    FLOAT;
   V_PCVSAT      VARCHAR2(20);
   V_PCDMAC      VARCHAR2(20);
   V_PCDMACANT   VARCHAR2(20);
   V_PCVDAV      VARCHAR2(20);
   V_PCMDAV      VARCHAR2(20);
   V_PCMPMV      VARCHAR2(20);
   V_PCDATV      VARCHAR2(20);
   V_PCMPTV      VARCHAR2(20);
   V_PCV15D      VARCHAR2(20);
   V_PCM15D      VARCHAR2(20);
   V_PCV30D      VARCHAR2(20);
   V_PCM30D      VARCHAR2(20);
   V_PCDTPC      VARCHAR2(20);
   V_PCVPCO      VARCHAR2(20);
   V_PCQPAG      VARCHAR2(20);
   V_PCQDAP      VARCHAR2(20);
   V_PCVPA       VARCHAR2(20);
   V_PCSVV       VARCHAR2(20);
   V_LINHA       VARCHAR2(30000);
   V_CONTADOR    NUMBER;
   V_DATA_CORTE  DATE;
   V_VRACUM      FLOAT;
   V_PCVRAUX     FLOAT;
   v_PCVLCR      varchar2(50);
   v_PCVLG       varchar2(50);
BEGIN
   V_DATA_CORTE := trunc(sysdate)-1;
   V_CONTADOR := 0;
   /* BUSCA TODOS AS CONTAS RECEBER */
   FOR C_CONTAS IN (SELECT DISTINCT DECODE(CL.IDTPIJURCLIE, 'J', '1', DECODE(CL.IDTPIJURCLIE, 'F','2','')) AS PCTIPO, /*CAMPO1*/
                           P_CDASSOCIADO AS PCCASS, /*CAMPO2*/
                           DECODE(CL.IDTPIJURCLIE, 'J', SUBSTR(NRINSJURCLIE,1,8), DECODE(CL.IDTPIJURCLIE, 'F',NRINSJURCLIE,'')) AS PCCCLI, /*CAMPO3*/
                           TO_CHAR(V_DATA_CORTE, 'YYYYMMDD') AS PCDDAT, /*CAMPO4*/
                           MENOR_DATA_CLIENTE(DECODE(IDTPIJURCLIE, 'J', SUBSTR(NRINSJURCLIE,1,8), DECODE(IDTPIJURCLIE, 'F',NRINSJURCLIE,'')),V_DATA_CORTE) AS PCDCDD, /*CAMPO5*/
                           --LTRIM(REPLACE(TO_CHAR(NVL(sum(VRLIMCREDCLI),0),'0000000000000.00'),'.','')) AS PCVLCR, /*CAMPO11*/
                           '6' AS PCVSIT,/*CAMPO25*/
                           max(NVL(IDGARANTCLIE,'0')) AS PCTIPG, /*CAMPO26*/
                           max(LTRIM(TO_CHAR(NVL(IDGRAUGARANT,'0'),'00'))) AS PCGGA,  /*CAMPO27*/
                           max(NVL(TO_CHAR(DTVALIDGARANT, 'YYYYMMDD'), LPAD('0',8,'0'))) AS PCDTG--, /*CAMPO28*/
                           --LTRIM(REPLACE(TO_CHAR(NVL(sum(VRGRANTCLIE),0),'0000000000000.00'),'.','')) AS PCVLG /*CAMPO29*/
                      FROM CONTARECEBER C, NOTAFISCAL N, CLIENTE CL
                     WHERE C.CDFILIAL = N.CDFILIAL
                       AND C.NRLANCTONF = N.NRLANCTONF
                       AND N.CDCLIENTE = CL.CDCLIENTE
                       AND N.IDENTRSAIDOP = 'S'
                       AND IDSISTEMA IN ('DFS', 'REC', 'EXP')
                       AND CL.IDTPIJURCLIE NOT IN ('O', 'I')
                       AND N.CDEMPRESA = P_CDEMPRESA
                       AND NVL(N.IDNFALTERADA,'N') <> 'S'
                       AND CL.IDTPIJURCLIE = 'J'
                       --AND IDSTATREC IN ('A','P')
                   --    AND DECODE(IDTPIJURCLIE, 'J', SUBSTR(NRINSJURCLIE,1,8), DECODE(IDTPIJURCLIE, 'F',NRINSJURCLIE,'')) = '02105874'
                       AND N.DTEMISSAO BETWEEN ADD_MONTHS(V_DATA_CORTE,-12) AND V_DATA_CORTE
                     GROUP BY DECODE(CL.IDTPIJURCLIE, 'J', '1', DECODE(CL.IDTPIJURCLIE, 'F','2','')),
                           DECODE(CL.IDTPIJURCLIE, 'J', SUBSTR(NRINSJURCLIE,1,8), DECODE(CL.IDTPIJURCLIE, 'F',NRINSJURCLIE,'')),
                           MENOR_DATA_CLIENTE(DECODE(IDTPIJURCLIE, 'J', SUBSTR(NRINSJURCLIE,1,8), DECODE(IDTPIJURCLIE, 'F',NRINSJURCLIE,'')),V_DATA_CORTE)
                     ORDER BY 1  ) LOOP
      
      SELECT LTRIM(REPLACE(TO_CHAR(NVL(sum(VRLIMCREDCLI),0),'0000000000000.00'),'.','')), 
             LTRIM(REPLACE(TO_CHAR(NVL(sum(VRGRANTCLIE),0),'0000000000000.00'),'.',''))
        INTO v_PCVLCR, v_PCVLG
        FROM CLIENTE
       WHERE DECODE(IDTPIJURCLIE, 'J', SUBSTR(NRINSJURCLIE,1,8), DECODE(IDTPIJURCLIE, 'F',NRINSJURCLIE,'')) = C_CONTAS.PCCCLI;
       
      SELECT TO_CHAR((DTEMISSAO), 'YYYYMMDD'), LTRIM(REPLACE(TO_CHAR((VRFINALBREC),'0000000000000.00'),'.',''))
        INTO V_PCDUCM,   V_PCVULC
             /*CAMPO6*/  /*CAMPO7*/
        FROM (SELECT DTEMISSAO, VRFINALBREC, ROWNUM AS LINHA1
                FROM  (SELECT DISTINCT C.VRORIGREC AS VRFINALBREC, N.DTEMISSAO AS DTEMISSAO
                         FROM NOTAFISCAL N, CONTARECEBER C, CLIENTE CL
                        WHERE N.CDFILIAL = C.CDFILIAL
                          AND N.NRLANCTONF = C.NRLANCTONF
                          AND N.CDCLIENTE = CL.CDCLIENTE
                          AND DECODE(IDTPIJURCLIE, 'J', SUBSTR(NRINSJURCLIE,1,8), DECODE(IDTPIJURCLIE, 'F',NRINSJURCLIE,'')) = C_CONTAS.PCCCLI
                          AND N.IDENTRSAIDOP = 'S'
                          --AND C.IDSTATREC IN ( 'A', 'P')
                          AND IDSISTEMA IN ('DFS', 'REC', 'EXP')
                          AND CL.IDTPIJURCLIE NOT IN ('O', 'I')
                          AND N.CDEMPRESA =  P_CDEMPRESA
                          AND NVL(N.IDNFALTERADA,'N') <> 'S'
                          AND DTEMISSAO BETWEEN ADD_MONTHS(V_DATA_CORTE,-12) AND V_DATA_CORTE) C
              WHERE C.VRFINALBREC > 0
               ORDER BY DTEMISSAO DESC)
       WHERE ROWNUM = 1;
       V_PCVRMAC := 0;
       V_VRACUM  := 0;
       V_PCVRAUX := 0;
       FOR C_VRMAC IN (SELECT  VRDIAFATU AS VRDIAFATU, VRPGTO AS VRPGTO,
                             (TO_CHAR(DTEMISSAO, 'YYYYMMDD')) AS PCDMAC,
                             IDSTATREC
                        FROM (SELECT  C.VRORIGREC AS VRDIAFATU, 0 AS VRPGTO, N.DTEMISSAO AS DTEMISSAO,  'A' AS IDSTATREC
                                FROM NOTAFISCAL N, CONTARECEBER C, CLIENTE CL
                               WHERE N.CDFILIAL = C.CDFILIAL
                                 AND N.NRLANCTONF = C.NRLANCTONF
                                 AND N.CDCLIENTE = CL.CDCLIENTE
                                 AND DECODE(IDTPIJURCLIE, 'J', SUBSTR(NRINSJURCLIE,1,8), DECODE(IDTPIJURCLIE, 'F',NRINSJURCLIE,'')) = C_CONTAS.PCCCLI
                                 AND N.IDENTRSAIDOP = 'S'
                                 AND IDSISTEMA IN ('DFS', 'REC', 'EXP')
                                 AND CL.IDTPIJURCLIE NOT IN ('O', 'I')
                                 AND N.CDEMPRESA =  P_CDEMPRESA
                                 AND DTEMISSAO < ADD_MONTHS(V_DATA_CORTE,-12)
                                 --AND C.DTMOVFLUXO BETWEEN ADD_MONTHS(V_DATA_CORTE,-12) AND V_DATA_CORTE
                               UNION ALL  
                              SELECT  0 AS VRDIAFATU, C.VRORIGREC AS VRPGTO, C.DTMOVFLUXO AS DTEMISSAO,  IDSTATREC
                                FROM NOTAFISCAL N, CONTARECEBER C, CLIENTE CL
                               WHERE N.CDFILIAL = C.CDFILIAL
                                 AND N.NRLANCTONF = C.NRLANCTONF
                                 AND N.CDCLIENTE = CL.CDCLIENTE
                                 AND DECODE(IDTPIJURCLIE, 'J', SUBSTR(NRINSJURCLIE,1,8), DECODE(IDTPIJURCLIE, 'F',NRINSJURCLIE,'')) = C_CONTAS.PCCCLI
                                 AND N.IDENTRSAIDOP = 'S'
                                 AND IDSISTEMA IN ('DFS', 'REC', 'EXP')
                                 AND CL.IDTPIJURCLIE NOT IN ('O', 'I')
                                 AND N.CDEMPRESA =  P_CDEMPRESA
                                 AND NVL(N.IDNFALTERADA,'N') <> 'S'
                                 AND DTEMISSAO < ADD_MONTHS(V_DATA_CORTE,-12)
                                -- AND C.DTMOVFLUXO BETWEEN ADD_MONTHS(V_DATA_CORTE,-12) AND V_DATA_CORTE
                                 AND IDSTATREC = 'B'                                 
                              UNION ALL
                              SELECT  C.VRORIGREC AS VRDIAFATU, 0 AS VRPGTO, N.DTEMISSAO AS DTEMISSAO, 'A' AS IDSTATREC
                                FROM NOTAFISCAL N, CONTARECEBER C, CLIENTE CL
                               WHERE N.CDFILIAL = C.CDFILIAL
                                 AND N.NRLANCTONF = C.NRLANCTONF
                                 AND N.CDCLIENTE = CL.CDCLIENTE
                                 AND DECODE(IDTPIJURCLIE, 'J', SUBSTR(NRINSJURCLIE,1,8), DECODE(IDTPIJURCLIE, 'F',NRINSJURCLIE,'')) = C_CONTAS.PCCCLI
                                 AND N.IDENTRSAIDOP = 'S'
                                 AND IDSISTEMA IN ('DFS', 'REC', 'EXP')
                                 AND CL.IDTPIJURCLIE NOT IN ('O', 'I')
                                 AND N.CDEMPRESA =  P_CDEMPRESA
                                 AND NVL(N.IDNFALTERADA,'N') <> 'S'
                                 AND DTEMISSAO BETWEEN ADD_MONTHS(V_DATA_CORTE,-12) AND V_DATA_CORTE
                               UNION ALL
                              SELECT  0 AS VRDIAFATU, C.VRORIGREC AS VRPGTO, n.DTEMISSAO AS DTEMISSAO,IDSTATREC
                                FROM NOTAFISCAL N, CONTARECEBER C, CLIENTE CL
                               WHERE N.CDFILIAL = C.CDFILIAL
                                 AND N.NRLANCTONF = C.NRLANCTONF
                                 AND N.CDCLIENTE = CL.CDCLIENTE
                                 AND DECODE(IDTPIJURCLIE, 'J', SUBSTR(NRINSJURCLIE,1,8), DECODE(IDTPIJURCLIE, 'F',NRINSJURCLIE,'')) = C_CONTAS.PCCCLI
                                 AND N.IDENTRSAIDOP = 'S'
                                 AND IDSISTEMA IN ('DFS', 'REC', 'EXP')
                                 AND CL.IDTPIJURCLIE NOT IN ('O', 'I')
                                 AND N.CDEMPRESA =  P_CDEMPRESA
                                 AND NVL(N.IDNFALTERADA,'N') <> 'S'
                                 AND IDSTATREC = 'B'
                                 AND DTMOVFLUXO BETWEEN ADD_MONTHS(V_DATA_CORTE,-12) AND V_DATA_CORTE)C
                                 ORDER BY PCDMAC, IDSTATREC)LOOP
            IF C_VRMAC.IDSTATREC = 'A' THEN
               V_VRACUM  := V_VRACUM+C_VRMAC.VRDIAFATU;
               V_PCVRAUX := V_PCVRAUX+C_VRMAC.VRDIAFATU;
               V_PCDMAC := C_VRMAC.PCDMAC;
            ELSE
               V_VRACUM := nvl(V_VRACUM,0) - C_VRMAC.VRPGTO;
               V_PCVRAUX := 0;
            END IF;
            IF V_PCVRMAC < V_PCVRAUX THEN
              V_PCVRMAC := V_PCVRAUX;
             -- V_PCDMAC := C_VRMAC.PCDMAC;
            ELSE
               V_PCVRMAC := V_PCVRMAC;
            END IF;
      END LOOP;
      V_PCVMAC := LTRIM(REPLACE(TO_CHAR((V_PCVRMAC),'0000000000000.00'),'.',''));
      SELECT LTRIM(REPLACE(TO_CHAR(SUM(VRFINALBREC),'0000000000000.00'),'.','')), SUM(VRFINALBREC)
        INTO V_PCVSAT, V_PCVVRSAT /*CAMPO10*/
        FROM (SELECT C.VRORIGREC AS VRFINALBREC
                FROM NOTAFISCAL N, CONTARECEBER C, CLIENTE CL
               WHERE N.CDFILIAL = C.CDFILIAL
                 AND N.NRLANCTONF = C.NRLANCTONF
                 AND N.CDCLIENTE = CL.CDCLIENTE
                 AND DECODE(IDTPIJURCLIE, 'J', SUBSTR(NRINSJURCLIE,1,8), DECODE(IDTPIJURCLIE, 'F',NRINSJURCLIE,'')) = C_CONTAS.PCCCLI
                 AND IDENTRSAIDOP = 'S'
                 AND C.IDSTATREC IN ('A', 'P')
                 AND IDSISTEMA IN ('DFS', 'REC', 'EXP')
                 AND NVL(N.IDNFALTERADA,'N') <> 'S'
                 AND DTEMISSAO BETWEEN ADD_MONTHS(V_DATA_CORTE,-12) AND V_DATA_CORTE
                 AND N.CDEMPRESA = P_CDEMPRESA);
      SELECT LTRIM(REPLACE(TO_CHAR(SUM((DIASEMATRASO*VRBOLETOS))/SUM(decode(VRBOLETOS,0,1,VRBOLETOS)),'0000.00'),'.',''))
        INTO V_PCQPAG  /*CAMPO12*/
        FROM (SELECT (TRUNC(DTBAIXAREC) - TRUNC(DTORIGVENREC)) AS DIASEMATRASO,
                     (VRFINALBREC) AS VRBOLETOS
                FROM BOLETRECEBER B, BAIXARECEBIDA BX, (SELECT DISTINCT CDFILIBOLREC, NRLANBOLREC, MAX(DTORIGVENREC) AS DTORIGVENREC
                                                          FROM CONTARECEBER C, NOTAFISCAL N
                                                         WHERE C.CDFILIAL = N.CDFILIAL
                                                           AND C.NRLANCTONF = N.NRLANCTONF
                                                           AND IDENTRSAIDOP = 'S'
                                                           AND IDSISTEMA IN ('DFS', 'REC', 'EXP')
                                                           AND DECODE(IDTPIJURNF, 'J', SUBSTR(NRINSJURNF,1,8), DECODE(IDTPIJURNF, 'F',NRINSJURNF,'')) = C_CONTAS.PCCCLI
                                                           AND DTEMISSAO BETWEEN ADD_MONTHS(V_DATA_CORTE,-12) AND V_DATA_CORTE
                                                           AND N.CDEMPRESA = P_CDEMPRESA
                                                           AND NVL(N.IDNFALTERADA,'N') <> 'S'
                                                           --AND IDSTATREC IN ('B')
                                                           GROUP BY CDFILIBOLREC, NRLANBOLREC) C
               WHERE B.CDFILIAL = C.CDFILIBOLREC
                 AND B.NRLANBOLREC = C.NRLANBOLREC
                 AND B.CDFILIBAIREC = BX.CDFILIAL
                 AND B.NRBAIXAREC = BX.NRBAIXAREC
                 AND TRUNC(DTBAIXAREC) - TRUNC(DTORIGVENREC) > 0);
      SELECT LTRIM(REPLACE(TO_CHAR(ROUND(((DIASEMATRASO/TOTALCONTAS)),2),'0000.00'),'.',''))
        INTO V_PCQDAP /*CAMPO13*/
      FROM(
    SELECT SUM(TOTALCONTAS) TOTALCONTAS, SUM(DIASEMATRASO) DIASEMATRASO    
     FROM (SELECT SUM(TOTALCONTAS) AS TOTALCONTAS, SUM((TRUNC(DTBAIXAREC) - TRUNC(DTORIGVENREC))) AS DIASEMATRASO
                FROM BOLETRECEBER B, BAIXARECEBIDA BX, (SELECT DISTINCT CDFILIBOLREC , NRLANBOLREC, MAX(DTORIGVENREC) AS DTORIGVENREC,
                                                               COUNT(*) AS TOTALCONTAS
                                                          FROM CONTARECEBER C, NOTAFISCAL N
                                                         WHERE C.CDFILIAL = N.CDFILIAL
                                                           AND C.NRLANCTONF = N.NRLANCTONF
                                                           AND DTEMISSAO BETWEEN ADD_MONTHS(V_DATA_CORTE,-12) AND V_DATA_CORTE
                                                           AND IDENTRSAIDOP = 'S'
                                                           AND IDSISTEMA IN ('DFS', 'REC', 'EXP')
                                                           --AND IDSTATREC IN ('A','P')
                                                           AND DECODE(IDTPIJURNF, 'J', SUBSTR(NRINSJURNF,1,8), DECODE(IDTPIJURNF, 'F',NRINSJURNF,'')) = C_CONTAS.PCCCLI
                                                           AND N.CDEMPRESA = P_CDEMPRESA
                                                           AND NVL(N.IDNFALTERADA,'N') <> 'S'
                                                           GROUP BY CDFILIBOLREC, NRLANBOLREC)C
               WHERE B.CDFILIAL = C.CDFILIBOLREC
                 AND B.NRLANBOLREC = C.NRLANBOLREC
                 AND B.CDFILIBAIREC = BX.CDFILIAL
                 AND B.NRBAIXAREC = BX.NRBAIXAREC
                 AND B.VRFINALBREC > 0
                 AND TRUNC(DTBAIXAREC) - TRUNC(DTORIGVENREC) > 0
               GROUP BY (TRUNC(DTBAIXAREC) - TRUNC(DTORIGVENREC)) ));

      SELECT LTRIM(REPLACE(TO_CHAR(SUM(C.VRFINALBREC),'0000000000000.00'),'.',''))
        INTO V_PCVDAV /*CAMPO14*/
        FROM BOLETRECEBER B, (SELECT DISTINCT CDFILIBOLREC, NRLANBOLREC, MAX(DTORIGVENREC) AS DTORIGVENREC, VRORIGREC AS VRFINALBREC
                                FROM CONTARECEBER C, NOTAFISCAL N, CLIENTE CL
                               WHERE C.CDFILIAL = N.CDFILIAL
                                 AND C.NRLANCTONF = N.NRLANCTONF
                                 AND IDENTRSAIDOP = 'S'
                                 AND IDSISTEMA IN ('DFS', 'REC', 'EXP')
                                 AND N.CDCLIENTE = CL.CDCLIENTE
                                 AND DECODE(IDTPIJURCLIE, 'J', SUBSTR(NRINSJURCLIE,1,8), DECODE(IDTPIJURCLIE, 'F',NRINSJURCLIE,'')) = C_CONTAS.PCCCLI
                                 AND DTEMISSAO BETWEEN ADD_MONTHS(V_DATA_CORTE,-12) AND V_DATA_CORTE
                                 AND N.CDEMPRESA = P_CDEMPRESA
                                 AND NVL(N.IDNFALTERADA,'N') <> 'S'
                                 AND IDSTATREC IN ('A','P')
                                 GROUP BY CDFILIBOLREC, NRLANBOLREC, VRORIGREC) C
       WHERE B.CDFILIAL(+) = C.CDFILIBOLREC
         AND B.NRLANBOLREC(+) = C.NRLANBOLREC
         AND DTORIGVENREC >= TRUNC(V_DATA_CORTE);
      SELECT LTRIM(REPLACE(TO_CHAR(SUM((DIASVENCEMIS*VRBOLETOS))/SUM(decode(VRBOLETOS,0,1,VRBOLETOS)),'0000.00'),'.',''))
        INTO V_PCMDAV /*CAMPO15*/
        FROM (SELECT (DTORIGVENREC-DTEMISSAO) AS DIASVENCEMIS,
                     DTORIGVENREC, DTEMISSAO,
                     VRORIGREC AS VRBOLETOS
                FROM BOLETRECEBER B, (SELECT DISTINCT CDFILIBOLREC, NRLANBOLREC, MAX(DTEMISSAO) AS DTEMISSAO,
                                             MAX(DTORIGVENREC) AS DTORIGVENREC, VRORIGREC
                                        FROM CONTARECEBER C, NOTAFISCAL N, CLIENTE CL
                                       WHERE C.CDFILIAL = N.CDFILIAL
                                         AND C.NRLANCTONF = N.NRLANCTONF
                                         AND IDENTRSAIDOP = 'S'
                                         AND IDSISTEMA IN ('DFS', 'REC', 'EXP')
                                         AND IDSTATREC IN ('A','P')
                                         AND N.CDCLIENTE = CL.CDCLIENTE
                                         AND DECODE(IDTPIJURCLIE, 'J', SUBSTR(NRINSJURCLIE,1,8), DECODE(IDTPIJURCLIE, 'F',NRINSJURCLIE,'')) = C_CONTAS.PCCCLI
                                         AND NVL(N.IDNFALTERADA,'N') <> 'S'
                                         AND DTEMISSAO BETWEEN ADD_MONTHS(V_DATA_CORTE,-12) AND V_DATA_CORTE
                                         AND N.CDEMPRESA = P_CDEMPRESA
                                       GROUP BY  CDFILIBOLREC, NRLANBOLREC, VRORIGREC) C
              WHERE B.CDFILIAL(+)    = C.CDFILIBOLREC
                AND B.NRLANBOLREC(+) = C.NRLANBOLREC
                AND DTORIGVENREC >= TRUNC(V_DATA_CORTE));
      BEGIN
         SELECT LTRIM(REPLACE(TO_CHAR(SUM(ROUND((DIASVENCEMIS/decode(TOTALCONTAS,0,1,TOTALCONTAS)),2)),'0000.00'),'.',''))
           INTO V_PCMPMV /*CAMPO16*/
           FROM (SELECT SUM(TOTALCONTAS) AS TOTALCONTAS, SUM(ABS(TRUNC(DTORIGVENREC) - TRUNC(DTEMISSAO))) AS DIASVENCEMIS
                   FROM BOLETRECEBER B, (SELECT DISTINCT CDFILIBOLREC, NRLANBOLREC, MAX(DTEMISSAO) AS DTEMISSAO,
                                                MAX(DTORIGVENREC) AS DTORIGVENREC, COUNT(*) AS TOTALCONTAS
                                           FROM CONTARECEBER C, NOTAFISCAL N, CLIENTE CL
                                          WHERE C.CDFILIAL = N.CDFILIAL
                                            AND C.NRLANCTONF = N.NRLANCTONF
                                            AND IDENTRSAIDOP = 'S'
                                            AND IDSISTEMA IN ('DFS', 'REC', 'EXP')
                                            AND IDSTATREC IN ('A','P')
                                            AND N.CDCLIENTE = CL.CDCLIENTE
                                            AND DECODE(IDTPIJURCLIE, 'J', SUBSTR(NRINSJURCLIE,1,8), DECODE(IDTPIJURCLIE, 'F',NRINSJURCLIE,'')) = C_CONTAS.PCCCLI
                                            AND DTEMISSAO BETWEEN ADD_MONTHS(V_DATA_CORTE,-12) AND V_DATA_CORTE
                                            AND N.CDEMPRESA = P_CDEMPRESA
                                            AND NVL(N.IDNFALTERADA,'N') <> 'S'
                                           GROUP BY  CDFILIBOLREC, NRLANBOLREC) C
                   WHERE B.CDFILIAL(+)   = C.CDFILIBOLREC
                     AND B.NRLANBOLREC(+) = C.NRLANBOLREC
                     AND DTORIGVENREC >= TRUNC(V_DATA_CORTE)
                   GROUP BY (TRUNC(DTORIGVENREC) - TRUNC(DTEMISSAO)));
      EXCEPTION
           WHEN NO_DATA_FOUND THEN
          V_PCMPMV := NULL;
      END;
      SELECT LTRIM(REPLACE(TO_CHAR(SUM(NVL(C.VRFINALBREC,0)),'0000000000000.00'),'.',''))
        INTO V_PCDATV /*CAMPO17*/
        FROM BOLETRECEBER B, (SELECT DISTINCT CDFILIBOLREC, NRLANBOLREC, MAX(DTORIGVENREC) AS DTORIGVENREC, VRORIGREC AS VRFINALBREC
                                FROM CONTARECEBER C, NOTAFISCAL N, CLIENTE CL
                               WHERE C.CDFILIAL = N.CDFILIAL
                                 AND C.NRLANCTONF = N.NRLANCTONF
                                 AND IDENTRSAIDOP = 'S'
                                 AND IDSISTEMA IN ('DFS', 'REC', 'EXP')
                                 AND IDSTATREC IN ('A','P')
                                 AND N.CDCLIENTE = CL.CDCLIENTE
                                 AND DECODE(IDTPIJURCLIE, 'J', SUBSTR(NRINSJURCLIE,1,8), DECODE(IDTPIJURCLIE, 'F',NRINSJURCLIE,'')) = C_CONTAS.PCCCLI
                                 AND DTEMISSAO BETWEEN ADD_MONTHS(V_DATA_CORTE,-12) AND V_DATA_CORTE
                                 AND NVL(N.IDNFALTERADA,'N') <> 'S'
                                 AND N.CDEMPRESA = P_CDEMPRESA
                                 GROUP BY CDFILIBOLREC, NRLANBOLREC, VRORIGREC) C
         WHERE B.CDFILIAL(+) = C.CDFILIBOLREC
           AND B.NRLANBOLREC(+) = C.NRLANBOLREC
           AND (TRUNC(TO_DATE(V_DATA_CORTE))-DTVENBOLREC ) >= 5;
      SELECT LTRIM(REPLACE(TO_CHAR(ROUND(SUM(DIASATRASO*VRBOLETOS)/SUM(VRBOLETOS),2),'0000'),'.',''))
        INTO V_PCMPTV /*CAMPO18*/
        FROM (SELECT (TRUNC(V_DATA_CORTE)-DTVENBOLREC) AS DIASATRASO,
                      C.VRFINALBREC AS VRBOLETOS
                FROM BOLETRECEBER B, (SELECT DISTINCT CDFILIBOLREC, NRLANBOLREC, MAX(DTORIGVENREC) AS DTORIGVENREC, VRORIGREC AS VRFINALBREC
                                        FROM CONTARECEBER C, NOTAFISCAL N, CLIENTE CL
                                       WHERE C.CDFILIAL = N.CDFILIAL
                                         AND C.NRLANCTONF = N.NRLANCTONF
                                         AND IDSTATREC IN ('A','P')
                                         AND IDSISTEMA IN ('DFS', 'REC', 'EXP')
                                         AND NVL(N.IDNFALTERADA,'N') <> 'S'
                                         AND N.CDCLIENTE = CL.CDCLIENTE
                                         AND DECODE(IDTPIJURCLIE, 'J', SUBSTR(NRINSJURCLIE,1,8), DECODE(IDTPIJURCLIE, 'F',NRINSJURCLIE,'')) = C_CONTAS.PCCCLI
                                         AND DTEMISSAO BETWEEN ADD_MONTHS(V_DATA_CORTE,-12) AND V_DATA_CORTE
                                         AND N.CDEMPRESA = P_CDEMPRESA
                                         GROUP BY CDFILIBOLREC, NRLANBOLREC, VRORIGREC) C
              WHERE B.CDFILIAL(+) = C.CDFILIBOLREC
                AND B.NRLANBOLREC(+) = C.NRLANBOLREC)
                WHERE (DIASATRASO) >= 5;
      SELECT LTRIM(REPLACE(TO_CHAR(SUM(NVL(C.VRFINALBREC,0)),'0000000000000.00'),'.',''))
        INTO V_PCV15D /*CAMPO19*/
        FROM BOLETRECEBER B, (SELECT DISTINCT CDFILIBOLREC, NRLANBOLREC, MAX(DTORIGVENREC) AS DTORIGVENREC, VRORIGREC AS VRFINALBREC
                                FROM CONTARECEBER C, NOTAFISCAL N, CLIENTE CL
                               WHERE  C.CDFILIAL = N.CDFILIAL
                                 AND C.NRLANCTONF = N.NRLANCTONF
                                 AND IDENTRSAIDOP = 'S'
                                 AND IDSISTEMA IN ('DFS', 'REC', 'EXP')
                                 AND IDSTATREC IN ('A','P')
                                 AND N.CDCLIENTE = CL.CDCLIENTE
                                 AND DECODE(IDTPIJURCLIE, 'J', SUBSTR(NRINSJURCLIE,1,8), DECODE(IDTPIJURCLIE, 'F',NRINSJURCLIE,'')) = C_CONTAS.PCCCLI
                                 AND DTEMISSAO BETWEEN ADD_MONTHS(V_DATA_CORTE,-12) AND V_DATA_CORTE
                                 AND NVL(N.IDNFALTERADA,'N') <> 'S'
                                 AND N.CDEMPRESA = P_CDEMPRESA
                                 GROUP BY CDFILIBOLREC, NRLANBOLREC, VRORIGREC) C
         WHERE B.CDFILIAL(+) = C.CDFILIBOLREC
           AND B.NRLANBOLREC(+) = C.NRLANBOLREC
           AND (TRUNC(TO_DATE(V_DATA_CORTE))-DTVENBOLREC ) >= 15;
      SELECT LTRIM(REPLACE(TO_CHAR(ROUND(SUM(DIASATRASO*VRBOLETOS)/SUM(VRBOLETOS),2),'0000'),'.',''))
        INTO V_PCM15D /*CAMPO20*/
        FROM (SELECT (TRUNC(V_DATA_CORTE)-DTVENBOLREC) AS DIASATRASO,
                      C.VRFINALBREC AS VRBOLETOS
                FROM BOLETRECEBER B, (SELECT DISTINCT CDFILIBOLREC, NRLANBOLREC, MAX(DTORIGVENREC) AS DTORIGVENREC, VRORIGREC AS VRFINALBREC
                                        FROM CONTARECEBER C, NOTAFISCAL N, CLIENTE CL
                                       WHERE C.CDFILIAL = N.CDFILIAL
                                         AND C.NRLANCTONF = N.NRLANCTONF
                                         AND IDENTRSAIDOP = 'S'
                                         AND IDSISTEMA IN ('DFS', 'REC', 'EXP')
                                         AND IDSTATREC IN ('A','P')
                                         AND N.CDCLIENTE = CL.CDCLIENTE
                                         AND DECODE(IDTPIJURCLIE, 'J', SUBSTR(NRINSJURCLIE,1,8), DECODE(IDTPIJURCLIE, 'F',NRINSJURCLIE,'')) = C_CONTAS.PCCCLI
                                         AND DTEMISSAO BETWEEN ADD_MONTHS(V_DATA_CORTE,-12) AND V_DATA_CORTE
                                         AND NVL(N.IDNFALTERADA,'N') <> 'S'
                                         AND N.CDEMPRESA = P_CDEMPRESA
                                         GROUP BY CDFILIBOLREC, NRLANBOLREC, VRORIGREC)C
               WHERE B.CDFILIAL(+) = C.CDFILIBOLREC
                 AND B.NRLANBOLREC(+) = C.NRLANBOLREC)
               WHERE (DIASATRASO) >= 15;
      SELECT LTRIM(REPLACE(TO_CHAR(SUM(NVL(C.VRFINALBREC,0)),'0000000000000.00'),'.',''))
        INTO V_PCV30D /*CAMPO21*/
        FROM BOLETRECEBER B, (SELECT DISTINCT CDFILIBOLREC, NRLANBOLREC, MAX(DTORIGVENREC) AS DTORIGVENREC, VRORIGREC AS VRFINALBREC
                                FROM CONTARECEBER C, NOTAFISCAL N, CLIENTE CL
                               WHERE C.CDFILIAL = N.CDFILIAL
                                 AND C.NRLANCTONF = N.NRLANCTONF
                                 AND IDENTRSAIDOP = 'S'
                                 AND IDSISTEMA IN ('DFS', 'REC', 'EXP')
                                 AND IDSTATREC IN ('A','P')
                                 AND N.CDCLIENTE = CL.CDCLIENTE
                                 AND DECODE(IDTPIJURCLIE, 'J', SUBSTR(NRINSJURCLIE,1,8), DECODE(IDTPIJURCLIE, 'F',NRINSJURCLIE,'')) = C_CONTAS.PCCCLI
                                 AND DTEMISSAO BETWEEN ADD_MONTHS(V_DATA_CORTE,-12) AND V_DATA_CORTE
                                 AND NVL(N.IDNFALTERADA,'N') <> 'S'
                                 AND N.CDEMPRESA = P_CDEMPRESA
                                 GROUP BY CDFILIBOLREC, NRLANBOLREC, VRORIGREC) C
       WHERE B.CDFILIAL(+) = C.CDFILIBOLREC
         AND B.NRLANBOLREC(+) = C.NRLANBOLREC
         AND (TRUNC(TO_DATE(V_DATA_CORTE))-DTVENBOLREC ) >= 30;
      SELECT LTRIM(REPLACE(TO_CHAR(ROUND(SUM(DIASATRASO*VRBOLETOS)/SUM(VRBOLETOS),2),'0000'),'.',''))
        INTO V_PCM30D /*CAMPO22*/
        FROM (SELECT (TRUNC(V_DATA_CORTE)-DTVENBOLREC) AS DIASATRASO,
                      C.VRFINALBREC AS VRBOLETOS
                FROM BOLETRECEBER B, (SELECT DISTINCT CDFILIBOLREC, NRLANBOLREC, MAX(DTORIGVENREC) AS DTORIGVENREC, VRORIGREC AS VRFINALBREC
                                        FROM CONTARECEBER C, NOTAFISCAL N, CLIENTE CL
                                       WHERE C.CDFILIAL = N.CDFILIAL
                                         AND C.NRLANCTONF = N.NRLANCTONF
                                         AND IDENTRSAIDOP = 'S'
                                         AND IDSISTEMA IN ('DFS', 'REC', 'EXP')
                                         AND IDSTATREC IN ('A','P')
                                         AND N.CDCLIENTE = CL.CDCLIENTE
                                         AND DECODE(IDTPIJURCLIE, 'J', SUBSTR(NRINSJURCLIE,1,8), DECODE(IDTPIJURCLIE, 'F',NRINSJURCLIE,'')) = C_CONTAS.PCCCLI 
                                         AND DTEMISSAO BETWEEN ADD_MONTHS(V_DATA_CORTE,-12) AND V_DATA_CORTE
                                         AND NVL(N.IDNFALTERADA,'N') <> 'S'
                                         AND N.CDEMPRESA = P_CDEMPRESA
                                         GROUP BY CDFILIBOLREC, NRLANBOLREC, VRORIGREC)C
       WHERE B.CDFILIAL(+) = C.CDFILIBOLREC
         AND B.NRLANBOLREC(+) = C.NRLANBOLREC)
       WHERE (DIASATRASO) >= 30;
      BEGIN
         SELECT (TO_CHAR(DTEMISSAO, 'YYYYMMDD')), LTRIM(REPLACE(TO_CHAR((VRFINALBREC),'0000000000000.00'),'.',''))
           INTO V_PCDTPC, /*CAMPO23*/ V_PCVPCO --CAMPO24
           FROM (SELECT ROWNUM AS  LINHA1, A.*
                   FROM (SELECT DTEMISSAO, C.VRFINALBREC
                           FROM BOLETRECEBER BO, (SELECT DISTINCT C.CDFILIBOLREC, C.NRLANBOLREC, MAX(N.DTEMISSAO ) AS DTEMISSAO, C.VRORIGREC AS VRFINALBREC
                                                    FROM NOTAFISCAL N, CONTARECEBER C, CLIENTE CL
                                                   WHERE N.CDFILIAL = C.CDFILIAL
                                                     AND N.NRLANCTONF = C.NRLANCTONF
                                                     AND N.CDCLIENTE = CL.CDCLIENTE
                                                     AND DECODE(IDTPIJURCLIE, 'J', SUBSTR(NRINSJURCLIE,1,8), DECODE(IDTPIJURCLIE, 'F',NRINSJURCLIE,'')) = C_CONTAS.PCCCLI
                                                     AND N.IDENTRSAIDOP = 'S'
                                                     AND IDSISTEMA IN ('DFS', 'REC', 'EXP')
                                                     AND CL.IDTPIJURCLIE NOT IN ('O', 'I')
                                                     AND N.CDEMPRESA =  P_CDEMPRESA
                                                     AND NVL(N.IDNFALTERADA,'N') <> 'S'
                                                     AND DTEMISSAO BETWEEN ADD_MONTHS(V_DATA_CORTE,-12) AND V_DATA_CORTE
                                                     GROUP BY C.CDFILIBOLREC, C.NRLANBOLREC, C.VRORIGREC) C
                 WHERE BO.CDFILIAL(+)    = C.CDFILIBOLREC
                   AND BO.NRLANBOLREC(+) = C.NRLANBOLREC
                   AND C.VRFINALBREC > 0
                   AND TO_CHAR(DTEMISSAO,'YYYYMMDD') < V_PCDUCM
                 ORDER BY DTEMISSAO DESC)A)
          WHERE LINHA1 = 1;
      EXCEPTION
           WHEN NO_DATA_FOUND THEN
                V_PCDTPC := NULL;
      END;
      SELECT LTRIM(REPLACE(TO_CHAR(NVL(SUM(C.VRFINALBREC),0),'0000000000000.00'),'.',''))
        INTO V_PCVPA --CAMPO30
        FROM BOLETRECEBER B,  BAIXARECEBIDA BX,(SELECT DISTINCT CDFILIBOLREC, NRLANBOLREC, MAX(DTORIGVENREC) AS DTORIGVENREC, C.VRORIGREC AS VRFINALBREC
                                                  FROM CONTARECEBER C, NOTAFISCAL N,CLIENTE CL
                                                 WHERE C.CDFILIAL = N.CDFILIAL
                                                   AND C.NRLANCTONF = N.NRLANCTONF
                                                   AND IDENTRSAIDOP = 'S'
                                                   AND IDSISTEMA IN ('DFS', 'REC', 'EXP')
                                                   AND NVL(N.IDNFALTERADA,'N') <> 'S'
                                                   AND DTEMISSAO BETWEEN ADD_MONTHS(V_DATA_CORTE,-12) AND V_DATA_CORTE
                                                   AND N.CDCLIENTE = CL.CDCLIENTE
                                                   AND DECODE(IDTPIJURCLIE, 'J', SUBSTR(NRINSJURCLIE,1,8), DECODE(IDTPIJURCLIE, 'F',NRINSJURCLIE,'')) = C_CONTAS.PCCCLI
                                                   AND N.CDEMPRESA = P_CDEMPRESA
                                                   GROUP BY CDFILIBOLREC, NRLANBOLREC, C.VRORIGREC) C
     WHERE B.CDFILIAL = C.CDFILIBOLREC
       AND B.NRLANBOLREC = C.NRLANBOLREC
       AND B.CDFILIBAIREC = BX.CDFILIAL
       AND B.NRBAIXAREC = BX.NRBAIXAREC
       AND DTBAIXAREC > DTORIGVENREC
       AND IDSTATUSBREC = 'B'
       AND DTORIGVENREC BETWEEN ADD_MONTHS(V_DATA_CORTE,-12) AND V_DATA_CORTE;
      SELECT NVL(MAX('VV'), '  ')
        INTO V_PCSVV --CAMPO31
        FROM BOLETRECEBER B, BAIXARECEBIDA BX, (SELECT DISTINCT CDFILIBOLREC, NRLANBOLREC, MAX(DTORIGVENREC) AS DTORIGVENREC
                                                  FROM CONTARECEBER C, NOTAFISCAL N,  TIPOCTRECEBER TC, CLIENTE CL
                                                 WHERE C.CDFILIAL = N.CDFILIAL
                                                   AND C.NRLANCTONF = N.NRLANCTONF
                                                   AND IDENTRSAIDOP = 'S'
                                                   AND IDSISTEMA IN ('DFS', 'REC', 'EXP')
                                                   AND TC.CDTPCTRECEB = C.CDTPCTRECEB
                                                   AND IDTPCTRECEBER = '2'
                                                   AND NVL(N.IDNFALTERADA,'N') <> 'S'
                                                   AND DTORIGVENREC BETWEEN ADD_MONTHS(V_DATA_CORTE,-12) AND V_DATA_CORTE
                                                   AND DTEMISSAO BETWEEN ADD_MONTHS(V_DATA_CORTE,-12) AND V_DATA_CORTE
                                                   AND N.CDCLIENTE = CL.CDCLIENTE
                                                   AND DECODE(IDTPIJURCLIE, 'J', SUBSTR(NRINSJURCLIE,1,8), DECODE(IDTPIJURCLIE, 'F',NRINSJURCLIE,'')) = C_CONTAS.PCCCLI
                                                   AND N.CDEMPRESA = P_CDEMPRESA
                                                   group by CDFILIBOLREC, NRLANBOLREC) C
        WHERE B.CDFILIAL = C.CDFILIBOLREC
          AND B.NRLANBOLREC = C.NRLANBOLREC
          AND B.CDFILIBAIREC = BX.CDFILIAL
          AND B.NRBAIXAREC = BX.NRBAIXAREC
          AND DTBAIXAREC > DTORIGVENREC
          AND IDSTATUSBREC = 'B';
      --MONTA ARQUIVO
      V_CONTADOR  := NVL(V_CONTADOR,0) + 1;
      if V_PCDTPC = V_PCDUCM then
        V_PCDTPC:= null;
        V_PCVPCO := null;
      end if;
      if V_PCVRMAC < V_PCVVRSAT then
         V_PCVRMAC := V_PCVVRSAT;
         V_PCVMAC  := LTRIM(REPLACE(TO_CHAR((V_PCVRMAC),'0000000000000.00'),'.',''));
      end if;
     BEGIN 
      SELECT VRMAIORACUM, DTMAIORACUM
        INTO V_PCVRMACANT, V_PCDMACANT 
        FROM MAIORACUMCLI
       WHERE NRINSJURCLIE = C_CONTAS.PCCCLI
         AND CDEMPRESA    = P_CDEMPRESA;
     EXCEPTION
          WHEN NO_DATA_FOUND THEN 
        BEGIN 
          V_PCVRMACANT := NULL;
          V_PCDMACANT  := NULL;
          INSERT INTO MAIORACUMCLI
             (CDEMPRESA, NRINSJURCLIE, DTMAIORACUM, VRMAIORACUM, DTULTATU)
            VALUES
             (P_CDEMPRESA, C_CONTAS.PCCCLI, V_PCDMAC,  V_PCVRMAC, SYSDATE);
        END;
     END;
     IF NVL(V_PCVRMACANT,0) > V_PCVRMAC THEN
     
        V_PCVRMAC := V_PCVRMACANT;
        V_PCVMAC  := LTRIM(REPLACE(TO_CHAR((V_PCVRMAC),'0000000000000.00'),'.',''));
        V_PCDMAC  := V_PCDMACANT;
     ELSE
       UPDATE MAIORACUMCLI
          SET VRMAIORACUM = V_PCVRMAC,
              DTMAIORACUM = V_PCDMAC,
              DTULTATU = SYSDATE
        WHERE NRINSJURCLIE = C_CONTAS.PCCCLI
          AND CDEMPRESA = P_CDEMPRESA;  
     END IF;
      V_LINHA := C_CONTAS.PCTIPO --CAMPO1
               ||C_CONTAS.PCCASS --CAMPO2
               ||lpad(C_CONTAS.PCCCLI, 20, '0') --CAMPO3
               ||C_CONTAS.PCDDAT --CAMPO4
               ||C_CONTAS.PCDCDD --CAMPO5
               ||lpad(nvl(V_PCDUCM,0), 8, '0')        --CAMPO6-data
               ||lpad(nvl(V_PCVULC,0), 15, '0')        --CAMPO7
               ||lpad(nvl(V_PCDMAC,0), 8, '0')         --CAMPO8-data
               ||lpad(nvl(V_PCVMAC,0), 15, '0')        --CAMPO9
               ||lpad(nvl(V_PCVSAT,0), 15, '0')        --CAMPO10
               ||v_PCVLCR --CAMPO11
               ||lpad(nvl(V_PCQPAG,0), 6, '0')         --CAMPO12
               ||lpad(nvl(V_PCQDAP,0), 6, '0')         --CAMPO13
               ||lpad(nvl(V_PCVDAV,0), 15, '0')        --CAMPO14
               ||lpad(nvl(V_PCMDAV,0), 6, '0')         --CAMPO15
               ||lpad(nvl(V_PCMPMV,0), 6 , '0')        --CAMPO16
               ||lpad(nvl(V_PCDATV,0), 15, '0')        --CAMPO17
               ||lpad(nvl(V_PCMPTV,0), 4, '0')         --CAMPO18
               ||lpad(nvl(V_PCV15D,0), 15, '0')        --CAMPO19
               ||lpad(nvl(V_PCM15D,0), 4, '0')         --CAMPO20
               ||lpad(nvl(V_PCV30D,0), 15, '0')        --CAMPO21
               ||lpad(nvl(V_PCM30D,0), 4, '0')         --CAMPO22
               ||lpad(nvl(V_PCDTPC,0), 8, '0')         --CAMPO23data
               ||lpad(nvl(V_PCVPCO,0), 15, '0')        --CAMPO24
               ||C_CONTAS.PCVSIT --CAMPO25
               ||C_CONTAS.PCTIPG --CAMPO26
               ||lpad(nvl(C_CONTAS.PCGGA,0), 2, '0')  --CAMPO27
               ||C_CONTAS.PCDTG  --CAMPO28data
               ||lpad(nvl(v_PCVLG,0), 15, '0')  --CAMPO29
               ||lpad(nvl(V_PCVPA,0), 15, '0')         --CAMPO30
               ||V_PCSVV;        --CAMPO31
  
  /*V_LINHA := C_CONTAS.PCTIPO --CAMPO1
               ||'|'||C_CONTAS.PCCASS --CAMPO2
               ||'|'||lpad(C_CONTAS.PCCCLI, 20, '0') --CAMPO3
               ||'|'||C_CONTAS.PCDDAT --CAMPO4
               ||'|'||C_CONTAS.PCDCDD --CAMPO5
               ||'|'||lpad(nvl(V_PCDUCM,0), 8, '0')        --CAMPO6-data
               ||'|'||lpad(nvl(V_PCVULC,0), 15, '0')        --CAMPO7
               ||'|'||lpad(nvl(V_PCDMAC,0), 8, '0')         --CAMPO8-data
               ||'|'||lpad(nvl(V_PCVMAC,0), 15, '0')        --CAMPO9
               ||'|'||lpad(nvl(V_PCVSAT,0), 15, '0')        --CAMPO10
               ||'|'||C_CONTAS.PCVLCR --CAMPO11
               ||'|'||lpad(nvl(V_PCQPAG,0), 6, '0')         --CAMPO12
               ||'|'||lpad(nvl(V_PCQDAP,0), 6, '0')         --CAMPO13
               ||'|'||lpad(nvl(V_PCVDAV,0), 15, '0')        --CAMPO14
               ||'|'||lpad(nvl(V_PCMDAV,0), 6, '0')         --CAMPO15
               ||'|'||lpad(nvl(V_PCMPMV,0), 6 , '0')        --CAMPO16
               ||'|'||lpad(nvl(V_PCDATV,0), 15, '0')        --CAMPO17
               ||'|'||lpad(nvl(V_PCMPTV,0), 4, '0')         --CAMPO18
               ||'|'||lpad(nvl(V_PCV15D,0), 15, '0')        --CAMPO19
               ||'|'||lpad(nvl(V_PCM15D,0), 4, '0')         --CAMPO20
               ||'|'||lpad(nvl(V_PCV30D,0), 15, '0')        --CAMPO21
               ||'|'||lpad(nvl(V_PCM30D,0), 4, '0')         --CAMPO22
               ||'|'||lpad(nvl(V_PCDTPC,0), 8, '0')         --CAMPO23data
               ||'|'||lpad(nvl(V_PCVPCO,0), 15, '0')        --CAMPO24
               ||'|'||C_CONTAS.PCVSIT --CAMPO25
               ||'|'||C_CONTAS.PCTIPG --CAMPO26
               ||'|'||lpad(nvl(C_CONTAS.PCGGA,0), 2, '0')  --CAMPO27
               ||'|'||C_CONTAS.PCDTG  --CAMPO28data
               ||'|'||lpad(nvl(C_CONTAS.PCVLG,0), 15, '0')  --CAMPO29
               ||'|'||lpad(nvl(V_PCVPA,0), 15, '0')         --CAMPO30
               ||'|'||V_PCSVV;        --CAMPO31

      V_LINHA :=       '|CP1|'||C_CONTAS.PCTIPO --CAMPO1
               ||'|CP2|'||C_CONTAS.PCCASS --CAMPO2
               ||'|CP3|'||lpad(C_CONTAS.PCCCLI, 20, '0') --CAMPO3
               ||'|CP4|'||C_CONTAS.PCDDAT --CAMPO4
               ||'|CP5|'||C_CONTAS.PCDCDD --CAMPO5
               ||'|CP6|'||lpad(nvl(V_PCDUCM,0), 8, '0')        --CAMPO6-data
               ||'|CP7|'||lpad(nvl(V_PCVULC,0), 15, '0')        --CAMPO7
               ||'|CP8|'||lpad(nvl(V_PCDMAC,0), 8, '0')         --CAMPO8-data
               ||'|CP9|'||lpad(nvl(V_PCVMAC,0), 15, '0')        --CAMPO9
               ||'|CP10|'||lpad(nvl(V_PCVSAT,0), 15, '0')        --CAMPO10
               ||'|CP11|'||C_CONTAS.PCVLCR --CAMPO11
               ||'|CP12|'||lpad(nvl(V_PCQPAG,0), 6, '0')         --CAMPO12
               ||'|CP13|'||lpad(nvl(V_PCQDAP,0), 6, '0')         --CAMPO13
               ||'|CP14|'||lpad(nvl(V_PCVDAV,0), 15, '0')        --CAMPO14
               ||'|CP15|'||lpad(nvl(V_PCMDAV,0), 6, '0')         --CAMPO15
               ||'|CP16|'||lpad(nvl(V_PCMPMV,0), 6 , '0')        --CAMPO16
               ||'|CP17|'||lpad(nvl(V_PCDATV,0), 15, '0')        --CAMPO17
               ||'|CP18|'||lpad(nvl(V_PCMPTV,0), 4, '0')         --CAMPO18
               ||'|CP19|'||lpad(nvl(V_PCV15D,0), 15, '0')        --CAMPO19
               ||'|CP20|'||lpad(nvl(V_PCM15D,0), 4, '0')         --CAMPO20
               ||'|CP21|'||lpad(nvl(V_PCV30D,0), 15, '0')        --CAMPO21
               ||'|CP22|'||lpad(nvl(V_PCM30D,0), 4, '0')         --CAMPO22
               ||'|CP23|'||lpad(nvl(V_PCDTPC,0), 8, '0')         --CAMPO23data
               ||'|CP24|'||lpad(nvl(V_PCVPCO,0), 15, '0')        --CAMPO24
               ||'|CP25|'||C_CONTAS.PCVSIT --CAMPO25
               ||'|CP26|'||C_CONTAS.PCTIPG --CAMPO26
               ||'|CP27|'||lpad(nvl(C_CONTAS.PCGGA,0), 2, '0')  --CAMPO27
               ||'|CP28|'||C_CONTAS.PCDTG  --CAMPO28data
               ||'|CP29|'||lpad(nvl(C_CONTAS.PCVLG,0), 15, '0')  --CAMPO29
               ||'|CP30|'||lpad(nvl(V_PCVPA,0), 15, '0')         --CAMPO30
               ||'|CP31|'||V_PCSVV;        --CAMPO31*/
      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
        (LPAD(RTRIM(V_CONTADOR), 8, '0'), V_LINHA, P_NRSEQARQMAG);
      V_LINHA  := NULL;
      V_PCDUCM := NULL;
      V_PCVULC := NULL;
      V_PCDMAC := NULL;
      V_PCVMAC := NULL;
      V_PCVSAT := NULL;
      V_PCQPAG := NULL;
      V_PCQDAP := NULL;
      V_PCVDAV := NULL;
      V_PCMDAV := NULL;
      V_PCMPMV := NULL;
      V_PCDATV := NULL;
      V_PCMPTV := NULL;
      V_PCV15D := NULL;
      V_PCM15D := NULL;
      V_PCV30D := NULL;
      V_PCM30D := NULL;
      V_PCDTPC := NULL;
      V_PCVPCO := NULL;
      V_PCVPA  := NULL;
      V_PCSVV  := NULL;
   END LOOP;
END;
/