@@ -212,25 +212,29 @@ def scan_document(self, barcode):
212
212
* summary: stock.picking is selected and all its lines have a
213
213
destination pack set
214
214
"""
215
- search_result = self ._scan_document_find (barcode )
216
- result_handler = getattr (self , "_select_document_from_" + search_result .type )
217
- return result_handler (search_result .record )
215
+ handlers = {
216
+ "picking" : self ._select_document_from_picking ,
217
+ "location" : self ._select_document_from_location ,
218
+ "package" : self ._select_document_from_package ,
219
+ "packaging" : self ._select_document_from_packaging ,
220
+ "product" : self ._select_document_from_product ,
221
+ "none" : self ._select_document_from_none ,
222
+ }
223
+ if self .work .menu .scan_location_or_pack_first :
224
+ handlers .pop ("product" )
225
+ search_result = self ._scan_document_find (barcode , handlers .keys ())
226
+ # Keep track of what has been initially scan, and forward it through kwargs
227
+ kwargs = {
228
+ "barcode" : barcode ,
229
+ "current_state" : "select_document" ,
230
+ "scanned_record" : search_result .record ,
231
+ }
232
+ handler = handlers .get (search_result .type , self ._select_document_from_none )
233
+ return handler (search_result .record , ** kwargs )
218
234
219
- def _scan_document_find (self , barcode , search_types = None ):
235
+ def _scan_document_find (self , barcode , search_types ):
220
236
search = self ._actions_for ("search" )
221
- search_types = (
222
- "picking" ,
223
- "location" ,
224
- "package" ,
225
- "packaging" ,
226
- ) + (("product" ,) if not self .work .menu .scan_location_or_pack_first else ())
227
- return search .find (
228
- barcode ,
229
- types = search_types ,
230
- )
231
-
232
- def _select_document_from_picking (self , picking , ** kw ):
233
- return self ._select_picking (picking , "select_document" )
237
+ return search .find (barcode , types = search_types )
234
238
235
239
def _select_document_from_location (self , location , ** kw ):
236
240
if not self .is_src_location_valid (location ):
@@ -249,7 +253,9 @@ def _select_document_from_location(self, location, **kw):
249
253
),
250
254
}
251
255
)
252
- return self ._select_picking (pickings , "select_document" )
256
+ # Keep track of what has been initially scan, and forward it through kwargs
257
+ kwargs = {** kw , "current_state" : "select_document" }
258
+ return self ._select_document_from_picking (pickings , ** kwargs )
253
259
254
260
def _select_document_from_package (self , package , ** kw ):
255
261
pickings = package .move_line_ids .filtered (
@@ -258,14 +264,15 @@ def _select_document_from_package(self, package, **kw):
258
264
if len (pickings ) > 1 :
259
265
# Filter only if we find several pickings to narrow the
260
266
# selection to one of the good type. If we have one picking
261
- # of the wrong type, it will be caught in _select_picking
267
+ # of the wrong type, it will be caught in _select_document_from_picking
262
268
# with the proper error message.
263
269
# Side note: rather unlikely to have several transfers ready
264
270
# and moving the same things
265
271
pickings = pickings .filtered (
266
272
lambda p : p .picking_type_id in self .picking_types
267
273
)
268
- return self ._select_picking (fields .first (pickings ), "select_document" )
274
+ kwargs = {** kw , "current_state" : "select_document" }
275
+ return self ._select_document_from_picking (fields .first (pickings ), ** kwargs )
269
276
270
277
def _select_document_from_product (self , product , line_domain = None , ** kw ):
271
278
line_domain = line_domain or []
@@ -285,7 +292,8 @@ def _select_document_from_product(self, product, line_domain=None, **kw):
285
292
order = "priority desc, scheduled_date asc, id desc" ,
286
293
limit = 1 ,
287
294
)
288
- return self ._select_picking (picking , "select_document" )
295
+ kwargs = {** kw , "current_state" : "select_document" }
296
+ return self ._select_document_from_picking (picking , ** kwargs )
289
297
290
298
def _select_document_from_packaging (self , packaging , ** kw ):
291
299
# And retrieve its product
@@ -296,35 +304,33 @@ def _select_document_from_packaging(self, packaging, **kw):
296
304
line_domain = [("product_uom_qty" , ">=" , packaging .qty )]
297
305
return self ._select_document_from_product (product , line_domain = line_domain )
298
306
299
- def _select_document_from_none (self , picking , ** kw ):
307
+ def _select_document_from_none (self , * args , barcode = None , ** kwargs ):
300
308
"""Handle result when no record is found."""
301
- return self ._select_picking (picking , "select_document" )
309
+ return self ._response_for_select_document (
310
+ message = self .msg_store .transfer_not_found_for_barcode (barcode )
311
+ )
302
312
303
- def _select_picking (self , picking , state_for_error ):
313
+ def _select_document_from_picking (
314
+ self , picking , current_state = None , barcode = None , ** kwargs
315
+ ):
316
+ # Get origin record to give more context to the user when raising an error
317
+ # as we got picking from product/package/packaging/...
318
+ scanned_record = kwargs .get ("scanned_record" )
304
319
if not picking :
305
- if state_for_error == "manual_selection" :
306
- return self ._response_for_manual_selection (
307
- message = self .msg_store .stock_picking_not_found ()
308
- )
309
- return self ._response_for_select_document (
310
- message = self .msg_store .barcode_not_found ()
311
- )
320
+ message = self .msg_store .transfer_not_found_for_record (scanned_record )
321
+ if current_state == "manual_selection" :
322
+ return self ._response_for_manual_selection (message = message )
323
+ return self ._response_for_select_document (message = message )
312
324
if picking .picking_type_id not in self .picking_types :
313
- if state_for_error == "manual_selection" :
314
- return self ._response_for_manual_selection (
315
- message = self .msg_store .cannot_move_something_in_picking_type ()
316
- )
317
- return self ._response_for_select_document (
318
- message = self .msg_store .cannot_move_something_in_picking_type ()
319
- )
325
+ message = self .msg_store .reserved_for_other_picking_type (picking )
326
+ if current_state == "manual_selection" :
327
+ return self ._response_for_manual_selection (message = message )
328
+ return self ._response_for_select_document (message = message )
320
329
if picking .state != "assigned" :
321
- if state_for_error == "manual_selection" :
322
- return self ._response_for_manual_selection (
323
- message = self .msg_store .stock_picking_not_available (picking )
324
- )
325
- return self ._response_for_select_document (
326
- message = self .msg_store .stock_picking_not_available (picking )
327
- )
330
+ message = self .msg_store .stock_picking_not_available (picking )
331
+ if current_state == "manual_selection" :
332
+ return self ._response_for_manual_selection (message = message )
333
+ return self ._response_for_select_document (message = message )
328
334
return self ._response_for_select_line (picking )
329
335
330
336
def _data_for_move_lines (self , lines , ** kw ):
@@ -403,7 +409,14 @@ def select(self, picking_id):
403
409
message = self ._check_picking_processible (picking )
404
410
if message :
405
411
return self ._response_for_manual_selection (message = message )
406
- return self ._select_picking (picking , "manual_selection" )
412
+ # Because _select_document_from_picking expects some context
413
+ # to give meaningful infos to the user, add some here.
414
+ kwargs = {
415
+ "current_state" : "manual_selection" ,
416
+ "barcode" : picking .name ,
417
+ "scanned_record" : picking ,
418
+ }
419
+ return self ._select_document_from_picking (picking , ** kwargs )
407
420
408
421
def _select_lines (self , lines , prefill_qty = 0 , related_lines = None ):
409
422
for i , line in enumerate (lines ):
0 commit comments