Advertisement
Guest User

asd2

a guest
Dec 3rd, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.55 KB | None | 0 0
  1. String mensaje = null;
  2.  
  3.         Empleado usuario = (Empleado) request.getSession().getAttribute("UsuarioLogeado");
  4.        
  5.         List<Producto>subproductos = new ArrayList<Producto>();
  6.         List<VentaXProductos>productos_cantidades = new ArrayList<VentaXProductos>();
  7.        
  8.         boolean valido = true;
  9.         float precioTotal = 0;
  10.         try{
  11.            
  12.             if (request.getParameter("btnNuevaVenta") != null) {               
  13.                        
  14.                 if (!request.getSession().getAttribute("Venta").equals(null)){
  15.                     Venta venta = (Venta) request.getSession().getAttribute("Venta");              
  16.                     @SuppressWarnings("unchecked")
  17.                     List<VentaXProductos>productos_cantidades_guardar = (List<VentaXProductos>) request.getSession().getAttribute("VentaXProductos");
  18.                    
  19.                     this.ventaService.save(venta);
  20.                    
  21.                    
  22.                     for(VentaXProductos vtx:productos_cantidades_guardar){
  23.                         vtx.setVenta(venta);
  24.                         this.ventaXProductosService.save(vtx);
  25.                         System.out.println("Se guardo ventaxproductos.");
  26.                     }              
  27.                    
  28.                     System.out.println("Se guardo venta.");
  29.                    
  30.                     mensaje =  "Venta Registrada!";                
  31.                    
  32.                     request.getSession().setAttribute("Venta", null);
  33.                     request.getSession().setAttribute("SubProductos", null);
  34.                 }  
  35.                    
  36.             }
  37.             if (request.getParameter("btnAgregarProducto") != null) {
  38.                 DecimalFormat df = new DecimalFormat("#.##");
  39.                 df.setRoundingMode(RoundingMode.CEILING);
  40.                
  41.                 Boolean valido_cantidades = false;
  42.                 Boolean valido_factura = false;            
  43.    
  44.                 Integer factura = Integer.parseInt(request.getParameter("inputFactura"));
  45.                 valido_factura = ValidacionesServicio.validarFactura(ventaService.getAll(), factura);
  46.                
  47.                 Float total = (float) 0;
  48.                
  49.                 Map<String, String[]> parameters = request.getParameterMap();
  50.                 for(String parameter : parameters.keySet()) {
  51.                     if(parameter.toLowerCase().startsWith("0")) {
  52.                         String[] values = parameters.get(parameter);
  53.                         for (int i = 0;i<values.length;i++){
  54.                             if (values[i] != ""){                              
  55.                                  Producto producto = productoService.getById(Integer.parseInt(parameter.toString().substring(1)));
  56.                                  producto.setCantidad(Integer.parseInt(values[i]));
  57.                                  subproductos.add(producto);
  58.                                  total+= producto.getPrecio() * producto.getCantidad();
  59.                                  valido_cantidades=true;
  60.                                  productos_cantidades.add(new VentaXProductos(producto,producto.getCantidad()));
  61.                             }                          
  62.                         }
  63.                     }
  64.                 }
  65.                
  66.                 if (valido_cantidades && valido_factura){
  67.                        
  68.                    
  69.                    
  70.                    
  71.                         total = Float.parseFloat(df.format(total));
  72.                         Venta venta = new Venta(total,
  73.                                 java.sql.Date.valueOf(request.getParameter("fecha")), factura, usuario);
  74.                        
  75.                    
  76.                        
  77.                         request.getSession().setAttribute("Venta", venta);
  78.                         request.getSession().setAttribute("SubProductos", subproductos);
  79.                         request.getSession().setAttribute("VentaXProductos", productos_cantidades);
  80.  
  81.                    
  82.                 }
  83.                 else{
  84.                     request.getSession().setAttribute("Venta", null);
  85.                     request.getSession().setAttribute("SubProductos", null);
  86.                     System.out.println("cantidades o factura error");
  87.                     if (!valido_cantidades) mensaje =  "Error en las cantidades";  
  88.                     if (!valido_factura) mensaje =  "Factura invalida, ya esta ingresada"; 
  89.                 }      
  90.             }      
  91.  
  92.         }
  93.         catch (Exception e){
  94.             System.out.println(e.getMessage());
  95.             mensaje =  "Error en completar los campos..";      
  96.             valido = false;
  97.         }
  98.        
  99.         request.getSession().setAttribute("MensajeAgregarVenta", mensaje);
  100.         response.sendRedirect("agregarVenta.jsp");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement