Bug 24448: Add Koha::Biblio->subscriptions_count
[srvgit] / Koha / Biblio.pm
index d944cb0..28609dc 100644 (file)
@@ -24,6 +24,7 @@ use List::MoreUtils qw(any);
 use URI;
 use URI::Escape;
 
+use C4::Koha;
 use C4::Biblio qw();
 
 use Koha::Database;
@@ -31,6 +32,7 @@ use Koha::DateUtils qw( dt_from_string );
 
 use base qw(Koha::Object);
 
+use Koha::Acquisition::Orders;
 use Koha::ArticleRequest::Status;
 use Koha::ArticleRequests;
 use Koha::Biblio::Metadatas;
@@ -39,6 +41,7 @@ use Koha::IssuingRules;
 use Koha::Item::Transfer::Limits;
 use Koha::Items;
 use Koha::Libraries;
+use Koha::Suggestions;
 use Koha::Subscriptions;
 
 =head1 NAME
@@ -80,19 +83,35 @@ sub metadata {
     return Koha::Biblio::Metadata->_new_from_dbic($metadata);
 }
 
-=head3 subtitles
+=head3 orders
 
-my @subtitles = $biblio->subtitles();
+my $orders = $biblio->orders();
 
-Returns list of subtitles for a record according to the framework.
+Returns a Koha::Acquisition::Orders object
 
 =cut
 
