1 package Koha::Patron::Category;
3 # This file is part of Koha.
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
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.
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.
22 use C4::Members::Messaging;
27 use base qw(Koha::Object);
31 Koha::Patron;;Category - Koha Patron;;Category Object class
39 =head3 effective_BlockExpiredPatronOpacActions
41 my $BlockExpiredPatronOpacActions = $category->effective_BlockExpiredPatronOpacActions
43 Return the effective BlockExpiredPatronOpacActions value.
47 sub effective_BlockExpiredPatronOpacActions {
49 return C4::Context->preference('BlockExpiredPatronOpacActions') if $self->BlockExpiredPatronOpacActions == -1;
50 return $self->BlockExpiredPatronOpacActions
53 =head3 default_messaging
55 my $messaging = $category->default_messaging();
59 sub default_messaging {
61 my $messaging_options = C4::Members::Messaging::GetMessagingOptions();
63 foreach my $option (@$messaging_options) {
64 my $pref = C4::Members::Messaging::GetMessagingPreferences(
66 categorycode => $self->categorycode,
67 message_name => $option->{message_name}
70 next unless $pref->{transports};
72 message_attribute_id => $option->{message_attribute_id},
73 message_name => $option->{message_name},
74 $option->{'message_name'} => 1,
76 foreach my $transport ( keys %{ $pref->{transports} } ) {
77 push @{ $brief_pref->{transports} }, { transport => $transport };
79 push @messaging, $brief_pref;
84 =head3 branch_limitations
86 my $limitations = $category->branch_limitations();
88 $category->branch_limitations( \@branchcodes );
92 sub branch_limitations {
93 my ( $self, $branchcodes ) = @_;
96 return $self->replace_branch_limitations($branchcodes);
99 return $self->get_branch_limitations();
104 =head3 get_branch_limitations
106 my $limitations = $category->get_branch_limitations();
110 sub get_branch_limitations {
114 $self->_catb_resultset->search( { categorycode => $self->categorycode } )
115 ->get_column('branchcode')->all();
117 return \@branchcodes;
120 =head3 add_branch_limitation
122 $category->add_branch_limitation( $branchcode );
126 sub add_branch_limitation {
127 my ( $self, $branchcode ) = @_;
129 croak("No branchcode passed in!") unless $branchcode;
131 my $limitation = $self->_catb_resultset->update_or_create(
132 { categorycode => $self->categorycode, branchcode => $branchcode } );
134 return $limitation ? 1 : undef;
137 =head3 del_branch_limitation
139 $category->del_branch_limitation( $branchcode );
143 sub del_branch_limitation {
144 my ( $self, $branchcode ) = @_;
146 croak("No branchcode passed in!") unless $branchcode;
149 $self->_catb_resultset->find(
150 { categorycode => $self->categorycode, branchcode => $branchcode } );
152 unless ($limitation) {
153 my $categorycode = $self->categorycode;
155 "No branch limit for branch $branchcode found for categorycode $categorycode to delete!"
160 return $limitation->delete();
163 =head3 replace_branch_limitations
165 $category->replace_branch_limitations( \@branchcodes );
169 sub replace_branch_limitations {
170 my ( $self, $branchcodes ) = @_;
172 $self->_catb_resultset->search( { categorycode => $self->categorycode } )->delete;
175 map { $self->add_branch_limitation($_) } @$branchcodes;
177 return \@return_values;
180 =head3 Koha::Objects->_catb_resultset
182 Returns the internal resultset or creates it if undefined
186 sub _catb_resultset {
189 $self->{_catb_resultset} ||=
190 Koha::Database->new->schema->resultset('CategoriesBranch');
192 return $self->{_catb_resultset};
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 );
202 return $self->enrolmentperioddate;