From 5ff44bcfa0da6a81f3a6ca2c91314dc745bbe9a8 Mon Sep 17 00:00:00 2001
From: Tomas Cohen Arazi <tomascohen@theke.io>
Date: Fri, 3 Jun 2016 11:26:42 -0300
Subject: [PATCH] Bug 14048: Hook new rules into C4::Circulation

This patch makes AddIssue and AddReturn use the new implementation

The behaviour should respect the specification.

Sponsored-by: DoverNet
Sponsored-by: South-East Kansas Library System
Sponsored-by: SWITCH Library Consortium
---
 C4/Circulation.pm | 21 +++++++++++++++++----
 1 file changed, 17 insertions(+), 4 deletions(-)

diff --git a/C4/Circulation.pm b/C4/Circulation.pm
index 50e6c10..a1dc440 100644
--- a/C4/Circulation.pm
+++ b/C4/Circulation.pm
@@ -53,6 +53,7 @@ use Koha::Patron::Debarments;
 use Koha::Database;
 use Koha::Libraries;
 use Koha::Holds;
+use Koha::RefundLostItemFeeRules;
 use Carp;
 use List::MoreUtils qw( uniq );
 use Scalar::Util qw( looks_like_number );
@@ -1437,7 +1438,12 @@ sub AddIssue {
 
         ## If item was lost, it has now been found, reverse any list item charges if necessary.
         if ( $item->{'itemlost'} ) {
-            if ( C4::Context->preference('RefundLostItemFeeOnReturn' ) ) {
+            if ( Koha::RefundLostItemFeeRules->should_refund(
+                    current_branch => C4::Context->userenv->{ branch },
+                    patron_branch  => $borrower->{ branchcode },
+                    item_home_branch => $item->{ homebranch },
+                    item_holding_branch => $item->{ holdingbranch }
+                 ) ) {
                 _FixAccountForLostAndReturned( $item->{'itemnumber'}, undef, $item->{'barcode'} );
             }
         }
@@ -2110,9 +2116,16 @@ sub AddReturn {
     if ( $item->{'itemlost'} ) {
         $messages->{'WasLost'} = 1;
 
-        if ( C4::Context->preference('RefundLostItemFeeOnReturn' ) ) {
-            _FixAccountForLostAndReturned($item->{'itemnumber'}, $borrowernumber, $barcode);    # can tolerate undef $borrowernumber
-            $messages->{'LostItemFeeRefunded'} = 1;
+        if ( $item->{'itemlost'} ) {
+            if ( Koha::RefundLostItemFeeRules->should_refund(
+                    current_branch => C4::Context->userenv->{ branch },
+                    patron_branch  => $borrower->{ branchcode },
+                    item_home_branch => $item->{ homebranch },
+                    item_holding_branch => $item->{ holdingbranch }
+                 ) ) {
+                _FixAccountForLostAndReturned( $item->{'itemnumber'}, $borrowernumber, $barcode );
+                $messages->{'LostItemFeeRefunded'} = 1;
+            }
         }
     }
 
-- 
2.8.4