[3.0.x] (bug #4055) backport canbook/itembereserved
authorNahuel ANGELINETTI <nahuel.angelinetti@biblibre.com>
Mon, 25 Jan 2010 09:16:41 +0000 (10:16 +0100)
committerHenri-Damien LAURENT <henridamien.laurent@biblibre.com>
Wed, 27 Jan 2010 13:14:48 +0000 (14:14 +0100)
This backport the functions from 3.2/3.4 to the 3.0 API, because we need to factorize the code.

C4/Circulation.pm
C4/Reserves.pm
koha-tmpl/opac-tmpl/prog/en/modules/opac-reserve.tmpl
opac/opac-reserve.pl
reserve/request.pl

index 93f1981..cfa1519 100644 (file)
@@ -682,7 +682,7 @@ sub CanBookBeIssued {
     if ( $borrower->{'dateexpiry'} eq '0000-00-00') {
         $issuingimpossible{EXPIRED} = 1;
     } else {
-        my @expirydate=  split /-/,$borrower->{'dateexpiry'};
+        my @expirydate=  split (/-/,$borrower->{'dateexpiry'});
         if($expirydate[0]==0 || $expirydate[1]==0|| $expirydate[2]==0 ||
             Date_to_Days(Today) > Date_to_Days( @expirydate )) {
             $issuingimpossible{EXPIRED} = 1;                                   
index c1d7b49..a867a86 100644 (file)
@@ -112,6 +112,8 @@ BEGIN {
         &ModReserveMinusPriority
         
         &CheckReserves
+        &CanBookBeReserved
+        &CanItemBeReserved
         &CancelReserve
 
         &IsAvailableForItemLevelRequest
@@ -431,6 +433,89 @@ sub GetReservesFromBorrowernumber {
     return @$data;
 }
 #-------------------------------------------------------------------------------------
+=item CanBookBeReserved
+
+$error = &CanBookBeReserved($borrowernumber, $biblionumber)
+
+=cut
+
+sub CanBookBeReserved{
+    my ($borrowernumber, $biblionumber) = @_;
+
+    my $dbh           = C4::Context->dbh;
+    my $biblio        = GetBiblioData($biblionumber);
+    my $borrower      = C4::Members::GetMember(borrowernumber=>$borrowernumber);
+    my $controlbranch = C4::Context->preference('ReservesControlBranch');
+    my $itype         = C4::Context->preference('item-level_itypes');
+    my $reservesrights= C4::Context->preference('maxreserves');
+    my $reservescount = 0;
+    
+    # we retrieve the user rights
+    my @args;
+    my $branchcode;
+    
+    
+    if($controlbranch eq "ItemHomeLibrary"){
+        $branchcode = '*';
+    }elsif($controlbranch eq "PatronLibrary"){
+        $branchcode = $borrower->{branchcode};
+    }
+
+    $reservescount = GetReserveCount($borrowernumber);
+
+    if($reservescount < $reservesrights){
+        return 1;
+    }else{
+        return 0;
+    }
+    
+}
+
+=item CanItemBeReserved
+
+$error = &CanItemBeReserved($borrowernumber, $itemnumber)
+
+this function return 1 if an item can be issued by this borrower.
+
+=cut
+
+sub CanItemBeReserved{
+    my ($borrowernumber, $itemnumber) = @_;
+    
+    my $dbh             = C4::Context->dbh;
+            
+    my $controlbranch   = C4::Context->preference('ReservesControlBranch') || "ItemHomeLibrary";
+    my $itype           = C4::Context->preference('item-level_itypes') ? "itype" : "itemtype";
+    my $allowedreserves = C4::Context->preference('maxreserves');
+    
+    # we retrieve borrowers and items informations #
+    my $item     = C4::Items::GetItem($itemnumber);
+    my $borrower = C4::Members::GetMember('borrowernumber'=>$borrowernumber);     
+
+    my $branchcode   = "*";
+    my $branchfield  = "reserves.branchcode";
+    
+    if( $controlbranch eq "ItemHomeLibrary" ){
+        $branchcode = $item->{homebranch};
+    }elsif( $controlbranch eq "PatronLibrary" ){
+        $branchcode = $borrower->{branchcode};
+    }
+    
+    # we retrieve user rights on this itemtype and branchcode
+    my $issuingrule = C4::Circulation::GetIssuingRule($borrower->{categorycode}, $item->{$itype}, $branchcode);
+    
+    # we retrieve count
+    
+    my $reservecount = GetReserveCount($borrowernumber);
+
+    # we check if it's ok or not
+    if(( $reservecount < $allowedreserves ) and $issuingrule->{maxissueqty} ){
+        return 1;
+    }else{
+        return 0;
+    }
+}
+#-------------------------------------------------------------------------------------
 
 =item GetReserveCount
 
index 51ed128..0eb4ea2 100644 (file)
         <!-- /TMPL_IF -->
 
         <!-- TMPL_IF Name="message" -->
-            <span class="lost">Unavailable (lost or missing)</span>
+               <span class="lost">Unavailable 
+               <!-- TMPL_UNLESS NAME="notholdable" -->
+            (lost or missing)
+            <!-- /TMPL_IF -->
+            </span>
         <!-- /TMPL_IF -->
 
         <!-- TMPL_IF Name="notforloan" -->
index 846a59e..2ec7c31 100755 (executable)
@@ -145,6 +145,7 @@ if ( $query->param('place_reserve') ) {
     my $notes=$query->param('notes');
     my $checkitem=$query->param('checkitem');
     my $found;
+    my $canreserve=0;
     
     #if we have an item selectionned, and the pickup branch is the same as the holdingbranch of the document, we force the value $rank and $found.
     if ($checkitem ne ''){
@@ -168,13 +169,24 @@ if ( $query->param('place_reserve') ) {
             $i2++;
         }
     }
-    # here we actually do the reserveration. Stage 3.
-    if ($query->param('request') eq 'any'){
-        # place a request on 1st available
-        AddReserve($branch,$borrowernumber,$biblionumber,'a',\@realbi,$rank,$notes,$bibdata->{'title'},$checkitem,$found);
-    } else {
-        AddReserve($branch,$borrowernumber,$biblionumber,'a',\@realbi,$rank,$notes,$bibdata->{'title'},$checkitem, $found);
+    
+    if ($checkitem ne ''){
+       $canreserve = 1 if CanItemBeReserved($borrowernumber,$checkitem);
+        $rank = '0' unless C4::Context->preference('ReservesNeedReturns');
+        my $item = GetItem($checkitem);
+        if ( $item->{'holdingbranch'} eq $branch ){
+            $found = 'W' unless C4::Context->preference('ReservesNeedReturns');
+        }
+    }
+    else {
+       $canreserve = 1 if CanBookBeReserved($borrowernumber,$biblionumber);
+        # Inserts a null into the 'itemnumber' field of 'reserves' table.
+        $checkitem = undef;
     }
+
+    # here we actually do the reserveration. Stage 3.
+    AddReserve($branch,$borrowernumber,$biblionumber,'a',\@realbi,$rank,$notes,
+                $bibdata->{'title'},$checkitem, $found) if ($canreserve);
     print $query->redirect("/cgi-bin/koha/opac-user.pl#opac-user-holds");
 }
 else {
@@ -342,7 +354,7 @@ foreach my $biblioitemnumber (@biblioitemnumbers) {
             $policy_holdallowed = 0;
         }
 
-        if (IsAvailableForItemLevelRequest($itemnumber) and $policy_holdallowed) {
+        if (IsAvailableForItemLevelRequest($itemnumber) and $policy_holdallowed and CanItemBeReserved($borrowernumber,$itemnumber)) {
             $item->{available} = 1;
             $num_available++;
         }
@@ -354,10 +366,17 @@ foreach my $biblioitemnumber (@biblioitemnumbers) {
         while (my $wait_hashref = $sth2->fetchrow_hashref) {
             $item->{waitingdate} = format_date($wait_hashref->{waitingdate});
         }
-       $item->{imageurl} = getitemtypeimagelocation( 'opac', $itemtypes->{ $item->{itype} }{imageurl} );
+       
+       $item->{imageurl} = getitemtypeimagelocation( 'opac', $itemtypes->{ $item->{itype} }{imageurl} );
+
         push @{ $biblioitem->{itemloop} }, $item;
     }
-
+    
+    if(not CanBookBeReserved($borrowernumber,$biblionumber)){
+        $biblioitem->{available} = undef;
+        $biblioitem->{notholdable} = 1;
+    }
+    
     push @bibitemloop, $biblioitem;
 }
 
index 8c55e54..acb813c 100755 (executable)
@@ -106,7 +106,7 @@ if ($cardnumber) {
     my $number_reserves =
       GetReserveCount( $borrowerinfo->{'borrowernumber'} );
 
-    if ( $number_reserves > C4::Context->preference('maxreserves') ) {
+    if ( not CanBookBeReserved($borrowerinfo->{borrowernumber}, $biblionumber)) {
                $warnings = 1;
         $maxreserves = 1;
     }
@@ -250,15 +250,18 @@ my @bibitemloop;
 
 foreach my $biblioitemnumber (@biblioitemnumbers) {
     my $biblioitem = $biblioiteminfos_of->{$biblioitemnumber};
-
+    
     my $num_available;
     my $num_override;
-
+    $biblioitem->{itemloop} = [];
+    
     $biblioitem->{description} =
       $itemtypes->{ $biblioitem->{itemtype} }{description};
 
     foreach my $itemnumber ( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} } ){
         my $item = $iteminfos_of->{$itemnumber};
+        
+        
         unless (C4::Context->preference('item-level_itypes')) {
             $item->{itype} = $biblioitem->{itemtype};
         }
@@ -356,7 +359,7 @@ foreach my $biblioitemnumber (@biblioitemnumbers) {
             $policy_holdallowed = 0;
         }
 
-        if (IsAvailableForItemLevelRequest($itemnumber) and not $item->{cantreserve}) {
+        if (IsAvailableForItemLevelRequest($itemnumber) and not $item->{cantreserve} and CanItemBeReserved($borrowerinfo->{borrowernumber}, $itemnumber)) {
             if ( not $policy_holdallowed and C4::Context->preference( 'AllowHoldPolicyOverride' ) ) {
                 $item->{override} = 1;
                 $num_override++;