From 5039857b0cc7719f6099d1a5c62207edf006af9b Mon Sep 17 00:00:00 2001
From: Julian Maurice <julian.maurice@biblibre.com>
Date: Wed, 8 Jan 2014 12:08:50 +0100
Subject: [PATCH] Bug 11708: basketgroup.pl can now display basketgroups for
 all suppliers

This happens when no booksellerid is given.
This patch also adds 4 columns:
- No. of ordered titles
- No. of received titles
- Ordered amount
- Charged amount (amount of received orders)

Add filters to basketgroup tables columns
Show closed date in closed basketgroups tables

Signed-off-by: juliette levast <juliette.levast@iepg.fr>
Signed-off-by: Paola Rossi <paola.rossi@cineca.it>
---
 acqui/basketgroup.pl                               |  37 ++-
 koha-tmpl/intranet-tmpl/prog/css/datatables.css    |   3 +
 .../prog/en/includes/acquisitions-menu.inc         |   3 +
 .../prog/en/modules/acqui/basketgroup.tt           | 265 +++++++++++++--------
 4 files changed, 204 insertions(+), 104 deletions(-)

diff --git a/acqui/basketgroup.pl b/acqui/basketgroup.pl
index 85422cb..74b354d 100755
--- a/acqui/basketgroup.pl
+++ b/acqui/basketgroup.pl
@@ -58,6 +58,8 @@ use Koha::EDI qw/create_edi_order get_edifact_ean/;
 
 use Koha::Acquisition::Bookseller;
 
+use List::MoreUtils qw/uniq/;
+
 our $input=new CGI;
 
 our ($template, $loggedinuser, $cookie)
@@ -388,12 +390,35 @@ if ( $op eq "add" ) {
     generate_edifact_orders( $basketgroupid );
     exit;
 }else{
-# no param : display the list of all basketgroups for a given vendor
-    my $basketgroups = &GetBasketgroups($booksellerid);
-    my $bookseller = Koha::Acquisition::Bookseller->fetch({ id => $booksellerid });
-    my $baskets = &GetBasketsByBookseller($booksellerid);
-
-    displaybasketgroups($basketgroups, $bookseller, $baskets);
+    my @booksellers;
+    if ($booksellerid) {
+        my $bookseller = Koha::Acquisition::Bookseller->fetch({ id => $booksellerid });
+        push @booksellers, $bookseller;
+        $template->param(booksellername => $booksellers[0]->{name});
+    } else {
+        @booksellers = Koha::Acquisition::Bookseller->search;
+    }
+    foreach my $bookseller (@booksellers) {
+        $bookseller->{basketgroups} = GetBasketgroups($bookseller->{id});
+        foreach my $basketgroup (@{ $bookseller->{basketgroups} }) {
+            my $baskets = GetBasketsByBasketgroup($basketgroup->{id});
+            $basketgroup->{basketsqty} = 0;
+            my (@ordered_biblionumbers, @received_biblionumbers);
+            foreach my $basket (@$baskets) {
+                $basketgroup->{basketsqty} += 1;
+                my @orders = GetOrders($basket->{basketno});
+                foreach my $order (@orders) {
+                    push @ordered_biblionumbers, $order->{biblionumber};
+                    if ($order->{datereceived}) {
+                        push @received_biblionumbers, $order->{biblionumber};
+                    }
+                }
+            }
+            $basketgroup->{ordered_titles_count} = uniq @ordered_biblionumbers;
+            $basketgroup->{received_titles_count} = uniq @received_biblionumbers;
+        }
+    }
+    $template->param(booksellers => \@booksellers);
 }
 $template->param(listclosed => ((defined $input->param('listclosed')) && ($input->param('listclosed') eq '1'))? 1:0 );
 #prolly won't use all these, maybe just use print, the rest can be done inside validate
