public bool Publish(out List <string> publishDetails, BackgroundWorker bw = null) { publishDetails = new List <string>(); string apiKey = Settings.GetApiKey(); var updateCounter = 0; if (!string.IsNullOrEmpty(apiKey)) { var pricesToImport = new ProductPriceRepository(Settings.ConnectionString).GetAll().ToList(); var existingInventory = WebServiceHelper.GetAllInventory(); var prices = pricesToImport; var allPrices = new Dictionary <string, List <PricingLevelItemRequest> >(); foreach (var price in prices) { var publishedProduct = existingInventory.FirstOrDefault(s => s.PrivateSKU == price.Id); if (publishedProduct == null) //this price's product doesn't exist on linkgreen so let's ignore it { publishDetails.Add($"SKU {price.Id} did not exist in LinkGreen to be updated with price"); continue; } if (price.MinimumPurchase <= 0) { price.MinimumPurchase = 1; } if (price.Price > 0 && price.Price < publishedProduct.NetPrice) //no point in creating a price level price if it will cost more than net { var item = new PricingLevelItemRequest { SupplierInventoryItemId = publishedProduct.Id, Price = price.Price.Value, MinimumPurchase = price.MinimumPurchase }; updateCounter++; if (allPrices.ContainsKey(price.PriceLevel)) { allPrices[price.PriceLevel].Add(item); } else { allPrices.Add(price.PriceLevel, new List <PricingLevelItemRequest> { item }); } } } foreach (var kvp in allPrices) { bw?.ReportProgress(0, $"Preparing to push {kvp.Key}"); var chunks = kvp.Value.ChunkBy(25); var iCountChunks = 0; Parallel.ForEach(chunks, new ParallelOptions { MaxDegreeOfParallelism = 10 }, (chunk) => { bw?.ReportProgress(0, $"Pushing {kvp.Key} Part {++iCountChunks} of {chunks.Count}"); WebServiceHelper.PushPricingLevel(kvp.Key, chunk.ToArray(), new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day)); }); } publishDetails.Insert(0, $"{updateCounter} products have had their prices updated."); return(true); } Logger.Instance.Warning("No Api Key set while executing pricing publish."); return(false); }
public bool Publish(out List <string> publishDetails, BackgroundWorker bw = null) { publishDetails = new List <string>(); try { // clear out transfer table Empty(); var mappedDsnName = new Mapping().GetDsnName("InventoryQuantities"); var newMapping = new Mapping(mappedDsnName); if (newMapping.MigrateData("InventoryQuantities")) { Logger.Instance.Debug($"Inventory Quantities migrated using DSN: {mappedDsnName}"); var apiKey = Settings.GetApiKey(); if (string.IsNullOrEmpty(apiKey)) { Logger.Instance.Warning("No Api Key set while executing inventory quantities publish."); publishDetails.Insert(0, "No Api Key set while executing inventory quantities publish"); return(false); } Thread.Sleep(500); // TODO: Code Smell - Figure out the real problem here var inventoryQuantityItems = repository.GetAll().ToList(); var existingInventory = WebServiceHelper.GetAllInventory(); var items = 0; var request = new List <IdSkuQuantity>(); foreach (var inventoryQuantityItem in inventoryQuantityItems) { var existingProduct = existingInventory.FirstOrDefault(i => i.PrivateSKU == inventoryQuantityItem.Sku); //var request = new InventoryItemRequest { PrivateSKU = inventoryQuantityItem.Sku }; // ReSharper disable once InvertIf if (existingProduct != null) { items++; request.Add(Mapper.Map <IdSkuQuantity>(inventoryQuantityItem)); } } if (items > 0 && request.All(i => string.IsNullOrEmpty(i.CatalogName))) { var chunks = request.ChunkBy(500); foreach (var chunk in chunks) { WebServiceHelper.PushInventoryQuantity(chunk); } } else if (items > 0) { foreach (var item in request) { WebServiceHelper.UpdateInventoryItemQuantity(item.SKU, item.Quantity, item.CatalogName); } } if (items < 1) { Logger.Instance.Warning("No inventory quantity items were published. Double check your skus."); publishDetails.Insert(0, "No inventory quantity items were published. Double check your skus"); return(false); } publishDetails.Insert(0, $"{items} inventory quantity items were published."); return(true); } } catch (System.Exception ex) { Logger.Instance.Warning("Failed to migrate Inventory Quantities."); publishDetails.Insert(0, "Failed to migrate Inventory Quantities"); Logger.Instance.Error("Inventory Quanity Error" + ex.Message); Logger.Instance.Error(ex.StackTrace); return(false); } Logger.Instance.Warning("Failed to migrate Inventory Quantities Non-Error."); publishDetails.Insert(0, "Failed to migrate Inventory Quantities Non-Error"); return(false); }
public bool Publish(out List <string> publishDetails, BackgroundWorker bw = null) { publishDetails = new List <string>(); Empty(); var mappedDsnName = new Mapping().GetDsnName("BuyerInventories"); var newMapping = new Mapping(mappedDsnName); if (newMapping.MigrateData("BuyerInventories")) { Logger.Instance.Debug($"Buyer Inventory migrated using DSN: {mappedDsnName}"); } else { Logger.Instance.Warning("Failed to migrate Buyer Inventory."); } var apiKey = Settings.GetApiKey(); if (string.IsNullOrEmpty(apiKey)) { Logger.Instance.Warning("No Api Key set while executing products publish."); return(false); } var products = _repository.GetAll().ToList(); var existingCategories = WebServiceHelper.GetAllCategories(); // TODO: There's no GetAll() for the BuyerInventory service var existingInventory = WebServiceHelper.GetAllInventory(); var items = 0; foreach (var product in products) { var existingCategory = existingCategories.FirstOrDefault(c => c.Name == product.Category); if (existingCategory == null) { existingCategory = WebServiceHelper.PushCategory(new PrivateCategory { Name = product.Category }); existingCategories.Add(existingCategory); } var existingProduct = existingInventory.FirstOrDefault(i => i.PrivateSKU == product.PrivateSku); dynamic request = new ExpandoObject(); // NOTE: This is case-sensitive!? request.PrivateSKU = product.PrivateSku; request.CategoryId = (existingCategory?.Id).GetValueOrDefault(); request.Description = product.Description ?? string.Empty; if (existingProduct != null) { request.Id = existingProduct.Id; } if (product.Inactive.HasValue) { request.Inactive = product.Inactive.Value; } //TODO: Get the location first if (product.LocationId.HasValue) { request.LocationId = product.LocationId.Value; } if (!string.IsNullOrEmpty(product.UPC)) { request.UPC = product.UPC; } if (product.MinOrderSpring.HasValue) { request.MinOrderSpring = product.MinOrderSpring.Value; } if (product.MinOrderSummer.HasValue) { request.MinOrderSummer = product.MinOrderSummer.Value; } if (product.FreightFactor.HasValue) { request.FreightFactor = product.FreightFactor.Value; } if (product.QuantityAvailable.HasValue) { request.QuantityAvailable = product.QuantityAvailable >= 1 ? product.QuantityAvailable : 1; } if (!string.IsNullOrEmpty(product.Comments)) { request.Comments = product.Comments; } if (product.SuggestedRetailPrice.HasValue) { request.SuggestedRetailPrice = product.SuggestedRetailPrice.Value; } if (!string.IsNullOrEmpty(product.OpenSizeDescription)) { request.OpenSizeDescription = product.OpenSizeDescription; } if (product.NetPrice.HasValue) { request.NetPrice = product.NetPrice; } if (product.SlaveQuantityPerMaster.HasValue) { request.SlaveQuantityPerMaster = product.SlaveQuantityPerMaster; } if (!string.IsNullOrEmpty(product.SlaveQuantityDescription)) { request.SlaveQuantityDescription = product.SlaveQuantityDescription; } if (!string.IsNullOrEmpty(product.MasterQuantityDescription)) { request.MasterQuantityDescription = product.MasterQuantityDescription; } if (product.RetailPrice.HasValue) { request.RetailPrice = product.RetailPrice; } if (product.RetailOrderLevel.HasValue) { request.RetailOrderLevel = product.RetailOrderLevel; } if (product.AmazonSell.HasValue) { request.AmazonSell = product.AmazonSell; } if (product.OnlineSell.HasValue) { request.OnlineSell = product.OnlineSell; } if (product.SupplierId.HasValue) { request.SupplierId = product.SupplierId; } if (!string.IsNullOrEmpty(product.SupplierSku)) { request.SupplierSku = product.SupplierSku; } WebServiceHelper.PushBuyerInventory(request); Logger.Instance.Debug($"Finished importing inventory {++items} of {products.Count}. Id: {product.Id}"); } publishDetails.Insert(0, $"{items} inventory items have been pushed to LinkGreen"); return(true); }
public bool Publish(out List <string> publishDetails, BackgroundWorker bw = null) { publishDetails = new List <string>(); string apiKey = Settings.GetApiKey(); if (!string.IsNullOrEmpty(apiKey)) { var products = new ProductInventoryRepository(Settings.ConnectionString).GetAll().ToList(); var existingCategories = WebServiceHelper.GetAllCategories(); var existingInventory = WebServiceHelper.GetAllInventory(); var items = 0; var bulkPushRequest = new List <InventoryItemRequest>(); var productsToAdd = products.Where(p => existingInventory.All(ei => ei.PrivateSKU != p.Id)); // Add new items foreach (var product in productsToAdd) { try { var request = AddOrUpdateSupplierItem(product, existingInventory, ref existingCategories); WebServiceHelper.PushInventoryItem(request, out var statusCode, out var content); bw?.ReportProgress(0, $"Processing product sync (Pushing {++items}/{products.Count})\n\rPlease wait"); Logger.Instance.Debug( $"Finished importing product {items} of {products.Count}. Id: {product.Id}"); Logger.Instance.Debug($"Adding response {product.Id} {statusCode}"); Logger.Instance.Debug($"Adding response {product.Id} {content}"); } catch (Exception ex) { Logger.Instance.Error("Adding " + JsonConvert.SerializeObject(product) + Environment.NewLine + ex.Message + Environment.NewLine + ex.StackTrace); } } // Update existing items foreach (var product in products.Where(p => existingInventory.Any(ei => ei.PrivateSKU == p.Id))) { try { var request = AddOrUpdateSupplierItem(product, existingInventory, ref existingCategories); if (request != null) { bulkPushRequest.Add(request); } if (bulkPushRequest.Count > 10) { WebServiceHelper.PushBulkUpdateInventoryItem(bulkPushRequest.ToArray(), out var statusCode, out var content); Logger.Instance.Debug($"Bulk Push: Response: {statusCode}"); Logger.Instance.Debug($"Bulk Push: Response Content: {content}"); bulkPushRequest.Clear(); } bw?.ReportProgress(0, $"Processing product sync (Pushing {++items}/{products.Count})\n\rPlease wait"); Logger.Instance.Debug( $"Finished importing product {items} of {products.Count}. Id: {product.Id}"); } catch (Exception ex) { Logger.Instance.Error("Updating " + JsonConvert.SerializeObject(product) + Environment.NewLine + ex.Message + Environment.NewLine + ex.StackTrace); } } if (bulkPushRequest.Count > 0) { WebServiceHelper.PushBulkUpdateInventoryItem(bulkPushRequest.ToArray(), out var statusCode, out var content); Logger.Instance.Debug($"Bulk Push Response {statusCode}"); Logger.Instance.Debug($"Bulk Push Response {content}"); } WebServiceHelper.PostInventoryImport(); publishDetails.Insert(0, $"{items} products published to LinkGreen"); return(true); } publishDetails.Insert(0, "No Api Key set while executing products publish."); Logger.Instance.Warning("No Api Key set while executing products publish."); return(false); }
public bool Publish(out List <string> publishDetails, BackgroundWorker bw = null) { publishDetails = new List <string>(); string apiKey = Settings.GetApiKey(); if (!string.IsNullOrEmpty(apiKey)) { var products = new ProductInventoryRepository(Settings.ConnectionString).GetAll().ToList(); var existingCategories = WebServiceHelper.GetAllCategories(); var existingInventory = WebServiceHelper.GetAllInventory(); var items = 0; var bulkPushRequest = new List <InventoryItemRequest>(); var productsToAdd = products.Where(p => existingInventory.All(ei => ei.PrivateSKU != p.Id)); // Add new items foreach (var product in productsToAdd) { var request = AddOrUpdateSupplierItem(product, existingInventory, ref existingCategories); WebServiceHelper.PushInventoryItem(request); bw?.ReportProgress(0, $"Processing product sync (Pushing {++items}/{products.Count})\n\rPlease wait"); Logger.Instance.Debug($"Finished importing product {items} of {products.Count}. Id: {product.Id}"); } // Update existing items foreach (var product in products.Where(p => existingInventory.Any(ei => ei.PrivateSKU == p.Id))) { var request = AddOrUpdateSupplierItem(product, existingInventory, ref existingCategories); if (request != null) { bulkPushRequest.Add(request); } if (bulkPushRequest.Count > 10) { WebServiceHelper.PushBulkUpdateInventoryItem(bulkPushRequest.ToArray()); bulkPushRequest.Clear(); } bw?.ReportProgress(0, $"Processing product sync (Pushing {++items}/{products.Count})\n\rPlease wait"); Logger.Instance.Debug($"Finished importing product {items} of {products.Count}. Id: {product.Id}"); } if (bulkPushRequest.Count > 0) { WebServiceHelper.PushBulkUpdateInventoryItem(bulkPushRequest.ToArray()); } WebServiceHelper.PostInventoryImport(); publishDetails.Insert(0, $"{items} products published to LinkGreen"); return(true); } publishDetails.Insert(0, "No Api Key set while executing products publish."); Logger.Instance.Warning("No Api Key set while executing products publish."); return(false); }