From a64b10776ea424cd4fd962086cc12c9fcba7e7f3 Mon Sep 17 00:00:00 2001 From: klmp200 Date: Tue, 6 Sep 2016 21:47:15 +0200 Subject: [PATCH] Monthly user account --- core/templates/core/user_account.jinja | 130 ++++-------------- core/templates/core/user_account_detail.jinja | 102 ++++++++++++++ core/urls.py | 1 + core/views/user.py | 64 +++++++-- 4 files changed, 188 insertions(+), 109 deletions(-) create mode 100644 core/templates/core/user_account_detail.jinja diff --git a/core/templates/core/user_account.jinja b/core/templates/core/user_account.jinja index d36f5432..dfc3cdd8 100644 --- a/core/templates/core/user_account.jinja +++ b/core/templates/core/user_account.jinja @@ -1,5 +1,31 @@ {% extends "core/base.jinja" %} +{% macro monthly(obj) %} + + + + + + + + + + {% for array in obj %} + {% for tuple in array %} + {% if tuple[0] != 0 %} + {% set link=url('core:user_account_detail', user_id=profile.id, year=tuple[1].year, month=tuple[1].month) %} + + + + + + {% endif %} + {% endfor %} + {% endfor %} + +
{% trans %}Year{% endtrans %}{% trans %}Month{% endtrans %}{% trans %}Total{% endtrans %}
{{ tuple[1].year }}{{ tuple[1]|date("E") }}{{ tuple[0] }} €
+{% endmacro %} + {% block title %} {% trans user_name=profile.get_display_name() %}{{ user_name }}'s account{% endtrans %} {% endblock %} @@ -10,114 +36,16 @@

{% trans %}Amount: {% endtrans %}{{ customer.amount }} €

{% if customer.refillings.exists() %}

{% trans %}Refillings{% endtrans %}

- - - - - - - - - - - - {% for i in customer.refillings.order_by('-date').all() %} - - - - - - - {% if i.is_owned_by(user) %} - - {% endif %} - -{% endfor %} - -
{% trans %}Date{% endtrans %}{% trans %}Counter{% endtrans %}{% trans %}Barman{% endtrans %}{% trans %}Amount{% endtrans %}{% trans %}Payment method{% endtrans %}
{{ i.date|localtime|date(DATETIME_FORMAT) }} - {{ i.date|localtime|time(DATETIME_FORMAT) }}{{ i.counter }}{{ i.operator.get_display_name() }}{{ i.amount }} €{{ i.get_payment_method_display() }}Delete
+ {{ monthly(refilling_month) }} {% endif %} {% if customer.buyings.exists() %}

{% trans %}Account buyings{% endtrans %}

- - - - - - - - - - - - - - {% for i in customer.buyings.order_by('-date').all() %} - - - - - - - - - {% if i.is_owned_by(user) %} - - {% endif %} - -{% endfor %} - -
{% trans %}Date{% endtrans %}{% trans %}Counter{% endtrans %}{% trans %}Barman{% endtrans %}{% trans %}Label{% endtrans %}{% trans %}Quantity{% endtrans %}{% trans %}Total{% endtrans %}{% trans %}Payment method{% endtrans %}
{{ i.date|localtime|date(DATETIME_FORMAT) }} - {{ i.date|localtime|time(DATETIME_FORMAT) }}{{ i.counter }}{{ i.seller.get_display_name() }}{{ i.label }}{{ i.quantity }}{{ i.quantity * i.unit_price }} €{{ i.get_payment_method_display() }}Delete
+ {{ monthly(buyings_month) }} {% endif %} {% if customer.user.invoices.exists() %}

{% trans %}Eboutic invoices{% endtrans %}

- - - - - - - - - - {% for i in customer.user.invoices.order_by('-date').all() %} - - - - - -{% endfor %} - -
{% trans %}Date{% endtrans %}{% trans %}Items{% endtrans %}{% trans %}Amount{% endtrans %}
{{ i.date|localtime|date(DATETIME_FORMAT) }} - {{ i.date|localtime|time(DATETIME_FORMAT) }} -
    - {% for it in i.items.all() %} -
  • {{ it.quantity }} x {{ it.product_name }} - {{ it.product_unit_price }} €
  • - {% endfor %} -
-
{{ i.get_total() }} €
+ {{ monthly(invoices_month) }} {% endif %} -

{% trans %}Account buyings{% endtrans %}

