Bug 21747: Use Koha::Account:: routines in UpdateFine
[koha.git] / C4 / Overdues.pm
index 1e67b7f..7f0394f 100644 (file)
@@ -191,7 +191,7 @@ sub checkoverdues {
 
 =head2 CalcFine
 
-    ($amount, $chargename,  $units_minus_grace, $chargeable_units) = &CalcFine($item,
+    ($amount, $units_minus_grace, $chargeable_units) = &CalcFine($item,
                                   $categorycode, $branch,
                                   $start_dt, $end_dt );
 
@@ -216,13 +216,10 @@ defining the date range over which to determine the fine.
 
 Fines scripts should just supply the date range over which to calculate the fine.
 
-C<&CalcFine> returns four values:
+C<&CalcFine> returns three values:
 
 C<$amount> is the fine owed by the patron (see above).
 
-C<$chargename> is the chargename field from the applicable record in
-the categoryitem table, whatever that is.
-
 C<$units_minus_grace> is the number of chargeable units minus the grace period
 
 C<$chargeable_units> is the number of chargeable units (days between start and end dates, Calendar adjusted where needed,
@@ -268,9 +265,8 @@ sub CalcFine {
 
     $amount = $item->{replacementprice} if ( $issuing_rule->cap_fine_to_replacement_price && $item->{replacementprice} && $amount > $item->{replacementprice} );
 
-    $debug and warn sprintf("CalcFine returning (%s, %s, %s, %s)", $amount, $issuing_rule->chargename, $units_minus_grace, $chargeable_units);
-    return ($amount, $issuing_rule->chargename, $units_minus_grace, $chargeable_units);
-    # FIXME: chargename is NEVER populated anywhere.
+    $debug and warn sprintf("CalcFine returning (%s, %s, %s)", $amount, $units_minus_grace, $chargeable_units);
+    return ($amount, $units_minus_grace, $chargeable_units);
 }
 
 
@@ -470,7 +466,15 @@ sub GetIssuesIteminfo {
 
 =head2 UpdateFine
 
-    &UpdateFine({ issue_id => $issue_id, itemnumber => $itemnumber, borrwernumber => $borrowernumber, amount => $amount, type => $type, $due => $date_due });
+    &UpdateFine(
+        {
+            issue_id       => $issue_id,
+            itemnumber     => $itemnumber,
+            borrowernumber => $borrowernumber,
+            amount         => $amount,
+            due            => $date_due
+        }
+    );
 
 (Note: the following is mostly conjecture and guesswork.)
 
@@ -483,8 +487,6 @@ has the book on loan.
 
 C<$amount> is the current amount owed by the patron.
 
-C<$type> will be used in the description of the fine.
-
 C<$due> is the due date formatted to the currently specified date format
 
 C<&UpdateFine> looks up the amount currently owed on the given item
@@ -508,10 +510,9 @@ sub UpdateFine {
     my $itemnum        = $params->{itemnumber};
     my $borrowernumber = $params->{borrowernumber};
     my $amount         = $params->{amount};
-    my $type           = $params->{type};
     my $due            = $params->{due};
 
-    $debug and warn "UpdateFine({ itemnumber => $itemnum, borrowernumber => $borrowernumber, type => $type, due => $due, issue_id => $issue_id})";
+    $debug and warn "UpdateFine({ itemnumber => $itemnum, borrowernumber => $borrowernumber, due => $due, issue_id => $issue_id})";
 
     unless ( $issue_id ) {
         carp("No issue_id passed in!");
@@ -577,28 +578,7 @@ sub UpdateFine {
         # (i.e. , of accounttype 'FU').  Doing so will break accrual.
         if ( $data->{'amount'} != $amount ) {
             my $accountline = Koha::Account::Lines->find( $data->{accountlines_id} );
-            my $diff = $amount - $data->{'amount'};
-
-            #3341: diff could be positive or negative!
-            my $out   = $data->{'amountoutstanding'} + $diff;
-
-            $accountline->set(
-                {
-                    date          => dt_from_string(),
-                    amount        => $amount,
-                    amountoutstanding   => $out,
-                    lastincrement => $diff,
-                    accounttype   => 'FU',
-                }
-            )->store();
-
-            Koha::Account::Offset->new(
-                {
-                    debit_id => $accountline->id,
-                    type     => 'Fine Update',
-                    amount   => $diff,
-                }
-            )->store();
+            $accountline->adjust({ amount => $amount, type => 'fine_increment' });
         }
     } else {
         if ( $amount ) { # Don't add new fines with an amount of 0
@@ -607,42 +587,23 @@ sub UpdateFine {
             );
             $sth4->execute($itemnum);
             my $title = $sth4->fetchrow;
+            my $desc = "$title $due";
 
-            my $nextaccntno = C4::Accounts::getnextacctno($borrowernumber);
-
-            my $desc = ( $type ? "$type " : '' ) . "$title $due";    # FIXEDME, avoid whitespace prefix on empty $type
-
-            my $accountline = Koha::Account::Line->new(
+            my $account = Koha::Account->new({ patron_id => $borrowernumber });
+            my $accountline = $account->add_debit(
                 {
-                    borrowernumber    => $borrowernumber,
-                    itemnumber        => $itemnum,
-                    date              => dt_from_string(),
-                    amount            => $amount,
-                    description       => $desc,
-                    accounttype       => 'FU',
-                    amountoutstanding => $amount,
-                    lastincrement     => $amount,
-                    accountno         => $nextaccntno,
-                    issue_id          => $issue_id,
+                    amount      => $amount,
+                    description => $desc,
+                    note        => undef,
+                    user_id     => undef,
+                    library_id  => undef,
+                    type        => 'fine',
+                    item_id     => $itemnum,
+                    issue_id    => $issue_id,
                 }
-            )->store();
-
-            Koha::Account::Offset->new(
-                {
-                    debit_id => $accountline->id,
-                    type     => 'Fine',
-                    amount   => $amount,
-                }
-            )->store();
+            );
         }
     }
-    # logging action
-    &logaction(
-        "FINES",
-        $type,
-        $borrowernumber,
-        "due=".$due."  amount=".$amount." itemnumber=".$itemnum
-        ) if C4::Context->preference("FinesLog");
 }
 
 =head2 BorType