From bfa86407ca67f8431000ca1f0776c4e0f6bf236b 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 Signed-off-by: David Nind Signed-off-by: PhilipOrr --- C4/ImportBatch.pm | 87 ++++++++++++++++++- .../en/modules/tools/manage-marc-import.tt | 9 ++ tools/manage-marc-import.pl | 10 ++- 3 files changed, 104 insertions(+), 2 deletions(-) diff --git a/C4/ImportBatch.pm b/C4/ImportBatch.pm index ccf87a8867..69ab902ce2 100644 --- a/C4/ImportBatch.pm +++ b/C4/ImportBatch.pm @@ -17,9 +17,38 @@ package C4::ImportBatch; # You should have received a copy of the GNU General Public License # along with Koha; if not, see . -use Modern::Perl; use base 'Exporter'; +use strict; +use warnings; + +use Modern::Perl; + +use C4::Context; +use C4::Koha qw( GetNormalizedISBN ); +use C4::Biblio qw( + AddBiblio + DelBiblio + GetMarcFromKohaField + GetXmlBiblio + ModBiblio + TransformMarcToKoha +); +use C4::Items qw( AddItemFromMarc ModItemFromMarc ); +use C4::Charset qw( MarcToUTF8Record SetUTF8Flag StripNonXmlChars ); +use C4::AuthoritiesMarc + qw( AddAuthority GuessAuthTypeCode GetAuthorityXML ModAuthority DelAuthority GetAuthorizedHeading ); +use C4::MarcModificationTemplates qw( ModifyRecordWithTemplate ); +use Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue; +use Koha::Items; +use Koha::SearchEngine; +use Koha::SearchEngine::Indexer; +use Koha::Plugins::Handler; +use Koha::Logger; +use List::MoreUtils qw( uniq ); + +our ( @ISA, @EXPORT_OK ); + BEGIN { our @EXPORT_OK = qw( GetZ3950BatchId @@ -46,6 +75,7 @@ BEGIN { GetImportBiblios GetImportRecordsRange GetItemNumbersFromImportBatch + GetImportItemsBranches GetImportBatchStatus SetImportBatchStatus @@ -839,6 +869,27 @@ 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 } @@ -1250,6 +1301,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 34cc0bd30e..6db3362512 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 @@ -257,6 +257,15 @@
+ [% IF ( missing_branches ) %] +
There are some missing branches in your installation : + [% FOREACH branche IN branches %] + + [% branche | html %] + + [% END %] +
+ [% END %] diff --git a/tools/manage-marc-import.pl b/tools/manage-marc-import.pl index 55f8bd264b..c48805e9c5 100755 --- a/tools/manage-marc-import.pl +++ b/tools/manage-marc-import.pl @@ -31,12 +31,13 @@ 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 ); + 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; @@ -93,6 +94,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 "cud-commit-batch" ) { -- 2.52.0