/* Procedure para gerar boletos a receber*/
create or replace PROCEDURE GERA_BOLETOS_REC(
   P_NRLANBOLREC  IN BOLETRECEBER.NRLANBOLREC%TYPE,
   P_CDFILIAL     IN NOTAFISCAL.CDFILIAL%TYPE,
   P_CDCLIENTE    IN BOLETRECEBER.CDCLIENTE%TYPE,
   P_CDEMPRESA    IN BOLETPAGAR.CDEMPRESA%TYPE,
   P_VRBOLREC     IN BOLETRECEBER.VRBOLREC%TYPE,
   P_DTVENBOL     IN BOLETPAGAR.DTVENBOLPAG%TYPE,
   P_CDTIPORECE   IN TIPORECE.CDTIPORECE%TYPE,
   P_NRCHEQUEVEND IN MOVCAIXA.NRCHEQUEVEND%TYPE,
   P_CDBANCHEQVEN IN MOVCAIXA.CDBANCHEQVEN%TYPE,
   P_CDAGECHEQVEN IN MOVCAIXA.CDAGECHEQVEN%TYPE,
   P_CDCNTCHEQVEN IN MOVCAIXA.CDCNTCHEQVEN%TYPE,
   P_CDREDUCONT   IN BOLETRECEBER.CDREDUCONT%TYPE,
   P_CDTPBAIXA    IN BOLETRECEBER.CDTPBAIXAREC%TYPE,
   P_DTVENDAECF   IN BOLETPAGAR.DTVENBOLPAG%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;
BEGIN

   V_EMPRPERMOR := 0;

   BEGIN
      SELECT MAX(VRPEMULBCLI), MAX(VRPEMORBCLI)
      INTO V_VRPEMULTA, V_VRPEMORA
      FROM CLIENTE
      WHERE CDCLIENTE = P_CDCLIENTE;
   EXCEPTION
      WHEN NO_DATA_FOUND THEN V_EMPRPERMOR := 1;
   END;
   IF V_EMPRPERMOR = 1 THEN
      BEGIN
         SELECT MAX(VRPEMULEMPR), MAX(VRPEMOREMPR)
         INTO V_VRPEMULTA, V_VRPEMORA
         FROM EMPRESA
         WHERE (CDEMPRESA = P_CDEMPRESA);
      EXCEPTION
         WHEN NO_DATA_FOUND THEN V_CDTPBAIXA := '';
      END;
   END IF;
   IF P_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    = P_CDCLIENTE);
      EXCEPTION
         WHEN NO_DATA_FOUND THEN V_CDTPBAIXA := '';
      END;
   END IF;
   IF P_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 = P_CDCLIENTE)    AND
            (BP.CDREDUCONT = CC.CDREDUCONT) AND
            (CC.CDEMPRESA = P_CDEMPRESA)    AND
            (BP.IDPRIORBPREF = 'S')         AND
            (EB.CDBANCO   (+) = CC.CDBANCO) AND
            (EB.CDEMPRESA (+) = CC.CDEMPRESA);
   END IF;
   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)
   VALUES
       (P_CDFILIAL, P_NRLANBOLREC, P_CDCLIENTE, P_CDEMPRESA, P_VRBOLREC,
        P_VRBOLREC, P_DTVENBOL, NVL(P_CDTPBAIXA,V_CDTPBAIXA), V_VRPEMULTA, 0, V_VRPEMORA,
        0, NVL(P_CDREDUCONT,V_CDREDUCONT), V_CDCARTCOBBOL, V_CDINS1COBBOL, V_CDINS2COBBOL,
        V_QTDIAPROTBOL, V_IDIMPRBOLBOL, V_TXMENSBOLBOL, '' , '',
        '', P_DTVENDAECF);

   INSERT INTO TPRECBOLETO
   (CDFILIAL, NRLANBOLREC, NRSEQTPRECBO, CDTIPORECE,
    VRTPRECBOLR, NRCHEQBOLREC, CDBANCBOLREC, CDAGENBOLREC, CDCONTBOLREC)
   VALUES
   (P_CDFILIAL, P_NRLANBOLREC, '01', P_CDTIPORECE,
    P_VRBOLREC, P_NRCHEQUEVEND, P_CDBANCHEQVEN, P_CDAGECHEQVEN, P_CDCNTCHEQVEN);

END;
/

-- VERSAO --
BEGIN ATUVEROBJ('GERA_BOLETOS_REC','5.01.016'); END;
/

