@@ -, +, @@ --- Koha/Account/Line.pm | 78 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 66 insertions(+), 12 deletions(-) --- a/Koha/Account/Line.pm +++ a/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 --