Bugzilla – Attachment 178921 Details for
Bug 21272
MARC import should warn about mis-matched branch during staged import
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 21272: MARC import should warn about mis-matched branch and item-type fields in 952 during staged import
Bug-21272-MARC-import-should-warn-about-mis-matche.patch (text/plain), 8.11 KB, created by
Hammat wele
on 2025-03-04 15:14:48 UTC
(
hide
)
Description:
Bug 21272: MARC import should warn about mis-matched branch and item-type fields in 952 during staged import
Filename:
MIME Type:
Creator:
Hammat wele
Created:
2025-03-04 15:14:48 UTC
Size:
8.11 KB
patch
obsolete
>From 5008bb9426429edf5a6a79d0de0abec829239d5b Mon Sep 17 00:00:00 2001 >From: Hammat Wele <hammat.wele@inlibro.com> >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 <david@davidnind.com> >--- > C4/ImportBatch.pm | 57 +++++++++++++++++++ > .../en/modules/tools/manage-marc-import.tt | 9 +++ > tools/manage-marc-import.pl | 10 +++- > 3 files changed, 75 insertions(+), 1 deletion(-) > >diff --git a/C4/ImportBatch.pm b/C4/ImportBatch.pm >index b476da0e51..42ea2594f5 100644 >--- a/C4/ImportBatch.pm >+++ b/C4/ImportBatch.pm >@@ -41,6 +41,7 @@ use Koha::SearchEngine; > use Koha::SearchEngine::Indexer; > use Koha::Plugins::Handler; > use Koha::Logger; >+use List::MoreUtils qw( uniq ); > > our ( @ISA, @EXPORT_OK ); > >@@ -72,6 +73,7 @@ BEGIN { > GetImportBiblios > GetImportRecordsRange > GetItemNumbersFromImportBatch >+ GetImportItemsBranches > > GetImportBatchStatus > SetImportBatchStatus >@@ -843,6 +845,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 } >@@ -1248,6 +1271,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 e78408d57d..732518cd63 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 @@ > <fieldset class="action"> > <input type="submit" class="button" name="mainformsubmit" value="Import this batch into the catalog" /> > </fieldset> >+ [% IF ( missing_branches ) %] >+ <div class="dialog alert">There are some missing branches in your installation : >+ [% FOREACH branche IN branches %] >+ <a href="/cgi-bin/koha/admin/branches.pl?op=add_form" target="_blank"> >+ [% branche | html %] >+ </a> >+ [% END %] >+ </div> >+ [% END %] > </form> > <!-- /#import_batch_form --> > </div> >diff --git a/tools/manage-marc-import.pl b/tools/manage-marc-import.pl >index 38f0426168..a38714ae77 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.34.1
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 21272
:
155038
|
155039
|
155043
|
155523
|
155524
|
155525
|
158683
|
163646
|
163648
|
164143
|
164147
|
164148
|
164149
|
178921
|
178922
|
178923
|
178924
|
178925
|
178926
|
178927
|
178928
|
179837
|
179838
|
179839
|
179840
|
179841
|
179842
|
179843
|
179844
|
180383
|
180534
|
180535
|
180536
|
180537
|
180538
|
180539
|
180540
|
180541
|
180542