From 33ae53c7c5adaef63c4e85ad2707823628e81795 Mon Sep 17 00:00:00 2001
From: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Date: Wed, 21 Feb 2024 15:20:23 +0000
Subject: [PATCH] Bug 36018: $orders->filter_by_active should check
 quantityreceived
Content-Type: text/plain; charset=utf-8

An active order that has no items to receive any more is actually
not very active anymore :)

Test plan:
Run t/db_dependent/Koha/Acquisition/Orders.t
Enable OPACAcquisitionDetails.
Add order on basket for new biblio. Check that opac-detail does not
yet count the item as on order (since status is still new).
Close basket. Check opac-detail again. Should count it now.
Reopen basket. Cancel line. Check opac-detail again.

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
---
 Koha/Acquisition/Orders.pm               |  6 ++++--
 t/db_dependent/Koha/Acquisition/Orders.t | 22 +++++++++++++++-------
 2 files changed, 19 insertions(+), 9 deletions(-)

diff --git a/Koha/Acquisition/Orders.pm b/Koha/Acquisition/Orders.pm
index 8672de10c9..43e0832277 100644
--- a/Koha/Acquisition/Orders.pm
+++ b/Koha/Acquisition/Orders.pm
@@ -152,7 +152,8 @@ sub filter_by_lates {
 Returns a new resultset containing active orders only.
 
 Note: An active order (line) has status ordered or partial, or it has status new
-and the basket is marked as standing order.
+and the basket is marked as standing order. Additionally, we still expect items
+on this order (checking quantity and quantityreceived).
 
 =cut
 
@@ -164,7 +165,8 @@ sub filter_by_active {
                 { 'basket.is_standing' => 1,
                   'orderstatus' => [ 'new', 'ordered', 'partial' ] },
                 { 'orderstatus' => [ 'ordered', 'partial' ] }
-            ]
+            ],
+            quantityreceived => { '<', \['COALESCE(quantity,0)'] },
         },
         { join => 'basket' }
     );
diff --git a/t/db_dependent/Koha/Acquisition/Orders.t b/t/db_dependent/Koha/Acquisition/Orders.t
index cf28bd07b7..f34dc7c2d9 100755
--- a/t/db_dependent/Koha/Acquisition/Orders.t
+++ b/t/db_dependent/Koha/Acquisition/Orders.t
@@ -32,7 +32,7 @@ my $builder = t::lib::TestBuilder->new;
 
 subtest 'filter_by_active() tests' => sub {
 
-    plan tests => 4;
+    plan tests => 5;
 
     $schema->storage->txn_begin;
 
@@ -66,29 +66,33 @@ subtest 'filter_by_active() tests' => sub {
         {
             class => 'Koha::Acquisition::Orders',
             value => {
-                basketno    => $basket_1->basketno,
-                orderstatus => 'new'
+                basketno         => $basket_1->basketno,
+                orderstatus      => 'new',
+                quantity         => 1,
+                quantityreceived => 0,
             }
         }
     );
     my $order_4 = $builder->build_object(
         {
             class => 'Koha::Acquisition::Orders',
-            value => { orderstatus => 'ordered' }
+            value => { orderstatus => 'ordered', quantity => 1, quantityreceived => 0 }
         }
     );
     my $order_5 = $builder->build_object(
         {
             class => 'Koha::Acquisition::Orders',
-            value => { orderstatus => 'partial' }
+            value => { orderstatus => 'partial', quantity => 2, quantityreceived => 1 }
         }
     );
     my $order_6 = $builder->build_object(
         {
             class => 'Koha::Acquisition::Orders',
             value => {
-                basketno    => $basket_2->basketno,
-                orderstatus => 'new'
+                basketno         => $basket_2->basketno,
+                orderstatus      => 'new',
+                quantity         => 1,
+                quantityreceived => 0,
             }
         }
     );
@@ -116,6 +120,10 @@ subtest 'filter_by_active() tests' => sub {
     is( $rs->next->ordernumber, $order_4->ordernumber , 'Expected order in resultset' );
     is( $rs->next->ordernumber, $order_5->ordernumber , 'Expected order in resultset' );
 
+    # If we change quantities on order_5 (partial), we should no longer see it
+    $order_5->quantityreceived(2)->store;
+    is( $this_orders_rs->filter_by_active->count, 2, 'Dropped one order as expected' );
+
     $schema->storage->txn_rollback;
 };
 
-- 
2.30.2