Bug 25858: Use bitwise OR for setting a bit in borrowers.flag
[koha.git] / Koha / Charges / Fees.pm
blob7eff76ba794779916741dd0d3c9210209262bdae
1 package Koha::Charges::Fees;
3 # Copyright 2018 ByWater Solutions
5 # This file is part of Koha.
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.
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.
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>.
20 use Modern::Perl;
22 use Carp qw( carp confess );
24 use Koha::Calendar;
25 use Koha::DateUtils qw( dt_from_string );
26 use Koha::Exceptions;
28 =head1 NAME
30 Koha::Charges::Fees - Module calculating fees in Koha
32 =head2 Class methods
34 =head3 new
36 Koha::Charges::Fees->new(
38 patron => $patron,
39 library => $library,
40 item => $item,
41 to_date => $to_dt,
42 [ from_date => $from_dt, ]
46 =cut
48 sub new {
49 my ( $class, $params ) = @_;
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};
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');
69 if ( $params->{from_date} ) {
70 Carp::croak("Key 'from_date' is not a DateTime object!")
71 unless $params->{from_date}->isa('DateTime');
73 else {
74 $params->{from_date} = dt_from_string();
77 return bless( $params, $class );
80 =head3 accumulate_rentalcharge
82 my $fee = $self->accumulate_rentalcharge();
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.
87 =cut
89 sub accumulate_rentalcharge {
90 my ($self) = @_;
92 my $itemtype = Koha::ItemTypes->find( $self->item->effective_itemtype );
93 my $lengthunit_rule = Koha::CirculationRules->get_effective_rule(
95 categorycode => $self->patron->categorycode,
96 itemtype => $itemtype->id,
97 branchcode => $self->library->id,
98 rule_name => 'lengthunit',
101 return 0 unless $lengthunit_rule;
103 my $units = $lengthunit_rule->rule_value;
104 my $rentalcharge_increment =
105 ( $units eq 'days' )
106 ? $itemtype->rentalcharge_daily
107 : $itemtype->rentalcharge_hourly;
109 return 0 unless $rentalcharge_increment && $rentalcharge_increment > 0;
111 my $duration;
112 my $calendar = Koha::Calendar->new( branchcode => $self->library->id );
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' )
121 else {
122 $duration = $self->to_date->truncate( to => 'minute' )
123 ->delta_ms( $self->from_date->truncate( to => 'minute' ) );
126 else {
127 if ( $itemtype->rentalcharge_daily_calendar ) {
128 $duration =
129 $calendar->days_between( $self->from_date, $self->to_date );
131 else {
132 $duration = $self->to_date->delta_days( $self->from_date );
136 my $charge = $rentalcharge_increment * $duration->in_units($units);
137 return $charge;
140 =head3 patron
142 my $patron = $fees->patron( $patron );
144 =cut
146 sub patron {
147 my ( $self, $patron ) = @_;
149 Carp::carp("Setting 'patron' to something other than a Koha::Patron is not supported!")
150 if ($patron && !$patron->isa('Koha::Patron'));
152 $self->{patron} = $patron if $patron;
154 return $self->{patron};
157 =head3 library
159 my $library = $fees->library( $library );
161 =cut
163 sub library {
164 my ( $self, $library ) = @_;
166 Carp::carp("Setting 'library' to something other than a Koha::Library is not supported!")
167 if ($library && !$library->isa('Koha::Library'));
169 $self->{library} = $library if $library;
171 return $self->{library};
174 =head3 item
176 my $item = $fees->item( $item );
178 =cut
180 sub item {
181 my ( $self, $item ) = @_;
183 Carp::carp("Setting 'item' to something other than a Koha::Item is not supported!")
184 if ($item && !$item->isa('Koha::Item'));
186 $self->{item} = $item if $item;
188 return $self->{item};
191 =head3 to_date
193 my $to_date = $fees->to_date( $to_date );
195 =cut
197 sub to_date {
198 my ( $self, $to_date ) = @_;
200 Carp::carp("Setting 'to_date' to something other than a DateTime is not supported!")
201 if ($to_date && !$to_date->isa('DateTime'));
203 $self->{to_date} = $to_date if $to_date;
205 return $self->{to_date};
208 =head3 from_date
210 my $from_date = $fees->from_date( $from_date );
212 =cut
214 sub from_date {
215 my ( $self, $from_date ) = @_;
217 Carp::carp("Setting 'from_date' to something other than a DateTime is not supported!")
218 if ($from_date && !$from_date->isa('DateTime'));
220 $self->{from_date} = $from_date if $from_date && $from_date->isa('DateTime');
222 return $self->{from_date};
225 =head1 AUTHORS
227 Kyle M Hall <kyle.m.hall@gmail.com>
228 Martin Renvoize <martin.renvoize@ptfs-europe.com>
230 =cut