diff --git a/config/urls.py b/config/urls.py index 308cc4b..becb61a 100644 --- a/config/urls.py +++ b/config/urls.py @@ -73,6 +73,7 @@ urlpatterns = [ path("bestand//loeschen/", views.inventory_delete, name="inventory_delete"), path("bestand//anbieten/", views.marketplace_offer, name="marketplace_offer"), path("bestellung/", views.orders, name="orders"), + path("bestellung/einkaufsliste/", views.order_shopping_list, name="order_shopping_list"), path("bestellung/historie/", views.order_history, name="order_history"), path( "bestellung/historie//uebergabe-rueckgaengig/", diff --git a/filament/middleware.py b/filament/middleware.py index e301874..02e6b54 100644 --- a/filament/middleware.py +++ b/filament/middleware.py @@ -16,6 +16,7 @@ class PrivatePageMiddleware: "empty_spools", "shared_inventory", "orders", + "order_shopping_list", "order_history", "marketplace", "marketplace_take", diff --git a/filament/static/filament/app.css b/filament/static/filament/app.css index 88631c7..81af973 100644 --- a/filament/static/filament/app.css +++ b/filament/static/filament/app.css @@ -325,6 +325,20 @@ main { gap: 20px; margin-bottom: clamp(22px, 3vw, 36px); } +.order-page-actions, +.order-shop-links { + display: flex; + flex-wrap: wrap; + gap: 10px; +} +.article-copy { + display: flex; + align-items: center; + gap: 10px; +} +.article-copy [hidden] { + display: none; +} .eyebrow { letter-spacing: 2px; font-size: 10px; @@ -1222,6 +1236,9 @@ td.quantity { .page-heading > .button { margin-top: 16px; } + .order-page-actions { + margin-top: 16px; + } .stats { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; diff --git a/filament/static/filament/order_shopping_list.js b/filament/static/filament/order_shopping_list.js new file mode 100644 index 0000000..d97bff8 --- /dev/null +++ b/filament/static/filament/order_shopping_list.js @@ -0,0 +1,30 @@ +"use strict"; + +const copyStatus = document.querySelector("[data-copy-status]"); + +for (const button of document.querySelectorAll("[data-copy-article]")) { + button.hidden = false; + let resetLabel; + button.addEventListener("click", async () => { + const article = document.getElementById(button.dataset.copyArticle); + const code = article.textContent.trim(); + clearTimeout(resetLabel); + copyStatus.textContent = ""; + try { + await navigator.clipboard.writeText(code); + button.textContent = "Kopiert"; + copyStatus.textContent = `Artikelnummer ${code} kopiert.`; + } catch { + const range = document.createRange(); + range.selectNodeContents(article); + const selection = window.getSelection(); + selection.removeAllRanges(); + selection.addRange(range); + button.textContent = "Markiert"; + copyStatus.textContent = "Artikelnummer markiert. Kopiere die Auswahl."; + } + resetLabel = setTimeout(() => { + button.textContent = "Kopieren"; + }, 2000); + }); +} diff --git a/filament/tests/test_orders.py b/filament/tests/test_orders.py index 3b025b1..ae16908 100644 --- a/filament/tests/test_orders.py +++ b/filament/tests/test_orders.py @@ -1,17 +1,76 @@ from datetime import UTC, datetime from unittest.mock import patch +from urllib.parse import parse_qs, urlsplit from django.contrib.auth import get_user_model from django.test import Client from django.urls import reverse -from filament.models import InventoryItem, OrderItem, StockMovement +from filament.models import Color, InventoryItem, OrderItem, StockMovement, Variant from filament.services import add_quantity from .base import CatalogTestCase class SharedOrderTests(CatalogTestCase): + def test_shopping_list_combines_open_quantities_and_preserves_spool_variants(self): + url = reverse("order_shopping_list") + self.assertRedirects(self.client.get(url), f"/anmelden/?next={url}") + Variant.objects.filter(pk__in=[self.spool.pk, self.refill.pk]).update( + manufacturer_color_code="10100" + ) + Variant.objects.filter(pk=self.black_spool.pk).update(manufacturer_color_code="10101") + OrderItem.objects.create(owner=self.alice, variant=self.spool, quantity=1) + OrderItem.objects.create(owner=self.bob, variant=self.spool, quantity=2) + OrderItem.objects.create(owner=self.alice, variant=self.refill, quantity=1) + OrderItem.objects.create(owner=self.bob, variant=self.black_spool, quantity=1) + for status in ( + OrderItem.Status.ORDERED, + OrderItem.Status.ARRIVED, + OrderItem.Status.HANDED_OVER, + ): + OrderItem.objects.create(owner=self.bob, variant=self.spool, quantity=4, status=status) + + self.client.force_login(self.alice) + self.assertContains(self.client.get(reverse("orders")), url) + result = self.client.get(url, {"q": "10100"}) + self.assertEqual( + {variant.pk: variant.order_quantity for variant in result.context["variants"]}, + {self.spool.pk: 3, self.refill.pk: 1}, + ) + self.assertContains(result, 'aria-label="Artikelnummer 10100 kopieren"', count=2) + self.assertEqual(result.context["total"], 4) + self.assertEqual( + result.context["shop_searches"], + ["https://eu.store.bambulab.com/search?q=10100"], + ) + self.assertEqual(self.client.get(url, {"q": "10100, 10101"}).context["total"], 5) + self.assertEqual(self.client.post(url).status_code, 405) + + def test_shopping_list_shop_search_batches_distinct_codes(self): + codes = [str(10800 + offset) for offset in range(13)] + for code in codes: + variant = Variant.objects.create( + product_type=self.product, + color=Color.objects.create(name=code), + spool_type=Variant.SpoolType.SPOOL, + manufacturer_color_code=code, + ) + OrderItem.objects.create(owner=self.alice, variant=variant, quantity=1) + OrderItem.objects.create( + owner=self.alice, variant=self.spool, quantity=2, status=OrderItem.Status.UNAVAILABLE + ) + self.client.force_login(self.bob) + url = reverse("order_shopping_list") + result = self.client.get(url) + batches = [ + parse_qs(urlsplit(link).query)["q"][0].split(", ") + for link in result.context["shop_searches"] + ] + self.assertEqual(batches, [codes[:12], codes[12:]]) + self.assertContains(result, "Im Bambu-Shop suchen (2/2)") + self.assertEqual(result.context["total"], 13) + def test_history_is_shared_and_records_order_and_handover_times(self): url = reverse("order_history") self.assertRedirects(self.client.get(url), f"/anmelden/?next={url}") diff --git a/filament/views.py b/filament/views.py index 2723d75..4c50adc 100644 --- a/filament/views.py +++ b/filament/views.py @@ -1,6 +1,7 @@ import re -from itertools import groupby +from itertools import batched, groupby from pathlib import PurePosixPath +from urllib.parse import urlencode from uuid import UUID, uuid4 from django.contrib import messages @@ -940,6 +941,54 @@ def orders(request): ) +@login_required +@require_http_methods(["GET", "HEAD"]) +def order_shopping_list(request): + search = request.GET.get("q", "").strip()[:200] + codes = {code.casefold() for code in re.split(r"[\s,;]+", search) if code} + if search: + codes.add(search.casefold()) + variants = list( + Variant.objects.filter(orderitem__status=OrderItem.Status.OPEN) + .select_related("product_type__manufacturer", "color") + .annotate(order_quantity=Sum("orderitem__quantity")) + .order_by( + "product_type__manufacturer__name", + "product_type__name", + "color__name", + "spool_type", + "pk", + ) + ) + if search: + variants = [ + variant + for variant in variants + if variant.manufacturer_color_code.strip().casefold() in codes + ] + bambu_codes = dict.fromkeys( + variant.manufacturer_color_code.strip() + for variant in variants + if variant.manufacturer.name.casefold() == "bambu lab" + and re.fullmatch(r"[0-9]{5}", variant.manufacturer_color_code.strip()) + ) + # The shop accepted a dozen codes in one search. A full order can exceed its limit. + shop_searches = [ + "https://eu.store.bambulab.com/search?" + urlencode({"q": ", ".join(batch)}) + for batch in batched(bambu_codes, 12) + ] + return render( + request, + "filament/order_shopping_list.html", + { + "variants": variants, + "total": sum(variant.order_quantity for variant in variants), + "search": search, + "shop_searches": shop_searches, + }, + ) + + @login_required @require_http_methods(["POST"]) def order_status(request, pk): diff --git a/templates/filament/order_shopping_list.html b/templates/filament/order_shopping_list.html new file mode 100644 index 0000000..afef75f --- /dev/null +++ b/templates/filament/order_shopping_list.html @@ -0,0 +1,59 @@ +{% extends "base.html" %} +{% load static %} +{% block title %}Konsolidierte Bestellliste · Fil-A-Management{% endblock %} +{% block content %} +← Sammelbestellung nach Person +
+

Konsolidierte Bestellliste

Offene Bestellwünsche aller Personen, zusammengezählt pro Variante.

+
+{% include "filament/list_search.html" with search_url="order_shopping_list" search_label="Nach einer oder mehreren Artikelnummern suchen" placeholder="Artikelnummern, z. B. 10100, 10604" %} +{% if shop_searches %} + +

Prüfe im Shop Farbe, Spulenausführung und Stückzahl anhand dieser Liste.

+{% endif %} +{% if variants %} +
+
+

{% if search %}Suchergebnis{% else %}Offene Bestellwünsche{% endif %}

+ {{ total }} Rolle{{ total|pluralize:"n" }} · {{ variants|length }} Variante{{ variants|length|pluralize:"n" }} +
+
+ + + + {% for variant in variants %} + {% with article=variant.manufacturer_color_code.strip %} + + + + + + + + {% endwith %} + {% endfor %} + +
StückProduktartFarbeSpulenausführungArtikelnr.
{{ variant.order_quantity }}{{ variant.product_type.name }}{{ variant.manufacturer }}
{% include "filament/variant_thumbnail.html" with variant=variant only %}{{ variant.color_label }}
+ {{ variant.get_spool_type_display }} + {% if variant.net_weight_g or variant.diameter_mm %}{% if variant.net_weight_g %}{{ variant.net_weight_g }} g{% endif %}{% if variant.net_weight_g and variant.diameter_mm %} · {% endif %}{% if variant.diameter_mm %}{{ variant.diameter_mm }} mm{% endif %}{% endif %} + {% if not variant.is_active %}Katalogvariante deaktiviert{% endif %} + + {% if article %} +
+ {{ article }} + +
+ {% else %}–{% endif %} +
+
+

+
+ +{% else %} +

{% if search %}Keine offenen Wünsche zu diesen Artikelnummern{% else %}Keine offenen Bestellwünsche{% endif %}

+{% endif %} +{% endblock %} diff --git a/templates/filament/orders.html b/templates/filament/orders.html index 5f7e371..74ce1ac 100644 --- a/templates/filament/orders.html +++ b/templates/filament/orders.html @@ -3,7 +3,10 @@ {% block content %}

Sammelbestellung

Bestellwünsche und Lieferstatus pro Person.

- + Bestellwunsch hinzufügen +
Rollen insgesamt{{ total }}