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 <josef.moravec@gmail.com>

Signed-off-by: Jonathan Druart <jonathan.druart@biblibre.com>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
This commit is contained in:
Kyle M Hall 2016-02-24 13:30:07 +00:00
parent 6b0562eee8
commit 9c9d0bcfe0
2 changed files with 32 additions and 92 deletions

View File

@ -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 @ids;
for my $accdata ( @{$rows} ) {
if ($amountleft == 0) {
last;
}
if ( $accdata->{amountoutstanding} < $amountleft ) {
$newamtos = 0;
$amountleft -= $accdata->{amountoutstanding};
}
else {
$newamtos = $accdata->{amountoutstanding} - $amountleft;
$amountleft = 0;
}
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}
my @lines = Koha::Account::Lines->search(
{
borrowernumber => $borrowernumber,
amountoutstanding => { '<>' => 0 },
accountno => { 'IN' => $accts },
},
{ order_by => 'date' }
);
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;
return Koha::Account->new(
{
patron_id => $borrowernumber,
}
)->pay(
{
amount => $amount,
lines => \@lines,
note => $note
}
);
}
# makepayment needs to be fixed to handle partials till then this separate subroutine

View File

@ -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 );
}