From e50eed0bbdef03ec899437a284e63a812d0829e6 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.
It also adds a new link to filter on opened baskets only and the ability
to switch to a simple view if the complete view is to complex

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.

Signed-off-by: Paola Rossi <paola.rossi@cineca.it>
---
 acqui/booksellers.pl                               |  11 +
 koha-tmpl/intranet-tmpl/prog/css/datatables.css    |  17 +
 .../prog/en/modules/acqui/booksellers.tt           | 364 +++++++++++++--------
 3 files changed, 258 insertions(+), 134 deletions(-)

diff --git a/acqui/booksellers.pl b/acqui/booksellers.pl
index b56f73e..e981b63 100755
--- a/acqui/booksellers.pl
+++ b/acqui/booksellers.pl
@@ -156,11 +156,22 @@ for my $vendor (@suppliers) {
       };
 
 }
+
+my $sessionID = $query->cookie('CGISESSID');
+my $session = C4::Auth::get_session($sessionID);
+my $view = $query->param('view');
+if ( $view ) {
+    $session->param('bookseller_default_view', $view);
+} else {
+    $view = $session->param('bookseller_default_view');
+}
+
 $template->param(
     loop_suppliers => $loop_suppliers,
     supplier       => ( $booksellerid || $supplier ),
     count          => $supplier_count,
     has_budgets          => $has_budgets,
+    view           => $view,
 );
 $template->{VARS}->{'allbaskets'} = $allbaskets;
 
