View source | Discuss this page | Page history | Printable version   

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.

Bulbgraph.png   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:

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:

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.

Retrieved from "http://wiki.openbravo.com/wiki/Retail:Developers_Guide/How-to/How_to_implement_a_new_Discount_and_Promotion_Type"

This page has been accessed 5,194 times. This page was last modified on 24 October 2022, at 07:16. Content is available under Creative Commons Attribution-ShareAlike 2.5 Spain License.