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