public ActionResult Save(Purchase purchase) { if (!ModelState.IsValid) { var viewModel = new PurchaseFormViewModel { Clients = _context.Clients.ToList(), Games = _context.Games.ToList(), PaymentMethods = _context.PaymentMethods.ToList() }; return(View("PurchaseForm", viewModel)); } if (purchase.PurchaseID == 0) { purchase.DateOfPurchase = DateTime.Now; _context.Purchases.Add(purchase); } else { var purchaseInDb = _context.Purchases.Single(c => c.PurchaseID == purchase.PurchaseID); purchaseInDb.ClientID = purchase.ClientID; purchaseInDb.GameID = purchase.GameID; purchaseInDb.PaymentMethodID = purchase.PaymentMethodID; } _context.SaveChanges(); return(RedirectToAction("Index", "Purchases")); }
// GET: Purchases/Create public IActionResult Create() { var purchaseVM = new PurchaseFormViewModel { Branches = _context.Branches.ToList(), Customers = _context.Customers.ToList(), Products = _context.Products.ToList() }; return(View(purchaseVM)); }
public ViewResult New() { var paymentMethods = _context.PaymentMethods.ToList(); var clients = _context.Clients.ToList(); var games = _context.Games.ToList(); var viewModel = new PurchaseFormViewModel { Clients = clients, Games = games, PaymentMethods = paymentMethods }; return(View("PurchaseForm", viewModel)); }
public ActionResult Edit(int id) { var purchase = _context.Purchases.SingleOrDefault(c => c.PurchaseID == id); if (purchase == null) { return(HttpNotFound()); } var viewModel = new PurchaseFormViewModel(purchase) { Clients = _context.Clients.ToList(), Games = _context.Games.ToList(), PaymentMethods = _context.PaymentMethods.ToList() }; return(View("PurchaseForm", viewModel)); }
// [ValidateAntiForgeryToken] public async Task <IActionResult> Create([FromBody] PurchaseFormViewModel purchaseVM) { if (ModelState.IsValid) { var branch = await _context.Branches .SingleOrDefaultAsync(m => m.Id == purchaseVM.BranchId); purchaseVM.EntityId = branch.EntityId; string timestamp = DateTime.Now.Ticks.ToString(); Random randonNum = new Random(); purchaseVM.Identification = timestamp + randonNum.Next(1, 9999999); purchaseVM.RegistrationDate = DateTime.Now; var purchase = _mapper.Map <Purchase>(purchaseVM); foreach (var purchaseitem in purchase.PurchaseItems) { purchase.Discount += purchaseitem.DiscountTotal; purchase.Total += purchaseitem.Total - purchaseitem.DiscountTotal; _context.Add(purchaseitem); var product = await _context.Products .SingleOrDefaultAsync(m => m.Id == purchaseitem.ProductId); product.QuantityInStock -= purchaseitem.ProductQuantity; _context.Entry(product).Property("QuantityInStock").IsModified = true; } _context.Add(purchase); await _context.SaveChangesAsync(); return(Json(null)); } purchaseVM.Branches = _context.Branches.ToList(); purchaseVM.Customers = _context.Customers.ToList(); purchaseVM.Products = _context.Products.ToList(); return(View(purchaseVM)); }
public async Task <IActionResult> Edit(int id, PurchaseFormViewModel purchaseVM) { if (id != purchaseVM.Id) { return(BadRequest()); } if (ModelState.IsValid) { try { var branch = await _context.Branches .SingleOrDefaultAsync(m => m.Id == purchaseVM.BranchId); purchaseVM.EntityId = branch.EntityId; var purchase = _mapper.Map <Purchase>(purchaseVM); _context.Entry(purchase).Property("BranchId").IsModified = true; _context.Entry(purchase).Property("CustomerId").IsModified = true; await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } catch (DbUpdateConcurrencyException) { if (!await BranchExists(purchaseVM.Id)) { return(NotFound()); } else { throw; } } } purchaseVM.Branches = _context.Branches.ToList(); purchaseVM.Customers = _context.Customers.ToList(); return(View(purchaseVM)); }