/// <summary> /// Retrieves the current open order for the customer. /// </summary> public void RefreshCurrentOrder() { using (var db = new StoreContext(this.GlobalState.DbOptions)) { var location = db.GetLocationById(this.OperatingLocationId); var currentOrder = db.FindCurrentOrder(location, this.CustomerId); if (currentOrder != null) { this.CurrentOrderId = currentOrder.OrderId; } else { this.CurrentOrderId = null; } } }
/// <summary> /// Print this menu. /// </summary> public void PrintMenu() { Console.Clear(); CliPrinter.Title("Order Items"); this.ApplicationState.UserData.RefreshCurrentOrder(); if (this.ApplicationState.UserData.CurrentOrderId == null) { using (var db = new StoreContext(this.ApplicationState.DbOptions)) { var customer = db.GetCustomerById(this.ApplicationState.UserData.CustomerId); var location = db.GetLocationById(this.ApplicationState.UserData.OperatingLocationId); var order = new Order(customer, location); order.Customer = customer; db.Add(order); db.SaveChanges(); this.ApplicationState.UserData.CurrentOrderId = order.OrderId; } } using (var db = new StoreContext(this.ApplicationState.DbOptions)) { var inventory = db.GetProductsAvailable(this.ApplicationState.UserData.OperatingLocationId); var order = db.GetOrderById(this.ApplicationState.UserData.CurrentOrderId); var i = 1; this.InventoryIds = inventory.Select(i => i.LocationInventoryId).ToList(); Console.WriteLine("#\tStock\tName"); foreach (var stock in inventory) { var projectedQuantity = db.ProjectStockBasedOnOrder(order, stock.Product); if (projectedQuantity < 0) { projectedQuantity = 0; } Console.WriteLine($"{i}.\t{projectedQuantity}\t{stock.Product.Name}"); i += 1; } Console.Write("\n----------------------------------------------\n"); } }
/// <summary> /// Sets the default location to be used by a customer. /// </summary> /// <param name="options">Options for creating a new context.</param> /// <param name="customerId">The ID of the customer to operate upon.</param> /// <param name="locationId">The location ID to set as the default.</param> /// <returns>Whether the operation succeeded.</returns> public static bool SetDefaultLocation(this DbContextOptions <StoreContext> options, Guid?customerId, Guid?locationId) { if (customerId == null || locationId == null) { return(false); } using (var db = new StoreContext(options)) { var location = db.GetLocationById(locationId); var customer = db.GetCustomerById(customerId); if (location == null || customer == null) { return(false); } customer.DefaultLocation = location; db.SaveChanges(); return(true); } }
/// <summary> /// Handle input. /// </summary> public void InputLoop() { do { PrintMenu(); using (var db = new StoreContext(this.ApplicationState.DbOptions)) { var location = db.GetLocationById(this.ApplicationState.UserData.OperatingLocationId); var order = db.FindCurrentOrder(location, this.ApplicationState.UserData.CustomerId); if (order == null) { CliPrinter.Error("There was an error retrieving your order. Please try again."); break; } bool itemNumberValidator(int num) { if (num > 0 && num <= this.InventoryIds.Count) { return(true); } else { CliPrinter.Error($"Please enter an inventory number between 1 and {this.InventoryIds.Count}"); return(false); } } var itemIndex = CliInput.GetInt(CliInput.GetIntOptions.AllowEmpty, itemNumberValidator, "Inventory number: ") ?? 0; if (itemIndex == 0) { break; } var inventoryId = this.InventoryIds[itemIndex - 1]; var product = db.GetProductFromInventoryId(inventoryId); var projectedQuantity = db.ProjectStockBasedOnOrder(order, product); if (projectedQuantity <= 0) { CliInput.PressAnyKey("That item is out of stock"); continue; } var orderQuantity = CliInput.GetInt(CliInput.GetIntOptions.AllowEmpty, v => v > 0 && v <= projectedQuantity, $"Quantity [1..{projectedQuantity}]: ") ?? 0; if (orderQuantity == 0) { continue; } db.AddLineItem(order, product, orderQuantity); db.SaveChanges(); CliInput.PressAnyKey("\nAdded to order."); } } while (true); this.MenuExit(); }
/// <summary> /// Print this menu. /// </summary> public void PrintMenu() { Console.Clear(); CliPrinter.Title("Order History"); switch (this.CurrentOperatingMode) { case OperatingMode.SelectLocation: using (var db = new StoreContext(this.ApplicationState.DbOptions)) { var locations = db.GetLocations(); this.LocationIds.Clear(); var i = 1; foreach (var location in locations) { this.LocationIds.Add(location.LocationId); Console.WriteLine($"{i}. {location.Name}"); i += 1; } Console.WriteLine("\n"); } break; case OperatingMode.ViewOrders: using (var db = new StoreContext(this.ApplicationState.DbOptions)) { var location = db.GetLocationById(this.SelectedLocation); if (location == null) { break; } var orders = db .GetOrderHistory(location) .Select(o => new { OrderId = o.OrderId, Customer = o.Customer, Location = o.Location, TimeCreated = o.TimeCreated, TimeSubmitted = o.TimeSubmitted, TimeFulfilled = o.TimeFulfilled, AmountPaid = o.AmountPaid, OrderLineItem = o.OrderLineItems, AmountCharged = db.GetAmountCharged(o), }).ToList(); if (orders.Count() == 0) { CliPrinter.Error("There are no orders for this location."); this.CurrentOperatingMode = OperatingMode.SelectLocation; CliInput.PressAnyKey(); this.PrintMenu(); return; } var upArrow = '↑'; var downArrow = '↓'; var priceSortSymbol = '-'; var dateSortSymbol = '-'; switch (this.SortKey) { case OrderSortKey.DateDesc: orders = orders.OrderByDescending(o => o.TimeSubmitted).ToList(); dateSortSymbol = downArrow; break; case OrderSortKey.DateAsc: orders = orders.OrderBy(o => o.TimeSubmitted).ToList(); dateSortSymbol = upArrow; break; case OrderSortKey.PriceDesc: orders = orders.OrderByDescending(o => o.AmountCharged).ToList(); priceSortSymbol = downArrow; break; case OrderSortKey.PriceAsc: orders = orders.OrderBy(o => o.AmountCharged).ToList(); priceSortSymbol = upArrow; break; } var historyDisplayAlignment = "{0,-6}{1,-9}{2,-25}"; var priceSortLine = $"{priceSortSymbol}----"; var dateSortLine = $"{dateSortSymbol}---"; Console.WriteLine(historyDisplayAlignment, "Num", "Price", "Date"); Console.WriteLine(historyDisplayAlignment, "---", priceSortLine, dateSortLine); var i = 1; this.OrderIds.Clear(); foreach (var order in orders) { this.OrderIds.Add(order.OrderId); Console.WriteLine(historyDisplayAlignment, i + ".", "$" + order.AmountCharged, order.TimeSubmitted, order.Location.Name); i += 1; } } break; default: break; } }