@@ -, +, @@ credit balance --- C4/Accounts.pm | 36 ++++++++++++++++++++++++------------ C4/Members.pm | 14 ++++++++++++-- members/paycollect.pl | 8 ++++++-- 3 files changed, 42 insertions(+), 16 deletions(-) --- a/C4/Accounts.pm +++ a/C4/Accounts.pm @@ -22,7 +22,7 @@ use strict; #use warnings; FIXME - Bug 2505 use C4::Context; use C4::Stats; -use C4::Members; +use C4::Members qw(GetMemberAccountRecords); use C4::Circulation qw(ReturnLostItem); use C4::Log qw(logaction); @@ -108,21 +108,32 @@ sub recordpayment { my $sth = $dbh->prepare( "SELECT * FROM accountlines WHERE (borrowernumber = ?) AND (amountoutstanding<>0) - ORDER BY date" + ORDER BY accountlines_id" ); $sth->execute($borrowernumber); + my ( $total, undef, undef, $original_credit ) = C4::Members::GetMemberAccountRecords( $borrowernumber ); + my $amount_to_pay = $total - $original_credit; + my $amount_paid = $amount_to_pay; + + # offset transactions my @ids; - while ( ( $accdata = $sth->fetchrow_hashref ) and ( $amountleft > 0 ) ) { - if ( $accdata->{'amountoutstanding'} < $amountleft ) { - $newamtos = 0; - $amountleft -= $accdata->{'amountoutstanding'}; + while ( ( $accdata = $sth->fetchrow_hashref ) ) { + if ( $accdata->{'amountoutstanding'} < 0 ) { + if ( $amount_to_pay <= ( 0 - $accdata->{'amountoutstanding'} ) ) { + $newamtos = $accdata->{'amountoutstanding'} + $amount_to_pay; + $amount_to_pay = 0; + } + else { + $newamtos = 0; + $amount_to_pay += $accdata->{'amountoutstanding'}; + } } else { - $newamtos = $accdata->{'amountoutstanding'} - $amountleft; - $amountleft = 0; + $newamtos = 0; } + my $thisacct = $accdata->{accountlines_id}; my $usth = $dbh->prepare( "UPDATE accountlines SET amountoutstanding= ? @@ -147,6 +158,7 @@ sub recordpayment { } } + # create new line my $usth = $dbh->prepare( "INSERT INTO accountlines @@ -156,13 +168,13 @@ sub recordpayment { my $paytype = "Pay"; $paytype .= $sip_paytype if defined $sip_paytype; - $usth->execute( $borrowernumber, $nextaccntno, 0 - $data, $paytype, 0 - $amountleft, $manager_id, $payment_note ); + $usth->execute( $borrowernumber, $nextaccntno, 0 - $amount_paid, $paytype, 0, $manager_id, $payment_note ); $usth->finish; UpdateStats({ branch => $branch, type =>'payment', - amount => $data, + amount => $amount_paid, borrowernumber => $borrowernumber, accountno => $nextaccntno } ); @@ -173,8 +185,8 @@ sub recordpayment { action => 'create_payment', borrowernumber => $borrowernumber, accountno => $nextaccntno, - amount => $data * -1, - amountoutstanding => $amountleft * -1, + amount => $amount_paid * -1, + amountoutstanding => $amount_to_pay * -1, accounttype => 'Pay', accountlines_paid => \@ids, manager_id => $manager_id, --- a/C4/Members.pm +++ a/C4/Members.pm @@ -1167,6 +1167,7 @@ sub GetMemberAccountRecords { $sth->execute( $borrowernumber ); my $total = 0; + my $credit = 0; while ( my $data = $sth->fetchrow_hashref ) { if ( $data->{itemnumber} ) { my $biblio = GetBiblioFromItemNumber( $data->{itemnumber} ); @@ -1175,10 +1176,19 @@ sub GetMemberAccountRecords { } $acctlines[$numlines] = $data; $numlines++; - $total += int(1000 * $data->{'amountoutstanding'}); # convert float to integer to avoid round-off errors + + my $amountoustanding = \$data->{'amountoutstanding'}; + + $total += int(1000 * $$amountoustanding); # convert float to integer to avoid round-off errors + + if ( $$amountoustanding < 0 ) { + $credit += int(1000 * $$amountoustanding); + } + } $total /= 1000; - return ( $total, \@acctlines,$numlines); + $credit /= 1000; + return ( $total, \@acctlines,$numlines, $credit); } =head2 GetMemberAccountBalance --- a/members/paycollect.pl +++ a/members/paycollect.pl @@ -51,7 +51,7 @@ my $user = $input->remote_user; # get account details my $branch = GetBranch( $input, GetBranches() ); -my ( $total_due, $accts, $numaccts ) = GetMemberAccountRecords($borrowernumber); +my ( $total_due, $accts, $numaccts, $credit ) = GetMemberAccountRecords($borrowernumber); my $total_paid = $input->param('paid'); my $individual = $input->param('pay_individual'); @@ -100,7 +100,11 @@ if ( $individual || $writeoff ) { ); } -if ( $total_paid and $total_paid ne '0.00' ) { +if ( $total_due - $credit + $credit <= 0) { + $total_due = 0; +} + +if ( $total_paid ) { if ( $total_paid < 0 or $total_paid > $total_due ) { $template->param( error_over => 1, --