Retail:Developers Guide/How-to/How to implement a new Discount and Promotion Type
Contents |
Introduction
This document aims to explain how to implement a Discount or Promotion Type to be used in Web POS. It is based on the how to that implements the same type to be used in backend which needs to be done before this one.
![]() | Note that Discounts and Promotions types that are going to be used in both places: back office and Web POS require to implement their logic in both sides. |
JavaScript Implementation
// 0. Register the rule OB.Model.Discounts.registerRule('E08EE3C23EBA49358A881EF06C139D63', { async: false, implementation: function (discountRule, receipt, line) { var alerts, qty, x, y, mod, chunks, price, finalPrice; // 1. Obtain information about how the rule is configured x = discountRule.get('oBDISCX'); y = discountRule.get('oBDISCY'); // 2. Obtain information about the line qty = line.get('qty'); // 3. Check if the rule can be applied and show alert if the discount is // partially applied mod = qty % x; if (mod !== 0) { alerts = OB.I18N.getLabel('OBDISC_AlertXYSameProduct', [x - mod, line.get('product').get('_identifier'), discountRule.get('printName') || discountRule.get('name')]); } if (qty >= x) { // 4. Apply the discount chunks = Math.floor(qty / x); price = line.get('discountedLinePrice') || line.get('price'); finalPrice = OB.DEC.add(OB.DEC.mul(OB.DEC.mul(chunks, y), price), OB.DEC.mul(mod, price)); receipt.addPromotion(line, discountRule, { amt: OB.DEC.sub(OB.DEC.mul(qty, price), finalPrice) }); } // 5. alerts can be returned return { alerts: alerts }; } });
Code explanation
0. Registering the Rule
OB.Model.Discounts.registerRule('RuleID', { implementation: function (discountRule, receipt, line) { // code here } });
Discounts and Promotions Types must be registered. To do so OB.Model.Discounts.registerRule
function is used with the following parameters:
ruleID
. It is the id of the record that defines the rule in Discounts and Promotions Type window.implementation object
. Is is the JavaScript object with the code that implements the rule. It must have a property namedimplementation
which is a function with the following parameters:discountRule
. It is an object that represents the instance of Discount rule that is being applied.receipt
. The complete receipt. It is intended to be used in case the rule needs to look not only to current line but also the rest of them in the ticket.line
. Current line that is being checked.
1. Obtain information about the Discount
Configuration of the Discount rule that is checked can be obtained from discountRule
object. In this case, we get actual values for x and y'.
2. Obtain information about the line
Line being checked is in line
object. In this case the only relevant information is qty
3. Check the rule and show alerts
At this point, it is checked whether the rule can be applied to current line. Web POS also supports to prompt an alert, this is accomplished by returning an object with an string property named alert
.
4. Apply the Discount
When finally the discount needs to be applied, addPromotion
method in receipt
object is used. The parameters it accepts are:
line
. The line the discount is applied to.discountRule
. The rule to apply.discount
. A JavaScript object with the actual discount information. Main attribute of this object isamt
which represents the amount to be discounted from price.
Testing
Here you can find how to test new discount types.
When this code is executed
Similarly to back office. The algorithm that determines when this code is invoked, is triggered by modifications in the ticket or lines. When this occurs, Discount and Promotions candidates for each line are checked, and the code implementing the rule for each candidate is called.