From f1d8754aaef18dadb60005998efc36bfa0aecbc1 Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Fri, 31 May 2024 11:13:23 +0000 Subject: [PATCH] Bug 36428: Add Bookings->filter_by_active and use it for display Currently the bookings tab on a biblio details and patron details use 'filter_by_future' which lists upcoming bookings. Libraries would like to see upcoming, and active bookings in these cases, and we should add a filter for bookings that have not ended. NOTE: This removes the only uses of filter_by_future, but I preserve this for Martin's decision as the creator of the bookings module To test: 1 - Make an item bookable from the items tab on a record details 2 - Return to details view and place a booking 3 - Note sidebar says "Bookings (1)" 4 - Make the booking current from the DB: UPDATE bookings SET start_date=NOW() WHERE biblio_id={biblionumber}; 5 - Reload the page 6 - Note the count is now "Bookings (0)" 7 - View the patron's details page - note "Bookings (0)" and none listed 8 - Apply patch 9 - Reload biblio details, note Bookings(1) 10 - Reload patron details, note Bookings(1) and booking is listed 11 - End the booking: UPDATE bookings SET end_date=NOW() WHERE biblio_id={biblionumber}; 12 - Confirm booking no longer listed on biblio or patron details Signed-off-by: Sam Lau --- Koha/Bookings.pm | 13 +++ .../prog/en/includes/biblio-view-menu.inc | 2 +- .../prog/en/includes/patron-detail-tabs.inc | 2 +- t/db_dependent/Koha/Bookings.t | 85 ++++++++++++++++++- 4 files changed, 96 insertions(+), 6 deletions(-) diff --git a/Koha/Bookings.pm b/Koha/Bookings.pm index b9bea5072c..d4c89b4278 100644 --- a/Koha/Bookings.pm +++ b/Koha/Bookings.pm @@ -45,6 +45,19 @@ sub filter_by_future { return $self->search( { start_date => { '>' => \'NOW()' } } ); } +=head3 filter_by_active + + $bookings->filter_by_active; + +Will return the bookings that have not ended. + +=cut + +sub filter_by_active { + my ($self) = @_; + return $self->search( { end_date => { '>=' => \'NOW()' } } ); +} + =head2 Internal Methods =head3 _type diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/biblio-view-menu.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/biblio-view-menu.inc index 74008ba117..3e5625eff0 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/includes/biblio-view-menu.inc +++ b/koha-tmpl/intranet-tmpl/prog/en/includes/biblio-view-menu.inc @@ -56,7 +56,7 @@ [%- ELSE -%]
  • [%- END -%] - Bookings ([% biblio.bookings.filter_by_future.count | html %]) + Bookings ([% biblio.bookings.filter_by_active.count | html %])
  • [% END %] diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/patron-detail-tabs.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/patron-detail-tabs.inc index b220d7952a..6ee596767d 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/includes/patron-detail-tabs.inc +++ b/koha-tmpl/intranet-tmpl/prog/en/includes/patron-detail-tabs.inc @@ -30,7 +30,7 @@ Holds ([% holds_count || 0 | html %]) [% END %] [% WRAPPER tab_item tabname="bookings" %] - [% SET bookings_count = patron.bookings.filter_by_future.count %] + [% SET bookings_count = patron.bookings.filter_by_active.count %] Bookings ([% bookings_count || 0 | html %]) [% END %] [% END %] diff --git a/t/db_dependent/Koha/Bookings.t b/t/db_dependent/Koha/Bookings.t index ca409a6123..9d29e4c2e8 100755 --- a/t/db_dependent/Koha/Bookings.t +++ b/t/db_dependent/Koha/Bookings.t @@ -19,7 +19,7 @@ use Modern::Perl; -use Test::More tests => 1; +use Test::More tests => 2; use Koha::Bookings; use Koha::Database; @@ -37,9 +37,7 @@ subtest 'filter_by_future' => sub { $schema->storage->txn_begin; - my $biblio = $builder->build_sample_biblio; - my $start_0 = dt_from_string->subtract( days => 2 )->truncate( to => 'day' ); - my $end_0 = dt_from_string->add( days => 4 )->truncate( to => 'day' ); + my $biblio = $builder->build_sample_biblio; $builder->build_object( { class => 'Koha::Bookings', @@ -90,3 +88,82 @@ subtest 'filter_by_future' => sub { $schema->storage->txn_rollback; }; + +subtest 'filter_by_active' => sub { + + plan tests => 5; + + $schema->storage->txn_begin; + + my $biblio = $builder->build_sample_biblio; + my $start_ago = dt_from_string->subtract( hours => 1 ); + my $start_hour = dt_from_string->add( hours => 1 ); + my $start_day = dt_from_string->add( days => 1 ); + my $end_ago = dt_from_string->subtract( minutes => 1 ); + my $end_hour = dt_from_string->add( hours => 1 ); + my $end_day = dt_from_string->add( days => 1 ); + $builder->build_object( + { + class => 'Koha::Bookings', + value => { + biblio_id => $biblio->biblionumber, + start_date => $start_ago, + end_date => $end_hour + } + } + ); + is( $biblio->bookings->filter_by_active->count, 1, 'Booking started in past, ending in future is counted' ); + + $builder->build_object( + { + class => 'Koha::Bookings', + value => { + biblio_id => $biblio->biblionumber, + start_date => $start_ago, + end_date => $end_ago + } + } + ); + is( $biblio->bookings->filter_by_active->count, 1, 'Booking started in past, ended now is not counted' ); + + $builder->build_object( + { + class => 'Koha::Bookings', + value => { + biblio_id => $biblio->biblionumber, + start_date => $start_hour, + end_date => $end_hour + } + } + ); + is( $biblio->bookings->filter_by_active->count, 2, 'Booking starting soon, ending soon is still counted' ); + + $builder->build_object( + { + class => 'Koha::Bookings', + value => { + biblio_id => $biblio->biblionumber, + start_date => $start_day, + end_date => $end_day + } + } + ); + is( $biblio->bookings->filter_by_active->count, 3, 'Booking starting tomorrow, ending tomorrow is counted' ); + + $builder->build_object( + { + class => 'Koha::Bookings', + value => { + biblio_id => $biblio->biblionumber, + start_date => $start_day, + end_date => $end_ago + } + } + ); + is( + $biblio->bookings->filter_by_active->count, 3, + 'EDGE CASE: Booking starting in future, already ended is not counted - should be impossible, but good check' + ); + + $schema->storage->txn_rollback; +}; -- 2.39.2