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