Monday 21 March 2016

Dynamics AX Overlapped Discounts | Optimal Combinations



 Introduction


This documents describes how Microsoft Dynamics AX for Retail determine which combination of discounts to apply to transactions when more than one discount applies to one or more items in the transaction and the total quantity of the items with multiple possible discounts increases beyond a few (2-5) items. The exact number of items depends on how many discounts can be applied to each item and how complex the discounts are.


Overlapping discounts is the term we’ll use when two or more discounts can be applied to the same set of items in a sales transaction. Determining the combination of discounts to use that would results in the lowest transaction total is a combinatorial optimization problem. This is especially true when a discount requires 2 or more items to be purchased together and the effective discount amount varies with the price of the two items, as is the case in the common retail discount Buy 1 Get 1 X% off. To determine the best combination of discounts to apply quickly and continue to provide the rich discount functionality available in Dynamics AX for Retail a new methodology has been introduced for determining the best combination of discounts by evaluating the discounts relative to each other as applied to all the items in a transaction with overlapping discounts.

Let’s look at an example to understand the problem of how to choose the best combination of discounts.
We’ll buy 2 items and there are two possible discounts which cannot be combined 

  • Discount 1: Buy 1 item get a 2nd item of equal or lesser value at 50% off.
  • Discount 2: Buy 2 items and get 20% off both.

For any 2 items the better of these two discounts depends on the price of the two items with respect to each other. When the price of both items is equal or close to equal then Discount 1 is better.  When the price of one item is significantly less than the other item then Discount 2 is better. The rule for evaluating these two specific discounts against each other works out to be: If     Then Discount 1 is the better value otherwise Discount 2 is the better value. Note, when the price of item 1 is equal to 2/3 the price of item 2 then the two discounts are equal.

In this example the effective discount percent for Discount 1 varies from a few percent when the price of the two items is far apart to a maximum of 25% when the two items have the same price. The effective discount percent for Discount 2 is fixed. It is always 20%. Because the effective discount for Discount 1 is ranges above and below Discount 2 the best discount can only be chosen based on the price of the items to be discounted.

If we buy 4 items and both discounts could be applied to all 4 items then only two applications of Discount 1 or Discount 2 could be applied to the transaction because each discount requires two items to qualify and the discounts cannot be combined on any single item.  These are ‘Exclusive’ discounts.

Let’s look at three sets of items with different price ranges to illustrate the possible combinations. 

  1. All items have the same Rs. 15 price. The best discount combination is two applications of Discount 1. You will have 2 full priced items and two 50% off items. The transaction total will be Rs. 45 which is net 25% off the total of Rs. 60. (Rs. 15, Rs. 15, Rs. 7.5, Rs. 7.5). Discount 2 is only 20% off.
  2. Now let’s say two items are Rs. 20 each and two items are Rs. 10 each. Now the best discount combination is two applications of Discount 2. All 4 items would have 20% off each. The transaction total will be Rs. 48 which is net 20% off the total of Rs. 60. (Rs. 16, Rs. 16, Rs. 8, Rs. 8). Discount 1 would be only 16.6% off. (Rs. 10 / Rs. 60).
  3. Finally let’s say two items are Rs. 20 each one item is Rs. 15 and one item is Rs. 5. Now the best discount combination is one application of Discount 2 and one application of Discount 1. Since the effective discount for Discount 2 is always 20% if we choose two items that eliminates the least effective combinations for Discount 1 we can maximize the effective discount of Discount 1. The least effective application of Discount 1 is a Rs. 20 item and a Rs. 5 item. So we use these two items for Discount 2. That leaves a Rs. 20 item and a Rs. 15 item. Discount 1 give a slightly better value so we’ll use it. The transaction total is Rs. 47.50 which is 20.8% off the total of Rs. 60. (Rs. 16, Rs. 4, Rs. 20, Rs. 7.5).

To make it easier to visual the 3rd example two tables below showing the effective discount percentage for all possible combinations of items for both Discount 1 and Discount 2. I’ve marked the lowest effective discount in red.
Discount 1 (50%)

 
20
20
15
5
20
25.0%
25.0%
21.4%
10.0%
20
25.0%
25.0%
21.4%
10.0%
15
21.4%
21.4%
25.0%
12.5%
5
10.0%
10.0%
12.5%
25.0%
 
 
 
 
 

Discount 2 (20%)

 
20
20
15
5
20
20%
20%
20%
20%
20
20%
20%
20%
20%
15
20%
20%
20%
20%
5
20%
20%
20%
20%
 
 
 
 
 

Now an example that falls in between. When prices vary and two or more discount competes the only way to guarantee the best combination of discounts is to evaluate the discounts and compare them.

 
Combination Examples



Continuing the example from the previous section let’s add more items and an additional discount that also requires 2 items to qualify. You’ll see the total number of possible combinations that need to be evaluated very quickly grows beyond what can possibly be calculated quickly.

# of items in the transaction
One 2-item discount can be applied
Two 2-item discounts can be applied
Three 2-item discounts can be applied
 
# of possible pair combinations
# of possible pair combinations
# of possible pair combinations
4
4
8
18
5
15
60
135
6
15
90
315
7
90
540
1890

 

