Skip to Content
Menu
This question has been flagged

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


Avatar
Discard
Best Answer

Hi,

To Add additional validation in the `set_discount` function in Odoo 17 POS, when using `var maxDiscount = this.product.maxDiscount`, it is essential to import the required classes and an Error Popup to facilitate proper validation. Following this, you can modify the code as outlined below:


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

patch(Orderline.prototype, {
    async 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);
        var maxDiscount = this.product.maxDiscount;
        if (disc > maxDiscount) {
            await this.env.services.popup.add(ErrorPopup, {
                title: _t("Exceed Discount Limit!"),
                body: _t("Sorry, Discount is not allowed. Maximum discount for this Product is %s %", maxDiscount),
            });
            this.discount = 0;
            this.discountStr = "0";
        } else {
            this.discount = disc;
            this.discountStr = "" + disc;
        }
    }


Regards

Avatar
Discard
Related Posts Replies Views Activity
1
Mar 24
1116
2
Jan 20
10540
0
Jul 22
2353
0
Jun 18
3096
1
Dec 24
890