Skip to content

Commit

Permalink
Update shipping and payment fields by ID
Browse files Browse the repository at this point in the history
  • Loading branch information
DWDBE committed Aug 20, 2024
1 parent 7f6d043 commit d626816
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Version>10.6.0</Version>
<Version>10.6.1</Version>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<Title>Order Provider</Title>
<Description>Order Provider</Description>
Expand Down
167 changes: 151 additions & 16 deletions src/OrderProvider.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Dynamicweb.Data;
using Dynamicweb.Core;
using Dynamicweb.Data;
using Dynamicweb.DataIntegration.Integration;
using Dynamicweb.DataIntegration.Integration.Interfaces;
using Dynamicweb.DataIntegration.ProviderHelpers;
Expand All @@ -25,6 +26,9 @@ public class OrderProvider : BaseSqlProvider, IParameterOptions, ISource, IDesti
private Job job = null;
private Schema Schema { get; set; }
private string SqlConnectionString { get; set; }
private string SourceColumnNameForDestinationOrderCustomerAccessUserId = string.Empty;
private ColumnMapping OrderShippingMethodCodeMapping = null;
private ColumnMapping OrderPaymentMethodCodeMapping = null;

[AddInParameter("Export not yet exported Orders"), AddInParameterEditor(typeof(YesNoParameterEditor), ""), AddInParameterGroup("Source")]
public virtual bool ExportNotExportedOrders { get; set; }
Expand Down Expand Up @@ -54,7 +58,6 @@ private SqlConnection Connection
set { connection = value; }
}


public OrderProvider(string connectionString)
{
SqlConnectionString = connectionString;
Expand Down Expand Up @@ -432,8 +435,6 @@ IEnumerable<ParameterOption> IParameterOptions.GetParameterOptions(string parame
return result;
}

private string SourceColumnNameForDestinationOrderCustomerAccessUserId = string.Empty;

private void AddMappingsToJobThatNeedsToBeThereForMoveToMainTables(Job job)
{
Mapping mapping = job.Mappings.Find(m => m.DestinationTable.Name == "EcomOrders");
Expand All @@ -457,6 +458,40 @@ private void AddMappingsToJobThatNeedsToBeThereForMoveToMainTables(Job job)
}
}
}
var ordersTable = job.Destination.GetSchema().GetTables().FirstOrDefault(t => string.Equals(t.Name, "EcomOrders", StringComparison.OrdinalIgnoreCase));
if (ordersTable is not null &&
columnMappings.FirstOrDefault(cm => cm.Active && string.Equals(cm.DestinationColumn.Name, "OrderId", StringComparison.OrdinalIgnoreCase)) is not null)
{
OrderShippingMethodCodeMapping = columnMappings.FirstOrDefault(cm => cm.Active && string.Equals(cm.DestinationColumn.Name, "OrderShippingMethodCode", StringComparison.OrdinalIgnoreCase));
if (OrderShippingMethodCodeMapping is not null)
{
EnsureMapping(columnMappings, mapping, ordersTable.Columns.FirstOrDefault(c => string.Equals(c.Name, "OrderShippingMethodId", StringComparison.OrdinalIgnoreCase)));
EnsureMapping(columnMappings, mapping, ordersTable.Columns.FirstOrDefault(c => string.Equals(c.Name, "OrderShippingMethod", StringComparison.OrdinalIgnoreCase)));
EnsureMapping(columnMappings, mapping, ordersTable.Columns.FirstOrDefault(c => string.Equals(c.Name, "OrderShippingMethodDescription", StringComparison.OrdinalIgnoreCase)));
EnsureMapping(columnMappings, mapping, ordersTable.Columns.FirstOrDefault(c => string.Equals(c.Name, "OrderShippingMethodAgentCode", StringComparison.OrdinalIgnoreCase)));
EnsureMapping(columnMappings, mapping, ordersTable.Columns.FirstOrDefault(c => string.Equals(c.Name, "OrderShippingMethodAgentServiceCode", StringComparison.OrdinalIgnoreCase)));
EnsureMapping(columnMappings, mapping, ordersTable.Columns.FirstOrDefault(c => string.Equals(c.Name, "OrderShippingProviderData", StringComparison.OrdinalIgnoreCase)));
}
OrderPaymentMethodCodeMapping = columnMappings.FirstOrDefault(cm => cm.Active && string.Equals(cm.DestinationColumn.Name, "OrderPaymentMethodCode", StringComparison.OrdinalIgnoreCase));
if (OrderPaymentMethodCodeMapping is not null)
{
EnsureMapping(columnMappings, mapping, ordersTable.Columns.FirstOrDefault(c => string.Equals(c.Name, "OrderPaymentMethodId", StringComparison.OrdinalIgnoreCase)));
EnsureMapping(columnMappings, mapping, ordersTable.Columns.FirstOrDefault(c => string.Equals(c.Name, "OrderPaymentMethod", StringComparison.OrdinalIgnoreCase)));
EnsureMapping(columnMappings, mapping, ordersTable.Columns.FirstOrDefault(c => string.Equals(c.Name, "OrderPaymentMethodDescription", StringComparison.OrdinalIgnoreCase)));
EnsureMapping(columnMappings, mapping, ordersTable.Columns.FirstOrDefault(c => string.Equals(c.Name, "OrderPaymentMethodTermsCode", StringComparison.OrdinalIgnoreCase)));
}
}
}
}

