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>.
22 use Carp
qw( carp confess );
25 use Koha
::IssuingRules
;
26 use Koha
::DateUtils
qw( dt_from_string );
31 Koha::Charges::Fees - Module calculating fees in Koha
37 Koha::Charges::Fees->new(
43 [ from_date => $from_dt, ]
50 my ( $class, $params ) = @_;
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
};
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');
70 if ( $params->{from_date
} ) {
71 Carp
::croak
("Key 'from_date' is not a DateTime object!")
72 unless $params->{from_date
}->isa('DateTime');
75 $params->{from_date
} = dt_from_string
();
78 return bless( $params, $class );
81 =head3 accumulate_rentalcharge
83 my $fee = $self->accumulate_rentalcharge();
85 This method calculates the daily/hourly rental fee for a given itemtype for a given
86 period of time passed in as a pair of DateTime objects.
90 sub accumulate_rentalcharge
{
93 my $itemtype = Koha
::ItemTypes
->find( $self->item->effective_itemtype );
94 my $issuing_rule = Koha
::IssuingRules
->get_effective_issuing_rule(
96 categorycode
=> $self->patron->categorycode,
97 itemtype
=> $itemtype->id,
98 branchcode
=> $self->library->id
101 return 0 unless $issuing_rule;
103 my $units = $issuing_rule->lengthunit;
104 my $rentalcharge_increment =
106 ?
$itemtype->rentalcharge_daily
107 : $itemtype->rentalcharge_hourly;
109 return 0 unless $rentalcharge_increment && $rentalcharge_increment > 0;
112 my $calendar = Koha
::Calendar
->new( branchcode
=> $self->library->id );
114 if ( $units eq 'hours' ) {
115 if ( C4
::Context
->preference('finesCalendar') eq 'noFinesWhenClosed' ) {
116 $duration = $calendar->hours_between(
117 $self->from_date->truncate( to
=> 'minute' ),
118 $self->to_date->truncate( to
=> 'minute' )
122 $duration = $self->to_date->truncate( to
=> 'minute' )
123 ->delta_ms( $self->from_date->truncate( to
=> 'minute' ) );
127 if ( C4
::Context
->preference('finesCalendar') eq 'noFinesWhenClosed' ) {
129 $calendar->days_between( $self->from_date, $self->to_date );
132 $duration = $self->to_date->delta_days( $self->from_date );
136 my $charge = $rentalcharge_increment * $duration->in_units($units);
142 my $patron = $fees->patron( $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
};
159 my $library = $fees->library( $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
};
176 my $item = $fees->item( $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
};
193 my $to_date = $fees->to_date( $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
};
210 my $from_date = $fees->from_date( $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
};
227 Kyle M Hall <kyle.m.hall@gmail.com>
228 Martin Renvoize <martin.renvoize@ptfs-europe.com>