|
Lines 1324-1329
sub _set_found_trigger {
Link Here
|
| 1324 |
} |
1324 |
} |
| 1325 |
} |
1325 |
} |
| 1326 |
|
1326 |
|
|
|
1327 |
my $processingreturn_policy = Koha::CirculationRules->get_processingreturn_policy( |
| 1328 |
{ |
| 1329 |
item => $self, |
| 1330 |
return_branch => C4::Context->userenv |
| 1331 |
? C4::Context->userenv->{'branch'} |
| 1332 |
: undef, |
| 1333 |
} |
| 1334 |
); |
| 1335 |
|
| 1336 |
if ( $processingreturn_policy ) { |
| 1337 |
|
| 1338 |
# refund processing charge made for lost book |
| 1339 |
my $processing_charge = Koha::Account::Lines->search( |
| 1340 |
{ |
| 1341 |
itemnumber => $self->itemnumber, |
| 1342 |
debit_type_code => 'PROCESSING', |
| 1343 |
status => [ undef, { '<>' => 'FOUND' } ] |
| 1344 |
}, |
| 1345 |
{ |
| 1346 |
order_by => { -desc => [ 'date', 'accountlines_id' ] }, |
| 1347 |
rows => 1 |
| 1348 |
} |
| 1349 |
)->single; |
| 1350 |
|
| 1351 |
if ( $processing_charge ) { |
| 1352 |
|
| 1353 |
my $patron = $processing_charge->patron; |
| 1354 |
if ( $patron ) { |
| 1355 |
|
| 1356 |
my $account = $patron->account; |
| 1357 |
|
| 1358 |
# Credit outstanding amount |
| 1359 |
my $credit_total = $processing_charge->amountoutstanding; |
| 1360 |
|
| 1361 |
# Use cases |
| 1362 |
if ( |
| 1363 |
$processing_charge->amount > $processing_charge->amountoutstanding && |
| 1364 |
$processingreturn_policy ne "refund_unpaid" |
| 1365 |
) { |
| 1366 |
# some amount has been cancelled. collect the offsets that are not writeoffs |
| 1367 |
# this works because the only way to subtract from this kind of a debt is |
| 1368 |
# using the UI buttons 'Pay' and 'Write off' |
| 1369 |
|
| 1370 |
# We don't credit any payments if return policy is |
| 1371 |
# "refund_unpaid" |
| 1372 |
# |
| 1373 |
# In that case only unpaid/outstanding amount |
| 1374 |
# will be credited which settles the debt without |
| 1375 |
# creating extra credits |
| 1376 |
|
| 1377 |
my $credit_offsets = $processing_charge->debit_offsets( |
| 1378 |
{ |
| 1379 |
'credit_id' => { '!=' => undef }, |
| 1380 |
'credit.credit_type_code' => { '!=' => 'Writeoff' } |
| 1381 |
}, |
| 1382 |
{ join => 'credit' } |
| 1383 |
); |
| 1384 |
|
| 1385 |
my $total_to_refund = ( $credit_offsets->count > 0 ) ? |
| 1386 |
# credits are negative on the DB |
| 1387 |
$credit_offsets->total * -1 : |
| 1388 |
0; |
| 1389 |
# Credit the outstanding amount, then add what has been |
| 1390 |
# paid to create a net credit for this amount |
| 1391 |
$credit_total += $total_to_refund; |
| 1392 |
} |
| 1393 |
|
| 1394 |
my $credit; |
| 1395 |
if ( $credit_total > 0 ) { |
| 1396 |
my $branchcode = |
| 1397 |
C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef; |
| 1398 |
$credit = $account->add_credit( |
| 1399 |
{ |
| 1400 |
amount => $credit_total, |
| 1401 |
description => 'Item found ' . $self->itemnumber, |
| 1402 |
type => 'PROCESSING_FOUND', |
| 1403 |
interface => C4::Context->interface, |
| 1404 |
library_id => $branchcode, |
| 1405 |
item_id => $self->itemnumber, |
| 1406 |
issue_id => $processing_charge->issue_id |
| 1407 |
} |
| 1408 |
); |
| 1409 |
|
| 1410 |
$credit->apply( { debits => [$processing_charge] } ); |
| 1411 |
$self->add_message( |
| 1412 |
{ |
| 1413 |
type => 'info', |
| 1414 |
message => 'processing_refunded', |
| 1415 |
payload => { credit_id => $credit->id } |
| 1416 |
} |
| 1417 |
); |
| 1418 |
} |
| 1419 |
|
| 1420 |
# Update the account status |
| 1421 |
$processing_charge->status('FOUND'); |
| 1422 |
$processing_charge->store(); |
| 1423 |
|
| 1424 |
# Reconcile balances if required |
| 1425 |
if ( C4::Context->preference('AccountAutoReconcile') ) { |
| 1426 |
$account->reconcile_balance; |
| 1427 |
} |
| 1428 |
} |
| 1429 |
} |
| 1430 |
} |
| 1431 |
|
| 1327 |
return $self; |
1432 |
return $self; |
| 1328 |
} |
1433 |
} |
| 1329 |
|
1434 |
|