Add monthly list and summary data

This commit is contained in:
Nyymix 2025-08-09 20:37:38 +03:00
parent fa19b593d8
commit 783e5e92cb
4 changed files with 92 additions and 13 deletions

View file

@ -24,7 +24,7 @@
{% endfor %}
<button type="submit" class="uk-button uk-button-primary">Save</button>
<a href="{% url 'fuelpurchase_list' %}" class="uk-button uk-button-default">Cancel</a>
<a href="{% url 'fuelpurchase_list_current' %}" class="uk-button uk-button-default">Cancel</a>
</form>

View file

@ -1,20 +1,53 @@
{% extends "base.html" %}
{% block title %}Fuel Purchases{% endblock %}
{% block title %}Fuel Purchases - {{ current_month }}.{{ current_year }}{% endblock %}
{% block content %}
<div class="uk-flex uk-flex-between uk-flex-middle uk-margin-bottom">
<h2 class="uk-heading-line"><span>Fuel Purchases</span></h2>
<h2 class="uk-heading-line">
<span>Fuel Purchases {{ current_month }}.{{ current_year }}</span>
</h2>
<a href="{% url 'fuelpurchase_add' %}" class="uk-button uk-button-primary">Add Purchase</a>
</div>
<!-- Navigointi ja alasvetovalikko -->
<div class="uk-flex uk-flex-between uk-flex-middle uk-margin-small-bottom">
<a href="{% url 'fuelpurchase_list' prev_year prev_month %}" class="uk-button uk-button-default">&larr; Previous</a>
<form method="get" action="" onChange="if(this.value) window.location.href=this.value;" class="uk-form-horizontal">
<select class="uk-select">
{% for month_date in available_months %}
<option value="{% url 'fuelpurchase_list' month_date.year month_date.month %}"
{% if month_date.year == current_year and month_date.month == current_month %}selected{% endif %}>
{{ month_date|date:"F Y" }}
</option>
{% endfor %}
</select>
</form>
<a href="{% url 'fuelpurchase_list' next_year next_month %}" class="uk-button uk-button-default">Next &rarr;</a>
</div>
<!-- Yhteenveto -->
<div class="uk-card uk-card-default uk-card-body uk-margin-bottom">
<h4 class="uk-card-title">Summary</h4>
<ul class="uk-list uk-list-divider">
<li><strong>Total Cost:</strong> {{ summary.total_cost_sum|default:"0.00" }} €</li>
<li><strong>Total Litres:</strong> {{ summary.total_litres_sum|floatformat:2|default:"0.00" }} L</li>
<li><strong>Average Litre Price:</strong> {{ summary.avg_price|floatformat:3|default:"0.000" }} €</li>
<li><strong>Min Litre Price:</strong> {{ summary.min_price|floatformat:3|default:"0.000" }} €</li>
<li><strong>Max Litre Price:</strong> {{ summary.max_price|floatformat:3|default:"0.000" }} €</li>
</ul>
</div>
<!-- Taulukko -->
{% if purchases %}
<table class="uk-table uk-table-divider uk-table-striped uk-table-hover">
<thead>
<tr>
<th>Date</th>
<th>Total Cost (€)</th>
<th>Price/Litre (€)</th>
<th>Litre Price (€)</th>
<th>Amount (litres)</th>
</tr>
</thead>
@ -30,6 +63,6 @@
</tbody>
</table>
{% else %}
<p class="uk-text-meta">No purchases recorded yet.</p>
<p class="uk-text-meta">No purchases recorded for this month.</p>
{% endif %}
{% endblock %}

View file

@ -3,6 +3,7 @@ from django.urls import path
from . import views
urlpatterns = [
path("", views.FuelPurchaseListView.as_view(), name="fuelpurchase_list"),
path("", views.FuelPurchaseMonthlyListView.as_view(), name="fuelpurchase_list_current"),
path("<int:year>/<int:month>/", views.FuelPurchaseMonthlyListView.as_view(), name="fuelpurchase_list"),
path("add/", views.FuelPurchaseCreateView.as_view(), name="fuelpurchase_add"),
]

View file

@ -1,21 +1,66 @@
from calendar import monthrange
from datetime import date, timedelta
from django.db.models import Avg, Max, Min, Sum
from django.http import HttpResponse
from django.shortcuts import render
from django.urls import reverse_lazy
from django.utils import timezone
from django.views.generic import CreateView, ListView
from .forms import FuelPurchaseForm
from .models import FuelPurchase
def index(request):
return HttpResponse("Hello, world.")
class FuelPurchaseListView(ListView):
class FuelPurchaseMonthlyListView(ListView):
model = FuelPurchase
template_name = "main/fuelpurchase_list.html"
template_name = "fuelpurchase_list.html"
context_object_name = "purchases"
ordering = ['-purchase_date']
def get_queryset(self):
year = int(self.kwargs.get("year", timezone.now().year))
month = int(self.kwargs.get("month", timezone.now().month))
self.current_year = year
self.current_month = month
start_date = date(year, month, 1)
end_date = date(year, month, monthrange(year, month)[1])
return FuelPurchase.objects.filter(
purchase_date__range=(start_date, end_date)
).order_by("-purchase_date")
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
qs = self.object_list
summary = qs.aggregate(
total_cost_sum=Sum("total_cost"),
total_litres_sum=Sum("amount_litres"),
avg_price=Avg("price_per_litre"),
min_price=Min("price_per_litre"),
max_price=Max("price_per_litre"),
)
context["summary"] = summary
context["current_year"] = self.current_year
context["current_month"] = self.current_month
# Kuukausinavigointi
current_date = date(self.current_year, self.current_month, 1)
prev_month = current_date - timedelta(days=1)
next_month = (current_date.replace(day=28) + timedelta(days=4)).replace(day=1)
context["prev_year"] = prev_month.year
context["prev_month"] = prev_month.month
context["next_year"] = next_month.year
context["next_month"] = next_month.month
# Kaikki kuukaudet alasvetovalikkoon
all_dates = FuelPurchase.objects.dates("purchase_date", "month", order="DESC")
context["available_months"] = all_dates
return context
class FuelPurchaseCreateView(CreateView):