Esempio n. 1
0
        public void UpdateAddress(ModelStateDictionary ModelState)
        {
            if (ResCodeId == 0)
            {
                ResCodeId = null;
            }
            var p = DbUtil.Db.LoadPersonById(PeopleId);
            var f = p.Family;

            var addrok = false;

            if (City.HasValue() && State.HasValue())
            {
                addrok = true;
            }
            if (Zip.HasValue())
            {
                addrok = true;
            }
            if (!City.HasValue() && !State.HasValue() && !Zip.HasValue())
            {
                addrok = true;
            }
            if (!addrok)
            {
                ModelState.AddModelError("zip", "city/state required or zip required (or \"na\" in all)");
            }

            if (Address1.HasValue() && (City.HasValue() || State.HasValue() || Zip.HasValue()) &&
                (Country == "United States" || !Country.HasValue()))
            {
                var r = AddressVerify.LookupAddress(Address1, Address2, City, State, Zip);
                if (r.Line1 != "error")
                {
                    if (!r.found)
                    {
                        ModelState.AddModelError("zip", r.address + ", if your address will not validate, change the country to 'USA, Not Validated'");
                        return;
                    }
                    if (r.Line1 != Address1)
                    {
                        ModelState.AddModelError("address1", "address changed from '{0}'".Fmt(Address1));
                        Address1 = r.Line1;
                    }
                    if (r.Line2 != (Address2 ?? ""))
                    {
                        ModelState.AddModelError("address2", "address2 changed from '{0}'".Fmt(Address2));
                        Address2 = r.Line2;
                    }
                    if (r.City != (City ?? ""))
                    {
                        ModelState.AddModelError("city", "city changed from '{0}'".Fmt(City));
                        City = r.City;
                    }
                    if (r.State != (State ?? ""))
                    {
                        ModelState.AddModelError("state", "state changed from '{0}'".Fmt(State));
                        State = r.State;
                    }
                    if (r.Zip != (Zip ?? ""))
                    {
                        ModelState.AddModelError("zip", "zip changed from '{0}'".Fmt(Zip));
                        Zip = r.Zip;
                    }
                    if (!ModelState.IsValid)
                    {
                        return;
                    }
                }
            }


            switch (Name)
            {
            case "FamilyAddr":
                UpdateValue(f, "AddressLineOne", Address1);
                UpdateValue(f, "AddressLineTwo", Address2);
                UpdateValue(f, "AddressToDate", ToDt);
                UpdateValue(f, "CityName", City);
                UpdateValue(f, "StateCode", State);
                UpdateValue(f, "ResCodeId", ResCodeId);
                UpdateValue(f, "ZipCode", Zip ?? "");
                UpdateValue(f, "CountryName", Country);
                if (Preferred)
                {
                    UpdateValue(p, "AddressTypeId", 10);
                }
                if (fsb.Length > 0)
                {
                    BadAddress = false;
                }
                UpdateValue(f, "BadAddressFlag", BadAddress);
                break;

            case "PersonalAddr":
                UpdateValue(p, "AddressLineOne", Address1);
                UpdateValue(p, "AddressLineTwo", Address2);
                UpdateValue(p, "AddressToDate", ToDt);
                UpdateValue(p, "CityName", City);
                UpdateValue(p, "StateCode", State);
                UpdateValue(p, "ResCodeId", ResCodeId);
                UpdateValue(p, "ZipCode", Zip ?? "");
                UpdateValue(p, "CountryName", Country);
                if (Preferred)
                {
                    UpdateValue(p, "AddressTypeId", 30);
                }
                if (psb.Length > 0)
                {
                    BadAddress = false;
                }
                UpdateValue(p, "BadAddressFlag", BadAddress);
                break;
            }
            if (psb.Length > 0)
            {
                var c = new ChangeLog
                {
                    UserPeopleId = Util.UserPeopleId.Value,
                    PeopleId     = PeopleId,
                    Field        = Name,
                    Data         = "<table>\n" + psb + "</table>",
                    Created      = Util.Now
                };
                DbUtil.Db.ChangeLogs.InsertOnSubmit(c);
            }
            if (fsb.Length > 0)
            {
                var c = new ChangeLog
                {
                    FamilyId     = p.FamilyId,
                    UserPeopleId = Util.UserPeopleId.Value,
                    PeopleId     = PeopleId,
                    Field        = Name,
                    Data         = "<table>\n" + fsb + "</table>",
                    Created      = Util.Now
                };
                DbUtil.Db.ChangeLogs.InsertOnSubmit(c);
            }
            try
            {
                DbUtil.Db.SubmitChanges();
            }
            catch (InvalidOperationException ex)
            {
                ModelState.AddModelError("error", ex.Message);
            }

            if (!HttpContext.Current.User.IsInRole("Access"))
            {
                if (psb.Length > 0 || fsb.Length > 0)
                {
                    DbUtil.Db.EmailRedacted(p.FromEmail, DbUtil.Db.GetNewPeopleManagers(),
                                            "Address Info Changed",
                                            "{0} changed the following information:<br />\n<table>{1}{2}</table>"
                                            .Fmt(Util.UserName, psb.ToString(), fsb.ToString()));
                }
            }
        }
