Bug 7298: (follow-up) fix uninitialized variable warning
[koha.git] / labels / label-edit-batch.pl
index 35c38fb..aea4742 100755 (executable)
@@ -4,7 +4,7 @@
 # Parts Copyright 2009 Foundations Bible College.
 #
 # This file is part of Koha.
-#       
+#
 # Koha is free software; you can redistribute it and/or modify it under the
 # terms of the GNU General Public License as published by the Free Software
 # Foundation; either version 2 of the License, or (at your option) any later
@@ -14,9 +14,9 @@
 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 #
-# You should have received a copy of the GNU General Public License along with
-# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
-# Suite 330, Boston, MA  02111-1307 USA
+# You should have received a copy of the GNU General Public License along
+# 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;
@@ -26,9 +26,9 @@ use CGI;
 
 use C4::Auth qw(get_template_and_user);
 use C4::Output qw(output_html_with_http_headers);
-use C4::Branch qw(get_branch_code_from_name);
-use C4::Labels::Lib 1.000000 qw(get_label_summary html_table);
-use C4::Labels::Batch 1.000000;
+use C4::Items qw(GetItemnumberFromBarcode);
+use C4::Creators;
+use C4::Labels;
 
 my $cgi = new CGI;
 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
@@ -55,11 +55,15 @@ my $display_columns = [ {_label_number  => {label => 'Label Number', link_field
                         {select         => {label => 'Select', value => '_label_id'}},
                       ];
 my $op = $cgi->param('op') || 'edit';
+my @label_ids;
+my @item_numbers;
+my $barcode;
 my $batch_id = $cgi->param('element_id') || $cgi->param('batch_id') || undef;
-my @label_ids = $cgi->param('label_id') if $cgi->param('label_id');
-my @item_numbers = $cgi->param('item_number') if $cgi->param('item_number');
+@label_ids = $cgi->param('label_id') if $cgi->param('label_id');
+@item_numbers = $cgi->param('item_number') if $cgi->param('item_number');
+$barcode = $cgi->param('barcode') if $cgi->param('barcode');
 
-my $branch_code = get_branch_code_from_name($template->param('LoginBranchname'));
+my $branch_code = C4::Context->userenv->{'branch'};
 
 if ($op eq 'remove') {
     $batch = C4::Labels::Batch->retrieve(batch_id => $batch_id);
@@ -76,6 +80,15 @@ elsif ($op eq 'delete') {
     $errstr = "batch $batch_id was not deleted." if $err;
 }
 elsif ($op eq 'add') {
+    if ($barcode) {
+        my @barcodes = split /\n/, $barcode; # $barcode is effectively passed in as a <cr> separated list
+        foreach my $number (@barcodes) {
+            $number =~ s/\r$//; # strip any naughty return chars
+            if (my $item_number = GetItemnumberFromBarcode($number)) {  # we must test in case an invalid barcode is passed in; we effectively disgard them atm
+                push @item_numbers, $item_number;
+            }
+        }
+    }
     $batch = C4::Labels::Batch->retrieve(batch_id => $batch_id);
     $batch = C4::Labels::Batch->new(branch_code => $branch_code) if $batch == -2;
     if ($branch_code){
@@ -108,7 +121,7 @@ $db_rows = get_label_summary(items => $items, batch_id => $batch_id);
 
 my $table = html_table($display_columns, $db_rows);
 
-$template->param(   
+$template->param(
                 err         => $err,
                 errstr      => $errstr,
                 ) if ($err ne 0);