From 24114187f1b5b377173dc8db6b6066033dced431 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Tue, 22 Oct 2024 17:09:39 +0100 Subject: [PATCH] Bug 38175: (QA follow-up) Ensure we handle cancelled in clash detection We need to ensure we handle the new status tracking cancelled/completed in our clash detection code too. Signed-off-by: Martin Renvoize Signed-off-by: Katrin Fischer --- Koha/Biblio.pm | 43 +++++++++++++++++++++--------------- Koha/Bookings.pm | 4 ++-- Koha/Item.pm | 43 +++++++++++++++++++++--------------- t/db_dependent/Koha/Biblio.t | 16 +++++++++++++- t/db_dependent/Koha/Item.t | 16 +++++++++++++- 5 files changed, 82 insertions(+), 40 deletions(-) diff --git a/Koha/Biblio.pm b/Koha/Biblio.pm index 8829a3e63c..67242a3308 100644 --- a/Koha/Biblio.pm +++ b/Koha/Biblio.pm @@ -296,24 +296,31 @@ sub check_booking { my $dtf = Koha::Database->new->schema->storage->datetime_parser; my $existing_bookings = $self->bookings( - [ - start_date => { - '-between' => [ - $dtf->format_datetime($start_date), - $dtf->format_datetime($end_date) - ] - }, - end_date => { - '-between' => [ - $dtf->format_datetime($start_date), - $dtf->format_datetime($end_date) - ] - }, - { - start_date => { '<' => $dtf->format_datetime($start_date) }, - end_date => { '>' => $dtf->format_datetime($end_date) } - } - ] + { + '-and' => [ + { + '-or' => [ + start_date => { + '-between' => [ + $dtf->format_datetime($start_date), + $dtf->format_datetime($end_date) + ] + }, + end_date => { + '-between' => [ + $dtf->format_datetime($start_date), + $dtf->format_datetime($end_date) + ] + }, + { + start_date => { '<' => $dtf->format_datetime($start_date) }, + end_date => { '>' => $dtf->format_datetime($end_date) } + } + ] + }, + { status => { '-not_in' => [ 'cancelled', 'completed' ] } } + ] + } ); my $booked_count = diff --git a/Koha/Bookings.pm b/Koha/Bookings.pm index d63e063647..ca89e8eacb 100644 --- a/Koha/Bookings.pm +++ b/Koha/Bookings.pm @@ -44,8 +44,8 @@ sub filter_by_active { my ($self) = @_; return $self->search( { - end_date => { '>=' => \'NOW()' }, - status => { q{!=} => [ -and => qw(cancelled completed) ] } + end_date => { '>=' => \'NOW()' }, + status => { '-not_in' => [ 'cancelled', 'completed' ] } } ); } diff --git a/Koha/Item.pm b/Koha/Item.pm index 286a0a7d95..f82b9b65a5 100644 --- a/Koha/Item.pm +++ b/Koha/Item.pm @@ -707,24 +707,31 @@ sub check_booking { my $dtf = Koha::Database->new->schema->storage->datetime_parser; my $existing_bookings = $self->bookings( - [ - start_date => { - '-between' => [ - $dtf->format_datetime($start_date), - $dtf->format_datetime($end_date) - ] - }, - end_date => { - '-between' => [ - $dtf->format_datetime($start_date), - $dtf->format_datetime($end_date) - ] - }, - { - start_date => { '<' => $dtf->format_datetime($start_date) }, - end_date => { '>' => $dtf->format_datetime($end_date) } - } - ] + { + '-and' => [ + { + '-or' => [ + start_date => { + '-between' => [ + $dtf->format_datetime($start_date), + $dtf->format_datetime($end_date) + ] + }, + end_date => { + '-between' => [ + $dtf->format_datetime($start_date), + $dtf->format_datetime($end_date) + ] + }, + { + start_date => { '<' => $dtf->format_datetime($start_date) }, + end_date => { '>' => $dtf->format_datetime($end_date) } + } + ] + }, + { status => { '-not_in' => [ 'cancelled', 'completed' ] } } + ] + } ); my $bookings_count = diff --git a/t/db_dependent/Koha/Biblio.t b/t/db_dependent/Koha/Biblio.t index f768f8a6f7..0fde70957f 100755 --- a/t/db_dependent/Koha/Biblio.t +++ b/t/db_dependent/Koha/Biblio.t @@ -1851,7 +1851,7 @@ sub host_record { } subtest 'check_booking tests' => sub { - plan tests => 4; + plan tests => 5; $schema->storage->txn_begin; @@ -1952,5 +1952,19 @@ subtest 'check_booking tests' => sub { "Koha::Biblio->check_booking returns true if we pass the booking_id of one of the bookings that we would conflict with" ); + # Cancelled booking + $current_bookings[0]->update( { status => 'cancelled' } ); + $can_book = $biblio->check_booking( + { + start_date => dt_from_string(), + end_date => dt_from_string()->add( days => 7 ), + } + ); + is( + $can_book, + 1, + "Koha::Item->check_booking takes account of cancelled status in bookings check" + ); + $schema->storage->txn_rollback; }; diff --git a/t/db_dependent/Koha/Item.t b/t/db_dependent/Koha/Item.t index e10d4f83f7..5c3be2b91c 100755 --- a/t/db_dependent/Koha/Item.t +++ b/t/db_dependent/Koha/Item.t @@ -3056,7 +3056,7 @@ subtest 'find_booking' => sub { }; subtest 'check_booking tests' => sub { - plan tests => 5; + plan tests => 6; $schema->storage->txn_begin; @@ -3145,6 +3145,20 @@ subtest 'check_booking tests' => sub { "Koha::Item->check_booking returns true if we pass the booking_id that would conflict" ); + # Cancelled booking + $booking2->update( { status => 'cancelled' } ); + $can_book = $item->check_booking( + { + start_date => dt_from_string(), + end_date => dt_from_string()->add( days => 7 ), + } + ); + is( + $can_book, + 1, + "Koha::Item->check_booking returns true if the conflicting booking is cancelled" + ); + $booking2->delete(); # Future booking -- 2.39.5