Esempio n. 2
0
        public void UpdateAddress(bool forceSave = false)
        {
            var p = DbUtil.Db.LoadPersonById(PeopleId);
            var f = p.Family;

            if (!forceSave)
            {
                if (!addrok)
                {
                    return;
                }

                if (Address1.HasValue() && (City.HasValue() || State.Value.HasValue() || Zip.HasValue()) &&
                    (Country.Value == "United States" || !Country.Value.HasValue()))
                {
                    Result = AddressVerify.LookupAddress(Address1, Address2, City, State.Value, Zip);
                    if (Result.Line1 == "error")
                    {
                        error          = "network error";
                        Result.address = AddrCityStateZip();
                        return;
                    }
                    if (resultnotfound)
                    {
                        return;
                    }
                    if (Result.Changed(Address1, Address2, City, State.Value, Zip))
                    {
                        resultchanged = true;
                        SetAddressInfo(PeopleId, Name);
                        return;
                    }
                }
                // at this point the address validated just fine.
            }

            int?ResCodeId = ResCode.Value.ToInt();

            if (ResCodeId == 0)
            {
                ResCodeId = null;
            }

            switch (Name)
            {
            case "FamilyAddr":
                UpdateValue(f, "AddressLineOne", Address1);
                UpdateValue(f, "AddressLineTwo", Address2);
                UpdateValue(f, "AddressToDate", ToDt);
                UpdateValue(f, "CityName", City);
                UpdateValue(f, "StateCode", State.Value);
                UpdateValue(f, "ResCodeId", ResCodeId);
                UpdateValue(f, "ZipCode", Zip ?? "");
                UpdateValue(f, "CountryName", Country.Value);
                if (Preferred)
                {
                    UpdateValue(p, "AddressTypeId", 10);
                }
                if (fsb.Length > 0)
                {
                    BadAddress = false;
                }
                UpdateValue(f, "BadAddressFlag", BadAddress);
                break;

            case "PersonalAddr":
                UpdateValue(p, "AddressLineOne", Address1);
                UpdateValue(p, "AddressLineTwo", Address2);
                UpdateValue(p, "AddressToDate", ToDt);
                UpdateValue(p, "CityName", City);
                UpdateValue(p, "StateCode", State.Value);
                UpdateValue(p, "ResCodeId", ResCodeId);
                UpdateValue(p, "ZipCode", Zip ?? "");
                UpdateValue(p, "CountryName", Country.Value);
                if (Preferred)
                {
                    UpdateValue(p, "AddressTypeId", 30);
                }
                if (psb.Length > 0)
                {
                    BadAddress = false;
                }
                UpdateValue(p, "BadAddressFlag", BadAddress);
                break;
            }
            if (psb.Length > 0)
            {
                var c = new ChangeLog
                {
                    UserPeopleId = Util.UserPeopleId.Value,
                    PeopleId     = PeopleId,
                    Field        = Name,
                    Data         = "<table>\n" + psb + "</table>",
                    Created      = Util.Now
                };
                DbUtil.Db.ChangeLogs.InsertOnSubmit(c);
            }
            if (fsb.Length > 0)
            {
                var c = new ChangeLog
                {
                    FamilyId     = p.FamilyId,
                    UserPeopleId = Util.UserPeopleId.Value,
                    PeopleId     = PeopleId,
                    Field        = Name,
                    Data         = "<table>\n" + fsb + "</table>",
                    Created      = Util.Now
                };
                DbUtil.Db.ChangeLogs.InsertOnSubmit(c);
            }
            try
            {
                DbUtil.Db.SubmitChanges();
                DbUtil.LogActivity("Update Address for: {0}".Fmt(person.Name));
            }
            catch (InvalidOperationException ex)
            {
                error = ex.Message;
                return;
            }
            saved = true;

            if (!HttpContext.Current.User.IsInRole("Access"))
            {
                if (psb.Length > 0 || fsb.Length > 0)
                {
                    DbUtil.Db.EmailRedacted(p.FromEmail, DbUtil.Db.GetNewPeopleManagers(),
                                            "Address Info Changed",
                                            "{0} changed the following information:<br />\n<table>{1}{2}</table>"
                                            .Fmt(Util.UserName, psb.ToString(), fsb.ToString()));
                }
            }
        }