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; +};