private IEnumerable <TemplateEmail> RunReport(EmployerReport report, VerifiedOrganisation organisation, DateTime startDate, DateTime endDate, IAdministrator accountManager) { // First work out who we need to send it to var contactDetails = report.SendToClient ? _organisationsQuery.GetEffectiveContactDetails(organisation.Id) : null; var accountManagerEmail = report.SendToAccountManager && accountManager.IsEnabled ? new AccountManagerReportEmail((ICommunicationUser)accountManager, report, organisation, startDate, endDate) : null; if (contactDetails == null && accountManagerEmail == null) { return(new TemplateEmail[0]); // No-one to send the report to. } // Run the report and attach it to the email(s). string fileName = GetFilename(report, organisation, ".pdf"); var pdfStream = new MemoryStream(); var sb = new StringBuilder(); var outcome = _executeEmployerReportsCommand.RunReport(report, true, organisation, accountManager, new DateRange(startDate, endDate), null, pdfStream, sb); if (outcome == ReportRunOutcome.InvalidParameters) { return(new TemplateEmail[0]); } if (accountManagerEmail != null) { switch (outcome) { case ReportRunOutcome.TextResultOnly: accountManagerEmail.AddCustomHtml(GetResultsHtml(report, organisation, sb.ToString())); break; case ReportRunOutcome.NoResults: accountManagerEmail.AddNoActivityReport(report); break; } } var allEmails = new List <TemplateEmail>(); if (accountManagerEmail != null) { allEmails.Add(accountManagerEmail); } if (contactDetails != null && (report.ReportAsFile || sb.Length != 0)) { if (!string.IsNullOrEmpty(contactDetails.EmailAddress)) { allEmails.Add(CreateEmail( new Employer { EmailAddress = new EmailAddress { Address = contactDetails.EmailAddress }, FirstName = contactDetails.FirstName, LastName = contactDetails.LastName, IsEnabled = true }, (ICommunicationUser)accountManager, report, organisation, startDate, endDate, outcome, sb.ToString(), _memberCount)); } if (!string.IsNullOrEmpty(contactDetails.SecondaryEmailAddresses)) { var secondaryEmailAddresses = TextUtil.SplitEmailAddresses(contactDetails.SecondaryEmailAddresses); if (secondaryEmailAddresses != null) { foreach (var secondaryEmailAddress in secondaryEmailAddresses) { allEmails.Add(CreateEmail( new Employer { EmailAddress = new EmailAddress { Address = secondaryEmailAddress }, IsEnabled = true }, (ICommunicationUser)accountManager, report, organisation, startDate, endDate, outcome, sb.ToString(), _memberCount)); } } } } if (outcome == ReportRunOutcome.FileResult || report.ReportFileEvenIfNoResults) { foreach (var email in allEmails) { if (email != null) { // The same Stream can't be used for multiple attachments, so clone it. var attachmentStream = new MemoryStream(pdfStream.ToArray()); email.AddAttachments(new[] { new ContentAttachment(attachmentStream, fileName, MediaType.Pdf) }); } } } return(allEmails); }
private ActionResult RunFileReport(Guid id, string type, bool includeCredits, DateTime?startDate, DateTime?endDate, bool isXls) { var organisation = _organisationsQuery.GetOrganisation(id); if (organisation == null) { return(NotFound("organisation", "id", id)); } var report = _employerReportsQuery.GetReport(id, type) ?? _employerReportsCommand.CreateReportTemplate(id, type); var accountManager = GetAccountManager(organisation); try { var errors = new List <ValidationError>(); if (startDate == null) { errors.Add(new RequiredValidationError("StartDate")); } if (endDate == null) { errors.Add(new RequiredValidationError("EndDate")); } if (errors.Count > 0) { throw new ValidationErrorsException(errors); } // Run the report. var output = new MemoryStream(); var sb = new StringBuilder(); var outcome = _executeEmployerReportsCommand.RunReport( report, includeCredits, organisation, accountManager, new DateRange(startDate.Value, endDate.Value), isXls ? output : null, !isXls ? output : null, sb); // Either report an error or return the file. switch (outcome) { case ReportRunOutcome.InvalidParameters: ModelState.AddModelError("The report parameters are invalid."); break; case ReportRunOutcome.NoResults: ModelState.AddModelError("No results were returned for the specified criteria."); break; case ReportRunOutcome.FileResult: output.Seek(0, SeekOrigin.Begin); var fileName = FileSystem.GetValidFileName(organisation.FullName.Replace(Organisation.FullNameSeparator, '-') + " - " + report.Name) + (isXls ? ".xls" : ".pdf"); return(new FileStreamResult(output, isXls ? MediaType.Excel : MediaType.Pdf) { FileDownloadName = fileName }); } } catch (UserException ex) { ModelState.AddModelError(ex, new StandardErrorHandler()); } return(View("Report", new ReportModel { Organisation = organisation, AccountManager = accountManager, ContactDetails = GetContactDetails(organisation), Report = report, IncludeCredits = includeCredits, StartDate = startDate, EndDate = endDate })); }