Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added logid for EcomStockUnit, so it can now set the StockUnitStockLo… #20

Merged
merged 2 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Version>10.0.11</Version>
<Version>10.0.12</Version>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<Title>Ecom Provider</Title>
<Description>Ecom Provider</Description>
Expand Down
88 changes: 87 additions & 1 deletion src/EcomDestinationWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1045,13 +1046,24 @@
{
if ((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))
MatthiasSort marked this conversation as resolved.
Show resolved Hide resolved
{
var stockLocationID = GetStockLocationIdByName(row, columnMapping);
dataRow[columnMapping.DestinationColumn.Name] = stockLocationID;
MatthiasSort marked this conversation as resolved.
Show resolved Hide resolved
row[columnMapping.SourceColumn.Name] = stockLocationID;
}

if (mappingColumns.Any(obj => obj.DestinationColumn.Name == columnMapping.DestinationColumn.Name && obj.GetId() != columnMapping.GetId()))
MatthiasSort marked this conversation as resolved.
Show resolved Hide resolved
{
dataRow[columnMapping.DestinationColumn.Name] += columnMapping.ConvertInputToOutputFormat(row[columnMapping.SourceColumn.Name]) + "";

Check warning on line 1058 in src/EcomDestinationWriter.cs

View workflow job for this annotation

GitHub Actions / call-workflow / Build

'ColumnMapping.ConvertInputToOutputFormat(object)' is obsolete: 'Use ConvertInputValueToOutputValue instead.'

Check warning on line 1058 in src/EcomDestinationWriter.cs

View workflow job for this annotation

GitHub Actions / call-workflow / Build

'ColumnMapping.ConvertInputToOutputFormat(object)' is obsolete: 'Use ConvertInputValueToOutputValue instead.'
}
else if (columnMapping.HasScriptWithValue)
{
dataRow[columnMapping.DestinationColumn.Name] = columnMapping.GetScriptValue();
}
else
{
dataRow[columnMapping.DestinationColumn.Name] = columnMapping.ConvertInputToOutputFormat(row[columnMapping.SourceColumn.Name]);

Check warning on line 1066 in src/EcomDestinationWriter.cs

View workflow job for this annotation

GitHub Actions / call-workflow / Build

'ColumnMapping.ConvertInputToOutputFormat(object)' is obsolete: 'Use ConvertInputValueToOutputValue instead.'

Check warning on line 1066 in src/EcomDestinationWriter.cs

View workflow job for this annotation

GitHub Actions / call-workflow / Build

'ColumnMapping.ConvertInputToOutputFormat(object)' is obsolete: 'Use ConvertInputValueToOutputValue instead.'
}
}
else
Expand Down Expand Up @@ -1103,6 +1115,9 @@
return;
}
break;
case "EcomStockUnit":
WriteStockUnits(row, columnMappings, dataRow);
break;
}

foreach (ColumnMapping columnMapping in mappingColumns)
Expand Down Expand Up @@ -1837,6 +1852,48 @@
return groupID;
}

private void WriteStockUnits(Dictionary<string, object> row, Dictionary<string, ColumnMapping> 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<string, object> row, ColumnMapping stockLocationIdColumn)
{
StockLocation existingStockLocation = GetExistingStockLocation(row, stockLocationIdColumn);
if (existingStockLocation != null)
{
return existingStockLocation.ID;
}
return 0;
}

private void WriteManufacturers(Dictionary<string, object> row, Dictionary<string, ColumnMapping> columnMappings, DataRow dataRow)
{
ColumnMapping manufacturerNameColumn = null;
Expand Down Expand Up @@ -3607,6 +3664,35 @@
return result;
}

private StockLocation GetExistingStockLocation(Dictionary<string, object> 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<string, object> row, ColumnMapping manufacturerNameColumn)
{
Expand All @@ -3629,7 +3715,7 @@
return result;
}

internal void Close()

Check warning on line 3718 in src/EcomDestinationWriter.cs

View workflow job for this annotation

GitHub Actions / call-workflow / Build

'EcomDestinationWriter.Close()' hides inherited member 'BaseDestinationWriter.Close()'. Use the new keyword if hiding was intended.

Check warning on line 3718 in src/EcomDestinationWriter.cs

View workflow job for this annotation

GitHub Actions / call-workflow / Build

'EcomDestinationWriter.Close()' hides inherited member 'BaseDestinationWriter.Close()'. Use the new keyword if hiding was intended.
{
//Reset Language cache
if (FindDataTablesStartingWithName("EcomLanguages").Count() > 0)
Expand Down
Loading