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
51 my $dbh = C4::Context->dbh;
53 my ( $template, $borrowernumber, $cookie, $flags ) = get_template_and_user(
55 template_name => "reserve/request.tt",
59 flagsrequired => { reserveforothers => 'place_holds' },
63 my $multihold = $input->param('multi_hold');
64 $template->param(multi_hold => $multihold);
65 my $showallitems = $input->param('showallitems');
67 my $itemtypes = GetItemTypes();
69 # Select borrowers infos
70 my $findborrower = $input->param('findborrower');
71 $findborrower = '' unless defined $findborrower;
72 $findborrower =~ s|,| |g;
73 my $borrowernumber_hold = $input->param('borrowernumber') || '';
77 my $exceeded_maxreserves;
78 my $exceeded_holds_per_record;
80 my $date = output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 });
81 my $action = $input->param('action');
84 if ( $action eq 'move' ) {
85 my $where = $input->param('where');
86 my $reserve_id = $input->param('reserve_id');
87 AlterPriority( $where, $reserve_id );
88 } elsif ( $action eq 'cancel' ) {
89 my $reserve_id = $input->param('reserve_id');
90 CancelReserve({ reserve_id => $reserve_id });
91 } elsif ( $action eq 'setLowestPriority' ) {
92 my $reserve_id = $input->param('reserve_id');
93 ToggleLowestPriority( $reserve_id );
94 } elsif ( $action eq 'toggleSuspend' ) {
95 my $reserve_id = $input->param('reserve_id');
96 my $suspend_until = $input->param('suspend_until');
97 ToggleSuspend( $reserve_id, $suspend_until );
101 my $borrower = C4::Members::GetMember( cardnumber => $findborrower );
103 $borrowernumber_hold = $borrower->{borrowernumber};
105 my $dt_params = { iDisplayLength => -1 };
106 my $results = C4::Utils::DataTables::Members::search(
108 searchmember => $findborrower,
109 dt_params => $dt_params,
112 my $borrowers = $results->{patrons};
113 if ( scalar @$borrowers == 1 ) {
114 $borrowernumber_hold = $borrowers->[0]->{borrowernumber};
115 } elsif ( @$borrowers ) {
116 $template->param( borrowers => $borrowers );
118 $messageborrower = "'$findborrower'";
123 my @biblionumbers = ();
124 my $biblionumbers = $input->param('biblionumbers');
126 @biblionumbers = split '/', $biblionumbers;
128 push @biblionumbers, $input->multi_param('biblionumber');
132 # If we have the borrowernumber because we've performed an action, then we
133 # don't want to try to place another reserve.
134 if ($borrowernumber_hold && !$action) {
135 my $borrowerinfo = GetMember( borrowernumber => $borrowernumber_hold );
138 # we check the reserves of the borrower, and if he can reserv a document
139 # FIXME At this time we have a simple count of reservs, but, later, we could improve the infos "title" ...
142 GetReserveCount( $borrowerinfo->{'borrowernumber'} );
144 my $new_reserves_count = scalar( @biblionumbers );
146 my $maxreserves = C4::Context->preference('maxreserves');
148 && ( $reserves_count + $new_reserves_count > $maxreserves ) )
150 my $new_reserves_allowed =
151 $maxreserves - $reserves_count > 0
152 ? $maxreserves - $reserves_count
155 $exceeded_maxreserves = 1;
157 new_reserves_allowed => $new_reserves_allowed,
158 new_reserves_count => $new_reserves_count,
159 reserves_count => $reserves_count,
160 maxreserves => $maxreserves,
164 # we check the date expiry of the borrower (only if there is an expiry date, otherwise, set to 1 (warn)
165 my $expiry_date = $borrowerinfo->{dateexpiry};
166 my $expiry = 0; # flag set if patron account has expired
167 if ($expiry_date and $expiry_date ne '0000-00-00' and
168 Date_to_Days(split /-/,$date) > Date_to_Days(split /-/,$expiry_date)) {
172 # check if the borrower make the reserv in a different branch
173 if ( $borrowerinfo->{'branchcode'} ne C4::Context->userenv->{'branch'} ) {
177 my $is_debarred = Koha::Patrons->find( $borrowerinfo->{borrowernumber} )->is_debarred;
179 borrowernumber => $borrowerinfo->{'borrowernumber'},
180 borrowersurname => $borrowerinfo->{'surname'},
181 borrowerfirstname => $borrowerinfo->{'firstname'},
182 borrowerstreetaddress => $borrowerinfo->{'address'},
183 borrowercity => $borrowerinfo->{'city'},
184 borrowerphone => $borrowerinfo->{'phone'},
185 borrowermobile => $borrowerinfo->{'mobile'},
186 borrowerfax => $borrowerinfo->{'fax'},
187 borrowerphonepro => $borrowerinfo->{'phonepro'},
188 borroweremail => $borrowerinfo->{'email'},
189 borroweremailpro => $borrowerinfo->{'emailpro'},
190 borrowercategory => $borrowerinfo->{'category'},
191 cardnumber => $borrowerinfo->{'cardnumber'},
193 diffbranch => $diffbranch,
194 messages => $messages,
195 warnings => $warnings,
196 restricted => $is_debarred,
197 amount_outstanding => GetMemberAccountRecords($borrowerinfo->{borrowernumber}),
201 $template->param( messageborrower => $messageborrower );
203 # FIXME launch another time GetMember perhaps until
204 my $borrowerinfo = GetMember( borrowernumber => $borrowernumber_hold );
206 my $logged_in_patron = Koha::Patrons->find( $borrowernumber );
208 my $itemdata_enumchron = 0;
210 foreach my $biblionumber (@biblionumbers) {
211 next unless $biblionumber =~ m|^\d+$|;
213 my %biblioloopiter = ();
215 my $dat = GetBiblioData($biblionumber);
217 my $canReserve = CanBookBeReserved( $borrowerinfo->{borrowernumber}, $biblionumber );
219 if ( $canReserve eq 'OK' ) {
221 #All is OK and we can continue
223 elsif ( $canReserve eq 'tooManyReserves' ) {
224 $exceeded_maxreserves = 1;
226 elsif ( $canReserve eq 'tooManyHoldsForThisRecord' ) {
227 $exceeded_holds_per_record = 1;
228 $biblioloopiter{$canReserve} = 1;
230 elsif ( $canReserve eq 'ageRestricted' ) {
231 $template->param( $canReserve => 1 );
232 $biblioloopiter{$canReserve} = 1;
235 $biblioloopiter{$canReserve} = 1;
238 my $force_hold_level;
239 if ( $borrowerinfo->{borrowernumber} ) {
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 => $borrowerinfo->{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( $borrowerinfo->{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 );
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 my $alreadypossession;
268 if ( !C4::Context->preference('AllowHoldsOnPatronsPossessions')
269 && CheckIfIssuedToPatron( $borrowerinfo->{borrowernumber}, $biblionumber ) )
271 $template->param( alreadypossession => $alreadypossession, );
275 my $count = Koha::Holds->search( { biblionumber => $biblionumber } )->count();
276 my $totalcount = $count;
278 # FIXME think @optionloop, is maybe obsolete, or must be switchable by a systeme preference fixed rank or not
279 # make priorities options
282 for ( 1 .. $count + 1 ) {
287 selected => ( $_ == $count + 1 ),
291 # adding a fixed value for priority options
292 my $fixedRank = $count+1;
294 my %itemnumbers_of_biblioitem;
297 ## $items is array of 'item' table numbers
298 if (my $items = get_itemnumbers_of($biblionumber)->{$biblionumber}){
299 @itemnumbers = @$items;
301 my @hostitems = get_hostitemnumbers_of($biblionumber);
303 $template->param('hostitemsflag' => 1);
304 push(@itemnumbers, @hostitems);
308 $template->param('noitems' => 1);
309 $biblioloopiter{noitems} = 1;
312 ## Hash of item number to 'item' table fields
313 my $iteminfos_of = GetItemInfosOf(@itemnumbers);
315 ## Here we go backwards again to create hash of biblioitemnumber to itemnumbers,
316 ## when by definition all of the itemnumber have the same biblioitemnumber
317 foreach my $itemnumber (@itemnumbers) {
318 my $biblioitemnumber = $iteminfos_of->{$itemnumber}->{biblioitemnumber};
319 push( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} }, $itemnumber );
322 ## Should be same as biblionumber
323 my @biblioitemnumbers = keys %itemnumbers_of_biblioitem;
325 ## Hash of biblioitemnumber to 'biblioitem' table records
326 my $biblioiteminfos_of = GetBiblioItemInfosOf(@biblioitemnumbers);
328 my $frameworkcode = GetFrameworkCode( $biblionumber );
329 my @notforloan_avs = Koha::AuthorisedValues->search_by_koha_field({ kohafield => 'items.notforloan', frameworkcode => $frameworkcode });
330 my $notforloan_label_of = { map { $_->authorised_value => $_->lib } @notforloan_avs };
334 my @available_itemtypes;
335 foreach my $biblioitemnumber (@biblioitemnumbers) {
336 my $biblioitem = $biblioiteminfos_of->{$biblioitemnumber};
337 my $num_available = 0;
338 my $num_override = 0;
341 $biblioitem->{force_hold_level} = $force_hold_level;
343 if ( $biblioitem->{biblioitemnumber} ne $biblionumber ) {
344 $biblioitem->{hostitemsflag} = 1;
347 $biblioloopiter{description} = $biblioitem->{description};
348 $biblioloopiter{itypename} = $biblioitem->{description};
349 if ( $biblioitem->{itemtype} ) {
351 $biblioitem->{description} =
352 $itemtypes->{ $biblioitem->{itemtype} }{description};
354 $biblioloopiter{imageurl} =
355 getitemtypeimagelocation( 'intranet',
356 $itemtypes->{ $biblioitem->{itemtype} }{imageurl} );
359 foreach my $itemnumber ( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} } ) {
360 my $item = $iteminfos_of->{$itemnumber};
362 $item->{force_hold_level} = $force_hold_level;
364 unless (C4::Context->preference('item-level_itypes')) {
365 $item->{itype} = $biblioitem->{itemtype};
368 $item->{itypename} = $itemtypes->{ $item->{itype} }{description};
369 $item->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtypes->{ $item->{itype} }{imageurl} );
370 $item->{homebranch} = $item->{homebranch};
372 # if the holdingbranch is different than the homebranch, we show the
373 # holdingbranch of the document too
374 if ( $item->{homebranch} ne $item->{holdingbranch} ) {
375 $item->{holdingbranch} = $item->{holdingbranch};
378 if($item->{biblionumber} ne $biblionumber){
379 $item->{hostitemsflag}=1;
380 $item->{hosttitle} = GetBiblioData($item->{biblionumber})->{title};
383 # if the item is currently on loan, we display its return date and
384 # change the background color
385 my $issues= GetItemIssue($itemnumber);
386 if ( $issues->{'date_due'} ) {
387 $item->{date_due} = $issues->{date_due_sql};
388 $item->{backgroundcolor} = 'onloan';
392 my $holds = Koha::Items->find( $itemnumber )->current_holds;
393 if ( my $first_hold = $holds->next ) {
394 my $ItemBorrowerReserveInfo = GetMember( borrowernumber => $first_hold->borrowernumber );
396 $item->{backgroundcolor} = 'reserved';
397 $item->{reservedate} = output_pref({ dt => dt_from_string( $first_hold->reservedate ), dateonly => 1 }); # FIXME Should be formatted in the template
398 $item->{ReservedForBorrowernumber} = $first_hold->borrowernumber;
399 $item->{ReservedForSurname} = $ItemBorrowerReserveInfo->{'surname'};
400 $item->{ReservedForFirstname} = $ItemBorrowerReserveInfo->{'firstname'};
401 $item->{ExpectedAtLibrary} = $first_hold->branchcode;
402 $item->{waitingdate} = $first_hold->waitingdate;
405 # Management of the notforloan document
406 if ( $item->{notforloan} ) {
407 $item->{backgroundcolor} = 'other';
408 $item->{notforloanvalue} =
409 $notforloan_label_of->{ $item->{notforloan} };
412 # Management of lost or long overdue items
413 if ( $item->{itemlost} ) {
415 # FIXME localized strings should never be in Perl code
417 $item->{itemlost} == 1 ? "(lost)"
418 : $item->{itemlost} == 2 ? "(long overdue)"
420 $item->{backgroundcolor} = 'other';
421 if ($logged_in_patron->category->hidelostitems && !$showallitems) {
427 # Check the transit status
428 my ( $transfertwhen, $transfertfrom, $transfertto ) =
429 GetTransfers($itemnumber);
431 if ( defined $transfertwhen && $transfertwhen ne '' ) {
432 $item->{transfertwhen} = output_pref({ dt => dt_from_string( $transfertwhen ), dateonly => 1 });
433 $item->{transfertfrom} = $transfertfrom;
434 $item->{transfertto} = $transfertto;
435 $item->{nocancel} = 1;
438 # If there is no loan, return and transfer, we show a checkbox.
439 $item->{notforloan} ||= 0;
441 # if independent branches is on we need to check if the person can reserve
442 # for branches they arent logged in to
443 if ( C4::Context->preference("IndependentBranches") ) {
444 if (! C4::Context->preference("canreservefromotherbranches")){
445 # cant reserve items so need to check if item homebranch and userenv branch match if not we cant reserve
446 my $userenv = C4::Context->userenv;
447 unless ( C4::Context->IsSuperLibrarian ) {
448 $item->{cantreserve} = 1 if ( $item->{homebranch} ne $userenv->{branch} );
453 my $branch = C4::Circulation::_GetCircControlBranch($item, $borrowerinfo);
455 my $branchitemrule = GetBranchItemRule( $branch, $item->{'itype'} );
457 $item->{'holdallowed'} = $branchitemrule->{'holdallowed'};
459 my $can_item_be_reserved = CanItemBeReserved( $borrowerinfo->{borrowernumber}, $itemnumber );
460 $item->{not_holdable} = $can_item_be_reserved unless ( $can_item_be_reserved eq 'OK' );
462 $item->{item_level_holds} = OPACItemHoldsAllowed( $item, $borrowerinfo );
465 !$item->{cantreserve}
466 && !$exceeded_maxreserves
467 && IsAvailableForItemLevelRequest($item, $borrowerinfo)
468 && $can_item_be_reserved eq 'OK'
471 $item->{available} = 1;
474 push( @available_itemtypes, $item->{itype} );
476 elsif ( C4::Context->preference('AllowHoldPolicyOverride') ) {
477 # If AllowHoldPolicyOverride is set, it should override EVERY restriction, not just branch item rules
478 $item->{override} = 1;
482 # If none of the conditions hold true, then neither override nor available is set and the item cannot be checked
484 # Show serial enumeration when needed
485 if ($item->{enumchron}) {
486 $itemdata_enumchron = 1;
489 push @{ $biblioitem->{itemloop} }, $item;
492 if ( $num_override == scalar( @{ $biblioitem->{itemloop} } ) ) { # That is, if all items require an override
493 $template->param( override_required => 1 );
494 } elsif ( $num_available == 0 ) {
495 $template->param( none_available => 1 );
496 $biblioloopiter{warn} = 1;
497 $biblioloopiter{none_avail} = 1;
499 $template->param( hiddencount => $hiddencount);
501 push @bibitemloop, $biblioitem;
504 @available_itemtypes = uniq( @available_itemtypes );
505 $template->param( available_itemtypes => \@available_itemtypes );
507 # existingreserves building
509 my @reserves = Koha::Holds->search( { biblionumber => $biblionumber }, { order_by => 'priority' } );
512 my $a_found = $a->found() || '';
513 my $b_found = $a->found() || '';
514 $a_found cmp $b_found;
518 my $priority = $res->priority();
521 for ( my $i = 1 ; $i <= $totalcount ; $i++ ) {
526 selected => ( $i == $priority ),
531 if ( $res->is_found() ) {
532 $reserve{'holdingbranch'} = $res->item()->holdingbranch();
533 $reserve{'biblionumber'} = $res->item()->biblionumber();
534 $reserve{'barcodenumber'} = $res->item()->barcode();
535 $reserve{'wbrcode'} = $res->branchcode();
536 $reserve{'itemnumber'} = $res->itemnumber();
537 $reserve{'wbrname'} = $res->branch()->branchname();
539 if ( $reserve{'holdingbranch'} eq $reserve{'wbrcode'} ) {
541 # Just because the holdingbranch matches the reserve branch doesn't mean the item
542 # has arrived at the destination, check for an open transfer for the item as well
543 my ( $transfertwhen, $transfertfrom, $transferto ) =
544 C4::Circulation::GetTransfers( $res->itemnumber() );
545 if ( not $transferto or $transferto ne $res->branchcode() ) {
546 $reserve{'atdestination'} = 1;
550 # set found to 1 if reserve is waiting for patron pickup
551 $reserve{'found'} = $res->is_found();
552 $reserve{'intransit'} = $res->is_in_transit();
554 elsif ( $res->priority() > 0 ) {
555 if ( my $item = $res->item() ) {
556 $reserve{'itemnumber'} = $item->id();
557 $reserve{'barcodenumber'} = $item->barcode();
558 $reserve{'item_level_hold'} = 1;
562 # get borrowers reserve info
563 if ( C4::Context->preference('HidePatronName') ) {
564 $reserve{'hidename'} = 1;
565 $reserve{'cardnumber'} = $res->borrower()->cardnumber();
567 $reserve{'expirationdate'} = output_pref( { dt => dt_from_string( $res->expirationdate ), dateonly => 1 } )
568 unless ( !defined( $res->expirationdate ) || $res->expirationdate eq '0000-00-00' );
569 $reserve{'date'} = output_pref( { dt => dt_from_string( $res->reservedate ), dateonly => 1 } );
570 $reserve{'borrowernumber'} = $res->borrowernumber();
571 $reserve{'biblionumber'} = $res->biblionumber();
572 $reserve{'borrowernumber'} = $res->borrowernumber();
573 $reserve{'firstname'} = $res->borrower()->firstname();
574 $reserve{'surname'} = $res->borrower()->surname();
575 $reserve{'notes'} = $res->reservenotes();
576 $reserve{'waiting_date'} = $res->waitingdate();
577 $reserve{'waiting_until'} = $res->is_waiting() ? $res->waiting_expires_on() : undef;
578 $reserve{'ccode'} = $res->item() ? $res->item()->ccode() : undef;
579 $reserve{'barcode'} = $res->item() ? $res->item()->barcode() : undef;
580 $reserve{'priority'} = $res->priority();
581 $reserve{'lowestPriority'} = $res->lowestPriority();
582 $reserve{'optionloop'} = \@optionloop;
583 $reserve{'suspend'} = $res->suspend();
584 $reserve{'suspend_until'} = $res->suspend_until();
585 $reserve{'reserve_id'} = $res->reserve_id();
586 $reserve{itemtype} = $res->itemtype();
587 $reserve{branchcode} = $res->branchcode();
589 push( @reserveloop, \%reserve );
592 # get the time for the form name...
597 fixedRank => $fixedRank,
602 optionloop => \@optionloop,
603 bibitemloop => \@bibitemloop,
604 itemdata_enumchron => $itemdata_enumchron,
606 biblionumber => $biblionumber,
607 findborrower => $findborrower,
608 title => $dat->{title},
609 author => $dat->{author},
611 C4::Search::enabled_staff_search_views,
613 if (defined $borrowerinfo && exists $borrowerinfo->{'branchcode'}) {
614 $template->param( borrower_branchcode => $borrowerinfo->{'branchcode'},);
617 $biblioloopiter{biblionumber} = $biblionumber;
618 $biblioloopiter{title} = $dat->{title};
619 $biblioloopiter{rank} = $fixedRank;
620 $biblioloopiter{reserveloop} = \@reserveloop;
623 $template->param( reserveloop => \@reserveloop );
626 push @biblioloop, \%biblioloopiter;
629 $template->param( biblioloop => \@biblioloop );
630 $template->param( biblionumbers => $biblionumbers );
631 $template->param( exceeded_maxreserves => $exceeded_maxreserves );
632 $template->param( exceeded_holds_per_record => $exceeded_holds_per_record );
635 $template->param( multi_hold => 1 );
638 if ( C4::Context->preference( 'AllowHoldDateInFuture' ) ) {
639 $template->param( reserve_in_future => 1 );
643 SuspendHoldsIntranet => C4::Context->preference('SuspendHoldsIntranet'),
644 AutoResumeSuspendedHolds => C4::Context->preference('AutoResumeSuspendedHolds'),
648 output_html_with_http_headers $input, $cookie, $template->output;
650 sub sort_borrowerlist {
651 my $borrowerslist = shift;
654 uc( $a->{surname} . $a->{firstname} ) cmp
655 uc( $b->{surname} . $b->{firstname} )