using InABox.Clients; using InABox.Core; using InABox.Reports; using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Net.Mail; using System.Reflection; using System.Text; using System.Threading.Tasks; using System.Windows.Controls; using System.Windows; using System.Windows.Forms; using System.Drawing; using InABox.WPF; using MessageBox = System.Windows.Forms.MessageBox; using Comal.Classes; using TextBox = System.Windows.Controls.TextBox; using InABox.Wpf.Reports; using InABox.Wpf; namespace PRSDesktop; public class PRSEmailUtils { public static IEnumerable CreateTemplateDefinitions(DataModel model) { var templates = new Client().Query(new Filter(x => x.Model).IsEqualTo(model.Name) .And(x => x.Visible).IsEqualTo(true)); if (templates.Rows.Any()) { List list = new List(); foreach (CoreRow row in templates.Rows) { Action action = new Action((model, data) => { DoEmailAction(model, data, row.Get(x => x.Name)); }); list.Add( new ReportExportDefinition( "Email Report", ImageUtils.CreatePreviewWindowButtonContent(row.Get(x => x.Name),PRSDesktop.Resources.emailreport), ReportExportType.PDF, action)); } return list; } else return new List() { new ReportExportDefinition( "Email Report", ImageUtils.CreatePreviewWindowButtonContent("Email",PRSDesktop.Resources.emailreport), ReportExportType.PDF, DoEmailReport) }; } private static void DoEmailAction(DataModel model, byte[] data, string templateName) { var template = Client.Query(new Filter(x => x.Name).IsEqualTo(templateName)) .ToObjects().First(); ParseTemplateAndCreateEmail(template, model, data); } private static void ParseTemplateAndCreateEmail(DataModelTemplate template, DataModel model, byte[] data) { var to = DataModelUtils.ParseTemplate(model, template.To).Replace("\n", "").Replace("\r", ""); var Subject = DataModelUtils.ParseTemplate(model, template.Subject).Replace("\n", "").Replace("\r", ""); var attachmentName = DataModelUtils.ParseTemplate(model, template.AttachmentName).Replace("\n", "").Replace("\r", ""); var body = DataModelUtils.ParseTemplate(model, template.Template); if (string.IsNullOrWhiteSpace(attachmentName)) attachmentName = model.Name; EmailUtils.CreateEMLFile(attachmentName, data, App.EmployeeEmail, Subject, body, to); } public static void DoEmailReport(DataModel model, byte[] data) { string attachmentName = DetermineName(model); EmailUtils.CreateEMLFile(attachmentName, data, App.EmployeeEmail, "Emailing report for " + attachmentName); } private static string DetermineName(DataModel model) { string title = model.Name; if (model.HasTable()) { CoreTable table = model.GetTable(); title = title + " - " + table.Rows.FirstOrDefault().Get(x => x.Title); } else if (model.HasTable()) { title = "Purchase Order "; CoreTable table = model.GetTable(); if (table.Rows.Count == 1) title += table.Rows.FirstOrDefault().Get(x => x.PONumber); else if (table.Rows.Count > 1) { foreach (CoreRow row in table.Rows) { title = title + row.Get(x => x.PONumber) + ", "; } title = title.Substring(0, title.Length - 2); } } return title; } }