Ejemplo n.º 1
0
        public ActionResult AddressEdit(AddressEditModel model)
        {
            if (ModelState.IsValid)
            {
                if (model.Address != null)
                {
                    var u = _userClient.GetCurrentCustomer(false);
                    Organization org = null;

                    if (!String.IsNullOrEmpty(model.OrganizationId))
                    {
                        org = _userClient.GetOrganizationById(model.OrganizationId);
                    }

                    var parent = org == null ? u : (Member)org;

                    if (model.IsDefaultBilling)
                    {
                        //remove previous default Billing
                        var address =
                            parent.Addresses.FirstOrDefault(x => x.Name.Contains(UserHelper.DefaultBilling));
                        if (address != null)
                        {
                            address.Name = address.Name.Replace(UserHelper.DefaultBilling, string.Empty);
                        }

                        model.Address.Name += UserHelper.DefaultBilling;
                    }

                    if (model.IsDefaultShipping)
                    {
                        //remove previous default Shipping
                        var address =
                            parent.Addresses.FirstOrDefault(x => x.Name.Contains(UserHelper.DefaultShipping));
                        if (address != null)
                        {
                            address.Name = address.Name.Replace(UserHelper.DefaultShipping, string.Empty);
                        }

                        model.Address.Name += UserHelper.DefaultShipping;
                    }

                    var exisintgAddress = parent.Addresses.FirstOrDefault(p => p.AddressId.Equals(model.Address.AddressId));

                    if (exisintgAddress != null)
                    {
                        exisintgAddress.InjectFrom(model.Address);
                    }
                    else
                    {
                        //create new
                        var newAddress = new Address();
                        newAddress.InjectFrom(model.Address);

                        if (parent.Addresses.Count == 0)
                        {
                            newAddress.Name += String.Format("{0}{1}", UserHelper.DefaultBilling,
                                                                UserHelper.DefaultShipping);
                        }

                        parent.Addresses.Add(newAddress);
                    }

                    _userClient.SaveCustomerChanges(u.MemberId);
                }

                return RedirectToAction(String.IsNullOrEmpty(model.OrganizationId) ? "AddressBook" : "CompanyAddressBook");
            }

            model.Countries = _countryClient.GetAllCountries();
            return View(model);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Addresses edit.
        /// </summary>
        /// <param name="addressId">The address identifier.</param>
        /// <param name="organizationId">The organization identifier.</param>
        /// <returns>ActionResult.</returns>
        public ActionResult AddressEdit(string addressId, string organizationId)
        {
            var aem = new AddressEditModel();
            var user = _userClient.GetCurrentCustomer();

            aem.OrganizationId = organizationId;

            var existingAddress = !String.IsNullOrEmpty(organizationId)
                              ? _userClient.GetCompanyAddress(addressId, organizationId)
                              : _userClient.GetUserAddress(addressId);

            if (existingAddress == null)
            {
                //fill some entries from contact
                aem.Address.Name = aem.Address.AddressId;

                if (user != null)
                {
                    //aem.Address.MemberId = user.MemberId;

                    if (String.IsNullOrEmpty(aem.OrganizationId))
                    {
                        var names = user.FullName.Split(' ');
                        if (names.Length > 0)
                        {
                            aem.Address.FirstName = names[0];
                        }
                        if (names.Length > 1)
                        {
                            aem.Address.LastName = names[1];
                        }
                    }

                    var firstOrDefault = user.Emails.FirstOrDefault();
                    if (firstOrDefault != null)
                    {
                        aem.Address.Email = firstOrDefault.Address;
                    }
                }
            }
            else
            {
                aem.Address.InjectFrom(existingAddress);
            }

            aem.Countries = _countryClient.GetAllCountries();
            return View(aem);
        }