From 42d110a774b65549241e191882f87a89fc312329 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Wed, 7 Dec 2016 16:38:38 +0100 Subject: [PATCH] Bug 17736: Add the Koha::Biblio->holds_placed_before_today method Signed-off-by: Marcel de Rooy Signed-off-by: Martin Renvoize Signed-off-by: Kyle M Hall --- Koha/Biblio.pm | 23 ++++++++++++++++++++--- t/db_dependent/Koha/Biblios.t | 15 +++++++++++++-- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/Koha/Biblio.pm b/Koha/Biblio.pm index d71ed8f325..f666eaa553 100644 --- a/Koha/Biblio.pm +++ b/Koha/Biblio.pm @@ -24,6 +24,7 @@ use Carp; use C4::Biblio qw( GetRecordValue GetMarcBiblio GetFrameworkCode ); use Koha::Database; +use Koha::DateUtils qw( dt_from_string ); use base qw(Koha::Object); @@ -259,10 +260,26 @@ return the current holds placed on this record =cut sub holds { - my ( $self ) = @_; + my ( $self, $params, $attributes ) = @_; + $attributes->{order_by} = 'priority' unless exists $attributes->{order_by}; + my $hold_rs = $self->_result->reserves->search( $params, $attributes ); + return Koha::Holds->_new_from_dbic($hold_rs); +} + +=head3 holds_placed_before_today + +my $holds = $biblio->holds_placed_before_today - my $holds_rs = $self->_result->reserves; - return Koha::Holds->_new_from_dbic( $holds_rs ); +Return the holds placed on this bibliographic record. +It does not include future holds. + +=cut + +sub holds_placed_before_today { + my ($self) = @_; + my $dtf = Koha::Database->new->schema->storage->datetime_parser; + return $self->holds( + { reservedate => { '<=' => $dtf->format_date(dt_from_string) } } ); } =head3 biblioitem diff --git a/t/db_dependent/Koha/Biblios.t b/t/db_dependent/Koha/Biblios.t index e16868f5bc..5f28df66ca 100644 --- a/t/db_dependent/Koha/Biblios.t +++ b/t/db_dependent/Koha/Biblios.t @@ -23,6 +23,7 @@ use Test::More tests => 2; use C4::Reserves; +use Koha::DateUtils qw( dt_from_string ); use Koha::Biblios; use Koha::Patrons; use Koha::Subscriptions; @@ -44,13 +45,23 @@ my $biblioitem = $schema->resultset('Biblioitem')->new( } )->insert(); -subtest 'holds' => sub { - plan tests => 3; +subtest 'holds + holds_placed_before_today' => sub { + plan tests => 5; C4::Reserves::AddReserve( $patron->branchcode, $patron->borrowernumber, $biblio->biblionumber ); my $holds = $biblio->holds; is( ref($holds), 'Koha::Holds', '->holds should return a Koha::Holds object' ); is( $holds->count, 1, '->holds should only return 1 hold' ); is( $holds->next->borrowernumber, $patron->borrowernumber, '->holds should return the correct hold' ); + $holds->delete; + + # Add a hold in the future + C4::Reserves::AddReserve( $patron->branchcode, $patron->borrowernumber, $biblio->biblionumber, undef, undef, dt_from_string->add( days => 2 ) ); + $holds = $biblio->holds; + is( $holds->count, 1, '->holds should return future holds' ); + $holds = $biblio->holds_placed_before_today; + is( $holds->count, 0, '->holds_placed_before_today should not return future holds' ); + $holds->delete; + }; subtest 'subscriptions' => sub { -- 2.39.5