diff --git a/koha-tmpl/intranet-tmpl/prog/css/datatables.css b/koha-tmpl/intranet-tmpl/prog/css/datatables.css
index c42e553..3f00a2f 100644
--- a/koha-tmpl/intranet-tmpl/prog/css/datatables.css
+++ b/koha-tmpl/intranet-tmpl/prog/css/datatables.css
@@ -251,6 +251,23 @@ table.group td.group {
     background-color: #D1CFD0;
     border-bottom: 2px solid #A19B9E;
     border-top: 2px solid #A19B9E;
+    font-weight: bold;
+}
+
+table.group tr.odd td {
+    background-color : #F3F3F3;
+    border : 1px solid #BCBCBC;
+    border-right : 1px solid #BCBCBC;
+}
+
+table.group tr.even td {
+    background-color: #FFF;
+    border-right : 1px solid #BCBCBC;
+}
+
+table.group td.group .action {
+    float: right;
+    font-weight: normal;
 }
 
 table.group tr.odd td {
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 ff92a91..0614a4c 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,79 @@
 [% 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="[% interface %]/[% theme %]/css/datatables.css" />
+[% INCLUDE 'calendar.inc' %]
 [% INCLUDE 'datatables.inc' %]
+<script type="text/javascript" src="[% interface %]/lib/jquery/plugins/jquery.dataTables.columnFilter.js"></script>
+
 <script type="text/javascript">
 //<![CDATA[
+var SHOW_ONLY_OPENED = _("Show opened baskets only");
+var SHOW_ALL = _("Show opened and closed baskets");
+var only_opened = 0;
 $(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: 'text' },
+                { type: 'text' },
+                { type: 'text' },
+                { type: 'text' },
+                { type: 'text' },
+                { 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;
@@ -33,6 +84,19 @@ $(document).ready(function() {
         var legend = _("Add order to basket %s").format(basket);
         $(this).find("legend").html(legend);
     });
+
+    $("#toggle_closed_baskets").on('click', function(e){
+        e.preventDefault();
+        if( only_opened ) {
+            $(this).text(SHOW_ONLY_OPENED);
+            table.fnFilter('', 9);
+            only_opened = 0;
+        } else {
+            $(this).text(SHOW_ALL);
+            table.fnFilter('^$', 9, 1);
+            only_opened = 1;
+        }
+    });
 });
 //]]>
 </script>
@@ -59,135 +123,167 @@ $(document).ready(function() {
     [% ELSE %]
         <h1>Vendor search: [% count %] results found for '[% supplier %]'</h1>
     [% END %]
-[% 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>
+
+    [% BLOCK action %]
+        <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="fa fa-plus"></i> New basket</a>
+               [% END %]
             [% END %]
-        </select>
-        </p>
+            [% IF ( supplier.loop_basket.size ) %]
+                <a class="btn btn-small" href="/cgi-bin/koha/acqui/parcels.pl?booksellerid=[% supplier.booksellerid %]"><i class="fa fa-inbox"></i> Receive shipment</a>
+            [% END %]
+        </span>
     [% END %]
-    [% IF ( allbaskets ) %]
-        <p><a href="/cgi-bin/koha/acqui/booksellers.pl?supplier=[% supplier %]&amp;booksellerid=[% booksellerid %]">Show active baskets only</a></p>
+    [% IF view == "complete" %]
+        <p><a href="/cgi-bin/koha/acqui/booksellers.pl?supplier=[% supplier %]&view=simple">Switch to the simple view</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>
+        <p><a href="/cgi-bin/koha/acqui/booksellers.pl?supplier=[% supplier %]&view=complete">Switch to the complete view</a></p>
     [% END %]
-    <div id="acqui_order_supplierlist">
-        [% 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>
+[% END %]
+[% IF ( loop_suppliers.size ) %]
+    [% IF view == "complete" %]
+        [% IF allbaskets %]
+            <a href="/cgi-bin/koha/acqui/booksellers.pl?supplier=[% supplier %]&amp;booksellerid=[% booksellerid %]">Show active baskets only</a>
+        [% ELSE %]
+            <a href="/cgi-bin/koha/acqui/booksellers.pl?supplier=[% supplier %]&amp;booksellerid=[% booksellerid %]&amp;allbaskets=1">Show active and inactive baskets</a>
+        [% END %]
+         | <a href="#" id="toggle_closed_baskets">Show opened baskets only</a>
+        <div id="acqui_order_basketlist">
+        <table id="baskets" class="group">
+            <thead>
+                <tr>
+                    <th class='NoVisible'>bookseller</th>
+                    <th>No.</th>
+                    <th>Name</th>
+                    <th>Item count</th>
+                    <th>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 %]
+                [% IF supplier.loop_basket.size == 0 %]
+                    <tr>
+                        <td>
+                            <a name="[% supplier.booksellerid %]" href="/cgi-bin/koha/acqui/supplier.pl?booksellerid=[% supplier.booksellerid %]">[% supplier.name %]</a>
+                            ([% supplier.booksellerid %])
+                            [% PROCESS action supplier = supplier %]
+                        </td>
+                        <td>
+                            [% IF allbaskets %]
+                                No basket.
+                            [% ELSE %]
+                                No active basket.
+                            [% END %]
+                        </td>
+                        <td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td>
+                    </tr>
+                [% END %]
+                [% FOREACH basket IN supplier.loop_basket %]
+                    [% IF ( basket.uncertainprices ) %]
+                        <tr class="problem">
                     [% ELSE %]
-                        <a name="[% supplier.booksellerid %]" href="/cgi-bin/koha/acqui/supplier.pl?booksellerid=[% supplier.booksellerid %]">NO NAME</a>
+                        <tr>
                     [% 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="fa fa-plus"></i> New basket</a>
+                        <td>
+                            <a name="[% supplier.booksellerid %]" href="/cgi-bin/koha/acqui/supplier.pl?booksellerid=[% supplier.booksellerid %]">[% supplier.name %]</a>
+                            ([% supplier.booksellerid %])
+                            [% PROCESS action supplier = supplier %]
+                        </td>
+                        <td>[% basket.basketno %]</td>
+                        <td>[% basket.basketname %]</td>
+                        <td>
+                            [% basket.total_items %]
+                            [% IF basket.total_items_cancelled %]
+                                ([% basket.total_items_cancelled %] cancelled)
+                            [% END %]
+                        </td>
+                        <td>
+                            [% basket.total_biblios %]
+                            [% IF basket.total_biblios_cancelled %]
+                                ([% basket.total_biblios_cancelled %] cancelled)
+                            [% END %]
+                        </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 class="actions">
+                            <a class="btn btn-mini" href="/cgi-bin/koha/acqui/basket.pl?basketno=[% basket.basketno %]"><i class="fa fa-eye"></i> View</a>
+
+                            [% IF not basket.closedate and supplier.active %]
+                                <a class="btn btn-mini" id="addtoBasketLabel[% basket.basketno %]" href="#addtoBasket[% basket.basketno %]" role="button" data-toggle="modal"><i class="fa fa-plus"></i> 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>
+        </div>
+    [% ELSE %]
+        <div id="acqui_order_supplierlist">
+            [% 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 %]
-                            (inactive)
+                            <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="fa fa-plus"></i> New basket</a>
+                            [% ELSE %]
+                                (inactive)
+                            [% END %]
                         [% END %]
-                    [% END %]
                         [% IF ( supplier.loop_basket.size ) %]
-                    <a class="btn btn-small" href="/cgi-bin/koha/acqui/parcels.pl?booksellerid=[% supplier.booksellerid %]"><i class="fa fa-inbox"></i> Receive shipment</a>
+                            <a class="btn btn-small" href="/cgi-bin/koha/acqui/parcels.pl?booksellerid=[% supplier.booksellerid %]"><i class="fa fa-inbox"></i> Receive shipment</a>
                         [% END %]
-                </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>Actions</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 class="actions">
-                                            <a class="btn btn-mini" href="/cgi-bin/koha/acqui/basket.pl?basketno=[% basket.basketno %]"><i class="fa fa-eye"></i> View</a>
-
-                                            [% IF not basket.closedate and supplier.active %]
-                                                <a class="btn btn-mini" id="addtoBasketLabel[% basket.basketno %]" href="#addtoBasket[% basket.basketno %]" role="button" data-toggle="modal"><i class="fa fa-plus"></i> 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>
-
-                    [% ELSE %]
-                        <p>No pending baskets</p>
-                    [% END %][%# IF ( supplier.loop_basket.size ) %]
+                    </span>
                 </div>
-            </div>
-        [% END %][%# FOREACH supplier IN loop_suppliers %]
-    </div>
+            [% END %]
+        </div>
+    [% END %]
+[% ELSE %]
+    <p>No pending baskets</p>
 [% END %][%# IF ( loop_suppliers.size ) %]
 </div>
 </div>
-- 
2.1.4