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
55 my $dbh = C4
::Context
->dbh;
57 my ( $template, $borrowernumber, $cookie, $flags ) = get_template_and_user
(
59 template_name
=> "reserve/request.tt",
63 flagsrequired
=> { reserveforothers
=> 'place_holds' },
67 my $multihold = $input->param('multi_hold');
68 $template->param(multi_hold
=> $multihold);
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 AlterPriority
( $where, $reserve_id );
92 } elsif ( $action eq 'cancel' ) {
93 my $reserve_id = $input->param('reserve_id');
94 my $hold = Koha
::Holds
->find( $reserve_id );
95 $hold->cancel if $hold;
96 } elsif ( $action eq 'setLowestPriority' ) {
97 my $reserve_id = $input->param('reserve_id');
98 ToggleLowestPriority
( $reserve_id );
99 } elsif ( $action eq 'toggleSuspend' ) {
100 my $reserve_id = $input->param('reserve_id');
101 my $suspend_until = $input->param('suspend_until');
102 ToggleSuspend
( $reserve_id, $suspend_until );
106 my $patron = Koha
::Patrons
->find( { cardnumber
=> $findborrower } );
108 $borrowernumber_hold = $patron->borrowernumber;
110 my $dt_params = { iDisplayLength
=> -1 };
111 my $results = C4
::Utils
::DataTables
::Members
::search
(
113 searchmember
=> $findborrower,
114 dt_params
=> $dt_params,
117 my $borrowers = $results->{patrons
};
118 if ( scalar @
$borrowers == 1 ) {
119 $borrowernumber_hold = $borrowers->[0]->{borrowernumber
};
120 } elsif ( @
$borrowers ) {
121 $template->param( borrowers
=> $borrowers );
123 $messageborrower = "'$findborrower'";
128 my @biblionumbers = ();
129 my $biblionumbers = $input->param('biblionumbers');
131 @biblionumbers = split '/', $biblionumbers;
133 push @biblionumbers, $input->multi_param('biblionumber');
137 # If we have the borrowernumber because we've performed an action, then we
138 # don't want to try to place another reserve.
139 if ($borrowernumber_hold && !$action) {
140 my $patron = Koha
::Patrons
->find( $borrowernumber_hold );
143 # we check the reserves of the user, and if they can reserve a document
144 # FIXME At this time we have a simple count of reservs, but, later, we could improve the infos "title" ...
146 my $reserves_count = $patron->holds->count;
148 my $new_reserves_count = scalar( @biblionumbers );
150 my $maxreserves = C4
::Context
->preference('maxreserves');
152 && ( $reserves_count + $new_reserves_count > $maxreserves ) )
154 my $new_reserves_allowed =
155 $maxreserves - $reserves_count > 0
156 ?
$maxreserves - $reserves_count
159 $exceeded_maxreserves = 1;
161 new_reserves_allowed
=> $new_reserves_allowed,
162 new_reserves_count
=> $new_reserves_count,
163 reserves_count
=> $reserves_count,
164 maxreserves
=> $maxreserves,
168 # we check the date expiry of the borrower (only if there is an expiry date, otherwise, set to 1 (warn)
169 my $expiry_date = $patron->dateexpiry;
170 my $expiry = 0; # flag set if patron account has expired
171 if ($expiry_date and $expiry_date ne '0000-00-00' and
172 Date_to_Days
(split /-/,$date) > Date_to_Days
(split /-/,$expiry_date)) {
176 # check if the borrower make the reserv in a different branch
177 if ( $patron->branchcode ne C4
::Context
->userenv->{'branch'} ) {
181 my $is_debarred = $patron->is_debarred;
183 borrowernumber
=> $patron->borrowernumber,
184 borrowersurname
=> $patron->surname,
185 borrowerfirstname
=> $patron->firstname,
186 borrowerstreetaddress
=> $patron->address,
187 borrowercity
=> $patron->city,
188 borrowerphone
=> $patron->phone,
189 borrowermobile
=> $patron->mobile,
190 borrowerfax
=> $patron->fax,
191 borrowerphonepro
=> $patron->phonepro,
192 borroweremail
=> $patron->email,
193 borroweremailpro
=> $patron->emailpro,
194 cardnumber
=> $patron->cardnumber,
196 diffbranch
=> $diffbranch,
197 messages
=> $messages,
198 warnings
=> $warnings,
199 restricted
=> $is_debarred,
200 amount_outstanding
=> GetMemberAccountRecords
($patron->borrowernumber),
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;
213 foreach my $biblionumber (@biblionumbers) {
214 next unless $biblionumber =~ m
|^\d
+$|;
216 my %biblioloopiter = ();
218 my $biblio = Koha
::Biblios
->find( $biblionumber );
220 my $force_hold_level;
222 { # CanBookBeReserved
223 my $canReserve = CanBookBeReserved
( $patron->borrowernumber, $biblionumber );
225 if ( $canReserve eq 'OK' ) {
227 #All is OK and we can continue
229 elsif ( $canReserve eq 'tooManyReserves' ) {
230 $exceeded_maxreserves = 1;
232 elsif ( $canReserve eq 'tooManyHoldsForThisRecord' ) {
233 $exceeded_holds_per_record = 1;
234 $biblioloopiter{$canReserve} = 1;
236 elsif ( $canReserve eq 'ageRestricted' ) {
237 $template->param( $canReserve => 1 );
238 $biblioloopiter{$canReserve} = 1;
241 $biblioloopiter{$canReserve} = 1;
245 # For multiple holds per record, if a patron has previously placed a hold,
246 # the patron can only place more holds of the same type. That is, if the
247 # patron placed a record level hold, all the holds the patron places must
248 # be record level. If the patron placed an item level hold, all holds
249 # the patron places must be item level
250 my $holds = Koha
::Holds
->search(
252 borrowernumber
=> $patron->borrowernumber,
253 biblionumber
=> $biblionumber,
257 $force_hold_level = $holds->forced_hold_level();
258 $biblioloopiter{force_hold_level
} = $force_hold_level;
259 $template->param( force_hold_level
=> $force_hold_level );
261 # For a librarian to be able to place multiple record holds for a patron for a record,
262 # we must find out what the maximum number of holds they can place for the patron is
263 my $max_holds_for_record = GetMaxPatronHoldsForRecord
( $patron->borrowernumber, $biblionumber );
264 my $remaining_holds_for_record = $max_holds_for_record - $holds->count();
265 $biblioloopiter{remaining_holds_for_record
} = $max_holds_for_record;
266 $template->param( max_holds_for_record
=> $max_holds_for_record );
267 $template->param( remaining_holds_for_record
=> $remaining_holds_for_record );
269 { # alreadypossession
270 # Check to see if patron is allowed to place holds on records where the
271 # patron already has an item from that record checked out
272 if ( !C4
::Context
->preference('AllowHoldsOnPatronsPossessions')
273 && CheckIfIssuedToPatron
( $patron->borrowernumber, $biblionumber ) )
275 $template->param( alreadypossession
=> 1, );
281 my $count = Koha
::Holds
->search( { biblionumber
=> $biblionumber } )->count();
282 my $totalcount = $count;
284 # FIXME think @optionloop, is maybe obsolete, or must be switchable by a systeme preference fixed rank or not
285 # make priorities options
288 for ( 1 .. $count + 1 ) {
293 selected
=> ( $_ == $count + 1 ),
297 # adding a fixed value for priority options
298 my $fixedRank = $count+1;
300 my %itemnumbers_of_biblioitem;
302 my @hostitems = get_hostitemnumbers_of
($biblionumber);
305 $template->param('hostitemsflag' => 1);
306 push(@itemnumbers, @hostitems);
309 my $items = Koha
::Items
->search({ -or => { biblionumber
=> $biblionumber, itemnumber
=> { in => \
@itemnumbers } } });
311 unless ( $items->count ) {
312 # FIXME Then why do we continue?
313 $template->param('noitems' => 1);
314 $biblioloopiter{noitems
} = 1;
317 ## Here we go backwards again to create hash of biblioitemnumber to itemnumbers,
318 ## when by definition all of the itemnumber have the same biblioitemnumber
319 my ( $iteminfos_of );
320 while ( my $item = $items->next ) {
321 $item = $item->unblessed;
322 my $biblioitemnumber = $item->{biblioitemnumber
};
323 my $itemnumber = $item->{itemnumber
};
324 push( @
{ $itemnumbers_of_biblioitem{$biblioitemnumber} }, $itemnumber );
325 $iteminfos_of->{$itemnumber} = $item;
328 ## Should be same as biblionumber
329 my @biblioitemnumbers = keys %itemnumbers_of_biblioitem;
331 ## Hash of biblioitemnumber to 'biblioitem' table records
332 my $biblioiteminfos_of = GetBiblioItemInfosOf
(@biblioitemnumbers);
334 my $frameworkcode = GetFrameworkCode
( $biblionumber );
335 my @notforloan_avs = Koha
::AuthorisedValues
->search_by_koha_field({ kohafield
=> 'items.notforloan', frameworkcode
=> $frameworkcode });
336 my $notforloan_label_of = { map { $_->authorised_value => $_->lib } @notforloan_avs };
340 my @available_itemtypes;
341 foreach my $biblioitemnumber (@biblioitemnumbers) {
342 my $biblioitem = $biblioiteminfos_of->{$biblioitemnumber};
343 my $num_available = 0;
344 my $num_override = 0;
347 $biblioitem->{force_hold_level
} = $force_hold_level;
349 if ( $biblioitem->{biblioitemnumber
} ne $biblionumber ) {
350 $biblioitem->{hostitemsflag
} = 1;
353 $biblioloopiter{description
} = $biblioitem->{description
};
354 $biblioloopiter{itypename
} = $biblioitem->{description
};
355 if ( $biblioitem->{itemtype
} ) {
357 $biblioitem->{description
} =
358 $itemtypes->{ $biblioitem->{itemtype
} }{description
};
360 $biblioloopiter{imageurl
} =
361 getitemtypeimagelocation
( 'intranet',
362 $itemtypes->{ $biblioitem->{itemtype
} }{imageurl
} );
365 foreach my $itemnumber ( @
{ $itemnumbers_of_biblioitem{$biblioitemnumber} } ) {
366 my $item = $iteminfos_of->{$itemnumber};
368 $item->{force_hold_level
} = $force_hold_level;
370 unless (C4
::Context
->preference('item-level_itypes')) {
371 $item->{itype
} = $biblioitem->{itemtype
};
374 $item->{itypename
} = $itemtypes->{ $item->{itype
} }{description
};
375 $item->{imageurl
} = getitemtypeimagelocation
( 'intranet', $itemtypes->{ $item->{itype
} }{imageurl
} );
376 $item->{homebranch
} = $item->{homebranch
};
378 # if the holdingbranch is different than the homebranch, we show the
379 # holdingbranch of the document too
380 if ( $item->{homebranch
} ne $item->{holdingbranch
} ) {
381 $item->{holdingbranch
} = $item->{holdingbranch
};
384 if($item->{biblionumber
} ne $biblionumber){
385 $item->{hostitemsflag
} = 1;
386 $item->{hosttitle
} = Koha
::Biblios
->find( $item->{biblionumber
} )->title;
389 # if the item is currently on loan, we display its return date and
390 # change the background color
391 my $issue = Koha
::Checkouts
->find( { itemnumber
=> $itemnumber } );
393 $item->{date_due
} = $issue->date_due;
394 $item->{backgroundcolor
} = 'onloan';
398 my $holds = Koha
::Items
->find( $itemnumber )->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->{ReservedForBorrowernumber
} = $p->borrowernumber;
405 $item->{ReservedForSurname
} = $p->surname;
406 $item->{ReservedForFirstname
} = $p->firstname;
407 $item->{ExpectedAtLibrary
} = $first_hold->branchcode;
408 $item->{waitingdate
} = $first_hold->waitingdate;
411 # Management of the notforloan document
412 if ( $item->{notforloan
} ) {
413 $item->{backgroundcolor
} = 'other';
414 $item->{notforloanvalue
} =
415 $notforloan_label_of->{ $item->{notforloan
} };
418 # Management of lost or long overdue items
419 if ( $item->{itemlost
} ) {
421 # FIXME localized strings should never be in Perl code
423 $item->{itemlost
} == 1 ?
"(lost)"
424 : $item->{itemlost
} == 2 ?
"(long overdue)"
426 $item->{backgroundcolor
} = 'other';
427 if ($logged_in_patron->category->hidelostitems && !$showallitems) {
433 # Check the transit status
434 my ( $transfertwhen, $transfertfrom, $transfertto ) =
435 GetTransfers
($itemnumber);
437 if ( defined $transfertwhen && $transfertwhen ne '' ) {
438 $item->{transfertwhen
} = output_pref
({ dt
=> dt_from_string
( $transfertwhen ), dateonly
=> 1 });
439 $item->{transfertfrom
} = $transfertfrom;
440 $item->{transfertto
} = $transfertto;
441 $item->{nocancel
} = 1;
444 # If there is no loan, return and transfer, we show a checkbox.
445 $item->{notforloan
} ||= 0;
447 # if independent branches is on we need to check if the person can reserve
448 # for branches they arent logged in to
449 if ( C4
::Context
->preference("IndependentBranches") ) {
450 if (! C4
::Context
->preference("canreservefromotherbranches")){
451 # cant reserve items so need to check if item homebranch and userenv branch match if not we cant reserve
452 my $userenv = C4
::Context
->userenv;
453 unless ( C4
::Context
->IsSuperLibrarian ) {
454 $item->{cantreserve
} = 1 if ( $item->{homebranch
} ne $userenv->{branch
} );
460 my $patron_unblessed = $patron->unblessed;
461 my $branch = C4
::Circulation
::_GetCircControlBranch
($item, $patron_unblessed);
463 my $branchitemrule = GetBranchItemRule
( $branch, $item->{'itype'} );
465 $item->{'holdallowed'} = $branchitemrule->{'holdallowed'};
467 my $can_item_be_reserved = CanItemBeReserved
( $patron->borrowernumber, $itemnumber );
468 $item->{not_holdable
} = $can_item_be_reserved unless ( $can_item_be_reserved eq 'OK' );
470 $item->{item_level_holds
} = OPACItemHoldsAllowed
( $item, $patron_unblessed);
473 !$item->{cantreserve
}
474 && !$exceeded_maxreserves
475 && IsAvailableForItemLevelRequest
($item, $patron_unblessed)
476 && $can_item_be_reserved eq 'OK'
479 $item->{available
} = 1;
482 push( @available_itemtypes, $item->{itype
} );
484 elsif ( C4
::Context
->preference('AllowHoldPolicyOverride') ) {
485 # If AllowHoldPolicyOverride is set, it should override EVERY restriction, not just branch item rules
486 $item->{override
} = 1;
489 push( @available_itemtypes, $item->{itype
} );
492 # If none of the conditions hold true, then neither override nor available is set and the item cannot be checked
494 # Show serial enumeration when needed
495 if ($item->{enumchron
}) {
496 $itemdata_enumchron = 1;
500 push @
{ $biblioitem->{itemloop
} }, $item;
503 if ( $num_override == scalar( @
{ $biblioitem->{itemloop
} } ) ) { # That is, if all items require an override
504 $template->param( override_required
=> 1 );
505 } elsif ( $num_available == 0 ) {
506 $template->param( none_available
=> 1 );
507 $biblioloopiter{warn} = 1;
508 $biblioloopiter{none_avail
} = 1;
510 $template->param( hiddencount
=> $hiddencount);
512 push @bibitemloop, $biblioitem;
515 @available_itemtypes = uniq
( @available_itemtypes );
516 $template->param( available_itemtypes
=> \
@available_itemtypes );
518 # existingreserves building
520 my @reserves = Koha
::Holds
->search( { biblionumber
=> $biblionumber }, { order_by
=> 'priority' } );
523 my $a_found = $a->found() || '';
524 my $b_found = $a->found() || '';
525 $a_found cmp $b_found;
529 my $priority = $res->priority();
532 for ( my $i = 1 ; $i <= $totalcount ; $i++ ) {
537 selected
=> ( $i == $priority ),
542 if ( $res->is_found() ) {
543 $reserve{'holdingbranch'} = $res->item()->holdingbranch();
544 $reserve{'biblionumber'} = $res->item()->biblionumber();
545 $reserve{'barcodenumber'} = $res->item()->barcode();
546 $reserve{'wbrcode'} = $res->branchcode();
547 $reserve{'itemnumber'} = $res->itemnumber();
548 $reserve{'wbrname'} = $res->branch()->branchname();
550 if ( $reserve{'holdingbranch'} eq $reserve{'wbrcode'} ) {
552 # Just because the holdingbranch matches the reserve branch doesn't mean the item
553 # has arrived at the destination, check for an open transfer for the item as well
554 my ( $transfertwhen, $transfertfrom, $transferto ) =
555 C4
::Circulation
::GetTransfers
( $res->itemnumber() );
556 if ( not $transferto or $transferto ne $res->branchcode() ) {
557 $reserve{'atdestination'} = 1;
561 # set found to 1 if reserve is waiting for patron pickup
562 $reserve{'found'} = $res->is_found();
563 $reserve{'intransit'} = $res->is_in_transit();
565 elsif ( $res->priority() > 0 ) {
566 if ( my $item = $res->item() ) {
567 $reserve{'itemnumber'} = $item->id();
568 $reserve{'barcodenumber'} = $item->barcode();
569 $reserve{'item_level_hold'} = 1;
573 # get borrowers reserve info
574 if ( C4
::Context
->preference('HidePatronName') ) {
575 $reserve{'hidename'} = 1;
576 $reserve{'cardnumber'} = $res->borrower()->cardnumber();
578 $reserve{'expirationdate'} = output_pref
( { dt
=> dt_from_string
( $res->expirationdate ), dateonly
=> 1 } )
579 unless ( !defined( $res->expirationdate ) || $res->expirationdate eq '0000-00-00' );
580 $reserve{'date'} = output_pref
( { dt
=> dt_from_string
( $res->reservedate ), dateonly
=> 1 } );
581 $reserve{'borrowernumber'} = $res->borrowernumber();
582 $reserve{'biblionumber'} = $res->biblionumber();
583 $reserve{'borrowernumber'} = $res->borrowernumber();
584 $reserve{'firstname'} = $res->borrower()->firstname();
585 $reserve{'surname'} = $res->borrower()->surname();
586 $reserve{'notes'} = $res->reservenotes();
587 $reserve{'waiting_date'} = $res->waitingdate();
588 $reserve{'ccode'} = $res->item() ?
$res->item()->ccode() : undef;
589 $reserve{'barcode'} = $res->item() ?
$res->item()->barcode() : undef;
590 $reserve{'priority'} = $res->priority();
591 $reserve{'lowestPriority'} = $res->lowestPriority();
592 $reserve{'optionloop'} = \
@optionloop;
593 $reserve{'suspend'} = $res->suspend();
594 $reserve{'suspend_until'} = $res->suspend_until();
595 $reserve{'reserve_id'} = $res->reserve_id();
596 $reserve{itemtype
} = $res->itemtype();
597 $reserve{branchcode
} = $res->branchcode();
599 push( @reserveloop, \
%reserve );
602 # get the time for the form name...
607 fixedRank
=> $fixedRank,
612 optionloop
=> \
@optionloop,
613 bibitemloop
=> \
@bibitemloop,
614 itemdata_enumchron
=> $itemdata_enumchron,
616 biblionumber
=> $biblionumber,
617 findborrower
=> $findborrower,
618 title
=> $biblio->title,
619 author
=> $biblio->author,
621 C4
::Search
::enabled_staff_search_views
,
624 $template->param( borrower_branchcode
=> $patron->branchcode );
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 );
645 $template->param( multi_hold
=> 1 );
648 if ( C4
::Context
->preference( 'AllowHoldDateInFuture' ) ) {
649 $template->param( reserve_in_future
=> 1 );
653 SuspendHoldsIntranet
=> C4
::Context
->preference('SuspendHoldsIntranet'),
654 AutoResumeSuspendedHolds
=> C4
::Context
->preference('AutoResumeSuspendedHolds'),
658 output_html_with_http_headers
$input, $cookie, $template->output;
660 sub sort_borrowerlist
{
661 my $borrowerslist = shift;
664 uc( $a->{surname
} . $a->{firstname
} ) cmp
665 uc( $b->{surname
} . $b->{firstname
} )