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>
22 use Test::More tests => 15;
29 use Koha::Account::CreditTypes;
30 use Koha::Account::Lines;
31 use Koha::Account::Offsets;
32 use Koha::DateUtils qw( dt_from_string );
35 use t::lib::TestBuilder;
37 my $schema = Koha::Database->new->schema;
38 $schema->storage->dbh->{PrintError} = 0;
39 my $builder = t::lib::TestBuilder->new;
40 C4::Context->interface('commandline');
42 subtest 'new' => sub {
46 $schema->storage->txn_begin;
48 throws_ok { Koha::Account->new(); } qr/No patron id passed in!/, 'Croaked on bad call to new';
50 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
51 my $account = Koha::Account->new( { patron_id => $patron->borrowernumber } );
52 is( defined $account, 1, "Account is defined" );
54 $schema->storage->txn_rollback;
57 subtest 'outstanding_debits() tests' => sub {
61 $schema->storage->txn_begin;
63 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
64 my $account = $patron->account;
67 push @generated_lines, $account->add_debit({ amount => 1, interface => 'commandline', type => 'OVERDUE' });
68 push @generated_lines, $account->add_debit({ amount => 2, interface => 'commandline', type => 'OVERDUE' });
69 push @generated_lines, $account->add_debit({ amount => 3, interface => 'commandline', type => 'OVERDUE' });
70 push @generated_lines, $account->add_debit({ amount => 4, interface => 'commandline', type => 'OVERDUE' });
72 my $lines = $account->outstanding_debits();
74 is( ref($lines), 'Koha::Account::Lines', 'Called in scalar context, outstanding_debits returns a Koha::Account::Lines object' );
75 is( $lines->total_outstanding, 10, 'Outstandig debits total is correctly calculated' );
77 my $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
78 Koha::Account::Line->new(
80 borrowernumber => $patron_2->id,
81 amountoutstanding => -2,
82 interface => 'commandline',
83 credit_type_code => 'PAYMENT'
86 my $just_one = Koha::Account::Line->new(
88 borrowernumber => $patron_2->id,
90 amountoutstanding => 3,
91 interface => 'commandline',
92 debit_type_code => 'OVERDUE'
95 Koha::Account::Line->new(
97 borrowernumber => $patron_2->id,
99 amountoutstanding => -6,
100 interface => 'commandline',
101 credit_type_code => 'PAYMENT'
104 $lines = $patron_2->account->outstanding_debits();
105 is( $lines->total_outstanding, 3, "Total if some outstanding debits and some credits is only debits" );
106 is( $lines->count, 1, "With 1 outstanding debits, we get back a Lines object with 1 lines" );
107 my $the_line = Koha::Account::Lines->find( $just_one->id );
108 is_deeply( $the_line->unblessed, $lines->next->unblessed, "We get back the one correct line");
110 my $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
111 my $account_3 = $patron_3->account;
112 $account_3->add_credit( { amount => 2, interface => 'commandline' } );
113 $account_3->add_credit( { amount => 20, interface => 'commandline' } );
114 $account_3->add_credit( { amount => 200, interface => 'commandline' } );
115 $lines = $account_3->outstanding_debits();
116 is( $lines->total_outstanding, 0, "Total if no outstanding debits total is 0" );
117 is( $lines->count, 0, "With 0 outstanding debits, we get back a Lines object with 0 lines" );
119 my $patron_4 = $builder->build_object({ class => 'Koha::Patrons' });
120 my $account_4 = $patron_4->account;
121 $lines = $account_4->outstanding_debits();
122 is( $lines->total_outstanding, 0, "Total if no outstanding debits is 0" );
123 is( $lines->count, 0, "With no outstanding debits, we get back a Lines object with 0 lines" );
125 # create a pathological credit with amountoutstanding > 0 (BZ 14591)
126 Koha::Account::Line->new(
128 borrowernumber => $patron_4->id,
130 amountoutstanding => 3,
131 interface => 'commandline',
132 credit_type_code => 'PAYMENT'
135 $lines = $account_4->outstanding_debits();
136 is( $lines->count, 0, 'No credits are confused with debits because of the amountoutstanding value' );
138 $schema->storage->txn_rollback;
141 subtest 'outstanding_credits() tests' => sub {
145 $schema->storage->txn_begin;
147 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
148 my $account = $patron->account;
151 push @generated_lines, $account->add_credit({ amount => 1, interface => 'commandline' });
152 push @generated_lines, $account->add_credit({ amount => 2, interface => 'commandline' });
153 push @generated_lines, $account->add_credit({ amount => 3, interface => 'commandline' });
154 push @generated_lines, $account->add_credit({ amount => 4, interface => 'commandline' });
156 my $lines = $account->outstanding_credits();
158 is( ref($lines), 'Koha::Account::Lines', 'Called in scalar context, outstanding_credits returns a Koha::Account::Lines object' );
159 is( $lines->total_outstanding, -10, 'Outstandig credits total is correctly calculated' );
161 my $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
162 $account = $patron_2->account;
163 $lines = $account->outstanding_credits();
164 is( $lines->total_outstanding, 0, "Total if no outstanding credits is 0" );
165 is( $lines->count, 0, "With no outstanding credits, we get back a Lines object with 0 lines" );
167 # create a pathological debit with amountoutstanding < 0 (BZ 14591)
168 Koha::Account::Line->new(
170 borrowernumber => $patron_2->id,
172 amountoutstanding => -3,
173 interface => 'commandline',
174 debit_type_code => 'OVERDUE'
177 $lines = $account->outstanding_credits();
178 is( $lines->count, 0, 'No debits are confused with credits because of the amountoutstanding value' );
180 $schema->storage->txn_rollback;
183 subtest 'add_credit() tests' => sub {
187 $schema->storage->txn_begin;
189 # delete logs and statistics
190 my $action_logs = $schema->resultset('ActionLog')->search()->count;
191 my $statistics = $schema->resultset('Statistic')->search()->count;
193 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
194 my $account = Koha::Account->new( { patron_id => $patron->borrowernumber } );
196 is( $account->balance, 0, 'Patron has no balance' );
199 t::lib::Mocks::mock_preference( 'FinesLog', 0 );
202 $account->add_credit(
205 description => 'Payment of 25',
206 library_id => $patron->branchcode,
207 note => 'not really important',
209 user_id => $patron->id
213 'Koha::Exceptions::MissingParameter', 'Exception thrown if interface parameter missing';
215 my $line_1 = $account->add_credit(
218 description => 'Payment of 25',
219 library_id => $patron->branchcode,
220 note => 'not really important',
222 user_id => $patron->id,
223 interface => 'commandline'
227 is( $account->balance, -25, 'Patron has a balance of -25' );
228 is( $schema->resultset('ActionLog')->count(), $action_logs + 0, 'No log was added' );
229 is( $schema->resultset('Statistic')->count(), $statistics + 1, 'Action added to statistics' );
230 is( $line_1->credit_type_code, 'PAYMENT', 'Account type is correctly set' );
231 ok( $line_1->amount < 0, 'Credit amount is stored as a negative' );
234 t::lib::Mocks::mock_preference( 'FinesLog', 1 );
236 my $line_2 = $account->add_credit(
238 description => 'Payment of 37',
239 library_id => $patron->branchcode,
240 note => 'not really important',
241 user_id => $patron->id,
242 interface => 'commandline'
246 is( $account->balance, -62, 'Patron has a balance of -25' );
247 is( $schema->resultset('ActionLog')->count(), $action_logs + 1, 'Log was added' );
248 is( $schema->resultset('Statistic')->count(), $statistics + 2, 'Action added to statistics' );
249 is( $line_2->credit_type_code, 'PAYMENT', 'Account type is correctly set' );
250 ok( $line_1->amount < 0, 'Credit amount is stored as a negative' );
252 # offsets have the credit_id set to accountlines_id, and debit_id is undef
253 my $offset_1 = Koha::Account::Offsets->search({ credit_id => $line_1->id })->next;
254 my $offset_2 = Koha::Account::Offsets->search({ credit_id => $line_2->id })->next;
256 is( $offset_1->credit_id, $line_1->id, 'No debit_id is set for credits' );
257 is( $offset_1->debit_id, undef, 'No debit_id is set for credits' );
258 ok( $offset_1->amount > 0, 'Credit creation offset is a positive' );
259 is( $offset_2->credit_id, $line_2->id, 'No debit_id is set for credits' );
260 is( $offset_2->debit_id, undef, 'No debit_id is set for credits' );
261 ok( $offset_2->amount > 0, 'Credit creation offset is a positive' );
263 my $line_3 = $account->add_credit(
266 description => 'Manual credit applied',
267 library_id => $patron->branchcode,
268 user_id => $patron->id,
270 interface => 'commandline'
274 is( $schema->resultset('ActionLog')->count(), $action_logs + 2, 'Log was added' );
275 is( $schema->resultset('Statistic')->count(), $statistics + 2, 'No action added to statistics, because of credit type' );
277 # Enable cash registers
278 t::lib::Mocks::mock_preference( 'UseCashRegisters', 1 );
280 $account->add_credit(
283 description => 'Cash payment without cash register',
284 library_id => $patron->branchcode,
285 user_id => $patron->id,
286 payment_type => 'CASH',
287 interface => 'intranet'
291 'Koha::Exceptions::Account::RegisterRequired',
292 'Exception thrown for UseCashRegisters:1 + payment_type:CASH + cash_register:undef';
294 # Disable cash registers
295 t::lib::Mocks::mock_preference( 'UseCashRegisters', 0 );
297 $schema->storage->txn_rollback;
300 subtest 'add_debit() tests' => sub {
304 $schema->storage->txn_begin;
306 # delete logs and statistics
307 my $action_logs = $schema->resultset('ActionLog')->search()->count;
308 my $statistics = $schema->resultset('Statistic')->search()->count;
310 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
312 Koha::Account->new( { patron_id => $patron->borrowernumber } );
314 is( $account->balance, 0, 'Patron has no balance' );
320 description => 'amount validation failure',
321 library_id => $patron->branchcode,
322 note => 'this should fail anyway',
324 user_id => $patron->id,
325 interface => 'commandline'
327 ); } 'Koha::Exceptions::Account::AmountNotPositive', 'Expected validation exception thrown (amount)';
331 open STDERR, '>', '/dev/null';
335 description => 'type validation failure',
336 library_id => $patron->branchcode,
337 note => 'this should fail anyway',
339 user_id => $patron->id,
340 interface => 'commandline'
345 'Koha::Exceptions::Account::UnrecognisedType',
346 'Expected validation exception thrown (type)';
352 description => 'Rental charge of 25',
353 library_id => $patron->branchcode,
354 note => 'not really important',
356 user_id => $patron->id
358 ); } 'Koha::Exceptions::MissingParameter', 'Exception thrown if interface parameter missing';
361 t::lib::Mocks::mock_preference( 'FinesLog', 0 );
363 my $line_1 = $account->add_debit(
366 description => 'Rental charge of 25',
367 library_id => $patron->branchcode,
368 note => 'not really important',
370 user_id => $patron->id,
371 interface => 'commandline'
375 is( $account->balance, 25, 'Patron has a balance of 25' );
377 $schema->resultset('ActionLog')->count(),
382 $line_1->debit_type_code,
384 'Account type is correctly set'
388 t::lib::Mocks::mock_preference( 'FinesLog', 1 );
390 my $line_2 = $account->add_debit(
393 description => 'Rental charge of 37',
394 library_id => $patron->branchcode,
395 note => 'not really important',
397 user_id => $patron->id,
398 interface => 'commandline'
402 is( $account->balance, 62, 'Patron has a balance of 62' );
404 $schema->resultset('ActionLog')->count(),
409 $line_2->debit_type_code,
411 'Account type is correctly set'
414 # offsets have the debit_id set to accountlines_id, and credit_id is undef
416 Koha::Account::Offsets->search( { debit_id => $line_1->id } )->next;
418 Koha::Account::Offsets->search( { debit_id => $line_2->id } )->next;
420 is( $offset_1->debit_id, $line_1->id, 'debit_id is set for debit 1' );
421 is( $offset_1->credit_id, undef, 'credit_id is not set for debit 1' );
422 is( $offset_2->debit_id, $line_2->id, 'debit_id is set for debit 2' );
423 is( $offset_2->credit_id, undef, 'credit_id is not set for debit 2' );
425 $schema->storage->txn_rollback;
428 subtest 'lines() tests' => sub {
432 $schema->storage->txn_begin;
434 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
435 my $account = $patron->account;
438 $account->add_credit({ amount => 1, interface => 'commandline' });
439 $account->add_credit({ amount => 2, interface => 'commandline' });
440 $account->add_credit({ amount => 3, interface => 'commandline' });
441 $account->add_credit({ amount => 4, interface => 'commandline' });
444 $account->add_debit({ amount => 1, interface => 'commandline', type => 'OVERDUE' });
445 $account->add_debit({ amount => 2, interface => 'commandline', type => 'OVERDUE' });
446 $account->add_debit({ amount => 3, interface => 'commandline', type => 'OVERDUE' });
447 $account->add_debit({ amount => 4, interface => 'commandline', type => 'OVERDUE' });
450 $account->add_credit( { amount => 1, interface => 'commandline' } )
451 ->apply( { debits => [ $account->outstanding_debits->as_list ] } );
453 my $lines = $account->lines;
454 is( $lines->_resultset->count, 9, "All accountlines (debits, credits and paid off) were fetched");
456 $schema->storage->txn_rollback;
459 subtest 'reconcile_balance' => sub {
463 subtest 'more credit than debit' => sub {
467 $schema->storage->txn_begin;
469 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
470 my $account = $patron->account;
473 $account->add_credit({ amount => 1, interface => 'commandline' });
474 $account->add_credit({ amount => 2, interface => 'commandline' });
475 $account->add_credit({ amount => 3, interface => 'commandline' });
476 $account->add_credit({ amount => 4, interface => 'commandline' });
477 $account->add_credit({ amount => 5, interface => 'commandline' });
480 $account->add_debit({ amount => 1, interface => 'commandline', type => 'OVERDUE' });
481 $account->add_debit({ amount => 2, interface => 'commandline', type => 'OVERDUE' });
482 $account->add_debit({ amount => 3, interface => 'commandline', type => 'OVERDUE' });
483 $account->add_debit({ amount => 4, interface => 'commandline', type => 'OVERDUE' });
486 Koha::Account::Line->new(
488 borrowernumber => $patron->id,
490 amountoutstanding => 0,
491 interface => 'commandline',
492 debit_type_code => 'OVERDUE'
495 Koha::Account::Line->new(
497 borrowernumber => $patron->id,
499 amountoutstanding => 0,
500 interface => 'commandline',
501 debit_type_code => 'OVERDUE'
505 is( $account->balance(), -5, "Account balance is -5" );
506 is( $account->outstanding_debits->total_outstanding, 10, 'Outstanding debits sum 10' );
507 is( $account->outstanding_credits->total_outstanding, -15, 'Outstanding credits sum -15' );
509 $account->reconcile_balance();
511 is( $account->balance(), -5, "Account balance is -5" );
512 is( $account->outstanding_debits->total_outstanding, 0, 'No outstanding debits' );
513 is( $account->outstanding_credits->total_outstanding, -5, 'Outstanding credits sum -5' );
515 $schema->storage->txn_rollback;
518 subtest 'same debit as credit' => sub {
522 $schema->storage->txn_begin;
524 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
525 my $account = $patron->account;
528 $account->add_credit({ amount => 1, interface => 'commandline' });
529 $account->add_credit({ amount => 2, interface => 'commandline' });
530 $account->add_credit({ amount => 3, interface => 'commandline' });
531 $account->add_credit({ amount => 4, interface => 'commandline' });
534 $account->add_debit({ amount => 1, interface => 'commandline', type => 'OVERDUE' });
535 $account->add_debit({ amount => 2, interface => 'commandline', type => 'OVERDUE' });
536 $account->add_debit({ amount => 3, interface => 'commandline', type => 'OVERDUE' });
537 $account->add_debit({ amount => 4, interface => 'commandline', type => 'OVERDUE' });
540 Koha::Account::Line->new(
542 borrowernumber => $patron->id,
544 amountoutstanding => 0,
545 interface => 'commandline',
546 debit_type_code => 'OVERDUE'
549 Koha::Account::Line->new(
551 borrowernumber => $patron->id,
553 amountoutstanding => 0,
554 interface => 'commandline',
555 debit_type_code => 'OVERDUE'
559 is( $account->balance(), 0, "Account balance is 0" );
560 is( $account->outstanding_debits->total_outstanding, 10, 'Outstanding debits sum 10' );
561 is( $account->outstanding_credits->total_outstanding, -10, 'Outstanding credits sum -10' );
563 $account->reconcile_balance();
565 is( $account->balance(), 0, "Account balance is 0" );
566 is( $account->outstanding_debits->total_outstanding, 0, 'No outstanding debits' );
567 is( $account->outstanding_credits->total_outstanding, 0, 'Outstanding credits sum 0' );
569 $schema->storage->txn_rollback;
572 subtest 'more debit than credit' => sub {
576 $schema->storage->txn_begin;
578 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
579 my $account = $patron->account;
582 $account->add_credit({ amount => 1, interface => 'commandline' });
583 $account->add_credit({ amount => 2, interface => 'commandline' });
584 $account->add_credit({ amount => 3, interface => 'commandline' });
585 $account->add_credit({ amount => 4, interface => 'commandline' });
588 $account->add_debit({ amount => 1, interface => 'commandline', type => 'OVERDUE' });
589 $account->add_debit({ amount => 2, interface => 'commandline', type => 'OVERDUE' });
590 $account->add_debit({ amount => 3, interface => 'commandline', type => 'OVERDUE' });
591 $account->add_debit({ amount => 4, interface => 'commandline', type => 'OVERDUE' });
592 $account->add_debit({ amount => 5, interface => 'commandline', type => 'OVERDUE' });
595 Koha::Account::Line->new(
597 borrowernumber => $patron->id,
599 amountoutstanding => 0,
600 interface => 'commandline',
601 debit_type_code => 'OVERDUE'
604 Koha::Account::Line->new(
606 borrowernumber => $patron->id,
608 amountoutstanding => 0,
609 interface => 'commandline',
610 debit_type_code => 'OVERDUE'
614 is( $account->balance(), 5, "Account balance is 5" );
615 is( $account->outstanding_debits->total_outstanding, 15, 'Outstanding debits sum 15' );
616 is( $account->outstanding_credits->total_outstanding, -10, 'Outstanding credits sum -10' );
618 $account->reconcile_balance();
620 is( $account->balance(), 5, "Account balance is 5" );
621 is( $account->outstanding_debits->total_outstanding, 5, 'Outstanding debits sum 5' );
622 is( $account->outstanding_credits->total_outstanding, 0, 'Outstanding credits sum 0' );
624 $schema->storage->txn_rollback;
627 subtest 'credits are applied to older debits first' => sub {
631 $schema->storage->txn_begin;
633 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
634 my $account = $patron->account;
637 $account->add_credit({ amount => 1, interface => 'commandline' });
638 $account->add_credit({ amount => 3, interface => 'commandline' });
641 my $debit_1 = $account->add_debit({ amount => 1, interface => 'commandline', type => 'OVERDUE' });
642 my $debit_2 = $account->add_debit({ amount => 2, interface => 'commandline', type => 'OVERDUE' });
643 my $debit_3 = $account->add_debit({ amount => 3, interface => 'commandline', type => 'OVERDUE' });
645 is( $account->balance(), 2, "Account balance is 2" );
646 is( $account->outstanding_debits->total_outstanding, 6, 'Outstanding debits sum 6' );
647 is( $account->outstanding_credits->total_outstanding, -4, 'Outstanding credits sum -4' );
649 $account->reconcile_balance();
651 is( $account->balance(), 2, "Account balance is 2" );
652 is( $account->outstanding_debits->total_outstanding, 2, 'Outstanding debits sum 2' );
653 is( $account->outstanding_credits->total_outstanding, 0, 'Outstanding credits sum 0' );
655 $debit_1->discard_changes;
656 is( $debit_1->amountoutstanding + 0, 0, 'Old debit payed' );
657 $debit_2->discard_changes;
658 is( $debit_2->amountoutstanding + 0, 0, 'Old debit payed' );
659 $debit_3->discard_changes;
660 is( $debit_3->amountoutstanding + 0, 2, 'Newest debit only partially payed' );
662 $schema->storage->txn_rollback;
666 subtest 'pay() tests' => sub {
670 $schema->storage->txn_begin;
672 # Disable renewing upon fine payment
673 t::lib::Mocks::mock_preference( 'RenewAccruingItemWhenPaid', 0 );
675 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
676 my $library = $builder->build_object({ class => 'Koha::Libraries' });
677 my $account = $patron->account;
679 my $context = Test::MockModule->new('C4::Context');
680 $context->mock( 'userenv', { branch => $library->id } );
682 my $credit_1_id = $account->pay({ amount => 200 })->{payment_id};
683 my $credit_1 = Koha::Account::Lines->find( $credit_1_id );
685 is( $credit_1->branchcode, undef, 'No branchcode is set if library_id was not passed' );
687 my $credit_2_id = $account->pay({ amount => 150, library_id => $library->id })->{payment_id};
688 my $credit_2 = Koha::Account::Lines->find( $credit_2_id );
690 is( $credit_2->branchcode, $library->id, 'branchcode set because library_id was passed' );
692 # Enable cash registers
693 t::lib::Mocks::mock_preference( 'UseCashRegisters', 1 );
698 payment_type => 'CASH',
699 interface => 'intranet'
703 'Koha::Exceptions::Account::RegisterRequired',
704 'Exception thrown for UseCashRegisters:1 + payment_type:CASH + cash_register:undef';
706 # Disable cash registers
707 t::lib::Mocks::mock_preference( 'UseCashRegisters', 0 );
710 $context->mock( 'userenv', undef );
711 my $result = $account->pay(
714 payment_Type => 'CASH',
715 interface => 'intranet'
718 ok($result, "Koha::Account->pay functions without a userenv");
719 my $payment = Koha::Account::Lines->find({accountlines_id => $result->{payment_id}});
720 is($payment->manager_id, undef, "manager_id left undefined when no userenv found");
722 subtest 'UseEmailReceipts tests' => sub {
726 t::lib::Mocks::mock_preference( 'UseEmailReceipts', 1 );
730 my $mocked_letters = Test::MockModule->new('C4::Letters');
731 # we want to test the params
732 $mocked_letters->mock( 'GetPreparedLetter', sub {
736 # we don't care about EnqueueLetter for now
737 $mocked_letters->mock( 'EnqueueLetter', sub {
741 $schema->storage->txn_begin;
743 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
744 my $account = $patron->account;
746 my $debit_1 = $account->add_debit( { amount => 5, interface => 'commandline', type => 'OVERDUE' } );
747 my $debit_2 = $account->add_debit( { amount => 10, interface => 'commandline', type => 'OVERDUE' } );
749 $account->pay({ amount => 6, lines => [ $debit_1, $debit_2 ] });
750 my @offsets = @{$params{substitute}{offsets}};
752 is( scalar @offsets, 2, 'Two offsets related to payment' );
753 is( ref($offsets[0]), 'Koha::Account::Offset', 'Type is correct' );
754 is( ref($offsets[1]), 'Koha::Account::Offset', 'Type is correct' );
755 is( $offsets[0]->type, 'APPLY', 'Only APPLY offsets are passed to the notice' );
756 is( $offsets[1]->type, 'APPLY', 'Only APPLY offsets are passed to the notice' );
758 $schema->storage->txn_rollback;
761 $schema->storage->txn_rollback;
764 subtest 'pay() handles lost items when paying a specific lost fee' => sub {
768 $schema->storage->txn_begin;
770 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
771 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
772 my $account = $patron->account;
774 my $context = Test::MockModule->new('C4::Context');
775 $context->mock( 'userenv', { branch => $library->id } );
777 my $biblio = $builder->build_sample_biblio();
779 $builder->build_sample_item( { biblionumber => $biblio->biblionumber } );
781 my $checkout = Koha::Checkout->new(
783 borrowernumber => $patron->id,
784 itemnumber => $item->id,
785 date_due => \'NOW()',
786 branchcode => $patron->branchcode,
787 issuedate => \'NOW()',
791 $item->itemlost('1')->store();
793 my $accountline = Koha::Account::Line->new(
795 issue_id => $checkout->id,
796 borrowernumber => $patron->id,
797 itemnumber => $item->id,
799 debit_type_code => 'LOST',
802 amountoutstanding => '1',
809 library_id => $library->id,
810 lines => [$accountline],
814 $accountline = Koha::Account::Lines->find( $accountline->id );
815 is( $accountline->amountoutstanding+0, .5, 'Account line was paid down by half' );
817 $checkout = Koha::Checkouts->find( $checkout->id );
818 ok( $checkout, 'Item still checked out to patron' );
823 library_id => $library->id,
824 lines => [$accountline],
828 $accountline = Koha::Account::Lines->find( $accountline->id );
829 is( $accountline->amountoutstanding+0, 0, 'Account line was paid down by half' );
831 $checkout = Koha::Checkouts->find( $checkout->id );
832 ok( !$checkout, 'Item was removed from patron account' );
834 subtest 'item was not checked out to the same patron' => sub {
837 my $patron_2 = $builder->build_object(
839 class => 'Koha::Patrons',
840 value => { branchcode => $library->branchcode }
843 $item->itemlost('1')->store();
844 C4::Accounts::chargelostitem( $patron->borrowernumber, $item->itemnumber, 5, "lost" );
845 my $accountline = Koha::Account::Lines->search(
847 borrowernumber => $patron->borrowernumber,
848 itemnumber => $item->itemnumber,
849 debit_type_code => 'LOST'
852 my $checkout = Koha::Checkout->new(
854 borrowernumber => $patron_2->borrowernumber,
855 itemnumber => $item->itemnumber,
856 date_due => \'NOW()',
857 branchcode => $patron_2->branchcode,
858 issuedate => \'NOW()',
862 $patron->account->pay(
865 library_id => $library->branchcode,
866 lines => [$accountline],
871 Koha::Checkouts->find( $checkout->issue_id ),
872 'If the item is checked out to another patron, a lost item should not be returned if lost fee is paid'
877 $schema->storage->txn_rollback;
880 subtest 'pay() handles lost items when paying by amount ( not specifying the lost fee )' => sub {
884 $schema->storage->txn_begin;
886 # Enable AccountAutoReconcile
887 t::lib::Mocks::mock_preference( 'AccountAutoReconcile', 1 );
889 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
890 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
891 my $account = $patron->account;
893 my $context = Test::MockModule->new('C4::Context');
894 $context->mock( 'userenv', { branch => $library->id } );
896 my $biblio = $builder->build_sample_biblio();
898 $builder->build_sample_item( { biblionumber => $biblio->biblionumber } );
900 my $checkout = Koha::Checkout->new(
902 borrowernumber => $patron->id,
903 itemnumber => $item->id,
904 date_due => \'NOW()',
905 branchcode => $patron->branchcode,
906 issuedate => \'NOW()',
910 $item->itemlost('1')->store();
912 my $accountline = Koha::Account::Line->new(
914 issue_id => $checkout->id,
915 borrowernumber => $patron->id,
916 itemnumber => $item->id,
918 debit_type_code => 'LOST',
921 amountoutstanding => '1',
928 library_id => $library->id,
932 $accountline = Koha::Account::Lines->find( $accountline->id );
933 is( $accountline->amountoutstanding+0, .5, 'Account line was paid down by half' );
935 $checkout = Koha::Checkouts->find( $checkout->id );
936 ok( $checkout, 'Item still checked out to patron' );
941 library_id => $library->id,
945 $accountline = Koha::Account::Lines->find( $accountline->id );
946 is( $accountline->amountoutstanding+0, 0, 'Account line was paid down by half' );
948 $checkout = Koha::Checkouts->find( $checkout->id );
949 ok( !$checkout, 'Item was removed from patron account' );
951 $schema->storage->txn_rollback;
954 subtest 'pay() renews items when appropriate' => sub {
958 $schema->storage->txn_begin;
960 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
961 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
962 my $account = $patron->account;
964 my $context = Test::MockModule->new('C4::Context');
965 $context->mock( 'userenv', { branch => $library->id } );
967 my $biblio = $builder->build_sample_biblio();
969 $builder->build_sample_item( { biblionumber => $biblio->biblionumber } );
971 my $now = dt_from_string();
972 my $seven_weeks = DateTime::Duration->new(weeks => 7);
973 my $five_weeks = DateTime::Duration->new(weeks => 5);
974 my $seven_weeks_ago = $now - $seven_weeks;
975 my $five_weeks_ago = $now - $five_weeks;
977 my $checkout = Koha::Checkout->new(
979 borrowernumber => $patron->id,
980 itemnumber => $item->id,
981 date_due => $five_weeks_ago,
982 branchcode => $patron->branchcode,
983 issuedate => $seven_weeks_ago
987 my $accountline = Koha::Account::Line->new(
989 issue_id => $checkout->id,
990 borrowernumber => $patron->id,
991 itemnumber => $item->id,
993 debit_type_code => 'OVERDUE',
994 status => 'UNRETURNED',
997 amountoutstanding => '1',
1001 # Enable renewing upon fine payment
1002 t::lib::Mocks::mock_preference( 'RenewAccruingItemWhenPaid', 1 );
1004 my $module = Test::MockModule->new('C4::Circulation');
1005 $module->mock('AddRenewal', sub { $called = 1; });
1006 $module->mock('CanBookBeRenewed', sub { return 1; });
1007 my $result = $account->pay(
1010 library_id => $library->id,
1014 is( $called, 1, 'RenewAccruingItemWhenPaid causes C4::Circulation::AddRenew to be called when appropriate' );
1015 is(ref($result->{renew_result}), 'ARRAY', "Pay result contains 'renew_result' ARRAY" );
1016 is( scalar @{$result->{renew_result}}, 1, "renew_result contains one renewal result" );
1017 is( $result->{renew_result}->[0]->{itemnumber}, $item->id, "renew_result contains itemnumber of renewed item" );
1019 # Reset test by adding a new overdue
1020 Koha::Account::Line->new(
1022 issue_id => $checkout->id,
1023 borrowernumber => $patron->id,
1024 itemnumber => $item->id,
1026 debit_type_code => 'OVERDUE',
1027 status => 'UNRETURNED',
1030 amountoutstanding => '1',
1035 t::lib::Mocks::mock_preference( 'RenewAccruingItemWhenPaid', 0 );
1036 $result = $account->pay(
1039 library_id => $library->id,
1043 is( $called, 0, 'C4::Circulation::AddRenew NOT called when RenewAccruingItemWhenPaid disabled' );
1044 is(ref($result->{renew_result}), 'ARRAY', "Pay result contains 'renew_result' ARRAY" );
1045 is( scalar @{$result->{renew_result}}, 0, "renew_result contains no renewal results" );
1047 $schema->storage->txn_rollback;
1050 subtest 'Koha::Account::Line::apply() handles lost items' => sub {
1054 $schema->storage->txn_begin;
1056 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
1057 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
1058 my $account = $patron->account;
1060 my $context = Test::MockModule->new('C4::Context');
1061 $context->mock( 'userenv', { branch => $library->id } );
1063 my $biblio = $builder->build_sample_biblio();
1065 $builder->build_sample_item( { biblionumber => $biblio->biblionumber } );
1067 my $checkout = Koha::Checkout->new(
1069 borrowernumber => $patron->id,
1070 itemnumber => $item->id,
1071 date_due => \'NOW()',
1072 branchcode => $patron->branchcode,
1073 issuedate => \'NOW()',
1077 $item->itemlost('1')->store();
1079 my $debit = Koha::Account::Line->new(
1081 issue_id => $checkout->id,
1082 borrowernumber => $patron->id,
1083 itemnumber => $item->id,
1085 debit_type_code => 'LOST',
1088 amountoutstanding => '1',
1092 my $credit = Koha::Account::Line->new(
1094 borrowernumber => $patron->id,
1095 date => '1970-01-01 00:00:01',
1097 amountoutstanding => -.5,
1098 interface => 'commandline',
1099 credit_type_code => 'PAYMENT'
1102 my $debits = $account->outstanding_debits;
1103 $credit->apply({ debits => [ $debits->as_list ] });
1105 $debit = Koha::Account::Lines->find( $debit->id );
1106 is( $debit->amountoutstanding+0, .5, 'Account line was paid down by half' );
1108 $checkout = Koha::Checkouts->find( $checkout->id );
1109 ok( $checkout, 'Item still checked out to patron' );
1111 $credit = Koha::Account::Line->new(
1113 borrowernumber => $patron->id,
1114 date => '1970-01-01 00:00:01',
1116 amountoutstanding => -.5,
1117 interface => 'commandline',
1118 credit_type_code => 'PAYMENT'
1121 $debits = $account->outstanding_debits;
1122 $credit->apply({ debits => [ $debits->as_list ] });
1124 $debit = Koha::Account::Lines->find( $debit->id );
1125 is( $debit->amountoutstanding+0, 0, 'Account line was paid down by half' );
1127 $checkout = Koha::Checkouts->find( $checkout->id );
1128 ok( !$checkout, 'Item was removed from patron account' );
1130 $schema->storage->txn_rollback;
1133 subtest 'Koha::Account::pay() generates credit number (Koha::Account::Line->store)' => sub {
1136 $schema->storage->txn_begin;
1138 Koha::Account::Lines->delete();
1140 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
1141 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
1142 my $account = $patron->account;
1144 #t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode });
1145 my $context = Test::MockModule->new('C4::Context');
1146 $context->mock( 'userenv', { branch => $library->id } );
1148 my $now = dt_from_string;
1149 my $year = $now->year;
1150 my $month = $now->month;
1151 my ($accountlines_id, $accountline);
1153 my $credit_type = Koha::Account::CreditTypes->find('PAYMENT');
1154 $credit_type->credit_number_enabled(1);
1155 $credit_type->store();
1157 t::lib::Mocks::mock_preference('AutoCreditNumber', '');
1158 $accountlines_id = $account->pay({ amount => '1.00', library_id => $library->id })->{payment_id};
1159 $accountline = Koha::Account::Lines->find($accountlines_id);
1160 is($accountline->credit_number, undef, 'No credit number is generated when syspref is off');
1162 t::lib::Mocks::mock_preference('AutoCreditNumber', 'incremental');
1164 $accountlines_id = $account->pay({ amount => '1.00', library_id => $library->id })->{payment_id};
1165 $accountline = Koha::Account::Lines->find($accountlines_id);
1166 is($accountline->credit_number, $i, "Incremental format credit number added for payments: $i");
1168 $accountlines_id = $account->pay({ type => 'WRITEOFF', amount => '1.00', library_id => $library->id })->{payment_id};
1169 $accountline = Koha::Account::Lines->find($accountlines_id);
1170 is($accountline->credit_number, undef, "Incremental credit number not added for writeoff");
1172 t::lib::Mocks::mock_preference('AutoCreditNumber', 'annual');
1174 $accountlines_id = $account->pay({ amount => '1.00', library_id => $library->id })->{payment_id};
1175 $accountline = Koha::Account::Lines->find($accountlines_id);
1176 is($accountline->credit_number, sprintf('%s-%04d', $year, $i), "Annual format credit number added for payments: " . sprintf('%s-%04d', $year, $i));
1178 $accountlines_id = $account->pay({ type => 'WRITEOFF', amount => '1.00', library_id => $library->id })->{payment_id};
1179 $accountline = Koha::Account::Lines->find($accountlines_id);
1180 is($accountline->credit_number, undef, "Annual format credit number not aded for writeoff");
1182 t::lib::Mocks::mock_preference('AutoCreditNumber', 'branchyyyymmincr');
1184 $accountlines_id = $account->pay({ amount => '1.00', library_id => $library->id })->{payment_id};
1185 $accountline = Koha::Account::Lines->find($accountlines_id);
1186 is($accountline->credit_number, sprintf('%s%d%02d%04d', $library->id, $year, $month, $i), "branchyyyymmincr format credit number added for payment: " . sprintf('%s%d%02d%04d', $library->id, $year, $month, $i));
1188 $accountlines_id = $account->pay({ type => 'WRITEOFF', amount => '1.00', library_id => $library->id })->{payment_id};
1189 $accountline = Koha::Account::Lines->find($accountlines_id);
1190 is($accountline->credit_number, undef, "branchyyyymmincr format credit number not added for writeoff");
1193 Koha::Account::Line->new(
1195 interface => 'test',
1197 credit_type_code => $credit_type->code,
1202 'Koha::Exceptions::Account',
1203 "Exception thrown when AutoCreditNumber is enabled but credit_number is already defined";
1205 $schema->storage->txn_rollback;
1208 subtest 'Koha::Account::payout_amount() tests' => sub {
1211 $schema->storage->txn_begin;
1213 # delete logs and statistics
1214 my $action_logs = $schema->resultset('ActionLog')->search()->count;
1215 my $statistics = $schema->resultset('Statistic')->search()->count;
1217 my $staff = $builder->build_object( { class => 'Koha::Patrons' } );
1218 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
1220 $builder->build_object( { class => 'Koha::Cash::Registers' } );
1221 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
1223 Koha::Account->new( { patron_id => $patron->borrowernumber } );
1225 is( $account->balance, 0, 'Test patron has no balance' );
1227 my $payout_params = {
1228 payout_type => 'CASH',
1229 branch => $library->id,
1230 register_id => $register->id,
1231 staff_id => $staff->id,
1232 interface => 'intranet',
1236 my @required_fields =
1237 ( 'interface', 'staff_id', 'branch', 'payout_type', 'amount' );
1238 for my $required_field (@required_fields) {
1239 my $this_payout = { %{$payout_params} };
1240 delete $this_payout->{$required_field};
1243 $account->payout_amount($this_payout);
1245 'Koha::Exceptions::MissingParameter',
1246 "Exception thrown if $required_field parameter missing";
1250 $account->payout_amount($payout_params);
1252 'Koha::Exceptions::Account::AmountNotPositive',
1253 'Expected validation exception thrown (amount not positive)';
1255 $payout_params->{amount} = 10;
1257 $account->payout_amount($payout_params);
1259 'Koha::Exceptions::ParameterTooHigh',
1260 'Expected validation exception thrown (amount greater than outstanding)';
1262 # Enable cash registers
1263 t::lib::Mocks::mock_preference( 'UseCashRegisters', 1 );
1265 $account->payout_amount($payout_params);
1267 'Koha::Exceptions::Account::RegisterRequired',
1268 'Exception thrown for UseCashRegisters:1 + payout_type:CASH + cash_register:undef';
1270 # Disable cash registers
1271 t::lib::Mocks::mock_preference( 'UseCashRegisters', 0 );
1273 # Add some outstanding credits
1274 my $credit_1 = $account->add_credit( { amount => 2, interface => 'commandline' } );
1275 my $credit_2 = $account->add_credit( { amount => 3, interface => 'commandline' } );
1276 my $credit_3 = $account->add_credit( { amount => 5, interface => 'commandline' } );
1277 my $credit_4 = $account->add_credit( { amount => 10, interface => 'commandline' } );
1278 my $credits = $account->outstanding_credits();
1279 is( $credits->count, 4, "Found 4 credits with outstanding amounts" );
1280 is( $credits->total_outstanding + 0, -20, "Total -20 outstanding credit" );
1282 my $payout = $account->payout_amount($payout_params);
1283 is(ref($payout), 'Koha::Account::Line', 'Return the Koha::Account::Line object for the payout');
1284 is($payout->amount + 0, 10, "Payout amount recorded correctly");
1285 is($payout->amountoutstanding + 0, 0, "Full amount was paid out");
1286 $credits = $account->outstanding_credits();
1287 is($credits->count, 1, "Payout was applied against oldest outstanding credits first");
1288 is($credits->total_outstanding + 0, -10, "Total of 10 outstanding credit remaining");
1290 my $offsets = Koha::Account::Offsets->search( { debit_id => $payout->id } );
1291 is( $offsets->count, 4, 'Four offsets generated' );
1292 my $offset = $offsets->next;
1293 is( $offset->type, 'CREATE', 'CREATE offset added for payout line' );
1294 is( $offset->amount * 1, 10, 'Correct offset amount recorded' );
1295 $offset = $offsets->next;
1296 is( $offset->credit_id, $credit_1->id, "Offset added against credit_1");
1297 is( $offset->type, 'APPLY', "APPLY used for offset_type" );
1298 is( $offset->amount * 1, -2, 'Correct amount offset against credit_1' );
1299 $offset = $offsets->next;
1300 is( $offset->credit_id, $credit_2->id, "Offset added against credit_2");
1301 is( $offset->type, 'APPLY', "APPLY used for offset_type" );
1302 is( $offset->amount * 1, -3, 'Correct amount offset against credit_2' );
1303 $offset = $offsets->next;
1304 is( $offset->credit_id, $credit_3->id, "Offset added against credit_3");
1305 is( $offset->type, 'APPLY', "APPLY used for offset_type" );
1306 is( $offset->amount * 1, -5, 'Correct amount offset against credit_3' );
1308 my $credit_5 = $account->add_credit( { amount => 5, interface => 'commandline' } );
1309 $credits = $account->outstanding_credits();
1310 is($credits->count, 2, "New credit added");
1311 $payout_params->{amount} = 2.50;
1312 $payout_params->{credits} = [$credit_5];
1313 $payout = $account->payout_amount($payout_params);
1315 $credits = $account->outstanding_credits();
1316 is($credits->count, 2, "Second credit not fully paid off");
1317 is($credits->total_outstanding + 0, -12.50, "12.50 outstanding credit remaining");
1318 $credit_4->discard_changes;
1319 $credit_5->discard_changes;
1320 is($credit_4->amountoutstanding + 0, -10, "Credit 4 unaffected when credit_5 was passed to payout_amount");
1321 is($credit_5->amountoutstanding + 0, -2.50, "Credit 5 correctly reduced when payout_amount called with credit_5 passed");
1323 $offsets = Koha::Account::Offsets->search( { debit_id => $payout->id } );
1324 is( $offsets->count, 2, 'Two offsets generated' );
1325 $offset = $offsets->next;
1326 is( $offset->type, 'CREATE', 'CREATE offset added for payout line' );
1327 is( $offset->amount * 1, 2.50, 'Correct offset amount recorded' );
1328 $offset = $offsets->next;
1329 is( $offset->credit_id, $credit_5->id, "Offset added against credit_5");
1330 is( $offset->type, 'APPLY', "APPLY used for offset_type" );
1331 is( $offset->amount * 1, -2.50, 'Correct amount offset against credit_5' );
1333 $schema->storage->txn_rollback;
1336 subtest 'Koha::Account::payin_amount() tests' => sub {
1339 $schema->storage->txn_begin;
1341 # delete logs and statistics
1342 my $action_logs = $schema->resultset('ActionLog')->search()->count;
1343 my $statistics = $schema->resultset('Statistic')->search()->count;
1345 my $staff = $builder->build_object( { class => 'Koha::Patrons' } );
1346 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
1348 $builder->build_object( { class => 'Koha::Cash::Registers' } );
1349 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
1351 Koha::Account->new( { patron_id => $patron->borrowernumber } );
1353 is( $account->balance, 0, 'Test patron has no balance' );
1355 my $payin_params = {
1357 payment_type => 'CASH',
1358 branch => $library->id,
1359 register_id => $register->id,
1360 staff_id => $staff->id,
1361 interface => 'intranet',
1365 my @required_fields =
1366 ( 'interface', 'amount', 'type' );
1367 for my $required_field (@required_fields) {
1368 my $this_payin = { %{$payin_params} };
1369 delete $this_payin->{$required_field};
1372 $account->payin_amount($this_payin);
1374 'Koha::Exceptions::MissingParameter',
1375 "Exception thrown if $required_field parameter missing";
1379 $account->payin_amount($payin_params);
1381 'Koha::Exceptions::Account::AmountNotPositive',
1382 'Expected validation exception thrown (amount not positive)';
1384 $payin_params->{amount} = 10;
1386 # Enable cash registers
1387 t::lib::Mocks::mock_preference( 'UseCashRegisters', 1 );
1389 $account->payin_amount($payin_params);
1391 'Koha::Exceptions::Account::RegisterRequired',
1392 'Exception thrown for UseCashRegisters:1 + payment_type:CASH + cash_register:undef';
1394 # Disable cash registers
1395 t::lib::Mocks::mock_preference( 'UseCashRegisters', 0 );
1397 # Enable AccountAutoReconcile
1398 t::lib::Mocks::mock_preference( 'AccountAutoReconcile', 1 );
1400 # Add some outstanding debits
1401 my $debit_1 = $account->add_debit( { amount => 2, interface => 'commandline', type => 'OVERDUE' } );
1402 my $debit_2 = $account->add_debit( { amount => 3, interface => 'commandline', type => 'OVERDUE' } );
1403 my $debit_3 = $account->add_debit( { amount => 5, interface => 'commandline', type => 'OVERDUE' } );
1404 my $debit_4 = $account->add_debit( { amount => 10, interface => 'commandline', type => 'OVERDUE' } );
1405 my $debits = $account->outstanding_debits();
1406 is( $debits->count, 4, "Found 4 debits with outstanding amounts" );
1407 is( $debits->total_outstanding + 0, 20, "Total 20 outstanding debit" );
1409 my $payin = $account->payin_amount($payin_params);
1410 is(ref($payin), 'Koha::Account::Line', 'Return the Koha::Account::Line object for the payin');
1411 is($payin->amount + 0, -10, "Payin amount recorded correctly");
1412 is($payin->amountoutstanding + 0, 0, "Full amount was used to pay debts");
1413 $debits = $account->outstanding_debits();
1414 is($debits->count, 1, "Payin was applied against oldest outstanding debits first");
1415 is($debits->total_outstanding + 0, 10, "Total of 10 outstanding debit remaining");
1417 my $offsets = Koha::Account::Offsets->search( { credit_id => $payin->id } );
1418 is( $offsets->count, 4, 'Four offsets generated' );
1419 my $offset = $offsets->next;
1420 is( $offset->type, 'CREATE', 'CREATE offset added for payin line' );
1421 is( $offset->amount * 1, 10, 'Correct offset amount recorded' );
1422 $offset = $offsets->next;
1423 is( $offset->debit_id, $debit_1->id, "Offset added against debit_1");
1424 is( $offset->type, 'APPLY', "APPLY used for offset_type" );
1425 is( $offset->amount * 1, -2, 'Correct amount offset against debit_1' );
1426 $offset = $offsets->next;
1427 is( $offset->debit_id, $debit_2->id, "Offset added against debit_2");
1428 is( $offset->type, 'APPLY', "APPLY used for offset_type" );
1429 is( $offset->amount * 1, -3, 'Correct amount offset against debit_2' );
1430 $offset = $offsets->next;
1431 is( $offset->debit_id, $debit_3->id, "Offset added against debit_3");
1432 is( $offset->type, 'APPLY', "APPLY used for offset_type" );
1433 is( $offset->amount * 1, -5, 'Correct amount offset against debit_3' );
1435 my $debit_5 = $account->add_debit( { amount => 5, interface => 'commandline', type => 'OVERDUE' } );
1436 $debits = $account->outstanding_debits();
1437 is($debits->count, 2, "New debit added");
1438 $payin_params->{amount} = 2.50;
1439 $payin_params->{debits} = [$debit_5];
1440 $payin = $account->payin_amount($payin_params);
1442 $debits = $account->outstanding_debits();
1443 is($debits->count, 2, "Second debit not fully paid off");
1444 is($debits->total_outstanding + 0, 12.50, "12.50 outstanding debit remaining");
1445 $debit_4->discard_changes;
1446 $debit_5->discard_changes;
1447 is($debit_4->amountoutstanding + 0, 10, "Debit 4 unaffected when debit_5 was passed to payin_amount");
1448 is($debit_5->amountoutstanding + 0, 2.50, "Debit 5 correctly reduced when payin_amount called with debit_5 passed");
1450 $offsets = Koha::Account::Offsets->search( { credit_id => $payin->id } );
1451 is( $offsets->count, 2, 'Two offsets generated' );
1452 $offset = $offsets->next;
1453 is( $offset->type, 'CREATE', 'CREATE offset added for payin line' );
1454 is( $offset->amount * 1, 2.50, 'Correct offset amount recorded' );
1455 $offset = $offsets->next;
1456 is( $offset->debit_id, $debit_5->id, "Offset added against debit_5");
1457 is( $offset->type, 'APPLY', "APPLY used for offset_type" );
1458 is( $offset->amount * 1, -2.50, 'Correct amount offset against debit_5' );
1460 $schema->storage->txn_rollback;