4 #writen 2/1/00 by chris@katipo.oc.nz
5 # Copyright 2000-2002 Katipo Communications
7 # This file is part of Koha.
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
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.
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
24 script to place reserves/requests
30 use C4::Branch; # GetBranches get_branchinfos_of
32 use List::MoreUtils qw/uniq/;
33 use Date::Calc qw/Date_to_Days/;
41 use C4::Dates qw/format_date/;
44 my $dbh = C4::Context->dbh;
47 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
49 template_name => "reserve/request.tmpl",
53 flagsrequired => { reserveforothers => 1 },
57 # get Branches and Itemtypes
58 my $branches = GetBranches();
59 my $itemtypes = GetItemTypes();
61 # get biblio information....
62 my $biblionumber = $input->param('biblionumber');
63 my $dat = GetBiblioData($biblionumber);
65 # Select borrowers infos
66 my $findborrower = $input->param('findborrower');
67 $findborrower =~ s|,| |g;
68 my $cardnumber = $input->param('cardnumber');
74 my $date = C4::Dates->today('iso');
77 my ( $count, $borrowers ) =
78 SearchMember($findborrower, 'cardnumber', 'web' );
80 my @borrowers = @$borrowers;
82 if ( $#borrowers == -1 ) {
83 $input->param( 'findborrower', '' );
84 $messageborrower = "'$findborrower'";
86 elsif ( $#borrowers == 0 ) {
87 $input->param( 'cardnumber', $borrowers[0]->{'cardnumber'} );
88 $cardnumber = $borrowers[0]->{'cardnumber'};
91 $borrowerslist = \@borrowers;
96 my $borrowerinfo = GetMemberDetails( 0, $cardnumber );
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" ...
105 my $number_reserves =
106 GetReserveCount( $borrowerinfo->{'borrowernumber'} );
108 if ( $number_reserves > C4::Context->preference('maxreserves') ) {
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;
122 # check if the borrower make the reserv in a different branch
123 if ( $borrowerinfo->{'branchcode'} ne C4::Context->userenv->{'branch'} ) {
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,
144 diffbranch => $diffbranch,
145 messages => $messages,
146 warnings => $warnings
150 $template->param( messageborrower => $messageborrower );
152 my $CGIselectborrower;
153 if ($borrowerslist) {
157 foreach my $borrower (
160 . $a->{firstname} cmp $b->{surname}
165 push @values, $borrower->{cardnumber};
167 $labels{ $borrower->{cardnumber} } = sprintf(
168 '%s, %s ... (%s - %s) ... %s',
169 $borrower->{surname}, $borrower->{firstname},
170 $borrower->{cardnumber}, $borrower->{categorycode},
171 $borrower->{address},
175 $CGIselectborrower = CGI::scrolling_list(
176 -name => 'cardnumber',
184 # get existing reserves .....
185 my ( $count, $reserves ) = GetReservesFromBiblionumber($biblionumber);
186 my $totalcount = $count;
189 # FIXME launch another time GetMemberDetails perhaps until
190 my $borrowerinfo = GetMemberDetails( 0, $cardnumber );
192 foreach my $res (@$reserves) {
193 if ( ( $res->{found} eq 'W' ) ) {
197 if ( $borrowerinfo->{borrowernumber} eq $res->{borrowernumber} ) {
199 $alreadyreserved = 1;
203 $template->param( alreadyreserved => $alreadyreserved,
204 messages => $messages,
205 warnings => $warnings );
207 # FIXME think @optionloop, is maybe obsolete, or must be switchable by a systeme preference fixed rank or not
208 # make priorities options
211 for ( 1 .. $count + 1 ) {
216 selected => ( $_ == $count + 1 ),
220 # adding a fixed value for priority options
221 my $fixedRank = $count+1;
224 my %itemnumbers_of_biblioitem;
227 if (my $items = get_itemnumbers_of($biblionumber)->{$biblionumber}){
228 @itemnumbers = @$items;
231 $template->param('noitems' => 1);
234 my $iteminfos_of = GetItemInfosOf(@itemnumbers);
236 foreach my $itemnumber (@itemnumbers) {
237 my $biblioitemnumber = $iteminfos_of->{$itemnumber}->{biblioitemnumber};
238 push( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} }, $itemnumber );
241 my @biblioitemnumbers = keys %itemnumbers_of_biblioitem;
243 my $notforloan_label_of = get_notforloan_label_of();
244 my $biblioiteminfos_of = GetBiblioItemInfosOf(@biblioitemnumbers);
248 foreach my $biblioitemnumber (@biblioitemnumbers) {
249 my $biblioitem = $biblioiteminfos_of->{$biblioitemnumber};
253 $biblioitem->{description} =
254 $itemtypes->{ $biblioitem->{itemtype} }{description};
256 foreach my $itemnumber ( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} } ) {
257 my $item = $iteminfos_of->{$itemnumber};
259 unless (C4::Context->preference('item-level_itypes')) {
260 $item->{itype} = $biblioitem->{itemtype};
263 $item->{itypename} = $itemtypes->{ $item->{itype} }{description};
264 $item->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtypes->{ $item->{itype} }{imageurl} );
265 $item->{homebranchname} = $branches->{ $item->{homebranch} }{branchname};
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};
275 $item->{itemcallnumber} = $item->{itemcallnumber};
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';
286 my ($reservedate,$reservedfor,$expectedAt) = GetReservesFromItemnumber($itemnumber);
287 my $ItemBorrowerReserveInfo = GetMemberDetails( $reservedfor, 0);
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};
299 # Management of the notforloan document
300 if ( $item->{notforloan} ) {
301 $item->{backgroundcolor} = 'other';
302 $item->{notforloanvalue} =
303 $notforloan_label_of->{ $item->{notforloan} };
306 # Management of lost or long overdue items
307 if ( $item->{itemlost} ) {
309 # FIXME localized strings should never be in Perl code
311 $item->{itemlost} == 1 ? "(lost)"
312 : $item->{itemlost} == 2 ? "(long overdue)"
314 $item->{backgroundcolor} = 'other';
317 # Check the transit status
318 my ( $transfertwhen, $transfertfrom, $transfertto ) =
319 GetTransfers($itemnumber);
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;
329 # If there is no loan, return and transfer, we show a checkbox.
330 $item->{notforloan} = $item->{notforloan} || 0;
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} );
344 my $branchitemrule = GetBranchItemRule( $item->{'homebranch'}, $item->{'itype'} );
345 my $policy_holdallowed = 1;
347 $item->{'holdallowed'} = $branchitemrule->{'holdallowed'};
349 if ( $branchitemrule->{'holdallowed'} == 0 ||
350 ( $branchitemrule->{'holdallowed'} == 1 && $borrowerinfo->{'branchcode'} ne $item->{'homebranch'} ) ) {
351 $policy_holdallowed = 0;
354 if (IsAvailableForItemLevelRequest($itemnumber) and not $item->{cantreserve}) {
355 if ( not $policy_holdallowed and C4::Context->preference( 'AllowHoldPolicyOverride' ) ) {
356 $item->{override} = 1;
358 } elsif ( $policy_holdallowed ) {
359 $item->{available} = 1;
363 # If none of the conditions hold true, then neither override nor available is set and the item cannot be checked
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});
371 push @{ $biblioitem->{itemloop} }, $item;
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 );
381 push @bibitemloop, $biblioitem;
384 # existingreserves building
386 ( $count, $reserves ) = GetReservesFromBiblionumber($biblionumber);
387 foreach my $res ( sort { $a->{found} cmp $b->{found} } @$reserves ) {
390 for ( my $i = 1 ; $i <= $totalcount ; $i++ ) {
395 selected => ( $i == $res->{priority} ),
400 foreach my $br ( keys %$branches ) {
402 $abranch{'selected'} = ( $br eq $res->{'branchcode'} );
403 $abranch{'branch'} = $br;
404 $abranch{'branchname'} = $branches->{$br}->{'branchname'};
405 push( @branchloop, \%abranch );
408 if ( ( $res->{'found'} eq 'W' ) ) {
409 my $item = $res->{'itemnumber'};
410 $item = GetBiblioFromItemNumber($item,undef);
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;
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;
432 # get borrowers reserve info
433 my $reserveborrowerinfo = GetMemberDetails( $res->{'borrowernumber'}, 0);
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'};
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;
453 push( @reserveloop, \%reserve );
456 my $default = C4::Context->userenv->{branch};
460 foreach my $branchcode (sort keys %{$branches} ) {
461 push @values, $branchcode;
462 $label_of{$branchcode} = $branches->{$branchcode}->{branchname};
464 my $CGIbranch = CGI::scrolling_list(
468 -default => $default,
469 -labels => \%label_of,
474 # get the time for the form name...
478 CGIbranch => $CGIbranch,
479 reserveloop => \@reserveloop,
481 fixedRank => $fixedRank,
486 optionloop => \@optionloop,
487 bibitemloop => \@bibitemloop,
489 biblionumber => $biblionumber,
490 findborrower => $findborrower,
491 cardnumber => $cardnumber,
492 CGIselectborrower => $CGIselectborrower,
493 title => $dat->{title},
494 author => $dat->{author},
496 borrower_branchname => $branches->{$borrowerinfo->{'branchcode'}}->{'branchname'},
497 borrower_branchcode => $borrowerinfo->{'branchcode'},
501 output_html_with_http_headers $input, $cookie, $template->output;