Allow changing active scan batch location
This commit is contained in:
@@ -51,6 +51,33 @@ def resume_active_run(user):
|
||||
.prefetch_related('batches__items').order_by('-created_at').first())
|
||||
|
||||
|
||||
def change_draft_batch_location(user, *, batch_id, location_id):
|
||||
"""Change the location for a live draft batch and all of its unsubmitted scans."""
|
||||
with transaction.atomic():
|
||||
batch = (InventoryScanBatch.objects.select_for_update().select_related('run')
|
||||
.filter(pk=batch_id, run__user=user).first())
|
||||
if not batch or batch.status != InventoryScanBatch.Status.DRAFT:
|
||||
raise ScanWorkflowError('Only the current unsaved batch can change location.')
|
||||
run = InventoryScanRun.objects.select_for_update().get(pk=batch.run_id)
|
||||
if run.status != InventoryScanRun.Status.ACTIVE:
|
||||
raise ScanWorkflowError('This scan session is no longer active.')
|
||||
location = InventoryLocation.objects.filter(user=user, pk=location_id).first()
|
||||
if not location:
|
||||
raise ScanWorkflowError('Choose one of your existing inventory locations.')
|
||||
|
||||
batch.location = location
|
||||
batch.save(update_fields=('location',))
|
||||
run.selected_location = location
|
||||
run.save(update_fields=('selected_location',))
|
||||
items = batch.items.select_for_update().filter(
|
||||
status__in=(InventoryScanItem.Status.ACTIVE, InventoryScanItem.Status.CONFIRMED))
|
||||
for item in items:
|
||||
item.location = location
|
||||
item.user_data = _item_data(item)
|
||||
item.save(update_fields=('location', 'user_data'))
|
||||
return batch
|
||||
|
||||
|
||||
def create_active_scan(run, user, barcode):
|
||||
if not barcode.isascii() or not barcode.isdigit() or not 6 <= len(barcode) <= 64:
|
||||
raise ScanWorkflowError('Enter a valid numeric barcode.')
|
||||
|
||||
Reference in New Issue
Block a user