Bug 23677: Schema fix
[koha.git] / Koha / CirculationRules.pm
blob1294eebde14c568f90312bf7e1a2f4b00651cc2a
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 my $rule_objects = [];
149 while ( my ( $rule_name, $rule_value ) = each %$rules ) {
150 my $rule_object = Koha::CirculationRules->set_rule(
152 branchcode => $branchcode,
153 categorycode => $categorycode,
154 itemtype => $itemtype,
155 rule_name => $rule_name,
156 rule_value => $rule_value,
159 push( @$rule_objects, $rule_object );
162 return $rule_objects;
165 =head3 delete
167 Delete a set of circulation rules, needed for cleaning up when deleting issuingrules
169 =cut
171 sub delete {
172 my ( $self ) = @_;
174 while ( my $rule = $self->next ){
175 $rule->delete;
180 =head3 type
182 =cut
184 sub _type {
185 return 'CirculationRule';
188 =head3 object_class
190 =cut
192 sub object_class {
193 return 'Koha::CirculationRule';