public void SendOrderConfirmationEmail(Order order)
 {
     // TODO: Mails are sent synchronously
     // this probably still should be refactored to use a real bg process
     _orderConfirmationMailerController.OrderConfirmationEmail(order).Deliver();
     _orderNotificationMailerController.OrderNotificationEmail(order).Deliver();
 }
 public void ApplyDiscountCode(Order order, string code)
 {
     var discountCode = _discountCodeQueryService.FindByCode(code);
     var applier = new DiscountCodeApplier(discountCode);
     AddAdjustment(order, applier.DiscountAdjustment(order.Total));
     OrderCalculator.Calculate(order);
     _orderRepository.Update(order);
 }
        public EmailResult OrderNotificationEmail(Order order)
        {
            var departure = order.ProductVariants.First().ProductVariant.Product.Departure;
            var tour = departure.Tour;
            var model = new OrderConfirmationEmail
              {
                  Order = order,
                  Tour = tour,
                  Departure = departure
              };

            To.Add(_orderNotificationEmailAddress);
            // TODO: from address should be configurable somewhere
            From = "*****@*****.**";
            Subject = string.Format("brite spokes order notification for Trip number: {0}", order.OrderNumber);
            return Email("OrderNotificationEmail", model);
        }
        public OrderSummary OrderSummary(Order order)
        {
            var orderSummary = new OrderSummary
              {
            Id = order.Id,
            Order = order,
            OrderVariants = new Dictionary<string, int>()
              };

              var departures = new List<Departure>();
              var tours = new List<Tour>();

              foreach (var orderProductVariant in order.ProductVariants)
              {
            var productVariant = orderProductVariant.ProductVariant;
            if (productVariant != null)
            {
              orderSummary.OrderVariants.Add(productVariant.Name, orderProductVariant.Quantity);
              var product = productVariant.Product;
              if (product != null)
              {
            var departure = product.Departure;
            if (departure != null)
            {
              departures.Add(departure);
              var tour = departure.Tour;
              if (tour != null)
                tours.Add(tour);
            }
              }
            }
              }

              orderSummary.DiscountCodes = (from discountAdjustment in order.Adjustments.OfType<DiscountAdjustment>()
            let codeId = discountAdjustment.DiscountCodeId
            where codeId != null
            where codeId.HasValue
            select codeId.Value
            into discountCodeId
            select _discountCodeRepo.Find(discountCodeId)).ToArray();
              orderSummary.Departures = departures.ToArray();
              orderSummary.Tours = tours.ToArray();
              return orderSummary;
        }
        public BillingDetails BuildBillingDetails(Order order, User user = null)
        {
            var billingOverview = BuildBillingOverview(order);

            var billingDetails = new BillingDetails
              {
                  OrderId = order.Id,
                  OrderNumber = order.OrderNumber,
                  DiscountCodes = DiscountCodesForOrder(order).Select(d => d.LowerCode).ToArray(),
                  UserId = order.UserId,
                  CountryId = 226, // TODO: hardcoded country for now
                  BillingOverview = billingOverview
              };

            if (user != null)
            {
                billingDetails.FirstName = user.FirstName;
                billingDetails.LastName = user.LastName;
                billingDetails.Email = user.Email;

                var lastOrder = LastOrderForUser(user);
                if (lastOrder != null)
                {
                    var billingAddress = lastOrder.BillingAddress;
                    if (billingAddress != null)
                    {
                        billingDetails.Address1 = billingAddress.Address1;
                        billingDetails.Address2 = billingAddress.Address2;
                        billingDetails.City = billingAddress.City;
                        billingDetails.StateOrProvince = billingAddress.StateOrProvince;
                        billingDetails.ZipCode = billingAddress.ZipCode;
                    }
                }
            }

            return billingDetails;
        }
 public static void Calculate(Order order)
 {
     var c = new OrderCalculator(order);
       c.Calculate();
 }
 public OrderCalculator(Order order)
 {
     _order = order;
 }
        private void AddProductVariant(Order order, ShoppingCartItem shoppingCartItem)
        {
            if (order.ProductVariants == null)
                order.ProductVariants = new List<OrderProductVariant>();

            order.ProductVariants.Add(new OrderProductVariant
              {
                  ProductVariant = shoppingCartItem.ProductVariant,
                  Quantity = shoppingCartItem.Quantity,
                  UnitPrice = shoppingCartItem.ProductVariant.Price
              });
        }
 private static List<Traveler> BuildTravelerList(Order order)
 {
     var travelerCount = order.ProductVariants.Sum(v => NumberOfTravelers.Compute(v));
     var travelers = new List<Traveler>(travelerCount);
     for (var i = 0; i < travelerCount; i++)
         travelers.Add(new Traveler { EmailItinerary = true, ConfirmationNumber = GenerateOrderNumber() });
     return travelers;
 }
 private void AddAdjustment(Order order, Adjustment adjustment)
 {
     if (order.Adjustments == null)
         order.Adjustments = new List<Adjustment>();
     order.Adjustments.Add(adjustment);
 }
 public Order FailOrder(Order order, string message)
 {
     order.LastFailureMessage = message;
     order.FailedAt = DateTime.UtcNow;
     order.OrderStatus = StatusFailed();
     _orderRepository.Update(order);
     return order;
 }
 public IQueryable<DiscountCode> DiscountCodesForOrder(Order order)
 {
     var discountCodes = new List<DiscountCode>();
     var adjustments = order.Adjustments;
     if (adjustments != null && adjustments.Count > 0)
     {
         foreach (var adjustment in adjustments)
         {
             var discountAdjustment = adjustment as DiscountAdjustment;
             if (discountAdjustment != null)
             {
                 if (discountAdjustment.DiscountCodeId.HasValue)
                     discountCodes.Add(_discountCodeQueryService.Find(discountAdjustment.DiscountCodeId.Value));
             }
         }
     }
     return discountCodes.AsQueryable();
 }
        public Order CreateOrder(int userId, List<ShoppingCartItem> shoppingCartItems)
        {
            var user = _userService.Find(userId);
            var order = new Order
              {
                  User = user,
                  OrderStatus = StatusPending(),
                  OrderNumber = GenerateOrderNumber()
              };

            foreach (var shoppingCartItem in shoppingCartItems)
                AddProductVariant(order, shoppingCartItem);

            order.Travelers = BuildTravelerList(order);
            OrderCalculator.Calculate(order);
            _orderRepository.Add(order);

            return order;
        }
 public Order CompleteOrder(Order order, string chargeId)
 {
     order.CompletedAt = DateTime.UtcNow;
     order.FailedAt = null;
     order.LastFailureMessage = null;
     order.ChargeId = chargeId;
     order.OrderStatus = StatusComplete();
     _orderRepository.Update(order);
     return order;
 }
        public BillingOverview BuildBillingOverview(Order order)
        {
            var departure = order.ProductVariants.First().ProductVariant.Product.Departure;
            var rooms =
              order.ProductVariants.Select(
                v =>
                new BillingRoomSummary
                  {
                      Quantity = v.Quantity,
                      DisplayName = v.ProductVariant.DisplayName,
                      PluralDisplayName = v.ProductVariant.PluralDisplayName
                  }).ToList();

            return new BillingOverview
              {
                  TourName = departure.Tour.Name,
                  DepartureDate = departure.DepartureDate,
                  ReturnDate = departure.ReturnDate,
                  DepartureCode = departure.Code,
                  NumberOfRooms = rooms.Sum(summary => summary.Quantity),
                  Total = order.Total,
                  ItemTotal = order.ItemTotal,
                  AdjustmentTotal = order.AdjustmentTotal,
                  Rooms = rooms,
                  Countries = Countries(),
                  States = States()
              };
        }