Esempio n. 1
0
        public async Task <GetOrderDetailsResponse> Handle(GetOrderDetailsRequest request, CancellationToken cancellationToken = default)
        {
            try
            {
                var orderNumber = request.OrderNumber;
                var order       = await _orderRepository.GetByOrderNumberAsync(orderNumber, cancellationToken);

                var requiredBinWidth = await _packageService.CalculateRequiredBinWidthByOrderAsync(order, cancellationToken);

                var productItemsFromRepository = await _productItemRepository.GetListByIdsAsync(order.OrderItems.Select(x => x.ProductId), cancellationToken);

                var productItems = new List <ProductItemDetailsResponse>();
                foreach (var item in order.GetOrderItemsGroupedByProductId())
                {
                    var productItem      = productItemsFromRepository.FirstOrDefault(x => x.Id == item.ProductId);
                    var productNumber    = productItem.ProductNumber;
                    var width            = productItem.ProductType.Width;
                    var maxStackQuantity = productItem.ProductType.MaxStackQuantity;
                    var type             = productItem.ProductType.Type;
                    var quantity         = item.Quantity;
                    productItems.Add(new ProductItemDetailsResponse(productNumber, width, maxStackQuantity, quantity, type));
                }

                return(new GetOrderDetailsResponse(requiredBinWidth, orderNumber, productItems));
            }
            catch (OrderNotFoundRepositoryException ex)
            {
                throw new EntityNotFoundServiceException($"Order {request.OrderNumber} not found");
            }
        }
Esempio n. 2
0
        public async Task <int> CalculateRequiredBinWidthByOrderAsync(Order order, CancellationToken cancellationToken = default)
        {
            int minWidth     = 0;
            var items        = order.GetOrderItemsGroupedByProductId();
            var productItems = await _productItemRepository.GetListByIdsAsync(items.Select(x => x.ProductId), cancellationToken);

            foreach (var item in items)
            {
                var productItem    = productItems.FirstOrDefault(x => x.Id == item.ProductId);
                var productWidth   = productItem.ProductType.Width;
                var maxStack       = productItem.ProductType.MaxStackQuantity;
                var quantity       = item.Quantity;
                var occupiedSpaces = Math.Ceiling(decimal.Divide(quantity, maxStack));

                minWidth += ((int)productWidth * (int)occupiedSpaces);
            }
            return(minWidth);
        }