From 228bacd9bc6986b156321793bcc9f4deaf7d88fe Mon Sep 17 00:00:00 2001 From: Matthias Le Gac Date: Fri, 29 Mar 2024 13:54:36 -0400 Subject: [PATCH] Bug 21272: Improve warning message To test Before make sure backgroundjobs process is running (/misc/workers/background_jobs_worker.pl --queue long_tasks) 1- Go to Cataloging -> Stage records for import 2 - Upload a MARC file (koha.mrc) that contain items with homebranch and/or homelibraary set and is not present in your installation 3- Click on Stage for import 4- Click on View batch --> There is no message for the missing branches 5- Click on "Import this batch into the catalog" --> The importation failed 6- Apply the patch 7- Run prove -t t/db_dependent/ImportBatch.t 8- Click on "Manage imported batch" --> There is a warning message showing the missing branches --> There is a warning message showing the titles of the wrong items 9- Click on "Import this batch into the catalog" again --> The records are imported --> The items with wrong branches are ignored --- C4/ImportBatch.pm | 74 +++++++++++++++++++ .../en/modules/tools/manage-marc-import.tt | 11 +++ tools/manage-marc-import.pl | 19 ++++- 3 files changed, 100 insertions(+), 4 deletions(-) diff --git a/C4/ImportBatch.pm b/C4/ImportBatch.pm index 002e2f6572..fba7446ea4 100644 --- a/C4/ImportBatch.pm +++ b/C4/ImportBatch.pm @@ -42,6 +42,7 @@ use Koha::SearchEngine::Indexer; use Koha::Plugins::Handler; use Koha::Logger; use List::MoreUtils qw( uniq any ); +use Array::Utils qw( array_minus ); our ( @ISA, @EXPORT_OK ); @@ -74,6 +75,7 @@ BEGIN { GetImportRecordsRange GetItemNumbersFromImportBatch GetImportItemsBranches + GetBadBranchesImportItems GetImportBatchStatus SetImportBatchStatus @@ -1304,6 +1306,78 @@ sub GetImportItemsBranches { return uniq @branch; } +=head2 GetBadBranchesImportItems + + my @results = GetBadBranchesImportItems($batch_id); + +Returns an array of bad branches imported items. + +=cut + +sub GetBadBranchesImportItems { + my ($batch_id) = @_; + + my $dbh = C4::Context->dbh; + my $query = "SELECT import_items_id, import_record_id, import_items.marcxml, encoding + FROM import_items + JOIN import_records USING (import_record_id) + WHERE import_batch_id = ? + ORDER BY import_items_id"; + my @params; + push( @params, $batch_id ); + my $sth = $dbh->prepare($query); + $sth->execute(@params); + my $results = $sth->fetchall_arrayref( {} ); + my @items; + + my @import_items_branches = GetImportItemsBranches($batch_id); + my @branches = map { $_->branchcode } Koha::Libraries->search->as_list; + my @missing_branches = array_minus( @import_items_branches, @branches ); + my ( $homebranchfield, $homebranchsubfield ) = GetMarcFromKohaField('items.homebranch'); + my ( $holdingbranchfield, $holdingbranchsubfield ) = GetMarcFromKohaField('items.holdingbranch'); + + foreach my $row (@$results) { + my $item_marc = + MARC::Record->new_from_xml( StripNonXmlChars( $row->{'marcxml'} ), 'UTF-8', $row->{'encoding'} ); + my $title = GetTitleImportRecord( $row->{'import_record_id'} ); + foreach my $branch (@missing_branches) { + if ( $item_marc->subfield( $homebranchfield, $homebranchsubfield ) eq $branch ) { + push @items, { title => $title, branch => $branch }; + } + if ( $item_marc->subfield( $holdingbranchfield, $holdingbranchsubfield ) eq $branch ) { + push @items, { title => $title, branch => $branch }; + } + } + } + $sth->finish(); + return uniq @items; +} + +=head2 GetTitleImportRecord + + my $status = GetTitleImportRecord($import_record_id); + +=cut + +sub GetTitleImportRecord { + my ($import_record_id) = @_; + + my $dbh = C4::Context->dbh; + my $sth = $dbh->prepare( + "SELECT import_record_id, import_records.marcxml, encoding + FROM import_records WHERE import_record_id = ?" + ); + $sth->execute($import_record_id); + my $record = $sth->fetchall_arrayref( {} ); + my $marc_record = MARC::Record->new_from_xml( + StripNonXmlChars( $record->[0]->{'marcxml'} ), 'UTF-8', + $record->[0]->{'encoding'} + ); + $sth->finish(); + my $title = $marc_record->field('245')->subfield('a'); + return $title; +} + =head2 GetBestRecordMatch my $record_id = GetBestRecordMatch($import_record_id); diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/manage-marc-import.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/manage-marc-import.tt index 9bb67274fc..2623f27b06 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/manage-marc-import.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/manage-marc-import.tt @@ -266,6 +266,17 @@ [% END %] + [% IF ( bad_titles ) %] +
+ The problems come from these items: + + [% FOREACH title IN titles %] + [% title | html %] + | + [% END %] + +
+ [% END %] diff --git a/tools/manage-marc-import.pl b/tools/manage-marc-import.pl index a38714ae77..110e5bfb33 100755 --- a/tools/manage-marc-import.pl +++ b/tools/manage-marc-import.pl @@ -31,13 +31,14 @@ use C4::Koha; use C4::Auth qw( get_template_and_user ); use C4::Output qw( output_html_with_http_headers ); use C4::ImportBatch - qw( CleanBatch DeleteBatch GetImportBatch GetImportBatchOverlayAction GetImportBatchNoMatchAction GetImportBatchItemAction SetImportBatchOverlayAction SetImportBatchNoMatchAction SetImportBatchItemAction BatchFindDuplicates SetImportBatchMatcher GetItemNumbersFromImportBatch GetImportBatchRangeDesc GetNumberOfNonZ3950ImportBatches BatchCommitRecords BatchRevertRecords GetImportItemsBranches ); + qw( CleanBatch DeleteBatch GetImportBatch GetImportBatchOverlayAction GetImportBatchNoMatchAction GetImportBatchItemAction SetImportBatchOverlayAction SetImportBatchNoMatchAction SetImportBatchItemAction BatchFindDuplicates SetImportBatchMatcher GetItemNumbersFromImportBatch GetImportBatchRangeDesc GetNumberOfNonZ3950ImportBatches BatchCommitRecords BatchRevertRecords GetImportItemsBranches GetBadBranchesImportItems ); use C4::Matcher; use C4::Labels::Batch; use Koha::BiblioFrameworks; use Koha::BackgroundJob::MARCImportCommitBatch; use Koha::BackgroundJob::MARCImportRevertBatch; use Array::Utils qw( array_minus ); +use List::MoreUtils qw( uniq any ); use Koha::Logger; @@ -95,11 +96,21 @@ if ( $op eq "" ) { import_batches_list( $template, $offset, $results_per_page ); } else { my @import_items_branches = GetImportItemsBranches($import_batch_id); - my @branches = map { $_->branchcode } Koha::Libraries->search->as_list; - my @missing_branches = array_minus( @import_items_branches, @branches ); + my @import_items = GetBadBranchesImportItems($import_batch_id); + my @bad_item_titles; + my @missing_branches; + foreach my $item (@import_items) { + push @bad_item_titles, $item->{title}; + push @missing_branches, $item->{branch}; + } + @bad_item_titles = uniq @bad_item_titles; + @missing_branches = uniq @missing_branches; + $template->param( + bad_titles => scalar @bad_item_titles, + titles => \@bad_item_titles, missing_branches => scalar @missing_branches, - branches => \@missing_branches + branches => \@missing_branches, ); import_records_list( $template, $import_batch_id, $offset, $results_per_page ); } -- 2.34.1