Bug 20912: (follow-up) Add hourly support
[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( confess );
23
24 use Koha::Calendar;
25 use Koha::IssuingRules;
26 use Koha::DateUtils qw( dt_from_string );
27 use Koha::Exceptions;
28
29 =head1 NAME
30
31 Koha::Charges::Fees - Module calculating fees in Koha
32
33 =head3 new
34
35 Koha::Charges::Fees->new(
36     {
37         patron    => $patron,
38         library   => $library,
39         item      => $item,
40         to_date   => $to_dt,
41         [ from_date => $from_dt, ]
42     }
43 );
44
45 =cut
46
47 sub new {
48     my ( $class, $params ) = @_;
49
50     Koha::Exceptions::MissingParameter->throw("Missing mandatory parameter: patron")
51         unless $params->{patron};
52     Koha::Exceptions::MissingParameter->throw("Missing mandatory parameter: library")
53         unless $params->{library};
54     Koha::Exceptions::MissingParameter->throw("Missing mandatory parameter: item")
55         unless $params->{item};
56     Koha::Exceptions::MissingParameter->throw("Missing mandatory parameter: to_date")
57         unless $params->{to_date};
58
59     Carp::confess("Key 'patron' is not a Koha::Patron object!")
60       unless $params->{patron}->isa('Koha::Patron');
61     Carp::confess("Key 'library' is not a Koha::Library object!")
62       unless $params->{library}->isa('Koha::Library');
63     Carp::confess("Key 'item' is not a Koha::Item object!")
64       unless $params->{item}->isa('Koha::Item');
65     Carp::confess("Key 'to_date' is not a DateTime object!")
66       unless $params->{to_date}->isa('DateTime');
67
68     if ( $params->{from_date} ) {
69         Carp::croak("Key 'from_date' is not a DateTime object!")
70           unless $params->{from_date}->isa('DateTime');
71     }
72     else {
73         $params->{from_date} = dt_from_string();
74     }
75
76     return bless( $params, $class );
77 }
78
79 =head3 accumulate_rentalcharge
80
81     my $fee = $self->accumulate_rentalcharge();
82
83     This method calculates the daily rental fee for a given itemtype for a given
84     period of time passed in as a pair of DateTime objects.
85
86 =cut
87
88 sub accumulate_rentalcharge {
89     my ( $self ) = @_;
90
91     my $itemtype = Koha::ItemTypes->find( $self->item->effective_itemtype );
92     my $issuing_rule = Koha::IssuingRules->get_effective_issuing_rule(
93         {
94             categorycode => $self->patron->categorycode,
95             itemtype     => $itemtype->id,
96             branchcode   => $self->library->id
97         }
98     );
99     my $units = $issuing_rule->lengthunit;
100     my $rentalcharge_increment = ( $units eq 'days' ) ? $itemtype->rentalcharge_daily : $itemtype->rentalcharge_hourly;
101
102     return 0 unless $rentalcharge_increment && $rentalcharge_increment > 0;
103
104     my $duration;
105     my $calendar = Koha::Calendar->new( branchcode => $self->library->id );
106
107     if ( $units eq 'hours' ) {
108         if ( C4::Context->preference('finesCalendar') eq 'noFinesWhenClosed' ) {
109             $duration =
110               $calendar->hours_between( $self->from_date, $self->to_date );
111         }
112         else {
113             $duration = $self->to_date->delta_ms($self->from_date);
114         }
115     }
116     else {
117         if ( C4::Context->preference('finesCalendar') eq 'noFinesWhenClosed' ) {
118             $duration =
119               $calendar->days_between( $self->from_date, $self->to_date );
120         }
121         else {
122             $duration = $self->to_date->delta_days( $self->from_date );
123         }
124     }
125
126     my $charge = $rentalcharge_increment * $duration->in_units($units);
127     return $charge;
128 }
129
130 =head3 patron
131
132 my $patron = $fees->patron( $patron );
133
134 =cut
135
136 sub patron {
137     my ( $self, $patron ) = @_;
138
139     $self->{patron} = $patron if $patron && $patron->isa('Koha::Patron');
140
141     return $self->{patron};
142 }
143
144 =head3 library
145
146 my $library = $fees->library( $library );
147
148 =cut
149
150 sub library {
151     my ( $self, $library ) = @_;
152
153     $self->{library} = $library if $library && $library->isa('Koha::Library');
154
155     return $self->{library};
156 }
157
158 =head3 item
159
160 my $item = $fees->item( $item );
161
162 =cut
163
164 sub item {
165     my ( $self, $item ) = @_;
166
167     $self->{item} = $item if $item && $item->isa('Koha::Item');
168
169     return $self->{item};
170 }
171
172 =head3 to_date
173
174 my $to_date = $fees->to_date( $to_date );
175
176 =cut
177
178 sub to_date {
179     my ( $self, $to_date ) = @_;
180
181     $self->{to_date} = $to_date if $to_date && $to_date->isa('DateTime');
182
183     return $self->{to_date};
184 }
185
186 =head3 from_date
187
188 my $from_date = $fees->from_date( $from_date );
189
190 =cut
191
192 sub from_date {
193     my ( $self, $from_date ) = @_;
194
195     $self->{from_date} = $from_date if $from_date && $from_date->isa('DateTime');
196
197     return $self->{from_date};
198 }
199
200 =head1 AUTHOR
201
202 Kyle M Hall <kyle.m.hall@gmail.com>
203
204 =cut
205
206 1;