1 | from django.http import HttpResponse |
---|
2 | import finance_core.models |
---|
3 | from django.contrib.auth.decorators import user_passes_test |
---|
4 | from django.shortcuts import render_to_response, get_object_or_404 |
---|
5 | from django.template import RequestContext |
---|
6 | from django.http import Http404, HttpResponseRedirect |
---|
7 | from django.db.models import Q, Sum |
---|
8 | from decimal import Decimal |
---|
9 | |
---|
10 | def display_tree(request): |
---|
11 | root = finance_core.models.BudgetArea.get_by_path(['Accounts']) |
---|
12 | return HttpResponse(root.dump_to_html()) |
---|
13 | |
---|
14 | def reporting(request): |
---|
15 | line_items = finance_core.models.LineItem.objects.all() |
---|
16 | term_name = 'All' |
---|
17 | term_primary_Q = Q() |
---|
18 | if 'term' in request.REQUEST: |
---|
19 | term_obj = get_object_or_404(finance_core.models.BudgetTerm, slug=request.REQUEST['term']) |
---|
20 | term_name = term_obj.name |
---|
21 | line_items = line_items.filter(budget_term=term_obj) |
---|
22 | term_primary_Q = Q(lineitem__budget_term=term_obj) |
---|
23 | if 'area' in request.REQUEST: |
---|
24 | base_area_obj = get_object_or_404(finance_core.models.BudgetArea, pk=request.REQUEST['area']) |
---|
25 | else: |
---|
26 | base_area_obj = finance_core.models.BudgetArea.get_by_path(['Accounts']) |
---|
27 | line_items = line_items.filter(budget_area__in=base_area_obj.get_descendants()) |
---|
28 | base_area_depth = base_area_obj.depth |
---|
29 | print base_area_obj |
---|
30 | |
---|
31 | primary_name = 'Budget Areas' |
---|
32 | primary_axis = [ |
---|
33 | (area.pk, area.indented_name(base_area_depth), Q(budget_area=area), ) for area in base_area_obj.get_descendants() |
---|
34 | ] |
---|
35 | primary_axis_objs = base_area_obj.get_descendants() |
---|
36 | secondary_name = 'Layers' |
---|
37 | secondary_axis = [ |
---|
38 | ( |
---|
39 | finance_core.models.layer_name(layer), |
---|
40 | Q(layer=finance_core.models.layer_num(layer)), |
---|
41 | Q(lineitem__layer=finance_core.models.layer_num(layer)), |
---|
42 | ) |
---|
43 | for layer in finance_core.models.layers |
---|
44 | ] |
---|
45 | |
---|
46 | secondary_axis.append(('Total', Q(), Q())) |
---|
47 | |
---|
48 | primary_labels = [ ] |
---|
49 | arcprimary = {} |
---|
50 | table = [] |
---|
51 | zero = Decimal('0.00') |
---|
52 | for num, (pk, label, qobj, ) in enumerate(primary_axis): |
---|
53 | primary_labels.append(label) |
---|
54 | arcprimary[pk] = num |
---|
55 | table.append([zero]*len(secondary_axis)) |
---|
56 | print arcprimary |
---|
57 | |
---|
58 | secondary_labels = [ secondary[0] for secondary in secondary_axis ] |
---|
59 | |
---|
60 | def lineitem_total(obj): |
---|
61 | if obj.lineitem__amount__sum is None: return zero |
---|
62 | else: return obj.lineitem__amount__sum |
---|
63 | for num, (label, qobj_lineitem, qobj_primary) in enumerate(secondary_axis): |
---|
64 | secondary_results = (primary_axis_objs.filter(qobj_primary, term_primary_Q).annotate(Sum('lineitem__amount'))) |
---|
65 | for cell in secondary_results: |
---|
66 | print cell, cell.pk, arcprimary[cell.pk], num, table[arcprimary[cell.pk]] |
---|
67 | table[arcprimary[cell.pk]][num] = lineitem_total(cell) |
---|
68 | |
---|
69 | slow = False |
---|
70 | if slow: |
---|
71 | # This uses a simpler but probably slower method |
---|
72 | # In theory, if we grow unit tests, comparing this method with |
---|
73 | # the one above using annotate would be a good idea |
---|
74 | def total_amount(queryset): |
---|
75 | amount = queryset.aggregate(Sum('amount'))['amount__sum'] |
---|
76 | if amount is None: return zero |
---|
77 | else: return amount |
---|
78 | table = [ # Primary axis |
---|
79 | [ # Secondary axis |
---|
80 | total_amount(line_items.filter(primary[1], secondary[1])) |
---|
81 | for secondary in secondary_axis] |
---|
82 | for primary in primary_axis] |
---|
83 | |
---|
84 | debug = False |
---|
85 | if debug: |
---|
86 | from django.db import connection |
---|
87 | print connection.queries |
---|
88 | print "Number of queries:\t%d" % (len(connection.queries),) |
---|
89 | print "Table size:\t%dx%d" % (len(primary_labels), len(secondary_labels), ) |
---|
90 | |
---|
91 | context = { |
---|
92 | 'pagename':'reporting', |
---|
93 | 'term_name': term_name, |
---|
94 | 'area': base_area_obj, |
---|
95 | 'primary_name': primary_name, |
---|
96 | 'secondary_name': secondary_name, |
---|
97 | 'primary_labels': primary_labels, |
---|
98 | 'secondary_labels': secondary_labels, |
---|
99 | 'table': table, |
---|
100 | 'table_with_row_labels': zip(primary_labels, table), |
---|
101 | } |
---|
102 | return render_to_response('finance_core/reporting.html', context, context_instance=RequestContext(request), ) |
---|