CREATE OR REPLACE PROCEDURE GERA_BOLETOS
   (P_CDFILIAL    IN NOTAFISCAL.CDFILIAL%TYPE,
    P_NRLANCTONF  IN NOTAFISCAL.NRLANCTONF%TYPE,
    P_CDFILILANC  IN BOLETRECEBER.CDFILIAL%TYPE,
    P_NRLANBOLREC IN BOLETRECEBER.NRLANBOLREC%TYPE,
    P_CDTPCTRECEB IN PRODUTO.CDTPCTRECEB%TYPE,
    P_IDEXCBOLREC IN VARCHAR2 DEFAULT 'N',
    P_CDFILBOLORI IN VARCHAR2 DEFAULT NULL,
    P_NRLANBOLORI IN VARCHAR2 DEFAULT NULL)

AS

   V_CDCLIENTE   CLIENTE.CDCLIENTE%TYPE;
   V_CDEMPRESA   EMPRESA.CDEMPRESA%TYPE;
   V_CDFILIEMIT  FILIAL.CDFILIAL%TYPE;
   V_VRPEMULBREC CLIENTE.VRPEMULBCLI%TYPE;
   V_VRMULTABREC CLIENTE.VRPEMULBCLI%TYPE;
   V_VRPEMORBREC CLIENTE.VRPEMORBCLI%TYPE;
   V_VRMORABREC  CLIENTE.VRPEMULBCLI%TYPE;

   V_CDSERIE     NOTAFISCAL.CDSERIE%TYPE;
   V_NRNOTAFISC  NOTAFISCAL.NRNOTAFISC%TYPE;
   V_NRINSJURNF  NOTAFISCAL.NRINSJURNF%TYPE;
   V_IDSISTEMA   NOTAFISCAL.IDSISTEMA%TYPE;

   V_CDCLASSFINA TIPOCTRECEBER.CDCLASSFINA%TYPE;
   V_CDCLASSAJUS TIPOCTRECEBER.CDCLASSFINA%TYPE;
   V_NRLANBOLREC BOLETRECEBER.NRLANBOLREC%TYPE;
   V_NRLANBOLAUX BOLETRECEBER.NRLANBOLREC%TYPE;

   V_VRPARCELAS  NUMBER;
   V_NRPARCELAS  NUMBER;
   V_NRCONTADOR  NUMBER;
   V_VRTOTACONT  CONTARECEBER.VRATUAREC%TYPE;
   V_VRACUMULADO CONTARECEBER.VRATUAREC%TYPE;
   V_VRAJUSTEBO  CONTARECEBER.VRATUAREC%TYPE;
   V_TIPO_CONTA_PROD CONTARECEBER.CDTPCTRECEB%TYPE;

   V_CDFIMFLUXO    PARAMGERAL.CDFILIAL%TYPE;
   V_IDCALPREVREC  PARAMGERAL.IDCALPREVREC%TYPE;
   V_NRREGFOR      NUMBER;

   V_CDCONTREDUGB PARAMFILIAL.CDREDUGBFILI%TYPE;
   V_CDTPBAIXAREC PARAMFILIAL.CDTPBAIXAREC%TYPE;
   V_DTVENBOLREC  BOLETRECEBER.DTVENBOLREC%TYPE;
   V_CDFILIAL     PARAMFILIAL.CDFILIAL%TYPE;

   V_CDCARTCOBR     EMPRBANC.CDCARTCOBR%TYPE;
   V_IDCARTCOBR     EMPRBANC.IDCARTCOBR%TYPE;
   V_IDIMPRBOLETO   EMPRBANC.IDIMPRBOLETO%TYPE;
   V_QTDIAPROTEST   EMPRBANC.QTDIAPROTEST%TYPE;
   V_CDINS1COBR_1   BANCOPREF.CDINS1COBRCL%TYPE;
   V_CDINS2COBR_1   BANCOPREF.CDINS2COBRCL%TYPE;
   V_CDDIAPROTEST   EMPRBANC.CDDIAPROTEST%TYPE;
   V_QTDIASPREREC   CONTARECEBER.QTDIASPREREC%TYPE;

   V_CDBANCO      BANCOPREF.CDBANCO%TYPE;
   V_IDPRIORBPREF BANCOPREF.IDPRIORBPREF%TYPE;
   V_QTDIAPRORCNT BANCO.QTDIAPRORCNT%TYPE;
   V_IDCOFERIBANC PARAMGERAL.IDCOFERIBANC%TYPE;
   DATAVENCCTAREC CONTARECEBER.DTATUAVENREC%TYPE;
   V_CDINS1COBR   BANCOPREF.CDINS1COBRCL%TYPE;
   V_CDINS2COBR   BANCOPREF.CDINS2COBRCL%TYPE;
   V_CDTIPORECE   MODPGTO.CDTIPORECE%TYPE;
   V_NRLANCCONT   NOTAFISCAL.NRLANCTBDOC%TYPE;
   V_NRSEQLANC    LANCCONT.NRSEQLANC%TYPE;
   V_VRNOTAFISC   NOTAFISCAL.VRNOTAFISC%TYPE;
   V_VRCONTAREC   CONTARECEBER.VRATUAREC%TYPE;

   V_NMRAZSOCCLIE CLIENTE.NMRAZSOCCLIE%TYPE;
   V_NMFANTCLIE   CLIENTE.NMFANTCLIE%TYPE;
   V_NMTPBAIXAREC TIPOBAIXAREC.NMTPBAIXAREC%TYPE;
   V_TIPOBAIXA    NUMBER;
   V_CDTPBAIXA    TIPOBAIXAREC.CDTPBAIXAREC%TYPE;
   V_CDTPCTRECEB  CONTARECEBER.CDTPCTRECEB%TYPE;
   V_COUNT_AJUSTE NUMBER;

   CURSOR C_CONTAAJUSTE IS
      SELECT NRSEQVENREC,  DTATUAVENREC, VRATUAREC,
             CDFILIBOLREC, NRLANBOLREC,  NRLANCTODUP,
             CDEMPRESA,    NRPARCELAREC, CDCLASSFINA,
             CDFIMOVFLREC, DTPREVRECEBI, DTMOVFLUXO, IDMOVFLUXO,
             CDTPCTRECEB
      FROM CONTARECEBER
      WHERE CDFILIAL   = P_CDFILIAL AND
            NRLANCTONF = P_NRLANCTONF
      ORDER BY NRSEQVENREC;

   CURSOR C_IMPOSNFTOT IS
      SELECT NVL(SUM(IT.VRIMPOSTOIT),0) AS VRTOTIMPONF, IP.CDPRODUTO, T.DSAJUCTREC,T.CDAJUCTREC
        FROM ITEMPROIMPOS IT, IMPOSTO I, TIPOAJRECEBER T, ITEMPRODUTO IP
       WHERE IT.CDFILIAL    = P_CDFILIAL   AND
           IT.NRLANCTONF  = P_NRLANCTONF AND
           IT.CDIMPOSTO   = I.CDIMPOSTO  AND
           IT.NRSEQITPROD = IP.NRSEQITPROD AND
           IT.CDFILIAL = IP.CDFILIAL     AND
           IT.NRLANCTONF = IP.NRLANCTONF AND
           I.IDTRATIMPO   = 'R'          AND
           I.IDDESCRFONTE = 'A'          AND
			  IT.CDFISCALIT = '1'            AND 
           I.CDAJUCTREC   = T.CDAJUCTREC
        GROUP BY IP.CDPRODUTO, T.DSAJUCTREC,T.CDAJUCTREC
        ORDER BY IP.CDPRODUTO;

