add some validation in odoo 17 point_of_sale set_discount function in file models.js

166 views Asked by At

in odoo 17 pos, there is a function named set_discount in models.js file , i want to add extra validation in it like this :

set_discount(discount) {
    var parsed_discount =
        typeof discount === "number"
            ? discount
            : isNaN(parseFloat(discount))
            ? 0
            : oParseFloat("" + discount);
    var disc = Math.min(Math.max(parsed_discount || 0, 0), 100);this.product.id });
    
    //here i want to get max discount from product.product
    
    //var maxDiscount = this.product.maxDiscount;
    //if(disc > maxDiscount){
        //alert("discount > max discount");
        //return;
    //}

    this.discount = disc;
    this.discountStr = "" + disc;
}

how can i do this (var maxDiscount = this.product.maxDiscount;) in odoo 17 js

1

There are 1 answers

6
Kenly On BEST ANSWER

You can  patch the orderline component

Example:

/* @odoo-modules */

import { Orderline } from "@point_of_sale/app/store/models";
import { patch } from "@web/core/utils/patch";

patch(Orderline.prototype, {
    set_discount(discount) {
        var self = this;
        super.set_discount(discount);
        var maxDiscount = self.product.max_discount;
        if(self.discount > maxDiscount){
            self.discount = 0.0;
            self.discountStr = "";
        }
    }
})

You need to click the Backspace button to undo the last operation.

Add the js file under assets as follows:

'assets': {
    'point_of_sale._assets_pos': [
        'MODULE_NAME/static/src/**/*',
    ],
},

You can use the pos popup to show notifications to users

Example:

self.env.services.popup.add(ErrorPopup, {
    title: _t("Invalid discount"),
    body: _t("You are not allowed to set a discount ..."),
});

Edit:

Override the _loader_params_product_product function to force Odoo to set the value of the product max discount

Example:

class PosSession(models.Model):
    _inherit = 'pos.session'

    def _loader_params_product_product(self):
        res = super()._loader_params_product_product()
        res['search_params']['fields'].append('max_discount')
        return res

Use the same field name (this.product.max_discount) in js code