Exemplo n.º 1
0
        public void Post([FromBody] OutboundOrderRequestModel request)
        {
            log.Info(string.Format("Processing outbound order: {0}", request));

            var lineItems  = new List <StockAlteration>();
            var productIds = new List <int>();
            var gtins      = AddGtins(request);

            Dictionary <string, Product> products = CreateProducts(gtins);

            GetProductIDs(request, productIds, products);
            GetAmountOfStockToRemove(request, lineItems, products);

            var stock      = stockRepository.GetStockByWarehouseAndProductIds(request.WarehouseId, productIds);
            var orderLines = request.OrderLines.ToList();

            CheckStocksForIfWeCanDoTheOrder(lineItems, stock, orderLines, products);

            Truck(request, gtins, products);

            stockRepository.RemoveStock(request.WarehouseId, lineItems);
        }
Exemplo n.º 2
0
        public List <Truck> Truck(OutboundOrderRequestModel request, List <string> gtins = default, Dictionary <string, Product> products = default)
        {
            // make a new list of trucks
            List <Truck> trucks = new List <Truck>();

            //Get all of the products in the order, if we need to
            if (gtins == default)
            {
                gtins = AddGtins(request);
            }
            if (products == default)
            {
                products = CreateProducts(gtins);
            }

            foreach (var orderLine in request.OrderLines)
            {
                // make a new trck at the start of a new order
                trucks.Add(new Truck()
                {
                    Products = new List <Product>()
                });

                for (int i = 0; i < orderLine.quantity; i++)
                {
                    var product = products[orderLine.gtin];

                    var truck = trucks.Last(); //Get the last truck in the list as it should have space left
                    if (truck.CurrentWeightInGrams + product.Weight > truck.MaxWeightInGrams)
                    {
                        truck = CreateTruckAndUseIt(trucks);
                    }
                    truck.Products.Add(product);
                    truck.CurrentWeightInGrams += product.Weight;
                }
            }
            return(trucks);
        }
        public void TestOutboundOrderTrucksweightLessThanMax()
        {
            // ARRANGE
            onSetUp();
            var productA = new ProductBuilder().setGtin("0001").CreateProductDatabaseModel();

            productRepository.AddProducts(new List <ProductDataModel>()
            {
                productA
            });
            var productAID = new Product(productRepository.GetProductByGtin("0001")).Id;

            stockRepository.AddStock(WAREHOUSE_ID, new List <StockAlteration>()
            {
                new StockAlteration(productAID, 2000)
            });

            var outboundOrder = new OutboundOrderRequestModel()
            {
                WarehouseId = WAREHOUSE_ID,
                OrderLines  = new List <OrderLine>()
                {
                    new OrderLine()
                    {
                        gtin     = "0001",
                        quantity = 1999
                    },
                }
            };

            // ACT
            var trucks = outboundOrderController.Truck(outboundOrder);

            // ASSERT
            Assert.AreEqual(1, trucks.Count);
            Assert.AreEqual(1999, trucks[0].Products.Count);
            Assert.AreEqual(1999000, trucks[0].CurrentWeightInGrams);
        }
        public void TestOutboundOrderDuplicateGtins()
        {
            onSetUp();
            stockRepository.AddStock(WAREHOUSE_ID, new List <StockAlteration>()
            {
                new StockAlteration(productId, 10, 200)
            });
            var outboundOrder = new OutboundOrderRequestModel()
            {
                WarehouseId = WAREHOUSE_ID,
                OrderLines  = new List <OrderLine>()
                {
                    new OrderLine()
                    {
                        gtin     = GTIN,
                        quantity = 1
                    },
                    new OrderLine()
                    {
                        gtin     = GTIN,
                        quantity = 1
                    }
                }
            };

            try
            {
                outboundOrderController.Post(outboundOrder);
                Console.WriteLine("PostContent\n" + outboundOrder)
                Assert.Fail("Expected exception to be thrown.");
            }
            catch (ValidationException e)
            {
                Assert.IsTrue(e.Message.Contains(GTIN));
            }
        }
