From d584f324f4553e8fd6a5cc80669a4301a75a7939 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Thu, 14 Dec 2023 10:55:42 +0100 Subject: [PATCH] Bug 33036: Use Koha::Objects Better to use Koha::Objects everywhere and have the same pattern for the different related objects. Signed-off-by: Jonathan Druart Signed-off-by: Katrin Fischer (cherry picked from commit 7bce391462de5665722994a61d5138841bf8db65) Signed-off-by: Fridolin Somers --- Koha/Biblio.pm | 69 +++++++++++++++++++++++++++----- t/db_dependent/Koha/Biblio.t | 76 ++++++++++++++++++++++++++++++++++++ 2 files changed, 136 insertions(+), 9 deletions(-) diff --git a/Koha/Biblio.pm b/Koha/Biblio.pm index 843ca2a01b..49175f1dcf 100644 --- a/Koha/Biblio.pm +++ b/Koha/Biblio.pm @@ -908,6 +908,34 @@ sub subscriptions { return Koha::Subscriptions->_new_from_dbic($rs); } +=head3 serials + +my $serials = $self->serials + +Returns the related Koha::Serials object for this Biblio object + +=cut + +sub serials { + my ($self) = @_; + my $rs = $self->_result->serials; + return Koha::Serials->_new_from_dbic($rs); +} + +=head3 subscription_histories + +my $subscription_histories = $self->subscription_histories + +Returns the related Koha::Subscription::Histories object for this Biblio object + +=cut + +sub subscription_histories { + my ($self) = @_; + my $rs = $self->_result->subscriptionhistories; + return Koha::Subscription::Histories->_new_from_dbic($rs); +} + =head3 has_items_waiting_or_intransit my $itemsWaitingOrInTransit = $biblio->has_items_waiting_or_intransit @@ -1866,27 +1894,50 @@ sub merge_with { foreach my $bn_merge (@biblio_ids_to_merge) { my $from_biblio = Koha::Biblios->find($bn_merge); $from_biblio->items->move_to_biblio($self); + + # Move article requests $from_biblio->article_requests->update( { biblionumber => $ref_biblionumber }, { no_triggers => 1 } ); - for my $resultset_name (qw(Subscription Subscriptionhistory Serial Suggestion)) { - $schema->resultset($resultset_name)->search( { biblionumber => $bn_merge } ) - ->update( { biblionumber => $ref_biblionumber } ); - } + # Move subscriptions + $from_biblio->subscriptions->update( + { biblionumber => $ref_biblionumber }, + { no_triggers => 1 } + ); - # TODO should this be ported to more modern DB usage (i.e. DBIx::Class)? - my @allorders = GetOrdersByBiblionumber($bn_merge); - foreach my $myorder (@allorders) { - $myorder->{'biblionumber'} = $ref_biblionumber; - ModOrder($myorder); + # Move subscription histories + $from_biblio->subscription_histories->update( + { biblionumber => $ref_biblionumber }, + { no_triggers => 1 } + ); + + # Move serials + $from_biblio->serials->update( + { biblionumber => $ref_biblionumber }, + { no_triggers => 1 } + ); + + # Move suggestions + $from_biblio->suggestions->update( + { biblionumber => $ref_biblionumber }, + { no_triggers => 1 } + ); + + my $orders = $from_biblio->orders->unblessed; + for my $order (@$orders) { + $order->{'biblionumber'} = $ref_biblionumber; + + # TODO Do we really need to call ModOrder here? + ModOrder($order); # TODO : add error control (in ModOrder?) } # Move holds MergeHolds( $schema->storage->dbh, $ref_biblionumber, $bn_merge ); + my $error = DelBiblio($bn_merge); #DelBiblio return undef unless an error occurs if ($error) { die $error; diff --git a/t/db_dependent/Koha/Biblio.t b/t/db_dependent/Koha/Biblio.t index 4217add479..d6cd9995d4 100755 --- a/t/db_dependent/Koha/Biblio.t +++ b/t/db_dependent/Koha/Biblio.t @@ -971,6 +971,41 @@ subtest 'tickets() tests' => sub { $schema->storage->txn_rollback; }; +subtest 'serials() tests' => sub { + + plan tests => 4; + + $schema->storage->txn_begin; + + my $biblio = $builder->build_sample_biblio; + + my $serials = $biblio->serials; + is( + ref($serials), 'Koha::Serials', + 'Koha::Biblio->serials should return a Koha::Serials object' + ); + is( $serials->count, 0, 'Koha::Biblio->serials should return the correct number of serials' ); + + # Add two serials + foreach ( 1 .. 2 ) { + $builder->build_object( + { + class => 'Koha::Serials', + value => { biblionumber => $biblio->biblionumber } + } + ); + } + + $serials = $biblio->serials; + is( + ref($serials), 'Koha::Serials', + 'Koha::Biblio->serials should return a Koha::Serials object' + ); + is( $serials->count, 2, 'Koha::Biblio->serials should return the correct number of serials' ); + + $schema->storage->txn_rollback; +}; + subtest 'subscriptions() tests' => sub { plan tests => 4; @@ -1004,6 +1039,47 @@ subtest 'subscriptions() tests' => sub { $schema->storage->txn_rollback; }; +subtest 'subscription_histories() tests' => sub { + + plan tests => 4; + + $schema->storage->txn_begin; + + my $biblio = $builder->build_sample_biblio; + + my $sub_histories = $biblio->subscription_histories; + is( + ref($sub_histories), 'Koha::Subscription::Histories', + 'Koha::Biblio->subscription_histories should return a Koha::Subscription::Histories object' + ); + is( + $sub_histories->count, 0, + 'Koha::Biblio->subscription_histories should return the correct number of subscription histories' + ); + + # Add two subscription histories + foreach ( 1 .. 2 ) { + $builder->build_object( + { + class => 'Koha::Subscription::Histories', + value => { biblionumber => $biblio->biblionumber } + } + ); + } + + $sub_histories = $biblio->subscription_histories; + is( + ref($sub_histories), 'Koha::Subscription::Histories', + 'Koha::Biblio->subscription_histories should return a Koha::Subscription::Histories object' + ); + is( + $sub_histories->count, 2, + 'Koha::Biblio->subscription_histories should return the correct number of subscription histories' + ); + + $schema->storage->txn_rollback; +}; + subtest 'get_marc_notes() MARC21 tests' => sub { plan tests => 14; -- 2.39.5