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