Now, I understand how ORM works... or not

This commit is contained in:
Pierre Brunet 2016-12-14 01:04:38 +01:00 committed by Skia
parent bfa09c0bcd
commit 147287f9a9
4 changed files with 39 additions and 5 deletions

View File

@ -14,13 +14,16 @@
<thead>
<tr>
<td>{% trans %}Nature of operation{% endtrans %}</td>
<td>{% trans %}Amount{% endtrans %}</td>
<td>{% trans %}Sum{% endtrans %}</td>
</tr>
</thead>
<tbody></tr>
<td>bla</td>
<td>{{self.get_test()}}</td>
<tr>
<tbody>
{% for key in bilan.keys() %}
<tr>
<td>{{object.operations.get(accounting_type__code=key).accounting_type.label}}</td>
<td>{{bilan[key]}}</td>
</tr>
{% endfor %}
</tbody>
</table>

View File

@ -22,6 +22,7 @@
<p>{% trans %}Journal is closed, you can not create operation{% endtrans %}</p>
{% else %}
<p><a href="{{ url('accounting:op_new', j_id=object.id) }}">{% trans %}New operation{% endtrans %}</a></p>
<p><a href="{{ url('accounting:journal_bilan', j_id=object.id) }}">{% trans %}Journal Bilan{% endtrans %}</a></p>
{% endif %}
<table>
<thead>

View File

@ -26,6 +26,7 @@ urlpatterns = [
url(r'^journal/create$', JournalCreateView.as_view(), name='journal_new'),
url(r'^journal/(?P<j_id>[0-9]+)$', JournalDetailView.as_view(), name='journal_details'),
url(r'^journal/(?P<j_id>[0-9]+)/edit$', JournalEditView.as_view(), name='journal_edit'),
url(r'^journal/(?P<j_id>[0-9]+)/bilan$', JournalBilanView.as_view(), name='journal_bilan'),
# Operations
url(r'^operation/create/(?P<j_id>[0-9]+)$', OperationCreateView.as_view(), name='op_new'),
url(r'^operation/(?P<op_id>[0-9]+)$', OperationEditView.as_view(), name='op_edit'),

View File

@ -444,6 +444,35 @@ class OperationPDFView(CanViewMixin, DetailView):
p.save()
return response
class JournalBilanView(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'
def sum_by_code(self, code):
from decimal import Decimal
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
def bilan(self):
bilan = {}
for el in AccountingType.objects.values('code').distinct():
bilan[el['code']] = self.sum_by_code(el['code'])
print(bilan)
return bilan
def get_context_data(self, **kwargs):
""" Add journal to the context """
kwargs = super(JournalBilanView, self).get_context_data(**kwargs)
kwargs['bilan'] = self.bilan()
return kwargs
# Company views
class CompanyListView(CanViewMixin, ListView):