public bool CheckExists(NotificationSearch NotificationSearch)
 {
     try
     {
         var loginModel = _NotificationService.CheckExists(NotificationSearch);
         if (loginModel != null)
         {
             return loginModel;
         }
         else
         {
             return false;
         }
     }
     catch (Exception ex)
     {
         return false;
     }
 }
 public dynamic GetAllNotification(NotificationSearch NotificationSearch)
 {
     try
     {
         dynamic NotificationList;
         NotificationList = _NotificationService.GetAllCustomer(NotificationSearch);
         return NotificationList;
     }
     catch (Exception e)
     {
         return null;
     }
 }
        public void SendMessage(NotificationSearch NotificationSearch, int approvedCount)
        {
            var testMode = Convert.ToBoolean(System.Configuration.ConfigurationManager.AppSettings["TestMode"]);
            string pwd = System.Configuration.ConfigurationManager.AppSettings["Password"];
            if (NotificationSearch.Password.ToLower() == pwd.ToLower())
            {
                var NotificationList = _NotificationService.GetCustomerSendMessage(NotificationSearch, approvedCount)
                    .Select(n => new
                    {
                        n.MobileNo,
                        n.TourOpCode,
                        n.Email,
                        n.Seed,
                        n.FirstName,
                        n.Surname
                    }).Distinct();

                if (NotificationList != null)
                {
                    foreach (var item in NotificationList)
                    {
                        if (!string.IsNullOrEmpty(item.Email))
                        {
                            try
                            {
                                if (!string.IsNullOrEmpty(NotificationSearch.MessageEmail))
                                {
                                    var response = NotificationService.SendEmail("*****@*****.**", item.Email, "Subject", NotificationSearch.MessageEmail);
                                    NotificationService.NotificationLogAdd(1, item.Email, item.FirstName, item.Surname, "*****@*****.**", NotificationSearch.MessageEmail, response, response, item.Seed, testMode);
                                }
                                else
                                {
                                    var response = NotificationService.SendEmail("*****@*****.**", item.Email, "Subject", NotificationSearch.Message);
                                    NotificationService.NotificationLogAdd(1, item.Email, item.FirstName, item.Surname, "*****@*****.**", NotificationSearch.Message, response, response, item.Seed, testMode);
                                }
                            }
                            catch (Exception e)
                            {
                                NotificationService.NotificationLogAdd(0, item.Email, item.FirstName, item.Surname, "*****@*****.**", NotificationSearch.Message, "Error", e.Message, item.Seed, testMode);
                            }
                        }
                        if (!string.IsNullOrEmpty(item.MobileNo))
                        {
                            try
                            {
                                var numberToSendTo = item.MobileNo;
                                var numberFrom = "Hotelplan";
                                var message = NotificationSearch.Message;

                                if (item.MobileNo.Substring(0, 1) == "0")
                                {
                                    numberToSendTo = string.Format("+44{0}", item.MobileNo.Substring(1, item.MobileNo.Length - 1));
                                }

                                numberToSendTo = numberToSendTo.Replace(" ", "");

                                switch (item.TourOpCode)
                                {
                                    case "IN":
                                        numberFrom = "Inghams";
                                        break;
                                    case "IT":
                                        numberFrom = "Inghams";
                                        break;
                                    case "ST":
                                        numberFrom = "Ski Total";
                                        break;
                                    case "ES":
                                        numberFrom = "Esprit Sun";
                                        break;
                                    case "EW":
                                        numberFrom = "Esprit Ski";
                                        break;
                                    case "SL":
                                        numberFrom = "Santa";
                                        break;
                                }

                                if (item.Seed)
                                {
                                    message = string.Format("Seed:{0}", message);
                                }

                                string s = message;
                                int partLength = 160;

                                if (s == null)
                                {
                                    throw new ArgumentNullException("s");
                                }
                                if (partLength <= 0)
                                {
                                    throw new ArgumentException("Part length has to be positive.", "partLength");
                                }

                                for (var i = 0; i < s.Length; i += partLength)
                                {
                                    var msg = s.Substring(i, Math.Min(partLength, s.Length - i));

                                    var notificationResponse = NotificationService.SendSMS(numberFrom, numberToSendTo, msg);
                                    NotificationService.NotificationLogAdd(2, numberToSendTo, item.FirstName, item.Surname, numberFrom, msg, notificationResponse, notificationResponse, item.Seed, testMode);
                                }
                            }
                            catch (Exception e)
                            {
                                NotificationService.NotificationLogAdd(0, item.MobileNo, item.FirstName, item.Surname, "", NotificationSearch.Message, "Error", e.Message, item.Seed, testMode);
                            }
                        }
                    }
                }
            }
        }
 public string CheckPassword(NotificationSearch NotificationSearch)
 {
     string pwd = System.Configuration.ConfigurationManager.AppSettings["Password"];
     if (NotificationSearch.Password.ToLower() == pwd.ToLower() && NotificationSearch.SendCountSMS == NotificationSearch.MobileCount)
     {
         SendMessage(NotificationSearch, NotificationSearch.SendCountSMS);
         //TODO: Handle response and errors
         return "1";
     }
     else if (NotificationSearch.Password.ToLower() != pwd.ToLower())
     {
         return "3";
     }
     else if (NotificationSearch.SendCountSMS != NotificationSearch.MobileCount)
     {
         return "2";
     }
     else
     {
         return "1";
     }
 }
        public IQueryable<NotificationData> NotificationDataList(NotificationSearch NotificationSearch)
        {
            try
            {
                var NotificationDataList = _CustomerService.NotificationData.AsQueryable();
                if (!string.IsNullOrEmpty(NotificationSearch.TourOpCode))
                {
                    NotificationDataList = from e in NotificationDataList where e.TourOpCode.ToString().Trim().ToLower() == NotificationSearch.TourOpCode.ToString().Trim().ToLower() select e;
                }
                if (!string.IsNullOrEmpty(NotificationSearch.DirectOrAgent))
                {
                    NotificationDataList = from e in NotificationDataList where e.DirectOrAgent.ToString().Trim().ToLower() == NotificationSearch.DirectOrAgent.ToString().Trim().ToLower() select e;
                }
                if (NotificationSearch.StartDate != null)
                {
                    NotificationDataList = from e in NotificationDataList where DbFunctions.TruncateTime(e.StartDate) == DbFunctions.TruncateTime(NotificationSearch.StartDate) select e;
                }
                if (!string.IsNullOrEmpty(NotificationSearch.DeparturePoint))
                {
                    NotificationDataList = from e in NotificationDataList where e.DeparturePoint.ToString().Trim().ToLower() == NotificationSearch.DeparturePoint.ToString().Trim().ToLower() select e;
                }
                if (!string.IsNullOrEmpty(NotificationSearch.ArrivalPoint))
                {
                    NotificationDataList = from e in NotificationDataList where e.ArrivalPoint.ToString().Trim().ToLower() == NotificationSearch.ArrivalPoint.ToString().Trim().ToLower() select e;
                }
                if (NotificationSearch.TravelDate != null)
                {
                    NotificationDataList = from e in NotificationDataList where e.TravelDate == NotificationSearch.TravelDate select e;
                }
                if (!string.IsNullOrEmpty(NotificationSearch.TravelDepatureTime))
                {
                    NotificationDataList = from e in NotificationDataList where e.TravelDepatureTime.ToString().Trim().ToLower().Replace(" ", "") == NotificationSearch.TravelDepatureTime.ToString().Trim().ToLower().Replace(" ", "") select e;
                }
                if (!string.IsNullOrEmpty(NotificationSearch.TravelArrivalTime))
                {
                    NotificationDataList = from e in NotificationDataList where e.TravelArrivalTime.ToString().Trim().ToLower().Replace(" ", "") == NotificationSearch.TravelArrivalTime.ToString().Trim().ToLower().Replace(" ", "") select e;
                }
                if (!string.IsNullOrEmpty(NotificationSearch.TravelDirection))
                {
                    NotificationDataList = from e in NotificationDataList where e.TravelDirection.ToString().Trim().ToLower() == NotificationSearch.TravelDirection.ToString().Trim().ToLower() select e;
                }
                if (!string.IsNullOrEmpty(NotificationSearch.TransportCarrier))
                {
                    NotificationDataList = from e in NotificationDataList where e.TransportCarrier.ToString().Trim().ToLower() == NotificationSearch.TransportCarrier.ToString().Trim().ToLower() select e;
                }
                if (!string.IsNullOrEmpty(NotificationSearch.TransportNumber))
                {
                    NotificationDataList = from e in NotificationDataList where e.TransportNumber.ToString().Trim().ToLower() == NotificationSearch.TransportNumber.ToString().Trim().ToLower() select e;
                }
                if (!string.IsNullOrEmpty(NotificationSearch.TransportType))
                {
                    NotificationDataList = from e in NotificationDataList where e.TransportType.ToString().Trim().ToLower() == NotificationSearch.TransportType.ToString().Trim().ToLower() select e;
                }
                if (!string.IsNullOrEmpty(NotificationSearch.TransportChain))
                {
                    NotificationDataList = from e in NotificationDataList where e.TransportChain.ToString().Trim().ToLower() == NotificationSearch.TransportChain.ToString().Trim().ToLower() select e;
                }
                if (!string.IsNullOrEmpty(NotificationSearch.CountryName))
                {
                    NotificationDataList = from e in NotificationDataList where e.ResortCode.Substring(0, 2).Trim().ToLower() == NotificationSearch.CountryName.Trim().ToLower() select e;
                }
                if (!string.IsNullOrEmpty(NotificationSearch.ResortName))
                {
                    NotificationDataList = from e in NotificationDataList where e.AccommodationCode.Substring(0, 2).Trim().ToLower() == NotificationSearch.ResortName.Trim().ToLower() select e;
                }
                if (!string.IsNullOrEmpty(NotificationSearch.AccommodationName))
                {
                    //var AccommodationCode = (from row in _CustomerService.NotificationData where row.AccommodationName.Trim().ToLower().ToString() == NotificationSearch.AccommodationName.Trim().ToLower().ToString() select row.AccommodationCode).FirstOrDefault();
                    NotificationDataList = from e in NotificationDataList where e.AccommodationCode.Trim().ToLower() == NotificationSearch.AccommodationName.Trim().ToLower() select e;
                }

                return NotificationDataList;
            }
            catch (Exception)
            {
                throw;
            }
        }
        public List<NotificationData> GetCustomerSendMessage(NotificationSearch notificationSearch, int approvedCount)
        {
            try
            {
                var notificationDataSeedList = _CustomerService.NotificationDataSeeds.AsQueryable();
                //TODO: Allow seeds against UK deaprture airports
                //if (!string.IsNullOrEmpty(notificationSearch.DeparturePoint))
                //{
                //    notificationDataSeedList = from e in notificationDataSeedList where e.ArrivalPoint.ToString().Trim().ToLower() 
                //                                   == notificationSearch.DeparturePoint.ToString().Trim().ToLower() select e;
                //}
                if (!string.IsNullOrEmpty(notificationSearch.ArrivalPoint))
                {
                    notificationDataSeedList = from e in notificationDataSeedList
                                               where
                                                   (e.ArrivalPoint.ToString().Trim().ToLower() ==
                                                   notificationSearch.ArrivalPoint.ToString().Trim().ToLower())
                                                   || (e.ArrivalPoint.ToString().Trim().ToLower() ==
                                           notificationSearch.DeparturePoint.ToString().Trim().ToLower())
                                               select e;
                }
                if (!string.IsNullOrEmpty(notificationSearch.CountryName))
                {
                    notificationDataSeedList = from e in notificationDataSeedList where e.CountryCode == ((from row in _CustomerService.NotificationDataSeeds where row.CountryName.ToLower().Trim() == notificationSearch.CountryName.ToLower().Trim() select row.CountryCode).FirstOrDefault()) select e;
                }
                if (!string.IsNullOrEmpty(notificationSearch.ResortName))
                {
                    notificationDataSeedList = from e in notificationDataSeedList where e.ResortCode == ((from row in _CustomerService.NotificationDataSeeds where row.CountryName.ToLower().Trim() == notificationSearch.CountryName.ToLower().Trim() select row.CountryCode).FirstOrDefault()) select e;
                }

                var notificationDataList = this.NotificationDataList(notificationSearch);

                var mobileCount = notificationDataList.Count(x => !string.IsNullOrEmpty(x.MobileNo));

                if (mobileCount != approvedCount)
                {
                    return null;
                }

                var customer = (from row in notificationDataList
                                select row).Where(x => !string.IsNullOrEmpty(x.Email) || !string.IsNullOrEmpty(x.MobileNo)).OrderByDescending(x => x.StartDate).ToList();

                foreach (var seed in notificationDataSeedList)
                {
                    customer.Add(new NotificationData()
                    {
                        Title = seed.Title,
                        FirstName = seed.FirstName,
                        Surname = seed.Surname,
                        Email = seed.Email,
                        MobileNo = seed.MobileNo,
                        Seed = true
                    }
                        );
                }

                return customer;

            }
            catch (Exception e)
            {
                return null;
                throw;
            }
        }
        public bool CheckExists(NotificationSearch NotificationSearch)
        {
            var db = new CustomerContext();
            bool flag = false;
            var NotificationDataList = this.NotificationDataList(NotificationSearch);
            try
            {
                foreach (var item in NotificationDataList)
                {
                    if (!string.IsNullOrEmpty(item.Email))
                    {
                        var logemail = db.NotificationLog.Where(x => x.SentTo.Trim().ToLower() == item.Email.Trim().ToLower()).Count();
                        if (logemail > 0)
                        {
                            flag = false;
                        }
                        else
                        {
                            flag = true;
                        }
                    }
                    if (!string.IsNullOrEmpty(item.MobileNo))
                    {
                        var logemail = db.NotificationLog.Where(x => x.SentTo == item.MobileNo).Count();
                        if (logemail > 0)
                        {
                            flag = false;
                        }
                        else
                        {
                            flag = true;
                        }
                    }
                }
            }
            catch (Exception e)
            {

            }
            return flag;
        }
        public dynamic GetAllCustomer(NotificationSearch NotificationSearch)
        {
            var NotificationDataList = this.NotificationDataList(NotificationSearch);

            var EmailCount = NotificationDataList.Where(x => !string.IsNullOrEmpty(x.Email)).Count();
            var MobileCount = NotificationDataList.Where(x => !string.IsNullOrEmpty(x.MobileNo)).Count();

            var notifications = (from n in NotificationDataList
                                 select new
                                 {
                                     n.BookingRef,
                                     n.TourOpCode,
                                     n.PassengerId,
                                     n.Title,
                                     n.FirstName,
                                     n.Surname,
                                     n.MobileNo,
                                     n.Email,
                                     n.DirectOrAgent,
                                     n.StartDate,
                                     n.TravelDate,
                                     n.DeparturePoint,
                                     n.ArrivalPoint,
                                     n.TransportChain,
                                     n.TransportCarrier,
                                     n.TransportNumber
                                 }).ToList();

            var customer = (from row in notifications
                            select new
                            {
                                BookingRef = row.BookingRef,
                                TourOpCode = row.TourOpCode,
                                PassengerId = row.PassengerId,
                                FullName = string.Format("{0}, {1} {2}", row.Surname, row.Title, row.FirstName),
                                MobileNo = row.MobileNo,
                                Email = row.Email,
                                DirectOrAgent = row.DirectOrAgent,
                                StartDate = row.StartDate,
                                TravelDate = row.TravelDate,
                                TransportNumber = row.TransportNumber,
                                FlightChain = string.Format("{0}-{1}-{2}", row.DeparturePoint, row.ArrivalPoint, row.TransportChain),
                                DeparturePoint = row.DeparturePoint,
                                ArrivalPoint = row.ArrivalPoint
                            }).Where(x => !string.IsNullOrEmpty(x.Email) || !string.IsNullOrEmpty(x.MobileNo)).OrderByDescending(x => x.StartDate).AsQueryable();

            if (NotificationSearch.iSortingCols == 1)
            {
                if (NotificationSearch.sSortDir_0 == "asc" && NotificationSearch.iSortCol_0 == 0) { customer = customer.OrderBy(p => p.BookingRef); }
                if (NotificationSearch.sSortDir_0 == "desc" && NotificationSearch.iSortCol_0 == 0) { customer = customer.OrderByDescending(p => p.BookingRef); }

                if (NotificationSearch.sSortDir_0 == "asc" && NotificationSearch.iSortCol_0 == 1) { customer = customer.OrderBy(p => p.TourOpCode); }
                if (NotificationSearch.sSortDir_0 == "desc" && NotificationSearch.iSortCol_0 == 1) { customer = customer.OrderByDescending(p => p.TourOpCode); }

                if (NotificationSearch.sSortDir_0 == "asc" && NotificationSearch.iSortCol_0 == 2) { customer = customer.OrderBy(p => p.FullName); }
                if (NotificationSearch.sSortDir_0 == "desc" && NotificationSearch.iSortCol_0 == 2) { customer = customer.OrderByDescending(p => p.FullName); }

                if (NotificationSearch.sSortDir_0 == "asc" && NotificationSearch.iSortCol_0 == 3) { customer = customer.OrderBy(p => p.MobileNo); }
                if (NotificationSearch.sSortDir_0 == "desc" && NotificationSearch.iSortCol_0 == 3) { customer = customer.OrderByDescending(p => p.MobileNo); }

                if (NotificationSearch.sSortDir_0 == "asc" && NotificationSearch.iSortCol_0 == 4) { customer = customer.OrderBy(p => p.DirectOrAgent); }
                if (NotificationSearch.sSortDir_0 == "desc" && NotificationSearch.iSortCol_0 == 4) { customer = customer.OrderByDescending(p => p.DirectOrAgent); }

                if (NotificationSearch.sSortDir_0 == "asc" && NotificationSearch.iSortCol_0 == 5) { customer = customer.OrderBy(p => p.TravelDate); }
                if (NotificationSearch.sSortDir_0 == "desc" && NotificationSearch.iSortCol_0 == 5) { customer = customer.OrderByDescending(p => p.TravelDate); }

                if (NotificationSearch.sSortDir_0 == "asc" && NotificationSearch.iSortCol_0 == 6) { customer = customer.OrderBy(p => p.DeparturePoint); }
                if (NotificationSearch.sSortDir_0 == "desc" && NotificationSearch.iSortCol_0 == 6) { customer = customer.OrderByDescending(p => p.DeparturePoint); }

                if (NotificationSearch.sSortDir_0 == "asc" && NotificationSearch.iSortCol_0 == 7) { customer = customer.OrderBy(p => p.ArrivalPoint); }
                if (NotificationSearch.sSortDir_0 == "desc" && NotificationSearch.iSortCol_0 == 7) { customer = customer.OrderByDescending(p => p.ArrivalPoint); }

                if (NotificationSearch.sSortDir_0 == "asc" && NotificationSearch.iSortCol_0 == 8) { customer = customer.OrderBy(p => p.TransportNumber); }
                if (NotificationSearch.sSortDir_0 == "desc" && NotificationSearch.iSortCol_0 == 8) { customer = customer.OrderByDescending(p => p.TransportNumber); }

                if (NotificationSearch.sSortDir_0 == "asc" && NotificationSearch.iSortCol_0 == 9) { customer = customer.OrderBy(p => p.FlightChain); }
                if (NotificationSearch.sSortDir_0 == "desc" && NotificationSearch.iSortCol_0 == 9) { customer = customer.OrderByDescending(p => p.FlightChain); }
            }
            var datatable = new
            {
                sEcho = NotificationSearch.sEcho,
                iTotalRecords = customer.OrderByDescending(e => e.StartDate).Skip(NotificationSearch.iDisplayStart).Take(NotificationSearch.iDisplayLength).Count(),
                iTotalDisplayRecords = customer.Count(),
                aaData = customer.OrderByDescending(e => e.StartDate).Skip(NotificationSearch.iDisplayStart).Take(NotificationSearch.iDisplayLength).ToList(),
                EmailCount = EmailCount,
                MobileCount = MobileCount

            };
            return datatable;
        }