From a97ee7fc6cfc04c3b31a5549cafe287317f01732 Mon Sep 17 00:00:00 2001 From: Kyle M Hall Date: Wed, 24 Feb 2016 13:30:07 +0000 Subject: [PATCH] Bug 15897 - Use Koha::Account::pay internally for recordpayment_selectaccts This is the third patch in a series to unify all payment functions into a single mathod Test Plan: 1) Apply this patch 2) prove t/db_dependent/Accounts.t 3) Test fine payment via the "Pay selected" button Signed-off-by: Josef Moravec Signed-off-by: Jonathan Druart --- C4/Accounts.pm | 101 +++++++++++--------------------------------------------- Koha/Account.pm | 21 ++++++------ 2 files changed, 31 insertions(+), 91 deletions(-) diff --git a/C4/Accounts.pm b/C4/Accounts.pm index 45d270a..1d0654e 100644 --- a/C4/Accounts.pm +++ b/C4/Accounts.pm @@ -88,8 +88,10 @@ was made. sub makepayment { my ( $accountlines_id, $borrowernumber, $accountno, $amount, $user, $branch, $payment_note ) = @_; + my $line = Koha::Account::Lines->find( $accountlines_id ); + return Koha::Account->new( { patron_id => $borrowernumber } ) - ->pay( { accountlines_id => $accountlines_id, amount => $amount, library_id => $branch, note => $payment_note } ); + ->pay( { lines => [ $line ], amount => $amount, library_id => $branch, note => $payment_note } ); } =head2 getnextacctno @@ -396,89 +398,26 @@ will be credited to the next one. sub recordpayment_selectaccts { my ( $borrowernumber, $amount, $accts, $note ) = @_; - my $dbh = C4::Context->dbh; - my $newamtos = 0; - my $accdata = q{}; - my $branch = C4::Context->userenv->{branch}; - my $amountleft = $amount; - my $manager_id = 0; - $manager_id = C4::Context->userenv->{'number'} if C4::Context->userenv; - my $sql = 'SELECT * FROM accountlines WHERE (borrowernumber = ?) ' . - 'AND (amountoutstanding<>0) '; - if (@{$accts} ) { - $sql .= ' AND accountlines_id IN ( ' . join ',', @{$accts}; - $sql .= ' ) '; - } - $sql .= ' ORDER BY date'; - # begin transaction - my $nextaccntno = getnextacctno($borrowernumber); - - # get lines with outstanding amounts to offset - my $rows = $dbh->selectall_arrayref($sql, { Slice => {} }, $borrowernumber); - - # offset transactions - my $sth = $dbh->prepare('UPDATE accountlines SET amountoutstanding= ? ' . - 'WHERE accountlines_id=?'); + my @lines = Koha::Account::Lines->search( + { + borrowernumber => $borrowernumber, + amountoutstanding => { '<>' => 0 }, + accountno => { 'IN' => $accts }, + }, + { order_by => 'date' } + ); - my @ids; - for my $accdata ( @{$rows} ) { - if ($amountleft == 0) { - last; - } - if ( $accdata->{amountoutstanding} < $amountleft ) { - $newamtos = 0; - $amountleft -= $accdata->{amountoutstanding}; + return Koha::Account->new( + { + patron_id => $borrowernumber, } - else { - $newamtos = $accdata->{amountoutstanding} - $amountleft; - $amountleft = 0; + )->pay( + { + amount => $amount, + lines => \@lines, + note => $note } - my $thisacct = $accdata->{accountlines_id}; - $sth->execute( $newamtos, $thisacct ); - - if ( C4::Context->preference("FinesLog") ) { - logaction("FINES", 'MODIFY', $borrowernumber, Dumper({ - action => 'fee_payment', - borrowernumber => $borrowernumber, - old_amountoutstanding => $accdata->{'amountoutstanding'}, - new_amountoutstanding => $newamtos, - amount_paid => $accdata->{'amountoutstanding'} - $newamtos, - accountlines_id => $accdata->{'accountlines_id'}, - accountno => $accdata->{'accountno'}, - manager_id => $manager_id, - })); - push( @ids, $accdata->{'accountlines_id'} ); - } - - } - - # create new line - $sql = 'INSERT INTO accountlines ' . - '(borrowernumber, accountno,date,amount,description,accounttype,amountoutstanding,manager_id,note) ' . - q|VALUES (?,?,now(),?,'','Pay',?,?,?)|; - $dbh->do($sql,{},$borrowernumber, $nextaccntno, 0 - $amount, 0 - $amountleft, $manager_id, $note ); - UpdateStats({ - branch => $branch, - type => 'payment', - amount => $amount, - borrowernumber => $borrowernumber, - accountno => $nextaccntno} - ); - - if ( C4::Context->preference("FinesLog") ) { - logaction("FINES", 'CREATE',$borrowernumber,Dumper({ - action => 'create_payment', - borrowernumber => $borrowernumber, - accountno => $nextaccntno, - amount => 0 - $amount, - amountoutstanding => 0 - $amountleft, - accounttype => 'Pay', - accountlines_paid => \@ids, - manager_id => $manager_id, - })); - } - - return; + ); } # makepayment needs to be fixed to handle partials till then this separate subroutine diff --git a/Koha/Account.pm b/Koha/Account.pm index 7041cb2..d88eb0d 100644 --- a/Koha/Account.pm +++ b/Koha/Account.pm @@ -54,6 +54,7 @@ Koha::Account->new( { patron_id => $borrowernumber } )->pay( note => $note, accountlines_id => $accountlines_id, library_id => $branchcode, + lines => $lines, # Arrayref of Koha::Account::Line objects to pay } ); @@ -65,8 +66,8 @@ sub pay { my $amount = $params->{amount}; my $sip = $params->{sip}; my $note = $params->{note} || q{}; - my $accountlines_id = $params->{accountlines_id}; my $library_id = $params->{library_id}; + my $lines = $params->{lines}, my $userenv = C4::Context->userenv; @@ -89,18 +90,18 @@ sub pay { $balance_remaining ||= 0; # We were passed a specific line to pay - if ( $accountlines_id ) { - my $fine = Koha::Account::Lines->find( $accountlines_id ); - - # If accountline id is passed but no amount, we pay that line in full - $amount = $fine->amountoutstanding unless defined($amount); + foreach my $fine ( @$lines ) { + my $amount_to_pay = + $fine->amountoutstanding > $balance_remaining + ? $balance_remaining + : $fine->amountoutstanding; my $old_amountoutstanding = $fine->amountoutstanding; - my $new_amountoutstanding = $old_amountoutstanding - $amount; - $fine->amountoutstanding( $new_amountoutstanding )->store(); - $balance_remaining = $balance_remaining - $amount; + my $new_amountoutstanding = $old_amountoutstanding - $amount_to_pay; + $fine->amountoutstanding($new_amountoutstanding)->store(); + $balance_remaining = $balance_remaining - $amount_to_pay; - if ( $fine->accounttype eq 'Rep' || $fine->accounttype eq 'L' ) + if ( $fine->accounttype && ( $fine->accounttype eq 'Rep' || $fine->accounttype eq 'L' ) ) { C4::Circulation::ReturnLostItem( $self->{patron_id}, $fine->itemnumber ); } -- 2.9.3