From 599c227092da598560212c34926c7bf124c90e57 Mon Sep 17 00:00:00 2001 From: Hammat Wele Date: Thu, 31 Aug 2023 19:10:12 +0000 Subject: [PATCH] Bug 21272: MARC import should warn about mis-matched branch and item-type fields in 952 during staged import This plugin add a warning message to show missing branches during staged import, it also ignore item with wrong banches. 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 in a link 9- Click on "Import this batch into the catalog" again --> The records are imported --> The items with wrong branches are ignored https://bugs.koha-community.org/show_bug.cgi?id=12532 --- C4/ImportBatch.pm | 56 +++++++++++++++++++ .../en/modules/tools/manage-marc-import.tt | 9 +++ tools/manage-marc-import.pl | 10 +++- 3 files changed, 74 insertions(+), 1 deletion(-) diff --git a/C4/ImportBatch.pm b/C4/ImportBatch.pm index b950d193bd..c9793c99ee 100644 --- a/C4/ImportBatch.pm +++ b/C4/ImportBatch.pm @@ -39,6 +39,7 @@ use Koha::SearchEngine; use Koha::SearchEngine::Indexer; use Koha::Plugins::Handler; use Koha::Logger; +use List::MoreUtils qw( uniq ); our (@ISA, @EXPORT_OK); BEGIN { @@ -69,6 +70,7 @@ BEGIN { GetImportBiblios GetImportRecordsRange GetItemNumbersFromImportBatch + GetImportItemsBranches GetImportBatchStatus SetImportBatchStatus @@ -812,6 +814,26 @@ sub _batchCommitItems { my ( $itemtag, $itemsubfield ) = GetMarcFromKohaField( "items.itemnumber" ); $item_marc->field($itemtag)->delete_subfield( code => $itemsubfield ); + my ( $homebranchfield, $homebranchsubfield ) = GetMarcFromKohaField('items.homebranch'); + my $homebranch = $item_marc->subfield( $homebranchfield, $homebranchsubfield ); + my ( $holdingbranchfield, $holdingbranchsubfield ) = GetMarcFromKohaField('items.holdingbranch'); + my $holdingbranch = $item_marc->subfield( $holdingbranchfield, $holdingbranchsubfield ); + + my @branches = map { $_->branchcode } Koha::Libraries->search->as_list; + my %branches = map { $_ => 1 } @branches; + + my $missing_branchcode; + $missing_branchcode = $homebranch if ( !exists( $branches{$homebranch} ) ); + $missing_branchcode = $holdingbranch if ( !exists( $branches{$holdingbranch} ) ); + if ($missing_branchcode) { + $updsth->bind_param( 1, 'error' ); + $updsth->bind_param( 2, undef ); + $updsth->bind_param( 3, "Branch code $missing_branchcode missing" ); + $updsth->bind_param( 4, $row->{'import_items_id'} ); + $updsth->execute(); + $num_items_errored++; + next; + } my ( $item_biblionumber, $biblioitemnumber, $itemnumber ) = AddItemFromMarc( $item_marc, $biblionumber, { biblioitemnumber => $biblioitemnumber, skip_record_index => 1 } ); if( $itemnumber ) { $updsth->bind_param( 1, 'imported' ); @@ -1195,6 +1217,40 @@ sub GetImportRecordsRange { } +=head2 GetImportItemsBranches + + my @results = GetImportItemsBranches($batch_id); + +Returns an array of imported item branches. + +=cut + +sub GetImportItemsBranches { + my ( $batch_id ) = @_; + + my $dbh = C4::Context->dbh; + my $query = "SELECT import_items_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_cached($query); + $sth->execute(@params); + my $results = $sth->fetchall_arrayref( {} ); + my @branch; + 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'} ); + push @branch, $item_marc->subfield( $homebranchfield, $homebranchsubfield ); + push @branch, $item_marc->subfield( $holdingbranchfield, $holdingbranchsubfield );; + } + $sth->finish(); + return uniq @branch; +} + =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 e11c2611a1..a9e218fbab 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 @@ -287,6 +287,15 @@
+ [% IF ( missing_branches ) %] +
There are some missing branches in your installation : + [% FOREACH branche IN branches %] + + [% branche | html %] + + [% END %] +
+ [% END %] [% END # /IF can_commit %] [% IF ( can_revert ) %] diff --git a/tools/manage-marc-import.pl b/tools/manage-marc-import.pl index e74d9255ab..c7869314f9 100755 --- a/tools/manage-marc-import.pl +++ b/tools/manage-marc-import.pl @@ -30,12 +30,13 @@ use C4::Context; 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 ); +use C4::ImportBatch qw( CleanBatch DeleteBatch GetImportBatch GetImportBatchOverlayAction GetImportBatchNoMatchAction GetImportBatchItemAction SetImportBatchOverlayAction SetImportBatchNoMatchAction SetImportBatchItemAction BatchFindDuplicates SetImportBatchMatcher GetItemNumbersFromImportBatch GetImportBatchRangeDesc GetNumberOfNonZ3950ImportBatches BatchCommitRecords BatchRevertRecords GetImportItemsBranches ); 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 Koha::Logger; @@ -85,6 +86,13 @@ if ($op eq "") { if ($import_batch_id 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 ); + $template->param( + missing_branches => scalar @missing_branches, + branches => \@missing_branches + ); import_records_list($template, $import_batch_id, $offset, $results_per_page); } } elsif ($op eq "commit-batch") { -- 2.34.1