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( confess );
25 use Koha::IssuingRules;
26 use Koha::DateUtils qw( dt_from_string );
31 Koha::Charges::Fees - Module calculating fees in Koha
35 Koha::Charges::Fees->new(
41 [ from_date => $from_dt, ]
48 my ( $class, $params ) = @_;
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};
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');
68 if ( $params->{from_date} ) {
69 Carp::croak("Key 'from_date' is not a DateTime object!")
70 unless $params->{from_date}->isa('DateTime');
73 $params->{from_date} = dt_from_string();
76 return bless( $params, $class );
79 =head3 accumulate_rentalcharge
81 my $fee = $self->accumulate_rentalcharge();
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.
88 sub accumulate_rentalcharge {
91 my $itemtype = Koha::ItemTypes->find( $self->item->effective_itemtype );
92 my $issuing_rule = Koha::IssuingRules->get_effective_issuing_rule(
94 categorycode => $self->patron->categorycode,
95 itemtype => $itemtype->id,
96 branchcode => $self->library->id
99 my $units = $issuing_rule->lengthunit;
100 my $rentalcharge_increment = ( $units eq 'days' ) ? $itemtype->rentalcharge_daily : $itemtype->rentalcharge_hourly;
102 return 0 unless $rentalcharge_increment && $rentalcharge_increment > 0;
105 my $calendar = Koha::Calendar->new( branchcode => $self->library->id );
107 if ( $units eq 'hours' ) {
108 if ( C4::Context->preference('finesCalendar') eq 'noFinesWhenClosed' ) {
110 $calendar->hours_between( $self->from_date, $self->to_date );
113 $duration = $self->to_date->delta_ms($self->from_date);
117 if ( C4::Context->preference('finesCalendar') eq 'noFinesWhenClosed' ) {
119 $calendar->days_between( $self->from_date, $self->to_date );
122 $duration = $self->to_date->delta_days( $self->from_date );
126 my $charge = $rentalcharge_increment * $duration->in_units($units);
132 my $patron = $fees->patron( $patron );
137 my ( $self, $patron ) = @_;
139 $self->{patron} = $patron if $patron && $patron->isa('Koha::Patron');
141 return $self->{patron};
146 my $library = $fees->library( $library );
151 my ( $self, $library ) = @_;
153 $self->{library} = $library if $library && $library->isa('Koha::Library');
155 return $self->{library};
160 my $item = $fees->item( $item );
165 my ( $self, $item ) = @_;
167 $self->{item} = $item if $item && $item->isa('Koha::Item');
169 return $self->{item};
174 my $to_date = $fees->to_date( $to_date );
179 my ( $self, $to_date ) = @_;
181 $self->{to_date} = $to_date if $to_date && $to_date->isa('DateTime');
183 return $self->{to_date};
188 my $from_date = $fees->from_date( $from_date );
193 my ( $self, $from_date ) = @_;
195 $self->{from_date} = $from_date if $from_date && $from_date->isa('DateTime');
197 return $self->{from_date};
202 Kyle M Hall <kyle.m.hall@gmail.com>