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