예제 #1
0
        /// <summary>
        /// Converts the billing address into an order address and saves it. If any changes to the address should be saved for future usage
        /// then the address will also be converted as a customer addresses and gets related to the current contact.
        /// </summary>
        /// <param name="checkoutViewModel">The view model representing the purchase order.</param>
        private void SaveBillingAddress(CheckoutViewModel checkoutViewModel)
        {
            if (!_addressBookService.CanSave(checkoutViewModel.BillingAddress))
            {
                return;
            }

            var orderAddress = _checkoutService.AddNewOrderAddress();

            _addressBookService.MapModelToOrderAddress(checkoutViewModel.BillingAddress, orderAddress);
            orderAddress.Name = Guid.NewGuid().ToString();
            orderAddress.AcceptChanges();
            _checkoutService.UpdateBillingAddressId(orderAddress.Name);

            if (checkoutViewModel.BillingAddress.SaveAddress && User.Identity.IsAuthenticated)
            {
                var currentContact  = _customerContext.CurrentContact.CurrentContact;
                var customerAddress = currentContact.ContactAddresses.FirstOrDefault(x => x.AddressId == checkoutViewModel.BillingAddress.AddressId)
                                      ?? CustomerAddress.CreateInstance();

                _addressBookService.MapModelToCustomerAddress(checkoutViewModel.BillingAddress, customerAddress);

                if (checkoutViewModel.BillingAddress.AddressId == null)
                {
                    currentContact.AddContactAddress(customerAddress);
                }
                else
                {
                    BusinessManager.Update(customerAddress);
                }
                currentContact.SaveChanges();
            }
        }
        public IActionResult Save(AddressViewModel viewModel, string returnUrl = "")
        {
            var referenceSettings = _settingsService.GetSiteSettings <ReferencePageSettings>();

            if (string.IsNullOrEmpty(viewModel.Address.Name))
            {
                ModelState.AddModelError("Address.Name", _localizationService.GetString("/Shared/Address/Form/Empty/Name", "Name is required"));
            }

            if (!_addressBookService.CanSave(viewModel.Address))
            {
                ModelState.AddModelError("Address.Name", _localizationService.GetString("/AddressBook/Form/Error/ExistingAddress", "An address with the same name already exists"));
            }

            if (!ModelState.IsValid)
            {
                _addressBookService.LoadAddress(viewModel.Address);

                return(AddressEditView(viewModel));
            }

            _addressBookService.Save(viewModel.Address);

            if (string.IsNullOrEmpty(returnUrl))
            {
                return(RedirectToAction("Index", new { node = referenceSettings?.AddressBookPage ?? ContentReference.StartPage }));
            }

            return(Redirect(returnUrl));
        }
예제 #3
0
        public ActionResult AddAddress(CheckoutPage currentPage, AddressModel viewModel, string returnUrl)
        {
            if (string.IsNullOrEmpty(viewModel.Name))
            {
                ModelState.AddModelError("Address.Name", _localizationService.GetString("/Shared/Address/Form/Empty/Name", "Name is required"));
            }

            if (!_addressBookService.CanSave(viewModel))
            {
                ModelState.AddModelError("Address.Name", _localizationService.GetString("/AddressBook/Form/Error/ExistingAddress", "An address with the same name already exists"));
            }

            if (!ModelState.IsValid)
            {
                var error = ModelState.Select(x =>
                {
                    if (x.Value.Errors.Count > 0)
                    {
                        return(x.Key + ": " + string.Join(" ", x.Value.Errors.Select(y => y.ErrorMessage)) + "</br>");
                    }
                    return("");
                });

                return(Json(new { Status = false, Message = error }));
            }

            _addressBookService.Save(viewModel);
            return(Json(new { Status = true, RedirectUrl = returnUrl }));
        }
예제 #4
0
        public ActionResult Save(AddressViewModel viewModel, string returnUrl = "")
        {
            if (string.IsNullOrEmpty(viewModel.Address.Name))
            {
                ModelState.AddModelError("Address.Name", _localizationService.GetString("/Shared/Address/Form/Empty/Name", "Name is required"));
            }

            if (!_addressBookService.CanSave(viewModel.Address))
            {
                ModelState.AddModelError("Address.Name", _localizationService.GetString("/AddressBook/Form/Error/ExistingAddress", "An address with the same name already exists"));
            }

            if (!ModelState.IsValid)
            {
                _addressBookService.LoadAddress(viewModel.Address);

                return(AddressEditView(viewModel));
            }

            _addressBookService.Save(viewModel.Address);

            if (Request.IsAjaxRequest())
            {
                return(Json(viewModel.Address));
            }

            if (string.IsNullOrEmpty(returnUrl))
            {
                return(RedirectToAction("Index", new { node = GetStartPage().AddressBookPage }));
            }

            return(Redirect(returnUrl));
        }
예제 #5
0
        private void SaveToAddressBookIfNeccessary(ShippingAddress address)
        {
            if (address.SaveAddress && User.Identity.IsAuthenticated && _addressBookService.CanSave(address))
            {
                var currentContact  = _customerContext.CurrentContact.CurrentContact;
                var customerAddress = currentContact.ContactAddresses.FirstOrDefault(x => x.AddressId == address.AddressId) ?? CustomerAddress.CreateInstance();

                _addressBookService.MapModelToCustomerAddress(address, customerAddress);

                if (address.AddressId == null)
                {
                    currentContact.AddContactAddress(customerAddress);
                }
                else
                {
                    BusinessManager.Update(customerAddress);
                }
                currentContact.SaveChanges();
            }
        }
        public ActionResult Save(AddressBookPage currentPage, AddressViewModel viewModel)
        {
            if (String.IsNullOrEmpty(viewModel.Address.Name))
            {
                ModelState.AddModelError("Address.Name", _localizationService.GetString("/Shared/Address/Form/Empty/Name"));
            }

            if (!_addressBookService.CanSave(viewModel.Address))
            {
                ModelState.AddModelError("Address.Name", _localizationService.GetString("/AddressBook/Form/Error/ExistingAddress"));
            }

            if (!ModelState.IsValid)
            {
                _addressBookService.LoadAddress(viewModel.Address);
                viewModel.CurrentPage = currentPage;

                return(View("EditForm", viewModel));
            }

            _addressBookService.Save(viewModel.Address);

            return(RedirectToAction("Index", new { node = GetStartPage().AddressBookPage }));
        }