From 7b114c51eb971010ddb20e492e74090b19a24be4 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Tue, 8 May 2018 13:30:44 -0300 Subject: [PATCH] Bug 20366: Add new method Koha::Acquisition::Order->subscription Can be moved to a separate bug report. Sponsored-by: BULAC - http://www.bulac.fr/ Signed-off-by: Josef Moravec Signed-off-by: Katrin Fischer Signed-off-by: Nick Clemens --- Koha/Acquisition/Order.pm | 16 +++++++++++++ t/db_dependent/Koha/Acquisition/Order.t | 31 ++++++++++++++++++++++++- 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/Koha/Acquisition/Order.pm b/Koha/Acquisition/Order.pm index c35fcdf1f1..5620cf61be 100644 --- a/Koha/Acquisition/Order.pm +++ b/Koha/Acquisition/Order.pm @@ -22,6 +22,7 @@ use Carp qw( croak ); use Koha::Acquisition::Baskets; use Koha::Acquisition::Funds; use Koha::Acquisition::Invoices; +use Koha::Subscriptions; use Koha::Database; use Koha::DateUtils qw( dt_from_string output_pref ); @@ -151,6 +152,21 @@ sub invoice { return Koha::Acquisition::Invoice->_new_from_dbic( $invoice_rs ); } +=head3 subscription + + my $subscription = $order->subscription + +Returns the subscription associated to the order. + +=cut + +sub subscription { + my ( $self ) = @_; + my $subscription_rs = $self->_result->subscriptionid; + return unless $subscription_rs; + return Koha::Subscription->_new_from_dbic( $subscription_rs ); +} + =head2 Internal methods =head3 _type diff --git a/t/db_dependent/Koha/Acquisition/Order.t b/t/db_dependent/Koha/Acquisition/Order.t index c662e798c5..75d369aae3 100644 --- a/t/db_dependent/Koha/Acquisition/Order.t +++ b/t/db_dependent/Koha/Acquisition/Order.t @@ -19,7 +19,7 @@ use Modern::Perl; -use Test::More tests => 4; +use Test::More tests => 5; use t::lib::TestBuilder; use t::lib::Mocks; @@ -143,3 +143,32 @@ subtest 'invoice' => sub { $schema->storage->txn_rollback; }; + +subtest 'subscription' => sub { + plan tests => 2; + + $schema->storage->txn_begin; + my $o = $builder->build_object( + { + class => 'Koha::Acquisition::Orders', + value => { subscriptionid => undef }, # not linked to a subscription + } + ); + + my $order = Koha::Acquisition::Orders->find( $o->ordernumber ); + is( $order->subscription, undef, + '->subscription should return undef if not created from a subscription'); + + $o = $builder->build_object( + { + class => 'Koha::Acquisition::Orders', + # Will be linked to a subscription by TestBuilder + } + ); + + $order = Koha::Acquisition::Orders->find( $o->ordernumber ); + is( ref( $order->subscription ), 'Koha::Subscription', + '->subscription should return a Koha::Subscription object if created from a subscription'); + + $schema->storage->txn_rollback; +}; -- 2.39.5