From 6bb1e0e41807d63f4de01d2e3ec56c5eec1fcd76 Mon Sep 17 00:00:00 2001 From: Krophil Date: Wed, 14 Dec 2016 18:04:57 +0100 Subject: [PATCH] person bilan added and renamed classes --- .../templates/accounting/journal_bilan.jinja | 33 ----- .../accounting/journal_bilan_nature.jinja | 55 ++++++++ .../accounting/journal_bilan_person.jinja | 63 +++++++++ .../accounting/journal_details.jinja | 4 +- accounting/urls.py | 5 +- accounting/views.py | 133 ++++++++++++++++-- 6 files changed, 247 insertions(+), 46 deletions(-) delete mode 100644 accounting/templates/accounting/journal_bilan.jinja create mode 100644 accounting/templates/accounting/journal_bilan_nature.jinja create mode 100644 accounting/templates/accounting/journal_bilan_person.jinja diff --git a/accounting/templates/accounting/journal_bilan.jinja b/accounting/templates/accounting/journal_bilan.jinja deleted file mode 100644 index 7e3dc461..00000000 --- a/accounting/templates/accounting/journal_bilan.jinja +++ /dev/null @@ -1,33 +0,0 @@ -{% extends "core/base.jinja" %} - -{% block title %} -{% trans %}General journal:{% endtrans %} {{ object.name }} -{% endblock %} - - -{% block content %} -

{% trans %}Nature bilan: {% endtrans %} {{ object.name }}

- -

{% trans %}Credit{% endtrans %}

- - - - - - - - - - {% for key in bilan.keys() %} - - - - - {% endfor %} - - -
{% trans %}Nature of operation{% endtrans %}{% trans %}Sum{% endtrans %}
{{object.operations.get(accounting_type__code=key).accounting_type.label}}{{bilan[key]}}
- -

{% trans %}Debit{% endtrans %}

- -{% endblock %} \ No newline at end of file diff --git a/accounting/templates/accounting/journal_bilan_nature.jinja b/accounting/templates/accounting/journal_bilan_nature.jinja new file mode 100644 index 00000000..d8de0864 --- /dev/null +++ b/accounting/templates/accounting/journal_bilan_nature.jinja @@ -0,0 +1,55 @@ +{% extends "core/base.jinja" %} + +{% block title %} +{% trans %}General journal:{% endtrans %} {{ object.name }} +{% endblock %} + + +{% block content %} +

{% trans %}Nature bilan: {% endtrans %} {{ object.name }}

+ +

{% trans %}Credit{% endtrans %}

+ + + + + + + + + + {% for key in bilan_credit.keys() %} + + + + + {% endfor %} + + +
{% trans %}Nature of operation{% endtrans %}{% trans %}Sum{% endtrans %}
{{key}}{{bilan_credit[key]}}
+ +

Total : {{total_credit}}

+ +

{% trans %}Debit{% endtrans %}

+ + + + + + + + + + {% for key in bilan_debit.keys() %} + + + + + {% endfor %} + + +
{% trans %}Nature of operation{% endtrans %}{% trans %}Sum{% endtrans %}
{{key}}{{bilan_debit[key]}}
+ +

Total : {{total_debit}}

+ +{% endblock %} \ No newline at end of file diff --git a/accounting/templates/accounting/journal_bilan_person.jinja b/accounting/templates/accounting/journal_bilan_person.jinja new file mode 100644 index 00000000..8b913123 --- /dev/null +++ b/accounting/templates/accounting/journal_bilan_person.jinja @@ -0,0 +1,63 @@ +{% extends "core/base.jinja" %} + +{% block title %} +{% trans %}General journal:{% endtrans %} {{ object.name }} +{% endblock %} + + +{% block content %} +

{% trans %}Person bilan: {% endtrans %} {{ object.name }}

+ +

{% trans %}Credit{% endtrans %}

+ + + + + + + + + + {% for key in bilan_credit.keys() %} + + {% if key.target_type == "OTHER" %} + + {% else %} + + {% endif %} + + + {% endfor %} + + +
{% trans %}Target of the operation{% endtrans %}{% trans %}Sum{% endtrans %}
{{ o.target_label }}{{ key.get_display_name() }}{{bilan_credit[key]}}
+ +

