[13e7c01] | 1 | from decimal import Decimal |
---|
[162f527] | 2 | from django.db.models import Q, Sum |
---|
| 3 | import finance_core.models |
---|
[13e7c01] | 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') |
---|
[162f527] | 10 | for num, (pk, label, qobj, objrel_qobj) in enumerate(primary_axis): |
---|
[13e7c01] | 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 |
---|
[162f527] | 19 | for num, (pk, label, qobj_lineitem, qobj_primary) in enumerate(secondary_axis): |
---|
[13e7c01] | 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 |
---|
[162f527] | 38 | total_amount(line_items.filter(primary[2], secondary[2])) |
---|
[13e7c01] | 39 | for secondary in secondary_axis] |
---|
| 40 | for primary in primary_axis] |
---|
| 41 | |
---|
| 42 | return table |
---|
| 43 | |
---|
| 44 | build_table = build_table_annotate |
---|
[162f527] | 45 | |
---|
| 46 | |
---|
[dd2c3d9] | 47 | def get_primary_axis(slug, base_area, term, ): |
---|
[8a0d18c] | 48 | if slug in axes and axes[slug][2]: |
---|
| 49 | return (axes[slug][0], ) + axes[slug][1](base_area, term, ) |
---|
[162f527] | 50 | else: |
---|
[3d00b0a] | 51 | raise NotImplementedError |
---|
[162f527] | 52 | |
---|
[dd2c3d9] | 53 | def get_secondary_axis(slug, base_area, term, ): |
---|
[8a0d18c] | 54 | if slug in axes and axes[slug][3]: |
---|
| 55 | return (axes[slug][0], ) + axes[slug][1](base_area, term, ) |
---|
[162f527] | 56 | else: |
---|
| 57 | raise NotImplementedError |
---|
| 58 | |
---|
[dd2c3d9] | 59 | def get_budget_areas(base_area, term, ): |
---|
[162f527] | 60 | base_area_depth = base_area.depth |
---|
[dd2c3d9] | 61 | areas = base_area.get_descendants() |
---|
| 62 | if term: |
---|
| 63 | areas = areas.filter(Q(always=True) or Q(budget_term=term)) |
---|
[162f527] | 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 | ) |
---|
[dd2c3d9] | 71 | for area in areas |
---|
[162f527] | 72 | ] |
---|
[dd2c3d9] | 73 | axis_objs = areas |
---|
[8a0d18c] | 74 | return axis, axis_objs, |
---|
[162f527] | 75 | |
---|
[dd2c3d9] | 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() |
---|
[ca03565] | 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 | ] |
---|
[8a0d18c] | 90 | return axis, terms |
---|
[ca03565] | 91 | |
---|
[dd2c3d9] | 92 | def get_layers(base_area, term, ): |
---|
[162f527] | 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 | ] |
---|
[8a0d18c] | 102 | return axis, None, |
---|
[162f527] | 103 | |
---|
[3d00b0a] | 104 | axes = { |
---|
[8a0d18c] | 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, ), |
---|
[3d00b0a] | 108 | } |
---|
| 109 | |
---|