Exemplo n.º 5
0
        public OutboundOrderTrucksResponse Post([FromBody] OutboundOrderRequestModel request)
        {
            log.Info(String.Format("Processing outbound order: {0}", request));

            var gtins = new List <String>();

            foreach (var orderLine in request.OrderLines)
            {
                if (gtins.Contains(orderLine.gtin))
                {
                    throw new ValidationException(
                              String.Format("Outbound order request contains duplicate product gtin: {0}", orderLine.gtin));
                }

                gtins.Add(orderLine.gtin);
            }

            var productDataModels = productRepository.GetProductsByGtin(gtins);
            var products          = productDataModels.ToDictionary(p => p.Gtin, p => new Product(p));

            var lineItems  = new List <StockAlteration>();
            var productIds = new List <int>();
            var errors     = new List <string>();

            foreach (var orderLine in request.OrderLines)
            {
                if (!products.ContainsKey(orderLine.gtin))
                {
                    errors.Add(string.Format("Unknown product gtin: {0}", orderLine.gtin));
                }
                else
                {
                    var product = products[orderLine.gtin];
                    lineItems.Add(new StockAlteration(product.Id, orderLine.quantity));
                    productIds.Add(product.Id);
                }
            }

            if (errors.Count > 0)
            {
                throw new NoSuchEntityException(string.Join("; ", errors));
            }

            var stock = stockRepository.GetStockByWarehouseAndProductIds(request.WarehouseId, productIds);

            var orderLines = request.OrderLines.ToList();

            errors = new List <string>();

            for (int i = 0; i < lineItems.Count; i++)
            {
                var lineItem  = lineItems[i];
                var orderLine = orderLines[i];

                if (!stock.ContainsKey(lineItem.ProductId))
                {
                    errors.Add(string.Format("Product: {0}, no stock held", orderLine.gtin));
                    continue;
                }

                var item = stock[lineItem.ProductId];
                if (lineItem.Quantity > item.held)
                {
                    errors.Add(
                        string.Format("Product: {0}, stock held: {1}, stock to remove: {2}", orderLine.gtin, item.held,
                                      lineItem.Quantity));
                }
            }

            if (errors.Count > 0)
            {
                throw new InsufficientStockException(string.Join("; ", errors));
            }

            stockRepository.RemoveStock(request.WarehouseId, lineItems);

            TruckService newTrucks = new TruckService(productRepository);
            var          trucks    = newTrucks.GetTrucks(lineItems);
            var          response  = new OutboundOrderTrucksResponse
            {
                Trucks         = trucks,
                NumberOfTrucks = trucks.Count
            };

            return(response);
        }
Exemplo n.º 6
0
        public TruckModel Post([FromBody] OutboundOrderRequestModel request)
        {
            Log.Info(String.Format("Processing outbound order: {0}", request));

            var gtins = new List <String>();

            foreach (var orderLine in request.OrderLines)
            {
                if (gtins.Contains(orderLine.gtin))
                {
                    throw new ValidationException(String.Format("Outbound order request contains duplicate product gtin: {0}", orderLine.gtin));
                }
                gtins.Add(orderLine.gtin);
            }

            var productDataModels = _productRepository.GetProductsByGtin(gtins);
            var products          = productDataModels.ToDictionary(p => p.Gtin, p => new Product(p));

            var lineItems  = new List <StockAlteration>();
            var productIds = new List <int>();
            var errors     = new List <string>();

            foreach (var orderLine in request.OrderLines)
            {
                if (!products.ContainsKey(orderLine.gtin))
                {
                    errors.Add(string.Format("Unknown product gtin: {0}", orderLine.gtin));
                }
                else
                {
                    var product = products[orderLine.gtin];
                    lineItems.Add(new StockAlteration(product.Id, orderLine.quantity));
                    productIds.Add(product.Id);
                }
            }

            if (errors.Count > 0)
            {
                throw new NoSuchEntityException(string.Join("; ", errors));
            }

            var stock = _stockRepository.GetStockByWarehouseAndProductIds(request.WarehouseId, productIds);

            var orderLines = request.OrderLines.ToList();

            errors = new List <string>();

            for (int i = 0; i < lineItems.Count; i++)
            {
                var lineItem  = lineItems[i];
                var orderLine = orderLines[i];

                if (!stock.ContainsKey(lineItem.ProductId))
                {
                    errors.Add(string.Format("Product: {0}, no stock held", orderLine.gtin));
                    continue;
                }

                var item = stock[lineItem.ProductId];
                if (lineItem.Quantity > item.held)
                {
                    errors.Add(
                        string.Format("Product: {0}, stock held: {1}, stock to remove: {2}", orderLine.gtin, item.held,
                                      lineItem.Quantity));
                }
            }

            if (errors.Count > 0)
            {
                throw new InsufficientStockException(string.Join("; ", errors));
            }

            _stockRepository.RemoveStock(request.WarehouseId, lineItems);

            // for each product id in lineItems, get data from gtin table. find the m_g. multuply by min_qt.
            // gives you the weight in gms. divide by 2000 to get trucks

            double totalWeight = 0;

            foreach (var line in lineItems)
            {
                var newproduct = _productRepository.GetProductById(line.ProductId);
                totalWeight = totalWeight + newproduct.Weight;
            }
            var Trucks = new TruckModel(0);

            Trucks.Trucks_Needed = Math.Ceiling(totalWeight / (1000 * 2000));
            return(new TruckModel(Trucks.Trucks_Needed));
        }
        public void Post([FromBody] OutboundOrderRequestModel request)
        {
            Log.Info($"Processing outbound order: {request}");
            var   products          = GetProductsFromRequest(request);
            var   lineItems         = new List <StockAlteration>();
            var   productIds        = new List <int>();
            var   errors            = new List <string>();
            float weightCalculation = 0;

            foreach (var orderLine in request.OrderLines)
            {
                if (!products.ContainsKey(orderLine.Gtin))
                {
                    errors.Add($"Unknown product gtin: {orderLine.Gtin}");
                }
                else
                {
                    var product = products[orderLine.Gtin];
                    lineItems.Add(new StockAlteration(product.Id, orderLine.Quantity));
                    productIds.Add(product.Id);
                }
            }

            if (errors.Count > 0)
            {
                throw new NoSuchEntityException(string.Join("; ", errors));
            }

            var stock = _stockRepository.GetStockByWarehouseAndProductIds(request.WarehouseId, productIds);

            var orderLines = request.OrderLines.ToList();

            errors = new List <string>();

            for (var i = 0; i < lineItems.Count; i++)
            {
                var lineItem  = lineItems[i];
                var orderLine = orderLines[i];

                if (!stock.ContainsKey(lineItem.ProductId))
                {
                    errors.Add($"Product: {orderLine.Gtin}, no stock held");
                    continue;
                }

                var item = stock[lineItem.ProductId];
                if (lineItem.Quantity > item.Held)
                {
                    errors.Add(
                        $"Product: {orderLine.Gtin}, stock held: {item.Held}, stock to remove: {lineItem.Quantity}");
                }
            }

            if (errors.Count > 0)
            {
                throw new InsufficientStockException(string.Join("; ", errors));
            }
            if (weightCalculation > 1000)
            {
                throw new TruckOverloadedException(string.Join("; ", errors));
            }

            _stockRepository.RemoveStock(request.WarehouseId, lineItems);
            _trucksService.GetTrucksForOrder(lineItems);
        }
