//Create ecoupon campaign backend
        public static int CreateEcouponCampaignBackend(EcouponCampaignDTO EcouponCampaignDTO)
        {
            try
            {
                GlobalSettings.LoggedInClientId = EcouponCampaignDTO.ClientId;
                GlobalSettings.LoggedInUserId = EcouponCampaignDTO.CreatedBy;

                ClientDTO ClientDTO = new ClientDTO();
                ClientDTO = ClientService.GetById(EcouponCampaignDTO.ClientId);
                int PartnerId = ClientDTO.PartnerId;// ClientService.GetById(EcouponCampaignDTO.ClientId).PartnerId;
                GlobalSettings.LoggedInPartnerId = PartnerId;

                //If SMS Credit balance is low then should not create campaign
                if (ClientDTO.SMSCredit < EcouponCampaignDTO.ConsumedCredits)
                {
                    return 0;
                }

                var EcouponCampaign = new EcouponCampaign();

                UnitOfWork uow = new UnitOfWork();
                if (EcouponCampaignDTO.ReceipentNumber != null && EcouponCampaignDTO.ReceipentNumber != "")
                {
                    EcouponCampaignDTO.ReceipentNumber = CommonService.RemoveDuplicateMobile(EcouponCampaignDTO.ReceipentNumber);
                }

                if (EcouponCampaignDTO.GroupId == null)
                {
                    EcouponCampaignDTO.RecipientsCount = CommonService.GetRecipientsCount(EcouponCampaignDTO.ReceipentNumber);
                }

                EcouponCampaignDTO.IsReconcile = false;
                EcouponCampaignDTO.ReconcileDate = System.DateTime.Now.Date;

                //Calculate consumed credits
                double ConsumedCreditPerOneMsg = CommonService.GetConsumedCreditsForOneMessage(EcouponCampaignDTO.Message, true);
                int RecepientsCount = CommonService.GetRecipientsCount(EcouponCampaignDTO.ReceipentNumber);
                EcouponCampaignDTO.ConsumedCredits = RecepientsCount * ConsumedCreditPerOneMsg;

                EcouponCampaign = Transform.EcouponCampaignToDomain(EcouponCampaignDTO);
                uow.EcouponCampaignRepo.Insert(EcouponCampaign);
                uow.SaveChanges();
                EcouponCampaignDTO.Id = EcouponCampaign.Id;

                //Deduct clients balance

                ClientDTO.SMSCredit = ClientDTO.SMSCredit - EcouponCampaignDTO.ConsumedCredits;
                ClientService.Edit(ClientDTO);

                return EcouponCampaignDTO.Id;
            }

            catch (Exception)
            {
                throw;
            }
        }
        //Get reedeemed count by clinet id and campaign id
        public static List<RedeemedCountDTO> GetByClientId(int ClientId, int CampaignId)
        {
            List<RedeemedCountDTO> RedeemedCountDTOList = new List<RedeemedCountDTO>();
            try
            {
                UnitOfWork uow = new UnitOfWork();
                IEnumerable<RedeemedCount> RedeemedCount = uow.RedeemedCountRepo.GetAll().Where(e => e.ClientId == ClientId && e.EcouponCampaignId == CampaignId);
                if (RedeemedCount != null)
                {
                    foreach (var item in RedeemedCount)
                    {
                        EcouponCampaignDTO EcouponCampaignDTO = new EcouponCampaignDTO();
                        EcouponCampaignDTO = EcouponCampaignService.GetById(item.EcouponCampaignId);
                        if (EcouponCampaignDTO.ExpiresOn > System.DateTime.Now)
                        {

                            RedeemedCountDTO RedeemedCountDTO = new RedeemedCountDTO();
                            RedeemedCountDTO = Transform.RedeemedCountToDTO(item);

                            UserDTO UserDTO = new UserDTO();
                            UserDTO = UserService.GetById(item.UserId);
                            RedeemedCountDTO.UserName = UserDTO.Name;
                            RedeemedCountDTO.Location = LocationService.GetById(UserDTO.LocationId).Name;
                            ClientDTO ClientDTO = new ClientDTO();
                            ClientDTO = ClientService.GetById(ClientId);
                            RedeemedCountDTO.ClientName = ClientDTO.Company;
                            RedeemedCountDTO.CampaignName = EcouponCampaignDTO.Title;
                            RedeemedCountDTOList.Add(RedeemedCountDTO);
                        }
                    }
                }

                return RedeemedCountDTOList;
            }
            catch
            {
                //  throw;
                return RedeemedCountDTOList;
            }
        }
 public void EditEcouponCampaign(string accessId, EcouponCampaignDTO EcouponCampaignDTO)
 {
     try
     {
         EcouponCampaignService.Edit(EcouponCampaignDTO);
     }
     catch (TimeoutException)
     {
         throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.RequestTimeout)
         {
             Content = new StringContent("An error occurred, please try again or contact the administrator."),
             ReasonPhrase = "Critical Exception"
         });
     }
     catch (Exception)
     {
         throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError)
         {
             Content = new StringContent("An error occurred, please try again or contact the administrator."),
             ReasonPhrase = "Critical Exception"
         });
     }
 }
        //Get all ecoupon campaign list with client id
        public static List<EcouponCampaignDTO> GetAllOpenEcouponCampaignsByClientId(int ClientId)
        {
            List<EcouponCampaignDTO> EcouponCampaignDTOList = new List<EcouponCampaignDTO>();
            try
            {

                using (var uow = new UnitOfWork())
                {
                    IEnumerable<EcouponCampaign> EcouponCampaign = uow.EcouponCampaignRepo.GetAll().Where(e => e.ClientId == ClientId && e.ExpiresOn > System.DateTime.Now.Date).ToList();
                    if (EcouponCampaign != null)
                    {
                        foreach (var item in EcouponCampaign)
                        {
                            EcouponCampaignDTO EcouponCampaignDTO = new EcouponCampaignDTO();
                            EcouponCampaignDTO = Transform.EcouponCampaignToDTO(item);

                            List<CouponDTO> CouponDTOList = new List<CouponDTO>();
                            CouponDTOList = CouponService.GetCouponListByEcouponCampaignId(item.Id).Where(e => e.IsRedeem == true).ToList();
                            if (CouponDTOList.Count > 0)
                            {
                                EcouponCampaignDTO.ReedeemedCount = CouponDTOList.Count;
                            }
                            else EcouponCampaignDTO.ReedeemedCount = 0;

                            EcouponCampaignDTOList.Add(EcouponCampaignDTO);// (Transform.EcouponCampaignToDTO(item));
                        }
                    }
                }

                return EcouponCampaignDTOList;
            }
            catch (Exception)
            {

                throw;
            }
        }
        //Get coupon as per Mobile and code
        public static CouponDTO GetCouponDetailsFromMobileAndCode(string Mobile, string Code)
        {
            try
            {
                CouponDTO CouponDTO = new CouponDTO();
                EcouponCampaignDTO EcouponCampaignDTO = new EcouponCampaignDTO();

                using (var uow = new UnitOfWork())
                {
                    IEnumerable<Coupon> Coupon = uow.CouponRepo.GetAll().Where(e => e.MobileNumber == Mobile && e.Code == Code);
                    if (Coupon != null)
                    {
                        foreach (var item in Coupon)
                        {
                            CouponDTO = GetById(item.Id);

                        }

                        if (CouponDTO.Id != 0)
                        {
                            EcouponCampaignDTO = EcouponCampaignService.GetById(CouponDTO.EcouponCampaignId);
                            CouponDTO.CouponCampaignName = EcouponCampaignDTO.Title;
                            CouponDTO.ExpiresOn = EcouponCampaignDTO.ExpiresOn;
                            CouponDTO.MinPurchaseAmount = EcouponCampaignDTO.MinPurchaseAmount;
                        }
                    }

                }
                return CouponDTO;
            }
            catch (Exception)
            {
                throw;
            }
        }
        public static void CreateCampaigns()
        {
            List<ClientDTO> ClientDTOList = new List<ClientDTO>();
            ClientDTOList = ClientService.GetAllActiveClients();

            SettingDTO SettingDTO = new SettingDTO();
            SettingDTO = SettingService.GetById(1);

            if (ClientDTOList != null)
            {
                foreach (var item in ClientDTOList)
                {
                    if (item.IsSendBirthdayMessages || item.IsSendBirthdayCoupons)
                    {
                        string RecipientsNumberList = null;
                        int userId = UserService.GetAdminUserByClientId(item.Id);
                        List<ContactDTO> ContactDTOList = new List<ContactDTO>();
                        ContactDTOList = ContactService.GetContactsForTodaysBirthday();
                        if (ContactDTOList.Count > 0)
                        {
                            if (ContactDTOList.Count == 1)
                            {
                                foreach (var Contactitem in ContactDTOList)
                                {
                                    RecipientsNumberList = Contactitem.MobileNumber;
                                }
                            }
                            else {
                                    foreach (var Contactitem in ContactDTOList)
                                    {
                                        RecipientsNumberList = Contactitem.MobileNumber + "," + RecipientsNumberList;
                                    }

                                    RecipientsNumberList = RecipientsNumberList.Remove(RecipientsNumberList.Length - 1);
                            }

                            if (item.IsSendBirthdayMessages)
                            {
                                CampaignDTO CampaignDTO = new CampaignDTO();
                                CampaignDTO.Message = item.BirthdayMessage;
                                CampaignDTO.RecipientsNumber = RecipientsNumberList;
                                CampaignDTO.Name = "Birthday Message_" + string.Format("{0:G}", System.DateTime.Now); ;
                                CampaignDTO.ClientId = item.Id;
                                CampaignDTO.CreatedDate = System.DateTime.Now.Date;
                                CampaignDTO.IsScheduled = false;
                                CampaignDTO.GroupId = null;
                                CampaignDTO.ScheduledDate = System.DateTime.Now.Date;
                                CampaignDTO.CreatedBy = userId;

                                ////Calculate consumed credits
                                //double ConsumedCreditPerOneMsg = CommonService.GetConsumedCreditsForOneMessage(CampaignDTO.Message, false);
                                //int RecepientsCount = CommonService.GetRecipientsCount(CampaignDTO.RecipientsNumber);
                                //CampaignDTO.ConsumededCredits = RecepientsCount * ConsumedCreditPerOneMsg;

                                CampaignService.CreateCampaignFromBackend(CampaignDTO);
                            }

                            if (item.IsSendBirthdayCoupons)
                            {
                                EcouponCampaignDTO EcouponCampaignDTO = new EcouponCampaignDTO();
                                EcouponCampaignDTO.Message = item.BirthdayMessage;
                                EcouponCampaignDTO.ReceipentNumber = RecipientsNumberList;
                                EcouponCampaignDTO.Title = "Birthday Coupon_" + string.Format("{0:G}", System.DateTime.Now); ;
                                EcouponCampaignDTO.ClientId = item.Id;
                                EcouponCampaignDTO.CreatedDate = System.DateTime.Now.Date;
                                EcouponCampaignDTO.SendOn = System.DateTime.Now.Date;
                                EcouponCampaignDTO.IsScheduled = false;
                                EcouponCampaignDTO.GroupId = null;
                                EcouponCampaignDTO.MinPurchaseAmount = Convert.ToDouble(item.MinPurchaseAmountForBirthdayCoupon);
                                EcouponCampaignDTO.CreatedBy = userId;

                                int value = Convert.ToInt32(item.BirthdayCouponExpire);

                                string expire_format = item.BirthdayCouponExpireType;
                                if (expire_format == "Day")
                                {
                                    EcouponCampaignDTO.ExpiresOn = System.DateTime.Now.Date.AddDays(value);
                                }
                                else if (expire_format == "Month")
                                {
                                    EcouponCampaignDTO.ExpiresOn = System.DateTime.Now.Date.AddMonths(value);
                                }
                                else if (expire_format == "Week")
                                {
                                    value = value * 7;
                                    EcouponCampaignDTO.ExpiresOn = System.DateTime.Now.Date.AddDays(value);
                                }
                                ////Calculate consumed credits
                                //double ConsumedCreditPerOneMsg = CommonService.GetConsumedCreditsForOneMessage(EcouponCampaignDTO.Message, true);
                                //int RecepientsCount = CommonService.GetRecipientsCount(EcouponCampaignDTO.ReceipentNumber);
                                //EcouponCampaignDTO.ConsumededCredits = RecepientsCount * ConsumedCreditPerOneMsg;

                                EcouponCampaignService.CreateEcouponCampaignBackend(EcouponCampaignDTO);
                            }
                        }
                    }

                    if (item.IsSendAnniversaryMessages || item.IsSendAnniversaryCoupons)
                    {

                        string RecipientsNumberList = null;
                        int userId = UserService.GetAdminUserByClientId(item.Id);
                        List<ContactDTO> ContactDTOList = new List<ContactDTO>();
                        ContactDTOList = ContactService.GetContactsForTodaysAnniversary();
                        if (ContactDTOList.Count > 0)
                        {
                            if (ContactDTOList.Count == 1)
                            {
                                foreach (var Contactitem in ContactDTOList)
                                {
                                    RecipientsNumberList = Contactitem.MobileNumber;
                                }
                            }
                            else
                            {
                                foreach (var Contactitem in ContactDTOList)
                                {
                                    RecipientsNumberList = Contactitem.MobileNumber + "," + RecipientsNumberList;
                                }

                                RecipientsNumberList = RecipientsNumberList.Remove(RecipientsNumberList.Length - 1);
                            }

                            if (item.IsSendAnniversaryMessages)
                            {
                                CampaignDTO CampaignDTO = new CampaignDTO();
                                CampaignDTO.Message = item.AnniversaryMessage;
                                CampaignDTO.RecipientsNumber = RecipientsNumberList;
                                CampaignDTO.Name = "Anniversary Message_" + string.Format("{0:G}", System.DateTime.Now); ;
                                CampaignDTO.ClientId = item.Id;
                                CampaignDTO.CreatedDate = System.DateTime.Now.Date;
                                CampaignDTO.IsScheduled = false;
                                CampaignDTO.GroupId = null;
                                CampaignDTO.ScheduledDate = System.DateTime.Now.Date;
                                CampaignDTO.CreatedBy = userId;

                                ////Calculate consumed credits
                                //double ConsumedCreditPerOneMsg = CommonService.GetConsumedCreditsForOneMessage(CampaignDTO.Message, false);
                                //int RecepientsCount = CommonService.GetRecipientsCount(CampaignDTO.RecipientsNumber);
                                //CampaignDTO.ConsumededCredits = RecepientsCount * ConsumedCreditPerOneMsg;

                                CampaignService.CreateCampaignFromBackend(CampaignDTO);
                            }

                            if (item.IsSendAnniversaryCoupons)
                            {
                                EcouponCampaignDTO EcouponCampaignDTO = new EcouponCampaignDTO();
                                EcouponCampaignDTO.Message = item.AnniversaryMessage;
                                EcouponCampaignDTO.ReceipentNumber = RecipientsNumberList;
                                EcouponCampaignDTO.Title = "Anniversary Coupon_" + string.Format("{0:G}", System.DateTime.Now); ;
                                EcouponCampaignDTO.ClientId = item.Id;
                                EcouponCampaignDTO.CreatedDate = System.DateTime.Now.Date;
                                EcouponCampaignDTO.SendOn = System.DateTime.Now.Date;
                                EcouponCampaignDTO.IsScheduled = false;
                                EcouponCampaignDTO.GroupId = null;
                                EcouponCampaignDTO.MinPurchaseAmount = Convert.ToDouble(item.MinPurchaseAmountForAnniversaryCoupon);
                                EcouponCampaignDTO.CreatedBy = userId;

                                int value = Convert.ToInt32(item.AnniversaryCouponExpire);

                                string expire_format = item.AnniversaryCouponExpireType;
                                if (expire_format == "Day")
                                {
                                    EcouponCampaignDTO.ExpiresOn = System.DateTime.Now.Date.AddDays(value);
                                }
                                else if (expire_format == "Month")
                                {
                                    EcouponCampaignDTO.ExpiresOn = System.DateTime.Now.Date.AddMonths(value);
                                }
                                else if (expire_format == "Week")
                                {
                                    value = value * 7;
                                    EcouponCampaignDTO.ExpiresOn = System.DateTime.Now.Date.AddDays(value);
                                }

                                ////Calculate consumed credits
                                //double ConsumedCreditPerOneMsg = CommonService.GetConsumedCreditsForOneMessage(EcouponCampaignDTO.Message, true);
                                //int RecepientsCount = CommonService.GetRecipientsCount(EcouponCampaignDTO.ReceipentNumber);
                                //EcouponCampaignDTO.ConsumededCredits = RecepientsCount * ConsumedCreditPerOneMsg;

                                EcouponCampaignService.CreateEcouponCampaignBackend(EcouponCampaignDTO);
                            }
                        }
                    }
                }
            }
        }
        public static void ExpireCoupon()
        {
            List<CouponDTO> CouponDTOList = new List<CouponDTO>();
            CouponDTOList = CouponService.GetCouponListWhichNotExpired();
            if (CouponDTOList != null)
            {
                foreach (var item in CouponDTOList)
                {
                    EcouponCampaignDTO EcouponCampaignDTO = new EcouponCampaignDTO();
                    EcouponCampaignDTO = EcouponCampaignService.GetById(item.EcouponCampaignId);
                    if (EcouponCampaignDTO.ExpiresOn != null)
                    {
                        if (EcouponCampaignDTO.ExpiresOn < System.DateTime.Now)
                        {
                            CouponDTO CouponDTO = new CouponDTO();
                            CouponDTO = item;// CouponService.GetById(item.Id);

                            CouponDTO.IsExpired = true;
                            CouponService.Edit(CouponDTO);

                        }
                    }

                }
            }
        }
        public static String SplitMobile(string Mobile, EcouponCampaignDTO EcouponCampaignDTO)
        {
            string result = "";
            bool IsSent = false;
            int FalseCount = 0;
            try
            {
                string finalstring = "";

                // Input string contain separators.
                string value1 = Mobile;

                char[] delimiter1 = new char[] { ',', ';' };   // <-- Split on these
                // ... Use StringSplitOptions.RemoveEmptyEntries.
                string[] array2 = value1.Split(delimiter1,
                    StringSplitOptions.RemoveEmptyEntries);

                //Console.WriteLine();
                foreach (string mobile in array2)
                {
                    Console.WriteLine(mobile);
                    bool isMessageSent = CheckCampainLogByCampaingIdAndMobile(EcouponCampaignDTO.Id, Mobile);

                    ClientDTO ClientDTO = new ClientDTO();
                    ClientDTO = ClientService.GetById(EcouponCampaignDTO.ClientId);

                    //SMSGatewayDTO SMSGatewayDTO = new SMSGatewayDTO();
                    //SMSGatewayDTO = SMSGatewayService.GetById(ClientDTO.SMSGatewayId);

                    if (isMessageSent == false)
                    {
                        Console.Write("Send SMS");
                        //if (ClientDTO.SMSCredit > 0)
                        //{
                            int ecouponcode = 0;
                        createnew:
                            CommonService CommonService = new CommonService();
                            ecouponcode = CommonService.GetRandomNumber();
                            string ecouponcodelength = ecouponcode.ToString();
                            if (ecouponcodelength.Length < 6 || ecouponcodelength.Length > 6)
                            {
                                goto createnew;
                            }
                            List<CouponDTO> CouponDTOList = new List<CouponDTO>();
                            CouponDTOList = CouponService.GetCouponListByCodeAndMobile(ecouponcode.ToString(), mobile);
                            if (CouponDTOList.Count == 0)
                            {
                                string Message = "";
                                EcouponCampaignDTO.Message = EcouponCampaignService.GetById(EcouponCampaignDTO.Id).Message;

                                //macros
                                List<Macros> MacrosList = Enum.GetValues(typeof(Macros)).Cast<Macros>().ToList();

                                ContactDTO ContactDTO = new ContactDTO();
                                ContactDTO = ContactService.GetContactByMobileNumberAndClientId(mobile, EcouponCampaignDTO.ClientId);

                                if (MacrosList.Count() > 0)
                                {
                                    foreach (var item in MacrosList)
                                    {

                                        if (item.ToString() == "FirstName")
                                        {
                                            string FirstName = "";
                                            FirstName = ContactDTO.FirstName;// CommonService.GetFirstname(ContactDTO.Name);
                                            EcouponCampaignDTO.Message = EcouponCampaignDTO.Message.Replace("[" + item.ToString() + "]", FirstName);
                                        }

                                        if (item.ToString() == "LastName")
                                        {
                                            string LastName = "";
                                            LastName = ContactDTO.LastName;// CommonService.GetLastname(ContactDTO.Name);
                                            EcouponCampaignDTO.Message = EcouponCampaignDTO.Message.Replace("[" + item.ToString() + "]", LastName);
                                        }

                                        if (item.ToString() == "BirthDate")
                                        {
                                            if (ContactDTO.BirthDate != null)
                                            {
                                                DateTime BirthDate = Convert.ToDateTime(ContactDTO.BirthDate);
                                                EcouponCampaignDTO.Message = EcouponCampaignDTO.Message.Replace("[" + item.ToString() + "]", BirthDate.ToString("dd-MMM"));
                                            }
                                            else { EcouponCampaignDTO.Message = EcouponCampaignDTO.Message.Replace("[" + item.ToString() + "]", ""); }
                                        }

                                        if (item.ToString() == "AnniversaryDate")
                                        {
                                            if (ContactDTO.AnniversaryDate != null)
                                            {
                                                DateTime AnniversaryDate = Convert.ToDateTime(ContactDTO.AnniversaryDate);
                                                EcouponCampaignDTO.Message = EcouponCampaignDTO.Message.Replace("[" + item.ToString() + "]", AnniversaryDate.ToString("dd-MMM"));
                                            }
                                            else { EcouponCampaignDTO.Message = EcouponCampaignDTO.Message.Replace("[" + item.ToString() + "]", ""); }
                                        }

                                        if (item.ToString() == "Email")
                                        {
                                            if (ContactDTO.Email != null)
                                            {
                                                string Email = ContactDTO.Email;
                                                EcouponCampaignDTO.Message = EcouponCampaignDTO.Message.Replace("[" + item.ToString() + "]", Email);
                                            }
                                            else { EcouponCampaignDTO.Message = EcouponCampaignDTO.Message.Replace("[" + item.ToString() + "]", ""); }
                                        }

                                        if (item.ToString() == "MobileNumber")
                                        {
                                            if (ContactDTO.MobileNumber != null)
                                            {
                                                string MobileNumber = ContactDTO.MobileNumber;
                                                EcouponCampaignDTO.Message = EcouponCampaignDTO.Message.Replace("[" + item.ToString() + "]", MobileNumber);
                                            }
                                            else { EcouponCampaignDTO.Message = EcouponCampaignDTO.Message.Replace("[" + item.ToString() + "]", ""); }
                                        }

                                        if (item.ToString() == "Gender")
                                        {
                                            if (ContactDTO.Gender != null)
                                            {
                                                string Gender = ContactDTO.Gender;

                                                //if (Gender == "0")
                                                //{
                                                //    Gender = "Male";
                                                //}
                                                //else
                                                //{
                                                //    Gender = "Female";
                                                //}

                                                EcouponCampaignDTO.Message = EcouponCampaignDTO.Message.Replace("[" + item.ToString() + "]", Gender);
                                            }
                                            else { EcouponCampaignDTO.Message = EcouponCampaignDTO.Message.Replace("[" + item.ToString() + "]", ""); }
                                        }

                                        if (item.ToString() == "ExpiresOn")
                                        {
                                            if (EcouponCampaignDTO.ExpiresOn != null)
                                            {
                                                DateTime ExpiresOn = Convert.ToDateTime(EcouponCampaignDTO.ExpiresOn);
                                                EcouponCampaignDTO.Message = EcouponCampaignDTO.Message.Replace("[" + item.ToString() + "]", ExpiresOn.ToString("dd-MMM-yy"));
                                            }
                                            else { EcouponCampaignDTO.Message = EcouponCampaignDTO.Message.Replace("[" + item.ToString() + "]", ""); }
                                        }

                                        if (item.ToString() == "Code")
                                        {
                                            string Code = ecouponcode.ToString();
                                            EcouponCampaignDTO.Message = EcouponCampaignDTO.Message.Replace("[" + item.ToString() + "]", Code);
                                        }

                                    }

                                    Message = EcouponCampaignDTO.Message;
                                    // Check the Message required credits and actual client credits
                                    double SMSMsgCount = GetMessageCount(Message);

                                    SettingDTO SettingDTO = new SettingDTO();
                                    SettingDTO = SettingService.GetById(1);
                                    SMSMsgCount = SMSMsgCount * SettingDTO.NationalCouponSMSCount;

                                    ////Check Credits
                                    //if (ClientDTO.SMSCredit >= SMSMsgCount)
                                    //{
                                        string sender = "";

                                        List<CouponDTO> CouponDTOListDuplicate = new List<CouponDTO>();
                                        CouponDTOListDuplicate = CouponService.GetCouponListByEcouponCampaignIdAndMobile(EcouponCampaignDTO.Id, mobile);
                                        if (CouponDTOListDuplicate.Count != 0)
                                        {
                                            ////If already sent then skip
                                            continue;
                                            ////foreach (var item in CouponDTOListDuplicate)
                                            ////{
                                            ////    if (item.IsExpired != true)
                                            ////    {
                                            ////        string MobileDuplicate = null;
                                            ////        CouponDTO CouponDTO = new CouponDTO();
                                            ////        CouponDTO = item;
                                            ////        CouponDTO.IsExpired=true;
                                            ////        CouponService.Edit(CouponDTO);

                                            ////        MobileDuplicate = item.MobileNumber;
                                            ////        Message = item.Message;
                                            ////        ecouponcode = Convert.ToInt32(item.Code);

                                            ////        if (ClientDTO.SenderCode != null && ClientDTO.SenderCode != "")
                                            ////        {
                                            ////            sender = ClientDTO.SenderCode;
                                            ////        }
                                            ////        else
                                            ////        {

                                            ////            sender = "022751";
                                            ////        }

                                            ////        IsSent = ActualSmsSend(mobile, Message, sender, EcouponCampaignDTO, ClientDTO, ecouponcode.ToString());
                                            ////        continue;
                                            ////    }

                                            ////}

                                        }

                                        if (ClientDTO.SenderCode != null && ClientDTO.SenderCode != "")
                                        {
                                            sender = ClientDTO.SenderCode;
                                        }
                                        else
                                        {

                                            sender = "022751";
                                        }

                                        IsSent = ActualSmsSend(mobile, Message, sender, EcouponCampaignDTO, ClientDTO, ecouponcode.ToString());
                                    //}
                                    //else goto nextprocess;

                                }

                                // Message = ReformatMsg(EcouponCampaignDTO.Message + " Your ecoupon code is " + ecouponcode + "");

                            }
                            else if (CouponDTOList.Count >= 1)
                            {
                                goto createnew;
                            }

                        //}
                        //else
                        //{
                        //    goto nextprocess;
                        //}

                    }
                    else
                    {

                    }
                }

                int TotatCouponSent = CouponService.GetCouponCountByEcouponCampaignId(EcouponCampaignDTO.Id);

                //if (EcouponCampaignDTO.RecipientsCount == TotatCouponSent)
                if (EcouponCampaignDTO.RecipientsCount <= TotatCouponSent)
                {
                    // Modify EcouponCampaign IsSent status
                    EcouponCampaignDTO.IsSent = true;
                    EcouponCampaignDTO.Message = EcouponCampaignService.GetById(EcouponCampaignDTO.Id).Message;
                    EcouponCampaignDTO.RequiredCredits = CouponService.GetECouponCampaignRequiredCreditsByEcouponCampaignId(EcouponCampaignDTO.Id);
                    EcouponCampaignDTO.CreditsDiffrence = EcouponCampaignDTO.ConsumedCredits - EcouponCampaignDTO.RequiredCredits;

                    if (EcouponCampaignDTO.ConsumedCredits != EcouponCampaignDTO.RequiredCredits)
                    {
                        if (EcouponCampaignDTO.CreditsDiffrence < 0)
                        {
                            //// deduct clients balance
                            ClientDTO ClientDTOUpdate = new ClientDTO();
                            ClientDTOUpdate = ClientService.GetById(EcouponCampaignDTO.ClientId);
                            ClientDTOUpdate.SMSCredit = ClientDTOUpdate.SMSCredit - (-(EcouponCampaignDTO.CreditsDiffrence));
                            ClientService.Edit(ClientDTOUpdate);

                            ////Reconcile Ecoupon Campaign
                            EcouponCampaignDTO.IsReconcile = true;
                            EcouponCampaignDTO.ReconcileDate = System.DateTime.Now;
                            EcouponCampaignService.EditEcouponCampaignFromBackend(EcouponCampaignDTO);

                        }
                        else if (EcouponCampaignDTO.CreditsDiffrence > 0)
                        {
                            ////Add clients balance
                            ClientDTO ClientDTOUpdate = new ClientDTO();
                            ClientDTOUpdate = ClientService.GetById(EcouponCampaignDTO.ClientId);
                            ClientDTOUpdate.SMSCredit = ClientDTOUpdate.SMSCredit + EcouponCampaignDTO.CreditsDiffrence;
                            ClientService.Edit(ClientDTOUpdate);

                            ////Reconcile Ecoupon Campaign
                            EcouponCampaignDTO.IsReconcile = true;
                            EcouponCampaignDTO.ReconcileDate = System.DateTime.Now;
                            EcouponCampaignService.EditEcouponCampaignFromBackend(EcouponCampaignDTO);

                        }
                    }
                    else if (EcouponCampaignDTO.CreditsDiffrence == 0)
                    {
                        EcouponCampaignDTO.IsReconcile = true;
                        EcouponCampaignDTO.ReconcileDate = System.DateTime.Now;
                        EcouponCampaignService.EditEcouponCampaignFromBackend(EcouponCampaignDTO);
                    }

                   // EcouponCampaignService.EditEcouponCampaignFromBackend(EcouponCampaignDTO);
                }

            nextprocess:
                result = finalstring;
            }

            catch (Exception ex)
            {

                result = "";

                using (FileStream file = new FileStream(Directory.GetCurrentDirectory() + "\\msgBlasterBackendService_Log.txt", FileMode.Append, FileAccess.Write))
                {
                    StreamWriter streamWriter = new StreamWriter(file);
                    streamWriter.WriteLine(System.DateTime.Now + " - " + "  SplitMobile()" + " - " + ex.Message);
                    streamWriter.Close();
                }

            }
            return result;
        }
        //Return Coupon paged list as per Ecoupon campaign id and Is sent status
        public static PageData<CouponDTO> GetCouponPagedListbyEcouponCampaignId(PagingInfo pagingInfo, int EcouponCampaignId, bool IsSent)
        {
            List<CouponDTO> CouponDTOList = new List<CouponDTO>();
            PageData<CouponDTO> pageList = new PageData<CouponDTO>();

            if (pagingInfo == null)
            {
                PagingInfo PagingInfoCreated = new PagingInfo();
                PagingInfoCreated.Page = 1;
                PagingInfoCreated.Reverse = false;
                PagingInfoCreated.ItemsPerPage = 1;
                PagingInfoCreated.Search = "";
                PagingInfoCreated.TotalItem = 0;

                pagingInfo = PagingInfoCreated;
            }
            if (pagingInfo.SortBy == "")
            {
                pagingInfo.SortBy = "SentDateTime";
            }

            CouponDTOList = GetCouponListSearchByEcouponCampaignId(EcouponCampaignId, pagingInfo.Search, IsSent, pagingInfo);
            IQueryable<CouponDTO> CouponDTOPagedList = CouponDTOList.AsQueryable();

            UnitOfWork uow = new UnitOfWork();
            int count = 0;
            if (pagingInfo.Search != "" && pagingInfo.Search != null)
            {
                bool IsDate = CommonService.IsDate(pagingInfo.Search);
                if (IsDate != true)
                {
                    count = 0;
                    EcouponCampaignDTO EcouponCampaignDTO = new EcouponCampaignDTO();
                    EcouponCampaignDTO = EcouponCampaignService.GetById(EcouponCampaignId);
                    //int UserId = 0;
                    //UserId = UserService.GetUserByName(pagingInfo.Search, EcouponCampaignDTO.ClientId);
                    string UserIdString = UserService.GetUserIdarrayByName(pagingInfo.Search, EcouponCampaignDTO.ClientId);
                    int CampaignId = 0;
                    CampaignId = EcouponCampaignService.GetEcouponCampaignByName(pagingInfo.Search, EcouponCampaignDTO.ClientId);

                    count = uow.CouponRepo.GetAll().Where(e => (e.Code.Contains(pagingInfo.Search) || (e.MobileNumber != null ? (e.MobileNumber.Contains(pagingInfo.Search)) : false) || (e.Remark != null ? (e.Remark.ToLower().Contains(pagingInfo.Search.ToLower())) : false) || (e.RedeemDateTime.ToString() != null ? (Convert.ToDateTime(e.RedeemDateTime).ToString("dd-MMM-yyyy").ToLower().Contains(pagingInfo.Search.ToLower())) : false) || (UserIdString != null ? (e.UserId.ToString().Split(',').Any(UserId => UserIdString.Contains(UserId))) : false) || (e.EcouponCampaignId != 0 ? (e.EcouponCampaignId == CampaignId) : false) || (e.BillNumber != null ? (e.BillNumber.ToLower().Contains(pagingInfo.Search.ToLower())) : false) || (e.BillDate.ToString() != null ? (Convert.ToDateTime(e.BillDate).ToString("dd-MMM-yyyy").ToLower().Contains(pagingInfo.Search.ToLower())) : false)) && e.EcouponCampaignId == EcouponCampaignId).Count();
                }
                else
                {
                    DateTime date = Convert.ToDateTime(pagingInfo.Search);
                    count = 0;
                    count = uow.CouponRepo.GetAll().Where(e => (e.RedeemDateTime >= date && e.RedeemDateTime < date.AddDays(1) && e.BillDate >= date && e.BillDate < date.AddDays(1)) && e.EcouponCampaignId == EcouponCampaignId).Count();

                }

            }
            else
            {
                count = uow.CouponRepo.GetAll().Where(e => e.EcouponCampaignId == EcouponCampaignId).Count();
            }

            ////Sorting
            //CouponDTOPagedList = PagingService.Sorting<CouponDTO>(CouponDTOPagedList, pagingInfo.SortBy, pagingInfo.Reverse);

            // paging
            if (CouponDTOPagedList.Count() > 0)
            {
                //var ContacDTOPerPage = PagingService.Paging<CouponDTO>(CouponDTOPagedList, pagingInfo.ItemsPerPage, pagingInfo.Page);
                pageList.Count = count; // CouponDTOPagedList.Count();

                List<CouponDTO> pagedCouponDTOList = new List<CouponDTO>();
                foreach (var item in CouponDTOPagedList)
                {
                    EcouponCampaignDTO EcouponCampaignDTO = new EcouponCampaignDTO();
                    EcouponCampaignDTO = EcouponCampaignService.GetById(item.EcouponCampaignId);

                    item.CouponCampaignName = EcouponCampaignDTO.Title;
                    item.CreatedBy = UserService.GetById(EcouponCampaignDTO.CreatedBy).Name;
                    item.CreatedOn = EcouponCampaignDTO.CreatedDate;
                    item.ExpiresOn = EcouponCampaignDTO.ExpiresOn;

                    pagedCouponDTOList.Add(item);
                }
                pageList.Data = pagedCouponDTOList;
            }
            else
            {
                pageList.Data = null;
            }

            return pageList;
        }
