Bug 16911: ->get_expiry_date accepts strings
[koha.git] / Koha / Patron / Category.pm
1 package Koha::Patron::Category;
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
22 use C4::Members::Messaging;
23
24 use Koha::Database;
25 use Koha::DateUtils;
26
27 use base qw(Koha::Object);
28
29 =head1 NAME
30
31 Koha::Patron;;Category - Koha Patron;;Category Object class
32
33 =head1 API
34
35 =head2 Class Methods
36
37 =cut
38
39 sub store {
40     my ( $self ) = @_;
41     $self->checkprevcheckout('inherit')
42         unless defined $self->checkprevcheckout;
43     return $self->SUPER::store;
44 }
45
46 =head3 default_messaging
47
48 my $messaging = $category->default_messaging();
49
50 =cut
51
52 sub default_messaging {
53     my ( $self ) = @_;
54     my $messaging_options = C4::Members::Messaging::GetMessagingOptions();
55     my @messaging;
56     foreach my $option (@$messaging_options) {
57         my $pref = C4::Members::Messaging::GetMessagingPreferences(
58             {
59                 categorycode => $self->categorycode,
60                 message_name => $option->{message_name}
61             }
62         );
63         next unless $pref->{transports};
64         my $brief_pref = {
65             message_attribute_id      => $option->{message_attribute_id},
66             message_name              => $option->{message_name},
67             $option->{'message_name'} => 1,
68         };
69         foreach my $transport ( keys %{ $pref->{transports} } ) {
70             push @{ $brief_pref->{transports} }, { transport => $transport };
71         }
72         push @messaging, $brief_pref;
73     }
74     return \@messaging;
75 }
76
77 =head3 branch_limitations
78
79 my $limitations = $category->branch_limitations();
80
81 $category->branch_limitations( \@branchcodes );
82
83 =cut
84
85 sub branch_limitations {
86     my ( $self, $branchcodes ) = @_;
87
88     if ($branchcodes) {
89         return $self->replace_branch_limitations($branchcodes);
90     }
91     else {
92         return $self->get_branch_limitations();
93     }
94
95 }
96
97 =head3 get_branch_limitations
98
99 my $limitations = $category->get_branch_limitations();
100
101 =cut
102
103 sub get_branch_limitations {
104     my ($self) = @_;
105
106     my @branchcodes =
107       $self->_catb_resultset->search( { categorycode => $self->categorycode } )
108       ->get_column('branchcode')->all();
109
110     return \@branchcodes;
111 }
112
113 =head3 add_branch_limitation
114
115 $category->add_branch_limitation( $branchcode );
116
117 =cut
118
119 sub add_branch_limitation {
120     my ( $self, $branchcode ) = @_;
121
122     croak("No branchcode passed in!") unless $branchcode;
123
124     my $limitation = $self->_catb_resultset->update_or_create(
125         { categorycode => $self->categorycode, branchcode => $branchcode } );
126
127     return $limitation ? 1 : undef;
128 }
129
130 =head3 del_branch_limitation
131
132 $category->del_branch_limitation( $branchcode );
133
134 =cut
135
136 sub del_branch_limitation {
137     my ( $self, $branchcode ) = @_;
138
139     croak("No branchcode passed in!") unless $branchcode;
140
141     my $limitation =
142       $self->_catb_resultset->find(
143         { categorycode => $self->categorycode, branchcode => $branchcode } );
144
145     unless ($limitation) {
146         my $categorycode = $self->categorycode;
147         carp(
148 "No branch limit for branch $branchcode found for categorycode $categorycode to delete!"
149         );
150         return;
151     }
152
153     return $limitation->delete();
154 }
155
156 =head3 replace_branch_limitations
157
158 $category->replace_branch_limitations( \@branchcodes );
159
160 =cut
161
162 sub replace_branch_limitations {
163     my ( $self, $branchcodes ) = @_;
164
165     $self->_catb_resultset->search( { categorycode => $self->categorycode } )->delete;
166
167     my @return_values =
168       map { $self->add_branch_limitation($_) } @$branchcodes;
169
170     return \@return_values;
171 }
172
173 =head3 Koha::Objects->_catb_resultset
174
175 Returns the internal resultset or creates it if undefined
176
177 =cut
178
179 sub _catb_resultset {
180     my ($self) = @_;
181
182     $self->{_catb_resultset} ||=
183       Koha::Database->new->schema->resultset('CategoriesBranch');
184
185     return $self->{_catb_resultset};
186 }
187
188 sub get_expiry_date {
189     my ($self, $date ) = @_;
190     if ( $self->enrolmentperiod ) {
191         $date ||= dt_from_string;
192         $date = dt_from_string( $date ) unless ref $date;
193         return $date->add( months => $self->enrolmentperiod );
194     } else {
195         return $self->enrolmentperioddate;
196     }
197 }
198
199 =head3 type
200
201 =cut
202
203 sub _type {
204     return 'Category';
205 }
206
207 1;