[Koha-patches] [PATCH] [HEAD](bug #3318) Granular renew permissions

Nahuel ANGELINETTI nahuel.angelinetti at biblibre.com
Wed Jun 10 14:25:41 CEST 2009


This patch move the renewals allowed permissions to issuing rules.
It permit to define renewals right using CircControl and branchcode/itemtype/categorycode, and allow librarian to define more granular rules.
It drop contraints on issuingrules table to allow librarian to have "defaults" rules(for the moment a bug exist because of use "*").
---
 C4/Circulation.pm                                  |   70 +++++++++++--------
 admin/itemtypes.pl                                 |    9 +--
 admin/smart-rules.pl                               |    9 ++-
 installer/data/mysql/kohastructure.sql             |    2 +-
 installer/data/mysql/updatedatabase.pl             |   26 +++++++
 .../prog/en/modules/admin/itemtypes.tmpl           |    8 --
 .../prog/en/modules/admin/smart-rules.tmpl         |    3 +
 7 files changed, 77 insertions(+), 50 deletions(-)

diff --git a/C4/Circulation.pm b/C4/Circulation.pm
index 07e0b39..f1fedfd 100644
--- a/C4/Circulation.pm
+++ b/C4/Circulation.pm
@@ -2019,40 +2019,50 @@ sub CanBookBeRenewed {
 
     # Look in the issues table for this item, lent to this borrower,
     # and not yet returned.
-
-    # FIXME - I think this function could be redone to use only one SQL call.
-    my $sth1 = $dbh->prepare(
-        "SELECT * FROM issues
-            WHERE borrowernumber = ?
-            AND itemnumber = ?"
-    );
-    $sth1->execute( $borrowernumber, $itemnumber );
-    if ( my $data1 = $sth1->fetchrow_hashref ) {
-
-        # Found a matching item
-
-        # See if this item may be renewed. This query is convoluted
-        # because it's a bit messy: given the item number, we need to find
-        # the biblioitem, which gives us the itemtype, which tells us
-        # whether it may be renewed.
-        my $query = "SELECT renewalsallowed FROM items ";
-        $query .= (C4::Context->preference('item-level_itypes'))
-                    ? "LEFT JOIN itemtypes ON items.itype = itemtypes.itemtype "
-                    : "LEFT JOIN biblioitems on items.biblioitemnumber = biblioitems.biblioitemnumber
-                       LEFT JOIN itemtypes ON biblioitems.itemtype = itemtypes.itemtype ";
-        $query .= "WHERE items.itemnumber = ?";
-        my $sth2 = $dbh->prepare($query);
-        $sth2->execute($itemnumber);
-        if ( my $data2 = $sth2->fetchrow_hashref ) {
-            $renews = $data2->{'renewalsallowed'};
-        }
-        if ( ( $renews && $renews > $data1->{'renewals'} ) || $override_limit ) {
+    my %branch = (
+            'ItemHomeLibrary' => 'items.homebranch',
+            'PickupLibrary'   => 'items.holdingbranch',
+            'PatronLibrary'   => 'borrowers.branchcode'
+            );
+    my $controlbranch = $branch{C4::Context->preference('CircControl')};
+    my $itype         = C4::Context->preference('item-level_itypes') ? 'items.itype' : 'biblioitems.itemtype';
+    
+    my $sthcount = $dbh->prepare("
+                   SELECT 
+                    borrowers.categorycode, biblioitems.itemtype, issues.renewals, renewalsallowed, $controlbranch
+                   FROM  issuingrules, 
+                   issues 
+                   LEFT JOIN items USING (itemnumber) 
+                   LEFT JOIN borrowers USING (borrowernumber) 
+                   LEFT JOIN biblioitems USING (biblioitemnumber)
+                   
+                   WHERE
+                    issuingrules.categorycode = borrowers.categorycode
+                   AND
+                    issuingrules.itemtype = $itype
+                   AND
+                    (issuingrules.branchcode = $controlbranch OR issuingrules.branchcode = '*') 
+                   AND 
+                    borrowernumber = ? 
+                   AND
+                    itemnumber = ?
+                   ORDER BY
+                    issuingrules.categorycode desc,
+                    issuingrules.itemtype desc,
+                    issuingrules.branchcode desc
+                   LIMIT 1;
+                  ");
+
+    $sthcount->execute( $borrowernumber, $itemnumber );
+    if ( my $data1 = $sthcount->fetchrow_hashref ) {
+        
+        if ( ( $data1->{renewalsallowed} && $data1->{renewalsallowed} > $data1->{renewals} ) || $override_limit ) {
             $renewokay = 1;
         }
         else {
 			$error="too_many";
 		}
-        $sth2->finish;
+		
         my ( $resfound, $resrec ) = C4::Reserves::CheckReserves($itemnumber);
         if ($resfound) {
             $renewokay = 0;
@@ -2060,7 +2070,7 @@ sub CanBookBeRenewed {
         }
 
     }
-    $sth1->finish;
+    $sthcount->finish;
     return ($renewokay,$error);
 }
 
diff --git a/admin/itemtypes.pl b/admin/itemtypes.pl
index 345e172..7ff1a47 100755
--- a/admin/itemtypes.pl
+++ b/admin/itemtypes.pl
@@ -112,7 +112,6 @@ if ( $op eq 'add_form' ) {
     $template->param(
         itemtype        => $itemtype,
         description     => $data->{'description'},
-        renewalsallowed => $data->{'renewalsallowed'},
         rentalcharge    => sprintf( "%.2f", $data->{'rentalcharge'} ),
         notforloan      => $data->{'notforloan'},
         imageurl        => $data->{'imageurl'},
@@ -138,7 +137,6 @@ elsif ( $op eq 'add_validate' ) {
         my $query2 = '
             UPDATE itemtypes
             SET    description = ?
-                 , renewalsallowed = ?
                  , rentalcharge = ?
                  , notforloan = ?
                  , imageurl = ?
@@ -148,7 +146,6 @@ elsif ( $op eq 'add_validate' ) {
         $sth = $dbh->prepare($query2);
         $sth->execute(
             $input->param('description'),
-            $input->param('renewalsallowed'),
             $input->param('rentalcharge'),
             ( $input->param('notforloan') ? 1 : 0 ),
             (
@@ -165,7 +162,7 @@ elsif ( $op eq 'add_validate' ) {
     else {    # add a new itemtype & not modif an old
         my $query = "
             INSERT INTO itemtypes
-                (itemtype,description,renewalsallowed,rentalcharge, notforloan, imageurl,summary)
+                (itemtype,description,rentalcharge, notforloan, imageurl,summary)
             VALUES
                 (?,?,?,?,?,?,?);
             ";
@@ -174,7 +171,6 @@ elsif ( $op eq 'add_validate' ) {
         $sth->execute(
             $input->param('itemtype'),
             $input->param('description'),
-            $input->param('renewalsallowed'),
             $input->param('rentalcharge'),
             $input->param('notforloan') ? 1 : 0,
             $image eq 'removeImage' ?           ''                 :
@@ -204,14 +200,13 @@ elsif ( $op eq 'delete_confirm' ) {
 
     my $sth =
       $dbh->prepare(
-"select itemtype,description,renewalsallowed,rentalcharge from itemtypes where itemtype=?"
+"select itemtype,description,rentalcharge from itemtypes where itemtype=?"
       );
     $sth->execute($itemtype);
     my $data = $sth->fetchrow_hashref;
     $template->param(
         itemtype        => $itemtype,
         description     => $data->{description},
-        renewalsallowed => $data->{renewalsallowed},
         rentalcharge    => sprintf( "%.2f", $data->{rentalcharge} ),
         imageurl        => $data->{imageurl},
         total           => $total
diff --git a/admin/smart-rules.pl b/admin/smart-rules.pl
index ce7dc9d..f4794a0 100755
--- a/admin/smart-rules.pl
+++ b/admin/smart-rules.pl
@@ -99,8 +99,8 @@ elsif ($op eq 'delete-branch-item') {
 # save the values entered
 elsif ($op eq 'add') {
     my $sth_search = $dbh->prepare("SELECT COUNT(*) AS total FROM issuingrules WHERE branchcode=? AND categorycode=? AND itemtype=?");
-    my $sth_insert = $dbh->prepare("INSERT INTO issuingrules (branchcode, categorycode, itemtype, maxissueqty, issuelength, fine, firstremind, chargeperiod) VALUES(?,?,?,?,?,?,?,?)");
-    my $sth_update=$dbh->prepare("UPDATE issuingrules SET fine=?, firstremind=?, chargeperiod=?, maxissueqty=?, issuelength=? WHERE branchcode=? AND categorycode=? AND itemtype=?");
+    my $sth_insert = $dbh->prepare("INSERT INTO issuingrules (branchcode, categorycode, itemtype, maxissueqty, renewalsallowed, issuelength, fine, firstremind, chargeperiod) VALUES(?,?,?,?,?,?,?,?,?)");
+    my $sth_update=$dbh->prepare("UPDATE issuingrules SET fine=?, firstremind=?, chargeperiod=?, maxissueqty=?, renewalsallowed=?, issuelength=? WHERE branchcode=? AND categorycode=? AND itemtype=?");
     
     my $br = $branch; # branch
     my $bor  = $input->param('categorycode'); # borrower category
@@ -109,6 +109,7 @@ elsif ($op eq 'add') {
     my $firstremind  = $input->param('firstremind');
     my $chargeperiod = $input->param('chargeperiod');
     my $maxissueqty  = $input->param('maxissueqty');
+    my $renewalsallowed  = $input->param('renewalsallowed');
     $maxissueqty =~ s/\s//g;
     $maxissueqty = undef if $maxissueqty !~ /^\d+/;
     my $issuelength  = $input->param('issuelength');
@@ -117,9 +118,9 @@ elsif ($op eq 'add') {
     $sth_search->execute($br,$bor,$cat);
     my $res = $sth_search->fetchrow_hashref();
     if ($res->{total}) {
-        $sth_update->execute($fine, $firstremind, $chargeperiod, $maxissueqty,$issuelength,$br,$bor,$cat);
+        $sth_update->execute($fine, $firstremind, $chargeperiod, $maxissueqty,$renewalsallowed,$issuelength,$br,$bor,$cat);
     } else {
-        $sth_insert->execute($br,$bor,$cat,$maxissueqty,$issuelength,$fine,$firstremind,$chargeperiod);
+        $sth_insert->execute($br,$bor,$cat,$maxissueqty,$renewalsallowed,$issuelength,$fine,$firstremind,$chargeperiod);
     }
 } 
 elsif ($op eq "set-branch-defaults") {
diff --git a/installer/data/mysql/kohastructure.sql b/installer/data/mysql/kohastructure.sql
index 2b2e0c9..a8412a6 100644
--- a/installer/data/mysql/kohastructure.sql
+++ b/installer/data/mysql/kohastructure.sql
@@ -1139,6 +1139,7 @@ CREATE TABLE `issuingrules` (
   `chargename` varchar(100) default NULL,
   `maxissueqty` int(4) default NULL,
   `issuelength` int(4) default NULL,
+  `renewalsallowed` smallint(6) NOT NULL default "0",
   `branchcode` varchar(10) NOT NULL default '',
   PRIMARY KEY  (`branchcode`,`categorycode`,`itemtype`),
   KEY `categorycode` (`categorycode`),
@@ -1207,7 +1208,6 @@ DROP TABLE IF EXISTS `itemtypes`;
 CREATE TABLE `itemtypes` (
   `itemtype` varchar(10) NOT NULL default '',
   `description` mediumtext,
-  `renewalsallowed` smallint(6) default NULL,
   `rentalcharge` double(16,4) default NULL,
   `notforloan` smallint(6) default NULL,
   `imageurl` varchar(200) default NULL,
diff --git a/installer/data/mysql/updatedatabase.pl b/installer/data/mysql/updatedatabase.pl
index 58497c8..1fe8dbc 100755
--- a/installer/data/mysql/updatedatabase.pl
+++ b/installer/data/mysql/updatedatabase.pl
@@ -2467,6 +2467,32 @@ if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
     print "Upgrade to $DBversion done (added FilterBeforeOverdueReport syspref and new index on authorised_values)\n";
 }
 
+$DBversion = '3.01.00.038';
+if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
+    $dbh->do('ALTER TABLE issuingrules DROP FOREIGN KEY issuingrules_ibfk_1');
+    $dbh->do('ALTER TABLE issuingrules DROP FOREIGN KEY issuingrules_ibfk_2');
+    SetVersion ($DBversion);
+    print "Upgrade to $DBversion done (deleting contraints in issuingrules)\n";
+}
+
+$DBversion = '3.01.00.039';
+if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
+    $dbh->do('ALTER TABLE issuingrules ADD COLUMN `renewalsallowed` smallint(6) NOT NULL default "0" AFTER `issuelength`;');
+    $sth = $dbh->prepare("SELECT itemtype, renewalsallowed FROM itemtypes");
+    $sth->execute();
+
+    my $sthupd = $dbh->prepare("UPDATE issuingrules SET renewalsallowed = ? WHERE itemtype = ?");
+    
+    while(my $row = $sth->fetchrow_hashref){
+        $sthupd->execute($row->{renewalsallowed}, $row->{itemtype});
+    }
+    
+    $dbh->do('ALTER TABLE itemtypes DROP COLUMN `renewalsallowed`;');
+    
+    SetVersion ($DBversion);
+    print "Upgrade to $DBversion done (Moving allowed renewals from itemtypes to issuingrule)\n";
+}
+
 =item DropAllForeignKeys($table)
 
   Drop all foreign keys of the table $table
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/itemtypes.tmpl b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/itemtypes.tmpl
index 93bcfb9..9a98e4c 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/itemtypes.tmpl
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/itemtypes.tmpl
@@ -285,7 +285,6 @@ Item Types Administration
     <th>Code</th>
     <th>Description</th>
     <th>Not for loan</th>
-    <th>Renewable</th>
     <th>Charge</th>
     <th>Actions</th>
   </tr>
@@ -304,13 +303,6 @@ Item Types Administration
     <td><!-- TMPL_VAR name="description" --></td>
     <td><!-- TMPL_IF NAME="notforloan" -->Yes<!-- TMPL_ELSE -->&nbsp;<!-- /TMPL_IF --></td>
     <td>
-    <!-- TMPL_IF NAME="renewalsallowed" -->
-      <!-- TMPL_VAR name="renewalsallowed" --> times
-    <!-- TMPL_ELSE -->
-      No
-    <!-- /TMPL_IF -->
-    </td>
-    <td>
     <!-- TMPL_UNLESS name="notforloan" -->
       <!-- TMPL_VAR NAME="rentalcharge" -->
     <!-- /TMPL_UNLESS -->
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/smart-rules.tmpl b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/smart-rules.tmpl
index 383e979..df7e3a3 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/smart-rules.tmpl
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/smart-rules.tmpl
@@ -68,6 +68,7 @@ $(document).ready(function() {
                 <th>Fine Grace Period</th>
                 <th>Fine Charging Interval</th>
                 <th>Current Checkouts Allowed</th>
+                <th>Renewals Allowed</th>
                 <th>Loan Period</th><th>&nbsp;</th>
             </tr>
             <!-- TMPL_LOOP NAME="rules" -->
@@ -93,6 +94,7 @@ $(document).ready(function() {
                             <!-- TMPL_VAR NAME="maxissueqty" -->
                         <!-- /TMPL_IF -->
                     </td>
+					<td><!-- TMPL_IF NAME="renewalsallowed" --><!-- TMPL_VAR NAME="renewalsallowed" --> time(s)<!-- /TMPL_IF --></td>
                     <td><!-- TMPL_IF NAME="issuelength" --><!-- TMPL_VAR NAME="issuelength" --> day(s)<!-- /TMPL_IF --></td>
                     <td>
                         <a class="button" href="/cgi-bin/koha/admin/smart-rules.pl?op=delete&amp;itemtype=<!-- TMPL_VAR NAME="itemtype" -->&amp;categorycode=<!-- TMPL_VAR NAME="categorycode" -->&amp;branch=<!-- TMPL_VAR NAME="branch" -->">Delete</a>
@@ -120,6 +122,7 @@ $(document).ready(function() {
                     <td><input name="firstremind" size="2" /> day(s)</td>
                     <td><input name="chargeperiod" size="2" /> day(s)</td>
                     <td><input name="maxissueqty" size="3" /></td>
+                    <td><input name="renewalsallowed" size="3" /></td>
                     <td><input name="issuelength" size="3" /> day(s)</td>
                     <td><input type="hidden" name="branch" value="<!-- TMPL_VAR NAME="branch" -->"/><input type="submit" value="Add" class="submit" /></td>
                 </tr>
-- 
1.6.0.4




More information about the Koha-patches mailing list