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 my $hold_itemnumber = $input->param('itemnumber');
96 if ( $prev_priority == 0 && $next_priority == 1 ){
97 C4
::Reserves
::RevertWaitingStatus
({ itemnumber
=> $hold_itemnumber });
99 AlterPriority
( $where, $reserve_id, $prev_priority, $next_priority, $first_priority, $last_priority );
101 } elsif ( $action eq 'cancel' ) {
102 my $reserve_id = $input->param('reserve_id');
103 my $hold = Koha
::Holds
->find( $reserve_id );
104 $hold->cancel if $hold;
105 } elsif ( $action eq 'setLowestPriority' ) {
106 my $reserve_id = $input->param('reserve_id');
107 ToggleLowestPriority
( $reserve_id );
108 } elsif ( $action eq 'toggleSuspend' ) {
109 my $reserve_id = $input->param('reserve_id');
110 my $suspend_until = $input->param('suspend_until');
111 ToggleSuspend
( $reserve_id, $suspend_until );
115 my $patron = Koha
::Patrons
->find( { cardnumber
=> $findborrower } );
117 $borrowernumber_hold = $patron->borrowernumber;
119 my $dt_params = { iDisplayLength
=> -1 };
120 my $results = C4
::Utils
::DataTables
::Members
::search
(
122 searchmember
=> $findborrower,
123 dt_params
=> $dt_params,
126 my $borrowers = $results->{patrons
};
127 if ( scalar @
$borrowers == 1 ) {
128 $borrowernumber_hold = $borrowers->[0]->{borrowernumber
};
129 } elsif ( @
$borrowers ) {
130 $template->param( borrowers
=> $borrowers );
132 $messageborrower = "'$findborrower'";
137 my @biblionumbers = ();
138 my $biblionumber = $input->param('biblionumber');
139 my $biblionumbers = $input->param('biblionumbers');
140 if ( $biblionumbers ) {
141 @biblionumbers = split '/', $biblionumbers;
143 push @biblionumbers, $input->multi_param('biblionumber');
146 # FIXME multi_hold should not be a variable but depends on the number of elements in @biblionumbers
147 $template->param(multi_hold
=> scalar $input->param('multi_hold'));
149 # If we have the borrowernumber because we've performed an action, then we
150 # don't want to try to place another reserve.
151 if ($borrowernumber_hold && !$action) {
152 my $patron = Koha
::Patrons
->find( $borrowernumber_hold );
155 # we check the reserves of the user, and if they can reserve a document
156 # FIXME At this time we have a simple count of reservs, but, later, we could improve the infos "title" ...
158 my $reserves_count = $patron->holds->count;
160 my $new_reserves_count = scalar( @biblionumbers );
162 my $maxreserves = C4
::Context
->preference('maxreserves');
164 && ( $reserves_count + $new_reserves_count > $maxreserves ) )
166 my $new_reserves_allowed =
167 $maxreserves - $reserves_count > 0
168 ?
$maxreserves - $reserves_count
171 $exceeded_maxreserves = 1;
173 new_reserves_allowed
=> $new_reserves_allowed,
174 new_reserves_count
=> $new_reserves_count,
175 reserves_count
=> $reserves_count,
176 maxreserves
=> $maxreserves,
180 # we check the date expiry of the borrower (only if there is an expiry date, otherwise, set to 1 (warn)
181 my $expiry_date = $patron->dateexpiry;
182 my $expiry = 0; # flag set if patron account has expired
183 if ($expiry_date and $expiry_date ne '0000-00-00' and
184 Date_to_Days
(split /-/,$date) > Date_to_Days
(split /-/,$expiry_date)) {
188 # check if the borrower make the reserv in a different branch
189 if ( $patron->branchcode ne C4
::Context
->userenv->{'branch'} ) {
193 my $amount_outstanding = $patron->account->balance;
197 diffbranch
=> $diffbranch,
198 messages
=> $messages,
199 warnings
=> $warnings,
200 amount_outstanding
=> $amount_outstanding,
204 $template->param( messageborrower
=> $messageborrower );
206 # FIXME launch another time GetMember perhaps until (Joubu: Why?)
207 my $patron = Koha
::Patrons
->find( $borrowernumber_hold );
209 my $logged_in_patron = Koha
::Patrons
->find( $borrowernumber );
211 my $itemdata_enumchron = 0;
212 my $itemdata_ccode = 0;
214 foreach my $biblionumber (@biblionumbers) {
215 next unless $biblionumber =~ m
|^\d
+$|;
217 my %biblioloopiter = ();
219 my $biblio = Koha
::Biblios
->find( $biblionumber );
221 my $force_hold_level;
223 { # CanBookBeReserved
224 my $canReserve = CanBookBeReserved
( $patron->borrowernumber, $biblionumber );
225 if ( $canReserve->{status
} eq 'OK' ) {
227 #All is OK and we can continue
229 elsif ( $canReserve->{status
} eq 'tooManyReserves' ) {
230 $exceeded_maxreserves = 1;
231 $template->param( maxreserves
=> $canReserve->{limit
} );
233 elsif ( $canReserve->{status
} eq 'tooManyHoldsForThisRecord' ) {
234 $exceeded_holds_per_record = 1;
235 $biblioloopiter{ $canReserve->{status
} } = 1;
237 elsif ( $canReserve->{status
} eq 'ageRestricted' ) {
238 $template->param( $canReserve->{status
} => 1 );
239 $biblioloopiter{ $canReserve->{status
} } = 1;
242 $biblioloopiter{ $canReserve->{status
} } = 1;
246 # For multiple holds per record, if a patron has previously placed a hold,
247 # the patron can only place more holds of the same type. That is, if the
248 # patron placed a record level hold, all the holds the patron places must
249 # be record level. If the patron placed an item level hold, all holds
250 # the patron places must be item level
251 my $holds = Koha
::Holds
->search(
253 borrowernumber
=> $patron->borrowernumber,
254 biblionumber
=> $biblionumber,
258 $force_hold_level = $holds->forced_hold_level();
259 $biblioloopiter{force_hold_level
} = $force_hold_level;
260 $template->param( force_hold_level
=> $force_hold_level );
262 # For a librarian to be able to place multiple record holds for a patron for a record,
263 # we must find out what the maximum number of holds they can place for the patron is
264 my $max_holds_for_record = GetMaxPatronHoldsForRecord
( $patron->borrowernumber, $biblionumber );
265 my $remaining_holds_for_record = $max_holds_for_record - $holds->count();
266 $biblioloopiter{remaining_holds_for_record
} = $max_holds_for_record;
267 $template->param( max_holds_for_record
=> $max_holds_for_record );
268 $template->param( remaining_holds_for_record
=> $remaining_holds_for_record );
270 { # alreadypossession
271 # Check to see if patron is allowed to place holds on records where the
272 # patron already has an item from that record checked out
273 if ( !C4
::Context
->preference('AllowHoldsOnPatronsPossessions')
274 && CheckIfIssuedToPatron
( $patron->borrowernumber, $biblionumber ) )
276 $template->param( alreadypossession
=> 1, );
282 my $count = Koha
::Holds
->search( { biblionumber
=> $biblionumber } )->count();
283 my $totalcount = $count;
285 # FIXME think @optionloop, is maybe obsolete, or must be switchable by a systeme preference fixed rank or not
286 # make priorities options
289 for ( 1 .. $count + 1 ) {
294 selected
=> ( $_ == $count + 1 ),
298 # adding a fixed value for priority options
299 my $fixedRank = $count+1;
301 my %itemnumbers_of_biblioitem;
303 my @hostitems = get_hostitemnumbers_of
($biblionumber);
306 $template->param('hostitemsflag' => 1);
307 push(@itemnumbers, @hostitems);
310 my $items = Koha
::Items
->search({ -or => { biblionumber
=> $biblionumber, itemnumber
=> { in => \
@itemnumbers } } });
312 unless ( $items->count ) {
313 # FIXME Then why do we continue?
314 $template->param('noitems' => 1);
315 $biblioloopiter{noitems
} = 1;
318 ## Here we go backwards again to create hash of biblioitemnumber to itemnumbers,
319 ## when by definition all of the itemnumber have the same biblioitemnumber
320 my ( $iteminfos_of );
321 while ( my $item = $items->next ) {
322 $item = $item->unblessed;
323 my $biblioitemnumber = $item->{biblioitemnumber
};
324 my $itemnumber = $item->{itemnumber
};
325 push( @
{ $itemnumbers_of_biblioitem{$biblioitemnumber} }, $itemnumber );
326 $iteminfos_of->{$itemnumber} = $item;
329 ## Should be same as biblionumber
330 my @biblioitemnumbers = keys %itemnumbers_of_biblioitem;
332 my $biblioiteminfos_of = {
335 ( $biblioitem->{biblioitemnumber
} => $biblioitem )
336 } @
{ Koha
::Biblioitems
->search(
337 { biblioitemnumber
=> { -in => \
@biblioitemnumbers } },
338 { select => ['biblioitemnumber', 'publicationyear', 'itemtype']}
343 my $frameworkcode = GetFrameworkCode
( $biblionumber );
344 my @notforloan_avs = Koha
::AuthorisedValues
->search_by_koha_field({ kohafield
=> 'items.notforloan', frameworkcode
=> $frameworkcode });
345 my $notforloan_label_of = { map { $_->authorised_value => $_->lib } @notforloan_avs };
349 my @available_itemtypes;
350 foreach my $biblioitemnumber (@biblioitemnumbers) {
351 my $biblioitem = $biblioiteminfos_of->{$biblioitemnumber};
352 my $num_available = 0;
353 my $num_override = 0;
356 $biblioitem->{force_hold_level
} = $force_hold_level;
358 if ( $biblioitem->{biblioitemnumber
} ne $biblionumber ) {
359 $biblioitem->{hostitemsflag
} = 1;
362 $biblioloopiter{description
} = $biblioitem->{description
};
363 $biblioloopiter{itypename
} = $biblioitem->{description
};
364 if ( $biblioitem->{itemtype
} ) {
366 $biblioitem->{description
} =
367 $itemtypes->{ $biblioitem->{itemtype
} }{description
};
369 $biblioloopiter{imageurl
} =
370 getitemtypeimagelocation
( 'intranet',
371 $itemtypes->{ $biblioitem->{itemtype
} }{imageurl
} );
374 foreach my $itemnumber ( @
{ $itemnumbers_of_biblioitem{$biblioitemnumber} } ) {
375 my $item = $iteminfos_of->{$itemnumber};
377 $item->{force_hold_level
} = $force_hold_level;
379 unless (C4
::Context
->preference('item-level_itypes')) {
380 $item->{itype
} = $biblioitem->{itemtype
};
383 $item->{itypename
} = $itemtypes->{ $item->{itype
} }{description
};
384 $item->{imageurl
} = getitemtypeimagelocation
( 'intranet', $itemtypes->{ $item->{itype
} }{imageurl
} );
385 $item->{homebranch
} = $item->{homebranch
};
387 # if the holdingbranch is different than the homebranch, we show the
388 # holdingbranch of the document too
389 if ( $item->{homebranch
} ne $item->{holdingbranch
} ) {
390 $item->{holdingbranch
} = $item->{holdingbranch
};
393 if($item->{biblionumber
} ne $biblionumber){
394 $item->{hostitemsflag
} = 1;
395 $item->{hosttitle
} = Koha
::Biblios
->find( $item->{biblionumber
} )->title;
398 # if the item is currently on loan, we display its return date and
399 # change the background color
400 my $issue = Koha
::Checkouts
->find( { itemnumber
=> $itemnumber } );
402 $item->{date_due
} = $issue->date_due;
403 $item->{backgroundcolor
} = 'onloan';
407 my $item_object = Koha
::Items
->find( $itemnumber );
408 my $holds = $item_object->current_holds;
409 if ( my $first_hold = $holds->next ) {
410 my $p = Koha
::Patrons
->find( $first_hold->borrowernumber );
412 $item->{backgroundcolor
} = 'reserved';
413 $item->{reservedate
} = output_pref
({ dt
=> dt_from_string
( $first_hold->reservedate ), dateonly
=> 1 }); # FIXME Should be formatted in the template
414 $item->{ReservedFor
} = $p;
415 $item->{ExpectedAtLibrary
} = $first_hold->branchcode;
416 $item->{waitingdate
} = $first_hold->waitingdate;
419 # Management of the notforloan document
420 if ( $item->{notforloan
} ) {
421 $item->{backgroundcolor
} = 'other';
422 $item->{notforloanvalue
} =
423 $notforloan_label_of->{ $item->{notforloan
} };
426 # Management of lost or long overdue items
427 if ( $item->{itemlost
} ) {
429 # FIXME localized strings should never be in Perl code
431 $item->{itemlost
} == 1 ?
"(lost)"
432 : $item->{itemlost
} == 2 ?
"(long overdue)"
434 $item->{backgroundcolor
} = 'other';
435 if ($logged_in_patron->category->hidelostitems && !$showallitems) {
441 # Check the transit status
442 my ( $transfertwhen, $transfertfrom, $transfertto ) =
443 GetTransfers
($itemnumber);
445 if ( defined $transfertwhen && $transfertwhen ne '' ) {
446 $item->{transfertwhen
} = output_pref
({ dt
=> dt_from_string
( $transfertwhen ), dateonly
=> 1 });
447 $item->{transfertfrom
} = $transfertfrom;
448 $item->{transfertto
} = $transfertto;
449 $item->{nocancel
} = 1;
452 # If there is no loan, return and transfer, we show a checkbox.
453 $item->{notforloan
} ||= 0;
455 # if independent branches is on we need to check if the person can reserve
456 # for branches they arent logged in to
457 if ( C4
::Context
->preference("IndependentBranches") ) {
458 if (! C4
::Context
->preference("canreservefromotherbranches")){
459 # can't reserve items so need to check if item homebranch and userenv branch match if not we can't reserve
460 my $userenv = C4
::Context
->userenv;
461 unless ( C4
::Context
->IsSuperLibrarian ) {
462 $item->{cantreserve
} = 1 if ( $item->{homebranch
} ne $userenv->{branch
} );
468 my $patron_unblessed = $patron->unblessed;
469 my $branch = C4
::Circulation
::_GetCircControlBranch
($item, $patron_unblessed);
471 my $branchitemrule = GetBranchItemRule
( $branch, $item->{'itype'} );
473 $item->{'holdallowed'} = $branchitemrule->{'holdallowed'};
475 my $can_item_be_reserved = CanItemBeReserved
( $patron->borrowernumber, $itemnumber );
476 $item->{not_holdable
} = $can_item_be_reserved->{status
} unless ( $can_item_be_reserved->{status
} eq 'OK' );
478 $item->{item_level_holds
} = Koha
::IssuingRules
->get_opacitemholds_policy( { item
=> $item_object, patron
=> $patron } );
481 !$item->{cantreserve
}
482 && !$exceeded_maxreserves
483 && IsAvailableForItemLevelRequest
($item, $patron_unblessed)
484 && $can_item_be_reserved->{status
} eq 'OK'
487 $item->{available
} = 1;
490 push( @available_itemtypes, $item->{itype
} );
492 elsif ( C4
::Context
->preference('AllowHoldPolicyOverride') ) {
493 # If AllowHoldPolicyOverride is set, it should override EVERY restriction, not just branch item rules
494 $item->{override
} = 1;
497 push( @available_itemtypes, $item->{itype
} );
500 # If none of the conditions hold true, then neither override nor available is set and the item cannot be checked
502 # Show serial enumeration when needed
503 if ($item->{enumchron
}) {
504 $itemdata_enumchron = 1;
506 # Show collection when needed
507 if ($item->{ccode
}) {
512 push @
{ $biblioitem->{itemloop
} }, $item;
515 if ( $num_override == scalar( @
{ $biblioitem->{itemloop
} } ) ) { # That is, if all items require an override
516 $template->param( override_required
=> 1 );
517 } elsif ( $num_available == 0 ) {
518 $template->param( none_available
=> 1 );
519 $biblioloopiter{warn} = 1;
520 $biblioloopiter{none_avail
} = 1;
522 $template->param( hiddencount
=> $hiddencount);
524 push @bibitemloop, $biblioitem;
527 @available_itemtypes = uniq
( @available_itemtypes );
528 $template->param( available_itemtypes
=> \
@available_itemtypes );
530 # existingreserves building
532 my @reserves = Koha
::Holds
->search( { biblionumber
=> $biblionumber }, { order_by
=> 'priority' } );
535 my $a_found = $a->found() || '';
536 my $b_found = $a->found() || '';
537 $a_found cmp $b_found;
541 my $priority = $res->priority();
544 for ( my $i = 1 ; $i <= $totalcount ; $i++ ) {
549 selected
=> ( $i == $priority ),
554 if ( $res->is_found() ) {
555 $reserve{'holdingbranch'} = $res->item()->holdingbranch();
556 $reserve{'biblionumber'} = $res->item()->biblionumber();
557 $reserve{'barcodenumber'} = $res->item()->barcode();
558 $reserve{'wbrcode'} = $res->branchcode();
559 $reserve{'itemnumber'} = $res->itemnumber();
560 $reserve{'wbrname'} = $res->branch()->branchname();
562 if ( $reserve{'holdingbranch'} eq $reserve{'wbrcode'} ) {
564 # Just because the holdingbranch matches the reserve branch doesn't mean the item
565 # has arrived at the destination, check for an open transfer for the item as well
566 my ( $transfertwhen, $transfertfrom, $transferto ) =
567 C4
::Circulation
::GetTransfers
( $res->itemnumber() );
568 if ( not $transferto or $transferto ne $res->branchcode() ) {
569 $reserve{'atdestination'} = 1;
573 # set found to 1 if reserve is waiting for patron pickup
574 $reserve{'found'} = $res->is_found();
575 $reserve{'intransit'} = $res->is_in_transit();
577 elsif ( $res->priority() > 0 ) {
578 if ( my $item = $res->item() ) {
579 $reserve{'itemnumber'} = $item->id();
580 $reserve{'barcodenumber'} = $item->barcode();
581 $reserve{'item_level_hold'} = 1;
585 $reserve{'expirationdate'} = output_pref
( { dt
=> dt_from_string
( $res->expirationdate ), dateonly
=> 1 } )
586 unless ( !defined( $res->expirationdate ) || $res->expirationdate eq '0000-00-00' );
587 $reserve{'date'} = output_pref
( { dt
=> dt_from_string
( $res->reservedate ), dateonly
=> 1 } );
588 $reserve{'borrowernumber'} = $res->borrowernumber();
589 $reserve{'biblionumber'} = $res->biblionumber();
590 $reserve{'patron'} = $res->borrower;
591 $reserve{'notes'} = $res->reservenotes();
592 $reserve{'waiting_date'} = $res->waitingdate();
593 $reserve{'ccode'} = $res->item() ?
$res->item()->ccode() : undef;
594 $reserve{'barcode'} = $res->item() ?
$res->item()->barcode() : undef;
595 $reserve{'priority'} = $res->priority();
596 $reserve{'lowestPriority'} = $res->lowestPriority();
597 $reserve{'optionloop'} = \
@optionloop;
598 $reserve{'suspend'} = $res->suspend();
599 $reserve{'suspend_until'} = $res->suspend_until();
600 $reserve{'reserve_id'} = $res->reserve_id();
601 $reserve{itemtype
} = $res->itemtype();
602 $reserve{branchcode
} = $res->branchcode();
603 $reserve{object
} = $res;
605 push( @reserveloop, \
%reserve );
608 # get the time for the form name...
613 fixedRank
=> $fixedRank,
618 optionloop
=> \
@optionloop,
619 bibitemloop
=> \
@bibitemloop,
620 itemdata_enumchron
=> $itemdata_enumchron,
621 itemdata_ccode
=> $itemdata_ccode,
623 biblionumber
=> $biblionumber,
624 findborrower
=> $findborrower,
625 title
=> $biblio->title,
626 author
=> $biblio->author,
628 C4
::Search
::enabled_staff_search_views
,
631 $biblioloopiter{biblionumber
} = $biblionumber;
632 $biblioloopiter{title
} = $biblio->title;
633 $biblioloopiter{rank
} = $fixedRank;
634 $biblioloopiter{reserveloop
} = \
@reserveloop;
637 $template->param( reserveloop
=> \
@reserveloop );
640 push @biblioloop, \
%biblioloopiter;
643 $template->param( biblioloop
=> \
@biblioloop );
644 $template->param( biblionumbers
=> $biblionumbers );
645 $template->param( exceeded_maxreserves
=> $exceeded_maxreserves );
646 $template->param( exceeded_holds_per_record
=> $exceeded_holds_per_record );
647 $template->param( subscriptionsnumber
=> CountSubscriptionFromBiblionumber
($biblionumber));
649 if ( C4
::Context
->preference( 'AllowHoldDateInFuture' ) ) {
650 $template->param( reserve_in_future
=> 1 );
654 SuspendHoldsIntranet
=> C4
::Context
->preference('SuspendHoldsIntranet'),
655 AutoResumeSuspendedHolds
=> C4
::Context
->preference('AutoResumeSuspendedHolds'),
659 output_html_with_http_headers
$input, $cookie, $template->output;
661 sub sort_borrowerlist
{
662 my $borrowerslist = shift;
665 uc( $a->{surname
} . $a->{firstname
} ) cmp
666 uc( $b->{surname
} . $b->{firstname
} )