From 3e791138a80a455d6486e4a5d383eb6dc71c5b7b Mon Sep 17 00:00:00 2001
From: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Date: Mon, 4 Nov 2019 16:29:23 +0100
Subject: [PATCH] Bug 23414: Easy readability and simplify logic in
 buildKohaItemsNamespace

This patch improves true/false logic and avoid autovivication.
Also note that xml_escape already deals with empty string.

An unecessary call to GetReserveStatus is removed.

Signed-off-by: Liz Rea <wizzyrea@gmail.com>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
---
 C4/XSLT.pm | 26 +++++++++++++-------------
 1 file changed, 13 insertions(+), 13 deletions(-)

diff --git a/C4/XSLT.pm b/C4/XSLT.pm
index 9b435cb1fe..9ed7dc3b54 100644
--- a/C4/XSLT.pm
+++ b/C4/XSLT.pm
@@ -292,10 +292,14 @@ Is only used in this module currently.
 sub buildKohaItemsNamespace {
     my ($biblionumber, $hidden_items) = @_;
 
-    my $search_params;
-    $search_params->{'me.biblionumber'} = $biblionumber;
-    $search_params->{'me.itemnumber'} = { not_in => $hidden_items } if $hidden_items;
-    my @items = Koha::Items->search($search_params,{prefetch=>['branchtransfers','reserves']});
+    $hidden_items ||= [];
+    my @items = Koha::Items->search(
+        {
+            'me.biblionumber' => $biblionumber,
+            'me.itemnumber'   => { not_in => $hidden_items }
+        },
+        { prefetch => [ 'branchtransfers', 'reserves' ] }
+    );
 
     my $shelflocations =
       { map { $_->{authorised_value} => $_->{opac_description} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => "", kohafield => 'items.location' } ) };
@@ -305,14 +309,10 @@ sub buildKohaItemsNamespace {
     my %branches = map { $_->branchcode => $_->branchname } Koha::Libraries->search({}, { order_by => 'branchname' });
 
     my $itemtypes = { map { $_->{itemtype} => $_ } @{ Koha::ItemTypes->search->unblessed } };
-    my $location = "";
-    my $ccode = "";
     my $xml = '';
     for my $item (@items) {
         my $status;
 
-        my $reservestatus = C4::Reserves::GetReserveStatus( $item->itemnumber );
-
         if ($item->has_pending_hold) {
             $status = 'Pending hold';
         }
@@ -343,12 +343,12 @@ sub buildKohaItemsNamespace {
         else {
             $status = "available";
         }
-        my $homebranch = $item->homebranch? xml_escape($branches{$item->homebranch}):'';
-        my $holdingbranch = $item->holdingbranch? xml_escape($branches{$item->holdingbranch}):'';
-        $location = $item->location? xml_escape($shelflocations->{$item->location}||$item->location):'';
-        $ccode = $item->ccode? xml_escape($ccodes->{$item->ccode}||$item->ccode):'';
+        my $homebranch     = xml_escape($branches{$item->homebranch});
+        my $holdingbranch  = xml_escape($branches{$item->holdingbranch});
+        my $location       = xml_escape($item->location && exists $shelflocations->{$item->location} ? $shelflocations->{$item->location} : $item->location);
+        my $ccode          = xml_escape($item->ccode    && exists $ccodes->{$item->ccode}            ? $ccodes->{$item->ccode}            : $item->ccode);
         my $itemcallnumber = xml_escape($item->itemcallnumber);
-        my $stocknumber = $item->stocknumber? xml_escape($item->stocknumber):'';
+        my $stocknumber    = xml_escape($item->stocknumber);
         $xml .=
             "<item>"
           . "<homebranch>$homebranch</homebranch>"
-- 
2.11.0