From ef9980d08c4ee6d95d650ea9e1779da306b232ea Mon Sep 17 00:00:00 2001
From: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Date: Tue, 12 May 2020 09:24:28 +0100
Subject: [PATCH] Bug 25417: (QA follow-up) Add integration test for AddReturn

Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
---
 t/db_dependent/Circulation.t | 48 +++++++++++++++++++++++++++++++++---
 1 file changed, 45 insertions(+), 3 deletions(-)

diff --git a/t/db_dependent/Circulation.t b/t/db_dependent/Circulation.t
index d75811062c..42b18d1d11 100755
--- a/t/db_dependent/Circulation.t
+++ b/t/db_dependent/Circulation.t
@@ -2337,7 +2337,7 @@ subtest 'CanBookBeIssued + AutoReturnCheckedOutItems' => sub {
 
 
 subtest 'AddReturn | is_overdue' => sub {
-    plan tests => 5;
+    plan tests => 6;
 
     t::lib::Mocks::mock_preference('MarkLostItemsAsReturned', 'batchmod|moredetail|cronjob|additem|pendingreserves|onpayment');
     t::lib::Mocks::mock_preference('CalculateFinesOnReturn', 1);
@@ -2432,7 +2432,49 @@ subtest 'AddReturn | is_overdue' => sub {
         AddReturn( $item->{barcode}, $library->{branchcode} );
         is( int( $patron->account->balance() ),
             17, "Should have a single 10 days overdue fine and lost charge" );
-      }
+
+        # Cleanup
+        Koha::Account::Lines->search({ borrowernumber => $patron->borrowernumber })->delete;
+    };
+
+    subtest 'bug 25417 | backdated return + exemptfine' => sub {
+
+        plan tests => 6;
+
+        t::lib::Mocks::mock_preference('CalculateFinesOnBackdate', 1);
+
+        my $issue = AddIssue( $patron->unblessed, $item->{barcode}, $one_day_ago );    # date due was 1d ago
+
+        # Fake fines cronjob on this checkout
+        my ($fine) =
+          CalcFine( $item, $patron->categorycode, $library->{branchcode},
+            $one_day_ago, $now );
+        UpdateFine(
+            {
+                issue_id       => $issue->issue_id,
+                itemnumber     => $item->{itemnumber},
+                borrowernumber => $patron->borrowernumber,
+                amount         => $fine,
+                due            => output_pref($one_day_ago)
+            }
+        );
+        is( int( $patron->account->balance() ),
+            1, "Overdue fine of 1 day overdue" );
+
+        # Backdated return (dropbox mode example - charge should exist but be zero)
+        AddReturn( $item->{barcode}, $library->{branchcode}, 1, $one_day_ago );
+        is( int( $patron->account->balance() ),
+            0, "Overdue fine should be annulled" );
+        my $lines = Koha::Account::Lines->search({ borrowernumber => $patron->borrowernumber });
+        is( $lines->count, 1, "Overdue fine accountlines still exists");
+        my $line = $lines->next;
+        is($line->amount+0,0, "Overdue fine amount has been reduced to 0");
+        is($line->amountoutstanding+0,0, "Overdue fine amount outstanding has been reduced to 0");
+        is($line->status,'RETURNED', "Overdue fine was fixed");
+
+        # Cleanup
+        Koha::Account::Lines->search({ borrowernumber => $patron->borrowernumber })->delete;
+    };
 };
 
 subtest '_FixAccountForLostAndFound' => sub {
@@ -2892,7 +2934,7 @@ subtest '_FixOverduesOnReturn' => sub {
     is( $credit->amount + 0, -99, "Credit amount is set correctly" );
     is( $credit->amountoutstanding + 0, 0, "Credit amountoutstanding is correctly set to 0" );
 
-    # Bug 25417 - Only forgive fines where there is an amount oustanding to forgive
+    # Bug 25417 - Only forgive fines where there is an amount outstanding to forgive
     $accountline->set(
         {
             debit_type_code    => 'OVERDUE',
-- 
2.24.2 (Apple Git-127)