@@ -, +, @@ --- Koha/Biblio.pm | 16 ++++++++++++++++ Koha/Item.pm | 22 ++++++++++++++++++++++ t/db_dependent/Koha/Biblio.t | 29 ++++++++++++++++++++++++++++- t/db_dependent/Koha/Item.t | 27 ++++++++++++++++++++++++++- 4 files changed, 92 insertions(+), 2 deletions(-) --- a/Koha/Biblio.pm +++ a/Koha/Biblio.pm @@ -34,6 +34,7 @@ use base qw(Koha::Object); use Koha::Acquisition::Orders; use Koha::ArticleRequests; use Koha::Biblio::Metadatas; +use Koha::Biblio::ItemGroups; use Koha::Biblioitems; use Koha::Checkouts; use Koha::CirculationRules; @@ -117,6 +118,21 @@ sub active_orders { return $self->orders->search({ datecancellationprinted => undef }); } +=head3 item_groups + +my $item_groups = $biblio->item_groups(); + +Returns a Koha::Biblio::ItemGroups object + +=cut + +sub item_groups { + my ( $self ) = @_; + + my $item_groups = $self->_result->item_groups; + return Koha::Biblio::ItemGroups->_new_from_dbic($item_groups); +} + =head3 can_article_request my $bool = $biblio->can_article_request( $borrower ); --- a/Koha/Item.pm +++ a/Koha/Item.pm @@ -30,6 +30,7 @@ use C4::Reserves; use C4::ClassSource qw( GetClassSort ); use C4::Log qw( logaction ); +use Koha::Biblio::ItemGroups; use Koha::Checkouts; use Koha::CirculationRules; use Koha::CoverImages; @@ -403,6 +404,27 @@ sub checkout { return Koha::Checkout->_new_from_dbic( $checkout_rs ); } +=head3 item_group + +my $item_group = $item->item_group; + +Return the item group for this item + +=cut + +sub item_group { + my ( $self ) = @_; + + my $item_group_item = $self->_result->item_group_item; + return unless $item_group_item; + + my $item_group_rs = $item_group_item->item_group; + return unless $item_group_rs; + + my $item_group = Koha::Biblio::ItemGroup->_new_from_dbic( $item_group_rs ); + return $item_group; +} + =head3 holds my $holds = $item->holds(); --- a/t/db_dependent/Koha/Biblio.t +++ a/t/db_dependent/Koha/Biblio.t @@ -17,7 +17,7 @@ use Modern::Perl; -use Test::More tests => 20; +use Test::More tests => 21; use Test::Warn; use C4::Biblio qw( AddBiblio ModBiblio ModBiblioMarc ); @@ -884,6 +884,33 @@ subtest 'get_marc_authors() tests' => sub { $schema->storage->txn_rollback; }; +subtest 'item_groups() tests' => sub { + + plan tests => 6; + + $schema->storage->txn_begin; + + my $biblio = $builder->build_sample_biblio(); + + my @item_groups = $biblio->item_groups->as_list; + is( scalar(@item_groups), 0, 'Got zero item groups'); + + my $item_group_1 = Koha::Biblio::ItemGroup->new( { biblio_id => $biblio->id } )->store(); + + @item_groups = $biblio->item_groups->as_list; + is( scalar(@item_groups), 1, 'Got one item group'); + is( $item_groups[0]->id, $item_group_1->id, 'Got correct item group'); + + my $item_group_2 = Koha::Biblio::ItemGroup->new( { biblio_id => $biblio->id } )->store(); + + @item_groups = $biblio->item_groups->as_list; + is( scalar(@item_groups), 2, 'Got two item groups'); + is( $item_groups[0]->id, $item_group_1->id, 'Got correct item group 1'); + is( $item_groups[1]->id, $item_group_2->id, 'Got correct item group 2'); + + $schema->storage->txn_rollback; +}; + sub component_record1 { my $marc = MARC::Record->new; $marc->append_fields( --- a/t/db_dependent/Koha/Item.t +++ a/t/db_dependent/Koha/Item.t @@ -20,7 +20,7 @@ use Modern::Perl; use utf8; -use Test::More tests => 14; +use Test::More tests => 15; use Test::Exception; use C4::Biblio qw( GetMarcSubfieldStructure ); @@ -1224,3 +1224,28 @@ subtest 'store() tests' => sub { $schema->storage->txn_rollback; }; }; + +subtest 'item_group() tests' => sub { + + plan tests => 4; + + $schema->storage->txn_begin; + + my $biblio = $builder->build_sample_biblio(); + my $item_1 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber }); + my $item_2 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber }); + + is( $item_1->item_group, undef, 'Item 1 has no item group'); + is( $item_2->item_group, undef, 'Item 2 has no item group'); + + my $item_group_1 = Koha::Biblio::ItemGroup->new( { biblio_id => $biblio->id } )->store(); + my $item_group_2 = Koha::Biblio::ItemGroup->new( { biblio_id => $biblio->id } )->store(); + + $item_group_1->add_item({ item_id => $item_1->id }); + $item_group_2->add_item({ item_id => $item_2->id }); + + is( $item_1->item_group->id, $item_group_1->id, 'Got item group 1 correctly' ); + is( $item_2->item_group->id, $item_group_2->id, 'Got item group 2 correctly' ); + + $schema->storage->txn_rollback; +}; --