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