public async Task CheckOut() { var sale = new SaleModel(); foreach (var item in Cart) { sale.SaleDetails.Add(new SaleDetailModel() { ProductId = item.Product.Id, Quantity = item.QuantityInCart, }); } await saleEndPoint.PostSale(sale); }
public async Task CheckOut() { //Create a SaleModel and post it to the API SaleModel sale = new SaleModel(); foreach (var item in Cart) { sale.SaleDeatils.Add(new SaleDetailModel { ProductId = item.Product.Id, Quantity = item.QuantityInCart }); } await _saleEndPoint.PostSale(sale); }
public async Task CheckOut() { //Create a sale model & post to the api SaleModel sale = new SaleModel(); foreach (var item in Cart) { sale.SaleDetails.Add(new SaleDetailModel { ProductId = item.Product.Id, Quantity = item.QuantityInCart }); } await _saleEndPoint.PostSale(sale); await ResetSalesViewModel(); }
public async Task CheckOut() { SaleModel sale = new SaleModel(); foreach (var item in Cart) { sale.SaleDetails.Add(new SaleDetilModel { ProductId = item.Product.Id, Quantity = item.QuantityInCart }); } await _saleEndpoint.PostSale(sale); await ResetSaleViewModel(); }
public async Task <ActionResult> Sales(ProductsViewModel model) { var productList = await GetAllProducts(); StringBuilder productsAddedToCartString = new StringBuilder(model.ProductsAddedToCartString); List <SelectListItem> listAvailableItems = new List <SelectListItem>(); List <SelectListItem> listCartItems = new List <SelectListItem>(); decimal subTotal = model.SubTotal; decimal taxRate = _configHelper.GetTaxRate() / 100; decimal tax = model.Tax; decimal total = model.Total; List <SaleDetailModel> saleDetails = new List <SaleDetailModel>();; foreach (ProductModel product in productList) { SelectListItem availableList = new SelectListItem() { Text = product.ProductName, Value = product.Id.ToString() }; listAvailableItems.Add(availableList); } if (model.SelectedAvailableProducts != null) { foreach (var selectedAvailableProduct in model.SelectedAvailableProducts) { int Id = Convert.ToInt32(selectedAvailableProduct); ProductModel selectedProduct = productList.FirstOrDefault(p => p.Id == Id); selectedProduct.QuantityInStock -= model.Quantity; productsAddedToCartString.Append($"{selectedAvailableProduct}:{selectedProduct.ProductName}:{model.Quantity};"); subTotal += selectedProduct.RetailPrice * model.Quantity; if (selectedProduct.IsTaxable) { tax += selectedProduct.RetailPrice * model.Quantity * taxRate; } } } if (model.SelectedProductsAddedToCart != null) { foreach (var selectedProductToBeRemoved in model.SelectedProductsAddedToCart) { ProductModel selectedProduct = productList.FirstOrDefault(p => p.Id == Convert.ToInt32(selectedProductToBeRemoved)); selectedProduct.QuantityInStock += model.Quantity; productsAddedToCartString.Replace($"{selectedProductToBeRemoved}:{selectedProduct.ProductName}:{model.Quantity};", string.Empty); subTotal -= selectedProduct.RetailPrice * model.Quantity; if (selectedProduct.IsTaxable) { tax -= selectedProduct.RetailPrice * model.Quantity * taxRate; } } } total = subTotal + tax; string[] productsAddedToCartStringArray = productsAddedToCartString.ToString().Split(';'); foreach (string productsAddedToCartStringItem in productsAddedToCartStringArray) { if (!productsAddedToCartStringItem.IsNullOrWhiteSpace()) { string[] itemInfo = productsAddedToCartStringItem.Split(':'); int cartItemId = Convert.ToInt32(itemInfo[0]); int cartItemQuantity = Convert.ToInt32(itemInfo[2]); SelectListItem cartList = new SelectListItem() { Text = itemInfo[1], Value = itemInfo[0] }; listCartItems.Add(cartList); SaleDetailModel saleDetailModel = new SaleDetailModel(); saleDetailModel.ProductId = cartItemId; saleDetailModel.Quantity = cartItemQuantity; saleDetails.Add(saleDetailModel); } } ProductsViewModel productsViewModel = new ProductsViewModel() { AvailableProducts = listAvailableItems, ProductsAddedToCart = listCartItems, ProductsAddedToCartString = productsAddedToCartString.ToString(), Quantity = model.Quantity, SubTotal = subTotal, Tax = tax, Total = total }; if (null == model.SelectedAvailableProducts && null == model.SelectedProductsAddedToCart) { SaleModel sale = new SaleModel(); sale.SaleDetails = saleDetails; await _saleEndPoint.PostSale(sale); ProductsViewModel resetProductsViewModel = new ProductsViewModel() { AvailableProducts = listAvailableItems, ProductsAddedToCart = new List <SelectListItem>(), ProductsAddedToCartString = "", Quantity = 1, SubTotal = 0, Tax = 0, Total = 0 }; return(RedirectToAction("Sales", new { serializedModel = JsonConvert.SerializeObject(resetProductsViewModel) })); } return(RedirectToAction("Sales", new { serializedModel = JsonConvert.SerializeObject(productsViewModel) })); }