/* Procedure para gerar boletos a receber*/

IF EXISTS (SELECT * FROM sysobjects O, sysusers U
           WHERE
            o.name = 'GERA_BOLETOS_REC'  and
            type = 'P'                        and
            u.uid = o.uid                     and
            u.name = USER)

   DROP PROCEDURE GERA_BOLETOS_REC;
GO


CREATE PROCEDURE GERA_BOLETOS_REC
  (@P_NRLANBOLREC  VARCHAR(20),
   @P_CDFILIAL     VARCHAR(4),
   @P_CDCLIENTE    VARCHAR(21),
   @P_CDEMPRESA    VARCHAR(2),
   @P_VRBOLREC     double precision,
   @P_DTVENBOL     datetime,
   @P_CDTIPORECE   VARCHAR(3),
   @P_NRCHEQUEVEND VARCHAR(6),
   @P_CDBANCHEQVEN VARCHAR(3),
   @P_CDAGECHEQVEN VARCHAR(5),
   @P_CDCNTCHEQVEN VARCHAR(16))
AS
   DECLARE @V_EMPRPERMOR INT;

   DECLARE @V_VRPEMULTA    double precision;
   DECLARE @V_VRPEMORA     double precision;
   DECLARE @V_CDTPBAIXA    VARCHAR(2);
   DECLARE @V_CDREDUCONT   VARCHAR(3);
   DECLARE @V_CDINS1COBBOL VARCHAR(2);
   DECLARE @V_CDINS2COBBOL VARCHAR(2);
   DECLARE @V_CDCARTCOBBOL VARCHAR(3);
   DECLARE @V_QTDIAPROTBOL NUMERIC(11);
   DECLARE @V_IDIMPRBOLBOL VARCHAR(1);
   DECLARE @V_TXMENSBOLBOL VARCHAR;

BEGIN

   SET @V_EMPRPERMOR = 0;

   BEGIN


         SELECT @V_VRPEMULTA = MAX(VRPEMULBCLI),
                @V_VRPEMORA  = MAX(VRPEMORBCLI)
         FROM   CLIENTE
         WHERE  CDCLIENTE = @P_CDCLIENTE;

   END;

   IF @V_EMPRPERMOR = 1 BEGIN


         SELECT @V_VRPEMULTA = MAX(VRPEMULEMPR),
                @V_VRPEMORA  = MAX(VRPEMOREMPR)
         FROM EMPRESA
         WHERE (CDEMPRESA = @P_CDEMPRESA);


   END;


      SELECT @V_CDTPBAIXA = MAX(CL.CDTPBAIXAREC)
      FROM CLIENTE CL, BOLETRECEBER BR, TIPOBAIXAREC TP
      WHERE (TP.CDTPBAIXAREC = CL.CDTPBAIXAREC) AND
            (BR.CDCLIENTE    = CL.CDCLIENTE)AND
            (CL.CDCLIENTE    = @P_CDCLIENTE);


   SELECT @V_CDREDUCONT    = MAX(BP.CDREDUCONT),
          @V_CDINS1COBBOL  = MAX(ISNULL(BP.CDINS1COBRCL, EB.CDINS1COBR)),
          @V_CDINS2COBBOL  = MAX(ISNULL(BP.CDINS2COBRCL, EB.CDINS2COBR)),
          @V_CDCARTCOBBOL  = MAX(EB.CDCARTCOBR),
          @V_QTDIAPROTBOL  = MAX(EB.QTDIAPROTEST),
          @V_IDIMPRBOLBOL  = MAX(EB.IDIMPRBOLETO)
          /*@V_TXMENSBOLBOL  = EB.TXMENSBOLETO*/
   FROM   EMPRBANC EB LEFT JOIN CONTCORR CC
                      ON (EB.CDEMPRESA = CC.CDEMPRESA) AND
                         (EB.CDBANCO   = CC.CDBANCO  ),
          BANCOPREF BP
   WHERE (BP.CDCLIENTE = @P_CDCLIENTE)    AND
         (BP.CDREDUCONT = CC.CDREDUCONT)  AND
         (CC.CDEMPRESA = @P_CDEMPRESA)    AND
         (BP.IDPRIORBPREF = 'S');

   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)
   VALUES
   (@P_CDFILIAL, @P_NRLANBOLREC, @P_CDCLIENTE, @P_CDEMPRESA, @P_VRBOLREC,
    @P_VRBOLREC, @P_DTVENBOL, @V_CDTPBAIXA, @V_VRPEMULTA, 0, @V_VRPEMORA,
    0, @V_CDREDUCONT, @V_CDCARTCOBBOL, @V_CDINS1COBBOL, @V_CDINS2COBBOL,
    @V_QTDIAPROTBOL, @V_IDIMPRBOLBOL, @V_TXMENSBOLBOL, '' , '',
    '');

   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;
GO

-- VERSAO --
EXEC ATUVEROBJ 'GERA_BOLETOS_REC','5.05.061';
GO

