Skip to content

[FIX] point_of_sale: Avoid transaction aborted errors when handling failed orders #27932

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: 16.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion addons/point_of_sale/models/pos_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -2115,6 +2115,7 @@ def _is_capture_system_activated(self):
return str2bool(self.env['ir.config_parameter'].sudo().get_param('point_of_sale.capture_unprocessed_order', True))

def _handle_order_process_fail(self, order: dict, exception: Exception, draft: bool):
self.env.cr.rollback() # It would have rollback anyway as it was raising an exception
if not self._is_capture_system_activated():
return

Expand All @@ -2125,7 +2126,6 @@ def _handle_order_process_fail(self, order: dict, exception: Exception, draft: b
_logger.info("order '%s' was not captured as it is draft", order['data']['name'])
return

self.env.cr.rollback() # It would have rollback anyway as it was raising an exception
self.sudo()._process_order_process_fail(order, exception, self.env.user.id)
self.env.cr.commit() # Make sure that our created records are stored

Expand Down
26 changes: 26 additions & 0 deletions addons/point_of_sale/tests/test_pos_capture.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import logging
from unittest.mock import patch

import psycopg2

import odoo
from odoo.addons.point_of_sale.models.pos_order import PosOrder
from odoo.addons.point_of_sale.models.pos_session import PosSession
Expand Down Expand Up @@ -171,3 +173,27 @@ def test_capture_two_orders_and_removed(self):
self.env['pos.order'].create_from_ui(order1)
# Should automatically remove the attachment for this order after sync
self.assert_activity_and_attachment(session, 0)

def test_capture_db_error(self):
# open a session
session = self.open_new_session()

orders = [self.create_ui_order_data([(self.product1, 1)])]

def mocked_process_order(*args):
""" force a database error """
self.env.cr.execute("select __non_existing_field__ from pos_order")

def mocked_handle_order_process_fail(*args):
""" acces db to check if transaction is still usable"""
self.env.cr.execute("select 1")
self.assertEqual(self.cr.fetchone()[0], 1)

with patch.object(PosOrder, '_process_order', mocked_process_order),\
patch.object(PosSession, '_handle_order_process_fail', mocked_handle_order_process_fail),\
odoo.tools.misc.mute_logger("odoo.sql_db"),\
self.assertLogs('odoo.addons.point_of_sale.models.pos_order', level=logging.ERROR):
try:
self.env['pos.order'].create_from_ui(orders)
except psycopg2.DatabaseError:
pass
1 change: 1 addition & 0 deletions doc/cla/corporate/braintec.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ Frédéric Garbely frederic.garbely@braintec.com https://github.com/BT-fgarbely
Carlos Serra Toro carlos.serra@braintec.com https://github.com/BT-cserra
Cristian Rodriguez Navarro cristian.rodriguez@braintec.com https://github.com/BT-crodriguez
Olivier Jossen olivier.jossen@braintec.com https://github.com/BT-ojossen
Simon Schmid simon.schmid@braintec.com https://github.com/BT-sschmid