Bug 12409: Fix fields order on exporting to bibtex
[koha_fer] / C4 / ImportBatch.pm
index f9d7b41..a487cc6 100644 (file)
@@ -26,6 +26,7 @@ use C4::Biblio;
 use C4::Items;
 use C4::Charset;
 use C4::AuthoritiesMarc;
+use C4::MarcModificationTemplates;
 
 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
 
@@ -316,18 +317,19 @@ sub ModAuthInBatch {
 =head2 BatchStageMarcRecords
 
   ($batch_id, $num_records, $num_items, @invalid_records) = 
-    BatchStageMarcRecords($record_type, $encoding, $marc_records, $file_name,
+    BatchStageMarcRecords($encoding, $marc_records, $file_name, $marc_modification_template,
                           $comments, $branch_code, $parse_items,
                           $leave_as_staging, 
                           $progress_interval, $progress_callback);
 
 =cut
 
-sub  BatchStageMarcRecords {
+sub BatchStageMarcRecords {
     my $record_type = shift;
     my $encoding = shift;
     my $marc_records = shift;
     my $file_name = shift;
+    my $marc_modification_template = shift;
     my $comments = shift;
     my $branch_code = shift;
     my $parse_items = shift;
@@ -378,6 +380,8 @@ sub  BatchStageMarcRecords {
 
         $encoding = $charset_guessed unless $encoding;
 
+        ModifyRecordWithTemplate( $marc_modification_template, $marc_record ) if ( $marc_modification_template );
+
         my $import_record_id;
         if (scalar($marc_record->fields()) == 0) {
             push @invalid_records, $marc_blob;
@@ -654,9 +658,10 @@ sub BatchCommitRecords {
             SetImportRecordOverlayStatus($rowref->{'import_record_id'}, 'match_applied');
             SetImportRecordStatus($rowref->{'import_record_id'}, 'imported');
         } elsif ($record_result eq 'ignore') {
+            $recordid = $record_match;
             $num_ignored++;
             $recordid = $record_match;
-            if ($record_type eq 'biblio' and defined $recordid and $item_result eq 'create_new') {
+            if ($record_type eq 'biblio' and defined $recordid and ( $item_result eq 'create_new' || $item_result eq 'replace' ) ) {
                 my ($bib_items_added, $bib_items_replaced, $bib_items_errored) = BatchCommitItems($rowref->{'import_record_id'}, $recordid, $item_result);
                 $num_items_added += $bib_items_added;
          $num_items_replaced += $bib_items_replaced;
@@ -687,20 +692,24 @@ sub BatchCommitItems {
 
     my $dbh = C4::Context->dbh;
 
-    my ( $num_items_added, $num_items_errored, $num_items_replaced ) = 0;
-    my $sth = $dbh->prepare("
+    my $num_items_added = 0;
+    my $num_items_errored = 0;
+    my $num_items_replaced = 0;
+
+    my $sth = $dbh->prepare( "
         SELECT import_items_id, import_items.marcxml, encoding
         FROM import_items
         JOIN import_records USING (import_record_id)
         WHERE import_record_id = ?
         ORDER BY import_items_id
-    ");
+    " );
     $sth->bind_param( 1, $import_record_id );
     $sth->execute();
+
     while ( my $row = $sth->fetchrow_hashref() ) {
         my $item_marc = MARC::Record->new_from_xml( StripNonXmlChars( $row->{'marcxml'} ), 'UTF-8', $row->{'encoding'} );
 
-        #delete date_due subfield as to not accidentally delete item checkout due dates
+        # Delete date_due subfield as to not accidentally delete item checkout due dates
         my ( $MARCfield, $MARCsubfield ) = GetMarcFromKohaField( 'items.onloan', GetFrameworkCode($biblionumber) );
         $item_marc->field($MARCfield)->delete_subfield( code => $MARCsubfield );
 
@@ -711,6 +720,7 @@ sub BatchCommitItems {
 
         my $updsth = $dbh->prepare("UPDATE import_items SET status = ?, itemnumber = ? WHERE import_items_id = ?");
         if ( $action eq "replace" && $duplicate_itemnumber ) {
+            # Duplicate itemnumbers have precedence, that way we can update barcodes by overlaying
             ModItemFromMarc( $item_marc, $biblionumber, $item->{itemnumber} );
             $updsth->bind_param( 1, 'imported' );
             $updsth->bind_param( 2, $item->{itemnumber} );
@@ -718,6 +728,15 @@ sub BatchCommitItems {
             $updsth->execute();
             $updsth->finish();
             $num_items_replaced++;
+        } elsif ( $action eq "replace" && $duplicate_barcode ) {
+            my $itemnumber = GetItemnumberFromBarcode( $item->{'barcode'} );
+            ModItemFromMarc( $item_marc, $biblionumber, $itemnumber );
+            $updsth->bind_param( 1, 'imported' );
+            $updsth->bind_param( 2, $item->{itemnumber} );
+            $updsth->bind_param( 3, $row->{'import_items_id'} );
+            $updsth->execute();
+            $updsth->finish();
+            $num_items_replaced++;
         } elsif ($duplicate_barcode) {
             $updsth->bind_param( 1, 'error' );
             $updsth->bind_param( 2, 'duplicate item barcode' );
@@ -1009,9 +1028,18 @@ starting at the given offset.
 =cut
 
 sub GetImportRecordsRange {
-    my ($batch_id, $offset, $results_per_group, $status) = @_;
+    my ( $batch_id, $offset, $results_per_group, $status, $parameters ) = @_;
 
     my $dbh = C4::Context->dbh;
+
+    my $order_by = $parameters->{order_by} || 'import_record_id';
+    ( $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';
+
+    $order_by .= " $order_by_direction, authorized_heading" if $order_by eq 'title';
+
     my $query = "SELECT title, author, isbn, issn, authorized_heading, import_records.import_record_id,
                                            record_sequence, status, overlay_status,
                                            matched_biblionumber, matched_authid, record_type
@@ -1025,7 +1053,8 @@ sub GetImportRecordsRange {
         $query .= " AND status=?";
         push(@params,$status);
     }
-    $query.=" ORDER BY import_record_id";
+
+    $query.=" ORDER BY $order_by $order_by_direction";
 
     if($results_per_group){
         $query .= " LIMIT ?";
@@ -1418,7 +1447,7 @@ sub _add_biblio_fields {
     my ($title, $author, $isbn, $issn) = _parse_biblio_fields($marc_record);
     my $dbh = C4::Context->dbh;
     # FIXME no controlnumber, originalsource
-    $isbn = C4::Koha::_isbn_cleanup($isbn); # FIXME C4::Koha::_isbn_cleanup should be made public
+    $isbn = C4::Koha::GetNormalizedISBN($isbn);
     my $sth = $dbh->prepare("INSERT INTO import_biblios (import_record_id, title, author, isbn, issn) VALUES (?, ?, ?, ?, ?)");
     $sth->execute($import_record_id, $title, $author, $isbn, $issn);
     $sth->finish();