1 | from decimal import Decimal |
---|
2 | from django.db.models import Q, Sum |
---|
3 | import finance_core.models |
---|
4 | |
---|
5 | def build_table_annotate(line_items, main_lineitem_limit_primary, primary_axis, primary_axis_objs, secondary_axis, ): |
---|
6 | # Setup |
---|
7 | arcprimary = {} |
---|
8 | table = [] |
---|
9 | zero = Decimal('0.00') |
---|
10 | for num, (pk, label, qobj, objrel_qobj) in enumerate(primary_axis): |
---|
11 | arcprimary[pk] = num |
---|
12 | table.append([zero]*len(secondary_axis)) |
---|
13 | |
---|
14 | def lineitem_total(obj): |
---|
15 | if obj.lineitem__amount__sum is None: return zero |
---|
16 | else: return obj.lineitem__amount__sum |
---|
17 | |
---|
18 | # Do the real work |
---|
19 | for num, (pk, label, qobj_lineitem, qobj_primary) in enumerate(secondary_axis): |
---|
20 | secondary_results = (primary_axis_objs.filter(main_lineitem_limit_primary, qobj_primary, ).annotate(Sum('lineitem__amount'))) |
---|
21 | for cell in secondary_results: |
---|
22 | #print cell, cell.pk, arcprimary[cell.pk], num, table[arcprimary[cell.pk]] |
---|
23 | table[arcprimary[cell.pk]][num] = lineitem_total(cell) |
---|
24 | |
---|
25 | return table |
---|
26 | |
---|
27 | def build_table_aggregate(line_items, main_lineitem_limit_primary, primary_axis, primary_axis_objs, secondary_axis): |
---|
28 | # This uses a simpler but probably slower method |
---|
29 | # In theory, if we grow unit tests, comparing this method with |
---|
30 | # the one above using annotate would be a good idea |
---|
31 | zero = Decimal('0.00') |
---|
32 | def total_amount(queryset): |
---|
33 | amount = queryset.aggregate(Sum('amount'))['amount__sum'] |
---|
34 | if amount is None: return zero |
---|
35 | else: return amount |
---|
36 | table = [ # Primary axis |
---|
37 | [ # Secondary axis |
---|
38 | total_amount(line_items.filter(primary[2], secondary[2])) |
---|
39 | for secondary in secondary_axis] |
---|
40 | for primary in primary_axis] |
---|
41 | |
---|
42 | return table |
---|
43 | |
---|
44 | build_table = build_table_annotate |
---|
45 | |
---|
46 | |
---|
47 | def get_primary_axis(slug, base_area, term, ): |
---|
48 | if slug in axes and axes[slug][2]: |
---|
49 | return (axes[slug][0], ) + axes[slug][1](base_area, term, ) |
---|
50 | else: |
---|
51 | raise NotImplementedError |
---|
52 | |
---|
53 | def get_secondary_axis(slug, base_area, term, ): |
---|
54 | if slug in axes and axes[slug][3]: |
---|
55 | return (axes[slug][0], ) + axes[slug][1](base_area, term, ) |
---|
56 | else: |
---|
57 | raise NotImplementedError |
---|
58 | |
---|
59 | def get_budget_areas(base_area, term, ): |
---|
60 | base_area_depth = base_area.depth |
---|
61 | areas = base_area.get_descendants() |
---|
62 | if term: |
---|
63 | areas = areas.filter(Q(always=True) or Q(budget_term=term)) |
---|
64 | axis = [ |
---|
65 | ( |
---|
66 | area.pk, |
---|
67 | area.indented_name(base_area_depth), |
---|
68 | Q(budget_area=area), |
---|
69 | Q(lineitem__budget_area=area), |
---|
70 | ) |
---|
71 | for area in areas |
---|
72 | ] |
---|
73 | axis_objs = areas |
---|
74 | return axis, axis_objs, |
---|
75 | |
---|
76 | def get_budget_terms(base_area, term, ): |
---|
77 | if term: |
---|
78 | terms = finance_core.models.BudgetTerm.objects.filter(pk=term.pk) |
---|
79 | else: |
---|
80 | terms = finance_core.models.BudgetTerm.objects.all() |
---|
81 | axis = [ |
---|
82 | ( |
---|
83 | term.pk, |
---|
84 | term.name, |
---|
85 | Q(budget_term=term), |
---|
86 | Q(lineitem__budget_term=term), |
---|
87 | ) |
---|
88 | for term in terms |
---|
89 | ] |
---|
90 | return axis, terms |
---|
91 | |
---|
92 | def get_layers(base_area, term, ): |
---|
93 | axis = [ |
---|
94 | ( |
---|
95 | finance_core.models.layer_num(layer), |
---|
96 | finance_core.models.layer_name(layer), |
---|
97 | Q(layer=finance_core.models.layer_num(layer)), |
---|
98 | Q(lineitem__layer=finance_core.models.layer_num(layer)), |
---|
99 | ) |
---|
100 | for layer in finance_core.models.layers |
---|
101 | ] |
---|
102 | return axis, None, |
---|
103 | |
---|
104 | axes = { |
---|
105 | 'budget-areas': ('Budget Areas', get_budget_areas, True, True, ), |
---|
106 | 'budget-terms': ('Budget Terms', get_budget_terms, True, True, ), |
---|
107 | 'layers': ('Layers', get_layers, False, True, ), |
---|
108 | } |
---|
109 | |
---|