public ActionResult ChangeShippingCosts() { ShippingCosts currentShipCosts = _context.ShippingCosts.FirstOrDefault(); //return View(); return(View(currentShipCosts)); }
public IHttpActionResult Get(string zone, decimal weight) { ShippingCost rate = ShippingCosts.GetOne(zone, weight); if (rate == null) { return(NotFound()); } return(Ok(rate)); }
public ActionResult ChangeShippingCosts(int ShipCostId, string FirstBookShipCost, string AddBookShipCost) { if (FirstBookShipCost == null || AddBookShipCost == null)//if the manager doesn't enter a value for both { ViewBag.InvalidCost = "You must enter values for both shipping costs"; return(View()); } else //check if text entered is a decimal { Decimal decFirstBookShipCost; Decimal decAddBookShipCost; ShippingCosts currentShipCosts = _context.ShippingCosts.FirstOrDefault(c => c.ShippingCostsID == ShipCostId); try { decFirstBookShipCost = Convert.ToDecimal(FirstBookShipCost); currentShipCosts.FirstBookShipCost = decFirstBookShipCost; } catch { ViewBag.InvalidFirstBook = "You must enter a valid decimal for the cost"; return(View()); } try { decAddBookShipCost = Convert.ToDecimal(AddBookShipCost); currentShipCosts.AddBookShipCost = decAddBookShipCost; } catch { ViewBag.InvalidAddBook = "You must enter a valid decimal for the cost"; return(View()); } _context.Update(currentShipCosts); _context.SaveChanges(); return(View(currentShipCosts)); //add the dec values to the model class properties //ShipFirstBook //ShipAddBook } }
public async Task <IActionResult> DeleteConfirmed(int id) { var orderDetail = await _context.OrderDetails.FindAsync(id); //before you remove from order detail, remove the shipping cost associated with it //fix shipping costs for the order OrderDetail od = _context.OrderDetails.Include(c => c.Order).FirstOrDefault(c => c.OrderDetailID == id); Order order = _context.Orders.Include(c => c.OrderDetails).ThenInclude(c => c.Book).FirstOrDefault(c => c.OrderID == od.Order.OrderID); ShippingCosts currentShipCosts = _context.ShippingCosts.FirstOrDefault(); int orderDetailCount = order.OrderDetails.Count(); //check if there's another book in the order already if (orderDetailCount > 1) ////there is another order detail connected to the existing open order { //if there is already other book(s) in the order decimal oldShippingCost = order.ShippingCost; //decimal leftoverShippingCost = oldShippingCost - (od.Quantity * 1.50m); decimal leftoverShippingCost = oldShippingCost - (od.Quantity * currentShipCosts.AddBookShipCost); //use the old quantity and subtract that old cost order.ShippingCost = leftoverShippingCost; } else { //if this is the only book in the order order.ShippingCost = 0m; //3.50m + ((orderDetail.Quantity - 1) * 1.50m); } //remove orderDetail from the database and save _context.OrderDetails.Remove(orderDetail); await _context.SaveChangesAsync(); return(RedirectToAction("ShoppingCart", "Orders")); }
//public async Task<IActionResult> Edit(int id, [Bind("OrderDetailID,Quantity,Price")] OrderDetail orderDetail) //{ //if (id != orderDetail.OrderDetailID) //{ // return NotFound(); //} public IActionResult Edit(OrderDetail orderDetail) { OrderDetail DbOrdDet = _context.OrderDetails.Include(r => r.Book).Include(r => r.Order).FirstOrDefault(r => r.OrderDetailID == orderDetail.OrderDetailID); ShippingCosts currentShipCosts = _context.ShippingCosts.FirstOrDefault(); if (ModelState.IsValid) { try { //if the quantity the user orders is higher than the inventory if (orderDetail.Quantity > DbOrdDet.Book.Inventory) { //figure out how to return an error message ViewBag.QuantityError = "The quantity you entered exceeds our stock."; orderDetail.Price = DbOrdDet.Price; return(View(orderDetail)); } else //allow them to make the change { //fix shipping costs for the order Order order = _context.Orders.Include(c => c.OrderDetails).ThenInclude(c => c.Book).FirstOrDefault(c => c.OrderID == DbOrdDet.Order.OrderID); int orderDetailCount = order.OrderDetails.Count(); //check if there's another book in the order already if (orderDetailCount > 1) ////there is another order detail connected to the existing open order { //if there is already other book(s) in the order decimal oldShippingCost = order.ShippingCost; //decimal additionalShippingCost = oldShippingCost - (DbOrdDet.Quantity * 1.50m); decimal additionalShippingCost = oldShippingCost - (DbOrdDet.Quantity * currentShipCosts.AddBookShipCost); //use the old quantity and subtract that old cost //order.ShippingCost = orderDetail.Quantity * 1.50m + additionalShippingCost; order.ShippingCost = orderDetail.Quantity * currentShipCosts.AddBookShipCost + additionalShippingCost; } else { //if this is the only book in the order //order.ShippingCost = 3.50m + ((orderDetail.Quantity - 1) * 1.50m); order.ShippingCost = currentShipCosts.FirstBookShipCost + ((orderDetail.Quantity - 1) * currentShipCosts.AddBookShipCost); } //update orderdetail DbOrdDet.Quantity = orderDetail.Quantity; DbOrdDet.Price = DbOrdDet.Price; //price should not change _context.OrderDetails.Update(DbOrdDet); //_context.Update(orderDetail); _context.SaveChanges(); } } catch (DbUpdateConcurrencyException) { if (!OrderDetailExists(orderDetail.OrderDetailID)) { return(NotFound()); } else { throw; } } //return RedirectToAction(nameof(Index)); return(RedirectToAction("ShoppingCart", "Orders")); } //this breaks return(View(DbOrdDet)); }
public override string ToString() { return(string.Concat(DeliveryTypeId, " ", DeliveryTypeName, " ", ShippingCosts.ToString("0.00 €", System.Globalization.CultureInfo.InvariantCulture))); }
public IEnumerable <ShippingCost> Get() { return(ShippingCosts.GetAll()); }
public IActionResult ShoppingCart() { //REMINDER: make it to find the open order for a //var orderList = _context.Orders.Include(m => m.OrderDetails).ThenInclude(m => m.Book).Where(c => c.IsComplete == false).Where(c => c.Customer == User.Identity).ToList(); Order order = _context.Orders.Include(c => c.Promo).Include(m => m.OrderDetails).ThenInclude(m => m.Book).Where(c => c.IsComplete == false).Where(c => c.Customer.UserName == User.Identity.Name).FirstOrDefault(); //Order order = _context.Orders.Include(m => m.OrderDetails).Where(c => c.IsComplete == false); if (order == null) { //Order NewOrder = new Order{}; //REMINDER: check for existing order (and create new one if needed) when a book is added to order //NewOrder.IsComplete = false; return(View("EmptyShoppingCart")); //REMINDER: return an empty shopping cart } else //return a view of the current shopping cart { if (order.OrderDetails.Count() == 0) { return(View("EmptyShoppingCart")); } else { //check for out of stock books --> show error //check for discontinued books foreach (OrderDetail od in order.OrderDetails.ToList()) { od.Price = od.Book.SalesPrice; //this actually saves all the data just entered, into the actual database _context.OrderDetails.Update(od); _context.SaveChanges(); if (od.Book.Inventory == 0) { //update the order to remove this order detail _context.OrderDetails.Remove(od); _context.SaveChanges(); ViewBag.OutOfStock = od.Book.Title + " is currently out of stock. It has been removed from your cart."; } if (od.Book.IsDiscontinued) //if it's true { Order orderWithCustomer = _context.Orders.Include(c => c.Customer).FirstOrDefault(c => c.OrderID == order.OrderID); //send email//send email String bookTitle = od.Book.Title; String bookAuthor = od.Book.Author; AppUser customer = orderWithCustomer.Customer; String emailsubject = "Team 22: Item in Cart is Discontinued"; String emailbody = "We apologize for the inconvenience, but the following book you have in your cart has been discontinued." + "\nBook: " + bookTitle + "\nAuthor: " + bookAuthor; OrdersController.SendEmail(customer.Email, customer.FirstName, emailbody, emailsubject); //update the order to remove this order detail _context.OrderDetails.Remove(od); _context.SaveChanges(); ViewBag.BookDiscontinued = od.Book.Title + " has been discontinued. It has been removed from your cart"; } } //recalculate the shipping costs when they come here??? ShippingCosts currentShipCosts = _context.ShippingCosts.FirstOrDefault(); //reset shipping cost order.ShippingCost = 0; int count = 0; foreach (OrderDetail od in order.OrderDetails.ToList()) { if (count == 0) //if this is the first order detail { if (od.Quantity == 1) { order.ShippingCost = currentShipCosts.FirstBookShipCost; } else //more than 1 book in the orderdetail { order.ShippingCost = currentShipCosts.FirstBookShipCost + ((od.Quantity - 1) * currentShipCosts.AddBookShipCost); } } else //all other orderdetails { order.ShippingCost += currentShipCosts.AddBookShipCost * od.Quantity; } count += 1; } _context.Update(order); _context.SaveChanges(); return(View(order)); } } }
public IActionResult AddToOrder(int?id) //book id { //find the book being added to the order Book book = _context.Books.Find(id); book.Inventory = GetBookInventory(book); //if the book is out of stock, cannot add to order if (book.Inventory == 0) { return(View("BookOutOfStock")); //this book is out of stock, return user to error page saying it cannot be ordered. } //when a user adds a book to an order, do they go to a page to choose how many???? (elif) else { String userid = User.Identity.Name; AppUser currentuser = _context.Users.FirstOrDefault(r => r.UserName == userid); var orderquery = from r in _context.OrderDetails.Include(r => r.Book).Include(r => r.Order).ThenInclude(r => r.Customer) select r; orderquery = orderquery.Where(r => r.Order.Customer.UserName == currentuser.UserName && r.Order.IsComplete == false); List <OrderDetail> currentorddetails = orderquery.ToList(); List <Book> booksinorder = new List <Book>(); if (currentorddetails.Count() != 0) { foreach (OrderDetail orddetail in currentorddetails) { booksinorder.Add(orddetail.Book); } foreach (Book bk in booksinorder) { if (bk.Title == book.Title) { ViewBag.CannotReAdd = "You cannot add this book to your order. It is already in your cart."; return(RedirectToAction("Details", "Search", new { id = id })); //book id } // else // { // ViewBag.CannotReAdd = ""; // } } } //else //{ // ViewBag.CannotReAdd = ""; //} //create a new order detail for the book for the shopping cart order OrderDetail od = new OrderDetail { }; //add values for all other fields for orderDetail od.Book = book; od.Price = od.Book.SalesPrice; od.Quantity = 1; //automatically add 1 book to the order //this actually saves all the data just entered, into the actual database _context.OrderDetails.Add(od); _context.SaveChanges(); //WORKS UP TO HERE //connect to the shopping cart order //od.Order = _context.Orders.Where(c => c.IsComplete == false).Where(c => c.Customer.UserName == User.Identity.Name).FirstOrDefault(); Order ShoppingCartOrder = _context.Orders.Include(c => c.OrderDetails).Where(c => c.IsComplete == false).Where(c => c.Customer.UserName == User.Identity.Name).FirstOrDefault(); ShippingCosts currentShipCosts = _context.ShippingCosts.FirstOrDefault(); //if a shopping cart doesn't exist, if (ShoppingCartOrder == null) //no current shopping cart --> add all the fields that need to be put in to create an order { ShoppingCartOrder = new Order { }; ShoppingCartOrder.OrderNumber = GenerateNextOrderNumber.GetNextOrderNumber(_context); ShoppingCartOrder.OrderDate = System.DateTime.Today; //ShoppingCartOrder.ShippingCost = 3.50m; //because this is the first book being added to order ShoppingCartOrder.ShippingCost = currentShipCosts.FirstBookShipCost; ShoppingCartOrder.IsComplete = false; //makes this the shopping cart //od.Order.OrderNumber = GenerateNextOrderNumber.GetNextOrderNumber(_context); od.Order = ShoppingCartOrder; //ShoppingCartOrder.OrderDetails.Add(od); //how to add customer to an order String userId = User.Identity.Name; AppUser user = _context.Users.FirstOrDefault(u => u.UserName == userId); ShoppingCartOrder.Customer = user; //THIS IS THROWING ERROR WITH IDENTITY_INSERT //adds this shopping cart ORDER to the orders table in database _context.Orders.Add(ShoppingCartOrder); _context.SaveChanges(); } //what to change for the order if it does already exist else { //_context.Orders.Add(od.Order); _context.SaveChanges(); //Order existingCart = od.Order; ShoppingCartOrder.OrderDate = System.DateTime.Today; //int orderDetailCount = 0; ////the count is not increasing!!!! //foreach(OrderDetail ordDet in existingCart.OrderDetails.ToList()) //{ // orderDetailCount = 1 + orderDetailCount; //} //od.Order = ShoppingCartOrder; ShoppingCartOrder.OrderDetails.Add(od); int orderDetailCount = ShoppingCartOrder.OrderDetails.Count(); //check if there's another book in the order already //if (existingCart.OrderDetails.Count() > 1) //there is another order detail connected to the existing open order if (orderDetailCount > 1) { //ShoppingCartOrder.ShippingCost = 1.50m + ShoppingCartOrder.ShippingCost; ShoppingCartOrder.ShippingCost = currentShipCosts.AddBookShipCost + ShoppingCartOrder.ShippingCost; } else { //ShoppingCartOrder.ShippingCost = 3.50m; //add 1.50 each additional book if one is already in cart ShoppingCartOrder.ShippingCost = currentShipCosts.FirstBookShipCost; } _context.SaveChanges(); } //Order order = _context.Orders.Find(od.Order.OrderID); return(RedirectToAction("ShoppingCart", "Orders", new { id = od.Book.BookID })); } }
public void GetOne_GoodZone_WeightInRangeHighEdge() { ShippingCost rate = ShippingCosts.GetOne("3", 1.5m); Assert.IsTrue(rate.Cost == 2.25m); }
public void GetOne_GoodZone_WeightOutOfRangeLowEdge() { ShippingCost rate = ShippingCosts.GetOne("4", 2.01m); Assert.IsNull(rate); }
public void GetOne_GoodZone_WeightInRangeLowEdge() { ShippingCost rate = ShippingCosts.GetOne("4", 0.01m); Assert.IsTrue(rate.Cost == 1.25m); }
public void GetOne_BadZone() { ShippingCost rate = ShippingCosts.GetOne("5", 2.0m); Assert.IsNull(rate); }
public void GetAll_Returns_Array() { ShippingCost[] costs = ShippingCosts.GetAll(); Assert.IsNotNull(costs); Assert.IsInstanceOfType(costs[0], typeof(ShippingCost)); }