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 $multihold = $input->param('multi_hold');
69 $template->param(multi_hold
=> $multihold);
70 my $showallitems = $input->param('showallitems');
72 my $itemtypes = { map { $_->{itemtype
} => $_ } @
{ Koha
::ItemTypes
->search_with_localization->unblessed } };
74 # Select borrowers infos
75 my $findborrower = $input->param('findborrower');
76 $findborrower = '' unless defined $findborrower;
77 $findborrower =~ s
|,| |g
;
78 my $borrowernumber_hold = $input->param('borrowernumber') || '';
82 my $exceeded_maxreserves;
83 my $exceeded_holds_per_record;
85 my $date = output_pref
({ dt
=> dt_from_string
, dateformat
=> 'iso', dateonly
=> 1 });
86 my $action = $input->param('action');
89 if ( $action eq 'move' ) {
90 my $where = $input->param('where');
91 my $reserve_id = $input->param('reserve_id');
92 AlterPriority
( $where, $reserve_id );
93 } elsif ( $action eq 'cancel' ) {
94 my $reserve_id = $input->param('reserve_id');
95 my $hold = Koha
::Holds
->find( $reserve_id );
96 $hold->cancel if $hold;
97 } elsif ( $action eq 'setLowestPriority' ) {
98 my $reserve_id = $input->param('reserve_id');
99 ToggleLowestPriority
( $reserve_id );
100 } elsif ( $action eq 'toggleSuspend' ) {
101 my $reserve_id = $input->param('reserve_id');
102 my $suspend_until = $input->param('suspend_until');
103 ToggleSuspend
( $reserve_id, $suspend_until );
107 my $patron = Koha
::Patrons
->find( { cardnumber
=> $findborrower } );
109 $borrowernumber_hold = $patron->borrowernumber;
111 my $dt_params = { iDisplayLength
=> -1 };
112 my $results = C4
::Utils
::DataTables
::Members
::search
(
114 searchmember
=> $findborrower,
115 dt_params
=> $dt_params,
118 my $borrowers = $results->{patrons
};
119 if ( scalar @
$borrowers == 1 ) {
120 $borrowernumber_hold = $borrowers->[0]->{borrowernumber
};
121 } elsif ( @
$borrowers ) {
122 $template->param( borrowers
=> $borrowers );
124 $messageborrower = "'$findborrower'";
129 my @biblionumbers = ();
130 my $biblionumbers = $input->param('biblionumbers');
132 @biblionumbers = split '/', $biblionumbers;
134 push @biblionumbers, $input->multi_param('biblionumber');
138 # If we have the borrowernumber because we've performed an action, then we
139 # don't want to try to place another reserve.
140 if ($borrowernumber_hold && !$action) {
141 my $patron = Koha
::Patrons
->find( $borrowernumber_hold );
144 # we check the reserves of the user, and if they can reserve a document
145 # FIXME At this time we have a simple count of reservs, but, later, we could improve the infos "title" ...
147 my $reserves_count = $patron->holds->count;
149 my $new_reserves_count = scalar( @biblionumbers );
151 my $maxreserves = C4
::Context
->preference('maxreserves');
153 && ( $reserves_count + $new_reserves_count > $maxreserves ) )
155 my $new_reserves_allowed =
156 $maxreserves - $reserves_count > 0
157 ?
$maxreserves - $reserves_count
160 $exceeded_maxreserves = 1;
162 new_reserves_allowed
=> $new_reserves_allowed,
163 new_reserves_count
=> $new_reserves_count,
164 reserves_count
=> $reserves_count,
165 maxreserves
=> $maxreserves,
169 # we check the date expiry of the borrower (only if there is an expiry date, otherwise, set to 1 (warn)
170 my $expiry_date = $patron->dateexpiry;
171 my $expiry = 0; # flag set if patron account has expired
172 if ($expiry_date and $expiry_date ne '0000-00-00' and
173 Date_to_Days
(split /-/,$date) > Date_to_Days
(split /-/,$expiry_date)) {
177 # check if the borrower make the reserv in a different branch
178 if ( $patron->branchcode ne C4
::Context
->userenv->{'branch'} ) {
182 my $amount_outstanding = $patron->account->balance;
186 diffbranch
=> $diffbranch,
187 messages
=> $messages,
188 warnings
=> $warnings,
189 amount_outstanding
=> $amount_outstanding,
193 $template->param( messageborrower
=> $messageborrower );
195 # FIXME launch another time GetMember perhaps until (Joubu: Why?)
196 my $patron = Koha
::Patrons
->find( $borrowernumber_hold );
198 my $logged_in_patron = Koha
::Patrons
->find( $borrowernumber );
200 my $itemdata_enumchron = 0;
202 foreach my $biblionumber (@biblionumbers) {
203 next unless $biblionumber =~ m
|^\d
+$|;
205 my %biblioloopiter = ();
207 my $biblio = Koha
::Biblios
->find( $biblionumber );
209 my $force_hold_level;
211 { # CanBookBeReserved
212 my $canReserve = CanBookBeReserved
( $patron->borrowernumber, $biblionumber );
214 if ( $canReserve eq 'OK' ) {
216 #All is OK and we can continue
218 elsif ( $canReserve eq 'tooManyReserves' ) {
219 $exceeded_maxreserves = 1;
221 elsif ( $canReserve eq 'tooManyHoldsForThisRecord' ) {
222 $exceeded_holds_per_record = 1;
223 $biblioloopiter{$canReserve} = 1;
225 elsif ( $canReserve eq 'ageRestricted' ) {
226 $template->param( $canReserve => 1 );
227 $biblioloopiter{$canReserve} = 1;
230 $biblioloopiter{$canReserve} = 1;
234 # For multiple holds per record, if a patron has previously placed a hold,
235 # the patron can only place more holds of the same type. That is, if the
236 # patron placed a record level hold, all the holds the patron places must
237 # be record level. If the patron placed an item level hold, all holds
238 # the patron places must be item level
239 my $holds = Koha
::Holds
->search(
241 borrowernumber
=> $patron->borrowernumber,
242 biblionumber
=> $biblionumber,
246 $force_hold_level = $holds->forced_hold_level();
247 $biblioloopiter{force_hold_level
} = $force_hold_level;
248 $template->param( force_hold_level
=> $force_hold_level );
250 # For a librarian to be able to place multiple record holds for a patron for a record,
251 # we must find out what the maximum number of holds they can place for the patron is
252 my $max_holds_for_record = GetMaxPatronHoldsForRecord
( $patron->borrowernumber, $biblionumber );
253 my $remaining_holds_for_record = $max_holds_for_record - $holds->count();
254 $biblioloopiter{remaining_holds_for_record
} = $max_holds_for_record;
255 $template->param( max_holds_for_record
=> $max_holds_for_record );
256 $template->param( remaining_holds_for_record
=> $remaining_holds_for_record );
258 { # alreadypossession
259 # Check to see if patron is allowed to place holds on records where the
260 # patron already has an item from that record checked out
261 if ( !C4
::Context
->preference('AllowHoldsOnPatronsPossessions')
262 && CheckIfIssuedToPatron
( $patron->borrowernumber, $biblionumber ) )
264 $template->param( alreadypossession
=> 1, );
270 my $count = Koha
::Holds
->search( { biblionumber
=> $biblionumber } )->count();
271 my $totalcount = $count;
273 # FIXME think @optionloop, is maybe obsolete, or must be switchable by a systeme preference fixed rank or not
274 # make priorities options
277 for ( 1 .. $count + 1 ) {
282 selected
=> ( $_ == $count + 1 ),
286 # adding a fixed value for priority options
287 my $fixedRank = $count+1;
289 my %itemnumbers_of_biblioitem;
291 my @hostitems = get_hostitemnumbers_of
($biblionumber);
294 $template->param('hostitemsflag' => 1);
295 push(@itemnumbers, @hostitems);
298 my $items = Koha
::Items
->search({ -or => { biblionumber
=> $biblionumber, itemnumber
=> { in => \
@itemnumbers } } });
300 unless ( $items->count ) {
301 # FIXME Then why do we continue?
302 $template->param('noitems' => 1);
303 $biblioloopiter{noitems
} = 1;
306 ## Here we go backwards again to create hash of biblioitemnumber to itemnumbers,
307 ## when by definition all of the itemnumber have the same biblioitemnumber
308 my ( $iteminfos_of );
309 while ( my $item = $items->next ) {
310 $item = $item->unblessed;
311 my $biblioitemnumber = $item->{biblioitemnumber
};
312 my $itemnumber = $item->{itemnumber
};
313 push( @
{ $itemnumbers_of_biblioitem{$biblioitemnumber} }, $itemnumber );
314 $iteminfos_of->{$itemnumber} = $item;
317 ## Should be same as biblionumber
318 my @biblioitemnumbers = keys %itemnumbers_of_biblioitem;
320 my $biblioiteminfos_of = {
323 ( $biblioitem->{biblioitemnumber
} => $biblioitem )
324 } @
{ Koha
::Biblioitems
->search(
325 { biblioitemnumber
=> { -in => \
@biblioitemnumbers } },
326 { select => ['biblioitemnumber', 'publicationyear', 'itemtype']}
331 my $frameworkcode = GetFrameworkCode
( $biblionumber );
332 my @notforloan_avs = Koha
::AuthorisedValues
->search_by_koha_field({ kohafield
=> 'items.notforloan', frameworkcode
=> $frameworkcode });
333 my $notforloan_label_of = { map { $_->authorised_value => $_->lib } @notforloan_avs };
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->{homebranch
} = $item->{homebranch
};
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->{holdingbranch
} = $item->{holdingbranch
};
381 if($item->{biblionumber
} ne $biblionumber){
382 $item->{hostitemsflag
} = 1;
383 $item->{hosttitle
} = Koha
::Biblios
->find( $item->{biblionumber
} )->title;
386 # if the item is currently on loan, we display its return date and
387 # change the background color
388 my $issue = Koha
::Checkouts
->find( { itemnumber
=> $itemnumber } );
390 $item->{date_due
} = $issue->date_due;
391 $item->{backgroundcolor
} = 'onloan';
395 my $item_object = Koha
::Items
->find( $itemnumber );
396 my $holds = $item_object->current_holds;
397 if ( my $first_hold = $holds->next ) {
398 my $p = Koha
::Patrons
->find( $first_hold->borrowernumber );
400 $item->{backgroundcolor
} = 'reserved';
401 $item->{reservedate
} = output_pref
({ dt
=> dt_from_string
( $first_hold->reservedate ), dateonly
=> 1 }); # FIXME Should be formatted in the template
402 $item->{ReservedFor
} = $p;
403 $item->{ExpectedAtLibrary
} = $first_hold->branchcode;
404 $item->{waitingdate
} = $first_hold->waitingdate;
407 # Management of the notforloan document
408 if ( $item->{notforloan
} ) {
409 $item->{backgroundcolor
} = 'other';
410 $item->{notforloanvalue
} =
411 $notforloan_label_of->{ $item->{notforloan
} };
414 # Management of lost or long overdue items
415 if ( $item->{itemlost
} ) {
417 # FIXME localized strings should never be in Perl code
419 $item->{itemlost
} == 1 ?
"(lost)"
420 : $item->{itemlost
} == 2 ?
"(long overdue)"
422 $item->{backgroundcolor
} = 'other';
423 if ($logged_in_patron->category->hidelostitems && !$showallitems) {
429 # Check the transit status
430 my ( $transfertwhen, $transfertfrom, $transfertto ) =
431 GetTransfers
($itemnumber);
433 if ( defined $transfertwhen && $transfertwhen ne '' ) {
434 $item->{transfertwhen
} = output_pref
({ dt
=> dt_from_string
( $transfertwhen ), dateonly
=> 1 });
435 $item->{transfertfrom
} = $transfertfrom;
436 $item->{transfertto
} = $transfertto;
437 $item->{nocancel
} = 1;
440 # If there is no loan, return and transfer, we show a checkbox.
441 $item->{notforloan
} ||= 0;
443 # if independent branches is on we need to check if the person can reserve
444 # for branches they arent logged in to
445 if ( C4
::Context
->preference("IndependentBranches") ) {
446 if (! C4
::Context
->preference("canreservefromotherbranches")){
447 # cant reserve items so need to check if item homebranch and userenv branch match if not we cant reserve
448 my $userenv = C4
::Context
->userenv;
449 unless ( C4
::Context
->IsSuperLibrarian ) {
450 $item->{cantreserve
} = 1 if ( $item->{homebranch
} ne $userenv->{branch
} );
456 my $patron_unblessed = $patron->unblessed;
457 my $branch = C4
::Circulation
::_GetCircControlBranch
($item, $patron_unblessed);
459 my $branchitemrule = GetBranchItemRule
( $branch, $item->{'itype'} );
461 $item->{'holdallowed'} = $branchitemrule->{'holdallowed'};
463 my $can_item_be_reserved = CanItemBeReserved
( $patron->borrowernumber, $itemnumber );
464 $item->{not_holdable
} = $can_item_be_reserved unless ( $can_item_be_reserved eq 'OK' );
466 $item->{item_level_holds
} = Koha
::IssuingRules
->get_opacitemholds_policy( { item
=> $item_object, patron
=> $patron } );
469 !$item->{cantreserve
}
470 && !$exceeded_maxreserves
471 && IsAvailableForItemLevelRequest
($item, $patron_unblessed)
472 && $can_item_be_reserved eq 'OK'
475 $item->{available
} = 1;
478 push( @available_itemtypes, $item->{itype
} );
480 elsif ( C4
::Context
->preference('AllowHoldPolicyOverride') ) {
481 # If AllowHoldPolicyOverride is set, it should override EVERY restriction, not just branch item rules
482 $item->{override
} = 1;
485 push( @available_itemtypes, $item->{itype
} );
488 # If none of the conditions hold true, then neither override nor available is set and the item cannot be checked
490 # Show serial enumeration when needed
491 if ($item->{enumchron
}) {
492 $itemdata_enumchron = 1;
496 push @
{ $biblioitem->{itemloop
} }, $item;
499 if ( $num_override == scalar( @
{ $biblioitem->{itemloop
} } ) ) { # That is, if all items require an override
500 $template->param( override_required
=> 1 );
501 } elsif ( $num_available == 0 ) {
502 $template->param( none_available
=> 1 );
503 $biblioloopiter{warn} = 1;
504 $biblioloopiter{none_avail
} = 1;
506 $template->param( hiddencount
=> $hiddencount);
508 push @bibitemloop, $biblioitem;
511 @available_itemtypes = uniq
( @available_itemtypes );
512 $template->param( available_itemtypes
=> \
@available_itemtypes );
514 # existingreserves building
516 my @reserves = Koha
::Holds
->search( { biblionumber
=> $biblionumber }, { order_by
=> 'priority' } );
519 my $a_found = $a->found() || '';
520 my $b_found = $a->found() || '';
521 $a_found cmp $b_found;
525 my $priority = $res->priority();
528 for ( my $i = 1 ; $i <= $totalcount ; $i++ ) {
533 selected
=> ( $i == $priority ),
538 if ( $res->is_found() ) {
539 $reserve{'holdingbranch'} = $res->item()->holdingbranch();
540 $reserve{'biblionumber'} = $res->item()->biblionumber();
541 $reserve{'barcodenumber'} = $res->item()->barcode();
542 $reserve{'wbrcode'} = $res->branchcode();
543 $reserve{'itemnumber'} = $res->itemnumber();
544 $reserve{'wbrname'} = $res->branch()->branchname();
546 if ( $reserve{'holdingbranch'} eq $reserve{'wbrcode'} ) {
548 # Just because the holdingbranch matches the reserve branch doesn't mean the item
549 # has arrived at the destination, check for an open transfer for the item as well
550 my ( $transfertwhen, $transfertfrom, $transferto ) =
551 C4
::Circulation
::GetTransfers
( $res->itemnumber() );
552 if ( not $transferto or $transferto ne $res->branchcode() ) {
553 $reserve{'atdestination'} = 1;
557 # set found to 1 if reserve is waiting for patron pickup
558 $reserve{'found'} = $res->is_found();
559 $reserve{'intransit'} = $res->is_in_transit();
561 elsif ( $res->priority() > 0 ) {
562 if ( my $item = $res->item() ) {
563 $reserve{'itemnumber'} = $item->id();
564 $reserve{'barcodenumber'} = $item->barcode();
565 $reserve{'item_level_hold'} = 1;
569 $reserve{'expirationdate'} = output_pref
( { dt
=> dt_from_string
( $res->expirationdate ), dateonly
=> 1 } )
570 unless ( !defined( $res->expirationdate ) || $res->expirationdate eq '0000-00-00' );
571 $reserve{'date'} = output_pref
( { dt
=> dt_from_string
( $res->reservedate ), dateonly
=> 1 } );
572 $reserve{'borrowernumber'} = $res->borrowernumber();
573 $reserve{'biblionumber'} = $res->biblionumber();
574 $reserve{'patron'} = $res->borrower;
575 $reserve{'notes'} = $res->reservenotes();
576 $reserve{'waiting_date'} = $res->waitingdate();
577 $reserve{'ccode'} = $res->item() ?
$res->item()->ccode() : undef;
578 $reserve{'barcode'} = $res->item() ?
$res->item()->barcode() : undef;
579 $reserve{'priority'} = $res->priority();
580 $reserve{'lowestPriority'} = $res->lowestPriority();
581 $reserve{'optionloop'} = \
@optionloop;
582 $reserve{'suspend'} = $res->suspend();
583 $reserve{'suspend_until'} = $res->suspend_until();
584 $reserve{'reserve_id'} = $res->reserve_id();
585 $reserve{itemtype
} = $res->itemtype();
586 $reserve{branchcode
} = $res->branchcode();
588 push( @reserveloop, \
%reserve );
591 # get the time for the form name...
596 fixedRank
=> $fixedRank,
601 optionloop
=> \
@optionloop,
602 bibitemloop
=> \
@bibitemloop,
603 itemdata_enumchron
=> $itemdata_enumchron,
605 biblionumber
=> $biblionumber,
606 findborrower
=> $findborrower,
607 title
=> $biblio->title,
608 author
=> $biblio->author,
610 C4
::Search
::enabled_staff_search_views
,
613 $biblioloopiter{biblionumber
} = $biblionumber;
614 $biblioloopiter{title
} = $biblio->title;
615 $biblioloopiter{rank
} = $fixedRank;
616 $biblioloopiter{reserveloop
} = \
@reserveloop;
619 $template->param( reserveloop
=> \
@reserveloop );
622 push @biblioloop, \
%biblioloopiter;
625 $template->param( biblioloop
=> \
@biblioloop );
626 $template->param( biblionumbers
=> $biblionumbers );
627 $template->param( exceeded_maxreserves
=> $exceeded_maxreserves );
628 $template->param( exceeded_holds_per_record
=> $exceeded_holds_per_record );
631 $template->param( multi_hold
=> 1 );
634 if ( C4
::Context
->preference( 'AllowHoldDateInFuture' ) ) {
635 $template->param( reserve_in_future
=> 1 );
639 SuspendHoldsIntranet
=> C4
::Context
->preference('SuspendHoldsIntranet'),
640 AutoResumeSuspendedHolds
=> C4
::Context
->preference('AutoResumeSuspendedHolds'),
644 output_html_with_http_headers
$input, $cookie, $template->output;
646 sub sort_borrowerlist
{
647 my $borrowerslist = shift;
650 uc( $a->{surname
} . $a->{firstname
} ) cmp
651 uc( $b->{surname
} . $b->{firstname
} )