Refactor statement by person, make it work

This commit is contained in:
Skia 2016-12-21 05:42:33 +01:00
parent 33ee449fb5
commit f6d34baf59
2 changed files with 49 additions and 60 deletions

View File

@ -6,58 +6,58 @@
{% block content %}
<h3>{% trans %}Statement by person: {% endtrans %} {{ object.name }}</h3>
<h3>{% trans %}Statement by person: {% endtrans %} {{ object.name }}</h3>
<h4>{% trans %}Credit{% endtrans %}</h4>
<h4>{% trans %}Credit{% endtrans %}</h4>
<table>
<thead>
<table>
<thead>
<tr>
<td>{% trans %}Target of the operation{% endtrans %}</td>
<td>{% trans %}Sum{% endtrans %}</td>
</tr>
</thead>
<tbody>
{% for key in credit_statement.keys() %}
<tr>
{% if key.target_type == "OTHER" %}
<td>{{ o.target_label }}</td>
{% else %}
<td><a href="{{ key.get_absolute_url() }}">{{ key.get_display_name() }}</a></td>
{% endif %}
<td>{{credit_statement[key]}}</td>
</tr>
{% endfor %}
</tbody>
</thead>
<tbody>
{% for key in credit_statement.keys() %}
<tr>
{% if key.target_type == "OTHER" %}
<td>{{ o.target_label }}</td>
{% else %}
<td><a href="{{ key.get_absolute_url() }}">{{ key.get_display_name() }}</a></td>
{% endif %}
<td>{{ credit_statement[key] }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</table>
<p>Total : {{total_credit}}</p>
<p>Total : {{ total_credit }}</p>
<h4>{% trans %}Debit{% endtrans %}</h4>
<h4>{% trans %}Debit{% endtrans %}</h4>
<table>
<thead>
<table>
<thead>
<tr>
<td>{% trans %}Target of the operation{% endtrans %}</td>
<td>{% trans %}Sum{% endtrans %}</td>
</tr>
</thead>
<tbody>
{% for key in debit_statement.keys() %}
<tr>
{% if key.target_type == "OTHER" %}
<td>{{ o.target_label }}</td>
{% else %}
<td><a href="{{ key.get_absolute_url() }}">{{ key.get_display_name() }}</a></td>
{% endif %}
<td>{{debit_statement[key]}}</td>
</tr>
{% endfor %}
</tbody>
</thead>
<tbody>
{% for key in debit_statement.keys() %}
<tr>
{% if key.target_type == "OTHER" %}
<td>{{ o.target_label }}</td>
{% else %}
<td><a href="{{ key.get_absolute_url() }}">{{ key.get_display_name() }}</a></td>
{% endif %}
<td>{{ debit_statement[key] }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</table>
<p>Total : {{total_debit}}</p>
<p>Total : {{ total_debit }}</p>
{% endblock %}

View File

@ -527,37 +527,26 @@ class JournalPersonStatementView(JournalTabsMixin, CanViewMixin, DetailView):
template_name='accounting/journal_statement_person.jinja'
current_tab='person_statement'
def sum_by_target(self, target_id):
from decimal import Decimal
from django.db.models import Sum, DecimalField
return(list((self.object.operations.filter(
target_id=target_id).aggregate(Sum('amount'))).values())[0])
def sum_by_target(self, target_id, target_type, movement_type):
return self.object.operations.filter(accounting_type__movement_type=movement_type,
target_id=target_id, target_type=target_type).aggregate(amount_sum=Sum('amount'))['amount_sum']
def credit_statement(self):
def statement(self, movement_type):
statement = {}
for el in Operation.objects.filter(accounting_type__movement_type='CREDIT'):
statement[el.target] = self.sum_by_target(el.target_id)
for op in Operation.objects.filter(accounting_type__movement_type=movement_type):
statement[op.target] = self.sum_by_target(op.target_id, op.target_type, movement_type)
return statement
def debit_statement(self):
statement = {}
for el in Operation.objects.filter(accounting_type__movement_type='DEBIT'):
statement[el.target] = self.sum_by_target(el.target_id)
return statement
def total_credit(self):
return sum(self.credit_statement().values())
def total_debit(self):
return sum(self.debit_statement().values())
def total(self, movement_type):
return sum(self.statement(movement_type).values())
def get_context_data(self, **kwargs):
""" Add journal to the context """
kwargs = super(JournalPersonStatementView, self).get_context_data(**kwargs)
kwargs['credit_statement'] = self.credit_statement()
kwargs['debit_statement'] = self.debit_statement()
kwargs['total_credit'] = self.total_credit()
kwargs['total_debit'] = self.total_debit()
kwargs['credit_statement'] = self.statement("CREDIT")
kwargs['debit_statement'] = self.statement("DEBIT")
kwargs['total_credit'] = self.total("CREDIT")
kwargs['total_debit'] = self.total("DEBIT")
return kwargs
class JournalAccountingStatementView(JournalTabsMixin, CanViewMixin, DetailView):