Lines 2404-2415
sub _FixAccountForLostAndReturned {
Link Here
|
2404 |
); |
2404 |
); |
2405 |
|
2405 |
|
2406 |
return unless $accountlines->count > 0; |
2406 |
return unless $accountlines->count > 0; |
2407 |
my $accountline = $accountlines->next; |
2407 |
my $accountline = $accountlines->next; |
|
|
2408 |
my $total_to_refund = 0; |
2409 |
my $account = Koha::Patrons->find( $accountline->borrowernumber )->account; |
2408 |
|
2410 |
|
2409 |
# Use cases |
2411 |
# Use cases |
2410 |
if ( $accountline->amount > $accountline->amountoutstanding ) { |
2412 |
if ( $accountline->amount > $accountline->amountoutstanding ) { |
2411 |
# some amount has been cancelled. collect the offsets that are not writeoffs |
2413 |
# some amount has been cancelled. collect the offsets that are not writeoffs |
2412 |
# this works because the only way to subtract from a debt is |
2414 |
# this works because the only way to subtract from this kind of a debt is |
2413 |
# using the UI buttons 'Pay' and 'Write off' |
2415 |
# using the UI buttons 'Pay' and 'Write off' |
2414 |
my $credits_offsets = Koha::Account::Offsets->search({ |
2416 |
my $credits_offsets = Koha::Account::Offsets->search({ |
2415 |
debit_id => $accountline->id, |
2417 |
debit_id => $accountline->id, |
Lines 2418-2447
sub _FixAccountForLostAndReturned {
Link Here
|
2418 |
amount => { '<' => 0 } # credits are negative on the DB |
2420 |
amount => { '<' => 0 } # credits are negative on the DB |
2419 |
}); |
2421 |
}); |
2420 |
|
2422 |
|
2421 |
my $total_to_refund = ( $credits_offsets->count > 0 ) |
2423 |
$total_to_refund = ( $credits_offsets->count > 0 ) |
2422 |
? $credits_offsets->total * -1 # credits are negative on the DB |
2424 |
? $credits_offsets->total * -1 # credits are negative on the DB |
2423 |
: 0; |
2425 |
: 0; |
|
|
2426 |
} |
2424 |
|
2427 |
|
2425 |
if ( $total_to_refund > 0 ) { |
2428 |
my $credit_total = $accountline->amountoutstanding + $total_to_refund; |
2426 |
my $account = Koha::Patrons->find( $accountline->borrowernumber )->account; |
|
|
2427 |
$credit = $account->add_credit( |
2428 |
{ |
2429 |
amount => $total_to_refund, |
2430 |
description => 'Item Returned ' . $item_id, |
2431 |
type => 'lost_item_return' |
2432 |
} |
2433 |
); |
2434 |
} |
2435 |
|
2429 |
|
2436 |
ModItem( { paidfor => '' }, undef, $itemnumber, { log_action => 0 } ); |
2430 |
if ( $credit_total > 0 ) { |
|
|
2431 |
$credit = $account->add_credit( |
2432 |
{ amount => $credit_total, |
2433 |
description => 'Item Returned ' . $item_id, |
2434 |
type => 'lost_item_return' |
2435 |
} |
2436 |
); |
2437 |
|
2438 |
# TODO: ->apply should just accept the accountline |
2439 |
$credit->apply( { debits => $accountlines->reset } ); |
2437 |
} |
2440 |
} |
2438 |
# else { |
|
|
2439 |
# $accountline->amount == $accountline->amountoutstanding |
2440 |
#} |
2441 |
|
2441 |
|
2442 |
$accountline->accounttype('LR'); |
2442 |
# Manually set the accounttype |
2443 |
$accountline->amountoutstanding(0); |
2443 |
$accountline->discard_changes->accounttype('LR'); |
2444 |
$accountline->store(); |
2444 |
$accountline->store; |
|
|
2445 |
|
2446 |
ModItem( { paidfor => '' }, undef, $itemnumber, { log_action => 0 } ); |
2445 |
|
2447 |
|
2446 |
return ($credit) ? $credit->id : undef; |
2448 |
return ($credit) ? $credit->id : undef; |
2447 |
} |
2449 |
} |
2448 |
- |
|
|