Skip to content

Commit

Permalink
Added overview and export written off orders
Browse files Browse the repository at this point in the history
  • Loading branch information
SimplyPancake committed Oct 8, 2024
1 parent eb71637 commit 9269ca8
Show file tree
Hide file tree
Showing 6 changed files with 869 additions and 277 deletions.
1 change: 1 addition & 0 deletions alexia/apps/billing/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
urlpatterns = [
url(r'^order/$', views.OrderListView.as_view(), name='orders'),
url(r'^order/(?P<pk>[0-9]+)/$', views.OrderDetailView.as_view(), name='event-orders'),
url(r'^order/writeoff/(?P<pk>[0-9]+)/$', views.WriteOffExportView.as_view(), name='writeoff_export'),
url(r'^order/export/$', views.OrderExportView.as_view(), name='export-orders'),
url(r'^stats/(?P<year>[0-9]{4})/$', views.OrderYearView.as_view(), name='year-orders'),
url(r'^stats/(?P<year>[0-9]{4})/(?P<month>[0-9]{1,2})/$', views.OrderMonthView.as_view(), name='month-orders'),
Expand Down
33 changes: 30 additions & 3 deletions alexia/apps/billing/views.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
from django.conf import settings
from django.contrib.auth.mixins import LoginRequiredMixin
from django.core.exceptions import PermissionDenied
from django.core import serializers
from django.db.models import Count, Sum
from django.db.models.functions import ExtractYear, TruncMonth
from django.http import Http404, HttpResponseRedirect
from django.http import Http404, JsonResponse, HttpResponseRedirect
from django.shortcuts import get_object_or_404, render
from django.urls import reverse, reverse_lazy
from django.utils.dates import MONTHS
from django.utils.translation import ugettext as _
from django.views import View
from django.views.generic.base import RedirectView, TemplateView
from django.views.generic.detail import DetailView, SingleObjectMixin
from django.views.generic.edit import (
Expand All @@ -33,8 +35,9 @@
OrganizationFilterMixin, OrganizationFormMixin,
)

from .models import Order, Purchase, WriteoffCategory

from .models import Order, Purchase, WriteOffPurchase, WriteoffCategory
from alexia.utils.writeoff import get_writeoff_products
import json

class JulianaView(TenderRequiredMixin, DetailView):
template_name = 'billing/juliana.html'
Expand Down Expand Up @@ -128,14 +131,38 @@ def get_context_data(self, **kwargs):
.values('product') \
.annotate(amount=Sum('amount'), price=Sum('price'))

writeoff_exists = self.object.writeoff_orders.exists

if writeoff_exists:
grouped_writeoff_products = get_writeoff_products(self.object, WriteOffPurchase.objects)

context = super(OrderDetailView, self).get_context_data(**kwargs)
context.update({
'orders': self.object.orders.select_related('authorization__user').order_by('-placed_at'),
'products': products,
'revenue': products.aggregate(Sum('price'))['price__sum'],
'writeoff_exists': writeoff_exists,
'grouped_writeoff_data': grouped_writeoff_products
})

return context

class WriteOffExportView(ManagerRequiredMixin, DenyWrongOrganizationMixin, View):
organization_field = 'organizer'

# get event
def get(self, request, *args, **kwargs):
event_pk = kwargs.get('pk') # Extract the pk from kwargs
event = get_object_or_404(Event, pk=event_pk) # Fetch the Event or raise 404 if not found

writeoff_exists = event.writeoff_orders.exists

if not writeoff_exists:
raise Http404

grouped_writeoff_products = get_writeoff_products(event, WriteOffPurchase.objects)

return JsonResponse(grouped_writeoff_products)

class OrderExportView(ManagerRequiredMixin, FormView):
template_name = 'billing/order_export_form.html'
Expand Down
21 changes: 21 additions & 0 deletions alexia/utils/writeoff.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from collections import defaultdict
from django.db.models import Sum

def get_writeoff_products(event, writeoff_purchases):
writeoff_products = writeoff_purchases.filter(order__event=event) \
.values('order__writeoff_category__name', 'product') \
.annotate(total_amount=Sum('amount'), total_price=Sum('price')) \
.order_by('order__writeoff_category__name', 'product')

# Group writeoffs by category
grouped_writeoff_products = defaultdict(lambda: {'products': [], 'total_amount': 0, 'total_price': 0})

# calculate total product amount and total price per category
for product in writeoff_products:
category_name = product['order__writeoff_category__name']
grouped_writeoff_products[category_name]['products'].append(product)
grouped_writeoff_products[category_name]['total_amount'] += product['total_amount']
grouped_writeoff_products[category_name]['total_price'] += product['total_price']

# Convert defaultdict to a regular dict for easier template use
return dict(grouped_writeoff_products)
Binary file modified locale/nl/LC_MESSAGES/django.mo
Binary file not shown.
Loading

0 comments on commit 9269ca8

Please sign in to comment.