From 94210ae47111210739daf12d801d8b5b472b0932 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Fri, 25 Oct 2024 12:04:24 +0100 Subject: [PATCH] Bug 38175: (QA follow-up) Add cancelled handling to find_booking We need to also exclude cancelled bookings from the find_booking routine used at checkout. Signed-off-by: Martin Renvoize Signed-off-by: Katrin Fischer --- Koha/Item.pm | 59 ++++++++++++++++++++++---------------- t/db_dependent/Koha/Item.t | 17 ++++++++++- 2 files changed, 50 insertions(+), 26 deletions(-) diff --git a/Koha/Item.pm b/Koha/Item.pm index f82b9b65a5..090fea5514 100644 --- a/Koha/Item.pm +++ b/Koha/Item.pm @@ -605,9 +605,9 @@ implementation. sub find_booking { my ( $self, $params ) = @_; - my $checkout_date = $params->{checkout_date}; - my $due_date = $params->{due_date}; - my $biblio = $self->biblio; + my $start_date = $params->{checkout_date}; + my $end_date = $params->{due_date}; + my $biblio = $self->biblio; my $rule = Koha::CirculationRules->get_effective_rule( { @@ -617,33 +617,41 @@ sub find_booking { } ); my $preparation_period = $rule ? $rule->rule_value : 0; - $due_date = $due_date->clone->add( days => $preparation_period ); + $end_date = $end_date->clone->add( days => $preparation_period ); my $dtf = Koha::Database->new->schema->storage->datetime_parser; my $bookings = $biblio->bookings( - [ - # Proposed checkout starts during booked period - start_date => { - '-between' => [ - $dtf->format_datetime($checkout_date), - $dtf->format_datetime($due_date) - ] - }, + { + '-and' => [ + { + '-or' => [ - # Proposed checkout is due during booked period - end_date => { - '-between' => [ - $dtf->format_datetime($checkout_date), - $dtf->format_datetime($due_date) - ] - }, + # Proposed checkout starts during booked period + start_date => { + '-between' => [ + $dtf->format_datetime($start_date), + $dtf->format_datetime($end_date) + ] + }, - # Proposed checkout would contain the booked period - { - start_date => { '<' => $dtf->format_datetime($checkout_date) }, - end_date => { '>' => $dtf->format_datetime($due_date) } - } - ], + # Proposed checkout is due during booked period + end_date => { + '-between' => [ + $dtf->format_datetime($start_date), + $dtf->format_datetime($end_date) + ] + }, + + # Proposed checkout would contain the booked period + { + start_date => { '<' => $dtf->format_datetime($start_date) }, + end_date => { '>' => $dtf->format_datetime($end_date) } + } + ] + }, + { status => { '-not_in' => [ 'cancelled', 'completed' ] } } + ] + }, { order_by => { '-asc' => 'start_date' } } ); @@ -668,6 +676,7 @@ sub find_booking { # Booking for another item elsif ( defined( $booking->item_id ) ) { + # Due for another booking, remove from pool delete $loanable_items->{ $booking->item_id }; next; diff --git a/t/db_dependent/Koha/Item.t b/t/db_dependent/Koha/Item.t index 5c3be2b91c..4138505031 100755 --- a/t/db_dependent/Koha/Item.t +++ b/t/db_dependent/Koha/Item.t @@ -2897,7 +2897,7 @@ subtest 'bookings' => sub { }; subtest 'find_booking' => sub { - plan tests => 7; + plan tests => 8; $schema->storage->txn_begin; @@ -3004,6 +3004,21 @@ subtest 'find_booking' => sub { "Koha::Item->find_booking returns the current booking not a future one" ); + # Cancelled booking + $booking2->status('cancelled')->store(); + $found_booking = $item->find_booking( + { + checkout_date => dt_from_string(), + due_date => dt_from_string()->add( days => 7 ), + } + ); + + is( + $found_booking, + undef, + "Koha::Item->find_booking returns undefined when the current booking is cancelled and the future booking is out of range" + ); + subtest "Preparation period handling" => sub { plan tests => 3; -- 2.39.5