public async Task Create(Sales entity) { entity.Status = entity.Cpf == "15350946056" ? SalesStatus.APPROVED : SalesStatus.VALIDATING; strategy.Apply(entity); await salesRepo.Create(entity); }
public IActionResult MakeSale() { var cartItems = _cartRepository.FindAll().ToList(); double total = 0; foreach (var item in cartItems) { total += item.TotalPrice; } var sale = new Sales { SalesDate = DateTime.Now, Amount = total }; var addSale = _salesRepository.Create(sale); if (addSale) { var lastSale = _salesRepository.GetLastSale(); for (int i = 0; i < cartItems.Count; i++) { var product = _productRepository.FindById(cartItems[i].ProductId); var salesDetail = new SalesDetail { SalesId = lastSale.Id, ProductName = product.ProductName, UnitPrice = product.UnitPrice, Quantity = cartItems[i].Quantity }; product.UnitsInStock -= cartItems[i].Quantity; _productRepository.Update(product); _salesDetailRepository.Create(salesDetail); } _cartRepository.RemoveAll(); return(RedirectToAction("Sales")); } return(View("Error", "Home")); }
public ActionResult Create(SalesItemViewModel model) { // var Customers = _custumRepo.FindAll(); try { //customer making the purchase and getting the user who is sign in. // getting if the person is inside of the data base // var customer = _custumRepo.GetCustomerByID(customerIdentity.Id); //var customerIdentity = _userManager.GetUserAsync(User).Result; var Customers = _custumRepo.FindAll(); var customername = Customers.Select(q => new SelectListItem { Text = q.CustomerNAme, Value = q.CustomerId.ToString() }); var Products = _ProdRepo.FindAll(); var productItems = Products.Select(q => new SelectListItem { Text = $"{q.ProductName} - ${q.ProductPrice}", Value = q.ProductId.ToString() }); model.Customers = customername; model.ProductDetails = productItems; var product = _ProdRepo.FindById(model.ProductId); var totalcost = model.Total; if (product.Quantity > model.Quantity) { // ModelState.AddModelError("", "Please place quantity value"); // totalcost = salesonitem.ProductPrice * salesonitem.Quantity; totalcost = product.ProductPrice * model.Quantity; } else if (model.Quantity <= 0) { ModelState.AddModelError("", "please enter a value for the quantity"); return(View(model)); } model.Total = totalcost; var calculation = new SalesItemViewModel {//objects CustomerId = model.CustomerId, CustomerNAme = model.CustomerNAme, Customers = model.Customers, ProductDetails = model.ProductDetails, ProductName = model.ProductName, ProductPrice = model.ProductPrice, Quantity = model.Quantity, SalesItemId = model.SalesItemId, Total = model.Total, ProductId = model.ProductId }; var salesproduct = _mapper.Map <SalesItem> (calculation); var issuccessful = _Salesrepos.Create(salesproduct); if (!issuccessful)//if the insertion failed { ModelState.AddModelError("", "Something Went wrong submitting your record......"); return(View(model)); } return(RedirectToAction(nameof(Index))); } catch (Exception ex) { ModelState.AddModelError("", "Something Went wrong submitting your record......"); return(View(model)); } }