[FIX] point_of_sale: forward port of 35e9fa5c41

Non-trivial port because 9.0 has rescue sessions that kind of try to
solve the same problems, however:

- they do not automatically put orders into a compatible, newly opened
  session because it explicitly searches for rescue sessions. So if
  you open a new session and create a new order in a POS associated to
  a closed session you will end up with two open sessions (one rescue,
  one regular).

- they throw exceptions with cash control enabled

- they're a bit more complicated because they introduce the idea of
  'rescue' sessions, which the 8.0 fix does not (it just uses regular
  sessions).

So this reverts bed409322d without
changing the database and while still ensuring that databases with
existing POS rescue sessions don't break.
This commit is contained in:
Joren Van Onder
2015-10-29 15:56:25 +01:00
parent 05381a8bed
commit 87a3fe92de
+30 -18
View File
@@ -676,27 +676,39 @@ class pos_order(osv.osv):
'journal': ui_paymentline['journal_id'],
}
def _get_rescue_session(self, cr, uid, order, context=None):
""" Find or generate a rescue session """
pos_session = self.pool['pos.session']
date = order.get('creation_date', time.strftime(tools.DEFAULT_SERVER_DATETIME_FORMAT))
closed_session = pos_session.browse(cr, uid, order['pos_session_id'], context=context)
rescue_session_ids = pos_session.search(cr, uid, [
('rescue', '=', True), ('config_id', '=', closed_session.config_id.id),
('start_at', '<=', date), ('state', '=', 'opened')
], limit=1, context=context)
if not rescue_session_ids:
return pos_session.copy(cr, uid, order['pos_session_id'], default={
'rescue': True,
# This deals with orders that belong to a closed session. In order
# to recover from this we:
# - assign the order to another compatible open session
# - if that doesn't exist, create a new one
def _get_valid_session(self, cr, uid, order, context=None):
session = self.pool.get('pos.session')
closed_session = session.browse(cr, uid, order['pos_session_id'], context=context)
open_sessions = session.search(cr, uid, [('state', '=', 'opened'),
('config_id', '=', closed_session.config_id.id),
('user_id', '=', closed_session.user_id.id)],
limit=1, order="start_at DESC", context=context)
if open_sessions:
return open_sessions[0]
else:
new_session_id = session.create(cr, uid, {
'config_id': closed_session.config_id.id,
}, context=context)
return rescue_session_ids[0]
new_session = session.browse(cr, uid, new_session_id, context=context)
# bypass opening_control (necessary when using cash control)
new_session.signal_workflow('open')
return new_session_id
def _process_order(self, cr, uid, order, context=None):
session = self.pool['pos.session'].browse(cr, uid, order['pos_session_id'], context=context)
if session.state == 'closed':
rescue_session_id = self._get_rescue_session(cr, uid, order, context=context)
order['pos_session_id'] = rescue_session_id
session = self.pool['pos.session'].browse(cr, uid, rescue_session_id, context=context)
session = self.pool.get('pos.session').browse(cr, uid, order['pos_session_id'], context=context)
if session.state == 'closing_control' or session.state == 'closed':
session_id = self._get_valid_session(cr, uid, order, context=context)
session = self.pool.get('pos.session').browse(cr, uid, session_id, context=context)
order['pos_session_id'] = session_id
order_id = self.create(cr, uid, self._order_fields(cr, uid, order, context=context),context)
journal_ids = set()
for payments in order['statement_ids']: