public ActionResult Create(CustmorVM c) { string locationname = c.LocationName; string country = c.CountryName; string city = c.CityName; CustomerMaster obj = new CustomerMaster(); PickupRequestDAO _dao = new PickupRequestDAO(); if (c.CustomerID == 0) { int max = (from d in db.CustomerMasters orderby d.CustomerID descending select d.CustomerID).FirstOrDefault(); int accompanyid = Convert.ToInt32(Session["CurrentCompanyID"].ToString()); int branchid = Convert.ToInt32(Session["CurrentBranchID"].ToString()); obj.CustomerID = max + 1; obj.AcCompanyID = accompanyid; obj.CustomerCode = c.CustomerCode; // _dao.GetMaxCustomerCode(branchid); // c.CustomerCode; } else { obj = db.CustomerMasters.Find(c.CustomerID); } obj.CustomerName = c.CustomerName; obj.CustomerType = c.CustomerType; obj.ReferenceCode = c.ReferenceCode; obj.ContactPerson = c.ContactPerson; obj.Address1 = c.Address1; obj.Address2 = c.Address2; obj.Address3 = c.Address3; obj.Phone = c.Phone; obj.Mobile = c.Mobile; obj.Fax = c.Fax; obj.Email = c.Email; obj.WebSite = c.Website; obj.CountryID = c.CountryID; if (c.CityID == 0) { obj.CityID = null; } else { obj.CityID = c.CityID; } if (c.LocationID == 0) { obj.LocationID = null; } else { obj.LocationID = c.LocationID; } obj.CountryName = c.CountryName; obj.CityName = c.CityName; obj.LocationName = c.LocationName; if (c.CurrenceyID == 0) { c.CurrenceyID = Convert.ToInt32(Session["CurrencyId"].ToString()); } else { obj.CurrencyID = c.CurrenceyID; } obj.StatusActive = c.StatusActive; obj.CreditLimit = c.CreditLimit; obj.StatusTaxable = c.StatusTaxable; obj.EmployeeID = c.EmployeeID; obj.statusCommission = c.StatusCommission; obj.VATTRN = c.VATTRN; obj.CourierServiceID = c.CourierServiceID; obj.BranchID = Convert.ToInt32(Session["CurrentBranchID"].ToString()); obj.CustomerUsername = c.CustomerUsername; //obj.Password = c.Password; obj.Password = _dao.RandomPassword(6); obj.BusinessTypeId = c.BusinessTypeId; obj.Referal = c.Referal; obj.OfficeOpenTime = c.OfficeTimeFrom; obj.OfficeCloseTime = c.OfficeTimeTo; if (c.DepotID == null) { obj.DepotID = Convert.ToInt32(Session["CurrentDepotID"].ToString()); } else { obj.DepotID = c.DepotID; } if (c.CustomerType == "CR" && c.ChkApprovedBy) { obj.ApprovedBy = Convert.ToInt32(Session["UserID"]); obj.ApprovedOn = c.ApprovedOn; } //UserRegistration u = new UserRegistration(); //if (c.Email != null) //{ // if (c.Email != "") // { // UserRegistration x = (from a in db.UserRegistrations where a.UserName == c.Email select a).FirstOrDefault(); // if (x == null) // { // int max1 = (from c1 in db.UserRegistrations orderby c1.UserID descending select c1.UserID).FirstOrDefault(); // u.UserID = max1 + 1; // u.UserName = c.Email; // u.EmailId = c.Email; // u.Password = obj.Password; // u.Phone = c.Phone; // u.IsActive = true; // u.RoleID = c.RoleID; // } // try // { // db.UserRegistrations.Add(u); // db.SaveChanges(); // } // catch (DbEntityValidationException e) // { // foreach (var eve in e.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); // } // } // } // } //} try { // obj.UserID = u.UserID; if (c.CustomerID > 0) { db.Entry(obj).State = EntityState.Modified; db.SaveChanges(); } else { db.CustomerMasters.Add(obj); db.SaveChanges(); ReceiptDAO.ReSaveCustomerCode(); } if (c.EmailNotify == true) { EmailDAO _emaildao = new EmailDAO(); _emaildao.SendCustomerEmail(c.Email, c.CustomerName, obj.Password); } } catch (DbEntityValidationException e) { foreach (var eve in e.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); } } } TempData["SuccessMsg"] = "You have successfully added Customer."; return(RedirectToAction("Index")); return(View()); }
public ActionResult Edit(AgentVM item) { UserRegistration u = new UserRegistration(); PickupRequestDAO _dao = new PickupRequestDAO(); int BranchID = Convert.ToInt32(Session["CurrentBranchID"].ToString()); int accompanyid = Convert.ToInt32(Session["CurrentCompanyID"].ToString()); AgentMaster a = db.AgentMasters.Find(item.ID); a.AcCompanyID = accompanyid; a.Name = item.AgentName; a.AgentCode = item.AgentCode; a.Address1 = item.Address1; a.Address2 = item.Address2; a.Address3 = item.Address3; a.Phone = item.Phone; a.Fax = item.Fax; a.WebSite = item.WebSite; a.ContactPerson = item.ContactPerson; a.CountryName = item.CountryName; a.CityName = item.CityName; a.LocationName = item.LocationName; a.CurrencyID = item.CurrencyID; // a.ZoneCategoryID = item.ZoneCategoryID; //a.AcHeadID = item.AcHeadID; a.CreditLimit = item.CreditLimit; a.AgentType = item.AgentType; if (item.StatusActive != null) { a.StatusActive = Convert.ToBoolean(item.StatusActive); } if (a.BranchID == null) { a.BranchID = BranchID; } UserRegistration x = null; if (a.UserID != null && a.UserID > 0) { x = (from b in db.UserRegistrations where b.UserID == a.UserID select b).FirstOrDefault(); } if (a.Email != item.Email || x == null) { if (x == null) { int max1 = (from c1 in db.UserRegistrations orderby c1.UserID descending select c1.UserID).FirstOrDefault(); int roleid = db.RoleMasters.Where(t => t.RoleName == "Agent").FirstOrDefault().RoleID; u.UserID = max1 + 1; u.UserName = item.Email; u.EmailId = item.Email; if (item.Password == "" || item.Password == null) { u.Password = _dao.RandomPassword(6); } else { u.Password = item.Password; } u.Phone = item.Phone; u.IsActive = true; u.RoleID = roleid; db.UserRegistrations.Add(u); db.SaveChanges(); a.Email = item.Email; a.UserID = u.UserID; } else { //checking duplicate UserRegistration x1 = (from b in db.UserRegistrations where b.UserName == item.Email && b.UserID != a.UserID select b).FirstOrDefault(); if (x1 == null) { x.EmailId = item.Email; if (item.Password == "") { x.Password = _dao.RandomPassword(6); } else { x.Password = item.Password; } db.Entry(x).State = EntityState.Modified; db.SaveChanges(); } a.Email = item.Email; a.UserID = x.UserID; } } else { if (a.UserID == null || a.UserID == 0) { int max1 = (from c1 in db.UserRegistrations orderby c1.UserID descending select c1.UserID).FirstOrDefault(); int roleid = db.RoleMasters.Where(t => t.RoleName == "Agent").FirstOrDefault().RoleID; u.UserID = max1 + 1; u.UserName = item.Email; u.EmailId = item.Email; if (item.Password == "") { u.Password = _dao.RandomPassword(6); } else { u.Password = item.Password; } u.Phone = item.Phone; u.IsActive = true; u.RoleID = roleid; db.UserRegistrations.Add(u); db.SaveChanges(); a.UserID = u.UserID; } else { u = (from b in db.UserRegistrations where b.UserID == a.UserID select b).FirstOrDefault(); if (item.Password != u.Password) { u.Password = item.Password; } db.Entry(u).State = EntityState.Modified; db.SaveChanges(); } } if (ModelState.IsValid) { db.Entry(a).State = EntityState.Modified; db.SaveChanges(); if (item.EmailNotify == true) { EmailDAO _emaildao = new EmailDAO(); _emaildao.SendCustomerEmail(item.Email, item.Email, u.Password); } TempData["SuccessMsg"] = "You have successfully Updated Agent."; return(RedirectToAction("Index")); } return(View()); }
public ActionResult Create(UserRegistrationVM v) { if (v.UserID == 0) { string status = "true"; UserRegistration x = (from b in db.UserRegistrations where b.UserName == v.EmailId select b).FirstOrDefault(); if (x != null) { TempData["ErrorMsg"] = "Email Id already exist!"; ViewBag.UserRole = db.RoleMasters.ToList(); return(View(v)); } UserRegistration a = new UserRegistration(); int max = (from c in db.UserRegistrations orderby c.UserID descending select c.UserID).FirstOrDefault(); a.UserID = max + 1; a.UserName = v.EmailId; a.Password = v.Password; a.RoleID = v.RoleID; a.Phone = ""; a.EmailId = v.EmailId; a.IsActive = v.IsActive; db.UserRegistrations.Add(a); db.SaveChanges(); if (a.RoleID == 13) //customer { var customer = db.CustomerMasters.Find(v.UserReferenceId); if (customer != null) { customer.UserID = a.UserID; db.Entry(customer).State = EntityState.Modified; db.SaveChanges(); } } else if (a.RoleID == 14) //AGent { var agent = db.AgentMasters.Find(v.UserReferenceId); if (agent != null) { agent.UserID = a.UserID; db.Entry(agent).State = EntityState.Modified; db.SaveChanges(); } } else { var employee = db.EmployeeMasters.Find(v.UserReferenceId); if (employee != null) { employee.UserID = a.UserID; db.Entry(employee).State = EntityState.Modified; db.SaveChanges(); } } if (v.EmailNotify == true) { EmailDAO _emaildao = new EmailDAO(); _emaildao.SendCustomerEmail(v.EmailId, v.UserName, v.Password); TempData["SuccessMsg"] = "You have successfully added User and Notification Mail has sent!"; } else { TempData["SuccessMsg"] = "You have successfully added User."; } } else { //UserRegistration uv = db.UserRegistrations.Find(v.UserID); var uv = db.UserRegistrations.Find(v.UserID);// (from c in db.UserRegistrations where c.UserID == v.UserID select c).FirstOrDefault(); //UserRegistration a = new UserRegistration(); //a.UserID = v.UserID; uv.UserName = v.EmailId; if (v.Password != null) { if (v.Password != uv.Password) { uv.Password = v.Password; } } //uv.RoleID = v.RoleID; uv.EmailId = v.EmailId; uv.IsActive = v.IsActive; db.Entry(uv).State = EntityState.Modified; db.SaveChanges(); if (uv.RoleID == 13) //customer { var customer = db.CustomerMasters.Find(v.UserReferenceId); if (customer != null) { customer.UserID = uv.UserID; db.Entry(customer).State = EntityState.Modified; db.SaveChanges(); } } else if (uv.RoleID == 14) //AGent { var agent = db.AgentMasters.Find(v.UserReferenceId); if (agent != null) { agent.UserID = uv.UserID; db.Entry(agent).State = EntityState.Modified; db.SaveChanges(); } } else { var employee = db.EmployeeMasters.Find(v.UserReferenceId); if (employee != null) { employee.UserID = uv.UserID; db.Entry(employee).State = EntityState.Modified; db.SaveChanges(); } } if (v.EmailNotify == true) { EmailDAO _emaildao = new EmailDAO(); _emaildao.SendCustomerEmail(v.EmailId, v.UserName, v.Password); TempData["SuccessMsg"] = "You have successfully Updated User Detail and Notification Mail has sent."; } else { TempData["SuccessMsg"] = "You have successfully Updated User."; } } return(RedirectToAction("Index")); }
public ActionResult Create(AgentVM item) { int companyId = Convert.ToInt32(Session["CurrentCompanyID"].ToString()); int BranchID = Convert.ToInt32(Session["CurrentBranchID"].ToString()); int? max = (from c in db.AgentMasters orderby c.AgentID descending select c.AgentID).FirstOrDefault(); AgentMaster a = new AgentMaster(); PickupRequestDAO _dao = new PickupRequestDAO(); int roleid = db.RoleMasters.Where(t => t.RoleName == "Agent").FirstOrDefault().RoleID; UserRegistration u = new UserRegistration(); UserRegistration x = (from b in db.UserRegistrations where b.UserName == item.Email select b).FirstOrDefault(); if (x == null) { int max1 = (from c1 in db.UserRegistrations orderby c1.UserID descending select c1.UserID).FirstOrDefault(); u.UserID = max1 + 1; u.UserName = item.Email; u.EmailId = item.Email; if (item.Password == null) { u.Password = _dao.RandomPassword(6); } else { u.Password = item.Password; } u.Phone = item.Phone; u.IsActive = true; u.RoleID = roleid; db.UserRegistrations.Add(u); db.SaveChanges(); } if (max == null || max == 0) { a.AgentID = 1; a.Name = item.AgentName; a.AgentCode = item.AgentCode; a.Address1 = item.Address1; a.Address2 = item.Address2; a.Address3 = item.Address3; a.Phone = item.Phone; a.Fax = item.Fax; a.WebSite = item.WebSite; a.ContactPerson = item.ContactPerson; a.AcCompanyID = companyId; //a.CountryID = item.CountryID; //a.CityID = item.CityID; //a.LocationID = item.LocationID; a.CurrencyID = item.CurrencyID; a.ZoneCategoryID = item.ZoneCategoryID; a.AcHeadID = item.AcHeadID; a.CreditLimit = item.CreditLimit; a.CountryName = item.CountryName; a.CityName = item.CityName; a.LocationName = item.LocationName; a.Email = item.Email; a.Password = ""; a.AgentType = item.AgentType; a.UserID = u.UserID; if (item.StatusActive == null) { a.StatusActive = false; } else { a.StatusActive = Convert.ToBoolean(item.StatusActive); } a.BranchID = BranchID; } else { a.AgentID = Convert.ToInt32(max) + 1; a.Name = item.AgentName; a.AgentCode = item.AgentCode; a.Address1 = item.Address1; a.Address2 = item.Address2; a.Address3 = item.Address3; a.Phone = item.Phone; a.Fax = item.Fax; a.WebSite = item.WebSite; a.ContactPerson = item.ContactPerson; a.AcCompanyID = companyId; a.CountryName = item.CountryName; a.CityName = item.CityName; a.LocationName = item.LocationName; a.CurrencyID = item.CurrencyID; a.ZoneCategoryID = item.ZoneCategoryID; a.AcHeadID = item.AcHeadID; a.CreditLimit = item.CreditLimit; a.Email = item.Email; a.Password = ""; a.AgentType = item.AgentType; a.UserID = u.UserID; a.BranchID = BranchID; if (item.StatusActive == null) { a.StatusActive = false; } else { a.StatusActive = Convert.ToBoolean(item.StatusActive); } } try { db.AgentMasters.Add(a); db.SaveChanges(); if (item.EmailNotify == true) { EmailDAO _emaildao = new EmailDAO(); _emaildao.SendCustomerEmail(item.Email, item.Email, u.Password); } TempData["SuccessMsg"] = "You have successfully added Agent."; return(RedirectToAction("Index")); } catch (Exception ex) { ViewBag.currency = db.CurrencyMasters.ToList(); ViewBag.zonecategory = db.ZoneCategories.ToList(); ViewBag.achead = db.AcHeads.ToList(); ViewBag.roles = db.RoleMasters.ToList(); TempData["WarningMsg"] = ex.Message; return(View(item)); } }