Exemplo n.º 8
0
        public JsonResult Post([FromBody] OutboundOrderRequestModel request)
        {
            Log.Info(String.Format("Processing outbound order: {0}", request));

            var gtins = new List <String>();

            foreach (var orderLine in request.OrderLines)
            {
                if (gtins.Contains(orderLine.gtin))
                {
                    throw new ValidationException(String.Format("Outbound order request contains duplicate product gtin: {0}", orderLine.gtin));
                }
                gtins.Add(orderLine.gtin);
            }

            var productDataModels = _productRepository.GetProductsByGtin(gtins);
            var products          = productDataModels.ToDictionary(p => p.Gtin, p => new Product(p));

            var lineItems  = new List <StockAlteration>(); //stock contains product id and quantity
            var productIds = new List <int>();
            var errors     = new List <string>();
            //float weight = 0
            float weight = 0;

            foreach (var orderLine in request.OrderLines)
            {
                if (!products.ContainsKey(orderLine.gtin))
                {
                    errors.Add(string.Format("Unknown product gtin: {0}", orderLine.gtin));
                }
                else
                {
                    var product = products[orderLine.gtin];
                    lineItems.Add(new StockAlteration(product.Id, orderLine.quantity));
                    productIds.Add(product.Id);
                    weight += product.Weight * orderLine.quantity;

                    //find weight for product of product.id, multiply by orderLine quantity
                    //weight += product.id.weight * orderline quantity
                }
            }
            float weightkg = weight / 1000;
            var   noTrucks = Math.Ceiling(weightkg / 2000);

            Console.WriteLine("Number of trucks: " + noTrucks);

            if (errors.Count > 0)
            {
                throw new NoSuchEntityException(string.Join("; ", errors));
            }

            var stock = _stockRepository.GetStockByWarehouseAndProductIds(request.WarehouseId, productIds);

            var orderLines = request.OrderLines.ToList();

            errors = new List <string>();

            for (int i = 0; i < lineItems.Count; i++)
            {
                var lineItem  = lineItems[i];
                var orderLine = orderLines[i];

                if (!stock.ContainsKey(lineItem.ProductId))
                {
                    errors.Add(string.Format("Product: {0}, no stock held", orderLine.gtin));
                    continue;
                }

                var item = stock[lineItem.ProductId];
                if (lineItem.Quantity > item.held)
                {
                    errors.Add(
                        string.Format("Product: {0}, stock held: {1}, stock to remove: {2}", orderLine.gtin, item.held,
                                      lineItem.Quantity));
                }
            }

            if (errors.Count > 0)
            {
                throw new InsufficientStockException(string.Join("; ", errors));
            }

            _stockRepository.RemoveStock(request.WarehouseId, lineItems);

            return(new JsonResult(noTrucks));
        }