Ejemplo n.º 10
0
        //Get Coupon by Id
        public static CouponDTO GetById(int Id)
        {
            try
            {
                UnitOfWork uow = new UnitOfWork();
                Coupon Coupon = uow.CouponRepo.GetById(Id);
                CouponDTO CouponDTO = Transform.CouponToDTO(Coupon);
                EcouponCampaignDTO EcouponCampaignDTO = new EcouponCampaignDTO();
                EcouponCampaignDTO = EcouponCampaignService.GetById(CouponDTO.EcouponCampaignId);

                CouponDTO.CouponCampaignName = EcouponCampaignDTO.Title;
                CouponDTO.CreatedBy = UserService.GetById(EcouponCampaignDTO.CreatedBy).Name;
                CouponDTO.CreatedOn = EcouponCampaignDTO.CreatedDate;
                CouponDTO.ExpiresOn = EcouponCampaignDTO.ExpiresOn;

                return CouponDTO;
            }
            catch
            {
                throw;
            }
        }
Ejemplo n.º 11
0
        //Return Coupon list as per partner id
        public static List<CouponDTO> GetCouponListSearchByPartnerId(int PartnerId, string search)
        {
            List<EcouponCampaignDTO> EcouponCampaignDTOList = new List<EcouponCampaignDTO>();
            List<CouponDTO> CouponDTOList = new List<CouponDTO>();
            try
            {
                EcouponCampaignDTOList = EcouponCampaignService.GetEcouponCampaignListSearchByPartnerId(PartnerId, "");

                if (EcouponCampaignDTOList.Count > 0)
                {
                    foreach (var item in EcouponCampaignDTOList)
                    {
                        var EcouponCampaign = CouponDTOList.Where(e => e.ClientId == item.ClientId).OrderByDescending(e => e.SentDateTime);// uow.EcouponCampaignRepo.GetAll().Where(e => e.ClientId == ClientId);
                        if (EcouponCampaign != null)
                        {

                            foreach (var itemEcouponCampaign in EcouponCampaign)
                            {
                                CouponDTO CouponDTONew = new CouponDTO();
                                CouponDTONew = itemEcouponCampaign;
                                UserDTO UserDTO = new UserDTO();

                                EcouponCampaignDTO EcouponCampaignDTO = new EcouponCampaignDTO();
                                EcouponCampaignDTO = EcouponCampaignService.GetById(itemEcouponCampaign.EcouponCampaignId);
                                CouponDTONew.CouponCampaignName = EcouponCampaignDTO.Title;

                                if (itemEcouponCampaign.UserId != null)
                                {
                                    int UserId = (int)itemEcouponCampaign.UserId;
                                    UserDTO = UserService.GetById(UserId);
                                    CouponDTONew.UserName = UserDTO.Name;
                                }
                                else
                                {
                                    CouponDTONew.UserName = "";
                                }

                                CouponDTOList.Add(CouponDTONew);
                            }
                        }
                    }
                }

                if (search != "" && search != null)
                {
                    bool Isdate = CommonService.IsDate(search);
                    if (Isdate != true)
                    {
                        List<CouponDTO> CouponDTOSearchList = new List<CouponDTO>();
                        var CouponSearch = CouponDTOList.Where(e => e.Code.Contains(search) || (e.MobileNumber != null ? (e.MobileNumber.Contains(search)) : false) || (e.Remark != null ? (e.Remark.ToLower().Contains(search.ToLower())) : false) || e.UserName.ToLower().Contains(search.ToLower()) || (e.RedeemDateTime.ToString() != null ? (Convert.ToDateTime(e.RedeemDateTime).ToString("dd-MMM-yyyy").ToLower().Contains(search.ToLower())) : false) || (e.CouponCampaignName != null ? (e.CouponCampaignName.ToLower().Contains(search.ToLower())) : false)).OrderByDescending(e => e.SentDateTime);
                        if (CouponSearch != null)
                        {
                            foreach (var itemsearch in CouponSearch)
                            {

                                CouponDTOSearchList.Add(itemsearch);
                            }
                        }
                        return CouponDTOSearchList;

                    }
                    else
                    {
                        List<CouponDTO> CouponDTOSearchList = new List<CouponDTO>();
                        DateTime date = Convert.ToDateTime(search);
                        var CouponSearch = CouponDTOList.Where(e => e.RedeemDateTime >= date && e.RedeemDateTime < date.AddDays(1)).OrderByDescending(e => e.SentDateTime);
                        if (CouponSearch != null)
                        {
                            foreach (var itemsearch in CouponSearch)
                            {

                                CouponDTOSearchList.Add(itemsearch);
                            }
                        }
                        return CouponDTOSearchList;
                    }
                }
                return CouponDTOList;
            }

              //catch (LoggedInUserException)
            //{
            //    throw new System.TimeoutException();
            //}
            catch (Exception)
            {

                throw;
            }
        }
