public void Handle(SellItemsRequest request) { var order = new Order { Status = OrderStatus.CREATED, Items = new List <OrderItem>(), Currency = "EUR", Total = 0, Tax = 0 }; foreach (var item in request.Items) { var product = _productCatalog.GetProductByName(item.ProductName); if (product == null) { throw new UnknownProductException(); } var unitaryTax = Math.Round(product.Price / 100 * product.Category.TaxPercentage, 2, MidpointRounding.AwayFromZero); var taxAmount = Math.Round(unitaryTax * item.Quantity, 2, MidpointRounding.AwayFromZero); var unitaryTaxedAmount = product.Price + unitaryTax; var taxedAmount = Math.Round(unitaryTaxedAmount * item.Quantity, 2, MidpointRounding.AwayFromZero); var orderItem = new OrderItem { Product = product, Quantity = item.Quantity, Tax = taxAmount, TaxedAmount = taxedAmount }; order.Items.Add(orderItem); order.Total += taxedAmount; order.Tax += taxAmount; } _orderRepository.Save(order); }
public OrderItem MapOrderItem(SellItemRequest item) { var product = _productCatalog.GetProductByName(item.ProductName); return(new OrderItem(product, item.Quantity)); }