From 62c1146c89699ba7f10a5dbd60f450d17e247206 Mon Sep 17 00:00:00 2001
From: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Date: Mon, 18 Nov 2019 15:52:57 +0100
Subject: [PATCH] Bug 23591: Add some useful methods to Koha::Suggestions

Those are methods initially written for bug 23991. I finally need them
before than expected.
---
 Koha/Suggestion.pm                | 75 +++++++++++++++++++++++++++++++++++++++
 t/db_dependent/Koha/Suggestions.t | 63 ++++++++++++++++++++++++++++++--
 2 files changed, 136 insertions(+), 2 deletions(-)

diff --git a/Koha/Suggestion.pm b/Koha/Suggestion.pm
index 2fef425197..2acf95873a 100644
--- a/Koha/Suggestion.pm
+++ b/Koha/Suggestion.pm
@@ -53,6 +53,81 @@ sub store {
     return $self->SUPER::store();
 }
 
+=head3 manager
+
+my $manager = $suggestion->manager;
+
+Returns the manager of the suggestion (Koha::Patron for managedby field)
+
+=cut
+
+sub manager {
+    my ($self) = @_;
+    my $manager_rs = $self->_result->managedby;
+    return unless $manager_rs;
+    return Koha::Patron->_new_from_dbic($manager_rs);
+}
+
+=head3 suggester
+
+my $suggester = $suggestion->suggester;
+
+Returns the suggester of the suggestion (Koha::Patron for suggestedby field)
+
+=cut
+
+sub suggester {
+    my ($self) = @_;
+    my $suggester_rs = $self->_result->managedby;
+    return unless $suggester_rs;
+    return Koha::Patron->_new_from_dbic($suggester_rs);
+}
+
+=head3 rejecter
+
+my $rejecter = $suggestion->rejecter;
+
+Returns the rejecter of the suggestion (Koha::Patron for rejectebby field)
+
+=cut
+
+sub rejecter {
+    my ($self) = @_;
+    my $rejecter_rs = $self->_result->managedby;
+    return unless $rejecter_rs;
+    return Koha::Patron->_new_from_dbic($rejecter_rs);
+}
+
+=head3 last_modifier
+
+my $last_modifier = $suggestion->last_modifier;
+
+Returns the librarian who last modified the suggestion (Koha::Patron for lastmodificationby field)
+
+=cut
+
+sub last_modifier {
+    my ($self) = @_;
+    my $last_modifier_rs = $self->_result->managedby;
+    return unless $last_modifier_rs;
+    return Koha::Patron->_new_from_dbic($last_modifier_rs);
+}
+
+=head3 fund
+
+my $fund = $suggestion->fund;
+
+Return the fund associated to the suggestion
+
+=cut
+
+sub fund {
+    my ($self) = @_;
+    my $fund_rs = $self->_result->budgetid;
+    return unless $fund_rs;
+    return Koha::Acquisition::Fund->_new_from_dbic($fund_rs);
+}
+
 =head3 type
 
 =cut
diff --git a/t/db_dependent/Koha/Suggestions.t b/t/db_dependent/Koha/Suggestions.t
index 480c387cb2..c1767904d5 100644
--- a/t/db_dependent/Koha/Suggestions.t
+++ b/t/db_dependent/Koha/Suggestions.t
@@ -1,6 +1,6 @@
 #!/usr/bin/perl
 
-# Copyright 2015 Koha Development team
+# Copyright 2015-2019 Koha Development team
 #
 # This file is part of Koha
 #
@@ -19,7 +19,7 @@
 
 use Modern::Perl;
 
-use Test::More tests => 6;
+use Test::More tests => 8;
 use Test::Exception;
 
 use Koha::Suggestion;
@@ -171,3 +171,62 @@ subtest 'constraints' => sub {
     $schema->storage->dbh->{PrintError} = $print_error;
     $schema->storage->txn_rollback;
 };
+
+subtest 'manager, suggester, rejecter, last_modifier' => sub {
+    plan tests => 8;
+    $schema->storage->txn_begin;
+
+    my $suggestion = $builder->build_object( { class => 'Koha::Suggestions' } );
+
+    is( ref( $suggestion->manager ),
+        'Koha::Patron',
+        '->manager should have returned a Koha::Patron object' );
+    is( ref( $suggestion->rejecter ),
+        'Koha::Patron',
+        '->rejecter should have returned a Koha::Patron object' );
+    is( ref( $suggestion->suggester ),
+        'Koha::Patron',
+        '->suggester should have returned a Koha::Patron object' );
+    is( ref( $suggestion->last_modifier ),
+        'Koha::Patron',
+        '->last_modifier should have returned a Koha::Patron object' );
+
+    $suggestion->set(
+        {
+            managedby          => undef,
+            rejectedby         => undef,
+            suggestedby        => undef,
+            lastmodificationby => undef
+        }
+    );
+
+    is( $suggestion->manager, undef,
+        '->manager should have returned undef if no manager set' );
+    is( $suggestion->rejecter, undef,
+        '->rejecter should have returned undef if no rejecter set' );
+    is( $suggestion->suggester, undef,
+        '->suggester should have returned undef if no suggester set' );
+    is( $suggestion->last_modifier,
+        undef,
+        '->last_modifier should have returned undef if no last_modifier set' );
+
+    $schema->storage->txn_rollback;
+};
+
+subtest 'fund' => sub {
+    plan tests => 2;
+
+    $schema->storage->txn_begin;
+
+    my $suggestion = $builder->build_object( { class => 'Koha::Suggestions' } );
+    is( ref( $suggestion->fund ),
+        'Koha::Acquisition::Fund',
+        '->fund should have returned a Koha::Acquisition::Fund object' );
+
+    $suggestion->set( { budgetid => undef } );
+
+    is( $suggestion->fund, undef,
+        '->fund should have returned undef if not fund set' );
+
+    $schema->storage->txn_rollback;
+  } :
-- 
2.11.0