From 48a9be786b746fd3eddfdc313f67e79d29cdc22a Mon Sep 17 00:00:00 2001 From: Baptiste Wojtkowski Date: Fri, 4 Oct 2024 11:59:31 +0200 Subject: [PATCH] Bug 38093: Bundles: Add MARC link options should also add a link in 462 Bundle implementation in Bug 37996 does not implement marc representation of the relation between bundles and items they contain. Bug 29560 creates a link (773 in MARC21, 461 in UNIMARC) from the item in the bundle to the bundle itself. We also should also add a MARC link from the bundle to the item contained in the dedicated field (774 in MARC21, 462 in UNIMARC). TEST PLAN: 1 - Apply patches 2 - Create a bundle item (cf after for more info on this step if you are unfamiliar with bundles) 3 - Click on "manage bundle", fill with an existing barcode, click on Add marc link 4 - Click on MARC -> tab 7 (on marc21) and tab 4 on unimarc -> there should be a link in field 733 (marc21) or 462 (unimarc) 5 - Go back to the bundle and remove the item from the bundle. 6 - Click again on MARC -> precedent fields should now be empty Create a bundle : 1 - Create a new biblio/Edit an existing one, edit the field 000, the field 7 must be set to c-Collection. 2 - Go on the biblio and create a new item attached to the biblio 3 - You should see an icon "Manage bundle" on the item created --- Koha/Biblio.pm | 40 ++++++++++++++++++++++++++++++++++++++++ Koha/REST/V1/Items.pm | 1 + 2 files changed, 41 insertions(+) diff --git a/Koha/Biblio.pm b/Koha/Biblio.pm index ceeac0f5..c2c011fe 100644 --- a/Koha/Biblio.pm +++ b/Koha/Biblio.pm @@ -1999,6 +1999,46 @@ sub link_marc_host { return $self; } +=head3 link_marc_bundled_item + + $biblio->link_marc_bundled_item({ bundled_item => $item }); + $biblio->link_marc_bundled_item({ bundled_item => $itemnumber }); + +Links a parent MARC record to the child. Expects either the biblio object or biblionumber of the host to link to. + +=cut + +sub link_marc_bundled_item { + my ( $self, $params ) = @_; + + my $host_link_field; + my $bundled_item; + if ( ref( $params->{bundled_item} ) eq 'Koha::Item' ) { + $bundled_item = $params->{bundled_item}; + } else { + my $bundled_item = Koha::Items->find( $params->{host} ); + } + + my $record = $self->metadata->record; + my $tag = C4::Context->preference('marcflavour') eq 'UNIMARC' ? '462' : '774'; + # Remove fields that references the same itemnumber + # to avoid duplicates + my @fields = $record->field($tag); + my @duplicate_fields = grep { $_->subfield('9') eq $bundled_item->itemnumber; } @fields; + $record->delete_fields(@duplicate_fields); + my $field = MARC::Field->new( + $tag, '', '', + 't' => $bundled_item->biblio->title, + '0' => $bundled_item->biblionumber, + '9' => $bundled_item->itemnumber + ); + $record->insert_fields_ordered($field); + + C4::Biblio::ModBiblio( $record, $self->biblionumber ); + + return $self; +} + =head3 recalls my $recalls = $biblio->recalls; diff --git a/Koha/REST/V1/Items.pm b/Koha/REST/V1/Items.pm index cfa60e3b..96978370 100644 --- a/Koha/REST/V1/Items.pm +++ b/Koha/REST/V1/Items.pm @@ -327,6 +327,7 @@ sub add_to_bundle { my $link = $item->add_to_bundle( $bundle_item, $options ); if ($add_link) { $bundle_item->biblio->link_marc_host( { host => $item->biblio } ); + $item->biblio->link_marc_bundled_item( { bundled_item => $bundle_item } ); } return $c->render( status => 201, -- 2.30.2