-sub subtitles {
+sub orders {
     my ( $self ) = @_;
 
-    my @subtitles = split( / \| /, $self->subtitle // '' );
-    return @subtitles;
+    my $orders = $self->_result->orders;
+    return Koha::Acquisition::Orders->_new_from_dbic($orders);
+}
+
+=head3 active_orders_count
+
+my $orders_count = $biblio->active_orders_count();
+
+Returns the number of active acquisition orders related to this biblio.
+An order is considered active when it is not cancelled (i.e. when datecancellation
+is not undef).
+
+=cut
+
+sub active_orders_count {
+    my ( $self ) = @_;
+
+    return $self->orders->search({ datecancellationprinted => undef })->count;
 }
 
 =head3 can_article_request
@@ -183,6 +202,33 @@ sub can_be_transferred {
     return 0;
 }
 
+
+=head3 pickup_locations
+
+@pickup_locations = $biblio->pickup_locations( {patron => $patron } )
+
+Returns possible pickup locations for this biblio items, according to patron's home library (if patron is defined and holds are allowed only from hold groups)
+and if item can be transferred to each pickup location.
+
+=cut
+
+sub pickup_locations {
+    my ($self, $params) = @_;
+
+    my $patron = $params->{patron};
+
+    my @pickup_locations;
+    foreach my $item_of_bib ($self->items->as_list) {
+        push @pickup_locations, $item_of_bib->pickup_locations( {patron => $patron} );
+    }
+
+    my %seen;
+    @pickup_locations =
+      grep { !$seen{ $_->branchcode }++ } @pickup_locations;
+
+    return wantarray ? @pickup_locations : \@pickup_locations;
+}
+
 =head3 hidden_in_opac
 
 my $bool = $biblio->hidden_in_opac({ [ rules => $rules ] })
@@ -365,6 +411,20 @@ sub items {
     return Koha::Items->_new_from_dbic( $items_rs );
 }
 
+=head3 items_count
+
+my $items_count = $biblio->items();
+
+Returns the count of the the related Koha::Items object for this biblio
+
+=cut
+
+sub items_count {
+    my ($self) = @_;
+
+    return $self->_result->items->count;
+}
+
 =head3 itemtype
 
 my $itemtype = $biblio->itemtype();
@@ -426,6 +486,21 @@ sub biblioitem {
     return $self->{_biblioitem};
 }
 
+=head3 suggestions
+
+my $suggestions = $self->suggestions
+
+Returns the related Koha::Suggestions object for this Biblio object
+
+=cut
+
+sub suggestions {
+    my ($self) = @_;
+
+    my $suggestions_rs = $self->_result->suggestions;
+    return Koha::Suggestions->_new_from_dbic( $suggestions_rs );
+}
+
 =head3 subscriptions
 
 my $subscriptions = $self->subscriptions
@@ -442,6 +517,20 @@ sub subscriptions {
     return $self->{_subscriptions};
 }
 
+=head3 subscriptions_count
+
+my $subscriptions_count = $self->subscriptions_count
+
+Returns the count of the the related Koha::Subscriptions object for this biblio
+
+=cut
+
+sub subscriptions_count {
+    my ($self) = @_;
+
+    return $self->subscriptions->count;
+}
+
 =head3 has_items_waiting_or_intransit
 
 my $itemsWaitingOrInTransit = $biblio->has_items_waiting_or_intransit
@@ -574,7 +663,7 @@ sub get_coins {
                 push @authors, $au;
             }
         }
-        $title = $record->subfield( '245', 'a' ) . $record->subfield( '245', 'b' );
+        $title = $record->field('245')->as_string('ab');
         if ($titletype eq 'a') {
             $pubyear   = $record->field('008') || '';
             $pubyear   = substr($pubyear->data(), 7, 4) if $pubyear;
@@ -655,6 +744,109 @@ sub get_openurl {
     return $OpenURLResolverURL;
 }
 
+=head3 is_serial
+
+my $serial = $biblio->is_serial
+
+Return boolean true if this bibbliographic record is continuing resource
+
+=cut
+
+sub is_serial {
+    my ( $self ) = @_;
+
+    return 1 if $self->serial;
+
+    my $record = $self->metadata->record;
+    return 1 if substr($record->leader, 7, 1) eq 's';
+
+    return 0;
+}
+
+=head3 custom_cover_image_url
+
+my $image_url = $biblio->custom_cover_image_url
+
+Return the specific url of the cover image for this bibliographic record.
+It is built regaring the value of the system preference CustomCoverImagesURL
+
+=cut
+
+sub custom_cover_image_url {
+    my ( $self ) = @_;
+    my $url = C4::Context->preference('CustomCoverImagesURL');
+    if ( $url =~ m|{isbn}| ) {
+        my $isbn = $self->biblioitem->isbn;
+        $url =~ s|{isbn}|$isbn|g;
+    }
+    if ( $url =~ m|{normalized_isbn}| ) {
+        my $normalized_isbn = C4::Koha::GetNormalizedISBN($self->biblioitem->isbn);
+        $url =~ s|{normalized_isbn}|$normalized_isbn|g;
+    }
+    if ( $url =~ m|{issn}| ) {
+        my $issn = $self->biblioitem->issn;
+        $url =~ s|{issn}|$issn|g;
+    }
+
+    my $re = qr|{(?<field>\d{3})\$(?<subfield>.)}|;
+    if ( $url =~ $re ) {
+        my $field = $+{field};
+        my $subfield = $+{subfield};
+        my $marc_record = $self->metadata->record;
+        my $value = $marc_record->subfield($field, $subfield);
+        $url =~ s|$re|$value|;
+    }
+
+    return $url;
+}
+
+=head3 to_api
+
+    my $json = $biblio->to_api;
+
+Overloaded method that returns a JSON representation of the Koha::Biblio object,
+suitable for API output. The related Koha::Biblioitem object is merged as expected
+on the API.
+
+=cut
+
+sub to_api {
+    my ($self, $args) = @_;
+
+    my @embeds = keys %{ $args->{embed} };
+    my $remaining_embeds = {};
+
+    foreach my $embed (@embeds) {
+        $remaining_embeds = delete $args->{embed}->{$embed}
+            unless $self->can($embed);
+    }
+
+    my $response = $self->SUPER::to_api( $args );
+    my $biblioitem = $self->biblioitem->to_api({ embed => $remaining_embeds });
+
+    return { %$response, %$biblioitem };
+}
+
+=head3 to_api_mapping
+
+This method returns the mapping for representing a Koha::Biblio object
+on the API.
+
+=cut
+
+sub to_api_mapping {
+    return {
+        biblionumber     => 'biblio_id',
+        frameworkcode    => 'framework_id',
+        unititle         => 'uniform_title',
+        seriestitle      => 'series_title',
+        copyrightdate    => 'copyright_date',
+        datecreated      => 'creation_date'
+    };
+}
+
+=head2 Internal methods
+
 =head3 type
 
 =cut