diff --git a/src/Dynamicweb.Ecommerce.DynamicwebLiveIntegration.Examples/Dynamicweb.Ecommerce.DynamicwebLiveIntegration.Examples.csproj b/src/Dynamicweb.Ecommerce.DynamicwebLiveIntegration.Examples/Dynamicweb.Ecommerce.DynamicwebLiveIntegration.Examples.csproj index 70a173d..8ccda05 100644 --- a/src/Dynamicweb.Ecommerce.DynamicwebLiveIntegration.Examples/Dynamicweb.Ecommerce.DynamicwebLiveIntegration.Examples.csproj +++ b/src/Dynamicweb.Ecommerce.DynamicwebLiveIntegration.Examples/Dynamicweb.Ecommerce.DynamicwebLiveIntegration.Examples.csproj @@ -18,15 +18,15 @@ false true true - true + true true snupkg - - - - + + + + diff --git a/src/Dynamicweb.Ecommerce.DynamicwebLiveIntegration/Dynamicweb.Ecommerce.DynamicwebLiveIntegration.csproj b/src/Dynamicweb.Ecommerce.DynamicwebLiveIntegration/Dynamicweb.Ecommerce.DynamicwebLiveIntegration.csproj index f3646fa..36ee499 100644 --- a/src/Dynamicweb.Ecommerce.DynamicwebLiveIntegration/Dynamicweb.Ecommerce.DynamicwebLiveIntegration.csproj +++ b/src/Dynamicweb.Ecommerce.DynamicwebLiveIntegration/Dynamicweb.Ecommerce.DynamicwebLiveIntegration.csproj @@ -1,6 +1,6 @@  - 10.4.26 + 10.16.0 1.0.0.0 Live Integration Live Integration @@ -23,11 +23,11 @@ snupkg - - - - - + + + + + diff --git a/src/Dynamicweb.Ecommerce.DynamicwebLiveIntegration/Extensions/ProductExtensions.cs b/src/Dynamicweb.Ecommerce.DynamicwebLiveIntegration/Extensions/ProductExtensions.cs index b1b3136..88c7dd5 100644 --- a/src/Dynamicweb.Ecommerce.DynamicwebLiveIntegration/Extensions/ProductExtensions.cs +++ b/src/Dynamicweb.Ecommerce.DynamicwebLiveIntegration/Extensions/ProductExtensions.cs @@ -24,7 +24,7 @@ public static bool HasIdentifier(this Product product, Settings settings) public static bool IsUnitUpdateNeeded(this Product product, string unitId) { if (string.IsNullOrEmpty(unitId) || product.DefaultUnitId == unitId || - !new Stocks.UnitOfMeasureService().GetUnitOfMeasures(product.Id).Any(u => u.UnitId.Equals(unitId))) + !Services.UnitOfMeasure.GetUnitOfMeasures(product.Id).Any(u => u.UnitId.Equals(unitId))) return false; return true; } diff --git a/src/Dynamicweb.Ecommerce.DynamicwebLiveIntegration/IntegrationCustomerCenterHandler.cs b/src/Dynamicweb.Ecommerce.DynamicwebLiveIntegration/IntegrationCustomerCenterHandler.cs index 0ff244f..20396d1 100644 --- a/src/Dynamicweb.Ecommerce.DynamicwebLiveIntegration/IntegrationCustomerCenterHandler.cs +++ b/src/Dynamicweb.Ecommerce.DynamicwebLiveIntegration/IntegrationCustomerCenterHandler.cs @@ -1,133 +1,76 @@ using System; using System.IO; using System.Xml; +using System.Xml.XPath; using System.Xml.Xsl; -using System.Collections.Generic; -using Dynamicweb.Rendering; +using Dynamicweb.Core; +using Dynamicweb.Ecommerce.DynamicwebLiveIntegration.Configuration; +using Dynamicweb.Ecommerce.DynamicwebLiveIntegration.Connectors; +using Dynamicweb.Ecommerce.DynamicwebLiveIntegration.Logging; +using Dynamicweb.Ecommerce.DynamicwebLiveIntegration.XmlGenerators.IntegrationCustomerCenter; using Dynamicweb.Ecommerce.Orders; -using System.Xml.XPath; -using Dynamicweb.Security.UserManagement; using Dynamicweb.Environment; using Dynamicweb.Extensibility.Notifications; -using Dynamicweb.Ecommerce.DynamicwebLiveIntegration.Logging; -using Dynamicweb.Ecommerce.DynamicwebLiveIntegration.XmlGenerators.IntegrationCustomerCenter; -using Dynamicweb.Ecommerce.DynamicwebLiveIntegration.Connectors; -using Dynamicweb.Ecommerce.DynamicwebLiveIntegration.Configuration; -using System.Linq; -using Dynamicweb.Ecommerce.Prices; -using Dynamicweb.Core; +using Dynamicweb.Rendering; +using Dynamicweb.Security.UserManagement; -namespace Dynamicweb.Ecommerce.DynamicwebLiveIntegration +namespace Dynamicweb.Ecommerce.DynamicwebLiveIntegration; + +/// +/// Represents sort direction +/// +internal enum SortDirection { - /// - /// Represents sort direction - /// - internal enum SortDirection - { - Asc = 0, - Desc - } + Asc = 0, + Desc +} + +/// +/// Static manager to handle integration customer center requests. +/// +internal class IntegrationCustomerCenterHandler +{ + private static readonly string OrderLinesLoop = "OrderLinesLoop"; /// - /// Static manager to handle integration customer center requests. + /// Retrieves integration customer center item details /// - internal class IntegrationCustomerCenterHandler + /// Template to render item details + /// Item type(OpenOrder,Invoice,Credit,SalesShipment) + /// User + /// item id + /// Template with rendered item details + internal static Template RetrieveItemDetailsFromRemoteSystem(Template template, string callType, User user, string itemID) { - private static readonly string OrderLinesLoop = "OrderLinesLoop"; - - /// - /// Retrieves integration customer center item details - /// - /// Template to render item details - /// Item type(OpenOrder,Invoice,Credit,SalesShipment) - /// User - /// item id - /// Template with rendered item details - internal static Template RetrieveItemDetailsFromRemoteSystem(Template template, string callType, User user, string itemID) + string shopId = Global.CurrentShopId; + var currentSettings = SettingsManager.GetSettingsByShop(shopId); + if (Global.IsIntegrationActive(currentSettings)) { - string shopId = Global.CurrentShopId; - var currentSettings = SettingsManager.GetSettingsByShop(shopId); - if (Global.IsIntegrationActive(currentSettings)) + var logger = new Logger(currentSettings); + Diagnostics.ExecutionTable.Current.Add("DynamicwebLiveIntegration.IntegrationCustomerCenterHandler.RetrieveItemDetailsFromRemoteSystem START"); + if (!string.IsNullOrEmpty(itemID) && !string.IsNullOrEmpty(Context.Current.Request.GetString("Redirect")) && Context.Current.Request.GetBoolean("addToCart")) { - var logger = new Logger(currentSettings); - Diagnostics.ExecutionTable.Current.Add("DynamicwebLiveIntegration.IntegrationCustomerCenterHandler.RetrieveItemDetailsFromRemoteSystem START"); - if (!string.IsNullOrEmpty(itemID) && !string.IsNullOrEmpty(Context.Current.Request.GetString("Redirect")) && Context.Current.Request.GetBoolean("addToCart")) - { - HandleReorder(itemID, logger); - } - else - { - if (!string.IsNullOrEmpty(callType) && user != null) - { - ItemDetailsXmlGeneratorSettings settings = new ItemDetailsXmlGeneratorSettings() - { - ItemType = callType, - CustomerId = user.CustomerNumber, - ItemId = itemID - }; - string request = new ItemDetailsXmlGenerator().GenerateItemDetailsXml(settings); - logger.Log(ErrorLevel.DebugInfo, string.Format("Request RetrieveItemDetailsFromRemoteSystem sent: '{0}'.", request)); - XmlDocument response = Connector.RetrieveDataFromRequestString(currentSettings, request, logger, SubmitType.Live); - if (response != null && !string.IsNullOrEmpty(response.InnerXml)) - { - logger.Log(ErrorLevel.DebugInfo, string.Format("Response RetrieveItemDetailsFromRemoteSystem received: '{0}'.", response.InnerXml)); - if (template.Type != Template.TemplateType.Xslt) - { - ProcessItemDetailsResponse(response, template, shopId, logger); - } - else - { - MakeXslTransformation(response, template, logger); - } - } - else - { - logger.Log(ErrorLevel.ResponseError, "Response RetrieveItemDetailsFromRemoteSystem returned null."); - } - } - } - Diagnostics.ExecutionTable.Current.Add("DynamicwebLiveIntegration.IntegrationCustomerCenterHandler.RetrieveItemDetailsFromRemoteSystem END"); + HandleReorder(itemID, logger); } - return template; - } - - /// - /// Retrieves integration customer center items list - /// - /// Template to render items list - /// Item type(OpenOrder,Invoice,Credit,SalesShipment) - /// User - /// List page size - /// List page index - /// Items count - /// Template with rendered items list - internal static Template RetrieveItemsListFromRemoteSystem(Template template, string callType, User user, int pageSize, int pageIndex, out int totalItemsCount) - { - totalItemsCount = 0; - string shopId = Global.CurrentShopId; - var currentSettings = SettingsManager.GetSettingsByShop(shopId); - if (Global.IsIntegrationActive(currentSettings)) - { - Diagnostics.ExecutionTable.Current.Add("DynamicwebLiveIntegration.IntegrationCustomerCenterHandler.RetrieveItemsListFromRemoteSystem START"); + else + { if (!string.IsNullOrEmpty(callType) && user != null) { - ItemListXmlGeneratorSettings settings = new ItemListXmlGeneratorSettings() + ItemDetailsXmlGeneratorSettings settings = new ItemDetailsXmlGeneratorSettings() { ItemType = callType, CustomerId = user.CustomerNumber, - PageIndex = pageIndex, - PageSize = pageSize + ItemId = itemID }; - string request = new ItemListXmlGenerator().GenerateItemListXml(settings); - var logger = new Logger(currentSettings); - logger.Log(ErrorLevel.DebugInfo, string.Format("Request RetrieveItemsListFromRemoteSystem sent: '{0}'.", request)); + string request = new ItemDetailsXmlGenerator().GenerateItemDetailsXml(settings); + logger.Log(ErrorLevel.DebugInfo, string.Format("Request RetrieveItemDetailsFromRemoteSystem sent: '{0}'.", request)); XmlDocument response = Connector.RetrieveDataFromRequestString(currentSettings, request, logger, SubmitType.Live); if (response != null && !string.IsNullOrEmpty(response.InnerXml)) { - logger.Log(ErrorLevel.DebugInfo, string.Format("Response RetrieveItemsListFromRemoteSystem received: '{0}'.", response.InnerXml)); + logger.Log(ErrorLevel.DebugInfo, string.Format("Response RetrieveItemDetailsFromRemoteSystem received: '{0}'.", response.InnerXml)); if (template.Type != Template.TemplateType.Xslt) { - ProcessItemsListResponse(response, template, callType, pageSize, pageIndex, out totalItemsCount, shopId, logger); + ProcessItemDetailsResponse(response, template, shopId, logger); } else { @@ -136,257 +79,309 @@ internal static Template RetrieveItemsListFromRemoteSystem(Template template, st } else { - logger.Log(ErrorLevel.ResponseError, "Response RetrieveItemsListFromRemoteSystem returned null."); + logger.Log(ErrorLevel.ResponseError, "Response RetrieveItemDetailsFromRemoteSystem returned null."); } } - Diagnostics.ExecutionTable.Current.Add("DynamicwebLiveIntegration.IntegrationCustomerCenterHandler.RetrieveItemsListFromRemoteSystem END"); } - return template; + Diagnostics.ExecutionTable.Current.Add("DynamicwebLiveIntegration.IntegrationCustomerCenterHandler.RetrieveItemDetailsFromRemoteSystem END"); } - - private static void ProcessItemDetailsResponse(XmlDocument response, Template template, string shopId, Logger logger) + return template; + } + + /// + /// Retrieves integration customer center items list + /// + /// Template to render items list + /// Item type(OpenOrder,Invoice,Credit,SalesShipment) + /// User + /// List page size + /// List page index + /// Items count + /// Template with rendered items list + internal static Template RetrieveItemsListFromRemoteSystem(Template template, string callType, User user, int pageSize, int pageIndex, out int totalItemsCount) + { + totalItemsCount = 0; + string shopId = Global.CurrentShopId; + var currentSettings = SettingsManager.GetSettingsByShop(shopId); + if (Global.IsIntegrationActive(currentSettings)) { - try + Diagnostics.ExecutionTable.Current.Add("DynamicwebLiveIntegration.IntegrationCustomerCenterHandler.RetrieveItemsListFromRemoteSystem START"); + if (!string.IsNullOrEmpty(callType) && user != null) { - XmlNode orderNode = response.SelectSingleNode("//item [@table='EcomOrders']"); - //Set Order tags - if (orderNode != null && orderNode.ChildNodes.Count > 0) + ItemListXmlGeneratorSettings settings = new ItemListXmlGeneratorSettings() { - foreach (XmlNode itemProperty in orderNode.ChildNodes) + ItemType = callType, + CustomerId = user.CustomerNumber, + PageIndex = pageIndex, + PageSize = pageSize + }; + string request = new ItemListXmlGenerator().GenerateItemListXml(settings); + var logger = new Logger(currentSettings); + logger.Log(ErrorLevel.DebugInfo, string.Format("Request RetrieveItemsListFromRemoteSystem sent: '{0}'.", request)); + XmlDocument response = Connector.RetrieveDataFromRequestString(currentSettings, request, logger, SubmitType.Live); + if (response != null && !string.IsNullOrEmpty(response.InnerXml)) + { + logger.Log(ErrorLevel.DebugInfo, string.Format("Response RetrieveItemsListFromRemoteSystem received: '{0}'.", response.InnerXml)); + if (template.Type != Template.TemplateType.Xslt) { - if (itemProperty.Attributes["columnName"] != null) - { - string tagName = itemProperty.Attributes["columnName"].Value; - template.SetTag(tagName, itemProperty.InnerText); - } + ProcessItemsListResponse(response, template, callType, pageSize, pageIndex, out totalItemsCount, shopId, logger); + } + else + { + MakeXslTransformation(response, template, logger); } } - //Set OrderLines loop - XmlNodeList orderLinesNodes = response.SelectNodes("//item [@table='EcomOrderLines']"); - if (orderLinesNodes != null && orderLinesNodes.Count > 0)//Process OrderLines + else { - Template orderLinesLoop = template.GetLoop(OrderLinesLoop); + logger.Log(ErrorLevel.ResponseError, "Response RetrieveItemsListFromRemoteSystem returned null."); + } + } + Diagnostics.ExecutionTable.Current.Add("DynamicwebLiveIntegration.IntegrationCustomerCenterHandler.RetrieveItemsListFromRemoteSystem END"); + } + return template; + } - foreach (XmlNode orderLineNode in orderLinesNodes) + private static void ProcessItemDetailsResponse(XmlDocument response, Template template, string shopId, Logger logger) + { + try + { + XmlNode orderNode = response.SelectSingleNode("//item [@table='EcomOrders']"); + //Set Order tags + if (orderNode != null && orderNode.ChildNodes.Count > 0) + { + foreach (XmlNode itemProperty in orderNode.ChildNodes) + { + if (itemProperty.Attributes["columnName"] != null) { - foreach (XmlNode itemProperty in orderLineNode.ChildNodes) - { - if (itemProperty.Attributes["columnName"] != null) - { - string tagName = itemProperty.Attributes["columnName"].Value; - orderLinesLoop.SetTag(tagName, itemProperty.InnerText); - } - } - orderLinesLoop.CommitLoop(); + string tagName = itemProperty.Attributes["columnName"].Value; + template.SetTag(tagName, itemProperty.InnerText); } } - SetUrlTags(template, shopId); } - catch (Exception e) + //Set OrderLines loop + XmlNodeList orderLinesNodes = response.SelectNodes("//item [@table='EcomOrderLines']"); + if (orderLinesNodes != null && orderLinesNodes.Count > 0)//Process OrderLines { - logger.Log(ErrorLevel.Error, string.Format("Response does not match schema: '{0}'.", e.Message)); + Template orderLinesLoop = template.GetLoop(OrderLinesLoop); + + foreach (XmlNode orderLineNode in orderLinesNodes) + { + foreach (XmlNode itemProperty in orderLineNode.ChildNodes) + { + if (itemProperty.Attributes["columnName"] != null) + { + string tagName = itemProperty.Attributes["columnName"].Value; + orderLinesLoop.SetTag(tagName, itemProperty.InnerText); + } + } + orderLinesLoop.CommitLoop(); + } } + SetUrlTags(template, shopId); } + catch (Exception e) + { + logger.Log(ErrorLevel.Error, string.Format("Response does not match schema: '{0}'.", e.Message)); + } + } - private static void ProcessItemsListResponse(XmlDocument response, Template template, string callType, int pageSize, int pageIndex, out int totalItemsCount, string shopId, Logger logger) + private static void ProcessItemsListResponse(XmlDocument response, Template template, string callType, int pageSize, int pageIndex, out int totalItemsCount, string shopId, Logger logger) + { + totalItemsCount = 0; + string tagPrefix = string.Format("Ecom:IntegrationCustomerCenter.{0}", callType); + try { - totalItemsCount = 0; - string tagPrefix = string.Format("Ecom:IntegrationCustomerCenter.{0}", callType); - try - { - XmlNodeList items = response.SelectNodes("//item"); + XmlNodeList items = response.SelectNodes("//item"); - if (items != null && items.Count > 0) + if (items != null && items.Count > 0) + { + XmlNode itemsNode = response.SelectSingleNode("Items"); + bool isTotalCountAttributePresent = itemsNode != null && itemsNode.Attributes["totalCount"] != null; + if (isTotalCountAttributePresent) { - XmlNode itemsNode = response.SelectSingleNode("Items"); - bool isTotalCountAttributePresent = itemsNode != null && itemsNode.Attributes["totalCount"] != null; - if (isTotalCountAttributePresent) - { - totalItemsCount = Converter.ToInt32(itemsNode.Attributes["totalCount"].Value); - } - if (totalItemsCount <= 0) - { - totalItemsCount = items.Count; - } + totalItemsCount = Converter.ToInt32(itemsNode.Attributes["totalCount"].Value); + } + if (totalItemsCount <= 0) + { + totalItemsCount = items.Count; + } - string itemsLoopName = string.Format("{0}Loop", tagPrefix); - Template itemsLoop = template.GetLoop(itemsLoopName); + string itemsLoopName = string.Format("{0}Loop", tagPrefix); + Template itemsLoop = template.GetLoop(itemsLoopName); - int index = 0; - bool usePaging = totalItemsCount > pageSize; - pageIndex = pageIndex > 0 ? pageIndex - 1 : 0; - int startIndex = pageSize * pageIndex; - int endIndex = startIndex + pageSize; + int index = 0; + bool usePaging = totalItemsCount > pageSize; + pageIndex = pageIndex > 0 ? pageIndex - 1 : 0; + int startIndex = pageSize * pageIndex; + int endIndex = startIndex + pageSize; - foreach (XmlNode item in items) + foreach (XmlNode item in items) + { + if (index >= startIndex || !usePaging || isTotalCountAttributePresent) { - if (index >= startIndex || !usePaging || isTotalCountAttributePresent) + foreach (XmlNode itemProperty in item.ChildNodes) { - foreach (XmlNode itemProperty in item.ChildNodes) + if (itemProperty.Attributes["columnName"] != null) { - if (itemProperty.Attributes["columnName"] != null) - { - string tagName = itemProperty.Attributes["columnName"].Value; - itemsLoop.SetTag(tagName, itemProperty.InnerText); - } + string tagName = itemProperty.Attributes["columnName"].Value; + itemsLoop.SetTag(tagName, itemProperty.InnerText); } - SetUrlTags(itemsLoop, shopId); - itemsLoop.CommitLoop(); - } - index++; - if (usePaging && index >= endIndex) - { - break; } + SetUrlTags(itemsLoop, shopId); + itemsLoop.CommitLoop(); + } + index++; + if (usePaging && index >= endIndex) + { + break; } - } - else - { - //set empty list tag - template.SetTag(string.Format("{0}.EmptyList", tagPrefix), true); } } - catch (Exception e) + else { - logger.Log(ErrorLevel.Error, string.Format("Response does not match schema: '{0}'.", e.Message)); + //set empty list tag + template.SetTag(string.Format("{0}.EmptyList", tagPrefix), true); } } - - private static void MakeXslTransformation(XmlDocument doc, Template template, Logger logger) + catch (Exception e) { - try - { - XslCompiledTransform xslTransform = new XslCompiledTransform(); - StringWriter writer = new StringWriter(); - xslTransform.Load(new XPathDocument(new StringReader(template.Html))); - xslTransform.Transform(doc.CreateNavigator(), null, writer); - template.Html = writer.ToString(); - } - catch (Exception ex) - { - string msg = string.Format("Error in xsl transformation: '{0}'.", ex.Message); - template.Html = msg; - logger.Log(ErrorLevel.Error, msg); - } + logger.Log(ErrorLevel.Error, string.Format("Response does not match schema: '{0}'.", e.Message)); } + } - private static void SetUrlTags(Template template, string shopId) + private static void MakeXslTransformation(XmlDocument doc, Template template, Logger logger) + { + try { - var user = Helpers.GetCurrentExtranetUser(); - string userID = user != null ? user.ID.ToString() : string.Empty; - template.SetTag("UrlUserID", userID); - template.SetTag("UrlShopID", shopId); + XslCompiledTransform xslTransform = new XslCompiledTransform(); + StringWriter writer = new StringWriter(); + xslTransform.Load(new XPathDocument(new StringReader(template.Html))); + xslTransform.Transform(doc.CreateNavigator(), null, writer); + template.Html = writer.ToString(); } + catch (Exception ex) + { + string msg = string.Format("Error in xsl transformation: '{0}'.", ex.Message); + template.Html = msg; + logger.Log(ErrorLevel.Error, msg); + } + } + + private static void SetUrlTags(Template template, string shopId) + { + var user = Helpers.GetCurrentExtranetUser(); + string userID = user != null ? user.ID.ToString() : string.Empty; + template.SetTag("UrlUserID", userID); + template.SetTag("UrlShopID", shopId); + } - private static void HandleReorder(string itemID, Logger logger) + private static void HandleReorder(string itemID, Logger logger) + { + OrderService orderService = new OrderService(); + Order order = orderService.GetById(itemID); + if (order != null) { - OrderService orderService = new OrderService(); - Order order = orderService.GetById(itemID); - if (order != null) + Order ecomCart; + if (Common.Context.Cart is not object) { - Order ecomCart; - if (Common.Context.Cart is not object) + ecomCart = new Order(Common.Context.Currency, Common.Context.Country, Common.Context.Language) { - ecomCart = new Order(Common.Context.Currency, Common.Context.Country, Common.Context.Language) - { - IsCart = true - }; - Common.Context.SetCart(ecomCart); - } + IsCart = true + }; + Common.Context.SetCart(ecomCart); + } - ecomCart = Common.Context.Cart; + ecomCart = Common.Context.Cart; - foreach (OrderLine orderLineToCopy in order.OrderLines) + foreach (OrderLine orderLineToCopy in order.OrderLines) + { + var OrderlineBuilder = new OrderLineBuilderConfig() { - var OrderlineBuilder = new OrderLineBuilderConfig() - { - ProductId = orderLineToCopy.ProductId, - Quantity = orderLineToCopy.Quantity, - VariantId = orderLineToCopy.ProductVariantId, - VariantText = orderLineToCopy.ProductVariantText, - UnitId = orderLineToCopy.UnitId, - OrderLineType = orderLineToCopy.OrderLineType, - ReferenceUrl = orderLineToCopy.Reference, - PageId = orderLineToCopy.PageId, - ProductName = string.IsNullOrEmpty(orderLineToCopy.ProductId) ? orderLineToCopy.ProductName : string.Empty, - ProductNumber = string.IsNullOrEmpty(orderLineToCopy.ProductId) ? orderLineToCopy.ProductNumber : string.Empty, - OrderLineFieldValues = orderLineToCopy.OrderLineFieldValues, - }; - - Frontend.Cart.CartService service = new Frontend.Cart.CartService(); - var newOrderLine = service.OrderLineBuilder(ecomCart, OrderlineBuilder); + ProductId = orderLineToCopy.ProductId, + Quantity = orderLineToCopy.Quantity, + VariantId = orderLineToCopy.ProductVariantId, + VariantText = orderLineToCopy.ProductVariantText, + UnitId = orderLineToCopy.UnitId, + OrderLineType = orderLineToCopy.OrderLineType, + ReferenceUrl = orderLineToCopy.Reference, + PageId = orderLineToCopy.PageId, + ProductName = string.IsNullOrEmpty(orderLineToCopy.ProductId) ? orderLineToCopy.ProductName : string.Empty, + ProductNumber = string.IsNullOrEmpty(orderLineToCopy.ProductId) ? orderLineToCopy.ProductNumber : string.Empty, + OrderLineFieldValues = orderLineToCopy.OrderLineFieldValues, + }; - Frontend.Cart.CartCatch.SaveCart(); + var newOrderLine = Services.Carts.OrderLineBuilder(ecomCart, OrderlineBuilder); - NotificationManager.Notify(Ecommerce.Notifications.Ecommerce.Cart.Line.Added, - new Ecommerce.Notifications.Ecommerce.Cart.Line.AddedArgs(newOrderLine, ecomCart)); - } - ecomCart.Comment = order.Comment; - ecomCart.CustomerComment = order.CustomerComment; - OrderFieldValueCollection orderFieldValues = new OrderFieldValueCollection(); - foreach (OrderFieldValue orderField in order.OrderFieldValues) - { - orderFieldValues.Add(new OrderFieldValue(orderField.OrderField, orderField.Value)); - } - ecomCart.OrderFieldValues = orderFieldValues; - orderService.Save(ecomCart); - orderService.RemoveNoneActiveProducts(ecomCart); + Frontend.Cart.CartCatch.SaveCart(); - string redirectUrl = Context.Current.Request.GetString("Redirect"); - if (Context.Current.Request.GetInt32("Redirect") != 0) - { - redirectUrl = "/Default.aspx?ID=" + Context.Current.Request.GetString("Redirect"); - } - Context.Current.Response.Redirect(redirectUrl); - Context.Current.Response.End(); + NotificationManager.Notify(Ecommerce.Notifications.Ecommerce.Cart.Line.Added, + new Ecommerce.Notifications.Ecommerce.Cart.Line.AddedArgs(newOrderLine, ecomCart)); } - else + ecomCart.Comment = order.Comment; + ecomCart.CustomerComment = order.CustomerComment; + OrderFieldValueCollection orderFieldValues = new OrderFieldValueCollection(); + foreach (OrderFieldValue orderField in order.OrderFieldValues) { - logger.Log(ErrorLevel.Error, string.Format("Can not find order with ID = '{0}'", itemID)); + orderFieldValues.Add(new OrderFieldValue(orderField.OrderField, orderField.Value)); } - } + ecomCart.OrderFieldValues = orderFieldValues; + orderService.Save(ecomCart); + orderService.RemoveNoneActiveProducts(ecomCart); - /// - /// Retrieves order information in pdf - /// - internal static string RetrievePDF(IRequest request) - { - string shopId = Global.CurrentShopId; - var currentSettings = SettingsManager.GetSettingsByShop(shopId); - if (Global.IsIntegrationActive(currentSettings)) + string redirectUrl = Context.Current.Request.GetString("Redirect"); + if (Context.Current.Request.GetInt32("Redirect") != 0) { - string id = request["id"]; - User user = Helpers.GetCurrentExtranetUser(); - - RetrievePdfXmlGeneratorSettings settings = new RetrievePdfXmlGeneratorSettings() - { - Type = request["type"], - CustomerId = user.CustomerNumber, - ItemId = id - }; - string requestString = new RetrievePdfXmlGenerator().GenerateXml(settings); - string base64EncodedPDF = Connector.RetrievePDF(currentSettings, requestString, SubmitType.Live); - return base64EncodedPDF; + redirectUrl = "/Default.aspx?ID=" + Context.Current.Request.GetString("Redirect"); } - return null; + Context.Current.Response.Redirect(redirectUrl); + Context.Current.Response.End(); + } + else + { + logger.Log(ErrorLevel.Error, string.Format("Can not find order with ID = '{0}'", itemID)); } + } - /// - /// Decodes the stream. - /// - /// The in stream. - /// The output. - private static void DecodeStream(Stream inStream, Stream output) + /// + /// Retrieves order information in pdf + /// + internal static string RetrievePDF(IRequest request) + { + string shopId = Global.CurrentShopId; + var currentSettings = SettingsManager.GetSettingsByShop(shopId); + if (Global.IsIntegrationActive(currentSettings)) { - using System.Security.Cryptography.ICryptoTransform transform = new System.Security.Cryptography.FromBase64Transform(); - using var cryptStream = new System.Security.Cryptography.CryptoStream(inStream, transform, System.Security.Cryptography.CryptoStreamMode.Read); - byte[] buffer = new byte[4096]; - int bytesRead = cryptStream.Read(buffer, 0, buffer.Length); - while (bytesRead > 0) + string id = request["id"]; + User user = Helpers.GetCurrentExtranetUser(); + + RetrievePdfXmlGeneratorSettings settings = new RetrievePdfXmlGeneratorSettings() { - output.Write(buffer, 0, bytesRead); - bytesRead = cryptStream.Read(buffer, 0, buffer.Length); - } - } + Type = request["type"], + CustomerId = user.CustomerNumber, + ItemId = id + }; + string requestString = new RetrievePdfXmlGenerator().GenerateXml(settings); + string base64EncodedPDF = Connector.RetrievePDF(currentSettings, requestString, SubmitType.Live); + return base64EncodedPDF; + } + return null; } -} + + /// + /// Decodes the stream. + /// + /// The in stream. + /// The output. + private static void DecodeStream(Stream inStream, Stream output) + { + using System.Security.Cryptography.ICryptoTransform transform = new System.Security.Cryptography.FromBase64Transform(); + using var cryptStream = new System.Security.Cryptography.CryptoStream(inStream, transform, System.Security.Cryptography.CryptoStreamMode.Read); + byte[] buffer = new byte[4096]; + int bytesRead = cryptStream.Read(buffer, 0, buffer.Length); + while (bytesRead > 0) + { + output.Write(buffer, 0, bytesRead); + bytesRead = cryptStream.Read(buffer, 0, buffer.Length); + } + } +} \ No newline at end of file diff --git a/src/Dynamicweb.Ecommerce.DynamicwebLiveIntegration/NotificationSubscribers/VariantListBeforeRender.cs b/src/Dynamicweb.Ecommerce.DynamicwebLiveIntegration/NotificationSubscribers/VariantListBeforeRender.cs index d1ba415..e9e3d06 100644 --- a/src/Dynamicweb.Ecommerce.DynamicwebLiveIntegration/NotificationSubscribers/VariantListBeforeRender.cs +++ b/src/Dynamicweb.Ecommerce.DynamicwebLiveIntegration/NotificationSubscribers/VariantListBeforeRender.cs @@ -1,57 +1,54 @@ using Dynamicweb.Ecommerce.DynamicwebLiveIntegration.Configuration; using Dynamicweb.Ecommerce.DynamicwebLiveIntegration.Products; -using Dynamicweb.Ecommerce.Products; using Dynamicweb.Extensibility.Notifications; using Dynamicweb.Security.UserManagement; -namespace Dynamicweb.Ecommerce.DynamicwebLiveIntegration.NotificationSubscribers +namespace Dynamicweb.Ecommerce.DynamicwebLiveIntegration.NotificationSubscribers; + +[Subscribe(Ecommerce.Notifications.Ecommerce.VariantList.BeforeRender)] +public class VariantListBeforeRender : NotificationSubscriberBase { - [Subscribe(Ecommerce.Notifications.Ecommerce.VariantList.BeforeRender)] - public class VariantListBeforeRender : NotificationSubscriberBase + public override void OnNotify(string notification, NotificationArgs args) { - public override void OnNotify(string notification, NotificationArgs args) + if (args is Ecommerce.Notifications.Ecommerce.VariantList.BeforeRenderArgs beforeRenderArgs) { - if (args is Ecommerce.Notifications.Ecommerce.VariantList.BeforeRenderArgs beforeRenderArgs) - { - var variantCombinationProducts = beforeRenderArgs.VariantCombinationsProducts; - if (variantCombinationProducts != null) { - var settings = SettingsManager.GetSettingsByShop(Global.CurrentShopId); - if (settings != null && - settings.AddProductFieldsToRequest && - settings.GetProductInformationForAllVariants && - CanCheckPrice(settings)) + var variantCombinationProducts = beforeRenderArgs.VariantCombinationsProducts; + if (variantCombinationProducts != null) { + var settings = SettingsManager.GetSettingsByShop(Global.CurrentShopId); + if (settings != null && + settings.AddProductFieldsToRequest && + settings.GetProductInformationForAllVariants && + CanCheckPrice(settings)) + { + var user = UserContext.Current.User; + foreach (var vcp in variantCombinationProducts) { - var user = UserContext.Current.User; - foreach (var vcp in variantCombinationProducts) + var variantProduct = vcp.Value; + if (variantProduct == null) + continue; + var productInfo = ProductManager.GetProductInfo(variantProduct, settings, user); + if (productInfo != null) { - var variantProduct = vcp.Value; - if (variantProduct == null) - continue; - var productInfo = ProductManager.GetProductInfo(variantProduct, settings, user); - if (productInfo != null) + if (variantProduct.ProductFieldValues == null) + { + Services.Products.SetDefaultProductFields(variantProduct); + } + if (variantProduct.ProductFieldValues != null) { - if (variantProduct.ProductFieldValues == null) - { - ProductService service = new ProductService(); - service.SetDefaultProductFields(variantProduct); - } - if (variantProduct.ProductFieldValues != null) - { - ProductManager.FillProductFieldValues(variantProduct, productInfo); - } + ProductManager.FillProductFieldValues(variantProduct, productInfo); } } } } } } + } - private static bool CanCheckPrice(Settings settings) - { - return EnabledAndActive(settings, SubmitType.Live) && settings.EnableLivePrices && - (settings.LiveProductInfoForAnonymousUsers || Helpers.GetCurrentExtranetUser() != null) && - (Helpers.GetCurrentExtranetUser() == null || !Helpers.GetCurrentExtranetUser().IsLivePricesDisabled) && - !Global.IsProductLazyLoad(settings); - } + private static bool CanCheckPrice(Settings settings) + { + return EnabledAndActive(settings, SubmitType.Live) && settings.EnableLivePrices && + (settings.LiveProductInfoForAnonymousUsers || Helpers.GetCurrentExtranetUser() != null) && + (Helpers.GetCurrentExtranetUser() == null || !Helpers.GetCurrentExtranetUser().IsLivePricesDisabled) && + !Global.IsProductLazyLoad(settings); } }