Bug 21606: [sql_modes] Fix matching rules insert
[koha.git] / Koha / CirculationRules.pm
blobc800673fcfa649da4b87206817e38392d8ac3b8d
1 package Koha::CirculationRules;
3 # Copyright Vaara-kirjastot 2015
4 # Copyright Koha Development Team 2016
6 # This file is part of Koha.
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.
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.
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.
21 use Modern::Perl;
23 use Koha::Exceptions;
24 use Koha::CirculationRule;
26 use base qw(Koha::Objects);
28 =head1 NAME
30 Koha::CirculationRules - Koha CirculationRule Object set class
32 =head1 API
34 =head2 Class Methods
36 =cut
38 =head3 get_effective_rule
40 =cut
42 sub get_effective_rule {
43 my ( $self, $params ) = @_;
45 my $rule_name = $params->{rule_name};
46 my $categorycode = $params->{categorycode};
47 my $itemtype = $params->{itemtype};
48 my $branchcode = $params->{branchcode};
50 Koha::Exceptions::MissingParameter->throw(
51 "Required parameter 'rule_name' missing")
52 unless $rule_name;
54 for my $v ( $branchcode, $categorycode, $itemtype ) {
55 $v = undef if $v and $v eq '*';
58 my $search_params;
59 $search_params->{rule_name} = $rule_name;
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;
65 my $rule = $self->search(
66 $search_params,
68 order_by => {
69 -desc => [ 'branchcode', 'categorycode', 'itemtype' ]
71 rows => 1,
73 )->single;
75 return $rule;
78 =head3 set_rule
80 =cut
82 sub set_rule {
83 my ( $self, $params ) = @_;
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};
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};
97 for my $v ( $branchcode, $categorycode, $itemtype ) {
98 $v = undef if $v and $v eq '*';
100 my $rule = $self->search(
102 rule_name => $rule_name,
103 branchcode => $branchcode,
104 categorycode => $categorycode,
105 itemtype => $itemtype,
107 )->next();
109 if ($rule) {
110 if ( defined $rule_value ) {
111 $rule->rule_value($rule_value);
112 $rule->update();
114 else {
115 $rule->delete();
118 else {
119 if ( defined $rule_value ) {
120 $rule = Koha::CirculationRule->new(
122 branchcode => $branchcode,
123 categorycode => $categorycode,
124 itemtype => $itemtype,
125 rule_name => $rule_name,
126 rule_value => $rule_value,
129 $rule->store();
133 return $rule;
136 =head3 set_rules
138 =cut
140 sub set_rules {
141 my ( $self, $params ) = @_;
143 my $branchcode = $params->{branchcode};
144 my $categorycode = $params->{categorycode};
145 my $itemtype = $params->{itemtype};
146 my $rules = $params->{rules};
148 foreach my $rule (@$rules) {
149 Koha::CirculationRules->set_rule(
151 branchcode => $branchcode,
152 categorycode => $categorycode,
153 itemtype => $itemtype,
154 rule_name => $rule->{rule_name},
155 rule_value => $rule->{rule_value},
161 =head3 type
163 =cut
165 sub _type {
166 return 'CirculationRule';
169 =head3 object_class
171 =cut
173 sub object_class {
174 return 'Koha::CirculationRule';