Bug 17301 - Add callnumber to label-edit-batch.pl
[koha.git] / Koha / Patron / Category.pm
blob331a1ce2cb499941788fdefb9b3803d217b433fe
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
8 # version.
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.
18 use Modern::Perl;
20 use Carp;
22 use C4::Members::Messaging;
24 use Koha::Database;
25 use Koha::DateUtils;
27 use base qw(Koha::Object);
29 =head1 NAME
31 Koha::Patron;;Category - Koha Patron;;Category Object class
33 =head1 API
35 =head2 Class Methods
37 =cut
39 sub store {
40 my ( $self ) = @_;
41 $self->checkprevcheckout('inherit')
42 unless defined $self->checkprevcheckout;
43 return $self->SUPER::store;
46 =head3 default_messaging
48 my $messaging = $category->default_messaging();
50 =cut
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(
59 categorycode => $self->categorycode,
60 message_name => $option->{message_name}
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,
69 foreach my $transport ( keys %{ $pref->{transports} } ) {
70 push @{ $brief_pref->{transports} }, { transport => $transport };
72 push @messaging, $brief_pref;
74 return \@messaging;
77 =head3 branch_limitations
79 my $limitations = $category->branch_limitations();
81 $category->branch_limitations( \@branchcodes );
83 =cut
85 sub branch_limitations {
86 my ( $self, $branchcodes ) = @_;
88 if ($branchcodes) {
89 return $self->replace_branch_limitations($branchcodes);
91 else {
92 return $self->get_branch_limitations();
97 =head3 get_branch_limitations
99 my $limitations = $category->get_branch_limitations();
101 =cut
103 sub get_branch_limitations {
104 my ($self) = @_;
106 my @branchcodes =
107 $self->_catb_resultset->search( { categorycode => $self->categorycode } )
108 ->get_column('branchcode')->all();
110 return \@branchcodes;
113 =head3 add_branch_limitation
115 $category->add_branch_limitation( $branchcode );
117 =cut
119 sub add_branch_limitation {
120 my ( $self, $branchcode ) = @_;
122 croak("No branchcode passed in!") unless $branchcode;
124 my $limitation = $self->_catb_resultset->update_or_create(
125 { categorycode => $self->categorycode, branchcode => $branchcode } );
127 return $limitation ? 1 : undef;
130 =head3 del_branch_limitation
132 $category->del_branch_limitation( $branchcode );
134 =cut
136 sub del_branch_limitation {
137 my ( $self, $branchcode ) = @_;
139 croak("No branchcode passed in!") unless $branchcode;
141 my $limitation =
142 $self->_catb_resultset->find(
143 { categorycode => $self->categorycode, branchcode => $branchcode } );
145 unless ($limitation) {
146 my $categorycode = $self->categorycode;
147 carp(
148 "No branch limit for branch $branchcode found for categorycode $categorycode to delete!"
150 return;
153 return $limitation->delete();
156 =head3 replace_branch_limitations
158 $category->replace_branch_limitations( \@branchcodes );
160 =cut
162 sub replace_branch_limitations {
163 my ( $self, $branchcodes ) = @_;
165 $self->_catb_resultset->search( { categorycode => $self->categorycode } )->delete;
167 my @return_values =
168 map { $self->add_branch_limitation($_) } @$branchcodes;
170 return \@return_values;
173 =head3 Koha::Objects->_catb_resultset
175 Returns the internal resultset or creates it if undefined
177 =cut
179 sub _catb_resultset {
180 my ($self) = @_;
182 $self->{_catb_resultset} ||=
183 Koha::Database->new->schema->resultset('CategoriesBranch');
185 return $self->{_catb_resultset};
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;
199 =head3 type
201 =cut
203 sub _type {
204 return 'Category';