From 24d508f2f123824b0e9a9e4f0f9eac4b8269bbad Mon Sep 17 00:00:00 2001 From: Martin Trigaux Date: Tue, 14 Mar 2017 17:20:45 +0100 Subject: [PATCH] [FIX] point_of_sale: restore rescue field Properly fix #15101 Unstable patch of fix made at #15862 --- addons/point_of_sale/controllers/main.py | 2 +- addons/point_of_sale/models/pos_config.py | 4 ++-- addons/point_of_sale/models/pos_order.py | 6 ++++-- addons/point_of_sale/models/pos_session.py | 8 ++++++-- addons/point_of_sale/tests/test_point_of_sale_flow.py | 1 + 5 files changed, 14 insertions(+), 7 deletions(-) diff --git a/addons/point_of_sale/controllers/main.py b/addons/point_of_sale/controllers/main.py index cd8a1e8e493..5958967cfc5 100644 --- a/addons/point_of_sale/controllers/main.py +++ b/addons/point_of_sale/controllers/main.py @@ -17,7 +17,7 @@ class PosController(http.Controller): pos_sessions = request.env['pos.session'].search([ ('state', '=', 'opened'), ('user_id', '=', request.session.uid), - ('name', 'not like', '(RESCUE FOR')]) + ('rescue', '=', False)]) if not pos_sessions: return werkzeug.utils.redirect('/web#action=point_of_sale.action_client_pos_menu') pos_sessions.login() diff --git a/addons/point_of_sale/models/pos_config.py b/addons/point_of_sale/models/pos_config.py index fc5c4753a3d..2f030103f5d 100644 --- a/addons/point_of_sale/models/pos_config.py +++ b/addons/point_of_sale/models/pos_config.py @@ -148,7 +148,7 @@ class PosConfig(models.Model): for pos_config in self: session = pos_config.session_ids.filtered(lambda r: r.user_id.id == self.env.uid and \ not r.state == 'closed' and \ - '(RESCUE FOR' not in r.name) + not r.rescue) # sessions ordered by id desc pos_config.current_session_id = session and session[0].id or False pos_config.current_session_state = session and session[0].state or False @@ -171,7 +171,7 @@ class PosConfig(models.Model): @api.depends('session_ids') def _compute_current_session_user(self): for pos_config in self: - session = pos_config.session_ids.filtered(lambda s: s.state == 'opened' and '(RESCUE FOR' not in s.name) + session = pos_config.session_ids.filtered(lambda s: s.state == 'opened' and not s.rescue) pos_config.pos_session_username = session and session[0].user_id.name or False @api.constrains('company_id', 'stock_location_id') diff --git a/addons/point_of_sale/models/pos_order.py b/addons/point_of_sale/models/pos_order.py index c958e853f75..78d7c2de296 100644 --- a/addons/point_of_sale/models/pos_order.py +++ b/addons/point_of_sale/models/pos_order.py @@ -67,8 +67,9 @@ class PosOrder(models.Model): order['name'], order['amount_total']) rescue_session = PosSession.search([ - ('name', 'like', '(RESCUE FOR %(session)s)' % {'session': closed_session.name}), ('state', 'not in', ('closed', 'closing_control')), + ('rescue', '=', True), + ('config_id', '=', closed_session.config_id.id), ], limit=1) if rescue_session: _logger.warning('reusing recovery session %s for saving order %s', rescue_session.name, order['name']) @@ -77,7 +78,8 @@ class PosOrder(models.Model): _logger.warning('attempting to create recovery session for saving order %s', order['name']) new_session = PosSession.create({ 'config_id': closed_session.config_id.id, - 'name': '(RESCUE FOR %(session)s)' % {'session': closed_session.name}, + 'name': _('(RESCUE FOR %(session)s)') % {'session': closed_session.name}, + 'rescue': True, # avoid conflict with live sessions }) # bypass opening_control (necessary when using cash control) new_session.action_pos_session_open() diff --git a/addons/point_of_sale/models/pos_session.py b/addons/point_of_sale/models/pos_session.py index 8382ccc7404..107458c7363 100644 --- a/addons/point_of_sale/models/pos_session.py +++ b/addons/point_of_sale/models/pos_session.py @@ -103,6 +103,10 @@ class PosSession(models.Model): order_ids = fields.One2many('pos.order', 'session_id', string='Orders') statement_ids = fields.One2many('account.bank.statement', 'pos_session_id', string='Bank Statement', readonly=True) picking_count = fields.Integer(compute='_compute_picking_count') + rescue = fields.Boolean(string='Recovery Session', + help="Auto-generated session for orphan orders, ignored in constraints", + readonly=True, + copy=False) _sql_constraints = [('uniq_name', 'unique(name)', "The name of this POS Session must be unique !")] @@ -140,7 +144,7 @@ class PosSession(models.Model): if self.search_count([ ('state', 'not in', ('closed', 'closing_control')), ('user_id', '=', self.user_id.id), - ('name', 'not like', 'RESCUE FOR'), + ('rescue', '=', False) ]) > 1: raise ValidationError(_("You cannot create two active sessions with the same responsible!")) @@ -149,7 +153,7 @@ class PosSession(models.Model): if self.search_count([ ('state', '!=', 'closed'), ('config_id', '=', self.config_id.id), - ('name', 'not like', 'RESCUE FOR'), + ('rescue', '=', False) ]) > 1: raise ValidationError(_("You cannot create two active sessions related to the same point of sale!")) diff --git a/addons/point_of_sale/tests/test_point_of_sale_flow.py b/addons/point_of_sale/tests/test_point_of_sale_flow.py index 730e818eac5..24869ad5881 100644 --- a/addons/point_of_sale/tests/test_point_of_sale_flow.py +++ b/addons/point_of_sale/tests/test_point_of_sale_flow.py @@ -586,6 +586,7 @@ class TestPointOfSaleFlow(TestPointOfSaleCommon): rescue_session = self.PosSession.search([ ('config_id', '=', self.pos_config.id), ('state', '=', 'opened'), + ('rescue', '=', True) ]) self.assertEqual(len(rescue_session), 1, "One (and only one) rescue session should be created for orphan orders") self.assertIn("(RESCUE FOR %s)" % current_session.name, rescue_session.name, "Rescue session is not linked to the previous one")