Skip to Content
Menu
This question has been flagged

I'm using Odoo 17 pos I've added new field called is_void, I want to access this field from the receipt, It comes with undefined (empty), but It sent to the DB Correctly here is my code below:

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;
  },
});
Getting the below error

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. (http://localhost:8069/web/assets/7b7dc0e/point_of_sale.assets_prod.min.js:6590:13)

Also I tried to load_params but this solution works only if the custom field is predefined not defined or set its value during making the order itself.


Avatar
Discard
Best Answer

The error you're encountering while attempting to include the is_void field in the Odoo POS receipt seems to be related to how the field data is being passed and processed between the client-side JavaScript code and the server-side Python code. Here's a breakdown of how to address this issue:

1. Ensure the Field is Properly Initialized

The is_void field needs to be correctly initialized in both the server-side and client-side code. You appear to be doing this in the export_as_JSON and init_from_JSON methods in JavaScript, and in the _order_line_fields and _export_for_ui methods in Python. These are the right places, but let’s ensure that everything is correctly linked.

2. Review the JavaScript Logic

You need to make sure that is_void is being handled correctly when the order is being exported and imported. Since the error happens during order saving, let’s focus on the methods dealing with data persistence:

javascriptCopy code// Extend Orderline with new logic for is_void field
patch(Orderline.prototype, {
  // Constructor logic to initialize is_void
  constructor() {
    this.is_void = this.is_void || false;
  },

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

  // Export data as JSON for backend
  export_as_JSON() {
    const result = super.export_as_JSON(...arguments);
    result["is_void"] = this.is_void;
    return result;
  },

  // Initialize from JSON data received from the backend
  init_from_JSON(json) {
    super.init_from_JSON(...arguments);
    this.is_void = json.is_void || false;
  },

  // Export data for printing (receipt)
  export_for_printing() {
    const json = this._super(...arguments);
    json.is_void = this.is_void;
    return json;
  },
});

3. Review the Python Code for Compatibility

Make sure that the Python methods handling the order lines are correctly updating and retrieving the is_void field:

pythonCopy codefrom 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

4. Check the Field Mapping in the POS Frontend

Ensure that your custom is_void field is correctly referenced in all parts of the code that manage the order data, particularly in the load_params and save_to_server processes.

5. Investigate the Error Message

The error Failed to send orders suggests that there might be an issue with how the order data, including your custom field, is being transmitted to the server. Here’s what to look for:

  • Check the Network Tab: In your browser's developer tools, check the network tab to see the payload of the request being sent to the server. Ensure that is_void is correctly included in the JSON data.
  • Check the Logs: Look at the Odoo server logs for any more detailed error messages that might give you clues.

6. Debug the Save Process

You might need to add some debug logging in the JavaScript and Python methods that handle order saving to identify where the process is failing. For example, log the content of export_as_JSON in the JavaScript to ensure the data is being exported correctly.

7. Odoo Field Initialization Timing

If the field is only set during the order process, make sure that any logic depending on is_void doesn't run before the field is initialized (e.g., during the first render or before the order is saved).

8. Possible Fix for load_params Issue

The load_params issue you mentioned typically arises if the field isn’t recognized during the order’s initial loading. Ensure that the field is part of the pos.order.line model and that any dependencies in load_params correctly refer to it.

Final Step: Testing

After applying these changes, test the functionality again by:

  • Adding a line with is_void set to True or False.
  • Saving the order and ensuring it is correctly stored in the database.
  • Checking the receipt to see if is_void is correctly reflected.

This process should help resolve the issue with your is_void field being undefined during the receipt printing process while ensuring it is correctly stored and retrieved in Odoo POS.

Avatar
Discard
Related Posts Replies Views Activity
0
Mar 25
819
1
Dec 24
1438
0
Dec 24
1112
1
Jan 21
2479
6
Nov 19
14760