// GET: Bosses/Create public ActionResult Create() { var user = db.Users.Where(u => u.UserName == User.Identity.Name).FirstOrDefault(); if (user == null) { return(RedirectToAction("Index", "Home")); } ViewBag.CountryId = new SelectList( CombosHelper.GetCountries(), "CountryId", "Name"); ViewBag.CityId = new SelectList( CombosHelper.GetCities(), "CityId", "Name"); ViewBag.CompanyId = new SelectList( CombosHelper.GetCompanies(), "CompanyId", "Name"); ViewBag.DepartmentId = new SelectList( CombosHelper.GetDepartments(), "DepartmentId", "Name"); ViewBag.BossId = new SelectList( CombosHelper.GetBosses(user.CompanyId), "BossId", "FullName"); ViewBag.CommuneId = new SelectList( CombosHelper.GetCommunes(), "CommuneId", "Name"); ViewBag.WorkPlaceId = new SelectList( CombosHelper.GetWorkPlaces(), "WorkPlaceId", "Name"); ViewBag.VotingPlaceId = new SelectList( CombosHelper.GetVotingPlaces(), "VotingPlaceId", "Name"); var link = new Link { CompanyId = user.CompanyId, Date = DateTime.Now, DateBorn = DateTime.Now, }; return(View(link)); }
public ActionResult DeleteConfirmed(int id) { var link = db.Links.Find(id); using (var transaction = db.Database.BeginTransaction()) { try { var response = FilesHelper.DeleteDocument(link.Photo); var Voter = db.Voters.Where(V => V.Document == link.Document).FirstOrDefault(); if (Voter != null) { db.Voters.Remove(Voter); } var Refer = db.Refers.Where(r => r.ReferType == 2 && r.UserId == link.LinkId).FirstOrDefault(); if (Refer != null) { var coordinators = db.Coordinators.Where(c => c.ReferId == Refer.ReferId).ToList(); foreach (var coor in coordinators) { db.Coordinators.Remove(coor); } var leaders = db.Leaders.Where(l => l.ReferId == Refer.ReferId).ToList(); foreach (var leader in leaders) { db.Leaders.Remove(leader); } var voters = db.Voters.Where(v => v.ReferId == Refer.ReferId).ToList(); foreach (var voter in voters) { db.Voters.Remove(voter); } db.Refers.Remove(Refer); db.SaveChanges(); } //Elimino todo Coordinador y Lider Asociado a este enlace //Borro cualquier anotacion que tenga en la agenda var dates = db.Dates.Where(d => d.ProfessionalId == link.Document).ToList(); foreach (var item in dates) { db.Dates.Remove(item); } var dateItems = db.TimesDates.Where(di => di.ProfessionalId == link.Document).ToList(); foreach (var it in dateItems) { db.TimesDates.Remove(it); } var HV = db.HojaVidas.Where(h => h.RolId == 2 && h.UserId == link.LinkId).FirstOrDefault(); if (HV != null) { db.HojaVidas.Remove(HV); } db.Links.Remove(link); db.SaveChanges(); transaction.Commit(); return(RedirectToAction("Index")); } catch (Exception ex) { transaction.Rollback(); if (ex.InnerException != null && ex.InnerException.InnerException != null && ex.InnerException.InnerException.Message.Contains("REFERENCE")) { ModelState.AddModelError(string.Empty, "El registro no se puede borrar porque tiene registros relacionados"); } else { ModelState.AddModelError(string.Empty, ex.ToString()); } } } ViewBag.CountryId = new SelectList( CombosHelper.GetCountries(), "CountryId", "Name", link.CountryId); ViewBag.CityId = new SelectList( CombosHelper.GetCities(), "CityId", "Name", link.CityId); ViewBag.CompanyId = new SelectList( CombosHelper.GetCompanies(), "CompanyId", "Name", link.CompanyId); ViewBag.DepartmentId = new SelectList( CombosHelper.GetDepartments(), "DepartmentId", "Name", link.DepartmentId); ViewBag.BossId = new SelectList( CombosHelper.GetBosses(link.CompanyId), "BossId", "FullName", link.BossId); ViewBag.CommuneId = new SelectList( CombosHelper.GetCommunes(), "CommuneId", "Name", link.CommuneId); ViewBag.WorkPlaceId = new SelectList( CombosHelper.GetWorkPlaces(), "WorkPlaceId", "Name", link.WorkPlaceId); ViewBag.VotingPlaceId = new SelectList( CombosHelper.GetVotingPlaces(), "VotingPlaceId", "Name", link.VotingPlaceId); return(View(link)); }
public ActionResult Edit(Link link, string newWorkPlace) { if (ModelState.IsValid) { if (link.WorkPlaceId == 9999) { if (!string.IsNullOrEmpty(newWorkPlace)) { var workPlace = new WorkPlace { Name = newWorkPlace, }; db.WorkPlaces.Add(workPlace); try { db.SaveChanges(); } catch (Exception ex) { if (ex.InnerException != null && ex.InnerException.InnerException != null && ex.InnerException.InnerException.Message.Contains("_Index")) { ModelState.AddModelError(string.Empty, "Ya existe un lugar de trabajo con ese nombre"); } else { ModelState.AddModelError(string.Empty, ex.ToString()); } ViewBag.CountryId = new SelectList( CombosHelper.GetCountries(), "CountryId", "Name", link.CountryId); ViewBag.CityId = new SelectList( CombosHelper.GetCities(), "CityId", "Name", link.CityId); ViewBag.CompanyId = new SelectList( CombosHelper.GetCompanies(), "CompanyId", "Name", link.CompanyId); ViewBag.DepartmentId = new SelectList( CombosHelper.GetDepartments(), "DepartmentId", "Name", link.DepartmentId); ViewBag.CommuneId = new SelectList( CombosHelper.GetCommunes(), "CommuneId", "Name", link.CommuneId); ViewBag.WorkPlaceId = new SelectList( CombosHelper.GetWorkPlaces(), "WorkPlaceId", "Name", link.WorkPlaceId); ViewBag.VotingPlaceId = new SelectList( CombosHelper.GetVotingPlaces(), "VotingPlaceId", "Name", link.VotingPlaceId); return(View(link)); } db.SaveChanges(); link.WorkPlaceId = workPlace.WorkPlaceId; } } if (link.PhotoFile != null) { var folder = "~/Content/Links"; var file = string.Format("{0}.jpg", link.LinkId); var response = FilesHelper.UploadPhoto(link.PhotoFile, folder, file); if (response) { var pic = string.Format("{0}/{1}", folder, file); link.Photo = pic; db.Entry(link).State = EntityState.Modified; db.SaveChanges(); } } db.Entry(link).State = EntityState.Modified; try { db.SaveChanges(); var referr = db.Refers.Where(r => r.ReferType == 1 && r.UserId == link.BossId).FirstOrDefault(); var voter = db.Voters.Where(v => v.Document == link.Document && v.UserName == link.UserName).FirstOrDefault(); var country = db.Countries.Find(link.CountryId); var department = db.Departments.Find(link.DepartmentId); var city = db.Cities.Find(link.CityId); var commune = db.Communes.Find(link.CommuneId); var voting = db.VotingPlaces.Find(link.VotingPlaceId); voter.Address = link.Address; voter.BossId = link.BossId; voter.CityId = city.Name; voter.CommuneId = commune.Name; voter.CompanyId = link.CompanyId; voter.CountryId = country.Name; voter.DepartmentId = department.Name; voter.Document = link.Document; voter.FirstName = link.FirstName; voter.LastName = link.LastName; voter.Phone = link.Phone; voter.UserName = link.UserName; voter.VotingPlaceId = voting.Name; voter.userId = 2; voter.ReferId = referr.ReferId; voter.PerfilId = 1; voter.Fname = string.Format("{0} {1}", link.FirstName, link.LastName); voter.Barrio = link.Barrio; voter.Profesion = link.Profesion; voter.DateBorn = link.DateBorn; db.Entry(voter).State = EntityState.Modified; var refer = db.Refers.Where(r => r.ReferType == 2 && r.UserId == link.LinkId).FirstOrDefault(); refer.FullName = link.FullName; db.Entry(refer).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } catch (Exception ex) { if (ex.InnerException != null && ex.InnerException.InnerException != null && ex.InnerException.InnerException.Message.Contains("_Index")) { ModelState.AddModelError(string.Empty, "Ya existe un Registro con esa descripción"); } else { ModelState.AddModelError(string.Empty, ex.ToString()); } } } ViewBag.CountryId = new SelectList( CombosHelper.GetCountries(), "CountryId", "Name", link.CountryId); ViewBag.CityId = new SelectList( CombosHelper.GetCities(), "CityId", "Name", link.CityId); ViewBag.CompanyId = new SelectList( CombosHelper.GetCompanies(), "CompanyId", "Name", link.CompanyId); ViewBag.DepartmentId = new SelectList( CombosHelper.GetDepartments(), "DepartmentId", "Name", link.DepartmentId); ViewBag.BossId = new SelectList( CombosHelper.GetBosses(link.CompanyId), "BossId", "FullName", link.BossId); ViewBag.CommuneId = new SelectList( CombosHelper.GetCommunes(), "CommuneId", "Name", link.CommuneId); ViewBag.WorkPlaceId = new SelectList( CombosHelper.GetWorkPlaces(), "WorkPlaceId", "Name", link.WorkPlaceId); ViewBag.VotingPlaceId = new SelectList( CombosHelper.GetVotingPlaces(), "VotingPlaceId", "Name", link.VotingPlaceId); return(View(link)); }
// GET: Bosses/Edit/5 public ActionResult Edit(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } var link = db.Links.Find(id); if (link == null) { return(HttpNotFound()); } ViewBag.CountryId = new SelectList( CombosHelper.GetCountries(), "CountryId", "Name", link.CountryId); ViewBag.CityId = new SelectList( CombosHelper.GetCities(), "CityId", "Name", link.CityId); ViewBag.CompanyId = new SelectList( CombosHelper.GetCompanies(), "CompanyId", "Name", link.CompanyId); ViewBag.DepartmentId = new SelectList( CombosHelper.GetDepartments(), "DepartmentId", "Name", link.DepartmentId); ViewBag.BossId = new SelectList( CombosHelper.GetBosses(link.CompanyId), "BossId", "FullName", link.BossId); ViewBag.CommuneId = new SelectList( CombosHelper.GetCommunes(), "CommuneId", "Name", link.CommuneId); ViewBag.WorkPlaceId = new SelectList( CombosHelper.GetWorkPlaces(), "WorkPlaceId", "Name", link.WorkPlaceId); ViewBag.VotingPlaceId = new SelectList( CombosHelper.GetVotingPlaces(), "VotingPlaceId", "Name", link.VotingPlaceId); return(View(link)); }