public async Task <ActionResult> Reset(NewInStoreSaleVM model) { if (!ModelState.IsValid) { model.ProductsDropdown = new SelectList(db.Products.ToList(), "Id", "Name"); return(RedirectToAction("NewInStoreSale")); } var itemsInCart = await db.InStoreCarts .Where(c => c.InStoreSaleId == null) .OrderByDescending(c => c.ProductId) .ToListAsync(); // Revert the product quantity back var actualProducts = await(from p in db.Products join c in db.InStoreCarts on p.Id equals c.ProductId where p.Id == c.ProductId && c.InStoreSaleId == null orderby p.Id descending select p).ToListAsync(); var product = new Product(); foreach (var item in itemsInCart) { product = actualProducts.Where(p => p.Id == item.ProductId).FirstOrDefault(); product.Quantity += item.Quantity; db.Entry(product).State = EntityState.Modified; await db.SaveChangesAsync(); } db.InStoreCarts.RemoveRange(itemsInCart); await db.SaveChangesAsync(); return(RedirectToAction("NewInStoreSale")); }
public async Task <ActionResult> AddItemToInStoreCart(NewInStoreSaleVM model) { if (!ModelState.IsValid) { TempData["Error"] = "You did not select a product"; model.ProductsDropdown = new SelectList(db.Products.ToList(), "Id", "Name"); return(RedirectToAction("NewInStoreSale")); } Product product = db.Products.Find(model.ProductId); var currentCart = db.InStoreCarts.GetCurrentCartItems(); // Quantity has to be greater than 0 if (model.Quantity <= 0) { TempData["Error"] = "That is an invalid quantity amount"; return(RedirectToAction("NewInStoreSale")); } // Display error if there is no stock if (product.IsOutOfStock(model.Quantity)) { TempData["Error"] = "You don't have enough stock: " + product.Name + " (" + product.PackSize + ")"; return(RedirectToAction("NewInStoreSale")); } if (product.IsItemInCart(currentCart)) { TempData["Error"] = "That product is already in cart"; return(RedirectToAction("NewInStoreSale")); } float price = product.IsOnSale ? product.DiscountedPrice * model.Quantity : product.SellingPrice * model.Quantity; float vatTotal = Calculation.CalculateVatOnProduct(price, model.Quantity, TAX_const); db.InStoreCarts.Add(new InStoreCart { ProductId = model.ProductId, VatAmount = vatTotal, Price = price, Quantity = model.Quantity, Product = product }); // Decrease product quantity product.Quantity -= model.Quantity; db.Entry(product).State = EntityState.Modified; await db.SaveChangesAsync(); return(RedirectToAction("NewInStoreSale")); }
public void CheckCashTenderedExistsTest_CashTendered_ReturnTrue() { NewInStoreSaleVM model = new NewInStoreSaleVM(); model.CashTendered = 200; var result = model.CheckCashTenderedExists(); Assert.IsTrue(result); }
public static bool CheckCashTenderedExists(this NewInStoreSaleVM model) { if (model.CashTendered != 0) { return(true); } else { return(false); } }
public async Task <ActionResult> NewInStoreSale() { var cashRegister = db.CashRegisters.Where(c => c.CurrentDate == DateTime.Today).FirstOrDefault(); if (cashRegister == null) { return(RedirectToAction("CashRegister")); } NewInStoreSaleVM newInStoreSaleVM = new NewInStoreSaleVM(); var model = newInStoreSaleVM; var lastInStoreSale = db.InStoreSales.GetLastInStoreSale(); model.ProductsDropdown = new SelectList(db.Products.ToList(), "Id", "Name"); model.ProductList = await db.Products.ToListAsync(); model.CurrentCartItems = db.InStoreCarts.GetCurrentCartItems(); model.TotalPrice = model.CurrentCartItems.GetTotalPriceOfCurrentCart(); model.TaxTotal = model.CurrentCartItems.GetTaxOfCurrentCart(); var exists = model.CheckCashTenderedExists(); if (exists) { model.Change = lastInStoreSale.Change; } if (lastInStoreSale != null) { ViewBag.ReceiptLink = "InStoreSales/Receipt/" + lastInStoreSale.Id; } ViewBag.CashFloat = cashRegister.CashFloat.ToString("n2"); return(View(model)); }
public async Task <ActionResult> CompleteInStoreSale(NewInStoreSaleVM model) { if (!ModelState.IsValid) { model.ProductsDropdown = new SelectList(db.Products.ToList(), "Id", "Name"); return(RedirectToAction("NewInStoreSale")); } float change = 0; if (model.PaymentMethod == null) { TempData["Error"] = "Please select a payment method"; return(RedirectToAction("NewInStoreSale")); } else if (model.PaymentMethod == "Cash") { change = model.CashTendered - model.TotalPrice; } else { } if (change < 0) { TempData["Error"] = "Cash Tendered can't be less than total price"; return(RedirectToAction("NewInStoreSale")); } var cashRegister = db.CashRegisters.Where(c => c.CurrentDate == DateTime.Today).FirstOrDefault(); cashRegister.CashFloat += model.CashTendered; if (change > cashRegister.CashFloat) { TempData["Error"] = "There's not enough money in the Cash Register: R" + cashRegister.CashFloat.ToString("n2"); return(RedirectToAction("NewInStoreSale")); } cashRegister.CashFloat -= change; db.Entry(cashRegister).State = EntityState.Modified; db.InStoreSales.Add(new InStoreSale { SaleDate = DateTime.Now, PaymentMethod = model.PaymentMethod, Change = change, CashTendered = model.PaymentMethod == "Cash" ? model.CashTendered : model.TotalPrice, TotalCost = model.TotalPrice, TotalTax = model.TaxTotal, }); await db.SaveChangesAsync(); // Assign InStoreSaleId to inStoreCart items var inStoreCart = await db.InStoreCarts.Where(c => c.InStoreSaleId.Equals(null)).ToListAsync(); var lastInStoreSale = await db.InStoreSales.OrderByDescending(s => s.Id) .FirstOrDefaultAsync(); inStoreCart.ForEach(c => c.InStoreSaleId = lastInStoreSale.Id); await db.SaveChangesAsync(); TempData["SM"] = "You completed a POS!"; return(lastInStoreSale != null?RedirectToAction("Receipt", new { id = lastInStoreSale.Id }) : RedirectToAction("NewInStoreSale")); }