Simon Capriles
Simon Capriles

Reputation: 183

How to add data to Odoo13's POS order

I'm trying to add data to the POS's order and send that data to the 'pos.order' model based on the site https://odoo-development.readthedocs.io/en/latest/dev/pos/load-data-to-pos.html. To make my case more generic I'm creating a new odoo model named 'custom.model', I'm creating a relation with the 'pos.config' to help me with the model domain in javascritp latter with the following code:

# -*- coding: utf-8 -*-

from odoo import models, fields

class custom_model(models.Model):
    _name = 'custom.model'

    name = fields.Char(string='name')

class myPosConfig(models.Model):
    _inherit = 'pos.config'

    custom_model_id = fields.Many2one('custom.model', string='My custom model')

Then I add the relation of my interest in the 'pos.order' model with the following python code:

# -*- coding: utf-8 -*-

from odoo import models, fields, api


class myPosOrder(models.Model):
    _inherit = 'pos.order'

    custom_model_id = fields.Many2one('custom.model', string='My model')

Then I add my custom model in the frontend with a javascript file with the following code:

odoo.define('kyohei_pos_computerized_billing.billing_dosage', function (require) {
    "use strict";

    var models = require('point_of_sale.models');
    var _super_order_model = models.Order.prototype;

    models.load_models([{
        model: 'custom.model',
        label: 'custom_model',
        fields: ['name'],
        // Domain to recover the custom.model record related to my pos.config
        domain: function(self){ return [['id', '=', self.config.custom_model_id[0]]];},
        loaded: function(self, dosage){self.dosage = dosage[0]},
    }]);

});

Then I add the following code to the same javascript file, so the record is stored in the browser and when needed to send the data to the backend:

    models.Order = models.Order.extend({
        initialize: function(){
          _super_order_model.initialize.apply(this,arguments);
          if (this.custom_model){
              this.custom_model = this.pos.custom_model;
          }
        },

        export_as_JSON: function () {
            var data = _super_order_model.export_as_JSON.apply(this, arguments);
            data.custom_model = this.custom_model;
            return data
        },

        init_from_JSON: function (json) {
            this.custom_model = json.custom_model;
            _super_order_model.init_from_JSON.call(this. json);
        },

        export_for_printing: function() {
        var json = _super_order_model.export_for_printing.apply(this,arguments);
        json.custom_model = this.custom_model;
        return json;
        },

    });

and finally added the following method to the 'pos.order' model so it stores what the frontend sends:

    @api.model
    def _order_fields(self, ui_order):
        fields = super(MyPosOrder, self)._order_fields(ui_order)

        fields.update({
            'custom_model': ui_order('custom_model.id')
        })

        return fields

But the field still isn't being filled with my custom_model's registry id, and I get the following error:

Odoo Server Error
Traceback (most recent call last):
  File "/opt/odoo/odoo13/odoo/http.py", line 619, in _handle_exception
    return super(JsonRequest, self)._handle_exception(exception)
  File "/opt/odoo/odoo13/odoo/http.py", line 309, in _handle_exception
    raise pycompat.reraise(type(exception), exception, sys.exc_info()[2])
  File "/opt/odoo/odoo13/odoo/tools/pycompat.py", line 14, in reraise
    raise value
  File "/opt/odoo/odoo13/odoo/http.py", line 664, in dispatch
    result = self._call_function(**self.params)
  File "/opt/odoo/odoo13/odoo/http.py", line 345, in _call_function
    return checked_call(self.db, *args, **kwargs)
  File "/opt/odoo/odoo13/odoo/service/model.py", line 93, in wrapper
    return f(dbname, *args, **kwargs)
  File "/opt/odoo/odoo13/odoo/http.py", line 338, in checked_call
    result = self.endpoint(*a, **kw)
  File "/opt/odoo/odoo13/odoo/http.py", line 910, in __call__
    return self.method(*args, **kw)
  File "/opt/odoo/odoo13/odoo/http.py", line 510, in response_wrap
    response = f(*args, **kw)
  File "/opt/odoo/odoo13/addons/web/controllers/main.py", line 1320, in call_kw
    return self._call_kw(model, method, args, kwargs)
  File "/opt/odoo/odoo13/addons/web/controllers/main.py", line 1312, in _call_kw
    return call_kw(request.env[model], method, args, kwargs)
  File "/opt/odoo/odoo13/odoo/api.py", line 383, in call_kw
    result = _call_kw_model(method, model, args, kwargs)
  File "/opt/odoo/odoo13/odoo/api.py", line 356, in _call_kw_model
    result = method(recs, *args, **kwargs)
  File "/opt/odoo/odoo13/addons/point_of_sale/models/pos_order.py", line 440, in create_from_ui
    order_ids.append(self._process_order(order, draft, existing_order))
  File "/opt/odoo/odoo13/addons/point_of_sale/models/pos_order.py", line 122, in _process_order
    pos_order = self.create(self._order_fields(order))
  File "/opt/odoo/odoo13/kyohei_addons/kyohei_pos_computerized_billing/models/pos_order.py", line 27, in _order_fields
    'test_string': ui_order('dosage.id'),
TypeError: 'dict' object is not callable

Upvotes: 1

Views: 1724

Answers (2)

Simon Capriles
Simon Capriles

Reputation: 183

I want to thank @Dipen Shah, finally got the code working. The python file should look like the following:

# -*- coding: utf-8 -*-

from odoo import models, fields, api


class MyPosOrder(models.Model):
    _inherit = 'pos.order'

    test_string = fields.Char(string='test_string')

    @api.model
    def _order_fields(self, ui_order):
        order_fields = super(MyPosOrder, self)._order_fields(ui_order)

        order_fields['test_string'] = ui_order.get('test_string')

        return order_fields

The file that could help to understand this issue is the pos_restaurant's pos_order.py

Upvotes: 0

Dipen Shah
Dipen Shah

Reputation: 2444

This error due to of the argument mismatch on the method, just check on the odoo-13 this method _process_order

In your code, you were using the older version method and from the odoo13 version, it is changed.

You have to update the field in this method, where the data came from the export_as_JSON function.

@api.model
def _order_fields(self, ui_order):
    pos_order = super(KyoheiComputerizedPosOrder, self)._order_fields(ui_order)
    # Get the data from ui_order  
    return pos_order

Thanks

Upvotes: 1

Related Questions