Bug 24469: Move the new queries to a dedicated ImportBatch subroutine
[srvgit] / C4 / ImportBatch.pm
index 739f9f6..2a5696d 100644 (file)
@@ -78,13 +78,12 @@ BEGIN {
     SetImportRecordOverlayStatus
     GetImportRecordStatus
     SetImportRecordStatus
+    SetMatchedBiblionumber
     GetImportRecordMatches
     SetImportRecordMatches
        );
 }
 
-our $logger = Koha::Logger->get( { category => 'C4.ImportBatch' } );
-
 =head1 NAME
 
 C4::ImportBatch - manage batches of imported MARC records
@@ -267,7 +266,7 @@ sub GetImportBatch {
     my ($batch_id) = @_;
 
     my $dbh = C4::Context->dbh;
-    my $sth = $dbh->prepare_cached("SELECT * FROM import_batches WHERE import_batch_id = ?");
+    my $sth = $dbh->prepare_cached("SELECT b.*, p.name as profile FROM import_batches b LEFT JOIN import_batch_profiles p ON p.id = b.profile_id WHERE import_batch_id = ?");
     $sth->bind_param(1, $batch_id);
     $sth->execute();
     my $result = $sth->fetchrow_hashref;
@@ -578,7 +577,7 @@ sub BatchCommitRecords {
     my $num_items_errored = 0;
     my $num_ignored = 0;
     # commit (i.e., save, all records in the batch)
-    SetImportBatchStatus('importing');
+    SetImportBatchStatus($batch_id, 'importing');
     my $overlay_action = GetImportBatchOverlayAction($batch_id);
     my $nomatch_action = GetImportBatchNoMatchAction($batch_id);
     my $item_action = GetImportBatchItemAction($batch_id);
@@ -633,7 +632,7 @@ sub BatchCommitRecords {
             if ($record_type eq 'biblio') {
                 my $biblioitemnumber;
                 ($recordid, $biblioitemnumber) = AddBiblio($marc_record, $framework);
-                $query = "UPDATE import_biblios SET matched_biblionumber = ? WHERE import_record_id = ?";
+                $query = "UPDATE import_biblios SET matched_biblionumber = ? WHERE import_record_id = ?"; # FIXME call SetMatchedBiblionumber instead
                 if ($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;
@@ -666,7 +665,7 @@ sub BatchCommitRecords {
                 $oldxml = $old_marc->as_xml($marc_type);
 
                 ModBiblio($marc_record, $recordid, $oldbiblio->frameworkcode);
-                $query = "UPDATE import_biblios SET matched_biblionumber = ? WHERE import_record_id = ?";
+                $query = "UPDATE import_biblios SET matched_biblionumber = ? WHERE import_record_id = ?"; # FIXME call SetMatchedBiblionumber instead
 
                 if ($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);
@@ -699,7 +698,7 @@ sub BatchCommitRecords {
                 $num_items_errored += $bib_items_errored;
                 # still need to record the matched biblionumber so that the
                 # items can be reverted
-                my $sth2 = $dbh->prepare_cached("UPDATE import_biblios SET matched_biblionumber = ? WHERE import_record_id = ?");
+                my $sth2 = $dbh->prepare_cached("UPDATE import_biblios SET matched_biblionumber = ? WHERE import_record_id = ?"); # FIXME call SetMatchedBiblionumber instead
                 $sth2->execute($recordid, $rowref->{'import_record_id'});
                 SetImportRecordOverlayStatus($rowref->{'import_record_id'}, 'match_applied');
             }
@@ -775,6 +774,10 @@ sub BatchCommitItems {
             $updsth->execute();
             $num_items_errored++;
         } else {
+            # Remove the itemnumber if it exists, we want to create a new item
+            my ( $itemtag, $itemsubfield ) = GetMarcFromKohaField( "items.itemnumber" );
+            $item_marc->field($itemtag)->delete_subfield( code => $itemsubfield );
+
             my ( $item_biblionumber, $biblioitemnumber, $itemnumber ) = AddItemFromMarc( $item_marc, $biblionumber );
             if( $itemnumber ) {
                 $updsth->bind_param( 1, 'imported' );
@@ -800,6 +803,8 @@ sub BatchCommitItems {
 sub BatchRevertRecords {
     my $batch_id = shift;
 
+    my $logger = Koha::Logger->get( { category => 'C4.ImportBatch' } );
+
     $logger->trace("C4::ImportBatch::BatchRevertRecords( $batch_id )");
 
     my $record_type;
@@ -809,7 +814,7 @@ sub BatchRevertRecords {
     my $num_ignored = 0;
     my $num_items_deleted = 0;
     # commit (i.e., save, all records in the batch)
-    SetImportBatchStatus('reverting');
+    SetImportBatchStatus($batch_id, 'reverting');
     my $overlay_action = GetImportBatchOverlayAction($batch_id);
     my $nomatch_action = GetImportBatchNoMatchAction($batch_id);
     my $dbh = C4::Context->dbh;
@@ -877,7 +882,7 @@ sub BatchRevertRecords {
         my $query;
         if ($record_type eq 'biblio') {
             # remove matched_biblionumber only if there is no 'imported' item left
-            $query = "UPDATE import_biblios SET matched_biblionumber = NULL WHERE import_record_id = ?";
+            $query = "UPDATE import_biblios SET matched_biblionumber = NULL WHERE import_record_id = ?"; # FIXME Remove me
             $query = "UPDATE import_biblios SET matched_biblionumber = NULL WHERE import_record_id = ?  AND NOT EXISTS (SELECT * FROM import_items WHERE import_items.import_record_id=import_biblios.import_record_id and status='imported')";
         } else {
             $query = "UPDATE import_auths SET matched_authid = NULL WHERE import_record_id = ?";
@@ -1023,9 +1028,11 @@ sub GetImportBatchRangeDesc {
     my ($offset, $results_per_group) = @_;
 
     my $dbh = C4::Context->dbh;
-    my $query = "SELECT * FROM import_batches
-                                    WHERE batch_type IN ('batch', 'webservice')
-                                    ORDER BY import_batch_id DESC";
+    my $query = "SELECT b.*, p.name as profile FROM import_batches b
+                                    LEFT JOIN import_batch_profiles p
+                                    ON b.profile_id = p.id
+                                    WHERE b.batch_type IN ('batch', 'webservice')
+                                    ORDER BY b.import_batch_id DESC";
     my @params;
     if ($results_per_group){
         $query .= " LIMIT ?";
@@ -1213,6 +1220,22 @@ sub SetImportBatchStatus {
 
 }
 
+=head2 SetMatchedBiblionumber
+
+  SetMatchedBiblionumber($import_record_id, $biblionumber);
+
+=cut
+
+sub SetMatchedBiblionumber {
+    my ($import_record_id, $biblionumber) = @_;
+
+    my $dbh = C4::Context->dbh;
+    $dbh->do(
+        q|UPDATE import_biblios SET matched_biblionumber = ? WHERE import_record_id = ?|,
+        undef, $biblionumber, $import_record_id
+    );
+}
+
 =head2 GetImportBatchOverlayAction
 
   my $overlay_action = GetImportBatchOverlayAction($batch_id);
@@ -1502,10 +1525,10 @@ sub RecordsFromISO2709File {
     my $marc_type = C4::Context->preference('marcflavour');
     $marc_type .= 'AUTH' if ($marc_type eq 'UNIMARC' && $record_type eq 'auth');
 
-    open IN, "<$input_file" or die "$0: cannot open input file $input_file: $!\n";
+    open my $fh, '<', $input_file or die "$0: cannot open input file $input_file: $!\n";
     my @marc_records;
     $/ = "\035";
-    while (<IN>) {
+    while (<$fh>) {
         s/^\s+//;
         s/\s+$//;
         next unless $_; # skip if record has only whitespace, as might occur
@@ -1517,7 +1540,7 @@ sub RecordsFromISO2709File {
                 "Unexpected charset $charset_guessed, expecting $encoding";
         }
     }
-    close IN;
+    close $fh;
     return ( \@errors, \@marc_records );
 }
 
@@ -1560,15 +1583,15 @@ sub RecordsFromMarcPlugin {
     return \@return if !$input_file || !$plugin_class;
 
     # Read input file
-    open IN, "<$input_file" or die "$0: cannot open input file $input_file: $!\n";
+    open my $fh, '<', $input_file or die "$0: cannot open input file $input_file: $!\n";
     $/ = "\035";
-    while (<IN>) {
+    while (<$fh>) {
         s/^\s+//;
         s/\s+$//;
         next unless $_;
         $text .= $_;
     }
-    close IN;
+    close $fh;
 
     # Convert to large MARC blob with plugin
     $text = Koha::Plugins::Handler->run({
@@ -1637,7 +1660,7 @@ sub _add_biblio_fields {
     # FIXME no controlnumber, originalsource
     $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->execute($import_record_id, $title, $author, $isbn, $issn) or die $sth->errstr;
     $sth->finish();
                 
 }