- my $sth = $dbh->prepare("SELECT * FROM accountlines WHERE itemnumber = ? AND accounttype IN ('L', 'Rep', 'W') ORDER BY date DESC, accountno DESC");
- $sth->execute($itemnumber);
- my $data = $sth->fetchrow_hashref;
- $data or return; # bail if there is nothing to do
- $data->{accounttype} eq 'W' and return; # Written off
-
- # writeoff this amount
- my $offset;
- my $amount = $data->{'amount'};
- my $acctno = $data->{'accountno'};
- my $amountleft; # Starts off undef/zero.
- if ($data->{'amountoutstanding'} == $amount) {
- $offset = $data->{'amount'};
- $amountleft = 0; # Hey, it's zero here, too.
- } else {
- $offset = $amount - $data->{'amountoutstanding'}; # Um, isn't this the same as ZERO? We just tested those two things are ==
- $amountleft = $data->{'amountoutstanding'} - $amount; # Um, isn't this the same as ZERO? We just tested those two things are ==
- }
- my $usth = $dbh->prepare("UPDATE accountlines SET accounttype = 'LR',amountoutstanding='0'
- WHERE (accountlines_id = ?)");
- $usth->execute($data->{'accountlines_id'}); # We might be adjusting an account for some OTHER borrowernumber now. Not the one we passed in.
- #check if any credit is left if so writeoff other accounts
- my $nextaccntno = getnextacctno($data->{'borrowernumber'});
- $amountleft *= -1 if ($amountleft < 0);
- if ($amountleft > 0) {
- my $msth = $dbh->prepare("SELECT * FROM accountlines WHERE (borrowernumber = ?)
- AND (amountoutstanding >0) ORDER BY date"); # might want to order by amountoustanding ASC (pay smallest first)
- $msth->execute($data->{'borrowernumber'});
- # offset transactions
- my $newamtos;
- my $accdata;
- while (($accdata=$msth->fetchrow_hashref) and ($amountleft>0)){
- if ($accdata->{'amountoutstanding'} < $amountleft) {
- $newamtos = 0;
- $amountleft -= $accdata->{'amountoutstanding'};
- } else {
- $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
- $amountleft = 0;
- }
- my $thisacct = $accdata->{'accountlines_id'};
- # FIXME: move prepares outside while loop!
- my $usth = $dbh->prepare("UPDATE accountlines SET amountoutstanding= ?
- WHERE (accountlines_id = ?)");
- $usth->execute($newamtos,$thisacct);
- $usth = $dbh->prepare("INSERT INTO accountoffsets
- (borrowernumber, accountno, offsetaccount, offsetamount)
- VALUES
- (?,?,?,?)");
- $usth->execute($data->{'borrowernumber'},$accdata->{'accountno'},$nextaccntno,$newamtos);
- }
- }
- $amountleft *= -1 if ($amountleft > 0);
- my $desc = "Item Returned " . $item_id;
- $usth = $dbh->prepare("INSERT INTO accountlines
- (borrowernumber,accountno,date,amount,description,accounttype,amountoutstanding)
- VALUES (?,?,now(),?,?,'CR',?)");
- $usth->execute($data->{'borrowernumber'},$nextaccntno,0-$amount,$desc,$amountleft);
- if ($borrowernumber) {
- # FIXME: same as query above. use 1 sth for both
- $usth = $dbh->prepare("INSERT INTO accountoffsets
- (borrowernumber, accountno, offsetaccount, offsetamount)
- VALUES (?,?,?,?)");
- $usth->execute($borrowernumber, $data->{'accountno'}, $nextaccntno, $offset);
- }
- ModItem({ paidfor => '' }, undef, $itemnumber);
- return;
+ my $accountline = Koha::Account::Lines->search(
+ {
+ itemnumber => $itemnumber,
+ accounttype => { -in => [ 'L', 'Rep', 'W' ] },
+ },
+ {
+ order_by => { -desc => [ 'date', 'accountno' ] }
+ }
+ )->next();
+
+ return unless $accountline;
+ return if $accountline->accounttype eq 'W'; # Written off
+
+ $accountline->accounttype('LR');
+ $accountline->store();
+
+ my $account = Koha::Account->new( { patron_id => $accountline->borrowernumber } );
+ my $credit_id = $account->pay(
+ {
+ amount => $accountline->amount,
+ description => "Item Returned " . $item_id,
+ account_type => 'CR',
+ offset_type => 'Lost Item Return',
+ accounlines => [$accountline],
+
+ }
+ );
+
+ ModItem( { paidfor => '' }, undef, $itemnumber, { log_action => 0 } );
+
+ return $credit_id;