From bcec57dafe6ab7a043979f852a2662ffa91c0aac Mon Sep 17 00:00:00 2001
From: bbe <benjamin.bernard-bouissieres@ipexia.com>
Date: Mon, 5 Oct 2020 10:20:03 +0200
Subject: [PATCH] Display total row in report output.

---
 helpdesk/templates/helpdesk/report_output.html |  4 ++--
 helpdesk/views/staff.py                        | 12 ++++++++++++
 2 files changed, 14 insertions(+), 2 deletions(-)

diff --git a/helpdesk/templates/helpdesk/report_output.html b/helpdesk/templates/helpdesk/report_output.html
index 6e036586..ea6e2fe6 100644
--- a/helpdesk/templates/helpdesk/report_output.html
+++ b/helpdesk/templates/helpdesk/report_output.html
@@ -96,7 +96,7 @@ Morris.Line({
   element: 'chart-content',
   data: {% autoescape on %}{{ morrisjs_data|safe }}{% endautoescape %},
   xkey: 'x',
-  ykeys: [{% for d in data %}{{ forloop.counter0 }}{% if not forloop.last %}, {% endif %}{% endfor %}],
+  ykeys: [{% for d in data|slice:":-1" %}{{ forloop.counter0 }}{% if not forloop.last %}, {% endif %}{% endfor %}],
   labels: [{% for n in series_names %}"{{ n }}"{% if not forloop.last %}, {% endif %}{% endfor %}],
   xLabels: "month"
 });
@@ -110,7 +110,7 @@ Morris.Bar({
   element: 'chart-content',
   data: {% autoescape on %}{{ morrisjs_data|safe }}{% endautoescape %},
   xkey: 'x',
-  ykeys: [{% for d in data %}{{ forloop.counter0 }}{% if not forloop.last %}, {% endif %}{% endfor %}],
+  ykeys: [{% for d in data|slice:":-1" %}{{ forloop.counter0 }}{% if not forloop.last %}, {% endif %}{% endfor %}],
   labels: [{% for n in series_names %}"{{ n }}"{% if not forloop.last %}, {% endif %}{% endfor %}]
 });
 
diff --git a/helpdesk/views/staff.py b/helpdesk/views/staff.py
index ba7ed16a..02c21580 100644
--- a/helpdesk/views/staff.py
+++ b/helpdesk/views/staff.py
@@ -1309,11 +1309,17 @@ def run_report(request, report):
 
     column_headings = [col1heading] + possible_options
 
+    # Prepare a dict to store totals for each possible option
+    totals = {}
     # Pivot the data so that 'header1' fields are always first column
     # in the row, and 'possible_options' are always the 2nd - nth columns.
     for item in header1:
         data = []
         for hdr in possible_options:
+            if hdr not in totals.keys():
+                totals[hdr] = summarytable[item, hdr]
+            else:
+                totals[hdr] += summarytable[item, hdr]
             data.append(summarytable[item, hdr])
         table.append([item] + data)
 
@@ -1332,6 +1338,12 @@ def run_report(request, report):
     for series in table:
         series_names.append(series[0])
 
+    # Add total row to table
+    total_data = []
+    for hdr in possible_options:
+        total_data.append(str(totals[hdr]))
+    table.append(['Total'] + total_data)
+
     return render(request, 'helpdesk/report_output.html', {
         'title': title,
         'charttype': charttype,