Add continuous inventory barcode scanning workflow

This commit is contained in:
Joren de Graaf
2026-10-26 01:19:55 +01:00
parent a66ecd7c85
commit d305a99b1b
10 changed files with 776 additions and 11 deletions
@@ -0,0 +1,69 @@
{% extends 'planner/base.html' %}{% load static %}
{% block title %}Bulk scan inventory · Tasknest{% endblock %}
{% block content %}
<link rel="stylesheet" href="{% static 'food/food.css' %}?v=20261026-bulk-2">
<main class="food-page inventory-scan-page">
<header class="food-heading">
<div><a href="{% url 'food:inventory' %}">← Kitchen inventory</a><h1>Bulk scan inventory</h1><p>Scan packages into a location, then review your session queue.</p></div>
{% if run %}<span class="inventory-scan-location" data-scan-current-location>{{ draft_batch.location.name }}</span>{% endif %}
</header>
{% if not run %}
<section class="food-card inventory-scan-start">
<h2>Choose a starting location</h2>
<p>Your session is saved as you scan, so you can return to it after navigating away.</p>
{% if start_error %}<p class="inventory-scan-error" role="alert">{{ start_error }}</p>{% endif %}
{% if locations %}
<form method="post" action="{% url 'food:inventory-scan-start' %}" class="inventory-scan-start-form">
{% csrf_token %}<label for="scan-start-location">Scan into</label>
<select id="scan-start-location" name="location_id" required>{% for location in locations %}<option value="{{ location.pk }}">{{ location.name }}</option>{% endfor %}</select>
<button class="button" type="submit">Start scanning</button>
</form>
{% else %}
<p>You need an inventory location before you can scan.</p><a class="button" href="{% url 'food:inventory-add' %}">Add an inventory location</a>
{% endif %}
</section>
{% else %}
<div class="inventory-scan-layout" data-inventory-scanner
data-run-id="{{ run.api_id }}"
data-batch-id="{{ draft_batch.pk }}"
data-create-url="{% url 'food:inventory-scan-create' run.api_id %}"
data-queue-url="{% url 'food:inventory-scan-queue' run.api_id %}"
data-submit-url="{% url 'food:inventory-scan-submit' draft_batch.pk %}"
data-zxing-url="{% static 'food/vendor/zxing-browser.min.js' %}">
<section class="inventory-scan-camera food-card" aria-label="Barcode scanner">
<div class="inventory-camera-label"><span class="inventory-camera-indicator" aria-hidden="true"></span><strong>Camera scanner</strong><span>Camera stays on while you confirm packages</span></div>
<div class="food-camera-frame"><video data-scanner-video autoplay muted playsinline aria-label="Camera preview"></video><div class="food-scan-guide" aria-hidden="true"></div></div>
<p data-scanner-status role="status" aria-live="polite">Camera is off. Start the camera or enter a barcode.</p>
<div class="food-actions"><button class="button" type="button" data-scanner-start>Start camera</button><button class="button button--secondary" type="button" data-scanner-stop hidden>Pause camera</button></div>
<form class="food-barcode-form inventory-scan-manual" data-scanner-form>
<label for="inventory-scanner-barcode">Enter barcode manually</label><div><input id="inventory-scanner-barcode" data-scanner-barcode name="barcode" inputmode="numeric" pattern="[0-9]{6,64}" autocomplete="off" required><button class="button button--secondary" type="submit">Add barcode</button></div>
</form>
<p class="food-attribution">Product data from <a href="https://world.openfoodfacts.org/" rel="noopener noreferrer">Open Food Facts</a>.</p>
</section>
<section class="inventory-scan-right">
<div class="inventory-scan-active-wrap" data-scan-active-wrap>
{% if active_item %}{% include 'food/partials/inventory_scan_active.html' with item=active_item locations=locations %}{% else %}
<article class="food-card inventory-scan-active inventory-scan-skeleton" data-scan-active hidden aria-live="polite"><p>Scan a product to begin.</p></article>
{% endif %}
</div>
<section class="food-card inventory-scan-queue" aria-labelledby="inventory-scan-queue-title">
<div class="inventory-scan-queue-heading"><div><p class="inventory-kicker">Current session</p><h2 id="inventory-scan-queue-title">Scan queue</h2></div><span data-scan-queue-count>{{ queued_count }} queued item{{ queued_count|pluralize }}</span></div>
<div id="inventory-scan-queue" data-scan-queue aria-live="polite" data-queue-url="{% url 'food:inventory-scan-queue' run.api_id %}">
{% include 'food/partials/inventory_scan_queue.html' with run=run batches=queue_batches %}
</div>
</section>
<section class="food-card inventory-scan-controls">
<p>The camera remains active as items move into the queue. Use the package controls to correct repeated scans or undo a count.</p>
<form data-scan-submit action="{% url 'food:inventory-scan-submit' draft_batch.pk %}">
{% csrf_token %}<label for="scan-next-location">Save this batch and scan next location</label>
<div class="inventory-scan-next-row"><select id="scan-next-location" name="next_location_id" aria-label="Next inventory location">{% for location in locations %}<option value="{{ location.pk }}" {% if location.pk == draft_batch.location_id %}selected{% endif %}>{{ location.name }}</option>{% endfor %}</select><button class="button button--secondary" type="button" data-scan-next disabled>Save &amp; next location</button></div>
<button class="button inventory-scan-done" type="button" data-scan-done disabled>Done · process confirmed items</button>
</form>
<p class="inventory-scan-note">Processing runs in the background. Items without a confirmed product or valid package setup appear in Needs Review.</p>
</section>
</section>
</div>
{% endif %}
</main>
<script src="{% static 'food/scanner.js' %}?v=20261026-bulk-2" defer></script>
{% endblock %}
@@ -0,0 +1,36 @@
<article class="food-card inventory-scan-active" data-scan-active data-item-id="{{ item.pk }}">
<div class="inventory-scan-active-heading"><span class="inventory-scan-status-dot" aria-hidden="true"></span><p>Barcode detected · <strong>{{ item.barcode }}</strong></p></div>
<form data-scan-confirm action="{% url 'food:inventory-scan-confirm' item.pk %}">
{% csrf_token %}
<div class="inventory-scan-product-title"><label for="scan-product-name-{{ item.pk }}">Product name</label><input id="scan-product-name-{{ item.pk }}" name="product_name" maxlength="255" value="{{ item.product_name }}" placeholder="Product name" required></div>
<label>Brand <input name="brand" maxlength="255" value="{{ item.brand }}" placeholder="Optional"></label>
<div class="inventory-scan-fields inventory-scan-fields--package">
<label>Packages <input type="number" name="package_count" min="1" max="10000" step="1" value="{{ item.package_count }}" required></label>
<label>Amount per package <input type="number" name="package_amount" min="0.001" step="0.001" value="{{ item.package_amount|default_if_none:'' }}" placeholder="Unknown"></label>
<label>Package unit <select name="package_unit"><option value="">Count only / unknown size</option><option value="mg" {% if item.package_unit == 'mg' %}selected{% endif %}>mg</option><option value="g" {% if item.package_unit == 'g' %}selected{% endif %}>g</option><option value="kg" {% if item.package_unit == 'kg' %}selected{% endif %}>kg</option><option value="ml" {% if item.package_unit == 'ml' %}selected{% endif %}>ml</option><option value="L" {% if item.package_unit == 'L' %}selected{% endif %}>L</option><option value="pieces" {% if item.package_unit == 'pieces' %}selected{% endif %}>pieces</option><option value="packs" {% if item.package_unit == 'packs' %}selected{% endif %}>packs</option><option value="portions" {% if item.package_unit == 'portions' %}selected{% endif %}>portions</option></select></label>
</div>
<p class="inventory-scan-total">Total: <strong data-scan-total>{{ item.package_count }} × {{ item.package_amount|default:'unknown' }} {{ item.package_unit }}</strong></p>
<details class="inventory-scan-more"><summary>Piece and inner pack details</summary>
<div class="inventory-scan-fields">
<label>Pieces in package <input type="number" name="pieces_per_package" min="1" step="1" value="{{ item.pieces_per_package|default_if_none:'' }}"></label>
<label>Measured amount per piece <input type="number" name="piece_amount" min="0.001" step="0.001" value="{{ item.piece_amount|default_if_none:'' }}"></label>
<label>Inner packs <input type="number" name="inner_packs_per_package" min="1" step="1" value="{{ item.inner_packs_per_package|default_if_none:'' }}"></label>
<label>Pieces per inner pack <input type="number" name="pieces_per_inner_pack" min="1" step="1" value="{{ item.pieces_per_inner_pack|default_if_none:'' }}"></label>
<label>Measured amount per inner pack <input type="number" name="inner_pack_amount" min="0.001" step="0.001" value="{{ item.inner_pack_amount|default_if_none:'' }}"></label>
<label>Inner pack label <input name="inner_pack_label" maxlength="40" value="{{ item.inner_pack_label }}" placeholder="wrapper"></label>
<label>Piece label <input name="piece_label" maxlength="40" value="{{ item.piece_label }}" placeholder="biscuit"></label>
</div>
</details>
<div class="inventory-scan-fields">
<label>Storage location <select name="location_id" required>{% for location in locations %}<option value="{{ location.pk }}" {% if item.location_id == location.pk %}selected{% endif %}>{{ location.name }}</option>{% endfor %}</select></label>
<label>Category <input name="category_name" maxlength="100" value="{{ item.category_name }}" placeholder="Optional"></label>
<label>Best before <input type="date" name="best_before" value="{{ item.best_before|date:'Y-m-d' }}"></label>
<label class="inventory-scan-checkbox"><input type="checkbox" name="opened" value="true" {% if item.opened %}checked{% endif %}> Already opened</label>
</div>
<label>Notes <textarea name="notes" rows="2" maxlength="4000" placeholder="Optional">{{ item.notes }}</textarea></label>
{% if item.source_data.nutrition %}<details class="inventory-scan-nutrition"><summary>Open Food Facts nutrition · unknown values stay blank</summary><dl>{% for name,value in item.source_data.nutrition.items %}<div><dt>{{ name|cut:'_100'|title }}</dt><dd>{% if value is None %}Unknown{% else %}{{ value }}{% endif %}</dd></div>{% endfor %}</dl></details>{% else %}<p class="inventory-scan-meta">Nutrition will be fetched in the background. Missing values remain unknown.</p>{% endif %}
<div class="inventory-scan-card-actions"><button class="button" type="submit">Confirm &amp; keep scanning</button><button class="button button--secondary" type="button" data-scan-discard>Remove this scan</button></div>
<p class="inventory-scan-hint">You can also swipe this card left to confirm. Keyboard users can submit with Enter.</p>
<p class="inventory-scan-error" data-scan-item-error role="alert" hidden></p>
</form>
</article>
@@ -0,0 +1,23 @@
<div class="inventory-scan-batches" data-scan-queue-content>
{% for batch in batches %}
<section class="inventory-scan-batch" data-batch-id="{{ batch.pk }}" data-batch-status="{{ batch.status }}">
<div class="inventory-scan-batch-heading"><h3>{{ batch.location.name }}</h3><span>{{ batch.get_status_display }}</span></div>
{% for item in batch.queue_items %}
<article class="inventory-scan-queue-item" data-queue-item data-queue-status="{{ item.status }}" data-item-id="{{ item.pk }}" data-barcode="{{ item.barcode }}">
<div class="inventory-scan-queue-item-main"><span class="inventory-scan-queue-state inventory-scan-queue-state--{{ item.status }}" aria-hidden="true"></span><div><strong>{{ item.product_name|default:'Product details needed' }}</strong><small>{{ item.barcode }} · {{ item.get_status_display }}{% if item.last_error %} · {{ item.last_error }}{% endif %}</small></div></div>
<div class="inventory-scan-queue-item-side">
<span>{{ item.package_count }} package{{ item.package_count|pluralize }}</span>
{% if item.package_amount %}<small>{{ item.package_amount }} {{ item.package_unit }}</small>{% endif %}
{% if batch.status == 'draft' and item.status == 'confirmed' %}
<div class="inventory-scan-count-controls" aria-label="Adjust {{ item.product_name }} package count">
<button type="button" data-scan-count="-1" aria-label="Remove one package">−</button>
<span>{{ item.package_count }}</span>
<button type="button" data-scan-count="1" aria-label="Add one package">+</button>
</div>
{% endif %}
</div>
</article>
{% empty %}<p class="inventory-scan-empty">No confirmed items in this location batch yet.</p>{% endfor %}
</section>
{% empty %}<p class="inventory-scan-empty">Your saved batches will appear here.</p>{% endfor %}
</div>