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
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
note => $note,
accountlines_id => $accountlines_id,
library_id => $branchcode,
+ lines => $lines, # Arrayref of Koha::Account::Line objects to 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;
$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 );
}