Develop > Controller layer > Application developer > Calculation framework > Examples: Applying the calculation framework
Example: Discounts
This example shows how you can implement a discount using the calculation framework. You can also implement discounts using WebSphere Commerce Accelerator.
Example description
Your store wants to offer a discount on the Books product category for a limited time. The time period is from StartDate to EndDate. The discount offers customers a 15.00 discount on their orders when the value before taxes of products from the Books category is 50.00 or greater.
The currency of the order is the local currency of the country or region where the store is located.
The discount does not affect any tax calculations. Taxes are calculated using order totals before the discount. This example does not cover the implementation of these tax calculations.
All store customers are eligible for this discount.
Discounts example implementation
To use the calculation framework to calculate shipping charges for this example...
- Define a calculation code.
- Define a calculation rule.
- Define a calculation scale.
- Define the calculation ranges.
- Define the look-up results for the calculation ranges.
- Associate the calculation scales with the calculation rules.
- Attach the calculation code with the Books product category catalog entry.
Important: Text identifiers are used in the following sections to make it easier to see the relationships between components.
Text identifiers are often not valid in the WebSphere Commerce database. You should convert the text identifiers to integers or allow WebSphere Commerce to generate the identifiers for you.
Define discount calculation codes
One calculation code needs to be defined in this example because the discount in this example is independent of other discounts that the store might be offering. Also, the discount only applies to one product category. The CalculationCodeQualify calculation method that must be specified as part of a calculation code does not need to be invoked in this example, so the restriction flag is not set.
Here are the key properties required for the calculation code define:
Property Value Description of value Group method 0 The calculation code will be attached to only one catalog group (Books). Grouping products is useful when the calculation code will be attached to multiple parent products. Qualification flag 0 The CalculationCodeQualify calculation method specified for this calculation code will not be invoked Identify string Book Discount Promotion This is a character string that uniquely identifies this calculation code, given a particular calculation usage and store or store group. Published 1 The calculation code is active Start date StartDate This calculation code starts being active on StartDate End date EndDate This calculation code stops being active on EndDate Calculation usage ID -1 This is the ID for discount calculation usage CalculationCodeQualify calculation method -2 This is the ID for CalculationCodeQualifyCmd CalculationCodeApply calculation method -4 This is the ID for DiscountCalculationCodeApplyCmd CalculationCodeCalculate calculation method -3 This is the ID for CalculationCodeCalculateCmd
Define discount calculation rules
You need one calculation rule to calculate this discount.
Here are the key properties for the calculation rule define:
Property Value Description of value Calculation code ID BookDiscCode This is the discount calculation code that was defined earlier Identify number 1 This is a number that, along with the calculation code ID, uniquely identifies this calculation rule. Qualification flag 0 The CalculationRuleQualify calculation method that you specify for this calculation rule will not be invoked. Combination 2 The results of this calculation rule can be combined with other calculation rules that have a 0 or 2 combination property. In this example, the value of the combination property has no effect because only one calculation rule is being defined for this calculation code CalculationRuleQualify calculation method -6 Calculation method ID for DiscountCalculationRuleQualifyCmd CalculationRuleCalculate calculation method -7 Calculation method ID for CalculationRuleCalculateCmd
Define discount calculation scales
The discount for this example can be expressed as one calculation scale, as this table shows:
Value of Book OrderItems Fixed amount discount less than 50.00 0.00 50.00 or greater 15.00
This table provides hints about how calculation scales and calculation ranges will be implemented:
- "Value of Book OrderItems" indicates that our CalculationScaleLookup calculation method will use the sum of the prices of all Book OrderItems.
- "Fixed amount discount" indicates that our CalculationRange calculation method will return a fixed amount.
- There are two calculation ranges in this calculation scale ("less than 50.00" and "50.00 or greater").
- There are two look-up results for this calculation scale ("0.00" and "15.00").
The calculation scale should be associated with a currency since the look-up number is a monetary amount. The currency will not be indicated in this example, but it is required when implementing this example in WebSphere Commerce.
You should define one scale for each currency that the store supports. In this example, the store supports only one currency, so we will define only one scale.
The following tables indicate the key values for the calculation scales:
Property Value Description of value Calculation usage -1 Discount calculation usage Identify string Book Discount Promotion This is a character string that uniquely identifies this calculation scale, given a particular calculation usage and store or store group. CalculationScaleLookup calculation method -10 NonDiscountedPriceCalculationScaleLookupCmd (the look-up number is the sum of the non-discounted prices of the Book OrderItems)
Define discount calculation ranges
The discount has two calculation ranges for the calculation scale. The calculation ranges are:
- value of Book OrderItems less than 50.00
- value of Book OrderItems 50.00 or greater
The calculation ranges are not cumulative.
Here are the key properties that define for each calculation range:
Table 1. Book0to50Range calculation range properties Property Value Description of value Calculation scale ID BookDiscountScale Calculation scale defined earlier Calculation range starting value 0.00 Calculation range applies when the sum of non-discounted prices of the OrderItems is greater than or equal to 0.00 Cumulative calculation range indicator 0 Calculation range is non-cumulative. CalculationRange calculation method -13 FixedAmountCalculationRangeCmd (the look-up result is treated as a fixed monetary amount)
Property Value Description of value Calculation scale ID BookDiscountScale Calculation scale defined earlier Calculation range starting value 50.00 Calculation range applies when the sum of non-discounted prices of the OrderItems is greater than or equal to 50.00 Cumulative calculation range indicator 0 Calculation range is non-cumulative CalculationRange calculation method -13 FixedAmountCalculationRangeCmd (the look-up result is treated as a fixed monetary amount)
Define look-up results for discount calculation ranges
Each calculation range needs a set of look-up results. For this example, the look-up results are fixed monetary amounts. Each calculation range has only one value associated with it, so only one look-up result is needed for each range. Each look-up result must be associated with the local currency of the country or region for the store because that is the currency of the shipping charges.
The look-up result values are added to price and other existing monetary amounts that are associated with an order item. For a discount, the look-up result value is a negative number.
The following tables show the key properties that define for each calculation range look-up result. The currency has been omitted here, but it must be included in an actual implementation.
Property Value Description of value Look-up result 0.00 No discount Calculation range ID Book0to50Range The identifier of the calculation range to which this look-up result belongs
Property Value Description of value Look-up result -15.00 15.00 discount Calculation range ID Book50+Range The identifier of the calculation range to which this look-up result belongs
Reminder:
These calculation range look-up result properties are missing the currency for the look-up results. The currency of the look-up results must be defined. For this example, it must match the currency of the calculation scale.
Associating discount calculation scales with discount calculation rules
The calculation scale that was defined must be associated with the defined calculation rule in the CRULESCALE database table. The relationship between the defined calculation scale and the defined calculation rule is shown in the following table:
Calculation scales Calculation rules BookDiscountScale BookDiscRule
Attach discount calculation code
To use the new discount calculation code that you defined earlier, attach the calculation code to the order items. The calculation code will be indirectly attached, because it applies to all items resulting from a customer purchasing a product from the store catalog, not to just one item.
The discount calculation code applies only to Books, so we will attach it to the catalog group for that product category. We will indirectly attach the calculation code by creating an entry in the CATGPCALCD database table.
Populate the CATGPCALCD database table with the following information:
Store ID Catalog group ID Calculation code Store BookCategory BookDiscCalcCode
Related concepts
Calculation scales and calculation ranges
Related reference
Example: Sales tax and shipping tax calculations