From 1481f65815e2589819705ae7a5770aa85b91a08d Mon Sep 17 00:00:00 2001 From: Baptiste Wojtkowski Date: Fri, 4 Oct 2024 11:59:58 +0200 Subject: [PATCH] Bug 38093: Bundles: Remove the link created in 462 when removing from the bundle --- Koha/Biblio.pm | 32 ++++++++++++++++++++++++++++++++ Koha/REST/V1/Items.pm | 1 + 2 files changed, 33 insertions(+) diff --git a/Koha/Biblio.pm b/Koha/Biblio.pm index c2c011fe..6831dd7d 100644 --- a/Koha/Biblio.pm +++ b/Koha/Biblio.pm @@ -2039,6 +2039,38 @@ sub link_marc_bundled_item { return $self; } +=head3 unlink_marc_bundled_item + + $biblio->unlink_marc_bundled_item({ bundled_item => $item }); + $biblio->unlink_marc_bundled_item({ bundled_item => $itemnumber }); + +Remove the link to the child created by link_marc_bundled_item. Expects either the item object or itemnumber of the host to link to. + +=cut + +sub unlink_marc_bundled_item { + my ( $self, $params ) = @_; + my $bundled_item_itemnumber; + if ( ref( $params->{bundled_item} ) eq 'Koha::Item' ) { + $bundled_item_itemnumber = $params->{bundled_item}->itemnumber; + } else { + $bundled_item_itemnumber = $params->{bundled_item}; + } + + my $record = $self->metadata->record; + my $field_number = C4::Context->preference('marcflavour') eq 'MARC21' ? '774' : '462'; + my @fields = $record->field($field_number); + + foreach my $field (@fields) { + my $subfield_itemnumber = $field->subfield('9') // q{}; + if ( $bundled_item_itemnumber eq $subfield_itemnumber ) { + $record->delete_field($field); + } + } + + C4::Biblio::ModBiblio( $record, $self->biblionumber ); +} + =head3 recalls my $recalls = $biblio->recalls; diff --git a/Koha/REST/V1/Items.pm b/Koha/REST/V1/Items.pm index 96978370..8c5b501e 100644 --- a/Koha/REST/V1/Items.pm +++ b/Koha/REST/V1/Items.pm @@ -413,6 +413,7 @@ sub remove_from_bundle { return try { $bundle_item->biblio->unlink_marc_host({ host => $item->biblio }); + $item->biblio->unlink_marc_bundled_item( { bundled_item => $bundle_item } ); $bundle_item->remove_from_bundle; return $c->render_resource_deleted; } catch { -- 2.30.2