Bug 28316: avoid messing up regexes in the search queries
[koha.git] / Koha / Charges / Fees.pm
1 package Koha::Charges::Fees;
2
3 # Copyright 2018 ByWater Solutions
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21
22 use Carp qw( carp confess );
23
24 use Koha::Calendar;
25 use Koha::DateUtils qw( dt_from_string );
26 use Koha::Exceptions;
27
28 =head1 NAME
29
30 Koha::Charges::Fees - Module calculating fees in Koha
31
32 =head2 Class methods
33
34 =head3 new
35
36 Koha::Charges::Fees->new(
37     {
38         patron    => $patron,
39         library   => $library,
40         item      => $item,
41         to_date   => $to_dt,
42         [ from_date => $from_dt, ]
43     }
44 );
45
46 =cut
47
48 sub new {
49     my ( $class, $params ) = @_;
50
51     Koha::Exceptions::MissingParameter->throw("Missing mandatory parameter: patron")
52         unless $params->{patron};
53     Koha::Exceptions::MissingParameter->throw("Missing mandatory parameter: library")
54         unless $params->{library};
55     Koha::Exceptions::MissingParameter->throw("Missing mandatory parameter: item")
56         unless $params->{item};
57     Koha::Exceptions::MissingParameter->throw("Missing mandatory parameter: to_date")
58         unless $params->{to_date};
59
60     Carp::confess("Key 'patron' is not a Koha::Patron object!")
61       unless $params->{patron}->isa('Koha::Patron');
62     Carp::confess("Key 'library' is not a Koha::Library object!")
63       unless $params->{library}->isa('Koha::Library');
64     Carp::confess("Key 'item' is not a Koha::Item object!")
65       unless $params->{item}->isa('Koha::Item');
66     Carp::confess("Key 'to_date' is not a DateTime object!")
67       unless $params->{to_date}->isa('DateTime');
68
69     if ( $params->{from_date} ) {
70         Carp::croak("Key 'from_date' is not a DateTime object!")
71           unless $params->{from_date}->isa('DateTime');
72     }
73     else {
74         $params->{from_date} = dt_from_string();
75     }
76
77     return bless( $params, $class );
78 }
79
80 =head3 accumulate_rentalcharge
81
82     my $fee = $self->accumulate_rentalcharge();
83
84     This method calculates the daily/hourly rental fee for a given itemtype for a given
85     period of time passed in as a pair of DateTime objects.
86
87 =cut
88
89 sub accumulate_rentalcharge {
90     my ($self) = @_;
91
92     my $itemtype        = Koha::ItemTypes->find( $self->item->effective_itemtype );
93     my $lengthunit_rule = Koha::CirculationRules->get_effective_rule(
94         {
95             categorycode => $self->patron->categorycode,
96             itemtype     => $itemtype->id,
97             branchcode   => $self->library->id,
98             rule_name    => 'lengthunit',
99         }
100     );
101     return 0 unless $lengthunit_rule;
102
103     my $units = $lengthunit_rule->rule_value;
104     my $rentalcharge_increment =
105       ( $units eq 'days' )
106       ? $itemtype->rentalcharge_daily
107       : $itemtype->rentalcharge_hourly;
108
109     return 0 unless $rentalcharge_increment && $rentalcharge_increment > 0;
110
111     my $duration;
112     my $calendar = Koha::Calendar->new( branchcode => $self->library->id );
113
114     if ( $units eq 'hours' ) {
115         if ( $itemtype->rentalcharge_hourly_calendar ) {
116             $duration = $calendar->hours_between(
117                 $self->from_date->truncate( to => 'minute' ),
118                 $self->to_date->truncate( to => 'minute' )
119             );
120         }
121         else {
122             $duration = $self->to_date->truncate( to => 'minute' )
123               ->delta_ms( $self->from_date->truncate( to => 'minute' ) );
124         }
125     }
126     else {
127         if ( $itemtype->rentalcharge_daily_calendar ) {
128             $duration =
129               $calendar->days_between( $self->from_date, $self->to_date );
130         }
131         else {
132             $duration = $self->to_date->delta_days( $self->from_date );
133         }
134     }
135
136     my $charge = $rentalcharge_increment * $duration->in_units($units);
137     return $charge;
138 }
139
140 =head3 patron
141
142 my $patron = $fees->patron( $patron );
143
144 =cut
145
146 sub patron {
147     my ( $self, $patron ) = @_;
148
149     Carp::carp("Setting 'patron' to something other than a Koha::Patron is not supported!")
150       if ($patron && !$patron->isa('Koha::Patron'));
151
152     $self->{patron} = $patron if $patron;
153
154     return $self->{patron};
155 }
156
157 =head3 library
158
159 my $library = $fees->library( $library );
160
161 =cut
162
163 sub library {
164     my ( $self, $library ) = @_;
165
166     Carp::carp("Setting 'library' to something other than a Koha::Library is not supported!")
167       if ($library && !$library->isa('Koha::Library'));
168
169     $self->{library} = $library if $library;
170
171     return $self->{library};
172 }
173
174 =head3 item
175
176 my $item = $fees->item( $item );
177
178 =cut
179
180 sub item {
181     my ( $self, $item ) = @_;
182
183     Carp::carp("Setting 'item' to something other than a Koha::Item is not supported!")
184       if ($item && !$item->isa('Koha::Item'));
185
186     $self->{item} = $item if $item;
187
188     return $self->{item};
189 }
190
191 =head3 to_date
192
193 my $to_date = $fees->to_date( $to_date );
194
195 =cut
196
197 sub to_date {
198     my ( $self, $to_date ) = @_;
199
200     Carp::carp("Setting 'to_date' to something other than a DateTime is not supported!")
201       if ($to_date && !$to_date->isa('DateTime'));
202
203     $self->{to_date} = $to_date if $to_date;
204
205     return $self->{to_date};
206 }
207
208 =head3 from_date
209
210 my $from_date = $fees->from_date( $from_date );
211
212 =cut
213
214 sub from_date {
215     my ( $self, $from_date ) = @_;
216
217     Carp::carp("Setting 'from_date' to something other than a DateTime is not supported!")
218       if ($from_date && !$from_date->isa('DateTime'));
219
220     $self->{from_date} = $from_date if $from_date && $from_date->isa('DateTime');
221
222     return $self->{from_date};
223 }
224
225 =head1 AUTHORS
226
227 Kyle M Hall <kyle.m.hall@gmail.com>
228 Martin Renvoize <martin.renvoize@ptfs-europe.com>
229
230 =cut
231
232 1;