public coreModel.Store[] GetByIds(string[] ids) { var retVal = new List <coreModel.Store>(); using (var repository = _repositoryFactory()) { var fulfillmentCenters = _commerceService.GetAllFulfillmentCenters().ToList(); var dbStores = repository.GetStoresByIds(ids); foreach (var dbStore in dbStores) { //Load original typed shipping method and populate it personalized information from db var store = dbStore.ToCoreModel(_shippingService.GetAllShippingMethods(), _paymentService.GetAllPaymentMethods(), _taxService.GetAllTaxProviders()); store.ReturnsFulfillmentCenter = fulfillmentCenters.FirstOrDefault(x => x.Id == dbStore.ReturnsFulfillmentCenterId); store.FulfillmentCenter = fulfillmentCenters.FirstOrDefault(x => x.Id == dbStore.FulfillmentCenterId); //Set default settings for store it can be override by store instance setting in LoadEntitySettingsValues store.Settings = _settingManager.GetModuleSettings("VirtoCommerce.Store"); _settingManager.LoadEntitySettingsValues(store); _dynamicPropertyService.LoadDynamicPropertyValues(store); retVal.Add(store); } } _commerceService.LoadSeoForObjects(retVal.ToArray()); return(retVal.ToArray()); }
public coreModel.Store GetById(string id) { var cacheKey = CacheKey.Create("StoreModule", "GetById", id); return(_cacheManager.Get(cacheKey, () => { coreModel.Store retVal = null; using (var repository = _repositoryFactory()) { var entity = repository.GetStoreById(id); if (entity != null) { //Load original typed shipping method and populate it personalized information from db retVal = entity.ToCoreModel(_shippingService.GetAllShippingMethods(), _paymentService.GetAllPaymentMethods()); var fulfillmentCenters = _commerceService.GetAllFulfillmentCenters().ToList(); retVal.ReturnsFulfillmentCenter = fulfillmentCenters.FirstOrDefault(x => x.Id == entity.ReturnsFulfillmentCenterId); retVal.FulfillmentCenter = fulfillmentCenters.FirstOrDefault(x => x.Id == entity.FulfillmentCenterId); retVal.SeoInfos = _commerceService.GetObjectsSeo(new[] { id }).ToList(); LoadObjectSettings(_settingManager, retVal); } } return retVal; })); }
/// <summary> /// Get nearest fulfillment center by given location /// </summary> /// <param name="centerRequest">Request with needed params to get nearest fulfillment center</param> /// <returns>Nearest fulfillment center</returns> public FulfillmentCenterDto GetNearestFulfillmentCenter(GettingNearestCenterRequestDto centerRequest) { if (centerRequest == null) { throw new ArgumentNullException("centerRequest"); } var inventories = _inventoryRepository.Inventories; var allCenters = _commerceService.GetAllFulfillmentCenters() .Where(x => x.PostalCode == centerRequest.PostalCode); foreach (FulfillmentCenter center in allCenters) { var centerInventories = inventories.Where(x => x.FulfillmentCenterId == center.Id); bool allProductsExist = true; foreach (string productId in centerRequest.ProductIds) { if (!centerInventories.Any(x => x.Sku == productId)) { allProductsExist = false; break; } } if (allProductsExist) { return(center.ToDto()); } } return(null); }
public IHttpActionResult GetProductsInventories([FromUri] string[] ids) { var retVal = new List <webModel.InventoryInfo>(); var allFulfillments = _commerceService.GetAllFulfillmentCenters(); var inventories = _inventoryService.GetProductsInventoryInfos(ids).ToList(); foreach (var productId in ids) { foreach (var fulfillment in allFulfillments) { var productInventory = inventories.FirstOrDefault(x => x.ProductId == productId && x.FulfillmentCenterId == fulfillment.Id); if (productInventory == null) { productInventory = new coreModel.InventoryInfo { FulfillmentCenterId = fulfillment.Id, ProductId = productId }; } var webModelInventory = productInventory.ToWebModel(); webModelInventory.FulfillmentCenter = fulfillment.ToWebModel(); retVal.Add(webModelInventory); } } return(Ok(retVal.ToArray())); }
public Store[] GetByIds(string[] ids) { var stores = new List <Store>(); using (var repository = _repositoryFactory()) { var fulfillmentCenters = _commerceService.GetAllFulfillmentCenters().ToList(); var dbStores = repository.GetStoresByIds(ids); foreach (var dbStore in dbStores) { var store = AbstractTypeFactory <Store> .TryCreateInstance(); dbStore.ToModel(store); //Return all registered methods with store settings store.PaymentMethods = _paymentService.GetAllPaymentMethods(); foreach (var paymentMethod in store.PaymentMethods) { var dbStoredPaymentMethod = dbStore.PaymentMethods.FirstOrDefault(x => x.Code.EqualsInvariant(paymentMethod.Code)); if (dbStoredPaymentMethod != null) { dbStoredPaymentMethod.ToModel(paymentMethod); } } store.ShippingMethods = _shippingService.GetAllShippingMethods(); foreach (var shippingMethod in store.ShippingMethods) { var dbStoredShippingMethod = dbStore.ShippingMethods.FirstOrDefault(x => x.Code.EqualsInvariant(shippingMethod.Code)); if (dbStoredShippingMethod != null) { dbStoredShippingMethod.ToModel(shippingMethod); } } store.TaxProviders = _taxService.GetAllTaxProviders(); foreach (var taxProvider in store.TaxProviders) { var dbStoredTaxProvider = dbStore.TaxProviders.FirstOrDefault(x => x.Code.EqualsInvariant(taxProvider.Code)); if (dbStoredTaxProvider != null) { dbStoredTaxProvider.ToModel(taxProvider); } } store.ReturnsFulfillmentCenter = fulfillmentCenters.FirstOrDefault(x => x.Id == dbStore.ReturnsFulfillmentCenterId); store.FulfillmentCenter = fulfillmentCenters.FirstOrDefault(x => x.Id == dbStore.FulfillmentCenterId); //Set default settings for store it can be override by store instance setting in LoadEntitySettingsValues store.Settings = _settingManager.GetModuleSettings("VirtoCommerce.Store"); _settingManager.LoadEntitySettingsValues(store); stores.Add(store); } } var result = stores.ToArray(); _dynamicPropertyService.LoadDynamicPropertyValues(result); _commerceService.LoadSeoForObjects(result); return(result); }
private BackupObject GetBackupObject(Action <ExportImportProgressInfo> progressCallback) { progressCallback(new ExportImportProgressInfo("fulfillmentCenters loading")); var fulfillments = _commerceService.GetAllFulfillmentCenters().ToArray(); progressCallback(new ExportImportProgressInfo(string.Format("fulfillmentCenters loaded: {0}", fulfillments.Count()))); return(new BackupObject() { FulfillmentCenters = fulfillments }); }
private void SaveProducts(coreModel.Catalog catalog, IEnumerable <CsvProduct> csvProducts, ExportImportProgressInfo progressInfo, Action <ExportImportProgressInfo> progressCallback) { int counter = 0; var notifyProductSizeLimit = 10; progressInfo.TotalCount = csvProducts.Count(); var defaultFulfilmentCenter = _commerceService.GetAllFulfillmentCenters().FirstOrDefault(); var options = new ParallelOptions() { MaxDegreeOfParallelism = 10 }; DetectParents(csvProducts); //First need create main products, second will be created variations foreach (var group in new IEnumerable <CsvProduct>[] { csvProducts.Where(x => x.MainProduct == null), csvProducts.Where(x => x.MainProduct != null) }) { Parallel.ForEach(group, options, csvProduct => { try { SaveProduct(catalog, defaultFulfilmentCenter, csvProduct); } catch (Exception ex) { lock (_lockObject) { progressInfo.Errors.Add(ex.ToString()); progressCallback(progressInfo); } } finally { lock (_lockObject) { //Raise notification each notifyProductSizeLimit category counter++; progressInfo.ProcessedCount = counter; progressInfo.Description = string.Format("Creating products: {0} of {1} created", progressInfo.ProcessedCount, progressInfo.TotalCount); if (counter % notifyProductSizeLimit == 0 || counter == progressInfo.TotalCount) { progressCallback(progressInfo); } } } }); } ; }
public coreModel.Store GetById(string id) { coreModel.Store retVal = null; using (var repository = _repositoryFactory()) { var entity = repository.GetStoreById(id); if (entity != null) { //Load original typed shipping method and populate it personalized information from db retVal = entity.ToCoreModel(_shippingService.GetAllShippingMethods(), _paymentService.GetAllPaymentMethods(), _taxService.GetAllTaxProviders()); var fulfillmentCenters = _commerceService.GetAllFulfillmentCenters().ToList(); retVal.ReturnsFulfillmentCenter = fulfillmentCenters.FirstOrDefault(x => x.Id == entity.ReturnsFulfillmentCenterId); retVal.FulfillmentCenter = fulfillmentCenters.FirstOrDefault(x => x.Id == entity.FulfillmentCenterId); _commerceService.LoadSeoForObjects(new[] { retVal }); _settingManager.LoadEntitySettingsValues(retVal); _dynamicPropertyService.LoadDynamicPropertyValues(retVal); } } return(retVal); }
public IHttpActionResult GetProductsInventories([ModelBinder(typeof(IdsStringArrayBinder))] string[] ids) { var result = new List <webModel.InventoryInfo>(); var allFulfillments = _commerceService.GetAllFulfillmentCenters().ToArray(); var inventories = _inventoryService.GetProductsInventoryInfos(ids).ToList(); foreach (var productId in ids) { foreach (var fulfillment in allFulfillments) { var productInventory = inventories.FirstOrDefault(x => x.ProductId == productId && x.FulfillmentCenterId == fulfillment.Id) ?? new coreModel.InventoryInfo { FulfillmentCenterId = fulfillment.Id, ProductId = productId }; var webModelInventory = productInventory.ToWebModel(); webModelInventory.FulfillmentCenter = fulfillment.ToWebModel(); result.Add(webModelInventory); } } return(Ok(result.ToArray())); }
private BackupObject GetBackupObject(Action <ExportImportProgressInfo> progressCallback) { progressCallback(new ExportImportProgressInfo("fulfillmentCenters loading")); var fulfillments = _commerceService.GetAllFulfillmentCenters().ToArray(); progressCallback(new ExportImportProgressInfo(string.Format("fulfillmentCenters loaded: {0}", fulfillments.Count()))); progressCallback(new ExportImportProgressInfo("currencies loading")); var currencies = _commerceService.GetAllCurrencies().ToArray(); progressCallback(new ExportImportProgressInfo(string.Format("currencies loaded: {0}", currencies.Count()))); progressCallback(new ExportImportProgressInfo("package types loading")); var packageTypes = _commerceService.GetAllPackageTypes().ToArray(); progressCallback(new ExportImportProgressInfo(string.Format("package types loaded: {0}", packageTypes.Count()))); return(new BackupObject() { FulfillmentCenters = fulfillments, Currencies = currencies, PackageTypes = packageTypes }); }
public IHttpActionResult GetFulfillmentCenters() { var retVal = _commerceService.GetAllFulfillmentCenters().ToArray(); return(Ok(retVal)); }
private void SaveProducts(Catalog catalog, List <CsvProduct> csvProducts, ExportImportProgressInfo progressInfo, Action <ExportImportProgressInfo> progressCallback) { progressInfo.ProcessedCount = 0; progressInfo.TotalCount = csvProducts.Count; var defaultFulfilmentCenter = _commerceService.GetAllFulfillmentCenters().FirstOrDefault(); ICollection <Property> modifiedProperties; LoadProductDependencies(csvProducts, catalog, out modifiedProperties); MergeFromAlreadyExistProducts(csvProducts, catalog); var defaultLanguge = catalog.DefaultLanguage != null ? catalog.DefaultLanguage.LanguageCode : "en-US"; foreach (var csvProduct in csvProducts) { //Try to detect and split single property value in to multiple values for multivalue properties csvProduct.PropertyValues = TryToSplitMultivaluePropertyValues(csvProduct); } progressInfo.Description = string.Format("Saving property dictionary values..."); progressCallback(progressInfo); _propertyService.Update(modifiedProperties.ToArray()); var totalProductsCount = csvProducts.Count(); //Order to save main products first then variations csvProducts = csvProducts.OrderBy(x => x.MainProductId != null).ToList(); for (int i = 0; i < totalProductsCount; i += 10) { var products = csvProducts.Skip(i).Take(10); try { //Save main products first and then variations _productService.Update(products.ToArray()); //Set productId for dependent objects foreach (var product in products) { if (defaultFulfilmentCenter != null || product.Inventory.FulfillmentCenterId != null) { product.Inventory.ProductId = product.Id; product.Inventory.FulfillmentCenterId = product.Inventory.FulfillmentCenterId ?? defaultFulfilmentCenter.Id; product.Price.ProductId = product.Id; } else { product.Inventory = null; } } var productIds = products.Select(x => x.Id).ToArray(); var existInventories = _inventoryService.GetProductsInventoryInfos(productIds); var inventories = products.Where(x => x.Inventory != null).Select(x => x.Inventory).ToArray(); foreach (var inventory in inventories) { var exitsInventory = existInventories.FirstOrDefault(x => x.ProductId == inventory.ProductId && x.FulfillmentCenterId == inventory.FulfillmentCenterId); if (exitsInventory != null) { inventory.InjectFrom(exitsInventory); } } _inventoryService.UpsertInventories(inventories); //We do not have information about concrete price list id and therefore select first product price then var existPrices = _pricingSearchService.SearchPrices(new Domain.Pricing.Model.Search.PricesSearchCriteria { ProductIds = productIds }).Results; var prices = products.Where(x => x.Price != null && x.Price.EffectiveValue > 0).Select(x => x.Price).ToArray(); foreach (var price in prices) { var existPrice = existPrices.FirstOrDefault(x => x.Currency.EqualsInvariant(price.Currency) && x.ProductId.EqualsInvariant(price.ProductId)); if (existPrice != null) { price.InjectFrom(existPrice); } } _pricingService.SavePrices(prices); } catch (Exception ex) { lock (_lockObject) { progressInfo.Errors.Add(ex.ToString()); progressCallback(progressInfo); } } finally { lock (_lockObject) { //Raise notification progressInfo.ProcessedCount += products.Count(); progressInfo.Description = string.Format("Saving products: {0} of {1} created", progressInfo.ProcessedCount, progressInfo.TotalCount); progressCallback(progressInfo); } } } }
private void SaveProducts(Catalog catalog, List <CsvProduct> csvProducts, ExportImportProgressInfo progressInfo, Action <ExportImportProgressInfo> progressCallback) { progressInfo.ProcessedCount = 0; progressInfo.TotalCount = csvProducts.Count; var defaultFulfilmentCenter = _commerceService.GetAllFulfillmentCenters().FirstOrDefault(); DetectParents(csvProducts); //Detect already exist product by Code using (var repository = _catalogRepositoryFactory()) { var codes = csvProducts.Where(x => x.IsTransient()).Select(x => x.Code).Where(x => x != null).Distinct().ToArray(); var existProducts = repository.Items.Where(x => x.CatalogId == catalog.Id && codes.Contains(x.Code)).Select(x => new { Id = x.Id, Code = x.Code }).ToArray(); foreach (var existProduct in existProducts) { var product = csvProducts.FirstOrDefault(x => x.Code == existProduct.Code); if (product != null) { product.Id = product.Id; } } } var categoriesIds = csvProducts.Where(x => x.CategoryId != null).Select(x => x.CategoryId).Distinct().ToArray(); var categpories = _categoryService.GetByIds(categoriesIds, CategoryResponseGroup.WithProperties); var defaultLanguge = catalog.DefaultLanguage != null ? catalog.DefaultLanguage.LanguageCode : "EN-US"; var changedProperties = new List <Property>(); foreach (var csvProduct in csvProducts) { csvProduct.CatalogId = catalog.Id; if (string.IsNullOrEmpty(csvProduct.Code)) { csvProduct.Code = _skuGenerator.GenerateSku(csvProduct); } //Set a parent relations if (csvProduct.MainProductId == null && csvProduct.MainProduct != null) { csvProduct.MainProductId = csvProduct.MainProduct.Id; } csvProduct.EditorialReview.LanguageCode = defaultLanguge; csvProduct.SeoInfo.LanguageCode = defaultLanguge; csvProduct.SeoInfo.SemanticUrl = string.IsNullOrEmpty(csvProduct.SeoInfo.SemanticUrl) ? csvProduct.Code : csvProduct.SeoInfo.SemanticUrl; var properties = catalog.Properties; if (csvProduct.CategoryId != null) { var category = categpories.FirstOrDefault(x => x.Id == csvProduct.CategoryId); if (category != null) { properties = category.Properties; } } //Try to fill properties meta information for values foreach (var propertyValue in csvProduct.PropertyValues) { if (propertyValue.Value != null) { var property = properties.FirstOrDefault(x => string.Equals(x.Name, propertyValue.PropertyName)); if (property != null) { propertyValue.ValueType = property.ValueType; if (property.Dictionary) { var dicValue = property.DictionaryValues.FirstOrDefault(x => Equals(x.Value, propertyValue.Value)); if (dicValue == null) { dicValue = new PropertyDictionaryValue { Alias = propertyValue.Value.ToString(), Value = propertyValue.Value.ToString(), Id = Guid.NewGuid().ToString() }; property.DictionaryValues.Add(dicValue); if (!changedProperties.Contains(property)) { changedProperties.Add(property); } } propertyValue.ValueId = dicValue.Id; } } } } } progressInfo.Description = string.Format("Saving property dictionary values..."); progressCallback(progressInfo); _propertyService.Update(changedProperties.ToArray()); var options = new ParallelOptions { MaxDegreeOfParallelism = 10 }; foreach (var group in new[] { csvProducts.Where(x => x.MainProduct == null), csvProducts.Where(x => x.MainProduct != null) }) { var partSize = 25; var partsCount = Math.Max(1, group.Count() / partSize); var parts = group.Select((x, i) => new { Index = i, Value = x }) .GroupBy(x => x.Index % partsCount) .Select(x => x.Select(v => v.Value)); Parallel.ForEach(parts, options, products => { try { //Save main products first and then variations var toUpdateProducts = products.Where(x => !x.IsTransient()); //Need to additional check that product with id exist using (var repository = _catalogRepositoryFactory()) { var updateProductIds = toUpdateProducts.Select(x => x.Id).ToArray(); var existProductIds = repository.Items.Where(x => updateProductIds.Contains(x.Id)).Select(x => x.Id).ToArray(); toUpdateProducts = toUpdateProducts.Where(x => existProductIds.Contains(x.Id)).ToList(); } var toCreateProducts = products.Except(toUpdateProducts); if (!toCreateProducts.IsNullOrEmpty()) { _productService.Create(toCreateProducts.ToArray()); } if (!toUpdateProducts.IsNullOrEmpty()) { _productService.Update(toUpdateProducts.ToArray()); } //Set productId for dependent objects foreach (var product in products) { product.Inventory.ProductId = product.Id; product.Inventory.FulfillmentCenterId = product.Inventory.FulfillmentCenterId ?? defaultFulfilmentCenter.Id; product.Price.ProductId = product.Id; } var inventories = products.Where(x => x.Inventory != null).Select(x => x.Inventory).ToArray(); _inventoryService.UpsertInventories(inventories); var prices = products.Where(x => x.Price != null && x.Price.EffectiveValue > 0).Select(x => x.Price).ToArray(); _pricingService.SavePrices(prices); } catch (Exception ex) { lock (_lockObject) { progressInfo.Errors.Add(ex.ToString()); progressCallback(progressInfo); } } finally { lock (_lockObject) { //Raise notification progressInfo.ProcessedCount += products.Count(); progressInfo.Description = string.Format("Saving products: {0} of {1} created", progressInfo.ProcessedCount, progressInfo.TotalCount); progressCallback(progressInfo); } } }); } }
public IHttpActionResult GetFulfillmentCenters() { var retVal = _commerceService.GetAllFulfillmentCenters().Select(x => x.ToWebModel()).ToArray(); return Ok(retVal); }
public IEnumerable <FulfillmentCenter> GetAllFulfillmentCenters() { return(_commerceService.GetAllFulfillmentCenters()); }