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 default_messaging
41 my $messaging = $category->default_messaging();
45 sub default_messaging {
47 my $messaging_options = C4::Members::Messaging::GetMessagingOptions();
49 foreach my $option (@$messaging_options) {
50 my $pref = C4::Members::Messaging::GetMessagingPreferences(
52 categorycode => $self->categorycode,
53 message_name => $option->{message_name}
56 next unless $pref->{transports};
58 message_attribute_id => $option->{message_attribute_id},
59 message_name => $option->{message_name},
60 $option->{'message_name'} => 1,
62 foreach my $transport ( keys %{ $pref->{transports} } ) {
63 push @{ $brief_pref->{transports} }, { transport => $transport };
65 push @messaging, $brief_pref;
70 =head3 branch_limitations
72 my $limitations = $category->branch_limitations();
74 $category->branch_limitations( \@branchcodes );
78 sub branch_limitations {
79 my ( $self, $branchcodes ) = @_;
82 return $self->replace_branch_limitations($branchcodes);
85 return $self->get_branch_limitations();
90 =head3 get_branch_limitations
92 my $limitations = $category->get_branch_limitations();
96 sub get_branch_limitations {
100 $self->_catb_resultset->search( { categorycode => $self->categorycode } )
101 ->get_column('branchcode')->all();
103 return \@branchcodes;
106 =head3 add_branch_limitation
108 $category->add_branch_limitation( $branchcode );
112 sub add_branch_limitation {
113 my ( $self, $branchcode ) = @_;
115 croak("No branchcode passed in!") unless $branchcode;
117 my $limitation = $self->_catb_resultset->update_or_create(
118 { categorycode => $self->categorycode, branchcode => $branchcode } );
120 return $limitation ? 1 : undef;
123 =head3 del_branch_limitation
125 $category->del_branch_limitation( $branchcode );
129 sub del_branch_limitation {
130 my ( $self, $branchcode ) = @_;
132 croak("No branchcode passed in!") unless $branchcode;
135 $self->_catb_resultset->find(
136 { categorycode => $self->categorycode, branchcode => $branchcode } );
138 unless ($limitation) {
139 my $categorycode = $self->categorycode;
141 "No branch limit for branch $branchcode found for categorycode $categorycode to delete!"
146 return $limitation->delete();
149 =head3 replace_branch_limitations
151 $category->replace_branch_limitations( \@branchcodes );
155 sub replace_branch_limitations {
156 my ( $self, $branchcodes ) = @_;
158 $self->_catb_resultset->search( { categorycode => $self->categorycode } )->delete;
161 map { $self->add_branch_limitation($_) } @$branchcodes;
163 return \@return_values;
166 =head3 Koha::Objects->_catb_resultset
168 Returns the internal resultset or creates it if undefined
172 sub _catb_resultset {
175 $self->{_catb_resultset} ||=
176 Koha::Database->new->schema->resultset('CategoriesBranch');
178 return $self->{_catb_resultset};
181 sub get_expiry_date {
182 my ($self, $date ) = @_;
183 if ( $self->enrolmentperiod ) {
184 $date ||= dt_from_string;
185 $date = dt_from_string( $date ) unless ref $date;
186 return $date->add( months => $self->enrolmentperiod );
188 return $self->enrolmentperioddate;