Ejemplo n.º 12
0
        //Return Coupon list as per Ecoupon campaign id and Is sent status
        public static List<CouponDTO> GetCouponListSearchByEcouponCampaignId(int EcouponCampaignId, string search, bool IsSent, PagingInfo pagingInfo)
        {
            List<CouponDTO> CouponDTOList = new List<CouponDTO>();
            try
            {
                using (var uow = new UnitOfWork())
                {
                    IEnumerable<EcouponCampaign> EcouponCampaign = uow.EcouponCampaignRepo.GetAll().Where(e => e.Id == EcouponCampaignId && e.IsSent == IsSent).OrderByDescending(e => e.CreatedDate);
                    //var EcouponCampaigncoupons = from c in EcouponCampaign where (from g in c.Coupons where c.ClientId == ClientId  select g).Any() select c;
                    int skip = (pagingInfo.Page - 1) * pagingInfo.ItemsPerPage;
                    int take = pagingInfo.ItemsPerPage;

                    if (EcouponCampaign != null)
                    {
                        foreach (var item in EcouponCampaign)
                        {
                            IQueryable<Coupon> Coupon = uow.CouponRepo.GetAll().Where(e => e.EcouponCampaignId == item.Id).AsQueryable();//.OrderByDescending(e => e.SentDateTime).Skip(skip).Take(take);
                            Coupon = PagingService.Sorting<Coupon>(Coupon, pagingInfo.SortBy, pagingInfo.Reverse);
                            Coupon = Coupon.Skip(skip).Take(take);

                            if (Coupon != null)
                            {
                                foreach (var itemCoupon in Coupon)
                                {
                                    CouponDTO CouponDTO = new CouponDTO();
                                    if (itemCoupon.EcouponCampaignId == item.Id)
                                    {
                                        UserDTO UserDTO = new UserDTO();
                                        CouponDTO = Transform.CouponToDTO(itemCoupon);

                                        EcouponCampaignDTO EcouponCampaignDTO = new EcouponCampaignDTO();
                                        EcouponCampaignDTO = EcouponCampaignService.GetById(itemCoupon.EcouponCampaignId);
                                        CouponDTO.CouponCampaignName = EcouponCampaignDTO.Title;
                                        CouponDTO.MinPurchaseAmount = EcouponCampaignDTO.MinPurchaseAmount;

                                        if (CouponDTO.UserId != null)
                                        {
                                            int UserId = (int)CouponDTO.UserId;
                                            UserDTO = UserService.GetById(UserId);
                                            CouponDTO.UserName = UserDTO.Name;
                                        }
                                        else
                                        {
                                            CouponDTO.UserName = "";
                                        }

                                        CouponDTOList.Add(CouponDTO);
                                        //CouponDTOList.Add(Transform.CouponToDTO(itemCoupon));
                                    }
                                }
                            }
                        }
                        if (search != "" & search != null)
                        {
                            EcouponCampaignDTO EcouponCampaignDTO = new EcouponCampaignDTO();
                            EcouponCampaignDTO = EcouponCampaignService.GetById(EcouponCampaignId);
                            //int UserId = 0;
                            //UserId = UserService.GetUserByName(search,EcouponCampaignDTO.ClientId);
                            int CampaignId = 0;

                            string UserIdString = UserService.GetUserIdarrayByName(search, EcouponCampaignDTO.ClientId);

                            CampaignId = EcouponCampaignService.GetEcouponCampaignByName(search, EcouponCampaignDTO.ClientId);

                            bool Isdate = CommonService.IsDate(search);
                            if (Isdate != true)
                            {
                                List<CouponDTO> CouponDTOSearchList = new List<CouponDTO>();
                                IQueryable<Coupon> CouponSearch = uow.CouponRepo.GetAll().Where(e => (e.Code.Contains(search) || (e.MobileNumber != null ? (e.MobileNumber.Contains(search)) : false) || (e.Remark != null ? (e.Remark.ToLower().Contains(search.ToLower())) : false) || (e.RedeemDateTime.ToString() != null ? (Convert.ToDateTime(e.RedeemDateTime).ToString("dd-MMM-yyyy").ToLower().Contains(pagingInfo.Search.ToLower())) : false) || (UserIdString != null ? (e.UserId.ToString().Split(',').Any(UserId => UserIdString.Contains(UserId))) : false) || (e.EcouponCampaignId != 0 ? (e.EcouponCampaignId == CampaignId) : false) || (e.BillNumber != null ? (e.BillNumber.ToLower().Contains(pagingInfo.Search.ToLower())) : false) || (e.BillDate.ToString() != null ? (Convert.ToDateTime(e.BillDate).ToString("dd-MMM-yyyy").ToLower().Contains(pagingInfo.Search.ToLower())) : false)) && e.EcouponCampaignId == EcouponCampaignId).AsQueryable();//.OrderByDescending(e => e.SentDateTime).Skip(skip).Take(take); //|| e.UserName.ToLower().Contains(search.ToLower()) || (e.CouponCampaignName != null ? (e.CouponCampaignName.ToLower().Contains(search.ToLower())) : false)

                                CouponSearch = PagingService.Sorting<Coupon>(CouponSearch, pagingInfo.SortBy, pagingInfo.Reverse);
                                CouponSearch = CouponSearch.Skip(skip).Take(take);

                                if (CouponSearch != null)
                                {
                                    foreach (var itemsearch in CouponSearch)
                                    {
                                        if (itemsearch.EcouponCampaignId == EcouponCampaignId)
                                        {
                                            CouponDTO CouponDTO = new CouponDTO();
                                            CouponDTO = Transform.CouponToDTO(itemsearch);

                                            EcouponCampaignDTO EcouponCampaignDTOSearch = new EcouponCampaignDTO();
                                            EcouponCampaignDTOSearch = EcouponCampaignService.GetById(itemsearch.EcouponCampaignId);
                                            CouponDTO.CouponCampaignName = EcouponCampaignDTOSearch.Title;
                                            CouponDTO.MinPurchaseAmount = EcouponCampaignDTOSearch.MinPurchaseAmount;

                                            UserDTO UserDTO = new UserDTO();
                                            if (itemsearch.UserId == null)
                                            { itemsearch.UserId = 0; }
                                            int userId = (int)itemsearch.UserId;
                                            if (userId != 0)
                                            {
                                                UserDTO = UserService.GetById(userId);
                                                CouponDTO.UserName = UserDTO.Name;
                                            }
                                            CouponDTOSearchList.Add(CouponDTO);
                                        }
                                    }
                                }
                                return CouponDTOSearchList;

                            }
                            else
                            {
                                List<CouponDTO> CouponDTOSearchList = new List<CouponDTO>();
                                DateTime date = Convert.ToDateTime(search);
                                IQueryable<Coupon> CouponSearch = uow.CouponRepo.GetAll().Where(e => (e.RedeemDateTime >= date && e.RedeemDateTime < date.AddDays(1) && e.BillDate >= date && e.BillDate < date.AddDays(1)) && e.EcouponCampaignId == EcouponCampaignId).AsQueryable();//.OrderByDescending(e => e.SentDateTime).Skip(skip).Take(take);
                                CouponSearch = PagingService.Sorting<Coupon>(CouponSearch, pagingInfo.SortBy, pagingInfo.Reverse);
                                CouponSearch = CouponSearch.Skip(skip).Take(take);

                                if (CouponSearch != null)
                                {
                                    foreach (var itemsearch in CouponSearch)
                                    {
                                        if (itemsearch.EcouponCampaignId == EcouponCampaignId)
                                        {
                                            CouponDTO CouponDTO = new CouponDTO();
                                            CouponDTO = Transform.CouponToDTO(itemsearch);
                                            UserDTO UserDTO = new UserDTO();

                                            EcouponCampaignDTO EcouponCampaignDTOSearch = new EcouponCampaignDTO();
                                            EcouponCampaignDTOSearch = EcouponCampaignService.GetById(itemsearch.EcouponCampaignId);
                                            CouponDTO.CouponCampaignName = EcouponCampaignDTOSearch.Title;
                                            CouponDTO.MinPurchaseAmount = EcouponCampaignDTOSearch.MinPurchaseAmount;

                                            if (itemsearch.UserId == null)
                                            { itemsearch.UserId = 0; }

                                            int userId = (int)itemsearch.UserId;
                                            if (userId != 0)
                                            {
                                                UserDTO = UserService.GetById(userId);
                                                CouponDTO.UserName = UserDTO.Name;
                                            }
                                            CouponDTOSearchList.Add(CouponDTO);
                                        }

                                        //CouponDTOSearchList.Add(Transform.CouponToDTO(itemsearch));
                                    }
                                }
                                return CouponDTOSearchList;

                            }

                        }

                    }

                }

                return CouponDTOList;
            }
            //catch (LoggedInUserException)
            //{
            //    throw new System.TimeoutException();
            //}
            catch (Exception)
            {

                throw;
            }
        }
