Bug 40134 - Fix and optimise 'Any item' functionality of bookings
Summary: Fix and optimise 'Any item' functionality of bookings
Status: Needs Signoff
Alias: None
Product: Koha
Classification: Unclassified
Component: Circulation (show other bugs)
Version: unspecified
Hardware: All All
: P5 - low normal
Assignee: Martin Renvoize (ashimema)
QA Contact: Testopia
URL:
Keywords:
Depends on: 39916
Blocks:
  Show dependency treegraph
 
Reported: 2025-06-12 14:45 UTC by Martin Renvoize (ashimema)
Modified: 2025-06-12 14:56 UTC (History)
2 users (show)

See Also:
GIT URL:
Change sponsored?: ---
Patch complexity: ---
Documentation contact:
Documentation submission:
Text to go in the release notes:
Version(s) released in:
Circulation function: Bookings


Attachments
Bug 40134: Implement dynamic item assignment with smart window maximization (45.99 KB, patch)
2025-06-12 14:54 UTC, Martin Renvoize (ashimema)
Details | Diff | Splinter Review

Note You need to log in before you can comment on or make changes to this bug.
Description Martin Renvoize (ashimema) 2025-06-12 14:45:25 UTC
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.
Comment 1 Martin Renvoize (ashimema) 2025-06-12 14:54:44 UTC
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