Bug 24603: Simplify tests using TestBuilder
[koha.git] / t / db_dependent / Koha / Account / Line.t
blob4a18526dac31e0c6be3113359b98d5f7e131abaa
1 #!/usr/bin/perl
3 # Copyright 2018 Koha Development team
5 # This file is part of Koha
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>
20 use Modern::Perl;
22 use Test::More tests => 14;
23 use Test::Exception;
24 use Test::MockModule;
26 use DateTime;
28 use C4::Circulation qw/AddIssue AddReturn/;
29 use Koha::Account;
30 use Koha::Account::Lines;
31 use Koha::Account::Offsets;
32 use Koha::Items;
33 use Koha::DateUtils qw( dt_from_string );
35 use t::lib::Mocks;
36 use t::lib::TestBuilder;
38 my $schema = Koha::Database->new->schema;
39 my $builder = t::lib::TestBuilder->new;
41 subtest 'patron() tests' => sub {
43 plan tests => 3;
45 $schema->storage->txn_begin;
47 my $library = $builder->build( { source => 'Branch' } );
48 my $patron = $builder->build( { source => 'Borrower' } );
50 my $line = Koha::Account::Line->new(
52 borrowernumber => $patron->{borrowernumber},
53 debit_type_code => "OVERDUE",
54 status => "RETURNED",
55 amount => 10,
56 interface => 'commandline',
57 })->store;
59 my $account_line_patron = $line->patron;
60 is( ref( $account_line_patron ), 'Koha::Patron', 'Koha::Account::Line->patron should return a Koha::Patron' );
61 is( $line->borrowernumber, $account_line_patron->borrowernumber, 'Koha::Account::Line->patron should return the correct borrower' );
63 $line->borrowernumber(undef)->store;
64 is( $line->patron, undef, 'Koha::Account::Line->patron should return undef if no patron linked' );
66 $schema->storage->txn_rollback;
69 subtest 'item() tests' => sub {
71 plan tests => 3;
73 $schema->storage->txn_begin;
75 my $library = $builder->build( { source => 'Branch' } );
76 my $patron = $builder->build( { source => 'Borrower' } );
77 my $item = $builder->build_sample_item(
79 library => $library->{branchcode},
80 barcode => 'some_barcode_12',
81 itype => 'BK',
85 my $line = Koha::Account::Line->new(
87 borrowernumber => $patron->{borrowernumber},
88 itemnumber => $item->itemnumber,
89 debit_type_code => "OVERDUE",
90 status => "RETURNED",
91 amount => 10,
92 interface => 'commandline',
93 })->store;
95 my $account_line_item = $line->item;
96 is( ref( $account_line_item ), 'Koha::Item', 'Koha::Account::Line->item should return a Koha::Item' );
97 is( $line->itemnumber, $account_line_item->itemnumber, 'Koha::Account::Line->item should return the correct item' );
99 $line->itemnumber(undef)->store;
100 is( $line->item, undef, 'Koha::Account::Line->item should return undef if no item linked' );
102 $schema->storage->txn_rollback;
105 subtest 'library() tests' => sub {
107 plan tests => 4;
109 $schema->storage->txn_begin;
111 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
112 my $patron = $builder->build( { source => 'Borrower' } );
114 my $line = Koha::Account::Line->new(
116 borrowernumber => $patron->{borrowernumber},
117 branchcode => $library->branchcode,
118 debit_type_code => "OVERDUE",
119 status => "RETURNED",
120 amount => 10,
121 interface => 'commandline',
123 )->store;
125 my $account_line_library = $line->library;
126 is( ref($account_line_library),
127 'Koha::Library',
128 'Koha::Account::Line->library should return a Koha::Library' );
130 $line->branchcode,
131 $account_line_library->branchcode,
132 'Koha::Account::Line->library should return the correct library'
135 # Test ON DELETE SET NULL
136 $library->delete;
137 my $found = Koha::Account::Lines->find( $line->accountlines_id );
138 ok( $found, "Koha::Account::Line not deleted when the linked library is deleted" );
140 is( $found->library, undef,
141 'Koha::Account::Line->library should return undef if linked library has been deleted'
144 $schema->storage->txn_rollback;
147 subtest 'is_credit() and is_debit() tests' => sub {
149 plan tests => 4;
151 $schema->storage->txn_begin;
153 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
154 my $account = $patron->account;
156 my $credit = $account->add_credit({ amount => 100, user_id => $patron->id, interface => 'commandline' });
158 ok( $credit->is_credit, 'is_credit detects credits' );
159 ok( !$credit->is_debit, 'is_debit detects credits' );
161 my $debit = Koha::Account::Line->new(
163 borrowernumber => $patron->id,
164 debit_type_code => "OVERDUE",
165 status => "RETURNED",
166 amount => 10,
167 interface => 'commandline',
168 })->store;
170 ok( !$debit->is_credit, 'is_credit detects debits' );
171 ok( $debit->is_debit, 'is_debit detects debits');
173 $schema->storage->txn_rollback;
176 subtest 'apply() tests' => sub {
178 plan tests => 25;
180 $schema->storage->txn_begin;
182 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
183 my $account = $patron->account;
185 my $credit = $account->add_credit( { amount => 100, user_id => $patron->id, interface => 'commandline' } );
187 my $debit_1 = Koha::Account::Line->new(
188 { borrowernumber => $patron->id,
189 debit_type_code => "OVERDUE",
190 status => "RETURNED",
191 amount => 10,
192 amountoutstanding => 10,
193 interface => 'commandline',
195 )->store;
197 my $debit_2 = Koha::Account::Line->new(
198 { borrowernumber => $patron->id,
199 debit_type_code => "OVERDUE",
200 status => "RETURNED",
201 amount => 100,
202 amountoutstanding => 100,
203 interface => 'commandline',
205 )->store;
207 $credit->discard_changes;
208 $debit_1->discard_changes;
210 my $debits = Koha::Account::Lines->search({ accountlines_id => $debit_1->id });
211 my $remaining_credit = $credit->apply( { debits => [ $debits->as_list ], offset_type => 'Manual Credit' } );
212 is( $remaining_credit * 1, 90, 'Remaining credit is correctly calculated' );
213 $credit->discard_changes;
214 is( $credit->amountoutstanding * -1, $remaining_credit, 'Remaining credit correctly stored' );
216 # re-read debit info
217 $debit_1->discard_changes;
218 is( $debit_1->amountoutstanding * 1, 0, 'Debit has been cancelled' );
220 my $offsets = Koha::Account::Offsets->search( { credit_id => $credit->id, debit_id => $debit_1->id } );
221 is( $offsets->count, 1, 'Only one offset is generated' );
222 my $THE_offset = $offsets->next;
223 is( $THE_offset->amount * 1, -10, 'Amount was calculated correctly (less than the available credit)' );
224 is( $THE_offset->type, 'Manual Credit', 'Passed type stored correctly' );
226 $debits = Koha::Account::Lines->search({ accountlines_id => $debit_2->id });
227 $remaining_credit = $credit->apply( { debits => [ $debits->as_list ] } );
228 is( $remaining_credit, 0, 'No remaining credit left' );
229 $credit->discard_changes;
230 is( $credit->amountoutstanding * 1, 0, 'No outstanding credit' );
231 $debit_2->discard_changes;
232 is( $debit_2->amountoutstanding * 1, 10, 'Outstanding amount decremented correctly' );
234 $offsets = Koha::Account::Offsets->search( { credit_id => $credit->id, debit_id => $debit_2->id } );
235 is( $offsets->count, 1, 'Only one offset is generated' );
236 $THE_offset = $offsets->next;
237 is( $THE_offset->amount * 1, -90, 'Amount was calculated correctly (less than the available credit)' );
238 is( $THE_offset->type, 'Credit Applied', 'Defaults to \'Credit Applied\' offset type' );
240 $debits = Koha::Account::Lines->search({ accountlines_id => $debit_1->id });
241 throws_ok
242 { $credit->apply({ debits => [ $debits->as_list ] }); }
243 'Koha::Exceptions::Account::NoAvailableCredit',
244 '->apply() can only be used with outstanding credits';
246 $debits = Koha::Account::Lines->search({ accountlines_id => $credit->id });
247 throws_ok
248 { $debit_1->apply({ debits => [ $debits->as_list ] }); }
249 'Koha::Exceptions::Account::IsNotCredit',
250 '->apply() can only be used with credits';
252 $debits = Koha::Account::Lines->search({ accountlines_id => $credit->id });
253 my $credit_3 = $account->add_credit({ amount => 1, interface => 'commandline' });
254 throws_ok
255 { $credit_3->apply({ debits => [ $debits->as_list ] }); }
256 'Koha::Exceptions::Account::IsNotDebit',
257 '->apply() can only be applied to credits';
259 my $credit_2 = $account->add_credit({ amount => 20, interface => 'commandline' });
260 my $debit_3 = Koha::Account::Line->new(
261 { borrowernumber => $patron->id,
262 debit_type_code => "OVERDUE",
263 status => "RETURNED",
264 amount => 100,
265 amountoutstanding => 100,
266 interface => 'commandline',
268 )->store;
270 $debits = Koha::Account::Lines->search({ accountlines_id => { -in => [ $debit_1->id, $debit_2->id, $debit_3->id, $credit->id ] } });
271 throws_ok {
272 $credit_2->apply( { debits => [ $debits->as_list ], offset_type => 'Manual Credit' } ); }
273 'Koha::Exceptions::Account::IsNotDebit',
274 '->apply() rolls back if any of the passed lines is not a debit';
276 is( $debit_1->discard_changes->amountoutstanding * 1, 0, 'No changes to already cancelled debit' );
277 is( $debit_2->discard_changes->amountoutstanding * 1, 10, 'Debit cancelled' );
278 is( $debit_3->discard_changes->amountoutstanding * 1, 100, 'Outstanding amount correctly calculated' );
279 is( $credit_2->discard_changes->amountoutstanding * -1, 20, 'No changes made' );
281 $debits = Koha::Account::Lines->search({ accountlines_id => { -in => [ $debit_1->id, $debit_2->id, $debit_3->id ] } });
282 $remaining_credit = $credit_2->apply( { debits => [ $debits->as_list ], offset_type => 'Manual Credit' } );
284 is( $debit_1->discard_changes->amountoutstanding * 1, 0, 'No changes to already cancelled debit' );
285 is( $debit_2->discard_changes->amountoutstanding * 1, 0, 'Debit cancelled' );
286 is( $debit_3->discard_changes->amountoutstanding * 1, 90, 'Outstanding amount correctly calculated' );
287 is( $credit_2->discard_changes->amountoutstanding * 1, 0, 'No remaining credit' );
289 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
290 my $biblio = $builder->build_sample_biblio();
291 my $item =
292 $builder->build_sample_item( { biblionumber => $biblio->biblionumber } );
293 my $now = dt_from_string();
294 my $seven_weeks = DateTime::Duration->new(weeks => 7);
295 my $five_weeks = DateTime::Duration->new(weeks => 5);
296 my $seven_weeks_ago = $now - $seven_weeks;
297 my $five_weeks_ago = $now - $five_weeks;
299 my $checkout = Koha::Checkout->new(
301 borrowernumber => $patron->id,
302 itemnumber => $item->id,
303 date_due => $five_weeks_ago,
304 branchcode => $library->id,
305 issuedate => $seven_weeks_ago
307 )->store();
309 my $accountline = Koha::Account::Line->new(
311 issue_id => $checkout->id,
312 borrowernumber => $patron->id,
313 itemnumber => $item->id,
314 branchcode => $library->id,
315 date => \'NOW()',
316 debit_type_code => 'OVERDUE',
317 status => 'UNRETURNED',
318 interface => 'cli',
319 amount => '1',
320 amountoutstanding => '1',
322 )->store();
324 # Enable renewing upon fine payment
325 t::lib::Mocks::mock_preference( 'RenewAccruingItemWhenPaid', 1 );
326 my $called = 0;
327 my $module = Test::MockModule->new('C4::Circulation');
328 $module->mock('AddRenewal', sub { $called = 1; });
329 $module->mock('CanBookBeRenewed', sub { return 1; });
330 my $credit_renew = $account->add_credit({ amount => 100, user_id => $patron->id, interface => 'commandline' });
331 my $debits_renew = Koha::Account::Lines->search({ accountlines_id => $accountline->id })->as_list;
332 $credit_renew->apply( { debits => $debits_renew, offset_type => 'Manual Credit' } );
334 is( $called, 1, 'RenewAccruingItemWhenPaid causes C4::Circulation::AddRenew to be called when appropriate' );
336 $schema->storage->txn_rollback;
339 subtest 'Keep account info when related patron, staff, item or cash_register is deleted' => sub {
341 plan tests => 4;
343 $schema->storage->txn_begin;
345 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
346 my $staff = $builder->build_object( { class => 'Koha::Patrons' } );
347 my $item = $builder->build_sample_item;
348 my $issue = $builder->build_object(
350 class => 'Koha::Checkouts',
351 value => { itemnumber => $item->itemnumber }
354 my $register = $builder->build_object({ class => 'Koha::Cash::Registers' });
356 my $line = Koha::Account::Line->new(
358 borrowernumber => $patron->borrowernumber,
359 manager_id => $staff->borrowernumber,
360 itemnumber => $item->itemnumber,
361 debit_type_code => "OVERDUE",
362 status => "RETURNED",
363 amount => 10,
364 interface => 'commandline',
365 register_id => $register->id
366 })->store;
368 $issue->delete;
369 $item->delete;
370 $line = $line->get_from_storage;
371 is( $line->itemnumber, undef, "The account line should not be deleted when the related item is delete");
373 $staff->delete;
374 $line = $line->get_from_storage;
375 is( $line->manager_id, undef, "The account line should not be deleted when the related staff is delete");
377 $patron->delete;
378 $line = $line->get_from_storage;
379 is( $line->borrowernumber, undef, "The account line should not be deleted when the related patron is delete");
381 $register->delete;
382 $line = $line->get_from_storage;
383 is( $line->register_id, undef, "The account line should not be deleted when the related cash register is delete");
385 $schema->storage->txn_rollback;
388 subtest 'Renewal related tests' => sub {
390 plan tests => 7;
392 $schema->storage->txn_begin;
394 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
395 my $staff = $builder->build_object( { class => 'Koha::Patrons' } );
396 my $item = $builder->build_object({ class => 'Koha::Items' });
397 my $issue = $builder->build_object(
399 class => 'Koha::Checkouts',
400 value => {
401 itemnumber => $item->itemnumber,
402 onsite_checkout => 0,
403 renewals => 99,
404 auto_renew => 0
408 my $line = Koha::Account::Line->new(
410 borrowernumber => $patron->borrowernumber,
411 manager_id => $staff->borrowernumber,
412 itemnumber => $item->itemnumber,
413 debit_type_code => "OVERDUE",
414 status => "UNRETURNED",
415 amountoutstanding => 0,
416 interface => 'commandline',
417 })->store;
419 is( $line->renewable, 1, "Item is returned as renewable when it meets the conditions" );
420 $line->amountoutstanding(5);
421 is( $line->renewable, 0, "Item is returned as unrenewable when it has outstanding fine" );
422 $line->amountoutstanding(0);
423 $line->debit_type_code("VOID");
424 is( $line->renewable, 0, "Item is returned as unrenewable when it has the wrong account type" );
425 $line->debit_type_code("OVERDUE");
426 $line->status("RETURNED");
427 is( $line->renewable, 0, "Item is returned as unrenewable when it has the wrong account status" );
430 t::lib::Mocks::mock_preference( 'RenewAccruingItemWhenPaid', 0 );
431 is ($line->renew_item, undef, 'Attempt to renew fails when syspref is not set');
432 t::lib::Mocks::mock_preference( 'RenewAccruingItemWhenPaid', 1 );
433 is_deeply(
434 $line->renew_item,
436 itemnumber => $item->itemnumber,
437 error => 'too_many',
438 success => 0
440 'Attempt to renew fails when CanBookBeRenewed returns false'
442 $issue->delete;
443 $issue = $builder->build_object(
445 class => 'Koha::Checkouts',
446 value => {
447 itemnumber => $item->itemnumber,
448 onsite_checkout => 0,
449 renewals => 0,
450 auto_renew => 0
454 my $called = 0;
455 my $module = Test::MockModule->new('C4::Circulation');
456 $module->mock('AddRenewal', sub { $called = 1; });
457 $module->mock('CanBookBeRenewed', sub { return 1; });
458 $line->renew_item;
459 is( $called, 1, 'Attempt to renew succeeds when conditions are met' );
461 $schema->storage->txn_rollback;
464 subtest 'adjust() tests' => sub {
466 plan tests => 29;
468 $schema->storage->txn_begin;
470 # count logs before any actions
471 my $action_logs = $schema->resultset('ActionLog')->search()->count;
473 # Disable logs
474 t::lib::Mocks::mock_preference( 'FinesLog', 0 );
476 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
477 my $account = $patron->account;
479 my $debit_1 = Koha::Account::Line->new(
480 { borrowernumber => $patron->id,
481 debit_type_code => "OVERDUE",
482 status => "RETURNED",
483 amount => 10,
484 amountoutstanding => 10,
485 interface => 'commandline',
487 )->store;
489 my $debit_2 = Koha::Account::Line->new(
490 { borrowernumber => $patron->id,
491 debit_type_code => "OVERDUE",
492 status => "UNRETURNED",
493 amount => 100,
494 amountoutstanding => 100,
495 interface => 'commandline'
497 )->store;
499 my $credit = $account->add_credit( { amount => 40, user_id => $patron->id, interface => 'commandline' } );
501 throws_ok { $debit_1->adjust( { amount => 50, type => 'bad', interface => 'commandline' } ) }
502 qr/Update type not recognised/, 'Exception thrown for unrecognised type';
504 throws_ok { $debit_1->adjust( { amount => 50, type => 'overdue_update', interface => 'commandline' } ) }
505 qr/Update type not allowed on this debit_type/,
506 'Exception thrown for type conflict';
508 # Increment an unpaid fine
509 $debit_2->adjust( { amount => 150, type => 'overdue_update', interface => 'commandline' } )->discard_changes;
511 is( $debit_2->amount * 1, 150, 'Fine amount was updated in full' );
512 is( $debit_2->amountoutstanding * 1, 150, 'Fine amountoutstanding was update in full' );
513 isnt( $debit_2->date, undef, 'Date has been set' );
515 my $offsets = Koha::Account::Offsets->search( { debit_id => $debit_2->id } );
516 is( $offsets->count, 1, 'An offset is generated for the increment' );
517 my $THIS_offset = $offsets->next;
518 is( $THIS_offset->amount * 1, 50, 'Amount was calculated correctly (increment by 50)' );
519 is( $THIS_offset->type, 'OVERDUE_INCREASE', 'Adjust type stored correctly' );
521 is( $schema->resultset('ActionLog')->count(), $action_logs + 0, 'No log was added' );
523 # Update fine to partially paid
524 my $debits = Koha::Account::Lines->search({ accountlines_id => $debit_2->id });
525 $credit->apply( { debits => [ $debits->as_list ], offset_type => 'Manual Credit' } );
527 $debit_2->discard_changes;
528 is( $debit_2->amount * 1, 150, 'Fine amount unaffected by partial payment' );
529 is( $debit_2->amountoutstanding * 1, 110, 'Fine amountoutstanding updated by partial payment' );
531 # Enable logs
532 t::lib::Mocks::mock_preference( 'FinesLog', 1 );
534 # Increment the partially paid fine
535 $debit_2->adjust( { amount => 160, type => 'overdue_update', interface => 'commandline' } )->discard_changes;
537 is( $debit_2->amount * 1, 160, 'Fine amount was updated in full' );
538 is( $debit_2->amountoutstanding * 1, 120, 'Fine amountoutstanding was updated by difference' );
540 $offsets = Koha::Account::Offsets->search( { debit_id => $debit_2->id } );
541 is( $offsets->count, 3, 'An offset is generated for the increment' );
542 $THIS_offset = $offsets->last;
543 is( $THIS_offset->amount * 1, 10, 'Amount was calculated correctly (increment by 10)' );
544 is( $THIS_offset->type, 'OVERDUE_INCREASE', 'Adjust type stored correctly' );
546 is( $schema->resultset('ActionLog')->count(), $action_logs + 1, 'Log was added' );
548 # Decrement the partially paid fine, less than what was paid
549 $debit_2->adjust( { amount => 50, type => 'overdue_update', interface => 'commandline' } )->discard_changes;
551 is( $debit_2->amount * 1, 50, 'Fine amount was updated in full' );
552 is( $debit_2->amountoutstanding * 1, 10, 'Fine amountoutstanding was updated by difference' );
554 $offsets = Koha::Account::Offsets->search( { debit_id => $debit_2->id } );
555 is( $offsets->count, 4, 'An offset is generated for the decrement' );
556 $THIS_offset = $offsets->last;
557 is( $THIS_offset->amount * 1, -110, 'Amount was calculated correctly (decrement by 110)' );
558 is( $THIS_offset->type, 'OVERDUE_DECREASE', 'Adjust type stored correctly' );
560 # Decrement the partially paid fine, more than what was paid
561 $debit_2->adjust( { amount => 30, type => 'overdue_update', interface => 'commandline' } )->discard_changes;
562 is( $debit_2->amount * 1, 30, 'Fine amount was updated in full' );
563 is( $debit_2->amountoutstanding * 1, 0, 'Fine amountoutstanding was zeroed (payment was 40)' );
565 $offsets = Koha::Account::Offsets->search( { debit_id => $debit_2->id } );
566 is( $offsets->count, 5, 'An offset is generated for the decrement' );
567 $THIS_offset = $offsets->last;
568 is( $THIS_offset->amount * 1, -20, 'Amount was calculated correctly (decrement by 20)' );
569 is( $THIS_offset->type, 'OVERDUE_DECREASE', 'Adjust type stored correctly' );
571 my $overpayment_refund = $account->lines->last;
572 is( $overpayment_refund->amount * 1, -10, 'A new credit has been added' );
573 is( $overpayment_refund->credit_type_code, 'OVERPAYMENT', 'Credit generated with the expected credit_type_code' );
575 $schema->storage->txn_rollback;
578 subtest 'checkout() tests' => sub {
579 plan tests => 6;
581 $schema->storage->txn_begin;
583 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
584 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
585 my $item = $builder->build_sample_item;
586 my $account = $patron->account;
588 t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode });
589 my $checkout = AddIssue( $patron->unblessed, $item->barcode );
591 my $line = $account->add_debit({
592 amount => 10,
593 interface => 'commandline',
594 item_id => $item->itemnumber,
595 issue_id => $checkout->issue_id,
596 type => 'OVERDUE',
599 my $line_checkout = $line->checkout;
600 is( ref($line_checkout), 'Koha::Checkout', 'Result type is correct' );
601 is( $line_checkout->issue_id, $checkout->issue_id, 'Koha::Account::Line->checkout should return the correct checkout');
603 my ( $returned, undef, $old_checkout) = C4::Circulation::AddReturn( $item->barcode, $library->branchcode );
604 is( $returned, 1, 'The item should have been returned' );
606 $line = $line->get_from_storage;
607 my $old_line_checkout = $line->checkout;
608 is( ref($old_line_checkout), 'Koha::Old::Checkout', 'Result type is correct' );
609 is( $old_line_checkout->issue_id, $old_checkout->issue_id, 'Koha::Account::Line->checkout should return the correct old_checkout' );
611 $line->issue_id(undef)->store;
612 is( $line->checkout, undef, 'Koha::Account::Line->checkout should return undef if no checkout linked' );
614 $schema->storage->txn_rollback;
617 subtest 'credits() and debits() tests' => sub {
618 plan tests => 12;
620 $schema->storage->txn_begin;
622 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
623 my $account = $patron->account;
625 my $debit1 = $account->add_debit({
626 amount => 8,
627 interface => 'commandline',
628 type => 'ACCOUNT',
630 my $debit2 = $account->add_debit({
631 amount => 12,
632 interface => 'commandline',
633 type => 'ACCOUNT',
635 my $credit1 = $account->add_credit({
636 amount => 5,
637 interface => 'commandline',
638 type => 'CREDIT',
640 my $credit2 = $account->add_credit({
641 amount => 10,
642 interface => 'commandline',
643 type => 'CREDIT',
646 $credit1->apply({ debits => [ $debit1 ] });
647 $credit2->apply({ debits => [ $debit1, $debit2 ] });
649 my $credits = $debit1->credits;
650 is($credits->count, 2, '2 Credits applied to debit 1');
651 my $credit = $credits->next;
652 is($credit->amount + 0, -5, 'Correct first credit');
653 $credit = $credits->next;
654 is($credit->amount + 0, -10, 'Correct second credit');
656 $credits = $debit2->credits;
657 is($credits->count, 1, '1 Credits applied to debit 2');
658 $credit = $credits->next;
659 is($credit->amount + 0, -10, 'Correct first credit');
661 my $debits = $credit1->debits;
662 is($debits->count, 1, 'Credit 1 applied to 1 debit');
663 my $debit = $debits->next;
664 is($debit->amount + 0, 8, 'Correct first debit');
666 $debits = $credit2->debits;
667 is($debits->count, 2, 'Credit 2 applied to 2 debits');
668 $debit = $debits->next;
669 is($debit->amount + 0, 8, 'Correct first debit');
670 $debit = $debits->next;
671 is($debit->amount + 0, 12, 'Correct second debit');
673 throws_ok
674 { $debit1->debits; }
675 'Koha::Exceptions::Account::IsNotCredit',
676 'Exception is thrown when requesting debits linked to debit';
678 throws_ok
679 { $credit1->credits; }
680 'Koha::Exceptions::Account::IsNotDebit',
681 'Exception is thrown when requesting credits linked to credit';
684 $schema->storage->txn_rollback;
687 subtest "void() tests" => sub {
689 plan tests => 16;
691 $schema->storage->txn_begin;
693 # Create a borrower
694 my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
695 my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode };
697 my $borrower = Koha::Patron->new( {
698 cardnumber => 'dariahall',
699 surname => 'Hall',
700 firstname => 'Daria',
701 } );
702 $borrower->categorycode( $categorycode );
703 $borrower->branchcode( $branchcode );
704 $borrower->store;
706 my $account = Koha::Account->new({ patron_id => $borrower->id });
708 my $line1 = Koha::Account::Line->new(
710 borrowernumber => $borrower->borrowernumber,
711 amount => 10,
712 amountoutstanding => 10,
713 interface => 'commandline',
714 debit_type_code => 'OVERDUE'
716 )->store();
717 my $line2 = Koha::Account::Line->new(
719 borrowernumber => $borrower->borrowernumber,
720 amount => 20,
721 amountoutstanding => 20,
722 interface => 'commandline',
723 debit_type_code => 'OVERDUE'
725 )->store();
727 is( $account->balance(), 30, "Account balance is 30" );
728 is( $line1->amountoutstanding, 10, 'First fee has amount outstanding of 10' );
729 is( $line2->amountoutstanding, 20, 'Second fee has amount outstanding of 20' );
731 my $id = $account->pay(
733 lines => [$line1, $line2],
734 amount => 30,
736 )->{payment_id};
738 my $account_payment = Koha::Account::Lines->find( $id );
740 is( $account->balance(), 0, "Account balance is 0" );
742 $line1->_result->discard_changes();
743 $line2->_result->discard_changes();
744 is( $line1->amountoutstanding+0, 0, 'First fee has amount outstanding of 0' );
745 is( $line2->amountoutstanding+0, 0, 'Second fee has amount outstanding of 0' );
747 my $ret = $account_payment->void();
749 is( ref($ret), 'Koha::Account::Line', 'Void returns the account line' );
750 is( $account->balance(), 30, "Account balance is again 30" );
752 $account_payment->_result->discard_changes();
753 $line1->_result->discard_changes();
754 $line2->_result->discard_changes();
756 is( $account_payment->credit_type_code, 'PAYMENT', 'Voided payment credit_type_code is still PAYMENT' );
757 is( $account_payment->status, 'VOID', 'Voided payment status is VOID' );
758 is( $account_payment->amount+0, 0, 'Voided payment amount is 0' );
759 is( $account_payment->amountoutstanding+0, 0, 'Voided payment amount outstanding is 0' );
761 is( $line1->amountoutstanding+0, 10, 'First fee again has amount outstanding of 10' );
762 is( $line2->amountoutstanding+0, 20, 'Second fee again has amount outstanding of 20' );
764 # Accountlines that are not credits should be un-voidable
765 my $line1_pre = $line1->unblessed();
766 $ret = $line1->void();
767 $line1->_result->discard_changes();
768 my $line1_post = $line1->unblessed();
769 is( $ret, undef, 'Attempted void on non-credit returns undef' );
770 is_deeply( $line1_pre, $line1_post, 'Non-credit account line cannot be voided' );
772 $schema->storage->txn_rollback;
775 subtest "payout() tests" => sub {
777 plan tests => 18;
779 $schema->storage->txn_begin;
781 # Create a borrower
782 my $categorycode =
783 $builder->build( { source => 'Category' } )->{categorycode};
784 my $branchcode = $builder->build( { source => 'Branch' } )->{branchcode};
786 my $borrower = Koha::Patron->new(
788 cardnumber => 'dariahall',
789 surname => 'Hall',
790 firstname => 'Daria',
793 $borrower->categorycode($categorycode);
794 $borrower->branchcode($branchcode);
795 $borrower->store;
797 my $staff = Koha::Patron->new(
799 cardnumber => 'bobby',
800 surname => 'Bloggs',
801 firstname => 'Bobby',
804 $staff->categorycode($categorycode);
805 $staff->branchcode($branchcode);
806 $staff->store;
808 my $account = Koha::Account->new( { patron_id => $borrower->id } );
810 my $debit1 = Koha::Account::Line->new(
812 borrowernumber => $borrower->borrowernumber,
813 amount => 10,
814 amountoutstanding => 10,
815 interface => 'commandline',
816 debit_type_code => 'OVERDUE'
818 )->store();
819 my $credit1 = Koha::Account::Line->new(
821 borrowernumber => $borrower->borrowernumber,
822 amount => -20,
823 amountoutstanding => -20,
824 interface => 'commandline',
825 credit_type_code => 'CREDIT'
827 )->store();
829 is( $account->balance(), -10, "Account balance is -10" );
830 is( $debit1->amountoutstanding + 0,
831 10, 'Overdue fee has an amount outstanding of 10' );
832 is( $credit1->amountoutstanding + 0,
833 -20, 'Credit has an amount outstanding of -20' );
835 my $pay_params = {
836 interface => 'intranet',
837 staff_id => $staff->borrowernumber,
838 branch => $branchcode,
839 payout_type => 'CASH',
840 amount => 20
843 throws_ok { $debit1->payout($pay_params); }
844 'Koha::Exceptions::Account::IsNotCredit',
845 '->payout() can only be used with credits';
847 my @required =
848 ( 'interface', 'staff_id', 'branch', 'payout_type', 'amount' );
849 for my $required (@required) {
850 my $params = {%$pay_params};
851 delete( $params->{$required} );
852 throws_ok {
853 $credit1->payout($params);
855 'Koha::Exceptions::MissingParameter',
856 "->payout() requires the `$required` parameter is passed";
859 throws_ok {
860 $credit1->payout(
862 interface => 'intranet',
863 staff_id => $staff->borrowernumber,
864 branch => $branchcode,
865 payout_type => 'CASH',
866 amount => 25
870 'Koha::Exceptions::ParameterTooHigh',
871 '->payout() cannot pay out more than the amountoutstanding';
873 t::lib::Mocks::mock_preference( 'UseCashRegisters', 1 );
874 throws_ok {
875 $credit1->payout(
877 interface => 'intranet',
878 staff_id => $staff->borrowernumber,
879 branch => $branchcode,
880 payout_type => 'CASH',
881 amount => 10
885 'Koha::Exceptions::Account::RegisterRequired',
886 '->payout() requires a cash_register if payout_type is `CASH`';
888 t::lib::Mocks::mock_preference( 'UseCashRegisters', 0 );
889 my $payout = $credit1->payout(
891 interface => 'intranet',
892 staff_id => $staff->borrowernumber,
893 branch => $branchcode,
894 payout_type => 'CASH',
895 amount => 10
899 is( ref($payout), 'Koha::Account::Line',
900 '->payout() returns a Koha::Account::Line' );
901 is( $payout->amount() + 0, 10, "Payout amount is 10" );
902 is( $payout->amountoutstanding() + 0, 0, "Payout amountoutstanding is 0" );
903 is( $account->balance() + 0, 0, "Account balance is 0" );
904 is( $debit1->amountoutstanding + 0,
905 10, 'Overdue fee still has an amount outstanding of 10' );
906 is( $credit1->amountoutstanding + 0,
907 -10, 'Credit has an new amount outstanding of -10' );
908 is( $credit1->status(), 'PAID', "Credit has a new status of PAID" );
910 $schema->storage->txn_rollback;
913 subtest "reduce() tests" => sub {
915 plan tests => 29;
917 $schema->storage->txn_begin;
919 # Create a borrower
920 my $categorycode =
921 $builder->build( { source => 'Category' } )->{categorycode};
922 my $branchcode = $builder->build( { source => 'Branch' } )->{branchcode};
924 my $borrower = Koha::Patron->new(
926 cardnumber => 'dariahall',
927 surname => 'Hall',
928 firstname => 'Daria',
931 $borrower->categorycode($categorycode);
932 $borrower->branchcode($branchcode);
933 $borrower->store;
935 my $staff = Koha::Patron->new(
937 cardnumber => 'bobby',
938 surname => 'Bloggs',
939 firstname => 'Bobby',
942 $staff->categorycode($categorycode);
943 $staff->branchcode($branchcode);
944 $staff->store;
946 my $account = Koha::Account->new( { patron_id => $borrower->id } );
948 my $debit1 = Koha::Account::Line->new(
950 borrowernumber => $borrower->borrowernumber,
951 amount => 20,
952 amountoutstanding => 20,
953 interface => 'commandline',
954 debit_type_code => 'LOST'
956 )->store();
957 my $credit1 = Koha::Account::Line->new(
959 borrowernumber => $borrower->borrowernumber,
960 amount => -20,
961 amountoutstanding => -20,
962 interface => 'commandline',
963 credit_type_code => 'CREDIT'
965 )->store();
967 is( $account->balance(), 0, "Account balance is 0" );
968 is( $debit1->amountoutstanding,
969 20, 'Overdue fee has an amount outstanding of 20' );
970 is( $credit1->amountoutstanding,
971 -20, 'Credit has an amount outstanding of -20' );
973 my $reduce_params = {
974 interface => 'commandline',
975 reduction_type => 'DISCOUNT',
976 amount => 5,
977 staff_id => $staff->borrowernumber,
978 branch => $branchcode
981 throws_ok { $credit1->reduce($reduce_params); }
982 'Koha::Exceptions::Account::IsNotDebit',
983 '->reduce() can only be used with debits';
985 my @required = ( 'interface', 'reduction_type', 'amount' );
986 for my $required (@required) {
987 my $params = {%$reduce_params};
988 delete( $params->{$required} );
989 throws_ok {
990 $debit1->reduce($params);
992 'Koha::Exceptions::MissingParameter',
993 "->reduce() requires the `$required` parameter is passed";
996 $reduce_params->{interface} = 'intranet';
997 my @dependant_required = ( 'staff_id', 'branch' );
998 for my $d (@dependant_required) {
999 my $params = {%$reduce_params};
1000 delete( $params->{$d} );
1001 throws_ok {
1002 $debit1->reduce($params);
1004 'Koha::Exceptions::MissingParameter',
1005 "->reduce() requires the `$d` parameter is passed when interface is intranet";
1008 throws_ok {
1009 $debit1->reduce(
1011 interface => 'intranet',
1012 staff_id => $staff->borrowernumber,
1013 branch => $branchcode,
1014 reduction_type => 'REFUND',
1015 amount => 25
1019 'Koha::Exceptions::ParameterTooHigh',
1020 '->reduce() cannot reduce more than original amount';
1022 # Partial Reduction
1023 # (Discount 5 on debt of 20)
1024 my $reduction = $debit1->reduce($reduce_params);
1026 is( ref($reduction), 'Koha::Account::Line',
1027 '->reduce() returns a Koha::Account::Line' );
1028 is( $reduction->amount() * 1, -5, "Reduce amount is -5" );
1029 is( $reduction->amountoutstanding() * 1,
1030 0, "Reduce amountoutstanding is 0" );
1031 is( $debit1->amountoutstanding() * 1,
1032 15, "Debit amountoutstanding reduced by 5 to 15" );
1033 is( $debit1->status(), 'DISCOUNTED', "Debit status updated to DISCOUNTED");
1034 is( $account->balance() * 1, -5, "Account balance is -5" );
1035 is( $reduction->status(), 'APPLIED', "Reduction status is 'APPLIED'" );
1037 my $offsets = Koha::Account::Offsets->search(
1038 { credit_id => $reduction->id, debit_id => $debit1->id } );
1039 is( $offsets->count, 1, 'Only one offset is generated' );
1040 my $THE_offset = $offsets->next;
1041 is( $THE_offset->amount * 1,
1042 -5, 'Correct amount was applied against debit' );
1043 is( $THE_offset->type, 'DISCOUNT', "Offset type set to 'DISCOUNT'" );
1045 # Zero offset created when zero outstanding
1046 # (Refund another 5 on paid debt of 20)
1047 $credit1->apply( { debits => [$debit1] } );
1048 is( $debit1->amountoutstanding + 0,
1049 0, 'Debit1 amountoutstanding reduced to 0' );
1050 $reduce_params->{reduction_type} = 'REFUND';
1051 $reduction = $debit1->reduce($reduce_params);
1052 is( $reduction->amount() * 1, -5, "Reduce amount is -5" );
1053 is( $reduction->amountoutstanding() * 1,
1054 -5, "Reduce amountoutstanding is -5" );
1055 is( $debit1->status(), 'REFUNDED', "Debit status updated to REFUNDED");
1057 $offsets = Koha::Account::Offsets->search(
1058 { credit_id => $reduction->id, debit_id => $debit1->id } );
1059 is( $offsets->count, 1, 'Only one new offset is generated' );
1060 $THE_offset = $offsets->next;
1061 is( $THE_offset->amount * 1,
1062 0, 'Zero offset created for already paid off debit' );
1063 is( $THE_offset->type, 'REFUND', "Offset type set to 'REFUND'" );
1065 # Compound reduction should not allow more than original amount
1066 # (Reduction of 5 + 5 + 20 > 20)
1067 $reduce_params->{amount} = 20;
1068 throws_ok {
1069 $debit1->reduce($reduce_params);
1071 'Koha::Exceptions::ParameterTooHigh',
1072 '->reduce cannot reduce more than the original amount (combined reductions test)';
1074 # Throw exception if attempting to reduce a payout
1075 my $payout = $reduction->payout(
1077 interface => 'intranet',
1078 staff_id => $staff->borrowernumber,
1079 branch => $branchcode,
1080 payout_type => 'CASH',
1081 amount => 5
1084 throws_ok {
1085 $payout->reduce($reduce_params);
1087 'Koha::Exceptions::Account::IsNotDebit',
1088 '->reduce() cannot be used on a payout debit';
1090 $schema->storage->txn_rollback;
1093 subtest "cancel() tests" => sub {
1094 plan tests => 16;
1096 $schema->storage->txn_begin;
1098 my $library = $builder->build_object( { class => 'Koha::Libraries' });
1099 my $patron = $builder->build_object({ class => 'Koha::Patrons', value => { branchcode => $library->branchcode } });
1100 my $staff = $builder->build_object({ class => 'Koha::Patrons', value => { branchcode => $library->branchcode } });
1102 t::lib::Mocks::mock_userenv({ patron => $patron });
1104 my $account = Koha::Account->new( { patron_id => $patron->borrowernumber } );
1106 my $debit1 = Koha::Account::Line->new(
1108 borrowernumber => $patron->borrowernumber,
1109 amount => 10,
1110 amountoutstanding => 10,
1111 interface => 'commandline',
1112 debit_type_code => 'OVERDUE',
1114 )->store();
1115 my $debit2 = Koha::Account::Line->new(
1117 borrowernumber => $patron->borrowernumber,
1118 amount => 20,
1119 amountoutstanding => 20,
1120 interface => 'commandline',
1121 debit_type_code => 'OVERDUE',
1123 )->store();
1125 my $ret = $account->pay(
1127 lines => [$debit2],
1128 amount => 5,
1131 my $credit = Koha::Account::Lines->find({ accountlines_id => $ret->{payment_id} });
1133 is( $account->balance(), 25, "Account balance is 25" );
1134 is( $debit1->amountoutstanding + 0,
1135 10, 'First fee has amount outstanding of 10' );
1136 is( $debit2->amountoutstanding + 0,
1137 15, 'Second fee has amount outstanding of 15' );
1138 throws_ok {
1139 $credit->cancel(
1140 { staff_id => $staff->borrowernumber, branch => $library->branchcode } );
1142 'Koha::Exceptions::Account::IsNotDebit',
1143 '->cancel() can only be used with debits';
1145 throws_ok {
1146 $debit1->reduce( { staff_id => $staff->borrowernumber } );
1148 'Koha::Exceptions::MissingParameter',
1149 "->cancel() requires the `branch` parameter is passed";
1150 throws_ok {
1151 $debit1->reduce( { branch => $library->branchcode } );
1153 'Koha::Exceptions::MissingParameter',
1154 "->cancel() requires the `staff_id` parameter is passed";
1156 throws_ok {
1157 $debit2->cancel(
1158 { staff_id => $staff->borrowernumber, branch => $library->branchcode } );
1160 'Koha::Exceptions::Account',
1161 '->cancel() can only be used with debits that have not been offset';
1163 my $cancellation = $debit1->cancel(
1164 { staff_id => $staff->borrowernumber, branch => $library->branchcode } );
1165 is( ref($cancellation), 'Koha::Account::Line',
1166 'Cancel returns an account line' );
1168 $cancellation->amount() * 1,
1169 $debit1->amount * -1,
1170 "Cancellation amount is " . $debit1->amount
1172 is( $cancellation->amountoutstanding() * 1,
1173 0, "Cancellation amountoutstanding is 0" );
1174 is( $debit1->amountoutstanding() * 1,
1175 0, "Debit amountoutstanding reduced to 0" );
1176 is( $debit1->status(), 'CANCELLED', "Debit status updated to CANCELLED" );
1177 is( $account->balance() * 1, 15, "Account balance is 15" );
1179 my $offsets = Koha::Account::Offsets->search(
1180 { credit_id => $cancellation->id, debit_id => $debit1->id } );
1181 is( $offsets->count, 1, 'Only one offset is generated' );
1182 my $THE_offset = $offsets->next;
1183 is( $THE_offset->amount * 1,
1184 -10, 'Correct amount was applied against debit' );
1185 is( $THE_offset->type, 'CANCELLATION',
1186 "Offset type set to 'CANCELLATION'" );
1188 $schema->storage->txn_rollback;