4 #writen 2/1/00 by chris@katipo.oc.nz
5 # Copyright 2000-2002 Katipo Communications
6 # Parts Copyright 2011 Catalyst IT
8 # This file is part of Koha.
10 # Koha is free software; you can redistribute it and/or modify it under the
11 # terms of the GNU General Public License as published by the Free Software
12 # Foundation; either version 2 of the License, or (at your option) any later
15 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
16 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License along
20 # with Koha; if not, write to the Free Software Foundation, Inc.,
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 script to place reserves/requests
31 use C4::Branch; # GetBranches get_branchinfos_of
33 use List::MoreUtils qw/uniq/;
34 use Date::Calc qw/Date_to_Days/;
42 use C4::Dates qw/format_date/;
44 use C4::Search; # enabled_staff_search_views
46 my $dbh = C4::Context->dbh;
49 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
51 template_name => "reserve/request.tmpl",
55 flagsrequired => { reserveforothers => 'place_holds' },
59 my $multihold = $input->param('multi_hold');
60 $template->param(multi_hold => $multihold);
61 my $showallitems = $input->param('showallitems');
63 # get Branches and Itemtypes
64 my $branches = GetBranches();
65 my $itemtypes = GetItemTypes();
67 my $default = C4::Context->userenv->{branch};
71 foreach my $branchcode (sort keys %{$branches} ) {
72 push @values, $branchcode;
73 $label_of{$branchcode} = $branches->{$branchcode}->{branchname};
75 my $CGIbranch = CGI::scrolling_list(
80 -labels => \%label_of,
85 # Select borrowers infos
86 my $findborrower = $input->param('findborrower');
87 $findborrower = '' unless defined $findborrower;
88 $findborrower =~ s|,| |g;
89 my $borrowernumber_hold = $input->param('borrowernumber') || '';
95 my $date = C4::Dates->today('iso');
96 my $action = $input->param('action');
99 if ( $action eq 'move' ) {
100 my $where = $input->param('where');
101 my $borrowernumber = $input->param('borrowernumber');
102 my $biblionumber = $input->param('biblionumber');
103 AlterPriority( $where, $borrowernumber, $biblionumber );
104 } elsif ( $action eq 'cancel' ) {
105 my $borrowernumber = $input->param('borrowernumber');
106 my $biblionumber = $input->param('biblionumber');
107 CancelReserve( $biblionumber, '', $borrowernumber );
108 } elsif ( $action eq 'setLowestPriority' ) {
109 my $borrowernumber = $input->param('borrowernumber');
110 my $biblionumber = $input->param('biblionumber');
111 ToggleLowestPriority( $borrowernumber, $biblionumber );
115 my ( $count, $borrowers ) =
116 SearchMember($findborrower, 'cardnumber', 'web' );
118 my @borrowers = @$borrowers;
121 $messageborrower = "'$findborrower'";
123 elsif ( @borrowers == 1 ) {
124 $borrowernumber_hold = $borrowers[0]->{'borrowernumber'};
127 $borrowerslist = \@borrowers;
131 # If we have the borrowernumber because we've performed an action, then we
132 # don't want to try to place another reserve.
133 if ($borrowernumber_hold && !$action) {
134 my $borrowerinfo = GetMemberDetails( $borrowernumber_hold );
137 my $count_reserv = 0;
140 # we check the reserves of the borrower, and if he can reserv a document
141 # FIXME At this time we have a simple count of reservs, but, later, we could improve the infos "title" ...
143 my $number_reserves =
144 GetReserveCount( $borrowerinfo->{'borrowernumber'} );
146 if ( C4::Context->preference('maxreserves') && ($number_reserves >= C4::Context->preference('maxreserves')) ) {
151 # we check the date expiry of the borrower (only if there is an expiry date, otherwise, set to 1 (warn)
152 my $expiry_date = $borrowerinfo->{dateexpiry};
153 my $expiry = 0; # flag set if patron account has expired
154 if ($expiry_date and $expiry_date ne '0000-00-00' and
155 Date_to_Days(split /-/,$date) > Date_to_Days(split /-/,$expiry_date)) {
156 $messages = $expiry = 1;
159 # check if the borrower make the reserv in a different branch
160 if ( $borrowerinfo->{'branchcode'} ne C4::Context->userenv->{'branch'} ) {
166 borrowernumber => $borrowerinfo->{'borrowernumber'},
167 borrowersurname => $borrowerinfo->{'surname'},
168 borrowerfirstname => $borrowerinfo->{'firstname'},
169 borrowerstreetaddress => $borrowerinfo->{'address'},
170 borrowercity => $borrowerinfo->{'city'},
171 borrowerphone => $borrowerinfo->{'phone'},
172 borrowermobile => $borrowerinfo->{'mobile'},
173 borrowerfax => $borrowerinfo->{'fax'},
174 borrowerphonepro => $borrowerinfo->{'phonepro'},
175 borroweremail => $borrowerinfo->{'email'},
176 borroweremailpro => $borrowerinfo->{'emailpro'},
177 borrowercategory => $borrowerinfo->{'category'},
178 borrowerreservs => $count_reserv,
179 cardnumber => $borrowerinfo->{'cardnumber'},
180 maxreserves => $maxreserves,
182 diffbranch => $diffbranch,
183 messages => $messages,
184 warnings => $warnings
188 $template->param( messageborrower => $messageborrower );
190 my $CGIselectborrower;
191 if ($borrowerslist) {
195 foreach my $borrower (
198 . $a->{firstname} cmp $b->{surname}
203 push @values, $borrower->{borrowernumber};
205 $labels{ $borrower->{borrowernumber} } = sprintf(
206 '%s, %s ... (%s - %s) ... %s',
207 $borrower->{surname} ||'', $borrower->{firstname} || '',
208 $borrower->{cardnumber} || '', $borrower->{categorycode} || '',
209 $borrower->{address} || '',
213 $CGIselectborrower = CGI::scrolling_list(
214 -name => 'borrowernumber',
222 # FIXME launch another time GetMemberDetails perhaps until
223 my $borrowerinfo = GetMemberDetails( $borrowernumber_hold );
225 my @biblionumbers = ();
226 my $biblionumbers = $input->param('biblionumbers');
228 @biblionumbers = split '/', $biblionumbers;
230 push @biblionumbers, $input->param('biblionumber');
233 my $itemdata_enumchron = 0;
235 foreach my $biblionumber (@biblionumbers) {
237 my %biblioloopiter = ();
240 my $dat = GetBiblioData($biblionumber);
242 unless ( CanBookBeReserved($borrowerinfo->{borrowernumber}, $biblionumber) ) {
246 # get existing reserves .....
247 my ( $count, $reserves ) = GetReservesFromBiblionumber($biblionumber,1);
248 my $totalcount = $count;
251 foreach my $res (@$reserves) {
252 if ( defined $res->{found} && $res->{found} eq 'W' ) {
256 if ( defined $borrowerinfo && ($borrowerinfo->{borrowernumber} eq $res->{borrowernumber}) ) {
258 $alreadyreserved = 1;
259 $biblioloopiter{warn} = 1;
260 $biblioloopiter{alreadyres} = 1;
264 $template->param( alreadyreserved => $alreadyreserved,
265 messages => $messages,
266 warnings => $warnings,
267 maxreserves=>$maxreserves
271 # FIXME think @optionloop, is maybe obsolete, or must be switchable by a systeme preference fixed rank or not
272 # make priorities options
275 for ( 1 .. $count + 1 ) {
280 selected => ( $_ == $count + 1 ),
284 # adding a fixed value for priority options
285 my $fixedRank = $count+1;
288 my %itemnumbers_of_biblioitem;
291 ## $items is array of 'item' table numbers
292 if (my $items = get_itemnumbers_of($biblionumber)->{$biblionumber}){
293 @itemnumbers = @$items;
296 $template->param('noitems' => 1);
297 $biblioloopiter{noitems} = 1;
300 ## Hash of item number to 'item' table fields
301 my $iteminfos_of = GetItemInfosOf(@itemnumbers);
303 ## Here we go backwards again to create hash of biblioitemnumber to itemnumbers,
304 ## when by definition all of the itemnumber have the same biblioitemnumber
305 foreach my $itemnumber (@itemnumbers) {
306 my $biblioitemnumber = $iteminfos_of->{$itemnumber}->{biblioitemnumber};
307 push( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} }, $itemnumber );
310 ## Should be same as biblionumber
311 my @biblioitemnumbers = keys %itemnumbers_of_biblioitem;
313 my $notforloan_label_of = get_notforloan_label_of();
315 ## Hash of biblioitemnumber to 'biblioitem' table records
316 my $biblioiteminfos_of = GetBiblioItemInfosOf(@biblioitemnumbers);
320 foreach my $biblioitemnumber (@biblioitemnumbers) {
321 my $biblioitem = $biblioiteminfos_of->{$biblioitemnumber};
322 my $num_available = 0;
323 my $num_override = 0;
326 $biblioitem->{description} =
327 $itemtypes->{ $biblioitem->{itemtype} }{description};
328 $biblioloopiter{description} = $biblioitem->{description};
329 $biblioloopiter{itypename} = $biblioitem->{description};
330 $biblioloopiter{imageurl} =
331 getitemtypeimagelocation('intranet', $itemtypes->{$biblioitem->{itemtype}}{imageurl});
333 foreach my $itemnumber ( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} } ) {
334 my $item = $iteminfos_of->{$itemnumber};
336 unless (C4::Context->preference('item-level_itypes')) {
337 $item->{itype} = $biblioitem->{itemtype};
340 $item->{itypename} = $itemtypes->{ $item->{itype} }{description};
341 $item->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtypes->{ $item->{itype} }{imageurl} );
342 $item->{homebranchname} = $branches->{ $item->{homebranch} }{branchname};
344 # if the holdingbranch is different than the homebranch, we show the
345 # holdingbranch of the document too
346 if ( $item->{homebranch} ne $item->{holdingbranch} ) {
347 $item->{holdingbranchname} =
348 $branches->{ $item->{holdingbranch} }{branchname};
352 $item->{itemcallnumber} = $item->{itemcallnumber};
354 # if the item is currently on loan, we display its return date and
355 # change the background color
356 my $issues= GetItemIssue($itemnumber);
357 if ( $issues->{'date_due'} ) {
358 $item->{date_due} = format_date($issues->{'date_due'});
359 $item->{backgroundcolor} = 'onloan';
363 my ($reservedate,$reservedfor,$expectedAt) = GetReservesFromItemnumber($itemnumber);
364 my $ItemBorrowerReserveInfo = GetMemberDetails( $reservedfor, 0);
366 if ( defined $reservedate ) {
367 $item->{backgroundcolor} = 'reserved';
368 $item->{reservedate} = format_date($reservedate);
369 $item->{ReservedForBorrowernumber} = $reservedfor;
370 $item->{ReservedForSurname} = $ItemBorrowerReserveInfo->{'surname'};
371 $item->{ReservedForFirstname} = $ItemBorrowerReserveInfo->{'firstname'};
372 $item->{ExpectedAtLibrary} = $branches->{$expectedAt}{branchname};
376 # Management of the notforloan document
377 if ( $item->{notforloan} ) {
378 $item->{backgroundcolor} = 'other';
379 $item->{notforloanvalue} =
380 $notforloan_label_of->{ $item->{notforloan} };
383 # Management of lost or long overdue items
384 if ( $item->{itemlost} ) {
386 # FIXME localized strings should never be in Perl code
388 $item->{itemlost} == 1 ? "(lost)"
389 : $item->{itemlost} == 2 ? "(long overdue)"
391 $item->{backgroundcolor} = 'other';
392 if (GetHideLostItemsPreference($borrowernumber) && !$showallitems) {
398 # Check the transit status
399 my ( $transfertwhen, $transfertfrom, $transfertto ) =
400 GetTransfers($itemnumber);
402 if ( defined $transfertwhen && $transfertwhen ne '' ) {
403 $item->{transfertwhen} = format_date($transfertwhen);
404 $item->{transfertfrom} =
405 $branches->{$transfertfrom}{branchname};
406 $item->{transfertto} = $branches->{$transfertto}{branchname};
407 $item->{nocancel} = 1;
410 # If there is no loan, return and transfer, we show a checkbox.
411 $item->{notforloan} = $item->{notforloan} || 0;
413 # if independent branches is on we need to check if the person can reserve
414 # for branches they arent logged in to
415 if ( C4::Context->preference("IndependantBranches") ) {
416 if (! C4::Context->preference("canreservefromotherbranches")){
417 # cant reserve items so need to check if item homebranch and userenv branch match if not we cant reserve
418 my $userenv = C4::Context->userenv;
419 if ( ($userenv) && ( $userenv->{flags} % 2 != 1 ) ) {
420 $item->{cantreserve} = 1 if ( $item->{homebranch} ne $userenv->{branch} );
425 my $branch = C4::Circulation::_GetCircControlBranch($item, $borrowerinfo);
427 my $branchitemrule = GetBranchItemRule( $branch, $item->{'itype'} );
428 my $policy_holdallowed = 1;
430 $item->{'holdallowed'} = $branchitemrule->{'holdallowed'};
432 if ( $branchitemrule->{'holdallowed'} == 0 ||
433 ( $branchitemrule->{'holdallowed'} == 1 &&
434 $borrowerinfo->{'branchcode'} ne $item->{'homebranch'} ) ) {
435 $policy_holdallowed = 0;
440 && !$item->{cantreserve}
441 && IsAvailableForItemLevelRequest($itemnumber)
442 && CanItemBeReserved(
443 $borrowerinfo->{borrowernumber}, $itemnumber
447 $item->{available} = 1;
450 elsif ( C4::Context->preference('AllowHoldPolicyOverride') ) {
452 # If AllowHoldPolicyOverride is set, it should override EVERY restriction, not just branch item rules
453 $item->{override} = 1;
457 # If none of the conditions hold true, then neither override nor available is set and the item cannot be checked
459 # FIXME: move this to a pm
460 my $sth2 = $dbh->prepare("SELECT * FROM reserves WHERE borrowernumber=? AND itemnumber=? AND found='W'");
461 $sth2->execute($item->{ReservedForBorrowernumber},$item->{itemnumber});
462 while (my $wait_hashref = $sth2->fetchrow_hashref) {
463 $item->{waitingdate} = format_date($wait_hashref->{waitingdate});
466 # Show serial enumeration when needed
467 if ($item->{enumchron}) {
468 $itemdata_enumchron = 1;
471 push @{ $biblioitem->{itemloop} }, $item;
474 if ( $num_override == scalar( @{ $biblioitem->{itemloop} } ) ) { # That is, if all items require an override
475 $template->param( override_required => 1 );
476 } elsif ( $num_available == 0 ) {
477 $template->param( none_available => 1 );
478 $template->param( warnings => 1 );
479 $biblioloopiter{warn} = 1;
480 $biblioloopiter{none_avail} = 1;
482 $template->param( hiddencount => $hiddencount);
484 push @bibitemloop, $biblioitem;
487 # existingreserves building
489 ( $count, $reserves ) = GetReservesFromBiblionumber($biblionumber,1);
490 foreach my $res ( sort {
491 my $a_found = $a->{found} || '';
492 my $b_found = $a->{found} || '';
493 $a_found cmp $b_found;
497 for ( my $i = 1 ; $i <= $totalcount ; $i++ ) {
502 selected => ( $i == $res->{priority} ),
507 if ( defined $res->{'found'} && $res->{'found'} eq 'W' || $res->{'found'} eq 'T' ) {
508 my $item = $res->{'itemnumber'};
509 $item = GetBiblioFromItemNumber($item,undef);
511 $reserve{'holdingbranch'}=$item->{'holdingbranch'};
512 $reserve{'biblionumber'}=$item->{'biblionumber'};
513 $reserve{'barcodenumber'} = $item->{'barcode'};
514 $reserve{'wbrcode'} = $res->{'branchcode'};
515 $reserve{'itemnumber'} = $res->{'itemnumber'};
516 $reserve{'wbrname'} = $branches->{$res->{'branchcode'}}->{'branchname'};
517 if($reserve{'holdingbranch'} eq $reserve{'wbrcode'}){
518 $reserve{'atdestination'} = 1;
520 # set found to 1 if reserve is waiting for patron pickup
521 $reserve{'found'} = 1 if $res->{'found'} eq 'W';
522 $reserve{'intransit'} = 1 if $res->{'found'} eq 'T';
523 } elsif ($res->{priority} > 0) {
524 if (defined($res->{itemnumber})) {
525 my $item = GetItem($res->{itemnumber});
526 $reserve{'itemnumber'} = $res->{'itemnumber'};
527 $reserve{'barcodenumber'} = $item->{'barcode'};
528 $reserve{'item_level_hold'} = 1;
532 # get borrowers reserve info
533 my $reserveborrowerinfo = GetMemberDetails( $res->{'borrowernumber'}, 0);
534 if (C4::Context->preference('HidePatronName')){
535 $reserve{'hidename'} = 1;
536 $reserve{'cardnumber'} = $reserveborrowerinfo->{'cardnumber'};
538 $reserve{'expirationdate'} = format_date( $res->{'expirationdate'} )
539 unless ( !defined($res->{'expirationdate'}) || $res->{'expirationdate'} eq '0000-00-00' );
540 $reserve{'date'} = format_date( $res->{'reservedate'} );
541 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
542 $reserve{'biblionumber'} = $res->{'biblionumber'};
543 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
544 $reserve{'firstname'} = $reserveborrowerinfo->{'firstname'};
545 $reserve{'surname'} = $reserveborrowerinfo->{'surname'};
546 $reserve{'notes'} = $res->{'reservenotes'};
548 ( ( defined $res->{'found'} and $res->{'found'} eq 'W' ) or ( $res->{'priority'} eq '0' ) );
549 $reserve{'constrainttypea'} = ( $res->{'constrainttype'} eq 'a' );
550 $reserve{'constrainttypeo'} = ( $res->{'constrainttype'} eq 'o' );
551 $reserve{'voldesc'} = $res->{'volumeddesc'};
552 $reserve{'ccode'} = $res->{'ccode'};
553 $reserve{'barcode'} = $res->{'barcode'};
554 $reserve{'priority'} = $res->{'priority'};
555 $reserve{'lowestPriority'} = $res->{'lowestPriority'};
556 $reserve{'branchloop'} = GetBranchesLoop($res->{'branchcode'});
557 $reserve{'optionloop'} = \@optionloop;
559 push( @reserveloop, \%reserve );
562 # get the time for the form name...
566 CGIbranch => $CGIbranch,
569 fixedRank => $fixedRank,
574 optionloop => \@optionloop,
575 bibitemloop => \@bibitemloop,
576 itemdata_enumchron => $itemdata_enumchron,
578 biblionumber => $biblionumber,
579 findborrower => $findborrower,
580 title => $dat->{title},
581 author => $dat->{author},
583 C4::Search::enabled_staff_search_views,
585 if (defined $borrowerinfo && exists $borrowerinfo->{'branchcode'}) {
587 borrower_branchname => $branches->{$borrowerinfo->{'branchcode'}}->{'branchname'},
588 borrower_branchcode => $borrowerinfo->{'branchcode'},
591 $template->param(CGIselectborrower => $CGIselectborrower) if defined $CGIselectborrower;
593 $biblioloopiter{biblionumber} = $biblionumber;
594 $biblioloopiter{title} = $dat->{title};
595 $biblioloopiter{rank} = $fixedRank;
596 $biblioloopiter{reserveloop} = \@reserveloop;
599 $template->param( reserveloop => \@reserveloop );
602 push @biblioloop, \%biblioloopiter;
605 $template->param( biblioloop => \@biblioloop );
606 $template->param( biblionumbers => $biblionumbers );
607 $template->param( DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar() );
610 $template->param( multi_hold => 1 );
613 if ( C4::Context->preference( 'AllowHoldDateInFuture' ) ) {
614 $template->param( reserve_in_future => 1 );
618 output_html_with_http_headers $input, $cookie, $template->output;