From f30c2874cdcbaf008ecc44fafccb89d44403e5f6 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Wed, 15 Jan 2020 16:25:52 -0300 Subject: [PATCH] Bug 24430: Add ->orders and ->active_orders_count to Koha::Biblio This patch introduces an accessor to the related orders, and a method that returns the active orders count. The target usage for the count is the API. In the rest of the codebase, we would just call: $biblio->orders->count To test: 1. Apply this patches 2. Run: $ kshell k$ prove t/db_dependent/Koha/Biblio.t => SUCCESS: Tests pass! 3. Sign off :-D Signed-off-by: Tomas Cohen Arazi Signed-off-by: Nick Clemens Signed-off-by: Martin Renvoize --- Koha/Biblio.pm | 32 ++++++++++++++++++++++++++++++++ Koha/Schema/Result/Biblio.pm | 7 +++++++ 2 files changed, 39 insertions(+) diff --git a/Koha/Biblio.pm b/Koha/Biblio.pm index 9ee4192e03..d0c291a528 100644 --- a/Koha/Biblio.pm +++ b/Koha/Biblio.pm @@ -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; @@ -82,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 ); diff --git a/Koha/Schema/Result/Biblio.pm b/Koha/Schema/Result/Biblio.pm index 116764ec69..7c66b1dc03 100644 --- a/Koha/Schema/Result/Biblio.pm +++ b/Koha/Schema/Result/Biblio.pm @@ -417,6 +417,13 @@ __PACKAGE__->has_one( { cascade_copy => 0, cascade_delete => 0 }, ); +__PACKAGE__->has_many( + "orders", + "Koha::Schema::Result::Aqorder", + { "foreign.biblionumber" => "self.biblionumber" }, + { cascade_copy => 0, cascade_delete => 0 }, +); + __PACKAGE__->add_columns( "+serial" => { is_boolean => 1 } ); -- 2.20.1