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
32 use List::MoreUtils qw/uniq/;
33 use Date::Calc qw/Date_to_Days/;
42 use C4::Utils::DataTables::Members;
44 use C4::Search; # enabled_staff_search_views
50 use Koha::IssuingRules;
56 my $dbh = C4::Context->dbh;
58 my ( $template, $borrowernumber, $cookie, $flags ) = get_template_and_user(
60 template_name => "reserve/request.tt",
64 flagsrequired => { reserveforothers => 'place_holds' },
68 my $showallitems = $input->param('showallitems');
70 my $itemtypes = { map { $_->{itemtype} => $_ } @{ Koha::ItemTypes->search_with_localization->unblessed } };
72 # Select borrowers infos
73 my $findborrower = $input->param('findborrower');
74 $findborrower = '' unless defined $findborrower;
75 $findborrower =~ s|,| |g;
76 my $borrowernumber_hold = $input->param('borrowernumber') || '';
80 my $exceeded_maxreserves;
81 my $exceeded_holds_per_record;
83 my $date = output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 });
84 my $action = $input->param('action');
87 if ( $action eq 'move' ) {
88 my $where = $input->param('where');
89 my $reserve_id = $input->param('reserve_id');
90 my $prev_priority = $input->param('prev_priority');
91 my $next_priority = $input->param('next_priority');
92 my $first_priority = $input->param('first_priority');
93 my $last_priority = $input->param('last_priority');
94 AlterPriority( $where, $reserve_id, $prev_priority, $next_priority, $first_priority, $last_priority );
95 } elsif ( $action eq 'cancel' ) {
96 my $reserve_id = $input->param('reserve_id');
97 my $hold = Koha::Holds->find( $reserve_id );
98 $hold->cancel if $hold;
99 } elsif ( $action eq 'setLowestPriority' ) {
100 my $reserve_id = $input->param('reserve_id');
101 ToggleLowestPriority( $reserve_id );
102 } elsif ( $action eq 'toggleSuspend' ) {
103 my $reserve_id = $input->param('reserve_id');
104 my $suspend_until = $input->param('suspend_until');
105 ToggleSuspend( $reserve_id, $suspend_until );
109 my $patron = Koha::Patrons->find( { cardnumber => $findborrower } );
111 $borrowernumber_hold = $patron->borrowernumber;
113 my $dt_params = { iDisplayLength => -1 };
114 my $results = C4::Utils::DataTables::Members::search(
116 searchmember => $findborrower,
117 dt_params => $dt_params,
120 my $borrowers = $results->{patrons};
121 if ( scalar @$borrowers == 1 ) {
122 $borrowernumber_hold = $borrowers->[0]->{borrowernumber};
123 } elsif ( @$borrowers ) {
124 $template->param( borrowers => $borrowers );
126 $messageborrower = "'$findborrower'";
131 my @biblionumbers = ();
132 my $biblionumber = $input->param('biblionumber');
133 my $biblionumbers = $input->param('biblionumbers');
134 if ( $biblionumbers ) {
135 @biblionumbers = split '/', $biblionumbers;
137 push @biblionumbers, $input->multi_param('biblionumber');
140 # FIXME multi_hold should not be a variable but depends on the number of elements in @biblionumbers
141 $template->param(multi_hold => scalar $input->param('multi_hold'));
143 # If we have the borrowernumber because we've performed an action, then we
144 # don't want to try to place another reserve.
145 if ($borrowernumber_hold && !$action) {
146 my $patron = Koha::Patrons->find( $borrowernumber_hold );
149 # we check the reserves of the user, and if they can reserve a document
150 # FIXME At this time we have a simple count of reservs, but, later, we could improve the infos "title" ...
152 my $reserves_count = $patron->holds->count;
154 my $new_reserves_count = scalar( @biblionumbers );
156 my $maxreserves = C4::Context->preference('maxreserves');
158 && ( $reserves_count + $new_reserves_count > $maxreserves ) )
160 my $new_reserves_allowed =
161 $maxreserves - $reserves_count > 0
162 ? $maxreserves - $reserves_count
165 $exceeded_maxreserves = 1;
167 new_reserves_allowed => $new_reserves_allowed,
168 new_reserves_count => $new_reserves_count,
169 reserves_count => $reserves_count,
170 maxreserves => $maxreserves,
174 # we check the date expiry of the borrower (only if there is an expiry date, otherwise, set to 1 (warn)
175 my $expiry_date = $patron->dateexpiry;
176 my $expiry = 0; # flag set if patron account has expired
177 if ($expiry_date and $expiry_date ne '0000-00-00' and
178 Date_to_Days(split /-/,$date) > Date_to_Days(split /-/,$expiry_date)) {
182 # check if the borrower make the reserv in a different branch
183 if ( $patron->branchcode ne C4::Context->userenv->{'branch'} ) {
187 my $amount_outstanding = $patron->account->balance;
191 diffbranch => $diffbranch,
192 messages => $messages,
193 warnings => $warnings,
194 amount_outstanding => $amount_outstanding,
198 $template->param( messageborrower => $messageborrower );
200 # FIXME launch another time GetMember perhaps until (Joubu: Why?)
201 my $patron = Koha::Patrons->find( $borrowernumber_hold );
203 my $logged_in_patron = Koha::Patrons->find( $borrowernumber );
205 my $itemdata_enumchron = 0;
207 foreach my $biblionumber (@biblionumbers) {
208 next unless $biblionumber =~ m|^\d+$|;
210 my %biblioloopiter = ();
212 my $biblio = Koha::Biblios->find( $biblionumber );
214 my $force_hold_level;
216 { # CanBookBeReserved
217 my $canReserve = CanBookBeReserved( $patron->borrowernumber, $biblionumber );
218 $canReserve->{status} //= '';
219 if ( $canReserve->{status} eq 'OK' ) {
221 #All is OK and we can continue
223 elsif ( $canReserve->{status} eq 'tooManyReserves' ) {
224 $exceeded_maxreserves = 1;
225 $template->param( maxreserves => $canReserve->{limit} );
227 elsif ( $canReserve->{status} eq 'tooManyHoldsForThisRecord' ) {
228 $exceeded_holds_per_record = 1;
229 $biblioloopiter{ $canReserve->{status} } = 1;
231 elsif ( $canReserve->{status} eq 'ageRestricted' ) {
232 $template->param( $canReserve->{status} => 1 );
233 $biblioloopiter{ $canReserve->{status} } = 1;
236 $biblioloopiter{ $canReserve->{status} } = 1;
240 # For multiple holds per record, if a patron has previously placed a hold,
241 # the patron can only place more holds of the same type. That is, if the
242 # patron placed a record level hold, all the holds the patron places must
243 # be record level. If the patron placed an item level hold, all holds
244 # the patron places must be item level
245 my $holds = Koha::Holds->search(
247 borrowernumber => $patron->borrowernumber,
248 biblionumber => $biblionumber,
252 $force_hold_level = $holds->forced_hold_level();
253 $biblioloopiter{force_hold_level} = $force_hold_level;
254 $template->param( force_hold_level => $force_hold_level );
256 # For a librarian to be able to place multiple record holds for a patron for a record,
257 # we must find out what the maximum number of holds they can place for the patron is
258 my $max_holds_for_record = GetMaxPatronHoldsForRecord( $patron->borrowernumber, $biblionumber );
259 my $remaining_holds_for_record = $max_holds_for_record - $holds->count();
260 $biblioloopiter{remaining_holds_for_record} = $max_holds_for_record;
261 $template->param( max_holds_for_record => $max_holds_for_record );
262 $template->param( remaining_holds_for_record => $remaining_holds_for_record );
264 { # alreadypossession
265 # Check to see if patron is allowed to place holds on records where the
266 # patron already has an item from that record checked out
267 if ( !C4::Context->preference('AllowHoldsOnPatronsPossessions')
268 && CheckIfIssuedToPatron( $patron->borrowernumber, $biblionumber ) )
270 $template->param( alreadypossession => 1, );
276 my $count = Koha::Holds->search( { biblionumber => $biblionumber } )->count();
277 my $totalcount = $count;
279 # FIXME think @optionloop, is maybe obsolete, or must be switchable by a systeme preference fixed rank or not
280 # make priorities options
283 for ( 1 .. $count + 1 ) {
288 selected => ( $_ == $count + 1 ),
292 # adding a fixed value for priority options
293 my $fixedRank = $count+1;
295 my %itemnumbers_of_biblioitem;
297 my @hostitems = get_hostitemnumbers_of($biblionumber);
300 $template->param('hostitemsflag' => 1);
301 push(@itemnumbers, @hostitems);
304 my $items = Koha::Items->search({ -or => { biblionumber => $biblionumber, itemnumber => { in => \@itemnumbers } } });
306 unless ( $items->count ) {
307 # FIXME Then why do we continue?
308 $template->param('noitems' => 1);
309 $biblioloopiter{noitems} = 1;
312 ## Here we go backwards again to create hash of biblioitemnumber to itemnumbers,
313 ## when by definition all of the itemnumber have the same biblioitemnumber
314 my ( $iteminfos_of );
315 while ( my $item = $items->next ) {
316 $item = $item->unblessed;
317 my $biblioitemnumber = $item->{biblioitemnumber};
318 my $itemnumber = $item->{itemnumber};
319 push( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} }, $itemnumber );
320 $iteminfos_of->{$itemnumber} = $item;
323 ## Should be same as biblionumber
324 my @biblioitemnumbers = keys %itemnumbers_of_biblioitem;
326 my $biblioiteminfos_of = {
329 ( $biblioitem->{biblioitemnumber} => $biblioitem )
330 } @{ Koha::Biblioitems->search(
331 { biblioitemnumber => { -in => \@biblioitemnumbers } },
332 { select => ['biblioitemnumber', 'publicationyear', 'itemtype']}
337 my $frameworkcode = GetFrameworkCode( $biblionumber );
338 my @notforloan_avs = Koha::AuthorisedValues->search_by_koha_field({ kohafield => 'items.notforloan', frameworkcode => $frameworkcode });
339 my $notforloan_label_of = { map { $_->authorised_value => $_->lib } @notforloan_avs };
343 my @available_itemtypes;
344 foreach my $biblioitemnumber (@biblioitemnumbers) {
345 my $biblioitem = $biblioiteminfos_of->{$biblioitemnumber};
346 my $num_available = 0;
347 my $num_override = 0;
350 $biblioitem->{force_hold_level} = $force_hold_level;
352 if ( $biblioitem->{biblioitemnumber} ne $biblionumber ) {
353 $biblioitem->{hostitemsflag} = 1;
356 $biblioloopiter{description} = $biblioitem->{description};
357 $biblioloopiter{itypename} = $biblioitem->{description};
358 if ( $biblioitem->{itemtype} ) {
360 $biblioitem->{description} =
361 $itemtypes->{ $biblioitem->{itemtype} }{description};
363 $biblioloopiter{imageurl} =
364 getitemtypeimagelocation( 'intranet',
365 $itemtypes->{ $biblioitem->{itemtype} }{imageurl} );
368 foreach my $itemnumber ( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} } ) {
369 my $item = $iteminfos_of->{$itemnumber};
371 $item->{force_hold_level} = $force_hold_level;
373 unless (C4::Context->preference('item-level_itypes')) {
374 $item->{itype} = $biblioitem->{itemtype};
377 $item->{itypename} = $itemtypes->{ $item->{itype} }{description};
378 $item->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtypes->{ $item->{itype} }{imageurl} );
379 $item->{homebranch} = $item->{homebranch};
381 # if the holdingbranch is different than the homebranch, we show the
382 # holdingbranch of the document too
383 if ( $item->{homebranch} ne $item->{holdingbranch} ) {
384 $item->{holdingbranch} = $item->{holdingbranch};
387 if($item->{biblionumber} ne $biblionumber){
388 $item->{hostitemsflag} = 1;
389 $item->{hosttitle} = Koha::Biblios->find( $item->{biblionumber} )->title;
392 # if the item is currently on loan, we display its return date and
393 # change the background color
394 my $issue = Koha::Checkouts->find( { itemnumber => $itemnumber } );
396 $item->{date_due} = $issue->date_due;
397 $item->{backgroundcolor} = 'onloan';
401 my $item_object = Koha::Items->find( $itemnumber );
402 my $holds = $item_object->current_holds;
403 if ( my $first_hold = $holds->next ) {
404 my $p = Koha::Patrons->find( $first_hold->borrowernumber );
406 $item->{backgroundcolor} = 'reserved';
407 $item->{reservedate} = output_pref({ dt => dt_from_string( $first_hold->reservedate ), dateonly => 1 }); # FIXME Should be formatted in the template
408 $item->{ReservedFor} = $p;
409 $item->{ExpectedAtLibrary} = $first_hold->branchcode;
410 $item->{waitingdate} = $first_hold->waitingdate;
413 # Management of the notforloan document
414 if ( $item->{notforloan} ) {
415 $item->{backgroundcolor} = 'other';
416 $item->{notforloanvalue} =
417 $notforloan_label_of->{ $item->{notforloan} };
420 # Management of lost or long overdue items
421 if ( $item->{itemlost} ) {
423 # FIXME localized strings should never be in Perl code
425 $item->{itemlost} == 1 ? "(lost)"
426 : $item->{itemlost} == 2 ? "(long overdue)"
428 $item->{backgroundcolor} = 'other';
429 if ($logged_in_patron->category->hidelostitems && !$showallitems) {
435 # Check the transit status
436 my ( $transfertwhen, $transfertfrom, $transfertto ) =
437 GetTransfers($itemnumber);
439 if ( defined $transfertwhen && $transfertwhen ne '' ) {
440 $item->{transfertwhen} = output_pref({ dt => dt_from_string( $transfertwhen ), dateonly => 1 });
441 $item->{transfertfrom} = $transfertfrom;
442 $item->{transfertto} = $transfertto;
443 $item->{nocancel} = 1;
446 # If there is no loan, return and transfer, we show a checkbox.
447 $item->{notforloan} ||= 0;
449 # if independent branches is on we need to check if the person can reserve
450 # for branches they arent logged in to
451 if ( C4::Context->preference("IndependentBranches") ) {
452 if (! C4::Context->preference("canreservefromotherbranches")){
453 # cant reserve items so need to check if item homebranch and userenv branch match if not we cant reserve
454 my $userenv = C4::Context->userenv;
455 unless ( C4::Context->IsSuperLibrarian ) {
456 $item->{cantreserve} = 1 if ( $item->{homebranch} ne $userenv->{branch} );
462 my $patron_unblessed = $patron->unblessed;
463 my $branch = C4::Circulation::_GetCircControlBranch($item, $patron_unblessed);
465 my $branchitemrule = GetBranchItemRule( $branch, $item->{'itype'} );
467 $item->{'holdallowed'} = $branchitemrule->{'holdallowed'};
469 my $can_item_be_reserved = CanItemBeReserved( $patron->borrowernumber, $itemnumber );
470 $item->{not_holdable} = $can_item_be_reserved->{status} unless ( $can_item_be_reserved->{status} eq 'OK' );
472 $item->{item_level_holds} = Koha::IssuingRules->get_opacitemholds_policy( { item => $item_object, patron => $patron } );
475 !$item->{cantreserve}
476 && !$exceeded_maxreserves
477 && IsAvailableForItemLevelRequest($item, $patron_unblessed)
478 && $can_item_be_reserved->{status} eq 'OK'
481 $item->{available} = 1;
484 push( @available_itemtypes, $item->{itype} );
486 elsif ( $can_item_be_reserved->{status} eq 'tooManyReserves' && C4::Context->preference('AllowHoldPolicyOverride') ) {
487 # If AllowHoldPolicyOverride is set, it should override EVERY restriction, not just branch item rules
488 $item->{override} = 1;
491 push( @available_itemtypes, $item->{itype} );
494 # If none of the conditions hold true, then neither override nor available is set and the item cannot be checked
496 # Show serial enumeration when needed
497 if ($item->{enumchron}) {
498 $itemdata_enumchron = 1;
502 push @{ $biblioitem->{itemloop} }, $item;
505 if ( $num_override == scalar( @{ $biblioitem->{itemloop} } ) ) { # That is, if all items require an override
506 $template->param( override_required => 1 );
507 } elsif ( $num_available == 0 ) {
508 $template->param( none_available => 1 );
509 $biblioloopiter{warn} = 1;
510 $biblioloopiter{none_avail} = 1;
512 $template->param( hiddencount => $hiddencount);
514 push @bibitemloop, $biblioitem;
517 @available_itemtypes = uniq( @available_itemtypes );
518 $template->param( available_itemtypes => \@available_itemtypes );
520 # existingreserves building
522 my @reserves = Koha::Holds->search( { biblionumber => $biblionumber }, { order_by => 'priority' } );
525 my $a_found = $a->found() || '';
526 my $b_found = $a->found() || '';
527 $a_found cmp $b_found;
531 my $priority = $res->priority();
534 for ( my $i = 1 ; $i <= $totalcount ; $i++ ) {
539 selected => ( $i == $priority ),
544 if ( $res->is_found() ) {
545 $reserve{'holdingbranch'} = $res->item()->holdingbranch();
546 $reserve{'biblionumber'} = $res->item()->biblionumber();
547 $reserve{'barcodenumber'} = $res->item()->barcode();
548 $reserve{'wbrcode'} = $res->branchcode();
549 $reserve{'itemnumber'} = $res->itemnumber();
550 $reserve{'wbrname'} = $res->branch()->branchname();
552 if ( $reserve{'holdingbranch'} eq $reserve{'wbrcode'} ) {
554 # Just because the holdingbranch matches the reserve branch doesn't mean the item
555 # has arrived at the destination, check for an open transfer for the item as well
556 my ( $transfertwhen, $transfertfrom, $transferto ) =
557 C4::Circulation::GetTransfers( $res->itemnumber() );
558 if ( not $transferto or $transferto ne $res->branchcode() ) {
559 $reserve{'atdestination'} = 1;
563 # set found to 1 if reserve is waiting for patron pickup
564 $reserve{'found'} = $res->is_found();
565 $reserve{'intransit'} = $res->is_in_transit();
567 elsif ( $res->priority() > 0 ) {
568 if ( my $item = $res->item() ) {
569 $reserve{'itemnumber'} = $item->id();
570 $reserve{'barcodenumber'} = $item->barcode();
571 $reserve{'item_level_hold'} = 1;
575 $reserve{'expirationdate'} = output_pref( { dt => dt_from_string( $res->expirationdate ), dateonly => 1 } )
576 unless ( !defined( $res->expirationdate ) || $res->expirationdate eq '0000-00-00' );
577 $reserve{'date'} = output_pref( { dt => dt_from_string( $res->reservedate ), dateonly => 1 } );
578 $reserve{'borrowernumber'} = $res->borrowernumber();
579 $reserve{'biblionumber'} = $res->biblionumber();
580 $reserve{'patron'} = $res->borrower;
581 $reserve{'notes'} = $res->reservenotes();
582 $reserve{'waiting_date'} = $res->waitingdate();
583 $reserve{'ccode'} = $res->item() ? $res->item()->ccode() : undef;
584 $reserve{'barcode'} = $res->item() ? $res->item()->barcode() : undef;
585 $reserve{'priority'} = $res->priority();
586 $reserve{'lowestPriority'} = $res->lowestPriority();
587 $reserve{'optionloop'} = \@optionloop;
588 $reserve{'suspend'} = $res->suspend();
589 $reserve{'suspend_until'} = $res->suspend_until();
590 $reserve{'reserve_id'} = $res->reserve_id();
591 $reserve{itemtype} = $res->itemtype();
592 $reserve{branchcode} = $res->branchcode();
593 $reserve{object} = $res;
595 push( @reserveloop, \%reserve );
598 # get the time for the form name...
603 fixedRank => $fixedRank,
608 optionloop => \@optionloop,
609 bibitemloop => \@bibitemloop,
610 itemdata_enumchron => $itemdata_enumchron,
612 biblionumber => $biblionumber,
613 findborrower => $findborrower,
614 title => $biblio->title,
615 author => $biblio->author,
617 C4::Search::enabled_staff_search_views,
620 $biblioloopiter{biblionumber} = $biblionumber;
621 $biblioloopiter{title} = $biblio->title;
622 $biblioloopiter{rank} = $fixedRank;
623 $biblioloopiter{reserveloop} = \@reserveloop;
626 $template->param( reserveloop => \@reserveloop );
629 push @biblioloop, \%biblioloopiter;
632 $template->param( biblioloop => \@biblioloop );
633 $template->param( biblionumbers => $biblionumbers );
634 $template->param( exceeded_maxreserves => $exceeded_maxreserves );
635 $template->param( exceeded_holds_per_record => $exceeded_holds_per_record );
637 if ( C4::Context->preference( 'AllowHoldDateInFuture' ) ) {
638 $template->param( reserve_in_future => 1 );
642 SuspendHoldsIntranet => C4::Context->preference('SuspendHoldsIntranet'),
643 AutoResumeSuspendedHolds => C4::Context->preference('AutoResumeSuspendedHolds'),
647 output_html_with_http_headers $input, $cookie, $template->output;
649 sub sort_borrowerlist {
650 my $borrowerslist = shift;
653 uc( $a->{surname} . $a->{firstname} ) cmp
654 uc( $b->{surname} . $b->{firstname} )