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
46 my $dbh = C4::Context->dbh;
49 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
51 template_name => "reserve/request.tmpl",
55 flagsrequired => { reserveforothers => 'place_holds' },
59 my $multihold = $input->param('multi_hold');
60 $template->param(multi_hold => $multihold);
61 my $showallitems = $input->param('showallitems');
63 # get Branches and Itemtypes
64 my $branches = GetBranches();
65 my $itemtypes = GetItemTypes();
67 my $default = C4::Context->userenv->{branch};
71 foreach my $branchcode (sort keys %{$branches} ) {
72 push @values, $branchcode;
73 $label_of{$branchcode} = $branches->{$branchcode}->{branchname};
75 my $CGIbranch = CGI::scrolling_list(
80 -labels => \%label_of,
85 # Select borrowers infos
86 my $findborrower = $input->param('findborrower');
87 $findborrower = '' unless defined $findborrower;
88 $findborrower =~ s|,| |g;
89 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 );
115 my ( $count, $borrowers ) =
116 SearchMember($findborrower, 'cardnumber', 'web' );
118 my @borrowers = @$borrowers;
121 $messageborrower = "'$findborrower'";
123 elsif ( @borrowers == 1 ) {
124 $borrowernumber_hold = $borrowers[0]->{'borrowernumber'};
127 $borrowerslist = \@borrowers;
131 # If we have the borrowernumber because we've performed an action, then we
132 # don't want to try to place another reserve.
133 if ($borrowernumber_hold && !$action) {
134 my $borrowerinfo = GetMemberDetails( $borrowernumber_hold );
137 my $count_reserv = 0;
140 # we check the reserves of the borrower, and if he can reserv a document
141 # FIXME At this time we have a simple count of reservs, but, later, we could improve the infos "title" ...
143 my $number_reserves =
144 GetReserveCount( $borrowerinfo->{'borrowernumber'} );
146 if ( C4::Context->preference('maxreserves') && ($number_reserves >= C4::Context->preference('maxreserves')) ) {
151 # we check the date expiry of the borrower (only if there is an expiry date, otherwise, set to 1 (warn)
152 my $expiry_date = $borrowerinfo->{dateexpiry};
153 my $expiry = 0; # flag set if patron account has expired
154 if ($expiry_date and $expiry_date ne '0000-00-00' and
155 Date_to_Days(split /-/,$date) > Date_to_Days(split /-/,$expiry_date)) {
156 $messages = $expiry = 1;
159 # check if the borrower make the reserv in a different branch
160 if ( $borrowerinfo->{'branchcode'} ne C4::Context->userenv->{'branch'} ) {
166 borrowernumber => $borrowerinfo->{'borrowernumber'},
167 borrowersurname => $borrowerinfo->{'surname'},
168 borrowerfirstname => $borrowerinfo->{'firstname'},
169 borrowerstreetaddress => $borrowerinfo->{'address'},
170 borrowercity => $borrowerinfo->{'city'},
171 borrowerphone => $borrowerinfo->{'phone'},
172 borrowermobile => $borrowerinfo->{'mobile'},
173 borrowerfax => $borrowerinfo->{'fax'},
174 borrowerphonepro => $borrowerinfo->{'phonepro'},
175 borroweremail => $borrowerinfo->{'email'},
176 borroweremailpro => $borrowerinfo->{'emailpro'},
177 borrowercategory => $borrowerinfo->{'category'},
178 borrowerreservs => $count_reserv,
179 cardnumber => $borrowerinfo->{'cardnumber'},
180 maxreserves => $maxreserves,
182 diffbranch => $diffbranch,
183 messages => $messages,
184 warnings => $warnings
188 $template->param( messageborrower => $messageborrower );
190 my $CGIselectborrower;
191 if ($borrowerslist) {
195 foreach my $borrower (
198 . $a->{firstname} cmp $b->{surname}
203 push @values, $borrower->{borrowernumber};
205 $labels{ $borrower->{borrowernumber} } = sprintf(
206 '%s, %s ... (%s - %s) ... %s',
207 $borrower->{surname} ||'', $borrower->{firstname} || '',
208 $borrower->{cardnumber} || '', $borrower->{categorycode} || '',
209 $borrower->{address} || '',
213 $CGIselectborrower = CGI::scrolling_list(
214 -name => 'borrowernumber',
222 # FIXME launch another time GetMemberDetails perhaps until
223 my $borrowerinfo = GetMemberDetails( $borrowernumber_hold );
225 my @biblionumbers = ();
226 my $biblionumbers = $input->param('biblionumbers');
228 @biblionumbers = split '/', $biblionumbers;
230 push @biblionumbers, $input->param('biblionumber');
233 my $itemdata_enumchron = 0;
235 foreach my $biblionumber (@biblionumbers) {
237 my %biblioloopiter = ();
240 my $dat = GetBiblioData($biblionumber);
242 unless ( CanBookBeReserved($borrowerinfo->{borrowernumber}, $biblionumber) ) {
246 # get existing reserves .....
247 my ( $count, $reserves ) = GetReservesFromBiblionumber($biblionumber,1);
248 my $totalcount = $count;
251 foreach my $res (@$reserves) {
252 if ( defined $res->{found} && $res->{found} eq 'W' ) {
256 if ( defined $borrowerinfo && ($borrowerinfo->{borrowernumber} eq $res->{borrowernumber}) ) {
258 $alreadyreserved = 1;
259 $biblioloopiter{warn} = 1;
260 $biblioloopiter{alreadyres} = 1;
264 $template->param( alreadyreserved => $alreadyreserved,
265 messages => $messages,
266 warnings => $warnings,
267 maxreserves=>$maxreserves
271 # FIXME think @optionloop, is maybe obsolete, or must be switchable by a systeme preference fixed rank or not
272 # make priorities options
275 for ( 1 .. $count + 1 ) {
280 selected => ( $_ == $count + 1 ),
284 # adding a fixed value for priority options
285 my $fixedRank = $count+1;
288 my %itemnumbers_of_biblioitem;
291 ## $items is array of 'item' table numbers
292 if (my $items = get_itemnumbers_of($biblionumber)->{$biblionumber}){
293 @itemnumbers = @$items;
295 my @hostitems = get_hostitemnumbers_of($biblionumber);
297 $template->param('hostitemsflag' => 1);
298 push(@itemnumbers, @hostitems);
302 $template->param('noitems' => 1);
303 $biblioloopiter{noitems} = 1;
306 ## Hash of item number to 'item' table fields
307 my $iteminfos_of = GetItemInfosOf(@itemnumbers);
309 ## Here we go backwards again to create hash of biblioitemnumber to itemnumbers,
310 ## when by definition all of the itemnumber have the same biblioitemnumber
311 foreach my $itemnumber (@itemnumbers) {
312 my $biblioitemnumber = $iteminfos_of->{$itemnumber}->{biblioitemnumber};
313 push( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} }, $itemnumber );
316 ## Should be same as biblionumber
317 my @biblioitemnumbers = keys %itemnumbers_of_biblioitem;
319 my $notforloan_label_of = get_notforloan_label_of();
321 ## Hash of biblioitemnumber to 'biblioitem' table records
322 my $biblioiteminfos_of = GetBiblioItemInfosOf(@biblioitemnumbers);
326 foreach my $biblioitemnumber (@biblioitemnumbers) {
327 my $biblioitem = $biblioiteminfos_of->{$biblioitemnumber};
328 my $num_available = 0;
329 my $num_override = 0;
332 $biblioitem->{description} =
333 $itemtypes->{ $biblioitem->{itemtype} }{description};
334 if($biblioitem->{biblioitemnumber} ne $biblionumber){
335 $biblioitem->{hostitemsflag}=1;
337 $biblioloopiter{description} = $biblioitem->{description};
338 $biblioloopiter{itypename} = $biblioitem->{description};
339 $biblioloopiter{imageurl} =
340 getitemtypeimagelocation('intranet', $itemtypes->{$biblioitem->{itemtype}}{imageurl});
342 foreach my $itemnumber ( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} } ) {
343 my $item = $iteminfos_of->{$itemnumber};
345 unless (C4::Context->preference('item-level_itypes')) {
346 $item->{itype} = $biblioitem->{itemtype};
349 $item->{itypename} = $itemtypes->{ $item->{itype} }{description};
350 $item->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtypes->{ $item->{itype} }{imageurl} );
351 $item->{homebranchname} = $branches->{ $item->{homebranch} }{branchname};
353 # if the holdingbranch is different than the homebranch, we show the
354 # holdingbranch of the document too
355 if ( $item->{homebranch} ne $item->{holdingbranch} ) {
356 $item->{holdingbranchname} =
357 $branches->{ $item->{holdingbranch} }{branchname};
360 if($item->{biblionumber} ne $biblionumber){
361 $item->{hostitemsflag}=1;
362 $item->{hosttitle} = GetBiblioData($item->{biblionumber})->{title};
366 $item->{itemcallnumber} = $item->{itemcallnumber};
368 # if the item is currently on loan, we display its return date and
369 # change the background color
370 my $issues= GetItemIssue($itemnumber);
371 if ( $issues->{'date_due'} ) {
372 $item->{date_due} = format_date($issues->{'date_due'});
373 $item->{backgroundcolor} = 'onloan';
377 my ($reservedate,$reservedfor,$expectedAt) = GetReservesFromItemnumber($itemnumber);
378 my $ItemBorrowerReserveInfo = GetMemberDetails( $reservedfor, 0);
380 if ( defined $reservedate ) {
381 $item->{backgroundcolor} = 'reserved';
382 $item->{reservedate} = format_date($reservedate);
383 $item->{ReservedForBorrowernumber} = $reservedfor;
384 $item->{ReservedForSurname} = $ItemBorrowerReserveInfo->{'surname'};
385 $item->{ReservedForFirstname} = $ItemBorrowerReserveInfo->{'firstname'};
386 $item->{ExpectedAtLibrary} = $branches->{$expectedAt}{branchname};
390 # Management of the notforloan document
391 if ( $item->{notforloan} ) {
392 $item->{backgroundcolor} = 'other';
393 $item->{notforloanvalue} =
394 $notforloan_label_of->{ $item->{notforloan} };
397 # Management of lost or long overdue items
398 if ( $item->{itemlost} ) {
400 # FIXME localized strings should never be in Perl code
402 $item->{itemlost} == 1 ? "(lost)"
403 : $item->{itemlost} == 2 ? "(long overdue)"
405 $item->{backgroundcolor} = 'other';
406 if (GetHideLostItemsPreference($borrowernumber) && !$showallitems) {
412 # Check the transit status
413 my ( $transfertwhen, $transfertfrom, $transfertto ) =
414 GetTransfers($itemnumber);
416 if ( defined $transfertwhen && $transfertwhen ne '' ) {
417 $item->{transfertwhen} = format_date($transfertwhen);
418 $item->{transfertfrom} =
419 $branches->{$transfertfrom}{branchname};
420 $item->{transfertto} = $branches->{$transfertto}{branchname};
421 $item->{nocancel} = 1;
424 # If there is no loan, return and transfer, we show a checkbox.
425 $item->{notforloan} = $item->{notforloan} || 0;
427 # if independent branches is on we need to check if the person can reserve
428 # for branches they arent logged in to
429 if ( C4::Context->preference("IndependantBranches") ) {
430 if (! C4::Context->preference("canreservefromotherbranches")){
431 # cant reserve items so need to check if item homebranch and userenv branch match if not we cant reserve
432 my $userenv = C4::Context->userenv;
433 if ( ($userenv) && ( $userenv->{flags} % 2 != 1 ) ) {
434 $item->{cantreserve} = 1 if ( $item->{homebranch} ne $userenv->{branch} );
439 my $branch = C4::Circulation::_GetCircControlBranch($item, $borrowerinfo);
441 my $branchitemrule = GetBranchItemRule( $branch, $item->{'itype'} );
442 my $policy_holdallowed = 1;
444 $item->{'holdallowed'} = $branchitemrule->{'holdallowed'};
446 if ( $branchitemrule->{'holdallowed'} == 0 ||
447 ( $branchitemrule->{'holdallowed'} == 1 &&
448 $borrowerinfo->{'branchcode'} ne $item->{'homebranch'} ) ) {
449 $policy_holdallowed = 0;
454 && !$item->{cantreserve}
455 && IsAvailableForItemLevelRequest($itemnumber)
456 && CanItemBeReserved(
457 $borrowerinfo->{borrowernumber}, $itemnumber
461 $item->{available} = 1;
464 elsif ( C4::Context->preference('AllowHoldPolicyOverride') ) {
466 # If AllowHoldPolicyOverride is set, it should override EVERY restriction, not just branch item rules
467 $item->{override} = 1;
471 # If none of the conditions hold true, then neither override nor available is set and the item cannot be checked
473 # FIXME: move this to a pm
474 my $sth2 = $dbh->prepare("SELECT * FROM reserves WHERE borrowernumber=? AND itemnumber=? AND found='W'");
475 $sth2->execute($item->{ReservedForBorrowernumber},$item->{itemnumber});
476 while (my $wait_hashref = $sth2->fetchrow_hashref) {
477 $item->{waitingdate} = format_date($wait_hashref->{waitingdate});
480 # Show serial enumeration when needed
481 if ($item->{enumchron}) {
482 $itemdata_enumchron = 1;
485 push @{ $biblioitem->{itemloop} }, $item;
488 if ( $num_override == scalar( @{ $biblioitem->{itemloop} } ) ) { # That is, if all items require an override
489 $template->param( override_required => 1 );
490 } elsif ( $num_available == 0 ) {
491 $template->param( none_available => 1 );
492 $template->param( warnings => 1 );
493 $biblioloopiter{warn} = 1;
494 $biblioloopiter{none_avail} = 1;
496 $template->param( hiddencount => $hiddencount);
498 push @bibitemloop, $biblioitem;
501 # existingreserves building
503 ( $count, $reserves ) = GetReservesFromBiblionumber($biblionumber,1);
504 foreach my $res ( sort {
505 my $a_found = $a->{found} || '';
506 my $b_found = $a->{found} || '';
507 $a_found cmp $b_found;
511 for ( my $i = 1 ; $i <= $totalcount ; $i++ ) {
516 selected => ( $i == $res->{priority} ),
521 if ( defined $res->{'found'} && $res->{'found'} eq 'W' || $res->{'found'} eq 'T' ) {
522 my $item = $res->{'itemnumber'};
523 $item = GetBiblioFromItemNumber($item,undef);
525 $reserve{'holdingbranch'}=$item->{'holdingbranch'};
526 $reserve{'biblionumber'}=$item->{'biblionumber'};
527 $reserve{'barcodenumber'} = $item->{'barcode'};
528 $reserve{'wbrcode'} = $res->{'branchcode'};
529 $reserve{'itemnumber'} = $res->{'itemnumber'};
530 $reserve{'wbrname'} = $branches->{$res->{'branchcode'}}->{'branchname'};
531 if($reserve{'holdingbranch'} eq $reserve{'wbrcode'}){
532 $reserve{'atdestination'} = 1;
534 # set found to 1 if reserve is waiting for patron pickup
535 $reserve{'found'} = 1 if $res->{'found'} eq 'W';
536 $reserve{'intransit'} = 1 if $res->{'found'} eq 'T';
537 } elsif ($res->{priority} > 0) {
538 if (defined($res->{itemnumber})) {
539 my $item = GetItem($res->{itemnumber});
540 $reserve{'itemnumber'} = $res->{'itemnumber'};
541 $reserve{'barcodenumber'} = $item->{'barcode'};
542 $reserve{'item_level_hold'} = 1;
546 # get borrowers reserve info
547 my $reserveborrowerinfo = GetMemberDetails( $res->{'borrowernumber'}, 0);
548 if (C4::Context->preference('HidePatronName')){
549 $reserve{'hidename'} = 1;
550 $reserve{'cardnumber'} = $reserveborrowerinfo->{'cardnumber'};
552 $reserve{'expirationdate'} = format_date( $res->{'expirationdate'} )
553 unless ( !defined($res->{'expirationdate'}) || $res->{'expirationdate'} eq '0000-00-00' );
554 $reserve{'date'} = format_date( $res->{'reservedate'} );
555 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
556 $reserve{'biblionumber'} = $res->{'biblionumber'};
557 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
558 $reserve{'firstname'} = $reserveborrowerinfo->{'firstname'};
559 $reserve{'surname'} = $reserveborrowerinfo->{'surname'};
560 $reserve{'notes'} = $res->{'reservenotes'};
562 ( ( defined $res->{'found'} and $res->{'found'} eq 'W' ) or ( $res->{'priority'} eq '0' ) );
563 $reserve{'constrainttypea'} = ( $res->{'constrainttype'} eq 'a' );
564 $reserve{'constrainttypeo'} = ( $res->{'constrainttype'} eq 'o' );
565 $reserve{'voldesc'} = $res->{'volumeddesc'};
566 $reserve{'ccode'} = $res->{'ccode'};
567 $reserve{'barcode'} = $res->{'barcode'};
568 $reserve{'priority'} = $res->{'priority'};
569 $reserve{'lowestPriority'} = $res->{'lowestPriority'};
570 $reserve{'branchloop'} = GetBranchesLoop($res->{'branchcode'});
571 $reserve{'optionloop'} = \@optionloop;
573 push( @reserveloop, \%reserve );
576 # get the time for the form name...
580 CGIbranch => $CGIbranch,
583 fixedRank => $fixedRank,
588 optionloop => \@optionloop,
589 bibitemloop => \@bibitemloop,
590 itemdata_enumchron => $itemdata_enumchron,
592 biblionumber => $biblionumber,
593 findborrower => $findborrower,
594 title => $dat->{title},
595 author => $dat->{author},
597 C4::Search::enabled_staff_search_views,
599 if (defined $borrowerinfo && exists $borrowerinfo->{'branchcode'}) {
601 borrower_branchname => $branches->{$borrowerinfo->{'branchcode'}}->{'branchname'},
602 borrower_branchcode => $borrowerinfo->{'branchcode'},
605 $template->param(CGIselectborrower => $CGIselectborrower) if defined $CGIselectborrower;
607 $biblioloopiter{biblionumber} = $biblionumber;
608 $biblioloopiter{title} = $dat->{title};
609 $biblioloopiter{rank} = $fixedRank;
610 $biblioloopiter{reserveloop} = \@reserveloop;
613 $template->param( reserveloop => \@reserveloop );
616 push @biblioloop, \%biblioloopiter;
619 $template->param( biblioloop => \@biblioloop );
620 $template->param( biblionumbers => $biblionumbers );
621 $template->param( DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar() );
624 $template->param( multi_hold => 1 );
627 if ( C4::Context->preference( 'AllowHoldDateInFuture' ) ) {
628 $template->param( reserve_in_future => 1 );
632 output_html_with_http_headers $input, $cookie, $template->output;