Skip to content

Commit

Permalink
Fix a bug with requires , in case of time series and crosstab
Browse files Browse the repository at this point in the history
  • Loading branch information
Ramez Ashraf committed Feb 19, 2023
1 parent c5d8f17 commit 71a9459
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
12 changes: 8 additions & 4 deletions slick_reporting/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ def init_preparation(self, q_filters=None, kwargs_filters=None, **kwargs):

debit_results, credit_results = self.prepare(q_filters, kwargs_filters, **kwargs)
self._cache = debit_results, credit_results, dep_values
return self._cache

def prepare(self, q_filters=None, kwargs_filters=None, **kwargs):
"""
Expand Down Expand Up @@ -188,7 +189,8 @@ def _prepare_dependencies(self, q_filters=None, extra_filters=None, ):
for dep_class in self._require_classes:
dep = dep_class(self.plus_side_q, self.minus_side_q, self.report_model,
date_field=self.date_field, group_by=self.group_by)
values[dep.name] = {'results': dep.init_preparation(q_filters, extra_filters),
results = dep.init_preparation(q_filters, extra_filters)
values[dep.name] = {'results': results,
'instance': dep}
return values

Expand Down Expand Up @@ -216,17 +218,19 @@ def get_dependency_value(self, current_obj, name=None):
:return: a dict containing dependencies names as keys and their calculation as values
or a specific value if name is specified.
"""
values = self._resolve_dependencies(current_obj)
values = self._resolve_dependencies(current_obj, name=None)
if name:
return values.get(name)
return values

def _resolve_dependencies(self, current_obj):
def _resolve_dependencies(self, current_obj, name=None):

dep_results = {}
cached_debit, cached_credit, dependencies_value = self._cache
dependencies_value = dependencies_value or {}
for d in dependencies_value.keys():
if name and d != name:
continue
d_instance = dependencies_value[d]['instance']
dep_results[d] = d_instance.resolve(current_obj)
return dep_results
Expand Down Expand Up @@ -382,7 +386,7 @@ class PercentageToBalance(SlickReportField):
def final_calculation(self, debit, credit, dep_dict):
obj_balance = dep_dict.get('__balance__')
total = debit - credit
return (obj_balance/total) * 100
return (obj_balance / total) * 100


class CreditReportField(SlickReportField):
Expand Down
7 changes: 5 additions & 2 deletions slick_reporting/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,11 @@ def _prepare_report_dependencies(self):
if isclass(klass) and issubclass(klass, SlickReportField):
dependencies_names = klass.get_full_dependency_list()

# check if any of this dependencies is on the report
fields_on_report = [x for x in window_cols if x['ref'] in dependencies_names]
# check if any of these dependencies is on the report, if found we call the child to
# resolve the value for its parent avoiding extra database call
fields_on_report = [x for x in window_cols if x['ref'] in dependencies_names
and ((window == 'time_series' and x.get('start_date', '') == col_data.get('start_date', '') and x.get('end_date') == col_data.get('end_date')) or
window == 'crosstab' and x.get('id') == col_data.get('id'))]
for field in fields_on_report:
self._report_fields_dependencies[window][field['name']] = col_data['name']
for col_data in window_cols:
Expand Down
8 changes: 8 additions & 0 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,14 @@ def test_client_client_sales_monthly(self):
self.assertEqual(data[0]['__total__TS%s0301' % year], 100)
self.assertEqual(data[0]['__total__TS%s0201' % year], 100)

self.assertEqual(data[0]['__debit__TS%s0401' % year], 100)
self.assertEqual(data[0]['__debit__TS%s0301' % year], 100)
self.assertEqual(data[0]['__debit__TS%s0201' % year], 100)

self.assertEqual(data[2]['__debit__TS%s0401' % year], 300)
self.assertEqual(data[2]['__debit__TS%s0301' % year], 300)
self.assertEqual(data[2]['__debit__TS%s0201' % year], 300)

# todo add __fb__ to time series and check the balance

def test_client_statement_detail(self):
Expand Down

0 comments on commit 71a9459

Please sign in to comment.