Bug 18887: Add Koha::CirculationRule(s)
[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::IssuingRules - Koha IssuingRule 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     my $search_params;
54     $search_params->{rule_name} = $rule_name;
55
56     $search_params->{categorycode} = defined $categorycode ? { 'in' => [ $categorycode, '*' ] } : undef;
57     $search_params->{itemtype}     = defined $itemtype     ? { 'in' => [ $itemtype,     '*' ] } : undef;
58     $search_params->{branchcode}   = defined $branchcode   ? { 'in' => [ $branchcode,   '*' ] } : undef;
59
60     my $rule = $self->search(
61         $search_params,
62         {
63             order_by => {
64                 -desc => [ 'branchcode', 'categorycode', 'itemtype' ]
65             },
66             rows => 1,
67         }
68     )->single;
69
70     return $rule;
71 }
72
73 =head3 set_rule
74
75 =cut
76
77 sub set_rule {
78     my ( $self, $params ) = @_;
79
80     croak q{set_rule requires the parameter 'branchcode'!}
81       unless exists $params->{branchcode};
82     croak q{set_rule requires the parameter 'categorycode'!}
83       unless exists $params->{categorycode};
84     croak q{set_rule requires the parameter 'itemtype'!}
85       unless exists $params->{itemtype};
86     croak q{set_rule requires the parameter 'rule_name'!}
87       unless exists $params->{rule_name};
88     croak q{set_rule requires the parameter 'rule_value'!}
89       unless exists $params->{rule_value};
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     my $rule = $self->search(
98         {
99             rule_name    => $rule_name,
100             branchcode   => $branchcode,
101             categorycode => $categorycode,
102             itemtype     => $itemtype,
103         }
104     )->next();
105
106     if ($rule) {
107         if ( defined $rule_value ) {
108             $rule->rule_value($rule_value);
109             $rule->update();
110         }
111         else {
112             $rule->delete();
113         }
114     }
115     else {
116         if ( defined $rule_value ) {
117             $rule = Koha::CirculationRule->new(
118                 {
119                     branchcode   => $branchcode,
120                     categorycode => $categorycode,
121                     itemtype     => $itemtype,
122                     rule_name    => $rule_name,
123                     rule_value   => $rule_value,
124                 }
125             );
126             $rule->store();
127         }
128     }
129
130     return $rule;
131 }
132
133 =head3 set_rules
134
135 =cut
136
137 sub set_rules {
138     my ( $self, $params ) = @_;
139     warn Data::Dumper::Dumper( $params );
140
141     my $branchcode   = $params->{branchcode};
142     my $categorycode = $params->{categorycode};
143     my $itemtype     = $params->{itemtype};
144     my $rules        = $params->{rules};
145
146     foreach my $rule (@$rules) {
147         Koha::CirculationRules->set_rule(
148             {
149                 branchcode   => $branchcode,
150                 categorycode => $categorycode,
151                 itemtype     => $itemtype,
152                 rule_name    => $rule->{rule_name},
153                 rule_value   => $rule->{rule_value},
154             }
155         );
156     }
157 }
158
159 =head3 type
160
161 =cut
162
163 sub _type {
164     return 'CirculationRule';
165 }
166
167 sub object_class {
168     return 'Koha::CirculationRule';
169 }
170
171 1;