Пример #1
0
 /// <summary>
 /// Ctor
 /// </summary>
 /// <param name="cacheManager">Cache manager</param>
 /// <param name="addressRepository">Address repository</param>
 /// <param name="countryService">Country service</param>
 /// <param name="stateProvinceService">State/province service</param>
 /// <param name="addressAttributeService">Address attribute service</param>
 /// <param name="eventPublisher">Event publisher</param>
 /// <param name="addressSettings">Address settings</param>
 public AddressDomainService(ICacheManager cacheManager,
                             IAddressRepository addressRepository,
                             CountryDomainService countryService,
                             StateProvinceDomainService stateProvinceService,
                             AddressAttributeDomainService addressAttributeService,
                             //IEventPublisher eventPublisher,
                             IStoreContext storeContext,
                             SettingDomainService settingDomainService)
 {
     this._cacheManager            = cacheManager;
     this._addressRepository       = addressRepository;
     this._countryService          = countryService;
     this._stateProvinceService    = stateProvinceService;
     this._addressAttributeService = addressAttributeService;
     //this._eventPublisher = eventPublisher;
     //this._addressSettings = addressSettings;
     this._storeContext    = storeContext;
     _settingDomainService = settingDomainService;
 }
Пример #2
0
        //address
        /// <summary>
        /// Prepare address model
        /// </summary>
        /// <param name="model">Model</param>
        /// <param name="address">Address</param>
        /// <param name="excludeProperties">A value indicating whether to exclude properties</param>
        /// <param name="addressSettings">Address settings</param>
        /// <param name="localizationService">Localization service (used to prepare a select list)</param>
        /// <param name="stateProvinceService">State service (used to prepare a select list). null to don't prepare the list.</param>
        /// <param name="addressAttributeService">Address attribute service. null to don't prepare the list.</param>
        /// <param name="addressAttributeParser">Address attribute parser. null to don't prepare the list.</param>
        /// <param name="addressAttributeFormatter">Address attribute formatter. null to don't prepare the formatted custom attributes.</param>
        /// <param name="loadCountries">A function to load countries  (used to prepare a select list). null to don't prepare the list.</param>
        /// <param name="prePopulateWithCustomerFields">A value indicating whether to pre-populate an address with customer fields entered during registration. It's used only when "address" parameter is set to "null"</param>
        /// <param name="customer">Customer record which will be used to pre-populate address. Used only when "prePopulateWithCustomerFields" is "true".</param>
        /// <param name="overrideAttributesXml">When specified we do not use attributes of an address; if null, then already saved ones are used</param>
        public static void PrepareModel(this AddressDto model,
                                        Address address,
                                        bool excludeProperties,
                                        AddressSettings addressSettings,
                                        //ILocalizationService localizationService = null,
                                        GenericAttributeDomianService genericAttributeDomianService,
                                        StateProvinceDomainService stateProvinceService       = null,
                                        AddressAttributeDomainService addressAttributeService = null,
                                        IAddressAttributeParser addressAttributeParser        = null,
                                        IAddressAttributeFormatter addressAttributeFormatter  = null,
                                        Func <IList <Country> > loadCountries = null,
                                        bool prePopulateWithCustomerFields    = false,
                                        User customer = null,
                                        string overrideAttributesXml = "")
        {
            if (model == null)
            {
                throw new ArgumentNullException("model");
            }

            if (addressSettings == null)
            {
                throw new ArgumentNullException("addressSettings");
            }

            if (!excludeProperties && address != null)
            {
                model.Id          = address.Id;
                model.FirstName   = address.FirstName;
                model.LastName    = address.LastName;
                model.Email       = address.Email;
                model.Company     = address.Company;
                model.CountryId   = address.CountryId;
                model.CountryName = address.Country != null
                    ? address.Country.Name
                    : null;
                model.StateProvinceId   = address.StateProvinceId;
                model.StateProvinceName = address.StateProvince != null
                    ? address.StateProvince.Name
                    : null;
                model.City          = address.City;
                model.Address1      = address.Address1;
                model.Address2      = address.Address2;
                model.ZipPostalCode = address.ZipPostalCode;
                model.PhoneNumber   = address.PhoneNumber;
                model.FaxNumber     = address.FaxNumber;
            }

            if (address == null && prePopulateWithCustomerFields)
            {
                if (customer == null)
                {
                    throw new Exception("Customer cannot be null when prepopulating an address");
                }
                model.Email         = customer.EmailAddress;
                model.FirstName     = customer.GetAttribute <string>(SystemCustomerAttributeNames.FirstName, genericAttributeDomianService);
                model.LastName      = customer.GetAttribute <string>(SystemCustomerAttributeNames.LastName, genericAttributeDomianService);
                model.Company       = customer.GetAttribute <string>(SystemCustomerAttributeNames.Company, genericAttributeDomianService);
                model.Address1      = customer.GetAttribute <string>(SystemCustomerAttributeNames.StreetAddress, genericAttributeDomianService);
                model.Address2      = customer.GetAttribute <string>(SystemCustomerAttributeNames.StreetAddress2, genericAttributeDomianService);
                model.ZipPostalCode = customer.GetAttribute <string>(SystemCustomerAttributeNames.ZipPostalCode, genericAttributeDomianService);
                model.City          = customer.GetAttribute <string>(SystemCustomerAttributeNames.City, genericAttributeDomianService);
                //ignore country and state for prepopulation. it can cause some issues when posting pack with errors, etc
                //model.CountryId = customer.GetAttribute<int>(SystemCustomerAttributeNames.CountryId);
                //model.StateProvinceId = customer.GetAttribute<int>(SystemCustomerAttributeNames.StateProvinceId);
                model.PhoneNumber = customer.GetAttribute <string>(SystemCustomerAttributeNames.Phone, genericAttributeDomianService);
                model.FaxNumber   = customer.GetAttribute <string>(SystemCustomerAttributeNames.Fax, genericAttributeDomianService);
            }

            //countries and states
            if (addressSettings.CountryEnabled && loadCountries != null)
            {
                //if (localizationService == null)
                //    throw new ArgumentNullException("localizationService");

                model.AvailableCountries.Add(new SelectListItemDto {
                    Text = InfoMsg.Address_SelectCountry, Value = "0"
                });
                foreach (var c in loadCountries())
                {
                    model.AvailableCountries.Add(new SelectListItemDto
                    {
                        Text     = c.Name,
                        Value    = c.Id.ToString(),
                        Selected = c.Id == model.CountryId
                    });
                }

                if (addressSettings.StateProvinceEnabled)
                {
                    //states
                    if (stateProvinceService == null)
                    {
                        throw new ArgumentNullException("stateProvinceService");
                    }

                    //var languageId = EngineContext.Current.Resolve<IWorkContext>().WorkingLanguage.Id;
                    var states = stateProvinceService
                                 .GetStateProvincesByCountryId(model.CountryId.HasValue ? model.CountryId.Value : 0)
                                 .ToList();
                    if (states.Count > 0)
                    {
                        model.AvailableStates.Add(new SelectListItemDto {
                            Text = InfoMsg.Address_SelectState, Value = "0"
                        });

                        foreach (var s in states)
                        {
                            model.AvailableStates.Add(new SelectListItemDto
                            {
                                Text     = s.Name,
                                Value    = s.Id.ToString(),
                                Selected = (s.Id == model.StateProvinceId)
                            });
                        }
                    }
                    else
                    {
                        bool anyCountrySelected = model.AvailableCountries.Any(x => x.Selected);
                        model.AvailableStates.Add(new SelectListItemDto
                        {
                            Text  = anyCountrySelected ? InfoMsg.Address_OtherNonUS : InfoMsg.Address_SelectState,
                            Value = "0"
                        });
                    }
                }
            }

            //form fields
            model.CompanyEnabled         = addressSettings.CompanyEnabled;
            model.CompanyRequired        = addressSettings.CompanyRequired;
            model.StreetAddressEnabled   = addressSettings.StreetAddressEnabled;
            model.StreetAddressRequired  = addressSettings.StreetAddressRequired;
            model.StreetAddress2Enabled  = addressSettings.StreetAddress2Enabled;
            model.StreetAddress2Required = addressSettings.StreetAddress2Required;
            model.ZipPostalCodeEnabled   = addressSettings.ZipPostalCodeEnabled;
            model.ZipPostalCodeRequired  = addressSettings.ZipPostalCodeRequired;
            model.CityEnabled            = addressSettings.CityEnabled;
            model.CityRequired           = addressSettings.CityRequired;
            model.CountryEnabled         = addressSettings.CountryEnabled;
            model.StateProvinceEnabled   = addressSettings.StateProvinceEnabled;
            model.PhoneEnabled           = addressSettings.PhoneEnabled;
            model.PhoneRequired          = addressSettings.PhoneRequired;
            model.FaxEnabled             = addressSettings.FaxEnabled;
            model.FaxRequired            = addressSettings.FaxRequired;

            //customer attribute services
            if (addressAttributeService != null && addressAttributeParser != null)
            {
                PrepareCustomAddressAttributes(model, address, addressAttributeService, addressAttributeParser, overrideAttributesXml);
            }
            if (addressAttributeFormatter != null && address != null)
            {
                model.FormattedCustomAddressAttributes = addressAttributeFormatter.FormatAttributes(address.CustomAttributes);
            }
        }