Bug 23425: (QA follow-up) Keep it generic
[koha.git] / Koha / IssuingRules.pm
blob608b4edeedb0056c3c6c00182d9ce72c2aa2db18
1 package Koha::IssuingRules;
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::Database;
24 use Koha::Caches;
26 use Koha::IssuingRule;
28 use base qw(Koha::Objects);
30 use constant GUESSED_ITEMTYPES_KEY => 'Koha_IssuingRules_last_guess';
32 =head1 NAME
34 Koha::IssuingRules - Koha IssuingRule Object set class
36 =head1 API
38 =head2 Class Methods
40 =cut
42 sub get_effective_issuing_rule {
43 my ( $self, $params ) = @_;
45 my $default = '*';
46 my $categorycode = $params->{categorycode};
47 my $itemtype = $params->{itemtype};
48 my $branchcode = $params->{branchcode};
50 my $search_categorycode = $default;
51 my $search_itemtype = $default;
52 my $search_branchcode = $default;
54 if ($categorycode) {
55 $search_categorycode = { 'in' => [ $categorycode, $default ] };
57 if ($itemtype) {
58 $search_itemtype = { 'in' => [ $itemtype, $default ] };
60 if ($branchcode) {
61 $search_branchcode = { 'in' => [ $branchcode, $default ] };
64 my $rule = $self->search({
65 categorycode => $search_categorycode,
66 itemtype => $search_itemtype,
67 branchcode => $search_branchcode,
68 }, {
69 order_by => {
70 -desc => ['branchcode', 'categorycode', 'itemtype']
72 rows => 1,
73 })->single;
74 return $rule;
77 =head3 get_opacitemholds_policy
79 my $can_place_a_hold_at_item_level = Koha::IssuingRules->get_opacitemholds_policy( { patron => $patron, item => $item } );
81 Return 'Y' or 'F' if the patron can place a hold on this item according to the issuing rules
82 and the "Item level holds" (opacitemholds).
83 Can be 'N' - Don't allow, 'Y' - Allow, and 'F' - Force
85 =cut
87 sub get_opacitemholds_policy {
88 my ( $class, $params ) = @_;
90 my $item = $params->{item};
91 my $patron = $params->{patron};
93 return unless $item or $patron;
95 my $issuing_rule = Koha::IssuingRules->get_effective_issuing_rule(
97 categorycode => $patron->categorycode,
98 itemtype => $item->effective_itemtype,
99 branchcode => $item->homebranch,
103 return $issuing_rule ? $issuing_rule->opacitemholds : undef;
106 =head3 get_onshelfholds_policy
108 my $on_shelf_holds = Koha::IssuingRules->get_onshelfholds_policy({ item => $item, patron => $patron });
110 =cut
112 sub get_onshelfholds_policy {
113 my ( $class, $params ) = @_;
114 my $item = $params->{item};
115 my $itemtype = $item->effective_itemtype;
116 my $patron = $params->{patron};
117 my $issuing_rule = Koha::IssuingRules->get_effective_issuing_rule(
119 ( $patron ? ( categorycode => $patron->categorycode ) : () ),
120 itemtype => $itemtype,
121 branchcode => $item->holdingbranch
124 return $issuing_rule ? $issuing_rule->onshelfholds : undef;
127 =head3 article_requestable_rules
129 Return rules that allow article requests, optionally filtered by
130 patron categorycode.
132 Use with care; see guess_article_requestable_itemtypes.
134 =cut
136 sub article_requestable_rules {
137 my ( $class, $params ) = @_;
138 my $category = $params->{categorycode};
140 return if !C4::Context->preference('ArticleRequests');
141 return $class->search({
142 $category ? ( categorycode => [ $category, '*' ] ) : (),
143 article_requests => { '!=' => 'no' },
147 =head3 guess_article_requestable_itemtypes
149 Return item types in a hashref that are likely possible to be
150 'article requested'. Constructed by an intelligent guess in the
151 issuing rules (see article_requestable_rules).
153 Note: pref ArticleRequestsLinkControl overrides the algorithm.
155 Optional parameters: categorycode.
157 Note: the routine is used in opac-search to obtain a reasonable
158 estimate within performance borders (not looking at all items but
159 just using default itemtype). Also we are not looking at the
160 branchcode here, since home or holding branch of the item is
161 leading and branch may be unknown too (anonymous opac session).
163 =cut
165 sub guess_article_requestable_itemtypes {
166 my ( $class, $params ) = @_;
167 my $category = $params->{categorycode};
168 return {} if !C4::Context->preference('ArticleRequests');
169 return { '*' => 1 } if C4::Context->preference('ArticleRequestsLinkControl') eq 'always';
171 my $cache = Koha::Caches->get_instance;
172 my $last_article_requestable_guesses = $cache->get_from_cache(GUESSED_ITEMTYPES_KEY);
173 my $key = $category || '*';
174 return $last_article_requestable_guesses->{$key}
175 if $last_article_requestable_guesses && exists $last_article_requestable_guesses->{$key};
177 my $res = {};
178 my $rules = $class->article_requestable_rules({
179 $category ? ( categorycode => $category ) : (),
181 return $res if !$rules;
182 foreach my $rule ( $rules->as_list ) {
183 $res->{ $rule->itemtype } = 1;
185 $last_article_requestable_guesses->{$key} = $res;
186 $cache->set_in_cache(GUESSED_ITEMTYPES_KEY, $last_article_requestable_guesses);
187 return $res;
190 =head3 type
192 =cut
194 sub _type {
195 return 'Issuingrule';
198 sub object_class {
199 return 'Koha::IssuingRule';