Add continuous inventory barcode scanning workflow
This commit is contained in:
@@ -7,6 +7,7 @@ from django.core.exceptions import ValidationError
|
||||
from django.db import IntegrityError, connections, transaction
|
||||
from django.db.models import F, Q
|
||||
from django.utils import timezone
|
||||
from django.utils.dateparse import parse_date
|
||||
|
||||
from food.models import (FoodProduct, InventoryCategory, InventoryLocation,
|
||||
InventoryReviewPreference, InventoryScanBatch,
|
||||
@@ -100,7 +101,10 @@ def confirm_scan_item(user, item_id, values):
|
||||
item.inner_pack_amount = _decimal_or_none(values.get('inner_pack_amount'), 'Enter a valid inner pack amount.')
|
||||
item.inner_pack_label = str(values.get('inner_pack_label', '')).strip()[:40]
|
||||
item.piece_label = str(values.get('piece_label', '')).strip()[:40]
|
||||
item.best_before = values.get('best_before') or None
|
||||
best_before = str(values.get('best_before', '')).strip()
|
||||
item.best_before = parse_date(best_before) if best_before else None
|
||||
if best_before and item.best_before is None:
|
||||
raise ScanWorkflowError('Enter a valid best-before date.')
|
||||
item.opened = bool(values.get('opened'))
|
||||
item.user_data = _item_data(item)
|
||||
item.status = InventoryScanItem.Status.CONFIRMED
|
||||
@@ -136,6 +140,7 @@ def adjust_confirmed_count(user, item_id, delta):
|
||||
|
||||
|
||||
def submit_batch(user, *, batch_id, next_location_id=None, finish=False):
|
||||
run_id = None
|
||||
with transaction.atomic():
|
||||
batch = (InventoryScanBatch.objects.select_for_update().select_related('run')
|
||||
.filter(pk=batch_id, run__user=user).first())
|
||||
@@ -149,13 +154,22 @@ def submit_batch(user, *, batch_id, next_location_id=None, finish=False):
|
||||
if batch.items.filter(status=InventoryScanItem.Status.ACTIVE).exists():
|
||||
raise ScanWorkflowError('Confirm or remove the active item before saving this batch.')
|
||||
items = batch.items.filter(status=InventoryScanItem.Status.CONFIRMED)
|
||||
if not items.exists():
|
||||
has_items = items.exists()
|
||||
prior_submissions = run.batches.exclude(pk=batch.pk).filter(
|
||||
status__in=(InventoryScanBatch.Status.QUEUED, InventoryScanBatch.Status.COMPLETE)).exists()
|
||||
if not has_items and not (finish and prior_submissions):
|
||||
raise ScanWorkflowError('Confirm at least one item before saving this batch.')
|
||||
now = timezone.now()
|
||||
items.update(status=InventoryScanItem.Status.QUEUED, next_attempt_at=now)
|
||||
batch.status = InventoryScanBatch.Status.QUEUED
|
||||
batch.submitted_at = now
|
||||
batch.save(update_fields=('status', 'submitted_at'))
|
||||
if has_items:
|
||||
items.update(status=InventoryScanItem.Status.QUEUED, next_attempt_at=now)
|
||||
batch.status = InventoryScanBatch.Status.QUEUED
|
||||
batch.submitted_at = now
|
||||
batch.save(update_fields=('status', 'submitted_at'))
|
||||
else:
|
||||
batch.status = InventoryScanBatch.Status.COMPLETE
|
||||
batch.submitted_at = now
|
||||
batch.completed_at = now
|
||||
batch.save(update_fields=('status', 'submitted_at', 'completed_at'))
|
||||
if finish:
|
||||
run.status = InventoryScanRun.Status.PROCESSING
|
||||
run.submitted_at = now
|
||||
@@ -168,7 +182,10 @@ def submit_batch(user, *, batch_id, next_location_id=None, finish=False):
|
||||
next_sequence = (run.batches.order_by('-sequence').values_list('sequence', flat=True).first() or 0) + 1
|
||||
InventoryScanBatch.objects.create(run=run, location=location, sequence=next_sequence)
|
||||
run.save(update_fields=('selected_location',))
|
||||
return run
|
||||
run_id = run.pk
|
||||
if finish:
|
||||
_refresh_completion(run_id)
|
||||
return InventoryScanRun.objects.get(pk=run_id)
|
||||
|
||||
|
||||
def _claim_next_item():
|
||||
|
||||
Reference in New Issue
Block a user