From 7ff2333ba1c7635dd6ae9cc87b67d3b75ce3559d Mon Sep 17 00:00:00 2001 From: Pedro Amorim Date: Fri, 10 May 2024 12:33:04 +0000 Subject: [PATCH] Bug 26869: Add MARC modification template mapping validation Follow the same test plan as above, but attempt to add an item without having at least homebranch, holdingbranch and type subfields. Notice you now get an error with an appropriate message and the item is not added. --- Koha/BackgroundJob/BatchUpdateBiblio.pm | 65 ++++++++++++++++++++++++- 1 file changed, 63 insertions(+), 2 deletions(-) diff --git a/Koha/BackgroundJob/BatchUpdateBiblio.pm b/Koha/BackgroundJob/BatchUpdateBiblio.pm index 36cc8258d1..e2ffb95721 100644 --- a/Koha/BackgroundJob/BatchUpdateBiblio.pm +++ b/Koha/BackgroundJob/BatchUpdateBiblio.pm @@ -87,8 +87,14 @@ sub process { my $record = $biblio->metadata->record; C4::MarcModificationTemplates::ModifyRecordWithTemplate( $mmtid, $record ); my $frameworkcode = C4::Biblio::GetFrameworkCode( $biblionumber ); - my $biblioitemnumber = $biblio->biblioitem->biblioitemnumber; - C4::Items::AddItemFromMarc( $record, $biblionumber, { biblioitemnumber => $biblioitemnumber } ); + + if ( marc_record_contains_item_data($record) ) { + my ( $can_add_item, $add_item_error_message ) = can_add_item_from_marc_record($record); + return $add_item_error_message unless $can_add_item; + + my $biblioitemnumber = $biblio->biblioitem->biblioitemnumber; + C4::Items::AddItemFromMarc( $record, $biblionumber, { biblioitemnumber => $biblioitemnumber } ); + } C4::Biblio::ModBiblio( $record, $biblionumber, $frameworkcode, { overlay_context => $args->{overlay_context}, @@ -166,4 +172,59 @@ sub additional_report { }; } +=head3 marc_record_contains_item_data + +Verify if marc record contains item data + +=cut + +sub marc_record_contains_item_data { + my ($record) = @_; + + my $itemfield = C4::Context->preference('marcflavour') eq 'UNIMARC' ? '995' : '952'; + my @item_fields = $record->field($itemfield); + return scalar @item_fields; +} + +=head3 can_add_item_from_marc_record + +Checks if adding an item from MARC can be done by checking mandatory fields + +=cut + +sub can_add_item_from_marc_record { + my ($record) = @_; + + # Check that holdingbranch is set + my $holdingbranch_mss = Koha::MarcSubfieldStructures->find( + { + kohafield => 'items.holdingbranch', + } + ); + my @holdingbranch_exists = + grep { $_->subfield( $holdingbranch_mss->tagsubfield ) } $record->field( $holdingbranch_mss->tagfield ); + return ( 0, "No holdingbranch subfield found" ) unless ( scalar @holdingbranch_exists ); + + # Check that homebranch is set + my $homebranch_mss = Koha::MarcSubfieldStructures->find( + { + kohafield => 'items.homebranch', + } + ); + my @homebranch_exists = + grep { $_->subfield( $homebranch_mss->tagsubfield ) } $record->field( $homebranch_mss->tagfield ); + return ( 0, "No homebranch subfield found" ) unless ( scalar @homebranch_exists ); + + # Check that itemtype is set + my $item_mss = Koha::MarcSubfieldStructures->find( + { + kohafield => C4::Context->preference('item-level_itypes') ? 'items.itype' : 'biblioitems.itemtype', + } + ); + my @itemtype_exists = grep { $_->subfield( $item_mss->tagsubfield ) } $record->field( $item_mss->tagfield ); + return ( 0, "No itemtype subfield found" ) unless ( scalar @itemtype_exists ); + + return 1; +} + 1; -- 2.39.2