Bug 17600: Standardize our EXPORT_OK
[srvgit] / Koha / Charges / Fees.pm
index 41299ce..a9bbf0f 100644 (file)
@@ -19,10 +19,9 @@ package Koha::Charges::Fees;
 
 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;
 
@@ -82,24 +81,30 @@ sub new {
 
     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;
 
@@ -107,16 +112,19 @@ sub accumulate_rentalcharge {
     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 );
         }
@@ -138,7 +146,10 @@ my $patron = $fees->patron( $patron );
 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};
 }
@@ -152,7 +163,10 @@ my $library = $fees->library( $library );
 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};
 }
@@ -166,7 +180,10 @@ my $item = $fees->item( $item );
 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};
 }
@@ -180,7 +197,10 @@ my $to_date = $fees->to_date( $to_date );
 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};
 }
@@ -194,14 +214,18 @@ my $from_date = $fees->from_date( $from_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