Bug 17783: Optimize Koha::IssuingRules->get_effective_issuing_rule
[koha.git] / Koha / IssuingRules.pm
1 package Koha::IssuingRules;
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::Database;
24
25 use Koha::IssuingRule;
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 sub get_effective_issuing_rule {
40     my ( $self, $params ) = @_;
41
42     my $default      = '*';
43     my $categorycode = $params->{categorycode};
44     my $itemtype     = $params->{itemtype};
45     my $branchcode   = $params->{branchcode};
46
47     my $rule = $self->search({
48         categorycode => { 'in' => [ $categorycode, $default ] },
49         itemtype     => { 'in' => [ $itemtype,     $default ] },
50         branchcode   => { 'in' => [ $branchcode,   $default ] },
51     }, {
52         order_by => {
53             -desc => ['branchcode', 'categorycode', 'itemtype']
54         },
55         rows => 1,
56     })->single;
57     return $rule;
58 }
59
60 =head3 type
61
62 =cut
63
64 sub _type {
65     return 'Issuingrule';
66 }
67
68 sub object_class {
69     return 'Koha::IssuingRule';
70 }
71
72 1;