Bug 30600: Update DBIC schema
[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
23 use Koha::Database;
24 use Koha::DateUtils qw( dt_from_string );
25
26 use Koha::Club;
27
28 use base qw(Koha::Objects);
29
30 =head1 NAME
31
32 Koha::Clubs - Koha Clubs Object class
33
34 This object represents a collection of clubs a patron may enroll in.
35
36 =head1 API
37
38 =head2 Class Methods
39
40 =cut
41
42 =head3 get_enrollable
43
44 =cut
45
46 sub get_enrollable {
47     my ( $self, $params ) = @_;
48
49     # We need to filter out all the already enrolled in clubs
50     my $borrower = $params->{borrower};
51     if ($borrower) {
52         delete( $params->{borrower} );
53         my @enrollments = $borrower->get_club_enrollments->as_list;
54         if (@enrollments) {
55             $params->{'me.id'} = { -not_in => [ map { $_->club()->id() } @enrollments ] };
56         }
57     }
58
59     my $dtf = Koha::Database->new->schema->storage->datetime_parser;
60
61     # Only clubs with no end date or an end date in the future can be enrolled in
62     $params->{'-and'} = [
63         -or => [
64             date_end => { '>=' => $dtf->format_datetime( dt_from_string() ) },
65             date_end => undef,
66         ],
67         -or => [
68             'me.branchcode' => $borrower->branchcode,
69             'me.branchcode' => undef,
70         ]
71     ];
72
73     return $self->search( $params, { prefetch => 'club_template' } );
74 }
75
76 =head3 filter_out_empty
77
78     my $filtered_rs = $clubs_rs->filter_out_empty;
79
80 Return a new I<Koha::Clubs> resultset, containing only clubs with current enrollments.
81
82 =cut
83
84 sub filter_out_empty {
85     my ($self) = @_;
86     return $self->search(
87         {
88             -and => [
89                 { 'club_enrollments.club_id'       => { '!=' => undef } },
90                 { 'club_enrollments.date_canceled' => undef },
91             ]
92         },
93         {
94             join     => 'club_enrollments',
95             distinct => 1,
96         }
97     );
98 }
99
100 =head3 type
101
102 =cut
103
104 sub _type {
105     return 'Club';
106 }
107
108 sub object_class {
109     return 'Koha::Club';
110 }
111
112 =head1 AUTHOR
113
114 Kyle M Hall <kyle@bywatersolutions.com>
115
116 =cut
117
118 1;