4 #written 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
11 # under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 3 of the License, or
13 # (at your option) any later version.
15 # Koha is distributed in the hope that it will be useful, but
16 # WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with Koha; if not, see <http://www.gnu.org/licenses>.
25 script to place reserves/requests
33 use List::MoreUtils qw/uniq/;
34 use Date::Calc qw/Date_to_Days/;
43 use C4::Utils::DataTables::Members;
45 use C4::Search; # enabled_staff_search_views
50 my $dbh = C4::Context->dbh;
52 my ( $template, $borrowernumber, $cookie, $flags ) = get_template_and_user(
54 template_name => "reserve/request.tt",
58 flagsrequired => { reserveforothers => 'place_holds' },
62 my $multihold = $input->param('multi_hold');
63 $template->param(multi_hold => $multihold);
64 my $showallitems = $input->param('showallitems');
66 # get Branches and Itemtypes
67 my $branches = GetBranches();
68 my $itemtypes = GetItemTypes();
71 if (C4::Context->userenv && C4::Context->userenv->{'branch'}) {
72 $userbranch = C4::Context->userenv->{'branch'};
76 # Select borrowers infos
77 my $findborrower = $input->param('findborrower');
78 $findborrower = '' unless defined $findborrower;
79 $findborrower =~ s|,| |g;
80 my $borrowernumber_hold = $input->param('borrowernumber') || '';
84 my $exceeded_maxreserves;
85 my $exceeded_holds_per_record;
87 my $date = output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 });
88 my $action = $input->param('action');
91 if ( $action eq 'move' ) {
92 my $where = $input->param('where');
93 my $reserve_id = $input->param('reserve_id');
94 AlterPriority( $where, $reserve_id );
95 } elsif ( $action eq 'cancel' ) {
96 my $reserve_id = $input->param('reserve_id');
97 CancelReserve({ reserve_id => $reserve_id });
98 } elsif ( $action eq 'setLowestPriority' ) {
99 my $reserve_id = $input->param('reserve_id');
100 ToggleLowestPriority( $reserve_id );
101 } elsif ( $action eq 'toggleSuspend' ) {
102 my $reserve_id = $input->param('reserve_id');
103 my $suspend_until = $input->param('suspend_until');
104 ToggleSuspend( $reserve_id, $suspend_until );
108 my $borrower = C4::Members::GetMember( cardnumber => $findborrower );
110 $borrowernumber_hold = $borrower->{borrowernumber};
112 my $dt_params = { iDisplayLength => -1 };
113 my $results = C4::Utils::DataTables::Members::search(
115 searchmember => $findborrower,
116 dt_params => $dt_params,
119 my $borrowers = $results->{patrons};
120 if ( scalar @$borrowers == 1 ) {
121 $borrowernumber_hold = $borrowers->[0]->{borrowernumber};
122 } elsif ( @$borrowers ) {
123 $template->param( borrowers => $borrowers );
125 $messageborrower = "'$findborrower'";
130 my @biblionumbers = ();
131 my $biblionumbers = $input->param('biblionumbers');
133 @biblionumbers = split '/', $biblionumbers;
135 push @biblionumbers, $input->multi_param('biblionumber');
139 # If we have the borrowernumber because we've performed an action, then we
140 # don't want to try to place another reserve.
141 if ($borrowernumber_hold && !$action) {
142 my $borrowerinfo = GetMember( borrowernumber => $borrowernumber_hold );
145 # we check the reserves of the borrower, and if he can reserv a document
146 # FIXME At this time we have a simple count of reservs, but, later, we could improve the infos "title" ...
149 GetReserveCount( $borrowerinfo->{'borrowernumber'} );
151 my $new_reserves_count = scalar( @biblionumbers );
153 my $maxreserves = C4::Context->preference('maxreserves');
155 && ( $reserves_count + $new_reserves_count > $maxreserves ) )
157 my $new_reserves_allowed =
158 $maxreserves - $reserves_count > 0
159 ? $maxreserves - $reserves_count
162 $exceeded_maxreserves = 1;
164 new_reserves_allowed => $new_reserves_allowed,
165 new_reserves_count => $new_reserves_count,
166 reserves_count => $reserves_count,
167 maxreserves => $maxreserves,
171 # we check the date expiry of the borrower (only if there is an expiry date, otherwise, set to 1 (warn)
172 my $expiry_date = $borrowerinfo->{dateexpiry};
173 my $expiry = 0; # flag set if patron account has expired
174 if ($expiry_date and $expiry_date ne '0000-00-00' and
175 Date_to_Days(split /-/,$date) > Date_to_Days(split /-/,$expiry_date)) {
179 # check if the borrower make the reserv in a different branch
180 if ( $borrowerinfo->{'branchcode'} ne C4::Context->userenv->{'branch'} ) {
184 my $is_debarred = Koha::Patrons->find( $borrowerinfo->{borrowernumber} )->is_debarred;
186 borrowernumber => $borrowerinfo->{'borrowernumber'},
187 borrowersurname => $borrowerinfo->{'surname'},
188 borrowerfirstname => $borrowerinfo->{'firstname'},
189 borrowerstreetaddress => $borrowerinfo->{'address'},
190 borrowercity => $borrowerinfo->{'city'},
191 borrowerphone => $borrowerinfo->{'phone'},
192 borrowermobile => $borrowerinfo->{'mobile'},
193 borrowerfax => $borrowerinfo->{'fax'},
194 borrowerphonepro => $borrowerinfo->{'phonepro'},
195 borroweremail => $borrowerinfo->{'email'},
196 borroweremailpro => $borrowerinfo->{'emailpro'},
197 borrowercategory => $borrowerinfo->{'category'},
198 cardnumber => $borrowerinfo->{'cardnumber'},
200 diffbranch => $diffbranch,
201 messages => $messages,
202 warnings => $warnings,
203 restricted => $is_debarred,
204 amount_outstanding => GetMemberAccountRecords($borrowerinfo->{borrowernumber}),
208 $template->param( messageborrower => $messageborrower );
210 # FIXME launch another time GetMember perhaps until
211 my $borrowerinfo = GetMember( borrowernumber => $borrowernumber_hold );
213 my $itemdata_enumchron = 0;
215 foreach my $biblionumber (@biblionumbers) {
216 next unless $biblionumber =~ m|^\d+$|;
218 my %biblioloopiter = ();
220 my $dat = GetBiblioData($biblionumber);
222 my $canReserve = CanBookBeReserved( $borrowerinfo->{borrowernumber}, $biblionumber );
224 if ( $canReserve eq 'OK' ) {
226 #All is OK and we can continue
228 elsif ( $canReserve eq 'tooManyReserves' ) {
229 $exceeded_maxreserves = 1;
231 elsif ( $canReserve eq 'tooManyHoldsForThisRecord' ) {
232 $exceeded_holds_per_record = 1;
233 $biblioloopiter{$canReserve} = 1;
235 elsif ( $canReserve eq 'ageRestricted' ) {
236 $template->param( $canReserve => 1 );
237 $biblioloopiter{$canReserve} = 1;
240 $biblioloopiter{$canReserve} = 1;
243 my $force_hold_level;
244 if ( $borrowerinfo->{borrowernumber} ) {
245 # For multiple holds per record, if a patron has previously placed a hold,
246 # the patron can only place more holds of the same type. That is, if the
247 # patron placed a record level hold, all the holds the patron places must
248 # be record level. If the patron placed an item level hold, all holds
249 # the patron places must be item level
250 my $holds = Koha::Holds->search(
252 borrowernumber => $borrowerinfo->{borrowernumber},
253 biblionumber => $biblionumber,
257 $force_hold_level = $holds->forced_hold_level();
258 $biblioloopiter{force_hold_level} = $force_hold_level;
259 $template->param( force_hold_level => $force_hold_level );
261 # For a librarian to be able to place multiple record holds for a patron for a record,
262 # we must find out what the maximum number of holds they can place for the patron is
263 my $max_holds_for_record = GetMaxPatronHoldsForRecord( $borrowerinfo->{borrowernumber}, $biblionumber );
264 my $remaining_holds_for_record = $max_holds_for_record - $holds->count();
265 $biblioloopiter{remaining_holds_for_record} = $max_holds_for_record;
266 $template->param( max_holds_for_record => $max_holds_for_record );
267 $template->param( remaining_holds_for_record => $remaining_holds_for_record );
270 # Check to see if patron is allowed to place holds on records where the
271 # patron already has an item from that record checked out
272 my $alreadypossession;
273 if ( !C4::Context->preference('AllowHoldsOnPatronsPossessions')
274 && CheckIfIssuedToPatron( $borrowerinfo->{borrowernumber}, $biblionumber ) )
276 $template->param( alreadypossession => $alreadypossession, );
280 my $count = Koha::Holds->search( { biblionumber => $biblionumber } )->count();
281 my $totalcount = $count;
283 # FIXME think @optionloop, is maybe obsolete, or must be switchable by a systeme preference fixed rank or not
284 # make priorities options
287 for ( 1 .. $count + 1 ) {
292 selected => ( $_ == $count + 1 ),
296 # adding a fixed value for priority options
297 my $fixedRank = $count+1;
299 my %itemnumbers_of_biblioitem;
302 ## $items is array of 'item' table numbers
303 if (my $items = get_itemnumbers_of($biblionumber)->{$biblionumber}){
304 @itemnumbers = @$items;
306 my @hostitems = get_hostitemnumbers_of($biblionumber);
308 $template->param('hostitemsflag' => 1);
309 push(@itemnumbers, @hostitems);
313 $template->param('noitems' => 1);
314 $biblioloopiter{noitems} = 1;
317 ## Hash of item number to 'item' table fields
318 my $iteminfos_of = GetItemInfosOf(@itemnumbers);
320 ## Here we go backwards again to create hash of biblioitemnumber to itemnumbers,
321 ## when by definition all of the itemnumber have the same biblioitemnumber
322 foreach my $itemnumber (@itemnumbers) {
323 my $biblioitemnumber = $iteminfos_of->{$itemnumber}->{biblioitemnumber};
324 push( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} }, $itemnumber );
327 ## Should be same as biblionumber
328 my @biblioitemnumbers = keys %itemnumbers_of_biblioitem;
330 my $notforloan_label_of = get_notforloan_label_of();
332 ## Hash of biblioitemnumber to 'biblioitem' table records
333 my $biblioiteminfos_of = GetBiblioItemInfosOf(@biblioitemnumbers);
337 my @available_itemtypes;
338 foreach my $biblioitemnumber (@biblioitemnumbers) {
339 my $biblioitem = $biblioiteminfos_of->{$biblioitemnumber};
340 my $num_available = 0;
341 my $num_override = 0;
344 $biblioitem->{force_hold_level} = $force_hold_level;
346 if ( $biblioitem->{biblioitemnumber} ne $biblionumber ) {
347 $biblioitem->{hostitemsflag} = 1;
350 $biblioloopiter{description} = $biblioitem->{description};
351 $biblioloopiter{itypename} = $biblioitem->{description};
352 if ( $biblioitem->{itemtype} ) {
354 $biblioitem->{description} =
355 $itemtypes->{ $biblioitem->{itemtype} }{description};
357 $biblioloopiter{imageurl} =
358 getitemtypeimagelocation( 'intranet',
359 $itemtypes->{ $biblioitem->{itemtype} }{imageurl} );
362 foreach my $itemnumber ( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} } ) {
363 my $item = $iteminfos_of->{$itemnumber};
365 $item->{force_hold_level} = $force_hold_level;
367 unless (C4::Context->preference('item-level_itypes')) {
368 $item->{itype} = $biblioitem->{itemtype};
371 $item->{itypename} = $itemtypes->{ $item->{itype} }{description};
372 $item->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtypes->{ $item->{itype} }{imageurl} );
373 $item->{homebranchname} = $branches->{ $item->{homebranch} }{branchname};
375 # if the holdingbranch is different than the homebranch, we show the
376 # holdingbranch of the document too
377 if ( $item->{homebranch} ne $item->{holdingbranch} ) {
378 $item->{holdingbranchname} =
379 $branches->{ $item->{holdingbranch} }{branchname};
382 if($item->{biblionumber} ne $biblionumber){
383 $item->{hostitemsflag}=1;
384 $item->{hosttitle} = GetBiblioData($item->{biblionumber})->{title};
387 # if the item is currently on loan, we display its return date and
388 # change the background color
389 my $issues= GetItemIssue($itemnumber);
390 if ( $issues->{'date_due'} ) {
391 $item->{date_due} = $issues->{date_due_sql};
392 $item->{backgroundcolor} = 'onloan';
396 my ($reservedate,$reservedfor,$expectedAt,$reserve_id,$wait) = GetReservesFromItemnumber($itemnumber);
397 if ( defined $reservedate ) {
398 my $ItemBorrowerReserveInfo = GetMember( borrowernumber => $reservedfor );
400 $item->{backgroundcolor} = 'reserved';
401 $item->{reservedate} = output_pref({ dt => dt_from_string( $reservedate ), dateonly => 1 });
402 $item->{ReservedForBorrowernumber} = $reservedfor;
403 $item->{ReservedForSurname} = $ItemBorrowerReserveInfo->{'surname'};
404 $item->{ReservedForFirstname} = $ItemBorrowerReserveInfo->{'firstname'};
405 $item->{ExpectedAtLibrary} = $branches->{$expectedAt}{branchname};
406 $item->{waitingdate} = $wait;
409 # Management of the notforloan document
410 if ( $item->{notforloan} ) {
411 $item->{backgroundcolor} = 'other';
412 $item->{notforloanvalue} =
413 $notforloan_label_of->{ $item->{notforloan} };
416 # Management of lost or long overdue items
417 if ( $item->{itemlost} ) {
419 # FIXME localized strings should never be in Perl code
421 $item->{itemlost} == 1 ? "(lost)"
422 : $item->{itemlost} == 2 ? "(long overdue)"
424 $item->{backgroundcolor} = 'other';
425 if (GetHideLostItemsPreference($borrowernumber) && !$showallitems) {
431 # Check the transit status
432 my ( $transfertwhen, $transfertfrom, $transfertto ) =
433 GetTransfers($itemnumber);
435 if ( defined $transfertwhen && $transfertwhen ne '' ) {
436 $item->{transfertwhen} = output_pref({ dt => dt_from_string( $transfertwhen ), dateonly => 1 });
437 $item->{transfertfrom} =
438 $branches->{$transfertfrom}{branchname};
439 $item->{transfertto} = $branches->{$transfertto}{branchname};
440 $item->{nocancel} = 1;
443 # If there is no loan, return and transfer, we show a checkbox.
444 $item->{notforloan} ||= 0;
446 # if independent branches is on we need to check if the person can reserve
447 # for branches they arent logged in to
448 if ( C4::Context->preference("IndependentBranches") ) {
449 if (! C4::Context->preference("canreservefromotherbranches")){
450 # cant reserve items so need to check if item homebranch and userenv branch match if not we cant reserve
451 my $userenv = C4::Context->userenv;
452 unless ( C4::Context->IsSuperLibrarian ) {
453 $item->{cantreserve} = 1 if ( $item->{homebranch} ne $userenv->{branch} );
458 my $branch = C4::Circulation::_GetCircControlBranch($item, $borrowerinfo);
460 my $branchitemrule = GetBranchItemRule( $branch, $item->{'itype'} );
462 $item->{'holdallowed'} = $branchitemrule->{'holdallowed'};
464 my $can_item_be_reserved = CanItemBeReserved( $borrowerinfo->{borrowernumber}, $itemnumber );
465 $item->{not_holdable} = $can_item_be_reserved unless ( $can_item_be_reserved eq 'OK' );
467 $item->{item_level_holds} = OPACItemHoldsAllowed( $item, $borrowerinfo );
470 !$item->{cantreserve}
471 && !$exceeded_maxreserves
472 && IsAvailableForItemLevelRequest($item, $borrowerinfo)
473 && $can_item_be_reserved eq 'OK'
476 $item->{available} = 1;
479 push( @available_itemtypes, $item->{itype} );
481 elsif ( C4::Context->preference('AllowHoldPolicyOverride') ) {
482 # If AllowHoldPolicyOverride is set, it should override EVERY restriction, not just branch item rules
483 $item->{override} = 1;
487 # If none of the conditions hold true, then neither override nor available is set and the item cannot be checked
489 # Show serial enumeration when needed
490 if ($item->{enumchron}) {
491 $itemdata_enumchron = 1;
494 push @{ $biblioitem->{itemloop} }, $item;
497 if ( $num_override == scalar( @{ $biblioitem->{itemloop} } ) ) { # That is, if all items require an override
498 $template->param( override_required => 1 );
499 } elsif ( $num_available == 0 ) {
500 $template->param( none_available => 1 );
501 $biblioloopiter{warn} = 1;
502 $biblioloopiter{none_avail} = 1;
504 $template->param( hiddencount => $hiddencount);
506 push @bibitemloop, $biblioitem;
509 @available_itemtypes = uniq( @available_itemtypes );
510 $template->param( available_itemtypes => \@available_itemtypes );
512 # existingreserves building
514 my @reserves = Koha::Holds->search( { biblionumber => $biblionumber }, { order_by => 'priority' } );
517 my $a_found = $a->found() || '';
518 my $b_found = $a->found() || '';
519 $a_found cmp $b_found;
525 for ( my $i = 1 ; $i <= $totalcount ; $i++ ) {
530 selected => ( $i == $res->priority() ),
535 if ( $res->is_found() ) {
536 $reserve{'holdingbranch'} = $res->item()->holdingbranch();
537 $reserve{'biblionumber'} = $res->item()->biblionumber();
538 $reserve{'barcodenumber'} = $res->item()->barcode();
539 $reserve{'wbrcode'} = $res->branchcode();
540 $reserve{'itemnumber'} = $res->itemnumber();
541 $reserve{'wbrname'} = $res->branch()->branchname();
543 if ( $reserve{'holdingbranch'} eq $reserve{'wbrcode'} ) {
545 # Just because the holdingbranch matches the reserve branch doesn't mean the item
546 # has arrived at the destination, check for an open transfer for the item as well
547 my ( $transfertwhen, $transfertfrom, $transferto ) =
548 C4::Circulation::GetTransfers( $res->itemnumber() );
549 if ( not $transferto or $transferto ne $res->branchcode() ) {
550 $reserve{'atdestination'} = 1;
554 # set found to 1 if reserve is waiting for patron pickup
555 $reserve{'found'} = $res->is_found();
556 $reserve{'intransit'} = $res->is_in_transit();
558 elsif ( $res->priority() > 0 ) {
559 if ( my $item = $res->item() ) {
560 $reserve{'itemnumber'} = $item->id();
561 $reserve{'barcodenumber'} = $item->barcode();
562 $reserve{'item_level_hold'} = 1;
566 # get borrowers reserve info
567 if ( C4::Context->preference('HidePatronName') ) {
568 $reserve{'hidename'} = 1;
569 $reserve{'cardnumber'} = $res->borrower()->cardnumber();
571 $reserve{'expirationdate'} = output_pref( { dt => dt_from_string( $res->expirationdate ), dateonly => 1 } )
572 unless ( !defined( $res->expirationdate ) || $res->expirationdate eq '0000-00-00' );
573 $reserve{'date'} = output_pref( { dt => dt_from_string( $res->reservedate ), dateonly => 1 } );
574 $reserve{'borrowernumber'} = $res->borrowernumber();
575 $reserve{'biblionumber'} = $res->biblionumber();
576 $reserve{'borrowernumber'} = $res->borrowernumber();
577 $reserve{'firstname'} = $res->borrower()->firstname();
578 $reserve{'surname'} = $res->borrower()->surname();
579 $reserve{'notes'} = $res->reservenotes();
580 $reserve{'waiting_date'} = $res->waitingdate();
581 $reserve{'waiting_until'} = $res->is_waiting() ? $res->waiting_expires_on() : undef;
582 $reserve{'ccode'} = $res->item() ? $res->item()->ccode() : undef;
583 $reserve{'barcode'} = $res->item() ? $res->item()->barcode() : undef;
584 $reserve{'priority'} = $res->priority();
585 $reserve{'lowestPriority'} = $res->lowestPriority();
586 $reserve{'optionloop'} = \@optionloop;
587 $reserve{'suspend'} = $res->suspend();
588 $reserve{'suspend_until'} = $res->suspend_until();
589 $reserve{'reserve_id'} = $res->reserve_id();
590 $reserve{itemtype} = $res->itemtype();
592 if ( C4::Context->preference('IndependentBranches') && $flags->{'superlibrarian'} != 1 ) {
593 $reserve{'branchloop'} = [ Koha::Libraries->find( $res->branchcode() ) ];
596 $reserve{'branchloop'} = GetBranchesLoop( $res->branchcode() );
599 push( @reserveloop, \%reserve );
602 # get the time for the form name...
606 branchloop => GetBranchesLoop($userbranch),
608 fixedRank => $fixedRank,
613 optionloop => \@optionloop,
614 bibitemloop => \@bibitemloop,
615 itemdata_enumchron => $itemdata_enumchron,
617 biblionumber => $biblionumber,
618 findborrower => $findborrower,
619 title => $dat->{title},
620 author => $dat->{author},
622 C4::Search::enabled_staff_search_views,
624 if (defined $borrowerinfo && exists $borrowerinfo->{'branchcode'}) {
626 borrower_branchname => $branches->{$borrowerinfo->{'branchcode'}}->{'branchname'},
627 borrower_branchcode => $borrowerinfo->{'branchcode'},
631 $biblioloopiter{biblionumber} = $biblionumber;
632 $biblioloopiter{title} = $dat->{title};
633 $biblioloopiter{rank} = $fixedRank;
634 $biblioloopiter{reserveloop} = \@reserveloop;
637 $template->param( reserveloop => \@reserveloop );
640 push @biblioloop, \%biblioloopiter;
643 $template->param( biblioloop => \@biblioloop );
644 $template->param( biblionumbers => $biblionumbers );
645 $template->param( exceeded_maxreserves => $exceeded_maxreserves );
646 $template->param( exceeded_holds_per_record => $exceeded_holds_per_record );
649 $template->param( multi_hold => 1 );
652 if ( C4::Context->preference( 'AllowHoldDateInFuture' ) ) {
653 $template->param( reserve_in_future => 1 );
657 SuspendHoldsIntranet => C4::Context->preference('SuspendHoldsIntranet'),
658 AutoResumeSuspendedHolds => C4::Context->preference('AutoResumeSuspendedHolds'),
662 output_html_with_http_headers $input, $cookie, $template->output;
664 sub sort_borrowerlist {
665 my $borrowerslist = shift;
668 uc( $a->{surname} . $a->{firstname} ) cmp
669 uc( $b->{surname} . $b->{firstname} )