public ActionResult Create(PersonContactInfoView personContactInfoView)
        {
            if (ModelState.IsValid)
            {
                TryCreatePersonContactDataDB(personContactInfoView);
                return RedirectToAction("Index", "Person");
            }

            return View(personContactInfoView);
        }
        public ActionResult Edit(PersonContactInfoView personContactInfoView)
        {
            if (!personContactInfoView.RedirectToPerson)
            {
                if (String.IsNullOrWhiteSpace(personContactInfoView.Email))
                {
                    ModelState.AddModelError("Email", "Your email is required");
                }
                personContactInfoView.CellPhoneConfirmed = true;
                personContactInfoView.HomePhoneConfirmed = true;
                personContactInfoView.WorkPhoneConfirmed = true;
            }

            if (IsUserEditingTheirContactInfo(personContactInfoView))
            {
                personContactInfoView.PromptContactInformationUpdate = false;
            }

            if (ModelState.IsValid)
            {
                TryUpdatePersonContactDataDB(personContactInfoView);

                if (!personContactInfoView.RedirectToPerson)
                {
                    PersonPrimaryInfo ppi = unitOfWork.SQLQueryRepository.GetCurrentUsersPrimaryContactInfo();
                    SessionManager.CreateSession<PersonPrimaryInfo>(ppi, SessionName.User);
                }

                if (personContactInfoView.RedirectToPerson)
                {
                    return RedirectToAction("Index", "Person");
                }
                else
                {
                    return RedirectToAction("UpdateAccountInfoUpdated", "UserAccountSettingsContact");
                }
            }

            return View(personContactInfoView);
        }
        private PersonPrimaryInfo GetPersonPrimaryInfoFromPersonContactInfoView(PersonContactInfoView personContactInfoView, PersonPrimaryInfo personPrimaryInfo)
        {
            personPrimaryInfo.PersonPrimaryInfoID = personContactInfoView.PersonPrimaryInfoID;
            personPrimaryInfo.Title = personContactInfoView.Title;
            personPrimaryInfo.FirstName = personContactInfoView.FirstName;
            personPrimaryInfo.LastName = personContactInfoView.LastName;
            personPrimaryInfo.Suffix = personContactInfoView.Suffix;
            personPrimaryInfo.IsMale = personContactInfoView.IsMale;

            if (User.IsInRole(AppRole.AccountingManager))
            {
                personPrimaryInfo.MembershipStartDate = personContactInfoView.MembershipStartDate;
                personPrimaryInfo.MembershipEndDate = personContactInfoView.MembershipEndDate;
            }

            personPrimaryInfo.PromptContactInformationUpdate = personContactInfoView.PromptContactInformationUpdate;
            personPrimaryInfo.DOB = personContactInfoView.DOB;

            return personPrimaryInfo;
        }
        private void CreateOrUpdateContactInfo(int personPrimaryInfoID, PersonContactInfoView personContactInfoView)
        {
            IEnumerable<PersonContactInfo> contactForPerson = unitOfWork.PersonContactInfoRepository.Get(x => x.PersonPrimaryInfoID == personPrimaryInfoID);
            var contactForPersons = unitOfWork.PersonContactInfoRepository.Get(x => x.PersonPrimaryInfoID == personPrimaryInfoID).ToArray();
            bool updateHome = false, updateMobile = false, updateWork = false;

            PersonContactInfo homePersonContactInfo = GetContactData("Home", contactForPerson, out updateHome);
            PersonContactInfo workPersonContactInfo = GetContactData("Work", contactForPerson, out updateWork);
            PersonContactInfo mobilePersonContactInfo = GetContactData("Mobile", contactForPerson, out updateMobile);

            homePersonContactInfo.PersonPrimaryInfoID = personPrimaryInfoID;
            homePersonContactInfo.FirmName = personContactInfoView.GetFirmName();
            homePersonContactInfo.Address1 = personContactInfoView.Address1;
            homePersonContactInfo.Address2 = personContactInfoView.Address2;
            homePersonContactInfo.City = personContactInfoView.City;
            homePersonContactInfo.State = personContactInfoView.State;
            homePersonContactInfo.Urbanization = personContactInfoView.Urbanization;
            homePersonContactInfo.ZipCode5 = personContactInfoView.ZipCode5;
            homePersonContactInfo.Address1Confirmed = personContactInfoView.Address1Confirmed;
            homePersonContactInfo.Email = personContactInfoView.Email;
            homePersonContactInfo.EmailConfirmed = personContactInfoView.EmailConfirmed;
            homePersonContactInfo.Phone = personContactInfoView.HomePhone;
            homePersonContactInfo.PhoneExtension = personContactInfoView.HomePhoneExtension;
            homePersonContactInfo.PhoneConfirmed = personContactInfoView.HomePhoneConfirmed;

            mobilePersonContactInfo.PersonPrimaryInfoID = personPrimaryInfoID;
            mobilePersonContactInfo.Phone = personContactInfoView.CellPhone;
            mobilePersonContactInfo.PhoneConfirmed = personContactInfoView.CellPhoneConfirmed;

            workPersonContactInfo.PersonPrimaryInfoID = personPrimaryInfoID;
            workPersonContactInfo.Phone = personContactInfoView.WorkPhone;
            workPersonContactInfo.PhoneExtension = personContactInfoView.WorkPhoneExtension;
            workPersonContactInfo.PhoneConfirmed = personContactInfoView.WorkPhoneConfirmed;

            ChangeModelStateForContactInfo(!updateHome, homePersonContactInfo);
            ChangeModelStateForContactInfo(!updateWork, workPersonContactInfo);
            ChangeModelStateForContactInfo(!updateMobile, mobilePersonContactInfo);

            unitOfWork.Save();
        }
        private bool TryCreatePersonContactDataDB(PersonContactInfoView personContactInfoView)
        {
            bool successful = false;
            PersonPrimaryInfo ppi = null;

            ppi = unitOfWork.PersonPrimaryInfoRepository.Insert(GetPersonPrimaryInfoFromPersonContactInfoView(personContactInfoView, new PersonPrimaryInfo()));
            unitOfWork.Save();
            CreateOrUpdateContactInfo(ppi.PersonPrimaryInfoID, personContactInfoView);

            return successful;
        }
        private bool TryUpdatePersonContactDataDB(PersonContactInfoView personContactInfoView)
        {
            bool successful = false;
            PersonPrimaryInfo ppi = unitOfWork.PersonPrimaryInfoRepository.GetByID(personContactInfoView.PersonPrimaryInfoID);

            unitOfWork.PersonPrimaryInfoRepository.Update(GetPersonPrimaryInfoFromPersonContactInfoView(personContactInfoView, ppi));
            unitOfWork.Save();
            CreateOrUpdateContactInfo(personContactInfoView.PersonPrimaryInfoID, personContactInfoView);

            return successful;
        }
        private PersonContactInfoView ImportPersonContactInfoViewData(PersonContactInfoView personContactInfoView)
        {
            PersonPrimaryInfo ppi = unitOfWork.PersonPrimaryInfoRepository.GetByID(personContactInfoView.PersonPrimaryInfoID);

            bool updateHome = false, updateMobile = false, updateWork = false;
            PersonContactInfo homePersonContactInfo = GetContactData("Home", ppi.PersonContactInfoes, out updateHome);
            PersonContactInfo workPersonContactInfo = GetContactData("Work", ppi.PersonContactInfoes, out updateWork);
            PersonContactInfo mobilePersonContactInfo = GetContactData("Mobile", ppi.PersonContactInfoes, out updateMobile);

            personContactInfoView.PersonPrimaryInfoID = ppi.PersonPrimaryInfoID;
            personContactInfoView.Title = ppi.Title;
            personContactInfoView.FirstName = ppi.FirstName;
            personContactInfoView.LastName = ppi.LastName;
            personContactInfoView.Suffix = ppi.Suffix;
            personContactInfoView.IsMale = ppi.IsMale;
            personContactInfoView.MembershipStartDate = ppi.MembershipStartDate;
            personContactInfoView.MembershipEndDate = ppi.MembershipEndDate;
            personContactInfoView.PromptContactInformationUpdate = ppi.PromptContactInformationUpdate;
            personContactInfoView.DOB = ppi.DOB;
            personContactInfoView.Email = homePersonContactInfo.Email;
            personContactInfoView.EmailConfirmed = homePersonContactInfo.EmailConfirmed;
            personContactInfoView.CellPhone = mobilePersonContactInfo.Phone;
            personContactInfoView.CellPhoneConfirmed = mobilePersonContactInfo.PhoneConfirmed;
            personContactInfoView.HomePhone = homePersonContactInfo.Phone;
            personContactInfoView.HomePhoneExtension = homePersonContactInfo.PhoneExtension;
            personContactInfoView.HomePhoneConfirmed = homePersonContactInfo.PhoneConfirmed;
            personContactInfoView.WorkPhone = homePersonContactInfo.Phone;
            personContactInfoView.WorkPhoneExtension = workPersonContactInfo.PhoneExtension;
            personContactInfoView.WorkPhoneConfirmed = workPersonContactInfo.PhoneConfirmed;
            personContactInfoView.FirmName = homePersonContactInfo.FirmName;
            personContactInfoView.Address1 = homePersonContactInfo.Address1;
            personContactInfoView.Address2 = homePersonContactInfo.Address2;
            personContactInfoView.City = homePersonContactInfo.City;
            personContactInfoView.State = homePersonContactInfo.State;
            personContactInfoView.Urbanization = homePersonContactInfo.Urbanization;
            personContactInfoView.ZipCode5 = homePersonContactInfo.ZipCode5;
            personContactInfoView.Address1Confirmed = homePersonContactInfo.Address1Confirmed;

            return personContactInfoView;
        }
        private PersonContactInfoView GetPersonContactInfoView(PersonContactInfoView personContactInfoView)
        {
            if (personContactInfoView == null || personContactInfoView.PersonPrimaryInfoID == 0)
            {
                personContactInfoView = new PersonContactInfoView();
            }
            else
            {
                personContactInfoView = ImportPersonContactInfoViewData(personContactInfoView);
            }

            return personContactInfoView;
        }
        private bool IsUserEditingTheirContactInfo(PersonContactInfoView personContactInfoView)
        {
            PersonPrimaryInfo p = SessionManager.GetSession<PersonPrimaryInfo>(SessionName.User);

            return p.PersonPrimaryInfoID == personContactInfoView.PersonPrimaryInfoID;
        }