Ejemplo n.º 13
0
        //Return Coupon list as per user id
        public static List<CouponDTO> GetCouponListByUserId(int UserId, PagingInfo pagingInfo)
        {
            List<CouponDTO> CouponDTOList = new List<CouponDTO>();
            try
            {
                UserDTO UserDTO = new UserDTO();
                UserDTO = UserService.GetById(UserId);

                using (var uow = new UnitOfWork())
                {
                    int skip = (pagingInfo.Page - 1) * pagingInfo.ItemsPerPage;
                    int take = pagingInfo.ItemsPerPage;

                    IQueryable<Coupon> Coupon = uow.CouponRepo.GetAll().Where(e => e.UserId == UserId && e.IsExpired != true && e.IsRedeem == true).AsQueryable(); //.OrderByDescending(e => e.SentDateTime);
                    Coupon = PagingService.Sorting<Coupon>(Coupon, pagingInfo.SortBy, pagingInfo.Reverse);
                    Coupon = Coupon.Skip(skip).Take(take);

                    if (Coupon != null)
                    {
                        foreach (var item in Coupon)
                        {

                            CouponDTO CouponDTO = new CouponDTO();
                            CouponDTO = Transform.CouponToDTO(item);
                            CouponDTO.UserName = UserDTO.Name;

                            EcouponCampaignDTO EcouponCampaignDTO = new EcouponCampaignDTO();
                            EcouponCampaignDTO = EcouponCampaignService.GetById(CouponDTO.EcouponCampaignId);
                            CouponDTO.CouponCampaignName = EcouponCampaignDTO.Title;
                            CouponDTO.ExpiresOn = EcouponCampaignDTO.ExpiresOn;
                            CouponDTO.MinPurchaseAmount = EcouponCampaignDTO.MinPurchaseAmount;

                            CouponDTOList.Add(CouponDTO);// (Transform.CouponToDTO(item));
                        }
                    }

                    if (pagingInfo.Search != "" && pagingInfo.Search != null)
                    {

                        bool Isdate = CommonService.IsDate(pagingInfo.Search);
                        if (Isdate != true)
                        {

                            IQueryable<CouponDTO> CouponList = CouponDTOList.Where(e => e.CouponCampaignName.ToLower().Contains(pagingInfo.Search.ToLower()) || (e.MobileNumber != null ? (e.MobileNumber.Contains(pagingInfo.Search)) : false) || (e.Amount != null ? (e.Amount.ToString() == pagingInfo.Search.ToString()) : false) || (e.Code != null ? (e.Code.Contains(pagingInfo.Search)) : false) || (e.Message != null ? (e.Message.ToLower().Contains(pagingInfo.Search.ToLower())) : false) || (e.RedeemDateTime.ToString() != null ? (Convert.ToDateTime(e.RedeemDateTime).ToString("dd-MMM-yyyy").ToLower().Contains(pagingInfo.Search.ToLower())) : false) || (e.Remark != null ? (e.Remark.ToLower().Contains(pagingInfo.Search.ToLower())) : false) || (e.BillNumber != null ? (e.BillNumber.ToLower().Contains(pagingInfo.Search.ToLower())) : false) || (e.BillDate.ToString() != null ? (Convert.ToDateTime(e.BillDate).ToString("dd-MMM-yyyy").ToLower().Contains(pagingInfo.Search.ToLower())) : false)).AsQueryable();//.OrderByDescending(e => e.SentDateTime).ToList();
                            CouponList = PagingService.Sorting<CouponDTO>(CouponList, pagingInfo.SortBy, pagingInfo.Reverse);
                            CouponList = CouponList.Skip(skip).Take(take);

                            List<CouponDTO> CouponDTOListNew = new List<CouponDTO>();
                            if (CouponDTOList.Count > 0)
                            {
                                foreach (var item in CouponList)
                                {
                                    EcouponCampaignDTO EcouponCampaignDTOSearch = new EcouponCampaignDTO();
                                    EcouponCampaignDTOSearch = EcouponCampaignService.GetById(item.EcouponCampaignId);
                                    item.CouponCampaignName = EcouponCampaignDTOSearch.Title;
                                    item.ExpiresOn = EcouponCampaignDTOSearch.ExpiresOn;
                                    item.MinPurchaseAmount = EcouponCampaignDTOSearch.MinPurchaseAmount;

                                    CouponDTOListNew.Add(item);
                                }
                                return CouponDTOListNew;
                            }

                        }
                        else
                        {
                            List<CouponDTO> CouponDTOListNew = new List<CouponDTO>();
                            DateTime date = Convert.ToDateTime(pagingInfo.Search);
                            IQueryable<CouponDTO> CouponList = CouponDTOList.Where(e => e.RedeemDateTime >= date && e.RedeemDateTime < date.AddDays(1) && e.BillDate >= date && e.BillDate < date.AddDays(1)).AsQueryable();//.OrderByDescending(e => e.SentDateTime).ToList();
                            CouponList = PagingService.Sorting<CouponDTO>(CouponList, pagingInfo.SortBy, pagingInfo.Reverse);
                            CouponList = CouponList.Skip(skip).Take(take);
                            if (CouponDTOList.Count > 0)
                            {
                                foreach (var item in CouponList)
                                {
                                    EcouponCampaignDTO EcouponCampaignDTOSearch = new EcouponCampaignDTO();
                                    EcouponCampaignDTOSearch = EcouponCampaignService.GetById(item.EcouponCampaignId);
                                    item.CouponCampaignName = EcouponCampaignDTOSearch.Title;
                                    item.ExpiresOn = EcouponCampaignDTOSearch.ExpiresOn;
                                    item.MinPurchaseAmount = EcouponCampaignDTOSearch.MinPurchaseAmount;

                                    CouponDTOListNew.Add(item);
                                }
                                return CouponDTOListNew;
                            }
                        }

                    }

                }

                return CouponDTOList;
            }
            //catch (LoggedInUserException)
            //{
            //    throw new System.TimeoutException();
            //}
            catch (Exception)
            {

                throw;
            }
        }
