use Modern::Perl;
-use Carp qw( confess );
+use Carp;
use Koha::Calendar;
-use Koha::IssuingRules;
use Koha::DateUtils qw( dt_from_string );
use Koha::Exceptions;
my $fee = $self->accumulate_rentalcharge();
- This method calculates the daily rental fee for a given itemtype for a given
+ This method calculates the daily/hourly rental fee for a given itemtype for a given
period of time passed in as a pair of DateTime objects.
=cut
sub accumulate_rentalcharge {
- my ( $self ) = @_;
+ my ($self) = @_;
- my $itemtype = Koha::ItemTypes->find( $self->item->effective_itemtype );
- my $issuing_rule = Koha::IssuingRules->get_effective_issuing_rule(
+ my $itemtype = Koha::ItemTypes->find( $self->item->effective_itemtype );
+ my $lengthunit_rule = Koha::CirculationRules->get_effective_rule(
{
categorycode => $self->patron->categorycode,
itemtype => $itemtype->id,
- branchcode => $self->library->id
+ branchcode => $self->library->id,
+ rule_name => 'lengthunit',
}
);
- my $units = $issuing_rule->lengthunit;
- my $rentalcharge_increment = ( $units eq 'days' ) ? $itemtype->rentalcharge_daily : $itemtype->rentalcharge_hourly;
+ return 0 unless $lengthunit_rule;
+
+ my $units = $lengthunit_rule->rule_value;
+ my $rentalcharge_increment =
+ ( $units eq 'days' )
+ ? $itemtype->rentalcharge_daily
+ : $itemtype->rentalcharge_hourly;
return 0 unless $rentalcharge_increment && $rentalcharge_increment > 0;
my $calendar = Koha::Calendar->new( branchcode => $self->library->id );
if ( $units eq 'hours' ) {
- if ( C4::Context->preference('finesCalendar') eq 'noFinesWhenClosed' ) {
- $duration =
- $calendar->hours_between( $self->from_date, $self->to_date );
+ if ( $itemtype->rentalcharge_hourly_calendar ) {
+ $duration = $calendar->hours_between(
+ $self->from_date->truncate( to => 'minute' ),
+ $self->to_date->truncate( to => 'minute' )
+ );
}
else {
- $duration = $self->to_date->delta_ms($self->from_date);
+ $duration = $self->to_date->truncate( to => 'minute' )
+ ->delta_ms( $self->from_date->truncate( to => 'minute' ) );
}
}
else {
- if ( C4::Context->preference('finesCalendar') eq 'noFinesWhenClosed' ) {
+ if ( $itemtype->rentalcharge_daily_calendar ) {
$duration =
$calendar->days_between( $self->from_date, $self->to_date );
}
sub patron {
my ( $self, $patron ) = @_;
- $self->{patron} = $patron if $patron && $patron->isa('Koha::Patron');
+ Carp::carp("Setting 'patron' to something other than a Koha::Patron is not supported!")
+ if ($patron && !$patron->isa('Koha::Patron'));
+
+ $self->{patron} = $patron if $patron;
return $self->{patron};
}
sub library {
my ( $self, $library ) = @_;
- $self->{library} = $library if $library && $library->isa('Koha::Library');
+ Carp::carp("Setting 'library' to something other than a Koha::Library is not supported!")
+ if ($library && !$library->isa('Koha::Library'));
+
+ $self->{library} = $library if $library;
return $self->{library};
}
sub item {
my ( $self, $item ) = @_;
- $self->{item} = $item if $item && $item->isa('Koha::Item');
+ Carp::carp("Setting 'item' to something other than a Koha::Item is not supported!")
+ if ($item && !$item->isa('Koha::Item'));
+
+ $self->{item} = $item if $item;
return $self->{item};
}
sub to_date {
my ( $self, $to_date ) = @_;
- $self->{to_date} = $to_date if $to_date && $to_date->isa('DateTime');
+ Carp::carp("Setting 'to_date' to something other than a DateTime is not supported!")
+ if ($to_date && !$to_date->isa('DateTime'));
+
+ $self->{to_date} = $to_date if $to_date;
return $self->{to_date};
}
sub from_date {
my ( $self, $from_date ) = @_;
+ Carp::carp("Setting 'from_date' to something other than a DateTime is not supported!")
+ if ($from_date && !$from_date->isa('DateTime'));
+
$self->{from_date} = $from_date if $from_date && $from_date->isa('DateTime');
return $self->{from_date};
}
-=head1 AUTHOR
+=head1 AUTHORS
Kyle M Hall <kyle.m.hall@gmail.com>
+Martin Renvoize <martin.renvoize@ptfs-europe.com>
=cut