Bug 19068: Add quantity field to the opac suggestions form
[koha.git] / labels / label-item-search.pl
blob53b7d46755735fae79964645dd12147cb41c4029
1 #!/usr/bin/perl
3 # Copyright 2000-2002 Katipo Communications
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use strict;
21 use warnings;
22 use vars qw($debug $cgi_debug);
24 use CGI qw ( -utf8 );
25 use List::Util qw( max min );
26 use POSIX qw(ceil);
28 use C4::Auth qw(get_template_and_user);
29 use C4::Output qw(output_html_with_http_headers);
30 use C4::Context;
31 use C4::Search qw(SimpleSearch);
32 use C4::Biblio qw(TransformMarcToKoha);
33 use C4::Creators::Lib qw(html_table);
34 use C4::Debug;
36 use Koha::DateUtils;
37 use Koha::Items;
38 use Koha::ItemTypes;
39 use Koha::SearchEngine::Search;
41 BEGIN {
42 $debug = $debug || $cgi_debug;
43 if ($debug) {
44 require Data::Dumper;
45 import Data::Dumper qw(Dumper);
49 my $query = new CGI;
51 my $type = $query->param('type');
52 my $op = $query->param('op') || '';
53 my $batch_id = $query->param('batch_id');
54 my $ccl_query = $query->param('ccl_query');
55 my $startfrom = $query->param('startfrom') || 1;
56 my ($template, $loggedinuser, $cookie) = (undef, undef, undef);
57 my (
58 $total_hits, $total, $error,
59 $marcresults, $idx, $datefrom, $dateto, $ccl_textbox
61 my $resultsperpage = C4::Context->preference('numSearchResults') || '20';
62 my $show_results = 0;
63 my $display_columns = [ {_add => {label => "Add Item", link_field => 1}},
64 {_item_call_number => {label => "Call Number", link_field => 0}},
65 {_date_accessioned => {label => "Accession Date", link_field => 0}},
66 {_barcode => {label => "Barcode", link_field => 0}},
67 {select => {label => "Select", value => "_item_number"}},
70 if ( $op eq "do_search" ) {
71 my $QParser;
72 $QParser = C4::Context->queryparser if (C4::Context->preference('UseQueryParser'));
73 $idx = $query->param('idx');
74 $ccl_textbox = $query->param('ccl_textbox');
75 if ( $ccl_textbox && $idx ) {
76 $ccl_query = "$idx:$ccl_textbox";
79 $datefrom = $query->param('datefrom');
80 $dateto = $query->param('dateto');
82 if ($datefrom) {
83 $datefrom = eval { dt_from_string ( $datefrom ) };
84 if ($datefrom) {
85 $datefrom = output_pref( { dt => $datefrom, dateonly => 1, dateformat => 'iso' } );
86 if ($QParser) {
87 $ccl_query .= ' && ' if $ccl_textbox;
88 $ccl_query .=
89 "acqdate(" . $datefrom . '-)';
90 } else {
91 $ccl_query .= ' and ' if $ccl_textbox;
92 $ccl_query .= "acqdate,st-date-normalized,ge=" . $datefrom;
97 if ($dateto) {
98 $dateto = eval { dt_from_string ( $dateto ) };
99 if ($dateto) {
100 $dateto = output_pref( { dt => $dateto, dateonly => 1, dateformat => 'iso' } );
101 if ($QParser) {
102 $ccl_query .= ' && ' if ( $ccl_textbox || $datefrom );
103 $ccl_query .= "acqdate(-" . $dateto . ')';
104 } else {
105 $ccl_query .= ' and ' if ( $ccl_textbox || $datefrom );
106 $ccl_query .= "acqdate,st-date-normalized,le=" . $dateto;
111 my $offset = $startfrom > 1 ? $startfrom - 1 : 0;
112 my $searcher = Koha::SearchEngine::Search->new({index => 'biblios'});
113 ( $error, $marcresults, $total_hits ) = $searcher->simple_search_compat($ccl_query, $offset, $resultsperpage);
115 if (!defined $error && @{$marcresults} ) {
116 $show_results = @{$marcresults};
118 else {
119 $debug and warn "ERROR label-item-search: no results from simple_search_compat";
121 # leave $show_results undef
125 if ($show_results) {
126 my $hits = $show_results;
127 my @results_set = ();
128 my @items =();
129 # This code needs to be refactored using these subs...
130 #my @items = &GetItemsInfo( $biblio->{biblionumber}, 'intra' );
131 for ( my $i = 0 ; $i < $hits ; $i++ ) {
132 my @row_data= ();
133 #DEBUG Notes: Decode the MARC record from each resulting MARC record...
134 my $marcrecord = C4::Search::new_record_from_zebra( 'biblioserver', $marcresults->[$i] );
135 #DEBUG Notes: Transform it to Koha form...
136 my $biblio = TransformMarcToKoha( $marcrecord, '' );
137 #DEBUG Notes: Stuff the bib into @biblio_data...
138 push (@results_set, $biblio);
139 my $biblionumber = $biblio->{'biblionumber'};
140 #DEBUG Notes: Grab the item numbers associated with this MARC record...
141 my $items = Koha::Items->search({ biblionumber => $biblionumber }, { order_by => { -desc => 'itemnumber' }});
142 #DEBUG Notes: Retrieve the item data for each number...
143 while ( my $item = $items->next ) {
144 #DEBUG Notes: Build an array element 'item' of the correct bib (results) hash which contains item-specific data...
145 if ( $item->biblionumber eq $results_set[$i]->{'biblionumber'} ) {
146 my $item_data;
147 $item_data->{'_item_number'} = $item->itemnumber;
148 $item_data->{'_item_call_number'} = ( $item->itemcallnumber || 'NA' );
149 $item_data->{'_date_accessioned'} = $item->dateaccessioned;
150 $item_data->{'_barcode'} = ( $item->barcode || 'NA' );
151 $item_data->{'_add'} = $item->itemnumber;
152 push @row_data, $item_data;
154 $results_set[$i]->{'item_table'} = html_table($display_columns, \@row_data);
158 ( $template, $loggedinuser, $cookie ) = get_template_and_user(
160 template_name => "labels/result.tt",
161 query => $query,
162 type => "intranet",
163 authnotrequired => 0,
164 flagsrequired => { borrowers => 1 },
165 flagsrequired => { catalogue => 1 },
166 debug => 1,
170 # build page nav stuff.
171 my @numbers;
172 $total = $total_hits;
174 my ( $from, $to, $startfromnext, $startfromprev, $displaynext,
175 $displayprev );
177 if ( $total > $resultsperpage ) {
178 my $num_of_pages = ceil( $total / $resultsperpage + 1 );
179 for ( my $page = 1 ; $page < $num_of_pages ; $page++ ) {
180 my $startfrm = ( ( $page - 1 ) * $resultsperpage ) + 1;
181 push @numbers,
183 number => $page,
184 startfrom => $startfrm
188 $from = $startfrom;
189 $startfromprev = $startfrom - $resultsperpage;
190 $startfromnext = $startfrom + $resultsperpage;
192 $to =
193 $startfrom + $resultsperpage > $total
194 ? $total
195 : $startfrom + $resultsperpage - 1;
197 # multi page display
198 $displaynext = 0;
199 $displayprev = $startfrom > 1 ? $startfrom : 0;
201 $displaynext = 1 if $to < $total_hits;
204 else {
205 $displayprev = 0;
206 $displaynext = 0;
209 $template->param(
210 total => $total_hits,
211 from => $from,
212 to => $to,
213 startfromnext => $startfromnext,
214 startfromprev => $startfromprev,
215 startfrom => $startfrom,
216 displaynext => $displaynext,
217 displayprev => $displayprev,
218 resultsperpage => $resultsperpage,
219 numbers => \@numbers,
222 $template->param(
223 results => ($show_results ? 1 : 0),
224 result_set=> \@results_set,
225 batch_id => $batch_id,
226 type => $type,
227 ccl_query => $ccl_query,
232 # search section
235 else {
236 ( $template, $loggedinuser, $cookie ) = get_template_and_user(
238 template_name => "labels/search.tt",
239 query => $query,
240 type => "intranet",
241 authnotrequired => 0,
242 flagsrequired => { catalogue => 1 },
243 debug => 1,
246 my $itemtypes = Koha::ItemTypes->search;
247 my @itemtypeloop;
248 while ( my $itemtype = $itemtypes->next ) {
249 # FIXME This must be improved:
250 # - pass the iterator to the template
251 # - display the translated_description
252 my %row = (
253 value => $itemtype->itemtype,
254 description => $itemtype->description,
256 push @itemtypeloop, \%row;
258 $template->param(
259 itemtypeloop => \@itemtypeloop,
260 batch_id => $batch_id,
261 type => $type,
266 $template->param( idx => $idx );
268 # Print the page
269 output_html_with_http_headers $query, $cookie, $template->output;