From 9a64617068432b2a403256313e7be93ace9e5a24 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Tue, 6 May 2025 15:47:05 -0300 Subject: [PATCH] Bug 39845: Add Koha::Acquisition::Orders->unreceived_totals This patch introduces a method for returning the following calculated values for a `Koha::Acquisition::Orders` resultset: * items_count * total_replacement_price To test: 1. Apply this patches 2. Run: $ ktd --shell k$ prove t/db_dependent/Koha/Acquisition/Orders.t => SUCCESS: Tests pass! 3. Sign off :-D --- Koha/Acquisition/Orders.pm | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/Koha/Acquisition/Orders.pm b/Koha/Acquisition/Orders.pm index c274e4b72aa..9cfe339d7c3 100644 --- a/Koha/Acquisition/Orders.pm +++ b/Koha/Acquisition/Orders.pm @@ -276,6 +276,43 @@ sub filter_by_obsolete { return $rs; } +=head3 unreceived_totals + + my $unreceived_totals = $orders->unreceived_totals(); + +Returns a hashref with the total unreceived items, and their total price, for the +given I resultset. + +Example returned value: + + { + items_count => 36, + total_replacement_price => 87.5 + } + +=cut + +sub unreceived_totals { + my ($self) = @_; + + my $me = $self->_resultset()->current_source_alias; + my $result = $self->search( + {}, + { + select => [ + \"SUM($me.quantity - $me.quantityreceived)", + \"SUM(($me.quantity - $me.quantityreceived) * $me.rrp)" + ], + as => [ 'total_count', 'total_rrp' ], + } + )->next; + + return { + items_count => $result->get_column('total_count') // 0, + total_replacement_price => $result->get_column('total_rrp') // 0, + }; +} + =head3 cancel $orders_rs->cancel( { delete_biblio => 0|1 } ); -- 2.49.0