From bf33ddd1ffb7f06e4aa6cc0f99ac851e07cd3184 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Fri, 12 Jan 2024 14:19:33 +0100 Subject: [PATCH] Bug 35788: Remove Koha::Template::Plugin::Biblio::BookingsCount We need a new method filter_by_future in Koha::Bookings. Additionally this TT plugin's method was not covered by tests. Neither is the whole Koha::Booking[s] objects btw. Test plan: Confirm that the number of bookings is displayed in the "Bookings" tab in the left menu of the bibliographic record detail page. --- 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 diff --git a/Koha/Bookings.pm b/Koha/Bookings.pm index 44bc032b02d..03e59d92658 100644 --- a/Koha/Bookings.pm +++ b/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 diff --git a/Koha/Template/Plugin/Biblio.pm b/Koha/Template/Plugin/Biblio.pm index 74fa7d42a1d..b8f38a61f4e 100644 --- a/Koha/Template/Plugin/Biblio.pm +++ b/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; 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 c657cf5edad..bac3667c3db 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.BookingsCount( biblio_object_id ) | html %]) + Bookings ([% biblio.bookings.filter_by_future.count | html %])
  • [% END %] diff --git a/t/db_dependent/Koha/Bookings.t b/t/db_dependent/Koha/Bookings.t new file mode 100755 index 00000000000..19e2e8e37c8 --- /dev/null +++ b/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; +}; -- 2.43.0