diff --git a/src/Dynamicweb.DataIntegration.Providers.EcomProvider.csproj b/src/Dynamicweb.DataIntegration.Providers.EcomProvider.csproj index 36e7b6a..24fb066 100644 --- a/src/Dynamicweb.DataIntegration.Providers.EcomProvider.csproj +++ b/src/Dynamicweb.DataIntegration.Providers.EcomProvider.csproj @@ -1,6 +1,6 @@  - 10.0.11 + 10.0.12 1.0.0.0 Ecom Provider Ecom Provider diff --git a/src/EcomDestinationWriter.cs b/src/EcomDestinationWriter.cs index ccb97fc..c946662 100644 --- a/src/EcomDestinationWriter.cs +++ b/src/EcomDestinationWriter.cs @@ -3,6 +3,7 @@ using Dynamicweb.Data; using Dynamicweb.DataIntegration.Integration; using Dynamicweb.DataIntegration.ProviderHelpers; +using Dynamicweb.Ecommerce.Stocks; using Dynamicweb.Logging; using System; using System.Collections; @@ -1043,9 +1044,20 @@ public void Write(Dictionary row, Mapping mapping, bool discardD var mappingColumns = ColumnMappingsByMappingId[mapping.GetId()]; foreach (ColumnMapping columnMapping in mappingColumns) { - if ((columnMapping.SourceColumn != null && row.ContainsKey(columnMapping.SourceColumn.Name)) || columnMapping.HasScriptWithValue) + if ((columnMapping.DestinationColumn != null && columnMapping.SourceColumn != null && row.ContainsKey(columnMapping.SourceColumn.Name)) || columnMapping.HasScriptWithValue) { - if (columnMapping.HasScriptWithValue) + if (mapping.DestinationTable.Name.Equals("EcomStockUnit", StringComparison.OrdinalIgnoreCase) && columnMapping.DestinationColumn.Name.Equals("StockUnitStockLocationId", StringComparison.OrdinalIgnoreCase)) + { + var stockLocationID = GetStockLocationIdByName(row, columnMapping); + dataRow[columnMapping.DestinationColumn.Name] = stockLocationID; + row[columnMapping.SourceColumn.Name] = stockLocationID; + } + + if (mappingColumns.Any(obj => obj.DestinationColumn.Name == columnMapping.DestinationColumn.Name && obj.GetId() != columnMapping.GetId())) + { + dataRow[columnMapping.DestinationColumn.Name] += columnMapping.ConvertInputToOutputFormat(row[columnMapping.SourceColumn.Name]) + ""; + } + else if (columnMapping.HasScriptWithValue) { dataRow[columnMapping.DestinationColumn.Name] = columnMapping.GetScriptValue(); } @@ -1058,6 +1070,14 @@ public void Write(Dictionary row, Mapping mapping, bool discardD { if (columnMapping.Active) { + if (columnMapping.DestinationColumn == null) + { + logger.Error($"The DestinationColumn is null for the table mapping {mapping.SourceTable?.Name} to {mapping.DestinationTable?.Name} on SourceColumn {columnMapping.SourceColumn?.Name}"); + } + if (columnMapping.SourceColumn == null && !columnMapping.HasScriptWithValue) + { + logger.Error($"The SourceColumn is null for the table mapping {mapping.SourceTable?.Name} to {mapping.DestinationTable?.Name} on DestinationColumn {columnMapping.DestinationColumn?.Name}"); + } throw new Exception(BaseDestinationWriter.GetRowValueNotFoundMessage(row, columnMapping.SourceColumn.Table.Name, columnMapping.SourceColumn.Name)); } } @@ -1103,6 +1123,9 @@ public void Write(Dictionary row, Mapping mapping, bool discardD return; } break; + case "EcomStockUnit": + WriteStockUnits(row, columnMappings, dataRow); + break; } foreach (ColumnMapping columnMapping in mappingColumns) @@ -1837,6 +1860,48 @@ private string HandleGroupId(Dictionary row, Dictionary row, Dictionary columnMappings, DataRow dataRow) + { + if (!columnMappings.TryGetValue("StockUnitId", out _)) + { + if (columnMappings.TryGetValue("StockUnitProductID", out var stockUnitProductIDColumn) && columnMappings.TryGetValue("StockUnitVariantID", out var stockUnitVariantIDColumn)) + { + var productID = row[stockUnitProductIDColumn.SourceColumn.Name].ToString(); + var variantID = row[stockUnitVariantIDColumn.SourceColumn.Name].ToString(); + if (productID.Equals(variantID, StringComparison.OrdinalIgnoreCase)) + { + variantID = string.Empty; + } + + var productBaseUnitOfMeasure = GetProductDefaultUnitId(productID, variantID); + if (!string.IsNullOrEmpty(productBaseUnitOfMeasure)) + { + dataRow["StockUnitId"] = productBaseUnitOfMeasure; + } + } + } + } + + private string GetProductDefaultUnitId(string productID, string variantID) + { + var product = Ecommerce.Services.Products.GetProductById(productID, variantID, true); + if (product == null) + { + logger.Warn($"Could not find product with productid: {productID} and variantid:{variantID} on the default language"); + } + return product.DefaultUnitId; + } + + private long GetStockLocationIdByName(Dictionary row, ColumnMapping stockLocationIdColumn) + { + StockLocation existingStockLocation = GetExistingStockLocation(row, stockLocationIdColumn); + if (existingStockLocation != null) + { + return existingStockLocation.ID; + } + return 0; + } + private void WriteManufacturers(Dictionary row, Dictionary columnMappings, DataRow dataRow) { ColumnMapping manufacturerNameColumn = null; @@ -3607,6 +3672,35 @@ private DataRow GetExistingProductDataRow(string searchString) return result; } + private StockLocation GetExistingStockLocation(Dictionary row, ColumnMapping stockLocationIdColumn) + { + StockLocation result = null; + if (stockLocationIdColumn != null && !string.IsNullOrEmpty(stockLocationIdColumn.SourceColumn.Name)) + { + var stockLocationId = row[stockLocationIdColumn.SourceColumn.Name]?.ToString() ?? string.Empty; + if (!string.IsNullOrEmpty(stockLocationId)) + { + if (long.TryParse(stockLocationId, out var stockLocationIdAsLong)) + { + result = Ecommerce.Services.StockService.GetStockLocation(stockLocationIdAsLong); + } + + if (result == null) + { + var defaultLanguageId = Ecommerce.Services.Languages.GetDefaultLanguageId(); + foreach (var location in Ecommerce.Services.StockService.GetStockLocations()) + { + if (location.GetName(defaultLanguageId).Equals(stockLocationId, StringComparison.OrdinalIgnoreCase)) + { + return location; + } + } + } + } + } + return result; + } + //Returns existing ManufacturerID if found Manufacturer by ManufacturerName. Returns null if no manufacturer found. private DataRow GetExistingManufacturer(Dictionary row, ColumnMapping manufacturerNameColumn) {