/// <summary> /// Helper method to create CompanyInvoiceEmailEditData objects /// </summary> /// <param name="ci">The company invoice from which to take the id and company id</param> /// <returns>A CompanyInvoiceEmailEditData object from the CompanyInvoice</returns> private CompanyInvoiceEmailEditData MakeEmailEditData(CompanyInvoice ci) { var cieed = new CompanyInvoiceEmailEditData { //TODO: populate subject and body with default data from settings data SelectedRecipiants = new int[0], CompanyInvoiceId = ci.id, PeopleChoices = _repository.GetAllCompanyPersons().Where( x => x.company_id == ci.company_id) .OrderByDescending(x => x.is_contact), InvoiceAttachmentFileName = "Invoice" + ci.id + ".pdf"//TODO: localize }; return cieed; }
public ActionResult Email(CompanyInvoiceEmailEditData cieed) { var ci = _repository.GetCompanyInvoiceById(cieed.CompanyInvoiceId); var message = new MailMessage(); if (ci == null) { return View("CompanyInvoiceNotFound"); } if (cieed.SelectedRecipiants != null) { foreach (var cp in cieed.SelectedRecipiants .Select(i => _repository.GetCompanyPersonById(i)) .Where(cp => cp != null && cp.company_id == ci.company_id)) { message.To.Add(cp.People.email); } } if (message.To.Count == 0) { ModelState.AddModelError("SelectedRecipiants", "No valid recipiants chosen"); } if (ModelState.IsValid) { try { message.Body = cieed.Body; message.Subject = cieed.Subject; message.Attachments.Add(new Attachment(ci.ToPdf(), cieed.InvoiceAttachmentFileName, "application/pdf")); Mailer.Send(message); ci.last_sent = DateTime.Now; _repository.Save(); TempData["Message"] = "Message sent successfully"; } catch { TempData["Message"] = "Email failed to send"; } return RedirectToAction("Edit", new {ci.id}); } cieed.PeopleChoices = _repository.GetAllCompanyPersons().Where(x => x.company_id == ci.company_id) .OrderByDescending(x => x.is_contact); return View("Email", cieed); }