/// <summary>
        /// Retrieves a list of
        /// </summary>
        /// <param name="locationName">Name of the location</param>
        /// <returns>A p</returns>
        public Packet GetLocationOrderHistoryPacket(string locationName)
        {
            // check if location exists
            Location location = GetLocationByName(locationName);

            if (location == null)
            {
                return(new Packet
                {
                    Text = $"The location {locationName} is not a registered location in our system",
                    Status = PacketStatus.Invalid
                });
            }
            // find order history
            OrderLogic   orderLogic           = new OrderLogic();
            List <Order> locationOrderHistory = orderLogic.FindOrdersByLocationName(locationName);

            if (locationOrderHistory.Count == 0) // Account Location having no orders
            {
                return(new Packet
                {
                    Text = "No orders were found for this location",
                    Status = PacketStatus.Pass
                });
            }

            // create response

            // Format list to string
            string orderListStr = $"Order history for {locationName}:\n";

            foreach (var order in locationOrderHistory)
            {
                orderListStr += $"Purchaser: {order.Customer.Username},  Product: {order.ProductName},  Amount Purchased: {order.QuantitySold}, Purchase Date: {order.CreatedDate}";
            }

            // Respond with results
            return(new Packet
            {
                Text = orderListStr,
                Status = PacketStatus.Pass
            });
        }
Exemplo n.º 2
0
        /// <summary>
        /// Registers a customer making a purchase
        /// </summary>
        /// <param name="customerName">
        ///  The name of the Customer making the purchase
        /// </param>
        /// <param name="locationName">
        ///  The name of the location the customer is purchasing from.
        /// </param>
        /// <param name="productName">
        /// The product being purchased
        /// </param>
        /// <param name="quantity">
        ///  The amount of the product being purchased
        /// </param>
        /// <returns>A Packet containing info of the purchase</returns>
        public Packet MakePurchase(string username, string locationName, string productName, int quantity)
        {
            CustomerLogic customerLogic = new CustomerLogic();
            Customer      customer;

            LocationLogic locationLogic = new LocationLogic();
            Location      location;

            ProductInventoryLogic pIL = new ProductInventoryLogic();
            ProductInventory      productInventory;

            ProductLogic productLogic = new ProductLogic();
            Product      product;

            OrderLogic orderLogic = new OrderLogic();

            // check if customer Exists
            customer = customerLogic.FindCustomerByUsername(username);
            if (customer == null)
            {
                return(new Packet
                {
                    Text = "That customer does not exist.",
                    Status = PacketStatus.Invalid,
                });
            }

            // check location exists
            location = locationLogic.GetLocationByName(locationName);
            if (location == null)
            {
                return(new Packet
                {
                    Text = "The location specified was not found.",
                    Status = PacketStatus.Invalid,
                });
            }
            // check product exists
            product = productLogic.FindProductByName(productName);
            if (product == null)
            {
                return(new Packet
                {
                    Text = "The product specified was not found.",
                    Status = PacketStatus.Invalid,
                });
            }

            // Check location has product in stock
            productInventory = pIL.GetProductInventory(location, product);
            if (productInventory == null)
            {
                return(new Packet
                {
                    Text = "This location does not currently carry this product",
                    Status = PacketStatus.Invalid
                });
            }

            // Check to see if customer isn't buying too many products
            if (quantity > productInventory.Quanitity)
            {
                return(new Packet
                {
                    Text = $"This location does not carry the amount of {product.Name} you are trying to purchase",
                    Status = PacketStatus.Invalid
                });
            }

            // All's good, being registering purchase

            // Decrease quantity of location by the amount the customer is purchasing
            int quantitySold = productInventory.Quanitity - quantity;

            pIL.UpdateInventory(location, product, quantitySold);

            // Register Order for customer
            orderLogic.RegisterOrder(customer, locationName, productName, quantity);

            return(new Packet
            {
                Text = $"Order purchased successfully - Purchaser {username} ordered product {productName} from location {locationName}",
                Status = PacketStatus.Pass
            });
        }