@@ -, +, @@
Koha::Template::Plugin::Biblio::BookingsCount
---
Koha/Bookings.pm | 20 +++++
Koha/Template/Plugin/Biblio.pm | 12 ---
.../prog/en/includes/biblio-view-menu.inc | 2 +-
t/db_dependent/Koha/Bookings.t | 79 +++++++++++++++++++
4 files changed, 100 insertions(+), 13 deletions(-)
create mode 100755 t/db_dependent/Koha/Bookings.t
--- a/Koha/Bookings.pm
+++ a/Koha/Bookings.pm
@@ -20,6 +20,7 @@ package Koha::Bookings;
use Modern::Perl;
use Koha::Database;
+use Koha::DateUtils qw( dt_from_string );
use Koha::Booking;
use base qw(Koha::Objects);
@@ -34,6 +35,25 @@ Koha::Bookings - Koha Booking object set class
=cut
+=head3 filter_by_future
+
+ $bookings->filter_by_future;
+
+ Will return the bookings starting from now.
+
+=cut
+
+sub filter_by_future {
+ my ($self) = @_;
+ my $now = dt_from_string;
+ my $dtf = Koha::Database->new->schema->storage->datetime_parser;
+ return $self->search( { start_date => { '>' => $dtf->format_datetime($now) } } );
+}
+
+=head2 Internal Methods
+
+=cut
+
=head3 type
=cut
--- a/Koha/Template/Plugin/Biblio.pm
+++ a/Koha/Template/Plugin/Biblio.pm
@@ -29,8 +29,6 @@ use Koha::Patrons;
use Koha::ArticleRequests;
use Koha::Recalls;
-use Koha::DateUtils qw(dt_from_string);
-
# Do not use HoldsCount, it is deprecated and will be removed in a future release.
sub HoldsCount {
my ( $self, $biblionumber ) = @_;
@@ -72,14 +70,4 @@ sub RecallsCount {
return $recalls->count;
}
-sub BookingsCount {
- my ( $self, $biblionumber ) = @_;
-
- my $biblio = Koha::Biblios->find($biblionumber);
-
- my $now = dt_from_string;
- my $dtf = Koha::Database->new->schema->storage->datetime_parser;
- return $biblio->bookings->search( { start_date => { '>' => $dtf->format_datetime($now) } } )->count;
-}
-
1;
--- a/koha-tmpl/intranet-tmpl/prog/en/includes/biblio-view-menu.inc
+++ a/koha-tmpl/intranet-tmpl/prog/en/includes/biblio-view-menu.inc
@@ -56,7 +56,7 @@
[%- ELSE -%]
[%- END -%]
- Bookings ([% Biblio.BookingsCount( biblio_object_id ) | html %])
+ Bookings ([% biblio.bookings.filter_by_future.count | html %])
[% END %]
--- a/t/db_dependent/Koha/Bookings.t
+++ a/t/db_dependent/Koha/Bookings.t
@@ -0,0 +1,79 @@
+#!/usr/bin/perl
+
+# Copyright 2024 Koha Development team
+#
+# This file is part of Koha
+#
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see .
+
+use Modern::Perl;
+
+use Test::More tests => 1;
+
+use Koha::Bookings;
+use Koha::Database;
+use Koha::DateUtils qw( dt_from_string );
+
+use t::lib::TestBuilder;
+
+my $schema = Koha::Database->new->schema;
+
+my $builder = t::lib::TestBuilder->new;
+
+subtest 'filter_by_future' => sub {
+
+ plan tests => 1;
+
+ $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' );
+ $builder->build_object(
+ {
+ class => 'Koha::Bookings',
+ value => {
+ biblio_id => $biblio->biblionumber,
+ start_date => dt_from_string->subtract( days => 1 )->truncate( to => 'day' ),
+ end_date => undef
+ }
+ }
+ );
+
+ $builder->build_object(
+ {
+ class => 'Koha::Bookings',
+ value => {
+ biblio_id => $biblio->biblionumber,
+ start_date => dt_from_string->add( days => 1 )->truncate( to => 'day' ),
+ end_date => undef
+ }
+ }
+ );
+
+ $builder->build_object(
+ {
+ class => 'Koha::Bookings',
+ value => {
+ biblio_id => $biblio->biblionumber,
+ start_date => dt_from_string->add( days => 2 )->truncate( to => 'day' ),
+ end_date => undef
+ }
+ }
+ );
+
+ is( $biblio->bookings->filter_by_future->count, 2, 'There should have 2 bookings starting after now' );
+
+ $schema->storage->txn_rollback;
+};
--