Ejemplo n.º 14
0
        //int Id,
        //Redeem Coupon
        public static CouponDTO RedeemCoupon(CouponDTO CouponDTO)
        {
            try
            {
                CouponDTO CouponDTONew = new CouponDTO();
                List<CouponDTO> CouponDTOList = new List<CouponDTO>();

                 using (var uow = new UnitOfWork())
                 {
                    List<Coupon> CouponList = new List<Coupon>();
                    CouponList = uow.CouponRepo.GetAll().Where(e => e.EcouponCampaignId == CouponDTO.EcouponCampaignId && e.MobileNumber == CouponDTO.MobileNumber && e.Code == CouponDTO.Code).ToList();

                    if (CouponList != null)
                    {
                        foreach (var item in CouponList)
                        {
                            CouponDTOList.Add(Transform.CouponToDTO(item));
                        }
                    }
                 }

               // CouponDTOList = GetCouponListByClientId(CouponDTO.ClientId);
                //IEnumerable<CouponDTO> CouponDTOSearch = CouponDTOList.Where(e => e.MobileNumber == CouponDTO.MobileNumber && e.Code == CouponDTO.Code); // && e.IsRedeem == false
                 IEnumerable<CouponDTO> CouponDTOSearch = CouponDTOList;

                foreach (var item in CouponDTOSearch)
                {
                    CouponDTONew = item;
                }

                if (CouponDTONew.Id != 0 && CouponDTONew.IsExpired != true && CouponDTONew.IsRedeem != true)
                {

                    CouponDTONew.IsRedeem = true;
                    CouponDTONew.Remark = CouponDTO.Remark;
                    CouponDTONew.UserId = CouponDTO.UserId;
                    CouponDTONew.Amount = CouponDTO.Amount;
                    CouponDTONew.RedeemDateTime = System.DateTime.Now;
                    CouponDTONew.BillDate = CouponDTO.BillDate;
                    CouponDTONew.BillNumber = CouponDTO.BillNumber;

                    UnitOfWork uow = new UnitOfWork();
                    Coupon Coupon = Transform.CouponToDomain(CouponDTONew);
                    uow.CouponRepo.Update(Coupon);
                    uow.SaveChanges();

                    EcouponCampaignDTO EcouponCampaignDTO = new EcouponCampaignDTO();
                    EcouponCampaignDTO = EcouponCampaignService.GetById(Coupon.EcouponCampaignId);
                    ClientDTO ClientDTO = new ClientDTO();
                    ClientDTO = ClientService.GetById(EcouponCampaignDTO.ClientId);

                    UpdateRedeemCount(Convert.ToInt32(Coupon.UserId), ClientDTO.Id, Coupon.EcouponCampaignId);

                }

                return CouponDTONew;
            }
            catch
            {
                throw;
            }
        }
        //Get contacts paged list by ecoupon campaign id
        public static PageData<ContactDTO> GetContactsByEcouponCampaignId(PagingInfo pagingInfo, int EcouponCampaignId, int ClientId)
        {
            UnitOfWork uow = new UnitOfWork();
            EcouponCampaignDTO EcouponCampaignDTO = new EcouponCampaignDTO();
            PageData<ContactDTO> pageList = new PageData<ContactDTO>();

            EcouponCampaignDTO = GetById(EcouponCampaignId);

            if (EcouponCampaignDTO != null)
            {
                if (EcouponCampaignDTO.ReceipentNumber != "")
                {
                    string value = EcouponCampaignDTO.ReceipentNumber;
                    string[] array = value.Replace(" ", "").Split(',');

                    List<ContactDTO> ContactDTOList = new List<ContactDTO>();
                    Contact Contact = new Contact();
                    ContactDTO ContactDTO = new ContactDTO();

                    int skip = (pagingInfo.Page - 1) * pagingInfo.ItemsPerPage;
                    int take = pagingInfo.ItemsPerPage;

                    var newlist = array.Skip(skip).Take(take).ToArray();

                    if (pagingInfo.Search != "")
                    {
                        ContactDTOList = SearchContacts(pagingInfo.Search, array, EcouponCampaignId, ClientId);
                        pageList.Data = ContactDTOList;
                        pageList.Count = ContactDTOList.Count;
                    }
                    else
                    {
                        for (int i = 0; i < newlist.Length; i++)
                        {
                            Contact = uow.ContactRepo.GetAll().Where(e => e.MobileNumber == newlist[i] && e.ClientId == ClientId).FirstOrDefault();
                            if (Contact == null)
                            {
                                Contact = new Contact();
                                Contact.MobileNumber = newlist.ToList()[i];
                                Contact.Gender = null;
                                Contact.Id = 0;
                                Contact.ClientId = 0;
                                //Contact.Name = null;
                            }

                            ContactDTO = Transform.ContactToDTO(Contact);

                            ContactDTOList.Add(ContactDTO);
                        }
                        pageList.Data = ContactDTOList;
                        pageList.Count = array.Length;
                    }
                }
                else if (EcouponCampaignDTO.GroupId != null)
                {
                    // GroupContactDTO GroupContactDTO =  GroupService.GetGroupContactById((int)CampaignDTO.GroupId);
                    pagingInfo.GroupId = (int)EcouponCampaignDTO.GroupId;
                    pageList = ContactService.GetGroupIdWisePresentContactsPagedListByClientId(pagingInfo, ClientId);
                }

            }
            else
            {
                pageList.Data = null;
            }

            return pageList;
        }
        //Get ecoupon campaign details from mobile and code
        public static EcouponCampaignDTO GetEcouponCampaignDetailsFromMobileAndCode(string Mobile, string Code)
        {
            try
            {
                CouponDTO CouponDTO = new CouponDTO();
                EcouponCampaignDTO EcouponCampaignDTO = new EcouponCampaignDTO();

                using (var uow = new UnitOfWork())
                {
                    IEnumerable<Coupon> Coupon = uow.CouponRepo.GetAll().Where(e => e.MobileNumber == Mobile && e.Code == Code && e.IsExpired != true && e.IsRedeem != true);
                    if (Coupon != null)
                    {
                        foreach (var item in Coupon)
                        {
                            CouponDTO.Id = item.Id;
                            CouponDTO.EcouponCampaignId = item.EcouponCampaignId;
                        }

                        EcouponCampaignDTO = EcouponCampaignService.GetById(CouponDTO.EcouponCampaignId);

                    }

                }
                return EcouponCampaignDTO;
            }
            catch (Exception)
            {
                throw;
            }
        }
        //Delete ecoupon campaign
        public static void Delete(int id)
        {
            try
            {

                EcouponCampaignDTO EcouponCampaignDTO = new EcouponCampaignDTO();
                EcouponCampaignDTO = EcouponCampaignService.GetById(id);
                //Add Consumed Credits to clients
                ClientDTO ClientDTO = new ClientDTO();
                ClientDTO = ClientService.GetById(EcouponCampaignDTO.ClientId);
                ClientDTO.SMSCredit = ClientDTO.SMSCredit + EcouponCampaignDTO.ConsumedCredits;
                ClientService.Edit(ClientDTO);

                UnitOfWork uow = new UnitOfWork();
                uow.EcouponCampaignRepo.Delete(id);
                uow.SaveChanges();

            }
            catch
            {
                throw;
            }
        }
