123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- 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<ReportExportDefinition> CreateTemplateDefinitions(DataModel model)
- {
- var templates = new Client<DataModelTemplate>().Query(new Filter<DataModelTemplate>(x => x.Model).IsEqualTo(model.Name)
- .And(x => x.Visible).IsEqualTo(true));
- if (templates.Rows.Any())
- {
- List<ReportExportDefinition> list = new List<ReportExportDefinition>();
- foreach (CoreRow row in templates.Rows)
- {
- Action<DataModel, byte[]> action = new Action<DataModel, byte[]>((model, data) =>
- {
- DoEmailAction(model, data, row.Get<DataModelTemplate, string>(x => x.Name));
- });
- list.Add(
- new ReportExportDefinition(
- "Email Report",
- ImageUtils.CreatePreviewWindowButtonContent(row.Get<DataModelTemplate, string>(x => x.Name),PRSDesktop.Resources.emailreport),
- ReportExportType.PDF,
- action));
- }
- return list;
- }
- else
- return new List<ReportExportDefinition>()
- {
- 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<DataModelTemplate>(x => x.Name).IsEqualTo(templateName))
- .ToObjects<DataModelTemplate>().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<Requisition>())
- {
- CoreTable table = model.GetTable<Requisition>();
- title = title + " - " + table.Rows.FirstOrDefault().Get<Requisition, string>(x => x.Title);
- }
- else if (model.HasTable<PurchaseOrder>())
- {
- title = "Purchase Order ";
- CoreTable table = model.GetTable<PurchaseOrder>();
- if (table.Rows.Count == 1)
- title += table.Rows.FirstOrDefault().Get<PurchaseOrder, string>(x => x.PONumber);
- else if (table.Rows.Count > 1)
- {
- foreach (CoreRow row in table.Rows)
- {
- title = title + row.Get<PurchaseOrder, string>(x => x.PONumber) + ", ";
- }
- title = title.Substring(0, title.Length - 2);
- }
- }
- return title;
- }
- }
|