Search code examples
odoopoint-of-sale

Override JS function which is already added by override


I am trying to override an Odoo 15 JS function, which is already added by override in Odoo source code.

I am speaking about "_getLinesToAdd" function which is added by "pos_coupon" module in "point_of_sale.models" (models.Order)

This is the Odoo source code :

    odoo.define('pos_coupon.pos', function (require) {
        'use strict';
    const models = require('point_of_sale.models');

    class RewardsContainer { 
        // code 
    }
    
    var _order_super = models.Order.prototype;
    models.Order = models.Order.extend({

    // Other code block

    // _getLinesToAdd is called here (added also by pos_coupon module)
    _getNewRewardLines: async function () {
            // Remove the reward lines before recalculation of rewards.
            this.orderlines.remove(this._getRewardLines());
            const rewardsContainer = await this._calculateRewards();
            // We set the programs that will generate coupons after validation of this order.
            // See `_postPushOrderResolve` in the `PaymentScreen`.
            await this._setProgramIdsToGenerateCoupons(rewardsContainer);
            // Create reward orderlines here based on the content of `rewardsContainer` field.
            return [this._getLinesToAdd(rewardsContainer), rewardsContainer];
        },


    // This function is newly added by pos_coupon to models.Order
    _getLinesToAdd: function (rewardsContainer) {
                this.assert_editable();
                return rewardsContainer
                    .getAwarded()
                    .map(({ product, unit_price, quantity, program, tax_ids, coupon_id }) => {
                        let description;
                        /**
                         * Improved description only aplicable for rewards of type discount, and the discount is a percentage
                         * of the price, those are:
                         * - % discount on specific products.
                         * - % discount on the whole order.
                         * - % discount on the cheapest product.
                         */
                        if (tax_ids && program.reward_type === "discount" && program.discount_type === "percentage") {
                            description =
                                tax_ids.length > 0
                                    ? _.str.sprintf(
                                        this.pos.env._t("Tax: %s"),
                                        tax_ids.map((tax_id) => `%${this.pos.taxes_by_id[tax_id].amount}`).join(", ")
                                    )
                                    : this.pos.env._t("No tax");
                        }
                        const options = {
                            description,
                            quantity: quantity,
                            price: unit_price,
                            lst_price: unit_price,
                            is_program_reward: true,
                            program_id: program.id,
                            tax_ids: tax_ids,
                            coupon_id: coupon_id,
                        };
                        const line = new models.Orderline({}, { pos: this.pos, order: this, product });
                        this.fix_tax_included_price(line);
                        this.set_orderline_options(line, options);
                        return line;
                    });
            },
    
    });

I just want to inherit and make modification in it, I'm not ADDING to it, I want to change in the code.

I tried :

    odoo.define('appointment_discount_card.models', function (require) {
        'use strict';
    
        const models = require('point_of_sale.models');
        const rpc = require('web.rpc');
    
        var _super_order = models.Order.prototype;
        models.Order = models.Order.extend({
            _getLinesToAdd: function (rewardsContainer) {
            console.log('in !!!'); // It's not showing this message in the log
                this.assert_editable();
                return rewardsContainer
                        // Rest of code
                        // I want to apply a test here before returning the line ( Run _checkValidityWithBackend )
                         return line;

            },
        });
    });

What I want to do is creating _checkValidityWithBackend function running an rpc query that gets parameters (I will take care of that) and returns from Python True or False If _checkValidityWithBackend returns True, then I will return the line; means execute _getLinesToAdd else, I will not return the line.

_checkValidityWithBackend: async function (origin, productId) {
        const { successful, payload } = await rpc.query({
            model: 'my.model.name',  // Replace with your actual model name
            method: 'check_validity',
            args: [origin, productId],
            kwargs: { context: session.user_context },
        });

        return successful && payload.valid;
    },

Now I am just testing with console.log to ensure that it takes in consideration my inheritance, but it's not...


Solution

  • You need to add the pos_coupon module to your module depends entry, as the _getLinesToAdd function is defined by the pos_coupon module.