Exemplo n.º 1
0
        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)
                        {
                            if (item.GroupId == null)
                            {
                                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
        }
Exemplo n.º 2
0
        public static String SplitMobile(string Mobile, EcouponCampaignDTO EcouponCampaignDTO)
        {
            string result = "";
            bool   IsSent = false;

            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
                    {
                    }
                }

                // Modify EcouponCampaign IsSent status
                int TotatCouponSent = CouponService.GetCouponCountByEcouponCampaignId(EcouponCampaignDTO.Id);
                //if (EcouponCampaignDTO.RecipientsCount == TotatCouponSent)
                if (EcouponCampaignDTO.RecipientsCount <= TotatCouponSent)
                {
                    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);
        }