From 47e6b21af3865679ab0243be825a6ee251d29a3e Mon Sep 17 00:00:00 2001
From: Jonathan Druart <jonathan.druart@biblibre.com>
Date: Thu, 29 Aug 2013 14:04:32 +0200
Subject: [PATCH] Bug 10855: Search subscriptions by additional fields on the
 claim page

Test plan:
- create some serials in late.
- go on serials/claims.pl.
- choose a vendor.
- verify that additional fields are displayed in new columns.
- try filters on columns.
- verify there is no regression on this page.
---
 C4/Serials.pm                                      |    8 ++
 .../prog/en/modules/serials/claims.tt              |   99 +++++++-------------
 serials/claims.pl                                  |   10 ++
 3 files changed, 53 insertions(+), 64 deletions(-)

diff --git a/C4/Serials.pm b/C4/Serials.pm
index f1115c4..fd5ef37 100644
--- a/C4/Serials.pm
+++ b/C4/Serials.pm
@@ -2049,6 +2049,7 @@ sub GetLateOrMissingIssues {
     return unless ($supplierid);
 
     my $dbh = C4::Context->dbh;
+
     my $sth;
     my $byserial = '';
     if ($serialid) {
@@ -2104,6 +2105,13 @@ sub GetLateOrMissingIssues {
             $line->{claimdate}   = format_date( $line->{claimdate} );
         }
         $line->{"status".$line->{status}}   = 1;
+
+        my $additional_field_values = Koha::AdditionalField->fetch_all_values({
+            record_id => $line->{subscriptionid},
+            tablename => 'subscription'
+        });
+        %$line = ( %$line, additional_fields => $additional_field_values->{$line->{subscriptionid}} );
+
         push @issuelist, $line;
     }
     return @issuelist;
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/serials/claims.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/serials/claims.tt
index f4de94c..c2b2048 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/serials/claims.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/serials/claims.tt
@@ -1,4 +1,5 @@
 [% INCLUDE 'doc-head-open.inc' %]
+[% USE KohaAuthorisedValues %]
     <title>Koha &rsaquo; Serials &rsaquo; Claims</title>
     [% INCLUDE 'doc-head-close.inc' %]
 [% INCLUDE 'calendar.inc' %]
@@ -9,15 +10,17 @@
 <script type="text/javascript">
 //<![CDATA[
     [% IF (dateformat == 'metric') %]dt_add_type_uk_date();[% END %]
+    var sTable;
 	 $(document).ready(function() {
         [% UNLESS ( preview ) %]
-         var sTable = $("#claimst").dataTable($.extend(true, {}, dataTablesDefaults, {
+         sTable = $("#claimst").dataTable($.extend(true, {}, dataTablesDefaults, {
             "sDom": 't',
             "aoColumnDefs": [
-                { "aTargets": [ 0,1 ], "bSortable": false, "bSearchable": false }
+                { "aTargets": [ 0 ], "bSortable": false, "bSearchable": false }
             ],
             "bPaginate": false
         }));
+        sTable.fnAddFilters("filter", "200");
         [% END %]
 	    $('#supplierid').change(function() {
     	    $('#claims').submit();
@@ -51,13 +54,6 @@
 		return false;
 	    });	
 
-        $("#titlefilter").keyup( function () {
-            sTable.fnFilter( this.value, 3 ); // 3 is position of title column
-        } );
-
-        $("#branchfilter").keyup(function() {
-            sTable.fnFilter( this.value, 2 ); // 2 is the position of the author column
-	    });
 	 });
 
 	// Checks if the form can be sent (at least one checkbox must be checked)
@@ -68,31 +64,6 @@
 	    }
 	}
 
-    // Filter by status
-    function filterByStatus() {
-        selectedStatus = $("#statusfilter").val();
-        if (selectedStatus == "all") {
-            clearFilters();
-        } else {
-            $("table#claimst tbody tr").hide();
-            $("table#claimst tbody tr").each( function() {
-                if ( $(this).find("span.status-" + selectedStatus).size() > 0 ) {
-                    $(this).show();
-                }
-            });
-        }
-    }
-
-	// Filter by branch
-	function filterByBranch() {
-	    selectedBranch = $("#branchfilter").val();
-	    if (selectedBranch == "all") {
-		clearFilters();
-	    } else {
-		$("table#claimst tbody tr").hide();
-		$("table#claimst tbody tr:contains(" + selectedBranch + ")").show();
-	    }
-	}
 	// Filter by date
 	function filterByDate() {
         var beginDate = Date_from_syspref($("#from").val()).getTime();
@@ -140,6 +111,7 @@
 	// Clears filters : shows everything
 	function clearFilters() {
 	    $("table#claimst tbody tr").show();
+
 	}
 
 	function popup(supplierid,serialid){
@@ -203,42 +175,14 @@
 	
 	<ol>
 	<li>
-	    <label for="statusfilter">Status : </label>
-	    <select id="statusfilter" onchange="filterByStatus();">
-            <option value="all" selected="selected">(All)</option>
-            <option value="expected">Expected</option>
-            <option value="late">Late</option>
-            <option value="missing">Missing</option>
-            <option value="claimed">Claimed</option>
-	    </select>
-	</li>
-	
-	<li>
-	    <label for="titlefilter">Title : </label>
-        <input id="titlefilter" type="text" />
-    </li>
-    <li>
-	    <label for="branchfilter">Library: </label>
-	    <select id="branchfilter" onchange="filterByBranch();">
-            [% FOREACH branchloo IN branchloop %]
-                [% IF ( branchloo.selected ) %]
-                <option value="[% branchloo.value %]" selected="selected">[% branchloo.branchname %]</option>
-                [% ELSE %]
-                <option value="[% branchloo.value %]">[% branchloo.branchname %]</option>
-                [% END %]
-            [% END %]
-        </select>
-	</li>
-	
-	<li>
         <label for="from">From:</label>
         <input type="text" name="begindate" id="from" value="[% begindate %]" size="10" maxlength="10" class="datepickerfrom" />
         <label for="to" style="float:none;">To:</label>
         <input type="text" name="enddate" id="to" value="[% enddate %]" size="10" maxlength="10" class="datepickerto" />
         <span class="hint">[% INCLUDE 'date-format.inc' %]</span>
 	    <input type="button" value="OK" onclick="filterByDate();" />
-        </li>
-	
+    </li>
+
 	<li>
         <input type="reset" value="Clear filters" onclick="clearFilters();" />
 	</li>
@@ -261,7 +205,27 @@
                     <th>Status</th>
                     <th>Since</th>
                     <th>Claim date</th>
+                    [% FOR field IN additional_fields_for_subscription %]
+                      <th>[% field.name %]</th>
+                    [% END %]
                 </tr></thead>
+                <tfoot>
+                  <tr>
+                    [% IF ( letter ) %]
+                      <td></td>
+                    [% END %]
+                    <td><input type="text" class="filter" data-column_num="1" placeholder="Search vendor" /></td>
+                    <td><input type="text" class="filter" data-column_num="2" placeholder="Search library" /></td>
+                    <td><input type="text" class="filter" data-column_num="3" placeholder="Search title" /></td>
+                    <td><input type="text" class="filter" data-column_num="4" placeholder="Search issue number" /></td>
+                    <td><input type="text" class="filter" data-column_num="5" placeholder="Search status" /></td>
+                    <td></td>
+                    <td><input type="text" class="filter" data-column_num="7" placeholder="Search claim date" /></td>
+                    [% FOR field IN additional_fields_for_subscription %]
+                      <td><input type="text" class="filter" data-column_num="[% loop.count + 7 %]" placeholder="Search [% field.name %]" /></td>
+                    [% END %]
+                  </tr>
+                </tfoot>
                 <tbody>[% FOREACH missingissue IN missingissues %]
                     <tr>
                         [% IF ( letter ) %]
@@ -293,6 +257,13 @@
                         <td>
                         [% missingissue.claimdate %]
                         </td>
+                        [% FOR field IN additional_fields_for_subscription %]
+                          [% IF field.authorised_value_category %]
+                            <td>[% KohaAuthorisedValues.GetByCode( field.authorised_value_category, missingissue.additional_fields.${field.name} ) %]</td>
+                          [% ELSE %]
+                            <td>[% missingissue.additional_fields.${field.name} %]</td>
+                          [% END %]
+                        [% END %]
                     </tr>
                 [% END %]</tbody>
             </table>
diff --git a/serials/claims.pl b/serials/claims.pl
index 5377478..d1af723 100755
--- a/serials/claims.pl
+++ b/serials/claims.pl
@@ -28,6 +28,8 @@ use C4::Bookseller qw( GetBookSeller );
 use C4::Context;
 use C4::Letters;
 use C4::Branch;    # GetBranches GetBranchesLoop
+use C4::Koha qw( GetAuthorisedValues );
+use Koha::AdditionalField;
 use C4::Csv qw( GetCsvProfiles );
 
 my $input = CGI->new;
@@ -58,6 +60,13 @@ for my $s (@{$supplierlist} ) {
     }
 }
 
+my $additional_fields = Koha::AdditionalField->all( { table => 'subscription', searchable => 1 } );
+for my $field ( @$additional_fields ) {
+    if ( $field->{authorised_value_category} ) {
+        $field->{authorised_value_choices} = GetAuthorisedValues( $field->{authorised_value_category} );
+    }
+}
+
 my $letters = GetLetters('claimissues');
 my @letters;
 foreach (keys %{$letters}){
@@ -99,6 +108,7 @@ $template->param(
         claimletter => $claimletter,
         supplierloop => \@supplierinfo,
         branchloop   => $branchloop,
+        additional_fields_for_subscription => $additional_fields,
         csv_profiles => C4::Csv::GetCsvProfiles( "sql" ),
         (uc(C4::Context->preference("marcflavour"))) => 1
         );
-- 
1.7.10.4