From a2ffd8bb64bf6adf7054687455d049ac1ed6d3a3 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Tue, 22 Oct 2024 08:48:37 -0300 Subject: [PATCH] Bug 38224: Add Koha::Biblio::populate_item_callnumbers This patch introduces two methods to the `Koha::Biblio` class: * `get_first_callnumber`: returns the first found callnumber on the record, based on the `itemcallnumber` syspref. * `populate_item_callnumbers`: populates the bibliographic record's items based on the return value of `get_first_callnumber`. Can be passed a filter on the items. To test: 1. Apply this patch 2. Run: $ ktd --shell k$ prove t/db_dependent/Koha/Biblio.t => SUCCESS: Tests pass! 3. Sign off :-D Signed-off-by: Nick Clemens --- Koha/Biblio.pm | 96 ++++++++++++++++++++++++++++++++++++++- Koha/Exceptions/Biblio.pm | 19 ++++++++ 2 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 Koha/Exceptions/Biblio.pm diff --git a/Koha/Biblio.pm b/Koha/Biblio.pm index d4d54eed42b..657901f42be 100644 --- a/Koha/Biblio.pm +++ b/Koha/Biblio.pm @@ -33,6 +33,8 @@ use C4::Acquisition qw( ModOrder GetOrdersByBiblionumber ); use Koha::Database; use Koha::DateUtils qw( dt_from_string ); use Koha::Exception; +use Koha::Exceptions::Biblio; +use Koha::Exceptions::SysPref; use base qw(Koha::Object); @@ -2230,9 +2232,101 @@ sub merge_with { return \%results; } +=head3 populate_item_callnumbers + + $biblio->populate_item_callnumbers( [ $filter ] ); + +Populates the linked items callnumbers following the I +system preference. Optionally, this action can be restricted by appliying +a I<$filter> on the items iterator. + +=cut + +sub populate_item_callnumbers { + my ( $self, $filter ) = @_; + + my $callnumber = $self->get_first_callnumber(); + my $items = $self->items->search($filter); + + if ( $items->count ) { + my ($res) = $items->batch_update( { new_values => { itemcallnumber => $callnumber } } ); + my @modified_itemnumbers = @{ $res->{modified_itemnumbers} }; + $self->add_message( + { + message => q{populate_item_callnumbers}, + type => q{info}, + payload => { + updated_items_count => scalar @modified_itemnumbers, + callnumber => $callnumber, + modified_item_ids => \@modified_itemnumbers, + } + } + ); + } else { + $self->add_message( + { + message => q{populate_item_callnumbers}, + type => q{info}, + payload => { + updated_items_count => 0, + callnumber => $callnumber, + } + } + ); + } + + return $self; +} + +=head3 get_first_callnumber + + my $callnumber = $biblio->get_first_callnumber(); + +Retrieves the first callnumber it finds using the I system +preference. Returns undef if it doesn't find any. + +Throws a I exception if the relevant +system preference is not set. + +Throws a I exception if the record +doesn't contain a callnumber. + +=cut + +sub get_first_callnumber { + my ($self) = @_; + + my $cn_fields = C4::Context->preference(q{itemcallnumber}); + Koha::Exceptions::SysPref::NotSet->throw( syspref => q{itemcallnumber} ) + unless $cn_fields; + + my $record = $self->record; + my $callnumber; + + foreach my $callnumber_marc_field ( split( /,/, $cn_fields ) ) { + + my $callnumber_tag = substr( $callnumber_marc_field, 0, 3 ); + my $callnumber_subfields = substr( $callnumber_marc_field, 3 ); + + next unless $callnumber_tag && $callnumber_subfields; + + my $field = $record->field($callnumber_tag); + + next unless $field; + + $callnumber = $field->as_string( $callnumber_subfields, '' ); + last if $callnumber; + } + + Koha::Exceptions::Biblio::MissingField->throw( field => q{callnumber} ) + unless defined $callnumber; + + return $callnumber; +} + =head2 Internal methods -=head3 type +=head3 _type =cut diff --git a/Koha/Exceptions/Biblio.pm b/Koha/Exceptions/Biblio.pm new file mode 100644 index 00000000000..6fd1abd811d --- /dev/null +++ b/Koha/Exceptions/Biblio.pm @@ -0,0 +1,19 @@ +package Koha::Exceptions::Biblio; + +use Modern::Perl; + +use Koha::Exception; + +use Exception::Class ( + + 'Koha::Exceptions::Biblio' => { + isa => 'Koha::Exception', + }, + 'Koha::Exceptions::Biblio::MissingField' => { + isa => 'Koha::Exceptions::Biblio', + description => 'The required field is missing in the record', + fields => ['field'] + } +); + +1; -- 2.39.5