private List <GrupoViewModel> RecuperacionGrupos(string id) { if (!string.IsNullOrWhiteSpace(id) && !GroupsXml.Any(p => p.GroupID == id)) { return(null); } DateTime inicio = DateTime.Now; using (InventarioEntities db = new InventarioEntities()) { string user = User.Identity.GetUserName(); var enCapsulas = db.CapsulasItems .Where(s => s.Capsulas.IdUsuario == user) .GroupBy(t => t.ItemID) .Select(u => new { ItemID = u.Key, CantidadCapsula = u.Sum(v => v.Cantidad) }).ToList(); var inventario = db.Inventarios .Where(s => s.IdUsuario == user) .Select(u => new { ItemID = u.ItemID, Cantidad = u.Cantidad }).ToList(); var grupos = GroupsXml.Where(z => string.IsNullOrWhiteSpace(id) || z.GroupID == id) .Select(p => new GrupoViewModel { GroupID = p.GroupID, Tipos = p.Types.Select(q => new TipoViewModel { TypeID = q.TypeID, Items = q.Items.Select(r => new ItemInventoryViewModel { CurrentItem = r, Cantidad = inventario.SingleOrDefault(s => s.ItemID == r.ItemID) == null ? 0 : inventario.Single(s => s.ItemID == r.ItemID).Cantidad, CantidadCapsulas = enCapsulas.SingleOrDefault(s => s.ItemID == r.ItemID) == null ? 0 : enCapsulas.Single(s => s.ItemID == r.ItemID).CantidadCapsula }) }) }); double elapsed = (DateTime.Now - inicio).TotalMilliseconds; var lista = new List <GrupoViewModel>(); if (!string.IsNullOrWhiteSpace(id)) { var model = grupos.Single(); model.Total = model.Tipos.SelectMany(q => q.Items).Sum(r => r.Cantidad); model.TotalCapsulas = model.Tipos.SelectMany(q => q.Items).Sum(r => r.CantidadCapsulas); lista.Add(model); } else { lista = grupos.ToList(); } return(lista); } }
private ActionResult GetItems(string groupId, bool ignoreZeroQuantity) { bool allGroups = groupId == GroupIdAll; if (!allGroups && !GroupsXml.Any(p => p.GroupId == groupId)) { return(new HttpStatusCodeResult(System.Net.HttpStatusCode.BadRequest, "GroupId does not exists")); } DateTime inicio = DateTime.Now; var enCapsules = Database.CapsulesItems .Where(s => s.Capsules.UserId == Username) .GroupBy(t => t.ItemId) .Select(u => new { ItemId = u.Key, CapsulesQuantity = u.Sum(v => v.Quantity) }).ToList(); var inventario = Database.InventoriesItems .Where(s => s.Inventories.UserId == Username) .Select(u => new { ItemId = u.ItemId, Quantity = u.Quantity }).ToList(); double elapsed = (DateTime.Now - inicio).TotalMilliseconds; var itemsGroups = ItemsXml .Where(z => (allGroups || z.GroupId == groupId) && (ignoreZeroQuantity || (inventario.Any(y => y.ItemId == z.ItemId) || enCapsules.Any(y => y.ItemId == z.ItemId)))) .Select(y => new { GroupId = y.GroupId, Item = ItemBase.Create(y), Quantity = inventario.SingleOrDefault(s => s.ItemId == y.ItemId) == null ? 0 : inventario.Single(s => s.ItemId == y.ItemId).Quantity, CapsulesQuantity = enCapsules.SingleOrDefault(s => s.ItemId == y.ItemId) == null ? 0 : enCapsules.Single(s => s.ItemId == y.ItemId).CapsulesQuantity }) .ToList(); if (itemsGroups.Count == 0) { return(Json(new { Result = false }, JsonRequestBehavior.DenyGet)); } var groups = itemsGroups .GroupBy(d => d.GroupId) .Select(p => new { GroupId = p.Key, Types = p.GroupBy(e => e.Item.TypeId) .Select(q => new { TypeId = q.Key, Quantity = q.Sum(f => f.Quantity), CapsulesQuantity = q.Sum(f => f.CapsulesQuantity), Items = q.Select(r => new { CurrentItem = r.Item, Quantity = r.Quantity, CapsulesQuantity = r.CapsulesQuantity }) }) }); return(Json(new { Result = true, Groups = groups }, JsonRequestBehavior.DenyGet)); }
public ActionResult Edit(string id) { if (string.IsNullOrWhiteSpace(id)) { return(new HttpNotFoundResult()); } if (!GroupsXml.Any(p => p.GroupID == id)) { return(new HttpNotFoundResult()); } using (InventarioEntities db = new InventarioEntities()) { string user = User.Identity.GetUserName(); var enCapsulas = db.CapsulasItems .Where(s => s.Capsulas.IdUsuario == user) .GroupBy(t => t.ItemID) .Select(u => new { ItemID = u.Key, CantidadCapsula = u.Sum(v => v.Cantidad) }).ToList(); var inventario = db.Inventarios .Where(s => s.IdUsuario == user) .Select(u => new { ItemID = u.ItemID, Cantidad = u.Cantidad }).ToList(); var grupo = GroupsXml.Where(z => z.GroupID == id) .Select(p => new { GroupID = p.GroupID, Items = p.Types.SelectMany(z => z.Items).Select(r => new ItemEditViewModel { CurrentItem = r, Cantidad = inventario.SingleOrDefault(s => s.ItemID == r.ItemID) == null ? 0 : inventario.Single(s => s.ItemID == r.ItemID).Cantidad, CantidadCapsulas = enCapsulas.SingleOrDefault(s => s.ItemID == r.ItemID) == null ? 0 : enCapsulas.Single(s => s.ItemID == r.ItemID).CantidadCapsula }) }).Single(); GrupoEditViewModel model = new GrupoEditViewModel(); model.GroupID = grupo.GroupID; model.Items = grupo.Items.ToList(); model.Total = model.Items.Sum(r => r.Cantidad); model.TotalCapsulas = model.Items.Sum(r => r.CantidadCapsulas); return(View(model)); } }