public IActionResult ExportProducts(string selectedIds) { //ensure that Avalara tax provider is active if (!_taxPluginManager.IsPluginActive(AvalaraTaxDefaults.SystemName)) { return(RedirectToAction("List", "Product")); } if (!_permissionService.Authorize(StandardPermissionProvider.ManageTaxSettings)) { return(AccessDeniedView()); } //prepare exported items var productIds = selectedIds?.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(id => Convert.ToInt32(id)).ToArray(); var exportedItems = new List <ItemModel>(); foreach (var product in _productService.GetProductsByIds(productIds)) { //find product combinations var combinations = _productAttributeService.GetAllProductAttributeCombinations(product.Id) .Where(combination => !string.IsNullOrEmpty(combination.Sku)); //export items with specified SKU only if (string.IsNullOrEmpty(product.Sku) && !combinations.Any()) { continue; } //prepare common properties var taxCategory = _taxCategoryService.GetTaxCategoryById(product.TaxCategoryId); var taxCode = CommonHelper.EnsureMaximumLength(taxCategory?.Name, 25); var description = CommonHelper.EnsureMaximumLength(product.ShortDescription ?? product.Name, 255); //add the product as exported item if (!string.IsNullOrEmpty(product.Sku)) { exportedItems.Add(new ItemModel { createdDate = DateTime.UtcNow, description = description, itemCode = CommonHelper.EnsureMaximumLength(product.Sku, 50), taxCode = taxCode }); } //add product combinations exportedItems.AddRange(combinations.Select(combination => new ItemModel { createdDate = DateTime.UtcNow, description = description, itemCode = CommonHelper.EnsureMaximumLength(combination.Sku, 50), taxCode = taxCode })); } //get existing items var existingItemCodes = _avalaraTaxManager.GetItems()?.Select(item => item.itemCode).ToList() ?? new List <string>(); //remove duplicates exportedItems = exportedItems.Where(item => !existingItemCodes.Contains(item.itemCode)).Distinct().ToList(); //export items if (exportedItems.Any()) { //create items and get the result var result = _avalaraTaxManager.CreateItems(exportedItems)?.Count; //display results if (result.HasValue && result > 0) { _notificationService.SuccessNotification(string.Format(_localizationService.GetResource("Plugins.Tax.Avalara.Items.Export.Success"), result)); } else { _notificationService.ErrorNotification(_localizationService.GetResource("PPlugins.Tax.Avalara.Items.Export.Error")); } } else { _notificationService.SuccessNotification(_localizationService.GetResource("Plugins.Tax.Avalara.Items.Export.AlreadyExported")); } return(RedirectToAction("List", "Product")); }