Lines 2392-2399
sub _FixAccountForLostAndReturned {
Link Here
|
2392 |
my $borrowernumber = @_ ? shift : undef; |
2392 |
my $borrowernumber = @_ ? shift : undef; |
2393 |
my $item_id = @_ ? shift : $itemnumber; # Send the barcode if you want that logged in the description |
2393 |
my $item_id = @_ ? shift : $itemnumber; # Send the barcode if you want that logged in the description |
2394 |
|
2394 |
|
|
|
2395 |
my $credit; |
2396 |
|
2395 |
# check for charge made for lost book |
2397 |
# check for charge made for lost book |
2396 |
my $accountline = Koha::Account::Lines->search( |
2398 |
my $accountlines = Koha::Account::Lines->search( |
2397 |
{ |
2399 |
{ |
2398 |
itemnumber => $itemnumber, |
2400 |
itemnumber => $itemnumber, |
2399 |
accounttype => { -in => [ 'L', 'Rep', 'W' ] }, |
2401 |
accounttype => { -in => [ 'L', 'Rep', 'W' ] }, |
Lines 2401-2429
sub _FixAccountForLostAndReturned {
Link Here
|
2401 |
{ |
2403 |
{ |
2402 |
order_by => { -desc => [ 'date', 'accountno' ] } |
2404 |
order_by => { -desc => [ 'date', 'accountno' ] } |
2403 |
} |
2405 |
} |
2404 |
)->next(); |
2406 |
); |
|
|
2407 |
|
2408 |
return unless $accountlines->count > 0; |
2409 |
my $accountline = $accountlines->next; |
2410 |
|
2411 |
# Use cases |
2412 |
if ( $accountline->amount > $accountline->amountoutstanding ) { |
2413 |
# some amount has been cancelled. collect the offsets that are not writeoffs |
2414 |
# this works because the only way to subtract from a debt is |
2415 |
# using the UI buttons 'Pay' and 'Write off' |
2416 |
my $credits_offsets = Koha::Account::Offsets->search({ |
2417 |
debit_id => $accountline->id, |
2418 |
credit_id => { '!=' => undef }, # it is not the debit itself |
2419 |
type => { '!=' => 'Writeoff' }, |
2420 |
amount => { '<' => 0 } # credits are negative on the DB |
2421 |
}); |
2422 |
|
2423 |
my $total_to_refund = ( $credits_offsets->count > 0 ) |
2424 |
? $credits_offsets->total * -1 # credits are negative on the DB |
2425 |
: 0; |
2426 |
|
2427 |
if ( $total_to_refund > 0 ) { |
2428 |
my $account = Koha::Patrons->find( $accountline->borrowernumber )->account; |
2429 |
$credit = $account->add_credit( |
2430 |
{ |
2431 |
amount => $total_to_refund, |
2432 |
description => 'Item Returned ' . $item_id, |
2433 |
type => 'lost_item_return' |
2434 |
} |
2435 |
); |
2436 |
} |
2405 |
|
2437 |
|
2406 |
return unless $accountline; |
2438 |
ModItem( { paidfor => '' }, undef, $itemnumber, { log_action => 0 } ); |
2407 |
return if $accountline->accounttype eq 'W'; # Written off |
2439 |
} |
|
|
2440 |
# else { |
2441 |
# $accountline->amount == $accountline->amountoutstanding |
2442 |
#} |
2408 |
|
2443 |
|
2409 |
$accountline->accounttype('LR'); |
2444 |
$accountline->accounttype('LR'); |
|
|
2445 |
$accountline->amountoutstanding(0); |
2410 |
$accountline->store(); |
2446 |
$accountline->store(); |
2411 |
|
2447 |
|
2412 |
my $account = Koha::Account->new( { patron_id => $accountline->borrowernumber } ); |
2448 |
return ($credit) ? $credit->id : undef; |
2413 |
my $credit_id = $account->pay( |
|
|
2414 |
{ |
2415 |
amount => $accountline->amount, |
2416 |
description => "Item Returned " . $item_id, |
2417 |
account_type => 'CR', |
2418 |
offset_type => 'Lost Item Return', |
2419 |
lines => [$accountline], |
2420 |
|
2421 |
} |
2422 |
); |
2423 |
|
2424 |
ModItem( { paidfor => '' }, undef, $itemnumber, { log_action => 0 } ); |
2425 |
|
2426 |
return $credit_id; |
2427 |
} |
2449 |
} |
2428 |
|
2450 |
|
2429 |
=head2 _GetCircControlBranch |
2451 |
=head2 _GetCircControlBranch |
2430 |
- |
|
|