From 9a318473f62e0cd24286ed1710774148439587d8 Mon Sep 17 00:00:00 2001
From: Kyle M Hall <kyle@bywatersolutions.com>
Date: Thu, 23 May 2013 07:20:54 -0400
Subject: [PATCH] Bug 10314 - CanItemBeReserved does not respect the holds
 policies - Followup - Tidy up CanItemBeReserved

Signed-off-by: Chris Cormack <chris@bigballofwax.co.nz>
---
 C4/Reserves.pm |  113 +++++++++++++++++++++++++++++---------------------------
 1 file changed, 59 insertions(+), 54 deletions(-)

diff --git a/C4/Reserves.pm b/C4/Reserves.pm
index c6ab228..c712f00 100644
--- a/C4/Reserves.pm
+++ b/C4/Reserves.pm
@@ -415,19 +415,20 @@ This function return 1 if an item can be issued by this borrower.
 
 =cut
 
-sub CanItemBeReserved{
-    my ($borrowernumber, $itemnumber) = @_;
-    
+sub CanItemBeReserved {
+    my ( $borrowernumber, $itemnumber ) = @_;
+
     my $dbh             = C4::Context->dbh;
     my $allowedreserves = 0;
-            
+
     my $controlbranch = C4::Context->preference('ReservesControlBranch');
-    my $itype         = C4::Context->preference('item-level_itypes') ? "itype" : "itemtype";
+    my $itype =
+      C4::Context->preference('item-level_itypes') ? "itype" : "itemtype";
 
     # we retrieve borrowers and items informations #
-    my $item     = GetItem($itemnumber);
-    my $borrower = C4::Members::GetMember('borrowernumber'=>$borrowernumber);     
-
+    my $item = GetItem($itemnumber);
+    my $borrower =
+      C4::Members::GetMember( 'borrowernumber' => $borrowernumber );
 
     # Before we check too see if the borrower has exceed the
     # maximum number of holds allowed for this itemtype,
@@ -455,71 +456,75 @@ sub CanItemBeReserved{
     # If we've gotten this far, the holds policy does not prevent the patron
     # from placing this hold. We now need to see if the patron has exceeded
     # the maximum number of holds allowed for this itemtype based on the patrons
-    my $sth = $dbh->prepare("SELECT categorycode, itemtype, branchcode, reservesallowed 
-                             FROM issuingrules 
-                             WHERE (categorycode in (?,'*') ) 
-                             AND (itemtype IN (?,'*')) 
-                             AND (branchcode IN (?,'*')) 
-                             ORDER BY 
-                               categorycode DESC, 
-                               itemtype     DESC, 
-                               branchcode   DESC;"
-                           );
-                           
-    my $querycount ="SELECT 
-                            count(*) as count
-                            FROM reserves
-                                LEFT JOIN items USING (itemnumber)
-                                LEFT JOIN biblioitems ON (reserves.biblionumber=biblioitems.biblionumber)
-                                LEFT JOIN borrowers USING (borrowernumber)
-                            WHERE borrowernumber = ?
-                                ";
-    
-    
+    my $sth = $dbh->prepare(q{
+        SELECT categorycode, itemtype, branchcode, reservesallowed 
+        FROM issuingrules 
+        WHERE (categorycode in (?,'*') ) 
+          AND (itemtype IN (?,'*')) 
+          AND (branchcode IN (?,'*')) 
+        ORDER BY 
+          categorycode DESC, 
+          itemtype     DESC, 
+          branchcode   DESC;
+    });
+
+    my $querycount = q{
+        SELECT count(*) as count
+        FROM reserves
+        LEFT JOIN items       USING (itemnumber)
+        LEFT JOIN biblioitems USING (biblionumber)
+        LEFT JOIN borrowers   USING (borrowernumber)
+        WHERE borrowernumber = ?
+    };
+
     my $itemtype     = $item->{$itype};
     my $categorycode = $borrower->{categorycode};
     my $branchcode   = "";
     my $branchfield  = "reserves.branchcode";
-    
-    if( $controlbranch eq "ItemHomeLibrary" ){
+
+    if ( $controlbranch eq "ItemHomeLibrary" ) {
         $branchfield = "items.homebranch";
-        $branchcode = $item->{homebranch};
-    }elsif( $controlbranch eq "PatronLibrary" ){
+        $branchcode  = $item->{homebranch};
+    }
+    elsif ( $controlbranch eq "PatronLibrary" ) {
         $branchfield = "borrowers.branchcode";
-        $branchcode = $borrower->{branchcode};
+        $branchcode  = $borrower->{branchcode};
     }
-    
-    # we retrieve rights 
-    $sth->execute($categorycode, $itemtype, $branchcode);
-    if(my $rights = $sth->fetchrow_hashref()){
+
+    # we retrieve rights
+    $sth->execute( $categorycode, $itemtype, $branchcode );
+    if ( my $rights = $sth->fetchrow_hashref() ) {
         $itemtype        = $rights->{itemtype};
-        $allowedreserves = $rights->{reservesallowed}; 
-    }else{
+        $allowedreserves = $rights->{reservesallowed};
+    }
+    else {
         $itemtype = '*';
     }
-    
+
     # we retrieve count
-    
+
     $querycount .= "AND $branchfield = ?";
-    
-    $querycount .= " AND $itype = ?" if ($itemtype ne "*");
+
+    $querycount .= " AND $itype = ?" if ( $itemtype ne "*" );
     my $sthcount = $dbh->prepare($querycount);
-    
-    if($itemtype eq "*"){
-        $sthcount->execute($borrowernumber, $branchcode);
-    }else{
-        $sthcount->execute($borrowernumber, $branchcode, $itemtype);
+
+    if ( $itemtype eq "*" ) {
+        $sthcount->execute( $borrowernumber, $branchcode );
     }
-    
+    else {
+        $sthcount->execute( $borrowernumber, $branchcode, $itemtype );
+    }
+
     my $reservecount = "0";
-    if(my $rowcount = $sthcount->fetchrow_hashref()){
+    if ( my $rowcount = $sthcount->fetchrow_hashref() ) {
         $reservecount = $rowcount->{count};
     }
-    
+
     # we check if it's ok or not
-    if( $reservecount < $allowedreserves ){
+    if ( $reservecount < $allowedreserves ) {
         return 1;
-    }else{
+    }
+    else {
         return 0;
     }
 }
-- 
1.7.10.4