AppendixNotificationPlugin.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. using GreenTree.Nachtragsmanagement.Core.Plugins;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Web;
  6. using GreenTree.Nachtragsmanagement.Core.Domain.Misc;
  7. using GreenTree.Nachtragsmanagement.Services.User;
  8. using GreenTree.Nachtragsmanagement.Services.Configuration;
  9. using GreenTree.Nachtragsmanagement.Services.Appendix;
  10. using GreenTree.Nachtragsmanagement.Core.Domain.Appendix;
  11. using System.Net.Mail;
  12. using System.Net;
  13. namespace GreenTree.Nachtragsmanagement.Web.Scheduling
  14. {
  15. public class AppendixNotificationPlugin : INotificationPlugin
  16. {
  17. #region Services
  18. private readonly IUserService _userService;
  19. private readonly IConfigurationService _configurationService;
  20. private readonly IAppendixService _appendixService;
  21. #endregion
  22. #region Properties
  23. /// <summary>
  24. /// Id
  25. /// </summary>
  26. public Guid Id
  27. {
  28. get
  29. {
  30. return Guid.Parse("E99CA4A1-B3A9-4AA6-BBAD-9254CEED0A45");
  31. }
  32. }
  33. /// <summary>
  34. /// System name
  35. /// </summary>
  36. public string SystemName
  37. {
  38. get
  39. {
  40. return "GreenTree.Nachtragsmanagement.AppendixNotificationPlugin";
  41. }
  42. }
  43. /// <summary>
  44. /// List of available notification jobs
  45. /// </summary>
  46. public List<NotificationJob> AvailableNotificationJobs
  47. {
  48. get
  49. {
  50. return new List<NotificationJob>
  51. {
  52. new NotificationJob
  53. (
  54. Guid.Parse("2F3642E0-259D-466D-8629-CB279F740313"),
  55. "GreenTree.Nachtragsmanagement.AppendixNotificationPlugin.ProcessNegotiationDate",
  56. "Verhandlungstermine überprüfen",
  57. "Erstellt automatisch Benachrichtigungen für Nachträge, die nach 8 Wochen nach Einreichung noch keinen " +
  58. "Verhandlungstermin gesetzt haben und ändert den entsprechenden Status ab"
  59. ),
  60. new NotificationJob
  61. (
  62. Guid.Parse("2E46B32A-1912-47F9-951E-C8188AA9BA50"),
  63. "GreenTree.Nachtragsmanagement.AppendixNotificationPlugin.ProcessNegotiationProtocol",
  64. "Verhandlungsprotokolle überprüfen",
  65. "Erstellt automatisch Benachrichtigungen für Nachträge, die nach 2 Wochen die zwar verhandelt sind, " +
  66. "jedoch noch kein Protokoll aufweisen."
  67. )
  68. };
  69. }
  70. }
  71. /// <summary>
  72. /// Displayed name
  73. /// </summary>
  74. public string Name
  75. {
  76. get
  77. {
  78. return "Nachtragsbenachrichtigung";
  79. }
  80. }
  81. /// <summary>
  82. /// Further description on how this plugin works
  83. /// </summary>
  84. public string Description
  85. {
  86. get
  87. {
  88. return
  89. "Erstellt automatisch Benachrichtigungen für Nachträge, die nach 8 Wochen nach Einreichung noch keinen " +
  90. "Verhandlungstermin gesetzt haben und ändert den entsprechenden Status ab. Außerdem werden alle 2 Wochen " +
  91. "Benachrichtigungen für Nachträge erstellt, die zwar verhandelt sind, jedoch noch kein Protokoll aufweisen.";
  92. }
  93. }
  94. #endregion
  95. /// <summary>
  96. /// Initializes a new instance of the AppendixNotificationPlugin class
  97. /// </summary>
  98. public AppendixNotificationPlugin() { }
  99. /// <summary>
  100. /// Initializes a new instance of the AppendixNotificationPlugin class
  101. /// </summary>
  102. public AppendixNotificationPlugin(
  103. IUserService userService,
  104. IConfigurationService configurationService,
  105. IAppendixService appendixService)
  106. {
  107. _userService = userService;
  108. _configurationService = configurationService;
  109. _appendixService = appendixService;
  110. }
  111. #region Processing
  112. /// <summary>
  113. /// Process all mail notifications registered for that plugin
  114. /// </summary>
  115. /// <param name="mailNotifications">The notifications which shall be generated.</param>
  116. public void ProcessNotifications(IEnumerable<MailNotification> mailNotifications)
  117. {
  118. if (mailNotifications == null || !mailNotifications.Any()) return;
  119. foreach (var notification in mailNotifications)
  120. {
  121. switch (notification.NotificationJobSystemName)
  122. {
  123. case "GreenTree.Nachtragsmanagement.AppendixNotificationPlugin.ProcessNegotiationDate":
  124. {
  125. var conversionSuccess = false;
  126. var ageDaysConfigItem = _configurationService.GetConfigItemByName(
  127. "GreenTree.Nachtragsmanagement.AppendixNotificationPlugin.ProcessNegotiationDate.AgeDays");
  128. var ageDays = ageDaysConfigItem == null
  129. ? 56
  130. : _configurationService.TryGetConfigItemValue<int>(ageDaysConfigItem, out conversionSuccess);
  131. if (!conversionSuccess)
  132. ageDays = 56;
  133. var stateSetConfigItem = _configurationService.GetConfigItemByName(
  134. "GreenTree.Nachtragsmanagement.AppendixNotificationPlugin.ProcessNegotiationDate.StateSet");
  135. var stateId = stateSetConfigItem == null
  136. ? 2
  137. : _configurationService.TryGetConfigItemValue<int>(stateSetConfigItem, out conversionSuccess);
  138. if (!conversionSuccess)
  139. stateId = 2;
  140. var appendices = _appendixService.GetAllAppendices()
  141. .Where(a => a.OfferingDate <= DateTime.Now.AddDays(ageDays) &&
  142. a.StateId != stateId &&
  143. a.NegotiationDate == null)
  144. .ToList();
  145. var mailBody = GenerateNegotiationDateMailBody(appendices);
  146. foreach (var appendix in appendices)
  147. {
  148. appendix.StateId = stateId;
  149. _appendixService.UpdateAppendix(appendix);
  150. }
  151. SendNotification(notification, "Autom. Übersicht nicht verhandelte Nachträge", mailBody);
  152. }
  153. break;
  154. case "GreenTree.Nachtragsmanagement.AppendixNotificationPlugin.ProcessNegotiationProtocol":
  155. break;
  156. default:
  157. continue;
  158. }
  159. }
  160. }
  161. /// <summary>
  162. /// Sets the corresponding status for appendices which offering date is older than 8 weeks and notifies the correspondig recipients
  163. /// </summary>
  164. private void ProcessNegotiationDateNotification()
  165. {
  166. }
  167. #endregion
  168. #region Mail body generation
  169. /// <summary>
  170. /// Generates a mail body with a list of all appendices with a offering date later than 8 weeks
  171. /// </summary>
  172. /// <param name="appendices">Appendices matching that criteria.</param>
  173. public string GenerateNegotiationDateMailBody(IEnumerable<Appendix> appendices)
  174. {
  175. var template =
  176. "<html>" +
  177. " <body>" +
  178. " <h3>Übersicht \"Nicht verhandelte Nachträge\"</h3>" +
  179. " <p>Folgende Nachträge haben ein Einreichdatum älter als 8 Wochen, jedoch noch kein Verhandlungstermin" +
  180. " gesetzt:</p>" +
  181. " <ul>" +
  182. " {0}" +
  183. " </ul>" +
  184. " </body>" +
  185. "</html>";
  186. if (!appendices.Any()) return String.Format(template, String.Empty);
  187. var appendicesList = String.Empty;
  188. foreach (var appendix in appendices)
  189. {
  190. appendicesList += String.Format(
  191. "<li>Nachtrag <b>\"{0}\"</b> in Baustelle <b>\"{1}\"</b> - Einreichdatum: <b>{2:dd.MM.yyyy}</b>",
  192. appendix.CustomNumber, appendix.Site.CustomNumber, appendix.OfferingDate);
  193. }
  194. return String.Format(template, appendicesList);
  195. }
  196. #endregion
  197. #region Mail sending
  198. /// <summary>
  199. /// Sends a generated mail body to the specified recipients in the mail notification
  200. /// </summary>
  201. /// <param name="mailNotification">The mail notification.</param>
  202. /// <param name="subject">The mail subject.</param>
  203. /// <param name="body">The mail body.</param>
  204. public void SendNotification(MailNotification mailNotification, string subject, string body)
  205. {
  206. if (mailNotification == null) return;
  207. var mailServerConfig = _configurationService.GetCurrentConfiguration().MailServerElement;
  208. var smptClient = new SmtpClient(mailServerConfig.SmtpServer, mailServerConfig.Port)
  209. {
  210. EnableSsl = mailServerConfig.UseSsl,
  211. Credentials = new NetworkCredential(
  212. mailServerConfig.Username,
  213. mailServerConfig.Password,
  214. mailServerConfig.Domain)
  215. };
  216. var recipients =
  217. mailNotification.Users
  218. .Select(u => u.MailAddress);
  219. var mailMessage = new MailMessage
  220. {
  221. IsBodyHtml = true,
  222. Subject = subject,
  223. Body = body,
  224. From = new MailAddress("Nachtragsbenachrichtigung@schweerbau.de")
  225. };
  226. foreach (var recipient in recipients)
  227. mailMessage.To.Add(recipient);
  228. smptClient.Send(mailMessage);
  229. }
  230. #endregion
  231. }
  232. }