From 17f1f4a4389427d67a3c85911757b703cdfc16b8 Mon Sep 17 00:00:00 2001
From: Nick Clemens <nick@bywatersolutions.com>
Date: Thu, 19 Dec 2019 05:29:47 +0000
Subject: [PATCH] Bug 3426: Allow for multiple fields in itemcallnumber syspref

Note that we change both cataloguing/additem.pl and C4/Items->PrepareItemrecordfordisplay

I can find no code that uses callnumber from the C4/Items sub, except for the itemrecorddisplay script
which is not called with an itemnumber from Koha and should be deprecated for REST or ILSDI or OAI (imho)

To test:
 1 - Define itemcallnumber syspref as "082ab,092ab,9520,245a"
 2 - Find a record with no items
 3 - Ensure it has no 082 field, but an 092 field
 4 - Go to add an item - itemcallnumber is empty
 5 - Apply patch
 6 - Go to add item, itemcallnumber should be the 092ab fields
 7 - Delete the 092 field
 8 - Go to add item, itemcallnumber should be the 245a
 9 - Edit the callnumber to be "testing" and save item
10 - For should now show itemcallnumber="testing" as default
11 - Browse to http://localhost:8081/cgi-bin/koha/services/itemrecorddisplay.pl?itemnumber=## subbing the correct itemnumber
12 - Ensure the callnumber is defaulting to testing
13 - delete the item you created
14 - browse to URL above - callnumber should now be 245 again
15 - Add an 092 field to record and ensure it is now default callnumber
16 - Add an 082 field, it should now be default
---
 C4/Items.pm            |  9 +++++----
 cataloguing/additem.pl | 13 ++++++-------
 2 files changed, 11 insertions(+), 11 deletions(-)

diff --git a/C4/Items.pm b/C4/Items.pm
index b81fb0aa17..7abfb48b46 100644
--- a/C4/Items.pm
+++ b/C4/Items.pm
@@ -2404,10 +2404,11 @@ sub PrepareItemrecordDisplay {
 
                 # search for itemcallnumber if applicable
                 if ( $tagslib->{$tag}->{$subfield}->{kohafield} eq 'items.itemcallnumber'
-                    && C4::Context->preference('itemcallnumber') ) {
-                    my $CNtag      = substr( C4::Context->preference('itemcallnumber'), 0, 3 );
-                    my $CNsubfield = substr( C4::Context->preference('itemcallnumber'), 3, 1 );
-                    if ( $itemrecord and my $field = $itemrecord->field($CNtag) ) {
+                    && C4::Context->preference('itemcallnumber') && $itemrecord) {
+                    foreach my $itemcn_pref (split(/,/,C4::Context->preference('itemcallnumber'))){
+                        my $CNtag      = substr( $itemcn_pref, 0, 3 );
+                        next unless my $field = $itemrecord->field($CNtag);
+                        my $CNsubfield = substr( $itemcn_pref, 3, 1 );
                         $defaultvalue = $field->subfield($CNsubfield);
                     }
                 }
diff --git a/cataloguing/additem.pl b/cataloguing/additem.pl
index 2c371def28..6f12f8b5cb 100755
--- a/cataloguing/additem.pl
+++ b/cataloguing/additem.pl
@@ -154,11 +154,12 @@ sub generate_subfield_form {
 
         my $pref_itemcallnumber = C4::Context->preference('itemcallnumber');
         if (!$value && $subfieldlib->{kohafield} eq 'items.itemcallnumber' && $pref_itemcallnumber) {
-            my $CNtag       = substr($pref_itemcallnumber, 0, 3);
-            my $CNsubfield  = substr($pref_itemcallnumber, 3, 1);
-            my $CNsubfield2 = substr($pref_itemcallnumber, 4, 1);
-            my $temp2 = $temp->field($CNtag);
-            if ($temp2) {
+            foreach my $pref_itemcallnumber_part (split(/,/, $pref_itemcallnumber)){
+                my $CNtag       = substr($pref_itemcallnumber_part, 0, 3);
+                my $temp2 = $temp->field($CNtag);
+                next unless $temp2;
+                my $CNsubfield  = substr($pref_itemcallnumber_part, 3, 1);
+                my $CNsubfield2 = substr($pref_itemcallnumber_part, 4, 1);
                 $value = join ' ', $temp2->subfield($CNsubfield) || q{}, $temp2->subfield($CNsubfield2) || q{};
                 #remove any trailing space incase one subfield is used
                 $value =~ s/^\s+|\s+$//g;
@@ -883,8 +884,6 @@ foreach my $subfield_code (sort keys(%witness)) {
 my @loop_data =();
 my $i=0;
 
-my $pref_itemcallnumber = C4::Context->preference('itemcallnumber');
-
 my $branch = $input->param('branch') || C4::Context->userenv->{branch};
 my $libraries = Koha::Libraries->search({}, { order_by => ['branchname'] })->unblessed;# build once ahead of time, instead of multiple times later.
 for my $library ( @$libraries ) {
-- 
2.11.0