From d64d02761d3002a458504c78537118611618d802 Mon Sep 17 00:00:00 2001 From: Andrii Nugged Date: Thu, 20 Aug 2026 09:21:41 +0300 Subject: [PATCH] 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 Signed-off-by: Olivia Reynolds Signed-off-by: Katrin Fischer Signed-off-by: Pedro Amorim --- Koha/Acquisition/Orders.pm | 3 +- t/db_dependent/Koha/Acquisition/Orders.t | 37 +++++++++++++++++++++++- 2 files changed, 38 insertions(+), 2 deletions(-) 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;