Ejemplo n.º 18
0
 public static EcouponCampaign EcouponCampaignToDomain(EcouponCampaignDTO EcouponCampaignDTO)
 {
     if (EcouponCampaignDTO == null) return null;
      Mapper.CreateMap<EcouponCampaignDTO, EcouponCampaign>();
      EcouponCampaign EcouponCampaign = Mapper.Map<EcouponCampaign>(EcouponCampaignDTO);
      return EcouponCampaign;
 }
        //Create ecoupon campaign
        public static int Create(EcouponCampaignDTO EcouponCampaignDTO)
        {
            try
            {
                var EcouponCampaign = new EcouponCampaign();

                GlobalSettings.LoggedInClientId = EcouponCampaignDTO.ClientId;
                GlobalSettings.LoggedInUserId = EcouponCampaignDTO.CreatedBy;

                ClientDTO ClientDTO = new ClientDTO();
                ClientDTO = ClientService.GetById(EcouponCampaignDTO.ClientId);
                int PartnerId = ClientDTO.PartnerId;// ClientService.GetById(EcouponCampaignDTO.ClientId).PartnerId;
                GlobalSettings.LoggedInPartnerId = PartnerId;

                //If SMS Credit balance is low then should not create campaign
                if (ClientDTO.SMSCredit < EcouponCampaignDTO.ConsumedCredits)
                {
                    return 0;
                }

                UnitOfWork uow = new UnitOfWork();
                if (EcouponCampaignDTO.ReceipentNumber != null && EcouponCampaignDTO.ReceipentNumber != "")
                {
                    EcouponCampaignDTO.ReceipentNumber = CommonService.RemoveDuplicateMobile(EcouponCampaignDTO.ReceipentNumber);
                }
                if (EcouponCampaignDTO.GroupId == null)
                {
                    EcouponCampaignDTO.RecipientsCount = CommonService.GetRecipientsCount(EcouponCampaignDTO.ReceipentNumber);
                }

                EcouponCampaignDTO.IPAddress = CommonService.GetIP();

                if (EcouponCampaignDTO.ForAllContact == true)
                {
                    EcouponCampaignDTO.ReceipentNumber = ContactService.GetAllReceipentNumberByClientId(EcouponCampaignDTO.ClientId);
                    EcouponCampaignDTO.RecipientsCount = CommonService.GetRecipientsCount(EcouponCampaignDTO.ReceipentNumber);
                    EcouponCampaignDTO.GroupId = null;
                    EcouponCampaignDTO.ReceipentNumber = "";
                }

                EcouponCampaignDTO.IsReconcile = false;
                EcouponCampaignDTO.ReconcileDate = System.DateTime.Now.Date;

                EcouponCampaign = Transform.EcouponCampaignToDomain(EcouponCampaignDTO);
                uow.EcouponCampaignRepo.Insert(EcouponCampaign);
                uow.SaveChanges();
                EcouponCampaignDTO.Id = EcouponCampaign.Id;

                // Deduct SMS credit balance

                ClientDTO.SMSCredit = ClientDTO.SMSCredit - EcouponCampaignDTO.ConsumedCredits;
                ClientService.Edit(ClientDTO);

                return EcouponCampaignDTO.Id;

            }
            catch (Exception)
            {
                throw;
            }
        }
        public static void ReadEcouponEcouponCampaign()
        {
            List<EcouponCampaignDTO> EcouponCampaignDTOList = new List<EcouponCampaignDTO>();
            EcouponCampaignDTOList = EcouponCampaignService.GetEcouponCampaignNotSentList();
            if (EcouponCampaignDTOList != null)
            {
                foreach (var item in EcouponCampaignDTOList)
                {
                    try
                    {
                        EcouponCampaignDTO EcouponCampaignDTO = new EcouponCampaignDTO();
                        EcouponCampaignDTO = item;
                        DateTime ScheduledDate = EcouponCampaignDTO.SendOn.Date;
                        DateTime Time;
                        if (EcouponCampaignDTO.ScheduleTime != "")
                        {
                            Time = Convert.ToDateTime(EcouponCampaignDTO.ScheduleTime);
                        }
                        else Time = Convert.ToDateTime("12:00 AM");

                        ScheduledDate = Convert.ToDateTime(ScheduledDate.Date.ToString("MM/dd/yyyy") + " " + Time.TimeOfDay);

                        Console.WriteLine("Scheduled Time = " + ScheduledDate);

                        if (ScheduledDate <= System.DateTime.Now)
                        {
                            //SplitMobile(item.ReceipentNumber, EcouponCampaignDTO);
                            if (item.GroupId == null && item.ForAllContact == false)
                            {
                                SplitMobile(item.ReceipentNumber, EcouponCampaignDTO);
                            }
                            else
                            {

                                string RecipientsNumberList = null;
                                if (item.GroupId > 0)
                                {
                                    GroupContactDTO GroupContactDTO = new GroupContactDTO();
                                    GroupDTO GroupDTO = new GroupDTO();
                                    GroupDTO = GroupService.GetById(Convert.ToInt32(item.GroupId));
                                    //GroupContactDTO = GroupService.GetGroupContactById(Convert.ToInt32(GroupDTO.Id));

                                    List<ContactDTO> ContactDTO = new List<ContactDTO>();
                                    ContactDTO = GroupContactService.GetGroupIdWiseContacts(GroupDTO.Id);

                                    foreach (var Contactitem in ContactDTO) //GroupContactDTO.Contacts
                                    {
                                        RecipientsNumberList = Contactitem.MobileNumber + "," + RecipientsNumberList;
                                    }
                                }
                                else if(item.ForAllContact == true)
                                {
                                    RecipientsNumberList = null;
                                    RecipientsNumberList = ContactService.GetAllReceipentNumberByClientId(item.ClientId);
                                    RecipientsNumberList = RecipientsNumberList + ",";
                                }

                                item.ReceipentNumber = RecipientsNumberList.Remove(RecipientsNumberList.LastIndexOf(','));
                                item.RecipientsCount = CommonService.GetRecipientsCount(item.ReceipentNumber);
                                EcouponCampaignService.EditEcouponCampaignFromBackend(item);

                                EcouponCampaignDTO EcouponCampaignGrpDTO = new EcouponCampaignDTO();
                                EcouponCampaignGrpDTO = item;
                                SplitMobile(item.ReceipentNumber, EcouponCampaignGrpDTO);
                            }

                        }
                        else { }

                    }
                    catch (Exception ex)
                    {
                        using (FileStream file = new FileStream(Directory.GetCurrentDirectory() + "\\msgBlasterBackendService_Log.txt", FileMode.Append, FileAccess.Write))
                        {
                            StreamWriter streamWriter = new StreamWriter(file);
                            streamWriter.WriteLine(System.DateTime.Now + " - " + "  ReadEcouponEcouponCampaign()" + " - " + ex.Message);
                            streamWriter.Close();
                        }

                        continue;
                    }

                }
            }

            //Check Client Balance

            //Modify Client Balance
        }
        //Edit ecoupon campaign
        public static void Edit(EcouponCampaignDTO EcouponCampaignDTO)
        {
            try
            {

                //Get previous ecoupon balance and restore the client balance
                ClientDTO ClientDTO = new ClientDTO();
                ClientDTO = ClientService.GetById(EcouponCampaignDTO.ClientId);
                EcouponCampaignDTO EcouponCampaignDTODRestore = new EcouponCampaignDTO();
                EcouponCampaignDTODRestore = GetById(EcouponCampaignDTO.Id);
                ClientDTO.SMSCredit = ClientDTO.SMSCredit + EcouponCampaignDTODRestore.ConsumedCredits;
                ClientService.Edit(ClientDTO);

                GlobalSettings.LoggedInClientId = EcouponCampaignDTO.ClientId;
                GlobalSettings.LoggedInUserId = EcouponCampaignDTO.CreatedBy;
                int PartnerId = ClientService.GetById(EcouponCampaignDTO.ClientId).PartnerId;
                GlobalSettings.LoggedInPartnerId = PartnerId;

                //If SMS Credit balance is greater or equal to ConsumededCredits then create campaign
                if (ClientDTO.SMSCredit >= EcouponCampaignDTO.ConsumedCredits)
                {
                        UnitOfWork uow = new UnitOfWork();
                        //EcouponCampaignDTO.MessageCount = CommonService.GetMessageCount(EcouponCampaignDTO.Message);
                        if (EcouponCampaignDTO.ReceipentNumber != null && EcouponCampaignDTO.ReceipentNumber != "")
                        {
                            EcouponCampaignDTO.ReceipentNumber = CommonService.RemoveDuplicateMobile(EcouponCampaignDTO.ReceipentNumber);
                        }

                        if (EcouponCampaignDTO.GroupId == null)
                        {
                            EcouponCampaignDTO.RecipientsCount = CommonService.GetRecipientsCount(EcouponCampaignDTO.ReceipentNumber);
                        }
                        EcouponCampaignDTO.IPAddress = CommonService.GetIP();

                        if (EcouponCampaignDTO.ForAllContact == true)
                        {

                            EcouponCampaignDTO.ReceipentNumber = ContactService.GetAllReceipentNumberByClientId(EcouponCampaignDTO.ClientId);
                            EcouponCampaignDTO.RecipientsCount = CommonService.GetRecipientsCount(EcouponCampaignDTO.ReceipentNumber);
                            EcouponCampaignDTO.GroupId = null;
                            EcouponCampaignDTO.Group = null;
                            EcouponCampaignDTO.ReceipentNumber = "";
                        }
                        if (EcouponCampaignDTO.GroupId > 0)
                        {
                            EcouponCampaignDTO.Group = null;
                        }

                        EcouponCampaignDTO.IsReconcile = false;
                        EcouponCampaignDTO.ReconcileDate = System.DateTime.Now.Date;

                        EcouponCampaign EcouponCampaign = Transform.EcouponCampaignToDomain(EcouponCampaignDTO);
                        uow.EcouponCampaignRepo.Update(EcouponCampaign);
                        uow.SaveChanges();

                       //Deduct client balance
                        ClientDTO.SMSCredit = ClientDTO.SMSCredit - EcouponCampaignDTO.ConsumedCredits;
                        ClientService.Edit(ClientDTO);
                }
            }
            catch
            {
                throw;
            }
        }
        private static bool ActualSmsSend(string mobilenumber, string message, string Gateway, EcouponCampaignDTO EcouponCampaignDTO, ClientDTO ClientDTO, string CouponCode)
        {
            string result = "";
            bool IsSent = false;
            int SMSMsgCount = GetMessageCount(message);
            message = MsgCorrect(message);

            if (message != "" && mobilenumber != "")// Check for empty message.
            {

                string Url = ConfigurationManager.AppSettings["TransactionalGateWay"].ToString();
                Url = Url.Replace("%26", "&");
                Url = Url.Replace("[recipient]", mobilenumber);
                Url = Url.Replace("[message]", message);
                if (Gateway != "022751") //if (Gateway.ToLower() != "default")
                {
                    Url = Url.Replace("[gateway]", Gateway); //Gateway = "MSGBLS"
                }
                else
                {
                    Url = "";
                    Url = ConfigurationManager.AppSettings["PromotionalGateWay"].ToString();
                    Url = Url.Replace("%26", "&");
                    Url = Url.Replace("[recipient]", mobilenumber);
                    Url = Url.Replace("[message]", message);
                }

                HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(Url);
                myRequest.Method = "GET";
                WebResponse myResponse = myRequest.GetResponse();
                StreamReader sr = new StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.UTF8);
                result = sr.ReadToEnd();
                sr.Close();

                string statuscode = "";
                //if (result.Contains('|'))
                //    statuscode = result.Substring(0, result.IndexOf('|'));
                //else
                //    statuscode = result;

                //string SMSReplyMessage = SMSResult(statuscode) + "-" + result; //result

                if (result.Contains('|'))
                {
                    //statuscode = result.Substring(0, result.IndexOf('|'));
                    statuscode = "";
                    string[] words = result.Split('|');
                    foreach (string word in words)
                    {
                        Console.WriteLine(word);
                        try
                        {
                            int code = Convert.ToInt32(word);

                            statuscode = code.ToString();
                        }
                        catch (Exception)
                        {
                            string code = word.Replace(" ", "");
                            if (code == "Success")
                            {
                                code = "0";
                                statuscode = code.ToString();
                                IsSent = true;
                            }
                            else
                            {
                                continue;
                            }
                        }
                    }

                }
                else
                {
                    statuscode = result;
                }

                myResponse.Close();
                if (IsSent == true)
                {
                CouponDTO CouponDTO = new CouponDTO();
                //CouponDTO.IsSuccess = true;
                CouponDTO.EcouponCampaignId = EcouponCampaignDTO.Id;
                CouponDTO.MobileNumber = mobilenumber;
                CouponDTO.Code = CouponCode;
                CouponDTO.IsRedeem = false;

                CouponDTO.MessageId = result;
                SettingDTO SettingDTO = new SettingDTO();
                SettingDTO = SettingService.GetById(1);
                double ActualSMSMsgCount = SettingDTO.NationalCouponSMSCount * SMSMsgCount;
                CouponDTO.MessageCount = SMSMsgCount;
                CouponDTO.RequiredCredits = ActualSMSMsgCount;

                CouponDTO.Message = message;
                //CouponDTO.MessageStatus = SMSReplyMessage;
                //CouponDTO.GatewayID = Gateway;
                CouponDTO.SentDateTime = System.DateTime.Now;
                CouponDTO.IsCouponSent = true;
                //CouponDTO.MessageID = statuscode;

                //if (statuscode == "1701")
                //{
                //    CampaignLogDTO.IsSuccess = true;
                //}
                //else if (statuscode != "1701")
                //{
                //    CampaignLogDTO.IsSuccess = false;
                //}
                CouponService.Create(CouponDTO);

                //// Reduce SMS Credits From Client
                //ClientDTO.SMSCredit = ClientDTO.SMSCredit - ActualSMSMsgCount;  //SMSMsgCount;
                //ClientService.Edit(ClientDTO);

                }

            }

            return IsSent; // result;
        }
        //Edit ecoupon campaign from backend process
        public static void EditEcouponCampaignFromBackend(EcouponCampaignDTO EcouponCampaignDTO)
        {
            try
            {
                GlobalSettings.LoggedInClientId = EcouponCampaignDTO.ClientId;
                GlobalSettings.LoggedInUserId = EcouponCampaignDTO.CreatedBy;
                int PartnerId = ClientService.GetById(EcouponCampaignDTO.ClientId).PartnerId;
                GlobalSettings.LoggedInPartnerId = PartnerId;

                UnitOfWork uow = new UnitOfWork();
                //EcouponCampaignDTO.MessageCount = CommonService.GetMessageCount(EcouponCampaignDTO.Message);
                if (EcouponCampaignDTO.GroupId > 0)
                {
                    EcouponCampaignDTO.Group = null;
                }
                EcouponCampaign EcouponCampaign = Transform.EcouponCampaignToDomain(EcouponCampaignDTO);
                uow.EcouponCampaignRepo.Update(EcouponCampaign);
                uow.SaveChanges();
            }
            catch
            {
                throw;
            }
        }
