From 1017fb222a65b8df7836bc215004dbe961c3e9e9 Mon Sep 17 00:00:00 2001
From: Nick Clemens <nick@bywatersolutions.com>
Date: Wed, 12 Dec 2018 13:30:11 +0000
Subject: [PATCH] Bug 14576: Update to use get_yaml_pref_hash

---
 C4/Circulation.pm | 42 ++++++++++++++++++------------------------
 1 file changed, 18 insertions(+), 24 deletions(-)

diff --git a/C4/Circulation.pm b/C4/Circulation.pm
index e8a84af107..de52be1aa8 100644
--- a/C4/Circulation.pm
+++ b/C4/Circulation.pm
@@ -59,6 +59,7 @@ use Koha::RefundLostItemFeeRules;
 use Koha::Account::Lines;
 use Koha::Account::Offsets;
 use Koha::Config::SysPrefs;
+use Koha::Util::SystemPreferences;
 use Carp;
 use List::MoreUtils qw( uniq any );
 use Scalar::Util qw( looks_like_number );
@@ -1851,32 +1852,25 @@ sub AddReturn {
     my $borrowernumber = $patron ? $patron->borrowernumber : undef;    # we don't know if we had a borrower or not
     my $patron_unblessed = $patron ? $patron->unblessed : {};
 
-    my $yaml_loc = C4::Context->preference('UpdateItemLocationOnCheckin');
-    if ($yaml_loc) {
-        $yaml_loc = "$yaml_loc\n\n";  # YAML is strict on ending \n. Surplus does not hurt
-        my $rules;
-        eval { $rules = YAML::Load($yaml_loc); };
-        if ($@) {
-            warn "Unable to parse UpdateItemLocationOnCheckin syspref : $@";
+    my $update_loc_rules = get_yaml_pref_hash('UpdateItemLocationOnCheckin');
+    map { $update_loc_rules->{$_} = $update_loc_rules->{$_}[0] } keys %$update_loc_rules; #We can only move to one location so we flatten the arrays
+    if ($update_loc_rules) {
+        if (defined $update_loc_rules->{_ALL_}) {
+            if ($update_loc_rules->{_ALL_} eq '_PERM_') { $update_loc_rules->{_ALL_} = $item->{permanent_location}; }
+            if ($update_loc_rules->{_ALL_} eq '_BLANK_') { $update_loc_rules->{_ALL_} = ''; }
+            if ( $item->{location} ne $update_loc_rules->{_ALL_}) {
+                $messages->{'ItemLocationUpdated'} = { from => $item->{location}, to => $update_loc_rules->{_ALL_} };
+                ModItem( { location => $update_loc_rules->{_ALL_} }, undef, $itemnumber );
+            }
         }
         else {
-            if (defined $rules->{_ALL_}) {
-                if ($rules->{_ALL_} eq '_PERM_') { $rules->{_ALL_} = $item->{permanent_location}; }
-                if ($rules->{_ALL_} eq '_BLANK_') { $rules->{_ALL_} = ''; }
-                if ( $item->{location} ne $rules->{_ALL_}) {
-                    $messages->{'ItemLocationUpdated'} = { from => $item->{location}, to => $rules->{_ALL_} };
-                    ModItem( { location => $rules->{_ALL_} }, undef, $itemnumber );
-                }
-            }
-            else {
-                foreach my $key ( keys %$rules ) {
-                    if ( $rules->{$key} eq '_PERM_' ) { $rules->{$key} = $item->{permanent_location}; }
-                    if ( $rules->{$key} eq '_BLANK_') { $rules->{$key} = '' ;}
-                    if ( ($item->{location} eq $key && $item->{location} ne $rules->{$key}) || ($key eq '_BLANK_' && $item->{location} eq '' && $rules->{$key} ne '') ) {
-                        $messages->{'ItemLocationUpdated'} = { from => $item->{location}, to => $rules->{$key} };
-                        ModItem( { location => $rules->{$key} }, undef, $itemnumber );
-                        last;
-                    }
+            foreach my $key ( keys %$update_loc_rules ) {
+                if ( $update_loc_rules->{$key} eq '_PERM_' ) { $update_loc_rules->{$key} = $item->{permanent_location}; }
+                if ( $update_loc_rules->{$key} eq '_BLANK_') { $update_loc_rules->{$key} = '' ;}
+                if ( ($item->{location} eq $key && $item->{location} ne $update_loc_rules->{$key}) || ($key eq '_BLANK_' && $item->{location} eq '' && $update_loc_rules->{$key} ne '') ) {
+                    $messages->{'ItemLocationUpdated'} = { from => $item->{location}, to => $update_loc_rules->{$key} };
+                    ModItem( { location => $update_loc_rules->{$key} }, undef, $itemnumber );
+                    last;
                 }
             }
         }
-- 
2.11.0