Exemplo n.º 9
0
        public TrucksNeeded Post([FromBody] OutboundOrderRequestModel request)
        {
            Log.Info(String.Format("Processing outbound order: {0}", request));

            var gtins = new List <String>();

            foreach (var orderLine in request.OrderLines)
            {
                if (gtins.Contains(orderLine.gtin))
                {
                    throw new ValidationException(String.Format("Outbound order request contains duplicate product gtin: {0}", orderLine.gtin));
                }
                gtins.Add(orderLine.gtin);
            }

            var productDataModels = _productRepository.GetProductsByGtin(gtins);
            var products          = productDataModels.ToDictionary(p => p.Gtin, p => new Product(p));

            var lineItems  = new List <StockAlteration>();
            var productIds = new List <int>();
            var errors     = new List <string>();

            foreach (var orderLine in request.OrderLines)
            {
                if (!products.ContainsKey(orderLine.gtin))
                {
                    errors.Add(string.Format("Unknown product gtin: {0}", orderLine.gtin));
                }
                else
                {
                    var product = products[orderLine.gtin];
                    lineItems.Add(new StockAlteration(product.Id, orderLine.quantity));
                    productIds.Add(product.Id);
                }
            }

            if (errors.Count > 0)
            {
                throw new NoSuchEntityException(string.Join("; ", errors));
            }

            var stock = _stockRepository.GetStockByWarehouseAndProductIds(request.WarehouseId, productIds);

            var orderLines = request.OrderLines.ToList();

            errors = new List <string>();

            for (int i = 0; i < lineItems.Count; i++)
            {
                var lineItem  = lineItems[i];
                var orderLine = orderLines[i];

                if (!stock.ContainsKey(lineItem.ProductId))
                {
                    errors.Add(string.Format("Product: {0}, no stock held", orderLine.gtin));
                    continue;
                }

                var item = stock[lineItem.ProductId];
                if (lineItem.Quantity > item.held)
                {
                    errors.Add(
                        string.Format("Product: {0}, stock held: {1}, stock to remove: {2}", orderLine.gtin, item.held,
                                      lineItem.Quantity));
                }
            }

            if (errors.Count > 0)
            {
                throw new InsufficientStockException(string.Join("; ", errors));
            }

            _stockRepository.RemoveStock(request.WarehouseId, lineItems);

            double totalWeight = 0;

            for (var i = 0; i < orderLines.Count; i++)
            {
                totalWeight += orderLines[i].quantity * products[orderLines[i].gtin].Weight;
            }
            var numTrucks = Math.Ceiling((totalWeight / 1000) / 2000);

            return(new TrucksNeeded {
                trucksNeeded = Convert.ToInt32(numTrucks)
            });
        }
        public IEnumerable <Truck> Post([FromBody] OutboundOrderRequestModel request)
        {
            log.Info(String.Format("Processing outbound order: {0}", request));

            var products   = GetProductsFromRequest(request);
            var lineItems  = new List <StockAlteration>();
            var productIds = new List <int>();

            var errors = new List <string>();

            foreach (var orderLine in request.OrderLines)
            {
                if (!products.ContainsKey(orderLine.gtin))
                {
                    errors.Add(string.Format("Unknown product gtin: {0}", orderLine.gtin));
                }
                else
                {
                    var product = products[orderLine.gtin];
                    lineItems.Add(new StockAlteration(product.Id, orderLine.quantity));
                    productIds.Add(product.Id);
                }
            }

            if (errors.Count > 0)
            {
                throw new NoSuchEntityException(string.Join("; ", errors));
            }

            var stock = stockRepository.GetStockByWarehouseAndProductIds(request.WarehouseId, productIds);

            var orderLines = request.OrderLines.ToList();

            errors = new List <string>();

            for (var i = 0; i < lineItems.Count; i++)
            {
                var lineItem  = lineItems[i];
                var orderLine = orderLines[i];

                if (!stock.ContainsKey(lineItem.ProductId))
                {
                    errors.Add(string.Format("Product: {0}, no stock held", orderLine.gtin));
                    continue;
                }

                var item = stock[lineItem.ProductId];
                if (lineItem.Quantity > item.held)
                {
                    errors.Add(
                        string.Format("Product: {0}, stock held: {1}, stock to remove: {2}", orderLine.gtin, item.held,
                                      lineItem.Quantity));
                }
            }

            if (errors.Count > 0)
            {
                throw new InsufficientStockException(string.Join("; ", errors));
            }

            stockRepository.RemoveStock(request.WarehouseId, lineItems);

            var trucks = trucksService.GetTrucksForOrder(lineItems);

            return(trucks);
        }