Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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.4.22</Version>
<Version>10.4.23</Version>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<Title>Live Integration</Title>
<Description>Live Integration</Description>
Expand All @@ -24,8 +24,10 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Dynamicweb.Core" Version="10.4.0" />
<PackageReference Include="Dynamicweb.CoreUI" Version="10.4.0" />
<PackageReference Include="Dynamicweb.DataIntegration" Version="10.4.0" />
<PackageReference Include="Dynamicweb.Ecommerce" Version="10.4.0" />
<PackageReference Include="Dynamicweb.Ecommerce.UI" Version="10.4.0" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,7 @@ internal static void ValidateLicense(string endpoint, string response, Logger lo
var status = GetStatus(endpoint);
if (status == null || status.Expired)
{
if (!Helpers.ParseResponseToXml(response, logger, out XmlDocument doc))
{
logger.Log(ErrorLevel.DebugInfo, $"License Response is not valid XML");
}
else
if (Helpers.ParseResponseToXml(response, logger, out XmlDocument doc))
{
ValidateLicense(doc, endpoint, status, logger);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ private static ResponseCacheLevel GetOrderCacheLevel(Settings settings)
string lastHash = GetLastOrderHash(settings);

if (liveIntegrationSubmitType != SubmitType.ScheduledTask && liveIntegrationSubmitType != SubmitType.CaptureTask &&
liveIntegrationSubmitType != SubmitType.ManualSubmit &&
!string.IsNullOrEmpty(lastHash) && lastHash == currentHash)
{
// no changes to order
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
using Dynamicweb.CoreUI.Data;
using Dynamicweb.CoreUI.Data.Validation;
using Dynamicweb.Ecommerce.DynamicwebLiveIntegration.Configuration;
using Dynamicweb.Ecommerce.DynamicwebLiveIntegration.Logging;
using Dynamicweb.Ecommerce.DynamicwebLiveIntegration.XmlGenerators;
using Dynamicweb.Ecommerce.Orders;
using System.IO;

namespace Dynamicweb.Ecommerce.DynamicwebLiveIntegration.UI.Commands;

public sealed class DownloadOrderXmlCommand : CommandBase
{
[Required]
public string OrderId { get; set; }

[Required]
public bool GetOriginalXml { get; set; }

public override CommandResult Handle()
{
var order = Services.Orders.GetById(OrderId);
if (order is null)
{
return new CommandResult
{
Message = $"The order with id: '{OrderId}' was not found",
Status = CommandResult.ResultType.NotFound
};
}

Settings settings = SettingsManager.GetSettingsByShop(order.ShopId);
if (settings is null && !GetOriginalXml)
{
return new CommandResult
{
Message = $"No active Dynamicweb Live integration instance found for Order id: {order.Id} and Shop Id: {order.ShopId}.",
Status = CommandResult.ResultType.NotFound
};
}

var fileName = $"Order_{order.Id}.xml";
var xml = GetOriginalXml ? GetOrderOriginalXml(order) : GetOrderCurrentXml(settings, order);

var stream = new MemoryStream();
using (var writeFile = new StreamWriter(stream, leaveOpen: true))
{
writeFile.Write(xml);
}
stream.Position = 0;

return new CommandResult
{
Status = CommandResult.ResultType.Ok,
Model = new FileResult
{
FileStream = stream,
ContentType = "application/octet-stream",
FileDownloadName = fileName
}
};
}

/// <summary>
/// Occurs when the button was clicked from edit order page. Gets the XML for the order and returns it to the browser.
/// </summary>
private static string GetOrderCurrentXml(Settings settings, Order order)
{
var logger = new Logger(settings);
var xmlGeneratorSettings = new OrderXmlGeneratorSettings
{
AddOrderLineFieldsToRequest = settings.AddOrderLineFieldsToRequest,
AddOrderFieldsToRequest = settings.AddOrderFieldsToRequest,
CreateOrder = true,
Beautify = true,
LiveIntegrationSubmitType = SubmitType.DownloadedFromBackEnd,
ReferenceName = "OrdersPut",
ErpControlsDiscount = settings.ErpControlsDiscount,
ErpControlsShipping = settings.ErpControlsShipping,
ErpShippingItemKey = settings.ErpShippingItemKey,
ErpShippingItemType = settings.ErpShippingItemType,
CalculateOrderUsingProductNumber = settings.CalculateOrderUsingProductNumber
};
return new OrderXmlGenerator().GenerateOrderXml(settings, order, xmlGeneratorSettings, logger);
}

/// <summary>
/// Gets the original XML by reading the original file from disk.
/// </summary>
private static string GetOrderOriginalXml(Order order) => File.ReadAllText(BuildXmlFileName(order));

internal static string BuildXmlFileName(Order order)
{
return OrderHandler.BuildXmlCopyPath(order.Id, OrderHandler.GetLogFolderForXmlCopies(order.CompletedDate));
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using Dynamicweb.CoreUI.Data;
using Dynamicweb.CoreUI.Data.Validation;
using Dynamicweb.Ecommerce.DynamicwebLiveIntegration.Configuration;

namespace Dynamicweb.Ecommerce.DynamicwebLiveIntegration.UI.Commands;

public sealed class TransferOrderToErpCommand : CommandBase
{
[Required]
public string OrderId { get; set; }

public override CommandResult Handle()
{
var order = Services.Orders.GetById(OrderId);
if (order is null)
{
return new CommandResult
{
Message = $"The order with id: '{OrderId}' was not found",
Status = CommandResult.ResultType.NotFound
};
}

Settings settings = SettingsManager.GetSettingsByShop(order.ShopId);
if (settings is null)
{
return new CommandResult
{
Message = $"No active Dynamicweb Live integration instance found for Order id: {order.Id} and Shop Id: {order.ShopId}.",
Status = CommandResult.ResultType.NotFound
};
}

bool result = OrderHandler.UpdateOrder(settings, order, SubmitType.ManualSubmit) ?? false;
return new CommandResult
{
Message = result ? "Order successfully transferred to ERP" : "Error creating order in ERP. Check the LiveIntegration log for details",
Status = result ? CommandResult.ResultType.Ok : CommandResult.ResultType.Error
};
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
using Dynamicweb.CoreUI.Data;
using Dynamicweb.Ecommerce.DynamicwebLiveIntegration.Configuration;
using Dynamicweb.Ecommerce.Orders;
using Dynamicweb.Ecommerce.UI.Commands;
using System.Collections.Generic;
using System.Linq;

namespace Dynamicweb.Ecommerce.DynamicwebLiveIntegration.UI.Commands;

public sealed class TransferOrdersToErpCommand : OrderBulkActionCommand
{
public override CommandResult Handle()
{
var orders = GetSelectedOrders().ToList();
if (orders.Count == 0)
{
return new()
{
Status = CommandResult.ResultType.NotFound,
Message = "The selected Orders could not be found"
};
}

List<string> exportedOrders = new List<string>();
List<string> alreadyExportedOrders = new List<string>();

foreach (var order in orders)
{
if (string.IsNullOrEmpty(order.IntegrationOrderId))
{
var settings = SettingsManager.GetSettingsByShop(order.ShopId);
if (settings is null)
continue;

bool exported = OrderHandler.UpdateOrder(settings, order, SubmitType.ManualSubmit) ?? false;
if (exported)
{
exportedOrders.Add(order.Id);
}
}
else
{
alreadyExportedOrders.Add(order.Id);
}
}

var message = GetExportedOrdersMessage(orders, exportedOrders, alreadyExportedOrders, out var success);

return new()
{
Status = success ? CommandResult.ResultType.Ok : CommandResult.ResultType.Error,
Message = message
};
}

/// <summary>
/// Gets the exported orders message.
/// </summary>
/// <returns>System.String.</returns>
private static string GetExportedOrdersMessage(List<Order> orders, List<string> exportedOrders, List<string> alreadyExportedOrders, out bool success)
{
string output = string.Empty;
success = false;

if (alreadyExportedOrders.Count > 0 && alreadyExportedOrders.Count == orders.Count())
{
output = "All selected orders are already transferred to ERP.";
success = true;
}
else if (exportedOrders.Count > 0 || alreadyExportedOrders.Count > 0)
{
if ((exportedOrders.Count + alreadyExportedOrders.Count) == orders.Count())
{
output = "All selected orders were successfully transferred to ERP.";
success = true;
}
else
{
if (alreadyExportedOrders.Count > 0)
{
output += $"Orders with IDs [{string.Join(",", alreadyExportedOrders)}] were already transferred to ERP. ";
}

if (exportedOrders.Count > 0)
{
output += $"Orders with IDs [{string.Join(",", exportedOrders)}] were successfully transferred to ERP. ";
}

output += $"Orders with IDs [{string.Join(",", orders
.Where(o => !(exportedOrders.Contains(o.Id) ||
alreadyExportedOrders.Contains(o.Id)))
.Select(o => o.Id).Distinct().ToArray())}] were not transferred to ERP. Check the LiveIntegration log for details";
}
}
else
{
output = "None of the selected orders were transferred to ERP. Check the LiveIntegration log for details.";
}

return output;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
using Dynamicweb.CoreUI.Actions;
using Dynamicweb.CoreUI.Actions.Implementations;
using Dynamicweb.CoreUI.Icons;
using Dynamicweb.CoreUI.Screens;
using Dynamicweb.Ecommerce.DynamicwebLiveIntegration.Configuration;
using Dynamicweb.Ecommerce.DynamicwebLiveIntegration.UI.Commands;
using Dynamicweb.Ecommerce.Orders;
using Dynamicweb.Ecommerce.UI.Models;
using Dynamicweb.Ecommerce.UI.Screens;
using System.Collections.Generic;

namespace Dynamicweb.Ecommerce.DynamicwebLiveIntegration.UI.Injectors
{
public sealed class OrderEditScreenInjector : EditScreenInjector<OrderEditScreen, OrderDataModel>
{
internal static readonly string LiveIntegrationTab = "Live Integration";

public override IEnumerable<ActionGroup> GetScreenActions()
{
return GetOrderScreenActions(Screen?.Model?.Id, Screen?.Model?.IntegrationOrderId);
}

internal static List<ActionGroup> GetOrderScreenActions(string orderId, string integrationOrderId)
{
var order = Services.Orders.GetById(orderId);
if (order is null)
return [];

var settings = SettingsManager.GetSettingsByShop(order.ShopId);
bool isLiveIntegrationFound = settings is not null;

if (isLiveIntegrationFound)
{
bool exported = !string.IsNullOrEmpty(integrationOrderId);
var actionNodes = new List<ActionNode>()
{
new()
{
Name = "Transfer via Live Integration",
Title = exported ? "Order already transferred" : "Transfer to ERP via Live Integration",
Icon = Icon.SignOutAlt,
NodeAction = ConfirmAction.For(RunCommandAction.For(new TransferOrderToErpCommand { OrderId = orderId }).WithReloadOnSuccess(),
"Transfer to ERP via Live Integration?",
exported ? $"Order {orderId} is already in the ERP, update again?" : $"Transfer order {orderId} to ERP via Live Integration?")
}
};
actionNodes.AddRange(GetOrderExportToXmlActions(settings, order));

return new List<ActionGroup>()
{
new()
{
Name = LiveIntegrationTab,
Title = LiveIntegrationTab,
Nodes = actionNodes
}
};
}

return [];
}

private static IEnumerable<ActionNode> GetOrderExportToXmlActions(Settings settings, Order order)
{
bool saveOrderXml = settings.SaveCopyOfOrderXml;

bool enableButton = saveOrderXml && System.IO.File.Exists(DownloadOrderXmlCommand.BuildXmlFileName(order));

return [
new ActionNode()
{
Icon = Icon.FileDownload,
Name = "Original XML",
Title = enableButton ?
"Downloads the original XML for an order as sent to the ERP" :
!saveOrderXml ?
"This option is not available because saving XML files is not enabled in the Live Integration setup." :
"This option is not available because the XML file does not exist.",
Disabled = !enableButton,
NodeAction = enableButton ? DownloadFileAction.Using(new DownloadOrderXmlCommand { OrderId = order.Id, GetOriginalXml = true }) : null
},

new ActionNode()
{
Icon = Icon.FileDownload,
Name = "Current XML",
Title = "Exports the order to an XML document",
NodeAction = DownloadFileAction.Using(new DownloadOrderXmlCommand { OrderId = order.Id, GetOriginalXml = false })
}
];
}
}
}

Loading
Loading