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