diff --git a/koha-tmpl/intranet-tmpl/prog/css/datatables.css b/koha-tmpl/intranet-tmpl/prog/css/datatables.css
index c42e553..fa5fef2 100644
--- a/koha-tmpl/intranet-tmpl/prog/css/datatables.css
+++ b/koha-tmpl/intranet-tmpl/prog/css/datatables.css
@@ -240,6 +240,9 @@ tr.even.selected td {
 
 /* ColumnFilter */
 span.filter_column > input[type="text"] {
+    box-sizing: border-box;
+    -moz-box-sizing: border-box;
+    -webkit-box-sizing: border-box;
     font-size: 80%;
     width: 100%;
     box-sizing: border-box;
diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/acquisitions-menu.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/acquisitions-menu.inc
index f61dfa8..b611d5b 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/includes/acquisitions-menu.inc
+++ b/koha-tmpl/intranet-tmpl/prog/en/includes/acquisitions-menu.inc
@@ -2,6 +2,9 @@
 	<li><a href="/cgi-bin/koha/acqui/lateorders.pl">Late orders</a></li>
 	[% IF ( suggestion ) %]<li><a href="/cgi-bin/koha/suggestion/suggestion.pl">Suggestions</a></li>[% ELSE %][% END %]
     <li><a href="/cgi-bin/koha/acqui/invoices.pl">Invoices</a></li>
+    [% IF ( CAN_user_acquisition_group_manage ) %]
+      <li><a href="/cgi-bin/koha/acqui/basketgroup.pl">Basket groups</a></li>
+    [% END %]
     [% IF ( CAN_user_acquisition_budget_manage ) %]
     <li><a href="/cgi-bin/koha/admin/aqbudgetperiods.pl">Budgets</a></li>
     <li><a href="/cgi-bin/koha/admin/aqbudgets.pl">Funds</a></li>
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/basketgroup.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/basketgroup.tt
index 100565c..8ac84d2 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/basketgroup.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/basketgroup.tt
@@ -1,9 +1,15 @@
 [% USE Branches %]
+[% USE KohaDates %]
 [% INCLUDE 'doc-head-open.inc' %]
-<title>Koha &rsaquo; Basket grouping for [% booksellername |html %]</title>
+[% IF booksellerid %]
+  <title>Koha &rsaquo; Basket groups for [% booksellername |html %]</title>
+[% ELSE %]
+  <title>Koha &rsaquo; Basket groups</title>
+[% END %]
 <link rel="stylesheet" type="text/css" href="[% interface %]/[% theme %]/css/datatables.css" />
 [% INCLUDE 'doc-head-close.inc' %]
 [% INCLUDE 'datatables.inc' %]
+<script type="text/javascript" src="[% interface %]/lib/jquery/plugins/jquery.dataTables.columnFilter.js"></script>
 <script type="text/javascript" src="[% interface %]/lib/yui/utilities/utilities.js"></script>
 <script type="text/javascript" src="[% interface %]/lib/yui/button/button-min.js"></script>
 <script type="text/javascript" src="[% interface %]/lib/yui/container/container_core-min.js"></script>
@@ -107,15 +113,35 @@ function submitForm(form) {
 }
 
  $(document).ready(function() {
-    $("#basket_groups").tabs();
+    $(".basket_groups").tabs();
 
-    $("table").dataTable($.extend(true, {}, dataTablesDefaults, {
-        "aoColumnDefs": [
-            { "aTargets": [ -1 ], "bSortable": false, "bSearchable": false },
-        ],
-        "bAutoWidth": false,
-        "sPaginationType": "four_button"
-    } ));
+    $("table").each(function () {
+      var column_filter_options = {
+        sPlaceHolder: "head:after",
+        aoColumns: [
+          { type: "text" },
+          { type: "text" },
+          { type: "text" },
+          { type: "text" },
+          { type: "text" },
+          { type: "text" },
+          { type: "text" },
+          null
+        ]
+      };
+      if ($(this).hasClass('basket_group_closed')) {
+        // There is one more column in closed basketgroups tables
+        column_filter_options.aoColumns.unshift({ type: "text" });
+      }
+      $(this).dataTable($.extend(true, {}, dataTablesDefaults, {
+          "aoColumnDefs": [
+              { "aTargets": [ -1 ], "bSortable": false, "bSearchable": false },
+          ],
+          "bPaginate": false,
+          "bAutoWidth": false
+      } ))
+      .columnFilter(column_filter_options);
+    });
 
  });
 
@@ -129,10 +155,14 @@ function submitForm(form) {
 
 <div id="breadcrumbs"><a href="/cgi-bin/koha/mainpage.pl">Home</a> &rsaquo; <a href="/cgi-bin/koha/acqui/acqui-home.pl">Acquisitions</a> &rsaquo;
 [% IF ( grouping ) %]
-    <a href="/cgi-bin/koha/acqui/supplier.pl?booksellerid=[% booksellerid %]">[% booksellername |html %]</a> &rsaquo; <a href="/cgi-bin/koha/acqui/basketgroup.pl?booksellerid=[% booksellerid %]">Basket grouping</a> &rsaquo; Add basket group for [% booksellername |html %]</div>
+    <a href="/cgi-bin/koha/acqui/supplier.pl?booksellerid=[% booksellerid %]">[% booksellername |html %]</a> &rsaquo; <a href="/cgi-bin/koha/acqui/basketgroup.pl?booksellerid=[% booksellerid %]">Basket groups</a> &rsaquo; Add basket group for [% booksellername |html %]
 [% ELSE %]
-    <a href="/cgi-bin/koha/acqui/supplier.pl?booksellerid=[% booksellerid %]">[% booksellername |html %]</a> &rsaquo; Basket grouping</div>
+  [% IF (booksellerid) %]
+    <a href="/cgi-bin/koha/acqui/supplier.pl?booksellerid=[% booksellerid %]">[% booksellername |html %]</a> &rsaquo;
+  [% END %]
+  Basket groups
 [% END %]
+</div>
 
 <div id="doc3" class="yui-t2">
     <div id="bd">
@@ -299,97 +329,136 @@ function submitForm(form) {
                         </div>
                     </div>
                 [% ELSE %]
-                    <div id="toolbar" class="btn-toolbar">
-                        <div class="btn-group"><a href="/cgi-bin/koha/acqui/basketgroup.pl?op=add&amp;booksellerid=[% booksellerid %]" class="btn btn-small" id="newbasketgroup"><i class="fa fa-plus"></i> New basket group</a></div>
-                    </div>
-                    <h1>Basket grouping for <a href="/cgi-bin/koha/acqui/supplier.pl?booksellerid=[% booksellerid %]">[% booksellername |html %]</a></h1>
-                    <div id="basket_groups" class="toptabs">
-                        <ul class="ui-tabs-nav">
-                            [% UNLESS ( listclosed) %]<li class="ui-tabs-active"><a href="#opened">Open</a></li>
-                            [% ELSE%]<li><a href="#opened">Open</a></li>[% END %]
-                            [% IF ( listclosed) %]<li class="ui-tabs-active"><a href="#closed">Closed</a></li>
-                            [% ELSE %]<li><a href="#closed">Closed</a></li>[% END %]
-                        </ul>
-                        <div id="opened">
-                            <table id="basket_group_opened">
-                                <thead>
-                                    <tr>
-                                        <th>Name</th>
-                                        <th>Number</th>
-                                        <th>Billing place</th>
-                                        <th>Delivery place</th>
-                                        <th>Number of baskets</th>
-                                        <th>Action</th>
-                                    </tr>
-                                </thead>
-                                <tbody>
-                                    [% FOREACH basketgroup IN basketgroups %]
-                                        [% UNLESS ( basketgroup.closed ) %]
+                    [% IF booksellerid %]
+                        <div id="toolbar" class="btn-toolbar">
+                            <div class="btn-group"><a href="/cgi-bin/koha/acqui/basketgroup.pl?op=add&amp;booksellerid=[% booksellerid %]" class="btn btn-small" id="newbasketgroup"><i class="fa fa-plus"></i> New basket group</a></div>
+                        </div>
+                    [% END %]
+
+                    [% FOREACH bookseller IN booksellers %]
+                        [% IF bookseller.basketgroups.size > 0 %]
+                            <h1>Basket groups for <a href="/cgi-bin/koha/acqui/supplier.pl?booksellerid=[% bookseller.id %]">[% bookseller.name |html %]</a></h1>
+                            <div class="basket_groups toptabs">
+                                <ul class="ui-tabs-nav">
+                                    [% UNLESS ( listclosed) %]<li class="ui-tabs-active"><a href="#opened[% bookseller.id %]">Open</a></li>
+                                    [% ELSE%]<li><a href="#opened[% bookseller.id %]">Open</a></li>[% END %]
+                                    [% IF ( listclosed) %]<li class="ui-tabs-active"><a href="#closed[% bookseller.id %]">Closed</a></li>
+                                    [% ELSE %]<li><a href="#closed[% bookseller.id %]">Closed</a></li>[% END %]
+                                </ul>
+                                <div id="opened[% bookseller.id %]">
+                                    <table id="basket_group_opened">
+                                        <thead>
                                             <tr>
-                                                <td>[% IF ( basketgroup.name ) %]
-                                                    [% basketgroup.name %]
-                                                    [% ELSE %]
-                                                        Basket group no. [% basketgroup.id %]
-                                                    [% END %]
-                                                </td>
-                                                <td>[% basketgroup.id %]</td>
-                                                <td>[% Branches.GetName( basketgroup.billingplace ) %]</td>
-                                                <td>[% IF (basketgroup.freedeliveryplace) %]Free delivery place[% ELSE %][% Branches.GetName( basketgroup.deliveryplace ) %][% END %]</td>
-                                                <td>[% basketgroup.basketsqty %]</td>
-                                                <td>
-                                                    <input type="button" onclick="closeandprint('[% basketgroup.id %]');" value="Close and export as PDF" />
-                                                    <form action="/cgi-bin/koha/acqui/basketgroup.pl" method="get"><input type="hidden" name="op" value="add" /><input type="hidden" name="booksellerid" value="[% basketgroup.booksellerid %]" /><input type="hidden" name="basketgroupid" value="[% basketgroup.id %]" /><input type="submit" value="Edit" /></form>
-                                                    [% UNLESS basketgroup.basketsqty %]
-                                                        <form action="/cgi-bin/koha/acqui/basketgroup.pl" method="get"><input type="hidden" name="op" value="delete" /><input type="hidden" name="booksellerid" value="[% basketgroup.booksellerid %]" /><input type="hidden" name="basketgroupid" value="[% basketgroup.id %]" /><input type="submit" value="Delete" /></form>
-                                                    [% END %]
-                                                </td>
+                                                <th>Search name</th>
+                                                <th>Search no.</th>
+                                                <th>Search billing place</th>
+                                                <th>Search delivery place</th>
+                                                <th>Search no. of baskets</th>
+                                                <th>Search no. of ordered titles</th>
+                                                <th>Search no. of received titles</th>
+                                                <th></th>
                                             </tr>
-                                        [% END %]
-                                    [% END %]
-                                </tbody>
-                            </table>
-                        </div>
-                        <div id="closed">
-                            <table id="basket_group_closed">
-                                <thead>
-                                    <tr>
-                                        <th>Name</th>
-                                        <th>Number</th>
-                                        <th>Billing place</th>
-                                        <th>Delivery place</th>
-                                        <th>Number of baskets</th>
-                                        <th>Action</th>
-                                    </tr>
-                                </thead>
-                                <tbody>
-                                    [% FOREACH basketgroup IN basketgroups %]
-                                        [% IF ( basketgroup.closed ) %]
                                             <tr>
-                                                <td>
-                                                    [% IF ( basketgroup.name ) %]
-                                                        [% basketgroup.name %]
-                                                        [% ELSE %]
-                                                            Basket group no. [% basketgroup.id %]
-                                                        [% END %]
-                                                </td>
-                                                <td>[% basketgroup.id %]</td>
-                                                <td>[% Branches.GetName( basketgroup.billingplace ) %]</td>
-                                                <td>[% IF (basketgroup.freedeliveryplace) %]Free delivery place[% ELSE %][% Branches.GetName( basketgroup.deliveryplace ) %][% END %]</td>
-                                                <td>[% basketgroup.basketsqty %]</td>
-                                                <td>
-                                                    <form action="/cgi-bin/koha/acqui/basketgroup.pl" method="get"><input type="hidden" name="op" value="add" /><input type="hidden" name="booksellerid" value="[% basketgroup.booksellerid %]" /><input type="hidden" name="basketgroupid" value="[% basketgroup.id %]" /><input type="submit" value="View" /></form>
-                                                    <form action="/cgi-bin/koha/acqui/basketgroup.pl" method="get"><input type="hidden" name="op" value="reopen" /><input type="hidden" name="booksellerid" value="[% basketgroup.booksellerid %]" /><input type="hidden" name="basketgroupid" value="[% basketgroup.id %]" /><input type="submit" value="Reopen" /></form>
-                                                    <form action="/cgi-bin/koha/acqui/basketgroup.pl" method="get"><input type="hidden" name="op" value="print" /><input type="hidden" name="basketgroupid" value="[% basketgroup.id %]" /><input type="submit" value="Export as PDF" /></form>
-                                                    <form action="/cgi-bin/koha/acqui/basketgroup.pl" method="get"><input type="hidden" name="op" value="export" /><input type="hidden" name="basketgroupid" value="[% basketgroup.id %]" /><input type="submit" value="Export as CSV" /></form>
-                                                    <form action="/cgi-bin/koha/acqui/basketgroup.pl" method="get"><input type="hidden" name="op" value="ediprint" /><input type="hidden" name="basketgroupid" value="[% basketgroup.id %]" /><input type="submit" value="Generate EDIFACT order" /></form>
-                                                </td>
+                                                <th>Name</th>
+                                                <th>No.</th>
+                                                <th>Billing place</th>
+                                                <th>Delivery place</th>
+                                                <th>No. of baskets</th>
+                                                <th>No. of ordered titles</th>
+                                                <th>No. of received titles</th>
+                                                <th>Action</th>
                                             </tr>
-                                        [% END %]
-                                    [% END %]
-                                </tbody>
-                            </table>
-                        </div>
-                    </div>
+                                        </thead>
+                                        <tbody>
+                                            [% FOREACH basketgroup IN bookseller.basketgroups %]
+                                                [% UNLESS ( basketgroup.closed ) %]
+                                                    <tr>
+                                                        <td>
+                                                            [% IF ( basketgroup.name ) %]
+                                                                [% basketgroup.name %]
+                                                            [% ELSE %]
+                                                                Basket group no. [% basketgroup.id %]
+                                                            [% END %]
+                                                        </td>
+                                                        <td>[% basketgroup.id %]</td>
+                                                        <td>[% Branches.GetName(basketgroup.billingplace) %]</td>
+                                                        <td>[% IF (basketgroup.freedeliveryplace) %]Free delivery place[% ELSE %][% Branches.GetName(basketgroup.deliveryplace) %][% END %]</td>
+                                                        <td>[% basketgroup.basketsqty %]</td>
+                                                        <td>[% basketgroup.ordered_titles_count %]</td>
+                                                        <td>[% basketgroup.received_titles_count %]</td>
+                                                        <td>
+                                                            <input type="button" onclick="closeandprint('[% basketgroup.id %]');" value="Close and export as PDF" />
+                                                            <form action="/cgi-bin/koha/acqui/basketgroup.pl" method="get"><input type="hidden" name="op" value="add" /><input type="hidden" name="booksellerid" value="[% basketgroup.booksellerid %]" /><input type="hidden" name="basketgroupid" value="[% basketgroup.id %]" /><input type="submit" value="Edit" /></form>
+                                                            [% UNLESS basketgroup.basketsqty %]
+                                                                <form action="/cgi-bin/koha/acqui/basketgroup.pl" method="get"><input type="hidden" name="op" value="delete" /><input type="hidden" name="booksellerid" value="[% basketgroup.booksellerid %]" /><input type="hidden" name="basketgroupid" value="[% basketgroup.id %]" /><input type="submit" value="Delete" /></form>
+                                                            [% END %]
+                                                        </td>
+                                                    </tr>
+                                                [% END %]
+                                            [% END %]
+                                        </tbody>
+                                    </table>
+                                </div>
+                                <div id="closed[% bookseller.id %]">
+                                    <table class="basket_group_closed">
+                                        <thead>
+                                            <tr>
+                                                <th>Search name</th>
+                                                <th>Search no.</th>
+                                                <th>Search date closed</th>
+                                                <th>Search billing place</th>
+                                                <th>Search delivery place</th>
+                                                <th>Search no. of baskets</th>
+                                                <th>Search no. of ordered titles</th>
+                                                <th>Search no. of received titles</th>
+                                                <th></th>
+                                            </tr>
+                                            <tr>
+                                                <th>Name</th>
+                                                <th>No.</th>
+                                                <th>Date closed</th>
+                                                <th>Billing place</th>
+                                                <th>Delivery place</th>
+                                                <th>No. of baskets</th>
+                                                <th>No. of ordered titles</th>
+                                                <th>No. of received titles</th>
+                                                <th>Action</th>
+                                            </tr>
+                                        </thead>
+                                        <tbody>
+                                            [% FOREACH basketgroup IN bookseller.basketgroups %]
+                                                [% IF ( basketgroup.closed ) %]
+                                                    <tr>
+                                                        <td>
+                                                            [% IF ( basketgroup.name ) %]
+                                                                [% basketgroup.name %]
+                                                            [% ELSE %]
+                                                                Basket group no. [% basketgroup.id %]
+                                                            [% END %]
+                                                        </td>
+                                                        <td>[% basketgroup.id %]</td>
+                                                        <td>[% basketgroup.closeddate |$KohaDates %]</td>
+                                                        <td>[% Branches.GetName(basketgroup.billingplace) %]</td>
+                                                        <td>[% IF (basketgroup.freedeliveryplace) %]Free delivery place[% ELSE %][% Branches.GetName(basketgroup.deliveryplace) %][% END %]</td>
+                                                        <td>[% basketgroup.basketsqty %]</td>
+                                                        <td>[% basketgroup.ordered_titles_count %]</td>
+                                                        <td>[% basketgroup.received_titles_count %]</td>
+                                                        <td>
+                                                            <form action="/cgi-bin/koha/acqui/basketgroup.pl" method="get"><input type="hidden" name="op" value="add" /><input type="hidden" name="booksellerid" value="[% basketgroup.booksellerid %]" /><input type="hidden" name="basketgroupid" value="[% basketgroup.id %]" /><input type="submit" value="View" /></form>
+                                                            <form action="/cgi-bin/koha/acqui/basketgroup.pl" method="get"><input type="hidden" name="op" value="reopen" /><input type="hidden" name="booksellerid" value="[% basketgroup.booksellerid %]" /><input type="hidden" name="basketgroupid" value="[% basketgroup.id %]" /><input type="submit" value="Reopen" /></form>
+                                                            <form action="/cgi-bin/koha/acqui/basketgroup.pl" method="get"><input type="hidden" name="op" value="print" /><input type="hidden" name="basketgroupid" value="[% basketgroup.id %]" /><input type="submit" value="Export as PDF" /></form>
+                                                            <form action="/cgi-bin/koha/acqui/basketgroup.pl" method="get"><input type="hidden" name="op" value="export" /><input type="hidden" name="basketgroupid" value="[% basketgroup.id %]" /><input type="submit" value="Export as CSV" /></form>
+                                                            <form action="/cgi-bin/koha/acqui/basketgroup.pl" method="get"><input type="hidden" name="op" value="ediprint" /><input type="hidden" name="basketgroupid" value="[% basketgroup.id %]" /><input type="submit" value="Generate EDIFACT order" /></form>
+                                                        </td>
+                                                    </tr>
+                                                [% END %]
+                                            [% END %]
+                                        </tbody>
+                                    </table>
+                                </div>
+                            </div>
+                        [% END %]
+                    [% END %]
                 [% END %]
             </div>
         </div>
-- 
2.1.4