The 'Any item' option in the bookings modal is currently not correctly calculating maximum available booking window correctly and we have older code left behind that attempted to allow any item at a biblio level without taking into account item type limitations. This bug acts to correct that by implementing an optimized algorithm for picking the item at booking time to try and maximize the pool of available items for future bookings.
Created attachment 183211 [details] [review] Bug 40134: Implement dynamic item assignment with smart window maximization This patch implements smart window maximization for "any item of itemtype X" bookings using dynamic item pool reduction. The core principle is "never re-add items to pool" - once an item is removed from the available pool because it becomes unavailable, it's never re-added even if it becomes available again later. This ensures optimal resource allocation and maximum booking windows. Key features: - Dynamic item pool reduction algorithm in isDateInMaximumWindow() - Smart itemtype availability calculation in isDateDisabledForItemtype() - Optimal item selection for booking submission in selectOptimalItem() - Future availability calculation to choose best items - Comprehensive JSDoc documentation with detailed examples The algorithm works by: 1. Starting with items available on the selected start date 2. Walking through each day from start to target end date 3. Removing items from pool when they become unavailable 4. Never re-adding items even if they become available again later 5. Disabling dates when no items remain in pool This maximizes booking windows while preventing conflicts by being conservative about item availability. Test plan: 1. Apply patch and restart services 2. Navigate to a biblio detail page with bookable items of multiple types 3. Click "Place booking" and select a patron and pickup location 4. Select an itemtype (not a specific item) to enable "any item" mode 5. Select a start date - verify end date options are maximized based on available items across the booking period 6. Test with complex booking scenarios where items have overlapping bookings 7. Verify that the algorithm correctly reduces the item pool as items become unavailable and extends booking windows as far as possible 8. Confirm that items which become available again later in the period are not re-added to the pool (key algorithm principle) 9. Submit booking and verify optimal item is selected automatically