AppendixNotificationPlugin.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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. using System.Globalization;
  14. namespace GreenTree.Nachtragsmanagement.Web.Scheduling
  15. {
  16. public class AppendixNotificationPlugin : INotificationPlugin
  17. {
  18. #region Services
  19. private readonly IUserService _userService;
  20. private readonly IConfigurationService _configurationService;
  21. private readonly IAppendixService _appendixService;
  22. #endregion
  23. #region Properties
  24. /// <summary>
  25. /// Id
  26. /// </summary>
  27. public Guid Id
  28. {
  29. get
  30. {
  31. return Guid.Parse("E99CA4A1-B3A9-4AA6-BBAD-9254CEED0A45");
  32. }
  33. }
  34. /// <summary>
  35. /// System name
  36. /// </summary>
  37. public string SystemName
  38. {
  39. get
  40. {
  41. return "GreenTree.Nachtragsmanagement.AppendixNotificationPlugin";
  42. }
  43. }
  44. /// <summary>
  45. /// List of available notification jobs
  46. /// </summary>
  47. public List<NotificationJob> AvailableNotificationJobs
  48. {
  49. get
  50. {
  51. return new List<NotificationJob>
  52. {
  53. new NotificationJob
  54. (
  55. Guid.Parse("2F3642E0-259D-466D-8629-CB279F740313"),
  56. "GreenTree.Nachtragsmanagement.AppendixNotificationPlugin.ProcessNegotiationDate",
  57. "Verhandlungstermine überprüfen",
  58. "Erstellt automatisch Benachrichtigungen für Nachträge, die nach 8 Wochen nach Einreichung noch keinen " +
  59. "Verhandlungstermin gesetzt haben und ändert den entsprechenden Status ab"
  60. ),
  61. new NotificationJob
  62. (
  63. Guid.Parse("2E46B32A-1912-47F9-951E-C8188AA9BA50"),
  64. "GreenTree.Nachtragsmanagement.AppendixNotificationPlugin.ProcessNegotiationProtocol",
  65. "Verhandlungsprotokolle überprüfen",
  66. "Erstellt automatisch Benachrichtigungen für Nachträge, die nach 2 Wochen die zwar verhandelt sind, " +
  67. "jedoch noch kein Protokoll aufweisen."
  68. )
  69. };
  70. }
  71. }
  72. /// <summary>
  73. /// Displayed name
  74. /// </summary>
  75. public string Name
  76. {
  77. get
  78. {
  79. return "Nachtragsbenachrichtigung";
  80. }
  81. }
  82. /// <summary>
  83. /// Further description on how this plugin works
  84. /// </summary>
  85. public string Description
  86. {
  87. get
  88. {
  89. return
  90. "Erstellt automatisch Benachrichtigungen für Nachträge, die nach 8 Wochen nach Einreichung noch keinen " +
  91. "Verhandlungstermin gesetzt haben und ändert den entsprechenden Status ab. Außerdem werden alle 2 Wochen " +
  92. "Benachrichtigungen für Nachträge erstellt, die zwar verhandelt sind, jedoch noch kein Protokoll aufweisen.";
  93. }
  94. }
  95. #endregion
  96. /// <summary>
  97. /// Initializes a new instance of the AppendixNotificationPlugin class
  98. /// </summary>
  99. public AppendixNotificationPlugin() { }
  100. /// <summary>
  101. /// Initializes a new instance of the AppendixNotificationPlugin class
  102. /// </summary>
  103. public AppendixNotificationPlugin(
  104. IUserService userService,
  105. IConfigurationService configurationService,
  106. IAppendixService appendixService)
  107. {
  108. _userService = userService;
  109. _configurationService = configurationService;
  110. _appendixService = appendixService;
  111. }
  112. #region Processing
  113. /// <summary>
  114. /// Process all mail notifications registered for that plugin
  115. /// </summary>
  116. /// <param name="mailNotifications">The notifications which shall be generated.</param>
  117. public void ProcessNotifications(IEnumerable<MailNotification> mailNotifications)
  118. {
  119. if (mailNotifications == null || !mailNotifications.Any()) return;
  120. foreach (var notification in mailNotifications)
  121. {
  122. switch (notification.NotificationJobSystemName)
  123. {
  124. case "GreenTree.Nachtragsmanagement.AppendixNotificationPlugin.ProcessNegotiationDate":
  125. {
  126. ProcessNegotiationDateNotification(notification);
  127. }
  128. break;
  129. case "GreenTree.Nachtragsmanagement.AppendixNotificationPlugin.ProcessNegotiationProtocol":
  130. {
  131. ProcessNegotiationProtocolNotification(notification);
  132. }
  133. break;
  134. default:
  135. continue;
  136. }
  137. }
  138. }
  139. /// <summary>
  140. /// Sets the corresponding status for appendices which offering date is older than N weeks and notifies
  141. /// the correspondig recipients
  142. /// </summary>
  143. /// <param name="mailNotification">The notification which shall be generated.</param>
  144. private void ProcessNegotiationDateNotification(MailNotification mailNotification)
  145. {
  146. var ageDays = _configurationService.TryGetConfigItemValue<int>(
  147. "GreenTree.Nachtragsmanagement.AppendixNotificationPlugin.ProcessNegotiationDate.AgeDays", 56);
  148. var stateConditionId = _configurationService.TryGetConfigItemValue<int>(
  149. "GreenTree.Nachtragsmanagement.AppendixNotificationPlugin.ProcessNegotiationDate.StateCondition", 1);
  150. var stateSetId = _configurationService.TryGetConfigItemValue<int>(
  151. "GreenTree.Nachtragsmanagement.AppendixNotificationPlugin.ProcessNegotiationDate.StateSet", 2);
  152. var interval = _configurationService.TryGetConfigItemValue<int>(
  153. "GreenTree.Nachtragsmanagement.AppendixNotificationPlugin.ProcessNegotiationDate.Interval", 2);
  154. interval = interval == 0
  155. ? 1
  156. : interval;
  157. var appendices = _appendixService.GetAllAppendices()
  158. .Where(a => a.OfferingDate.HasValue &&
  159. a.OfferingDate <= DateTime.Now.AddDays(ageDays) &&
  160. a.StateId == stateConditionId &&
  161. a.NegotiationDate == null)
  162. .ToList();
  163. var currentCalendarWeek = GetCalendarWeek(DateTime.Now);
  164. appendices = appendices
  165. .Where(a => ((currentCalendarWeek - GetCalendarWeek(a.OfferingDate.Value) % interval == 0)))
  166. .ToList();
  167. if (appendices.Any())
  168. {
  169. var mailBody = GenerateNegotiationDateMailBody(appendices);
  170. foreach (var appendix in appendices)
  171. {
  172. appendix.StateId = stateSetId;
  173. _appendixService.UpdateAppendix(appendix);
  174. }
  175. SendNotification(mailNotification, "Autom. Übersicht nicht verhandelte Nachträge", mailBody);
  176. }
  177. }
  178. /// <summary>
  179. /// Sets the corresponding status for appendices which negotiation date is older than N weeks and notifies
  180. /// the correspondig recipients
  181. /// </summary>
  182. /// <param name="mailNotification">The notification which shall be generated.</param>
  183. private void ProcessNegotiationProtocolNotification(MailNotification mailNotification)
  184. {
  185. var ageDays = _configurationService.TryGetConfigItemValue<int>(
  186. "GreenTree.Nachtragsmanagement.AppendixNotificationPlugin.ProcessNegotiationProtocol.AgeDays", 14);
  187. var stateConditionId = _configurationService.TryGetConfigItemValue<int>(
  188. "GreenTree.Nachtragsmanagement.AppendixNotificationPlugin.ProcessNegotiationProtocol.StateCondition", 3);
  189. var interval = _configurationService.TryGetConfigItemValue<int>(
  190. "GreenTree.Nachtragsmanagement.AppendixNotificationPlugin.ProcessNegotiationProtocol.Interval", 2);
  191. interval = interval == 0
  192. ? 1
  193. : interval;
  194. var appendices = _appendixService.GetAllAppendices()
  195. .Where(a => a.NegotiationDate.HasValue &&
  196. a.NegotiationDate <= DateTime.Now.AddDays(ageDays) &&
  197. a.StateId == stateConditionId)
  198. .ToList();
  199. var currentCalendarWeek = GetCalendarWeek(DateTime.Now);
  200. appendices = appendices
  201. .Where(a => ((currentCalendarWeek - GetCalendarWeek(a.OfferingDate.Value) % interval == 0)))
  202. .ToList();
  203. if (appendices.Any())
  204. {
  205. var mailBody = GenerateNegotiationProtocolMailBody(appendices);
  206. SendNotification(mailNotification, "Autom. Übersicht verhandelte Nachträge o. Protokoll", mailBody);
  207. }
  208. }
  209. #endregion
  210. #region Mail body generation
  211. /// <summary>
  212. /// Generates a mail body with a list of all appendices with a offering date later than N weeks
  213. /// </summary>
  214. /// <param name="appendices">Appendices matching that criteria.</param>
  215. public string GenerateNegotiationDateMailBody(IEnumerable<Appendix> appendices)
  216. {
  217. var template =
  218. "<html>" +
  219. " <body>" +
  220. " <h3>Übersicht \"Nicht verhandelte Nachträge\"</h3>" +
  221. " <p>Folgende Nachträge haben ein Einreichdatum älter als 8 Wochen, jedoch noch kein Verhandlungstermin" +
  222. " gesetzt:</p>" +
  223. " <ul>" +
  224. " {0}" +
  225. " </ul>" +
  226. " </body>" +
  227. "</html>";
  228. if (!appendices.Any()) return String.Format(template, String.Empty);
  229. var appendicesList = String.Empty;
  230. foreach (var appendix in appendices)
  231. {
  232. appendicesList += String.Format(
  233. "<li>Nachtrag <b>\"{0}\"</b> in Baustelle <b>\"{1}\"</b> - Einreichdatum: <b>{2:dd.MM.yyyy}</b>",
  234. appendix.CustomNumber, appendix.Site.CustomNumber, appendix.OfferingDate);
  235. }
  236. return String.Format(template, appendicesList);
  237. }
  238. /// <summary>
  239. /// Generates a mail body with a list of all appendices with a negotiation date later than N weeks
  240. /// </summary>
  241. /// <param name="appendices">Appendices matching that criteria.</param>
  242. public string GenerateNegotiationProtocolMailBody(IEnumerable<Appendix> appendices)
  243. {
  244. var template =
  245. "<html>" +
  246. " <body>" +
  247. " <h3>Übersicht \"Verhandelte Nachträge ohne Protokoll\"</h3>" +
  248. " <p>Folgende Nachträge haben ein Verhandlungstermin älter als zwei Wochen, jedoch noch kein Protokoll:" +
  249. " <ul>" +
  250. " {0}" +
  251. " </ul>" +
  252. " </body>" +
  253. "</html>";
  254. if (!appendices.Any()) return String.Format(template, String.Empty);
  255. var appendicesList = String.Empty;
  256. foreach (var appendix in appendices)
  257. {
  258. appendicesList += String.Format(
  259. "<li>Nachtrag <b>\"{0}\"</b> in Baustelle <b>\"{1}\"</b> - Verhandlungsdatum: <b>{2:dd.MM.yyyy}</b>",
  260. appendix.CustomNumber, appendix.Site.CustomNumber, appendix.NegotiationDate);
  261. }
  262. return String.Format(template, appendicesList);
  263. }
  264. #endregion
  265. #region Mail sending
  266. /// <summary>
  267. /// Sends a generated mail body to the specified recipients in the mail notification
  268. /// </summary>
  269. /// <param name="mailNotification">The mail notification.</param>
  270. /// <param name="subject">The mail subject.</param>
  271. /// <param name="body">The mail body.</param>
  272. public void SendNotification(MailNotification mailNotification, string subject, string body)
  273. {
  274. if (mailNotification == null) return;
  275. var mailServerConfig = _configurationService.GetCurrentConfiguration().MailServerElement;
  276. var smptClient = new SmtpClient(mailServerConfig.SmtpServer, mailServerConfig.Port)
  277. {
  278. EnableSsl = mailServerConfig.UseSsl,
  279. Credentials = new NetworkCredential(
  280. mailServerConfig.Username,
  281. mailServerConfig.Password,
  282. mailServerConfig.Domain)
  283. };
  284. var recipients =
  285. mailNotification.Users
  286. .Select(u => u.MailAddress);
  287. var mailMessage = new MailMessage
  288. {
  289. IsBodyHtml = true,
  290. Subject = subject,
  291. Body = body,
  292. From = new MailAddress("Nachtragsbenachrichtigung@schweerbau.de")
  293. };
  294. foreach (var recipient in recipients)
  295. mailMessage.To.Add(recipient);
  296. smptClient.Send(mailMessage);
  297. }
  298. #endregion
  299. #region Helper
  300. /// <summary>
  301. /// Determines the calendar week of a specific datetime
  302. /// </summary>
  303. /// <param name="date">The datetime which calendarweek should be calculated.</param>
  304. public static int GetCalendarWeek(DateTime date)
  305. {
  306. var currentCulture = CultureInfo.CurrentCulture;
  307. var calendar = currentCulture.Calendar;
  308. var calendarWeek = calendar.GetWeekOfYear(
  309. date,
  310. currentCulture.DateTimeFormat.CalendarWeekRule,
  311. currentCulture.DateTimeFormat.FirstDayOfWeek);
  312. if (calendarWeek > 52)
  313. {
  314. date = date.AddDays(7);
  315. var testCalendarWeek = calendar.GetWeekOfYear(date,
  316. currentCulture.DateTimeFormat.CalendarWeekRule,
  317. currentCulture.DateTimeFormat.FirstDayOfWeek);
  318. if (testCalendarWeek == 2)
  319. calendarWeek = 1;
  320. }
  321. return calendarWeek;
  322. }
  323. #endregion
  324. }
  325. }