From 131a87567bcf7b649f2ca2eb727645c8550443b1 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Tue, 10 Oct 2023 17:09:38 +0100 Subject: [PATCH] Bug 29002: Prevent booking a checked out item This patch updates the bookings logic to prevent placing a booking on an item that is checked out to start prior to it's due date. Test plan 1) Follow the previous test plans 2) Add a checkout of an item 3) Attempt to place a booking and note that the item is not available to be booked until after it's due date. Signed-off-by: Martin Renvoize Signed-off-by: Janet McGowan Signed-off-by: Caroline Cyr La Rose Signed-off-by: Laurence Rault Signed-off-by: Kyle M Hall Signed-off-by: Tomas Cohen Arazi --- Koha/Biblio.pm | 16 +++++++++-- Koha/Item.pm | 4 +++ .../prog/js/place_booking_modal.js | 27 ++++++++++++++++--- 3 files changed, 42 insertions(+), 5 deletions(-) diff --git a/Koha/Biblio.pm b/Koha/Biblio.pm index fea47ea986..fdadcc3811 100644 --- a/Koha/Biblio.pm +++ b/Koha/Biblio.pm @@ -267,6 +267,10 @@ sub check_booking { ? $existing_bookings->search( { booking_id => { '!=' => $booking_id } } ) ->count : $existing_bookings->count; + + my $checkouts = $self->current_checkouts->search( { date_due => { '>=' => $dtf->format_datetime($start_date) } } ); + $booked_count += $checkouts->count; + return ( ( $total_bookable - $booked_count ) > 0 ) ? 1 : 0; } @@ -302,9 +306,17 @@ sub assign_item_for_booking { ] ); + my $checkouts = $self->current_checkouts->search( { date_due => { '>=' => $dtf->format_datetime($start_date) } } ); + my $bookable_items = $self->bookable_items->search( - { itemnumber => { '-not_in' => $existing_bookings->_resultset->get_column('item_id')->as_query } }, - { rows => 1 } + { + itemnumber => [ + '-and' => + { '-not_in' => $existing_bookings->_resultset->get_column('item_id')->as_query }, + { '-not_in' => $checkouts->_resultset->get_column('itemnumber')->as_query } + ] + }, + { rows => 1 } ); return $bookable_items->single->itemnumber; } diff --git a/Koha/Item.pm b/Koha/Item.pm index fe72b43292..ea2b8e39d2 100644 --- a/Koha/Item.pm +++ b/Koha/Item.pm @@ -544,6 +544,10 @@ sub check_booking { my $end_date = dt_from_string( $params->{end_date} ); my $booking_id = $params->{booking_id}; + if ( my $checkout = $self->checkout ) { + return 0 if ( $start_date <= dt_from_string( $checkout->date_due ) ); + } + my $dtf = Koha::Database->new->schema->storage->datetime_parser; my $existing_bookings = $self->bookings( [ diff --git a/koha-tmpl/intranet-tmpl/prog/js/place_booking_modal.js b/koha-tmpl/intranet-tmpl/prog/js/place_booking_modal.js index cf480d9796..f457aa2179 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/place_booking_modal.js +++ b/koha-tmpl/intranet-tmpl/prog/js/place_booking_modal.js @@ -1,5 +1,5 @@ let dataFetched = false; -let bookable_items, bookings, booking_id, booking_item_id, booking_patron; +let bookable_items, bookings, checkouts, booking_id, booking_item_id, booking_patron; $('#placeBookingModal').on('show.bs.modal', function(e) { @@ -138,14 +138,35 @@ $('#placeBookingModal').on('show.bs.modal', function(e) { dataType: 'json', type: 'GET' }); + + // Fetch list of current checkouts + let checkoutsFetch = $.ajax({ + url: '/api/v1/biblios/' + biblionumber + '/checkouts?_per_page=-1', + dataType: 'json', + type: 'GET' + }); // Update item select2 and period flatpickr - $.when(itemsFetch, bookingsFetch).then( - function(itemsFetch,bookingsFetch){ + $.when(itemsFetch, bookingsFetch, checkoutsFetch).then( + function(itemsFetch,bookingsFetch, checkoutsFetch){ // Set variables bookable_items = itemsFetch[0]; bookings = bookingsFetch[0]; + checkouts = checkoutsFetch[0]; + + // Merge current checkouts into bookings + for (checkout of checkouts) { + let booking = { + biblio_id: biblionumber, + booking_id: null, + end_date: checkout.due_date, + item_id: checkout.item_id, + patron_id: checkout.patron_id, + start_date: new Date().toISOString(), + }; + bookings.unshift(booking); + } // Item select2 $("#booking_item_id").select2({ -- 2.39.5