From b1ff9306a6b1d0f2109f4bed361567db8e2334f4 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Fri, 14 May 2021 14:04:35 +0100 Subject: [PATCH] Bug 22435: Fix _set_found_trigger The _set_found_trigger utilised the 'Writeoff' offset type to distinguish between the application of a writeoff and any other form of offset application. This patch updates the trigger to use the full link through from offset to account credit line to get the credit type being offset. Test plan 1/ Run t/db_dependent/Koha/Items.t and prove it fails before the patch, but passes after applying this patch. 2/ Run t/db_dependent/Koha/Account/Offsets.t and prove it passes both before and after applying this patch. Signed-off-by: Victor Grousset/tuxayo Signed-off-by: Nick Clemens Signed-off-by: Tomas Cohen Arazi --- Koha/Account/Line.pm | 8 ++++---- Koha/Account/Offsets.pm | 2 +- Koha/Item.pm | 15 +++++++-------- t/db_dependent/Koha/Account/Offsets.t | 2 +- 4 files changed, 13 insertions(+), 14 deletions(-) diff --git a/Koha/Account/Line.pm b/Koha/Account/Line.pm index 13e9b07a48..a05ab65c28 100644 --- a/Koha/Account/Line.pm +++ b/Koha/Account/Line.pm @@ -131,8 +131,8 @@ Return the credit_offsets linked to this account line if some exist =cut sub credit_offsets { - my ( $self ) = @_; - my $rs = $self->_result->account_offsets_credits; + my ( $self, $cond, $attr ) = @_; + my $rs = $self->_result->search_related( 'account_offsets_credits', $cond, $attr); return unless $rs; return Koha::Account::Offsets->_new_from_dbic($rs); } @@ -144,8 +144,8 @@ Return the debit_offsets linked to this account line if some exist =cut sub debit_offsets { - my ( $self ) = @_; - my $rs = $self->_result->account_offsets_debits; + my ( $self, $cond, $attr ) = @_; + my $rs = $self->_result->search_related( 'account_offsets_debits', $cond, $attr); return unless $rs; return Koha::Account::Offsets->_new_from_dbic($rs); } diff --git a/Koha/Account/Offsets.pm b/Koha/Account/Offsets.pm index 6a401968f2..e2884114f6 100644 --- a/Koha/Account/Offsets.pm +++ b/Koha/Account/Offsets.pm @@ -50,7 +50,7 @@ sub total { my $offsets = $self->search( {}, { - select => [ { sum => 'amount' } ], + select => [ { sum => 'me.amount' } ], as => ['total_amount'], } ); diff --git a/Koha/Item.pm b/Koha/Item.pm index c653dc2583..ca17ed0637 100644 --- a/Koha/Item.pm +++ b/Koha/Item.pm @@ -1004,17 +1004,16 @@ sub _set_found_trigger { # some amount has been cancelled. collect the offsets that are not writeoffs # this works because the only way to subtract from this kind of a debt is # using the UI buttons 'Pay' and 'Write off' - my $credits_offsets = Koha::Account::Offsets->search( + my $credit_offsets = $lost_charge->debit_offsets( { - debit_id => $lost_charge->id, - credit_id => { '!=' => undef }, # it is not the debit itself - type => { '!=' => 'Writeoff' }, - amount => { '<' => 0 } # credits are negative on the DB - } + 'credit_id' => { '!=' => undef }, + 'credit.credit_type_code' => { '!=' => 'Writeoff' } + }, + { join => 'credit' } ); - $total_to_refund = ( $credits_offsets->count > 0 ) - ? $credits_offsets->total * -1 # credits are negative on the DB + $total_to_refund = ( $credit_offsets->count > 0 ) + ? $credit_offsets->total * -1 # credits are negative on the DB : 0; } diff --git a/t/db_dependent/Koha/Account/Offsets.t b/t/db_dependent/Koha/Account/Offsets.t index 7e5e33f44b..cc271b5086 100755 --- a/t/db_dependent/Koha/Account/Offsets.t +++ b/t/db_dependent/Koha/Account/Offsets.t @@ -29,7 +29,7 @@ use t::lib::TestBuilder; my $schema = Koha::Database->new->schema; my $builder = t::lib::TestBuilder->new; -subtest 'total_outstanding() tests' => sub { +subtest 'total() tests' => sub { plan tests => 4; -- 2.20.1