From 349d86a267b1626ae26bfc6d2f45403e5f481b15 Mon Sep 17 00:00:00 2001
From: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Date: Wed, 24 May 2023 10:05:50 +0200
Subject: [PATCH] Bug 33568: Restore bundle items
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Owen Leonard <oleonard@myacpl.org>

Signed-off-by: Laurence Rault <laurence.rault@biblibre.com>
Signed-off-by: Emily Lamancusa <emily.lamancusa@montgomerycountymd.gov>
Signed-off-by: Tomás Cohen Arazi <tomascohen@theke.io>
---
 Koha/Item.pm                                  | 27 +++++++++++++++++++
 api/v1/swagger/definitions/item.yaml          | 10 +++++++
 api/v1/swagger/paths/biblios.yaml             |  2 ++
 .../prog/en/modules/catalogue/detail.tt       | 23 +++++++++-------
 4 files changed, 53 insertions(+), 9 deletions(-)

diff --git a/Koha/Item.pm b/Koha/Item.pm
index 42897cfb1c0..ea99889e696 100644
--- a/Koha/Item.pm
+++ b/Koha/Item.pm
@@ -1888,6 +1888,33 @@ sub bundle_items {
     return Koha::Items->_new_from_dbic($rs);
 }
 
+
+=head3 bundle_items_not_lost
+
+  my $bundle_items = $item->bundle_items_not_lost;
+
+Returns the items associated with this bundle that are not lost
+
+=cut
+
+sub bundle_items_not_lost {
+    my ($self) = @_;
+    return $self->bundle_items->search( { itemlost => { '!=' => 0 } } );
+}
+
+=head3 bundle_items_lost
+
+  my $bundle_items = $item->bundle_items_lost;
+
+Returns the items associated with this bundle that are lost
+
+=cut
+
+sub bundle_items_lost {
+    my ($self) = @_;
+    return $self->bundle_items->search( { itemlost => 0 } );
+}
+
 =head3 is_bundle
 
   my $is_bundle = $item->is_bundle;
diff --git a/api/v1/swagger/definitions/item.yaml b/api/v1/swagger/definitions/item.yaml
index d3a035b514e..cb7894a899c 100644
--- a/api/v1/swagger/definitions/item.yaml
+++ b/api/v1/swagger/definitions/item.yaml
@@ -270,6 +270,16 @@ properties:
     type:
       - object
       - "null"
+  bundle_items_lost_count:
+    type:
+      - integer
+      - "null"
+    description: Number of lost items in the bundle
+  bundle_items_not_lost_count:
+    type:
+      - integer
+      - "null"
+    description: Number of items not lost in the bundle
   course_item:
     type:
       - object
diff --git a/api/v1/swagger/paths/biblios.yaml b/api/v1/swagger/paths/biblios.yaml
index 8e666251237..a0f83fd9c23 100644
--- a/api/v1/swagger/paths/biblios.yaml
+++ b/api/v1/swagger/paths/biblios.yaml
@@ -437,6 +437,8 @@
             - item_type
             - in_bundle
             - bundle_host
+            - bundle_items_lost+count
+            - bundle_items_not_lost+count
             - course_item.course_reserves.course
             - cover_image_ids
             - item_group_item.item_group.description
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 b4632ecda80..287493fef8d 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/detail.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/detail.tt
@@ -1857,14 +1857,15 @@
 
                 [% IF bundlesEnabled %]
                 // Add event listener for opening and closing bundle details
-                $('#' + table_name + ' tbody').on('click', 'button.details-control', function () {
+                $('#' + tab_id + '_table tbody').on('click', 'button.details-control', function () {
                     var button = $(this);
                     var tr = button.closest('tr');
                     var dTable = button.closest('table').DataTable({ 'retrieve': true });
 
-                    var itemnumber = tr.data('itemnumber');
-                    var duedate = tr.data('duedate');
-                    var row = dTable.row( tr );
+                    let row = dTable.row( tr );
+                    let data = row.data();
+                    let itemnumber = data.item_id;
+                    let duedate = (data.checkout&&data.checkout.due_date) || null;
 
                     if ( row.child.isShown() ) {
                         // This row is already open - close it
@@ -2186,7 +2187,9 @@
         [% IF Koha.Preference('UseRecalls') %]
             embed.push('recall', 'recall+strings', 'recall.patron')
         [% END %]
-        embed.push('in_bundle', 'bundle_host');
+        [% IF bundlesEnabled %]
+            embed.push('in_bundle', 'bundle_host', 'bundle_items_lost+count', 'bundle_items_not_lost+count');
+        [% END %]
         [% IF Koha.Preference('UseCourseReserves') %]
             embed.push('course_item.course_reserves.course');
         [% END %]
@@ -2460,9 +2463,11 @@
                             nodes += '<span class="restricted">(%s)</span>'.format(escape_str(av_restricted.get(row.restricted_status.toString())));
                         }
 
-                        if ( row.in_bundle ) {
-                            nodes += '<span class="bundled">%s</span>'.format(_("In bundle: %s").format($biblio_to_html(row.bundle_host.biblio, { link: true })));
-                        }
+                        [% IF bundlesEnabled %]
+                            if ( row.in_bundle ) {
+                                nodes += '<span class="bundled">%s</span>'.format(_("In bundle: %s").format($biblio_to_html(row.bundle_host.biblio, { link: true })));
+                            }
+                        [% END %]
                         return nodes;
                     }
                 },
@@ -2659,7 +2664,7 @@
                         }
                         [% IF bundlesEnabled %]
                             // FIXME How do we handle that correctly?
-                            //nodes += '<button class="btn btn-default btn-xs details-control"><i class="fa fa-folder"></i> Manage bundle (%s|%s)</button>'.format(escape_str(row.bundled), escape_str(row.bundled_lost));
+                            nodes += '<button class="btn btn-default btn-xs details-control"><i class="fa fa-folder"></i> Manage bundle (%s|%s)</button>'.format(escape_str(row.bundle_items_not_lost_count), escape_str(row.bundle_items_lost_count));
                         [% END %]
 
                         return nodes;
-- 
2.34.1