From 1239b93d2f4f0b2931b21e9d80ed87ae91c03ac5 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 Content-Type: text/plain; charset=utf-8 Signed-off-by: Marcel de Rooy --- 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 d5ca370..5b3ce08 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); @@ -258,10 +259,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 2498b86..dce43ad 100644 --- a/t/db_dependent/Koha/Biblios.t +++ b/t/db_dependent/Koha/Biblios.t @@ -23,6 +23,7 @@ use Test::More tests => 1; use C4::Reserves; +use Koha::DateUtils qw( dt_from_string ); use Koha::Biblios; use Koha::Patrons; use t::lib::TestBuilder; @@ -43,13 +44,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; + }; $schema->storage->txn_rollback; -- 2.1.4