Add nested inventory package tracking

This commit is contained in:
Joren de Graaf
2026-10-26 00:25:00 +01:00
parent 4d76a2bc9c
commit 5217c7f30c
19 changed files with 838 additions and 44 deletions
+2 -2
View File
@@ -6,7 +6,7 @@
<section class="food-card inventory-product">{% if product.image_url %}<img src="{{ product.image_url }}" alt="" loading="lazy">{% endif %}<div><h2>{{ product.product_name }}</h2>{% if product.brand %}<p>{{ product.brand }}</p>{% endif %}{% if product.package_quantity %}<p>{{ product.package_quantity }}</p>{% endif %}{% if off_categories %}<p><small>Open Food Facts: {{ off_categories }}</small></p>{% endif %}<p><a href="{% url 'food:product-edit' product.pk %}">View or edit product nutrition</a></p></div></section>
{% if existing %}<section class="food-card"><h2>Already in inventory</h2>{% for item in existing %}<div class="inventory-existing"><span>{{ item.location.name }} · Quantity {{ item.quantity }}{% if item.best_before %} · Best before {{ item.best_before }}{% endif %}</span><form method="post" action="{% url 'food:inventory-add-one' item.pk %}">{% csrf_token %}<button class="button button--secondary" type="submit">+1 existing item</button></form></div>{% endfor %}<p>Use the form below to add this product separately.</p></section>{% endif %}
<form class="food-card inventory-form" method="post">{% csrf_token %}<input type="hidden" name="product" value="{{ product.pk }}">{{ form.non_field_errors }}
{% for field in form %}<div class="form-field">{{ field.label_tag }}{% if field.name == 'quantity' %}<div class="inventory-quantity" data-inventory-quantity><button type="button" data-inventory-step="-1" aria-label="Decrease quantity">−</button>{{ field }}<button type="button" data-inventory-step="1" aria-label="Increase quantity">+</button></div>{% else %}{{ field }}{% endif %}{{ field.errors }}</div>{% endfor %}
{% for field in form %}<div class="form-field">{{ field.label_tag }}{% if field.name == 'quantity' %}<div class="inventory-quantity" data-inventory-quantity><button type="button" data-inventory-step="-1" aria-label="Decrease quantity">−</button>{{ field }}<button type="button" data-inventory-step="1" aria-label="Increase quantity">+</button></div>{% else %}{{ field }}{% endif %}{% if field.help_text %}<small>{{ field.help_text }}</small>{% endif %}{{ field.errors }}</div>{% endfor %}
<datalist id="inventory-locations">{% for location in locations %}<option value="{{ location.name }}">{% endfor %}</datalist><datalist id="inventory-categories">{% for category in categories %}<option value="{{ category.name }}">{% endfor %}</datalist>
<button class="button" type="submit">Add to inventory</button></form>
<a href="{% url 'food:inventory-add' %}">Choose another product</a>
@@ -16,4 +16,4 @@
<ul class="food-product-list">{% for item in products %}<li><a href="{% url 'food:inventory-add' %}?product={{ item.pk }}">{% if item.image_url %}<img src="{{ item.image_url }}" alt="" loading="lazy">{% endif %}<span><strong>{{ item.product_name }}</strong>{% if item.brand %}<small>{{ item.brand }}</small>{% endif %}</span></a></li>{% empty %}<li>No saved products found.</li>{% endfor %}</ul>
{% include 'food/partials/inventory_product_actions.html' %}
<form method="post" action="{% url 'food:inventory-lookup' %}" class="food-barcode-form">{% csrf_token %}<label for="inventory-barcode">Enter barcode</label><input id="inventory-barcode" name="barcode" inputmode="numeric" pattern="[0-9]+" required><button class="button button--secondary" type="submit">Look up</button></form>
{% endif %}</div><script src="{% static 'food/inventory.js' %}" defer></script>{% endblock %}
{% endif %}</div><script src="{% static 'food/inventory.js' %}?v=20261026-nested" defer></script>{% endblock %}
+1 -1
View File
@@ -1,3 +1,3 @@
{% extends 'planner/base.html' %}{% load static %}
{% block title %}Edit inventory item · Tasknest{% endblock %}
{% block content %}<link rel="stylesheet" href="{% static 'food/food.css' %}"><div class="food-form-page inventory-page"><a href="{% url 'food:inventory' %}">← Kitchen Inventory</a><h1>Edit {{ item.product.product_name }}</h1><p>This form changes stock for this package. <a href="{% url 'food:product-edit' item.product.pk %}">View or edit product nutrition</a> for future food logs.</p><form method="post" class="food-card inventory-form">{% csrf_token %}{{ form.non_field_errors }}{% for field in form %}<div class="form-field">{{ field.label_tag }}{% if field.name == 'quantity' %}<div class="inventory-quantity" data-inventory-quantity><button type="button" data-inventory-step="-1" aria-label="Decrease quantity">−</button>{{ field }}<button type="button" data-inventory-step="1" aria-label="Increase quantity">+</button></div>{% else %}{{ field }}{% endif %}{{ field.errors }}</div>{% endfor %}<datalist id="inventory-locations">{% for location in locations %}<option value="{{ location.name }}">{% endfor %}</datalist><datalist id="inventory-categories">{% for category in categories %}<option value="{{ category.name }}">{% endfor %}</datalist><button class="button" type="submit">Save changes</button></form></div><script src="{% static 'food/inventory.js' %}" defer></script>{% endblock %}
{% block content %}<link rel="stylesheet" href="{% static 'food/food.css' %}"><div class="food-form-page inventory-page"><a href="{% url 'food:inventory' %}">← Kitchen Inventory</a><h1>Edit {{ item.product.product_name }}</h1><p>This form changes stock for this package. <a href="{% url 'food:product-edit' item.product.pk %}">View or edit product nutrition</a> for future food logs.</p><form method="post" class="food-card inventory-form">{% csrf_token %}{{ form.non_field_errors }}{% for field in form %}<div class="form-field">{{ field.label_tag }}{% if field.name == 'quantity' %}<div class="inventory-quantity" data-inventory-quantity><button type="button" data-inventory-step="-1" aria-label="Decrease quantity">−</button>{{ field }}<button type="button" data-inventory-step="1" aria-label="Increase quantity">+</button></div>{% else %}{{ field }}{% endif %}{% if field.help_text %}<small>{{ field.help_text }}</small>{% endif %}{{ field.errors }}</div>{% endfor %}<datalist id="inventory-locations">{% for location in locations %}<option value="{{ location.name }}">{% endfor %}</datalist><datalist id="inventory-categories">{% for category in categories %}<option value="{{ category.name }}">{% endfor %}</datalist><button class="button" type="submit">Save changes</button></form></div><script src="{% static 'food/inventory.js' %}?v=20261026-nested" defer></script>{% endblock %}
@@ -1,16 +1,16 @@
{% extends 'planner/base.html' %}{% load static %}
{% block title %}Kitchen Inventory · Tasknest{% endblock %}
{% block content %}<link rel="stylesheet" href="{% static 'food/food.css' %}?v=20261012-filters">
{% block content %}<link rel="stylesheet" href="{% static 'food/food.css' %}?v=20261026-nested">
<div class="food-page inventory-page"><header class="food-heading"><div><h1>Kitchen Inventory</h1><p>What you have and where it lives.</p></div><div class="inventory-heading-actions"><a class="button" href="{% url 'food:inventory-add' %}">+ Add item</a><a class="button button--secondary" href="{% url 'food:scanner' %}?mode=inventory">Scan barcode</a></div></header>
<form method="get" class="food-card inventory-filters" role="search" aria-label="Search kitchen inventory">
<div class="inventory-filter-search"><label for="inventory-search">Search inventory</label><div><input id="inventory-search" type="search" name="q" value="{{ query }}"><button class="button" type="submit">Search</button></div></div>
<details class="inventory-filter-details" data-responsive-filters open><summary>More filters{% if selected_location or selected_category or opened or use_soon == '1' or sort != 'location' %} · Applied{% endif %}</summary><div class="inventory-filter-options"><label>Location<select name="location"><option value="">All locations</option>{% for location in locations %}<option value="{{ location.pk }}" {% if selected_location == location.pk|stringformat:'s' %}selected{% endif %}>{{ location.name }}</option>{% endfor %}</select></label><label>Category<select name="category"><option value="">All categories</option>{% for category in categories %}<option value="{{ category.pk }}" {% if selected_category == category.pk|stringformat:'s' %}selected{% endif %}>{{ category.name }}</option>{% endfor %}</select></label><label>Opened<select name="opened"><option value="">All</option><option value="yes" {% if opened == 'yes' %}selected{% endif %}>Opened</option><option value="no" {% if opened == 'no' %}selected{% endif %}>Unopened</option></select></label><label>Sort<select name="sort"><option value="location" {% if sort == 'location' %}selected{% endif %}>Location</option><option value="category" {% if sort == 'category' %}selected{% endif %}>Category</option><option value="name" {% if sort == 'name' %}selected{% endif %}>Product name</option><option value="recent" {% if sort == 'recent' %}selected{% endif %}>Recently added</option><option value="best_before" {% if sort == 'best_before' %}selected{% endif %}>Best before</option></select></label><label class="inventory-check"><input type="checkbox" name="use_soon" value="1" {% if use_soon == '1' %}checked{% endif %}> Use soon</label><button class="button" type="submit">Apply filters</button></div></details>
</form>
{% for card in cards %}<article class="food-card inventory-product-card">
<div class="inventory-product-heading">{% if card.product.image_url %}<img src="{{ card.product.image_url }}" alt="" loading="lazy">{% endif %}<div><h2>{{ card.product.product_name }}</h2>{% if card.product.brand %}<p>{{ card.product.brand }}</p>{% endif %}<p><strong>{{ card.total|floatformat:-3 }} {% if card.unit == "packs" and card.total == 1 %}pack{% else %}{{ card.unit }}{% endif %} remaining</strong> · {{ card.packages|length }} package{{ card.packages|length|pluralize }}</p></div></div>
<div class="inventory-product-heading">{% if card.product.image_url %}<img src="{{ card.product.image_url }}" alt="" loading="lazy">{% endif %}<div><h2>{{ card.product.product_name }}</h2>{% if card.product.brand %}<p>{{ card.product.brand }}</p>{% endif %}<p><strong>{% if card.has_pieces %}{{ card.piece_total }} piece{{ card.piece_total|pluralize }}{% if card.unit != 'pieces' %} ({{ card.total|floatformat:-3 }} {{ card.unit }}){% endif %}{% else %}{{ card.total|floatformat:-3 }} {% if card.unit == 'packs' %}package{{ card.total|pluralize }}{% else %}{{ card.unit }}{% endif %}{% endif %} remaining</strong> · {{ card.package_count }} package{{ card.package_count|pluralize }}</p></div></div>
<p><a href="{% url 'food:product-edit' card.product.pk %}">View or edit product nutrition</a></p>
<details class="inventory-packages"><summary>View packages</summary><div class="inventory-package-list">{% for item in card.packages %}<div class="inventory-package"><div class="inventory-package-fill" role="img" aria-label="{{ item.percentage_remaining }}% remaining"><span style="height:{{ item.percentage_remaining }}%"></span></div><div><strong>Pack {{ forloop.counter }} · {{ item.package_status|title }}</strong><p>{% if item.original_amount %}{{ item.remaining_amount|floatformat:-3 }} / {{ item.original_amount|floatformat:-3 }} {{ item.inventory_unit }} · {{ item.percentage_remaining }}% remaining{% else %}{{ item.quantity }} pack{{ item.quantity|pluralize }} · size unknown{% endif %}</p><small>{{ item.location.name }}{% if item.category %} · {{ item.category.name }}{% endif %}{% if item.best_before %} · Best before {{ item.best_before }}{% endif %}</small><div><a href="{% url 'food:inventory-edit' item.pk %}">Edit package</a></div></div></div>{% endfor %}</div></details>
<details class="inventory-use"><summary>Use amount</summary><form method="post" action="{% url 'food:inventory-preview' card.product.pk %}" hx-post="{% url 'food:inventory-preview' card.product.pk %}" hx-target="#inventory-preview-{{ card.product.pk }}-{{ card.unit }}" hx-swap="innerHTML" class="inventory-use-form">{% csrf_token %}<label>Amount<input type="number" name="amount" min="{% if card.count_unit %}1{% else %}0.001{% endif %}" step="{% if card.count_unit %}1{% else %}0.001{% endif %}" required inputmode="decimal"></label><label>Unit<select name="unit">{% for unit in card.units %}<option value="{{ unit }}" {% if unit == card.unit %}selected{% endif %}>{{ unit }}</option>{% endfor %}</select></label><label>Package (optional)<select name="package_id"><option value="">Automatic selection</option>{% for item in card.packages %}<option value="{{ item.api_id }}">Pack {{ forloop.counter }} · {% if item.remaining_amount is not None %}{{ item.remaining_amount|floatformat:-3 }} {{ item.inventory_unit }}{% else %}{{ item.quantity }} pack{{ item.quantity|pluralize }}{% endif %}</option>{% endfor %}</select></label><button class="button button--secondary" type="submit">Preview</button></form><div id="inventory-preview-{{ card.product.pk }}-{{ card.unit }}" aria-live="polite"></div></details>
<details class="inventory-packages"><summary>View packages</summary><div class="inventory-package-list">{% for item in card.packages %}<div class="inventory-package"><div class="inventory-package-fill" role="img" aria-label="{{ item.percentage_remaining }}% remaining"><span style="height:{{ item.percentage_remaining }}%"></span></div><div><strong>Package {{ forloop.counter }} · {{ item.package_status|title }}</strong><p>{% if item.inner_packs_per_package %}{{ item.remaining_inner_pack_balance|capfirst }} · {{ item.remaining_pieces }} / {{ item.pieces_per_package }} {{ item.piece_label }}{% if item.inventory_unit != 'pieces' %} · {{ item.remaining_amount|floatformat:-3 }} / {{ item.original_amount|floatformat:-3 }} {{ item.inventory_unit }}{% endif %}{% elif item.pieces_per_package %}{{ item.remaining_pieces }} / {{ item.pieces_per_package }} piece{{ item.pieces_per_package|pluralize }}{% if item.inventory_unit != 'pieces' %} · {{ item.remaining_amount|floatformat:-3 }} / {{ item.original_amount|floatformat:-3 }} {{ item.inventory_unit }}{% endif %}{% elif item.original_amount %}{{ item.remaining_amount|floatformat:-3 }} / {{ item.original_amount|floatformat:-3 }} {{ item.inventory_unit }} · {{ item.percentage_remaining }}% remaining{% else %}{{ item.quantity }} package{{ item.quantity|pluralize }} · size unknown{% endif %}</p><small>{{ item.location.name }}{% if item.category %} · {{ item.category.name }}{% endif %}{% if item.best_before %} · Best before {{ item.best_before }}{% endif %}</small><div><a href="{% url 'food:inventory-edit' item.pk %}">Edit package</a></div></div></div>{% endfor %}</div></details>
<details class="inventory-use"><summary>Use amount</summary><form method="post" action="{% url 'food:inventory-preview' card.product.pk %}" hx-post="{% url 'food:inventory-preview' card.product.pk %}" hx-target="#inventory-preview-{{ card.product.pk }}-{{ card.unit }}" hx-swap="innerHTML" class="inventory-use-form">{% csrf_token %}<label>Amount<input type="number" name="amount" min="{% if card.count_unit %}1{% else %}0.001{% endif %}" step="{% if card.count_unit %}1{% else %}0.001{% endif %}" required inputmode="decimal"></label><label>Unit<select name="unit">{% for unit in card.units %}<option value="{{ unit }}" {% if unit == card.unit %}selected{% endif %}>{% if unit == 'inner_packs' %}inner packs{% else %}{{ unit }}{% endif %}</option>{% endfor %}</select></label><label>Package (optional)<select name="package_id"><option value="">Automatic selection</option>{% for item in card.packages %}<option value="{{ item.api_id }}">Package {{ forloop.counter }} · {% if item.inner_packs_per_package %}{{ item.remaining_inner_pack_balance }} · {{ item.remaining_pieces }} {{ item.piece_label }}{{ item.remaining_pieces|pluralize }} left{% elif item.pieces_per_package %}{{ item.remaining_pieces }} pieces left{% if item.inventory_unit != 'pieces' %} · {{ item.remaining_amount|floatformat:-3 }} {{ item.inventory_unit }}{% endif %}{% elif item.remaining_amount is not None %}{{ item.remaining_amount|floatformat:-3 }} {{ item.inventory_unit }}{% else %}{{ item.quantity }} package{{ item.quantity|pluralize }}{% endif %}</option>{% endfor %}</select></label><button class="button button--secondary" type="submit">Preview</button></form><div id="inventory-preview-{{ card.product.pk }}-{{ card.unit }}" aria-live="polite"></div></details>
</article>{% empty %}<section class="food-card"><p>No inventory items found.</p><a class="button" href="{% url 'food:inventory-add' %}">Add your first item</a></section>{% endfor %}
{% if recent_consumption %}<section class="food-card inventory-history"><h2>Recent use</h2><ul>{% for entry in recent_consumption %}<li>{{ entry.created_at|date:'j M, H:i' }} · {{ entry.product.product_name }} · {{ entry.amount|floatformat:-3 }} {{ entry.unit }}<details><summary>Package changes</summary><ul>{% for line in entry.lines.all %}<li>Pack {{ line.package.pk }}: {{ line.before_amount|floatformat:-3 }} → {{ line.after_amount|floatformat:-3 }} {{ line.unit }}</li>{% endfor %}</ul></details>{% if not entry.reversal %}<form method="post" action="{% url 'food:inventory-undo' entry.pk %}">{% csrf_token %}<button type="submit">Undo</button></form>{% else %} · Undone{% endif %}</li>{% endfor %}</ul></section>{% endif %}
</div><script src="{% static 'planner/responsive_filters.js' %}?v=1" defer></script>{% endblock %}
{% if recent_consumption %}<section class="food-card inventory-history"><h2>Recent use</h2><ul>{% for entry in recent_consumption %}<li>{{ entry.created_at|date:'j M, H:i' }} · {{ entry.product.product_name }} · {{ entry.amount|floatformat:-3 }} {% if entry.unit == 'inner_packs' %}inner pack{{ entry.amount|pluralize }}{% elif entry.unit == 'pieces' %}{{ entry.amount|pluralize:'piece,pieces' }}{% else %}{{ entry.unit }}{% endif %}<details><summary>Package changes</summary><ul>{% for line in entry.lines.all %}<li>Package {{ line.package.pk }}: {{ line.before_amount|floatformat:-3 }} → {{ line.after_amount|floatformat:-3 }} {{ line.unit }}{% if line.after_pieces is not None %} · {{ line.before_pieces }} → {{ line.after_pieces }} pieces{% endif %}</li>{% endfor %}</ul></details>{% if not entry.reversal %}<form method="post" action="{% url 'food:inventory-undo' entry.pk %}">{% csrf_token %}<button type="submit">Undo</button></form>{% else %} · Undone{% endif %}</li>{% endfor %}</ul></section>{% endif %}
</div><script src="{% static 'planner/responsive_filters.js' %}?v=1" defer></script><script src="{% static 'food/inventory.js' %}?v=20261026-nested" defer></script>{% endblock %}
@@ -1 +1 @@
{% if error %}<p role="alert">{{ error }}</p>{% elif plan %}<div class="inventory-preview"><h3>Consumption preview</h3><p>Available: {{ plan.total|floatformat:-3 }} {{ plan.unit }}</p><ul>{% for deduction in plan.deductions %}<li>{{ deduction.package.location.name }} · Pack {{ deduction.package.pk }}: −{{ deduction.amount|floatformat:-3 }} {{ deduction.unit }} ({{ deduction.after|floatformat:-3 }} left)</li>{% endfor %}</ul>{% if plan.shortage %}<p role="alert">Shortage: {{ plan.shortage|floatformat:-3 }} {{ plan.unit }}. Choose a partial use explicitly or cancel.</p>{% endif %}<form method="post" action="{% url 'food:inventory-use' product.pk %}">{% csrf_token %}<input type="hidden" name="amount" value="{{ plan.requested }}"><input type="hidden" name="unit" value="{{ plan.unit }}"><input type="hidden" name="package_id" value="{{ package_id }}">{% if plan.shortage %}<label><input type="checkbox" name="allow_partial" value="yes" required> Use only the available amount</label>{% endif %}<button class="button" type="submit" {% if not plan.deductions %}disabled{% endif %}>Confirm use</button><button type="button" onclick="this.closest('.inventory-preview').remove()">Cancel</button></form></div>{% endif %}
{% if error %}<p role="alert">{{ error }}</p>{% elif plan %}<div class="inventory-preview"><h3>Consumption preview</h3><p>Available: {{ plan.total|floatformat:-3 }} {{ plan.unit }}</p>{% if plan.equivalent_amount is not None %}<p>Calculated amount: {{ plan.equivalent_amount|floatformat:-3 }} {{ plan.equivalent_unit }}{% if plan.estimated_piece_weight %} <strong>(estimated from measured inner-pack weight)</strong>{% endif %}</p>{% endif %}<ul>{% for deduction in plan.deductions %}<li>{{ deduction.package.location.name }} · Package {{ deduction.package.pk }}: −{{ deduction.amount|floatformat:-3 }} {{ deduction.unit }} ({{ deduction.after|floatformat:-3 }} left){% if deduction.after_pieces is not None %} · {{ deduction.after_pieces }} {{ deduction.package.piece_label|default:'piece' }}{% if deduction.after_pieces != 1 %}s{% endif %} left{% endif %}{% if deduction.package.inner_packs_per_package %} · {{ deduction.after_inner_pack_balance }}{% endif %}</li>{% endfor %}</ul>{% if plan.nutrition %}<p><strong>Estimated nutrition for this use:</strong> {% for nutrient in plan.nutrition %}{{ nutrient.value|floatformat:-1 }} {{ nutrient.unit }} {{ nutrient.label|lower }}{% if not forloop.last %} · {% endif %}{% endfor %}</p>{% endif %}{% if plan.shortage %}<p role="alert">Shortage: {{ plan.shortage|floatformat:-3 }} {{ plan.unit }}. Choose a partial use explicitly or cancel.</p>{% endif %}<form method="post" action="{% url 'food:inventory-use' product.pk %}">{% csrf_token %}<input type="hidden" name="amount" value="{{ plan.requested }}"><input type="hidden" name="unit" value="{{ plan.unit }}"><input type="hidden" name="package_id" value="{{ package_id }}">{% if plan.shortage %}<label><input type="checkbox" name="allow_partial" value="yes" required> Use only the available amount</label>{% endif %}<button class="button" type="submit" {% if not plan.deductions %}disabled{% endif %}>Confirm use</button><button type="button" onclick="this.closest('.inventory-preview').remove()">Cancel</button></form></div>{% endif %}