private void EnsureMapping(ColumnMappingCollection columnMappings, Mapping mapping, Column destinationColumn)
{
if (destinationColumn is null || mapping is null || columnMappings is null)
return;
var cm = columnMappings.FirstOrDefault(cm => cm.Active && string.Equals(cm.DestinationColumn.Name, destinationColumn.Name, StringComparison.OrdinalIgnoreCase));
if (cm is null)
{
mapping.AddMapping(destinationColumn, destinationColumn, false);
}
}

Expand Down Expand Up @@ -505,28 +540,39 @@ private Hashtable ExistingUsers

private void ProcessRow(Mapping mapping, ColumnMappingCollection columnMappings, Dictionary<string, object> row)
{
if (mapping != null && mapping.DestinationTable != null && mapping.DestinationTable.Name == "EcomOrders" && !string.IsNullOrEmpty(SourceColumnNameForDestinationOrderCustomerAccessUserId))
if (mapping != null && mapping.DestinationTable != null && mapping.DestinationTable.Name == "EcomOrders")
{
object accessUserId = DBNull.Value;
var OrderCustomerAccessUserExternalIdMapping = columnMappings.Find(cm => string.Compare(cm.DestinationColumn.Name, OrderCustomerAccessUserExternalId, true) == 0);
if (OrderCustomerAccessUserExternalIdMapping != null && OrderCustomerAccessUserExternalIdMapping.SourceColumn != null)
if (!string.IsNullOrEmpty(SourceColumnNameForDestinationOrderCustomerAccessUserId))
{
if (row.ContainsKey(OrderCustomerAccessUserExternalIdMapping.SourceColumn.Name))
object accessUserId = DBNull.Value;
var OrderCustomerAccessUserExternalIdMapping = columnMappings.Find(cm => string.Compare(cm.DestinationColumn.Name, OrderCustomerAccessUserExternalId, true) == 0);
if (OrderCustomerAccessUserExternalIdMapping != null && OrderCustomerAccessUserExternalIdMapping.SourceColumn != null)
{
string externalID = Convert.ToString(row[OrderCustomerAccessUserExternalIdMapping.SourceColumn.Name]);
if (!string.IsNullOrEmpty(externalID) && ExistingUsers.ContainsKey(externalID))
if (row.ContainsKey(OrderCustomerAccessUserExternalIdMapping.SourceColumn.Name))
{
accessUserId = ExistingUsers[externalID];
string externalID = Convert.ToString(row[OrderCustomerAccessUserExternalIdMapping.SourceColumn.Name]);
if (!string.IsNullOrEmpty(externalID) && ExistingUsers.ContainsKey(externalID))
{
accessUserId = ExistingUsers[externalID];
}
}
}
if (!row.ContainsKey(SourceColumnNameForDestinationOrderCustomerAccessUserId))
{
row.Add(SourceColumnNameForDestinationOrderCustomerAccessUserId, accessUserId);
}
else
{
row[SourceColumnNameForDestinationOrderCustomerAccessUserId] = accessUserId;
}
}
if (!row.ContainsKey(SourceColumnNameForDestinationOrderCustomerAccessUserId))
if (OrderShippingMethodCodeMapping is not null)
{
row.Add(SourceColumnNameForDestinationOrderCustomerAccessUserId, accessUserId);
ProcessShipping(mapping, columnMappings, row);
}
else
if (OrderPaymentMethodCodeMapping is not null)
{
row[SourceColumnNameForDestinationOrderCustomerAccessUserId] = accessUserId;
ProcessPayment(mapping, columnMappings, row);
}
}
}
Expand Down Expand Up @@ -555,5 +601,94 @@ private void RemoveMissingRows(IEnumerable<OrderDestinationWriter> writers, SqlT
}
}
}


