public ActionResult EditPerson(string data)
        {
            // Authenticate first
            if (!Auth())
            {
                return(Message.createErrorReturn("Authentication failed, please try again", Message.API_ERROR_INVALID_CREDENTIALS));
            }

            Message message = Message.createFromString(data);

            Models.CheckInAPIv2.Person person = JsonConvert.DeserializeObject <Models.CheckInAPIv2.Person>(message.data);
            person.clean();

            CmsData.Person p = CurrentDatabase.LoadPersonById(person.id);

            if (p == null)
            {
                return(Message.createErrorReturn("Person not found", Message.API_ERROR_PERSON_NOT_FOUND));
            }

            CmsData.Family f = CurrentDatabase.Families.First(fam => fam.FamilyId == p.FamilyId);

            person.fillPerson(p);
            person.fillFamily(f);

            CurrentDatabase.SubmitChanges();

            AddEditPersonResults results = new AddEditPersonResults {
                familyID   = f.FamilyId,
                peopleID   = p.PeopleId,
                positionID = p.PositionInFamilyId,
                barcodeID  = CmsData.Person.Barcode(CurrentDatabase, p.PeopleId)
            };

            Message response = new Message();

            response.setNoError();
            response.count = 1;
            response.data  = SerializeJSON(results);

            return(response);
        }
        public ActionResult AddPerson(string data)
        {
            if (!Auth())
            {
                return(Message.createErrorReturn("Authentication failed, please try again", Message.API_ERROR_INVALID_CREDENTIALS));
            }

            Message message = Message.createFromString(data);

            Models.CheckInAPIv2.Person person = JsonConvert.DeserializeObject <Models.CheckInAPIv2.Person>(message.data);
            person.clean();

            // Create or Edit Family
            CmsData.Family f = person.familyID > 0 ? CurrentDatabase.Families.First(fam => fam.FamilyId == person.familyID) : new CmsData.Family();
            person.fillFamily(f);

            if (person.familyID == 0)
            {
                CurrentDatabase.Families.InsertOnSubmit(f);
            }

            // Create Person
            CmsData.Person p = new CmsData.Person {
                CreatedDate    = Util.Now,
                CreatedBy      = Util.UserId,
                MemberStatusId = MemberStatusCode.JustAdded,
                AddressTypeId  = 10,
                OriginId       = OriginCode.Visit,
                EntryPoint     = getCheckInEntryPointID(),
                CampusId       = person.campus > 0 ? person.campus : (int?)null,
                Name           = ""
            };

            person.fillPerson(p);

            // Calculate position before submitting changes so they aren't part of the calculation
            using (SqlConnection db = new SqlConnection(Util.ConnectionString)) {
                p.PositionInFamilyId = person.computePositionInFamily(db);
            }

            // p.PositionInFamilyId = CurrentDatabase.ComputePositionInFamily( person.getAge(),
            //                                                                                      person.maritalStatusID == CmsData.Codes.MaritalStatusCode.Married, f.FamilyId ) ?? CmsData.Codes.PositionInFamily.PrimaryAdult;

            f.People.Add(p);

            CurrentDatabase.SubmitChanges();

            AddEditPersonResults results = new AddEditPersonResults {
                familyID   = f.FamilyId,
                peopleID   = p.PeopleId,
                positionID = p.PositionInFamilyId
            };

            Message response = new Message();

            response.setNoError();
            response.count = 1;
            response.data  = SerializeJSON(results);

            return(response);
        }
        public ActionResult EditPerson(string data)
        {
            // Authenticate first
            if (!Auth())
            {
                return(Message.createErrorReturn("Authentication failed, please try again", Message.API_ERROR_INVALID_CREDENTIALS));
            }

            Message message = Message.createFromString(data);

            Models.CheckInAPIv2.Person person = JsonConvert.DeserializeObject <Models.CheckInAPIv2.Person>(message.data);    // new data
            person.clean();

            var NewContext = CurrentDatabase.Copy();

            CmsData.Person p = NewContext.LoadPersonById(person.id);   // existing data

            if (p == null)
            {
                return(Message.createErrorReturn("Person not found", Message.API_ERROR_PERSON_NOT_FOUND));
            }

            person.fillPerson(p);
            BasicPersonInfo m = new BasicPersonInfo()
            {
                Id = person.id
            };

            p.CopyProperties2(m);

            m.CellPhone = new CellPhoneInfo()
            {
                Number      = person.mobilePhone,
                ReceiveText = p.ReceiveSMS
            };
            m.Gender        = new Code.CodeInfo(p.GenderId, p.Gender.Description);
            m.MaritalStatus = new Code.CodeInfo(p.MaritalStatusId, p.MaritalStatus.Description);
            m.EmailAddress  = new EmailInfo()
            {
                Address = p.EmailAddress,
                Send    = p.SendEmailAddress1.GetValueOrDefault(true)
            };
            m.EmailAddress2 = new EmailInfo()
            {
                Address = p.EmailAddress2,
                Send    = p.SendEmailAddress2.GetValueOrDefault(true)
            };

            m.UpdatePerson(CurrentDatabase);
            DbUtil.LogPersonActivity($"Update Basic Info for: {m.person.Name}", m.Id, m.person.Name);

            CmsData.Family f = NewContext.Families.First(fam => fam.FamilyId == p.FamilyId);
            p.SetRecReg().MedicalDescription = person.allergies;
            p.SetRecReg().Emcontact          = person.emergencyName;
            p.SetRecReg().Emphone            = person.emergencyPhone.Truncate(50);

            var fsb = new List <ChangeDetail>();

            f.UpdateValue(fsb, "AddressLineOne", person.address);
            f.UpdateValue(fsb, "AddressLineTwo", person.address2);
            f.UpdateValue(fsb, "CityName", person.city);
            f.UpdateValue(fsb, "StateCode", person.state);
            f.UpdateValue(fsb, "ZipCode", person.zipCode);
            f.UpdateValue(fsb, "CountryName", person.country);
            f.LogChanges(NewContext, fsb, p.PeopleId, CurrentDatabase.UserPeopleId ?? 0);
            person.fillFamily(f);
            NewContext.SubmitChanges();

            AddEditPersonResults results = new AddEditPersonResults {
                familyID   = f.FamilyId,
                peopleID   = p.PeopleId,
                positionID = p.PositionInFamilyId,
                barcodeID  = CmsData.Person.Barcode(CurrentDatabase, p.PeopleId)
            };

            Message response = new Message();

            response.setNoError();
            response.count = 1;
            response.data  = SerializeJSON(results);

            return(response);
        }