Bug 12278: (follow-up) Use invoice quantity to sum total

Prevents additional orders from subscriptions from being swallowed into the invoice total calculation

Signed-off-by: Pedro Amorim <pedro.amorim@openfifth.co.uk>
This commit is contained in:
Matt Blenkinsop 2026-08-24 09:55:29 +01:00 committed by Pedro Amorim
parent f7bbdd4976
commit 2ce97a58e2
No known key found for this signature in database
GPG Key ID: 06B75BA7F3894D55
2 changed files with 66 additions and 3 deletions

View File

@ -2434,8 +2434,14 @@ sub GetInvoices {
) AS is_linked_to_subscriptions,
SUM(aqorders.quantityreceived) AS receiveditems,
SUM(aqorders.quantity) AS itemsexpected,
COALESCE(SUM(aqorders.unitprice_tax_excluded * aqorders.quantity), 0) AS total_tax_excluded,
COALESCE(SUM(aqorders.unitprice_tax_included * aqorders.quantity), 0) AS total_tax_included,
COALESCE(
(SELECT SUM(o.unitprice_tax_excluded * o.quantity) FROM aqorders o WHERE o.invoiceid = aqinvoices.invoiceid),
0
) AS total_tax_excluded,
COALESCE(
(SELECT SUM(o.unitprice_tax_included * o.quantity) FROM aqorders o WHERE o.invoiceid = aqinvoices.invoiceid),
0
) AS total_tax_included,
COALESCE(
(SELECT SUM(adj.adjustment) FROM aqinvoice_adjustments adj WHERE adj.invoiceid = aqinvoices.invoiceid),
0

View File

@ -7,9 +7,10 @@ use C4::Biblio qw( AddBiblio );
use Koha::Acquisition::Booksellers;
use Koha::Acquisition::Orders;
use Koha::Database;
use Koha::Subscription;
use Test::NoWarnings;
use Test::More tests => 31;
use Test::More tests => 33;
BEGIN {
use_ok(
@ -198,6 +199,62 @@ subtest 'GetInvoices() returns financial totals' => sub {
cmp_ok( $inv2->{total_tax_included}, '>=', 0, 'total_tax_included is non-negative' );
};
subtest 'GetInvoices() financial totals are not inflated by multiple subscriptions on a biblio' => sub {
plan tests => 2;
my ($sub_biblionumber) = AddBiblio( MARC::Record->new, '' );
my $sub_order = Koha::Acquisition::Order->new(
{
basketno => $basketno,
quantity => 3,
biblionumber => $sub_biblionumber,
budget_id => $budget->{budget_id},
}
)->store;
my $sub_invoiceid = AddInvoice(
invoicenumber => 'invoice_subs_test', booksellerid => $booksellerid, unknown => "unknown",
);
my $sub_invoice = GetInvoice($sub_invoiceid);
my $order_data = $sub_order->unblessed;
$order_data->{unitprice} = 7;
$order_data->{unitprice_tax_excluded} = 7;
$order_data->{unitprice_tax_included} = 7;
ModReceiveOrder(
{
biblionumber => $sub_biblionumber,
order => $order_data,
quantityreceived => 3,
invoice => $sub_invoice,
}
);
my @inv_before = GetInvoices( invoicenumber => 'invoice_subs_test' );
my $inv_before = $inv_before[0];
# Attach two subscriptions to the order's biblio. The subscription table
# is LEFT JOINed on biblionumber in GetInvoices(), so without correlated
# subqueries these totals would be multiplied by the number of
# subscriptions on the biblio.
Koha::Subscription->new( { biblionumber => $sub_biblionumber } )->store;
Koha::Subscription->new( { biblionumber => $sub_biblionumber } )->store;
my @inv_after = GetInvoices( invoicenumber => 'invoice_subs_test' );
my $inv_after = $inv_after[0];
is(
$inv_after->{total_tax_excluded}, $inv_before->{total_tax_excluded},
'total_tax_excluded is unaffected by additional subscriptions on the biblio'
);
is(
$inv_after->{total_tax_included}, $inv_before->{total_tax_included},
'total_tax_included is unaffected by additional subscriptions on the biblio'
);
};
my $invoicesummary1 = GetInvoice($invoiceid1);
is( $invoicesummary1->{'invoicenumber'}, 'invoice1', 'GetInvoice retrieves correct invoice' );
is(