From 42147c595a098435865767ae3074871e1363fdec Mon Sep 17 00:00:00 2001
From: Baptiste Wojtkowski <bski@laposte.net>
Date: Thu, 18 Jul 2024 15:05:58 +0200
Subject: [PATCH] Bug 18657: Add the possibility to add lost items to the
 report

Currently the tool reports:
Incorrect not-for-loan values
Wrong place
Checked out

We should have:
Optional display of items w/o problems
Display of missing/lost items now marked found

TEST PLAN
1 - Mark an item as lost
2 - Go to tools->inventory
3 - Fill in the barcode list with the barcode of the item, notice there
  is one checkbox in "Additional options"
4 - Submit -> there should be no result
5 - Apply patches and redo 1,2&3
6 - Notice there are now 3 checkboxes in "Additional options"
7 - Click on "Add lost items to the report"
8 - Submit -> there is now one line in the report with problem "Item was lost and is now marked as found"
9 - Go to tools->inventory
10 - Click on "Add lost items to the report"
11 - Submit -> there is no line in the report
12 - Go to tools->inventory
13 - Click on "Add items without problem to the report"
11 - Submit -> there is now line in the report with an empty problem

Signed-off-by: Mathieu Saby <mathsabypro@gmail.com>
---
 .../prog/en/modules/tools/inventory.tt         |  6 ++++++
 tools/inventory.pl                             | 18 ++++++++++++++++++
 2 files changed, 24 insertions(+)

diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/inventory.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/inventory.tt
index 60c4c8e4ac..79665ed4ee 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/inventory.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/inventory.tt
@@ -252,6 +252,10 @@
                                 <label for="CSVexport">Export to CSV file: </label>
                                 <input type="checkbox" name="CSVexport" id="CSVexport" />
                             </li>
+                            <li>
+                                <label for="ReportLostItems">Add losts items to the report: </label>
+                                <input type="checkbox" name="ReportLostItems" id="ReportLostItems" />
+                            </li>
                         </ol>
                     </fieldset>
 
@@ -368,6 +372,8 @@
                                                     <span>No barcode</span><br/>
                                                 [% ELSIF problem.key == 'out_of_order' %]
                                                     <span>Item may be shelved out of order</span><br/>
+                                                [% ELSIF problem.key == 'lost' %]
+                                                    <span>Item was lost and is now marked as found</span><br/>
                                                 [% END %]
                                             [% END %]
                                         </td>
diff --git a/tools/inventory.pl b/tools/inventory.pl
index e38d7c4b65..473a334abe 100755
--- a/tools/inventory.pl
+++ b/tools/inventory.pl
@@ -123,6 +123,11 @@ for my $authvfield (@$statuses) {
     }
 }
 
+my $report_lost_items;
+if ( defined $input->param('ReportLostItems') && $input->param('ReportLostItems') eq 'on' ) {
+    $report_lost_items = "1";
+}
+
 # if there's a list of not for loans types selected use it rather than
 # the full set.
 @notforloans = @{$staton->{'items.notforloan'}} if defined $staton->{'items.notforloan'} and scalar @{$staton->{'items.notforloan'}} > 0;
@@ -162,6 +167,7 @@ my $results = {};
 my @scanned_items;
 my @errorloop;
 my $moddatecount = 0;
+my @lost_items;
 if ( $op eq 'cud-inventory'
     && ( ( $uploadbarcodes && length($uploadbarcodes) > 0 ) || ( $barcodelist && length($barcodelist) > 0 ) ) )
 {
@@ -225,6 +231,9 @@ if ( $op eq 'cud-inventory'
                 next;
             }
             # Modify date last seen for scanned items, remove lost status
+            if ( $item->unblessed->{itemlost} ) {
+                push @lost_items, $barcode;
+            }
             $item->set({ itemlost => 0, datelastseen => $date_dt })->store;
             my $item_unblessed = $item->unblessed;
             $moddatecount++;
@@ -335,6 +344,12 @@ for ( my $i = 0; $i < @scanned_items; $i++ ) {
         $item->{problems}->{wrongplace} = 1;
         additemtoresults( $item, $results );
     }
+
+    # Report a lost item if asked
+    if ( @lost_items && ( scalar grep { $_ eq $item->{barcode} } @lost_items ) && $report_lost_items ) {
+        $item->{problems}->{lost} = 1;
+        additemtoresults( $item, $results );
+    }
 }
 
 # Compare barcodes with inventory list, report no_barcode and not_scanned.
@@ -418,8 +433,11 @@ if (defined $input->param('CSVexport') && $input->param('CSVexport') eq 'on'){
                 $errstr .= "checked out,";
             } elsif( $key eq 'out_of_order' ) {
                 $errstr .= "shelved out of order,";
+            } elsif ( $key eq 'lost' ) {
+                $errstr .= "item was lost";
             }
         }
+
         $errstr =~ s/,$//;
         push @line, $errstr;
         $csv->combine(@line);
-- 
2.39.2