using GreenTree.Nachtragsmanagement.Core.Plugins; using System; using System.Collections.Generic; using System.Linq; using System.Web; using GreenTree.Nachtragsmanagement.Core.Domain.Misc; using GreenTree.Nachtragsmanagement.Services.User; using GreenTree.Nachtragsmanagement.Services.Configuration; using GreenTree.Nachtragsmanagement.Services.Appendix; using GreenTree.Nachtragsmanagement.Core.Domain.Appendix; using System.Net.Mail; using System.Net; using System.Globalization; using GreenTree.Nachtragsmanagement.Services.Deviation; using GreenTree.Nachtragsmanagement.Core.Domain.Deviation; using Quartz; namespace GreenTree.Nachtragsmanagement.Web.Implementations { public class DeviationNotificationPlugin : INotificationPlugin, IJob { #region Services private readonly IUserService _userService; private readonly IConfigurationService _configurationService; private readonly IDeviationService _deviationService; #endregion #region Properties /// /// Id /// public Guid Id { get { return Guid.Parse("77D662E0-F621-4567-9030-2A106533FE06"); } } /// /// System name /// public string SystemName { get { return "GreenTree.Nachtragsmanagement.DeviationNotificationPlugin"; } } /// /// List of available notification jobs /// public List AvailableNotificationJobs { get { return new List { new NotificationJob ( Guid.Parse("144DFF97-4EE4-4B32-967C-C0375D133DCF"), "GreenTree.Nachtragsmanagement.DeviationNotificationPlugin.ProcessDeviationReceipt", "Eingangsdatum überprüfen", "Erstellt automatisch Benachrichtigungen für Vertragsabweichungen, die 40 Tagen, bzw. 60 Tagen nach " + "Einreichung noch keinem Nachtrag zugeordnet sind." ) }; } } /// /// Displayed name /// public string Name { get { return "Vertragsabweichungsbenachrichtigung"; } } /// /// Further description on how this plugin works /// public string Description { get { return "Erstellt automatisch Benachrichtigungen für Vertragsabweichungen, die 40 Tagen (Stufe 1), bzw. 60 Tagen (Stufe 2)" + "nach Einreichung noch keinem Nachtrag zugeordnet sind."; } } #endregion /// /// Initializes a new instance of the DeviationNotificationPlugin class /// public DeviationNotificationPlugin() { } /// /// Initializes a new instance of the DeviationNotificationPlugin class /// public DeviationNotificationPlugin( IUserService userService, IConfigurationService configurationService, IDeviationService deviationService) { _userService = userService; _configurationService = configurationService; _deviationService = deviationService; } #region Quartz implmentation /// /// Executes the current job /// /// public void Execute(IJobExecutionContext context) { if (!context.JobDetail.JobDataMap.ContainsKey("MailNotifications")) return; var mailNotifications = context.JobDetail.JobDataMap.Get("MailNotifications") as IEnumerable; if (mailNotifications == null) return; ProcessNotifications(mailNotifications); } #endregion #region Processing /// /// Process all mail notifications registered for that plugin /// /// The notifications which shall be generated. public void ProcessNotifications(IEnumerable mailNotifications) { if (mailNotifications == null || !mailNotifications.Any()) return; foreach (var notification in mailNotifications) { switch (notification.NotificationJobSystemName) { case "GreenTree.Nachtragsmanagement.DeviationNotificationPlugin.ProcessDeviationReceipt": { ProcessDeviationReceiptNotification(notification); } break; default: continue; } } } /// /// Notifies the corresponding recipients about all deviations whose receipt date is older than N days in two different groups /// /// The notification which shall be generated. private void ProcessDeviationReceiptNotification(MailNotification mailNotification) { var ageDaysLevel1 = _configurationService.TryGetConfigItemValue( "GreenTree.Nachtragsmanagement.DeviationNotificationPlugin.ProcessDeviationReceipt.AgeDaysLevel1", 40); var ageDaysLevel2 = _configurationService.TryGetConfigItemValue( "GreenTree.Nachtragsmanagement.DeviationNotificationPlugin.ProcessDeviationReceipt.AgeDaysLevel2", 60); var deviationsLevel1 = _deviationService.GetAllDeviations() .Where(d => !d.AppendixId.HasValue && (DateTime.Now - d.ReceiptDate).Value.Days >= ageDaysLevel1 && (DateTime.Now - d.ReceiptDate).Value.Days < ageDaysLevel2) .ToList(); var deviationsLevel2 = _deviationService.GetAllDeviations() .Where(d => !d.AppendixId.HasValue && (DateTime.Now - d.ReceiptDate).Value.Days >= ageDaysLevel2) .ToList(); if (deviationsLevel1.Any() || deviationsLevel2.Any()) { var mailBody = GenerateDeviationReceiptMailBody(deviationsLevel1, deviationsLevel2); SendNotification(mailNotification, "Autom. Übersicht nicht zugewiesene Vertragsabweichungen", mailBody); } } #endregion #region Mail body generation /// /// Generates a mail body with a list of all deviations whose receipt date is older than N days in two different groups /// /// All deviations matching the level 1 criteria. /// All deviations matching the level 2 criteria. public string GenerateDeviationReceiptMailBody(IEnumerable deviationsLevel1, IEnumerable deviationsLevel2) { var ageDaysLevel1 = _configurationService.TryGetConfigItemValue( "GreenTree.Nachtragsmanagement.DeviationNotificationPlugin.ProcessDeviationReceipt.AgeDaysLevel1", 40); var ageDaysLevel2 = _configurationService.TryGetConfigItemValue( "GreenTree.Nachtragsmanagement.DeviationNotificationPlugin.ProcessDeviationReceipt.AgeDaysLevel2", 60); var template = "" + " " + " {0}" + " {1}" + " " + ""; var templateLevel1 = "

Übersicht \"Offene Vertragsabweichungen älter als {0} Tage\"

" + "

Folgende Vertragsabweichungen haben ein Einreichdatum älter als {0} Tage, sind aber noch keinen Nachtrag zugeordnet:

" + "
    " + " {1}" + "
"; var templateLevel1List = String.Empty; if (deviationsLevel1.Any()) { foreach (var deviation in deviationsLevel1) { templateLevel1List += String.Format( "
  • Vertragsabweichung \"{0}\" " + "in Baustelle \"{1}\" - " + "Einreichdatum: {2:dd.MM.yyyy} ({3} Tage)", deviation.CustomNumber, deviation.Site.CustomNumber, deviation.ReceiptDate, (DateTime.Now - deviation.ReceiptDate).Value.Days); } } var resultLevel1List = String.Format(templateLevel1, ageDaysLevel1, templateLevel1List); var templateLevel2 = "

    Übersicht \"Offene Vertragsabweichungen älter als {0} Tage\"

    " + "

    Folgende Vertragsabweichungen haben ein Einreichdatum älter als {0} Tage, sind aber noch keinen Nachtrag zugeordnet:

    " + "
      " + " {1}" + "
    "; var templateLevel2List = String.Empty; if (deviationsLevel2.Any()) { foreach (var deviation in deviationsLevel2) { templateLevel2List += String.Format( "
  • Vertragsabweichung \"{0}\" " + "in Baustelle \"{1}\" - " + "Einreichdatum: {2:dd.MM.yyyy} ({3} Tage)", deviation.CustomNumber, deviation.Site.CustomNumber, deviation.ReceiptDate, (DateTime.Now - deviation.ReceiptDate).Value.Days); } } var resultLevel2List = String.Format(templateLevel2, ageDaysLevel2, templateLevel2List); return String.Format(template, resultLevel1List, resultLevel2List); } #endregion #region Mail sending /// /// Sends a generated mail body to the specified recipients in the mail notification /// /// The mail notification. /// The mail subject. /// The mail body. public void SendNotification(MailNotification mailNotification, string subject, string body) { if (mailNotification == null) return; var mailServerConfig = _configurationService.GetCurrentConfiguration().MailServerElement; var smptClient = new SmtpClient(mailServerConfig.SmtpServer, mailServerConfig.Port) { EnableSsl = mailServerConfig.UseSsl, Credentials = new NetworkCredential( mailServerConfig.Username, mailServerConfig.Password, mailServerConfig.Domain) }; var recipients = mailNotification.Users .Select(u => u.MailAddress); var mailMessage = new MailMessage { IsBodyHtml = true, Subject = subject, Body = body, From = new MailAddress("Nachtragsbenachrichtigung@schweerbau.de") }; foreach (var recipient in recipients) mailMessage.To.Add(recipient); smptClient.Send(mailMessage); } #endregion } }