private async Task <stock_available> GetStockValue(product product, Assort assort) { combination combination = null; if (!string.IsNullOrWhiteSpace(assort.Size) || !string.IsNullOrWhiteSpace(assort.Color) || !string.IsNullOrWhiteSpace(assort.Reference)) { combination = await _combinationProcessor.GetOrCreateCombination(product, assort); } var filter = new Dictionary <string, string> { { "id_product", product.id.Value.ToString(CultureInfo.InvariantCulture) } }; var stocks = await _apiFactory.StockFactory.GetByFilter(filter, null, null); if (stocks == null || !stocks.Any()) { return(await CreateStock(product, assort, combination)); } var stock = combination != null ? stocks.FirstOrDefault(s => s.id_product_attribute == combination.id) : stocks.FirstOrDefault(s => s.id_product == product.id); return(stock ?? await CreateStock(product, assort, combination)); }
private async Task <combination> CheckCombination(combination combination, Assort assort, product product) { if (combination != null && combination.reference != assort.Reference) { combination.reference = assort.Reference; await _apiFactory.CombinationFactory.Update(combination); } if (!product.associations.combinations.Exists(s => s.id == combination.id)) { product = ProductsMapper.MapCombination(product, combination); await _apiFactory.ProductFactory.Update(product); } return(combination); }
private async Task <stock_available> CreateStock(product product, Assort assort, combination combination) { var stock = new stock_available { id_product = product.id, quantity = assort.Balance }; if (combination != null) { stock.id_product_attribute = combination.id; } return((await _apiFactory.StockFactory.AddList(new List <stock_available> { stock })).First()); }
private async Task <combination> CreateCombination(product product, Assort assort, product_option_value sizeOptionValue, product_option_value colorOptionValue, bool isDefault) { var combination = new combination { id_product = product.id, reference = assort.Reference, ean13 = assort.Ean13, associations = new AssociationsCombination(), minimal_quantity = 1, default_on = Convert.ToInt32(isDefault), }; combination.associations.product_option_values = new List <Bukimedia.PrestaSharp.Entities.AuxEntities.product_option_value>(); if (colorOptionValue != null) { combination.associations.product_option_values.Add(new Bukimedia.PrestaSharp.Entities.AuxEntities.product_option_value { id = colorOptionValue.id.Value }); } if (sizeOptionValue != null) { combination.associations.product_option_values.Add(new Bukimedia.PrestaSharp.Entities.AuxEntities.product_option_value { id = sizeOptionValue.id.Value }); } combination = await _apiFactory.CombinationFactory.Add(combination); product = ProductsMapper.MapCombination(product, combination); Log.Information("Combitation created. Size: {0}, Color: {1}. Product reference: {2}, Combination Reference: {3}", sizeOptionValue?.name[0].Value, colorOptionValue?.name[0].Value, product.reference, combination.reference); await _apiFactory.ProductFactory.Update(product); return(combination); }
public async Task <combination> GetOrCreateCombination(product product, Assort assort) { Dictionary <string, string> filter; List <combination> combinations = null; if (!string.IsNullOrWhiteSpace(assort.Reference)) { filter = new Dictionary <string, string> { { "reference", assort.Reference } }; combinations = await _apiFactory.CombinationFactory.GetByFilter(filter, null, null); } if (combinations != null && combinations.Any()) { return(combinations.First()); } product_option_value colorOptionValue = null; if (!string.IsNullOrWhiteSpace(assort.Color)) { colorOptionValue = await GetOptionValue(assort.Color, assort.ColorCode, _apiFactory.ColorOption.id.Value); } product_option_value sizeOptionValue = null; if (!string.IsNullOrWhiteSpace(assort.Size)) { sizeOptionValue = await GetOptionValue(assort.Size, string.Empty, _apiFactory.SizeOption.id.Value); } if (colorOptionValue == null && sizeOptionValue == null) { return(null); } filter = new Dictionary <string, string> { { "id_product", product.id.Value.ToString() } }; combinations = await _apiFactory.CombinationFactory.GetByFilter(filter, null, null); if (combinations == null || !combinations.Any()) { return(await CreateCombination(product, assort, sizeOptionValue, colorOptionValue, true)); } foreach (var combination in combinations) { if (sizeOptionValue != null && colorOptionValue != null) { if (combination.associations.product_option_values.Exists(s => s.id == sizeOptionValue.id.Value) && combination.associations.product_option_values.Exists(s => s.id == colorOptionValue.id.Value)) { return(await CheckCombination(combination, assort, product)); } } else if (colorOptionValue != null) { if (combination.associations.product_option_values.Exists(s => s.id == colorOptionValue.id.Value)) { return(await CheckCombination(combination, assort, product)); } } else { if (combination.associations.product_option_values.Exists(s => s.id == sizeOptionValue.id.Value)) { return(await CheckCombination(combination, assort, product)); } } } return(await CreateCombination(product, assort, sizeOptionValue, colorOptionValue, false)); }
public PriceItem Build(XmlItem fileItem) { var result = new PriceItem { Active = true, Manufacturer = fileItem.Vendor, Name = !string.IsNullOrWhiteSpace(fileItem.Name) ? fileItem.Name.MakeSafeName() : string.Empty, PhotoSmall = fileItem.PictureSmall, Reference = "200" + fileItem.Id.Trim('"', ';'), RetailPrice = float.Parse(fileItem.PriceInfo.RetailPrice.Trim('"', ';').Replace(" ", "").Replace(",", "."), new NumberFormatInfo { NumberDecimalSeparator = "." }), SupplierName = "happiness", SupplierReference = fileItem.VendorCode, WholesalePrice = float.Parse(fileItem.PriceInfo.Wholesale.Replace(" ", "").Replace(",", "."), new NumberFormatInfo { NumberDecimalSeparator = "." }), }; if (!string.IsNullOrWhiteSpace(fileItem.Packing)) { result.Packing = fileItem.Packing.FirstLetterToUpper(); } if (!string.IsNullOrWhiteSpace(fileItem.Length)) { result.Length = fileItem.Length; } if (!string.IsNullOrWhiteSpace(fileItem.Weight)) { result.Weight = fileItem.Weight; } if (!string.IsNullOrWhiteSpace(fileItem.Material)) { result.Material = fileItem.Material.FirstLetterToUpper(); } if (!string.IsNullOrWhiteSpace(fileItem.Batteries)) { result.Battery = fileItem.Batteries.FirstLetterToUpper().CapitalizeEnglish(); } if (!string.IsNullOrWhiteSpace(fileItem.Diameter)) { result.Diameter = fileItem.Diameter; } if (fileItem.Pictures != null) { foreach (var picture in fileItem.Pictures) { result.Photos.Add(picture); } } if (fileItem.Assort != null) { foreach (var assort in fileItem.Assort) { var a = new Assort { Balance = assort.Sklad, Color = !string.IsNullOrWhiteSpace(assort.Color) ? _colorCodeBuilder.GetMainColor(assort.Color.FirstLetterToUpper()) : string.Empty, Size = assort.Size, Ean13 = assort.Barcode, Reference = "200" + assort.Aid, }; a.ColorCode = !string.IsNullOrWhiteSpace(a.Color) ? _colorCodeBuilder.GetCode(a.Color) : string.Empty; if (!string.IsNullOrEmpty(a.Ean13)) { a.Ean13 = _ean13Regex.Match(a.Ean13).Value.Replace(" ", ""); } result.Assort.Add(a); } } if (!string.IsNullOrWhiteSpace(result.Ean13)) { result.Ean13 = _ean13Regex.Match(result.Ean13).Value; } if (fileItem.Categories != null) { result.Categories = new List <CategoryInfo>(); foreach (var category in fileItem.Categories) { if (string.IsNullOrEmpty(category.Name)) { throw new Exception("Category is null"); } result.Categories.Add(new CategoryInfo(category.Name, !string.IsNullOrWhiteSpace(category.SubName) ? category.SubName : category.Name)); } } if (!string.IsNullOrEmpty(result.SupplierReference) && result.SupplierReference.Length > 32) { result.SupplierReference = result.SupplierReference.Substring(0, 32); } if (!string.IsNullOrEmpty(fileItem.Description)) { string shortDescription; var parameters = Helper.ParseDescription(fileItem.Description.Trim('"', ';'), out shortDescription); result.ShortDescription = shortDescription; result.Description = string.Empty; if (shortDescription.Length > 800) { result.Description = shortDescription; result.ShortDescription = shortDescription.TruncateAtWord(796); } if (string.IsNullOrEmpty(result.Length)) { result.Length = parameters.ContainsKey("Длина") ? parameters["Длина"] : string.Empty; } if (string.IsNullOrEmpty(result.Diameter)) { result.Diameter = parameters.ContainsKey("Диаметр") ? parameters["Диаметр"] : string.Empty; } } return(result); }