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 CancelReserve
({ reserve_id
=> $reserve_id });
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 $biblionumbers = $input->param('biblionumbers');
130 @biblionumbers = split '/', $biblionumbers;
132 push @biblionumbers, $input->multi_param('biblionumber');
136 # If we have the borrowernumber because we've performed an action, then we
137 # don't want to try to place another reserve.
138 if ($borrowernumber_hold && !$action) {
139 my $patron = Koha
::Patrons
->find( $borrowernumber_hold );
142 # we check the reserves of the user, and if they can reserve a document
143 # FIXME At this time we have a simple count of reservs, but, later, we could improve the infos "title" ...
145 my $reserves_count = $patron->holds->count;
147 my $new_reserves_count = scalar( @biblionumbers );
149 my $maxreserves = C4
::Context
->preference('maxreserves');
151 && ( $reserves_count + $new_reserves_count > $maxreserves ) )
153 my $new_reserves_allowed =
154 $maxreserves - $reserves_count > 0
155 ?
$maxreserves - $reserves_count
158 $exceeded_maxreserves = 1;
160 new_reserves_allowed
=> $new_reserves_allowed,
161 new_reserves_count
=> $new_reserves_count,
162 reserves_count
=> $reserves_count,
163 maxreserves
=> $maxreserves,
167 # we check the date expiry of the borrower (only if there is an expiry date, otherwise, set to 1 (warn)
168 my $expiry_date = $patron->dateexpiry;
169 my $expiry = 0; # flag set if patron account has expired
170 if ($expiry_date and $expiry_date ne '0000-00-00' and
171 Date_to_Days
(split /-/,$date) > Date_to_Days
(split /-/,$expiry_date)) {
175 # check if the borrower make the reserv in a different branch
176 if ( $patron->branchcode ne C4
::Context
->userenv->{'branch'} ) {
180 my $is_debarred = $patron->is_debarred;
182 borrowernumber
=> $patron->borrowernumber,
183 borrowersurname
=> $patron->surname,
184 borrowerfirstname
=> $patron->firstname,
185 borrowerstreetaddress
=> $patron->address,
186 borrowercity
=> $patron->city,
187 borrowerphone
=> $patron->phone,
188 borrowermobile
=> $patron->mobile,
189 borrowerfax
=> $patron->fax,
190 borrowerphonepro
=> $patron->phonepro,
191 borroweremail
=> $patron->email,
192 borroweremailpro
=> $patron->emailpro,
193 cardnumber
=> $patron->cardnumber,
195 diffbranch
=> $diffbranch,
196 messages
=> $messages,
197 warnings
=> $warnings,
198 restricted
=> $is_debarred,
199 amount_outstanding
=> GetMemberAccountRecords
($patron->borrowernumber),
203 $template->param( messageborrower
=> $messageborrower );
205 # FIXME launch another time GetMember perhaps until (Joubu: Why?)
206 my $patron = Koha
::Patrons
->find( $borrowernumber_hold );
208 my $logged_in_patron = Koha
::Patrons
->find( $borrowernumber );
210 my $itemdata_enumchron = 0;
212 foreach my $biblionumber (@biblionumbers) {
213 next unless $biblionumber =~ m
|^\d
+$|;
215 my %biblioloopiter = ();
217 my $biblio = Koha
::Biblios
->find( $biblionumber );
219 my $force_hold_level;
221 { # CanBookBeReserved
222 my $canReserve = CanBookBeReserved
( $patron->borrowernumber, $biblionumber );
224 if ( $canReserve eq 'OK' ) {
226 #All is OK and we can continue
228 elsif ( $canReserve eq 'tooManyReserves' ) {
229 $exceeded_maxreserves = 1;
231 elsif ( $canReserve eq 'tooManyHoldsForThisRecord' ) {
232 $exceeded_holds_per_record = 1;
233 $biblioloopiter{$canReserve} = 1;
235 elsif ( $canReserve eq 'ageRestricted' ) {
236 $template->param( $canReserve => 1 );
237 $biblioloopiter{$canReserve} = 1;
240 $biblioloopiter{$canReserve} = 1;
244 # For multiple holds per record, if a patron has previously placed a hold,
245 # the patron can only place more holds of the same type. That is, if the
246 # patron placed a record level hold, all the holds the patron places must
247 # be record level. If the patron placed an item level hold, all holds
248 # the patron places must be item level
249 my $holds = Koha
::Holds
->search(
251 borrowernumber
=> $patron->borrowernumber,
252 biblionumber
=> $biblionumber,
256 $force_hold_level = $holds->forced_hold_level();
257 $biblioloopiter{force_hold_level
} = $force_hold_level;
258 $template->param( force_hold_level
=> $force_hold_level );
260 # For a librarian to be able to place multiple record holds for a patron for a record,
261 # we must find out what the maximum number of holds they can place for the patron is
262 my $max_holds_for_record = GetMaxPatronHoldsForRecord
( $patron->borrowernumber, $biblionumber );
263 my $remaining_holds_for_record = $max_holds_for_record - $holds->count();
264 $biblioloopiter{remaining_holds_for_record
} = $max_holds_for_record;
265 $template->param( max_holds_for_record
=> $max_holds_for_record );
266 $template->param( remaining_holds_for_record
=> $remaining_holds_for_record );
268 { # alreadypossession
269 # Check to see if patron is allowed to place holds on records where the
270 # patron already has an item from that record checked out
271 my $alreadypossession;
272 if ( !C4
::Context
->preference('AllowHoldsOnPatronsPossessions')
273 && CheckIfIssuedToPatron
( $patron->borrowernumber, $biblionumber ) )
275 $template->param( alreadypossession
=> $alreadypossession, );
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;
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 # get borrowers reserve info
572 if ( C4
::Context
->preference('HidePatronName') ) {
573 $reserve{'hidename'} = 1;
574 $reserve{'cardnumber'} = $res->borrower()->cardnumber();
576 $reserve{'expirationdate'} = output_pref
( { dt
=> dt_from_string
( $res->expirationdate ), dateonly
=> 1 } )
577 unless ( !defined( $res->expirationdate ) || $res->expirationdate eq '0000-00-00' );
578 $reserve{'date'} = output_pref
( { dt
=> dt_from_string
( $res->reservedate ), dateonly
=> 1 } );
579 $reserve{'borrowernumber'} = $res->borrowernumber();
580 $reserve{'biblionumber'} = $res->biblionumber();
581 $reserve{'borrowernumber'} = $res->borrowernumber();
582 $reserve{'firstname'} = $res->borrower()->firstname();
583 $reserve{'surname'} = $res->borrower()->surname();
584 $reserve{'notes'} = $res->reservenotes();
585 $reserve{'waiting_date'} = $res->waitingdate();
586 $reserve{'ccode'} = $res->item() ?
$res->item()->ccode() : undef;
587 $reserve{'barcode'} = $res->item() ?
$res->item()->barcode() : undef;
588 $reserve{'priority'} = $res->priority();
589 $reserve{'lowestPriority'} = $res->lowestPriority();
590 $reserve{'optionloop'} = \
@optionloop;
591 $reserve{'suspend'} = $res->suspend();
592 $reserve{'suspend_until'} = $res->suspend_until();
593 $reserve{'reserve_id'} = $res->reserve_id();
594 $reserve{itemtype
} = $res->itemtype();
595 $reserve{branchcode
} = $res->branchcode();
597 push( @reserveloop, \
%reserve );
600 # get the time for the form name...
605 fixedRank
=> $fixedRank,
610 optionloop
=> \
@optionloop,
611 bibitemloop
=> \
@bibitemloop,
612 itemdata_enumchron
=> $itemdata_enumchron,
614 biblionumber
=> $biblionumber,
615 findborrower
=> $findborrower,
616 title
=> $biblio->title,
617 author
=> $biblio->author,
619 C4
::Search
::enabled_staff_search_views
,
622 $template->param( borrower_branchcode
=> $patron->branchcode );
625 $biblioloopiter{biblionumber
} = $biblionumber;
626 $biblioloopiter{title
} = $biblio->title;
627 $biblioloopiter{rank
} = $fixedRank;
628 $biblioloopiter{reserveloop
} = \
@reserveloop;
631 $template->param( reserveloop
=> \
@reserveloop );
634 push @biblioloop, \
%biblioloopiter;
637 $template->param( biblioloop
=> \
@biblioloop );
638 $template->param( biblionumbers
=> $biblionumbers );
639 $template->param( exceeded_maxreserves
=> $exceeded_maxreserves );
640 $template->param( exceeded_holds_per_record
=> $exceeded_holds_per_record );
643 $template->param( multi_hold
=> 1 );
646 if ( C4
::Context
->preference( 'AllowHoldDateInFuture' ) ) {
647 $template->param( reserve_in_future
=> 1 );
651 SuspendHoldsIntranet
=> C4
::Context
->preference('SuspendHoldsIntranet'),
652 AutoResumeSuspendedHolds
=> C4
::Context
->preference('AutoResumeSuspendedHolds'),
656 output_html_with_http_headers
$input, $cookie, $template->output;
658 sub sort_borrowerlist
{
659 my $borrowerslist = shift;
662 uc( $a->{surname
} . $a->{firstname
} ) cmp
663 uc( $b->{surname
} . $b->{firstname
} )