From 4f9ad7745720368ec8d6781aa0a1850b7a4c9bae Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Wed, 17 Mar 2021 09:10:17 +0000 Subject: [PATCH] Bug 27971: Update void method to use double entry accounting This patch adds double-entry accounting to the Koha::Account::Line->void method. This results in the addition of a VOID debit type line that is offset against the original credit type line that is being voided. This allows us to accurately record when the void took place, at what branch and by whome the void was triggered. --- Koha/Account/Line.pm | 78 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 66 insertions(+), 12 deletions(-) diff --git a/Koha/Account/Line.pm b/Koha/Account/Line.pm index 2cc81993ef..95654d9f1b 100644 --- a/Koha/Account/Line.pm +++ b/Koha/Account/Line.pm @@ -219,17 +219,39 @@ as 'void' by updating it's status to "VOID". =cut sub void { - my ($self) = @_; + my ($self, $params) = @_; - # Make sure it is a payment we are voiding - return unless $self->amount < 0; + # Make sure it is a credit we are voiding + unless ( $self->is_credit ) { + Koha::Exceptions::Account::IsNotCredit->throw( + error => 'Account line ' . $self->id . 'is not a credit' ); + } + + # Make sure it is not already voided + if ( $self->status && $self->status eq 'VOID' ) { + Koha::Exceptions::Account->throw( + error => 'Account line ' . $self->id . 'is already void' ); + } + + # Check for mandatory parameters + my @mandatory = ( 'staff_id', 'branch' ); + for my $param (@mandatory) { + unless ( defined( $params->{$param} ) ) { + Koha::Exceptions::MissingParameter->throw( + error => "The $param parameter is mandatory" ); + } + } + # Find any applied offsets for the credit so we may reverse them my @account_offsets = Koha::Account::Offsets->search( { credit_id => $self->id, amount => { '<' => 0 } } ); + my $void; $self->_result->result_source->schema->txn_do( sub { + + # Reverse any applied payments foreach my $account_offset (@account_offsets) { my $fee_paid = Koha::Account::Lines->find( $account_offset->debit_id ); @@ -251,6 +273,45 @@ sub void { )->store(); } + # A 'void' is a 'debit' + $void = Koha::Account::Line->new( + { + date => \'NOW()', + amount => $self->amount * -1, + debit_type_code => 'VOID', + status => 'ADDED', + amountoutstanding => 0, + manager_id => $params->{staff_id}, + borrowernumber => $self->borrowernumber, + interface => 'intranet', + branchcode => $params->{branch}, + } + )->store(); + + Koha::Account::Offset->new( + { + debit_id => $void->accountlines_id, + type => 'VOID', + amount => $self->amount * -1 + } + )->store(); + + # Link void to payment + Koha::Account::Offset->new( + { credit_id => $self->id, + debit_id => $void->id, + amount => $self->amount, + type => 'VOID' + } + )->store(); + $self->set( + { + status => 'VOID', + amountoutstanding => 0 + } + ); + $self->store(); + if ( C4::Context->preference("FinesLog") ) { logaction( "FINES", 'VOID', @@ -273,18 +334,11 @@ sub void { ) ); } - - $self->set( - { - status => 'VOID', - amountoutstanding => 0, - amount => 0, - } - ); - $self->store(); } ); + $void->discard_changes; + return $void; } =head3 cancel -- 2.20.1