public async Task <ActionResult> CreateBusiness() { TransferBusinessModel model = new TransferBusinessModel(); Guid business_working = Guid.Parse(Session["BusinessWorking"].ToString()); ViewBag.Categories = await applicationDbContext.Categories.Where(x => x.Business_Id == business_working).OrderBy(x => x.Name).ToListAsync(); string userId = User.Identity.GetUserId(); ViewBag.To = await applicationDbContext.Businesses.Include("BusinessUsers").Where(x => x.Id != business_working && x.BusinessUsers.FirstOrDefault(y => y.User_Id == userId) != null).OrderBy(x => x.Name).ToListAsync(); return(View(model)); }
public async Task <ActionResult> CreateBusiness(TransferBusinessModel model) { string userId = User.Identity.GetUserId(); if (ModelState.IsValid) { Guid productId = Guid.Parse(model.ProductName); Product product = await applicationDbContext.Products.Include("Category").FirstOrDefaultAsync(x => x.Id == productId); Guid business_to = Guid.Parse(model.FromTo); Category category = await applicationDbContext.Categories.FirstOrDefaultAsync(x => x.Business_Id == business_to && x.Name.Equals(product.Category.Name)); if (category == null) { category = new Category() { Id = Guid.NewGuid(), Business_Id = business_to, Name = product.Category.Name, ActionIn = product.Category.ActionIn, ActionOut = product.Category.ActionOut, LastUpdated = DateTime.Now, ShowDashboard = product.Category.ShowDashboard, SystemAction = product.Category.SystemAction }; applicationDbContext.Categories.Add(category); } Product product_to = await applicationDbContext.Products.FirstOrDefaultAsync(x => x.Name.Equals(product.Name) && x.Category_Id == category.Id); if (product_to == null) { product_to = new Product() { Id = Guid.NewGuid(), Category_Id = category.Id, Business_Id = business_to, Name = product.Name, CurrentPrice = product.CurrentPrice, Description = "", Stock = model.Quantity, SalePrice = product.SalePrice, User_Id = userId, LastUpdated = DateTime.Now, isAccesory = product.isAccesory }; applicationDbContext.Products.Add(product_to); } else { product_to.Stock += model.Quantity; product_to.CurrentPrice = ((product_to.CurrentPrice * product_to.Stock) + (product.CurrentPrice * model.Quantity)) / product_to.Stock + model.Quantity; product_to.LastUpdated = DateTime.Now; product.Stock -= model.Quantity; product.LastUpdated = DateTime.Now; applicationDbContext.Entry(product).State = System.Data.Entity.EntityState.Modified; applicationDbContext.Entry(product_to).State = System.Data.Entity.EntityState.Modified; } TransferBusinessProduct transfer = new TransferBusinessProduct() { Id = Guid.NewGuid(), Date = DateTime.Now, ProductFrom_Id = product.Id, ProductTo_Id = product_to.Id, Quantity = model.Quantity, User_Id = userId }; applicationDbContext.TransferBusinessProducts.Add(transfer); await applicationDbContext.SaveChangesAsync(); return(RedirectToAction("Index", new { selectedTab = "nav-business" })); } Guid business_working = Guid.Parse(Session["BusinessWorking"].ToString()); ViewBag.Categories = await applicationDbContext.Categories.Where(x => x.Business_Id == business_working).OrderBy(x => x.Name).ToListAsync(); ViewBag.To = await applicationDbContext.Businesses.Include("BusinessUsers").Where(x => x.Id != business_working && x.BusinessUsers.FirstOrDefault(y => y.User_Id == userId) != null).OrderBy(x => x.Name).ToListAsync(); return(View(model)); }