0cdfa9fb0949ebe74b8f14fe125ced91917f58c2
[koha.git] / Koha / Patron / Relationships.pm
1 package Koha::Patron::Relationships;
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 3 of the License, or (at your option) any later
8 # version.
9 #
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18 use Modern::Perl;
19
20 use Carp;
21 use List::MoreUtils qw( uniq );
22
23 use Koha::Database;
24 use Koha::Patrons;
25 use Koha::Patron::Relationship;
26
27 use base qw(Koha::Objects);
28
29 =head1 NAME
30
31 Koha::Patron::Relationships - Koha Patron Relationship Object set class
32
33 =head1 API
34
35 =head2 Class Methods
36
37 =cut
38
39 =head3 guarantors
40
41 Returns all the guarantors in this set of relationships as a list of Koha::Patron objects
42 or as a Koha::Patrons object depending on the calling context
43
44 =cut
45
46 sub guarantors {
47     my ($self) = @_;
48
49     my $rs = $self->_resultset();
50
51     my @guarantor_ids = $rs->get_column('guarantor_id')->all();
52     # Guarantors may not have a guarantor_id, strip out undefs
53     @guarantor_ids = grep { defined $_ } @guarantor_ids;
54     @guarantor_ids = uniq( @guarantor_ids );
55
56     my $guarantors = Koha::Patrons->search( { borrowernumber => \@guarantor_ids } );
57
58     return wantarray ? $guarantors->as_list : $guarantors;
59 }
60
61 =head3 guarantees
62
63 Returns all the guarantees in this set of relationships as a list of Koha::Patron objects
64 or as a Koha::Patrons object depending on the calling context
65
66 =cut
67
68 sub guarantees {
69     my ($self) = @_;
70
71     my $rs = $self->_resultset();
72
73     my @guarantee_ids = uniq( $rs->get_column('guarantee_id')->all() );
74
75     my $guarantees = Koha::Patrons->search(
76         { borrowernumber => \@guarantee_ids },
77         {
78             order_by => { -asc => [ 'surname', 'firstname' ] }
79         },
80     );
81
82     return wantarray ? $guarantees->as_list : $guarantees;
83 }
84
85 =cut
86
87 =head3 type
88
89 =cut
90
91 sub _type {
92     return 'BorrowerRelationship';
93 }
94
95 sub object_class {
96     return 'Koha::Patron::Relationship';
97 }
98
99 1;