Remake the statement by nature

This commit is contained in:
Skia 2016-12-21 04:58:52 +01:00
parent 6d756423d9
commit bdcc0bf6e9
4 changed files with 126 additions and 143 deletions

View File

@ -6,28 +6,27 @@
{% block content %} {% block content %}
<h2>{% trans %}Accounting bilan: {% endtrans %} {{ object.name }}</h2> <h3>{% trans %}Accounting bilan: {% endtrans %} {{ object.name }}</h3>
<h3>{% trans %}Credit{% endtrans %}</h3> <table>
<table>
<thead> <thead>
<tr> <tr>
<td>{% trans %}Type of operations{% endtrans %}</td> <td>{% trans %}Operation type{% endtrans %}</td>
<td>{% trans %}Sum{% endtrans %}</td> <td>{% trans %}Sum{% endtrans %}</td>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
{% for key in bilan.keys() %} {% for k,v in bilan.items() %}
<tr> <tr>
<td>{{ key }}</td> <td>{{ k }}</td>
<td>{{ bilan[key] }}</td> <td>{{ v }}</td>
</tr> </tr>
{% endfor %} {% endfor %}
</tbody> </tbody>
</table> </table>
<p>Total : {{ total_credit }}</p> <p><strong>{% trans %}Amount: {% endtrans %}</strong>{{ object.amount }} €</p>
<p><strong>{% trans %}Effective amount: {% endtrans %}</strong>{{ object.effective_amount }} €</p>
{% endblock %} {% endblock %}

View File

@ -4,52 +4,53 @@
{% trans %}General journal:{% endtrans %} {{ object.name }} {% trans %}General journal:{% endtrans %} {{ object.name }}
{% endblock %} {% endblock %}
{% macro display_tables(dict) %}
<h4>{% trans %}Credit{% endtrans %}</h4>
<table>
<thead>
<tr>
<td>{% trans %}Nature of operation{% endtrans %}</td>
<td>{% trans %}Sum{% endtrans %}</td>
</tr>
</thead>
<tbody>
{% for k,v in dict['CREDIT'].items() %}
<tr>
<td>{{ k }}</td>
<td>{{ v }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% trans %}Total: {% endtrans %}{{ dict['CREDIT_sum'] }}
<h4>{% trans %}Debit{% endtrans %}</h4>
<table>
<thead>
<tr>
<td>{% trans %}Nature of operation{% endtrans %}</td>
<td>{% trans %}Sum{% endtrans %}</td>
</tr>
</thead>
<tbody>
{% for k,v in dict['DEBIT'].items() %}
<tr>
<td>{{ k }}</td>
<td>{{ v }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% trans %}Total: {% endtrans %}{{ dict['DEBIT_sum'] }}
{% endmacro %}
{% block content %} {% block content %}
<h2>{% trans %}Nature bilan: {% endtrans %} {{ object.name }}</h2> <h2>{% trans %}Statement by nature: {% endtrans %} {{ object.name }}</h2>
<h3>{% trans %}Credit{% endtrans %}</h3> {% for k,v in statement.items() %}
<h3>{{ k }}</h3>
<table> {{ display_tables(v) }}
<thead> <hr>
<tr> {% endfor %}
<td>{% trans %}Nature of operation{% endtrans %}</td>
<td>{% trans %}Sum{% endtrans %}</td>
</tr>
</thead>
<tbody>
{% for key in bilan_credit.keys() %}
<tr>
<td>{{key}}</td>
<td>{{bilan_credit[key]}}</td>
</tr>
{% endfor %}
</tbody>
</table>
<p>Total : {{total_credit}}</p>
<h3>{% trans %}Debit{% endtrans %}</h3>
<table>
<thead>
<tr>
<td>{% trans %}Nature of operation{% endtrans %}</td>
<td>{% trans %}Sum{% endtrans %}</td>
</tr>
</thead>
<tbody>
{% for key in bilan_debit.keys() %}
<tr>
<td>{{key}}</td>
<td>{{bilan_debit[key]}}</td>
</tr>
{% endfor %}
</tbody>
</table>
<p>Total : {{total_debit}}</p>
{% endblock %} {% endblock %}

View File

