You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Odoo 17 POS自定义is_void字段收据显示异常及RPC报错求助

Odoo 17 POS自定义字段问题

我在Odoo 17 POS的pos.order.line模型中新增了布尔字段is_void,该字段能正常写入数据库,但存在两个问题:

  1. 打印收据时,该字段显示为undefined
  2. 订单保存到服务器时触发RPC错误
    尝试过load_params方法,但该方案仅适用于预定义字段,无法满足订单创建时动态设置值的需求。

附上当前的Python和JavaScript代码,以及报错信息:

Python 代码

from odoo import models, fields


class PosOrderLine(models.Model):
    _inherit = "pos.order.line"

    is_void = fields.Boolean(
        string="Is Void",
        default=False,
        help="Indicates whether this line is voided or not.",
    )

    def _order_line_fields(self, line, session_id=None):
        result = super(PosOrderLine, self)._order_line_fields(line, session_id)
        result["is_void"] = line.get("is_void", False)
        return result

    def _export_for_ui(self, orderline):
        result = super()._export_for_ui(orderline)
        result.update(
            {
                "is_void": orderline.is_void,
            }
        )
        return result

JavaScript 代码

patch(Orderline.prototype, {
  constructor() {
    this.is_void = this.is_void || false;
  },

  // Toggle the is_void flag
  toggleVoid() {
    this.is_void = !this.is_void;
  },

  // Include is_void in export_as_JSON
  export_as_JSON() {
    const result = super.export_as_JSON(...arguments);
    result["is_void"] = this.is_void;
    return result;
  },

  init_from_JSON(json) {
    super.init_from_JSON(...arguments);
    this.is_void = json.is_void || false;
  },

  export_for_printing() {
    const json = this._super(...arguments);
    json.is_void = this.is_void;
    return json;
  },
});

报错信息

point_of_sale.assets_prod.min.js:9701 Failed to send orders: [{…}]
_save_to_server @ point_of_sale.assets_prod.min.js:9701
RPC_ERROR://localhost:8069/web/assets/7b7dc0e/point_of_sale.assets_prod.min.js:6586:163)
    at XMLHttpRequest.<anonymous> (http://localhost:8069/web/assets/7b7dc0e/point_of_sale.assets_prod.min.js:6590:13)

解决方案

1. 修复RPC错误问题

RPC错误的核心原因是Odoo 17 POS同步订单时,默认不会传递自定义字段到服务器的订单行创建逻辑。需要确保Python层正确接收并处理字段值:

  • 修改_order_line_fields方法,强制转换为布尔值,避免类型不匹配:
def _order_line_fields(self, line, session_id=None):
    result = super()._order_line_fields(line, session_id)
    # 确保字段值为标准布尔类型
    result["is_void"] = bool(line.get("is_void", False))
    return result

2. 修复收据显示undefined问题

收据显示异常是因为打印模板未正确获取字段,或JS的打印导出方法存在调用错误:

  • 修正JS的export_for_printing方法:Odoo patch机制中需使用super.export_for_printing而非this._super:
export_for_printing() {
  const json = super.export_for_printing(...arguments);
  json.is_void = this.is_void || false;
  return json;
},
  • 调整打印模板:在POS收据模板(如pos_receipt)中添加字段判断,避免undefined显示:
<!-- 订单行区域添加示例 -->
<t t-if="line.is_void">
    <span class="text-danger">已作废</span>
</t>

3. 验证步骤

  • 清除浏览器缓存和Odoo资产缓存(设置>技术>资产>清除缓存)
  • 打开POS开发者工具,检查订单行JSON数据是否包含is_void字段
  • 查看Odoo服务器日志(odoo.log),获取更详细的RPC错误详情

内容的提问来源于Stack Exchange,提问作者Kareem Magdy El-Zomor

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 10:05:18