From 885dcecc922d448f9d893438bce1c730a8ab1fb5 Mon Sep 17 00:00:00 2001
From: Julian Maurice <julian.maurice@biblibre.com>
Date: Tue, 6 Mar 2018 14:47:46 +0100
Subject: [PATCH] Bug 20343: Show number of checkouts by itemtype in
 circulation.pl

It is sometimes useful to have the number of checkouts grouped by
itemtype (to see if we're approaching the maximum number of checkouts
allowed for instance).
This patch adds this information above the checkouts table, in
circ/circulation.pl

Test plan:
1. Check out some items with various item types to a patron
2. In circ/circulation.pl, click on 'Show checkouts' button to display
   the table
3. Notice the line "Number of checkouts by item type" above the table,
   click on it
4. See that the list of item types is displayed with a count in front of
   each one
5. Compare the numbers to the checkouts in the table and verify that the
   numbers are correct

Signed-off-by: claude <claude.brayer@cea.fr>

Signed-off-by: Jesse Maseto <jesse@bywatersolutions.com>
---
 koha-tmpl/intranet-tmpl/prog/css/staff-global.css | 15 +++++++++++++++
 koha-tmpl/intranet-tmpl/prog/js/checkouts.js      | 19 ++++++++++++++++++-
 2 files changed, 33 insertions(+), 1 deletion(-)

diff --git a/koha-tmpl/intranet-tmpl/prog/css/staff-global.css b/koha-tmpl/intranet-tmpl/prog/css/staff-global.css
index 3ab9d7e..e1b8f81 100644
--- a/koha-tmpl/intranet-tmpl/prog/css/staff-global.css
+++ b/koha-tmpl/intranet-tmpl/prog/css/staff-global.css
@@ -385,6 +385,21 @@ legend {
     width: auto;
 }
 
+details > summary {
+    cursor: pointer;
+}
+
+details > summary::before {
+    content: "\f0da";
+    display: inline-block;
+    font-family: FontAwesome;
+    width: 1em;
+}
+
+details[open] > summary::before {
+    content: "\f0d7";
+}
+
 #floating-save {
     background-color: rgba(185, 216, 217, 0.6);
     bottom: 3%;
diff --git a/koha-tmpl/intranet-tmpl/prog/js/checkouts.js b/koha-tmpl/intranet-tmpl/prog/js/checkouts.js
index 5167ee1..5718bbc 100644
--- a/koha-tmpl/intranet-tmpl/prog/js/checkouts.js
+++ b/koha-tmpl/intranet-tmpl/prog/js/checkouts.js
@@ -475,7 +475,7 @@ $(document).ready(function() {
                     fnCallback(json)
                 } );
             },
-            "fnInitComplete": function(oSettings) {
+            "fnInitComplete": function(oSettings, json) {
                 // Disable rowGrouping plugin after first use
                 // so any sorting on the table doesn't use it
                 var oSettings = issuesTable.fnSettings();
@@ -488,6 +488,23 @@ $(document).ready(function() {
                 }
 
                 oSettings.aaSortingFixed = null;
+
+                // Build a summary of checkouts grouped by itemtype
+                var checkoutsByItype = json.aaData.reduce(function (obj, row) {
+                    obj[row.itemtype_description] = (obj[row.itemtype_description] || 0) + 1;
+                    return obj;
+                }, {});
+                var ul = $('<ul>');
+                Object.keys(checkoutsByItype).sort().forEach(function (itype) {
+                    var li = $('<li>')
+                        .append($('<strong>').html(itype || _("No itemtype")))
+                        .append(': ' + checkoutsByItype[itype]);
+                    ul.append(li);
+                })
+                $('<details>')
+                    .append($('<summary>').html(_("Number of checkouts by item type")))
+                    .append(ul)
+                    .insertBefore(oSettings.nTableWrapper)
             },
         }, columns_settings).rowGrouping(
             {
-- 
2.1.4