Bug 32336: (QA follow-up) Use $metadata->schema
[srvgit] / catalogue / itemsearch.pl
index ee8ca66..72074a8 100755 (executable)
@@ -3,29 +3,28 @@
 #
 # This file is part of Koha
 #
-# Koha is free software; you can redistribute it and/or modify it under the
-# terms of the GNU General Public License as published by the Free Software
-# Foundation; either version 3 of the License, or (at your option) any later
-# version.
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
 #
-# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
-# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
 #
-# You should have received a copy of the GNU General Public License along
-# with Koha; if not, write to the Free Software Foundation, Inc.,
-# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
 
 use Modern::Perl;
 use CGI;
 
-use JSON;
+use JSON qw( to_json );
 
-use C4::Auth;
-use C4::Output;
-use C4::Items;
-use C4::Biblio;
-use C4::Koha;
+use C4::Auth qw( get_template_and_user );
+use C4::Output qw( output_with_http_headers output_html_with_http_headers );
+use C4::Items qw( SearchItems );
+use C4::Koha qw( GetAuthorisedValues );
 
 use Koha::AuthorisedValues;
 use Koha::Biblios;
@@ -33,7 +32,7 @@ use Koha::Item::Search::Field qw(GetItemSearchFields);
 use Koha::ItemTypes;
 use Koha::Libraries;
 
-my $cgi = new CGI;
+my $cgi = CGI->new;
 my %params = $cgi->Vars;
 
 my $format = $cgi->param('format');
@@ -61,9 +60,15 @@ if (defined $format and $format eq 'json') {
             my @words = split /\s+/, $sSearch;
             foreach my $word (@words) {
                 push @f, $columns[$i];
-                push @q, "%$word%";
-                push @op, 'like';
                 push @c, 'and';
+
+                if ( grep { $_ eq $columns[$i] } qw( ccode homebranch holdingbranch location itype notforloan itemlost onloan ) ) {
+                    push @q, "$word";
+                    push @op, '=';
+                } else {
+                    push @q, "%$word%";
+                    push @op, 'like';
+                }
             }
         }
     }
@@ -87,17 +92,20 @@ my ($template, $borrowernumber, $cookie) = get_template_and_user({
     template_name => $template_name,
     query => $cgi,
     type => 'intranet',
-    authnotrequired => 0,
     flagsrequired   => { catalogue => 1 },
 });
 
-my $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => '', kohafield => 'items.notforloan', authorised_value => { not => undef } });
-my $notforloan_values = $mss->count ? GetAuthorisedValues($mss->next->authorised_value) : [];
+my $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => '', kohafield => 'items.itemlost', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
+my $itemlost_values = $mss->count ? GetAuthorisedValues($mss->next->authorised_value) : [];
 
