public ActionResult SendHasNotifications(DateTime from, DateTime to)
 {
     using (var j = new CC.Web.Jobs.HcStatusChangeNotificationsJob())
     {
         var data         = db.spHasNotificationResend(from, to).ToList();
         var groupedBySer = data.GroupBy(f => f.AgencyGroupId).Select(f => new ClientsHcStatusChangeEmailModel
         {
             AgencyGroupId = f.Key,
             Clients       = f.Select(c => new ClientHcStatusChangeEmailModel
             {
                 ClientId           = c.ClientId,
                 ClientName         = c.FirstName + " " + c.LastName,
                 ApprovalStatusName = ((ApprovalStatusEnum)c.NewApprovalStatusId).DisplayName(),
                 BirthCountryName   = c.BirthCountryName,
                 CountryName        = c.CountryName,
                 HcStatusName       = c.HcStatusName,
                 AgencyId           = c.AgencyId
             })
         });
         j.SendNotifications(groupedBySer);
         return(this.Json(groupedBySer));
     }
 }
        public ActionResult Import(Guid id)
        {
            try
            {
                var clients             = db.ImportClientFundStatusProc(id).ToList();
                var IsraelCashSerNumber = System.Web.Configuration.WebConfigurationManager.AppSettings["IsraelCashSerNumber"].Parse <int>();
                var cfsClients          = clients.Where(c => c.AgencyGroupId == IsraelCashSerNumber).ToList();
                var context             = System.Web.HttpContext.Current;
                new Thread(delegate()
                {
                    try
                    {
                        var sendClients        = clients.Where(c => c.LeaveReasonId == null || c.LeaveReasonId != (int)LeaveReasonEnum.Deceased).ToList();
                        var approvalStatusSend = new List <ImportClientFundStatusProc_Result>();
                        var hcStatusSend       = new List <ImportClientFundStatusProc_Result>();
                        foreach (var c in sendClients)
                        {
                            //sp returns clients with approval OR hc status changed
                            if (c.OldHcStatusId != c.NewHcStatusId && c.OldApprovalStatusId == c.NewApprovalStatusId)
                            {
                                hcStatusSend.Add(c);
                            }
                            else if (c.OldApprovalStatusId != c.NewApprovalStatusId)
                            {
                                approvalStatusSend.Add(c);
                            }
                        }

                        if (approvalStatusSend.Count > 0)
                        {
                            SendStatusChangeNotifications(sendClients); //set as job
                        }
                        if (hcStatusSend.Any())
                        {
                            using (var j = new CC.Web.Jobs.HcStatusChangeNotificationsJob())
                            {
                                var groupedBySer = hcStatusSend.GroupBy(f => f.AgencyGroupId).Select(f => new ClientsHcStatusChangeEmailModel {
                                    AgencyGroupId = f.Key,
                                    Clients       = f.Select(c => new ClientHcStatusChangeEmailModel {
                                        ClientId           = c.ClientId ?? 0,
                                        ClientName         = c.FirstName + " " + c.LastName,
                                        ApprovalStatusName = ((ApprovalStatusEnum)c.NewApprovalStatusId).DisplayName(),
                                        BirthCountryName   = c.BirthCountryName,
                                        CountryName        = c.CountryName,
                                        HcStatusName       = c.HcStatusName,
                                        AgencyId           = c.AgencyId
                                    })
                                });
                                j.SendNotifications(groupedBySer);
                            }
                        }
                        if (cfsClients.Count > 0)
                        {
                            CC.Web.Controllers.ClientsController.ExportCsvForChangedApprovalStatus(cfsClients);
                        }
                    }
                    catch (Exception ex)
                    {
                        _log.Error(ex);
                    }
                }).Start();

                return(RedirectToAction("Index"));
            }
            catch (System.Data.EntityCommandExecutionException ex)
            {
                _log.Fatal(ex.Message, ex);
                ModelState.AddModelError(string.Empty, ex.InnerException.Message);
            }
            catch (Exception ex)
            {
                _log.Fatal(ex.Message, ex);
                ModelState.AddModelError(string.Empty, ex);
            }
            return(View("Preview", id));
        }