//Cancel an order and decrement a counter so the delivery employee may be available again //The user must enter his firstname and lastaname correctly to make all this happening. public ActionResult CancelOrder(Customer customerModel) { var customerDbManager = new CustomerManager(Configuration); int idCustomer = (int)HttpContext.Session.GetInt32("idCustomer"); Customer customer = customerDbManager.GetFirstnameLastname(idCustomer); //We make our strings lower case so we don't care whether the customer use capital case or not. string firstnameC = customerModel.FirstName.ToLower(); string lastnameC = customerModel.LastName.ToLower(); if (firstnameC.Equals(customer.FirstName.ToLower()) && lastnameC.Equals(customer.LastName.ToLower())) { int idOrder = (int)HttpContext.Session.GetInt32("idOrder"); int idStaffToDecrement = (int)HttpContext.Session.GetInt32("idStaffToDecrement"); TimeSpan deliveryTimeToCancel = SessionHelper.GetObjectFromJson <TimeSpan>(HttpContext.Session, "deliveryTimeToCancel"); Dishes_orderManager dManager = new Dishes_orderManager(Configuration); dManager.UpdateOrderStatusToCancel(idOrder); AvailibilityManager aManager = new AvailibilityManager(Configuration); aManager.DecrementCounter(idStaffToDecrement); Availability availabilityStatus = aManager.IsAvailable(idStaffToDecrement, deliveryTimeToCancel); if (!availabilityStatus.isAvailable) { aManager.UpdateAvailability(availabilityStatus.idAvailability, 1); } return(RedirectToAction("customerOrders", "DishesOrder")); } else { //If the user fails in entering a correct firstname and lastname the cancellation do not happen return(RedirectToAction("OrderCancelError", "Error", new { message = "Unfortunately your firstname or lastname did not match our records. Please try again." })); } }
//Display all the orders and their status that a customer have ever made public IActionResult CustomerOrders() { int id = (int)HttpContext.Session.GetInt32("idCustomer"); ViewBag.username = HttpContext.Session.GetString("username"); ViewBag.userType = HttpContext.Session.GetInt32("userType"); Dishes_orderManager dManager = new Dishes_orderManager(Configuration); return(View(dManager.GetDishes_orderByCustomer(id))); }
//Used by the delivery employee to archive a delivery once it is done. public IActionResult UpdateOrderStatus(int idOrder) { int id = (int)HttpContext.Session.GetInt32("idStaff"); Dishes_orderManager dManager = new Dishes_orderManager(Configuration); dManager.UpdateOrderStatus(idOrder); return(RedirectToAction("Index")); }
//Display to the delivery employee all the deliveries to do of the day public IActionResult Index() { int id = (int)HttpContext.Session.GetInt32("idStaff"); ViewBag.username = HttpContext.Session.GetString("username"); ViewBag.userType = HttpContext.Session.GetInt32("userType"); Dishes_orderManager dManager = new Dishes_orderManager(Configuration); List <deliveryItem> OrderToDeliver = dManager.GetDishes_orderByStaff(id); if (OrderToDeliver != null) { return(View(OrderToDeliver)); } return(RedirectToAction("NoOrderError", "Error", new { message = "You don't have any orders, enjoy while it lasts! :-)" })); }
//Insert the cart into the database public IActionResult ProceedCheckout(int idAvailability, int idStaff, TimeSpan choosenTime) { ViewBag.username = HttpContext.Session.GetString("username"); ViewBag.userType = HttpContext.Session.GetInt32("userType"); ViewBag.totalAmount = HttpContext.Session.GetInt32("TotalAmount"); int idCredentials = (int)HttpContext.Session.GetInt32("id"); CustomerManager cManger = new CustomerManager(Configuration); int idCustomer = cManger.GetCustomerIDByCredentials(idCredentials); AvailibilityManager aManager = new AvailibilityManager(Configuration); if (idAvailability % 15 < 14 && idAvailability % 15 >= 1) { //additioning the counter of the cur. time + cur. time + 15 + cur. time + 30 int totalCounter = aManager.GetCounter(idAvailability) + aManager.GetCounter(idAvailability + 1) + aManager.GetCounter(idAvailability + 2); if (totalCounter >= 4) { //Make deliveryBoy unavailable at the choosen time, 0 means unavailable aManager.UpdateAvailability(idAvailability, 0); } for (int i = idAvailability; i <= idAvailability + 2; i++) { //increment counter aManager.IncrementCounter(i); } } else if (idAvailability % 15 == 14) { int totalCounter = aManager.GetCounter(idAvailability) + aManager.GetCounter(idAvailability + 1); if (totalCounter >= 4) { //Make deliveryBoy unavailable at the choosen time aManager.UpdateAvailability(idAvailability, 0); } for (int i = idAvailability; i <= idAvailability + 1; i++) { //increment counter aManager.IncrementCounter(i); } } else if (idAvailability % 15 == 0) { int totalCounter = aManager.GetCounter(idAvailability); if (totalCounter >= 4) { //Make deliveryBoy unavailable at the choosen time aManager.UpdateAvailability(idAvailability, 0); } aManager.IncrementCounter(idAvailability); } //First we add the delivery DeliveryManager dManager = new DeliveryManager(Configuration); dManager.AddDelivery(choosenTime, idStaff); int lastDelivery = dManager.GetLastId(); OrderManager oManager = new OrderManager(Configuration); //Then we create the order oManager.AddOrder(idCustomer, lastDelivery); int lastOrder = oManager.GetLastId(); //Finaly we link each dish to the order in the dish_order table List <Item> cart = SessionHelper.GetObjectFromJson <List <Item> >(HttpContext.Session, "cart"); Dishes_orderManager doManager = new Dishes_orderManager(Configuration); foreach (Item dish in cart) { doManager.AddDishes_order(dish.Dishe.idDishes, lastOrder, dish.Quantity); } //This way we respect every foreign key and we do not generate errors. cart = null; //Clean up cart SessionHelper.SetObjectAsJson(HttpContext.Session, "cart", cart); HttpContext.Session.SetInt32("TotalAmount", 0); return(View()); }