Lines 19-25
Link Here
|
19 |
|
19 |
|
20 |
use Modern::Perl; |
20 |
use Modern::Perl; |
21 |
|
21 |
|
22 |
use Test::More tests => 4; |
22 |
use Test::More tests => 5; |
23 |
use Test::Exception; |
23 |
use Test::Exception; |
24 |
|
24 |
|
25 |
use Koha::Account; |
25 |
use Koha::Account; |
Lines 27-32
use Koha::Account::Lines;
Link Here
|
27 |
use Koha::Account::Offsets; |
27 |
use Koha::Account::Offsets; |
28 |
use Koha::Items; |
28 |
use Koha::Items; |
29 |
|
29 |
|
|
|
30 |
use t::lib::Mocks; |
30 |
use t::lib::TestBuilder; |
31 |
use t::lib::TestBuilder; |
31 |
|
32 |
|
32 |
my $schema = Koha::Database->new->schema; |
33 |
my $schema = Koha::Database->new->schema; |
Lines 268-270
subtest 'apply() tests' => sub {
Link Here
|
268 |
|
269 |
|
269 |
$schema->storage->txn_rollback; |
270 |
$schema->storage->txn_rollback; |
270 |
}; |
271 |
}; |
271 |
- |
272 |
|
|
|
273 |
subtest 'adjust() tests' => sub { |
274 |
|
275 |
plan tests => 16; |
276 |
|
277 |
$schema->storage->txn_begin; |
278 |
|
279 |
# count logs before any actions |
280 |
my $action_logs = $schema->resultset('ActionLog')->search()->count; |
281 |
|
282 |
# Disable logs |
283 |
t::lib::Mocks::mock_preference( 'FinesLog', 0 ); |
284 |
|
285 |
my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
286 |
my $account = $patron->account; |
287 |
|
288 |
my $debit_1 = Koha::Account::Line->new( |
289 |
{ borrowernumber => $patron->id, |
290 |
accounttype => "F", |
291 |
amount => 10, |
292 |
amountoutstanding => 10 |
293 |
} |
294 |
)->store; |
295 |
|
296 |
my $debit_2 = Koha::Account::Line->new( |
297 |
{ borrowernumber => $patron->id, |
298 |
accounttype => "FU", |
299 |
amount => 100, |
300 |
amountoutstanding => 100 |
301 |
} |
302 |
)->store; |
303 |
|
304 |
my $credit = $account->add_credit( { amount => 40, user_id => $patron->id } ); |
305 |
|
306 |
throws_ok { $debit_1->adjust( { amount => 50, type => 'bad' } ) } |
307 |
qr/Update type not recognised/, 'Exception thrown for unrecognised type'; |
308 |
|
309 |
throws_ok { $debit_1->adjust( { amount => 50, type => 'fine_increment' } ) } |
310 |
qr/Update type not allowed on this accounttype/, |
311 |
'Exception thrown for type conflict'; |
312 |
|
313 |
# Increment an unpaid fine |
314 |
$debit_2->adjust( { amount => 150, type => 'fine_increment' } ); |
315 |
|
316 |
is( $debit_2->discard_changes->amount * 1, 150, 'Fine amount was updated in full' ); |
317 |
is( $debit_2->discard_changes->amountoutstanding * 1, 150, 'Fine amountoutstanding was update in full' ); |
318 |
|
319 |
my $offsets = Koha::Account::Offsets->search( { debit_id => $debit_2->id } ); |
320 |
is( $offsets->count, 1, 'An offset is generated for the increment' ); |
321 |
my $THIS_offset = $offsets->next; |
322 |
is( $THIS_offset->amount * 1, 50, 'Amount was calculated correctly (increment by 50)' ); |
323 |
is( $THIS_offset->type, 'Fine Update', 'Adjust type stored correctly' ); |
324 |
|
325 |
is( $schema->resultset('ActionLog')->count(), $action_logs + 0, 'No log was added' ); |
326 |
|
327 |
# Update fine to partially paid |
328 |
my $debits = Koha::Account::Lines->search({ accountlines_id => $debit_2->id }); |
329 |
$credit->apply( { debits => $debits, offset_type => 'Manual Credit' } ); |
330 |
|
331 |
is( $debit_2->discard_changes->amount * 1, 150, 'Fine amount unaffected by partial payment' ); |
332 |
is( $debit_2->discard_changes->amountoutstanding * 1, 110, 'Fine amountoutstanding updated by partial payment' ); |
333 |
|
334 |
# Enable logs |
335 |
t::lib::Mocks::mock_preference( 'FinesLog', 1 ); |
336 |
|
337 |
# Increment the partially paid fine |
338 |
$debit_2->adjust( { amount => 160, type => 'fine_increment' } ); |
339 |
|
340 |
is( $debit_2->discard_changes->amount * 1, 160, 'Fine amount was updated in full' ); |
341 |
is( $debit_2->discard_changes->amountoutstanding * 1, 120, 'Fine amountoutstanding was updated by difference' ); |
342 |
|
343 |
$offsets = Koha::Account::Offsets->search( { debit_id => $debit_2->id } ); |
344 |
is( $offsets->count, 3, 'An offset is generated for the increment' ); |
345 |
$THIS_offset = $offsets->last; |
346 |
is( $THIS_offset->amount * 1, 10, 'Amount was calculated correctly (increment by 10)' ); |
347 |
is( $THIS_offset->type, 'Fine Update', 'Adjust type stored correctly' ); |
348 |
|
349 |
is( $schema->resultset('ActionLog')->count(), $action_logs + 1, 'Log was added' ); |
350 |
|
351 |
$schema->storage->txn_rollback; |
352 |
}; |
353 |
|
354 |
1; |