EmailUtils.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using InABox.Clients;
  2. using InABox.Core;
  3. using InABox.Reports;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Diagnostics;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Net.Mail;
  10. using System.Reflection;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows.Controls;
  14. using System.Windows;
  15. using System.Windows.Forms;
  16. using System.Drawing;
  17. using InABox.WPF;
  18. using MessageBox = System.Windows.Forms.MessageBox;
  19. using Comal.Classes;
  20. using TextBox = System.Windows.Controls.TextBox;
  21. using InABox.Wpf.Reports;
  22. using InABox.Wpf;
  23. namespace PRSDesktop;
  24. public class PRSEmailUtils
  25. {
  26. public static IEnumerable<ReportExportDefinition> CreateTemplateDefinitions(DataModel model)
  27. {
  28. var templates = new Client<DataModelTemplate>().Query(new Filter<DataModelTemplate>(x => x.Model).IsEqualTo(model.Name)
  29. .And(x => x.Visible).IsEqualTo(true));
  30. if (templates.Rows.Any())
  31. {
  32. List<ReportExportDefinition> list = new List<ReportExportDefinition>();
  33. foreach (CoreRow row in templates.Rows)
  34. {
  35. Action<DataModel, byte[]> action = new Action<DataModel, byte[]>((model, data) =>
  36. {
  37. DoEmailAction(model, data, row.Get<DataModelTemplate, string>(x => x.Name));
  38. });
  39. list.Add(
  40. new ReportExportDefinition(
  41. "Email Report",
  42. ImageUtils.CreatePreviewWindowButtonContent(row.Get<DataModelTemplate, string>(x => x.Name),PRSDesktop.Resources.emailreport),
  43. ReportExportType.PDF,
  44. action));
  45. }
  46. return list;
  47. }
  48. else
  49. return new List<ReportExportDefinition>()
  50. {
  51. new ReportExportDefinition(
  52. "Email Report",
  53. ImageUtils.CreatePreviewWindowButtonContent("Email",PRSDesktop.Resources.emailreport),
  54. ReportExportType.PDF,
  55. DoEmailReport)
  56. };
  57. }
  58. private static void DoEmailAction(DataModel model, byte[] data, string templateName)
  59. {
  60. var template = Client.Query(new Filter<DataModelTemplate>(x => x.Name).IsEqualTo(templateName))
  61. .ToObjects<DataModelTemplate>().First();
  62. ParseTemplateAndCreateEmail(template, model, data);
  63. }
  64. private static void ParseTemplateAndCreateEmail(DataModelTemplate template, DataModel model, byte[] data)
  65. {
  66. var to = DataModelUtils.ParseTemplate(model, template.To).Replace("\n", "").Replace("\r", "");
  67. var Subject = DataModelUtils.ParseTemplate(model, template.Subject).Replace("\n", "").Replace("\r", "");
  68. var attachmentName = DataModelUtils.ParseTemplate(model, template.AttachmentName).Replace("\n", "").Replace("\r", "");
  69. var body = DataModelUtils.ParseTemplate(model, template.Template);
  70. if (string.IsNullOrWhiteSpace(attachmentName))
  71. attachmentName = model.Name;
  72. EmailUtils.CreateEMLFile(attachmentName, data, App.EmployeeEmail, Subject, body, to);
  73. }
  74. public static void DoEmailReport(DataModel model, byte[] data)
  75. {
  76. string attachmentName = DetermineName(model);
  77. EmailUtils.CreateEMLFile(attachmentName, data, App.EmployeeEmail, "Emailing report for " + attachmentName);
  78. }
  79. private static string DetermineName(DataModel model)
  80. {
  81. string title = model.Name;
  82. if (model.HasTable<Requisition>())
  83. {
  84. CoreTable table = model.GetTable<Requisition>();
  85. title = title + " - " + table.Rows.FirstOrDefault().Get<Requisition, string>(x => x.Title);
  86. }
  87. else if (model.HasTable<PurchaseOrder>())
  88. {
  89. title = "Purchase Order ";
  90. CoreTable table = model.GetTable<PurchaseOrder>();
  91. if (table.Rows.Count == 1)
  92. title += table.Rows.FirstOrDefault().Get<PurchaseOrder, string>(x => x.PONumber);
  93. else if (table.Rows.Count > 1)
  94. {
  95. foreach (CoreRow row in table.Rows)
  96. {
  97. title = title + row.Get<PurchaseOrder, string>(x => x.PONumber) + ", ";
  98. }
  99. title = title.Substring(0, title.Length - 2);
  100. }
  101. }
  102. return title;
  103. }
  104. }