public ActionResult New()
        {
            var person = new Person();

            person.PersonID = 0;
            person.FirstName = string.Empty;
            person.LastName = string.Empty;
            person.EmailAddress = string.Empty;

            person.Phones = new List<Phone>();
            person.Phones.Add(new Phone { PhoneTypeID = 1, Number = string.Empty });
            person.Phones.Add(new Phone { PhoneTypeID = 2, Number = string.Empty });
            person.Phones.Add(new Phone { PhoneTypeID = 3, Number = string.Empty });

            var emptyAddress = new Address
            {
                StreetAddress = string.Empty,
                City = string.Empty,
                State = string.Empty,
                Zip = string.Empty
            };

            person.Addresses = new List<PersonAddressAssn>();
            person.Addresses.Add(new PersonAddressAssn { AddressTypeID = 1, Address = emptyAddress });
            person.Addresses.Add(new PersonAddressAssn { AddressTypeID = 2, Address = emptyAddress });
            person.Addresses.Add(new PersonAddressAssn { AddressTypeID = 3, Address = emptyAddress });

            ViewBag.States = stateSelectList();

            return PartialView("Edit", person);
        }
        public ActionResult Edit(int personID)
        {
            // When editing persons from here only include the phones and addresses
            var personIncludes = new PersonIncludes();
            personIncludes.Phones = true;
            personIncludes.Addressses = true;
            personIncludes.Accounts = true;
            personIncludes.Login = true;

            var person = _footlooseFSService.GetPerson(personID, personIncludes);

            // Add home phone if not in the person Object
            if (!person.Phones.Where(p => p.PhoneTypeID == 1).Any())
                person.Phones.Add(new Phone { PhoneTypeID = 1, Number = string.Empty });

            // Add work phone if not in the person Object
            if (!person.Phones.Where(p => p.PhoneTypeID == 2).Any())
                person.Phones.Add(new Phone { PhoneTypeID = 2, Number = string.Empty });

            // Add cell phone if not in the person Object
            if (!person.Phones.Where(p => p.PhoneTypeID == 3).Any())
                person.Phones.Add(new Phone { PhoneTypeID = 3, Number = string.Empty });

            var emptyAddress = new Address
            {
                StreetAddress = string.Empty,
                City = string.Empty,
                State = string.Empty,
                Zip = string.Empty
            };

            if (!person.Addresses.Where(a => a.AddressTypeID == 1).Any())
                person.Addresses.Add(new PersonAddressAssn { AddressTypeID = 1, Address = emptyAddress });

            if (!person.Addresses.Where(a => a.AddressTypeID == 2).Any())
                person.Addresses.Add(new PersonAddressAssn { AddressTypeID = 2, Address = emptyAddress });

            if (!person.Addresses.Where(a => a.AddressTypeID == 3).Any())
                person.Addresses.Add(new PersonAddressAssn { AddressTypeID = 3, Address = emptyAddress });

            if (person.Login == null)
                person.Login = new PersonLogin();

            ViewBag.States = stateSelectList();

            return PartialView(person);
        }
        public JsonResult Save(FormCollection formCollection)
        {
            var person = new Person();

            int personID = 0;
            Int32.TryParse(formCollection["personID"], out personID);

            person.PersonID = personID;
            person.FirstName = formCollection["firstName"];
            person.LastName = formCollection["lastName"];
            person.EmailAddress = formCollection["emailAddress"];

            person.Phones = new List<Phone>();
            person.Phones.Add(new Phone { PersonID = personID, PhoneTypeID = (int)PhoneTypes.Home, Number = formCollection["homePhone"] });
            person.Phones.Add(new Phone { PersonID = personID, PhoneTypeID = (int)PhoneTypes.Work, Number = formCollection["workPhone"] });
            person.Phones.Add(new Phone { PersonID = personID, PhoneTypeID = (int)PhoneTypes.Cell, Number = formCollection["cellPhone"] });

            person.Addresses = new List<PersonAddressAssn>();

            if (!string.IsNullOrEmpty(formCollection["homeStreetAddress"]))
            {
                var address = new Address();

                int homeAddressID = 0;
                Int32.TryParse(formCollection["homeAddressID"], out homeAddressID);
                address.AddressID = homeAddressID;

                address.StreetAddress = formCollection["homeStreetAddress"];
                address.City = formCollection["homeCity"];
                address.State = formCollection["homeState"];
                address.Zip = formCollection["homeZip"];

                person.Addresses.Add(new PersonAddressAssn { PersonID = personID, AddressID = homeAddressID, Address = address, AddressTypeID = (int)AddressTypes.Home });
            }

            if (!string.IsNullOrEmpty(formCollection["workStreetAddress"]))
            {
                var address = new Address();

                int workAddressID = 0;
                Int32.TryParse(formCollection["workAddressID"], out workAddressID);
                address.AddressID = workAddressID;

                address.StreetAddress = formCollection["workStreetAddress"];
                address.City = formCollection["workCity"];
                address.State = formCollection["workState"];
                address.Zip = formCollection["workZip"];

                person.Addresses.Add(new PersonAddressAssn { PersonID = personID, AddressID = workAddressID, Address = address, AddressTypeID = (int)AddressTypes.Work });
            }

            if (!string.IsNullOrEmpty(formCollection["altStreetAddress"]))
            {
                var address = new Address();

                int altAddressID = 0;
                Int32.TryParse(formCollection["altAddressID"], out altAddressID);
                address.AddressID = altAddressID;

                address.StreetAddress = formCollection["altStreetAddress"];
                address.City = formCollection["altCity"];
                address.State = formCollection["altState"];
                address.Zip = formCollection["altZip"];

                person.Addresses.Add(new PersonAddressAssn { PersonID = personID, AddressID = altAddressID, Address = address, AddressTypeID = (int)AddressTypes.Alternate });
            }

            if (person.PersonID == 0)
            {
                var opStatus = _footlooseFSService.InsertPerson(person);

                var newPerson = (Person)opStatus.Data;

                var savePersonResult = new SavePersonResult
                    {
                        Message = "The person has been created in the system.",
                        Person = newPerson,
                        PersonID = newPerson.PersonID,
                        HomeAddressID = newPerson.Addresses.Where(a => a.AddressTypeID == (int)AddressTypes.Home).Any() ?
                                        newPerson.Addresses.Where(a => a.AddressTypeID == (int)AddressTypes.Home).First().AddressID : 0,
                        WorkAddressID = newPerson.Addresses.Where(a => a.AddressTypeID == (int)AddressTypes.Work).Any() ?
                                        newPerson.Addresses.Where(a => a.AddressTypeID == (int)AddressTypes.Work).First().AddressID : 0,
                        AlternateAddressID = newPerson.Addresses.Where(a => a.AddressTypeID == (int)AddressTypes.Alternate).Any() ?
                                        newPerson.Addresses.Where(a => a.AddressTypeID == (int)AddressTypes.Alternate).First().AddressID : 0,
                    };

                return Json(savePersonResult);
            }
            else
            {
                var opStatus = _footlooseFSService.UpdatePerson(person);

                var updatedPerson = (Person)opStatus.Data;

                var savePersonResult = new SavePersonResult
                {
                    Message = "The person has been updated",
                    Person = updatedPerson,
                    PersonID = updatedPerson.PersonID,
                    HomeAddressID = updatedPerson.Addresses.Where(a => a.AddressTypeID == (int)AddressTypes.Home).Any() ?
                                    updatedPerson.Addresses.Where(a => a.AddressTypeID == (int)AddressTypes.Home).First().AddressID : 0,
                    WorkAddressID = updatedPerson.Addresses.Where(a => a.AddressTypeID == (int)AddressTypes.Work).Any() ?
                                    updatedPerson.Addresses.Where(a => a.AddressTypeID == (int)AddressTypes.Work).First().AddressID : 0,
                    AlternateAddressID = updatedPerson.Addresses.Where(a => a.AddressTypeID == (int)AddressTypes.Alternate).Any() ?
                                    updatedPerson.Addresses.Where(a => a.AddressTypeID == (int)AddressTypes.Alternate).First().AddressID : 0,
                };

                return Json(savePersonResult);
            }
        }
        public Person GetPersonById(int personID, PersonIncludes personIncludes)
        {
            using (var unitOfWork = unitOfWorkFactory.CreateUnitOfWork())
            {
                var person = unitOfWork.Persons.Find(personID, personIncludes);

                if (person != null)
                { 
                    if (personIncludes.Phones)
                    {
                        // Add home phone if not in the person Object
                        if (!person.Phones.Where(p => p.PhoneTypeID == 1).Any())
                            person.Phones.Add(new Phone { PhoneTypeID = 1, Number = string.Empty, PhoneType = new PhoneType { Name = "Home" } });

                        // Add work phone if not in the person Object
                        if (!person.Phones.Where(p => p.PhoneTypeID == 2).Any())
                            person.Phones.Add(new Phone { PhoneTypeID = 2, Number = string.Empty, PhoneType = new PhoneType { Name = "Work" } });

                        // Add cell phone if not in the person Object
                        if (!person.Phones.Where(p => p.PhoneTypeID == 3).Any())
                            person.Phones.Add(new Phone { PhoneTypeID = 3, Number = string.Empty, PhoneType = new PhoneType { Name = "Cell" } });
                    }

                    if (personIncludes.Addressses)
                    {
                        var emptyAddress = new Address
                        {
                            StreetAddress = string.Empty,
                            City = string.Empty,
                            State = string.Empty,
                            Zip = string.Empty
                        };

                        if (!person.Addresses.Where(a => a.AddressTypeID == 1).Any())
                            person.Addresses.Add(new PersonAddressAssn { AddressTypeID = 1, Address = emptyAddress, AddressType = new AddressType { Name = "Home" } });

                        if (!person.Addresses.Where(a => a.AddressTypeID == 2).Any())
                            person.Addresses.Add(new PersonAddressAssn { AddressTypeID = 2, Address = emptyAddress, AddressType = new AddressType { Name = "Work" } });

                        if (!person.Addresses.Where(a => a.AddressTypeID == 3).Any())
                            person.Addresses.Add(new PersonAddressAssn { AddressTypeID = 3, Address = emptyAddress, AddressType = new AddressType { Name = "Alternate" } });
                    }

                    if (personIncludes.Login && person.Login == null)
                        person.Login = new PersonLogin();
                }

                return person;
            }           
        }