Commit 147287f9 authored by Krophil's avatar Krophil Committed by Skia

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

parent bfa09c0b
......@@ -14,13 +14,16 @@
<td>{% trans %}Nature of operation{% endtrans %}</td>
<td>{% trans %}Amount{% endtrans %}</td>
<td>{% trans %}Sum{% endtrans %}</td>
{% for key in bilan.keys() %}
{% endfor %}
......@@ -22,6 +22,7 @@
<p>{% trans %}Journal is closed, you can not create operation{% endtrans %}</p>
{% else %}
<p><a href="{{ url('accounting:op_new', }}">{% trans %}New operation{% endtrans %}</a></p>
<p><a href="{{ url('accounting:journal_bilan', }}">{% trans %}Journal Bilan{% endtrans %}</a></p>
{% endif %}
......@@ -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'),
......@@ -444,6 +444,35 @@ class OperationPDFView(CanViewMixin, DetailView):
return response
class JournalBilanView(CanViewMixin, DetailView):
Calculate a dictionary with operation code and sum of operations
model = GeneralJournal
pk_url_kwarg = "j_id"
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'])
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):
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment