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

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