Merge branch 'bug_10230' into 3.14-master
[koha.git] / C4 / Overdues.pm
index d17f445..9c05668 100644 (file)
@@ -72,10 +72,6 @@ BEGIN {
        push @EXPORT, qw(
         &GetIssuesIteminfo
        );
-    #
-       # &GetIssuingRules - delete.
-       # use C4::Circulation::GetIssuingRule instead.
-       
        # subs to move to Members.pm
        push @EXPORT, qw(
         &CheckBorrowerDebarred
@@ -263,10 +259,14 @@ sub CalcFine {
     my $chargeable_units = _get_chargeable_units($fine_unit, $start_date, $end_date, $branchcode);
     my $units_minus_grace = $chargeable_units - $data->{firstremind};
     my $amount = 0;
-    if ($data->{'chargeperiod'}  && $units_minus_grace  ) {
-        $amount = int($chargeable_units / $data->{'chargeperiod'}) * $data->{'fine'};# TODO fine calc should be in cents
+    if ($data->{'chargeperiod'}  && ($units_minus_grace > 0)  ) {
+        if ( C4::Context->preference('FinesIncludeGracePeriod') ) {
+            $amount = int($chargeable_units / $data->{'chargeperiod'}) * $data->{'fine'};# TODO fine calc should be in cents
+        } else {
+            $amount = int($units_minus_grace / $data->{'chargeperiod'}) * $data->{'fine'};
+        }
     } else {
-        # a zero (or null)  chargeperiod means no charge.
+        # a zero (or null) chargeperiod or negative units_minus_grace value means no charge.
     }
     $amount = $data->{overduefinescap} if $data->{overduefinescap} && $amount > $data->{overduefinescap};
     $debug and warn sprintf("CalcFine returning (%s, %s, %s, %s)", $amount, $data->{'chargename'}, $units_minus_grace, $chargeable_units);
@@ -540,14 +540,14 @@ sub UpdateFine {
                 next;
             }
         }
-        $total_amount_other += $rec->{'amount'};
+        $total_amount_other += $rec->{'amountoutstanding'};
     }
+
     if (my $maxfine = C4::Context->preference('MaxFine')) {
         if ($total_amount_other + $amount > $maxfine) {
             my $new_amount = $maxfine - $total_amount_other;
-            warn "Reducing fine for item $itemnum borrower $borrowernumber from $amount to $new_amount - MaxFine reached";
             return if $new_amount <= 0.00;
-
+            warn "Reducing fine for item $itemnum borrower $borrowernumber from $amount to $new_amount - MaxFine reached";
             $amount = $new_amount;
         }
     }
@@ -692,41 +692,6 @@ sub GetFine {
     return 0;
 }
 
-
-=head2 GetIssuingRules
-
-FIXME - This sub should be deprecated and removed.
-It ignores branch and defaults.
-
-    $data = &GetIssuingRules($itemtype,$categorycode);
-
-Looks up for all issuingrules an item info 
-
-C<$itemnumber> is a reference-to-hash whose keys are all of the fields
-from the borrowers and categories tables of the Koha database. Thus,
-
-C<$categorycode> contains  information about borrowers category 
-
-C<$data> contains all information about both the borrower and
-category he or she belongs to.
-=cut 
-
-sub GetIssuingRules {
-       warn "GetIssuingRules is deprecated: use GetIssuingRule from C4::Circulation instead.";
-   my ($itemtype,$categorycode)=@_;
-   my $dbh   = C4::Context->dbh();    
-   my $query=qq|SELECT *
-        FROM issuingrules
-        WHERE issuingrules.itemtype=?
-            AND issuingrules.categorycode=?
-        |;
-    my $sth = $dbh->prepare($query);
-    #  print $query;
-    $sth->execute($itemtype,$categorycode);
-    return $sth->fetchrow_hashref;
-}
-
-
 sub ReplacementCost2 {
     my ( $itemnum, $borrowernumber ) = @_;
     my $dbh   = C4::Context->dbh();