Ejemplo n.º 24
0
        public static CouponDTO ResendCouponByCouponDTOAndClientId(CouponDTO CouponDTO, int ClientId)
        {
            SettingDTO SettingDTO = new SettingDTO();
            SettingDTO = SettingService.GetById(1);
            double RequiredCredit = CommonService.GetMessageCount(CouponDTO.Message);
            double ActualRequiredCredits = RequiredCredit * SettingDTO.NationalCouponSMSCount;
            int OldId = 0;

            OldId = CouponDTO.Id;
            CouponDTO CouponDTONew = new CouponDTO();
            CouponDTONew = null;

            string result = "";
            bool IsSent = false;
            if (CouponDTO.Message != "" && CouponDTO.MobileNumber != "")// Check for empty message.
            {
                ClientDTO ClientDTO = new ClientDTO();
                ClientDTO = ClientService.GetById(ClientId);
                string Url = null;

                if (ClientDTO.SenderCode != null && ClientDTO.SenderCode != "")
                {
                    Url = ConfigurationManager.AppSettings["TransactionalGateWay"].ToString();
                }
                else
                {

                    Url = ConfigurationManager.AppSettings["PromotionalGateWay"].ToString();
                }

                Url = Url.Replace("%26", "&");
                Url = Url.Replace("[recipient]", CouponDTO.MobileNumber);
                Url = Url.Replace("[message]", CouponDTO.Message);
                Url = Url.Replace("[gateway]", ClientDTO.SenderCode);   //SMSGatewayDTO.Name

                HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(Url);
                myRequest.Method = "GET";
                WebResponse myResponse = myRequest.GetResponse();
                StreamReader sr = new StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.UTF8);
                result = sr.ReadToEnd();
                sr.Close();

                string statuscode = "";
                if (result.Contains('|'))
                    statuscode = result.Substring(0, result.IndexOf('|'));
                else
                    statuscode = result;

                SMSResult(result);
                myResponse.Close();

                if (statuscode == "1701" || statuscode == "1705" || statuscode == "1706" || statuscode == "1032")
                {
                    IsSent = true;

                    CouponDTO.SentDateTime = System.DateTime.Now;
                    CouponDTO.Id = 0;
                    CouponDTO.IsExpired = false;
                    int NewCouponId = CouponService.Create(CouponDTO);

                    CouponDTONew = CouponService.GetById(NewCouponId);

                    //Expire previous coupon
                    CouponDTO CouponDTOPrevious = new CouponDTO();
                    CouponDTOPrevious = CouponService.GetById(OldId);
                    CouponDTOPrevious.IsExpired = true;
                    CouponDTOPrevious.MessageId = result;
                    CouponService.Edit(CouponDTOPrevious);

                    // Modify EcouponCampaign message count
                    EcouponCampaignDTO EcouponCampaignDTO = new EcouponCampaignDTO();
                    EcouponCampaignDTO = EcouponCampaignService.GetById(CouponDTONew.EcouponCampaignId);
                    EcouponCampaignDTO.ReceipentNumber = EcouponCampaignDTO.ReceipentNumber + "," + CouponDTO.MobileNumber;
                    EcouponCampaignDTO.RecipientsCount = EcouponCampaignDTO.RecipientsCount + 1;
                    if (EcouponCampaignDTO.GroupId == 0)
                    {
                        EcouponCampaignDTO.GroupId = null;
                        EcouponCampaignDTO.Group = null;
                    }

                    EcouponCampaignDTO.RequiredCredits = CouponService.GetECouponCampaignRequiredCreditsByEcouponCampaignId(EcouponCampaignDTO.Id);

                    EcouponCampaignService.EditForEcouponResend(EcouponCampaignDTO);

                    //Modify client SMS credits
                    ClientDTO.SMSCredit = ClientDTO.SMSCredit - ActualRequiredCredits;// RequiredCredit;
                    ClientService.Edit(ClientDTO);

                }
                else
                {
                    IsSent = false;

                    string UrlWhiz = "";
                    string resultWhiz = "";
                    if (ClientDTO.SenderCode != null && ClientDTO.SenderCode != "")
                    {
                        UrlWhiz = ConfigurationManager.AppSettings["TransactionalGateWayWhiz"].ToString();
                    }
                    else
                    {

                        UrlWhiz = ConfigurationManager.AppSettings["PromotionalGateWayWhiz"].ToString();
                    }

                    UrlWhiz = UrlWhiz.Replace("%26", "&");
                    UrlWhiz = UrlWhiz.Replace("[recipient]", CouponDTO.MobileNumber);
                    UrlWhiz = UrlWhiz.Replace("[message]", CouponDTO.Message);
                    UrlWhiz = UrlWhiz.Replace("[gateway]", ClientDTO.SenderCode);   //SMSGatewayDTO.Name

                    HttpWebRequest myRequesWhiz = (HttpWebRequest)WebRequest.Create(UrlWhiz);
                    myRequesWhiz.Method = "GET";
                    WebResponse myResponseWhiz = myRequesWhiz.GetResponse();
                    StreamReader srWhiz = new StreamReader(myResponseWhiz.GetResponseStream(), System.Text.Encoding.UTF8);
                    resultWhiz = srWhiz.ReadToEnd();
                    srWhiz.Close();

                    if (resultWhiz.Contains('|'))
                    {
                        //statuscode = result.Substring(0, result.IndexOf('|'));
                        statuscode = "";
                        string[] words = resultWhiz.Split('|');
                        foreach (string word in words)
                        {
                            Console.WriteLine(word);
                            try
                            {
                                int code = Convert.ToInt32(word);

                                statuscode = code.ToString();
                            }
                            catch (Exception)
                            {
                                string code = word.Replace(" ", "");
                                if (code == "Success")
                                {
                                    code = "0";
                                    statuscode = code.ToString();
                                    IsSent = true;

                                    CouponDTO.SentDateTime = System.DateTime.Now;
                                    CouponDTO.Id = 0;
                                    CouponDTO.IsExpired = false;
                                    int NewCouponId = CouponService.Create(CouponDTO);

                                    CouponDTONew = CouponService.GetById(NewCouponId);

                                    //Expire previous coupon
                                    CouponDTO CouponDTOPrevious = new CouponDTO();
                                    CouponDTOPrevious = CouponService.GetById(OldId);
                                    CouponDTOPrevious.IsExpired = true;
                                    CouponDTOPrevious.MessageId = resultWhiz;
                                    CouponService.Edit(CouponDTOPrevious);

                                    // Modify EcouponCampaign message count
                                    EcouponCampaignDTO EcouponCampaignDTO = new EcouponCampaignDTO();
                                    EcouponCampaignDTO = EcouponCampaignService.GetById(CouponDTONew.EcouponCampaignId);
                                    EcouponCampaignDTO.ReceipentNumber = EcouponCampaignDTO.ReceipentNumber + "," + CouponDTO.MobileNumber;
                                    EcouponCampaignDTO.RecipientsCount = EcouponCampaignDTO.RecipientsCount + 1;
                                    if (EcouponCampaignDTO.GroupId == 0)
                                    {
                                        EcouponCampaignDTO.GroupId = null;
                                        EcouponCampaignDTO.Group = null;
                                    }

                                    EcouponCampaignDTO.RequiredCredits = CouponService.GetECouponCampaignRequiredCreditsByEcouponCampaignId(EcouponCampaignDTO.Id);

                                    EcouponCampaignService.EditForEcouponResend(EcouponCampaignDTO);

                                    //Modify client SMS credits
                                    ClientDTO.SMSCredit = ClientDTO.SMSCredit - ActualRequiredCredits;// RequiredCredit;
                                    ClientService.Edit(ClientDTO);

                                }
                                else
                                {
                                    continue;
                                }
                            }
                        }

                    }
                    else
                    {
                        statuscode = resultWhiz;

                    }

                }
            }
            return CouponDTONew;
        }
        //Edit for ecoupon resend
        public static void EditForEcouponResend(EcouponCampaignDTO EcouponCampaignDTO)
        {
            GlobalSettings.LoggedInClientId = EcouponCampaignDTO.ClientId;
            GlobalSettings.LoggedInUserId = EcouponCampaignDTO.CreatedBy;
            int PartnerId = ClientService.GetById(EcouponCampaignDTO.ClientId).PartnerId;
            GlobalSettings.LoggedInPartnerId = PartnerId;

            UnitOfWork uow = new UnitOfWork();
            if (EcouponCampaignDTO.GroupId > 0)
            {
                EcouponCampaignDTO.Group = null;
            }
            EcouponCampaign EcouponCampaign = Transform.EcouponCampaignToDomain(EcouponCampaignDTO);
            uow.EcouponCampaignRepo.Update(EcouponCampaign);
            uow.SaveChanges();
        }
        //Extend or expire Ecoupon campaign by ecoupon campaign id, date and expire status
        public static bool ExtendOrExpireEcouponCampaign(int EcouponCampaignId, DateTime ExpiredOn, bool IsExpired)
        {
            try
            {
                if (IsExpired == true)
                {
                    try
                    {
                        bool IsStatusChanged = false;
                        IsStatusChanged = CouponService.ExpireAllCouponsByCampaignId(EcouponCampaignId);
                        EcouponCampaignDTO EcouponCampaignDTO = new EcouponCampaignDTO();
                        EcouponCampaignDTO = GetById(EcouponCampaignId);
                        EcouponCampaignDTO.ExpiresOn = System.DateTime.Now.Date;
                        if (EcouponCampaignDTO.ForAllContact == true)
                        {
                            EcouponCampaignDTO.GroupId = null;
                        }

                        Edit(EcouponCampaignDTO);
                        return IsStatusChanged;
                    }
                    catch
                    {
                        return false;
                    }
                }
                else
                {
                    try
                    {
                        EcouponCampaignDTO EcouponCampaignDTO = new EcouponCampaignDTO();
                        EcouponCampaignDTO = GetById(EcouponCampaignId);
                        EcouponCampaignDTO.ExpiresOn = ExpiredOn;
                        Edit(EcouponCampaignDTO);
                        return true;
                    }
                    catch
                    {
                        return false;
                    }
                }

            }
            catch
            {
                throw;
            }
        }
