public EmailResult GiftOrderNotificationEmail(GiftOrder giftorder)
        {
            var model = new GiftOrderConfirmationEmail
            {
            GiftOrder = giftorder
            };

            To.Add(giftorder.User.Email);
            // TODO: from address should be configurable somewhere
            From = "*****@*****.**";
            Subject = string.Format("brite spokes order notification for Order number: {0}", giftorder.OrderNumber);
            return Email("GiftOrderNotificationEmail", model);
        }
        public void SendGiftOrderConfirmationEmail(GiftOrder order)
        {
            // TODO: Mails are sent synchronously
            // this probably still should be refactored to use a real bg process
            _giftorderConfirmationMailerController.GiftOrderConfirmationEmail(order).Deliver();
            _giftorderNotificationMailerController.GiftOrderNotificationEmail(order).Deliver();

            for (int i = 0; i < order.GiftOrderDetail.Count; i++)
            {
                _giftorderConfirmationMailerController.GiftOrderConfirmationEmailToRecipient(order,i).Deliver();

            }
        }
        public BillingOverview BuildBillingOverview(GiftOrder order)
        {
            var giftCard = order.GiftCard;

              return new BillingOverview
              {
              GiftCardName = giftCard.Name,
              NumberOfCards = order.Quantity,
              Total = order.Total,
              ItemTotal = order.ItemTotal,
              Countries = Countries(),
              States = States()
              };
        }
        public EmailResult GiftOrderConfirmationEmailToRecipient(GiftOrder giftorder,int index)
        {
            //var departure = order.ProductVariants.First().ProductVariant.Product.Departure;
            // var tour = departure.Tour;
            var model = new GiftOrderConfirmationEmail
            {
            GiftOrder = giftorder

            };

            To.Add(giftorder.GiftOrderDetail[index].RecipientEmail);
            // TODO: from address should be configurable somewhere
            From = "*****@*****.**";

            Subject = string.Format("brite spokes gift gift code: {0}", giftorder.GiftOrderDetail[index].RecipientGiftCode);

            return Email("RecipientGiftCodeEmail", model);
        }
        public EmailResult GiftOrderConfirmationEmail(GiftOrder giftorder)
        {
            //var departure = order.ProductVariants.First().ProductVariant.Product.Departure;
               // var tour = departure.Tour;
            var model = new GiftOrderConfirmationEmail
            {
            GiftOrder = giftorder

            };

            To.Add(giftorder.User.Email);
            // TODO: from address should be configurable somewhere
            From = "*****@*****.**";
            //foreach (var item in order.GiftOrderDetail)
            //{
            //    BCC.Add(item.RecipientEmail);
            //    Subject = string.Format("brite spokes order confirmation Gift Code: {0}", item.RecipientGiftCode);
            //}
            Subject = string.Format("brite spokes gift order confirmation number: {0}", giftorder.OrderNumber);

            return Email("GiftOrderConfirmationEmail", model);
        }
        public BillingDetails BuildBillingDetails(GiftOrder order, User user = null)
        {
            var billingOverview = BuildBillingOverview(order);

              var billingDetails = new BillingDetails
              {
              GiftOrderId = order.Id,
              GiftOrderNumber = order.OrderNumber,
              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 List<GiftOrder> GiftOrderByUsedstatus(string orderStatus)
        {
            var results = _giftorderRepo.All
             .Join(_giftorderdetailRepo.All, d => d.Id, o => o.GiftOrderId, (d, o) => new { d, o})
                .Join(_giftcardsummary.All, t => t.o.Id, c => c.GiftOrderDetailId, (t, c) => new { t.d, t.o, c })
                .Join(_OrderRepo.All, p => p.c.OrderId, s => s.Id, (p, s) => new {p.c,p.d,p.o,s })
                .Where(a => a.s.OrderStatusId == 3)
                .Where(a => a.d.OrderStatusId == 3);

            List<GiftOrder> orderlist = new List<GiftOrder>();
            foreach (var p in results)
            {
                GiftOrder ord = new
               GiftOrder()
                {
                    User = p.s.User,
                    UserId = p.s.UserId,
                    GiftCardId = p.d.GiftCardId,
                    BillingAddressId = p.d.BillingAddressId,
                    ShippingAddressId = p.d.ShippingAddressId,
                    OrderStatusId = p.s.OrderStatusId,
                    OrderNumber = p.s.OrderNumber,
                    SpecialInstructions = p.s.SpecialInstructions,
                    Quantity = p.d.Quantity,
                    Total = p.d.Total,
                    ItemTotal = p.d.ItemTotal,
                    AdjustmentTotal = p.d.AdjustmentTotal,
                    ChargeId = p.d.ChargeId,
                    IsDeleted = p.d.IsDeleted,
                    CreatedAt = p.d.CreatedAt,
                    UpdatedAt = p.d.UpdatedAt,
                    CompletedAt = p.d.CompletedAt,
                    FailedAt = p.d.FailedAt,
                    LastFailureMessage = p.d.LastFailureMessage,
                    OrderStatus = p.s.OrderStatus,
                    Id = p.d.Id
                };
                ord.OrderStatus.Name = "Used";
                orderlist.Add(ord);
            }
            return orderlist;
        }
        public GiftOrderSummary GiftOrderSummary(GiftOrder giftorder)
        {
            List<GiftOrderDetails> orderdetails = new List<GiftOrderDetails>();

            var resultsUsed = _giftorderdetailRepo.All
             .Join(_giftorderRepo.All, d => d.GiftOrderId, o => o.Id, (d, o) => new { d, o })
                .Join(_giftcardsummary.All, t => t.d.Id, c => c.GiftOrderDetailId, (t, c) => new { t.d, t.o, c })
                .Join(_OrderRepo.All, p => p.c.OrderId, s => s.Id, (p, s) => new { p.c, p.d, p.o, s })
                .Where(a => a.o.OrderStatusId == 3)
                .Where(a => a.o.Id == giftorder.Id)
                .Where(a => a.s.OrderStatusId == 3)
                .Select(a => new GiftOrderDetails
                {
                    Id = a.d.Id,

                    Amount = a.d.Amount,

                    RecipientEmail = a.d.RecipientEmail,

                    YourName = a.d.YourName,

                    Message = a.d.Message,

                    RecipientGiftCode = a.d.RecipientGiftCode,

                    StatusName = "Used"
                }).ToList();

            var resultsUnUsed1 = _giftorderdetailRepo.All
             .Join(_giftorderRepo.All, d => d.GiftOrderId, o => o.Id, (d, o) => new { d, o })
                .Join(_giftcardsummary.All, t => t.d.Id, c => c.GiftOrderDetailId, (t, c) => new { t.d, t.o, c })
                .Join(_OrderRepo.All, p => p.c.OrderId, s => s.Id, (p, s) => new { p.c, p.d, p.o, s })
                .Where(a => a.o.OrderStatusId == 3)
                .Where(a => a.o.Id == giftorder.Id)
                .Where(a => a.s.OrderStatusId != 3)
                .Select(a => new GiftOrderDetails
                {
                    Id = a.d.Id,

                    Amount = a.d.Amount,

                    RecipientEmail = a.d.RecipientEmail,

                    YourName = a.d.YourName,

                    Message = a.d.Message,

                    RecipientGiftCode = a.d.RecipientGiftCode,

                    StatusName = "Unused"
                }).ToList();

            var resultsUnUsed2 = _giftorderdetailRepo.All
             .Join(_giftorderRepo.All, d => d.GiftOrderId, o => o.Id, (d, o) => new { d, o })
                .Where(a => a.o.OrderStatusId == 3)
                .Where(a => a.o.Id == giftorder.Id)
                .Select(a => new GiftOrderDetails
                {
                    Id = a.d.Id,

                    Amount = a.d.Amount,

                    RecipientEmail = a.d.RecipientEmail,

                    YourName = a.d.YourName,

                    Message = a.d.Message,

                    RecipientGiftCode = a.d.RecipientGiftCode,

                    StatusName = "Unused"
                }).ToList();

            foreach (var item in resultsUsed)
            {
                resultsUnUsed2.Remove(resultsUnUsed2.Single(s => s.Id == item.Id));

            }

            foreach (var item in resultsUnUsed1)
            {
                resultsUnUsed2.Remove(resultsUnUsed2.Single(s => s.Id == item.Id));

            }

            orderdetails = orderdetails.Concat(resultsUsed).ToList();
            orderdetails = orderdetails.Concat(resultsUnUsed1).ToList();
            orderdetails = orderdetails.Concat(resultsUnUsed2).ToList();

            var giftorderSummary = new GiftOrderSummary
            {
                Id = giftorder.Id,
                GiftOrder = giftorder,
                GiftOrderDetails = orderdetails

            };
            return giftorderSummary;
        }
        public ActionResult ConfirmGiftOrders(GiftOrder giftOrder)
        {
            try
            {
                if (Request.IsAjaxRequest())
                {
                    //creating pending order for gift cards
                    giftOrder.UserId = UserContext.UserId;
                    var order = _giftCardOrderService.CreateOrder(giftOrder);

                    var confirmGiftCards = Mapper.Map<ConfirmGiftCards>(order);
                    for (var i = 0; i < confirmGiftCards.GiftOrderDetail.Count; i++)
                        confirmGiftCards.GiftOrderDetail[i].Index = i + 1;

                    var GiftOrderDetails = confirmGiftCards.GiftOrderDetail;

                    return PartialView("_GiftOrderDetails", confirmGiftCards);
                }
                return PartialView("_GiftOrderDetails");
            }
            catch (Exception e)
            {
                Elmah.ErrorSignal.FromCurrentContext().Raise(e);
                return PartialView("_GiftOrderDetails");
            }
        }
 public GiftOrder CompleteOrder(GiftOrder order, string chargeId)
 {
     order.CompletedAt = DateTime.UtcNow;
       order.FailedAt = null;
       order.LastFailureMessage = null;
       order.ChargeId = chargeId;
       order.OrderStatus = StatusComplete();
       _giftCardOrderRepository.Update(order);
       return order;
 }
 private static List<GiftOrderDetail> BuildGiftOrderDetailList(GiftOrder order)
 {
     var giftCardCount = order.Quantity;
       var giftCards = new List<GiftOrderDetail>(giftCardCount);
       for (var i = 0; i < giftCardCount; i++)
       giftCards.Add(new GiftOrderDetail { RecipientGiftCode = GenerateOrderNumber() });
       return giftCards;
 }
 public GiftOrder FailOrder(GiftOrder order, string message)
 {
     order.LastFailureMessage = message;
       order.FailedAt = DateTime.UtcNow;
       order.OrderStatus = StatusFailed();
       _giftCardOrderRepository.Update(order);
       return order;
 }
        public GiftOrder CreateOrder(GiftOrder giftOrder)
        {
            var user = _userService.Find(giftOrder.UserId);
              var order = new GiftOrder
              {
              User = user,
              OrderStatus = StatusPending(),
              OrderNumber = GenerateOrderNumber(),
              Quantity = giftOrder.Quantity,
              GiftCardId=giftOrder.GiftCardId
              };

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

              order.GiftOrderDetail = BuildGiftOrderDetailList(order);
             // OrderCalculator.Calculate(order);
              _giftCardOrderRepository.Add(order);

              return order;
        }