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 AlterPriority
( $where, $reserve_id );
91 } elsif ( $action eq 'cancel' ) {
92 my $reserve_id = $input->param('reserve_id');
93 my $hold = Koha
::Holds
->find( $reserve_id );
94 $hold->cancel if $hold;
95 } elsif ( $action eq 'setLowestPriority' ) {
96 my $reserve_id = $input->param('reserve_id');
97 ToggleLowestPriority
( $reserve_id );
98 } elsif ( $action eq 'toggleSuspend' ) {
99 my $reserve_id = $input->param('reserve_id');
100 my $suspend_until = $input->param('suspend_until');
101 ToggleSuspend
( $reserve_id, $suspend_until );
105 my $patron = Koha
::Patrons
->find( { cardnumber
=> $findborrower } );
107 $borrowernumber_hold = $patron->borrowernumber;
109 my $dt_params = { iDisplayLength
=> -1 };
110 my $results = C4
::Utils
::DataTables
::Members
::search
(
112 searchmember
=> $findborrower,
113 dt_params
=> $dt_params,
116 my $borrowers = $results->{patrons
};
117 if ( scalar @
$borrowers == 1 ) {
118 $borrowernumber_hold = $borrowers->[0]->{borrowernumber
};
119 } elsif ( @
$borrowers ) {
120 $template->param( borrowers
=> $borrowers );
122 $messageborrower = "'$findborrower'";
127 my @biblionumbers = ();
128 my $biblionumber = $input->param('biblionumber');
129 my $biblionumbers = $input->param('biblionumbers');
130 if ( $biblionumbers ) {
131 @biblionumbers = split '/', $biblionumbers;
133 push @biblionumbers, $input->multi_param('biblionumber');
136 # FIXME multi_hold should not be a variable but depends on the number of elements in @biblionumbers
137 $template->param(multi_hold
=> scalar $input->param('multi_hold'));
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 $patron = Koha
::Patrons
->find( $borrowernumber_hold );
145 # we check the reserves of the user, and if they can reserve a document
146 # FIXME At this time we have a simple count of reservs, but, later, we could improve the infos "title" ...
148 my $reserves_count = $patron->holds->count;
150 my $new_reserves_count = scalar( @biblionumbers );
152 my $maxreserves = C4
::Context
->preference('maxreserves');
154 && ( $reserves_count + $new_reserves_count > $maxreserves ) )
156 my $new_reserves_allowed =
157 $maxreserves - $reserves_count > 0
158 ?
$maxreserves - $reserves_count
161 $exceeded_maxreserves = 1;
163 new_reserves_allowed
=> $new_reserves_allowed,
164 new_reserves_count
=> $new_reserves_count,
165 reserves_count
=> $reserves_count,
166 maxreserves
=> $maxreserves,
170 # we check the date expiry of the borrower (only if there is an expiry date, otherwise, set to 1 (warn)
171 my $expiry_date = $patron->dateexpiry;
172 my $expiry = 0; # flag set if patron account has expired
173 if ($expiry_date and $expiry_date ne '0000-00-00' and
174 Date_to_Days
(split /-/,$date) > Date_to_Days
(split /-/,$expiry_date)) {
178 # check if the borrower make the reserv in a different branch
179 if ( $patron->branchcode ne C4
::Context
->userenv->{'branch'} ) {
183 my $amount_outstanding = $patron->account->balance;
187 diffbranch
=> $diffbranch,
188 messages
=> $messages,
189 warnings
=> $warnings,
190 amount_outstanding
=> $amount_outstanding,
194 $template->param( messageborrower
=> $messageborrower );
196 # FIXME launch another time GetMember perhaps until (Joubu: Why?)
197 my $patron = Koha
::Patrons
->find( $borrowernumber_hold );
199 my $logged_in_patron = Koha
::Patrons
->find( $borrowernumber );
201 my $itemdata_enumchron = 0;
203 foreach my $biblionumber (@biblionumbers) {
204 next unless $biblionumber =~ m
|^\d
+$|;
206 my %biblioloopiter = ();
208 my $biblio = Koha
::Biblios
->find( $biblionumber );
210 my $force_hold_level;
212 { # CanBookBeReserved
213 my $canReserve = CanBookBeReserved
( $patron->borrowernumber, $biblionumber );
214 $canReserve->{status
} //= '';
215 if ( $canReserve->{status
} eq 'OK' ) {
217 #All is OK and we can continue
219 elsif ( $canReserve->{status
} eq 'tooManyReserves' ) {
220 $exceeded_maxreserves = 1;
221 $template->param( maxreserves
=> $canReserve->{limit
} );
223 elsif ( $canReserve->{status
} eq 'tooManyHoldsForThisRecord' ) {
224 $exceeded_holds_per_record = 1;
225 $biblioloopiter{ $canReserve->{status
} } = 1;
227 elsif ( $canReserve->{status
} eq 'ageRestricted' ) {
228 $template->param( $canReserve->{status
} => 1 );
229 $biblioloopiter{ $canReserve->{status
} } = 1;
232 $biblioloopiter{ $canReserve->{status
} } = 1;
236 # For multiple holds per record, if a patron has previously placed a hold,
237 # the patron can only place more holds of the same type. That is, if the
238 # patron placed a record level hold, all the holds the patron places must
239 # be record level. If the patron placed an item level hold, all holds
240 # the patron places must be item level
241 my $holds = Koha
::Holds
->search(
243 borrowernumber
=> $patron->borrowernumber,
244 biblionumber
=> $biblionumber,
248 $force_hold_level = $holds->forced_hold_level();
249 $biblioloopiter{force_hold_level
} = $force_hold_level;
250 $template->param( force_hold_level
=> $force_hold_level );
252 # For a librarian to be able to place multiple record holds for a patron for a record,
253 # we must find out what the maximum number of holds they can place for the patron is
254 my $max_holds_for_record = GetMaxPatronHoldsForRecord
( $patron->borrowernumber, $biblionumber );
255 my $remaining_holds_for_record = $max_holds_for_record - $holds->count();
256 $biblioloopiter{remaining_holds_for_record
} = $max_holds_for_record;
257 $template->param( max_holds_for_record
=> $max_holds_for_record );
258 $template->param( remaining_holds_for_record
=> $remaining_holds_for_record );
260 { # alreadypossession
261 # Check to see if patron is allowed to place holds on records where the
262 # patron already has an item from that record checked out
263 if ( !C4
::Context
->preference('AllowHoldsOnPatronsPossessions')
264 && CheckIfIssuedToPatron
( $patron->borrowernumber, $biblionumber ) )
266 $template->param( alreadypossession
=> 1, );
272 my $count = Koha
::Holds
->search( { biblionumber
=> $biblionumber } )->count();
273 my $totalcount = $count;
275 # FIXME think @optionloop, is maybe obsolete, or must be switchable by a systeme preference fixed rank or not
276 # make priorities options
279 for ( 1 .. $count + 1 ) {
284 selected
=> ( $_ == $count + 1 ),
288 # adding a fixed value for priority options
289 my $fixedRank = $count+1;
291 my %itemnumbers_of_biblioitem;
293 my @hostitems = get_hostitemnumbers_of
($biblionumber);
296 $template->param('hostitemsflag' => 1);
297 push(@itemnumbers, @hostitems);
300 my $items = Koha
::Items
->search({ -or => { biblionumber
=> $biblionumber, itemnumber
=> { in => \
@itemnumbers } } });
302 unless ( $items->count ) {
303 # FIXME Then why do we continue?
304 $template->param('noitems' => 1);
305 $biblioloopiter{noitems
} = 1;
308 ## Here we go backwards again to create hash of biblioitemnumber to itemnumbers,
309 ## when by definition all of the itemnumber have the same biblioitemnumber
310 my ( $iteminfos_of );
311 while ( my $item = $items->next ) {
312 $item = $item->unblessed;
313 my $biblioitemnumber = $item->{biblioitemnumber
};
314 my $itemnumber = $item->{itemnumber
};
315 push( @
{ $itemnumbers_of_biblioitem{$biblioitemnumber} }, $itemnumber );
316 $iteminfos_of->{$itemnumber} = $item;
319 ## Should be same as biblionumber
320 my @biblioitemnumbers = keys %itemnumbers_of_biblioitem;
322 my $biblioiteminfos_of = {
325 ( $biblioitem->{biblioitemnumber
} => $biblioitem )
326 } @
{ Koha
::Biblioitems
->search(
327 { biblioitemnumber
=> { -in => \
@biblioitemnumbers } },
328 { select => ['biblioitemnumber', 'publicationyear', 'itemtype']}
333 my $frameworkcode = GetFrameworkCode
( $biblionumber );
334 my @notforloan_avs = Koha
::AuthorisedValues
->search_by_koha_field({ kohafield
=> 'items.notforloan', frameworkcode
=> $frameworkcode });
335 my $notforloan_label_of = { map { $_->authorised_value => $_->lib } @notforloan_avs };
339 my @available_itemtypes;
340 foreach my $biblioitemnumber (@biblioitemnumbers) {
341 my $biblioitem = $biblioiteminfos_of->{$biblioitemnumber};
342 my $num_available = 0;
343 my $num_override = 0;
346 $biblioitem->{force_hold_level
} = $force_hold_level;
348 if ( $biblioitem->{biblioitemnumber
} ne $biblionumber ) {
349 $biblioitem->{hostitemsflag
} = 1;
352 $biblioloopiter{description
} = $biblioitem->{description
};
353 $biblioloopiter{itypename
} = $biblioitem->{description
};
354 if ( $biblioitem->{itemtype
} ) {
356 $biblioitem->{description
} =
357 $itemtypes->{ $biblioitem->{itemtype
} }{description
};
359 $biblioloopiter{imageurl
} =
360 getitemtypeimagelocation
( 'intranet',
361 $itemtypes->{ $biblioitem->{itemtype
} }{imageurl
} );
364 foreach my $itemnumber ( @
{ $itemnumbers_of_biblioitem{$biblioitemnumber} } ) {
365 my $item = $iteminfos_of->{$itemnumber};
367 $item->{force_hold_level
} = $force_hold_level;
369 unless (C4
::Context
->preference('item-level_itypes')) {
370 $item->{itype
} = $biblioitem->{itemtype
};
373 $item->{itypename
} = $itemtypes->{ $item->{itype
} }{description
};
374 $item->{imageurl
} = getitemtypeimagelocation
( 'intranet', $itemtypes->{ $item->{itype
} }{imageurl
} );
375 $item->{homebranch
} = $item->{homebranch
};
377 # if the holdingbranch is different than the homebranch, we show the
378 # holdingbranch of the document too
379 if ( $item->{homebranch
} ne $item->{holdingbranch
} ) {
380 $item->{holdingbranch
} = $item->{holdingbranch
};
383 if($item->{biblionumber
} ne $biblionumber){
384 $item->{hostitemsflag
} = 1;
385 $item->{hosttitle
} = Koha
::Biblios
->find( $item->{biblionumber
} )->title;
388 # if the item is currently on loan, we display its return date and
389 # change the background color
390 my $issue = Koha
::Checkouts
->find( { itemnumber
=> $itemnumber } );
392 $item->{date_due
} = $issue->date_due;
393 $item->{backgroundcolor
} = 'onloan';
397 my $item_object = Koha
::Items
->find( $itemnumber );
398 my $holds = $item_object->current_holds;
399 if ( my $first_hold = $holds->next ) {
400 my $p = Koha
::Patrons
->find( $first_hold->borrowernumber );
402 $item->{backgroundcolor
} = 'reserved';
403 $item->{reservedate
} = output_pref
({ dt
=> dt_from_string
( $first_hold->reservedate ), dateonly
=> 1 }); # FIXME Should be formatted in the template
404 $item->{ReservedFor
} = $p;
405 $item->{ExpectedAtLibrary
} = $first_hold->branchcode;
406 $item->{waitingdate
} = $first_hold->waitingdate;
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 ($logged_in_patron->category->hidelostitems && !$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
} = $transfertfrom;
438 $item->{transfertto
} = $transfertto;
439 $item->{nocancel
} = 1;
442 # If there is no loan, return and transfer, we show a checkbox.
443 $item->{notforloan
} ||= 0;
445 # if independent branches is on we need to check if the person can reserve
446 # for branches they arent logged in to
447 if ( C4
::Context
->preference("IndependentBranches") ) {
448 if (! C4
::Context
->preference("canreservefromotherbranches")){
449 # cant reserve items so need to check if item homebranch and userenv branch match if not we cant reserve
450 my $userenv = C4
::Context
->userenv;
451 unless ( C4
::Context
->IsSuperLibrarian ) {
452 $item->{cantreserve
} = 1 if ( $item->{homebranch
} ne $userenv->{branch
} );
458 my $patron_unblessed = $patron->unblessed;
459 my $branch = C4
::Circulation
::_GetCircControlBranch
($item, $patron_unblessed);
461 my $branchitemrule = GetBranchItemRule
( $branch, $item->{'itype'} );
463 $item->{'holdallowed'} = $branchitemrule->{'holdallowed'};
465 my $can_item_be_reserved = CanItemBeReserved
( $patron->borrowernumber, $itemnumber );
466 $item->{not_holdable
} = $can_item_be_reserved->{status
} unless ( $can_item_be_reserved->{status
} eq 'OK' );
468 $item->{item_level_holds
} = Koha
::IssuingRules
->get_opacitemholds_policy( { item
=> $item_object, patron
=> $patron } );
471 !$item->{cantreserve
}
472 && !$exceeded_maxreserves
473 && IsAvailableForItemLevelRequest
($item, $patron_unblessed)
474 && $can_item_be_reserved->{status
} eq 'OK'
477 $item->{available
} = 1;
480 push( @available_itemtypes, $item->{itype
} );
482 elsif ( $can_item_be_reserved->{status
} eq 'tooManyReserves' && C4
::Context
->preference('AllowHoldPolicyOverride') ) {
483 # If AllowHoldPolicyOverride is set, it should override EVERY restriction, not just branch item rules
484 $item->{override
} = 1;
487 push( @available_itemtypes, $item->{itype
} );
490 # If none of the conditions hold true, then neither override nor available is set and the item cannot be checked
492 # Show serial enumeration when needed
493 if ($item->{enumchron
}) {
494 $itemdata_enumchron = 1;
498 push @
{ $biblioitem->{itemloop
} }, $item;
501 if ( $num_override == scalar( @
{ $biblioitem->{itemloop
} } ) ) { # That is, if all items require an override
502 $template->param( override_required
=> 1 );
503 } elsif ( $num_available == 0 ) {
504 $template->param( none_available
=> 1 );
505 $biblioloopiter{warn} = 1;
506 $biblioloopiter{none_avail
} = 1;
508 $template->param( hiddencount
=> $hiddencount);
510 push @bibitemloop, $biblioitem;
513 @available_itemtypes = uniq
( @available_itemtypes );
514 $template->param( available_itemtypes
=> \
@available_itemtypes );
516 # existingreserves building
518 my @reserves = Koha
::Holds
->search( { biblionumber
=> $biblionumber }, { order_by
=> 'priority' } );
521 my $a_found = $a->found() || '';
522 my $b_found = $a->found() || '';
523 $a_found cmp $b_found;
527 my $priority = $res->priority();
530 for ( my $i = 1 ; $i <= $totalcount ; $i++ ) {
535 selected
=> ( $i == $priority ),
540 if ( $res->is_found() ) {
541 $reserve{'holdingbranch'} = $res->item()->holdingbranch();
542 $reserve{'biblionumber'} = $res->item()->biblionumber();
543 $reserve{'barcodenumber'} = $res->item()->barcode();
544 $reserve{'wbrcode'} = $res->branchcode();
545 $reserve{'itemnumber'} = $res->itemnumber();
546 $reserve{'wbrname'} = $res->branch()->branchname();
548 if ( $reserve{'holdingbranch'} eq $reserve{'wbrcode'} ) {
550 # Just because the holdingbranch matches the reserve branch doesn't mean the item
551 # has arrived at the destination, check for an open transfer for the item as well
552 my ( $transfertwhen, $transfertfrom, $transferto ) =
553 C4
::Circulation
::GetTransfers
( $res->itemnumber() );
554 if ( not $transferto or $transferto ne $res->branchcode() ) {
555 $reserve{'atdestination'} = 1;
559 # set found to 1 if reserve is waiting for patron pickup
560 $reserve{'found'} = $res->is_found();
561 $reserve{'intransit'} = $res->is_in_transit();
563 elsif ( $res->priority() > 0 ) {
564 if ( my $item = $res->item() ) {
565 $reserve{'itemnumber'} = $item->id();
566 $reserve{'barcodenumber'} = $item->barcode();
567 $reserve{'item_level_hold'} = 1;
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{'patron'} = $res->borrower;
577 $reserve{'notes'} = $res->reservenotes();
578 $reserve{'waiting_date'} = $res->waitingdate();
579 $reserve{'ccode'} = $res->item() ?
$res->item()->ccode() : undef;
580 $reserve{'barcode'} = $res->item() ?
$res->item()->barcode() : undef;
581 $reserve{'priority'} = $res->priority();
582 $reserve{'lowestPriority'} = $res->lowestPriority();
583 $reserve{'optionloop'} = \
@optionloop;
584 $reserve{'suspend'} = $res->suspend();
585 $reserve{'suspend_until'} = $res->suspend_until();
586 $reserve{'reserve_id'} = $res->reserve_id();
587 $reserve{itemtype
} = $res->itemtype();
588 $reserve{branchcode
} = $res->branchcode();
590 push( @reserveloop, \
%reserve );
593 # get the time for the form name...
598 fixedRank
=> $fixedRank,
603 optionloop
=> \
@optionloop,
604 bibitemloop
=> \
@bibitemloop,
605 itemdata_enumchron
=> $itemdata_enumchron,
607 biblionumber
=> $biblionumber,
608 findborrower
=> $findborrower,
609 title
=> $biblio->title,
610 author
=> $biblio->author,
612 C4
::Search
::enabled_staff_search_views
,
615 $biblioloopiter{biblionumber
} = $biblionumber;
616 $biblioloopiter{title
} = $biblio->title;
617 $biblioloopiter{rank
} = $fixedRank;
618 $biblioloopiter{reserveloop
} = \
@reserveloop;
621 $template->param( reserveloop
=> \
@reserveloop );
624 push @biblioloop, \
%biblioloopiter;
627 $template->param( biblioloop
=> \
@biblioloop );
628 $template->param( biblionumbers
=> $biblionumbers );
629 $template->param( exceeded_maxreserves
=> $exceeded_maxreserves );
630 $template->param( exceeded_holds_per_record
=> $exceeded_holds_per_record );
632 if ( C4
::Context
->preference( 'AllowHoldDateInFuture' ) ) {
633 $template->param( reserve_in_future
=> 1 );
637 SuspendHoldsIntranet
=> C4
::Context
->preference('SuspendHoldsIntranet'),
638 AutoResumeSuspendedHolds
=> C4
::Context
->preference('AutoResumeSuspendedHolds'),
642 output_html_with_http_headers
$input, $cookie, $template->output;
644 sub sort_borrowerlist
{
645 my $borrowerslist = shift;
648 uc( $a->{surname
} . $a->{firstname
} ) cmp
649 uc( $b->{surname
} . $b->{firstname
} )