From 85682fab1eebe2d05e4291ebec277799fd21ba63 Mon Sep 17 00:00:00 2001 From: Srdjan Jankovic Date: Fri, 4 Nov 2011 14:14:07 +1300 Subject: [PATCH] bug_6650: CanBookBeReserved() now returns array with reason CanBookBeReserved() now returns array providing the reason for not being able to be reserved Use that reason to give getter message on intranet reserve page. --- C4/Reserves.pm | 44 +++++++++++++------- .../prog/en/modules/reserve/request.tt | 3 + reserve/request.pl | 21 +++++++--- 3 files changed, 47 insertions(+), 21 deletions(-) diff --git a/C4/Reserves.pm b/C4/Reserves.pm index 4af8a85..041d1de 100644 --- a/C4/Reserves.pm +++ b/C4/Reserves.pm @@ -376,35 +376,51 @@ sub GetReservesFromBorrowernumber { #------------------------------------------------------------------------------------- =head2 CanBookBeReserved - $error = &CanBookBeReserved($borrowernumber, $biblionumber) + my ($can, $reason) = CanBookBeReserved($borrowernumber, $biblionumber) +Calls CanItemBeReserved() for each item, stopping when first available item is found. +For return values see CanItemBeReserved(), with addition of reason constant + NO_ITEMS =cut +use constant NO_ITEMS => "NO ITEMS"; +use constant NO_RESERVES_ALLOWED => "NO RESERVES ALLOWED"; +use constant MAX_RESERVES_REACHED => "BORROWER MAX RESERVES REACHED"; sub CanBookBeReserved{ my ($borrowernumber, $biblionumber) = @_; my @items = get_itemnumbers_of($biblionumber); #get items linked via host records - my @hostitems = get_hostitemnumbers_of($biblionumber); - if (@hostitems){ - push (@items,@hostitems); + if ( my @hostitems = get_hostitemnumbers_of($biblionumber) ) { + push (@items,@hostitems); } + my ($can, $reason) = (0, NO_ITEMS); foreach my $item (@items){ - return 1 if CanItemBeReserved($borrowernumber, $item); + ($can, $reason) = _CanItemBeReserved($borrowernumber, $item); + last if $can; } - return 0; + return wantarray ? ($can, $reason) : $can; } =head2 CanItemBeReserved - $error = &CanItemBeReserved($borrowernumber, $itemnumber) + my ($can, $reason) = CanItemBeReserved($borrowernumber, $itemnumber) -This function return 1 if an item can be issued by this borrower. +This function returns (1) if an item can be issued by this borrower. +If not, it returns (0, $reason), reson being a constat: + NO_RESERVES_ALLOWED + MAX_RESERVES_REACHED + +In scalar context it returns $can; =cut sub CanItemBeReserved{ + my ($can, $reason) = _CanItemBeReserved(@_); + return wantarray ? ($can, $reason) : $can; +} +sub _CanItemBeReserved{ my ($borrowernumber, $itemnumber) = @_; my $dbh = C4::Context->dbh; @@ -460,6 +476,7 @@ sub CanItemBeReserved{ }else{ $itemtype = '*'; } + return (0, NO_RESERVES_ALLOWED) unless $allowedreserves; # we retrieve count @@ -474,17 +491,14 @@ sub CanItemBeReserved{ $sthcount->execute($borrowernumber, $branchcode, $itemtype); } - my $reservecount = "0"; + my $reservecount = 0; if(my $rowcount = $sthcount->fetchrow_hashref()){ $reservecount = $rowcount->{count}; } - # we check if it's ok or not - if( $reservecount < $allowedreserves ){ - return 1; - }else{ - return 0; - } + return (0, MAX_RESERVES_REACHED) unless $reservecount < $allowedreserves; + + return (1); } #-------------------------------------------------------------------------------- =head2 GetReserveCount diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/reserve/request.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/reserve/request.tt index 1719af4..f88c77e 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/reserve/request.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/reserve/request.tt @@ -207,6 +207,9 @@ function checkMultiHold() { [% IF ( alreadyreserved ) %]
  • [% borrowerfirstname %] [% borrowersurname %] already has a hold on this item
  • [% END %] + [% IF ( reservesnotallowed ) %] +
  • Items can not be placed on hold
  • + [% END %] [% IF ( none_available ) %]
  • No copies are available to be placed on hold
  • [% END %] diff --git a/reserve/request.pl b/reserve/request.pl index 21183da..0706491 100755 --- a/reserve/request.pl +++ b/reserve/request.pl @@ -141,9 +141,11 @@ if ($borrowernumber_hold && !$action) { my $number_reserves = GetReserveCount( $borrowerinfo->{'borrowernumber'} ); - if ( C4::Context->preference('maxreserves') && ($number_reserves >= C4::Context->preference('maxreserves')) ) { - $warnings = 1; - $maxreserves = 1; + if ( my $max = C4::Context->preference('maxreserves') ) { + if ( $number_reserves >= $max ) { + $warnings = 1; + $maxreserves = 1; + } } # we check the date expiry of the borrower (only if there is an expiry date, otherwise, set to 1 (warn) @@ -234,12 +236,18 @@ foreach my $biblionumber (@biblionumbers) { my %biblioloopiter = (); my $maxreserves; + my $reservesnotallowed; my $dat = GetBiblioData($biblionumber); - unless ( CanBookBeReserved($borrowerinfo->{borrowernumber}, $biblionumber) ) { + my ($can_reserve, $reason) = CanBookBeReserved($borrowerinfo->{borrowernumber}, $biblionumber); + unless ( $can_reserve ) { $warnings = 1; - $maxreserves = 1; + if ($reason eq C4::Reserves->MAX_RESERVES_REACHED) { + $maxreserves = 1; + } else { + $reservesnotallowed = 1; + } } # get existing reserves ..... my ( $count, $reserves ) = GetReservesFromBiblionumber($biblionumber,1); @@ -262,7 +270,8 @@ foreach my $biblionumber (@biblionumbers) { $template->param( alreadyreserved => $alreadyreserved, messages => $messages, warnings => $warnings, - maxreserves=>$maxreserves + maxreserves=>$maxreserves, + reservesnotallowed=>$reservesnotallowed, ); -- 1.6.5