Merge remote-tracking branch 'origin/new/bug_2629'
[srvgit] / cataloguing / value_builder / stocknumber.pl
index 005f011..711904f 100755 (executable)
@@ -17,8 +17,8 @@
 # with Koha; if not, write to the Free Software Foundation, Inc.,
 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
-#use strict;
-#use warnings; FIXME - Bug 2505
+use strict;
+use warnings;
 use C4::Context;
 
 =head1 plugin_parameters
@@ -53,15 +53,12 @@ sub plugin_javascript {
 
        my $branchcode = C4::Context->userenv->{'branch'};
 
-       $query = "SELECT MAX(CAST(SUBSTRING_INDEX(stocknumber,'_',-1) AS SIGNED)) FROM items WHERE homebranch = ?";
+       my $query = "SELECT MAX(CAST(SUBSTRING_INDEX(stocknumber,'_',-1) AS SIGNED))+1 FROM items WHERE homebranch = ? AND stocknumber LIKE ?";
        my $sth=$dbh->prepare($query);
-       $sth->execute($branchcode);
-       while (my ($count)= $sth->fetchrow_array) {
-               $nextnum = $count;
-       }
-       $nextnum++;
 
-       my $nextnum = $branchcode.'_'.$nextnum;
+       $sth->execute($branchcode,$branchcode."_%");
+       my ($nextnum) = $sth->fetchrow;
+       $nextnum = $branchcode.'_'.$nextnum;
 
     my $scr = <<END_OF_JS;
 if (\$('#' + id).val() == '' || force) {