Search code examples
pythonodoo-13

How to override the models in models.js in pos odoo13


How to add a domain to the following model and load it at the point of sale startup.

{
    model:  'res.partner',
    label: 'load_partners',
    fields: ['name','street','city','state_id','country_id','vat',
             'phone','zip','mobile','email','barcode','write_date',
             'property_account_position_id','property_product_pricelist'],
    loaded: function(self,partners){
        self.partners = partners;
        self.db.add_partners(partners);
    },
}

Solution

  • Use load_models function of point_of_sale.models to load res.partner model, (You can find many models using domain in the original models file):

    odoo.define('my_module.partners', function (require) {
    "use strict";
    
        var models = require('point_of_sale.models');
    
        models.load_models([{
            model:  'res.partner',
            fields: ['name','street','city','state_id','country_id','vat', 'phone','zip','mobile','email','barcode','write_date', 'property_account_position_id','property_product_pricelist'],
            domain: function(self){ return [['company_id', '=', self.config.company_id[0]]]; },
            loaded: function(self, partner) {}
        }]);
    });
    

    domain: [domain|function] the domain that determines what models need to be loaded. Null loads everything.

    Edit:

    You can access models using models.PosModel.prototype.models, you will also need to override prepare_new_partners_domain method.

    Try the following example:

    var _super_pos_model = models.PosModel.prototype;
    var _models = models.PosModel.prototype.models;
    
    var _domain = [['id', '<=', 3]];
    // partner model is the fifth element in models (index==4)
    _models[4]['domain']  = function(self){ return _domain; };
    
    models.PosModel = models.PosModel.extend({
         prepare_new_partners_domain: function(){
            var domain = _super_pos_model.prepare_new_partners_domain.apply(this, arguments);
            domain.push(..._domain);
            console.log("domain", domain);
            return domain;
        },
    });
    
    console.log("models", models.PosModel.prototype.models);