コード例 #1
0
        public void Execute(IJobExecutionContext context)
        {
            try
            {
                Log.Debug("");
                Log.Info("Executing queued message send job");

                IEmailSender                _emailSender           = new DefaultEmailSender();
                QueuedEmailServices         _queuedEmailService    = ServiceProvider.GetQueuedEmailServices();
                QueuedSMSServices           _queuedSMSService      = ServiceProvider.GetQueuedSMSServices();
                ApplicationSettingsServices generalSettingsService = ServiceProvider.GetApplicationSettingsServices();

                var sendTriesStart = 0;
                var sendTriesEnd   = OpenCBSConstants.MessagingMaxSentTries;

                int enableSmsMsg = Convert.ToInt32(generalSettingsService.SelectParameterValue(OGeneralSettings.ENABLE_SMS_MESSAGING));
                Log.Debug("Send SMS status: " + enableSmsMsg);
                if (enableSmsMsg == 1)
                {
                    var queuedsms = _queuedSMSService.SearchSMSs(null, null, null, null, true, sendTriesStart, sendTriesEnd, false, 0, 10000);

                    Log.Debug("");
                    Log.Debug("-------------------------------------");
                    Log.Info("Sending SMS");
                    Log.Debug("Queued sms count: " + queuedsms.Count);

                    foreach (var qe in queuedsms)
                    {
                        try
                        {
                            ISms sms = new EstoreSms();
                            sms.UserName = Convert.ToString(generalSettingsService.SelectParameterValue(OGeneralSettings.SMS_GATEWAY_USERNAME));
                            sms.Password = Convert.ToString(generalSettingsService.SelectParameterValue(OGeneralSettings.SMS_GATEWAY_PASSWORD));

                            sms.Message = qe.Message;
                            sms.AddRecipient(qe.Recipient);
                            sms.MessageFrom = qe.From;

                            qe.Response = sms.SendSms();

                            if (qe.Response.ToLower().Contains("ok"))
                            {
                                qe.SentOnUtc = DateTime.UtcNow;
                            }
                            Log.Info(qe.Response);
                        }
                        catch (Exception exc)
                        {
                            Log.Error(string.Format("Error sending sms: {0}", exc.Message), exc);
                        }
                        finally
                        {
                            qe.SentTries = qe.SentTries + 1;
                            _queuedSMSService.Update(qe);
                        }
                    }
                }

                var queuedEmails = _queuedEmailService.SearchEmails(null, null, null, null, true, sendTriesStart, sendTriesEnd, false, 0, 10000);

                Log.Debug("");
                Log.Debug("");
                Log.Debug("-------------------------------------");
                Log.Info("Sending Emails");
                Log.Debug("Queued email count: " + queuedEmails.Count);

                foreach (var qe in queuedEmails)
                {
                    var bcc = String.IsNullOrWhiteSpace(qe.Bcc)
                                ? null
                                : qe.Bcc.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                    var cc = String.IsNullOrWhiteSpace(qe.CC)
                                ? null
                                : qe.CC.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);

                    try
                    {
                        var smtpContext = new SmtpContext(qe.EmailAccount);

                        var msg = new EmailMessage(
                            new EmailAddress(qe.To, qe.ToName),
                            qe.Subject,
                            qe.Body,
                            new EmailAddress(qe.From, qe.FromName));

                        if (qe.ReplyTo.HasValue())
                        {
                            msg.ReplyTo.Add(new EmailAddress(qe.ReplyTo, qe.ReplyToName));
                        }

                        if (cc != null)
                        {
                            msg.Cc.AddRange(cc.Where(x => x.HasValue()).Select(x => new EmailAddress(x)));
                        }

                        if (bcc != null)
                        {
                            msg.Bcc.AddRange(bcc.Where(x => x.HasValue()).Select(x => new EmailAddress(x)));
                        }

                        _emailSender.SendEmail(smtpContext, msg);

                        qe.SentOnUtc = DateTime.UtcNow;
                    }
                    catch (Exception exc)
                    {
                        Log.Error(string.Format("Error sending e-mail: {0}", exc.Message), exc);
                    }
                    finally
                    {
                        qe.SentTries = qe.SentTries + 1;
                        _queuedEmailService.Update(qe);
                    }
                }
                Log.Info("Queued message send completed");
            }
            catch (Exception exc)
            {
                Log.Error(exc);
            }
        }
