print notice that member returned all books
[koha_fer] / 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 under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use strict;
21 use warnings;
22 use vars qw($debug $cgi_debug);
23
24 use CGI;
25 use List::Util qw( max min );
26 use POSIX qw(ceil);
27
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::Dates;
32 use C4::Search qw(SimpleSearch);
33 use C4::Biblio qw(TransformMarcToKoha);
34 use C4::Items qw(GetItemInfosOf get_itemnumbers_of);
35 use C4::Koha qw(GetItemTypes);    # XXX subfield_is_koha_internal_p
36 use C4::Creators::Lib qw(html_table);
37 use C4::Debug;
38
39 BEGIN {
40     $debug = $debug || $cgi_debug;
41     if ($debug) {
42         require Data::Dumper;
43         import Data::Dumper qw(Dumper);
44     }
45 }
46
47 my $query = new CGI;
48
49 my $type      = $query->param('type');
50 my $op        = $query->param('op') || '';
51 my $batch_id  = $query->param('batch_id');
52 my $ccl_query = $query->param('ccl_query');
53 my $startfrom = $query->param('startfrom') || 1;
54 my ($template, $loggedinuser, $cookie) = (undef, undef, undef);
55 my (
56     $total_hits,  $orderby, $results,  $total,  $error,
57     $marcresults, $idx,     $datefrom, $dateto, $ccl_textbox
58 );
59 my $resultsperpage = C4::Context->preference('numSearchResults') || '20';
60 my $show_results = 0;
61 my $display_columns = [ {_add                   => {label => "Add Item", link_field => 1}},
62                                                 {_item_copynumber       => {label => "Inv. no", link_field => 0}},
63                                                 {_item_stocknumber       => {label => "NEW Inv. no", link_field => 0}},
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"}},
68                       ];
69
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";
77     }
78
79     $datefrom = $query->param('datefrom');
80     $dateto   = $query->param('dateto');
81
82     if ($datefrom) {
83         $datefrom = C4::Dates->new($datefrom);
84         if ($QParser) {
85             $ccl_query .= ' && ' if $ccl_textbox;
86             $ccl_query .=
87                 "acqdate(" . $datefrom->output("iso") . '-)';
88         } else {
89             $ccl_query .= ' and ' if $ccl_textbox;
90             $ccl_query .=
91                 "acqdate,st-date-normalized,ge=" . $datefrom->output("iso");
92         }
93     }
94
95     if ($dateto) {
96         $dateto = C4::Dates->new($dateto);
97         if ($QParser) {
98             $ccl_query .= ' && ' if ( $ccl_textbox || $datefrom );
99             $ccl_query .= "acqdate(-" . $dateto->output("iso") . ')';
100         } else {
101             $ccl_query .= ' and ' if ( $ccl_textbox || $datefrom );
102             $ccl_query .= "acqdate,st-date-normalized,le=" . $dateto->output("iso");
103         }
104     }
105
106     my $offset = $startfrom > 1 ? $startfrom - 1 : 0;
107     ( $error, $marcresults, $total_hits ) =
108       SimpleSearch( $ccl_query, $offset, $resultsperpage );
109
110     if (!defined $error && @{$marcresults} ) {
111         $show_results = @{$marcresults};
112     }
113     else {
114         $debug and warn "ERROR label-item-search: no results from SimpleSearch";
115
116         # leave $show_results undef
117     }
118 }
119
120 if ($show_results) {
121     my $hits = $show_results;
122     my @results_set = ();
123     my @items =();
124     # This code needs to be refactored using these subs...
125     #my @items = &GetItemsInfo( $biblio->{biblionumber}, 'intra' );
126     #my $dat = &GetBiblioData( $biblio->{biblionumber} );
127     for ( my $i = 0 ; $i < $hits ; $i++ ) {
128         my @row_data= ();
129         #DEBUG Notes: Decode the MARC record from each resulting MARC record...
130         my $marcrecord = C4::Search::new_record_from_zebra( 'biblioserver', $marcresults->[$i] );
131         #DEBUG Notes: Transform it to Koha form...
132         my $biblio = TransformMarcToKoha( C4::Context->dbh, $marcrecord, '' );
133         #DEBUG Notes: Stuff the bib into @biblio_data...
134         push (@results_set, $biblio);
135         my $biblionumber = $biblio->{'biblionumber'};
136         #DEBUG Notes: Grab the item numbers associated with this MARC record...
137         my $itemnums = get_itemnumbers_of($biblionumber);
138         #DEBUG Notes: Retrieve the item data for each number...
139         if (my $iii = $itemnums->{$biblionumber}) {
140             my $item_results = GetItemInfosOf(@$iii);
141             foreach my $item ( keys %$item_results ) {
142                 #DEBUG Notes: Build an array element 'item' of the correct bib (results) hash which contains item-specific data...
143                 if ($item_results->{$item}->{'biblionumber'} eq $results_set[$i]->{'biblionumber'}) {
144                     my $item_data;
145                     $item_data->{'_item_number'} = $item_results->{$item}->{'itemnumber'};
146                     $item_data->{'_item_call_number'} = ($item_results->{$item}->{'itemcallnumber'} ? $item_results->{$item}->{'itemcallnumber'} : 'NA');
147                     $item_data->{'_date_accessioned'} = $item_results->{$item}->{'dateaccessioned'};
148                     $item_data->{'_barcode'} = ( $item_results->{$item}->{'barcode'} ? $item_results->{$item}->{'barcode'} : 'NA');
149                     $item_data->{'_add'} = $item_results->{$item}->{'itemnumber'};
150                                         $item_data->{'_item_copynumber'} = ($item_results->{$item}->{'copynumber'} ? $item_results->{$item}->{'copynumber'} : 'NA');
151                                         $item_data->{'_item_stocknumber'} = ($item_results->{$item}->{'stocknumber'} ? $item_results->{$item}->{'stocknumber'} : 'NA');
152                     unshift (@row_data, $item_data);    # item numbers are given to us in descending order by get_itemnumbers_of()...
153                 }
154             }
155             $results_set[$i]->{'item_table'} = html_table($display_columns, \@row_data);
156         }
157         else {
158             # FIXME: Some error trapping code needed
159             warn sprintf('No item numbers retrieved for biblio number: %s', $biblionumber);
160         }
161     }
162
163     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
164         {
165             template_name   => "labels/result.tmpl",
166             query           => $query,
167             type            => "intranet",
168             authnotrequired => 0,
169             flagsrequired   => { borrowers => 1 },
170             flagsrequired   => { catalogue => 1 },
171             debug           => 1,
172         }
173     );
174
175     # build page nav stuff.
176     my ( @field_data, @numbers );
177     $total = $total_hits;
178
179     my ( $from, $to, $startfromnext, $startfromprev, $displaynext,
180         $displayprev );
181
182     if ( $total > $resultsperpage ) {
183         my $num_of_pages = ceil( $total / $resultsperpage + 1 );
184         for ( my $page = 1 ; $page < $num_of_pages ; $page++ ) {
185             my $startfrm = ( ( $page - 1 ) * $resultsperpage ) + 1;
186             push @numbers,
187               {
188                 number    => $page,
189                 startfrom => $startfrm
190               };
191         }
192
193         $from          = $startfrom;
194         $startfromprev = $startfrom - $resultsperpage;
195         $startfromnext = $startfrom + $resultsperpage;
196
197         $to =
198             $startfrom + $resultsperpage > $total
199           ? $total
200           : $startfrom + $resultsperpage - 1;
201
202         # multi page display
203         $displaynext = 0;
204         $displayprev = $startfrom > 1 ? $startfrom : 0;
205
206         $displaynext = 1 if $to < $total_hits;
207
208     }
209     else {
210         $displayprev = 0;
211         $displaynext = 0;
212     }
213
214     $template->param(
215         total          => $total_hits,
216         from           => $from,
217         to             => $to,
218         startfromnext  => $startfromnext,
219         startfromprev  => $startfromprev,
220         startfrom      => $startfrom,
221         displaynext    => $displaynext,
222         displayprev    => $displayprev,
223         resultsperpage => $resultsperpage,
224         numbers        => \@numbers,
225     );
226
227     $template->param(
228         results   => ($show_results ? 1 : 0),
229         result_set=> \@results_set,
230         batch_id  => $batch_id,
231         type      => $type,
232         idx       => $idx,
233         ccl_query => $ccl_query,
234     );
235 }
236
237 #
238 #   search section
239 #
240
241 else {
242     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
243         {
244             template_name   => "labels/search.tmpl",
245             query           => $query,
246             type            => "intranet",
247             authnotrequired => 0,
248             flagsrequired   => { catalogue => 1 },
249             debug           => 1,
250         }
251     );
252     my $itemtypes = GetItemTypes;
253     my @itemtypeloop;
254     foreach my $thisitemtype ( keys %$itemtypes ) {
255         my %row = (
256             value       => $thisitemtype,
257             description => $itemtypes->{$thisitemtype}->{'description'},
258         );
259         push @itemtypeloop, \%row;
260     }
261     $template->param(
262         itemtypeloop => \@itemtypeloop,
263         batch_id     => $batch_id,
264         type         => $type,
265     );
266
267 }
268
269 # Print the page
270 output_html_with_http_headers $query, $cookie, $template->output;