覆盖已经通过override添加的JS函数

问题描述 投票:0回答:2

我正在尝试重写 Odoo 15 JS 函数,该函数已通过 Odoo 源代码中的 override 添加。

我说的是“_getLinesToAdd”函数 这是由“point_of_sale.models”(models.Order)中的“pos_coupon”模块添加的

这是 Odoo 源代码:

    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;
                    });
            },
    
    });

我只是想继承并修改它,我不是添加它,我想在代码中更改。

我试过了:

    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;

            },
        });
    });

我想要做的是创建 _checkValidityWithBackend 函数,运行一个 rpc 查询,获取参数(我会处理这个)并从 Python 返回 True 或 False 如果 _checkValidityWithBackend 返回 True,那么我将返回该行;表示执行_getLinesToAdd 否则我不会退线。

_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;
    },

现在我只是使用 console.log 进行测试,以确保它考虑到我的继承,但事实并非如此......

odoo point-of-sale
2个回答
1
投票

在自定义模块的清单中,将

pos_coupon
写入
depends
中作为条目。然后在你的 static/js/custom_file.js 中:

import 'pos_coupon.pos';
models.PosModel.include({...

0
投票

您需要将

pos_coupon
模块添加到模块
depends
条目中,因为
_getLinesToAdd
函数是由
pos_coupon
模块定义的。

© www.soinside.com 2019 - 2024. All rights reserved.