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