Bug 26265: (QA follow-up) Remove g option from regex, add few dirs
[koha-equinox.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 Koha::Exceptions;
24 use Koha::CirculationRule;
25
26 use base qw(Koha::Objects);
27
28 =head1 NAME
29
30 Koha::CirculationRules - Koha CirculationRule Object set class
31
32 =head1 API
33
34 =head2 Class Methods
35
36 =cut
37
38 =head3 get_effective_rule
39
40 =cut
41
42 sub get_effective_rule {
43     my ( $self, $params ) = @_;
44
45     my $rule_name    = $params->{rule_name};
46     my $categorycode = $params->{categorycode};
47     my $itemtype     = $params->{itemtype};
48     my $branchcode   = $params->{branchcode};
49
50     Koha::Exceptions::MissingParameter->throw(
51         "Required parameter 'rule_name' missing")
52       unless $rule_name;
53
54     for my $v ( $branchcode, $categorycode, $itemtype ) {
55         $v = undef if $v and $v eq '*';
56     }
57
58     my $search_params;
59     $search_params->{rule_name} = $rule_name;
60
61     $search_params->{categorycode} = defined $categorycode ? [ $categorycode, undef ] : undef;
62     $search_params->{itemtype}     = defined $itemtype     ? [ $itemtype, undef ] : undef;
63     $search_params->{branchcode}   = defined $branchcode   ? [ $branchcode,   undef ] : undef;
64
65     my $rule = $self->search(
66         $search_params,
67         {
68             order_by => {
69                 -desc => [ 'branchcode', 'categorycode', 'itemtype' ]
70             },
71             rows => 1,
72         }
73     )->single;
74
75     return $rule;
76 }
77
78 =head3 set_rule
79
80 =cut
81
82 sub set_rule {
83     my ( $self, $params ) = @_;
84
85     for my $mandatory_parameter (qw( branchcode categorycode itemtype rule_name rule_value ) ){
86         Koha::Exceptions::MissingParameter->throw(
87             "Required parameter 'branchcode' missing")
88           unless exists $params->{$mandatory_parameter};
89     }
90
91     my $branchcode   = $params->{branchcode};
92     my $categorycode = $params->{categorycode};
93     my $itemtype     = $params->{itemtype};
94     my $rule_name    = $params->{rule_name};
95     my $rule_value   = $params->{rule_value};
96
97     for my $v ( $branchcode, $categorycode, $itemtype ) {
98         $v = undef if $v and $v eq '*';
99     }
100     my $rule = $self->search(
101         {
102             rule_name    => $rule_name,
103             branchcode   => $branchcode,
104             categorycode => $categorycode,
105             itemtype     => $itemtype,
106         }
107     )->next();
108
109     if ($rule) {
110         if ( defined $rule_value ) {
111             $rule->rule_value($rule_value);
112             $rule->update();
113         }
114         else {
115             $rule->delete();
116         }
117     }
118     else {
119         if ( defined $rule_value ) {
120             $rule = Koha::CirculationRule->new(
121                 {
122                     branchcode   => $branchcode,
123                     categorycode => $categorycode,
124                     itemtype     => $itemtype,
125                     rule_name    => $rule_name,
126                     rule_value   => $rule_value,
127                 }
128             );
129             $rule->store();
130         }
131     }
132
133     return $rule;
134 }
135
136 =head3 set_rules
137
138 =cut
139
140 sub set_rules {
141     my ( $self, $params ) = @_;
142
143     my $branchcode   = $params->{branchcode};
144     my $categorycode = $params->{categorycode};
145     my $itemtype     = $params->{itemtype};
146     my $rules        = $params->{rules};
147
148     my $rule_objects = [];
149     while ( my ( $rule_name, $rule_value ) = each %$rules ) {
150         my $rule_object = Koha::CirculationRules->set_rule(
151             {
152                 branchcode   => $branchcode,
153                 categorycode => $categorycode,
154                 itemtype     => $itemtype,
155                 rule_name    => $rule_name,
156                 rule_value   => $rule_value,
157             }
158         );
159         push( @$rule_objects, $rule_object );
160     }
161
162     return $rule_objects;
163 }
164
165 =head3 delete
166
167 Delete a set of circulation rules, needed for cleaning up when deleting issuingrules
168
169 =cut
170
171 sub delete {
172     my ( $self ) = @_;
173
174     while ( my $rule = $self->next ){
175         $rule->delete;
176     }
177
178 }
179
180 =head3 type
181
182 =cut
183
184 sub _type {
185     return 'CirculationRule';
186 }
187
188 =head3 object_class
189
190 =cut
191
192 sub object_class {
193     return 'Koha::CirculationRule';
194 }
195
196 1;