コード例 #2
0
        static void Main(string[] args)
        {
            QueuedSMSServices     _queuedSMSService     = ServiceProvider.GetQueuedSMSServices();
            UserServices          _userService          = ServiceProvider.GetUserServices();
            ClientServices        _clientService        = ServiceProvider.GetClientServices();
            SavingServices        _savingService        = ServiceProvider.GetSavingServices();
            PaymentMethodServices _paymentMethodService = ServiceProvider.GetPaymentMethodServices();

            var generalSettingsService = ServicesProvider.GetInstance().GetApplicationSettingsServices();

            generalSettingsService.FillGeneralDatabaseParameter();

            Console.WriteLine("Executing sms charges job");

            var queuedsms = _queuedSMSService.GetUncharged();

            Console.WriteLine("");
            Console.WriteLine("-------------------------------------");
            Console.WriteLine("Charging SMS");
            Console.WriteLine("Uncharged sms count: " + queuedsms.Count);

            decimal smsCharge = Convert.ToDecimal(ApplicationSettings.GetInstance("").GetSpecificParameter(OGeneralSettings.SMS_CHARGE));

            var adminUser     = _userService.Find(1);
            var paymentMethod = _paymentMethodService.GetPaymentMethodByName("Cash");

            foreach (QueuedSMS qe in queuedsms)
            {
                try
                {
                    if (qe.ContractId.HasValue && qe.Charged.HasValue && !qe.Charged.Value)
                    {
                        SavingBookContract _saving = _savingService.GetSaving(qe.ContractId.Value);

                        //Get all sms for same saving book contract
                        var smsGroup = _queuedSMSService.FindByContractId(_saving.Id);

                        string description = "";

                        string formatedAccountNumber = "******" + _saving.Code.Substring(_saving.Code.Length - 4);

                        OCurrency SMSCharges = smsGroup.Count * smsCharge;

                        if (smsGroup.Count > 0)
                        {
                            string   desc  = "SMS charges of {0:.00} for {1:dd.MM.yyyy} - {2:dd.MM.yyyy} : {3}";
                            object[] items = new object[] { SMSCharges.GetFormatedValue(true), smsGroup.First().CreatedOnUtc, smsGroup.Last().CreatedOnUtc, formatedAccountNumber };
                            description = string.Format(desc, items);
                        }
                        if (smsGroup.Count == 0)
                        {
                            string   desc  = "SMS charges of {0:.00} for {1:dd.MM.yyyy} : {3}";
                            object[] items = new object[] { SMSCharges.GetFormatedValue(true), smsGroup.First().CreatedOnUtc, formatedAccountNumber };
                            description = string.Format(desc, items);
                            smsGroup.First().Charged = true;
                        }
                        _savingService.Withdraw(_saving, DateTime.Now, SMSCharges, true, description, "", adminUser,
                                                Teller.CurrentTeller, paymentMethod);

                        qe.Charged = true;
                        foreach (var sms in smsGroup)
                        {
                            sms.Charged = true;
                            queuedsms.Where(s => s.Id == sms.Id).First().Charged = true;
                            _queuedSMSService.Update(sms);
                        }

                        //Send sms charge notification
                        Person person = _clientService.FindPersonById(qe.RecipientId.Value);
                        if (person != null)
                        {
                            if (person.SMSDelivery.HasValue && person.SMSDelivery.Value)
                            {
                                string mfbName = Convert.ToString(ServicesProvider.GetInstance().GetGeneralSettings().GetSpecificParameter(OGeneralSettings.MFI_NAME));
                                //var message = messageTemplate.Body;
                                var messageReplaced = mfbName + " " + description;// Tokenizer.Replace(message, tokens, false);

                                var sms = new QueuedSMS()
                                {
                                    From         = Convert.ToString(ServicesProvider.GetInstance().GetGeneralSettings().GetSpecificParameter(OGeneralSettings.SMS_FROM_NUMBER)),
                                    Recipient    = person.PersonalPhone,
                                    RecipientId  = person.Id,
                                    ContractId   = _saving != null ? _saving.Id : 0,
                                    Charged      = true,
                                    Message      = messageReplaced,
                                    SentTries    = 0,
                                    CreatedOnUtc = DateTime.UtcNow,
                                };

                                _queuedSMSService.Add(sms);
                            }
                        }
                    }
                }
                catch (Exception exc)
                {
                    Console.WriteLine(string.Format("Error charging sms: {0}", exc.Message), exc);
                }
                finally
                {
                }
            }
        }