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/;
43 use C4
::Utils
::DataTables
::Members
;
45 use C4
::Search
; # enabled_staff_search_views
51 use Koha
::IssuingRules
;
57 my $dbh = C4
::Context
->dbh;
59 my ( $template, $borrowernumber, $cookie, $flags ) = get_template_and_user
(
61 template_name
=> "reserve/request.tt",
65 flagsrequired
=> { reserveforothers
=> 'place_holds' },
69 my $showallitems = $input->param('showallitems');
71 my $itemtypes = { map { $_->{itemtype
} => $_ } @
{ Koha
::ItemTypes
->search_with_localization->unblessed } };
73 # Select borrowers infos
74 my $findborrower = $input->param('findborrower');
75 $findborrower = '' unless defined $findborrower;
76 $findborrower =~ s
|,| |g
;
77 my $borrowernumber_hold = $input->param('borrowernumber') || '';
81 my $exceeded_maxreserves;
82 my $exceeded_holds_per_record;
84 my $date = output_pref
({ dt
=> dt_from_string
, dateformat
=> 'iso', dateonly
=> 1 });
85 my $action = $input->param('action');
88 if ( $action eq 'move' ) {
89 my $where = $input->param('where');
90 my $reserve_id = $input->param('reserve_id');
91 my $prev_priority = $input->param('prev_priority');
92 my $next_priority = $input->param('next_priority');
93 my $first_priority = $input->param('first_priority');
94 my $last_priority = $input->param('last_priority');
95 AlterPriority
( $where, $reserve_id, $prev_priority, $next_priority, $first_priority, $last_priority );
96 } elsif ( $action eq 'cancel' ) {
97 my $reserve_id = $input->param('reserve_id');
98 my $hold = Koha
::Holds
->find( $reserve_id );
99 $hold->cancel if $hold;
100 } elsif ( $action eq 'setLowestPriority' ) {
101 my $reserve_id = $input->param('reserve_id');
102 ToggleLowestPriority
( $reserve_id );
103 } elsif ( $action eq 'toggleSuspend' ) {
104 my $reserve_id = $input->param('reserve_id');
105 my $suspend_until = $input->param('suspend_until');
106 ToggleSuspend
( $reserve_id, $suspend_until );
110 my $patron = Koha
::Patrons
->find( { cardnumber
=> $findborrower } );
112 $borrowernumber_hold = $patron->borrowernumber;
114 my $dt_params = { iDisplayLength
=> -1 };
115 my $results = C4
::Utils
::DataTables
::Members
::search
(
117 searchmember
=> $findborrower,
118 dt_params
=> $dt_params,
121 my $borrowers = $results->{patrons
};
122 if ( scalar @
$borrowers == 1 ) {
123 $borrowernumber_hold = $borrowers->[0]->{borrowernumber
};
124 } elsif ( @
$borrowers ) {
125 $template->param( borrowers
=> $borrowers );
127 $messageborrower = "'$findborrower'";
132 my @biblionumbers = ();
133 my $biblionumber = $input->param('biblionumber');
134 my $biblionumbers = $input->param('biblionumbers');
135 if ( $biblionumbers ) {
136 @biblionumbers = split '/', $biblionumbers;
138 push @biblionumbers, $input->multi_param('biblionumber');
141 # FIXME multi_hold should not be a variable but depends on the number of elements in @biblionumbers
142 $template->param(multi_hold
=> scalar $input->param('multi_hold'));
144 # If we have the borrowernumber because we've performed an action, then we
145 # don't want to try to place another reserve.
146 if ($borrowernumber_hold && !$action) {
147 my $patron = Koha
::Patrons
->find( $borrowernumber_hold );
150 # we check the reserves of the user, and if they can reserve a document
151 # FIXME At this time we have a simple count of reservs, but, later, we could improve the infos "title" ...
153 my $reserves_count = $patron->holds->count;
155 my $new_reserves_count = scalar( @biblionumbers );
157 my $maxreserves = C4
::Context
->preference('maxreserves');
159 && ( $reserves_count + $new_reserves_count > $maxreserves ) )
161 my $new_reserves_allowed =
162 $maxreserves - $reserves_count > 0
163 ?
$maxreserves - $reserves_count
166 $exceeded_maxreserves = 1;
168 new_reserves_allowed
=> $new_reserves_allowed,
169 new_reserves_count
=> $new_reserves_count,
170 reserves_count
=> $reserves_count,
171 maxreserves
=> $maxreserves,
175 # we check the date expiry of the borrower (only if there is an expiry date, otherwise, set to 1 (warn)
176 my $expiry_date = $patron->dateexpiry;
177 my $expiry = 0; # flag set if patron account has expired
178 if ($expiry_date and $expiry_date ne '0000-00-00' and
179 Date_to_Days
(split /-/,$date) > Date_to_Days
(split /-/,$expiry_date)) {
183 # check if the borrower make the reserv in a different branch
184 if ( $patron->branchcode ne C4
::Context
->userenv->{'branch'} ) {
188 my $amount_outstanding = $patron->account->balance;
192 diffbranch
=> $diffbranch,
193 messages
=> $messages,
194 warnings
=> $warnings,
195 amount_outstanding
=> $amount_outstanding,
199 $template->param( messageborrower
=> $messageborrower );
201 # FIXME launch another time GetMember perhaps until (Joubu: Why?)
202 my $patron = Koha
::Patrons
->find( $borrowernumber_hold );
204 my $logged_in_patron = Koha
::Patrons
->find( $borrowernumber );
206 my $itemdata_enumchron = 0;
207 my $itemdata_ccode = 0;
209 foreach my $biblionumber (@biblionumbers) {
210 next unless $biblionumber =~ m
|^\d
+$|;
212 my %biblioloopiter = ();
214 my $biblio = Koha
::Biblios
->find( $biblionumber );
216 my $force_hold_level;
218 { # CanBookBeReserved
219 my $canReserve = CanBookBeReserved
( $patron->borrowernumber, $biblionumber );
220 $canReserve->{status
} //= '';
221 if ( $canReserve->{status
} eq 'OK' ) {
223 #All is OK and we can continue
225 elsif ( $canReserve->{status
} eq 'tooManyReserves' ) {
226 $exceeded_maxreserves = 1;
227 $template->param( maxreserves
=> $canReserve->{limit
} );
229 elsif ( $canReserve->{status
} eq 'tooManyHoldsForThisRecord' ) {
230 $exceeded_holds_per_record = 1;
231 $biblioloopiter{ $canReserve->{status
} } = 1;
233 elsif ( $canReserve->{status
} eq 'ageRestricted' ) {
234 $template->param( $canReserve->{status
} => 1 );
235 $biblioloopiter{ $canReserve->{status
} } = 1;
238 $biblioloopiter{ $canReserve->{status
} } = 1;
242 # For multiple holds per record, if a patron has previously placed a hold,
243 # the patron can only place more holds of the same type. That is, if the
244 # patron placed a record level hold, all the holds the patron places must
245 # be record level. If the patron placed an item level hold, all holds
246 # the patron places must be item level
247 my $holds = Koha
::Holds
->search(
249 borrowernumber
=> $patron->borrowernumber,
250 biblionumber
=> $biblionumber,
254 $force_hold_level = $holds->forced_hold_level();
255 $biblioloopiter{force_hold_level
} = $force_hold_level;
256 $template->param( force_hold_level
=> $force_hold_level );
258 # For a librarian to be able to place multiple record holds for a patron for a record,
259 # we must find out what the maximum number of holds they can place for the patron is
260 my $max_holds_for_record = GetMaxPatronHoldsForRecord
( $patron->borrowernumber, $biblionumber );
261 my $remaining_holds_for_record = $max_holds_for_record - $holds->count();
262 $biblioloopiter{remaining_holds_for_record
} = $max_holds_for_record;
263 $template->param( max_holds_for_record
=> $max_holds_for_record );
264 $template->param( remaining_holds_for_record
=> $remaining_holds_for_record );
266 { # alreadypossession
267 # Check to see if patron is allowed to place holds on records where the
268 # patron already has an item from that record checked out
269 if ( !C4
::Context
->preference('AllowHoldsOnPatronsPossessions')
270 && CheckIfIssuedToPatron
( $patron->borrowernumber, $biblionumber ) )
272 $template->param( alreadypossession
=> 1, );
278 my $count = Koha
::Holds
->search( { biblionumber
=> $biblionumber } )->count();
279 my $totalcount = $count;
281 # FIXME think @optionloop, is maybe obsolete, or must be switchable by a systeme preference fixed rank or not
282 # make priorities options
285 for ( 1 .. $count + 1 ) {
290 selected
=> ( $_ == $count + 1 ),
294 # adding a fixed value for priority options
295 my $fixedRank = $count+1;
297 my %itemnumbers_of_biblioitem;
299 my @hostitems = get_hostitemnumbers_of
($biblionumber);
302 $template->param('hostitemsflag' => 1);
303 push(@itemnumbers, @hostitems);
306 my $items = Koha
::Items
->search({ -or => { biblionumber
=> $biblionumber, itemnumber
=> { in => \
@itemnumbers } } });
308 unless ( $items->count ) {
309 # FIXME Then why do we continue?
310 $template->param('noitems' => 1);
311 $biblioloopiter{noitems
} = 1;
314 ## Here we go backwards again to create hash of biblioitemnumber to itemnumbers,
315 ## when by definition all of the itemnumber have the same biblioitemnumber
316 my ( $iteminfos_of );
317 while ( my $item = $items->next ) {
318 $item = $item->unblessed;
319 my $biblioitemnumber = $item->{biblioitemnumber
};
320 my $itemnumber = $item->{itemnumber
};
321 push( @
{ $itemnumbers_of_biblioitem{$biblioitemnumber} }, $itemnumber );
322 $iteminfos_of->{$itemnumber} = $item;
325 ## Should be same as biblionumber
326 my @biblioitemnumbers = keys %itemnumbers_of_biblioitem;
328 my $biblioiteminfos_of = {
331 ( $biblioitem->{biblioitemnumber
} => $biblioitem )
332 } @
{ Koha
::Biblioitems
->search(
333 { biblioitemnumber
=> { -in => \
@biblioitemnumbers } },
334 { select => ['biblioitemnumber', 'publicationyear', 'itemtype']}
339 my $frameworkcode = GetFrameworkCode
( $biblionumber );
340 my @notforloan_avs = Koha
::AuthorisedValues
->search_by_koha_field({ kohafield
=> 'items.notforloan', frameworkcode
=> $frameworkcode });
341 my $notforloan_label_of = { map { $_->authorised_value => $_->lib } @notforloan_avs };
345 my @available_itemtypes;
346 foreach my $biblioitemnumber (@biblioitemnumbers) {
347 my $biblioitem = $biblioiteminfos_of->{$biblioitemnumber};
348 my $num_available = 0;
349 my $num_override = 0;
352 $biblioitem->{force_hold_level
} = $force_hold_level;
354 if ( $biblioitem->{biblioitemnumber
} ne $biblionumber ) {
355 $biblioitem->{hostitemsflag
} = 1;
358 $biblioloopiter{description
} = $biblioitem->{description
};
359 $biblioloopiter{itypename
} = $biblioitem->{description
};
360 if ( $biblioitem->{itemtype
} ) {
362 $biblioitem->{description
} =
363 $itemtypes->{ $biblioitem->{itemtype
} }{description
};
365 $biblioloopiter{imageurl
} =
366 getitemtypeimagelocation
( 'intranet',
367 $itemtypes->{ $biblioitem->{itemtype
} }{imageurl
} );
370 foreach my $itemnumber ( @
{ $itemnumbers_of_biblioitem{$biblioitemnumber} } ) {
371 my $item = $iteminfos_of->{$itemnumber};
373 $item->{force_hold_level
} = $force_hold_level;
375 unless (C4
::Context
->preference('item-level_itypes')) {
376 $item->{itype
} = $biblioitem->{itemtype
};
379 $item->{itypename
} = $itemtypes->{ $item->{itype
} }{description
};
380 $item->{imageurl
} = getitemtypeimagelocation
( 'intranet', $itemtypes->{ $item->{itype
} }{imageurl
} );
381 $item->{homebranch
} = $item->{homebranch
};
383 # if the holdingbranch is different than the homebranch, we show the
384 # holdingbranch of the document too
385 if ( $item->{homebranch
} ne $item->{holdingbranch
} ) {
386 $item->{holdingbranch
} = $item->{holdingbranch
};
389 if($item->{biblionumber
} ne $biblionumber){
390 $item->{hostitemsflag
} = 1;
391 $item->{hosttitle
} = Koha
::Biblios
->find( $item->{biblionumber
} )->title;
394 # if the item is currently on loan, we display its return date and
395 # change the background color
396 my $issue = Koha
::Checkouts
->find( { itemnumber
=> $itemnumber } );
398 $item->{date_due
} = $issue->date_due;
399 $item->{backgroundcolor
} = 'onloan';
403 my $item_object = Koha
::Items
->find( $itemnumber );
404 my $holds = $item_object->current_holds;
405 if ( my $first_hold = $holds->next ) {
406 my $p = Koha
::Patrons
->find( $first_hold->borrowernumber );
408 $item->{backgroundcolor
} = 'reserved';
409 $item->{reservedate
} = output_pref
({ dt
=> dt_from_string
( $first_hold->reservedate ), dateonly
=> 1 }); # FIXME Should be formatted in the template
410 $item->{ReservedFor
} = $p;
411 $item->{ExpectedAtLibrary
} = $first_hold->branchcode;
412 $item->{waitingdate
} = $first_hold->waitingdate;
415 # Management of the notforloan document
416 if ( $item->{notforloan
} ) {
417 $item->{backgroundcolor
} = 'other';
418 $item->{notforloanvalue
} =
419 $notforloan_label_of->{ $item->{notforloan
} };
422 # Management of lost or long overdue items
423 if ( $item->{itemlost
} ) {
425 # FIXME localized strings should never be in Perl code
427 $item->{itemlost
} == 1 ?
"(lost)"
428 : $item->{itemlost
} == 2 ?
"(long overdue)"
430 $item->{backgroundcolor
} = 'other';
431 if ($logged_in_patron->category->hidelostitems && !$showallitems) {
437 # Check the transit status
438 my ( $transfertwhen, $transfertfrom, $transfertto ) =
439 GetTransfers
($itemnumber);
441 if ( defined $transfertwhen && $transfertwhen ne '' ) {
442 $item->{transfertwhen
} = output_pref
({ dt
=> dt_from_string
( $transfertwhen ), dateonly
=> 1 });
443 $item->{transfertfrom
} = $transfertfrom;
444 $item->{transfertto
} = $transfertto;
445 $item->{nocancel
} = 1;
448 # If there is no loan, return and transfer, we show a checkbox.
449 $item->{notforloan
} ||= 0;
451 # if independent branches is on we need to check if the person can reserve
452 # for branches they arent logged in to
453 if ( C4
::Context
->preference("IndependentBranches") ) {
454 if (! C4
::Context
->preference("canreservefromotherbranches")){
455 # can't reserve items so need to check if item homebranch and userenv branch match if not we can't reserve
456 my $userenv = C4
::Context
->userenv;
457 unless ( C4
::Context
->IsSuperLibrarian ) {
458 $item->{cantreserve
} = 1 if ( $item->{homebranch
} ne $userenv->{branch
} );
464 my $patron_unblessed = $patron->unblessed;
465 my $branch = C4
::Circulation
::_GetCircControlBranch
($item, $patron_unblessed);
467 my $branchitemrule = GetBranchItemRule
( $branch, $item->{'itype'} );
469 $item->{'holdallowed'} = $branchitemrule->{'holdallowed'};
471 my $can_item_be_reserved = CanItemBeReserved
( $patron->borrowernumber, $itemnumber );
472 $item->{not_holdable
} = $can_item_be_reserved->{status
} unless ( $can_item_be_reserved->{status
} eq 'OK' );
474 $item->{item_level_holds
} = Koha
::IssuingRules
->get_opacitemholds_policy( { item
=> $item_object, patron
=> $patron } );
477 !$item->{cantreserve
}
478 && !$exceeded_maxreserves
479 && IsAvailableForItemLevelRequest
($item, $patron_unblessed)
480 && $can_item_be_reserved->{status
} eq 'OK'
483 $item->{available
} = 1;
486 push( @available_itemtypes, $item->{itype
} );
488 elsif ( $can_item_be_reserved->{status
} eq 'tooManyReserves' && C4
::Context
->preference('AllowHoldPolicyOverride') ) {
489 # If AllowHoldPolicyOverride is set, it should override EVERY restriction, not just branch item rules
490 $item->{override
} = 1;
493 push( @available_itemtypes, $item->{itype
} );
496 # If none of the conditions hold true, then neither override nor available is set and the item cannot be checked
498 # Show serial enumeration when needed
499 if ($item->{enumchron
}) {
500 $itemdata_enumchron = 1;
502 # Show collection when needed
503 if ($item->{ccode
}) {
508 push @
{ $biblioitem->{itemloop
} }, $item;
511 if ( $num_override == scalar( @
{ $biblioitem->{itemloop
} } ) ) { # That is, if all items require an override
512 $template->param( override_required
=> 1 );
513 } elsif ( $num_available == 0 ) {
514 $template->param( none_available
=> 1 );
515 $biblioloopiter{warn} = 1;
516 $biblioloopiter{none_avail
} = 1;
518 $template->param( hiddencount
=> $hiddencount);
520 push @bibitemloop, $biblioitem;
523 @available_itemtypes = uniq
( @available_itemtypes );
524 $template->param( available_itemtypes
=> \
@available_itemtypes );
526 # existingreserves building
528 my @reserves = Koha
::Holds
->search( { biblionumber
=> $biblionumber }, { order_by
=> 'priority' } );
531 my $a_found = $a->found() || '';
532 my $b_found = $a->found() || '';
533 $a_found cmp $b_found;
537 my $priority = $res->priority();
540 for ( my $i = 1 ; $i <= $totalcount ; $i++ ) {
545 selected
=> ( $i == $priority ),
550 if ( $res->is_found() ) {
551 $reserve{'holdingbranch'} = $res->item()->holdingbranch();
552 $reserve{'biblionumber'} = $res->item()->biblionumber();
553 $reserve{'barcodenumber'} = $res->item()->barcode();
554 $reserve{'wbrcode'} = $res->branchcode();
555 $reserve{'itemnumber'} = $res->itemnumber();
556 $reserve{'wbrname'} = $res->branch()->branchname();
558 if ( $reserve{'holdingbranch'} eq $reserve{'wbrcode'} ) {
560 # Just because the holdingbranch matches the reserve branch doesn't mean the item
561 # has arrived at the destination, check for an open transfer for the item as well
562 my ( $transfertwhen, $transfertfrom, $transferto ) =
563 C4
::Circulation
::GetTransfers
( $res->itemnumber() );
564 if ( not $transferto or $transferto ne $res->branchcode() ) {
565 $reserve{'atdestination'} = 1;
569 # set found to 1 if reserve is waiting for patron pickup
570 $reserve{'found'} = $res->is_found();
571 $reserve{'intransit'} = $res->is_in_transit();
573 elsif ( $res->priority() > 0 ) {
574 if ( my $item = $res->item() ) {
575 $reserve{'itemnumber'} = $item->id();
576 $reserve{'barcodenumber'} = $item->barcode();
577 $reserve{'item_level_hold'} = 1;
581 $reserve{'expirationdate'} = output_pref
( { dt
=> dt_from_string
( $res->expirationdate ), dateonly
=> 1 } )
582 unless ( !defined( $res->expirationdate ) || $res->expirationdate eq '0000-00-00' );
583 $reserve{'date'} = output_pref
( { dt
=> dt_from_string
( $res->reservedate ), dateonly
=> 1 } );
584 $reserve{'borrowernumber'} = $res->borrowernumber();
585 $reserve{'biblionumber'} = $res->biblionumber();
586 $reserve{'patron'} = $res->borrower;
587 $reserve{'notes'} = $res->reservenotes();
588 $reserve{'waiting_date'} = $res->waitingdate();
589 $reserve{'ccode'} = $res->item() ?
$res->item()->ccode() : undef;
590 $reserve{'barcode'} = $res->item() ?
$res->item()->barcode() : undef;
591 $reserve{'priority'} = $res->priority();
592 $reserve{'lowestPriority'} = $res->lowestPriority();
593 $reserve{'optionloop'} = \
@optionloop;
594 $reserve{'suspend'} = $res->suspend();
595 $reserve{'suspend_until'} = $res->suspend_until();
596 $reserve{'reserve_id'} = $res->reserve_id();
597 $reserve{itemtype
} = $res->itemtype();
598 $reserve{branchcode
} = $res->branchcode();
599 $reserve{object
} = $res;
601 push( @reserveloop, \
%reserve );
604 # get the time for the form name...
609 fixedRank
=> $fixedRank,
614 optionloop
=> \
@optionloop,
615 bibitemloop
=> \
@bibitemloop,
616 itemdata_enumchron
=> $itemdata_enumchron,
617 itemdata_ccode
=> $itemdata_ccode,
619 biblionumber
=> $biblionumber,
620 findborrower
=> $findborrower,
621 title
=> $biblio->title,
622 author
=> $biblio->author,
624 C4
::Search
::enabled_staff_search_views
,
627 $biblioloopiter{biblionumber
} = $biblionumber;
628 $biblioloopiter{title
} = $biblio->title;
629 $biblioloopiter{rank
} = $fixedRank;
630 $biblioloopiter{reserveloop
} = \
@reserveloop;
633 $template->param( reserveloop
=> \
@reserveloop );
636 push @biblioloop, \
%biblioloopiter;
639 $template->param( biblioloop
=> \
@biblioloop );
640 $template->param( biblionumbers
=> $biblionumbers );
641 $template->param( exceeded_maxreserves
=> $exceeded_maxreserves );
642 $template->param( exceeded_holds_per_record
=> $exceeded_holds_per_record );
643 $template->param( subscriptionsnumber
=> CountSubscriptionFromBiblionumber
($biblionumber));
645 if ( C4
::Context
->preference( 'AllowHoldDateInFuture' ) ) {
646 $template->param( reserve_in_future
=> 1 );
650 SuspendHoldsIntranet
=> C4
::Context
->preference('SuspendHoldsIntranet'),
651 AutoResumeSuspendedHolds
=> C4
::Context
->preference('AutoResumeSuspendedHolds'),
655 output_html_with_http_headers
$input, $cookie, $template->output;
657 sub sort_borrowerlist
{
658 my $borrowerslist = shift;
661 uc( $a->{surname
} . $a->{firstname
} ) cmp
662 uc( $b->{surname
} . $b->{firstname
} )