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') || '';
95 my $date = C4::Dates->today('iso');
96 my $action = $input->param('action');
99 if ( $action eq 'move' ) {
100 my $where = $input->param('where');
101 my $borrowernumber = $input->param('borrowernumber');
102 my $biblionumber = $input->param('biblionumber');
103 AlterPriority( $where, $borrowernumber, $biblionumber );
104 } elsif ( $action eq 'cancel' ) {
105 my $borrowernumber = $input->param('borrowernumber');
106 my $biblionumber = $input->param('biblionumber');
107 CancelReserve( $biblionumber, '', $borrowernumber );
108 } elsif ( $action eq 'setLowestPriority' ) {
109 my $borrowernumber = $input->param('borrowernumber');
110 my $biblionumber = $input->param('biblionumber');
111 ToggleLowestPriority( $borrowernumber, $biblionumber );
112 } elsif ( $action eq 'toggleSuspend' ) {
113 my $borrowernumber = $input->param('borrowernumber');
114 my $biblionumber = $input->param('biblionumber');
115 my $suspend_until = $input->param('suspend_until');
116 ToggleSuspend( $borrowernumber, $biblionumber, $suspend_until );
120 my $borrowers = Search($findborrower, 'cardnumber');
122 if ($borrowers && @$borrowers) {
123 if ( @$borrowers == 1 ) {
124 $borrowernumber_hold = $borrowers->[0]->{'borrowernumber'};
127 $template->param( borrower_list => sort_borrowerlist($borrowers));
130 $messageborrower = "'$findborrower'";
134 # If we have the borrowernumber because we've performed an action, then we
135 # don't want to try to place another reserve.
136 if ($borrowernumber_hold && !$action) {
137 my $borrowerinfo = GetMember( borrowernumber => $borrowernumber_hold );
140 my $count_reserv = 0;
143 # we check the reserves of the borrower, and if he can reserv a document
144 # FIXME At this time we have a simple count of reservs, but, later, we could improve the infos "title" ...
146 my $number_reserves =
147 GetReserveCount( $borrowerinfo->{'borrowernumber'} );
149 if ( C4::Context->preference('maxreserves') && ($number_reserves >= C4::Context->preference('maxreserves')) ) {
154 # we check the date expiry of the borrower (only if there is an expiry date, otherwise, set to 1 (warn)
155 my $expiry_date = $borrowerinfo->{dateexpiry};
156 my $expiry = 0; # flag set if patron account has expired
157 if ($expiry_date and $expiry_date ne '0000-00-00' and
158 Date_to_Days(split /-/,$date) > Date_to_Days(split /-/,$expiry_date)) {
159 $messages = $expiry = 1;
162 # check if the borrower make the reserv in a different branch
163 if ( $borrowerinfo->{'branchcode'} ne C4::Context->userenv->{'branch'} ) {
169 borrowernumber => $borrowerinfo->{'borrowernumber'},
170 borrowersurname => $borrowerinfo->{'surname'},
171 borrowerfirstname => $borrowerinfo->{'firstname'},
172 borrowerstreetaddress => $borrowerinfo->{'address'},
173 borrowercity => $borrowerinfo->{'city'},
174 borrowerphone => $borrowerinfo->{'phone'},
175 borrowermobile => $borrowerinfo->{'mobile'},
176 borrowerfax => $borrowerinfo->{'fax'},
177 borrowerphonepro => $borrowerinfo->{'phonepro'},
178 borroweremail => $borrowerinfo->{'email'},
179 borroweremailpro => $borrowerinfo->{'emailpro'},
180 borrowercategory => $borrowerinfo->{'category'},
181 borrowerreservs => $count_reserv,
182 cardnumber => $borrowerinfo->{'cardnumber'},
183 maxreserves => $maxreserves,
185 diffbranch => $diffbranch,
186 messages => $messages,
187 warnings => $warnings
191 $template->param( messageborrower => $messageborrower );
193 # FIXME launch another time GetMember perhaps until
194 my $borrowerinfo = GetMember( borrowernumber => $borrowernumber_hold );
196 my @biblionumbers = ();
197 my $biblionumbers = $input->param('biblionumbers');
199 @biblionumbers = split '/', $biblionumbers;
201 push @biblionumbers, $input->param('biblionumber');
204 my $itemdata_enumchron = 0;
206 foreach my $biblionumber (@biblionumbers) {
208 my %biblioloopiter = ();
211 my $dat = GetBiblioData($biblionumber);
213 unless ( CanBookBeReserved($borrowerinfo->{borrowernumber}, $biblionumber) ) {
217 # get existing reserves .....
218 my ( $count, $reserves ) = GetReservesFromBiblionumber($biblionumber,1);
219 my $totalcount = $count;
222 foreach my $res (@$reserves) {
223 if ( defined $res->{found} && $res->{found} eq 'W' ) {
227 if ( defined $borrowerinfo && ($borrowerinfo->{borrowernumber} eq $res->{borrowernumber}) ) {
229 $alreadyreserved = 1;
230 $biblioloopiter{warn} = 1;
231 $biblioloopiter{alreadyres} = 1;
235 $template->param( alreadyreserved => $alreadyreserved,
236 messages => $messages,
237 warnings => $warnings,
238 maxreserves=>$maxreserves
242 # FIXME think @optionloop, is maybe obsolete, or must be switchable by a systeme preference fixed rank or not
243 # make priorities options
246 for ( 1 .. $count + 1 ) {
251 selected => ( $_ == $count + 1 ),
255 # adding a fixed value for priority options
256 my $fixedRank = $count+1;
259 my %itemnumbers_of_biblioitem;
262 ## $items is array of 'item' table numbers
263 if (my $items = get_itemnumbers_of($biblionumber)->{$biblionumber}){
264 @itemnumbers = @$items;
266 my @hostitems = get_hostitemnumbers_of($biblionumber);
268 $template->param('hostitemsflag' => 1);
269 push(@itemnumbers, @hostitems);
273 $template->param('noitems' => 1);
274 $biblioloopiter{noitems} = 1;
277 ## Hash of item number to 'item' table fields
278 my $iteminfos_of = GetItemInfosOf(@itemnumbers);
280 ## Here we go backwards again to create hash of biblioitemnumber to itemnumbers,
281 ## when by definition all of the itemnumber have the same biblioitemnumber
282 foreach my $itemnumber (@itemnumbers) {
283 my $biblioitemnumber = $iteminfos_of->{$itemnumber}->{biblioitemnumber};
284 push( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} }, $itemnumber );
287 ## Should be same as biblionumber
288 my @biblioitemnumbers = keys %itemnumbers_of_biblioitem;
290 my $notforloan_label_of = get_notforloan_label_of();
292 ## Hash of biblioitemnumber to 'biblioitem' table records
293 my $biblioiteminfos_of = GetBiblioItemInfosOf(@biblioitemnumbers);
297 foreach my $biblioitemnumber (@biblioitemnumbers) {
298 my $biblioitem = $biblioiteminfos_of->{$biblioitemnumber};
299 my $num_available = 0;
300 my $num_override = 0;
303 $biblioitem->{description} =
304 $itemtypes->{ $biblioitem->{itemtype} }{description};
305 if($biblioitem->{biblioitemnumber} ne $biblionumber){
306 $biblioitem->{hostitemsflag}=1;
308 $biblioloopiter{description} = $biblioitem->{description};
309 $biblioloopiter{itypename} = $biblioitem->{description};
310 $biblioloopiter{imageurl} =
311 getitemtypeimagelocation('intranet', $itemtypes->{$biblioitem->{itemtype}}{imageurl});
313 foreach my $itemnumber ( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} } ) {
314 my $item = $iteminfos_of->{$itemnumber};
316 unless (C4::Context->preference('item-level_itypes')) {
317 $item->{itype} = $biblioitem->{itemtype};
320 $item->{itypename} = $itemtypes->{ $item->{itype} }{description};
321 $item->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtypes->{ $item->{itype} }{imageurl} );
322 $item->{homebranchname} = $branches->{ $item->{homebranch} }{branchname};
324 # if the holdingbranch is different than the homebranch, we show the
325 # holdingbranch of the document too
326 if ( $item->{homebranch} ne $item->{holdingbranch} ) {
327 $item->{holdingbranchname} =
328 $branches->{ $item->{holdingbranch} }{branchname};
331 if($item->{biblionumber} ne $biblionumber){
332 $item->{hostitemsflag}=1;
333 $item->{hosttitle} = GetBiblioData($item->{biblionumber})->{title};
337 $item->{itemcallnumber} = $item->{itemcallnumber};
339 # if the item is currently on loan, we display its return date and
340 # change the background color
341 my $issues= GetItemIssue($itemnumber);
342 if ( $issues->{'date_due'} ) {
343 $item->{date_due} = format_sqldatetime($issues->{date_due});
344 $item->{backgroundcolor} = 'onloan';
348 my ($reservedate,$reservedfor,$expectedAt) = GetReservesFromItemnumber($itemnumber);
349 my $ItemBorrowerReserveInfo = GetMember( borrowernumber => $reservedfor );
351 if ( defined $reservedate ) {
352 $item->{backgroundcolor} = 'reserved';
353 $item->{reservedate} = format_date($reservedate);
354 $item->{ReservedForBorrowernumber} = $reservedfor;
355 $item->{ReservedForSurname} = $ItemBorrowerReserveInfo->{'surname'};
356 $item->{ReservedForFirstname} = $ItemBorrowerReserveInfo->{'firstname'};
357 $item->{ExpectedAtLibrary} = $branches->{$expectedAt}{branchname};
361 # Management of the notforloan document
362 if ( $item->{notforloan} ) {
363 $item->{backgroundcolor} = 'other';
364 $item->{notforloanvalue} =
365 $notforloan_label_of->{ $item->{notforloan} };
368 # Management of lost or long overdue items
369 if ( $item->{itemlost} ) {
371 # FIXME localized strings should never be in Perl code
373 $item->{itemlost} == 1 ? "(lost)"
374 : $item->{itemlost} == 2 ? "(long overdue)"
376 $item->{backgroundcolor} = 'other';
377 if (GetHideLostItemsPreference($borrowernumber) && !$showallitems) {
383 # Check the transit status
384 my ( $transfertwhen, $transfertfrom, $transfertto ) =
385 GetTransfers($itemnumber);
387 if ( defined $transfertwhen && $transfertwhen ne '' ) {
388 $item->{transfertwhen} = format_date($transfertwhen);
389 $item->{transfertfrom} =
390 $branches->{$transfertfrom}{branchname};
391 $item->{transfertto} = $branches->{$transfertto}{branchname};
392 $item->{nocancel} = 1;
395 # If there is no loan, return and transfer, we show a checkbox.
396 $item->{notforloan} = $item->{notforloan} || 0;
398 # if independent branches is on we need to check if the person can reserve
399 # for branches they arent logged in to
400 if ( C4::Context->preference("IndependantBranches") ) {
401 if (! C4::Context->preference("canreservefromotherbranches")){
402 # cant reserve items so need to check if item homebranch and userenv branch match if not we cant reserve
403 my $userenv = C4::Context->userenv;
404 if ( ($userenv) && ( $userenv->{flags} % 2 != 1 ) ) {
405 $item->{cantreserve} = 1 if ( $item->{homebranch} ne $userenv->{branch} );
410 my $branch = C4::Circulation::_GetCircControlBranch($item, $borrowerinfo);
412 my $branchitemrule = GetBranchItemRule( $branch, $item->{'itype'} );
413 my $policy_holdallowed = 1;
415 $item->{'holdallowed'} = $branchitemrule->{'holdallowed'};
417 if ( $branchitemrule->{'holdallowed'} == 0 ||
418 ( $branchitemrule->{'holdallowed'} == 1 &&
419 $borrowerinfo->{'branchcode'} ne $item->{'homebranch'} ) ) {
420 $policy_holdallowed = 0;
425 && !$item->{cantreserve}
426 && IsAvailableForItemLevelRequest($itemnumber)
427 && CanItemBeReserved(
428 $borrowerinfo->{borrowernumber}, $itemnumber
432 $item->{available} = 1;
435 elsif ( C4::Context->preference('AllowHoldPolicyOverride') ) {
437 # If AllowHoldPolicyOverride is set, it should override EVERY restriction, not just branch item rules
438 $item->{override} = 1;
442 # If none of the conditions hold true, then neither override nor available is set and the item cannot be checked
444 # FIXME: move this to a pm
445 my $sth2 = $dbh->prepare("SELECT * FROM reserves WHERE borrowernumber=? AND itemnumber=? AND found='W'");
446 $sth2->execute($item->{ReservedForBorrowernumber},$item->{itemnumber});
447 while (my $wait_hashref = $sth2->fetchrow_hashref) {
448 $item->{waitingdate} = format_date($wait_hashref->{waitingdate});
451 # Show serial enumeration when needed
452 if ($item->{enumchron}) {
453 $itemdata_enumchron = 1;
456 push @{ $biblioitem->{itemloop} }, $item;
459 if ( $num_override == scalar( @{ $biblioitem->{itemloop} } ) ) { # That is, if all items require an override
460 $template->param( override_required => 1 );
461 } elsif ( $num_available == 0 ) {
462 $template->param( none_available => 1 );
463 $template->param( warnings => 1 );
464 $biblioloopiter{warn} = 1;
465 $biblioloopiter{none_avail} = 1;
467 $template->param( hiddencount => $hiddencount);
469 push @bibitemloop, $biblioitem;
472 # existingreserves building
474 ( $count, $reserves ) = GetReservesFromBiblionumber($biblionumber,1);
475 foreach my $res ( sort {
476 my $a_found = $a->{found} || '';
477 my $b_found = $a->{found} || '';
478 $a_found cmp $b_found;
482 for ( my $i = 1 ; $i <= $totalcount ; $i++ ) {
487 selected => ( $i == $res->{priority} ),
492 if ( defined $res->{'found'} && $res->{'found'} eq 'W' || $res->{'found'} eq 'T' ) {
493 my $item = $res->{'itemnumber'};
494 $item = GetBiblioFromItemNumber($item,undef);
496 $reserve{'holdingbranch'}=$item->{'holdingbranch'};
497 $reserve{'biblionumber'}=$item->{'biblionumber'};
498 $reserve{'barcodenumber'} = $item->{'barcode'};
499 $reserve{'wbrcode'} = $res->{'branchcode'};
500 $reserve{'itemnumber'} = $res->{'itemnumber'};
501 $reserve{'wbrname'} = $branches->{$res->{'branchcode'}}->{'branchname'};
502 if($reserve{'holdingbranch'} eq $reserve{'wbrcode'}){
503 $reserve{'atdestination'} = 1;
505 # set found to 1 if reserve is waiting for patron pickup
506 $reserve{'found'} = 1 if $res->{'found'} eq 'W';
507 $reserve{'intransit'} = 1 if $res->{'found'} eq 'T';
508 } elsif ($res->{priority} > 0) {
509 if (defined($res->{itemnumber})) {
510 my $item = GetItem($res->{itemnumber});
511 $reserve{'itemnumber'} = $res->{'itemnumber'};
512 $reserve{'barcodenumber'} = $item->{'barcode'};
513 $reserve{'item_level_hold'} = 1;
517 # get borrowers reserve info
518 my $reserveborrowerinfo = GetMember( borrowernumber => $res->{'borrowernumber'} );
519 if (C4::Context->preference('HidePatronName')){
520 $reserve{'hidename'} = 1;
521 $reserve{'cardnumber'} = $reserveborrowerinfo->{'cardnumber'};
523 $reserve{'expirationdate'} = format_date( $res->{'expirationdate'} )
524 unless ( !defined($res->{'expirationdate'}) || $res->{'expirationdate'} eq '0000-00-00' );
525 $reserve{'date'} = format_date( $res->{'reservedate'} );
526 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
527 $reserve{'biblionumber'} = $res->{'biblionumber'};
528 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
529 $reserve{'firstname'} = $reserveborrowerinfo->{'firstname'};
530 $reserve{'surname'} = $reserveborrowerinfo->{'surname'};
531 $reserve{'notes'} = $res->{'reservenotes'};
533 ( ( defined $res->{'found'} and $res->{'found'} eq 'W' ) or ( $res->{'priority'} eq '0' ) );
534 $reserve{'constrainttypea'} = ( $res->{'constrainttype'} eq 'a' );
535 $reserve{'constrainttypeo'} = ( $res->{'constrainttype'} eq 'o' );
536 $reserve{'voldesc'} = $res->{'volumeddesc'};
537 $reserve{'ccode'} = $res->{'ccode'};
538 $reserve{'barcode'} = $res->{'barcode'};
539 $reserve{'priority'} = $res->{'priority'};
540 $reserve{'lowestPriority'} = $res->{'lowestPriority'};
541 $reserve{'branchloop'} = GetBranchesLoop($res->{'branchcode'});
542 $reserve{'optionloop'} = \@optionloop;
543 $reserve{'suspend'} = $res->{'suspend'};
544 $reserve{'suspend_until'} = $res->{'suspend_until'};
545 push( @reserveloop, \%reserve );
548 # get the time for the form name...
552 CGIbranch => $CGIbranch,
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( DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar() );
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} )