Пример #1
0
  @RequestMapping(value = "/lote/crea", method = RequestMethod.POST)
  public String creaLote(
      HttpServletRequest request,
      @Valid LoteEntrada lote,
      BindingResult bindingResult,
      Errors errors,
      Model modelo,
      RedirectAttributes redirectAttributes) {
    if (bindingResult.hasErrors()) {
      log.error("Hubo algun error en la forma, regresando");
      return "inventario/entrada/lote/" + request.getParameter("entrada.id");
    }

    try {
      if (request.getParameter("producto.id") == null) {
        log.warn("No se puede crear la entrada si no ha seleccionado un proveedor");
        errors.rejectValue("producto", "lote.sin.producto.message");
        return "inventario/entrada/lote/" + request.getParameter("entrada.id");
      }
      Producto producto = productoDao.obtiene(new Long(request.getParameter("producto.id")));
      Entrada entrada = entradaDao.obtiene(new Long(request.getParameter("entrada.id")));
      lote.setProducto(producto);
      lote.setEntrada(entrada);
      lote.setFechaCreacion(new Date());
      lote = entradaDao.creaLote(lote);
    } catch (NoEstaAbiertaException e) {
      log.error("No se pudo cerrar la entrada", e);
      redirectAttributes.addFlashAttribute("message", "entrada.intento.modificar.cerrada.message");
      redirectAttributes.addFlashAttribute("messageStyle", "alert-error");
      redirectAttributes.addFlashAttribute("messageAttrs", new String[] {""});
    } catch (ProductoNoSoportaFraccionException e) {
      log.error("No se pudo crear la entrada porque no se encontro el producto", e);
      return "inventario/entrada/lote/" + request.getParameter("entrada.id");
    }

    redirectAttributes.addFlashAttribute("message", "lote.creado.message");
    redirectAttributes.addFlashAttribute(
        "messageAttrs",
        new String[] {
          lote.getProducto().getNombre(),
          lote.getCantidad().toString(),
          lote.getPrecioUnitario().toString(),
          lote.getProducto().getUnidadMedida(),
          lote.getIva().add(lote.getPrecioUnitario().multiply(lote.getCantidad())).toString()
        });

    return "redirect:/inventario/entrada/ver/" + lote.getEntrada().getId();
  }