コード例 #1
0
        public OrderTotal CalculateTotal(Guid workshopId, ICollection <AnchorQuantity> seatItems)
        {
            using (var scope = factory.CreateReadOnly())
            {
                var seatTypes = workshopDao.GetPublishedAnchorTypes(workshopId);
                var lineItems = new List <OrderLine>();
                foreach (var item in seatItems)
                {
                    var seatType = seatTypes.FirstOrDefault(x => x.ID == item.AnchorType);
                    if (seatType == null)
                    {
                        throw new InvalidDataException(string.Format(CultureInfo.InvariantCulture, "Invalid anchor type ID '{0}' for workshop with ID '{1}'", item.AnchorType, workshopId));
                    }

                    lineItems.Add(new SeatOrderLine {
                        SeatType = item.AnchorType, Quantity = item.Quantity, UnitPrice = seatType.Price, LineTotal = Math.Round(seatType.Price * item.Quantity, 2)
                    });
                }

                return(new OrderTotal
                {
                    Total = lineItems.Sum(x => x.LineTotal),
                    Lines = lineItems
                });
            }
        }