BEGIN
   SELECT N.CDCLIENTE, N.CDEMPRESA, N.CDFILIEMIT,
          N.CDSERIE, N.NRNOTAFISC, N.NRINSJURNF,
          NVL(C.VRPEMULBCLI, VRPEMULEMPR),
          NVL(C.VRPEMORBCLI, E.VRPEMOREMPR),
          N.IDSISTEMA, N.NRLANCTBDOC, N.VRNOTAFISC
   INTO V_CDCLIENTE, V_CDEMPRESA, V_CDFILIEMIT,
        V_CDSERIE, V_NRNOTAFISC, V_NRINSJURNF,
        V_VRPEMULBREC, V_VRPEMORBREC, V_IDSISTEMA,
        V_NRLANCCONT, V_VRNOTAFISC
   FROM NOTAFISCAL N, CLIENTE C, EMPRESA E
   WHERE N.CDFILIAL   = P_CDFILIAL     AND
         N.NRLANCTONF = P_NRLANCTONF   AND
         N.CDCLIENTE  = C.CDCLIENTE(+) AND
         N.CDEMPRESA  = E.CDEMPRESA;

   SELECT MAX(CDCLASSFINA)
   INTO V_CDCLASSFINA
   FROM TIPOCTRECEBER
   WHERE CDTPCTRECEB = P_CDTPCTRECEB;

   SELECT MAX(NVL(IDCALPREVREC, 'V')) INTO V_IDCALPREVREC
   FROM PARAMGERAL;

   IF (P_CDFILBOLORI IS NULL) AND (P_NRLANBOLORI IS NULL) THEN
      SELECT MAX(BF.CDREDUCONT), MAX(BF.IDPRIORBPREF), MAX(BF.CDBANCO), MAX(BF.CDINS1COBRCL), MAX(BF.CDINS2COBRCL)
      INTO   V_CDCONTREDUGB, V_IDPRIORBPREF, V_CDBANCO , V_CDINS1COBR ,V_CDINS2COBR
      FROM BANCOPREF BF, CONTCORR  CC
      WHERE
         (CC.CDEMPRESA    = V_CDEMPRESA  ) AND
         (CC.CDREDUCONT   = BF.CDREDUCONT) AND
         (BF.CDCLIENTE    = V_CDCLIENTE  ) AND
         (BF.IDPRIORBPREF = 'S');

      IF (NOT V_CDBANCO IS NULL) THEN
         SELECT MAX(QTDIAPRORCNT)
         INTO V_QTDIAPRORCNT
         FROM BANCO
         WHERE (CDBANCO = V_CDBANCO);

         SELECT MAX(IDCOFERIBANC)
         INTO V_IDCOFERIBANC
         FROM PARAMGERAL;
      END IF;
   END IF;

   SELECT MAX(CDTPBAIXAREC)
   INTO   V_CDTPBAIXAREC
   FROM PARAMFILIAL
   WHERE
      (CDFILIAL = (SELECT CDFILIEMIT
                   FROM NOTAFISCAL
                   WHERE
                      CDFILIAL   = P_CDFILIAL AND
                      NRLANCTONF = P_NRLANCTONF));

   IF V_CDCONTREDUGB IS NULL THEN
      SELECT MAX(CDREDUGBFILI), MAX(CDFILIAL)
      INTO   V_CDCONTREDUGB, V_CDFILIAL
      FROM PARAMFILIAL
      WHERE
         (CDFILIAL = (SELECT CDFILIEMIT
                      FROM NOTAFISCAL
                      WHERE
                         CDFILIAL   = P_CDFILIAL AND
                         NRLANCTONF = P_NRLANCTONF));
      IF NOT V_CDCONTREDUGB IS NULL THEN
         V_CDINS1COBR := NULL;
         V_CDINS2COBR := NULL;
         SELECT MAX(CC.CDREDUCONT)
         INTO   V_CDCONTREDUGB
         FROM CONTCORR CC, FILIAL FI
         WHERE
            (CC.CDEMPRESA  = FI.CDEMPRESA) AND
            (FI.CDFILIAL   = V_CDFILIAL)   AND
            (CC.CDREDUCONT = V_CDCONTREDUGB);
      END IF;
   END IF;

   IF V_CDCONTREDUGB IS NULL THEN
     SELECT MAX(P.CDREDUGBEMPR)
      INTO V_CDCONTREDUGB
      FROM PARAMEMPRESA P
      WHERE (P.CDEMPRESA = V_CDEMPRESA);
   END IF;

   IF NOT V_CDCONTREDUGB IS NULL THEN
      SELECT MAX(E.CDINS1COBR),
             MAX(E.CDINS2COBR),
             MAX(E.CDCARTCOBR),
             MAX(E.IDCARTCOBR),
             MAX(E.IDIMPRBOLETO),
             MAX(E.QTDIAPROTEST),
             MAX(E.CDDIAPROTEST)
      INTO V_CDINS1COBR_1,
           V_CDINS2COBR_1,
           V_CDCARTCOBR,
           V_IDCARTCOBR,
           V_IDIMPRBOLETO,
           V_QTDIAPROTEST,
           V_CDDIAPROTEST
      FROM EMPRBANC E, CONTCORR C
      WHERE (E.CDEMPRESA  = V_CDEMPRESA) AND
            (E.CDBANCO    = C.CDBANCO)   AND
            (C.CDREDUCONT = V_CDCONTREDUGB);

      IF V_CDINS1COBR IS NULL AND
         V_CDINS2COBR IS NULL THEN
         V_CDINS1COBR := V_CDINS1COBR_1;
         V_CDINS2COBR := V_CDINS2COBR_1;
      END IF;
   END IF;

   IF V_CDTPBAIXAREC IS NULL THEN
      SELECT MAX(CDTPBAIXAREC)
      INTO V_CDTPBAIXAREC
      FROM PARAMEMPRESA
      WHERE (CDEMPRESA = V_CDEMPRESA);
   END IF;

   IF (V_IDSISTEMA = 'PED') OR (V_IDSISTEMA = 'EXP') THEN
      SELECT DISTINCT MAX(MO.CDTIPORECE)
      INTO  V_CDTIPORECE
      FROM PEDIDO PE, ITEMPRODUTO IP, MODPGTO MO
      WHERE MO.CDMODPGTO   = PE.CDMODPGTO AND
            IP.CDFILIALPED = PE.CDFILIAL  AND
            IP.NRPEDIDO    = PE.NRPEDIDO  AND
            IP.CDFILIAL    = P_CDFILIAL   AND
            IP.NRLANCTONF  = P_NRLANCTONF;
   END IF;

    /*gera o ajuste na conta a receber se o imposto retido estiver parametrizado para gerar na conta*/
   SELECT COUNT(*) AS NRPARCELAS, SUM(VRATUAREC) AS VRTOTACONT
   INTO V_NRPARCELAS, V_VRTOTACONT
   FROM CONTARECEBER
   WHERE CDFILIAL   = P_CDFILIAL AND
         NRLANCTONF = P_NRLANCTONF;

   FOR C_AJUSTE IN C_IMPOSNFTOT LOOP
      IF C_AJUSTE.VRTOTIMPONF > 0 THEN
         V_NRCONTADOR  := 1;
      BEGIN
        SELECT MAX(TR.CDTPCTRECEB), MAX(TR.CDCLASSFINA)
           INTO V_TIPO_CONTA_PROD, V_CDCLASSFINA
           FROM (SELECT CDARVPROD
                   FROM ITEMPRODUTO I, PRODUTO P
                  WHERE CDFILIAL    = P_CDFILIAL AND
                        NRLANCTONF  = P_NRLANCTONF AND
                        I.CDPRODUTO = P.CDPRODUTO AND
                        I.CDPRODUTO = C_AJUSTE.CDPRODUTO           
                 HAVING VRTOTAITEM = (SELECT MAX(VRTOTAITEM)
                           FROM ITEMPRODUTO
                           WHERE CDFILIAL    = P_CDFILIAL AND
                               NRLANCTONF  = P_NRLANCTONF AND
                               CDPRODUTO = C_AJUSTE.CDPRODUTO )) IT,
             PRODUTO PR, TIPOCTRECEBER TR
          WHERE TR.CDTPCTRECEB = PR.CDTPCTRECEB AND
                PR.CDPRODUTO = (SELECT MAX(CDPRODUTO)
                                  FROM TIPOCTRECEPROD C
                                 WHERE (INSTR(IT.CDARVPROD,RTRIM(C.CDARVPROD)) = 1));
       EXCEPTION
          WHEN NO_DATA_FOUND THEN
           V_TIPO_CONTA_PROD := NULL;
           V_CDCLASSFINA := NULL;
       END;
											
		   IF(V_TIPO_CONTA_PROD IS NULL) THEN
				SELECT TP.CDTPCTRECEB, TR.CDCLASSFINA
				  INTO V_TIPO_CONTA_PROD, V_CDCLASSFINA
				  FROM TIPOOPERAC TP, NOTAFISCAL NF, TIPOCTRECEBER TR
				 WHERE TP.CDTIPOOPER = NF.CDTIPOOPER
					AND NF.CDFILIAL = P_CDFILIAL
					AND NF.NRLANCTONF = P_NRLANCTONF
               AND TP.CDTPCTRECEB = TR.CDTPCTRECEB
					AND TP.IDENTRSAIDOP = 'S'
               AND NF.IDENTRSAIDOP = TP.IDENTRSAIDOP;
         END IF;

         V_VRACUMULADO := ROUND(C_AJUSTE.VRTOTIMPONF, 2);

        FOR C_CNTREC IN C_CONTAAJUSTE LOOP
          IF(V_TIPO_CONTA_PROD = C_CNTREC.CDTPCTRECEB) THEN
        
            V_VRAJUSTEBO  := ROUND(C_AJUSTE.VRTOTIMPONF, 2);
			
            IF C_CNTREC.CDCLASSFINA IS NULL THEN
              V_CDCLASSAJUS := V_CDCLASSFINA;
            ELSE
              V_CDCLASSAJUS := C_CNTREC.CDCLASSFINA;
            END IF;

            IF C_CNTREC.NRLANCTODUP IS NULL THEN
					
              ATUALIZA_FLUXO_DE_CAIXA(V_CDEMPRESA,
                                      V_CDFILIEMIT,
                                      C_CNTREC.DTATUAVENREC,
                                      'E',
                                      V_CDCLASSAJUS,
                                      V_VRAJUSTEBO,
                                      0);
                                      
          --Verifica se ja existe um ajuste com mesmo codigo
          V_COUNT_AJUSTE := 0;
          BEGIN
            SELECT COUNT(CDAJUCTREC)
              INTO V_COUNT_AJUSTE
              FROM AJCTRECEBER
             WHERE CDFILIAL = P_CDFILIAL
               AND NRLANCTONF = P_NRLANCTONF
               AND NRSEQVENREC = C_CNTREC.NRSEQVENREC
               AND NRPARCELAREC = C_CNTREC.NRPARCELAREC
               AND CDAJUCTREC = C_AJUSTE.CDAJUCTREC;							
          EXCEPTION
          WHEN NO_DATA_FOUND THEN
             V_COUNT_AJUSTE := 0;       
          END;
              
              IF(V_COUNT_AJUSTE = 0) THEN								
                INSERT INTO AJCTRECEBER
                  (CDFILIAL, NRLANCTONF, DTAJUSTEREC, CDAJUCTREC, NRSEQVENREC, NRPARCELAREC,
                   VRAJUSTEREC, DSAJUSTEREC,CDCLASSFINA, DTMOVFLUXO, IDMOVFLUXO, CDEMMFAJCNTR, CDFIMFAJCNTR,IDAJIMPRET)
                VALUES
                  (P_CDFILIAL, P_NRLANCTONF, C_CNTREC.DTATUAVENREC, C_AJUSTE.CDAJUCTREC, C_CNTREC.NRSEQVENREC, C_CNTREC.NRPARCELAREC, 
                   V_VRAJUSTEBO, SUBSTR(C_AJUSTE.DSAJUCTREC,1,20), V_CDCLASSAJUS, C_CNTREC.DTATUAVENREC, 'E', V_CDEMPRESA, V_CDFILIEMIT,'S');
              ELSE
                UPDATE AJCTRECEBER
                   SET VRAJUSTEREC    = ROUND((VRAJUSTEREC + V_VRAJUSTEBO),2)
                 WHERE CDFILIAL     = P_CDFILIAL
                   AND NRLANCTONF   = P_NRLANCTONF
                   AND NRSEQVENREC  = C_CNTREC.NRSEQVENREC
                   AND NRPARCELAREC = C_CNTREC.NRPARCELAREC
                AND CDAJUCTREC = C_AJUSTE.CDAJUCTREC;	
              END IF;
  
              UPDATE CONTARECEBER
                SET VRATUAREC    = ROUND((VRATUAREC - V_VRAJUSTEBO),2)
               WHERE CDFILIAL     = P_CDFILIAL
                AND NRLANCTONF   = P_NRLANCTONF
                AND NRSEQVENREC  = C_CNTREC.NRSEQVENREC
                AND NRPARCELAREC = C_CNTREC.NRPARCELAREC;
            END IF;
				   V_NRCONTADOR := V_NRCONTADOR + 1;
				 END IF;
       END LOOP;
      END IF;
  END LOOP;

   V_NRPARCELAS  := 0;
   V_NRLANBOLREC := P_NRLANBOLREC;

   FOR C_CNTREC IN (SELECT C.NRSEQVENREC, C.DTATUAVENREC, C.DTORIGVENREC,
                           C.VRATUAREC, C.CDFILIBOLREC, C.NRLANBOLREC,
                           C.CDEMPRESA, C.CDFIMOVFLREC, C.DTMOVFLUXO,
                           C.IDMOVFLUXO, C.CDCLASSFINA, C.VRORIGREC,
                           C.NRPARCELAREC, C.IDSTATREC, C.DTPREVRECEBI,
                           C.QTDIASPREREC
                      FROM CONTARECEBER C, DUPLICATA D
                     WHERE C.CDFILIAL    = P_CDFILIAL       AND
                           C.NRLANCTONF  = P_NRLANCTONF     AND
                           C.CDFILIALDUP = D.CDFILIAL(+)    AND
                           C.NRLANCTODUP = D.NRLANCTODUP(+) AND
                           (C.NRLANCTODUP IS NULL OR D.CDINTEGDUP IS NOT NULL)
                     ORDER BY NRSEQVENREC) LOOP

      IF (P_CDFILBOLORI IS NULL AND P_NRLANBOLORI IS NULL) OR
         (C_CNTREC.CDFILIBOLREC = P_CDFILBOLORI AND C_CNTREC.NRLANBOLREC = P_NRLANBOLORI) THEN

         IF (P_CDFILBOLORI IS NULL) AND (P_NRLANBOLORI IS NULL) THEN
            IF (NOT V_CDBANCO IS NULL) THEN
               DATAVENCCTAREC := NVL(C_CNTREC.DTPREVRECEBI, C_CNTREC.DTATUAVENREC);
               C_CNTREC.DTATUAVENREC := C_CNTREC.DTORIGVENREC;
               IF (NOT V_QTDIAPRORCNT IS NULL) THEN
                  C_CNTREC.DTATUAVENREC := C_CNTREC.DTATUAVENREC + V_QTDIAPRORCNT;
               END IF;
               IF V_IDCOFERIBANC = 'S' THEN
                  FOR C_FERIBC IN (
                      SELECT DTFERIBANC
                      FROM FERIBANC
                      WHERE
                         (CDFILIAL =    P_CDFILIAL) AND
                         (DTFERIBANC >= C_CNTREC.DTATUAVENREC)) LOOP

                     IF C_FERIBC.DTFERIBANC = C_CNTREC.DTATUAVENREC THEN
                        C_CNTREC.DTATUAVENREC := C_CNTREC.DTATUAVENREC + 1;
                     ELSE
                        EXIT;
                     END IF;
                  END LOOP;
               END IF;
            END IF;
         END IF;

         IF P_IDEXCBOLREC = 'S' THEN
            /* - NA EXCLUSAO DA DUPLICATA VOLTA TODOS OS VENCIMENTOS PARA DATA ORIGINAL - */
            V_DTVENBOLREC := C_CNTREC.DTORIGVENREC;
         ELSE
            V_DTVENBOLREC := C_CNTREC.DTATUAVENREC;
         END IF;

         V_TIPOBAIXA := 0;

         SELECT
            COUNT(*)
         INTO
            V_TIPOBAIXA
         FROM
            CLIENTE CL, TIPOBAIXAREC TP
         WHERE
                (TP.CDTPBAIXAREC = CL.CDTPBAIXAREC   )
            AND (CL.CDCLIENTE    = V_CDCLIENTE       )
         ORDER BY
            CL.NMRAZSOCCLIE;

         IF V_TIPOBAIXA > 0 THEN
            SELECT
               CL.CDTPBAIXAREC, CL.CDCLIENTE,
               CL.NMRAZSOCCLIE, CL.NMFANTCLIE , TP.NMTPBAIXAREC
            INTO
               V_CDTPBAIXA, V_CDCLIENTE,
               V_NMRAZSOCCLIE, V_NMFANTCLIE , V_NMTPBAIXAREC
            FROM
               CLIENTE CL, TIPOBAIXAREC TP
            WHERE
                   (TP.CDTPBAIXAREC = CL.CDTPBAIXAREC   )
               AND (CL.CDCLIENTE    = V_CDCLIENTE       )
            ORDER BY
               CL.NMRAZSOCCLIE;


            IF NOT V_CDTPBAIXA IS NULL THEN
               V_CDTPBAIXAREC := V_CDTPBAIXA;
            END IF ;
         END IF;

         /*CALCULA O VALOR DA MULTA CONFORME PERCENTUAL PARAMETRIZADO*/
         V_VRMULTABREC := 0;
         IF V_VRPEMULBREC > 0 THEN
            V_VRMULTABREC := C_CNTREC.VRATUAREC * V_VRPEMULBREC / 100;
         END IF;

         /*CALCULA O VALOR DA MORA DIARIA CONFORME PERCENTUAL PARAMETRIZADO*/
         V_VRMORABREC := 0;
         IF V_VRPEMORBREC > 0 THEN
            V_VRMORABREC := C_CNTREC.VRATUAREC * V_VRPEMORBREC / 100;
         END IF;

         V_NRLANBOLAUX := V_NRLANBOLREC;
         IF (V_IDSISTEMA = 'PDV') AND (C_CNTREC.IDSTATREC = 'P') THEN
            V_NRLANBOLREC := C_CNTREC.NRLANBOLREC;
            UPDATE BOLETRECEBER
            SET VRBOLREC     = C_CNTREC.VRATUAREC,
                VRFINALBREC  = C_CNTREC.VRATUAREC,
                DTVENBOLREC  = V_DTVENBOLREC,
                VRPEMULBREC  = V_VRPEMULBREC,
                VRMULTABREC  = V_VRMULTABREC,
                VRPEMORBREC  = V_VRPEMORBREC,
                VRMORDIABREC = V_VRMORABREC,
                CDINS1COBBOL = V_CDINS1COBR,
                CDINS2COBBOL = V_CDINS2COBR,
                CDCARTCOBBOL = V_CDCARTCOBR,
                IDCARTCOBREC = V_IDCARTCOBR,
                IDIMPRBOLBOL = V_IDIMPRBOLETO,
                QTDIAPROTBOL = V_QTDIAPROTEST,
                CDDIAPROTBOL = V_CDDIAPROTEST
            WHERE
               CDFILIAL    = P_CDFILILANC AND
               NRLANBOLREC = V_NRLANBOLREC;
         ELSE
            INSERT INTO BOLETRECEBER
               (CDFILIAL,     NRLANBOLREC,  CDCLIENTE,    CDEMPRESA,
                VRBOLREC,     VRFINALBREC,  DTVENBOLREC,  CDTPBAIXAREC,
                IDSTATBREC,   VRPEMULBREC,  VRMULTABREC,  VRPEMORBREC,
                VRMORDIABREC, CDREDUCONT,   CDINS1COBBOL, CDINS2COBBOL,
                CDCARTCOBBOL, IDCARTCOBREC, IDIMPRBOLBOL, QTDIAPROTBOL,
                CDDIAPROTBOL)
            VALUES
               (P_CDFILILANC, V_NRLANBOLREC, V_CDCLIENTE, V_CDEMPRESA,
                C_CNTREC.VRATUAREC, C_CNTREC.VRATUAREC, V_DTVENBOLREC,   V_CDTPBAIXAREC,
                'A', V_VRPEMULBREC, V_VRMULTABREC, V_VRPEMORBREC,
                V_VRMORABREC, V_CDCONTREDUGB, V_CDINS1COBR, V_CDINS2COBR,
                V_CDCARTCOBR, V_IDCARTCOBR, V_IDIMPRBOLETO, V_QTDIAPROTEST,
                V_CDDIAPROTEST);
         END IF;

         IF (NOT V_CDTIPORECE IS NULL) THEN
            INSERT INTO TPRECBOLETO
               (CDFILIAL,   NRLANBOLREC, NRSEQTPRECBO,
                CDTIPORECE, VRTPRECBOLR)
            VALUES
               (P_CDFILILANC, V_NRLANBOLREC, '01',
                V_CDTIPORECE, C_CNTREC.VRATUAREC);
         END IF;

         IF C_CNTREC.QTDIASPREREC IS NULL THEN
            IF V_IDCALPREVREC = '6' THEN
               SELECT NVL(QTPREVRECCLI, 0) INTO V_QTDIASPREREC
               FROM CLIENTE
               WHERE CDCLIENTE = V_CDCLIENTE;
            ELSE
               V_QTDIASPREREC := 0;
            END IF;
         ELSE
            V_QTDIASPREREC := C_CNTREC.QTDIASPREREC;
         END IF;

         /* - VERIFICA SE O PROCEDURE ESTA SENDO CHAMADA DA PROCEDURE DE EXCLUSAO DE DUPLICATA - */
         IF P_IDEXCBOLREC = 'S' THEN
            IF C_CNTREC.DTORIGVENREC + TRUNC(V_QTDIASPREREC) <> C_CNTREC.DTATUAVENREC THEN
               /* - ATUALIZA O FLUXO DE CAIXA -------------------------- */
               IF C_CNTREC.DTMOVFLUXO IS NOT NULL THEN
                  ATUALIZA_FLUXO_DE_CAIXA(C_CNTREC.CDEMPRESA,
                                          C_CNTREC.CDFIMOVFLREC,
                                          C_CNTREC.DTMOVFLUXO,
                                          C_CNTREC.IDMOVFLUXO,
                                          C_CNTREC.CDCLASSFINA,
                                          C_CNTREC.VRORIGREC * -1,
                                          0);
               END IF;

               ATUALIZA_FLUXO_DE_CAIXA(C_CNTREC.CDEMPRESA,
                                       C_CNTREC.CDFIMOVFLREC,
                                       C_CNTREC.DTORIGVENREC + TRUNC(V_QTDIASPREREC),
                                       C_CNTREC.IDMOVFLUXO,
                                       C_CNTREC.CDCLASSFINA,
                                       C_CNTREC.VRORIGREC,
                                       0);

               /* -- ATUALIZA O FLUXO DE CAIXA DOS AJUSTES DA CONTA -------------*/
               SELECT
                  COUNT(*)
               INTO
                  V_NRREGFOR
               FROM
                  AJCTRECEBER
               WHERE
                  (CDFILIAL     = P_CDFILIAL)           AND
                  (NRLANCTONF   = P_NRLANCTONF)         AND
                  (NRSEQVENREC  = C_CNTREC.NRSEQVENREC) AND
                  (NRPARCELAREC = C_CNTREC.NRPARCELAREC);

               IF V_NRREGFOR > 0 THEN
                  FOR C_AJUSTECNT IN
                     (SELECT
                         AJ.CDFILIAL, AJ.NRLANCTONF, AJ.NRSEQVENREC,
                         AJ.NRPARCELAREC, AJ.DTAJUSTEREC, AJ.CDAJUCTREC,
                         AJ.CDCLASSFINA, AJ.CDEMMFAJCNTR, AJ.CDFIMFAJCNTR,
                         AJ.DTMOVFLUXO, AJ.IDMOVFLUXO, AJ.VRAJUSTEREC, TA.IDAJUCTREC
                      FROM
                         AJCTRECEBER   AJ,
                         TIPOAJRECEBER TA
                      WHERE
                         (AJ.CDFILIAL     = P_CDFILIAL)            AND
                         (AJ.NRLANCTONF   = P_NRLANCTONF)          AND
                         (AJ.NRSEQVENREC  = C_CNTREC.NRSEQVENREC)  AND
                         (AJ.NRPARCELAREC = C_CNTREC.NRPARCELAREC) AND
                         (TA.CDAJUCTREC   = AJ.CDAJUCTREC)
                      )LOOP

                     IF C_AJUSTECNT.IDAJUCTREC = 'A' THEN
                        ATUALIZA_FLUXO_DE_CAIXA(C_AJUSTECNT.CDEMMFAJCNTR,
                                                C_AJUSTECNT.CDFIMFAJCNTR,
                                                C_AJUSTECNT.DTMOVFLUXO,
                                                C_AJUSTECNT.IDMOVFLUXO,
                                                C_AJUSTECNT.CDCLASSFINA,
                                                C_AJUSTECNT.VRAJUSTEREC * -1,
                                                0);

                        ATUALIZA_FLUXO_DE_CAIXA(C_AJUSTECNT.CDEMMFAJCNTR,
                                                C_AJUSTECNT.CDFIMFAJCNTR,
                                                C_CNTREC.DTORIGVENREC + TRUNC(V_QTDIASPREREC),
                                                C_AJUSTECNT.IDMOVFLUXO,
                                                C_AJUSTECNT.CDCLASSFINA,
                                                C_AJUSTECNT.VRAJUSTEREC,
                                                0);
                     ELSE
                        ATUALIZA_FLUXO_DE_CAIXA(C_AJUSTECNT.CDEMMFAJCNTR,
                                                C_AJUSTECNT.CDFIMFAJCNTR,
                                                C_AJUSTECNT.DTMOVFLUXO,
                                                C_AJUSTECNT.IDMOVFLUXO,
                                                C_AJUSTECNT.CDCLASSFINA,
                                                C_AJUSTECNT.VRAJUSTEREC,
                                                0);

                        ATUALIZA_FLUXO_DE_CAIXA(C_AJUSTECNT.CDEMMFAJCNTR,
                                                C_AJUSTECNT.CDFIMFAJCNTR,
                                                C_CNTREC.DTORIGVENREC + TRUNC(V_QTDIASPREREC),
                                                C_AJUSTECNT.IDMOVFLUXO,
                                                C_AJUSTECNT.CDCLASSFINA,
                                                C_AJUSTECNT.VRAJUSTEREC * -1,
                                                0);
                     END IF;

                     /* -- ALTERA A CHAVE DO MOVIMENTO DO FLUXO -------------*/
                     UPDATE AJCTRECEBER
                     SET DTMOVFLUXO = C_CNTREC.DTORIGVENREC + TRUNC(V_QTDIASPREREC)
                     WHERE
                        (CDFILIAL     = C_AJUSTECNT.CDFILIAL)     AND
                        (NRLANCTONF   = C_AJUSTECNT.NRLANCTONF)   AND
                        (NRSEQVENREC  = C_AJUSTECNT.NRSEQVENREC)  AND
                        (NRPARCELAREC = C_AJUSTECNT.NRPARCELAREC) AND
                        (DTAJUSTEREC  = C_AJUSTECNT.DTAJUSTEREC)  AND
                        (CDAJUCTREC   = C_AJUSTECNT.CDAJUCTREC)   AND
                        (CDCLASSFINA  = C_AJUSTECNT.CDCLASSFINA);
                  END LOOP;
               END IF;
            END IF;

            /* -- ALTERA A CHAVE DO MOVIMENTO DO FLUXO E DO BOLETO-------------*/
            UPDATE CONTARECEBER
            SET CDFILIBOLREC   = P_CDFILILANC,
                NRLANBOLREC    = V_NRLANBOLREC,
                DTATUAVENREC   = C_CNTREC.DTORIGVENREC,
                CDMOTIPROR     = NULL,
                QTDIASPREREC   = V_QTDIASPREREC,
                DTPREVRECEBI   = C_CNTREC.DTORIGVENREC + TRUNC(V_QTDIASPREREC),
                DTMOVFLUXO     = C_CNTREC.DTORIGVENREC + TRUNC(V_QTDIASPREREC)
            WHERE CDFILIAL     = P_CDFILIAL           AND
                  NRLANCTONF   = P_NRLANCTONF         AND
                  NRSEQVENREC  = C_CNTREC.NRSEQVENREC AND
                  NRPARCELAREC = C_CNTREC.NRPARCELAREC;
         ELSE
            /*RETORNA O TIPO DE CONTA JA CADASTRADO*/
            SELECT CDTPCTRECEB
              INTO V_CDTPCTRECEB
              FROM CONTARECEBER
             WHERE CDFILIAL     = P_CDFILIAL           AND
                   NRLANCTONF   = P_NRLANCTONF         AND
                   NRSEQVENREC  = C_CNTREC.NRSEQVENREC AND
                   NRPARCELAREC = C_CNTREC.NRPARCELAREC;

            /* SE O TIPO NAO ESTIVER CADASTRADO SALVA O TIPO PADRAO */
            IF (V_CDTPCTRECEB IS NULL) THEN
               V_CDTPCTRECEB := P_CDTPCTRECEB;
            END IF;

            IF C_CNTREC.DTMOVFLUXO IS NOT NULL THEN
               /* - ATUALIZA O FLUXO DE CAIXA -------------------------- */
               ATUALIZA_FLUXO_DE_CAIXA(V_CDEMPRESA,
                                       V_CDFILIEMIT,
                                       C_CNTREC.DTMOVFLUXO,
                                       'E',
                                       V_CDCLASSFINA,
                                       C_CNTREC.VRORIGREC * -1,
                                       0);
            END IF;

            ATUALIZA_FLUXO_DE_CAIXA(V_CDEMPRESA,
                                    V_CDFILIEMIT,
                                    C_CNTREC.DTATUAVENREC + TRUNC(V_QTDIASPREREC),
                                    'E',
                                    V_CDCLASSFINA,
                                    C_CNTREC.VRORIGREC,
                                    0);

            UPDATE CONTARECEBER
            SET CDTPCTRECEB    = V_CDTPCTRECEB,
                CDFILIBOLREC   = P_CDFILILANC,
                NRLANBOLREC    = V_NRLANBOLREC,
                DTATUAVENREC   = C_CNTREC.DTATUAVENREC,
                CDCLASSFINA    = V_CDCLASSFINA,
                CDEMPRESA      = V_CDEMPRESA,
                CDFIMOVFLREC   = V_CDFILIEMIT,
                QTDIASPREREC   = V_QTDIASPREREC,
                DTPREVRECEBI   = C_CNTREC.DTATUAVENREC + TRUNC(V_QTDIASPREREC),
                DTMOVFLUXO     = C_CNTREC.DTATUAVENREC + TRUNC(V_QTDIASPREREC),
                IDMOVFLUXO     = 'E',
                DSCONTAREC     = NVL(DSCONTAREC,'NF: '||V_NRINSJURNF||' - '||V_NRNOTAFISC||' - '||V_CDSERIE)
            WHERE CDFILIAL     = P_CDFILIAL           AND
                  NRLANCTONF   = P_NRLANCTONF         AND
                  NRSEQVENREC  = C_CNTREC.NRSEQVENREC AND
                  NRPARCELAREC = C_CNTREC.NRPARCELAREC;

            V_NRCONTADOR := 1;
            V_VRACUMULADO := 0;

            DELETE RATCTRECEBER
            WHERE CDFILIAL    = P_CDFILIAL   AND
                  NRLANCTONF  = P_NRLANCTONF AND
                  NRSEQVENREC = C_CNTREC.NRSEQVENREC;

            SELECT MAX(NRSEQLANC)
            INTO V_NRSEQLANC
            FROM LANCCONT
            WHERE CDEMPRESA   = V_CDEMPRESA    AND
                  NRLANCCONT  = V_NRLANCCONT   AND
                  VRLANCAMENT = (SELECT MAX(VRLANCAMENT)
                                 FROM LANCCONT
                                 WHERE CDEMPRESA  = V_CDEMPRESA AND
                                       NRLANCCONT = V_NRLANCCONT);

            IF V_NRSEQLANC IS NOT NULL THEN
               FOR C_RATTPCTR IN (SELECT LC.CDCENTCUST, LC.CDFILIAL, (LC.VRLANCCUST/L.VRLANCAMENT) AS VRLANCCUST
                                  FROM LANCCCUSTO LC, LANCCONT L
                                  WHERE LC.CDEMPRESA  = V_CDEMPRESA  AND
                                        LC.NRLANCCONT = V_NRLANCCONT AND
                                        LC.NRSEQLANC  = V_NRSEQLANC  AND
                                        L.CDEMPRESA   = V_CDEMPRESA  AND
                                        L.NRLANCCONT  = V_NRLANCCONT AND
                                        L.NRSEQLANC   = V_NRSEQLANC) LOOP

                  V_VRPARCELAS  := ROUND(C_CNTREC.VRATUAREC * C_RATTPCTR.VRLANCCUST, 2);
                  V_VRACUMULADO := V_VRACUMULADO + V_VRPARCELAS;

                  IF V_NRPARCELAS = V_NRCONTADOR THEN
                     IF V_VRACUMULADO > C_CNTREC.VRATUAREC THEN
                        V_VRPARCELAS := ROUND(V_VRPARCELAS - (ABS(C_CNTREC.VRATUAREC - V_VRACUMULADO)),2);
                     ELSE
                        IF V_VRACUMULADO < C_CNTREC.VRATUAREC THEN
                           V_VRPARCELAS := ROUND(V_VRPARCELAS + (ABS(C_CNTREC.VRATUAREC - V_VRACUMULADO)),2);
                        END IF;
                     END IF;
                  END IF;

                  INSERT INTO RATCTRECEBER
                     (CDFILIAL, NRLANCTONF, NRSEQVENREC, NRPARCELAREC,
                      CDCENTCUST, CDFIRATCTREC, VRRATCCCTREC)
                  VALUES
                     (P_CDFILIAL, P_NRLANCTONF, C_CNTREC.NRSEQVENREC, C_CNTREC.NRPARCELAREC,
                      C_RATTPCTR.CDCENTCUST, C_RATTPCTR.CDFILIAL, V_VRPARCELAS);

                  V_NRCONTADOR := V_NRCONTADOR + 1;
               END LOOP;
            ELSE
               SELECT COUNT(*)
               INTO V_NRPARCELAS
               FROM RATTPCTR
               WHERE CDTPCTRECEB = P_CDTPCTRECEB;

               IF V_NRPARCELAS > 0 THEN
                  FOR C_RATTPCTR IN (SELECT CDTPCTRECEB, CDCENTCUST,
                                            NVL(LTRIM(REPLACE(CDFIRATTPCTR, '----', V_CDFILIEMIT)), V_CDFILIEMIT) AS CDFIRATTPCTR,
                                            CDFILIAL, VRPERATTPCTR
                                     FROM RATTPCTR
                                     WHERE CDTPCTRECEB = P_CDTPCTRECEB) LOOP

                     V_VRPARCELAS  := ROUND(C_CNTREC.VRATUAREC * (C_RATTPCTR.VRPERATTPCTR/100),2);
                     V_VRACUMULADO := V_VRACUMULADO + V_VRPARCELAS;


                     IF V_NRPARCELAS = V_NRCONTADOR THEN
                        IF V_VRACUMULADO > C_CNTREC.VRATUAREC THEN
                           V_VRPARCELAS := ROUND(V_VRPARCELAS - (ABS(C_CNTREC.VRATUAREC - V_VRACUMULADO)),2);
                        ELSE
                           IF V_VRACUMULADO < C_CNTREC.VRATUAREC THEN
                              V_VRPARCELAS := ROUND(V_VRPARCELAS + (ABS(C_CNTREC.VRATUAREC - V_VRACUMULADO)),2);
                           END IF;
                        END IF;
                     END IF;

                     INSERT INTO RATCTRECEBER
                        (CDFILIAL, NRLANCTONF, NRSEQVENREC, NRPARCELAREC,
                         CDCENTCUST, CDFIRATCTREC, VRRATCCCTREC)
                     VALUES
                        (P_CDFILIAL, P_NRLANCTONF, C_CNTREC.NRSEQVENREC, C_CNTREC.NRPARCELAREC,
                         C_RATTPCTR.CDCENTCUST, C_RATTPCTR.CDFIRATTPCTR, V_VRPARCELAS);

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

         IF (V_IDSISTEMA = 'PDV') AND (C_CNTREC.IDSTATREC = 'P') THEN
            V_NRLANBOLREC := V_NRLANBOLAUX;
         END IF;

         V_NRLANBOLREC := LPAD(TO_CHAR(TO_NUMBER(V_NRLANBOLREC) + 1), 10, '0');
      END IF;
   END LOOP;
END;
/