public void AddNotification(NotificationModel nmodel) { notification nm = new notification(); nm.NotificationText = nmodel.NotificationText; nm.N_To = nmodel.N_To; nm.N_from = nmodel.N_from; nm.IsRead = false; nm.dateCreated = DateTime.Now; db = new LeadCRMEntities(); db.notifications.Add(nm); db.SaveChanges(); }
public JsonResult AddNewLead(LeadModel newlead) { if (Session["UserId"] == null) { return(Json(new { status = "Ok", code = 401, msg = "Unauthorized Access", data = "" }, JsonRequestBehavior.AllowGet)); } else { #region AddLead //checking validation //if (ModelState.IsValid) //{ try { if (newlead.ID <= 0 || newlead.ID == null) { //assigning form data to table model var newleadrgstr = new Lead(); newleadrgstr.ClientName = newlead.ClientName; newleadrgstr.LeadDate = newlead.LeadDate; newleadrgstr.LeadTime = newlead.LeadTime; newleadrgstr.LeadType = newlead.LeadTypeID; newleadrgstr.ContactNo = newlead.ContactNo; newleadrgstr.Email = newlead.Email; newleadrgstr.TimeZone = newlead.TimeZoneID; newleadrgstr.Location = newlead.Location; newleadrgstr.IPAddress = newlead.IPAddress; newleadrgstr.LeadStatus = newlead.LeadStatus; newleadrgstr.Budget = newlead.Budget; newleadrgstr.NextPlan = newlead.NextPlan; newleadrgstr.LastStatus = newlead.LastStatus; newleadrgstr.IntialRequirements = newlead.IntialRequirements; newleadrgstr.AssignedToUSerID = newlead.AssignedToUSerID; newleadrgstr.DateCreated = DateTime.Now; newleadrgstr.Is_Active = true; newleadrgstr.Status = "Active"; newleadrgstr.Created_By = Convert.ToInt32(Session["UserId"]); //var adminId = Convert.ToInt32(Session["UserId"].ToString()); //var user = db.USERs.Where(c => c.ID == newlead.AssignedToUSerID).FirstOrDefault(); ////var admin = db.USERs.Where(c => c.ID == adminId).FirstOrDefault(); //var mes = "Dear " + user.UserName + " a lead has been assigned to you"; //var body = "<p>Email From: {0} ({1})</p><p>Message:</p><p>{2}</p>"; //var message = new MailMessage(); //message.To.Add(new MailAddress(user.Email)); // replace with valid value //message.From = new MailAddress("*****@*****.**"); // replace with valid value //message.Subject = "You have been assigned a Lead"; //message.Body = string.Format(body, "Rashid Rahim", "*****@*****.**", mes); //message.IsBodyHtml = true; //using (var smtp = new SmtpClient()) //{ // var credential = new NetworkCredential // { // UserName = "******", // replace with valid value // Password = "******" // replace with valid value // }; // smtp.Credentials = credential; // smtp.Host = "smtp-mail.outlook.com"; // smtp.Port = 587; // smtp.EnableSsl = true; // smtp.SendMailAsync(message); //} //insert to daatbase db.Leads.Add(newleadrgstr); db.SaveChanges(); NotificationModel nm = new NotificationModel(); nm.N_To = newlead.AssignedToUSerID; nm.NotificationText = "You have Assigned to New Lead"; nm.N_from = Convert.ToInt32(Session["UserId"]); nm.IsRead = false; nm.dateCreated = DateTime.Now; AddNotification(nm); } else { if (Convert.ToInt32(Session["RoleID"]) == 1) { var result = db.Leads.SingleOrDefault(b => b.ID == newlead.ID); if (result != null) { result.ClientName = newlead.ClientName; result.LeadDate = newlead.LeadDate; result.LeadTime = newlead.LeadTime; result.LeadType = newlead.LeadTypeID; result.ContactNo = newlead.ContactNo; result.Email = newlead.Email; result.TimeZone = newlead.TimeZoneID; result.Location = newlead.Location; result.IPAddress = newlead.IPAddress; result.LeadStatus = newlead.LeadStatus; result.Budget = newlead.Budget; result.NextPlan = newlead.NextPlan; result.LastStatus = newlead.LastStatus; result.IntialRequirements = newlead.IntialRequirements; result.AssignedToUSerID = newlead.AssignedToUSerID; result.DateCreated = DateTime.Now; result.Is_Active = true; result.Status = "Active"; result.Created_By = Convert.ToInt32(Session["UserId"]); db.SaveChanges(); NotificationModel nm = new NotificationModel(); nm.N_To = newlead.AssignedToUSerID; nm.NotificationText = "Lead has been Edited"; nm.N_from = Convert.ToInt32(Session["UserId"]); nm.IsRead = false; nm.dateCreated = DateTime.Now; AddNotification(nm); } } else { var result = db.Leads.SingleOrDefault(b => b.ID == newlead.ID); if (result != null) { result.ClientName = newlead.ClientName; result.LeadDate = newlead.LeadDate; result.LeadTime = newlead.LeadTime; result.LeadType = newlead.LeadTypeID; result.ContactNo = newlead.ContactNo; result.Email = newlead.Email; result.TimeZone = newlead.TimeZoneID; result.Location = newlead.Location; result.IPAddress = newlead.IPAddress; result.LeadStatus = newlead.LeadStatus; result.Budget = newlead.Budget; result.NextPlan = newlead.NextPlan; result.LastStatus = newlead.LastStatus; result.IntialRequirements = newlead.IntialRequirements; result.AssignedToUSerID = newlead.AssignedToUSerID; result.DateCreated = DateTime.Now; result.Is_Active = true; result.Status = "Active"; result.Created_By = Convert.ToInt32(Session["UserId"]); db.SaveChanges(); // use fro notification for all Admin Roles db = new LeadCRMEntities(); var adminslist = db.USERs.Where(p => p.RoleId == 1).ToList(); for (int i = 0; i < adminslist.Count; i++) { NotificationModel nm = new NotificationModel(); nm.N_To = adminslist[i].ID; nm.NotificationText = "Lead has been Edited"; nm.N_from = Convert.ToInt32(Session["UserId"]); nm.IsRead = false; nm.dateCreated = DateTime.Now; AddNotification(nm); } } } } return(Json(new { status = "Ok", code = 200, msg = "No User Found", data = "" }, JsonRequestBehavior.AllowGet)); } catch (Exception ex) { //if any error return(Json(new { status = "Bad Gateway", code = 400, msg = "Failed" + ex.Message, data = "" }, JsonRequestBehavior.AllowGet)); } //} //else //{ // //foreach (var eve in ModelState.EntityValidationErrors) // //{ // // Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", // // eve.Entry.Entity.GetType().Name, eve.Entry.State); // // foreach (var ve in eve.ValidationErrors) // // { // // Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", // // ve.PropertyName, ve.ErrorMessage); // // } // //} // //Validation Failed // return Json(new { status = "Bad Gateway", code = 400, msg = "Validation Failed", data = "" }, JsonRequestBehavior.AllowGet); //} #endregion } }