Neue Bestellübersicht erstellt. /bestellungen/summary
This commit is contained in:
@@ -48,3 +48,54 @@ class SpeiseplanView(TemplateView):
|
||||
context['next_date'] = next_tag.datum.strftime('%Y-%m-%d') if next_tag else None
|
||||
|
||||
return context
|
||||
|
||||
|
||||
from django.views.generic import TemplateView
|
||||
from django.utils import timezone
|
||||
from django.db.models import Count, Sum
|
||||
from datetime import datetime
|
||||
from .models import Bestellung, SpeiseplanTag, Gericht, Menue
|
||||
|
||||
class BestellSummaryView(TemplateView):
|
||||
template_name = 'mensa_app/bestell_summary.html'
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super().get_context_data(**kwargs)
|
||||
|
||||
# 1. Datum aus der URL holen (wie beim Speiseplan)
|
||||
date_str = self.request.GET. get('datum')
|
||||
if date_str:
|
||||
try:
|
||||
target_date = datetime.strptime(date_str, '%Y-%m-%d').date()
|
||||
except ValueError:
|
||||
target_date = timezone.now().date()
|
||||
else:
|
||||
target_date = timezone.now().date()
|
||||
|
||||
context['target_date'] = target_date
|
||||
|
||||
# 2. Aggregation: Bestellungen nach Gericht gruppieren
|
||||
# Wir suchen alle Bestellungen, deren Menü am target_date stattfindet
|
||||
summary_stats = (
|
||||
Bestellung.objects.filter(menue__tag__datum=target_date)
|
||||
.values('menue__gericht__name', 'menue__gericht__kategorie__name') # Gruppierung nach Name & Kategorie
|
||||
.annotate(
|
||||
anzahl=Count('id'), # Wie viele wurden bestellt?
|
||||
umsatz=Sum('menue__preis') # Was macht das für einen Umsatz?
|
||||
)
|
||||
.order_by('menue__gericht__name')
|
||||
)
|
||||
context['summary_stats'] = summary_stats
|
||||
|
||||
# 3. Pager-Logik (identisch mit dem Speiseplan-View)
|
||||
prev_tag = SpeiseplanTag.objects.filter(datum__lt=target_date).order_by('-datum').first()
|
||||
next_tag = SpeiseplanTag.objects.filter(datum__gt=target_date).order_by('datum').first()
|
||||
|
||||
context['prev_date'] = prev_tag.datum.strftime('%Y-%m-%d') if prev_tag else None
|
||||
context['next_date'] = next_tag.datum.strftime('%Y-%m-%d') if next_tag else None
|
||||
|
||||
# 4. Gesamtzahlen für die Übersicht
|
||||
context['total_bestellungen'] = Bestellung.objects.filter(menue__tag__datum=target_date).count()
|
||||
context['total_umsatz'] = Bestellung.objects.filter(menue__tag__datum=target_date).aggregate(Sum('menue__preis'))['menue__preis__sum'] or 0
|
||||
|
||||
return context
|
||||
|
||||
Reference in New Issue
Block a user