From 10dd350a8bdb408c226721e1387edd9dd3b1575e Mon Sep 17 00:00:00 2001 From: Kyle M Hall Date: Fri, 13 Apr 2018 12:25:51 +0000 Subject: [PATCH] Bug 18790: (QA follow-up) Use a transaction for voiding Signed-off-by: Jonathan Druart --- Koha/Account/Line.pm | 98 +++++++++++++++++++++++++------------------- 1 file changed, 56 insertions(+), 42 deletions(-) diff --git a/Koha/Account/Line.pm b/Koha/Account/Line.pm index e2cb749f67..b32f9a7b91 100644 --- a/Koha/Account/Line.pm +++ b/Koha/Account/Line.pm @@ -63,52 +63,66 @@ sub void { return unless $self->accounttype =~ /^Pay/; my @account_offsets = - Koha::Account::Offsets->search( { credit_id => $self->id, type => 'Payment' } ); - - foreach my $account_offset (@account_offsets) { - my $fee_paid = Koha::Account::Lines->find( $account_offset->debit_id ); - - next unless $fee_paid; - - my $amount_paid = $account_offset->amount * -1; # amount paid is stored as a negative amount - my $new_amount = $fee_paid->amountoutstanding + $amount_paid; - $fee_paid->amountoutstanding($new_amount); - $fee_paid->store(); + Koha::Account::Offsets->search( + { credit_id => $self->id, type => 'Payment' } ); + + $self->_result->result_source->schema->txn_do( + sub { + foreach my $account_offset (@account_offsets) { + my $fee_paid = + Koha::Account::Lines->find( $account_offset->debit_id ); + + next unless $fee_paid; + + my $amount_paid = $account_offset->amount * -1; # amount paid is stored as a negative amount + my $new_amount = $fee_paid->amountoutstanding + $amount_paid; + $fee_paid->amountoutstanding($new_amount); + $fee_paid->store(); + + Koha::Account::Offset->new( + { + credit_id => $self->id, + debit_id => $fee_paid->id, + amount => $amount_paid, + type => 'Void Payment', + } + )->store(); + } - Koha::Account::Offset->new( - { - credit_id => $self->id, - debit_id => $fee_paid->id, - amount => $amount_paid, - type => 'Void Payment', + if ( C4::Context->preference("FinesLog") ) { + logaction( + "FINES", 'VOID', + $self->borrowernumber, + Dumper( + { + action => 'void_payment', + borrowernumber => $self->borrowernumber, + amount => $self->amount, + amountoutstanding => $self->amountoutstanding, + description => $self->description, + accounttype => $self->accounttype, + payment_type => $self->payment_type, + note => $self->note, + itemnumber => $self->itemnumber, + manager_id => $self->manager_id, + offsets => + [ map { $_->unblessed } @account_offsets ], + } + ) + ); } - )->store(); - } - - if ( C4::Context->preference("FinesLog") ) { - logaction("FINES", 'VOID', $self->borrowernumber, Dumper({ - action => 'void_payment', - borrowernumber => $self->borrowernumber,, - amount => $self->amount, - amountoutstanding => $self->amountoutstanding, - description => $self->description, - accounttype => $self->accounttype, - payment_type => $self->payment_type, - note => $self->note, - itemnumber => $self->itemnumber, - manager_id => $self->manager_id, - offsets => [ map { $_->unblessed } @account_offsets ], - })); - } - - $self->set( - { - accounttype => 'VOID', - amountoutstanding => 0, - amount => 0, + + $self->set( + { + accounttype => 'VOID', + amountoutstanding => 0, + amount => 0, + } + ); + $self->store(); } ); - $self->store(); + } =head3 _type -- 2.39.5