public void Run(SellItemsRequest request)
        {
            Order order = new Order();

            order.SetStatus(OrderStatus.Created);
            order.SetItems(new List <OrderItem>());
            order.SetCurrency("EUR");
            order.SetTotal((decimal)0.0);
            order.SetTax((decimal)0.0);

            int numberOfFoodItems = 0;

            foreach (SellItemRequest itemRequest in request.GetRequests())
            {
                Product product = productCatalog.GetByName(itemRequest.GetProductName());

                if (product == null)
                {
                    throw new UnknownProductException();
                }
                else
                {
                    decimal unitaryTax         = Math.Round((product.GetPrice() / 100) * (product.GetCategory().GetTaxPercentage()), 2, MidpointRounding.AwayFromZero);
                    decimal unitaryTaxedAmount = Math.Round(product.GetPrice() + unitaryTax, 2, MidpointRounding.AwayFromZero);
                    decimal taxedAmount        = Math.Round(unitaryTaxedAmount * itemRequest.GetQuantity(), 2, MidpointRounding.AwayFromZero);
                    decimal taxAmount          = Math.Round(unitaryTax * itemRequest.GetQuantity(), 2, MidpointRounding.AwayFromZero);

                    OrderItem orderItem = new OrderItem();
                    orderItem.SetProduct(product);
                    orderItem.SetQuantity(itemRequest.GetQuantity());
                    orderItem.SetTax(taxAmount);
                    orderItem.SetTaxedAmount(taxedAmount);
                    order.GetItems().Add(orderItem);

                    foreach (OrderItem item in order.GetItems())
                    {
                        if (item.GetProduct().GetCategory().GetName().Equals("food"))
                        {
                            numberOfFoodItems += item.getQuantity();
                        }
                    }

                    if (numberOfFoodItems > 100)
                    {
                        throw new MaximumNumberOfFoodItemsExceeded();
                    }

                    order.SetTotal(order.GetTotal() + taxedAmount);
                    order.SetTax(order.GetTax() + taxAmount);
                }
            }

            orderRepository.Save(order);
        }
示例#2
0
        public void Run(SellItemsRequest request)
        {
            var order = new Order();

            foreach (var itemRequest in request.Requests)
            {
                var product = productCatalog.GetByName(itemRequest.ProductName);
                order.Add(product, itemRequest.Quantity);
            }

            orderRepository.Save(order);
        }
示例#3
0
        public void Run(SellItemsRequest request)
        {
            var order = new Order
            {
                Status   = OrderStatus.Created,
                Items    = new List <OrderItem>(),
                Currency = "EUR",
                Total    = 0.0m,
                Tax      = 0.0m
            };

            foreach (var itemRequest in request.Requests)
            {
                var product = productCatalog.GetByName(itemRequest.ProductName);
                if (product == null)
                {
                    throw new UnknownProductException();
                }
                else

                {
                    var unitaryTax = Math.Round(product.Price / 100 * product.Category.TaxPercentage, 2,
                                                MidpointRounding.AwayFromZero);

                    var unitaryTaxAmount = Math.Round(product.Price + unitaryTax, 2, MidpointRounding.AwayFromZero);

                    var taxedAmount = Math.Round(unitaryTaxAmount * itemRequest.Quantity, 2,
                                                 MidpointRounding.AwayFromZero);

                    var taxAmount = Math.Round(unitaryTax * itemRequest.Quantity, 2, MidpointRounding.AwayFromZero);

                    var orderItem = new OrderItem
                    {
                        Product     = product,
                        Quantity    = itemRequest.Quantity,
                        Tax         = taxAmount,
                        TaxedAmount = taxedAmount
                    };

                    order.Items.Add(orderItem);
                    order.Total += taxedAmount;
                    order.Tax   += taxAmount;
                }
            }
            orderRepository.Save(order);
        }