From cf0512d3f3a7b4afb4043c4665396698e0304d14 Mon Sep 17 00:00:00 2001
From: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Date: Mon, 10 Aug 2020 16:51:11 +0200
Subject: [PATCH] Bug 18501: Auto refund if mark as found from cataloguing

If an item is marked as found (ie. not itemlost) we need to refund the
patron.

Test plan:
Mark an item as lost to create a fee for the patron
Mark it as found from the cataloguing module and confirm that the patron
is refunded
---
 catalogue/updateitem.pl | 3 +++
 cataloguing/additem.pl  | 7 +++++--
 2 files changed, 8 insertions(+), 2 deletions(-)

diff --git a/catalogue/updateitem.pl b/catalogue/updateitem.pl
index f8295a0314..43f076c3d2 100755
--- a/catalogue/updateitem.pl
+++ b/catalogue/updateitem.pl
@@ -68,6 +68,9 @@ elsif ( $op eq "set_public_note" ) { # i.e., itemnotes parameter passed from for
         $item->itemnotes($itemnotes);
     }
 } elsif ( $op eq "set_lost" && $itemlost ne $item_data_hashref->{'itemlost'}) {
+    $item->set_found
+        if !$itemlost && $item->itemlost && $item->itemlost ge '1';
+
     $item->itemlost($itemlost);
 } elsif ( $op eq "set_withdrawn" && $withdrawn ne $item_data_hashref->{'withdrawn'}) {
     $item->withdrawn($withdrawn);
diff --git a/cataloguing/additem.pl b/cataloguing/additem.pl
index 001d9d8c15..6677848605 100755
--- a/cataloguing/additem.pl
+++ b/cataloguing/additem.pl
@@ -747,8 +747,11 @@ if ($op eq "additem") {
         $itemnumber = q{};
         my $olditemlost = $item->itemlost;
         my $newitemlost = $newitem->{itemlost};
-        LostItem( $item->itemnumber, 'additem' )
-            if $newitemlost && $newitemlost ge '1' && !$olditemlost;
+        if ( $newitemlost && $newitemlost ge '1' && !$olditemlost ) {
+            LostItem( $item->itemnumber, 'additem' )
+        } elsif ( !$newitemlost && $olditemlost && $olditemlost ge '1' ) {
+            $item->set_found;
+        }
     }
     $nextop="additem";
 } elsif ($op eq "delinkitem"){
-- 
2.20.1