Bug 24448: Add Koha::Biblio->subscriptions_count
[srvgit] / Koha / Biblio.pm
index 5b63653..28609dc 100644 (file)
@@ -32,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;
@@ -40,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
@@ -81,6 +83,37 @@ sub metadata {
     return Koha::Biblio::Metadata->_new_from_dbic($metadata);
 }
 
+=head3 orders
+
+my $orders = $biblio->orders();
+
+Returns a Koha::Acquisition::Orders object
+
+=cut
+
+sub orders {
+    my ( $self ) = @_;
+
+    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
 
 my $bool = $biblio->can_article_request( $borrower );
@@ -378,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();
@@ -439,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
@@ -455,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
@@ -724,6 +800,53 @@ sub custom_cover_image_url {
     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