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
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.
25 use Koha::IssuingRule;
27 use base qw(Koha::Objects);
31 Koha::IssuingRules - Koha IssuingRule Object set class
39 sub get_effective_issuing_rule {
40 my ( $self, $params ) = @_;
43 my $categorycode = $params->{categorycode};
44 my $itemtype = $params->{itemtype};
45 my $branchcode = $params->{branchcode};
47 my $search_categorycode = $default;
48 my $search_itemtype = $default;
49 my $search_branchcode = $default;
52 $search_categorycode = { 'in' => [ $categorycode, $default ] };
55 $search_itemtype = { 'in' => [ $itemtype, $default ] };
58 $search_branchcode = { 'in' => [ $branchcode, $default ] };
61 my $rule = $self->search({
62 categorycode => $search_categorycode,
63 itemtype => $search_itemtype,
64 branchcode => $search_branchcode,
67 -desc => ['branchcode', 'categorycode', 'itemtype']
74 =head3 get_opacitemholds_policy
76 my $can_place_a_hold_at_item_level = Koha::IssuingRules->get_opacitemholds_policy( { patron => $patron, item => $item } );
78 Return 'Y' or 'F' if the patron can place a hold on this item according to the issuing rules
79 and the "Item level holds" (opacitemholds).
80 Can be 'N' - Don't allow, 'Y' - Allow, and 'F' - Force
84 sub get_opacitemholds_policy {
85 my ( $class, $params ) = @_;
87 my $item = $params->{item};
88 my $patron = $params->{patron};
90 return unless $item or $patron;
92 my $issuing_rule = Koha::IssuingRules->get_effective_issuing_rule(
94 categorycode => $patron->categorycode,
95 itemtype => $item->effective_itemtype,
96 branchcode => $item->homebranch,
100 return $issuing_rule ? $issuing_rule->opacitemholds : undef;
103 =head3 get_onshelfholds_policy
105 my $on_shelf_holds = Koha::IssuingRules->get_onshelfholds_policy({ item => $item, patron => $patron });
109 sub get_onshelfholds_policy {
110 my ( $class, $params ) = @_;
111 my $item = $params->{item};
112 my $itemtype = $item->effective_itemtype;
113 my $patron = $params->{patron};
114 my $issuing_rule = Koha::IssuingRules->get_effective_issuing_rule(
116 ( $patron ? ( categorycode => $patron->categorycode ) : () ),
117 itemtype => $itemtype,
118 branchcode => $item->holdingbranch
121 return $issuing_rule ? $issuing_rule->onshelfholds : undef;
124 =head3 article_requestable_rules
126 Return rules that allow article requests, optionally filtered by
129 Use with care; see guess_article_requestable_itemtypes.
133 sub article_requestable_rules {
134 my ( $class_or_self, $params ) = @_;
135 my $category = $params->{categorycode};
137 return if !C4::Context->preference('ArticleRequests');
138 return $class_or_self->search({
139 $category ? ( categorycode => [ $category, '*' ] ) : (),
140 article_requests => { '!=' => 'no' },
144 =head3 guess_article_requestable_itemtypes
146 Return item types in a hashref that are likely possible to be
147 'article requested'. Constructed by an intelligent guess in the
148 issuing rules (see article_requestable_rules).
150 Optional parameters: categorycode.
152 Note: the routine is used in opac-search to obtain a reasonable
153 estimate within performance borders (not looking at all items but
154 just using default itemtype). Also we are not looking at the
155 branchcode here, since home or holding branch of the item is
156 leading and branch may be unknown too (anonymous opac session).
160 our $last_article_requestable_guesses; # used during Plack life time
162 sub guess_article_requestable_itemtypes {
163 my ( $class_or_self, $params ) = @_;
164 my $category = $params->{categorycode};
165 return {} if !C4::Context->preference('ArticleRequests');
167 my $key = $category || '*';
168 return $last_article_requestable_guesses->{$key}
169 if $last_article_requestable_guesses && exists $last_article_requestable_guesses->{$key};
172 my $rules = $class_or_self->article_requestable_rules({
173 $category ? ( categorycode => $category ) : (),
175 return $res if !$rules;
176 foreach my $rule ( $rules->as_list ) {
177 $res->{ $rule->itemtype } = 1;
179 $last_article_requestable_guesses->{$key} = $res;
188 return 'Issuingrule';
192 return 'Koha::IssuingRule';