如何在pos中继承models.js并进行一些更改?

时间:2018-02-13 09:54:09

标签: javascript inheritance odoo odoo-10 point-of-sale

models_extend.js

 odoo.define('pos_ticket.models_extend', function (require) {
    "use strict";

    var x = require('point_of_sale.models');
    var models = pos_model.PosModel.prototype.models;

    models.push(
            {
             model:  'res.company',
            fields: [ 'currency_id', 'email', 'website', 'company_registry', 'vat', 'name', 'phone', 'partner_id' , 'country_id', 'tax_calculation_rounding_method','city','trn_no'],
            ids:    function(self){ return [self.user.company_id[0]]; },
            loaded: function(self,companies){ self.company = companies[0]; },
            },

            {
             model:  'product.product',
             fields: ['display_name', 'list_price','price','pos_categ_id', 'taxes_id', 'barcode', 'default_code', 
                     'to_weight', 'uom_id', 'description_sale', 'description',
                     'product_tmpl_id','tracking','arb'],
            order:  ['sequence','default_code','name'],
            domain: [['sale_ok','=',true],['available_in_pos','=',true]],
            context: function(self){ return { pricelist: self.pricelist.id, display_default_code: false }; },
            loaded: function(self, products){
                self.db.add_products(products);
            },
            },
            {
            model:  'product.product',
            fields: ['display_name', 'list_price','price','pos_categ_id', 'taxes_id', 'barcode', 'default_code', 
                     'to_weight', 'uom_id', 'description_sale', 'description',
                     'product_tmpl_id','tracking','arb'],
            order:  ['sequence','default_code','name'],
            domain: [['sale_ok','=',true],['available_in_pos','=',true]],
            context: function(self){ return { pricelist: self.pricelist.id, display_default_code: false }; },
            loaded: function(self, products){
                self.db.add_products(products);
                },
            }
            );

    x.Order = x.Order.extend({

            export_for_printing: function(){
            var self = this;
            this.pos = options.pos;
            var company = this.pos.company;

            var receipt = {

                company:{
                    city:company.city,
                    trn_no:company.trn_no,
                }

            }

            return receipt;
        },
    });

我想在res.company中添加city和trn_no,在product.product中添加arb以查看阿拉伯语翻译。然后我才能及时提交我的项目,我真的被困了请帮助我。我是一名实习生。 要在models.js中必要的POS模块中添加新字段,请覆盖我们从“point_of_sale.models”获取的父模型中的PosModel。

经过一些改变

odoo.define('pos_ticket.models_extend', function (require) {
"use strict";

var x = require('point_of_sale.models');

var _super = x.PosModel.prototype;
    module.PosModel = x.PosModel.extend({
     initialize: function (session, attributes) {
        // call super to set all properties
        _super.initialize.apply(this, arguments);
        // here i can access the models list like this and add an element.
        this.models.push(
        {
        // load allowed users
            model:  'res.company',
            fields: ['city','trn_no'],
            domain: function(self){ return [['id','in',self.users.company_id]]; },
            loaded: function(self,companies){
                console.log(companies);
                self.allowed_users = companies;
         }
         },{
        model:  'product.product',
        fields: ['arb'],
        order:  ['sequence','default_code','name'],
        domain: [['sale_ok','=',true],['available_in_pos','=',true]],
        context: function(self){ return { pricelist: self.pricelist.id, display_default_code: false }; },
        loaded: function(self, products){
            self.db.add_products(products);
        }
    },
    )
      return this;
  }
});


});

现在我需要继承另一个名为" export_for_printing"的函数。并在其中添加这些新字段,以便我可以打印这些字段。

1 个答案:

答案 0 :(得分:0)

只需将修改添加到self.models数组即可。这适用于版本8.也许你需要调整它:

if (typeof jQuery === 'undefined') { throw new Error('Product multi POS needs jQuery'); }

+function ($) {
    'use strict';

    openerp.your_module_name = function(instance, module) {
        var PosModelParent = instance.point_of_sale.PosModel;
        instance.point_of_sale.PosModel = instance.point_of_sale.PosModel.extend({
            load_server_data: function(){
                var self = this;

                self.models.forEach(function(elem) {
                    if (elem.model == 'res.company') {
                        elem.fields = // make your new assignations here
                        elem.domain = // ...
                        elem.loaded = // ...
                    } else if (elem.model == 'product.product') {
                        // [...]
                    }
                })

                var loaded = PosModelParent.prototype.load_server_data.apply(this, arguments);
                return loaded;
            },

        });
    }
}(jQuery);