Bug 29786: Unit tests
[koha.git] / Koha / Clubs.pm
1 package Koha::Clubs;
2
3 # Copyright ByWater Solutions 2014
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21
22 use Carp;
23
24 use Koha::Database;
25 use Koha::DateUtils qw( dt_from_string );
26
27 use Koha::Club;
28
29 use base qw(Koha::Objects);
30
31 =head1 NAME
32
33 Koha::Clubs - Koha Clubs Object class
34
35 This object represents a collection of clubs a patron may enroll in.
36
37 =head1 API
38
39 =head2 Class Methods
40
41 =cut
42
43 =head3 get_enrollable
44
45 =cut
46
47 sub get_enrollable {
48     my ( $self, $params ) = @_;
49
50     # We need to filter out all the already enrolled in clubs
51     my $borrower = $params->{borrower};
52     if ($borrower) {
53         delete( $params->{borrower} );
54         my @enrollments = $borrower->get_club_enrollments();
55         if (@enrollments) {
56             $params->{'me.id'} = { -not_in => [ map { $_->club()->id() } @enrollments ] };
57         }
58     }
59
60     my $dtf = Koha::Database->new->schema->storage->datetime_parser;
61
62     # Only clubs with no end date or an end date in the future can be enrolled in
63     $params->{'-and'} = [
64         -or => [
65             date_end => { '>=' => $dtf->format_datetime( dt_from_string() ) },
66             date_end => undef,
67         ],
68         -or => [
69             'me.branchcode' => $borrower->branchcode,
70             'me.branchcode' => undef,
71         ]
72     ];
73
74     my $rs = $self->_resultset()->search( $params, { prefetch => 'club_template' } );
75
76     if (wantarray) {
77         my $class = ref($self) ? ref($self) : $self;
78
79         return $class->_wrap( $rs->all() );
80
81     }
82     else {
83         my $class = ref($self) ? ref($self) : $self;
84
85         return $class->_new_from_dbic($rs);
86     }
87 }
88
89 =head3 filter_out_empty
90
91     my $filtered_rs = $clubs_rs->filter_out_empty;
92
93 Return a new I<Koha::Clubs> resultset, containing only clubs with current enrollments.
94
95 =cut
96
97 sub filter_out_empty {
98     my ($self) = @_;
99     return $self->search(
100         {
101             -and => [
102                 { 'club_enrollments.club_id'       => { '!=' => undef } },
103                 { 'club_enrollments.date_canceled' => undef },
104             ]
105         },
106         {
107             join     => 'club_enrollments',
108             distinct => 1,
109         }
110     );
111 }
112
113 =head3 type
114
115 =cut
116
117 sub _type {
118     return 'Club';
119 }
120
121 sub object_class {
122     return 'Koha::Club';
123 }
124
125 =head1 AUTHOR
126
127 Kyle M Hall <kyle@bywatersolutions.com>
128
129 =cut
130
131 1;