Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- String mensaje = null;
- Empleado usuario = (Empleado) request.getSession().getAttribute("UsuarioLogeado");
- List<Producto>subproductos = new ArrayList<Producto>();
- List<VentaXProductos>productos_cantidades = new ArrayList<VentaXProductos>();
- boolean valido = true;
- float precioTotal = 0;
- try{
- if (request.getParameter("btnNuevaVenta") != null) {
- if (!request.getSession().getAttribute("Venta").equals(null)){
- Venta venta = (Venta) request.getSession().getAttribute("Venta");
- @SuppressWarnings("unchecked")
- List<VentaXProductos>productos_cantidades_guardar = (List<VentaXProductos>) request.getSession().getAttribute("VentaXProductos");
- this.ventaService.save(venta);
- for(VentaXProductos vtx:productos_cantidades_guardar){
- vtx.setVenta(venta);
- this.ventaXProductosService.save(vtx);
- System.out.println("Se guardo ventaxproductos.");
- }
- System.out.println("Se guardo venta.");
- mensaje = "Venta Registrada!";
- request.getSession().setAttribute("Venta", null);
- request.getSession().setAttribute("SubProductos", null);
- }
- }
- if (request.getParameter("btnAgregarProducto") != null) {
- DecimalFormat df = new DecimalFormat("#.##");
- df.setRoundingMode(RoundingMode.CEILING);
- Boolean valido_cantidades = false;
- Boolean valido_factura = false;
- Integer factura = Integer.parseInt(request.getParameter("inputFactura"));
- valido_factura = ValidacionesServicio.validarFactura(ventaService.getAll(), factura);
- Float total = (float) 0;
- Map<String, String[]> parameters = request.getParameterMap();
- for(String parameter : parameters.keySet()) {
- if(parameter.toLowerCase().startsWith("0")) {
- String[] values = parameters.get(parameter);
- for (int i = 0;i<values.length;i++){
- if (values[i] != ""){
- Producto producto = productoService.getById(Integer.parseInt(parameter.toString().substring(1)));
- producto.setCantidad(Integer.parseInt(values[i]));
- subproductos.add(producto);
- total+= producto.getPrecio() * producto.getCantidad();
- valido_cantidades=true;
- productos_cantidades.add(new VentaXProductos(producto,producto.getCantidad()));
- }
- }
- }
- }
- if (valido_cantidades && valido_factura){
- total = Float.parseFloat(df.format(total));
- Venta venta = new Venta(total,
- java.sql.Date.valueOf(request.getParameter("fecha")), factura, usuario);
- request.getSession().setAttribute("Venta", venta);
- request.getSession().setAttribute("SubProductos", subproductos);
- request.getSession().setAttribute("VentaXProductos", productos_cantidades);
- }
- else{
- request.getSession().setAttribute("Venta", null);
- request.getSession().setAttribute("SubProductos", null);
- System.out.println("cantidades o factura error");
- if (!valido_cantidades) mensaje = "Error en las cantidades";
- if (!valido_factura) mensaje = "Factura invalida, ya esta ingresada";
- }
- }
- }
- catch (Exception e){
- System.out.println(e.getMessage());
- mensaje = "Error en completar los campos..";
- valido = false;
- }
- request.getSession().setAttribute("MensajeAgregarVenta", mensaje);
- response.sendRedirect("agregarVenta.jsp");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement