Exemplo n.º 1
0
        public ActionResult Index()
        {
            DateTime inicio = DateTime.Now;

            string user = User.Identity.GetUserName();

            using (InventarioEntities db = new InventarioEntities())
            {
                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 model = GroupsXml.Select(p => new GrupoViewModel
                {
                    GroupID       = p.GroupID,
                    Total         = inventario.Where(s => p.Types.SelectMany(t => t.Items).Any(u => u.ItemID == s.ItemID)).DefaultIfEmpty().Sum(v => v == null ? 0 : v.Cantidad),
                    TotalCapsulas = enCapsulas.Where(s => p.Types.SelectMany(t => t.Items).Any(u => u.ItemID == s.ItemID)).DefaultIfEmpty().Sum(v => v == null ? 0 : v.CantidadCapsula)
                }).ToList();

                double elapsed = (DateTime.Now - inicio).TotalMilliseconds;

                return(View(model));
            }
        }
Exemplo n.º 2
0
        public JsonResult GroupsTotals()
        {
            DateTime inicio = DateTime.Now;

            var enCapsules = Database.CapsulesItems
                             .Where(s => s.Capsules.UserId == Username)
                             .GroupBy(t => t.ItemId)
                             .Select(u => new { ItemId = u.Key, CapsuleQuantity = 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();

            var model = GroupsXml.Select(p => new GroupViewModel {
                GroupId       = p.GroupId,
                GroupName     = Resources.Groups.ResourceManager.GetString(p.GroupId),
                TotalQuantity = inventario.Where(s => p.Types.SelectMany(t => t.Items).Any(u => u.ItemId == s.ItemId)).DefaultIfEmpty().Sum(v => v == null ? 0 : v.Quantity),
                CapsulesTotal = enCapsules.Where(s => p.Types.SelectMany(t => t.Items).Any(u => u.ItemId == s.ItemId)).DefaultIfEmpty().Sum(v => v == null ? 0 : v.CapsuleQuantity)
            }).ToList();

            model.Add(new GroupViewModel {
                GroupId       = GroupIdAll,
                GroupName     = Resources.Groups.ResourceManager.GetString(GroupIdAll),
                TotalQuantity = model.Sum(p => p.TotalQuantity),
                CapsulesTotal = model.Sum(p => p.CapsulesTotal)
            });

            double elapsed = (DateTime.Now - inicio).TotalMilliseconds;

            return(Json(model, JsonRequestBehavior.AllowGet));
        }
Exemplo n.º 3
0
        public ActionResult Manage(string id, bool?editing)
        {
            Dictionary <string, string> groups = GroupsXml.Select(p => new { GroupId = p.GroupId, Name = Resources.Groups.ResourceManager.GetString(p.GroupId) }).ToDictionary(p => p.GroupId, q => q.Name);

            groups.Add(GroupIdAll, Resources.Groups.ResourceManager.GetString(GroupIdAll));

            var model = new ManageViewModel {
                Groups = groups
            };

            if (!string.IsNullOrEmpty(id))
            {
                if (groups.ContainsKey(id))
                {
                    model.GroupId = id;
                    model.Editing = editing.GetValueOrDefault();
                }
                else
                {
                    return(new HttpStatusCodeResult(System.Net.HttpStatusCode.NotFound, "GroupId does not exists"));
                }
            }
            else
            {
                model.GroupId = GroupsXml.First().GroupId;
                model.Editing = false;
            }

            return(View(model));
        }
Exemplo n.º 4
0
        public ActionResult Difference()
        {
            using (InventarioEntities db = new InventarioEntities())
            {
                string user = User.Identity.GetUserName();
                if (user != "*****@*****.**" && user != "*****@*****.**")
                {
                    return(new HttpNotFoundResult());
                }

                DifferenceViewModel model = new DifferenceViewModel();
                string usuarioA           = user;
                string usuarioB           = (user == "*****@*****.**" ? "*****@*****.**" : "*****@*****.**");

                model.UsuarioA = usuarioA;
                model.UsuarioB = usuarioB;

                var inventarioA = db.Inventarios
                                  .Where(s => s.IdUsuario == usuarioA)
                                  .Select(u => new ItemBasico {
                    ItemID = u.ItemID, Cantidad = u.Cantidad
                }).ToList();
                var inventarioB = db.Inventarios
                                  .Where(s => s.IdUsuario == usuarioB)
                                  .Select(u => new ItemBasico {
                    ItemID = u.ItemID, Cantidad = u.Cantidad
                }).ToList();

                var capsulasA = db.CapsulasItems
                                .Where(s => s.Capsulas.IdUsuario == usuarioA)
                                .GroupBy(t => t.ItemID)
                                .Select(u => new ItemBasico {
                    ItemID = u.Key, Cantidad = u.Sum(v => v.Cantidad)
                });
                var capsulasB = db.CapsulasItems
                                .Where(s => s.Capsulas.IdUsuario == usuarioB)
                                .GroupBy(t => t.ItemID)
                                .Select(u => new ItemBasico {
                    ItemID = u.Key, Cantidad = u.Sum(v => v.Cantidad)
                });

                foreach (var itemC in capsulasA)
                {
                    var itemI = inventarioA.SingleOrDefault(f => f.ItemID == itemC.ItemID);
                    if (itemI == null)
                    {
                        inventarioA.Add(new ItemBasico {
                            ItemID = itemC.ItemID, Cantidad = itemC.Cantidad
                        });
                    }
                    else
                    {
                        itemI.Cantidad += itemC.Cantidad;
                    }
                }

                foreach (var itemC in capsulasB)
                {
                    var itemI = inventarioB.SingleOrDefault(f => f.ItemID == itemC.ItemID);
                    if (itemI == null)
                    {
                        inventarioB.Add(new ItemBasico {
                            ItemID = itemC.ItemID, Cantidad = itemC.Cantidad
                        });
                    }
                    else
                    {
                        itemI.Cantidad += itemC.Cantidad;
                    }
                }
                model.Grupos = GroupsXml
                               .Select(p => new GrupoDifferenceViewModel
                {
                    GroupID = p.GroupID,
                    Tipos   = p.Types.Select(q => new TipoDifferenceViewModel
                    {
                        TypeID = q.TypeID,
                        Items  = q.Items.Select(r => new ItemDifferenceViewModel
                        {
                            CurrentItem      = r,
                            CantidadUsuarioA = inventarioA.SingleOrDefault(s => s.ItemID == r.ItemID) == null ? 0 : inventarioA.Single(s => s.ItemID == r.ItemID).Cantidad,
                            CantidadUsuarioB = inventarioB.SingleOrDefault(s => s.ItemID == r.ItemID) == null ? 0 : inventarioB.Single(s => s.ItemID == r.ItemID).Cantidad
                        })
                    })
                }).ToList();

                return(View(model));
            }
        }
Exemplo n.º 5
0
        public ActionResult DifferenceData()
        {
            if (Username != "*****@*****.**" && Username != "*****@*****.**")
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest, "Invalid user."));
            }

            DifferenceViewModel model = new DifferenceViewModel();
            string userIdA            = Username;
            string userIdB            = (Username == "*****@*****.**" ? "*****@*****.**" : "*****@*****.**");

            model.OrigUserId = MyInventory.Utils.GetUsernamFromEmail(userIdA);
            model.DestUserId = MyInventory.Utils.GetUsernamFromEmail(userIdB);

            var inventoryA = Database.InventoriesItems
                             .Where(s => s.Inventories.UserId == userIdA)
                             .Select(u => new ItemQuantity {
                ItemId = u.ItemId, Quantity = u.Quantity
            }).ToList();
            var inventoryB = Database.InventoriesItems
                             .Where(s => s.Inventories.UserId == userIdB)
                             .Select(u => new ItemQuantity {
                ItemId = u.ItemId, Quantity = u.Quantity
            }).ToList();

            var capsulesA = Database.CapsulesItems
                            .Where(s => s.Capsules.UserId == userIdA)
                            .GroupBy(t => t.ItemId)
                            .Select(u => new ItemQuantity {
                ItemId = u.Key, Quantity = u.Sum(v => v.Quantity)
            });
            var capsulesB = Database.CapsulesItems
                            .Where(s => s.Capsules.UserId == userIdB)
                            .GroupBy(t => t.ItemId)
                            .Select(u => new ItemQuantity {
                ItemId = u.Key, Quantity = u.Sum(v => v.Quantity)
            });

            foreach (var itemC in capsulesA)
            {
                var itemI = inventoryA.SingleOrDefault(f => f.ItemId == itemC.ItemId);
                if (itemI == null)
                {
                    inventoryA.Add(new ItemQuantity {
                        ItemId = itemC.ItemId, Quantity = itemC.Quantity
                    });
                }
                else
                {
                    itemI.Quantity += itemC.Quantity;
                }
            }

            foreach (var itemC in capsulesB)
            {
                var itemI = inventoryB.SingleOrDefault(f => f.ItemId == itemC.ItemId);
                if (itemI == null)
                {
                    inventoryB.Add(new ItemQuantity {
                        ItemId = itemC.ItemId, Quantity = itemC.Quantity
                    });
                }
                else
                {
                    itemI.Quantity += itemC.Quantity;
                }
            }

            model.Groups = GroupsXml
                           .Select(p => new GroupDifferenceViewModel {
                GroupId = p.GroupId,
                Types   = p.Types.Select(q => new TypeDifferenceViewModel {
                    TypeId = q.TypeId,
                    Items  = q.Items.Select(r => new ItemDifferenceViewModel {
                        CurrentItem  = ItemBase.Create(r),
                        OrigQuantity = inventoryA.SingleOrDefault(s => s.ItemId == r.ItemId) == null ? 0 : inventoryA.Single(s => s.ItemId == r.ItemId).Quantity,
                        DestQuantity = inventoryB.SingleOrDefault(s => s.ItemId == r.ItemId) == null ? 0 : inventoryB.Single(s => s.ItemId == r.ItemId).Quantity
                    }).ToList()
                }).ToList()
            }).ToList();

            model.Groups.RemoveAll(p => p.OrigDifference == 0 && p.DestDifference == 0);
            foreach (var group in model.Groups)
            {
                group.Types.RemoveAll(p => p.OrigDifference == 0 && p.DestDifference == 0);
                foreach (var type in group.Types)
                {
                    type.Items.RemoveAll(p => p.OrigDifference == 0 && p.DestDifference == 0);
                }
            }

            return(Json(model, JsonRequestBehavior.AllowGet));
        }