CREATE OR REPLACE PROCEDURE GERA_BOLETOS_ASSINATURA(

   P_CDFILIAL        IN NOTAFISCAL.CDFILIAL%TYPE,
   P_NRCPFRESPCON    IN BOLETRECEBER.CDCLIENTE%TYPE,
   P_NRSERVASSCLI    IN NCSERVASSCLI.NRSERVASSCLI%TYPE)
AS
   V_EMPRPERMOR   NUMBER;
   V_VRPEMULTA    BOLETRECEBER.VRPEMULBREC%TYPE;
   V_VRPEMORA     BOLETRECEBER.VRPEMORBREC%TYPE;
   V_CDTPBAIXA    BOLETRECEBER.CDTPBAIXAREC%TYPE;
   V_CDREDUCONT   BOLETRECEBER.CDREDUCONT%TYPE;
   V_CDINS1COBBOL BOLETRECEBER.CDINS1COBBOL%TYPE;
   V_CDINS2COBBOL BOLETRECEBER.CDINS2COBBOL%TYPE;
   V_CDCARTCOBBOL BOLETRECEBER.CDCARTCOBBOL%TYPE;
   V_QTDIAPROTBOL BOLETRECEBER.QTDIAPROTBOL%TYPE;
   V_IDIMPRBOLBOL BOLETRECEBER.IDIMPRBOLBOL%TYPE;
   V_TXMENSBOLBOL BOLETRECEBER.TXMENSBOLBOL%TYPE;
   V_NRLANCTONF   VARCHAR2(20);
   V_NRLANBOL     VARCHAR2(20);
   V_FORMPAGTO    NCPARAMFIN.DSFORMPG%TYPE;
   V_CDTPCTRECEB  TIPOCTRECEBER.CDTPCTRECEB%TYPE;
   V_CDTIPOOPER   TIPOOPERAC.CDTIPOOPER%TYPE;
   V_CDEMPRESA    EMPRESA.CDEMPRESA%TYPE;
   V_CDINSCESTA   INSCRESTAD.CDINSCESTA%TYPE;
   V_CDINSCMUNI   INSCRMUNICIP.CDINSCMUNI%TYPE;
   V_CDCLASSFINA  TIPOCTRECEBER.CDCLASSFINA%TYPE;
   V_NRNOTAFISC   VARCHAR2(20);
   V_CDHISTPADR   HISTPADR.CDHISTPADR%TYPE;
   V_DESCRICAO    VARCHAR2(400);
   V_SEQUENCIAL   NUMBER := 1;
   V_NRSEQVENC    NUMBER := 1;
   V_NRLANCCTB    LANCCONT.NRLANCCONT%TYPE;
   V_VRSERVICO    FLOAT := 0;
   V_VRPARCELA    FLOAT := 0;
   V_VRDIF        FLOAT := 0;
   V_VRCONTA      FLOAT := 0;
   V_VRTOTAL      FLOAT := 0;
   V_QTPARCELA    NUMBER := 0;
   V_QTPARCELACNT NUMBER := 0;
   V_TOTPARCELA   NUMBER := 0;
   V_DTASSINA     DATE;
   V_DTVIGENCIA   DATE;
   V_DTVENCIMENTO   DATE;
   V_NCSERVASSASSOC VARCHAR2(20);
   V_CDCONSUMIDOR VARCHAR2(21);
   V_NRINSJURCLIE VARCHAR2(20);
   V_CDCLIENTE    BOLETRECEBER.CDCLIENTE%TYPE;
   V_CDSERIE      SERIENF.CDSERIE%TYPE;
   V_ERRO         VARCHAR2(4000);
   V_CDNCSERVASS  VARCHAR2(10);
   V_NRLANBOLREC VARCHAR2(10);
   V_NMCONSUMIDOR VARCHAR2(400);
   FUNCTION MONTA_HISTORICO(P_NRSERVASSASSOC VARCHAR2,
                            P_CDNCSERVASS VARCHAR2,
                            P_QTPARCELA VARCHAR2 ,
                            P_NRPARCELA VARCHAR2,
                            P_DTINICIAL DATE) RETURN VARCHAR2

   IS
   V_DESCRICAO   VARCHAR2(400);
   BEGIN
     SELECT DISTINCT
           'ASSINATURA REFERENTE AOS SERVICOS: '||SUBSTR(CONCAT_LIST(CURSOR(SELECT NMSERVASS
                                                                              FROM NCSERVASS NS, NCSERVASSASSOC NC
                                                                             WHERE NS.NRSERVASS = NC.NRSERVASS
                                                                               AND ((NS.CDNCSERVASS = P_CDNCSERVASS) OR (P_CDNCSERVASS = 'T'))
                                                                               AND NC.NRSERVASSCLI = P_NRSERVASSCLI),'/'),1,4000)
       INTO V_DESCRICAO
       FROM DUAL;
       RETURN  SUBSTR(V_DESCRICAO||' Data Inicial:'||TO_CHAR(P_DTINICIAL,'DD/MM/YYYY')||' CONSUMIDOR: '||V_NMCONSUMIDOR||' PARC:'||P_NRPARCELA||'/'||P_QTPARCELA,1,200);
   END;
