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 my $item = $builder->build_sample_item;
299 my $checkout = Koha::Checkout->new(
301 borrowernumber => $patron->id,
302 itemnumber => $item->id,
303 date_due => \'NOW()',
304 branchcode => $patron->branchcode,
305 issuedate => \'NOW()',
309 my $line_4 = $account->add_credit(
312 description => 'Manual credit applied',
313 library_id => $patron->branchcode,
314 user_id => $patron->id,
316 interface => 'commandline',
317 issue_id => $checkout->id
321 is( $line_4->issue_id, $checkout->id, 'The issue ID matches the checkout ID' );
323 $schema->storage->txn_rollback;
326 subtest 'add_debit() tests' => sub {
330 $schema->storage->txn_begin;
332 # delete logs and statistics
333 my $action_logs = $schema->resultset('ActionLog')->search()->count;
334 my $statistics = $schema->resultset('Statistic')->search()->count;
336 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
338 Koha::Account->new( { patron_id => $patron->borrowernumber } );
340 is( $account->balance, 0, 'Patron has no balance' );
346 description => 'amount validation failure',
347 library_id => $patron->branchcode,
348 note => 'this should fail anyway',
350 user_id => $patron->id,
351 interface => 'commandline'
353 ); } 'Koha::Exceptions::Account::AmountNotPositive', 'Expected validation exception thrown (amount)';
357 open STDERR, '>', '/dev/null';
361 description => 'type validation failure',
362 library_id => $patron->branchcode,
363 note => 'this should fail anyway',
365 user_id => $patron->id,
366 interface => 'commandline'
371 'Koha::Exceptions::Account::UnrecognisedType',
372 'Expected validation exception thrown (type)';
378 description => 'Rental charge of 25',
379 library_id => $patron->branchcode,
380 note => 'not really important',
382 user_id => $patron->id
384 ); } 'Koha::Exceptions::MissingParameter', 'Exception thrown if interface parameter missing';
387 t::lib::Mocks::mock_preference( 'FinesLog', 0 );
389 my $line_1 = $account->add_debit(
392 description => 'Rental charge of 25',
393 library_id => $patron->branchcode,
394 note => 'not really important',
396 user_id => $patron->id,
397 interface => 'commandline'
401 is( $account->balance, 25, 'Patron has a balance of 25' );
403 $schema->resultset('ActionLog')->count(),
408 $line_1->debit_type_code,
410 'Account type is correctly set'
414 t::lib::Mocks::mock_preference( 'FinesLog', 1 );
416 my $line_2 = $account->add_debit(
419 description => 'Rental charge of 37',
420 library_id => $patron->branchcode,
421 note => 'not really important',
423 user_id => $patron->id,
424 interface => 'commandline'
428 is( $account->balance, 62, 'Patron has a balance of 62' );
430 $schema->resultset('ActionLog')->count(),
435 $line_2->debit_type_code,
437 'Account type is correctly set'
440 # offsets have the debit_id set to accountlines_id, and credit_id is undef
442 Koha::Account::Offsets->search( { debit_id => $line_1->id } )->next;
444 Koha::Account::Offsets->search( { debit_id => $line_2->id } )->next;
446 is( $offset_1->debit_id, $line_1->id, 'debit_id is set for debit 1' );
447 is( $offset_1->credit_id, undef, 'credit_id is not set for debit 1' );
448 is( $offset_2->debit_id, $line_2->id, 'debit_id is set for debit 2' );
449 is( $offset_2->credit_id, undef, 'credit_id is not set for debit 2' );
451 $schema->storage->txn_rollback;
454 subtest 'lines() tests' => sub {
458 $schema->storage->txn_begin;
460 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
461 my $account = $patron->account;
464 $account->add_credit({ amount => 1, interface => 'commandline' });
465 $account->add_credit({ amount => 2, interface => 'commandline' });
466 $account->add_credit({ amount => 3, interface => 'commandline' });
467 $account->add_credit({ amount => 4, interface => 'commandline' });
470 $account->add_debit({ amount => 1, interface => 'commandline', type => 'OVERDUE' });
471 $account->add_debit({ amount => 2, interface => 'commandline', type => 'OVERDUE' });
472 $account->add_debit({ amount => 3, interface => 'commandline', type => 'OVERDUE' });
473 $account->add_debit({ amount => 4, interface => 'commandline', type => 'OVERDUE' });
476 $account->add_credit( { amount => 1, interface => 'commandline' } )
477 ->apply( { debits => [ $account->outstanding_debits->as_list ] } );
479 my $lines = $account->lines;
480 is( $lines->_resultset->count, 9, "All accountlines (debits, credits and paid off) were fetched");
482 $schema->storage->txn_rollback;
485 subtest 'reconcile_balance' => sub {
489 subtest 'more credit than debit' => sub {
493 $schema->storage->txn_begin;
495 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
496 my $account = $patron->account;
499 $account->add_credit({ amount => 1, interface => 'commandline' });
500 $account->add_credit({ amount => 2, interface => 'commandline' });
501 $account->add_credit({ amount => 3, interface => 'commandline' });
502 $account->add_credit({ amount => 4, interface => 'commandline' });
503 $account->add_credit({ amount => 5, interface => 'commandline' });
506 $account->add_debit({ amount => 1, interface => 'commandline', type => 'OVERDUE' });
507 $account->add_debit({ amount => 2, interface => 'commandline', type => 'OVERDUE' });
508 $account->add_debit({ amount => 3, interface => 'commandline', type => 'OVERDUE' });
509 $account->add_debit({ amount => 4, interface => 'commandline', type => 'OVERDUE' });
512 Koha::Account::Line->new(
514 borrowernumber => $patron->id,
516 amountoutstanding => 0,
517 interface => 'commandline',
518 debit_type_code => 'OVERDUE'
521 Koha::Account::Line->new(
523 borrowernumber => $patron->id,
525 amountoutstanding => 0,
526 interface => 'commandline',
527 debit_type_code => 'OVERDUE'
531 is( $account->balance(), -5, "Account balance is -5" );
532 is( $account->outstanding_debits->total_outstanding, 10, 'Outstanding debits sum 10' );
533 is( $account->outstanding_credits->total_outstanding, -15, 'Outstanding credits sum -15' );
535 $account->reconcile_balance();
537 is( $account->balance(), -5, "Account balance is -5" );
538 is( $account->outstanding_debits->total_outstanding, 0, 'No outstanding debits' );
539 is( $account->outstanding_credits->total_outstanding, -5, 'Outstanding credits sum -5' );
541 $schema->storage->txn_rollback;
544 subtest 'same debit as credit' => sub {
548 $schema->storage->txn_begin;
550 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
551 my $account = $patron->account;
554 $account->add_credit({ amount => 1, interface => 'commandline' });
555 $account->add_credit({ amount => 2, interface => 'commandline' });
556 $account->add_credit({ amount => 3, interface => 'commandline' });
557 $account->add_credit({ amount => 4, interface => 'commandline' });
560 $account->add_debit({ amount => 1, interface => 'commandline', type => 'OVERDUE' });
561 $account->add_debit({ amount => 2, interface => 'commandline', type => 'OVERDUE' });
562 $account->add_debit({ amount => 3, interface => 'commandline', type => 'OVERDUE' });
563 $account->add_debit({ amount => 4, interface => 'commandline', type => 'OVERDUE' });
566 Koha::Account::Line->new(
568 borrowernumber => $patron->id,
570 amountoutstanding => 0,
571 interface => 'commandline',
572 debit_type_code => 'OVERDUE'
575 Koha::Account::Line->new(
577 borrowernumber => $patron->id,
579 amountoutstanding => 0,
580 interface => 'commandline',
581 debit_type_code => 'OVERDUE'
585 is( $account->balance(), 0, "Account balance is 0" );
586 is( $account->outstanding_debits->total_outstanding, 10, 'Outstanding debits sum 10' );
587 is( $account->outstanding_credits->total_outstanding, -10, 'Outstanding credits sum -10' );
589 $account->reconcile_balance();
591 is( $account->balance(), 0, "Account balance is 0" );
592 is( $account->outstanding_debits->total_outstanding, 0, 'No outstanding debits' );
593 is( $account->outstanding_credits->total_outstanding, 0, 'Outstanding credits sum 0' );
595 $schema->storage->txn_rollback;
598 subtest 'more debit than credit' => sub {
602 $schema->storage->txn_begin;
604 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
605 my $account = $patron->account;
608 $account->add_credit({ amount => 1, interface => 'commandline' });
609 $account->add_credit({ amount => 2, interface => 'commandline' });
610 $account->add_credit({ amount => 3, interface => 'commandline' });
611 $account->add_credit({ amount => 4, interface => 'commandline' });
614 $account->add_debit({ amount => 1, interface => 'commandline', type => 'OVERDUE' });
615 $account->add_debit({ amount => 2, interface => 'commandline', type => 'OVERDUE' });
616 $account->add_debit({ amount => 3, interface => 'commandline', type => 'OVERDUE' });
617 $account->add_debit({ amount => 4, interface => 'commandline', type => 'OVERDUE' });
618 $account->add_debit({ amount => 5, interface => 'commandline', type => 'OVERDUE' });
621 Koha::Account::Line->new(
623 borrowernumber => $patron->id,
625 amountoutstanding => 0,
626 interface => 'commandline',
627 debit_type_code => 'OVERDUE'
630 Koha::Account::Line->new(
632 borrowernumber => $patron->id,
634 amountoutstanding => 0,
635 interface => 'commandline',
636 debit_type_code => 'OVERDUE'
640 is( $account->balance(), 5, "Account balance is 5" );
641 is( $account->outstanding_debits->total_outstanding, 15, 'Outstanding debits sum 15' );
642 is( $account->outstanding_credits->total_outstanding, -10, 'Outstanding credits sum -10' );
644 $account->reconcile_balance();
646 is( $account->balance(), 5, "Account balance is 5" );
647 is( $account->outstanding_debits->total_outstanding, 5, 'Outstanding debits sum 5' );
648 is( $account->outstanding_credits->total_outstanding, 0, 'Outstanding credits sum 0' );
650 $schema->storage->txn_rollback;
653 subtest 'credits are applied to older debits first' => sub {
657 $schema->storage->txn_begin;
659 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
660 my $account = $patron->account;
663 $account->add_credit({ amount => 1, interface => 'commandline' });
664 $account->add_credit({ amount => 3, interface => 'commandline' });
667 my $debit_1 = $account->add_debit({ amount => 1, interface => 'commandline', type => 'OVERDUE' });
668 my $debit_2 = $account->add_debit({ amount => 2, interface => 'commandline', type => 'OVERDUE' });
669 my $debit_3 = $account->add_debit({ amount => 3, interface => 'commandline', type => 'OVERDUE' });
671 is( $account->balance(), 2, "Account balance is 2" );
672 is( $account->outstanding_debits->total_outstanding, 6, 'Outstanding debits sum 6' );
673 is( $account->outstanding_credits->total_outstanding, -4, 'Outstanding credits sum -4' );
675 $account->reconcile_balance();
677 is( $account->balance(), 2, "Account balance is 2" );
678 is( $account->outstanding_debits->total_outstanding, 2, 'Outstanding debits sum 2' );
679 is( $account->outstanding_credits->total_outstanding, 0, 'Outstanding credits sum 0' );
681 $debit_1->discard_changes;
682 is( $debit_1->amountoutstanding + 0, 0, 'Old debit payed' );
683 $debit_2->discard_changes;
684 is( $debit_2->amountoutstanding + 0, 0, 'Old debit payed' );
685 $debit_3->discard_changes;
686 is( $debit_3->amountoutstanding + 0, 2, 'Newest debit only partially payed' );
688 $schema->storage->txn_rollback;
692 subtest 'pay() tests' => sub {
696 $schema->storage->txn_begin;
698 # Disable renewing upon fine payment
699 t::lib::Mocks::mock_preference( 'RenewAccruingItemWhenPaid', 0 );
702 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
703 my $library = $builder->build_object({ class => 'Koha::Libraries' });
704 my $account = $patron->account;
706 t::lib::Mocks::mock_preference( 'RequirePaymentType', 1 );
711 interface => 'intranet',
715 'Koha::Exceptions::Account::PaymentTypeRequired',
716 'Exception thrown for RequirePaymentType:1 + payment_type:undef';
722 interface => 'intranet',
723 payment_type => 'FOOBAR'
727 'Koha::Exceptions::Account::InvalidPaymentType',
728 'Exception thrown for InvalidPaymentType:1 + payment_type:FOOBAR';
730 my $writeoff_id = $account->pay(
733 interface => 'intranet',
737 my $writeoff = Koha::Account::Lines->find($writeoff_id);
738 is( $writeoff->payment_type, undef, "Writeoff should not have a payment_type " );
740 t::lib::Mocks::mock_preference( 'RequirePaymentType', 0 );
741 my $context = Test::MockModule->new('C4::Context');
742 $context->mock( 'userenv', { branch => $library->id } );
744 my $credit_1_id = $account->pay({ amount => 200 })->{payment_id};
745 my $credit_1 = Koha::Account::Lines->find( $credit_1_id );
747 is( $credit_1->branchcode, undef, 'No branchcode is set if library_id was not passed' );
749 my $credit_2_id = $account->pay({ amount => 150, library_id => $library->id })->{payment_id};
750 my $credit_2 = Koha::Account::Lines->find( $credit_2_id );
752 is( $credit_2->branchcode, $library->id, 'branchcode set because library_id was passed' );
753 # Enable cash registers
754 t::lib::Mocks::mock_preference( 'UseCashRegisters', 1 );
759 payment_type => 'CASH',
760 interface => 'intranet'
764 'Koha::Exceptions::Account::RegisterRequired',
765 'Exception thrown for UseCashRegisters:1 + payment_type:CASH + cash_register:undef';
767 # Disable cash registers
768 t::lib::Mocks::mock_preference( 'UseCashRegisters', 0 );
771 $context->mock( 'userenv', undef );
772 my $result = $account->pay(
775 payment_type => 'CASH',
776 interface => 'intranet'
779 ok($result, "Koha::Account->pay functions without a userenv");
780 my $payment = Koha::Account::Lines->find({accountlines_id => $result->{payment_id}});
781 is($payment->manager_id, undef, "manager_id left undefined when no userenv found");
783 subtest 'UseEmailReceipts tests' => sub {
787 t::lib::Mocks::mock_preference( 'UseEmailReceipts', 1 );
791 my $mocked_letters = Test::MockModule->new('C4::Letters');
792 # we want to test the params
793 $mocked_letters->mock( 'GetPreparedLetter', sub {
797 # we don't care about EnqueueLetter for now
798 $mocked_letters->mock( 'EnqueueLetter', sub {
802 $schema->storage->txn_begin;
804 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
805 my $account = $patron->account;
807 my $debit_1 = $account->add_debit( { amount => 5, interface => 'commandline', type => 'OVERDUE' } );
808 my $debit_2 = $account->add_debit( { amount => 10, interface => 'commandline', type => 'OVERDUE' } );
810 $account->pay({ amount => 6, lines => [ $debit_1, $debit_2 ] });
811 my @offsets = @{$params{substitute}{offsets}};
813 is( scalar @offsets, 2, 'Two offsets related to payment' );
814 is( ref($offsets[0]), 'Koha::Account::Offset', 'Type is correct' );
815 is( ref($offsets[1]), 'Koha::Account::Offset', 'Type is correct' );
816 is( $offsets[0]->type, 'APPLY', 'Only APPLY offsets are passed to the notice' );
817 is( $offsets[1]->type, 'APPLY', 'Only APPLY offsets are passed to the notice' );
819 $schema->storage->txn_rollback;
822 $schema->storage->txn_rollback;
825 subtest 'pay() handles lost items when paying a specific lost fee' => sub {
829 $schema->storage->txn_begin;
831 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
832 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
833 my $account = $patron->account;
835 my $context = Test::MockModule->new('C4::Context');
836 $context->mock( 'userenv', { branch => $library->id } );
838 my $biblio = $builder->build_sample_biblio();
840 $builder->build_sample_item( { biblionumber => $biblio->biblionumber } );
842 my $checkout = Koha::Checkout->new(
844 borrowernumber => $patron->id,
845 itemnumber => $item->id,
846 date_due => \'NOW()',
847 branchcode => $patron->branchcode,
848 issuedate => \'NOW()',
852 $item->itemlost('1')->store();
854 my $accountline = Koha::Account::Line->new(
856 issue_id => $checkout->id,
857 borrowernumber => $patron->id,
858 itemnumber => $item->id,
860 debit_type_code => 'LOST',
863 amountoutstanding => '1',
870 library_id => $library->id,
871 lines => [$accountline],
875 $accountline = Koha::Account::Lines->find( $accountline->id );
876 is( $accountline->amountoutstanding+0, .5, 'Account line was paid down by half' );
878 $checkout = Koha::Checkouts->find( $checkout->id );
879 ok( $checkout, 'Item still checked out to patron' );
884 library_id => $library->id,
885 lines => [$accountline],
889 $accountline = Koha::Account::Lines->find( $accountline->id );
890 is( $accountline->amountoutstanding+0, 0, 'Account line was paid down by half' );
892 $checkout = Koha::Checkouts->find( $checkout->id );
893 ok( !$checkout, 'Item was removed from patron account' );
895 subtest 'item was not checked out to the same patron' => sub {
898 my $patron_2 = $builder->build_object(
900 class => 'Koha::Patrons',
901 value => { branchcode => $library->branchcode }
904 $item->itemlost('1')->store();
905 C4::Accounts::chargelostitem( $patron->borrowernumber, $item->itemnumber, 5, "lost" );
906 my $accountline = Koha::Account::Lines->search(
908 borrowernumber => $patron->borrowernumber,
909 itemnumber => $item->itemnumber,
910 debit_type_code => 'LOST'
913 my $checkout = Koha::Checkout->new(
915 borrowernumber => $patron_2->borrowernumber,
916 itemnumber => $item->itemnumber,
917 date_due => \'NOW()',
918 branchcode => $patron_2->branchcode,
919 issuedate => \'NOW()',
923 $patron->account->pay(
926 library_id => $library->branchcode,
927 lines => [$accountline],
932 Koha::Checkouts->find( $checkout->issue_id ),
933 'If the item is checked out to another patron, a lost item should not be returned if lost fee is paid'
938 $schema->storage->txn_rollback;
941 subtest 'pay() handles lost items when paying by amount ( not specifying the lost fee )' => sub {
945 $schema->storage->txn_begin;
947 # Enable AccountAutoReconcile
948 t::lib::Mocks::mock_preference( 'AccountAutoReconcile', 1 );
950 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
951 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
952 my $account = $patron->account;
954 my $context = Test::MockModule->new('C4::Context');
955 $context->mock( 'userenv', { branch => $library->id } );
957 my $biblio = $builder->build_sample_biblio();
959 $builder->build_sample_item( { biblionumber => $biblio->biblionumber } );
961 my $checkout = Koha::Checkout->new(
963 borrowernumber => $patron->id,
964 itemnumber => $item->id,
965 date_due => \'NOW()',
966 branchcode => $patron->branchcode,
967 issuedate => \'NOW()',
971 $item->itemlost('1')->store();
973 my $accountline = Koha::Account::Line->new(
975 issue_id => $checkout->id,
976 borrowernumber => $patron->id,
977 itemnumber => $item->id,
979 debit_type_code => 'LOST',
982 amountoutstanding => '1',
989 library_id => $library->id,
993 $accountline = Koha::Account::Lines->find( $accountline->id );
994 is( $accountline->amountoutstanding+0, .5, 'Account line was paid down by half' );
996 $checkout = Koha::Checkouts->find( $checkout->id );
997 ok( $checkout, 'Item still checked out to patron' );
1002 library_id => $library->id,
1006 $accountline = Koha::Account::Lines->find( $accountline->id );
1007 is( $accountline->amountoutstanding+0, 0, 'Account line was paid down by half' );
1009 $checkout = Koha::Checkouts->find( $checkout->id );
1010 ok( !$checkout, 'Item was removed from patron account' );
1012 $schema->storage->txn_rollback;
1015 subtest 'pay() renews items when appropriate' => sub {
1019 $schema->storage->txn_begin;
1021 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
1022 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
1023 my $account = $patron->account;
1025 my $context = Test::MockModule->new('C4::Context');
1026 $context->mock( 'userenv', { branch => $library->id } );
1028 my $biblio = $builder->build_sample_biblio();
1030 $builder->build_sample_item( { biblionumber => $biblio->biblionumber } );
1032 my $now = dt_from_string();
1033 my $seven_weeks = DateTime::Duration->new(weeks => 7);
1034 my $five_weeks = DateTime::Duration->new(weeks => 5);
1035 my $seven_weeks_ago = $now - $seven_weeks;
1036 my $five_weeks_ago = $now - $five_weeks;
1038 my $checkout = Koha::Checkout->new(
1040 borrowernumber => $patron->id,
1041 itemnumber => $item->id,
1042 date_due => $five_weeks_ago,
1043 branchcode => $patron->branchcode,
1044 issuedate => $seven_weeks_ago
1048 my $accountline = Koha::Account::Line->new(
1050 issue_id => $checkout->id,
1051 borrowernumber => $patron->id,
1052 itemnumber => $item->id,
1054 debit_type_code => 'OVERDUE',
1055 status => 'UNRETURNED',
1058 amountoutstanding => '1',
1062 # Enable renewing upon fine payment
1063 t::lib::Mocks::mock_preference( 'RenewAccruingItemWhenPaid', 1 );
1065 my $module = Test::MockModule->new('C4::Circulation');
1066 $module->mock('AddRenewal', sub { $called = 1; });
1067 $module->mock('CanBookBeRenewed', sub { return 1; });
1068 my $result = $account->pay(
1071 library_id => $library->id,
1075 is( $called, 1, 'RenewAccruingItemWhenPaid causes C4::Circulation::AddRenew to be called when appropriate' );
1076 is(ref($result->{renew_result}), 'ARRAY', "Pay result contains 'renew_result' ARRAY" );
1077 is( scalar @{$result->{renew_result}}, 1, "renew_result contains one renewal result" );
1078 is( $result->{renew_result}->[0]->{itemnumber}, $item->id, "renew_result contains itemnumber of renewed item" );
1080 # Reset test by adding a new overdue
1081 Koha::Account::Line->new(
1083 issue_id => $checkout->id,
1084 borrowernumber => $patron->id,
1085 itemnumber => $item->id,
1087 debit_type_code => 'OVERDUE',
1088 status => 'UNRETURNED',
1091 amountoutstanding => '1',
1096 t::lib::Mocks::mock_preference( 'RenewAccruingItemWhenPaid', 0 );
1097 $result = $account->pay(
1100 library_id => $library->id,
1104 is( $called, 0, 'C4::Circulation::AddRenew NOT called when RenewAccruingItemWhenPaid disabled' );
1105 is(ref($result->{renew_result}), 'ARRAY', "Pay result contains 'renew_result' ARRAY" );
1106 is( scalar @{$result->{renew_result}}, 0, "renew_result contains no renewal results" );
1108 $schema->storage->txn_rollback;
1111 subtest 'Koha::Account::Line::apply() handles lost items' => sub {
1115 $schema->storage->txn_begin;
1117 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
1118 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
1119 my $account = $patron->account;
1121 my $context = Test::MockModule->new('C4::Context');
1122 $context->mock( 'userenv', { branch => $library->id } );
1124 my $biblio = $builder->build_sample_biblio();
1126 $builder->build_sample_item( { biblionumber => $biblio->biblionumber } );
1128 my $checkout = Koha::Checkout->new(
1130 borrowernumber => $patron->id,
1131 itemnumber => $item->id,
1132 date_due => \'NOW()',
1133 branchcode => $patron->branchcode,
1134 issuedate => \'NOW()',
1138 $item->itemlost('1')->store();
1140 my $debit = Koha::Account::Line->new(
1142 issue_id => $checkout->id,
1143 borrowernumber => $patron->id,
1144 itemnumber => $item->id,
1146 debit_type_code => 'LOST',
1149 amountoutstanding => '1',
1153 my $credit = Koha::Account::Line->new(
1155 borrowernumber => $patron->id,
1156 date => '1970-01-01 14:00:01',
1158 amountoutstanding => -.5,
1159 interface => 'commandline',
1160 credit_type_code => 'PAYMENT'
1163 my $debits = $account->outstanding_debits;
1164 $credit->apply({ debits => [ $debits->as_list ] });
1166 $debit = Koha::Account::Lines->find( $debit->id );
1167 is( $debit->amountoutstanding+0, .5, 'Account line was paid down by half' );
1169 $checkout = Koha::Checkouts->find( $checkout->id );
1170 ok( $checkout, 'Item still checked out to patron' );
1172 $credit = Koha::Account::Line->new(
1174 borrowernumber => $patron->id,
1175 date => '1970-01-01 14:00:01',
1177 amountoutstanding => -.5,
1178 interface => 'commandline',
1179 credit_type_code => 'PAYMENT'
1182 $debits = $account->outstanding_debits;
1183 $credit->apply({ debits => [ $debits->as_list ] });
1185 $debit = Koha::Account::Lines->find( $debit->id );
1186 is( $debit->amountoutstanding+0, 0, 'Account line was paid down by half' );
1188 $checkout = Koha::Checkouts->find( $checkout->id );
1189 ok( !$checkout, 'Item was removed from patron account' );
1191 $schema->storage->txn_rollback;
1194 subtest 'Koha::Account::pay() generates credit number (Koha::Account::Line->store)' => sub {
1197 $schema->storage->txn_begin;
1199 Koha::Account::Lines->delete();
1201 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
1202 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
1203 my $account = $patron->account;
1205 #t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode });
1206 my $context = Test::MockModule->new('C4::Context');
1207 $context->mock( 'userenv', { branch => $library->id } );
1209 my $now = dt_from_string;
1210 my $year = $now->year;
1211 my $month = $now->month;
1212 my ($accountlines_id, $accountline);
1214 my $credit_type = Koha::Account::CreditTypes->find('PAYMENT');
1215 $credit_type->credit_number_enabled(1);
1216 $credit_type->store();
1218 t::lib::Mocks::mock_preference('AutoCreditNumber', '');
1219 $accountlines_id = $account->pay({ amount => '1.00', library_id => $library->id })->{payment_id};
1220 $accountline = Koha::Account::Lines->find($accountlines_id);
1221 is($accountline->credit_number, undef, 'No credit number is generated when syspref is off');
1223 t::lib::Mocks::mock_preference('AutoCreditNumber', 'incremental');
1225 $accountlines_id = $account->pay({ amount => '1.00', library_id => $library->id })->{payment_id};
1226 $accountline = Koha::Account::Lines->find($accountlines_id);
1227 is($accountline->credit_number, $i, "Incremental format credit number added for payments: $i");
1229 $accountlines_id = $account->pay({ type => 'WRITEOFF', amount => '1.00', library_id => $library->id })->{payment_id};
1230 $accountline = Koha::Account::Lines->find($accountlines_id);
1231 is($accountline->credit_number, undef, "Incremental credit number not added for writeoff");
1233 t::lib::Mocks::mock_preference('AutoCreditNumber', 'annual');
1235 $accountlines_id = $account->pay({ amount => '1.00', library_id => $library->id })->{payment_id};
1236 $accountline = Koha::Account::Lines->find($accountlines_id);
1237 is($accountline->credit_number, sprintf('%s-%04d', $year, $i), "Annual format credit number added for payments: " . sprintf('%s-%04d', $year, $i));
1239 $accountlines_id = $account->pay({ type => 'WRITEOFF', amount => '1.00', library_id => $library->id })->{payment_id};
1240 $accountline = Koha::Account::Lines->find($accountlines_id);
1241 is($accountline->credit_number, undef, "Annual format credit number not aded for writeoff");
1243 t::lib::Mocks::mock_preference('AutoCreditNumber', 'branchyyyymmincr');
1245 $accountlines_id = $account->pay({ amount => '1.00', library_id => $library->id })->{payment_id};
1246 $accountline = Koha::Account::Lines->find($accountlines_id);
1247 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));
1249 $accountlines_id = $account->pay({ type => 'WRITEOFF', amount => '1.00', library_id => $library->id })->{payment_id};
1250 $accountline = Koha::Account::Lines->find($accountlines_id);
1251 is($accountline->credit_number, undef, "branchyyyymmincr format credit number not added for writeoff");
1254 Koha::Account::Line->new(
1256 interface => 'test',
1258 credit_type_code => $credit_type->code,
1263 'Koha::Exceptions::Account',
1264 "Exception thrown when AutoCreditNumber is enabled but credit_number is already defined";
1266 $schema->storage->txn_rollback;
1269 subtest 'Koha::Account::payout_amount() tests' => sub {
1272 $schema->storage->txn_begin;
1274 # delete logs and statistics
1275 my $action_logs = $schema->resultset('ActionLog')->search()->count;
1276 my $statistics = $schema->resultset('Statistic')->search()->count;
1278 my $staff = $builder->build_object( { class => 'Koha::Patrons' } );
1279 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
1281 $builder->build_object( { class => 'Koha::Cash::Registers' } );
1282 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
1284 Koha::Account->new( { patron_id => $patron->borrowernumber } );
1286 is( $account->balance, 0, 'Test patron has no balance' );
1288 my $payout_params = {
1289 payout_type => 'CASH',
1290 branch => $library->id,
1291 register_id => $register->id,
1292 staff_id => $staff->id,
1293 interface => 'intranet',
1297 my @required_fields =
1298 ( 'interface', 'staff_id', 'branch', 'payout_type', 'amount' );
1299 for my $required_field (@required_fields) {
1300 my $this_payout = { %{$payout_params} };
1301 delete $this_payout->{$required_field};
1304 $account->payout_amount($this_payout);
1306 'Koha::Exceptions::MissingParameter',
1307 "Exception thrown if $required_field parameter missing";
1311 $account->payout_amount($payout_params);
1313 'Koha::Exceptions::Account::AmountNotPositive',
1314 'Expected validation exception thrown (amount not positive)';
1316 $payout_params->{amount} = 10;
1318 $account->payout_amount($payout_params);
1320 'Koha::Exceptions::ParameterTooHigh',
1321 'Expected validation exception thrown (amount greater than outstanding)';
1323 # Enable cash registers
1324 t::lib::Mocks::mock_preference( 'UseCashRegisters', 1 );
1326 $account->payout_amount($payout_params);
1328 'Koha::Exceptions::Account::RegisterRequired',
1329 'Exception thrown for UseCashRegisters:1 + payout_type:CASH + cash_register:undef';
1331 # Disable cash registers
1332 t::lib::Mocks::mock_preference( 'UseCashRegisters', 0 );
1334 # Add some outstanding credits
1335 my $credit_1 = $account->add_credit( { amount => 2, interface => 'commandline' } );
1336 my $credit_2 = $account->add_credit( { amount => 3, interface => 'commandline' } );
1337 my $credit_3 = $account->add_credit( { amount => 5, interface => 'commandline' } );
1338 my $credit_4 = $account->add_credit( { amount => 10, interface => 'commandline' } );
1339 my $credits = $account->outstanding_credits();
1340 is( $credits->count, 4, "Found 4 credits with outstanding amounts" );
1341 is( $credits->total_outstanding + 0, -20, "Total -20 outstanding credit" );
1343 my $payout = $account->payout_amount($payout_params);
1344 is(ref($payout), 'Koha::Account::Line', 'Return the Koha::Account::Line object for the payout');
1345 is($payout->amount + 0, 10, "Payout amount recorded correctly");
1346 is($payout->amountoutstanding + 0, 0, "Full amount was paid out");
1347 $credits = $account->outstanding_credits();
1348 is($credits->count, 1, "Payout was applied against oldest outstanding credits first");
1349 is($credits->total_outstanding + 0, -10, "Total of 10 outstanding credit remaining");
1351 my $offsets = Koha::Account::Offsets->search( { debit_id => $payout->id } );
1352 is( $offsets->count, 4, 'Four offsets generated' );
1353 my $offset = $offsets->next;
1354 is( $offset->type, 'CREATE', 'CREATE offset added for payout line' );
1355 is( $offset->amount * 1, 10, 'Correct offset amount recorded' );
1356 $offset = $offsets->next;
1357 is( $offset->credit_id, $credit_1->id, "Offset added against credit_1");
1358 is( $offset->type, 'APPLY', "APPLY used for offset_type" );
1359 is( $offset->amount * 1, -2, 'Correct amount offset against credit_1' );
1360 $offset = $offsets->next;
1361 is( $offset->credit_id, $credit_2->id, "Offset added against credit_2");
1362 is( $offset->type, 'APPLY', "APPLY used for offset_type" );
1363 is( $offset->amount * 1, -3, 'Correct amount offset against credit_2' );
1364 $offset = $offsets->next;
1365 is( $offset->credit_id, $credit_3->id, "Offset added against credit_3");
1366 is( $offset->type, 'APPLY', "APPLY used for offset_type" );
1367 is( $offset->amount * 1, -5, 'Correct amount offset against credit_3' );
1369 my $credit_5 = $account->add_credit( { amount => 5, interface => 'commandline' } );
1370 $credits = $account->outstanding_credits();
1371 is($credits->count, 2, "New credit added");
1372 $payout_params->{amount} = 2.50;
1373 $payout_params->{credits} = [$credit_5];
1374 $payout = $account->payout_amount($payout_params);
1376 $credits = $account->outstanding_credits();
1377 is($credits->count, 2, "Second credit not fully paid off");
1378 is($credits->total_outstanding + 0, -12.50, "12.50 outstanding credit remaining");
1379 $credit_4->discard_changes;
1380 $credit_5->discard_changes;
1381 is($credit_4->amountoutstanding + 0, -10, "Credit 4 unaffected when credit_5 was passed to payout_amount");
1382 is($credit_5->amountoutstanding + 0, -2.50, "Credit 5 correctly reduced when payout_amount called with credit_5 passed");
1384 $offsets = Koha::Account::Offsets->search( { debit_id => $payout->id } );
1385 is( $offsets->count, 2, 'Two offsets generated' );
1386 $offset = $offsets->next;
1387 is( $offset->type, 'CREATE', 'CREATE offset added for payout line' );
1388 is( $offset->amount * 1, 2.50, 'Correct offset amount recorded' );
1389 $offset = $offsets->next;
1390 is( $offset->credit_id, $credit_5->id, "Offset added against credit_5");
1391 is( $offset->type, 'APPLY', "APPLY used for offset_type" );
1392 is( $offset->amount * 1, -2.50, 'Correct amount offset against credit_5' );
1394 $schema->storage->txn_rollback;
1397 subtest 'Koha::Account::payin_amount() tests' => sub {
1400 $schema->storage->txn_begin;
1402 # delete logs and statistics
1403 my $action_logs = $schema->resultset('ActionLog')->search()->count;
1404 my $statistics = $schema->resultset('Statistic')->search()->count;
1406 my $staff = $builder->build_object( { class => 'Koha::Patrons' } );
1407 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
1409 $builder->build_object( { class => 'Koha::Cash::Registers' } );
1410 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
1412 Koha::Account->new( { patron_id => $patron->borrowernumber } );
1414 is( $account->balance, 0, 'Test patron has no balance' );
1416 my $payin_params = {
1418 payment_type => 'CASH',
1419 branch => $library->id,
1420 register_id => $register->id,
1421 staff_id => $staff->id,
1422 interface => 'intranet',
1426 my @required_fields =
1427 ( 'interface', 'amount', 'type' );
1428 for my $required_field (@required_fields) {
1429 my $this_payin = { %{$payin_params} };
1430 delete $this_payin->{$required_field};
1433 $account->payin_amount($this_payin);
1435 'Koha::Exceptions::MissingParameter',
1436 "Exception thrown if $required_field parameter missing";
1440 $account->payin_amount($payin_params);
1442 'Koha::Exceptions::Account::AmountNotPositive',
1443 'Expected validation exception thrown (amount not positive)';
1445 $payin_params->{amount} = 10;
1447 # Enable cash registers
1448 t::lib::Mocks::mock_preference( 'UseCashRegisters', 1 );
1450 $account->payin_amount($payin_params);
1452 'Koha::Exceptions::Account::RegisterRequired',
1453 'Exception thrown for UseCashRegisters:1 + payment_type:CASH + cash_register:undef';
1455 # Disable cash registers
1456 t::lib::Mocks::mock_preference( 'UseCashRegisters', 0 );
1458 # Enable AccountAutoReconcile
1459 t::lib::Mocks::mock_preference( 'AccountAutoReconcile', 1 );
1461 # Add some outstanding debits
1462 my $debit_1 = $account->add_debit( { amount => 2, interface => 'commandline', type => 'OVERDUE' } );
1463 my $debit_2 = $account->add_debit( { amount => 3, interface => 'commandline', type => 'OVERDUE' } );
1464 my $debit_3 = $account->add_debit( { amount => 5, interface => 'commandline', type => 'OVERDUE' } );
1465 my $debit_4 = $account->add_debit( { amount => 10, interface => 'commandline', type => 'OVERDUE' } );
1466 my $debits = $account->outstanding_debits();
1467 is( $debits->count, 4, "Found 4 debits with outstanding amounts" );
1468 is( $debits->total_outstanding + 0, 20, "Total 20 outstanding debit" );
1470 my $payin = $account->payin_amount($payin_params);
1471 is(ref($payin), 'Koha::Account::Line', 'Return the Koha::Account::Line object for the payin');
1472 is($payin->amount + 0, -10, "Payin amount recorded correctly");
1473 is($payin->amountoutstanding + 0, 0, "Full amount was used to pay debts");
1474 $debits = $account->outstanding_debits();
1475 is($debits->count, 1, "Payin was applied against oldest outstanding debits first");
1476 is($debits->total_outstanding + 0, 10, "Total of 10 outstanding debit remaining");
1478 my $offsets = Koha::Account::Offsets->search( { credit_id => $payin->id } );
1479 is( $offsets->count, 4, 'Four offsets generated' );
1480 my $offset = $offsets->next;
1481 is( $offset->type, 'CREATE', 'CREATE offset added for payin line' );
1482 is( $offset->amount * 1, 10, 'Correct offset amount recorded' );
1483 $offset = $offsets->next;
1484 is( $offset->debit_id, $debit_1->id, "Offset added against debit_1");
1485 is( $offset->type, 'APPLY', "APPLY used for offset_type" );
1486 is( $offset->amount * 1, -2, 'Correct amount offset against debit_1' );
1487 $offset = $offsets->next;
1488 is( $offset->debit_id, $debit_2->id, "Offset added against debit_2");
1489 is( $offset->type, 'APPLY', "APPLY used for offset_type" );
1490 is( $offset->amount * 1, -3, 'Correct amount offset against debit_2' );
1491 $offset = $offsets->next;
1492 is( $offset->debit_id, $debit_3->id, "Offset added against debit_3");
1493 is( $offset->type, 'APPLY', "APPLY used for offset_type" );
1494 is( $offset->amount * 1, -5, 'Correct amount offset against debit_3' );
1496 my $debit_5 = $account->add_debit( { amount => 5, interface => 'commandline', type => 'OVERDUE' } );
1497 $debits = $account->outstanding_debits();
1498 is($debits->count, 2, "New debit added");
1499 $payin_params->{amount} = 2.50;
1500 $payin_params->{debits} = [$debit_5];
1501 $payin = $account->payin_amount($payin_params);
1503 $debits = $account->outstanding_debits();
1504 is($debits->count, 2, "Second debit not fully paid off");
1505 is($debits->total_outstanding + 0, 12.50, "12.50 outstanding debit remaining");
1506 $debit_4->discard_changes;
1507 $debit_5->discard_changes;
1508 is($debit_4->amountoutstanding + 0, 10, "Debit 4 unaffected when debit_5 was passed to payin_amount");
1509 is($debit_5->amountoutstanding + 0, 2.50, "Debit 5 correctly reduced when payin_amount called with debit_5 passed");
1511 $offsets = Koha::Account::Offsets->search( { credit_id => $payin->id } );
1512 is( $offsets->count, 2, 'Two offsets generated' );
1513 $offset = $offsets->next;
1514 is( $offset->type, 'CREATE', 'CREATE offset added for payin line' );
1515 is( $offset->amount * 1, 2.50, 'Correct offset amount recorded' );
1516 $offset = $offsets->next;
1517 is( $offset->debit_id, $debit_5->id, "Offset added against debit_5");
1518 is( $offset->type, 'APPLY', "APPLY used for offset_type" );
1519 is( $offset->amount * 1, -2.50, 'Correct amount offset against debit_5' );
1521 $schema->storage->txn_rollback;