diff --git a/Koha/Acquisition/Orders.pm b/Koha/Acquisition/Orders.pm index 9f80355900..5ffd58756b 100644 --- a/Koha/Acquisition/Orders.pm +++ b/Koha/Acquisition/Orders.pm @@ -160,6 +160,7 @@ on this order (checking quantity and quantityreceived). sub filter_by_active { my ($self) = @_; + my $source_alias = $self->_resultset->current_source_alias; return $self->search( { '-or' => [ @@ -169,7 +170,7 @@ sub filter_by_active { }, { 'orderstatus' => [ 'ordered', 'partial' ] } ], - quantityreceived => { '<', \['COALESCE(me.quantity,0)'] }, + quantityreceived => { '<', \["COALESCE($source_alias.quantity,0)"] }, }, { join => 'basket' } ); diff --git a/t/db_dependent/Koha/Acquisition/Orders.t b/t/db_dependent/Koha/Acquisition/Orders.t index f9bb533633..eaa92d4db9 100755 --- a/t/db_dependent/Koha/Acquisition/Orders.t +++ b/t/db_dependent/Koha/Acquisition/Orders.t @@ -20,7 +20,7 @@ use Modern::Perl; use Test::NoWarnings; -use Test::More tests => 4; +use Test::More tests => 5; use Test::Exception; use t::lib::TestBuilder; @@ -126,6 +126,41 @@ subtest 'filter_by_active() tests' => sub { $schema->storage->txn_rollback; }; +subtest 'filter_by_active() tests with an aliased resultset' => sub { + + plan tests => 1; + + $schema->storage->txn_begin; + + my $biblio = $builder->build_sample_biblio; + my $item = $builder->build_sample_item( { biblionumber => $biblio->biblionumber } ); + my $order = $builder->build_object( + { + class => 'Koha::Acquisition::Orders', + value => { + biblionumber => $biblio->biblionumber, + datecancellationprinted => undef, + orderstatus => 'ordered', + quantity => 1, + quantityreceived => 0, + } + } + ); + $builder->build( + { + source => 'AqordersItem', + value => { + ordernumber => $order->ordernumber, + itemnumber => $item->itemnumber, + } + } + ); + + is( $item->orders->filter_by_active->count, 1, 'Active orders are returned from an aliased resultset' ); + + $schema->storage->txn_rollback; +}; + subtest 'filter_by_id_including_transfers() tests' => sub { plan tests => 4;