Total : {{total_credit}}

+ +

{% trans %}Debit{% endtrans %}

+ + + + + + + + + + {% for key in bilan_debit.keys() %} + + {% if key.target_type == "OTHER" %} + + {% else %} + + {% endif %} + + + {% endfor %} + + +
{% trans %}Target of the operation{% endtrans %}{% trans %}Sum{% endtrans %}
{{ o.target_label }}{{ key.get_display_name() }}{{bilan_debit[key]}}
+ +

Total : {{total_debit}}

+ +{% endblock %} \ No newline at end of file diff --git a/accounting/templates/accounting/journal_details.jinja b/accounting/templates/accounting/journal_details.jinja index 4e22eb1a..7fa34111 100644 --- a/accounting/templates/accounting/journal_details.jinja +++ b/accounting/templates/accounting/journal_details.jinja @@ -22,7 +22,9 @@

{% trans %}Journal is closed, you can not create operation{% endtrans %}

{% else %}

{% trans %}New operation{% endtrans %}

-

{% trans %}Journal Bilan{% endtrans %}

+

{% trans %}Journal Bilan Nature{% endtrans %}

+

{% trans %}Journal Bilan Person{% endtrans %}

+

{% trans %}Journal Bilan Accounting{% endtrans %}

{% endif %} diff --git a/accounting/urls.py b/accounting/urls.py index 02033b2e..b91620a2 100644 --- a/accounting/urls.py +++ b/accounting/urls.py @@ -26,7 +26,10 @@ urlpatterns = [ url(r'^journal/create$', JournalCreateView.as_view(), name='journal_new'), url(r'^journal/(?P[0-9]+)$', JournalDetailView.as_view(), name='journal_details'), url(r'^journal/(?P[0-9]+)/edit$', JournalEditView.as_view(), name='journal_edit'), - url(r'^journal/(?P[0-9]+)/bilan$', JournalBilanView.as_view(), name='journal_bilan'), + url(r'^journal/(?P[0-9]+)/bilan/nature$', JournalBilanNatureView.as_view(), name='journal_bilan_nature'), + url(r'^journal/(?P[0-9]+)/bilan/person$', JournalBilanPersonView.as_view(), name='journal_bilan_person'), + url(r'^journal/(?P[0-9]+)/bilan/accounting$', JournalBilanAccountingView.as_view(), name='journal_bilan_accounting'), + # Operations url(r'^operation/create/(?P[0-9]+)$', OperationCreateView.as_view(), name='op_new'), url(r'^operation/(?P[0-9]+)$', OperationEditView.as_view(), name='op_edit'), diff --git a/accounting/views.py b/accounting/views.py index 83b3fbcf..f878a663 100644 --- a/accounting/views.py +++ b/accounting/views.py @@ -444,34 +444,145 @@ class OperationPDFView(CanViewMixin, DetailView): p.save() return response -class JournalBilanView(CanViewMixin, DetailView): +class JournalBilanNatureView(CanViewMixin, DetailView): """ Calculate a dictionary with operation code and sum of operations """ model = GeneralJournal pk_url_kwarg = "j_id" - template_name='accounting/journal_bilan.jinja' + template_name='accounting/journal_bilan_nature.jinja' def sum_by_code(self, code): from decimal import Decimal + from django.db.models import Sum, DecimalField amount_sum = Decimal(0) - for amount in self.get_object().operations.filter(accounting_type__code=code).values('amount'): - amount_sum += amount['amount'] - return amount_sum + print(self.object.operations.filter( + accounting_type__code=code).values('amount').annotate( + sum = Sum('amount')).values('sum').first()['sum']) + return(self.object.operations.filter( + accounting_type__code=code).values('amount').annotate( + sum = Sum('amount')).values('sum').first()['sum']) - def bilan(self): + def bilan_credit(self): bilan = {} - for el in AccountingType.objects.values('code').distinct(): - bilan[el['code']] = self.sum_by_code(el['code']) - print(bilan) + for el in AccountingType.objects.filter(movement_type='CREDIT'): + bilan["%s - %s" % (el.code, el.label)] = self.sum_by_code(el.code) return bilan + def bilan_debit(self): + bilan = {} + for el in AccountingType.objects.filter(movement_type='DEBIT'): + bilan["%s - %s" % (el.code, el.label)] = self.sum_by_code(el.code) + return bilan + + def total_credit(self): + return sum(self.bilan_credit().values()) + + def total_debit(self): + return sum(self.bilan_debit().values()) + def get_context_data(self, **kwargs): """ Add journal to the context """ - kwargs = super(JournalBilanView, self).get_context_data(**kwargs) - kwargs['bilan'] = self.bilan() + kwargs = super(JournalBilanNatureView, self).get_context_data(**kwargs) + kwargs['bilan_credit'] = self.bilan_credit() + kwargs['bilan_debit'] = self.bilan_debit() + kwargs['total_credit'] = self.total_credit() + kwargs['total_debit'] = self.total_debit() return kwargs +class JournalBilanPersonView(CanViewMixin, DetailView): + """ + Calculate a dictionary with operation target and sum of operations + """ + model = GeneralJournal + pk_url_kwarg = "j_id" + template_name='accounting/journal_bilan_person.jinja' + + def sum_by_target(self, target_id): + from decimal import Decimal + from django.db.models import Sum, DecimalField + amount_sum = Decimal(0) + print(self.object.operations.filter( + target_id=target_id).values('amount').annotate( + sum = Sum('amount')).values('sum').first()['sum']) + return(self.object.operations.filter( + target_id=target_id).values('amount').annotate( + sum = Sum('amount')).values('sum').first()['sum']) + + def bilan_credit(self): + + bilan = {} + for el in Operation.objects.filter(accounting_type__movement_type='CREDIT'): + bilan[el.target] = self.sum_by_target(el.target_id) + return bilan + + def bilan_debit(self): + bilan = {} + for el in Operation.objects.filter(accounting_type__movement_type='DEBIT'): + bilan[el.target] = self.sum_by_target(el.target_id) + return bilan + + def total_credit(self): + return sum(self.bilan_credit().values()) + + def total_debit(self): + return sum(self.bilan_debit().values()) + + def get_context_data(self, **kwargs): + """ Add journal to the context """ + kwargs = super(JournalBilanPersonView, self).get_context_data(**kwargs) + kwargs['bilan_credit'] = self.bilan_credit() + kwargs['bilan_debit'] = self.bilan_debit() + kwargs['total_credit'] = self.total_credit() + kwargs['total_debit'] = self.total_debit() + return kwargs + +class JournalBilanAccountingView(CanViewMixin, DetailView): + """ + Calculate a dictionary with operation type and sum of operations + """ + model = GeneralJournal + pk_url_kwarg = "j_id" + template_name='accounting/journal_bilan_person.jinja' + + def sum_by_code(self, target_id): + from decimal import Decimal + from django.db.models import Sum, DecimalField + amount_sum = Decimal(0) + print(self.object.operations.filter( + target_id=target_id).values('amount').annotate( + sum = Sum('amount')).values('sum').first()['sum']) + return(self.object.operations.filter( + target_id=target_id).values('amount').annotate( + sum = Sum('amount')).values('sum').first()['sum']) + + def bilan_credit(self): + + bilan = {} + for el in Operation.objects.filter(accounting_type__movement_type='CREDIT'): + bilan[el.target] = self.sum_by_code(el.target_id) + return bilan + + def bilan_debit(self): + bilan = {} + for el in Operation.objects.filter(accounting_type__movement_type='DEBIT'): + bilan[el.target] = self.sum_by_code(el.target_id) + return bilan + + def total_credit(self): + return sum(self.bilan_credit().values()) + + def total_debit(self): + return sum(self.bilan_debit().values()) + + def get_context_data(self, **kwargs): + """ Add journal to the context """ + kwargs = super(JournalBilanAccountingView, self).get_context_data(**kwargs) + kwargs['bilan_credit'] = self.bilan_credit() + kwargs['bilan_debit'] = self.bilan_debit() + kwargs['total_credit'] = self.total_credit() + kwargs['total_debit'] = self.total_debit() + return kwargs # Company views