簡體   English   中英

如何向 Odoo13 的 POS 訂單添加數據

[英]How to add data to Odoo13's POS order

我正在嘗試將數據添加到 POS 的訂單並將該數據發送到基於站點https://odoo-development.readthedocs.io/en/latest/dev/pos/load-data 的“pos.order”model -to-pos.html 為了使我的案例更通用,我正在創建一個名為“custom.model”的新 odoo model,我正在創建與“pos.config”的關系,以幫助我使用 javascritp 后者中的 model 域,代碼如下:

# -*- 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')

然后我在“pos.order”model 中添加我感興趣的關系,並使用以下 python 代碼:

# -*- 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')

然后我在前端添加了我的自定義 model 和一個 javascript 文件,代碼如下:

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

});

然后我將以下代碼添加到同一個 javascript 文件中,因此記錄存儲在瀏覽器中,並在需要時將數據發送到后端:

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

    });

最后將以下方法添加到 'pos.order' model 以便它存儲前端發送的內容:

    @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

但是該字段仍未填充我的 custom_model 的注冊表 ID,並且我收到以下錯誤:

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

由於方法上的參數不匹配導致此錯誤,只需檢查 odoo-13 此方法_process_order

在您的代碼中,您使用的是舊版本方法,並且從 odoo13 版本開始,它已更改。

您必須更新此方法中的字段,其中數據來自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

謝謝

我要感謝@Dipen Shah,終於讓代碼工作了。 python 文件應如下所示:

# -*- 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

可以幫助理解這個問題的文件是 pos_restaurant 的 pos_order.py

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM