From 2c2558ae2719101a66e63756194e2e00dcc3f32b Mon Sep 17 00:00:00 2001
From: David Cook <dcook@prosentient.com.au>
Date: Tue, 13 Jun 2023 04:44:41 +0000
Subject: [PATCH] Bug 33989: Only look up authorized value descriptions for
 items in results

This patch changes the inventory tool so that it only populates the
authorized value descriptions for items that show up in the results.
That is, items that will be viewed by a human.

This change saves a lot of time by not performing unnecessary
database lookups.

Test plan:

0. Apply patch and koha-plack --restart kohadev
1. Go to create a SQL report from SQL
/cgi-bin/koha/reports/guided_reports.pl?phase=Create%20report%20from%20SQL
2. Save a report with the following SQL:
SELECT barcode FROM items where barcode <> '';
3. Run the report
4. Download as CSV
5. Edit the CSV and remove the "barcode" heading

6. Go to edit item for barcode 3999900000001
/cgi-bin/koha/cataloguing/additem.pl?op=edititem&biblionumber=1&itemnumber=1
7. Add "Damaged status" of "Damaged"
8. Click "Save changes"

9. koha-mysql kohadev
10. update items set notforloan = 9 where barcode = 3999900000001;

11. Go to inventory tool
http://localhost:8081/cgi-bin/koha/tools/inventory.pl
12. Upload barcode file via "Choose file"
13. Uncheck "Compare barcodes list to results"
14. Open F12 tools
15. Click "Submit"
16. Click "OK" on confirm box

17. Note that the inventory job takes around 30 seconds to run
instead of 60 seconds
17b. The exact figures may vary. If you compare with before patch
inventory runs, you'll be saving roughly 18 seconds.
18. Note that "Damaged" appears in the "Damaged" column, which
demonstrates that the authorized value lookup was completed for
the item in the result list
---
 tools/inventory.pl | 22 ++++++++++++----------
 1 file changed, 12 insertions(+), 10 deletions(-)

diff --git a/tools/inventory.pl b/tools/inventory.pl
index ace4e7cb7b..6038931f62 100755
--- a/tools/inventory.pl
+++ b/tools/inventory.pl
@@ -303,16 +303,6 @@ for ( my $i = 0; $i < @scanned_items; $i++ ) {
     my $item = $scanned_items[$i];
 
     $item->{notforloancode} = $item->{notforloan}; # save for later use
-    my $fc = $item->{'frameworkcode'} || '';
-
-    # Populating with authorised values description
-    foreach my $field (qw/ location notforloan itemlost damaged withdrawn /) {
-        my $av = Koha::AuthorisedValues->get_description_by_koha_field(
-            { frameworkcode => $fc, kohafield => "items.$field", authorised_value => $item->{$field} } );
-        if ( $av and defined $item->{$field} and defined $av->{lib} ) {
-            $item->{$field} = $av->{lib};
-        }
-    }
 
     # If we have scanned items with a non-matching notforloan value
     if( none { $item->{'notforloancode'} eq $_ } @notforloans ) {
@@ -453,6 +443,18 @@ output_html_with_http_headers $input, $cookie, $template->output;
 sub additemtoresults {
     my ( $item, $results ) = @_;
     my $itemno = $item->{itemnumber};
+
+    my $fc = $item->{'frameworkcode'} || '';
+
+    # Populating with authorised values description
+    foreach my $field (qw/ location notforloan itemlost damaged withdrawn /) {
+        my $av = Koha::AuthorisedValues->get_description_by_koha_field(
+            { frameworkcode => $fc, kohafield => "items.$field", authorised_value => $item->{$field} } );
+        if ( $av and defined $item->{$field} and defined $av->{lib} ) {
+            $item->{$field} = $av->{lib};
+        }
+    }
+
     # since the script appends to $item, we can just overwrite the hash entry
     $results->{$itemno} = $item;
 }
-- 
2.30.2