From 18030d3cd75f144c794b8d74399b96a42b125080 Mon Sep 17 00:00:00 2001
From: Jonathan Druart <jonathan.druart@biblibre.com>
Date: Mon, 1 Dec 2014 18:08:45 +0100
Subject: [PATCH] Bug 13371: Add filters on the supplier list view

This patch adds the ability to filter the basket list depending on its
creator name, basketgroup name, dates (creation, close), etc.

Test plan:
Go on the supplier list view (acqui/booksellers.pl) and confirm that the
view is more ergonomic and the filters permit to the librarians to find
a basket more easily than before.
---
 koha-tmpl/intranet-tmpl/prog/en/css/datatables.css |   4 +-
 .../prog/en/modules/acqui/booksellers.tt           | 282 +++++++++++----------
 2 files changed, 152 insertions(+), 134 deletions(-)

diff --git a/koha-tmpl/intranet-tmpl/prog/en/css/datatables.css b/koha-tmpl/intranet-tmpl/prog/en/css/datatables.css
index b3bae9a..ee95fd8 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/css/datatables.css
+++ b/koha-tmpl/intranet-tmpl/prog/en/css/datatables.css
@@ -235,7 +235,9 @@ tr.even.selected td {
 }
 
 /* ColumnFilter */
-span.filter_column > input.text_filter {
+span.filter_column > input.text_filter,
+span.filter_column > input.number_filter,
+span.filter_column > input.date_range_filter {
     font-size: 80%;
     width: 100%;
     padding: 0;
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/booksellers.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/booksellers.tt
index 3db9188..1887cb8 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/booksellers.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/booksellers.tt
@@ -2,28 +2,75 @@
 [% INCLUDE 'doc-head-open.inc' %]
 <title>Koha &rsaquo; Search for vendor [% supplier %]</title>
 [% INCLUDE 'doc-head-close.inc' %]
-<style type="text/css">
-#vendors td{
-	vertical-align: top;
-}
-</style>
 <link rel="stylesheet" href="[% themelang %]/css/datatables.css" />
 [% INCLUDE 'datatables.inc' %]
+<script type="text/javascript" src="[% interface %]/lib/jquery/plugins/jquery.dataTables.columnFilter.js"></script>
+
 <script type="text/javascript">
 //<![CDATA[
 $(document).ready(function() {
-    $("table.baskets").dataTable($.extend(true, {}, dataTablesDefaults, {
-        'sDom': 't',
-        'bPaginate': false,
-        'bFilter': false,
-        'bInfo': false,
-        "aaSorting": [[ 1, "asc" ]],
+    var thead_row = $("#baskets").find('thead tr');
+    var clone = thead_row.clone().addClass('filters_row');
+    thead_row.before(clone);
+
+    var table = $("#baskets").dataTable($.extend(true, {}, dataTablesDefaults, {
+        "fnDrawCallback": function ( oSettings ) {
+            if ( oSettings.aiDisplay.length == 0 )
+            {
+                return;
+            }
+
+            var nTrs = $('#baskets tbody tr');
+            var iColspan = nTrs[0].getElementsByTagName('td').length;
+            var sLastGroup = "";
+            for ( var i=0 ; i<nTrs.length ; i++ )
+            {
+                var iDisplayIndex = oSettings._iDisplayStart + i;
+                var sGroup = oSettings.aoData[ oSettings.aiDisplay[iDisplayIndex] ]._aData[0];
+                if ( sGroup != sLastGroup )
+                {
+                    var nGroup = document.createElement( 'tr' );
+                    var nCell = document.createElement( 'td' );
+                    nCell.colSpan = iColspan;
+                    nCell.className = "group";
+                    nCell.innerHTML = sGroup;
+                    nGroup.appendChild( nCell );
+                    nTrs[i].parentNode.insertBefore( nGroup, nTrs[i] );
+                    sLastGroup = sGroup;
+                }
+            }
+        },
         "aoColumnDefs": [
-            { "sType": "title-string", "aTargets" : [ "title-string" ] },
-            { "sType": "title-numeric", "aTargets" : [ "title-numeric" ] },
-            { 'aTargets': [-1], 'bSortable': false }
-        ]
-    }));
+            { "bVisible": false, "aTargets": [ 'NoVisible' ] },
+            { "bSortable": false, "aTargets": ["_all"] }
+        ],
+        'bSort': true,
+        'aaSortingFixed': [[ 0, 'asc' ]],
+        'bPaginate': false,
+        'bAutoWidth': false
+    })).columnFilter(
+        {
+            'sPlaceHolder': 'head:after',
+            // The first column is hidden
+            // Without bUseColVis, the filters don't work on the correct column.
+            'bUseColVis': true,
+            'aoColumns': [
+                null,
+                { type: 'number' },
+                { type: 'text' },
+                { type: 'number' },
+                { type: 'number' },
+                { type: 'number' },
+                { type: 'text' },
+                { type: 'date-range' },
+                { type: 'text' },
+                { type: 'date-range' },
+                null
+
+            ]
+        }
+    );
+
     $("#supplierlist").change(function() {
         var id = $(this).find("option:selected").val();
         window.location.href = "#" + id;
@@ -55,134 +102,103 @@ $(document).ready(function() {
 <h1>You searched on <b>vendor [% supplier %],</b> [% count %] results found</h1>
 [% END %]
 [% IF ( loop_suppliers.size ) %]
-    [% UNLESS (count == 1) %]
-        <p>Choose a vendor in the list to jump directly to the right place.
-        <select id="supplierlist">
-            [% FOREACH supplier1 IN loop_suppliers %]
-                <option value="[% supplier1.booksellerid %]">[% supplier1.name %]</option>
-            [% END %]
-        </select>
-        </p>
-    [% END %]
-    [% IF ( allbaskets ) %]
+    [% IF allbaskets %]
         <p><a href="/cgi-bin/koha/acqui/booksellers.pl?supplier=[% supplier %]&amp;booksellerid=[% booksellerid %]">Show active baskets only</a></p>
     [% ELSE %]
         <p><a href="/cgi-bin/koha/acqui/booksellers.pl?supplier=[% supplier %]&amp;booksellerid=[% booksellerid %]&amp;allbaskets=1">Show all baskets</a></p>
     [% END %]
-    <div id="acqui_order_supplierlist">
+    <div id="acqui_order_basketlist">
+    <table id="baskets" class="group">
+        <thead>
+            <tr>
+                <th class='NoVisible'>bookseller</th>
+                <th>No.</th>
+                <th>Name</th>
+                <th class="title-numeric">Item count</th>
+                <th class="title-numeric">Biblio count</th>
+                <th>Items expected</th>
+                <th>Created by</th>
+                <th class="title-string">Date</th>
+                <th>Basket group</th>
+                <th class="title-string">Closed</th>
+                <th>&nbsp;</th>
+            </tr>
+        </thead>
+        <tbody>
         [% FOREACH supplier IN loop_suppliers %]
-            <div class="supplier">
-                <span class="suppliername">
-                    [% IF (supplier.name) %]
-                        <a name="[% supplier.booksellerid %]" href="/cgi-bin/koha/acqui/supplier.pl?booksellerid=[% supplier.booksellerid %]">[% supplier.name %]</a>
-                    [% ELSE %]
-                        <a name="[% supplier.booksellerid %]" href="/cgi-bin/koha/acqui/supplier.pl?booksellerid=[% supplier.booksellerid %]">NO NAME</a>
-                    [% END %]
-                </span>
-                <span class="action">
-                    [% IF ( CAN_user_acquisition_order_manage ) %]
-                        [% IF ( supplier.active ) %]
-                            <a class="btn btn-small" href="/cgi-bin/koha/acqui/basketheader.pl?booksellerid=[% supplier.booksellerid %]&amp;op=add_form"><i class="icon-plus"></i> New basket</a>
+            [% FOREACH basket IN supplier.loop_basket %]
+                [% IF ( basket.uncertainprices ) %]
+                    <tr class="problem">
+                [% ELSE %]
+                    <tr>
+                [% END %]
+                    <td>[% supplier.name %] ([% supplier.booksellerid %])</td>
+                    <td>[% basket.basketno %]</td>
+                    <td>[% basket.basketname %]</td>
+                    <td>
+                        <span title="[% basket.total_items %]">[% basket.total_items %]
+                            [% IF basket.total_items_cancelled %]
+                                ([% basket.total_items_cancelled %] cancelled)
+                            [% END %]
+                        </span>
+                    </td>
+                    <td>
+                        <span title="[% basket.total_biblios %]">[% basket.total_biblios %]
+                            [% IF basket.total_biblios_cancelled %]
+                                ([% basket.total_biblios_cancelled %] cancelled)
+                            [% END %]
+                        </span>
+                    </td>
+                    <td>[% basket.expected_items %]</td>
+                    <td>
+                        [% basket.authorisedby_firstname %]
+                        [% basket.authorisedby_surname %]
+                    </td>
+                    <td><span title="[% basket.creationdate %]">[% basket.creationdate | $KohaDates %]</span></td>
+                    <td>
+                      [% IF basket.basketgroup %]
+                        [% basketgroup = basket.basketgroup %]
+                        [% IF basketgroup.closed %]
+                          [% basketgroup.name %] (closed)
                         [% ELSE %]
-                            (inactive)
+                          <a href="/cgi-bin/koha/acqui/basketgroup.pl?op=add&amp;booksellerid=[% basket.booksellerid %]&amp;basketgroupid=[% basketgroup.id %]">[% basketgroup.name %]</a>
                         [% END %]
-                    [% END %]
-                    <a class="btn btn-small" href="/cgi-bin/koha/acqui/parcels.pl?booksellerid=[% supplier.booksellerid %]"><i class="icon-inbox"></i> Receive shipment</a>
-                </span>
-                <div class="baskets">
-                    [% IF ( supplier.loop_basket.size ) %]
-                        <table class="baskets">
-                            <thead>
-                                <tr>
-                                    <th>No.</th>
-                                    <th>Name</th>
-                                    <th class="title-numeric">Item count</th>
-                                    <th class="title-numeric">Biblio count</th>
-                                    <th>Items expected</th>
-                                    <th>Created by</th>
-                                    <th class="title-string">Date</th>
-                                    <th>Basket group</th>
-                                    <th class="title-string">Closed</th>
-                                    <th>&nbsp;</th>
-                                </tr>
-                            </thead>
-                            <tbody>
-                                [% FOREACH basket IN supplier.loop_basket %]
-                                    [% IF ( basket.uncertainprices ) %]
-                                        <tr class="problem">
-                                    [% ELSE %]
-                                        <tr>
-                                    [% END %]
-                                        <td>[% basket.basketno %]</td>
-                                        <td>[% basket.basketname %]</td>
-                                        <td>
-                                            <span title="[% basket.total_items %]">[% basket.total_items %]
-                                                [% IF basket.total_items_cancelled %]
-                                                    ([% basket.total_items_cancelled %] cancelled)
-                                                [% END %]
-                                            </span>
-                                        </td>
-                                        <td>
-                                            <span title="[% basket.total_biblios %]">[% basket.total_biblios %]
-                                                [% IF basket.total_biblios_cancelled %]
-                                                    ([% basket.total_biblios_cancelled %] cancelled)
-                                                [% END %]
-                                            </span>
-                                        </td>
-                                        <td>[% basket.expected_items %]</td>
-                                        <td>
-                                            [% basket.authorisedby_firstname %]
-                                            [% basket.authorisedby_surname %]
-                                        </td>
-                                        <td><span title="[% basket.creationdate %]">[% basket.creationdate | $KohaDates %]</span></td>
-                                        <td>
-                                          [% IF basket.basketgroup %]
-                                            [% basketgroup = basket.basketgroup %]
-                                            [% IF basketgroup.closed %]
-                                              [% basketgroup.name %] (closed)
-                                            [% ELSE %]
-                                              <a href="/cgi-bin/koha/acqui/basketgroup.pl?op=add&amp;booksellerid=[% basket.booksellerid %]&amp;basketgroupid=[% basketgroup.id %]">[% basketgroup.name %]</a>
-                                            [% END %]
-                                          [% END %]
-                                        </td>
-                                        <td>
-                                            [% IF ( basket.closedate ) %]
-                                                <span title="[% basket.closedate %]">[% basket.closedate | $KohaDates %]</span>
-                                            [% ELSE %]
-                                                <span title="9999-99-99"></span>
-                                            [% END %]
-                                        </td>
-                                        <td>
-                                            <a href="/cgi-bin/koha/acqui/basket.pl?basketno=[% basket.basketno %]">View</a>
-
-                                            [% UNLESS ( basket.closedate ) %]
-                                               | <a id="addtoBasketLabel[% basket.basketno %]" href="#addtoBasket[% basket.basketno %]" role="button" data-toggle="modal">Add to basket</a>
-                                                <!-- Modal -->
-                                                <div id="addtoBasket[% basket.basketno %]" class="modal hide" tabindex="-1" role="dialog" aria-labelledby="addtoBasketLabel[% basket.basketno %]" aria-hidden="true" data-basketno="[% basket.basketname %]">
-                                                    <div class="modal-body">
-                                                        [% INCLUDE 'acquisitions-add-to-basket.inc' booksellerid=supplier.booksellerid basketno=basket.basketno %]
-                                                    </div>
-                                                    <div class="modal-footer">
-                                                        <a href="#" class="cancel" data-dismiss="modal" aria-hidden="true">Cancel</a>
-                                                    </div>
-                                                </div>
-                                            [% END %]
-                                        </td>
-                                    </tr>
-                                [% END %][%# FOREACH basket IN supplier.loop_basket %]
-                            </tbody>
-                        </table>
+                      [% END %]
+                    </td>
+                    <td>
+                        [% IF ( basket.closedate ) %]
+                            <span title="[% basket.closedate %]">[% basket.closedate | $KohaDates %]</span>
+                        [% ELSE %]
+                            <span title="9999-99-99"></span>
+                        [% END %]
+                    </td>
+                    <td>
+                        <a href="/cgi-bin/koha/acqui/basket.pl?basketno=[% basket.basketno %]">View</a>
 
-                    [% ELSE %]
-                        <p>No pending baskets</p>
-                    [% END %][%# IF ( supplier.loop_basket.size ) %]
-                </div>
-            </div>
-        [% END %][%# FOREACH supplier IN loop_suppliers %]
-    </div>
+                        [% UNLESS ( basket.closedate ) %]
+                           | <a id="addtoBasketLabel[% basket.basketno %]" href="#addtoBasket[% basket.basketno %]" role="button" data-toggle="modal">Add to basket</a>
+                            <!-- Modal -->
+                            <div id="addtoBasket[% basket.basketno %]" class="modal hide" tabindex="-1" role="dialog" aria-labelledby="addtoBasketLabel[% basket.basketno %]" aria-hidden="true" data-basketno="[% basket.basketname %]">
+                                <div class="modal-body">
+                                    [% INCLUDE 'acquisitions-add-to-basket.inc' booksellerid=supplier.booksellerid basketno=basket.basketno %]
+                                </div>
+                                <div class="modal-footer">
+                                    <a href="#" class="cancel" data-dismiss="modal" aria-hidden="true">Cancel</a>
+                                </div>
+                            </div>
+                        [% END %]
+                    </td>
+                </tr>
+            [% END %][%# FOREACH basket IN supplier.loop_basket %]
+            [% END %]
+        </tbody>
+    </table>
+[% ELSE %]
+    <p>No pending baskets</p>
 [% END %][%# IF ( loop_suppliers.size ) %]
 </div>
 </div>
+</div>
 <div class="yui-b">
 [% IF ( booksellerid ) %]
 [% INCLUDE 'vendor-menu.inc' %]
-- 
2.1.0