From c11955bd78a8175e92b0d71e16928571cab77f0b Mon Sep 17 00:00:00 2001
From: Julian Maurice <julian.maurice@biblibre.com>
Date: Tue, 30 Nov 2021 14:04:21 +0100
Subject: [PATCH] Bug 29606: Include items from host record in XML for XSLT
 processing

If EasyAnalyticalRecords is enabled, items from host records should be
included in the XML. Otherwise, OPAC search results show no items.

Test plan:
0. Do not apply the patch yet
1. Enable syspref EasyAnalyticalRecords and set OPACXSLTResultsDisplay
   to 'default'
2. Create a biblio record with title "parent" and create an item with a
   barcode for this biblio
3. Create a biblio record with title "child" with no items. On the
   biblio detail page, in the toolbar, click "Edit -> Link to host
   record" and enter the barcode for the item you created at step 2
4. Duplicate this biblio record, to be sure you will have at least two
   results with the search "child" (and not be redirected to the detail
   page)
5. Make sure your search index (zebra or elasticsearch) is up to date
6. Go to OPAC and search "child".
   You should see two results, no items.
   Click on one of them. On the detail page you should see one item (the
   one from the host record)
7. Apply the patch
8. Go to OPAC and search "child".
   You should now see one item (the same item) for each result.

Signed-off-by: David Nind <david@davidnind.com>
---
 C4/XSLT.pm | 28 +++++++++++++++++++---------
 1 file changed, 19 insertions(+), 9 deletions(-)

diff --git a/C4/XSLT.pm b/C4/XSLT.pm
index 0fb7be0d6c..762de0992f 100644
--- a/C4/XSLT.pm
+++ b/C4/XSLT.pm
@@ -263,7 +263,7 @@ sub XSLTParse4Display {
     if ( $xslsyspref eq "OPACXSLTDetailsDisplay" || $xslsyspref eq "XSLTDetailsDisplay" || $xslsyspref eq "XSLTResultsDisplay" ) {
         $itemsxml = ""; #We don't use XSLT for items display on these pages
     } else {
-        $itemsxml = buildKohaItemsNamespace($biblionumber, $hidden_items, $items_rs);
+        $itemsxml = buildKohaItemsNamespace($biblionumber, $hidden_items, $items_rs, $record);
     }
     my $xmlrecord = $record->as_xml(C4::Context->preference('marcflavour'));
 
@@ -324,20 +324,30 @@ Is only used in this module currently.
 =cut
 
 sub buildKohaItemsNamespace {
-    my ($biblionumber, $hidden_items, $items_rs) = @_;
+    my ($biblionumber, $hidden_items, $items_rs, $record) = @_;
 
     $hidden_items ||= [];
 
-    my $query = {};
-    $query = { 'me.itemnumber' => { not_in => $hidden_items } }
-      if $hidden_items;
-
     unless ( $items_rs && ref($items_rs) eq 'Koha::Items' ) {
-        $query->{'me.biblionumber'} = $biblionumber;
-        $items_rs = Koha::Items->new;
+        # adding linked items that belong to host records
+        my @linkeditemnumbers;
+        if ( C4::Context->preference('EasyAnalyticalRecords') ) {
+            my $analyticsfield = C4::Context->preference("marcflavour") eq 'UNIMARC' ? '461' : '773';
+            @linkeditemnumbers = map { $_->subfield('9') } $record->field($analyticsfield);
+        }
+
+        $items_rs = Koha::Items->search(
+            [
+                'me.biblionumber' => $biblionumber,
+                'me.itemnumber' => { in => \@linkeditemnumbers },
+            ]
+        );
     }
 
-    my $items = $items_rs->search( $query, { prefetch => [ 'branchtransfers', 'reserves' ] } );
+    my $items = $items_rs->search(
+        { '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' } ) };
-- 
2.30.2