From 9427cfc1be97e277983a0547e32b8933db23189b Mon Sep 17 00:00:00 2001
From: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Date: Thu, 25 Nov 2021 12:10:57 +0000
Subject: [PATCH] Bug 28854: Improve lost details display for bundle items

This patch adds the return claim details to the bundle item status
display on the catalogue details page.

Test plan
---
 Koha/Checkouts/ReturnClaims.pm                 |  2 +-
 Koha/Item.pm                                   | 17 +++++++++++++++++
 api/v1/swagger/definitions/item.yaml           |  8 ++++++++
 api/v1/swagger/paths/items.yaml                |  3 +++
 .../prog/en/modules/catalogue/detail.tt        | 18 +++++++++++++-----
 5 files changed, 42 insertions(+), 6 deletions(-)

diff --git a/Koha/Checkouts/ReturnClaims.pm b/Koha/Checkouts/ReturnClaims.pm
index c1c2b3bd44..1174cdd1a2 100644
--- a/Koha/Checkouts/ReturnClaims.pm
+++ b/Koha/Checkouts/ReturnClaims.pm
@@ -60,7 +60,7 @@ sub resolved {
     return Koha::Checkouts::ReturnClaims->_new_from_dbic( $results );
 }
 
-=head3 type
+=head3 _type
 
 =cut
 
diff --git a/Koha/Item.pm b/Koha/Item.pm
index 550189caa1..dac6f46254 100644
--- a/Koha/Item.pm
+++ b/Koha/Item.pm
@@ -418,6 +418,23 @@ sub return_claims {
     return Koha::Checkouts::ReturnClaims->_new_from_dbic( $claims_rs );
 }
 
+=head3 return_claim
+
+  my $return_claim = $item->return_claim;
+
+Returns the most recent unresolved return_claims associated with this item
+
+=cut
+
+sub return_claim {
+    my ( $self ) = @_;
+    my $claims_rs =
+      $self->_result->return_claims->search( { resolution => undef },
+        { order_by => { '-desc' => 'created_on' } } )->single;
+    return unless $claims_rs;
+    return Koha::Checkouts::ReturnClaim->_new_from_dbic( $claims_rs );
+}
+
 =head3 holds
 
 my $holds = $item->holds();
diff --git a/api/v1/swagger/definitions/item.yaml b/api/v1/swagger/definitions/item.yaml
index b525c16484..22df7d4aad 100644
--- a/api/v1/swagger/definitions/item.yaml
+++ b/api/v1/swagger/definitions/item.yaml
@@ -212,6 +212,14 @@ properties:
   exclude_from_local_holds_priority:
     type: boolean
     description: Exclude this item from local holds priority.
+  return_claims:
+    type: array
+    description: An array of all return claims associated with this item
+  return_claim:
+    type:
+      - object
+      - "null"
+    description: A return claims object if one exists that's unresolved
 additionalProperties: false
 required:
   - item_id
diff --git a/api/v1/swagger/paths/items.yaml b/api/v1/swagger/paths/items.yaml
index d1b38595d0..0b5ef2029c 100644
--- a/api/v1/swagger/paths/items.yaml
+++ b/api/v1/swagger/paths/items.yaml
@@ -201,6 +201,9 @@
     x-koha-embed:
       - biblio
       - checkout
+      - return_claims
+      - return_claim
+      - return_claim.patron
 /items/{item_id}/bundled_items/{bundled_item_id}:
   delete:
     x-mojo-to: Items#remove_from_bundle
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/detail.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/detail.tt
index 8b403c2f9a..e813b0563a 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/detail.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/detail.tt
@@ -1366,6 +1366,7 @@ Note that permanent location is a code, and location may be an authval.
     [% Asset.js("lib/jquery/plugins/jquery.dataTables.columnFilter.js") | $raw %]
     [% INCLUDE 'columns_settings.inc' %]
     [% INCLUDE 'js-date-format.inc' %]
+    [% INCLUDE 'js-patron-format.inc' %]
     [% Asset.js("js/browser.js") | $raw %]
     [% Asset.js("js/table_filters.js") | $raw %]
     <script>
@@ -1399,7 +1400,8 @@ Note that permanent location is a code, and location may be an authval.
                     },
                     "header_filter": false,
                     "embed": [
-                        "biblio"
+                        "biblio",
+                        "return_claim.patron"
                     ],
                     "order": [[ 1, "asc" ]],
                     "columnDefs": [ {
@@ -1459,15 +1461,22 @@ Note that permanent location is a code, and location may be an authval.
                             "orderable": true,
                         },
                         {
-                            "data": "lost_status:last_seen_date",
+                            "data": "lost_status:last_seen_date:return_claim.patron",
                             "title": "Status",
                             "searchable": false,
                             "orderable": true,
                             "render": function(data, type, row, meta) {
                                 if ( row.lost_status == bundle_lost_value ) {
-                                    return "Last seen: " + row.last_seen_date;
+                                    let out = '<span class="lost">' + _("Last seen") + ': ' + row.last_seen_date + '</span>';
+                                    if ( row.return_claim ) {
+                                        out = out + '<span class="claims_return">' + _("Claims returned by") + ': ' + $patron_to_html( row.return_claim.patron, { display_cardnumber: false, url: true } ) + '</span>';
+                                    }
+                                    return out;
                                 }
-                                return "Present";
+                                else if ( row.lost_status !== 0 ) {
+                                    return '<span class="lost">' + _("Lost") + ': ' + row.lost_status + '</span>';
+                                }
+                                return '<span class="available">' + _("Present") + '</span>';
                             }
                         },
                         {
@@ -1482,7 +1491,6 @@ Note that permanent location is a code, and location may be an authval.
                         }
                     ]
                 }, bundle_columns, 1);
-
                 $(".tbundle").on("click", ".remove", function(){
                     var bundle_table = $(this).closest('table');
                     var host_itemnumber = bundle_table.data('itemnumber');
-- 
2.20.1