[Koha-patches] [PATCH] This patch allows the Branch Transfer Limits feature to limit transfers either by itemtype ( like the original commit ) or collection code ( new feature ).

Galen Charlton galen.charlton at liblime.com
Wed Mar 4 23:15:32 CET 2009


From: koha <koha at false.(none)>

[Note inserted by RM: this patch is by Kyle Hall]

Signed-off-by: Galen Charlton <galen.charlton at liblime.com>
---
 C4/Circulation.pm                                  |   33 ++++++++++------
 admin/branch_transfer_limits.pl                    |   41 +++++++++++++-------
 circ/branchtransfers.pl                            |   13 +++++-
 installer/data/mysql/en/mandatory/sysprefs.sql     |    1 +
 .../1-Obligatoire/unimarc_standard_systemprefs.sql |    2 +-
 installer/data/mysql/kohastructure.sql             |    3 +-
 installer/data/mysql/updatedatabase.pl             |   14 +++++++
 .../en/modules/admin/branch_transfer_limits.tmpl   |   10 ++--
 .../prog/en/modules/circ/branchtransfers.tmpl      |    2 +-
 9 files changed, 82 insertions(+), 37 deletions(-)

diff --git a/C4/Circulation.pm b/C4/Circulation.pm
index e4019a4..10460ee 100644
--- a/C4/Circulation.pm
+++ b/C4/Circulation.pm
@@ -279,13 +279,13 @@ sub transferbook {
 
     # if using Branch Transfer Limits
     if ( C4::Context->preference("UseBranchTransferLimits") == 1 ) {
-        if ( C4::Context->preference("item-level_itypes") ) {
+        if ( C4::Context->preference("item-level_itypes") && C4::Context->preference("BranchTransferLimitsType") eq 'itemtype' ) {
             if ( ! IsBranchTransferAllowed( $tbr, $fbr, $biblio->{'itype'} ) ) {
                 $messages->{'NotAllowed'} = $tbr . "::" . $biblio->{'itype'};
                 $dotransfer = 0;
             }
-        } elsif ( ! IsBranchTransferAllowed( $tbr, $fbr, $biblio->{'itemtype'} ) ) {
-            $messages->{'NotAllowed'} = $tbr . "::" . $biblio->{'itemtype'};
+        } elsif ( ! IsBranchTransferAllowed( $tbr, $fbr, $biblio->{ C4::Context->preference("BranchTransferLimitsType") } ) ) {
+            $messages->{'NotAllowed'} = $tbr . "::" . $biblio->{ C4::Context->preference("BranchTransferLimitsType") };
             $dotransfer = 0;
     	}
     }
@@ -1547,7 +1547,7 @@ sub AddReturn {
 				ModItemTransfer($iteminformation->{'itemnumber'}, C4::Context->userenv->{'branch'}, $iteminformation->{'homebranch'});
 				$messages->{'WasTransfered'} = 1;
 			} elsif ( C4::Context->preference("UseBranchTransferLimits") == 1 
-					&& ! IsTransferAllowed( $branch, $iteminformation->{'homebranch'}, $iteminformation->{'itemtype'} )
+					&& ! IsTransferAllowed( $branch, $iteminformation->{'homebranch'}, $iteminformation->{ C4::Context->preference("BranchTransferLimitsType") } )
 				) {
 				ModItemTransfer($iteminformation->{'itemnumber'}, C4::Context->userenv->{'branch'}, $iteminformation->{'homebranch'});
                                 $messages->{'WasTransfered'} = 1;
@@ -2630,19 +2630,22 @@ return $exist;
 
 =head2 IsBranchTransferAllowed
 
-$allowed = IsBranchTransferAllowed( $toBranch, $fromBranch, $itemtype );
+$allowed = IsBranchTransferAllowed( $toBranch, $fromBranch, $code );
+
+Code is either an itemtype or collection doe depending on the pref BranchTransferLimitsType
 
 =cut
 
 sub IsBranchTransferAllowed {
-	my ( $toBranch, $fromBranch, $itemtype ) = @_;
-    
+	my ( $toBranch, $fromBranch, $code ) = @_;
+
 	if ( $toBranch eq $fromBranch ) { return 1; } ## Short circuit for speed.
         
+	my $limitType = C4::Context->preference("BranchTransferLimitsType");   
 	my $dbh = C4::Context->dbh;
             
-	my $sth = $dbh->prepare('SELECT * FROM branch_transfer_limits WHERE toBranch = ? AND fromBranch = ? AND itemtype = ?');
-	$sth->execute( $toBranch, $fromBranch, $itemtype );
+	my $sth = $dbh->prepare("SELECT * FROM branch_transfer_limits WHERE toBranch = ? AND fromBranch = ? AND $limitType = ?");
+	$sth->execute( $toBranch, $fromBranch, $code );
 	my $limit = $sth->fetchrow_hashref();
                         
 	## If a row is found, then that combination is not allowed, if no matching row is found, then the combination *is allowed*
@@ -2655,17 +2658,21 @@ sub IsBranchTransferAllowed {
 
 =head2 CreateBranchTransferLimit
 
-CreateBranchTransferLimit( $toBranch, $fromBranch, $itemtype );
+CreateBranchTransferLimit( $toBranch, $fromBranch, $code );
+
+$code is either itemtype or collection code depending on what the pref BranchTransferLimitsType is set to.
 
 =cut
 
 sub CreateBranchTransferLimit {
-   my ( $toBranch, $fromBranch, $itemtype ) = @_;
+   my ( $toBranch, $fromBranch, $code ) = @_;
+
+   my $limitType = C4::Context->preference("BranchTransferLimitsType");
    
    my $dbh = C4::Context->dbh;
    
-   my $sth = $dbh->prepare("INSERT INTO branch_transfer_limits ( itemtype, toBranch, fromBranch ) VALUES ( ?, ?, ? )");
-   $sth->execute( $itemtype, $toBranch, $fromBranch );
+   my $sth = $dbh->prepare("INSERT INTO branch_transfer_limits ( $limitType, toBranch, fromBranch ) VALUES ( ?, ?, ? )");
+   $sth->execute( $code, $toBranch, $fromBranch );
 }
 
 =head2 DeleteBranchTransferLimits
diff --git a/admin/branch_transfer_limits.pl b/admin/branch_transfer_limits.pl
index c05b0f7..2cee238 100755
--- a/admin/branch_transfer_limits.pl
+++ b/admin/branch_transfer_limits.pl
@@ -40,13 +40,25 @@ my ($template, $loggedinuser, $cookie)
 
 my $dbh = C4::Context->dbh;
 
-my @itemtypes;
+# Set the template language for the correct limit type
+my $limit_phrase = 'Collection Code';
+my $limitType = C4::Context->preference("BranchTransferLimitsType");
+if ( $limitType eq 'itemtype' ) {
+	$limit_phrase = 'Item Type';
+}
+
+my @codes;
 my @branchcodes;
 
-my $sth = $dbh->prepare("SELECT itemtype FROM itemtypes");
+my $sth;
+if ( $limitType eq 'ccode' ) {
+	$sth = $dbh->prepare('SELECT authorised_value AS ccode FROM authorised_values WHERE category = "CCODE"');
+} elsif ( $limitType eq 'itemtype' ) {
+	$sth = $dbh->prepare('SELECT itemtype FROM itemtypes');
+}
 $sth->execute();
 while ( my $row = $sth->fetchrow_hashref ) {
-	push( @itemtypes, $row->{'itemtype'} );
+	push( @codes, $row->{ $limitType } );
 }
 
 $sth = $dbh->prepare("SELECT branchcode FROM branches");
@@ -59,12 +71,12 @@ while ( my $row = $sth->fetchrow_hashref ) {
 if ( $input->param('updateLimits') ) {
     DeleteBranchTransferLimits();
 
-	foreach my $itemtype ( @itemtypes ) {
+	foreach my $code ( @codes ) {
 		foreach my $toBranch ( @branchcodes ) {
 			foreach my $fromBranch ( @branchcodes ) {
-				my $isSet = $input->param( $itemtype . "_" . $toBranch . "_" . $fromBranch );
+				my $isSet = $input->param( $code . "_" . $toBranch . "_" . $fromBranch );
 				if ( $isSet ) {
-                                    CreateBranchTransferLimit( $toBranch, $fromBranch, $itemtype );
+                                    CreateBranchTransferLimit( $toBranch, $fromBranch, $code );
 				}
 			}
 		}
@@ -80,23 +92,23 @@ foreach my $branchcode ( @branchcodes ) {
 }
 
 ## Build the default data
-my @itemtypes_loop;
-foreach my $itemtype ( @itemtypes ) {
+my @codes_loop;
+foreach my $code ( @codes ) {
 	my @to_branch_loop;
 	my %row_data;
-	$row_data{ itemtype } = $itemtype;
+	$row_data{ code } = $code;
 	$row_data{ to_branch_loop } = \@to_branch_loop;
 	foreach my $toBranch ( @branchcodes ) {
 		my @from_branch_loop;
 		my %row_data;
-		$row_data{ itemtype } = $itemtype;
+		$row_data{ code } = $code;
 		$row_data{ toBranch } = $toBranch;
 		$row_data{ from_branch_loop } = \@from_branch_loop;
 		
 		foreach my $fromBranch ( @branchcodes ) {
 			my %row_data;
-                        my $isChecked = ! IsBranchTransferAllowed( $toBranch, $fromBranch, $itemtype );
-			$row_data{ itemtype } = $itemtype;
+                        my $isChecked = ! IsBranchTransferAllowed( $toBranch, $fromBranch, $code );
+			$row_data{ code } = $code;
 			$row_data{ toBranch } = $toBranch;
 			$row_data{ fromBranch } = $fromBranch;
                         $row_data{ isChecked } = $isChecked;
@@ -107,13 +119,14 @@ foreach my $itemtype ( @itemtypes ) {
 		push( @to_branch_loop, \%row_data );
 	}
 
-	push( @itemtypes_loop, \%row_data );
+	push( @codes_loop, \%row_data );
 }
 
 
 $template->param(
-		itemtypes_loop => \@itemtypes_loop,
+		codes_loop => \@codes_loop,
 		branchcode_loop => \@branchcode_loop,
+		limit_phrase => $limit_phrase,
 		);
 
 output_html_with_http_headers $input, $cookie, $template->output;
diff --git a/circ/branchtransfers.pl b/circ/branchtransfers.pl
index 92b139c..8ae283d 100755
--- a/circ/branchtransfers.pl
+++ b/circ/branchtransfers.pl
@@ -193,6 +193,14 @@ if ($found) {
 
 #####################
 
+# Used for branch transfer limits error messages.
+my $codeTypeDescription = 'Collection Code';
+my $codeType = C4::Context->preference("BranchTransferLimitsType");
+if ( $codeType eq 'itemtype' ) {   
+  $codeTypeDescription = 'Item Type';
+}
+
+
 my @errmsgloop;
 foreach my $code ( keys %$messages ) {
     my %err;
@@ -206,9 +214,10 @@ foreach my $code ( keys %$messages ) {
     warn $messages->{'NotAllowed'};
     warn  $branches->{ $messages->{'NotAllowed'} }->{'branchname'};
         $err{errnotallowed} =  1;
-        my ( $tbr, $itemtype ) = split( /::/,  $messages->{'NotAllowed'} );
+        my ( $tbr, $typecode ) = split( /::/,  $messages->{'NotAllowed'} );
         $err{tbr} = $branches->{ $tbr }->{'branchname'};
-        $err{itemtype} = $itemtype;
+        $err{code} = $typecode;
+        $err{codeType} = $codeTypeDescription; 
     }
     
     if ( $code eq 'IsPermanent' ) {
diff --git a/installer/data/mysql/en/mandatory/sysprefs.sql b/installer/data/mysql/en/mandatory/sysprefs.sql
index 47a0c4b..c79a727 100644
--- a/installer/data/mysql/en/mandatory/sysprefs.sql
+++ b/installer/data/mysql/en/mandatory/sysprefs.sql
@@ -214,3 +214,4 @@ INSERT INTO systempreferences (variable,value,explanation,options,type) VALUES('
 INSERT INTO `systempreferences` (variable,value,options,explanation,type) VALUES ('OPACDisplayRequestPriority','0','','Show patrons the priority level on holds in the OPAC','YesNo');
 INSERT INTO `systempreferences` ( `variable` , `value` , `options` , `explanation` , `type` ) VALUES ( 'UseBranchTransferLimits', '0', '', 'If ON, Koha will will use the rules defined in branch_transfer_limits to decide if an item transfer should be allowed.', 'YesNo');
 INSERT INTO systempreferences (variable,value,explanation,options,type) VALUES('AllowHoldPolicyOverride', '0', 'Allow staff to override hold policies when placing holds',NULL,'YesNo');
+INSERT INTO `systempreferences` ( `variable` , `value` , `options` , `explanation` , `type` ) VALUES ( 'BranchTransferLimitsType', 'ccode', 'itemtype|ccode', 'When using branch transfer limits, choose whether to limit by itemtype or collection code.', 'Choice');
diff --git a/installer/data/mysql/fr-FR/1-Obligatoire/unimarc_standard_systemprefs.sql b/installer/data/mysql/fr-FR/1-Obligatoire/unimarc_standard_systemprefs.sql
index 47da596..3469106 100644
--- a/installer/data/mysql/fr-FR/1-Obligatoire/unimarc_standard_systemprefs.sql
+++ b/installer/data/mysql/fr-FR/1-Obligatoire/unimarc_standard_systemprefs.sql
@@ -216,4 +216,4 @@ INSERT INTO systempreferences (variable,value,explanation,options,type) VALUES('
 INSERT INTO `systempreferences` (variable,value,options,explanation,type) VALUES ('OPACDisplayRequestPriority','0','','Afficher l\'ordre des réservation pour les adhérents á l\'opac','YesNo');
 INSERT INTO `systempreferences` ( `variable` , `value` , `options` , `explanation` , `type` ) VALUES ( 'UseBranchTransferLimits', '0', '', 'If ON, Koha will will use the rules defined in branch_transfer_limits to decide if an item transfer should be allowed.', 'YesNo');
 INSERT INTO systempreferences (variable,value,explanation,options,type) VALUES('AllowHoldPolicyOverride', '0', "Autorise le personnel á outrepasser la politique de réservation au moment d'une réservation",NULL,'YesNo');
-
+INSERT INTO `systempreferences` ( `variable` , `value` , `options` , `explanation` , `type` ) VALUES ('BranchTransferLimitsType', 'ccode', 'itemtype|ccode', 'When using branch transfer limits, choose whether to limit by itemtype or collection code.', 'Choice');
diff --git a/installer/data/mysql/kohastructure.sql b/installer/data/mysql/kohastructure.sql
index 2d4b415..a7e555f 100644
--- a/installer/data/mysql/kohastructure.sql
+++ b/installer/data/mysql/kohastructure.sql
@@ -2337,7 +2337,8 @@ CREATE TABLE branch_transfer_limits (
     limitId int(8) NOT NULL auto_increment,
     toBranch varchar(4) NOT NULL,
     fromBranch varchar(4) NOT NULL,
-    itemtype varchar(4) NOT NULL,  
+    itemtype varchar(4) NULL,
+    ccode varchar(10) NULL,  
     PRIMARY KEY  (limitId)
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
 
diff --git a/installer/data/mysql/updatedatabase.pl b/installer/data/mysql/updatedatabase.pl
index 30f1887..85e5d79 100755
--- a/installer/data/mysql/updatedatabase.pl
+++ b/installer/data/mysql/updatedatabase.pl
@@ -2221,6 +2221,20 @@ if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
     SetVersion ($DBversion);
 }
 
+$DBversion = "3.01.00.013";
+if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
+    $dbh->do("ALTER TABLE `branch_transfer_limits` CHANGE `itemtype` `itemtype` VARCHAR( 4 ) CHARACTER SET utf8 COLLATE utf8_general_ci NULL");
+    $dbh->do("ALTER TABLE `branch_transfer_limits` ADD `ccode` VARCHAR( 10 ) NULL ;");
+    $dbh->do("INSERT INTO `systempreferences` ( `variable` , `value` , `options` , `explanation` , `type` )
+    VALUES (
+    'BranchTransferLimitsType', 'ccode', 'itemtype|ccode', 'When using branch transfer limits, choose whether to limit by itemtype or collection code.', 'Choice'
+    );");
+    
+    print "Upgrade to $DBversion done ( Updated table for Branch Transfer Limits)\n";
+    SetVersion ($DBversion);
+}
+
+
 =item DropAllForeignKeys($table)
 
   Drop all foreign keys of the table $table
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/branch_transfer_limits.tmpl b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/branch_transfer_limits.tmpl
index c863f83..f47e622 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/branch_transfer_limits.tmpl
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/branch_transfer_limits.tmpl
@@ -62,7 +62,7 @@
 </ul></div>
 
 <h1 class="parameters">Library Transfer Limits</h1>
-<div class="help">Check the boxes for the items that should <em>not</em> be transferable.</div>
+<div class="help">Check the boxes for the items that should <strong><em>not</em></strong> be transferable.</div>
 
 	<div class="table">
 		<form name="mainform" method="post" action="branch_transfer_limits.pl">
@@ -77,8 +77,8 @@
 			</thead>
 
 			<tbody>
-				<!-- TMPL_LOOP NAME="itemtypes_loop" -->
-					<tr><th>Limits for Item Type: <!-- TMPL_VAR NAME="itemtype" --></th></tr>
+				<!-- TMPL_LOOP NAME="codes_loop" -->
+					<tr><th>Limits for <!--TMPL_VAR NAME="limit_phrase" -->: <!-- TMPL_VAR NAME="code" --></th></tr>
 	
 					<!-- TMPL_LOOP NAME="to_branch_loop" -->
 						<tr>
@@ -87,11 +87,11 @@
 								<td>
                                     <!-- TMPL_IF NAME="isChecked" -->
 									<input 
-										name="<!-- TMPL_VAR NAME="itemtype" -->_<!-- TMPL_VAR NAME="toBranch" -->_<!-- TMPL_VAR NAME="fromBranch" -->"
+										name="<!-- TMPL_VAR NAME="code" -->_<!-- TMPL_VAR NAME="toBranch" -->_<!-- TMPL_VAR NAME="fromBranch" -->"
 										type="checkbox" value="1" checked="checked" />
                                     <!-- TMPL_ELSE-->
 									<input 
-										name="<!-- TMPL_VAR NAME="itemtype" -->_<!-- TMPL_VAR NAME="toBranch" -->_<!-- TMPL_VAR NAME="fromBranch" -->"
+										name="<!-- TMPL_VAR NAME="code" -->_<!-- TMPL_VAR NAME="toBranch" -->_<!-- TMPL_VAR NAME="fromBranch" -->"
 										type="checkbox" value="1" />
                                     <!-- TMPL_ELSE-->
                                     <!-- /TMPL_IF -->
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/circ/branchtransfers.tmpl b/koha-tmpl/intranet-tmpl/prog/en/modules/circ/branchtransfers.tmpl
index 8b2981a..1db6630 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/circ/branchtransfers.tmpl
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/circ/branchtransfers.tmpl
@@ -143,7 +143,7 @@
                             <li>Please return item to home library: <!-- TMPL_VAR Name="msg" --></li>
                     <!-- /TMPL_IF -->
                     <!-- TMPL_IF Name="errnotallowed" -->
-                            <li>You cannot transfer items of type <b><!-- TMPL_VAR Name="itemtype" --></b> to <b><!-- TMPL_VAR Name="tbr" --></b></li>
+                            <li>You cannot transfer items of <!--TMPL_VAR Name="codeType" --> <b><!-- TMPL_VAR Name="code" --></b> to <b><!-- TMPL_VAR Name="tbr" --></b></li>
                     <!-- /TMPL_IF -->
                     <!-- TMPL_IF Name="errdesteqholding" -->
                         <li>Item is already at destination library.</li>
-- 
1.5.5.GIT



More information about the Koha-patches mailing list