@ -7,12 +7,14 @@ from django.forms.models import modelform_factory
from django.core.exceptions import PermissionDenied from django.core.exceptions import PermissionDenied
from django.forms import HiddenInput from django.forms import HiddenInput
from django.db import transaction from django.db import transaction
from django.db.models import Sum
from django.conf import settings from django.conf import settings
from django import forms from django import forms
from django.http import HttpResponseRedirect, HttpResponse from django.http import HttpResponseRedirect, HttpResponse
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
from django.conf import settings from django.conf import settings
import collections
from ajax_select.fields import AutoCompleteSelectField, AutoCompleteSelectMultipleField from ajax_select.fields import AutoCompleteSelectField, AutoCompleteSelectMultipleField
@ -363,8 +365,6 @@ class OperationPDFView(CanViewMixin, DetailView):
pdfmetrics.registerFont(TTFont('DejaVu', 'DejaVuSerif.ttf')) pdfmetrics.registerFont(TTFont('DejaVu', 'DejaVuSerif.ttf'))
self.object = self.get_object() self.object = self.get_object()
amount = self.object.amount amount = self.object.amount
remark = self.object.remark remark = self.object.remark
@ -384,7 +384,6 @@ class OperationPDFView(CanViewMixin, DetailView):
else: else:
target = self.object.target.get_display_name() target = self.object.target.get_display_name()
response = HttpResponse(content_type='application/pdf') response = HttpResponse(content_type='application/pdf')
response['Content-Disposition'] = 'attachment; filename="op-%d(%s_on_%s).pdf"' %(num, ti, club_name) response['Content-Disposition'] = 'attachment; filename="op-%d(%s_on_%s).pdf"' %(num, ti, club_name)
p = canvas.Canvas(response) p = canvas.Canvas(response)
@ -464,8 +463,6 @@ class OperationPDFView(CanViewMixin, DetailView):
t.drawOn(p, 90, 350) t.drawOn(p, 90, 350)
p.drawCentredString(10.5 * cm, 2 * cm, club_name) p.drawCentredString(10.5 * cm, 2 * cm, club_name)
p.drawCentredString(10.5 * cm, 1 * cm, club_address) p.drawCentredString(10.5 * cm, 1 * cm, club_address)
@ -475,44 +472,50 @@ class OperationPDFView(CanViewMixin, DetailView):
class JournalBilanNatureView(JournalTabsMixin, CanViewMixin, DetailView): class JournalBilanNatureView(JournalTabsMixin, CanViewMixin, DetailView):
""" """
Calculate a dictionary with operation code and sum of operations Display a statement sorted by labels
""" """
model = GeneralJournal model = GeneralJournal
pk_url_kwarg = "j_id" pk_url_kwarg = "j_id"
template_name='accounting/journal_bilan_nature.jinja' template_name='accounting/journal_bilan_nature.jinja'
current_tab='bilan_nature' current_tab='bilan_nature'
def sum_by_code(self, code): def statement(self, queryset, movement_type):
from decimal import Decimal ret = collections.OrderedDict()
from django.db.models import Sum, DecimalField statement = collections.OrderedDict()
return(list((self.object.operations.filter( total_sum = 0
accounting_type__code=code).aggregate(Sum('amount'))).values())[0]) for at in AccountingType.objects.order_by('label').all():
sum = queryset.filter(accounting_type__movement_type=movement_type,
accounting_type__code__startswith=at.code).aggregate(amount_sum=Sum('amount'))['amount_sum']
if sum:
total_sum += sum
statement[at.label] = sum
ret[movement_type] = statement
ret[movement_type+"_sum"] = total_sum
return ret
def bilan_credit(self): def big_statement(self):
bilan = {} label_list = self.object.operations.order_by('label').values_list('label').distinct()
for el in AccountingType.objects.filter(movement_type='CREDIT'): labels = Label.objects.filter(id__in=label_list).all()
bilan["%s - %s" % (el.code, el.label)] = self.sum_by_code(el.code) statement = collections.OrderedDict()
return bilan gen_statement = collections.OrderedDict()
no_label_statement = collections.OrderedDict()
def bilan_debit(self): gen_statement.update(self.statement(self.object.operations.all(), "CREDIT"))
bilan = {} gen_statement.update(self.statement(self.object.operations.all(), "DEBIT"))
for el in AccountingType.objects.filter(movement_type='DEBIT'): statement[_("General statement")] = gen_statement
bilan["%s - %s" % (el.code, el.label)] = self.sum_by_code(el.code) no_label_statement.update(self.statement(self.object.operations.filter(label=None).all(), "CREDIT"))
return bilan no_label_statement.update(self.statement(self.object.operations.filter(label=None).all(), "DEBIT"))
statement[_("No label operations")] = no_label_statement
def total_credit(self): for l in labels:
return sum(self.bilan_credit().values()) l_stmt = collections.OrderedDict()
l_stmt.update(self.statement(self.object.operations.filter(label=l).all(), "CREDIT"))
def total_debit(self): l_stmt.update(self.statement(self.object.operations.filter(label=l).all(), "DEBIT"))
return sum(self.bilan_debit().values()) statement[l] = l_stmt
return statement
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
""" Add journal to the context """ """ Add infos to the context """
kwargs = super(JournalBilanNatureView, self).get_context_data(**kwargs) kwargs = super(JournalBilanNatureView, self).get_context_data(**kwargs)
kwargs['bilan_credit'] = self.bilan_credit() kwargs['statement'] = self.big_statement()
kwargs['bilan_debit'] = self.bilan_debit()
kwargs['total_credit'] = self.total_credit()
kwargs['total_debit'] = self.total_debit()
return kwargs return kwargs
class JournalBilanPersonView(JournalTabsMixin, CanViewMixin, DetailView): class JournalBilanPersonView(JournalTabsMixin, CanViewMixin, DetailView):
@ -530,30 +533,29 @@ class JournalBilanPersonView(JournalTabsMixin, CanViewMixin, DetailView):
return(list((self.object.operations.filter( return(list((self.object.operations.filter(
target_id=target_id).aggregate(Sum('amount'))).values())[0]) target_id=target_id).aggregate(Sum('amount'))).values())[0])
def bilan_credit(self): def credit_statement(self):
statement = {}
bilan = {}
for el in Operation.objects.filter(accounting_type__movement_type='CREDIT'): for el in Operation.objects.filter(accounting_type__movement_type='CREDIT'):
bilan[el.target] = self.sum_by_target(el.target_id) statement[el.target] = self.sum_by_target(el.target_id)
return bilan return statement
def bilan_debit(self): def debit_statement(self):
bilan = {} statement = {}
for el in Operation.objects.filter(accounting_type__movement_type='DEBIT'): for el in Operation.objects.filter(accounting_type__movement_type='DEBIT'):
bilan[el.target] = self.sum_by_target(el.target_id) statement[el.target] = self.sum_by_target(el.target_id)
return bilan return statement
def total_credit(self): def total_credit(self):
return sum(self.bilan_credit().values()) return sum(self.credit_statement().values())
def total_debit(self): def total_debit(self):
return sum(self.bilan_debit().values()) return sum(self.debit_statement().values())
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
""" Add journal to the context """ """ Add journal to the context """
kwargs = super(JournalBilanPersonView, self).get_context_data(**kwargs) kwargs = super(JournalBilanPersonView, self).get_context_data(**kwargs)
kwargs['bilan_credit'] = self.bilan_credit() kwargs['credit_statement'] = self.credit_statement()
kwargs['bilan_debit'] = self.bilan_debit() kwargs['debit_statement'] = self.debit_statement()
kwargs['total_credit'] = self.total_credit() kwargs['total_credit'] = self.total_credit()
kwargs['total_debit'] = self.total_debit() kwargs['total_debit'] = self.total_debit()
return kwargs return kwargs
@ -567,38 +569,19 @@ class JournalBilanAccountingView(JournalTabsMixin, CanViewMixin, DetailView):
template_name='accounting/journal_bilan_accounting.jinja' template_name='accounting/journal_bilan_accounting.jinja'
current_tab = "bilan_accounting" current_tab = "bilan_accounting"
def recursive_sum(self, max_length): def statement(self):
import collections statement = collections.OrderedDict()
from decimal import Decimal
from django.db.models import Sum, DecimalField
bilan = {}
bilan = collections.OrderedDict(bilan)
for at in AccountingType.objects.order_by('code').all(): for at in AccountingType.objects.order_by('code').all():
#bilan[at] = self.object.operations.filter(type__startswith=at.code) sum_by_type = self.object.operations.filter(
sum_by_type = list((self.object.operations.filter( accounting_type__code__startswith=at.code).aggregate(amount_sum=Sum('amount'))['amount_sum']
accounting_type__code__startswith=at.code).aggregate(Sum('amount'))).values())[0]
if sum_by_type != 0: if sum_by_type != 0:
bilan[at] = sum_by_type statement[at] = sum_by_type
return bilan return statement
def bilan(self):
bilan = self.recursive_sum(3)
return bilan
def total_credit(self):
return self.bilan().get('6')#sum(self.bilan().values())
def total_debit(self):
return self.bilan().get('6')#sum(self.bilan().values())
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
""" Add journal to the context """ """ Add journal to the context """
kwargs = super(JournalBilanAccountingView, self).get_context_data(**kwargs) kwargs = super(JournalBilanAccountingView, self).get_context_data(**kwargs)
kwargs['bilan'] = self.bilan() kwargs['statement'] = self.statement()
kwargs['total_credit'] = self.total_credit()
kwargs['total_debit'] = self.total_debit()
return kwargs return kwargs
# Company views # Company views
@ -634,7 +617,7 @@ class LabelListView(CanViewMixin, DetailView):
pk_url_kwarg = "clubaccount_id" pk_url_kwarg = "clubaccount_id"
template_name = 'accounting/label_list.jinja' template_name = 'accounting/label_list.jinja'
class LabelCreateView(CanEditMixin, CreateView): # FIXME we need to check the rights before creating the object class LabelCreateView(CanCreateMixin, CreateView): # FIXME we need to check the rights before creating the object
model = Label model = Label
form_class = modelform_factory(Label, fields=['name', 'club_account'], widgets={ form_class = modelform_factory(Label, fields=['name', 'club_account'], widgets={
'club_account': HiddenInput, 'club_account': HiddenInput,

View File

@ -161,7 +161,7 @@ msgstr "type comptable"
#: accounting/models.py:205 accounting/models.py:299 accounting/models.py:325 #: accounting/models.py:205 accounting/models.py:299 accounting/models.py:325
#: accounting/models.py:348 counter/models.py:282 #: accounting/models.py:348 counter/models.py:282
msgid "label" msgid "label"
msgstr "intitulé" msgstr "étiquette"
#: accounting/models.py:206 #: accounting/models.py:206
msgid "target type" msgid "target type"