-
Notifications
You must be signed in to change notification settings - Fork 0
New "Hybrid" Shipping Control Mode — DW Selects, BC Calculates #68
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
Draft
DWDBE
wants to merge
7
commits into
main
Choose a base branch
from
dbe/27886-New-Shipping-Control-Mode-DW-Selects-BC-Calculates
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5ebfa7a
New "Hybrid" Shipping Control Mode — DW Selects, BC Calculates
DWDBE b86f869
fix build
DWDBE a1f1749
code review and bug fixes
DWDBE 3254a6e
NormalizeShippingControlMode
DWDBE e0c64e5
fix breaking change by obsolete old ErpControlsShipping property
DWDBE e1c2741
Apply suggestions from code review
DWDBE 70f8edd
fix build
DWDBE File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
...eb.Ecommerce.DynamicwebLiveIntegration/Dynamicweb.Ecommerce.DynamicwebLiveIntegration.sln
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| Microsoft Visual Studio Solution File, Format Version 12.00 | ||
| # Visual Studio Version 17 | ||
| VisualStudioVersion = 17.5.2.0 | ||
| MinimumVisualStudioVersion = 10.0.40219.1 | ||
| Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dynamicweb.Ecommerce.DynamicwebLiveIntegration", "Dynamicweb.Ecommerce.DynamicwebLiveIntegration.csproj", "{5B09B2C6-8E15-A19A-AA43-E303D71A0874}" | ||
| EndProject | ||
| Global | ||
| GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
| Debug|Any CPU = Debug|Any CPU | ||
| Release|Any CPU = Release|Any CPU | ||
| EndGlobalSection | ||
| GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
| {5B09B2C6-8E15-A19A-AA43-E303D71A0874}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
| {5B09B2C6-8E15-A19A-AA43-E303D71A0874}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
| {5B09B2C6-8E15-A19A-AA43-E303D71A0874}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
| {5B09B2C6-8E15-A19A-AA43-E303D71A0874}.Release|Any CPU.Build.0 = Release|Any CPU | ||
| EndGlobalSection | ||
| GlobalSection(SolutionProperties) = preSolution | ||
| HideSolutionNode = FALSE | ||
| EndGlobalSection | ||
| GlobalSection(ExtensibilityGlobals) = postSolution | ||
| SolutionGuid = {8116CA11-5865-48E8-9C90-5CBF48FF6A82} | ||
| EndGlobalSection | ||
| EndGlobal |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/Dynamicweb.Ecommerce.DynamicwebLiveIntegration/Shipping/CartBeforeShippingCalculation.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| using Dynamicweb.Ecommerce.DynamicwebLiveIntegration.NotificationSubscribers; | ||
| using Dynamicweb.Extensibility.Notifications; | ||
|
|
||
| namespace Dynamicweb.Ecommerce.DynamicwebLiveIntegration.Shipping | ||
| { | ||
| [Subscribe(Ecommerce.Notifications.Ecommerce.Cart.BeforeShippingCalculation)] | ||
| public class CartBeforeShippingCalculation : NotificationSubscriberBase | ||
| { | ||
| public override void OnNotify(string notification, NotificationArgs args) | ||
| { | ||
| if (args is null || Context.Current?.Items is null) | ||
| return; | ||
|
|
||
| var myArgs = (Ecommerce.Notifications.Ecommerce.Cart.BeforeShippingCalculationArgs)args; | ||
| if (myArgs.Shipping is null || myArgs.Order is null || Context.Current.Session?["ErpShippingFeeProvider_" + myArgs.Order.Id] is null) | ||
|
DWDBE marked this conversation as resolved.
Outdated
|
||
| { | ||
| return; | ||
| } | ||
| Context.Current.Items[$"CartBeforeShippingCalculationShippingId{myArgs.Order.Id}"] = myArgs.Shipping.Id; | ||
| } | ||
| } | ||
| } | ||
102 changes: 102 additions & 0 deletions
102
src/Dynamicweb.Ecommerce.DynamicwebLiveIntegration/Shipping/ErpShippingFeeProvider.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| using Dynamicweb.Ecommerce.DynamicwebLiveIntegration.Configuration; | ||
| using Dynamicweb.Ecommerce.DynamicwebLiveIntegration.Logging; | ||
| using Dynamicweb.Ecommerce.DynamicwebLiveIntegration.NotificationSubscribers; | ||
| using Dynamicweb.Ecommerce.Orders; | ||
| using Dynamicweb.Ecommerce.Prices; | ||
| using System.Xml; | ||
|
|
||
| namespace Dynamicweb.Ecommerce.DynamicwebLiveIntegration.Shipping | ||
| { | ||
| public class ErpShippingFeeProvider : FeeProvider | ||
| { | ||
| private const string CachePrefix = "ErpShippingFeeProvider_"; | ||
|
|
||
| private static string OrderMarkerKey(string orderId) => CachePrefix + orderId; | ||
| private static string MethodCacheKey(string orderId, string shippingMethodId) => CachePrefix + orderId + "_" + shippingMethodId; | ||
| private static string BeforeShippingItemKey(string orderId) => "CartBeforeShippingCalculationShippingId" + orderId; | ||
|
|
||
| public override PriceRaw FindFee(Order order) | ||
| { | ||
| if (Context.Current?.Session is null || Context.Current.Items is null) | ||
| return null; | ||
|
|
||
| var shippingMethodId = Core.Converter.ToString(Context.Current.Items[BeforeShippingItemKey(order.Id)]); | ||
| if (string.IsNullOrEmpty(shippingMethodId)) | ||
| return null; | ||
|
|
||
| var cached = Context.Current.Session[MethodCacheKey(order.Id, shippingMethodId)] as PriceInfo; | ||
| if (cached is null) | ||
| return null; | ||
|
|
||
| return new PriceRaw(cached.PriceWithVAT, order.Currency); | ||
| } | ||
|
|
||
| internal static void ProcessShipping(Settings settings, Order order, XmlNode orderNode, Logger logger) | ||
| { | ||
| Diagnostics.ExecutionTable.Current.Add("DynamicwebLiveIntegration.ErpShippingFeeProvider START"); | ||
|
|
||
| string shippingFee = orderNode.SelectSingleNode("column [@columnName='OrderShippingFee']")?.InnerText; | ||
| if (string.IsNullOrEmpty(shippingFee)) | ||
| { | ||
| ClearCache(order); | ||
| return; | ||
| } | ||
|
|
||
| double fee = Helpers.ToDouble(settings, logger, shippingFee); | ||
|
|
||
| string shippingFeeWithoutVat = orderNode.SelectSingleNode("column [@columnName='OrderShippingFeeWithoutVat']")?.InnerText; | ||
| double feeWithoutVat = 0; | ||
| double vatPercent = order.Price.VATPercent; | ||
| if (!string.IsNullOrEmpty(shippingFeeWithoutVat)) | ||
| { | ||
| feeWithoutVat = Helpers.ToDouble(settings, logger, shippingFeeWithoutVat); | ||
| feeWithoutVat = feeWithoutVat > fee ? fee : feeWithoutVat; | ||
| vatPercent = feeWithoutVat > 0 ? (fee / feeWithoutVat - 1) * 100 : 0; | ||
| } | ||
| if (feeWithoutVat <= 0) | ||
| { | ||
| feeWithoutVat = MinusVat(fee, vatPercent); | ||
| } | ||
|
DWDBE marked this conversation as resolved.
|
||
|
|
||
| var price = new PriceInfo(order.Currency); | ||
| price.PriceWithVAT = fee; | ||
| price.PriceWithoutVAT = feeWithoutVat; | ||
| price.VATPercent = vatPercent; | ||
| price.VAT = fee - feeWithoutVat; | ||
|
|
||
| AddToCache(order, price); | ||
|
|
||
| order.ShippingFee.PriceWithVAT = price.PriceWithVAT; | ||
| order.ShippingFee.VATPercent = price.VATPercent; | ||
| order.ShippingFee.PriceWithoutVAT = price.PriceWithoutVAT; | ||
| order.ShippingFee.VAT = price.VAT; | ||
|
|
||
| Diagnostics.ExecutionTable.Current.Add("DynamicwebLiveIntegration.ErpShippingFeeProvider END"); | ||
|
DWDBE marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| internal static double MinusVat(double price, double percent) | ||
| { | ||
| return (double)((decimal)price / ((decimal)percent / 100M + 1M)); | ||
| } | ||
|
|
||
| private static void AddToCache(Order order, PriceInfo shippingFee) | ||
| { | ||
| if (Context.Current?.Session is null || string.IsNullOrEmpty(order.ShippingMethodId)) | ||
| return; | ||
|
|
||
| // Existence marker lets the notification subscriber know ERP fee caching is active for this order | ||
| Context.Current.Session[OrderMarkerKey(order.Id)] = true; | ||
| Context.Current.Session[MethodCacheKey(order.Id, order.ShippingMethodId)] = shippingFee; | ||
| } | ||
|
|
||
| private static void ClearCache(Order order) | ||
| { | ||
| if (Context.Current?.Session is null) | ||
| return; | ||
|
|
||
| Context.Current.Session.Remove(OrderMarkerKey(order.Id)); | ||
| if (!string.IsNullOrEmpty(order.ShippingMethodId)) | ||
| Context.Current.Session.Remove(MethodCacheKey(order.Id, order.ShippingMethodId)); | ||
|
DWDBE marked this conversation as resolved.
|
||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.