private void ProcessShipping(Mapping mapping, ColumnMappingCollection columnMappings, Dictionary<string, object> row)
{
var code = GetValue(OrderShippingMethodCodeMapping, row);
if (!string.IsNullOrWhiteSpace(code))
{
var shipping = Ecommerce.Services.Shippings.GetShippings().FirstOrDefault(s => string.Equals(s.Code, code, StringComparison.OrdinalIgnoreCase));
if (shipping is not null)
{
row["OrderShippingMethodId"] = shipping.Id;
row["OrderShippingMethod"] = shipping.GetName(Ecommerce.Common.Context.LanguageID);
row["OrderShippingMethodDescription"] = shipping.GetDescription(Ecommerce.Common.Context.LanguageID);
row["OrderShippingMethodAgentCode"] = shipping.AgentCode;
row["OrderShippingMethodAgentServiceCode"] = shipping.AgentServiceCode;

Dictionary<string, object> shippingProviderValues = null;

var orderIdMapping = columnMappings.FirstOrDefault(cm => cm.Active && string.Equals(cm.DestinationColumn.Name, "OrderId", StringComparison.OrdinalIgnoreCase));
if (orderIdMapping is not null)
{
var orderId = GetValue(orderIdMapping, row);
if (!string.IsNullOrWhiteSpace(orderId))
{
var order = Ecommerce.Services.Orders.GetById(orderId);
if (order is not null)
{
if (string.Equals(order.ShippingMethodId, shipping.Id, StringComparison.OrdinalIgnoreCase))
{
shippingProviderValues = order.ShippingProviderValues;
}
else
{
var orderShipping = Ecommerce.Services.Shippings.GetShipping(order.ShippingMethodId);
if (orderShipping is not null && string.Equals(orderShipping.ServiceSystemName, shipping.ServiceSystemName))
{
shippingProviderValues = order.ShippingProviderValues;
}
}
}
}
}
row["OrderShippingProviderData"] = shippingProviderValues is not null && shippingProviderValues.Count > 0 ? Converter.Serialize(shippingProviderValues) : null;
}
}
}

private void ProcessPayment(Mapping mapping, ColumnMappingCollection columnMappings, Dictionary<string, object> row)
{
var code = GetValue(OrderPaymentMethodCodeMapping, row);
if (!string.IsNullOrWhiteSpace(code))
{
var payment = Ecommerce.Services.Payments.GetPayments().FirstOrDefault(s => string.Equals(s.Code, code, StringComparison.OrdinalIgnoreCase));
if (payment is not null)
{
row["OrderPaymentMethodId"] = payment.Id;
row["OrderPaymentMethod"] = payment.GetName(Ecommerce.Common.Context.LanguageID);
row["OrderPaymentMethodDescription"] = payment.GetDescription(Ecommerce.Common.Context.LanguageID);
row["OrderPaymentMethodTermsCode"] = payment.TermsCode;
}
}
}

private string GetValue(ColumnMapping? columnMapping, Dictionary<string, object> row)

Check warning on line 667 in src/OrderProvider.cs

View workflow job for this annotation

GitHub Actions / call-workflow / Build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 667 in src/OrderProvider.cs

View workflow job for this annotation

GitHub Actions / call-workflow / Build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
{
string result = null;
if (columnMapping != null && (columnMapping.HasScriptWithValue || row.ContainsKey(columnMapping.SourceColumn.Name)))
{
switch (columnMapping.ScriptType)
{
case ScriptType.None:
result = Converter.ToString(row[columnMapping.SourceColumn.Name]);
break;
case ScriptType.Append:
result = Converter.ToString(row[columnMapping.SourceColumn.Name]) + columnMapping.ScriptValue;
break;
case ScriptType.Prepend:
result = columnMapping.ScriptValue + Converter.ToString(row[columnMapping.SourceColumn.Name]);
break;
case ScriptType.Constant:
result = columnMapping.GetScriptValue();
break;
case ScriptType.NewGuid:
result = columnMapping.GetScriptValue();
break;
}
}
return result;
}
}

0 comments on commit d626816

Please sign in to comment.