- - - - - - - - - - {% for array in selling_months %} - {% for tuple in array %} - {% if tuple[0] != 0 %} - - - - - - {% endif %} - {% endfor %} - {% endfor %} - -
{% trans %}Year{% endtrans %}{% trans %}Month{% endtrans %}{% trans %}Total{% endtrans %}
{{ tuple[1].year }}{{ tuple[1]|date("E") }}{{ tuple[0] }} €
{% else %}

{% trans %}User has no account{% endtrans %}

{% endif %} diff --git a/core/templates/core/user_account_detail.jinja b/core/templates/core/user_account_detail.jinja new file mode 100644 index 00000000..5c352b3c --- /dev/null +++ b/core/templates/core/user_account_detail.jinja @@ -0,0 +1,102 @@ +{% extends "core/base.jinja" %} + +{% block title %} +{% trans user_name=profile.get_display_name() %}{{ user_name }}'s account{% endtrans %} +{% endblock %} + +{% block content %} +{% if customer %} +

{% trans %}User account{% endtrans %}

+

{% trans %}Amount: {% endtrans %}{{ customer.amount }} €

+{% if customer.refillings.exists() %} +

{% trans %}Refillings{% endtrans %}

+ + + + + + + + + + + + {% for i in customer.refillings.order_by('-date').all() %} + + + + + + + {% if i.is_owned_by(user) %} + + {% endif %} + +{% endfor %} + +
{% trans %}Date{% endtrans %}{% trans %}Counter{% endtrans %}{% trans %}Barman{% endtrans %}{% trans %}Amount{% endtrans %}{% trans %}Payment method{% endtrans %}
{{ i.date|localtime|date(DATETIME_FORMAT) }} - {{ i.date|localtime|time(DATETIME_FORMAT) }}{{ i.counter }}{{ i.operator.get_display_name() }}{{ i.amount }} €{{ i.get_payment_method_display() }}Delete
+{% endif %} +{% if customer.buyings.exists() %} +

{% trans %}Account buyings{% endtrans %}

+ + + + + + + + + + + + + + {% for i in customer.buyings.order_by('-date').all() %} + + + + + + + + + {% if i.is_owned_by(user) %} + + {% endif %} + +{% endfor %} + +
{% trans %}Date{% endtrans %}{% trans %}Counter{% endtrans %}{% trans %}Barman{% endtrans %}{% trans %}Label{% endtrans %}{% trans %}Quantity{% endtrans %}{% trans %}Total{% endtrans %}{% trans %}Payment method{% endtrans %}
{{ i.date|localtime|date(DATETIME_FORMAT) }} - {{ i.date|localtime|time(DATETIME_FORMAT) }}{{ i.counter }}{{ i.seller.get_display_name() }}{{ i.label }}{{ i.quantity }}{{ i.quantity * i.unit_price }} €{{ i.get_payment_method_display() }}Delete
+{% endif %} +{% if customer.user.invoices.exists() %} +

{% trans %}Eboutic invoices{% endtrans %}

+ + + + + + + + + + {% for i in customer.user.invoices.order_by('-date').all() %} + + + + + +{% endfor %} + +
{% trans %}Date{% endtrans %}{% trans %}Items{% endtrans %}{% trans %}Amount{% endtrans %}
{{ i.date|localtime|date(DATETIME_FORMAT) }} - {{ i.date|localtime|time(DATETIME_FORMAT) }} +
    + {% for it in i.items.all() %} +
  • {{ it.quantity }} x {{ it.product_name }} - {{ it.product_unit_price }} €
  • + {% endfor %} +
+
{{ i.get_total() }} €
+{% endif %} +{% else %} +

{% trans %}User has no account{% endtrans %}

+{% endif %} +

Retour

+{% endblock %} diff --git a/core/urls.py b/core/urls.py index baeae800..94767c20 100644 --- a/core/urls.py +++ b/core/urls.py @@ -37,6 +37,7 @@ urlpatterns = [ url(r'^user/(?P[0-9]+)/groups$', UserUpdateGroupView.as_view(), name='user_groups'), url(r'^user/tools/$', UserToolsView.as_view(), name='user_tools'), url(r'^user/(?P[0-9]+)/account$', UserAccountView.as_view(), name='user_account'), + url(r'^user/(?P[0-9]+)/account/(?P[0-9]+)/(?P[0-9]+)$', UserAccountDetailView.as_view(), name='user_account_detail'), url(r'^user/(?P[0-9]+)/stats$', UserStatsView.as_view(), name='user_stats'), # File views diff --git a/core/views/user.py b/core/views/user.py index e0b5950d..716a2ffd 100644 --- a/core/views/user.py +++ b/core/views/user.py @@ -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