@@ -, +, @@ method --- Koha/Biblio.pm | 23 ++++++++++++++++++++--- t/db_dependent/Koha/Biblios.t | 15 +++++++++++++-- 2 files changed, 33 insertions(+), 5 deletions(-) --- a/Koha/Biblio.pm +++ a/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 --- a/t/db_dependent/Koha/Biblios.t +++ a/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; --