public ActionResult Editar(ArticuloEditarViewModel articuloVM) { if (articuloVM != null && ModelState.IsValid) { //Valido que la longitud de Nombre Etiqueta sea correcta var LongitudNombreEtiqueta = _configuracionesServicios.GetLongitudNombreEtiqueta(); if (articuloVM.NombreEtiqueta.Length > LongitudNombreEtiqueta) { ViewBag.Error = "Longitud Máxima del campo Nombre Etqueta " + LongitudNombreEtiqueta + " caracteres"; ViewBag.Rubros = _rubrosServicios.GetAll(); return(View("Editar", articuloVM)); } var bandera = _articulosServicios.Editar(articuloVM.Mapear()); if (bandera) { var mensaje = "El Artículo se ha actualizado correctamente!"; return(RedirectToAction("Index", new { msj = mensaje })); } else { ViewBag.Error = "El Artículo no se ha actualizado, vuelva a intentarlo."; ViewBag.Rubros = _rubrosServicios.GetAll(); return(View("Editar", articuloVM)); } } else { ViewBag.Error = "El Artículo no se ha actualizado, vuelva a intentarlo."; ViewBag.Rubros = _rubrosServicios.GetAll(); return(View("Editar", articuloVM)); } }
public ActionResult Importar(HttpPostedFileBase upload) { try { if (upload != null && upload.ContentLength > 0) { //string filePath = Path.Combine(HttpContext.Server.MapPath("~/Uploads"), Path.GetFileName(upload.FileName)); string file = Path.GetFileName(upload.FileName); //string path = WebConfigurationManager.AppSettings["CarpetaUpload"].ToString(); //string filePath = @"" + path + file; string filePath = Path.Combine(System.Web.HttpContext.Current.Server.MapPath(@"~/"), file); //finalFileNameWithPath = string.Format("{0}.xlsx", currentDirectorypath); upload.SaveAs(filePath); DataSet ds = new DataSet(); string ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties=Excel 12.0;"; // string ConnectionString = WebConfigurationManager.AppSettings["ExcelConn"].ToString(); using (OleDbConnection conn = new System.Data.OleDb.OleDbConnection(ConnectionString)) { conn.Open(); using (DataTable dtExcelSchema = conn.GetSchema("Tables")) { string sheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString(); string query = "SELECT * FROM [" + sheetName + "]"; OleDbDataAdapter adapter = new OleDbDataAdapter(query, conn); adapter.Fill(ds, "Items"); if (ds.Tables.Count > 0) { List <Articulo> la = new List <Articulo>(); if (ds.Tables[0].Rows.Count > 0) { for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { //TODO: POR SI HAY QUE ACTUALIZAR PRECIOS //var idArt = 0; var nombreArt = ""; var precioCompra = 0m; var precioVenta = 0m; var rubroId = 0; var stockMax = 0; var stockMin = 0; var stockAct = 0; var stockSuc1 = 0; var stockSuc2 = 0; //try { idProd = int.Parse(ds.Tables[0].Rows[i].ItemArray[0].ToString()); } //catch { } try { nombreArt = ds.Tables[0].Rows[i].ItemArray[0].ToString(); } catch { } try { precioCompra = decimal.Parse(ds.Tables[0].Rows[i].ItemArray[1].ToString()); } catch { } try { precioVenta = decimal.Parse(ds.Tables[0].Rows[i].ItemArray[2].ToString()); } catch { } try { rubroId = int.Parse(ds.Tables[0].Rows[i].ItemArray[3].ToString()); } catch { } try { stockMax = int.Parse(ds.Tables[0].Rows[i].ItemArray[4].ToString()); } catch { } try { stockMin = int.Parse(ds.Tables[0].Rows[i].ItemArray[5].ToString()); } catch { } try { stockAct = int.Parse(ds.Tables[0].Rows[i].ItemArray[6].ToString()); } catch { } try { stockSuc1 = int.Parse(ds.Tables[0].Rows[i].ItemArray[7].ToString()); } catch { } try { stockSuc2 = int.Parse(ds.Tables[0].Rows[i].ItemArray[8].ToString()); } catch { } Articulo a = new Articulo(); a.Nombre = nombreArt; //Lleno el Nombre etiqueta. Si es necesario lo corto var LongitudNombreEtiqueta = _configuracionServicios.GetLongitudNombreEtiqueta(); var LongitudNombre = a.Nombre.Length; if (LongitudNombre > LongitudNombreEtiqueta) { a.NombreEtiqueta = a.Nombre.Remove(LongitudNombreEtiqueta, LongitudNombre - LongitudNombreEtiqueta); } else { a.NombreEtiqueta = a.Nombre; } a.PrecioActualCompra = precioCompra; a.PrecioActualVenta = precioVenta; a.RubroID = rubroId; a.StockMaximo = stockMax; a.StockMinimo = stockMin; a.Habilitado = true; //TODO (Agregar sucursales dinámicamente) //Esto es Harcodeo del bueno!! Ssssabor! Azúca! a.Stock = new List <StockArticuloSucursal>(); //Stock deposito a.Stock = AsignoStockASucursales(stockAct, stockSuc1, stockSuc2); la.Add(a); } } _articulosServicios.ImportarArticulos(la); ViewBag.Informacion = "Los articulos se han importado correctamente!"; } } } } } catch (Exception ex) { ViewBag.Error = "Ocurrió un error al intentar importar los precios. Revise el archivo .xlsx" + ex; } return(View()); }