Bug 16330: Add routes to add, update and delete patrons
[koha.git] / labels / label-item-search.pl
1 #!/usr/bin/perl
2 #
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
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.
11 #
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.
16 #
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>.
19
20 use Modern::Perl;
21 use vars qw($debug $cgi_debug);
22
23 use CGI qw ( -utf8 );
24 use List::Util qw( max min );
25 use POSIX qw(ceil);
26
27 use C4::Auth qw(get_template_and_user);
28 use C4::Output qw(output_html_with_http_headers);
29 use C4::Context;
30 use C4::Search qw(SimpleSearch);
31 use C4::Biblio qw(TransformMarcToKoha);
32 use C4::Creators::Lib qw(html_table);
33 use C4::Debug;
34
35 use Koha::DateUtils;
36 use Koha::Items;
37 use Koha::ItemTypes;
38 use Koha::SearchEngine::Search;
39
40 BEGIN {
41     $debug = $debug || $cgi_debug;
42     if ($debug) {
43         require Data::Dumper;
44         import Data::Dumper qw(Dumper);
45     }
46 }
47
48 my $query = new CGI;
49
50 my $type      = $query->param('type');
51 my $op        = $query->param('op') || '';
52 my $batch_id  = $query->param('batch_id');
53 my $ccl_query = $query->param('ccl_query');
54 my $startfrom = $query->param('startfrom') || 1;
55 my ($template, $loggedinuser, $cookie) = (undef, undef, undef);
56 my (
57     $total_hits,  $total,  $error,
58     $marcresults, $idx,     $datefrom, $dateto, $ccl_textbox
59 );
60 my $resultsperpage = C4::Context->preference('numSearchResults') || '20';
61 my $show_results = 0;
62 my $display_columns = [ {_add                   => {label => "Add Item", link_field => 1}},
63                         {_item_call_number      => {label => "Call Number", link_field => 0}},
64                         {_date_accessioned      => {label => "Accession Date", link_field => 0}},
65                         {_barcode               => {label => "Barcode", link_field => 0}},
66                         {select                 => {label => "Select", value => "_item_number"}},
67                       ];
68
69 if ( $op eq "do_search" ) {
70     my $QParser;
71     $QParser = C4::Context->queryparser if (C4::Context->preference('UseQueryParser'));
72     $idx         = $query->param('idx');
73     $ccl_textbox = $query->param('ccl_textbox');
74     if ( $ccl_textbox && $idx ) {
75         $ccl_query = "$idx:$ccl_textbox";
76     }
77
78     $datefrom = $query->param('datefrom');
79     $dateto   = $query->param('dateto');
80
81     if ($datefrom) {
82         $datefrom = eval { dt_from_string ( $datefrom ) };
83         if ($datefrom) {
84             $datefrom = output_pref( { dt => $datefrom, dateonly => 1, dateformat => 'iso' } );
85             if ($QParser) {
86                 $ccl_query .= ' && ' if $ccl_textbox;
87                 $ccl_query .=
88                     "acqdate(" . $datefrom . '-)';
89             } else {
90                 $ccl_query .= ' and ' if $ccl_textbox;
91                 $ccl_query .= "acqdate,st-date-normalized,ge=" . $datefrom;
92             }
93         }
94     }
95
96     if ($dateto) {
97         $dateto = eval { dt_from_string ( $dateto ) };
98         if ($dateto) {
99            $dateto = output_pref( { dt => $dateto, dateonly => 1, dateformat => 'iso' } );
100             if ($QParser) {
101                 $ccl_query .= ' && ' if ( $ccl_textbox || $datefrom );
102                 $ccl_query .= "acqdate(-" . $dateto . ')';
103             } else {
104                 $ccl_query .= ' and ' if ( $ccl_textbox || $datefrom );
105                 $ccl_query .= "acqdate,st-date-normalized,le=" . $dateto;
106             }
107         }
108     }
109
110     my $offset = $startfrom > 1 ? $startfrom - 1 : 0;
111     my $searcher = Koha::SearchEngine::Search->new({index => 'biblios'});
112     ( $error, $marcresults, $total_hits ) = $searcher->simple_search_compat($ccl_query, $offset, $resultsperpage);
113
114     if (!defined $error && @{$marcresults} ) {
115         $show_results = @{$marcresults};
116     }
117     else {
118         $debug and warn "ERROR label-item-search: no results from simple_search_compat";
119
120         # leave $show_results undef
121     }
122 }
123
124 if ($show_results) {
125     my $hits = $show_results;
126     my @results_set = ();
127     my @items =();
128     # This code needs to be refactored using these subs...
129     #my @items = &GetItemsInfo( $biblio->{biblionumber}, 'intra' );
130     for ( my $i = 0 ; $i < $hits ; $i++ ) {
131         my @row_data= ();
132         #DEBUG Notes: Decode the MARC record from each resulting MARC record...
133         my $marcrecord = C4::Search::new_record_from_zebra( 'biblioserver', $marcresults->[$i] );
134         #DEBUG Notes: Transform it to Koha form...
135         my $biblio = TransformMarcToKoha( $marcrecord, '' );
136         #DEBUG Notes: Stuff the bib into @biblio_data...
137         push (@results_set, $biblio);
138         my $biblionumber = $biblio->{'biblionumber'};
139         #DEBUG Notes: Grab the item numbers associated with this MARC record...
140         my $items = Koha::Items->search({ biblionumber => $biblionumber }, { order_by => { -desc => 'itemnumber' }});
141         #DEBUG Notes: Retrieve the item data for each number...
142         while ( my $item = $items->next ) {
143             #DEBUG Notes: Build an array element 'item' of the correct bib (results) hash which contains item-specific data...
144             if ( $item->biblionumber eq $results_set[$i]->{'biblionumber'} ) {
145                 my $item_data;
146                 $item_data->{'_item_number'}      = $item->itemnumber;
147                 $item_data->{'_item_call_number'} = ( $item->itemcallnumber || 'NA' );
148                 $item_data->{'_date_accessioned'} = $item->dateaccessioned;
149                 $item_data->{'_barcode'}          = ( $item->barcode || 'NA' );
150                 $item_data->{'_add'}              = $item->itemnumber;
151                 push @row_data, $item_data;
152             }
153             $results_set[$i]->{'item_table'} = html_table($display_columns, \@row_data);
154         }
155     }
156
157     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
158         {
159             template_name   => "labels/result.tt",
160             query           => $query,
161             type            => "intranet",
162             authnotrequired => 0,
163             flagsrequired   => { borrowers => 'edit_borrowers' },
164             flagsrequired   => { catalogue => 1 },
165             debug           => 1,
166         }
167     );
168
169     # build page nav stuff.
170     my @numbers;
171     $total = $total_hits;
172
173     my ( $from, $to, $startfromnext, $startfromprev, $displaynext,
174         $displayprev );
175
176     if ( $total > $resultsperpage ) {
177         my $num_of_pages = ceil( $total / $resultsperpage + 1 );
178         for ( my $page = 1 ; $page < $num_of_pages ; $page++ ) {
179             my $startfrm = ( ( $page - 1 ) * $resultsperpage ) + 1;
180             push @numbers,
181               {
182                 number    => $page,
183                 startfrom => $startfrm
184               };
185         }
186
187         $from          = $startfrom;
188         $startfromprev = $startfrom - $resultsperpage;
189         $startfromnext = $startfrom + $resultsperpage;
190
191         $to =
192             $startfrom + $resultsperpage > $total
193           ? $total
194           : $startfrom + $resultsperpage - 1;
195
196         # multi page display
197         $displaynext = 0;
198         $displayprev = $startfrom > 1 ? $startfrom : 0;
199
200         $displaynext = 1 if $to < $total_hits;
201
202     }
203     else {
204         $from = 1;
205         $to = $total_hits;
206         $displayprev = 0;
207         $displaynext = 0;
208     }
209
210     $template->param(
211         total          => $total_hits,
212         from           => $from,
213         to             => $to,
214         startfromnext  => $startfromnext,
215         startfromprev  => $startfromprev,
216         startfrom      => $startfrom,
217         displaynext    => $displaynext,
218         displayprev    => $displayprev,
219         resultsperpage => $resultsperpage,
220         numbers        => \@numbers,
221     );
222
223     $template->param(
224         results   => ($show_results ? 1 : 0),
225         result_set=> \@results_set,
226         batch_id  => $batch_id,
227         type      => $type,
228         ccl_query => $ccl_query,
229     );
230 }
231
232 #
233 #   search section
234 #
235
236 else {
237     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
238         {
239             template_name   => "labels/search.tt",
240             query           => $query,
241             type            => "intranet",
242             authnotrequired => 0,
243             flagsrequired   => { catalogue => 1 },
244             debug           => 1,
245         }
246     );
247     my $itemtypes = Koha::ItemTypes->search;
248     my @itemtypeloop;
249     while ( my $itemtype = $itemtypes->next ) {
250         # FIXME This must be improved:
251         # - pass the iterator to the template
252         # - display the translated_description
253         my %row = (
254             value       => $itemtype->itemtype,
255             description => $itemtype->description,
256         );
257         push @itemtypeloop, \%row;
258     }
259     $template->param(
260         itemtypeloop => \@itemtypeloop,
261         batch_id     => $batch_id,
262         type         => $type,
263     );
264
265 }
266
267 $template->param( idx => $idx );
268
269 # Print the page
270 output_html_with_http_headers $query, $cookie, $template->output;