Bug 32843: Fix cataloguing/value_builder/unimarc_field_124a.pl
[koha-ffzg.git] / labels / label-edit-batch.pl
index 5252213..230291e 100755 (executable)
 # You should have received a copy of the GNU General Public License
 # along with Koha; if not, see <http://www.gnu.org/licenses>.
 
-use strict;
-use warnings;
-use vars qw($debug);
+use Modern::Perl;
 
 use CGI qw ( -utf8 );
 
-use C4::Auth qw(get_template_and_user);
-use C4::Output qw(output_html_with_http_headers);
-use C4::Items qw(GetItem GetItemnumberFromBarcode);
-use C4::Creators;
+use C4::Auth qw( get_template_and_user );
+use C4::Circulation qw( barcodedecode );
+use C4::Creators qw( get_label_summary html_table );
 use C4::Labels;
+use C4::Output qw( output_html_with_http_headers );
 
-my $cgi = new CGI;
+use Koha::Items;
+
+my $cgi = CGI->new;
 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
     {
         template_name   => "labels/label-edit-batch.tt",
         query           => $cgi,
         type            => "intranet",
-        authnotrequired => 0,
         flagsrequired   => { catalogue => 1 },
-        debug           => 1,
     }
 );
 
 my $err = 0;
-my $errstr = undef;
+my $errtype = undef;
 my $duplicate_count = undef;
 my $duplicate_message = undef;
 my $db_rows = {};
@@ -62,6 +60,7 @@ my @item_numbers;
 my $number_list;
 my $number_type = $cgi->param('number_type') || "barcode";
 my $batch_id = $cgi->param('element_id') || $cgi->param('batch_id') || 0;
+my $description = $cgi->param('description') || '';
 @label_ids = $cgi->multi_param('label_id') if $cgi->param('label_id');
 @item_numbers = $cgi->multi_param('item_number') if $cgi->param('item_number');
 $number_list = $cgi->param('number_list') if $cgi->param('number_list');
@@ -73,42 +72,43 @@ if ($op eq 'remove') {
     foreach my $label_id (@label_ids) {
     $err = $batch->remove_item($label_id);
     }
-    $errstr = "item(s) not removed from batch $batch_id." if $err;
+    $errtype = 'ITEM_NOT_REMOVED' if $err;
 #    Something like this would be nice to avoid problems with the browser's 'refresh' button, but it needs an error handling mechanism...
 #    print $cgi->redirect("label-edit-batch.pl?op=edit&batch_id=$batch_id");
 #    exit;
 }
 elsif ($op eq 'delete') {
     $err = C4::Labels::Batch::delete(batch_id => $batch_id, branch_code => $branch_code);
-    $errstr = "batch $batch_id was not deleted." if $err;
+    $errtype = 'BATCH_NOT_DELETED' if $err;
 }
 elsif ($op eq 'add') {
     if ($number_list) {
         my @numbers_list = split /\n/, $number_list; # Entries are effectively passed in as a <cr> separated list
         foreach my $number (@numbers_list) {
             $number =~ s/\r$//; # strip any naughty return chars
-            if( $number_type eq "itemnumber" && GetItem($number) ) {
+            if( $number_type eq "itemnumber" && Koha::Items->find($number) ) {
                 push @item_numbers, $number;
             }
             elsif ($number_type eq "barcode" ) {  # we must test in case an invalid barcode is passed in; we effectively disgard them atm
-                if( my $item_number = GetItemnumberFromBarcode($number) ){
-                    push @item_numbers, $item_number;
-                }
+                $number = barcodedecode($number);
+                my $item = Koha::Items->find({barcode => $number});
+                push @item_numbers, $item->itemnumber if $item;
             }
         }
     }
     if ($batch_id != 0) {$batch = C4::Labels::Batch->retrieve(batch_id => $batch_id);}
     if ($batch_id == 0 || $batch == -2) {$batch = C4::Labels::Batch->new(branch_code => $branch_code);}
+    $template->param( description => $batch->{description} );
     if ($branch_code){
         foreach my $item_number (@item_numbers) {
             $err = $batch->add_item($item_number);
         }
         $batch_id = $batch->get_attr('batch_id') if $batch_id == 0; #update batch_id if we added to a new batch
-        $errstr = "item(s) not added to batch $batch_id." if $err;
+        $errtype = 'ITEM_NOT_ADDED' if $err;
     }
     else {
         $err = 1;
-        $errstr = "items(s) not added, the error was: Branch is not set, you please set your branch before adding items to a batch";
+        $errtype = 'BRANCH_NOT_SET';
     }
 }
 elsif ($op eq 'new') {
@@ -119,10 +119,11 @@ elsif ($op eq 'de_duplicate') {
     $batch = C4::Labels::Batch->retrieve(batch_id => $batch_id);
     $duplicate_count = $batch->remove_duplicates();
     $duplicate_message = 1 if $duplicate_count != -1;
-    $errstr = "batch $batch_id not fully de-duplicated." if $duplicate_count == -1;
+    $errtype = 'BATCH_NOT_DEDUP' if $duplicate_count == -1;
 }
 else { # edit
     $batch = C4::Labels::Batch->retrieve(batch_id => $batch_id);
+    $template->param( description => $batch->{description} );
 }
 
 my $items = $batch->get_attr('items');
@@ -132,7 +133,7 @@ my $table = html_table($display_columns, $db_rows);
 
 $template->param(
                 err         => $err,
-                errstr      => $errstr,
+                errtype     => $errtype,
                 ) if ($err ne 0);
 
 $template->param(