/// <summary> /// Sends out notifications to contact people whos machines are going to be offrented in two days /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void SendNotifications(object sender, ElapsedEventArgs e) { lock (_sendNotifications_Lock) { DAL _database = new DAL(); DBEntities db = new DBEntities(); //Get a list of ContactPersonIds from users who have notifications turned on string[] ContactPersonIds = db.Users.Where(i => i.SendDeliveryNotifications && i.ContactPersonId != null).Select(i => i.ContactPersonId).ToArray(); // Return all customers that have any users that need a notification sent to them List <Customer> customers = db.Customers.Where(custEntity => custEntity.UserCompanies.Any(userCompEntity => ContactPersonIds.Contains(userCompEntity.User.ContactPersonId))).ToList(); List <OrderState> tempOrderStates = new List <OrderState>(); //Get every customer from the portal foreach (Customer customer in customers) { //Get each customers current orders List <AxOrderState> axOrderStates = _database.GetOrderStates(customer, DateTime.Now).ToList(); //check for deliveries and send notifications foreach (AxOrderState delivery in axOrderStates.Where(i => i.SalesStatus == 2 || i.SalesStatus == 3)) { //search for the order in the portal OrderState portalOrderState = db.OrderStates.FirstOrDefault(i => i.InventTransId == delivery.InventTransId); if (portalOrderState != null && portalOrderState.SalesStatus != 2 && portalOrderState.SalesStatus != 3) { MailClient.SetCulture(customer.CompanyId); DeliveryEmail deliveryEmail = new DeliveryEmail() { Customer = customer, OrderDetails = _database.GetRentalOrderDetails(customer, delivery.InventTransId), BannerUrl = MailClient.GetBannerUrl(customer.CompanyId) }; //Check to see if the order contact person has notifcation enabled if (ContactPersonIds.Contains(deliveryEmail.OrderDetails.ContactPersonId)) { string template = File.ReadAllText(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\MachineDeliveryEmail.html"); var html = Engine.Razor.RunCompile(template, "DeliveryEmail", null, deliveryEmail); string amEmail = _database.GetAccountManagerDetails(customer).Email; string[] notificationEmails = null; if (!String.IsNullOrEmpty(customer.NotificationEmailAddress)) { notificationEmails = customer.NotificationEmailAddress.Split(';'); } MailClient.SendEmail(customer, Resources.MachineDelivered, html, amEmail, "", notificationEmails, null, "Delivery Notification", deliveryEmail.OrderDetails.ContactEmail); } } } //Prepare updated order states data foreach (AxOrderState axOrder in axOrderStates) { var orderState = db.OrderStates.FirstOrDefault(i => i.InventTransId == axOrder.InventTransId); if (orderState == null) { OrderState newOrderState = new OrderState() { DatabaseName = customer.DatabaseName, CompanyId = customer.CompanyId, CustomerId = customer.CustomerId, InventTransId = axOrder.InventTransId, DatetimeId = DateTime.Now, SalesStatus = axOrder.SalesStatus, ModifiedDate = axOrder.ModifiedDate, ModifiedTime = axOrder.ModifiedTime }; db.OrderStates.Add(newOrderState); } else { //update portal order orderState.SalesStatus = axOrder.SalesStatus; orderState.DatetimeId = DateTime.Now; } } } //Save changes db.SaveChanges(); } ScheduleNextExecution(); }