Added inventory amount per food package management

This commit is contained in:
Joren de Graaf
2026-10-11 15:46:22 +02:00
parent 400e8254c0
commit cb12c23732
19 changed files with 927 additions and 35 deletions
@@ -1,6 +1,12 @@
{% extends 'planner/base.html' %}{% load static %}
{% block title %}Kitchen Inventory · Tasknest{% endblock %}
{% block content %}<link rel="stylesheet" href="{% static 'food/food.css' %}">
{% block content %}<link rel="stylesheet" href="{% static 'food/food.css' %}?v=20261011-packages">
<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><a class="button" href="{% url 'food:inventory-add' %}">+ Add item</a></header>
<form method="get" class="food-card inventory-filters"><label>Search<input type="search" name="q" value="{{ query }}"></label><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</button></form>
{% for item in items %}{% if sort == 'location' %}{% ifchanged item.location_id %}<h2 class="inventory-group">{{ item.location.name }}</h2>{% endifchanged %}{% endif %}<article class="food-card inventory-row">{% if item.product.image_url %}<img src="{{ item.product.image_url }}" alt="" loading="lazy">{% endif %}<div class="inventory-row-main"><h3>{{ item.product.product_name }}</h3>{% if item.product.brand %}<p>{{ item.product.brand }}</p>{% endif %}<p>Qty: {{ item.quantity }} · {{ item.location.name }}{% if item.category %} · {{ item.category.name }}{% endif %}</p>{% if item.opened %}<span class="inventory-tag">Opened</span>{% endif %}{% if item.best_before %}<span class="inventory-tag {% if item.best_before < today %}inventory-expired{% elif item.best_before <= soon_date %}inventory-soon{% endif %}">{% if item.best_before < today %}Expired{% elif item.best_before <= soon_date %}Use soon{% else %}Best before{% endif %} {{ item.best_before }}</span>{% endif %}</div><div class="inventory-row-actions"><form method="post" action="{% url 'food:inventory-quantity' item.pk %}">{% csrf_token %}<button type="submit" name="action" value="increase" aria-label="Increase quantity of {{ item.product.product_name }}">+1</button><button type="submit" name="action" value="used">Used up</button></form><a href="{% url 'food:inventory-edit' item.pk %}">Edit</a></div></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 %}</div>{% endblock %}
{% 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>
<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>
</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>{% endblock %}
@@ -0,0 +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 %}