From 6ced29d5bf839ef56d292c76460fc6b6a3834c92 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Fri, 12 Feb 2021 10:27:43 +0100 Subject: [PATCH] Bug 24469: Move the new queries to a dedicated ImportBatch subroutine Signed-off-by: Marcel de Rooy Amended: POD line for $import_record_id. Signed-off-by: Jonathan Druart (cherry picked from commit 03a9bdc851dcfe114c1676ac55def1e1c5d51970) Signed-off-by: Fridolin Somers (cherry picked from commit aba575daeda3f1cc545d80bea916148ffdd77910) Signed-off-by: Andrew Fuerste-Henry --- C4/ImportBatch.pm | 25 +++++++++++++++++++++---- acqui/addorderiso2709.pl | 6 +----- acqui/neworderempty.pl | 6 +----- t/db_dependent/ImportBatch.t | 7 ++++++- 4 files changed, 29 insertions(+), 15 deletions(-) diff --git a/C4/ImportBatch.pm b/C4/ImportBatch.pm index 5636b9ca96..63ca5e98a3 100644 --- a/C4/ImportBatch.pm +++ b/C4/ImportBatch.pm @@ -78,6 +78,7 @@ BEGIN { SetImportRecordOverlayStatus GetImportRecordStatus SetImportRecordStatus + SetMatchedBiblionumber GetImportRecordMatches SetImportRecordMatches ); @@ -631,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; @@ -664,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); @@ -697,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'); } @@ -881,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 = ?"; @@ -1217,6 +1218,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); diff --git a/acqui/addorderiso2709.pl b/acqui/addorderiso2709.pl index 83c6eaa5bc..4995b5a370 100755 --- a/acqui/addorderiso2709.pl +++ b/acqui/addorderiso2709.pl @@ -198,11 +198,7 @@ if ($op eq ""){ SetImportRecordStatus( $biblio->{'import_record_id'}, 'imported' ); } - my $dbh = C4::Context->dbh; - $dbh->do( - q|UPDATE import_biblios SET matched_biblionumber = ? WHERE import_record_id = ?|, - undef, $biblionumber, $biblio->{import_record_id} - ); + SetMatchedBiblionumber( $biblio->{import_record_id}, $biblionumber ); # Add items from MarcItemFieldsToOrder my @homebranches = $input->multi_param('homebranch_' . $biblio_count); diff --git a/acqui/neworderempty.pl b/acqui/neworderempty.pl index efbc188f90..43da8519d3 100755 --- a/acqui/neworderempty.pl +++ b/acqui/neworderempty.pl @@ -180,11 +180,7 @@ if ( $ordernumber eq '' and defined $params->{'breedingid'}){ $listprice = GetMarcPrice($marcrecord, $marcflavour); SetImportRecordStatus($params->{'breedingid'}, 'imported'); - my $dbh = C4::Context->dbh; - $dbh->do( - q|UPDATE import_biblios SET matched_biblionumber = ? WHERE import_record_id = ?|, - undef, $biblionumber, $params->{breedingid} - ); + SetMatchedBiblionumber( $params->{breedingid}, $biblionumber ); } diff --git a/t/db_dependent/ImportBatch.t b/t/db_dependent/ImportBatch.t index f437edc39b..9549bf907f 100644 --- a/t/db_dependent/ImportBatch.t +++ b/t/db_dependent/ImportBatch.t @@ -1,7 +1,7 @@ #!/usr/bin/perl use Modern::Perl; -use Test::More tests => 15; +use Test::More tests => 16; use utf8; use File::Basename; use File::Temp qw/tempfile/; @@ -130,6 +130,11 @@ my $record_from_import_biblio_without_items = C4::ImportBatch::GetRecordFromImpo $original_record->leader($record_from_import_biblio_without_items->leader()); is_deeply( $record_from_import_biblio_without_items, $original_record, 'GetRecordFromImportBiblio should return the record without items by default' ); +my $another_biblio = $builder->build_sample_biblio; +C4::ImportBatch::SetMatchedBiblionumber( $import_record_id, $another_biblio->biblionumber ); +my $import_biblios = GetImportBiblios( $import_record_id ); +is( $import_biblios->[0]->{matched_biblionumber}, $another_biblio->biblionumber, 'SetMatchedBiblionumber should set the correct biblionumber' ); + # Add a few tests for GetItemNumbersFromImportBatch my @a = GetItemNumbersFromImportBatch( $id_import_batch1 ); is( @a, 0, 'No item numbers expected since we did not commit' ); -- 2.39.5