From 178b5f1f276bd87b32f453e5b49db9f915bf86b4 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Thu, 21 Nov 2019 13:22:39 +0000 Subject: [PATCH] Bug 24080: Add 'payout' method to Koha::Account::Line This enhancement adds a 'payout' method to Koha::Account::Line which can be used to 'pay out' a credit to a patron. When such a credit is 'paid out' this method will create a corresponding account debit line with an amount equal to the amountoutstanding on the original credit and the two acocuntlines will be immediately applied against each other. Test Plan: 1) Run the included tests and verify they pass. 2) Signoff --- Koha/Account/Line.pm | 89 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) diff --git a/Koha/Account/Line.pm b/Koha/Account/Line.pm index 571c26a986..09d4338eef 100644 --- a/Koha/Account/Line.pm +++ b/Koha/Account/Line.pm @@ -278,6 +278,95 @@ sub apply { return $available_credit; } +=head3 payout + + $credit_accountline->payout( + { + payout_type => $payout_type, + register_id => $register_id, + amount => $amount + } + ); + +Used to 'pay out' a credit to a user. + +Payout type may be one of any existing payment types + +=cut + +sub payout { + my ( $self, $params ) = @_; + + # Make sure it is a credit we are paying out + unless ( $self->is_credit ) { + Koha::Exceptions::Account::IsNotCredit->throw( + error => 'Account line ' . $self->id . ' is not a credit' ); + } + + unless ( $params->{interface} ) { + Koha::Exceptions::MissingParameter->throw( + error => 'The interface parameter is mandatory' ); + } + + # Make sure there is outstanding credit to pay out + my $amount = + $params->{amount} ? $params->{amount} : $self->amountoutstanding; + return unless $self->amountoutstanding >= $amount; + + # Make sure we record the cash register for cash transactions + Koha::Exceptions::Account::RegisterRequired->throw() + if ( C4::Context->preference("UseCashRegisters") + && defined( $params->{payout_type} ) + && ( $params->{payout_type} eq 'CASH' ) + && !defined( $params->{cash_register} ) ); + + $params->{branch} //= $self->branchcode; + + my $payout; + $self->_result->result_source->schema->txn_do( + sub { + + # A 'payout' is a 'debit' + $payout = Koha::Account::Line->new( + { + date => \'NOW()', + amount => 0 - $amount, + debit_type_code => 'PAYOUT', + payment_type => $params->{payout_type}, + amountoutstanding => 0, + manager_id => $params->{staff_id}, + borrowernumber => $params->{patron_id}, + interface => $params->{interface}, + branchcode => $params->{branch}, + register_id => $params->{cash_register}, + note => $params->{quantity} + } + )->store(); + + my $payout_offset = Koha::Account::Offset->new( + { + debit_id => $payout->accountlines_id, + type => 'PAYOUT', + amount => 0 - $amount + } + )->store(); + + my $application_offset = Koha::Account::Offset->new( + { + debit_id => $payout->accountlines_id, + credit_id => $self->accountlines_id, + type => 'PAYOUT', + amount => 0 - $amount + } + )->store(); + + $self->status('PAID')->store; + } + ); + + return $payout; +} + =head3 adjust This method allows updating a debit or credit on a patron's account -- 2.20.1