b08ac1179dbf99ab48ef0496dd4c251c172a4fcd
[koha.git] / Koha / RefundLostItemFeeRules.pm
1 package Koha::RefundLostItemFeeRules;
2
3 # Copyright Theke Solutions 2016
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 3 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use Modern::Perl;
21
22 use Koha::Database;
23 use Koha::Exceptions;
24
25 use Koha::CirculationRule;
26
27 use base qw(Koha::CirculationRules);
28
29 =head1 NAME
30
31 Koha::RefundLostItemFeeRules - Koha RefundLostItemFeeRules object set class
32
33 =head1 API
34
35 =head2 Class Methods
36
37 =cut
38
39 =head3 type
40
41 =cut
42
43 sub _type {
44     return 'CirculationRule';
45 }
46
47 =head3 object_class
48
49 =cut
50
51 sub object_class {
52     return 'Koha::CirculationRule';
53 }
54
55 =head3 should_refund
56
57 Koha::RefundLostItemFeeRules->should_refund()
58
59 Returns a boolean telling if the fee needs to be refund given the
60 passed params, and the current rules/sysprefs configuration.
61
62 =cut
63
64 sub should_refund {
65
66     my $self = shift;
67     my $params = shift;
68
69     return $self->_effective_branch_rule( $self->_choose_branch( $params ) );
70 }
71
72
73 =head3 _effective_branch_rule
74
75 Koha::RefundLostItemFeeRules->_effective_branch_rule
76
77 Given a branch, returns a boolean representing the resulting rule.
78 It tries the branch-specific first. Then falls back to the defined default.
79
80 =cut
81
82 sub _effective_branch_rule {
83
84     my $self   = shift;
85     my $branch = shift;
86
87     my $specific_rule = $self->search(
88         {
89             branchcode   => $branch,
90             categorycode => undef,
91             itemtype     => undef,
92             rule_name    => 'refund',
93         }
94     )->next();
95
96     return ( defined $specific_rule )
97                 ? $specific_rule->rule_value
98                 : $self->_default_rule;
99 }
100
101 =head3 _choose_branch
102
103 my $branch = Koha::RefundLostItemFeeRules->_choose_branch({
104                 current_branch => 'current_branch_code',
105                 item_home_branch => 'item_home_branch',
106                 item_holding_branch => 'item_holding_branch'
107 });
108
109 Helper function that determines the branch to be used to apply the rule.
110
111 =cut
112
113 sub _choose_branch {
114
115     my $self = shift;
116     my $param = shift;
117
118     my $behaviour = C4::Context->preference( 'RefundLostOnReturnControl' ) // 'CheckinLibrary';
119
120     my $param_mapping = {
121            CheckinLibrary => 'current_branch',
122            ItemHomeBranch => 'item_home_branch',
123         ItemHoldingBranch => 'item_holding_branch'
124     };
125
126     my $branch = $param->{ $param_mapping->{ $behaviour } };
127
128     if ( !defined $branch ) {
129         Koha::Exceptions::MissingParameter->throw(
130             "$behaviour requires the " .
131             $param_mapping->{ $behaviour } .
132             " param"
133         );
134     }
135
136     return $branch;
137 }
138
139 =head3 _default_rule (internal)
140
141 This function returns the default rule defined for refunding lost
142 item fees on return. It defaults to 1 if no rule is defined.
143
144 =cut
145
146 sub _default_rule {
147
148     my $self = shift;
149     my $default_rule = $self->search(
150         {
151             branchcode   => '*',
152             categorycode => undef,
153             itemtype     => undef,
154             rule_name    => 'refund',
155         }
156     )->next();
157
158     return (defined $default_rule)
159                 ? $default_rule->rule_value
160                 : 1;
161 }
162
163 1;