public ActionResult InsertPastHistory(string employeeIds, DateTime dated) { Console.WriteLine(dated); var ids = employeeIds.Split(','); var history = new History(); foreach (var id in ids) { history.EmployeeID = Int32.Parse(id); history.Dated = dated; db.Histories.Add(history); } try { db.SaveChanges(); } catch { throw new Exception("We can't save the modified data."); } return RedirectToAction("Index"); }
public ActionResult ProcessPayment(EmailDetailViewModel emailInfo) { var selectedEmployeesForPayment = (List<Employee>) TempData["SelectedEmployees"]; emailInfo.EmailBody = emailInfo.EmailBody.Replace("$amount", AppConfig.Config.DefaultBeveragePrice.ToString()); foreach (var employee in selectedEmployeesForPayment) { employee.Cycle = employee.Cycle + 1; employee.LastPaymentDate = DateTime.Now; if (ModelState.IsValid) { db.Entry(employee).State = System.Data.Entity.EntityState.Modified; History history = new History(); history.EmployeeID = employee.EmployeeID; history.Dated = DateTime.Now; history.WeekNumber = AppConfig.Config.CurrentRunningCycle; history.Amount = (int) AppConfig.Config.DefaultBeveragePrice; db.Histories.Add(history); } } try { db.SaveChanges(); } catch { throw new Exception("We can't save the modified data."); } string timeStamp = DateTime.Now.ToString("dd_MMM_yy_h_mm_ss_tt"); string folderPath = DirectoryExtension.GetBaseOrAppDirectory() + "ExcelFiles\\"; var attachmentFilePathAndName = folderPath + timeStamp + ".xls"; var lastTwoYearsHistories = _logic.GetLastTwoYearsHistories(DateTime.Now); ExcelFileCreation(lastTwoYearsHistories, attachmentFilePathAndName); #region Thread for mailing and excel deletion var thread = new Thread(() => { List<Attachment> attachments = new List<Attachment>() { new Attachment(attachmentFilePathAndName) }; attachments[0].Name = AppConfig.Config.EmailAttachmentName + ".xls"; string[] employeeEmails = new string[1]; employeeEmails[0] = selectedEmployeesForPayment.FirstOrDefault().Email; MailSendingWrapper mailWrapper = Mvc.Mailer.GetMailSendingWrapper(employeeEmails, emailInfo.EmailSubject, emailInfo.EmailBody, null, attachments, MailingType.MailBlindCarbonCopy); foreach (var employee in selectedEmployeesForPayment) { employeeEmails[0] = employee.Email; mailWrapper = Mvc.Mailer.GetMailSendingWrapper(employeeEmails, emailInfo.EmailSubject, emailInfo.EmailBody.Replace("$name", employee.Name), null, attachments, MailingType.MailBlindCarbonCopy); Mvc.Mailer.SendMail(mailWrapper, false); } mailWrapper.MailMessage.Dispose(); mailWrapper.MailServer.Dispose(); attachments[0] = null; attachments = null; GC.Collect(); System.IO.File.Delete(attachmentFilePathAndName); }); #endregion thread.Start(); return RedirectToAction("Index"); }