Beispiel #1
0
        public NotificationResultViewModel SendUpcomingBoxNotifications()
        {
            try
            {
                int notificationsSent = 0;

                Stopwatch sw = new Stopwatch();
                sw.Start();

                using (ManBoxEntities ent = new ManBoxEntities())
                {
                    var upcomingDeliveries = from sd in ent.SubscriptionDeliveries
                                             where sd.DeliveryStateCV == CodeValues.DeliveryState.Pending &&
                                             sd.Subscription.GiftId == null &&
                                             sd.Subscription.SubscriptionStateCV == CodeValues.SubscriptionState.Subscribed &&
                                             EntityFunctions.AddDays(sd.DeliveryDate.Value, -daysBefore).Value < DateTime.Now &&
                                             !sd.SubscriptionDeliveryMessages.Any(m => m.DeliveryMessageTypeCV == CodeValues.DeliveryMessageType.Upcoming)
                                             select new
                    {
                        SubscriptionDelivery = sd,
                        Name       = sd.Subscription.User.FirstName,
                        Token      = sd.Subscription.Token,
                        Email      = sd.Subscription.User.Email,
                        LangIso    = sd.Subscription.User.Language.IsoCode,
                        CountryIso = sd.Subscription.User.Country.IsoCode,
                        Address    = sd.Subscription.Address,
                        Products   = from m in sd.SubscriptionDeliveryModels
                                     select new
                        {
                            ProductName = (from tt in ent.TranslationTexts where tt.TranslationId == m.Model.Product.TitleTrId && tt.LanguageId == sd.Subscription.User.LanguageId select tt.Text).FirstOrDefault(),
                            ModelName   = m.Model.Name,
                            Quantity    = m.Quantity,
                            Price       = m.Model.ShopPrice,
                            TotalPrice  = m.Model.ShopPrice * m.Quantity
                        }
                    };

                    var notificationMails = new List <UpcomingBoxNotificationMail>();

                    foreach (var del in upcomingDeliveries)
                    {
                        var itemsTotal   = Utilities.GetDeliveryTotal(del.SubscriptionDelivery);
                        var shippingFee  = Utilities.CalculateShippingFee(itemsTotal);
                        var couponAmount = Utilities.CalculateCouponAmount(itemsTotal, del.SubscriptionDelivery);
                        var total        = itemsTotal + shippingFee - couponAmount;
                        var couponLabel  = Utilities.GetCouponLabel(del.SubscriptionDelivery.Coupon);

                        var products         = new List <MailProduct>();
                        var notificationMail = new UpcomingBoxNotificationMail()
                        {
                            Name        = del.Name,
                            Email       = del.Email,
                            Token       = del.Token,
                            LanguageIso = del.LangIso,
                            CountryIso  = del.CountryIso,
                            Address     = new MailAddress()
                            {
                                City       = del.Address.City,
                                Street     = del.Address.Street,
                                Province   = del.Address.Province,
                                PostalCode = del.Address.PostalCode
                            },
                            SubTotal     = itemsTotal,
                            Total        = total,
                            ShippingFee  = shippingFee,
                            CouponAmount = couponAmount,
                            CouponLabel  = couponLabel
                        };

                        foreach (var prod in del.Products)
                        {
                            products.Add(new MailProduct()
                            {
                                ModelName   = prod.ModelName,
                                ProductName = prod.ProductName,
                                Price       = prod.Price,
                                Quantity    = prod.Quantity,
                                TotalPrice  = prod.TotalPrice
                            });
                        }

                        notificationMail.Products = products;

                        SendMail(notificationMail);
                        ShipmentRepository.StoreDeliveryMessage(del.SubscriptionDelivery, CodeValues.DeliveryMessageType.Upcoming);

                        notificationsSent++;
                    }

                    sw.Stop();
                    ent.SaveChanges();
                }

                return(new NotificationResultViewModel()
                {
                    NotificationsSent = notificationsSent,
                    EndedDateTime = DateTime.Now,
                    ElapsedTime = sw.Elapsed,
                    MessageType = CodeValues.DeliveryMessageType.Upcoming
                });
            }
            catch (Exception e)
            {
                logger.Log(e);
                throw;
            }
        }