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
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 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 my $units = $issuing_rule->lengthunit;
102 my $rentalcharge_increment = ( $units eq 'days' ) ?
$itemtype->rentalcharge_daily : $itemtype->rentalcharge_hourly;
104 return 0 unless $rentalcharge_increment && $rentalcharge_increment > 0;
107 my $calendar = Koha
::Calendar
->new( branchcode
=> $self->library->id );
109 if ( $units eq 'hours' ) {
110 if ( C4
::Context
->preference('finesCalendar') eq 'noFinesWhenClosed' ) {
112 $calendar->hours_between( $self->from_date, $self->to_date );
115 $duration = $self->to_date->delta_ms($self->from_date);
119 if ( C4
::Context
->preference('finesCalendar') eq 'noFinesWhenClosed' ) {
121 $calendar->days_between( $self->from_date, $self->to_date );
124 $duration = $self->to_date->delta_days( $self->from_date );
128 my $charge = $rentalcharge_increment * $duration->in_units($units);
134 my $patron = $fees->patron( $patron );
139 my ( $self, $patron ) = @_;
141 $self->{patron
} = $patron if $patron && $patron->isa('Koha::Patron');
143 return $self->{patron
};
148 my $library = $fees->library( $library );
153 my ( $self, $library ) = @_;
155 $self->{library
} = $library if $library && $library->isa('Koha::Library');
157 return $self->{library
};
162 my $item = $fees->item( $item );
167 my ( $self, $item ) = @_;
169 $self->{item
} = $item if $item && $item->isa('Koha::Item');
171 return $self->{item
};
176 my $to_date = $fees->to_date( $to_date );
181 my ( $self, $to_date ) = @_;
183 $self->{to_date
} = $to_date if $to_date && $to_date->isa('DateTime');
185 return $self->{to_date
};
190 my $from_date = $fees->from_date( $from_date );
195 my ( $self, $from_date ) = @_;
197 $self->{from_date
} = $from_date if $from_date && $from_date->isa('DateTime');
199 return $self->{from_date
};
204 Kyle M Hall <kyle.m.hall@gmail.com>