From 946c27996bbe16b4b5a0ca2a0647cb46a4f83dd6 Mon Sep 17 00:00:00 2001
From: Maryse Simard <maryse.simard@inlibro.com>
Date: Wed, 25 Jul 2018 16:41:28 -0400
Subject: [PATCH] Bug 17483: Forgiving charge should apply to item's last fine.

    When returning an item with a fine, if the Forgive checkbox is clicked, this forgives
    the first accountline associated to the item/user, not the current/latest as it should.

    Scenario (Real world):
    - A patron returns a book late, ensuring a 5$ fine.  Does not pay it.
    - The patron borrows the book again 3 months later.  Gets late again, and fined 2$.
    - Great timing: The library offers amnesty for a week.  Bring back your book and we'll forget that they're late.
    - On checkin, the librarian clicks "Forgive overdue charges" and scan the book.
    - Koha cancels the 3-months-ago 5$ fine.

    Test scenario:
    - Find a user with an unpaid fine (accounttype = 'FU' in db) on a returned item.
    - Checkout the same item for the same user.
    - CHOICE 1: - Force it being late by modifying the date_due to last year.
                - Run fines.pl to generate another fine.
    - CHOICE 2: - Create directly another accountlines entry in the DB with the same info
                - You may change the amounts to help differentiate
    - Go to the returns.pl screen, check the Forgive checkbox.  Return the item.
    - Look at the DB, the first line got paid.  Not the latest one.
    - APPLY THE PATCH
    - Redo scenario, this time the latest entry (by the timestamp column) is paid.
---
 C4/Circulation.pm | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/C4/Circulation.pm b/C4/Circulation.pm
index 87812a3..b5daadd 100644
--- a/C4/Circulation.pm
+++ b/C4/Circulation.pm
@@ -2320,7 +2320,8 @@ sub _FixOverduesOnReturn {
                 accounttype => 'FU',
                 accounttype => 'O',
             ],
-        }
+        },
+        { order_by => \'timestamp desc' }
     )->next();
     return 0 unless $accountline;    # no warning, there's just nothing to fix
 
-- 
2.7.4