Bugfix for 1725 request.pl erroring with a biblio that has no items
[koha.git] / reserve / request.pl
1 #!/usr/bin/perl
2
3
4 #writen 2/1/00 by chris@katipo.oc.nz
5 # Copyright 2000-2002 Katipo Communications
6 #
7 # This file is part of Koha.
8 #
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
12 # version.
13 #
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License along with
19 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
20 # Suite 330, Boston, MA  02111-1307 USA
21
22 =head1 request.pl
23
24 script to place reserves/requests
25
26 =cut
27
28 use strict;
29 use C4::Branch; # GetBranches get_branchinfos_of
30 use CGI;
31 use List::MoreUtils qw/uniq/;
32 use Date::Calc qw/Today Date_to_Days/;
33 use C4::Output;
34 use C4::Auth;
35 use C4::Reserves;
36 use C4::Biblio;
37 use C4::Koha;
38 use C4::Circulation;
39 use C4::Dates qw/format_date/;
40 use C4::Members;
41
42 my $dbh = C4::Context->dbh;
43 my $sth;
44 my $input = new CGI;
45 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
46     {
47         template_name   => "reserve/request.tmpl",
48         query           => $input,
49         type            => "intranet",
50         authnotrequired => 0,
51         flagsrequired   => { reserveforothers => 1 },
52     }
53 );
54
55 # get Branches and Itemtypes
56 my $branches = GetBranches();
57 my $itemtypes = GetItemTypes();
58
59 # get biblio information....
60 my $biblionumber = $input->param('biblionumber');
61 my $dat          = GetBiblioData($biblionumber);
62
63 # Select borrowers infos
64 my $findborrower = $input->param('findborrower');
65 $findborrower =~ s|,| |g;
66 my $cardnumber = $input->param('cardnumber');
67 my $borrowerslist;
68 my $messageborrower;
69
70 my $date = sprintf( '%04d-%02d-%02d', Today() );
71
72 if ($findborrower) {
73     my ( $count, $borrowers ) =
74       SearchMember($findborrower, 'cardnumber', 'web' );
75
76     my @borrowers = @$borrowers;
77
78     if ( $#borrowers == -1 ) {
79         $input->param( 'findborrower', '' );
80         $messageborrower = "'$findborrower'";
81     }
82     elsif ( $#borrowers == 0 ) {
83         $input->param( 'cardnumber', $borrowers[0]->{'cardnumber'} );
84         $cardnumber = $borrowers[0]->{'cardnumber'};
85     }
86     else {
87         $borrowerslist = \@borrowers;
88     }
89 }
90
91 if ($cardnumber) {
92     my $borrowerinfo = GetMemberDetails( 0, $cardnumber );
93     my $expiry;
94     my $diffbranch;
95     my @getreservloop;
96     my $count_reserv = 0;
97     my $maxreserves;
98
99 #   we check the reserves of the borrower, and if he can reserv a document
100 # FIXME At this time we have a simple count of reservs, but, later, we could improve the infos "title" ...
101
102     my $number_reserves =
103       GetReserveCount( $borrowerinfo->{'borrowernumber'} );
104
105     if ( $number_reserves > C4::Context->preference('maxreserves') ) {
106         $maxreserves = 1;
107     }
108
109     # we check the date expiricy of the borrower (only if there is an expiry date, otherwise, set to 1 (warn)
110     if ($borrowerinfo->{'dateexpiry'} ne '0000-00-00') {
111         my $warning = (Date_to_Days(split /-/,$date) > Date_to_Days( split /-/,$borrowerinfo->{'dateexpiry'}));
112         if ( $warning > 0 ) {
113             $expiry = 1;
114         }
115     } else {
116         $expiry = 1;
117     }
118      
119
120     # check if the borrower make the reserv in a different branch
121     if ( $borrowerinfo->{'branchcode'} ne C4::Context->userenv->{'branch'} ) {
122         $diffbranch = 1;
123     }
124
125     $template->param(
126                 borrowernumber => $borrowerinfo->{'borrowernumber'},
127                 borrowersurname   => $borrowerinfo->{'surname'},
128                 borrowerfirstname => $borrowerinfo->{'firstname'},
129                 borrowerstreetaddress => $borrowerinfo->{'address'},
130                 borrowercity => $borrowerinfo->{'city'},
131                 borrowerphone => $borrowerinfo->{'phone'},
132                 borrowermobile => $borrowerinfo->{'mobile'},
133                 borrowerfax => $borrowerinfo->{'fax'},
134                 borrowerphonepro => $borrowerinfo->{'phonepro'},
135                 borroweremail => $borrowerinfo->{'email'},
136                 borroweremailpro => $borrowerinfo->{'emailpro'},
137                 borrowercategory => $borrowerinfo->{'category'},
138                 borrowerreservs   => $count_reserv,
139                 maxreserves       => $maxreserves,
140                 expiry            => $expiry,
141                 diffbranch        => $diffbranch
142     );
143 }
144
145 $template->param( messageborrower => $messageborrower );
146
147 my $CGIselectborrower;
148 if ($borrowerslist) {
149     my @values;
150     my %labels;
151
152     foreach my $borrower (
153         sort {
154                 $a->{surname}
155               . $a->{firstname} cmp $b->{surname}
156               . $b->{firstname}
157         } @{$borrowerslist}
158       )
159     {
160         push @values, $borrower->{cardnumber};
161
162         $labels{ $borrower->{cardnumber} } = sprintf(
163             '%s, %s ... (%s - %s) ... %s',
164             $borrower->{surname},    $borrower->{firstname},
165             $borrower->{cardnumber}, $borrower->{categorycode},
166             $borrower->{streetaddress},
167         );
168     }
169
170     $CGIselectborrower = CGI::scrolling_list(
171         -name     => 'cardnumber',
172         -values   => \@values,
173         -labels   => \%labels,
174         -size     => 7,
175         -multiple => 0,
176     );
177 }
178
179 # get existing reserves .....
180 my ( $count, $reserves ) = GetReservesFromBiblionumber($biblionumber);
181 my $totalcount = $count;
182 my $alreadyreserved;
183
184 # FIXME launch another time GetMemberDetails perhaps until
185 my $borrowerinfo = GetMemberDetails( 0, $cardnumber );
186
187 foreach my $res (@$reserves) {
188     if ( ( $res->{found} eq 'W' ) ) {
189         $count--;
190     }
191
192     if ( $borrowerinfo->{borrowernumber} eq $res->{borrowernumber} ) {
193         $alreadyreserved = 1;
194     }
195 }
196
197 $template->param( alreadyreserved => $alreadyreserved );
198
199 # FIXME think @optionloop, is maybe obsolete, or  must be switchable by a systeme preference fixed rank or not
200 # make priorities options
201
202 my @optionloop;
203 for ( 1 .. $count + 1 ) {
204     push(
205         @optionloop,
206         {
207             num      => $_,
208             selected => ( $_ == $count + 1 ),
209         }
210     );
211 }
212 # adding a fixed value for priority options
213 my $fixedRank = $count+1;
214
215 my @branchcodes;
216 my %itemnumbers_of_biblioitem;
217 my @itemnumbers;
218
219 if (my $items = get_itemnumbers_of($biblionumber)->{$biblionumber}){
220         @itemnumbers  = @$items;
221 }
222 else {
223         $template->param('noitems' => 1);
224 }
225         
226 my $iteminfos_of = GetItemInfosOf(@itemnumbers);
227
228 foreach my $itemnumber (@itemnumbers) {
229     my $biblioitemnumber = $iteminfos_of->{$itemnumber}->{biblioitemnumber};
230     push( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} }, $itemnumber );
231 }
232
233 my @biblioitemnumbers = keys %itemnumbers_of_biblioitem;
234
235 my $notforloan_label_of = get_notforloan_label_of();
236 my $biblioiteminfos_of  = GetBiblioItemInfosOf(@biblioitemnumbers);
237
238 my @bibitemloop;
239
240 foreach my $biblioitemnumber (@biblioitemnumbers) {
241     my $biblioitem = $biblioiteminfos_of->{$biblioitemnumber};
242
243     $biblioitem->{description} =
244       $itemtypes->{ $biblioitem->{itemtype} }{description};
245
246     foreach
247       my $itemnumber ( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} } )
248     {
249         my $item = $iteminfos_of->{$itemnumber};
250     $item->{itypename} = $itemtypes->{ $item->{itype} }{description};
251     $item->{imageurl} = getitemtypeimagesrc() . "/".$itemtypes->{ $item->{itype} }{imageurl};
252         $item->{homebranchname} =
253           $branches->{ $item->{homebranch} }{branchname};
254
255         # if the holdingbranch is different than the homebranch, we show the
256         # holdingbranch of the document too
257         if ( $item->{homebranch} ne $item->{holdingbranch} ) {
258             $item->{holdingbranchname} =
259               $branches->{ $item->{holdingbranch} }{branchname};
260         }
261         
262 #   add information
263     $item->{itemcallnumber} = $item->{itemcallnumber};
264     
265         # if the item is currently on loan, we display its return date and
266         # change the background color
267         my $issues= GetItemIssue($itemnumber);
268         if ( $issues->{'date_due'} ) {
269             $item->{date_due} = format_date($issues->{'date_due'});
270             $item->{backgroundcolor} = 'onloan';
271         }
272
273         # checking reserve
274         my ($reservedate,$reservedfor,$expectedAt) = GetReservesFromItemnumber($itemnumber);
275         my $ItemBorrowerReserveInfo = GetMemberDetails( $reservedfor, 0);
276
277         if ( defined $reservedate ) {
278             $item->{backgroundcolor} = 'reserved';
279             $item->{reservedate}     = format_date($reservedate);
280             $item->{ReservedForBorrowernumber}     = $reservedfor;
281             $item->{ReservedForSurname}     = $ItemBorrowerReserveInfo->{'surname'};
282             $item->{ReservedForFirstname}     = $ItemBorrowerReserveInfo->{'firstname'};
283             $item->{ExpectedAtLibrary}     = $branches->{$expectedAt}{branchname};
284             
285         }
286
287         # Management of the notforloan document
288         if ( $item->{notforloan} ) {
289             $item->{backgroundcolor} = 'other';
290             $item->{notforloanvalue} =
291               $notforloan_label_of->{ $item->{notforloan} };
292         }
293
294         # Management of lost or long overdue items
295         if ( $item->{itemlost} ) {
296
297             # FIXME localized strings should never be in Perl code
298             $item->{message} =
299                 $item->{itemlost} == 1 ? "(lost)"
300               : $item->{itemlost} == 2 ? "(long overdue)"
301               : "";
302             $item->{backgroundcolor} = 'other';
303         }
304
305         # Check the transit status
306         my ( $transfertwhen, $transfertfrom, $transfertto ) =
307           GetTransfers($itemnumber);
308
309         if ( $transfertwhen ne '' ) {
310             $item->{transfertwhen} = format_date($transfertwhen);
311             $item->{transfertfrom} =
312               $branches->{$transfertfrom}{branchname};
313             $item->{transfertto} = $branches->{$transfertto}{branchname};
314         $item->{nocancel} = 1;
315         }
316
317         # If there is no loan, return and transfer, we show a checkbox.
318         $item->{notforloan} = $item->{notforloan} || 0;
319     
320     # if independent branches is on we need to check if the person can reserve
321     # for branches they arent logged in to
322     if ( C4::Context->preference("IndependantBranches") ) { 
323         if (! C4::Context->preference("canreservefromotherbranches")){
324         # cant reserve items so need to check if item homebranch and userenv branch match if not we cant reserve
325         my $userenv = C4::Context->userenv; 
326         if ( ($userenv) && ( $userenv->{flags} != 1 ) ) {
327             $item->{cantreserve} = 1 if ( $item->{homebranch} ne $userenv->{branch} );
328         } 
329         }
330     }
331
332     # FIXME: every library will define this differently
333         # An item is available only if:
334         if (
335             not defined $reservedate    # not reserved yet
336             and $issues->{'date_due'} eq ''         # not currently on loan
337             and not $item->{itemlost}   # not lost
338             and not $item->{notforloan} # not forbidden to loan
339         and not $item->{cantreserve}
340             and $transfertwhen eq ''    # not currently on transfert
341           )
342         {
343             $item->{available} = 1;
344         }
345
346     # FIXME: move this to a pm
347     my $sth2 = $dbh->prepare("SELECT * FROM reserves WHERE borrowernumber=? AND itemnumber=? AND found='W' AND cancellationdate IS NULL");
348     $sth2->execute($item->{ReservedForBorrowernumber},$item->{itemnumber});
349     while (my $wait_hashref = $sth2->fetchrow_hashref) {
350         $item->{waitingdate} = format_date($wait_hashref->{waitingdate});
351     }
352         push @{ $biblioitem->{itemloop} }, $item;
353     }
354
355     push @bibitemloop, $biblioitem;
356 }
357
358 # existingreserves building
359 my @reserveloop;
360 ( $count, $reserves ) = GetReservesFromBiblionumber($biblionumber);
361 foreach my $res ( sort { $a->{found} cmp $b->{found} } @$reserves ) {
362     my %reserve;
363     my @optionloop;
364     for ( my $i = 1 ; $i <= $totalcount ; $i++ ) {
365         push(
366             @optionloop,
367             {
368                 num      => $i,
369                 selected => ( $i == $res->{priority} ),
370             }
371         );
372     }
373     my @branchloop;
374     foreach my $br ( keys %$branches ) {
375         my %abranch;
376         $abranch{'selected'}   = ( $br eq $res->{'branchcode'} );
377         $abranch{'branch'}     = $br;
378         $abranch{'branchname'} = $branches->{$br}->{'branchname'};
379         push( @branchloop, \%abranch );
380     }
381
382     if ( ( $res->{'found'} eq 'W' ) ) {
383         my $item = $res->{'itemnumber'};
384         $item = GetBiblioFromItemNumber($item,undef);
385         $reserve{'wait'}= 1; 
386         $reserve{'holdingbranch'}=$item->{'holdingbranch'};
387         $reserve{'biblionumber'}=$item->{'biblionumber'};
388         $reserve{'barcodenumber'}   = $item->{'barcode'};
389         $reserve{'wbrcode'} = $res->{'branchcode'};
390         $reserve{'itemnumber'}  = $res->{'itemnumber'};
391         $reserve{'wbrname'} = $branches->{$res->{'branchcode'}}->{'branchname'};
392         if($reserve{'holdingbranch'} eq $reserve{'wbrcode'}){
393             $reserve{'atdestination'} = 1;
394         }
395         # set found to 1 if reserve is waiting for patron pickup
396         $reserve{'found'} = 1 if $res->{'found'} eq 'W';
397     }
398     
399 #     get borrowers reserve info
400 my $reserveborrowerinfo = GetMemberDetails( $res->{'borrowernumber'}, 0);
401
402     $reserve{'date'}           = format_date( $res->{'reservedate'} );
403     $reserve{'borrowernumber'} = $res->{'borrowernumber'};
404     $reserve{'biblionumber'}   = $res->{'biblionumber'};
405     $reserve{'borrowernumber'} = $res->{'borrowernumber'};
406     $reserve{'firstname'}      = $reserveborrowerinfo->{'firstname'};
407     $reserve{'surname'}        = $reserveborrowerinfo->{'surname'};
408     $reserve{'notes'}          = $res->{'reservenotes'};
409     $reserve{'wait'}           =
410       ( ( $res->{'found'} eq 'W' ) or ( $res->{'priority'} eq '0' ) );
411     $reserve{'constrainttypea'} = ( $res->{'constrainttype'} eq 'a' );
412     $reserve{'constrainttypeo'} = ( $res->{'constrainttype'} eq 'o' );
413     $reserve{'voldesc'}         = $res->{'volumeddesc'};
414     $reserve{'ccode'}           = $res->{'ccode'};
415     $reserve{'barcode'}         = $res->{'barcode'};
416     $reserve{'priority'}    = $res->{'priority'};
417     $reserve{'branchloop'} = \@branchloop;
418     $reserve{'optionloop'} = \@optionloop;
419
420     push( @reserveloop, \%reserve );
421 }
422
423 my $default = C4::Context->userenv->{branch};
424 my @values;
425 my %label_of;
426
427 foreach my $branchcode ( keys %{$branches} ) {
428     push @values, $branchcode;
429     $label_of{$branchcode} = $branches->{$branchcode}->{branchname};
430 }
431 my $CGIbranch = CGI::scrolling_list(
432     -name     => 'pickup',
433     -id          => 'pickup',
434     -values   => \@values,
435     -default  => $default,
436     -labels   => \%label_of,
437     -size     => 1,
438     -multiple => 0,
439 );
440
441 # get the time for the form name...
442 my $time = time();
443
444 $template->param(
445     CGIbranch   => $CGIbranch,
446     reserveloop => \@reserveloop,
447     time        => $time,
448     fixedRank   => $fixedRank,
449 );
450
451 # display infos
452 $template->param(
453     optionloop        => \@optionloop,
454     bibitemloop       => \@bibitemloop,
455     date              => $date,
456     biblionumber      => $biblionumber,
457     findborrower      => $findborrower,
458     cardnumber        => $cardnumber,
459     CGIselectborrower => $CGIselectborrower,
460     title             => $dat->{title},
461     author            => $dat->{author}
462 );
463
464 # printout the page
465 output_html_with_http_headers $input, $cookie, $template->output;