Ejemplo n.º 27
0
        //Return Coupon list as per mobile and Client id
        public static List<CouponDTO> GetMobileNumberAndClientIdWiseCouponList(String Mobile, int ClientId)
        {
            List<CouponDTO> CouponDTODTOList = new List<CouponDTO>();
            try
            {
                UnitOfWork uow = new UnitOfWork();
                IEnumerable<Coupon> Coupon = uow.CouponRepo.GetAll().Where(e => (e.MobileNumber.Equals(Mobile) && e.IsExpired != true && e.IsRedeem != true));
                if (Coupon != null)
                {
                    foreach (var item in Coupon)
                    {
                        CouponDTO CouponDTO = new CouponDTO();
                        CouponDTO = Transform.CouponToDTO(item);
                        EcouponCampaignDTO EcouponCampaignDTO = new EcouponCampaignDTO();
                        EcouponCampaignDTO = EcouponCampaignService.GetById(CouponDTO.EcouponCampaignId);
                        CouponDTO.CouponCampaignName = EcouponCampaignDTO.Title;
                        CouponDTO.Message = EcouponCampaignDTO.Message;
                        if (EcouponCampaignDTO.ClientId == ClientId)
                        {
                            CouponDTODTOList.Add(CouponDTO);
                        }
                    }
                }

                return CouponDTODTOList;
            }
            catch
            {
                throw;
            }
        }