-$mss = Koha::MarcSubfieldStructures->search({ frameworkcode => '', kohafield => 'items.location', authorised_value => { not => undef } });
-my $location_values = $mss->count ? GetAuthorisedValues($mss->next->authorised_value) : [];
+$mss = Koha::MarcSubfieldStructures->search({ frameworkcode => '', kohafield => 'items.withdrawn', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
+my $withdrawn_values = $mss->count ? GetAuthorisedValues($mss->next->authorised_value) : [];
+
+if ( Koha::MarcSubfieldStructures->search( { frameworkcode => '', kohafield => 'items.new_status' } )->count ) {
+    $template->param( has_new_status => 1 );
+}
 
-if (scalar keys %params > 0) {
+if ( defined $format ) {
     # Parameters given, it's a search
 
     my $filter = {
@@ -105,7 +113,7 @@ if (scalar keys %params > 0) {
         filters => [],
     };
 
-    foreach my $p (qw(homebranch holdingbranch location itype ccode issues datelastborrowed notforloan)) {
+    foreach my $p (qw(homebranch holdingbranch location itype ccode issues datelastborrowed notforloan itemlost withdrawn)) {
         if (my @q = $cgi->multi_param($p)) {
             if ($q[0] ne '') {
                 my $f = {
@@ -131,10 +139,11 @@ if (scalar keys %params > 0) {
         my $q = shift @q;
         my $op = shift @op;
         if (defined $q and $q ne '') {
+            if (C4::Context->preference("marcflavour") ne "UNIMARC" && $field eq 'publicationyear') {
+                $field = 'copyrightdate';
+            }
+
             if ($i == 0) {
-                if (C4::Context->preference("marcflavour") ne "UNIMARC" && $field eq 'publicationyear') {
-                    $field = 'copyrightdate';
-                }
                 $f = {
                     field => $field,
                     query => $q,
@@ -158,12 +167,15 @@ if (scalar keys %params > 0) {
     push @{ $filter->{filters} }, $f;
 
     # Yes/No parameters
-    foreach my $p (qw(damaged itemlost)) {
+    foreach my $p (qw( damaged new_status )) {
         my $v = $cgi->param($p) // '';
         my $f = {
             field => $p,
             query => 0,
         };
+        if ( $p eq 'new_status' ) {
+            $f->{ifnull} = 0;
+        }
         if ($v eq 'yes') {
             $f->{operator} = '!=';
             push @{ $filter->{filters} }, $f;
@@ -173,6 +185,21 @@ if (scalar keys %params > 0) {
         }
     }
 
+    # null/is not null parameters
+    foreach my $p (qw( onloan )) {
+        my $v = $cgi->param($p) // '';
+        my $f = {
+            field => $p,
+            operator => "is",
+        };
+        if ( $v eq 'IS NOT NULL' ) {
+            $f->{query} = "not null";
+        } elsif ( $v eq 'IS NULL' ) {
+            $f->{query} = "null";
+        }
+        push @{ $filter->{filters} }, $f unless ( $v eq "" );
+    }
+
     if (my $itemcallnumber_from = scalar $cgi->param('itemcallnumber_from')) {
         push @{ $filter->{filters} }, {
             field => 'itemcallnumber',
@@ -214,33 +241,21 @@ if (scalar keys %params > 0) {
     }
 
     if ($results) {
-        # Get notforloan labels
-        my $notforloan_map = {};
-        foreach my $nfl_value (@$notforloan_values) {
-            $notforloan_map->{$nfl_value->{authorised_value}} = $nfl_value->{lib};
-        }
-
-        # Get location labels
-        my $location_map = {};
-        foreach my $loc_value (@$location_values) {
-            $location_map->{$loc_value->{authorised_value}} = $loc_value->{lib};
-        }
-
         foreach my $item (@$results) {
-            $item->{biblio} = Koha::Biblios->find( $item->{biblionumber} );
-            ($item->{biblioitem}) = GetBiblioItemByBiblioNumber($item->{biblionumber});
-            $item->{status} = $notforloan_map->{$item->{notforloan}};
-            if (defined $item->{location}) {
-                $item->{location} = $location_map->{$item->{location}};
-            }
+            my $biblio = Koha::Biblios->find( $item->{biblionumber} );
+            $item->{biblio} = $biblio;
+            $item->{biblioitem} = $biblio->biblioitem->unblessed;
+            my $checkout = Koha::Checkouts->find({ itemnumber => $item->{itemnumber} });
+            $item->{checkout} = $checkout;
         }
     }
 
     $template->param(
-        filter => $filter,
+        filter        => $filter,
         search_params => $search_params,
-        results => $results,
-        total_rows => $total_rows,
+        results       => $results,
+        total_rows    => $total_rows,
+        user          => Koha::Patrons->find( $borrowernumber ),
     );
 
     if ($format eq 'csv') {
@@ -253,6 +268,7 @@ if (scalar keys %params > 0) {
             print "$line\n" unless $line =~ m|^\s*$|;
         }
     } elsif ($format eq 'json') {
+        $template->param(sEcho => scalar $cgi->param('sEcho'));
         output_with_http_headers $cgi, $cookie, $template->output, 'json';
     }
 
@@ -261,36 +277,26 @@ if (scalar keys %params > 0) {
 
 # Display the search form
 
-my @branches = map { value => $_->branchcode, label => $_->branchname }, Koha::Libraries->search( {}, { order_by => 'branchname' } );
-my @locations;
-foreach my $location (@$location_values) {
-    push @locations, {
-        value => $location->{authorised_value},
-        label => $location->{lib} // $location->{authorised_value},
-    };
-}
-my @itemtypes;
-foreach my $itemtype ( Koha::ItemTypes->search ) {
-    push @itemtypes, {
-        value => $itemtype->itemtype,
-        label => $itemtype->translated_description,
-    };
+my @branches = map { value => $_->branchcode, label => $_->branchname }, Koha::Libraries->search( {}, { order_by => 'branchname' } )->as_list;
+my @itemtypes = map { value => $_->itemtype, label => $_->translated_description }, Koha::ItemTypes->search_with_localization->as_list;
+
+my @ccodes = Koha::AuthorisedValues->get_descriptions_by_koha_field({ kohafield => 'items.ccode' });
+foreach my $ccode (@ccodes) {
+    $ccode->{value} = $ccode->{authorised_value},
+    $ccode->{label} = $ccode->{lib},
 }
 
-$mss = Koha::MarcSubfieldStructures->search({ frameworkcode => '', kohafield => 'items.ccode', authorised_value => { not => undef } });
-my $ccode_avcode = $mss->count ? $mss->next->authorised_value : 'CCODE';
-my $ccodes = GetAuthorisedValues($ccode_avcode);
-my @ccodes;
-foreach my $ccode (@$ccodes) {
-    push @ccodes, {
-        value => $ccode->{authorised_value},
-        label => $ccode->{lib},
+my @itemlosts;
+foreach my $value (@$itemlost_values) {
+    push @itemlosts, {
+        value => $value->{authorised_value},
+        label => $value->{lib},
     };
 }
 
-my @notforloans;
-foreach my $value (@$notforloan_values) {
-    push @notforloans, {
+my @withdrawns;
+foreach my $value (@$withdrawn_values) {
+    push @withdrawns, {
         value => $value->{authorised_value},
         label => $value->{lib},
     };
@@ -307,10 +313,10 @@ foreach my $field (@items_search_fields) {
 
 $template->param(
     branches => \@branches,
-    locations => \@locations,
     itemtypes => \@itemtypes,
     ccodes => \@ccodes,
-    notforloans => \@notforloans,
+    itemlosts => \@itemlosts,
+    withdrawns => \@withdrawns,
     items_search_fields => \@items_search_fields,
     authorised_values_json => to_json($authorised_values),
 );