Bug 22330: (QA follow-up) Remove duplicate use lines, combine and sort remaning lines
[koha.git] / C4 / ImportBatch.pm
index 82d6af2..99bc428 100644 (file)
@@ -27,6 +27,7 @@ use C4::Items;
 use C4::Charset;
 use C4::AuthoritiesMarc;
 use C4::MarcModificationTemplates;
+use Koha::Items;
 use Koha::Plugins::Handler;
 use Koha::Logger;
 
@@ -747,7 +748,7 @@ sub BatchCommitItems {
 
         my $item = TransformMarcToKoha( $item_marc );
 
-        my $duplicate_barcode = exists( $item->{'barcode'} ) && GetItemnumberFromBarcode( $item->{'barcode'} );
+        my $duplicate_barcode = exists( $item->{'barcode'} ) && Koha::Items->find({ barcode => $item->{'barcode'} });
         my $duplicate_itemnumber = exists( $item->{'itemnumber'} );
 
         my $updsth = $dbh->prepare("UPDATE import_items SET status = ?, itemnumber = ? WHERE import_items_id = ?");
@@ -761,7 +762,7 @@ sub BatchCommitItems {
             $updsth->finish();
             $num_items_replaced++;
         } elsif ( $action eq "replace" && $duplicate_barcode ) {
-            my $itemnumber = GetItemnumberFromBarcode( $item->{'barcode'} );
+            my $itemnumber = $duplicate_barcode->itemnumber;
             ModItemFromMarc( $item_marc, $biblionumber, $itemnumber );
             $updsth->bind_param( 1, 'imported' );
             $updsth->bind_param( 2, $item->{itemnumber} );
@@ -1118,7 +1119,7 @@ sub GetImportRecordsRange {
     ( $order_by ) = grep( /^$order_by$/, qw( import_record_id title status overlay_status ) ) ? $order_by : 'import_record_id';
 
     my $order_by_direction =
-      uc( $parameters->{order_by_direction} ) eq 'DESC' ? 'DESC' : 'ASC';
+      uc( $parameters->{order_by_direction} // 'ASC' ) eq 'DESC' ? 'DESC' : 'ASC';
 
     $order_by .= " $order_by_direction, authorized_heading" if $order_by eq 'title';
 
@@ -1556,10 +1557,11 @@ sub RecordsFromMARCXMLFile {
 
 sub RecordsFromMarcPlugin {
     my ($input_file, $plugin_class, $encoding) = @_;
+    my ( $text, @return );
+    return \@return if !$input_file || !$plugin_class;
 
     # Read input file
     open IN, "<$input_file" or die "$0: cannot open input file $input_file: $!\n";
-    my ( $text, $marc, @return );
     $/ = "\035";
     while (<IN>) {
         s/^\s+//;
@@ -1574,14 +1576,16 @@ sub RecordsFromMarcPlugin {
         class  => $plugin_class,
         method => 'to_marc',
         params => { data => $text },
-    });
+    }) if $text;
 
     # Convert to array of MARC records
-    my $marc_type = C4::Context->preference('marcflavour');
-    foreach my $blob ( split(/\x1D/, $text) ) {
-        next if $blob =~ /^\s*$/;
-        my ($marcrecord) = MarcToUTF8Record($blob, $marc_type, $encoding);
-        push @return, $marcrecord;
+    if( $text ) {
+        my $marc_type = C4::Context->preference('marcflavour');
+        foreach my $blob ( split(/\x1D/, $text) ) {
+            next if $blob =~ /^\s*$/;
+            my ($marcrecord) = MarcToUTF8Record($blob, $marc_type, $encoding);
+            push @return, $marcrecord;
+        }
     }
     return \@return;
 }
@@ -1592,10 +1596,10 @@ sub _create_import_record {
     my ($batch_id, $record_sequence, $marc_record, $record_type, $encoding, $z3950random, $marc_type) = @_;
 
     my $dbh = C4::Context->dbh;
-    my $sth = $dbh->prepare("INSERT INTO import_records (import_batch_id, record_sequence, marc, marcxml, 
+    my $sth = $dbh->prepare("INSERT INTO import_records (import_batch_id, record_sequence, marc, marcxml, marcxml_old,
                                                          record_type, encoding, z3950random)
-                                    VALUES (?, ?, ?, ?, ?, ?, ?)");
-    $sth->execute($batch_id, $record_sequence, $marc_record->as_usmarc(), $marc_record->as_xml($marc_type),
+                                    VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
+    $sth->execute($batch_id, $record_sequence, $marc_record->as_usmarc(), $marc_record->as_xml($marc_type), '',
                   $record_type, $encoding, $z3950random);
     my $import_record_id = $dbh->{'mysql_insertid'};
     $sth->finish();