Bug 18887: Fix POD c/p issues
[koha.git] / Koha / CirculationRules.pm
1 package Koha::CirculationRules;
2
3 # Copyright Vaara-kirjastot 2015
4 # Copyright Koha Development Team 2016
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 3 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
21 use Modern::Perl;
22
23 use Carp qw(croak);
24
25 use Koha::CirculationRule;
26
27 use base qw(Koha::Objects);
28
29 =head1 NAME
30
31 Koha::CirculationRules - Koha CirculationRule Object set class
32
33 =head1 API
34
35 =head2 Class Methods
36
37 =cut
38
39 =head3 get_effective_rule
40
41 =cut
42
43 sub get_effective_rule {
44     my ( $self, $params ) = @_;
45
46     my $rule_name    = $params->{rule_name};
47     my $categorycode = $params->{categorycode};
48     my $itemtype     = $params->{itemtype};
49     my $branchcode   = $params->{branchcode};
50
51     croak q{No rule name passed in!} unless $rule_name;
52
53     for my $v ( $branchcode, $categorycode, $itemtype ) {
54         $v = undef if $v and $v eq '*';
55     }
56
57     my $search_params;
58     $search_params->{rule_name} = $rule_name;
59
60     $search_params->{categorycode} = defined $categorycode ? [ $categorycode, undef ] : undef;
61     $search_params->{itemtype}     = defined $itemtype     ? [ $itemtype, undef ] : undef;
62     $search_params->{branchcode}   = defined $branchcode   ? [ $branchcode,   undef ] : undef;
63
64     my $rule = $self->search(
65         $search_params,
66         {
67             order_by => {
68                 -desc => [ 'branchcode', 'categorycode', 'itemtype' ]
69             },
70             rows => 1,
71         }
72     )->single;
73
74     return $rule;
75 }
76
77 =head3 set_rule
78
79 =cut
80
81 sub set_rule {
82     my ( $self, $params ) = @_;
83
84     croak q{set_rule requires the parameter 'branchcode'!}
85       unless exists $params->{branchcode};
86     croak q{set_rule requires the parameter 'categorycode'!}
87       unless exists $params->{categorycode};
88     croak q{set_rule requires the parameter 'itemtype'!}
89       unless exists $params->{itemtype};
90     croak q{set_rule requires the parameter 'rule_name'!}
91       unless exists $params->{rule_name};
92     croak q{set_rule requires the parameter 'rule_value'!}
93       unless exists $params->{rule_value};
94
95     my $branchcode   = $params->{branchcode};
96     my $categorycode = $params->{categorycode};
97     my $itemtype     = $params->{itemtype};
98     my $rule_name    = $params->{rule_name};
99     my $rule_value   = $params->{rule_value};
100
101     for my $v ( $branchcode, $categorycode, $itemtype ) {
102         $v = undef if $v and $v eq '*';
103     }
104     my $rule = $self->search(
105         {
106             rule_name    => $rule_name,
107             branchcode   => $branchcode,
108             categorycode => $categorycode,
109             itemtype     => $itemtype,
110         }
111     )->next();
112
113     if ($rule) {
114         if ( defined $rule_value ) {
115             $rule->rule_value($rule_value);
116             $rule->update();
117         }
118         else {
119             $rule->delete();
120         }
121     }
122     else {
123         if ( defined $rule_value ) {
124             $rule = Koha::CirculationRule->new(
125                 {
126                     branchcode   => $branchcode,
127                     categorycode => $categorycode,
128                     itemtype     => $itemtype,
129                     rule_name    => $rule_name,
130                     rule_value   => $rule_value,
131                 }
132             );
133             $rule->store();
134         }
135     }
136
137     return $rule;
138 }
139
140 =head3 set_rules
141
142 =cut
143
144 sub set_rules {
145     my ( $self, $params ) = @_;
146
147     my $branchcode   = $params->{branchcode};
148     my $categorycode = $params->{categorycode};
149     my $itemtype     = $params->{itemtype};
150     my $rules        = $params->{rules};
151
152     foreach my $rule (@$rules) {
153         Koha::CirculationRules->set_rule(
154             {
155                 branchcode   => $branchcode,
156                 categorycode => $categorycode,
157                 itemtype     => $itemtype,
158                 rule_name    => $rule->{rule_name},
159                 rule_value   => $rule->{rule_value},
160             }
161         );
162     }
163 }
164
165 =head3 type
166
167 =cut
168
169 sub _type {
170     return 'CirculationRule';
171 }
172
173 =head3 object_class
174
175 =cut
176
177 sub object_class {
178     return 'Koha::CirculationRule';
179 }
180
181 1;