From 47acc780c0be614898ff4348e85529213707e88b Mon Sep 17 00:00:00 2001
From: Thibaud Guillot <thibaud.guillot@biblibre.com>
Date: Fri, 17 May 2024 15:50:09 +0200
Subject: [PATCH] Bug 36271: (follow-up) Add switch for join table according to
 item-level_itypes syspref

---
 Koha/Booking.pm | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)

diff --git a/Koha/Booking.pm b/Koha/Booking.pm
index e7d4f0e96cc..5c3461f9303 100644
--- a/Koha/Booking.pm
+++ b/Koha/Booking.pm
@@ -91,13 +91,25 @@ sub can_be_booked_in_advance {
     my $bookings_per_item_count = Koha::Bookings->search( { patron_id => $patron->borrowernumber, item_id => $item->itemnumber } )->count();
     return { status => 'tooManyBookings', limit => $bookings_per_item, rule => 'bookings_per_item' } if defined($bookings_per_item) && $bookings_per_item <= $bookings_per_item_count;
 
-    my $querycount = q{
-        SELECT count(*) AS count
-            FROM bookings AS b
-            LEFT JOIN biblioitems AS bi ON (b.biblio_id=bi.biblionumber)
-            WHERE b.patron_id = ?
-            AND bi.itemtype = ?
-            };
+
+    my $querycount;
+    if (C4::Context->preference('item-level_itypes')) {
+        $querycount = q{
+            SELECT count(*) AS count
+                FROM bookings AS b
+                LEFT JOIN items AS i ON (b.item_id=i.itemnumber)
+                WHERE b.patron_id = ?
+                AND i.itype = ?
+                };
+    } else {
+        $querycount = q{
+            SELECT count(*) AS count
+                FROM bookings AS b
+                LEFT JOIN biblioitems AS bi ON (b.biblio_id=bi.biblionumber)
+                WHERE b.patron_id = ?
+                AND bi.itemtype = ?
+                };
+    }
 
     my $sthcount = $dbh->prepare($querycount);
     $sthcount->execute( $patron->borrowernumber, $item->effective_itemtype );
-- 
2.30.2