private static string GenerateClientComplaintNumber(ClientComplaintEntities context) { // get max id of new item var maxId = 0; var clientComplaint = context.ClientComplaints.OrderByDescending(c => c.ClientComplaintID).FirstOrDefault(); if (clientComplaint != null) { maxId = clientComplaint.ClientComplaintID; } return((++maxId).ToString("D6")); }
private static void SendEmailNotification(ClientComplaintEntities context) { try { var messageBody = new StringBuilder(); var messageTitle = ""; var messageSendTo = ""; ClientComplaint complaint = null; string description = string.Empty; int? LoadingPlanDetailID = null; foreach (var entry in context.ChangeTracker.Entries().Where(o => o.State != EntityState.Unchanged && o.State != EntityState.Detached)) { ClientComplaintItem item; switch (entry.State) { case EntityState.Added: if (entry.Entity is ClientComplaint) { // new complaint added complaint = entry.Entity as ClientComplaint; messageTitle = string.Format("Client complaint added, number: [{0}]. Received Date: [{1}]", complaint.ComplaintNumber, complaint.ReceivedDate); } else if (entry.Entity is ClientComplaintItem) { item = entry.Entity as ClientComplaintItem; complaint = context.ClientComplaints.FirstOrDefault(o => o.ClientComplaintID == item.ClientComplaintID); description = context.ClientComplaint_ItemInfo_View.FirstOrDefault(o => o.LoadingPlanDetailID == item.LoadingPlanDetailID.Value).ModelNM; messageTitle = string.Format("Some changes on client complaint number: [{0}]. Received Date: [{1}]", complaint.ComplaintNumber, complaint.ReceivedDate); messageBody.AppendFormat("Add new complaint item [{0}] description: [{1}]<br/>", description, item.ComplaintDescription); } break; case EntityState.Deleted: case EntityState.Modified: if (entry.Entity is ClientComplaint) { // new complaint added complaint = entry.Entity as ClientComplaint; messageTitle = string.Format("Some changes on client complaint number: [{0}]. Received Date: [{1}]", complaint.ComplaintNumber, complaint.ReceivedDate); } else if (entry.Entity is ClientComplaintItem) { item = entry.Entity as ClientComplaintItem; if (item != null) { complaint = context.ClientComplaints.FirstOrDefault(o => o.ClientComplaintID == item.ClientComplaintID); if (complaint != null && complaint.ClientComplaintID > 0) { messageTitle = string.Format("Some changes on client complaint number: [{0}]. Received Date: [{1}]", complaint.ComplaintNumber, complaint.ReceivedDate); if (entry.State == EntityState.Added) { description = context.ClientComplaint_ItemInfo_View.FirstOrDefault(o => o.LoadingPlanDetailID == item.LoadingPlanDetailID.Value).ModelNM; messageBody.AppendFormat("Add new complaint item [{0}] description: [{1}]<br/>", description, item.ComplaintDescription); } else if (entry.State == EntityState.Deleted) { LoadingPlanDetailID = (int?)entry.OriginalValues["LoadingPlanDetailID"]; description = context.ClientComplaint_ItemInfo_View.FirstOrDefault(o => o.LoadingPlanDetailID == LoadingPlanDetailID.Value).ModelNM; messageBody.AppendFormat("Delete a complaint item: [{0}]<br/>", description); } else if (entry.State == EntityState.Modified) { description = context.ClientComplaint_ItemInfo_View.FirstOrDefault(o => o.LoadingPlanDetailID == item.LoadingPlanDetailID.Value).ModelNM; messageBody.AppendFormat("Update a complaint item [{0}] description: [{1}]<br/>", description, item.ComplaintDescription); } } } } break; } } if (string.IsNullOrEmpty(messageTitle) || complaint == null) { return; } foreach (var employee in complaint.ClientComplaintUsers) { var emp = context.Employees.FirstOrDefault(o => o.EmployeeID == employee.EmployeeID); var profile = context.AccountMng_UserProfile_View.FirstOrDefault(o => o.UserId == emp.UserID); if (profile != null && !string.IsNullOrEmpty(profile.Email)) { messageSendTo += !string.IsNullOrEmpty(messageSendTo) ? ";" + profile.Email : profile.Email; } } // add email message to database if (!string.IsNullOrEmpty(messageSendTo)) { var dbEmail = new EmailNotificationMessage { EmailBody = messageBody.ToString(), EmailSubject = messageTitle, SendTo = messageSendTo }; context.EmailNotificationMessages.Add(dbEmail); } } catch (Exception ex) { } }