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 ) = get_template_and_user(
52 template_name => "reserve/request.tmpl",
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();
68 my $default = C4::Context->userenv->{branch};
72 foreach my $branchcode ( sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %$branches ) {
73 push @values, $branchcode;
74 $label_of{$branchcode} = $branches->{$branchcode}->{branchname};
76 my $CGIbranch = CGI::scrolling_list(
81 -labels => \%label_of,
86 # Select borrowers infos
87 my $findborrower = $input->param('findborrower');
88 $findborrower = '' unless defined $findborrower;
89 $findborrower =~ s|,| |g;
90 my $borrowernumber_hold = $input->param('borrowernumber') || '';
96 my $date = C4::Dates->today('iso');
97 my $action = $input->param('action');
100 if ( $action eq 'move' ) {
101 my $where = $input->param('where');
102 my $borrowernumber = $input->param('borrowernumber');
103 my $biblionumber = $input->param('biblionumber');
104 AlterPriority( $where, $borrowernumber, $biblionumber );
105 } elsif ( $action eq 'cancel' ) {
106 my $borrowernumber = $input->param('borrowernumber');
107 my $biblionumber = $input->param('biblionumber');
108 CancelReserve( $biblionumber, '', $borrowernumber );
109 } elsif ( $action eq 'setLowestPriority' ) {
110 my $borrowernumber = $input->param('borrowernumber');
111 my $biblionumber = $input->param('biblionumber');
112 ToggleLowestPriority( $borrowernumber, $biblionumber );
113 } elsif ( $action eq 'toggleSuspend' ) {
114 my $borrowernumber = $input->param('borrowernumber');
115 my $biblionumber = $input->param('biblionumber');
116 my $suspend_until = $input->param('suspend_until');
117 ToggleSuspend( $borrowernumber, $biblionumber, $suspend_until );
121 my $borrowers = Search($findborrower, 'cardnumber');
123 if ($borrowers && @$borrowers) {
124 if ( @$borrowers == 1 ) {
125 $borrowernumber_hold = $borrowers->[0]->{'borrowernumber'};
128 $borrowerslist = $borrowers;
131 $messageborrower = "'$findborrower'";
135 # If we have the borrowernumber because we've performed an action, then we
136 # don't want to try to place another reserve.
137 if ($borrowernumber_hold && !$action) {
138 my $borrowerinfo = GetMember( borrowernumber => $borrowernumber_hold );
141 my $count_reserv = 0;
144 # we check the reserves of the borrower, and if he can reserv a document
145 # FIXME At this time we have a simple count of reservs, but, later, we could improve the infos "title" ...
147 my $number_reserves =
148 GetReserveCount( $borrowerinfo->{'borrowernumber'} );
150 if ( C4::Context->preference('maxreserves') && ($number_reserves >= C4::Context->preference('maxreserves')) ) {
155 # we check the date expiry of the borrower (only if there is an expiry date, otherwise, set to 1 (warn)
156 my $expiry_date = $borrowerinfo->{dateexpiry};
157 my $expiry = 0; # flag set if patron account has expired
158 if ($expiry_date and $expiry_date ne '0000-00-00' and
159 Date_to_Days(split /-/,$date) > Date_to_Days(split /-/,$expiry_date)) {
160 $messages = $expiry = 1;
163 # check if the borrower make the reserv in a different branch
164 if ( $borrowerinfo->{'branchcode'} ne C4::Context->userenv->{'branch'} ) {
170 borrowernumber => $borrowerinfo->{'borrowernumber'},
171 borrowersurname => $borrowerinfo->{'surname'},
172 borrowerfirstname => $borrowerinfo->{'firstname'},
173 borrowerstreetaddress => $borrowerinfo->{'address'},
174 borrowercity => $borrowerinfo->{'city'},
175 borrowerphone => $borrowerinfo->{'phone'},
176 borrowermobile => $borrowerinfo->{'mobile'},
177 borrowerfax => $borrowerinfo->{'fax'},
178 borrowerphonepro => $borrowerinfo->{'phonepro'},
179 borroweremail => $borrowerinfo->{'email'},
180 borroweremailpro => $borrowerinfo->{'emailpro'},
181 borrowercategory => $borrowerinfo->{'category'},
182 borrowerreservs => $count_reserv,
183 cardnumber => $borrowerinfo->{'cardnumber'},
184 maxreserves => $maxreserves,
186 diffbranch => $diffbranch,
187 messages => $messages,
188 warnings => $warnings
192 $template->param( messageborrower => $messageborrower );
194 my $CGIselectborrower;
195 if ($borrowerslist) {
199 foreach my $borrower (
202 . $a->{firstname}) cmp uc($b->{surname}
207 push @values, $borrower->{borrowernumber};
209 $labels{ $borrower->{borrowernumber} } = sprintf(
210 '%s, %s ... (%s - %s) ... %s',
211 $borrower->{surname} ||'', $borrower->{firstname} || '',
212 $borrower->{cardnumber} || '', $borrower->{categorycode} || '',
213 $borrower->{address} || '',
217 $CGIselectborrower = CGI::scrolling_list(
218 -name => 'borrowernumber',
226 # FIXME launch another time GetMember perhaps until
227 my $borrowerinfo = GetMember( borrowernumber => $borrowernumber_hold );
229 my @biblionumbers = ();
230 my $biblionumbers = $input->param('biblionumbers');
232 @biblionumbers = split '/', $biblionumbers;
234 push @biblionumbers, $input->param('biblionumber');
237 my $itemdata_enumchron = 0;
239 foreach my $biblionumber (@biblionumbers) {
241 my %biblioloopiter = ();
244 my $dat = GetBiblioData($biblionumber);
246 unless ( CanBookBeReserved($borrowerinfo->{borrowernumber}, $biblionumber) ) {
250 if (not C4::Context->preference('AllowHoldsOnPatronsPossessions') and CheckIfIssuedToPatron($borrowerinfo->{borrowernumber},$biblionumber)) {
252 $alreadypossession = 1;
255 # get existing reserves .....
256 my ( $count, $reserves ) = GetReservesFromBiblionumber($biblionumber,1);
257 my $totalcount = $count;
260 foreach my $res (@$reserves) {
261 if ( defined $res->{found} && $res->{found} eq 'W' ) {
265 if ( defined $borrowerinfo && ($borrowerinfo->{borrowernumber} eq $res->{borrowernumber}) ) {
267 $alreadyreserved = 1;
268 $biblioloopiter{warn} = 1;
269 $biblioloopiter{alreadyres} = 1;
273 $template->param( alreadyreserved => $alreadyreserved,
274 messages => $messages,
275 warnings => $warnings,
276 maxreserves=>$maxreserves,
277 alreadypossession => $alreadypossession,
281 # FIXME think @optionloop, is maybe obsolete, or must be switchable by a systeme preference fixed rank or not
282 # make priorities options
285 for ( 1 .. $count + 1 ) {
290 selected => ( $_ == $count + 1 ),
294 # adding a fixed value for priority options
295 my $fixedRank = $count+1;
298 my %itemnumbers_of_biblioitem;
301 ## $items is array of 'item' table numbers
302 if (my $items = get_itemnumbers_of($biblionumber)->{$biblionumber}){
303 @itemnumbers = @$items;
305 my @hostitems = get_hostitemnumbers_of($biblionumber);
307 $template->param('hostitemsflag' => 1);
308 push(@itemnumbers, @hostitems);
312 $template->param('noitems' => 1);
313 $biblioloopiter{noitems} = 1;
316 ## Hash of item number to 'item' table fields
317 my $iteminfos_of = GetItemInfosOf(@itemnumbers);
319 ## Here we go backwards again to create hash of biblioitemnumber to itemnumbers,
320 ## when by definition all of the itemnumber have the same biblioitemnumber
321 foreach my $itemnumber (@itemnumbers) {
322 my $biblioitemnumber = $iteminfos_of->{$itemnumber}->{biblioitemnumber};
323 push( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} }, $itemnumber );
326 ## Should be same as biblionumber
327 my @biblioitemnumbers = keys %itemnumbers_of_biblioitem;
329 my $notforloan_label_of = get_notforloan_label_of();
331 ## Hash of biblioitemnumber to 'biblioitem' table records
332 my $biblioiteminfos_of = GetBiblioItemInfosOf(@biblioitemnumbers);
336 foreach my $biblioitemnumber (@biblioitemnumbers) {
337 my $biblioitem = $biblioiteminfos_of->{$biblioitemnumber};
338 my $num_available = 0;
339 my $num_override = 0;
342 $biblioitem->{description} =
343 $itemtypes->{ $biblioitem->{itemtype} }{description};
344 if($biblioitem->{biblioitemnumber} ne $biblionumber){
345 $biblioitem->{hostitemsflag}=1;
347 $biblioloopiter{description} = $biblioitem->{description};
348 $biblioloopiter{itypename} = $biblioitem->{description};
349 $biblioloopiter{imageurl} =
350 getitemtypeimagelocation('intranet', $itemtypes->{$biblioitem->{itemtype}}{imageurl});
352 foreach my $itemnumber ( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} } ) {
353 my $item = $iteminfos_of->{$itemnumber};
355 unless (C4::Context->preference('item-level_itypes')) {
356 $item->{itype} = $biblioitem->{itemtype};
359 $item->{itypename} = $itemtypes->{ $item->{itype} }{description};
360 $item->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtypes->{ $item->{itype} }{imageurl} );
361 $item->{homebranchname} = $branches->{ $item->{homebranch} }{branchname};
363 # if the holdingbranch is different than the homebranch, we show the
364 # holdingbranch of the document too
365 if ( $item->{homebranch} ne $item->{holdingbranch} ) {
366 $item->{holdingbranchname} =
367 $branches->{ $item->{holdingbranch} }{branchname};
370 if($item->{biblionumber} ne $biblionumber){
371 $item->{hostitemsflag}=1;
372 $item->{hosttitle} = GetBiblioData($item->{biblionumber})->{title};
376 $item->{itemcallnumber} = $item->{itemcallnumber};
378 # if the item is currently on loan, we display its return date and
379 # change the background color
380 my $issues= GetItemIssue($itemnumber);
381 if ( $issues->{'date_due'} ) {
382 $item->{date_due} = format_sqldatetime($issues->{date_due});
383 $item->{backgroundcolor} = 'onloan';
387 my ($reservedate,$reservedfor,$expectedAt) = GetReservesFromItemnumber($itemnumber);
388 my $ItemBorrowerReserveInfo = GetMember( borrowernumber => $reservedfor );
390 if ( defined $reservedate ) {
391 $item->{backgroundcolor} = 'reserved';
392 $item->{reservedate} = format_date($reservedate);
393 $item->{ReservedForBorrowernumber} = $reservedfor;
394 $item->{ReservedForSurname} = $ItemBorrowerReserveInfo->{'surname'};
395 $item->{ReservedForFirstname} = $ItemBorrowerReserveInfo->{'firstname'};
396 $item->{ExpectedAtLibrary} = $branches->{$expectedAt}{branchname};
400 # Management of the notforloan document
401 if ( $item->{notforloan} ) {
402 $item->{backgroundcolor} = 'other';
403 $item->{notforloanvalue} =
404 $notforloan_label_of->{ $item->{notforloan} };
407 # Management of lost or long overdue items
408 if ( $item->{itemlost} ) {
410 # FIXME localized strings should never be in Perl code
412 $item->{itemlost} == 1 ? "(lost)"
413 : $item->{itemlost} == 2 ? "(long overdue)"
415 $item->{backgroundcolor} = 'other';
416 if (GetHideLostItemsPreference($borrowernumber) && !$showallitems) {
422 # Check the transit status
423 my ( $transfertwhen, $transfertfrom, $transfertto ) =
424 GetTransfers($itemnumber);
426 if ( defined $transfertwhen && $transfertwhen ne '' ) {
427 $item->{transfertwhen} = format_date($transfertwhen);
428 $item->{transfertfrom} =
429 $branches->{$transfertfrom}{branchname};
430 $item->{transfertto} = $branches->{$transfertto}{branchname};
431 $item->{nocancel} = 1;
434 # If there is no loan, return and transfer, we show a checkbox.
435 $item->{notforloan} = $item->{notforloan} || 0;
437 # if independent branches is on we need to check if the person can reserve
438 # for branches they arent logged in to
439 if ( C4::Context->preference("IndependantBranches") ) {
440 if (! C4::Context->preference("canreservefromotherbranches")){
441 # cant reserve items so need to check if item homebranch and userenv branch match if not we cant reserve
442 my $userenv = C4::Context->userenv;
443 if ( ($userenv) && ( $userenv->{flags} % 2 != 1 ) ) {
444 $item->{cantreserve} = 1 if ( $item->{homebranch} ne $userenv->{branch} );
449 my $branch = C4::Circulation::_GetCircControlBranch($item, $borrowerinfo);
451 my $branchitemrule = GetBranchItemRule( $branch, $item->{'itype'} );
452 my $policy_holdallowed = 1;
454 $item->{'holdallowed'} = $branchitemrule->{'holdallowed'};
456 if ( $branchitemrule->{'holdallowed'} == 0 ||
457 ( $branchitemrule->{'holdallowed'} == 1 &&
458 $borrowerinfo->{'branchcode'} ne $item->{'homebranch'} ) ) {
459 $policy_holdallowed = 0;
464 && !$item->{cantreserve}
465 && IsAvailableForItemLevelRequest($itemnumber)
466 && CanItemBeReserved(
467 $borrowerinfo->{borrowernumber}, $itemnumber
471 $item->{available} = 1;
474 elsif ( C4::Context->preference('AllowHoldPolicyOverride') ) {
476 # If AllowHoldPolicyOverride is set, it should override EVERY restriction, not just branch item rules
477 $item->{override} = 1;
481 # If none of the conditions hold true, then neither override nor available is set and the item cannot be checked
483 # FIXME: move this to a pm
484 my $sth2 = $dbh->prepare("SELECT * FROM reserves WHERE borrowernumber=? AND itemnumber=? AND found='W'");
485 $sth2->execute($item->{ReservedForBorrowernumber},$item->{itemnumber});
486 while (my $wait_hashref = $sth2->fetchrow_hashref) {
487 $item->{waitingdate} = format_date($wait_hashref->{waitingdate});
490 # Show serial enumeration when needed
491 if ($item->{enumchron}) {
492 $itemdata_enumchron = 1;
495 push @{ $biblioitem->{itemloop} }, $item;
498 if ( $num_override == scalar( @{ $biblioitem->{itemloop} } ) ) { # That is, if all items require an override
499 $template->param( override_required => 1 );
500 } elsif ( $num_available == 0 ) {
501 $template->param( none_available => 1 );
502 $template->param( warnings => 1 );
503 $biblioloopiter{warn} = 1;
504 $biblioloopiter{none_avail} = 1;
506 $template->param( hiddencount => $hiddencount);
508 push @bibitemloop, $biblioitem;
511 # existingreserves building
513 ( $count, $reserves ) = GetReservesFromBiblionumber($biblionumber,1);
514 foreach my $res ( sort {
515 my $a_found = $a->{found} || '';
516 my $b_found = $a->{found} || '';
517 $a_found cmp $b_found;
521 for ( my $i = 1 ; $i <= $totalcount ; $i++ ) {
526 selected => ( $i == $res->{priority} ),
531 if ( defined $res->{'found'} && $res->{'found'} eq 'W' || $res->{'found'} eq 'T' ) {
532 my $item = $res->{'itemnumber'};
533 $item = GetBiblioFromItemNumber($item,undef);
535 $reserve{'holdingbranch'}=$item->{'holdingbranch'};
536 $reserve{'biblionumber'}=$item->{'biblionumber'};
537 $reserve{'barcodenumber'} = $item->{'barcode'};
538 $reserve{'wbrcode'} = $res->{'branchcode'};
539 $reserve{'itemnumber'} = $res->{'itemnumber'};
540 $reserve{'wbrname'} = $branches->{$res->{'branchcode'}}->{'branchname'};
541 if($reserve{'holdingbranch'} eq $reserve{'wbrcode'}){
542 $reserve{'atdestination'} = 1;
544 # set found to 1 if reserve is waiting for patron pickup
545 $reserve{'found'} = 1 if $res->{'found'} eq 'W';
546 $reserve{'intransit'} = 1 if $res->{'found'} eq 'T';
547 } elsif ($res->{priority} > 0) {
548 if (defined($res->{itemnumber})) {
549 my $item = GetItem($res->{itemnumber});
550 $reserve{'itemnumber'} = $res->{'itemnumber'};
551 $reserve{'barcodenumber'} = $item->{'barcode'};
552 $reserve{'item_level_hold'} = 1;
556 # get borrowers reserve info
557 my $reserveborrowerinfo = GetMember( borrowernumber => $res->{'borrowernumber'} );
558 if (C4::Context->preference('HidePatronName')){
559 $reserve{'hidename'} = 1;
560 $reserve{'cardnumber'} = $reserveborrowerinfo->{'cardnumber'};
562 $reserve{'expirationdate'} = format_date( $res->{'expirationdate'} )
563 unless ( !defined($res->{'expirationdate'}) || $res->{'expirationdate'} eq '0000-00-00' );
564 $reserve{'date'} = format_date( $res->{'reservedate'} );
565 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
566 $reserve{'biblionumber'} = $res->{'biblionumber'};
567 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
568 $reserve{'firstname'} = $reserveborrowerinfo->{'firstname'};
569 $reserve{'surname'} = $reserveborrowerinfo->{'surname'};
570 $reserve{'notes'} = $res->{'reservenotes'};
572 ( ( defined $res->{'found'} and $res->{'found'} eq 'W' ) or ( $res->{'priority'} eq '0' ) );
573 $reserve{'constrainttypea'} = ( $res->{'constrainttype'} eq 'a' );
574 $reserve{'constrainttypeo'} = ( $res->{'constrainttype'} eq 'o' );
575 $reserve{'voldesc'} = $res->{'volumeddesc'};
576 $reserve{'ccode'} = $res->{'ccode'};
577 $reserve{'barcode'} = $res->{'barcode'};
578 $reserve{'priority'} = $res->{'priority'};
579 $reserve{'lowestPriority'} = $res->{'lowestPriority'};
580 $reserve{'branchloop'} = GetBranchesLoop($res->{'branchcode'});
581 $reserve{'optionloop'} = \@optionloop;
582 $reserve{'suspend'} = $res->{'suspend'};
583 $reserve{'suspend_until'} = $res->{'suspend_until'};
584 push( @reserveloop, \%reserve );
587 # get the time for the form name...
591 CGIbranch => $CGIbranch,
594 fixedRank => $fixedRank,
599 optionloop => \@optionloop,
600 bibitemloop => \@bibitemloop,
601 itemdata_enumchron => $itemdata_enumchron,
603 biblionumber => $biblionumber,
604 findborrower => $findborrower,
605 title => $dat->{title},
606 author => $dat->{author},
608 C4::Search::enabled_staff_search_views,
610 if (defined $borrowerinfo && exists $borrowerinfo->{'branchcode'}) {
612 borrower_branchname => $branches->{$borrowerinfo->{'branchcode'}}->{'branchname'},
613 borrower_branchcode => $borrowerinfo->{'branchcode'},
616 $template->param(CGIselectborrower => $CGIselectborrower) if defined $CGIselectborrower;
618 $biblioloopiter{biblionumber} = $biblionumber;
619 $biblioloopiter{title} = $dat->{title};
620 $biblioloopiter{rank} = $fixedRank;
621 $biblioloopiter{reserveloop} = \@reserveloop;
624 $template->param( reserveloop => \@reserveloop );
627 push @biblioloop, \%biblioloopiter;
630 $template->param( biblioloop => \@biblioloop );
631 $template->param( biblionumbers => $biblionumbers );
632 $template->param( DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar() );
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;