4 #writen 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 under the
11 # terms of the GNU General Public License as published by the Free Software
12 # Foundation; either version 2 of the License, or (at your option) any later
15 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
16 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License along
20 # with Koha; if not, write to the Free Software Foundation, Inc.,
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 script to place reserves/requests
33 use List::MoreUtils qw/uniq/;
34 use Date::Calc qw/Date_to_Days/;
42 use C4::Dates qw/format_date/;
44 use C4::Search; # enabled_staff_search_views
47 my $dbh = C4::Context->dbh;
50 my ( $template, $borrowernumber, $cookie, $flags ) = get_template_and_user(
52 template_name => "reserve/request.tt",
56 flagsrequired => { reserveforothers => 'place_holds' },
60 my $multihold = $input->param('multi_hold');
61 $template->param(multi_hold => $multihold);
62 my $showallitems = $input->param('showallitems');
64 # get Branches and Itemtypes
65 my $branches = GetBranches();
66 my $itemtypes = GetItemTypes();
69 if (C4::Context->userenv && C4::Context->userenv->{'branch'}) {
70 $userbranch = C4::Context->userenv->{'branch'};
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') || '';
84 my $date = C4::Dates->today('iso');
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 $borrowers = Search($findborrower, 'cardnumber');
107 if ($borrowers && @$borrowers) {
108 if ( @$borrowers == 1 ) {
109 $borrowernumber_hold = $borrowers->[0]->{'borrowernumber'};
112 $template->param( borrower_list => sort_borrowerlist($borrowers));
115 $messageborrower = "'$findborrower'";
119 # If we have the borrowernumber because we've performed an action, then we
120 # don't want to try to place another reserve.
121 if ($borrowernumber_hold && !$action) {
122 my $borrowerinfo = GetMember( borrowernumber => $borrowernumber_hold );
125 my $count_reserv = 0;
127 # we check the reserves of the borrower, and if he can reserv a document
128 # FIXME At this time we have a simple count of reservs, but, later, we could improve the infos "title" ...
130 my $number_reserves =
131 GetReserveCount( $borrowerinfo->{'borrowernumber'} );
133 if ( C4::Context->preference('maxreserves') && ($number_reserves >= C4::Context->preference('maxreserves')) ) {
138 # we check the date expiry of the borrower (only if there is an expiry date, otherwise, set to 1 (warn)
139 my $expiry_date = $borrowerinfo->{dateexpiry};
140 my $expiry = 0; # flag set if patron account has expired
141 if ($expiry_date and $expiry_date ne '0000-00-00' and
142 Date_to_Days(split /-/,$date) > Date_to_Days(split /-/,$expiry_date)) {
146 # check if the borrower make the reserv in a different branch
147 if ( $borrowerinfo->{'branchcode'} ne C4::Context->userenv->{'branch'} ) {
152 borrowernumber => $borrowerinfo->{'borrowernumber'},
153 borrowersurname => $borrowerinfo->{'surname'},
154 borrowerfirstname => $borrowerinfo->{'firstname'},
155 borrowerstreetaddress => $borrowerinfo->{'address'},
156 borrowercity => $borrowerinfo->{'city'},
157 borrowerphone => $borrowerinfo->{'phone'},
158 borrowermobile => $borrowerinfo->{'mobile'},
159 borrowerfax => $borrowerinfo->{'fax'},
160 borrowerphonepro => $borrowerinfo->{'phonepro'},
161 borroweremail => $borrowerinfo->{'email'},
162 borroweremailpro => $borrowerinfo->{'emailpro'},
163 borrowercategory => $borrowerinfo->{'category'},
164 borrowerreservs => $count_reserv,
165 cardnumber => $borrowerinfo->{'cardnumber'},
167 diffbranch => $diffbranch,
168 messages => $messages,
169 warnings => $warnings
173 $template->param( messageborrower => $messageborrower );
175 # FIXME launch another time GetMember perhaps until
176 my $borrowerinfo = GetMember( borrowernumber => $borrowernumber_hold );
178 my @biblionumbers = ();
179 my $biblionumbers = $input->param('biblionumbers');
181 @biblionumbers = split '/', $biblionumbers;
183 push @biblionumbers, $input->param('biblionumber');
186 my $itemdata_enumchron = 0;
188 foreach my $biblionumber (@biblionumbers) {
190 my %biblioloopiter = ();
192 my $dat = GetBiblioData($biblionumber);
194 my $canReserve = CanBookBeReserved( $borrowerinfo->{borrowernumber}, $biblionumber );
195 if ( $canReserve eq 'OK' ) {
197 #All is OK and we can continue
199 elsif ( $canReserve eq 'tooManyReserves' ) {
202 elsif ( $canReserve eq 'ageRestricted' ) {
203 $template->param( $canReserve => 1 );
204 $biblioloopiter{$canReserve} = 1;
207 $biblioloopiter{$canReserve} = 1;
210 my $alreadypossession;
211 if (not C4::Context->preference('AllowHoldsOnPatronsPossessions') and CheckIfIssuedToPatron($borrowerinfo->{borrowernumber},$biblionumber)) {
212 $alreadypossession = 1;
215 # get existing reserves .....
216 my $reserves = GetReservesFromBiblionumber({ biblionumber => $biblionumber, all_dates => 1 });
217 my $count = scalar( @$reserves );
218 my $totalcount = $count;
220 my $alreadyreserved = 0;
222 foreach my $res (@$reserves) {
223 if ( defined $res->{found} ) { # found can be 'W' or 'T'
227 if ( defined $borrowerinfo && defined($borrowerinfo->{borrowernumber}) && ($borrowerinfo->{borrowernumber} eq $res->{borrowernumber}) ) {
232 if ( $holds_count ) {
233 $alreadyreserved = 1;
234 $biblioloopiter{warn} = 1;
235 $biblioloopiter{alreadyres} = 1;
239 alreadyreserved => $alreadyreserved,
240 alreadypossession => $alreadypossession,
243 # FIXME think @optionloop, is maybe obsolete, or must be switchable by a systeme preference fixed rank or not
244 # make priorities options
247 for ( 1 .. $count + 1 ) {
252 selected => ( $_ == $count + 1 ),
256 # adding a fixed value for priority options
257 my $fixedRank = $count+1;
260 my %itemnumbers_of_biblioitem;
263 ## $items is array of 'item' table numbers
264 if (my $items = get_itemnumbers_of($biblionumber)->{$biblionumber}){
265 @itemnumbers = @$items;
267 my @hostitems = get_hostitemnumbers_of($biblionumber);
269 $template->param('hostitemsflag' => 1);
270 push(@itemnumbers, @hostitems);
274 $template->param('noitems' => 1);
275 $biblioloopiter{noitems} = 1;
278 ## Hash of item number to 'item' table fields
279 my $iteminfos_of = GetItemInfosOf(@itemnumbers);
281 ## Here we go backwards again to create hash of biblioitemnumber to itemnumbers,
282 ## when by definition all of the itemnumber have the same biblioitemnumber
283 foreach my $itemnumber (@itemnumbers) {
284 my $biblioitemnumber = $iteminfos_of->{$itemnumber}->{biblioitemnumber};
285 push( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} }, $itemnumber );
288 ## Should be same as biblionumber
289 my @biblioitemnumbers = keys %itemnumbers_of_biblioitem;
291 my $notforloan_label_of = get_notforloan_label_of();
293 ## Hash of biblioitemnumber to 'biblioitem' table records
294 my $biblioiteminfos_of = GetBiblioItemInfosOf(@biblioitemnumbers);
298 foreach my $biblioitemnumber (@biblioitemnumbers) {
299 my $biblioitem = $biblioiteminfos_of->{$biblioitemnumber};
300 my $num_available = 0;
301 my $num_override = 0;
304 $biblioitem->{description} =
305 $itemtypes->{ $biblioitem->{itemtype} }{description};
306 if($biblioitem->{biblioitemnumber} ne $biblionumber){
307 $biblioitem->{hostitemsflag}=1;
309 $biblioloopiter{description} = $biblioitem->{description};
310 $biblioloopiter{itypename} = $biblioitem->{description};
311 $biblioloopiter{imageurl} =
312 getitemtypeimagelocation('intranet', $itemtypes->{$biblioitem->{itemtype}}{imageurl});
314 foreach my $itemnumber ( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} } ) {
315 my $item = $iteminfos_of->{$itemnumber};
317 unless (C4::Context->preference('item-level_itypes')) {
318 $item->{itype} = $biblioitem->{itemtype};
321 $item->{itypename} = $itemtypes->{ $item->{itype} }{description};
322 $item->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtypes->{ $item->{itype} }{imageurl} );
323 $item->{homebranchname} = $branches->{ $item->{homebranch} }{branchname};
325 # if the holdingbranch is different than the homebranch, we show the
326 # holdingbranch of the document too
327 if ( $item->{homebranch} ne $item->{holdingbranch} ) {
328 $item->{holdingbranchname} =
329 $branches->{ $item->{holdingbranch} }{branchname};
332 if($item->{biblionumber} ne $biblionumber){
333 $item->{hostitemsflag}=1;
334 $item->{hosttitle} = GetBiblioData($item->{biblionumber})->{title};
337 # if the item is currently on loan, we display its return date and
338 # change the background color
339 my $issues= GetItemIssue($itemnumber);
340 if ( $issues->{'date_due'} ) {
341 $item->{date_due} = format_sqldatetime($issues->{date_due});
342 $item->{backgroundcolor} = 'onloan';
346 my ($reservedate,$reservedfor,$expectedAt,$reserve_id,$wait) = GetReservesFromItemnumber($itemnumber);
347 if ( defined $reservedate ) {
348 my $ItemBorrowerReserveInfo = GetMember( borrowernumber => $reservedfor );
350 $item->{backgroundcolor} = 'reserved';
351 $item->{reservedate} = format_date($reservedate);
352 $item->{ReservedForBorrowernumber} = $reservedfor;
353 $item->{ReservedForSurname} = $ItemBorrowerReserveInfo->{'surname'};
354 $item->{ReservedForFirstname} = $ItemBorrowerReserveInfo->{'firstname'};
355 $item->{ExpectedAtLibrary} = $branches->{$expectedAt}{branchname};
356 $item->{waitingdate} = $wait;
359 # Management of the notforloan document
360 if ( $item->{notforloan} ) {
361 $item->{backgroundcolor} = 'other';
362 $item->{notforloanvalue} =
363 $notforloan_label_of->{ $item->{notforloan} };
366 # Management of lost or long overdue items
367 if ( $item->{itemlost} ) {
369 # FIXME localized strings should never be in Perl code
371 $item->{itemlost} == 1 ? "(lost)"
372 : $item->{itemlost} == 2 ? "(long overdue)"
374 $item->{backgroundcolor} = 'other';
375 if (GetHideLostItemsPreference($borrowernumber) && !$showallitems) {
381 # Check the transit status
382 my ( $transfertwhen, $transfertfrom, $transfertto ) =
383 GetTransfers($itemnumber);
385 if ( defined $transfertwhen && $transfertwhen ne '' ) {
386 $item->{transfertwhen} = format_date($transfertwhen);
387 $item->{transfertfrom} =
388 $branches->{$transfertfrom}{branchname};
389 $item->{transfertto} = $branches->{$transfertto}{branchname};
390 $item->{nocancel} = 1;
393 # If there is no loan, return and transfer, we show a checkbox.
394 $item->{notforloan} ||= 0;
396 # if independent branches is on we need to check if the person can reserve
397 # for branches they arent logged in to
398 if ( C4::Context->preference("IndependentBranches") ) {
399 if (! C4::Context->preference("canreservefromotherbranches")){
400 # cant reserve items so need to check if item homebranch and userenv branch match if not we cant reserve
401 my $userenv = C4::Context->userenv;
402 unless ( C4::Context->IsSuperLibrarian ) {
403 $item->{cantreserve} = 1 if ( $item->{homebranch} ne $userenv->{branch} );
408 my $branch = C4::Circulation::_GetCircControlBranch($item, $borrowerinfo);
410 my $branchitemrule = GetBranchItemRule( $branch, $item->{'itype'} );
411 my $policy_holdallowed = 1;
413 $item->{'holdallowed'} = $branchitemrule->{'holdallowed'};
415 if ( $branchitemrule->{'holdallowed'} == 0 ||
416 ( $branchitemrule->{'holdallowed'} == 1 &&
417 $borrowerinfo->{'branchcode'} ne $item->{'homebranch'} ) ) {
418 $policy_holdallowed = 0;
423 && !$item->{cantreserve}
424 && IsAvailableForItemLevelRequest($item, $borrowerinfo)
425 && CanItemBeReserved(
426 $borrowerinfo->{borrowernumber}, $itemnumber
430 $item->{available} = 1;
433 elsif ( C4::Context->preference('AllowHoldPolicyOverride') ) {
434 # If AllowHoldPolicyOverride is set, it should override EVERY restriction, not just branch item rules
435 $item->{override} = 1;
439 # If none of the conditions hold true, then neither override nor available is set and the item cannot be checked
441 # Show serial enumeration when needed
442 if ($item->{enumchron}) {
443 $itemdata_enumchron = 1;
446 push @{ $biblioitem->{itemloop} }, $item;
449 if ( $num_override == scalar( @{ $biblioitem->{itemloop} } ) ) { # That is, if all items require an override
450 $template->param( override_required => 1 );
451 } elsif ( $num_available == 0 ) {
452 $template->param( none_available => 1 );
453 $biblioloopiter{warn} = 1;
454 $biblioloopiter{none_avail} = 1;
456 $template->param( hiddencount => $hiddencount);
458 push @bibitemloop, $biblioitem;
461 # existingreserves building
463 $reserves = GetReservesFromBiblionumber({ biblionumber => $biblionumber, all_dates => 1 });
464 foreach my $res ( sort {
465 my $a_found = $a->{found} || '';
466 my $b_found = $a->{found} || '';
467 $a_found cmp $b_found;
471 for ( my $i = 1 ; $i <= $totalcount ; $i++ ) {
476 selected => ( $i == $res->{priority} ),
481 if ( defined $res->{'found'} && ($res->{'found'} eq 'W' || $res->{'found'} eq 'T' )) {
482 my $item = $res->{'itemnumber'};
483 $item = GetBiblioFromItemNumber($item,undef);
485 $reserve{'holdingbranch'}=$item->{'holdingbranch'};
486 $reserve{'biblionumber'}=$item->{'biblionumber'};
487 $reserve{'barcodenumber'} = $item->{'barcode'};
488 $reserve{'wbrcode'} = $res->{'branchcode'};
489 $reserve{'itemnumber'} = $res->{'itemnumber'};
490 $reserve{'wbrname'} = $branches->{$res->{'branchcode'}}->{'branchname'};
491 if($reserve{'holdingbranch'} eq $reserve{'wbrcode'}){
492 # Just because the holdingbranch matches the reserve branch doesn't mean the item
493 # has arrived at the destination, check for an open transfer for the item as well
494 my ( $transfertwhen, $transfertfrom, $transferto ) = C4::Circulation::GetTransfers( $res->{itemnumber} );
495 if ( not $transferto or $transferto ne $res->{branchcode} ) {
496 $reserve{'atdestination'} = 1;
499 # set found to 1 if reserve is waiting for patron pickup
500 $reserve{'found'} = 1 if $res->{'found'} eq 'W';
501 $reserve{'intransit'} = 1 if $res->{'found'} eq 'T';
502 } elsif ($res->{priority} > 0) {
503 if (defined($res->{itemnumber})) {
504 my $item = GetItem($res->{itemnumber});
505 $reserve{'itemnumber'} = $res->{'itemnumber'};
506 $reserve{'barcodenumber'} = $item->{'barcode'};
507 $reserve{'item_level_hold'} = 1;
511 # get borrowers reserve info
512 my $reserveborrowerinfo = GetMember( borrowernumber => $res->{'borrowernumber'} );
513 if (C4::Context->preference('HidePatronName')){
514 $reserve{'hidename'} = 1;
515 $reserve{'cardnumber'} = $reserveborrowerinfo->{'cardnumber'};
517 $reserve{'expirationdate'} = format_date( $res->{'expirationdate'} )
518 unless ( !defined($res->{'expirationdate'}) || $res->{'expirationdate'} eq '0000-00-00' );
519 $reserve{'date'} = format_date( $res->{'reservedate'} );
520 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
521 $reserve{'biblionumber'} = $res->{'biblionumber'};
522 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
523 $reserve{'firstname'} = $reserveborrowerinfo->{'firstname'};
524 $reserve{'surname'} = $reserveborrowerinfo->{'surname'};
525 $reserve{'notes'} = $res->{'reservenotes'};
527 ( ( defined $res->{'found'} and $res->{'found'} eq 'W' ) or ( $res->{'priority'} eq '0' ) );
528 $reserve{'constrainttypea'} = ( $res->{'constrainttype'} eq 'a' );
529 $reserve{'constrainttypeo'} = ( $res->{'constrainttype'} eq 'o' );
530 $reserve{'voldesc'} = $res->{'volumeddesc'};
531 $reserve{'ccode'} = $res->{'ccode'};
532 $reserve{'barcode'} = $res->{'barcode'};
533 $reserve{'priority'} = $res->{'priority'};
534 $reserve{'lowestPriority'} = $res->{'lowestPriority'};
535 $reserve{'optionloop'} = \@optionloop;
536 $reserve{'suspend'} = $res->{'suspend'};
537 $reserve{'suspend_until'} = $res->{'suspend_until'};
538 $reserve{'reserve_id'} = $res->{'reserve_id'};
540 if ( C4::Context->preference('IndependentBranches') && $flags->{'superlibrarian'} != 1 ) {
541 $reserve{'branchloop'} = [ GetBranchDetail($res->{'branchcode'}) ];
543 $reserve{'branchloop'} = GetBranchesLoop($res->{'branchcode'});
546 push( @reserveloop, \%reserve );
549 # get the time for the form name...
553 branchloop => GetBranchesLoop($userbranch),
555 fixedRank => $fixedRank,
560 optionloop => \@optionloop,
561 bibitemloop => \@bibitemloop,
562 itemdata_enumchron => $itemdata_enumchron,
564 biblionumber => $biblionumber,
565 findborrower => $findborrower,
566 title => $dat->{title},
567 author => $dat->{author},
569 C4::Search::enabled_staff_search_views,
571 if (defined $borrowerinfo && exists $borrowerinfo->{'branchcode'}) {
573 borrower_branchname => $branches->{$borrowerinfo->{'branchcode'}}->{'branchname'},
574 borrower_branchcode => $borrowerinfo->{'branchcode'},
578 $biblioloopiter{biblionumber} = $biblionumber;
579 $biblioloopiter{title} = $dat->{title};
580 $biblioloopiter{rank} = $fixedRank;
581 $biblioloopiter{reserveloop} = \@reserveloop;
584 $template->param( reserveloop => \@reserveloop );
587 push @biblioloop, \%biblioloopiter;
590 $template->param( biblioloop => \@biblioloop );
591 $template->param( biblionumbers => $biblionumbers );
592 $template->param( maxreserves => $maxreserves );
595 $template->param( multi_hold => 1 );
598 if ( C4::Context->preference( 'AllowHoldDateInFuture' ) ) {
599 $template->param( reserve_in_future => 1 );
603 SuspendHoldsIntranet => C4::Context->preference('SuspendHoldsIntranet'),
604 AutoResumeSuspendedHolds => C4::Context->preference('AutoResumeSuspendedHolds'),
608 output_html_with_http_headers $input, $cookie, $template->output;
610 sub sort_borrowerlist {
611 my $borrowerslist = shift;
614 uc( $a->{surname} . $a->{firstname} ) cmp
615 uc( $b->{surname} . $b->{firstname} )