Bug 43344: Use the current source alias in filter_by_active

Bug 36018 qualified the quantity column with the default "me" alias to
avoid ambiguity when joined tables also have a quantity column.

DBIx::Class does not always use "me" for related resultsets. The
many-to-many item_orders relationship uses "ordernumber", so evaluating
item.orders.filter_by_active from the OPAC refers to a non-existent
me.quantity column.

Use current_source_alias to qualify quantity with the alias of the actual
resultset. Add regression coverage for filtering active orders through the
item relationship.

Test plan:
Before applying the patch:
1. Set OPACAcquisitionDetails to Show.
2. Set AcqCreateItem to create an item when placing an order.
3. Create a basket and add an order for a bibliographic record, including an
   item.
4. Close the basket so the order is active and outstanding.
5. Open the bibliographic record in the OPAC.
6. Confirm the item is not shown as On order and the log contains an Unknown
   column 'me.quantity' error.

Apply the patch.

After applying the patch:
1. Open the same bibliographic record in the OPAC again.
2. Confirm the item is shown as On order and the SQL error is gone.

Co-Authored-By: Loontik <loontik@nugged.team>
Signed-off-by: Olivia Reynolds <olivia.reynolds@openfifth.co.uk>
Signed-off-by: Katrin Fischer <katrin.fischer@bsz-bw.de>
Signed-off-by: Pedro Amorim <pedro.amorim@openfifth.co.uk>
This commit is contained in:
Andrii Nugged 2026-08-20 09:21:41 +03:00 committed by Pedro Amorim
parent 0d80c7d440
commit d64d02761d
No known key found for this signature in database
GPG Key ID: 06B75BA7F3894D55
2 changed files with 38 additions and 2 deletions

View File

@ -160,6 +160,7 @@ on this order (checking quantity and quantityreceived).
sub filter_by_active { sub filter_by_active {
my ($self) = @_; my ($self) = @_;
my $source_alias = $self->_resultset->current_source_alias;
return $self->search( return $self->search(
{ {
'-or' => [ '-or' => [
@ -169,7 +170,7 @@ sub filter_by_active {
}, },
{ 'orderstatus' => [ 'ordered', 'partial' ] } { 'orderstatus' => [ 'ordered', 'partial' ] }
], ],
quantityreceived => { '<', \['COALESCE(me.quantity,0)'] }, quantityreceived => { '<', \["COALESCE($source_alias.quantity,0)"] },
}, },
{ join => 'basket' } { join => 'basket' }
); );

View File

@ -20,7 +20,7 @@
use Modern::Perl; use Modern::Perl;
use Test::NoWarnings; use Test::NoWarnings;
use Test::More tests => 4; use Test::More tests => 5;
use Test::Exception; use Test::Exception;
use t::lib::TestBuilder; use t::lib::TestBuilder;
@ -126,6 +126,41 @@ subtest 'filter_by_active() tests' => sub {
$schema->storage->txn_rollback; $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 { subtest 'filter_by_id_including_transfers() tests' => sub {
plan tests => 4; plan tests => 4;