Ejemplo n.º 1
0
        /// <summary>
        /// Calculates how many items would remain in stock at a location given the order
        /// and product.
        ///
        /// This is used to show the user updated location stocks based on what is in their current
        /// order without actually committing any changes.
        /// In a multiuser scenario, each user would see different stocks since they may
        /// have different quantities of line items.
        ///
        /// Ex: 10 in stock, 5 in order -> return 5
        /// </summary>
        /// <param name="ctx">Database context object.</param>
        /// <param name="order">Order to query against.</param>
        /// <param name="product">Product to check for.</param>
        /// <returns>The amount of items that would be remaining if the order was committed.</returns>
        public static int ProjectStockBasedOnOrder(this StoreContext ctx, Order order, Product product)
        {
            int stock = ctx.GetProductsAvailable(order.Location.LocationId)
                        .Where(inv => inv.Product.ProductId == product.ProductId)
                        .Select(inv => inv.Quantity)
                        .FirstOrDefault();

            int quantityInOrder = order.OrderLineItems
                                  .Where(ol => ol.Product.ProductId == product.ProductId)
                                  .Select(ol => ol.Quantity)
                                  .FirstOrDefault();

            return(stock - quantityInOrder);
        }
Ejemplo n.º 2
0
        /// <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");
            }
        }