mirror of
https://github.com/ae-utbm/sith.git
synced 2025-07-09 19:40:19 +00:00
Monthly user account
This commit is contained in:
@ -11,6 +11,7 @@ from django.forms.models import modelform_factory
|
||||
from django.forms import CheckboxSelectMultiple
|
||||
from django.template.response import TemplateResponse
|
||||
from django.conf import settings
|
||||
from django.views.generic.dates import YearMixin, MonthMixin
|
||||
|
||||
from django.utils import timezone
|
||||
from datetime import timedelta, datetime, date
|
||||
@ -300,24 +301,31 @@ class UserToolsView(UserTabsMixin, TemplateView):
|
||||
kwargs['object'] = self.request.user
|
||||
return kwargs
|
||||
|
||||
class UserAccountView(UserTabsMixin, DetailView):
|
||||
class UserAccountBase(UserTabsMixin, DetailView):
|
||||
"""
|
||||
Display a user's account
|
||||
Base class for UserAccount
|
||||
"""
|
||||
|
||||
model = User
|
||||
pk_url_kwarg = "user_id"
|
||||
template_name = "core/user_account.jinja"
|
||||
current_tab = "account"
|
||||
|
||||
def dispatch(self, request, *arg, **kwargs): # Manually validates the rights
|
||||
res = super(UserAccountView, self).dispatch(request, *arg, **kwargs)
|
||||
res = super(UserAccountBase, self).dispatch(request, *arg, **kwargs)
|
||||
if (self.object == request.user
|
||||
or request.user.is_in_group(settings.SITH_GROUPS['accounting-admin']['name'])
|
||||
or request.user.is_root):
|
||||
return res
|
||||
raise PermissionDenied
|
||||
|
||||
def expense_by_month(self):
|
||||
class UserAccountView(UserAccountBase):
|
||||
"""
|
||||
Display a user's account
|
||||
"""
|
||||
|
||||
template_name = "core/user_account.jinja"
|
||||
|
||||
def expense_by_month(self, obj, calc):
|
||||
stats = []
|
||||
joined = self.object.date_joined.year
|
||||
|
||||
@ -329,13 +337,13 @@ class UserAccountView(UserTabsMixin, DetailView):
|
||||
for y in years:
|
||||
stats.append([])
|
||||
for m in months:
|
||||
q = self.object.customer.buyings.filter(
|
||||
q = obj.filter(
|
||||
date__year=joined + y,
|
||||
date__month=m,
|
||||
)
|
||||
stats[y].append(
|
||||
(
|
||||
sum([p.unit_price * p.quantity for p in q]),
|
||||
sum([calc(p) for p in q]),
|
||||
date(joined + y, m, 17)
|
||||
)
|
||||
)
|
||||
@ -343,15 +351,55 @@ class UserAccountView(UserTabsMixin, DetailView):
|
||||
print(stats)
|
||||
return stats
|
||||
|
||||
def buyings_calc(self, query):
|
||||
return query.unit_price * query.quantity
|
||||
|
||||
def invoices_calc(self, query):
|
||||
t = 0
|
||||
for it in query.items.all():
|
||||
t += it.quantity * it.product_unit_price
|
||||
return t
|
||||
|
||||
def refilling_calc(self, query):
|
||||
return query.amount
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
kwargs = super(UserAccountView, self).get_context_data(**kwargs)
|
||||
kwargs['profile'] = self.object
|
||||
try:
|
||||
kwargs['customer'] = self.object.customer
|
||||
kwargs['selling_months'] = self.expense_by_month()
|
||||
kwargs['buyings_month'] = self.expense_by_month(
|
||||
self.object.customer.buyings,
|
||||
self.buyings_calc
|
||||
)
|
||||
kwargs['invoices_month'] = self.expense_by_month(
|
||||
self.object.customer.user.invoices,
|
||||
self.invoices_calc
|
||||
)
|
||||
kwargs['refilling_month'] = self.expense_by_month(
|
||||
self.object.customer.refillings,
|
||||
self.refilling_calc
|
||||
)
|
||||
except:
|
||||
pass
|
||||
# TODO: add list of month where account has activity
|
||||
return kwargs
|
||||
|
||||
|
||||
|
||||
class UserAccountDetailView(UserAccountBase, YearMixin, MonthMixin):
|
||||
"""
|
||||
Display a user's account for month
|
||||
"""
|
||||
|
||||
template_name = "core/user_account_detail.jinja"
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
kwargs = super(UserAccountDetailView, self).get_context_data(**kwargs)
|
||||
kwargs['profile'] = self.object
|
||||
try:
|
||||
kwargs['customer'] = self.object.customer
|
||||
except:
|
||||
pass
|
||||
kwargs['tab'] = "account"
|
||||
return kwargs
|
||||
|
Reference in New Issue
Block a user