When additional multi-item discounts are applied to a transaction or larger quantities are added to the transaction then the total number of possible discount combinations grows quickly into the millions and the time required to calculate the best possible combination of discounts will quickly become a noticeable amount of time. Some optimizations can be done to reduce the total number of combinations that need to be evaluated, however if the number overlapping discounts or the quantities in the transaction are not limited you will always end up with a very large number of combinations to evaluate whenever you have overlapping multi-item discounts.


Marginal Value


As mentioned at the beginning, to solve this problem we have added an optimization that calculates the value per shared item of each discount on the set of items that can have two or more discounts applied to them.  We refer to this as the Marginal value of the discount for the shared items. The marginal value is the average per item increase in the discount amount when the shared items are included in each discount. Marginal value is calculated by taking the total discount amount (DTotal) and subtracting the discount amount without the shared items (DMinus Shared) and dividing that difference by the number of shared items (ItemsShared).


((DTotal)  -  (DMinus Shared)) / (ItemsShared) = Marginal valueD


 
After we calculate the marginal value of each discount on a shared set of items we then apply the discounts to the shared items in order from highest to lowest marginal value

Microsoft Dynamics AX Retail Modern POS | Adding Customized Content on a Modern POS Receipt

In this blog, I will take you through to add a customized content on the receipt in MPOS.
You can add custom fields in AX and bind it in the code in Modern POS.
  1. Add Custom Fields in AX. This process is same as we used to do for Enterprise POS.
Lets take example of adding barcodes for the items in the receipt. Go to Retail>Setup>Language Text>Click New>Add Barcode.


Now go to Retail>Setup>Custom Fields>Add Barcode. Select Type as Receipt.


Add Barcode in the Lines Section of the Receipt designer.


Push this to the Channel DB.
2. Now We will look upon the Retail SDK.
For any customization in MPOS where we have to get data from the database or save data into database, We need to write a Request Class,Response Class and Handler Class. For all these class code is written on CommerceRuntimeSDK(This is part of RetailSDK).
Open the sdk.SqlserverDataservice project and add classes.
For this particular customization, Request class will be like this:

Response class is like this:

Code for the Handler Class is as follows:
namespace Microsoft.Dynamics.Commerce.Runtime.DataServices
{
public class GetItemBarcodeHandler:IRequestHandler
{
public IEnumerable<Type> SupportedRequestTypes
{
get { yield return typeof(GetItemBarCodeRequest); }
}
public Response Execute(Request request)
{
if (request == null)
{
throw new ArgumentNullException(“request”);
}
Response response = null;
Type reqType = request.GetType();
if (reqType == typeof(GetItemBarCodeRequest))
{
response = GetItemBarcode((GetItemBarCodeRequest)request);
}
else
{
string message = string.Format(CultureInfo.InvariantCulture, “Request ‘{0}’ is not supported.”, reqType);
throw new NotSupportedException(message);
}
return response;
}
private GetItemBarcodeResponse GetItemBarcode(GetItemBarCodeRequest request)
{
ThrowIf.Null(request, “request”);
SqlServerDatabaseContext sqlServerDatabaseContext = new SqlServerDatabaseContext(request.RequestContext);
if (!string.IsNullOrEmpty(request.ItemId) && !string.IsNullOrEmpty(request.InventDimId))
{
return new GetItemBarcodeResponse() { ItemBarcode = GetItemBarcodeforItem(request.ItemId, request.InventDimId, sqlServerDatabaseContext).ToString() };
}
else
{
throw new ArgumentException(“Barcode not available.”);
}
}
private string GetItemBarcodeforItem(string itemId, string inventDimId, SqlServerDatabaseContext sqlServerDatabaseCon)
{
string barCode = string.Empty;
ParameterSet parameters = new ParameterSet();
parameters[“@ItemId”] = itemId;
parameters[“@InventDimId”] = inventDimId;
string selectQuery = @”DECLARE @Count INT
SET @Count=(SELECT COUNT(*) from ax.INVENTITEMBARCODE WHERE INVENTDIMID=@InventDimId and ITEMID=@ItemId)
IF(@Count!=0)
BEGIN
select ITEMBARCODE from ax.INVENTITEMBARCODE WHERE INVENTDIMID=@InventDimId and ITEMID=@ItemId
END
ELSE
BEGIN
SELECT ‘None’ AS ITEMBARCODE
END”;
var sqlQuery = new SqlQuery(selectQuery, parameters);
barCode = sqlServerDatabaseCon.ExecuteScalar<string>(sqlQuery);
Trace.Write(barCode);
return barCode;
}
}
}
Now Come to the sdk.services project and open Receipt folder. There is a class called ReceiptService.cs inside the folder.
Add the below mentioned code in the class:
private static string GetItemBarcode(string itemId, string inventDimId, RequestContext context)
{
string returnValues = string.Empty;
ChannelDataManager channelDataManager = new ChannelDataManager(context);
long ChannelId = channelDataManager.Context.GetPrincipal().ChannelId;
GetItemBarcodeHandler obj = new GetItemBarcodeHandler();
var request = new GetItemBarCodeRequest(itemId, inventDimId);
GetItemBarcodeResponse response = context.Execute<GetItemBarcodeResponse>(request);
if (response != null)
{
returnValues = response.ItemBarcode;
}
return returnValues;
}
Goto GetinfofromSalelineItem method and write below mentioned code as show in the image:

Build the project and put the dll in the retail server bin folder and Run MPOS.

Barcode shown in the Receipt.



Wikipedia

Search results

Contact Form

Name

Email *

Message *