public ActionResult Create() { ResidentIncomeModel residentIncomeModel = new ResidentIncomeModel(); ViewBag.StateTerritoryID = new SelectList(db.States, "StateTerritoryID", "State", residentIncomeModel.StateTerritoryID); ViewBag.AdmissionType = new SelectList(db.ProgramTypes .Where(t => t.EventType == EnumEventType.ADMISSION), "ProgramTypeID", "ProgramDescription", 2); ViewBag.ReferralID = new SelectList(db.Referrals, "ReferralID", "ReferralName", 1); var allMilitaryCampaigns = db.MilitaryCampaigns; var viewModel = new List <AssignedCampaignData>(); foreach (var militaryCampaign in allMilitaryCampaigns) { viewModel.Add(new AssignedCampaignData { MilitaryCampaignID = militaryCampaign.MilitaryCampaignID, MilitaryCampaign = militaryCampaign.CampaignName, Assigned = false }); } ViewBag.Campaigns = viewModel.OrderBy(i => i.MilitaryCampaign).ToList(); return(View(new ResidentIncomeModel())); }
public bool IsDuplicateResident(ResidentIncomeModel resident) { /* Check for the possible pre-existence of the resident in the system. */ if (db.Residents.AsNoTracking().ToList().Any(r => r.ClearFirstMidName.Contains(resident.FirstMidName) && r.ClearLastName.Contains(resident.LastName) && r.ClearBirthdate?.Date == resident.Birthdate.Date && r.ServiceBranch == resident.ServiceBranch && r.ToDelete == false)) { // Found a match. return(true); } return(false); }
public ActionResult Create(ResidentIncomeModel residentIncomeModel, string[] selectedCampaigns, int AdmissionType) { if (residentIncomeModel == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } ViewBag.AdmissionType = new SelectList(db.ProgramTypes .Where(t => t.EventType == EnumEventType.ADMISSION), "ProgramTypeID", "ProgramDescription", AdmissionType); ViewBag.StateTerritoryID = new SelectList(db.States, "StateTerritoryID", "State", residentIncomeModel.StateTerritoryID); ViewBag.ReferralID = new SelectList(db.Referrals, "ReferralID", "ReferralName", residentIncomeModel.ReferralID); var allMilitaryCampaigns = db.MilitaryCampaigns; var viewModel = new List <AssignedCampaignData>(); foreach (var militaryCampaign in allMilitaryCampaigns) { viewModel.Add(new AssignedCampaignData { MilitaryCampaignID = militaryCampaign.MilitaryCampaignID, MilitaryCampaign = militaryCampaign.CampaignName, Assigned = false }); } ViewBag.Campaigns = viewModel.OrderBy(i => i.MilitaryCampaign).ToList(); Resident resident = new Resident { ClearFirstMidName = residentIncomeModel.FirstMidName, ClearLastName = residentIncomeModel.LastName, IsCurrent = AdmissionType != 1, Gender = residentIncomeModel.Gender, Ethnicity = residentIncomeModel.Ethnicity, Religion = residentIncomeModel.Religion, ClearBirthdate = residentIncomeModel.Birthdate, ServiceBranch = residentIncomeModel.ServiceBranch, NGReserve = residentIncomeModel.NGReserve, MilitaryDischarge = residentIncomeModel.DischargeStatus, InVetCourt = residentIncomeModel.InVetCourt, IsNoncombat = residentIncomeModel.IsNoncombat, StateTerritoryID = residentIncomeModel.StateTerritoryID, ReferralID = residentIncomeModel.ReferralID, Note = residentIncomeModel.Note, MilitaryCampaigns = new List <MilitaryCampaign>(), ProgramEvents = new List <ProgramEvent>(), Benefit = new Benefit(), }; if (!string.IsNullOrEmpty(residentIncomeModel.ReferralOther)) { resident.OptionalReferralDescription = residentIncomeModel.ReferralOther; } if (!string.IsNullOrEmpty(residentIncomeModel.StateTerritoryOther)) { resident.StateTerritoryOther = residentIncomeModel.StateTerritoryOther; } Benefit benefit = new Benefit { DisabilityPercentage = residentIncomeModel.DisabilityPercentage, DisabilityAmount = residentIncomeModel.DisabilityAmount, SSI = residentIncomeModel.SSI, SSDI = residentIncomeModel.SSDI, FoodStamp = residentIncomeModel.FoodStamp, OtherDescription = residentIncomeModel.OtherDescription, Other = residentIncomeModel.Other, TotalBenefitAmount = residentIncomeModel.TotalBenefitAmount }; try { if (ModelState.IsValid) { if (!IsDuplicateResident(residentIncomeModel)) { db.Residents.Add(resident); var admitEvent = new ProgramEvent { ProgramTypeID = AdmissionType, ClearStartDate = residentIncomeModel.AdmitDate, }; resident.ProgramEvents.Add(admitEvent); db.Benefits.Add(benefit); resident.Benefit = benefit; db.SaveChanges(); } else { ModelState.AddModelError("", "This resident may already exist in the system. Please check their full name, birthdate, and service branch. Try to add another resident to continue."); return(View(residentIncomeModel)); } } } catch (DataException dex) { //Log the error (uncomment dex variable name and add a line here to write a log. ModelState.AddModelError(dex.Message, "Unable to save changes. Try again, and if the problem persists, see your system administrator."); } finally { resident.Dispose(); } if (TryUpdateModel(residentIncomeModel, "", new string[] { "LastName", "FirstMidName", "Ethnicity", "StateTerritoryID", "ReferralID", "Gender", "Religion", "ClearBirthdate", "ServiceBranch", "NGReserve", "Note", "IsNoncombat", "InVetCourt", "Benefit", "MilitaryCampaigns", "TotalBenefitAmount" })) { try { UpdateResidentCampaigns(selectedCampaigns, resident); db.SaveChanges(); TempData["UserMessage"] = residentIncomeModel.LastName + " has been admitted into your center. "; return(RedirectToAction("Manage", new RouteValueDictionary( new { controller = "ProgramEvents", action = "Manage", Id = resident.ResidentID, FromPage = 1 }))); } catch (DataException /* dex */) { //Log the error (uncomment dex variable name and add a line here to write a log. ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists, see your system administrator."); } finally { resident.Dispose(); } } resident.Dispose(); return(View(residentIncomeModel)); }