BEGIN
          
   V_EMPRPERMOR := 0;
   
   BEGIN
      SELECT VLRASS, DTASSINATURA,  NRSERVASSCLI, REGEXP_REPLACE(NRCPFRESPCON, '\D'), CDCONSUMIDOR
        INTO V_VRSERVICO, V_DTASSINA,  V_NCSERVASSASSOC, V_NRINSJURCLIE, V_CDCONSUMIDOR
        FROM NCSERVASSCLI
       WHERE CDFILIAL = P_CDFILIAL
         AND NRCPFRESPCON = REGEXP_REPLACE(P_NRCPFRESPCON, '\D')
         AND NVL(IDSTATUSBOLREC,'P') = 'P'
         AND NRSERVASSCLI = P_NRSERVASSCLI ;
   EXCEPTION
        WHEN NO_DATA_FOUND
        THEN V_VRSERVICO := 0;
             V_DTASSINA := NULL;
             V_QTPARCELA := 0;
             V_NCSERVASSASSOC := NULL;
             V_NRINSJURCLIE   := NULL;
             
   END;
   BEGIN
     SELECT DSFORMPG, CDCONTCOR, CDCONTREC,
            CDTPOPER, F.CDEMPRESA, N.CDTPBAIXAREC, F.CDSERIE
       INTO V_FORMPAGTO, V_CDREDUCONT, V_CDTPCTRECEB,
            V_CDTIPOOPER, V_CDEMPRESA, V_CDTPBAIXA, V_CDSERIE 
       FROM NCPARAMFIN  N, FILIAL F
      WHERE N.CDFILIAL = P_CDFILIAL
        AND N.CDFILIAL = F.CDFILIAL;
   EXCEPTION
         WHEN NO_DATA_FOUND THEN 
         V_ERRO := 'NAO EXISTE PARAMETRIZACAO FINANCEIRA PARA A FILIAL';
   END;

   BEGIN
      SELECT CDCLIENTE
        INTO V_CDCLIENTE
        FROM CLIENTE
       WHERE REGEXP_REPLACE(NRINSJURCLIE, '\D') = V_NRINSJURCLIE;
   EXCEPTION
      WHEN OTHERS THEN
         V_CDCLIENTE := NULL;
   END;

   SELECT NMCONSUMIDOR
     INTO V_NMCONSUMIDOR
     FROM CONSUMIDOR
    WHERE CDCONSUMIDOR = V_CDCONSUMIDOR;    
   IF V_CDCLIENTE IS NULL THEN
           
      
      BEGIN
         V_CDCLIENTE := 'F'||V_NRINSJURCLIE;
         
  
         INSERT INTO CLIENTE(CDCLIENTE, IDTPIJURCLIE, NRINSJURCLIE, NMRAZSOCCLIE, NMFANTCLIE, CDFORMPGTO, IDTPFORPGTCL, IDUTDIAPGTCL, CDTPBAIXAREC)
              SELECT V_CDCLIENTE, 'F', V_NRINSJURCLIE, NMRESPCONS, NMRESPCONS, V_FORMPAGTO, 'V','I', V_CDTPBAIXA
                FROM CONSUMIDOR
                WHERE CDCONSUMIDOR = V_CDCONSUMIDOR
                 AND REGEXP_REPLACE(NRCPFRESPCON, '\D') = V_NRINSJURCLIE;
         INSERT INTO ENDECLIE(CDCLIENTE, IDTPENDECLIE, CDPAIS, SGESTADO, CDMUNICIPIO, DSENDECLIE, NRCEPCLIE, NMBAIRCLIE, DSCOMPENDCLI, NRTELECLIE, DSEMAILCLIE)
              SELECT V_CDCLIENTE, 'P', CDPAIS, SGESTADO, CDMUNICIPIO, DSENDECONS||' '||NRENDECONS, NRCEPCONS, NMBAIRCONS, DSCOMPLENDECONS , NRTELECONS, DSEMAILCONS
                 FROM CONSUMIDOR
                 WHERE CDCONSUMIDOR = V_CDCONSUMIDOR
                 AND REGEXP_REPLACE(NRCPFRESPCON, '\D') = V_NRINSJURCLIE;
         INSERT INTO ENDECLIE(CDCLIENTE, IDTPENDECLIE, CDPAIS, SGESTADO, CDMUNICIPIO, DSENDECLIE, NRCEPCLIE, NMBAIRCLIE, DSCOMPENDCLI, NRTELECLIE, DSEMAILCLIE)
              SELECT V_CDCLIENTE, 'C', CDPAIS, SGESTADO, CDMUNICIPIO, DSENDECONS||' '||NRENDECONS, NRCEPCONS, NMBAIRCONS, DSCOMPLENDECONS , NRTELECONS, DSEMAILCONS
                 FROM CONSUMIDOR
                 WHERE CDCONSUMIDOR = V_CDCONSUMIDOR
                   AND REGEXP_REPLACE(NRCPFRESPCON, '\D') = V_NRINSJURCLIE;

         INSERT INTO PRAZOPGTCLIE
              (CDCLIENTE,NRDIASPGTOCL, VRPERCPGTOCL)
            VALUES
              (V_CDCLIENTE, 28, 100);
      EXCEPTION
         WHEN OTHERS THEN
            V_ERRO := 'OCORREU UM ERRO EXISTE UMA INFORMAÇAO NÃO CADASTRADA PARA O CONSUMIDOR';
            ROLLBACK;
      END;
   ELSE
     UPDATE CLIENTE
        SET CDTPBAIXAREC = V_CDTPBAIXA
      WHERE CDCLIENTE    = V_CDCLIENTE;
     INSERT INTO PRAZOPGTCLIE 
              (CDCLIENTE,NRDIASPGTOCL, VRPERCPGTOCL)
      SELECT V_CDCLIENTE, 28, 100 
        FROM CLIENTE C
       WHERE NOT EXISTS (SELECT 1 FROM PRAZOPGTCLIE PZ WHERE PZ.CDCLIENTE = C.CDCLIENTE)
         AND C.CDCLIENTE = V_CDCLIENTE; 
   END IF;
   BEGIN
      SELECT MAX(VRPEMULBCLI), MAX(VRPEMORBCLI)
        INTO V_VRPEMULTA, V_VRPEMORA
        FROM CLIENTE
       WHERE CDCLIENTE = V_CDCLIENTE;
   EXCEPTION
        WHEN NO_DATA_FOUND
        THEN V_EMPRPERMOR := 0;
   END;

   SELECT CDCLASSFINA, CDHISTPADR
     INTO V_CDCLASSFINA, V_CDHISTPADR
     FROM TIPOCTRECEBER
    WHERE CDTPCTRECEB = V_CDTPCTRECEB;

   IF V_EMPRPERMOR = 1 THEN
      BEGIN
         SELECT MAX(VRPEMULEMPR), MAX(VRPEMOREMPR)
         INTO V_VRPEMULTA, V_VRPEMORA
         FROM EMPRESA
         WHERE (CDEMPRESA = V_CDEMPRESA);
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
              V_VRPEMULTA := 0;
              V_VRPEMORA  := 0;
      END;
   END IF;
   IF V_CDTPBAIXA IS NULL THEN
      BEGIN
         SELECT MAX(CL.CDTPBAIXAREC)
           INTO V_CDTPBAIXA
           FROM CLIENTE CL, BOLETRECEBER BR, TIPOBAIXAREC TP
          WHERE (TP.CDTPBAIXAREC = CL.CDTPBAIXAREC) AND
                (BR.CDCLIENTE    = CL.CDCLIENTE)AND
                (CL.CDCLIENTE    = V_CDCLIENTE);
      EXCEPTION
         WHEN NO_DATA_FOUND
         THEN V_CDTPBAIXA := '';
      END;
   END IF;
    SELECT   EB.CDINS1COBR, EB.CDINS2COBR, EB.CDCARTCOBR, 
            EB.QTDIAPROTEST, EB.IDIMPRBOLETO, EB.TXMENSBOLETO
      INTO  V_CDINS1COBBOL, V_CDINS2COBBOL, V_CDCARTCOBBOL,
            V_QTDIAPROTBOL, V_IDIMPRBOLBOL, V_TXMENSBOLBOL
      
      FROM  CONTCORR  CC, EMPRBANC  EB
      WHERE (CC.CDREDUCONT = V_CDREDUCONT) AND
            (CC.CDEMPRESA  =  V_CDEMPRESA)    AND
            (EB.CDBANCO    = CC.CDBANCO) AND
            (EB.CDEMPRESA  = CC.CDEMPRESA);   
            
   IF V_CDREDUCONT IS NULL THEN
      SELECT MAX(BP.CDREDUCONT),
             MAX(NVL(BP.CDINS1COBRCL, EB.CDINS1COBR)) CDINS1COBRCL,
             MAX(NVL(BP.CDINS2COBRCL, EB.CDINS2COBR)) CDINS2COBRCL,
             MAX(EB.CDCARTCOBR), MAX(EB.QTDIAPROTEST), MAX(EB.IDIMPRBOLETO), MAX(EB.TXMENSBOLETO)
      INTO V_CDREDUCONT, V_CDINS1COBBOL, V_CDINS2COBBOL, V_CDCARTCOBBOL,
           V_QTDIAPROTBOL, V_IDIMPRBOLBOL, V_TXMENSBOLBOL
      FROM BANCOPREF BP, CONTCORR  CC, EMPRBANC  EB
      WHERE (BP.CDCLIENTE = V_CDCLIENTE)    AND
            (BP.CDREDUCONT = CC.CDREDUCONT) AND
            (CC.CDEMPRESA = V_CDEMPRESA)    AND
            (BP.IDPRIORBPREF = 'S')         AND
            (EB.CDBANCO   (+) = CC.CDBANCO) AND
            (EB.CDEMPRESA (+) = CC.CDEMPRESA);
   END IF;

   SELECT MAX(FILIAL.CDINSCESTA), MAX(FILIAL.CDINSCMUNI)
     INTO V_CDINSCESTA, V_CDINSCMUNI
     FROM FILIAL
    WHERE FILIAL.CDFILIAL  = P_CDFILIAL
      AND FILIAL.CDEMPRESA = V_CDEMPRESA;
   IF V_CDINSCESTA IS NULL THEN
      SELECT MAX(EF.CDINSCESTA), MAX(EF.CDINSCMUNI)
        INTO V_CDINSCESTA, V_CDINSCMUNI
        FROM EMPRCNTRFIL EF
       WHERE EF.CDEMPRESA = V_CDEMPRESA
        AND EF.CDFILIAL  = P_CDFILIAL;

   END IF;
   IF V_CDSERIE IS  NULL THEN
        BEGIN
          SELECT MAX(CDSERIE)
            INTO V_CDSERIE
            FROM SERIENF
           WHERE CDEMPRESA  = V_CDEMPRESA
             AND CDINSCESTA = V_CDINSCESTA
             AND CDINSCMUNI = V_CDINSCMUNI;
        EXCEPTION
           WHEN NO_DATA_FOUND THEN
            V_CDSERIE := NULL;
        END;
    END IF;

   V_QTPARCELA := 0;
   FOR C_PARCELAS IN (SELECT NUMPARCELAS, VRNCSERVASS, NRSERVASS
                        FROM NCSERVASSASSOC
                       WHERE NRSERVASSCLI = P_NRSERVASSCLI)LOOP
       V_QTPARCELA    := C_PARCELAS.NUMPARCELAS;
       V_VRPARCELA    := C_PARCELAS.VRNCSERVASS;
       V_TOTPARCELA   := V_QTPARCELA;
            
       
            SELECT NC.VRNCSERVASS, NC.NUMPARCELAS, NS.CDNCSERVASS, DTVINGINI
              INTO V_VRPARCELA, V_QTPARCELACNT, V_CDNCSERVASS, V_DTVIGENCIA
              FROM NCSERVASSASSOC NC, NCSERVASS NS
             WHERE NRSERVASSCLI = P_NRSERVASSCLI
               AND NS.NRSERVASS = NC.NRSERVASS
               AND NC.NRSERVASS = C_PARCELAS.NRSERVASS;
           IF (V_DTVIGENCIA <= TRUNC(SYSDATE)) THEN
              V_DTASSINA := TRUNC(SYSDATE);
           ELSE   
              V_DTASSINA := V_DTVIGENCIA;

           END IF;
           
           V_DTVENCIMENTO :=  FIRST_DAY(V_DTASSINA);
           WHILE V_QTPARCELACNT > 0 LOOP
               V_VRCONTA := ROUND_ABNT(V_VRPARCELA/V_QTPARCELA,2);
               V_VRTOTAL := V_VRTOTAL + V_VRCONTA;

               NOVO_CODIGO_NT('LANCNF/FILIAL' || P_CDFILIAL,V_NRLANCTONF);
               NOVO_CODIGO_NT('NRNOTAFISCREC', V_NRNOTAFISC);
               V_NRLANCTONF  := SUBSTR(V_NRLANCTONF,11,10);
               V_NRNOTAFISC  := SUBSTR(V_NRNOTAFISC,14,7);
               INSERT INTO NOTAFISCAL
                        (CDFILIAL, NRLANCTONF, CDEMPRESA,
                         CDINSCESTA, CDINSCMUNI, CDCLIENTE, IDBLOQUEADO, VRPRODUTOS,
                         VRNOTAFISC, IDENTRSAIDOP, IDSTATUS, CDTIPOOPER, DTENTRSAID,
                         DTEMISSAO, IDSISTEMA, CDFILIEMIT, NRNOTAFISC, CDSERIE, NRSERVASS)
                     VALUES
                        (P_CDFILIAL, V_NRLANCTONF, V_CDEMPRESA,
                         V_CDINSCESTA, V_CDINSCMUNI, V_CDCLIENTE, 'S', V_VRCONTA,
                         V_VRCONTA, 'S', 'A', V_CDTIPOOPER,
                         V_DTASSINA, V_DTASSINA, 'REC', P_CDFILIAL, V_NRNOTAFISC, V_CDSERIE, V_CDNCSERVASS);


                /*MONTA HISTORICO PARA A CONTA*/
               V_DESCRICAO := MONTA_HISTORICO(V_NCSERVASSASSOC, V_CDNCSERVASS, TO_CHAR(V_TOTPARCELA),TO_CHAR(V_NRSEQVENC), V_DTASSINA);


               INSERT INTO CONTARECEBER (CDFILIAL, NRLANCTONF, CDMOTIPROR, CDMOTIPARC, CDTPCTRECEB, NRSEQVENREC,
                                         NRPARCELAREC, QTDIASVENREC, DTORIGVENREC, VRORIGREC, DTATUAVENREC, VRATUAREC,
                                         IDSTATREC, DSCONTAREC, CDFILIBOLREC, NRLANBOLREC, NRSERVASS, CDCONSUMIDOR)
                                 VALUES (P_CDFILIAL, V_NRLANCTONF, NULL, NULL, V_CDTPCTRECEB, '01', '01',
                                         0, V_DTASSINA, V_VRCONTA, V_DTASSINA,
                                         V_VRCONTA, 'A', V_DESCRICAO, NULL, NULL, V_CDNCSERVASS, V_CDCONSUMIDOR);
               /*GERA O RATEIO DA CONTA*/
                  GERA_RATEIO_CONTA('S', P_CDFILIAL, V_NRLANCTONF, '01',
                                    '01', V_VRCONTA, V_CDTPCTRECEB, P_CDFILIAL);
                V_NRSEQVENC := V_NRSEQVENC + 1;
              

                /*INSERE A CONTA A RECEBER DE ACORDO COM OS VENCIMENTOS GERADOS*/
               DELETE FROM TRAB_VENCIMENTO
                WHERE CDFILIALVENC = P_CDFILIAL
                  AND NRLANCTONFVENC = V_NRLANCTONF;
               CRIA_VENCIMENTO(P_CDFILIAL,
                               V_NRLANCTONF,
                              'S',
                              V_VRCONTA,
                              V_DTVENCIMENTO,
                              V_DTVENCIMENTO,
                              V_CDCLIENTE,
                              P_CDFILIAL,
                              NULL,
                              P_CDFILIAL,
                              P_CDFILIAL,
                              NULL,
                              NULL,
                              NULL);
              /*TRAB_VENCIMENTO : TABELA TEMPORARIA PREENCHIDA PELA CRIA_VENCIMENTO*/

              FOR C_ITEMTRAB IN (SELECT DTVENCIMENTO, VRVENCIMENTO, QTDIASVENC,  '01' AS NRSEQVENC, CDFILIALVENC AS CDFILIAL,
                                        NRLANCTONFVENC AS NRLANCTONF
                                  FROM TRAB_VENCIMENTO
                                 WHERE CDFILIALVENC = P_CDFILIAL
                                   AND NRLANCTONFVENC = V_NRLANCTONF
                                 ORDER BY NRSEQVENC) LOOP


                  ATUALIZA_FLUXO_DE_CAIXA(V_CDEMPRESA,
                         P_CDFILIAL,
                         C_ITEMTRAB.DTVENCIMENTO,
                         'E',
                         V_CDCLASSFINA,
                         C_ITEMTRAB.VRVENCIMENTO,
                         0);

                  /*MONTA HISTORICO PARA o BOLETO*/
                    BEGIN
                      SELECT MAX(CR.NRLANBOLREC)
                        INTO V_NRLANBOLREC
                        FROM CONTARECEBER CR, BOLETRECEBER BO
                       WHERE CR.CDFILIAL      = C_ITEMTRAB.CDFILIAL
                         AND CR.NRSEQVENREC   = C_ITEMTRAB.NRSEQVENC
                         AND CR.DTATUAVENREC  = C_ITEMTRAB.DTVENCIMENTO
                         AND BO.CDCLIENTE     = V_CDCLIENTE
                         AND CR.CDFILIBOLREC  = BO.CDFILIAL
                         AND CR.NRLANBOLREC   = BO.NRLANBOLREC
                         AND BO.IDSTATBREC    = 'A'
                         AND CR.CDCONSUMIDOR  = V_CDCONSUMIDOR;
                    EXCEPTION
                         WHEN NO_DATA_FOUND THEN
                         V_NRLANBOLREC := NULL;
                    END;
                    V_DESCRICAO := MONTA_HISTORICO(V_NCSERVASSASSOC, 'T',TO_CHAR(V_TOTPARCELA),TO_CHAR(V_SEQUENCIAL), V_DTASSINA);

                    IF V_NRLANBOLREC IS NULL THEN
                         NOVO_CODIGO_NT('BOLETRECEBER' || P_CDFILIAL, V_NRLANBOL, 1);
                         V_NRLANBOL    := SUBSTR(V_NRLANBOL, 11, 10);
                      INSERT INTO BOLETRECEBER
                          (CDFILIAL, NRLANBOLREC, CDCLIENTE, CDEMPRESA, VRBOLREC, VRFINALBREC,
                           DTVENBOLREC, CDTPBAIXAREC, VRPEMULBREC, VRMULTABREC, VRPEMORBREC,
                           VRMORDIABREC, CDREDUCONT, CDCARTCOBBOL, CDINS1COBBOL, CDINS2COBBOL,
                           QTDIAPROTBOL, IDIMPRBOLBOL, TXMENSBOLBOL, CDNOSNUMREC , DSLINHABARREC,
                           CDBARRAREC, DTVENDAECF, DSBOLETREC)
                      VALUES
                          (P_CDFILIAL, V_NRLANBOL, V_CDCLIENTE, V_CDEMPRESA, C_ITEMTRAB.VRVENCIMENTO,
                           C_ITEMTRAB.VRVENCIMENTO, C_ITEMTRAB.DTVENCIMENTO, V_CDTPBAIXA, V_VRPEMULTA, 0, V_VRPEMORA,
                           0, V_CDREDUCONT , V_CDCARTCOBBOL, V_CDINS1COBBOL, V_CDINS2COBBOL,
                           V_QTDIAPROTBOL, V_IDIMPRBOLBOL, V_TXMENSBOLBOL, '' , '',
                           '', C_ITEMTRAB.DTVENCIMENTO, V_DESCRICAO);

                      INSERT INTO BOLETRECASSINA
                               (CDFILIAL, NRLANBOLREC, CDCLIENTE, NRPARCELABOLREC, DTVENBOLREC, VRJUROS, VRMULTA, VRFINALBREC, IDSTATUS, CDBARRA, DSLINHADIGI , CDCONSUMIDOR)
                              VALUES
                               (P_CDFILIAL, V_NRLANBOL, V_CDCLIENTE, TO_NUMBER('00'), C_ITEMTRAB.DTVENCIMENTO, 0, 0,  C_ITEMTRAB.VRVENCIMENTO, 'A', NULL, NULL, V_CDCONSUMIDOR);
                    ELSE
                      V_NRLANBOL := V_NRLANBOLREC;
                      UPDATE BOLETRECEBER
                         SET VRBOLREC    = VRBOLREC +  C_ITEMTRAB.VRVENCIMENTO,
                             VRFINALBREC = VRFINALBREC + C_ITEMTRAB.VRVENCIMENTO
                       WHERE CDFILIAL    = C_ITEMTRAB.CDFILIAL
                         AND NRLANBOLREC = V_NRLANBOL;
                    END IF;
                    UPDATE CONTARECEBER
                       SET CDFILIBOLREC =  P_CDFILIAL,
                           NRLANBOLREC  =  V_NRLANBOL,
                           CDCLASSFINA  =  V_CDCLASSFINA,
                           CDEMPRESA    =  V_CDEMPRESA,
                           IDMOVFLUXO   =  'E',
                           CDFIMOVFLREC =  P_CDFILIAL,
                           DTMOVFLUXO   =  C_ITEMTRAB.DTVENCIMENTO,
                           QTDIASVENREC =  C_ITEMTRAB.QTDIASVENC,
                           DTATUAVENREC = C_ITEMTRAB.DTVENCIMENTO,
                           DTORIGVENREC = C_ITEMTRAB.DTVENCIMENTO
                     WHERE CDFILIAL     =  C_ITEMTRAB.CDFILIAL
                       AND NRLANCTONF   =  C_ITEMTRAB.NRLANCTONF
                       AND NRSEQVENREC  = C_ITEMTRAB.NRSEQVENC
                       AND NRPARCELAREC = '01';
                UPDATE NOTAFISCAL
                   SET DTEMISSAO   =  C_ITEMTRAB.DTVENCIMENTO,
                       DTENTRSAID  =  C_ITEMTRAB.DTVENCIMENTO
                 WHERE CDFILIAL    =  P_CDFILIAL
                   AND NRLANCTONF  =  V_NRLANCTONF;
                 CTB_REC_LANC(P_CDFILIAL, V_NRLANCTONF, '01', '01' , NULL, NULL, 'D', V_NRLANCCTB);
                UPDATE NOTAFISCAL
                   SET NRLANCTBDOC = V_NRLANCCTB
                 WHERE CDFILIAL    = P_CDFILIAL
                   AND NRLANCTONF  = V_NRLANCTONF;
              END LOOP;
              V_DTVENCIMENTO := ADD_MONTHS(V_DTVENCIMENTO,1);
              V_DTVENCIMENTO := FIRST_DAY(V_DTVENCIMENTO);
              V_SEQUENCIAL := V_SEQUENCIAL + 1;
             IF V_QTPARCELACNT = 1 THEN
                 IF V_VRPARCELA <> V_VRTOTAL THEN
                    V_VRDIF := V_VRPARCELA - V_VRTOTAL;
                    UPDATE CONTARECEBER
                       SET VRATUAREC    = VRATUAREC + V_VRDIF,
                           VRORIGREC    = VRORIGREC + V_VRDIF
                     WHERE CDFILIAL     = P_CDFILIAL
                       AND NRLANCTONF   = V_NRLANCTONF
                       AND NRSEQVENREC  = '01'
                       AND NRPARCELAREC = '01';
                      UPDATE BOLETRECEBER
                        SET VRFINALBREC = VRFINALBREC + V_VRDIF,
                            VRBOLREC    = VRBOLREC + V_VRDIF
                      WHERE CDFILIAL    = P_CDFILIAL
                        AND NRLANBOLREC = V_NRLANBOL;
                    UPDATE NOTAFISCAL
                       SET VRNOTAFISC   = VRNOTAFISC + V_VRDIF
                     WHERE CDFILIAL     = P_CDFILIAL
                       AND NRLANCTONF   = V_NRLANCTONF;
                    CTB_REC_LANC(P_CDFILIAL, V_NRLANCTONF, '01', '01' , NULL, NULL, 'D', V_NRLANCCTB);
                 END IF;
             END IF;    
             V_QTPARCELACNT :=  V_QTPARCELACNT - 1;
           END LOOP;
           
           V_NRSEQVENC := 1;
           V_VRDIF    := 0;
           V_VRTOTAL := 0;
           V_SEQUENCIAL := 1;

   END LOOP;
   UPDATE NCSERVASSCLI
       SET IDSTATUSBOLREC = 'A'
     WHERE CDFILIAL = P_CDFILIAL
       AND NRCPFRESPCON = REGEXP_REPLACE(P_NRCPFRESPCON, '\D')
       AND NRSERVASSCLI = P_NRSERVASSCLI;
EXCEPTION WHEN OTHERS THEN
    ROLLBACK;
      DELETE FROM NCSERVASSCLI 
       WHERE CDFILIAL = P_CDFILIAL
         AND NRCPFRESPCON = REGEXP_REPLACE(P_NRCPFRESPCON, '\D')
         AND NRSERVASSCLI = P_NRSERVASSCLI;
      DELETE FROM NCSERVASSASSOC
       WHERE NRSERVASSCLI = P_NRSERVASSCLI;
       COMMIT;
   RAISE_APPLICATION_ERROR(-20501,V_ERRO,TRUE);
END;
/

-- VERSAO --
BEGIN ATUVEROBJ('ATUALIZACAO_ESTRUTURA_BD','6.49.xxx') END
/