From 1acd8cbc7581c14299a86966c920e435dfefcde8 Mon Sep 17 00:00:00 2001 From: "laetitia.gangloff@acsone.eu" Date: Thu, 31 Jul 2014 16:13:32 +0200 Subject: [PATCH] compute average in comparison column --- mis_builder/models/mis_builder.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/mis_builder/models/mis_builder.py b/mis_builder/models/mis_builder.py index a5da6810..fa969e53 100644 --- a/mis_builder/models/mis_builder.py +++ b/mis_builder/models/mis_builder.py @@ -162,10 +162,14 @@ class mis_report_kpi(orm.Model): else: return unicode(value) - def _render_comparison(self, kpi, value, base_value): + def _render_comparison(self, kpi, value, base_value, average_value, average_base_value): """ render the comparison of two KPI values, ready for display """ if value is None or base_value is None: return '' + if average_value: + value = value / float(average_value) + if average_base_value: + base_value = base_value / float(average_base_value) if kpi.type == 'pct': return self._render_num(value - base_value, 0.01, kpi.dp, _('pp'), @@ -583,8 +587,14 @@ class mis_report_instance(orm.Model): column2_values = period_values[compare_col.name] for kpi in r.report_id.kpi_ids: content[kpi.name]['cols'].append({'val_r': kpi_obj._render_comparison(kpi, - column1_values[kpi.name]['val'], - column2_values[kpi.name]['val'])}) + column1_values[kpi.name]['val'], + column2_values[kpi.name]['val'], + (datetime.strptime(period.date_to, tools.DEFAULT_SERVER_DATE_FORMAT) - + datetime.strptime(period.date_from, + tools.DEFAULT_SERVER_DATE_FORMAT)).days + 1, + (datetime.strptime(compare_col.date_to, tools.DEFAULT_SERVER_DATE_FORMAT) - + datetime.strptime(compare_col.date_from, + tools.DEFAULT_SERVER_DATE_FORMAT)).days + 1)}) return {'header': header, 'content': content}