public async Task <ActionResult> AllReceiver() { Guid business_working = Guid.Parse(Session["BusinessWorking"].ToString()); List <StockOut> stockOuts = await applicationDbContext.StockOuts.Include("Product").Include("Product.Category").Where(x => x.Receiver_Id == null).ToListAsync(); foreach (var stockOut in stockOuts) { stockOut.Receiver_Id = User.Identity.GetUserId(); applicationDbContext.Entry(stockOut).State = System.Data.Entity.EntityState.Modified; BusinessUser businessUser = await applicationDbContext.BusinessUsers.FirstOrDefaultAsync(x => x.User_Id == stockOut.Receiver_Id && x.Business_Id == business_working); if (stockOut.Product.Category.ActionOut == ActionConstants.Sum) { businessUser.Cash += stockOut.SalePrice * stockOut.Quantity; } else if (stockOut.Product.Category.ActionOut == ActionConstants.Rest) { businessUser.Cash -= stockOut.SalePrice * stockOut.Quantity; } applicationDbContext.Entry(businessUser).State = System.Data.Entity.EntityState.Modified; await ActivityPublisher.Publish(User.Identity.GetUserId(), ActivityTypeConstants.Stock_Out_Receive, stockOut.Id, stockOut.Product.Name, business_working); } await applicationDbContext.SaveChangesAsync(); return(Json(true)); }
public async Task <ActionResult> Create(ProductModel model) { Guid business_working = Guid.Parse(Session["BusinessWorking"].ToString()); if (ModelState.IsValid) { Product product = new Product() { Id = Guid.NewGuid(), Name = model.Product, Category_Id = Guid.Parse(model.Category), Description = "", Stock = model.Stock, User_Id = User.Identity.GetUserId(), CurrentPrice = decimal.Parse(model.CurrentPrice.Replace(".", ",")), LastUpdated = DateTime.Now, NoCountOut = model.NoCountOut, Business_Id = business_working, SalePrice = decimal.Parse(model.SalePrice.Replace(".", ",")), isAccesory = model.isAccesory }; applicationDbContext.Products.Add(product); await applicationDbContext.SaveChangesAsync(); await ActivityPublisher.Publish(User.Identity.GetUserId(), ActivityTypeConstants.Stock_Create, product.Id, model.Product, business_working); return(RedirectToAction("Index")); } ViewBag.Categories = await applicationDbContext.Categories.Where(x => x.Business_Id == business_working).OrderBy(x => x.Name).ToListAsync(); return(View(model)); }
public async Task <ActionResult> Edit(string id, ProductModel model) { Guid business_working = Guid.Parse(Session["BusinessWorking"].ToString()); if (ModelState.IsValid) { Guid prod_id = Guid.Parse(id); Product product = await applicationDbContext.Products.FirstOrDefaultAsync(x => x.Id == prod_id); product.Name = model.Product; product.Category_Id = Guid.Parse(model.Category); product.Stock = model.Stock; product.CurrentPrice = decimal.Parse(model.CurrentPrice.Replace(".", ",")); product.SalePrice = decimal.Parse(model.SalePrice.Replace(".", ",")); product.LastUpdated = DateTime.Now; product.NoCountOut = model.NoCountOut; product.isAccesory = model.isAccesory; applicationDbContext.Entry(product).State = System.Data.Entity.EntityState.Modified; await applicationDbContext.SaveChangesAsync(); await ActivityPublisher.Publish(User.Identity.GetUserId(), ActivityTypeConstants.Stock_Edit, product.Id, model.Product, business_working); return(RedirectToAction("Index")); } ViewBag.Categories = await applicationDbContext.Categories.Where(x => x.Business_Id == business_working).OrderBy(x => x.Name).ToListAsync(); return(View(model)); }
public async Task <ActionResult> Delete(string id) { Guid out_id = Guid.Parse(id); Guid business_working = Guid.Parse(Session["BusinessWorking"].ToString()); StockOut stockOut = await applicationDbContext.StockOuts.Include("Product").FirstOrDefaultAsync(x => x.Id == out_id); if (stockOut != null) { if (stockOut.Receiver_Id != null) { if (!stockOut.Product.NoCountOut) { BusinessUser businessUser = await applicationDbContext.BusinessUsers.FirstOrDefaultAsync(x => x.User_Id == stockOut.Receiver_Id && x.Business_Id == business_working); /*Operacion inversa para revertir la situación*/ if (stockOut.Product.Category.ActionOut == ActionConstants.Sum) { businessUser.Cash -= stockOut.SalePrice * stockOut.Quantity; } else if (stockOut.Product.Category.ActionOut == ActionConstants.Rest) { businessUser.Cash += stockOut.SalePrice * stockOut.Quantity; } applicationDbContext.Entry(businessUser).State = System.Data.Entity.EntityState.Modified; } } stockOut.Product.Stock += stockOut.Quantity; stockOut.Product.LastUpdated = DateTime.Now; applicationDbContext.Entry(stockOut.Product).State = System.Data.Entity.EntityState.Modified; applicationDbContext.Entry(stockOut).State = System.Data.Entity.EntityState.Deleted; await ActivityPublisher.Publish(User.Identity.GetUserId(), ActivityTypeConstants.Stock_Out_Remove, stockOut.Id, stockOut.Product.Name, business_working); await applicationDbContext.SaveChangesAsync(); } ViewBag.Msg = "La salida ha sido eliminada correctamente"; return(RedirectToAction("Index")); }
public async Task <ActionResult> Create(StockOutModel model) { Guid business_working = Guid.Parse(Session["BusinessWorking"].ToString()); if (ModelState.IsValid) { Guid productId = Guid.Parse(model.ProductName); Product product = await applicationDbContext.Products.Include("Category").FirstOrDefaultAsync(x => x.Id == productId); StockOut stockOut = new StockOut() { Id = Guid.NewGuid(), Product_Id = productId, Date = DateTime.Now, Quantity = model.Quantity, SalePrice = decimal.Parse(model.SalePrice.Replace(".", ",")), User_Id = User.Identity.GetUserId(), Gain = decimal.Parse(model.SalePrice.Replace(".", ",")) - product.CurrentPrice, Description = model.Description }; if (RoleManager.IsInRole(RoleManager.Editor) || RoleManager.IsInRole(RoleManager.Administrator)) { stockOut.Receiver_Id = User.Identity.GetUserId(); BusinessUser businessUser = await applicationDbContext.BusinessUsers.FirstOrDefaultAsync(x => x.User_Id == stockOut.Receiver_Id && x.Business_Id == business_working); if (product.Category.ActionOut == ActionConstants.Sum) { businessUser.Cash += stockOut.SalePrice * stockOut.Quantity; } else if (product.Category.ActionOut == ActionConstants.Rest) { businessUser.Cash -= stockOut.SalePrice * stockOut.Quantity; } applicationDbContext.Entry(businessUser).State = System.Data.Entity.EntityState.Modified; } applicationDbContext.StockOuts.Add(stockOut); if (!product.NoCountOut) { product.Stock -= stockOut.Quantity; product.LastUpdated = DateTime.Now; applicationDbContext.Entry(product).State = System.Data.Entity.EntityState.Modified; } if (model.AccesoriesIds != null) { foreach (var acc in model.AccesoriesIds) { Guid accesory = Guid.Parse(acc); Product acc_product = await applicationDbContext.Products.Include("Category").FirstOrDefaultAsync(x => x.Id == accesory); StockOut acc_stockOut = new StockOut() { Id = Guid.NewGuid(), Product_Id = accesory, Date = DateTime.Now, Quantity = 1, SalePrice = 0, User_Id = User.Identity.GetUserId(), Gain = 0 - acc_product.CurrentPrice, Description = $"Con producto {product.Name}" }; applicationDbContext.StockOuts.Add(acc_stockOut); if (!acc_product.NoCountOut) { acc_product.Stock -= acc_stockOut.Quantity; acc_product.LastUpdated = DateTime.Now; applicationDbContext.Entry(acc_product).State = System.Data.Entity.EntityState.Modified; } } } await applicationDbContext.SaveChangesAsync(); await ActivityPublisher.Publish(User.Identity.GetUserId(), ActivityTypeConstants.Stock_Out_Create, product.Id, product.Name, business_working); return(RedirectToAction("Index")); } ViewBag.Categories = await applicationDbContext.Categories.Where(x => x.Business_Id == business_working).OrderBy(x => x.Name).ToListAsync(); return(View(model)); }
public async Task <ActionResult> Create(StockInModel model) { Guid business_working = Guid.Parse(Session["BusinessWorking"].ToString()); if (ModelState.IsValid) { Guid productId = Guid.Parse(model.ProductName); StockIn stockIn = new StockIn() { Id = Guid.NewGuid(), Product_Id = productId, Date = DateTime.Now, Quantity = model.Quantity, ShopPrice = decimal.Parse(model.ShopPrice.Replace(".", ",")), User_Id = User.Identity.GetUserId(), Description = model.Description }; if (Guid.Parse(model.Provider) != Guid.Empty) { stockIn.Provider_Id = Guid.Parse(model.Provider); } applicationDbContext.StockIns.Add(stockIn); Product product = await applicationDbContext.Products.Include("Category").FirstOrDefaultAsync(x => x.Id == productId); product.CurrentPrice = ((product.CurrentPrice * product.Stock) + (stockIn.ShopPrice * stockIn.Quantity)) / (product.Stock + stockIn.Quantity); product.Stock += stockIn.Quantity; product.LastUpdated = DateTime.Now; if (RoleManager.IsInRole(RoleManager.Editor) || RoleManager.IsInRole(RoleManager.Administrator)) { BusinessUser businessUser = await applicationDbContext.BusinessUsers.FirstOrDefaultAsync(x => x.User_Id == stockIn.User_Id && x.Business_Id == business_working); if (product.Category.ActionIn == ActionConstants.Sum) { businessUser.Cash += stockIn.ShopPrice * stockIn.Quantity; } else if (product.Category.ActionIn == ActionConstants.Rest) { businessUser.Cash -= stockIn.ShopPrice * stockIn.Quantity; } applicationDbContext.Entry(businessUser).State = System.Data.Entity.EntityState.Modified; } applicationDbContext.Entry(product).State = System.Data.Entity.EntityState.Modified; await applicationDbContext.SaveChangesAsync(); await ActivityPublisher.Publish(User.Identity.GetUserId(), ActivityTypeConstants.Stock_In_Create, stockIn.Id, product.Name, business_working); return(RedirectToAction("Index")); } ViewBag.Categories = await applicationDbContext.Categories.Where(x => x.Business_Id == business_working).OrderBy(x => x.Name).ToListAsync(); ViewBag.Providers = await applicationDbContext.Providers.Where(x => x.Business_Id == business_working).OrderBy(x => x.Name).ToListAsync(); return(View(model)); }