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 );
701 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
702 my $library = $builder->build_object({ class => 'Koha::Libraries' });
703 my $account = $patron->account;
705 my $context = Test::MockModule->new('C4::Context');
706 $context->mock( 'userenv', { branch => $library->id } );
708 my $credit_1_id = $account->pay({ amount => 200 })->{payment_id};
709 my $credit_1 = Koha::Account::Lines->find( $credit_1_id );
711 is( $credit_1->branchcode, undef, 'No branchcode is set if library_id was not passed' );
713 my $credit_2_id = $account->pay({ amount => 150, library_id => $library->id })->{payment_id};
714 my $credit_2 = Koha::Account::Lines->find( $credit_2_id );
716 is( $credit_2->branchcode, $library->id, 'branchcode set because library_id was passed' );
718 # Enable cash registers
719 t::lib::Mocks::mock_preference( 'UseCashRegisters', 1 );
724 payment_type => 'CASH',
725 interface => 'intranet'
729 'Koha::Exceptions::Account::RegisterRequired',
730 'Exception thrown for UseCashRegisters:1 + payment_type:CASH + cash_register:undef';
732 # Disable cash registers
733 t::lib::Mocks::mock_preference( 'UseCashRegisters', 0 );
736 $context->mock( 'userenv', undef );
737 my $result = $account->pay(
740 payment_Type => 'CASH',
741 interface => 'intranet'
744 ok($result, "Koha::Account->pay functions without a userenv");
745 my $payment = Koha::Account::Lines->find({accountlines_id => $result->{payment_id}});
746 is($payment->manager_id, undef, "manager_id left undefined when no userenv found");
748 subtest 'UseEmailReceipts tests' => sub {
752 t::lib::Mocks::mock_preference( 'UseEmailReceipts', 1 );
756 my $mocked_letters = Test::MockModule->new('C4::Letters');
757 # we want to test the params
758 $mocked_letters->mock( 'GetPreparedLetter', sub {
762 # we don't care about EnqueueLetter for now
763 $mocked_letters->mock( 'EnqueueLetter', sub {
767 $schema->storage->txn_begin;
769 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
770 my $account = $patron->account;
772 my $debit_1 = $account->add_debit( { amount => 5, interface => 'commandline', type => 'OVERDUE' } );
773 my $debit_2 = $account->add_debit( { amount => 10, interface => 'commandline', type => 'OVERDUE' } );
775 $account->pay({ amount => 6, lines => [ $debit_1, $debit_2 ] });
776 my @offsets = @{$params{substitute}{offsets}};
778 is( scalar @offsets, 2, 'Two offsets related to payment' );
779 is( ref($offsets[0]), 'Koha::Account::Offset', 'Type is correct' );
780 is( ref($offsets[1]), 'Koha::Account::Offset', 'Type is correct' );
781 is( $offsets[0]->type, 'APPLY', 'Only APPLY offsets are passed to the notice' );
782 is( $offsets[1]->type, 'APPLY', 'Only APPLY offsets are passed to the notice' );
784 $schema->storage->txn_rollback;
787 $schema->storage->txn_rollback;
790 subtest 'pay() handles lost items when paying a specific lost fee' => sub {
794 $schema->storage->txn_begin;
796 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
797 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
798 my $account = $patron->account;
800 my $context = Test::MockModule->new('C4::Context');
801 $context->mock( 'userenv', { branch => $library->id } );
803 my $biblio = $builder->build_sample_biblio();
805 $builder->build_sample_item( { biblionumber => $biblio->biblionumber } );
807 my $checkout = Koha::Checkout->new(
809 borrowernumber => $patron->id,
810 itemnumber => $item->id,
811 date_due => \'NOW()',
812 branchcode => $patron->branchcode,
813 issuedate => \'NOW()',
817 $item->itemlost('1')->store();
819 my $accountline = Koha::Account::Line->new(
821 issue_id => $checkout->id,
822 borrowernumber => $patron->id,
823 itemnumber => $item->id,
825 debit_type_code => 'LOST',
828 amountoutstanding => '1',
835 library_id => $library->id,
836 lines => [$accountline],
840 $accountline = Koha::Account::Lines->find( $accountline->id );
841 is( $accountline->amountoutstanding+0, .5, 'Account line was paid down by half' );
843 $checkout = Koha::Checkouts->find( $checkout->id );
844 ok( $checkout, 'Item still checked out to patron' );
849 library_id => $library->id,
850 lines => [$accountline],
854 $accountline = Koha::Account::Lines->find( $accountline->id );
855 is( $accountline->amountoutstanding+0, 0, 'Account line was paid down by half' );
857 $checkout = Koha::Checkouts->find( $checkout->id );
858 ok( !$checkout, 'Item was removed from patron account' );
860 subtest 'item was not checked out to the same patron' => sub {
863 my $patron_2 = $builder->build_object(
865 class => 'Koha::Patrons',
866 value => { branchcode => $library->branchcode }
869 $item->itemlost('1')->store();
870 C4::Accounts::chargelostitem( $patron->borrowernumber, $item->itemnumber, 5, "lost" );
871 my $accountline = Koha::Account::Lines->search(
873 borrowernumber => $patron->borrowernumber,
874 itemnumber => $item->itemnumber,
875 debit_type_code => 'LOST'
878 my $checkout = Koha::Checkout->new(
880 borrowernumber => $patron_2->borrowernumber,
881 itemnumber => $item->itemnumber,
882 date_due => \'NOW()',
883 branchcode => $patron_2->branchcode,
884 issuedate => \'NOW()',
888 $patron->account->pay(
891 library_id => $library->branchcode,
892 lines => [$accountline],
897 Koha::Checkouts->find( $checkout->issue_id ),
898 'If the item is checked out to another patron, a lost item should not be returned if lost fee is paid'
903 $schema->storage->txn_rollback;
906 subtest 'pay() handles lost items when paying by amount ( not specifying the lost fee )' => sub {
910 $schema->storage->txn_begin;
912 # Enable AccountAutoReconcile
913 t::lib::Mocks::mock_preference( 'AccountAutoReconcile', 1 );
915 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
916 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
917 my $account = $patron->account;
919 my $context = Test::MockModule->new('C4::Context');
920 $context->mock( 'userenv', { branch => $library->id } );
922 my $biblio = $builder->build_sample_biblio();
924 $builder->build_sample_item( { biblionumber => $biblio->biblionumber } );
926 my $checkout = Koha::Checkout->new(
928 borrowernumber => $patron->id,
929 itemnumber => $item->id,
930 date_due => \'NOW()',
931 branchcode => $patron->branchcode,
932 issuedate => \'NOW()',
936 $item->itemlost('1')->store();
938 my $accountline = Koha::Account::Line->new(
940 issue_id => $checkout->id,
941 borrowernumber => $patron->id,
942 itemnumber => $item->id,
944 debit_type_code => 'LOST',
947 amountoutstanding => '1',
954 library_id => $library->id,
958 $accountline = Koha::Account::Lines->find( $accountline->id );
959 is( $accountline->amountoutstanding+0, .5, 'Account line was paid down by half' );
961 $checkout = Koha::Checkouts->find( $checkout->id );
962 ok( $checkout, 'Item still checked out to patron' );
967 library_id => $library->id,
971 $accountline = Koha::Account::Lines->find( $accountline->id );
972 is( $accountline->amountoutstanding+0, 0, 'Account line was paid down by half' );
974 $checkout = Koha::Checkouts->find( $checkout->id );
975 ok( !$checkout, 'Item was removed from patron account' );
977 $schema->storage->txn_rollback;
980 subtest 'pay() renews items when appropriate' => sub {
984 $schema->storage->txn_begin;
986 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
987 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
988 my $account = $patron->account;
990 my $context = Test::MockModule->new('C4::Context');
991 $context->mock( 'userenv', { branch => $library->id } );
993 my $biblio = $builder->build_sample_biblio();
995 $builder->build_sample_item( { biblionumber => $biblio->biblionumber } );
997 my $now = dt_from_string();
998 my $seven_weeks = DateTime::Duration->new(weeks => 7);
999 my $five_weeks = DateTime::Duration->new(weeks => 5);
1000 my $seven_weeks_ago = $now - $seven_weeks;
1001 my $five_weeks_ago = $now - $five_weeks;
1003 my $checkout = Koha::Checkout->new(
1005 borrowernumber => $patron->id,
1006 itemnumber => $item->id,
1007 date_due => $five_weeks_ago,
1008 branchcode => $patron->branchcode,
1009 issuedate => $seven_weeks_ago
1013 my $accountline = Koha::Account::Line->new(
1015 issue_id => $checkout->id,
1016 borrowernumber => $patron->id,
1017 itemnumber => $item->id,
1019 debit_type_code => 'OVERDUE',
1020 status => 'UNRETURNED',
1023 amountoutstanding => '1',
1027 # Enable renewing upon fine payment
1028 t::lib::Mocks::mock_preference( 'RenewAccruingItemWhenPaid', 1 );
1030 my $module = Test::MockModule->new('C4::Circulation');
1031 $module->mock('AddRenewal', sub { $called = 1; });
1032 $module->mock('CanBookBeRenewed', sub { return 1; });
1033 my $result = $account->pay(
1036 library_id => $library->id,
1040 is( $called, 1, 'RenewAccruingItemWhenPaid causes C4::Circulation::AddRenew to be called when appropriate' );
1041 is(ref($result->{renew_result}), 'ARRAY', "Pay result contains 'renew_result' ARRAY" );
1042 is( scalar @{$result->{renew_result}}, 1, "renew_result contains one renewal result" );
1043 is( $result->{renew_result}->[0]->{itemnumber}, $item->id, "renew_result contains itemnumber of renewed item" );
1045 # Reset test by adding a new overdue
1046 Koha::Account::Line->new(
1048 issue_id => $checkout->id,
1049 borrowernumber => $patron->id,
1050 itemnumber => $item->id,
1052 debit_type_code => 'OVERDUE',
1053 status => 'UNRETURNED',
1056 amountoutstanding => '1',
1061 t::lib::Mocks::mock_preference( 'RenewAccruingItemWhenPaid', 0 );
1062 $result = $account->pay(
1065 library_id => $library->id,
1069 is( $called, 0, 'C4::Circulation::AddRenew NOT called when RenewAccruingItemWhenPaid disabled' );
1070 is(ref($result->{renew_result}), 'ARRAY', "Pay result contains 'renew_result' ARRAY" );
1071 is( scalar @{$result->{renew_result}}, 0, "renew_result contains no renewal results" );
1073 $schema->storage->txn_rollback;
1076 subtest 'Koha::Account::Line::apply() handles lost items' => sub {
1080 $schema->storage->txn_begin;
1082 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
1083 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
1084 my $account = $patron->account;
1086 my $context = Test::MockModule->new('C4::Context');
1087 $context->mock( 'userenv', { branch => $library->id } );
1089 my $biblio = $builder->build_sample_biblio();
1091 $builder->build_sample_item( { biblionumber => $biblio->biblionumber } );
1093 my $checkout = Koha::Checkout->new(
1095 borrowernumber => $patron->id,
1096 itemnumber => $item->id,
1097 date_due => \'NOW()',
1098 branchcode => $patron->branchcode,
1099 issuedate => \'NOW()',
1103 $item->itemlost('1')->store();
1105 my $debit = Koha::Account::Line->new(
1107 issue_id => $checkout->id,
1108 borrowernumber => $patron->id,
1109 itemnumber => $item->id,
1111 debit_type_code => 'LOST',
1114 amountoutstanding => '1',
1118 my $credit = Koha::Account::Line->new(
1120 borrowernumber => $patron->id,
1121 date => '1970-01-01 00:00:01',
1123 amountoutstanding => -.5,
1124 interface => 'commandline',
1125 credit_type_code => 'PAYMENT'
1128 my $debits = $account->outstanding_debits;
1129 $credit->apply({ debits => [ $debits->as_list ] });
1131 $debit = Koha::Account::Lines->find( $debit->id );
1132 is( $debit->amountoutstanding+0, .5, 'Account line was paid down by half' );
1134 $checkout = Koha::Checkouts->find( $checkout->id );
1135 ok( $checkout, 'Item still checked out to patron' );
1137 $credit = Koha::Account::Line->new(
1139 borrowernumber => $patron->id,
1140 date => '1970-01-01 00:00:01',
1142 amountoutstanding => -.5,
1143 interface => 'commandline',
1144 credit_type_code => 'PAYMENT'
1147 $debits = $account->outstanding_debits;
1148 $credit->apply({ debits => [ $debits->as_list ] });
1150 $debit = Koha::Account::Lines->find( $debit->id );
1151 is( $debit->amountoutstanding+0, 0, 'Account line was paid down by half' );
1153 $checkout = Koha::Checkouts->find( $checkout->id );
1154 ok( !$checkout, 'Item was removed from patron account' );
1156 $schema->storage->txn_rollback;
1159 subtest 'Koha::Account::pay() generates credit number (Koha::Account::Line->store)' => sub {
1162 $schema->storage->txn_begin;
1164 Koha::Account::Lines->delete();
1166 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
1167 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
1168 my $account = $patron->account;
1170 #t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode });
1171 my $context = Test::MockModule->new('C4::Context');
1172 $context->mock( 'userenv', { branch => $library->id } );
1174 my $now = dt_from_string;
1175 my $year = $now->year;
1176 my $month = $now->month;
1177 my ($accountlines_id, $accountline);
1179 my $credit_type = Koha::Account::CreditTypes->find('PAYMENT');
1180 $credit_type->credit_number_enabled(1);
1181 $credit_type->store();
1183 t::lib::Mocks::mock_preference('AutoCreditNumber', '');
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, undef, 'No credit number is generated when syspref is off');
1188 t::lib::Mocks::mock_preference('AutoCreditNumber', 'incremental');
1190 $accountlines_id = $account->pay({ amount => '1.00', library_id => $library->id })->{payment_id};
1191 $accountline = Koha::Account::Lines->find($accountlines_id);
1192 is($accountline->credit_number, $i, "Incremental format credit number added for payments: $i");
1194 $accountlines_id = $account->pay({ type => 'WRITEOFF', amount => '1.00', library_id => $library->id })->{payment_id};
1195 $accountline = Koha::Account::Lines->find($accountlines_id);
1196 is($accountline->credit_number, undef, "Incremental credit number not added for writeoff");
1198 t::lib::Mocks::mock_preference('AutoCreditNumber', 'annual');
1200 $accountlines_id = $account->pay({ amount => '1.00', library_id => $library->id })->{payment_id};
1201 $accountline = Koha::Account::Lines->find($accountlines_id);
1202 is($accountline->credit_number, sprintf('%s-%04d', $year, $i), "Annual format credit number added for payments: " . sprintf('%s-%04d', $year, $i));
1204 $accountlines_id = $account->pay({ type => 'WRITEOFF', amount => '1.00', library_id => $library->id })->{payment_id};
1205 $accountline = Koha::Account::Lines->find($accountlines_id);
1206 is($accountline->credit_number, undef, "Annual format credit number not aded for writeoff");
1208 t::lib::Mocks::mock_preference('AutoCreditNumber', 'branchyyyymmincr');
1210 $accountlines_id = $account->pay({ amount => '1.00', library_id => $library->id })->{payment_id};
1211 $accountline = Koha::Account::Lines->find($accountlines_id);
1212 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));
1214 $accountlines_id = $account->pay({ type => 'WRITEOFF', amount => '1.00', library_id => $library->id })->{payment_id};
1215 $accountline = Koha::Account::Lines->find($accountlines_id);
1216 is($accountline->credit_number, undef, "branchyyyymmincr format credit number not added for writeoff");
1219 Koha::Account::Line->new(
1221 interface => 'test',
1223 credit_type_code => $credit_type->code,
1228 'Koha::Exceptions::Account',
1229 "Exception thrown when AutoCreditNumber is enabled but credit_number is already defined";
1231 $schema->storage->txn_rollback;
1234 subtest 'Koha::Account::payout_amount() tests' => sub {
1237 $schema->storage->txn_begin;
1239 # delete logs and statistics
1240 my $action_logs = $schema->resultset('ActionLog')->search()->count;
1241 my $statistics = $schema->resultset('Statistic')->search()->count;
1243 my $staff = $builder->build_object( { class => 'Koha::Patrons' } );
1244 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
1246 $builder->build_object( { class => 'Koha::Cash::Registers' } );
1247 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
1249 Koha::Account->new( { patron_id => $patron->borrowernumber } );
1251 is( $account->balance, 0, 'Test patron has no balance' );
1253 my $payout_params = {
1254 payout_type => 'CASH',
1255 branch => $library->id,
1256 register_id => $register->id,
1257 staff_id => $staff->id,
1258 interface => 'intranet',
1262 my @required_fields =
1263 ( 'interface', 'staff_id', 'branch', 'payout_type', 'amount' );
1264 for my $required_field (@required_fields) {
1265 my $this_payout = { %{$payout_params} };
1266 delete $this_payout->{$required_field};
1269 $account->payout_amount($this_payout);
1271 'Koha::Exceptions::MissingParameter',
1272 "Exception thrown if $required_field parameter missing";
1276 $account->payout_amount($payout_params);
1278 'Koha::Exceptions::Account::AmountNotPositive',
1279 'Expected validation exception thrown (amount not positive)';
1281 $payout_params->{amount} = 10;
1283 $account->payout_amount($payout_params);
1285 'Koha::Exceptions::ParameterTooHigh',
1286 'Expected validation exception thrown (amount greater than outstanding)';
1288 # Enable cash registers
1289 t::lib::Mocks::mock_preference( 'UseCashRegisters', 1 );
1291 $account->payout_amount($payout_params);
1293 'Koha::Exceptions::Account::RegisterRequired',
1294 'Exception thrown for UseCashRegisters:1 + payout_type:CASH + cash_register:undef';
1296 # Disable cash registers
1297 t::lib::Mocks::mock_preference( 'UseCashRegisters', 0 );
1299 # Add some outstanding credits
1300 my $credit_1 = $account->add_credit( { amount => 2, interface => 'commandline' } );
1301 my $credit_2 = $account->add_credit( { amount => 3, interface => 'commandline' } );
1302 my $credit_3 = $account->add_credit( { amount => 5, interface => 'commandline' } );
1303 my $credit_4 = $account->add_credit( { amount => 10, interface => 'commandline' } );
1304 my $credits = $account->outstanding_credits();
1305 is( $credits->count, 4, "Found 4 credits with outstanding amounts" );
1306 is( $credits->total_outstanding + 0, -20, "Total -20 outstanding credit" );
1308 my $payout = $account->payout_amount($payout_params);
1309 is(ref($payout), 'Koha::Account::Line', 'Return the Koha::Account::Line object for the payout');
1310 is($payout->amount + 0, 10, "Payout amount recorded correctly");
1311 is($payout->amountoutstanding + 0, 0, "Full amount was paid out");
1312 $credits = $account->outstanding_credits();
1313 is($credits->count, 1, "Payout was applied against oldest outstanding credits first");
1314 is($credits->total_outstanding + 0, -10, "Total of 10 outstanding credit remaining");
1316 my $offsets = Koha::Account::Offsets->search( { debit_id => $payout->id } );
1317 is( $offsets->count, 4, 'Four offsets generated' );
1318 my $offset = $offsets->next;
1319 is( $offset->type, 'CREATE', 'CREATE offset added for payout line' );
1320 is( $offset->amount * 1, 10, 'Correct offset amount recorded' );
1321 $offset = $offsets->next;
1322 is( $offset->credit_id, $credit_1->id, "Offset added against credit_1");
1323 is( $offset->type, 'APPLY', "APPLY used for offset_type" );
1324 is( $offset->amount * 1, -2, 'Correct amount offset against credit_1' );
1325 $offset = $offsets->next;
1326 is( $offset->credit_id, $credit_2->id, "Offset added against credit_2");
1327 is( $offset->type, 'APPLY', "APPLY used for offset_type" );
1328 is( $offset->amount * 1, -3, 'Correct amount offset against credit_2' );
1329 $offset = $offsets->next;
1330 is( $offset->credit_id, $credit_3->id, "Offset added against credit_3");
1331 is( $offset->type, 'APPLY', "APPLY used for offset_type" );
1332 is( $offset->amount * 1, -5, 'Correct amount offset against credit_3' );
1334 my $credit_5 = $account->add_credit( { amount => 5, interface => 'commandline' } );
1335 $credits = $account->outstanding_credits();
1336 is($credits->count, 2, "New credit added");
1337 $payout_params->{amount} = 2.50;
1338 $payout_params->{credits} = [$credit_5];
1339 $payout = $account->payout_amount($payout_params);
1341 $credits = $account->outstanding_credits();
1342 is($credits->count, 2, "Second credit not fully paid off");
1343 is($credits->total_outstanding + 0, -12.50, "12.50 outstanding credit remaining");
1344 $credit_4->discard_changes;
1345 $credit_5->discard_changes;
1346 is($credit_4->amountoutstanding + 0, -10, "Credit 4 unaffected when credit_5 was passed to payout_amount");
1347 is($credit_5->amountoutstanding + 0, -2.50, "Credit 5 correctly reduced when payout_amount called with credit_5 passed");
1349 $offsets = Koha::Account::Offsets->search( { debit_id => $payout->id } );
1350 is( $offsets->count, 2, 'Two offsets generated' );
1351 $offset = $offsets->next;
1352 is( $offset->type, 'CREATE', 'CREATE offset added for payout line' );
1353 is( $offset->amount * 1, 2.50, 'Correct offset amount recorded' );
1354 $offset = $offsets->next;
1355 is( $offset->credit_id, $credit_5->id, "Offset added against credit_5");
1356 is( $offset->type, 'APPLY', "APPLY used for offset_type" );
1357 is( $offset->amount * 1, -2.50, 'Correct amount offset against credit_5' );
1359 $schema->storage->txn_rollback;
1362 subtest 'Koha::Account::payin_amount() tests' => sub {
1365 $schema->storage->txn_begin;
1367 # delete logs and statistics
1368 my $action_logs = $schema->resultset('ActionLog')->search()->count;
1369 my $statistics = $schema->resultset('Statistic')->search()->count;
1371 my $staff = $builder->build_object( { class => 'Koha::Patrons' } );
1372 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
1374 $builder->build_object( { class => 'Koha::Cash::Registers' } );
1375 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
1377 Koha::Account->new( { patron_id => $patron->borrowernumber } );
1379 is( $account->balance, 0, 'Test patron has no balance' );
1381 my $payin_params = {
1383 payment_type => 'CASH',
1384 branch => $library->id,
1385 register_id => $register->id,
1386 staff_id => $staff->id,
1387 interface => 'intranet',
1391 my @required_fields =
1392 ( 'interface', 'amount', 'type' );
1393 for my $required_field (@required_fields) {
1394 my $this_payin = { %{$payin_params} };
1395 delete $this_payin->{$required_field};
1398 $account->payin_amount($this_payin);
1400 'Koha::Exceptions::MissingParameter',
1401 "Exception thrown if $required_field parameter missing";
1405 $account->payin_amount($payin_params);
1407 'Koha::Exceptions::Account::AmountNotPositive',
1408 'Expected validation exception thrown (amount not positive)';
1410 $payin_params->{amount} = 10;
1412 # Enable cash registers
1413 t::lib::Mocks::mock_preference( 'UseCashRegisters', 1 );
1415 $account->payin_amount($payin_params);
1417 'Koha::Exceptions::Account::RegisterRequired',
1418 'Exception thrown for UseCashRegisters:1 + payment_type:CASH + cash_register:undef';
1420 # Disable cash registers
1421 t::lib::Mocks::mock_preference( 'UseCashRegisters', 0 );
1423 # Enable AccountAutoReconcile
1424 t::lib::Mocks::mock_preference( 'AccountAutoReconcile', 1 );
1426 # Add some outstanding debits
1427 my $debit_1 = $account->add_debit( { amount => 2, interface => 'commandline', type => 'OVERDUE' } );
1428 my $debit_2 = $account->add_debit( { amount => 3, interface => 'commandline', type => 'OVERDUE' } );
1429 my $debit_3 = $account->add_debit( { amount => 5, interface => 'commandline', type => 'OVERDUE' } );
1430 my $debit_4 = $account->add_debit( { amount => 10, interface => 'commandline', type => 'OVERDUE' } );
1431 my $debits = $account->outstanding_debits();
1432 is( $debits->count, 4, "Found 4 debits with outstanding amounts" );
1433 is( $debits->total_outstanding + 0, 20, "Total 20 outstanding debit" );
1435 my $payin = $account->payin_amount($payin_params);
1436 is(ref($payin), 'Koha::Account::Line', 'Return the Koha::Account::Line object for the payin');
1437 is($payin->amount + 0, -10, "Payin amount recorded correctly");
1438 is($payin->amountoutstanding + 0, 0, "Full amount was used to pay debts");
1439 $debits = $account->outstanding_debits();
1440 is($debits->count, 1, "Payin was applied against oldest outstanding debits first");
1441 is($debits->total_outstanding + 0, 10, "Total of 10 outstanding debit remaining");
1443 my $offsets = Koha::Account::Offsets->search( { credit_id => $payin->id } );
1444 is( $offsets->count, 4, 'Four offsets generated' );
1445 my $offset = $offsets->next;
1446 is( $offset->type, 'CREATE', 'CREATE offset added for payin line' );
1447 is( $offset->amount * 1, 10, 'Correct offset amount recorded' );
1448 $offset = $offsets->next;
1449 is( $offset->debit_id, $debit_1->id, "Offset added against debit_1");
1450 is( $offset->type, 'APPLY', "APPLY used for offset_type" );
1451 is( $offset->amount * 1, -2, 'Correct amount offset against debit_1' );
1452 $offset = $offsets->next;
1453 is( $offset->debit_id, $debit_2->id, "Offset added against debit_2");
1454 is( $offset->type, 'APPLY', "APPLY used for offset_type" );
1455 is( $offset->amount * 1, -3, 'Correct amount offset against debit_2' );
1456 $offset = $offsets->next;
1457 is( $offset->debit_id, $debit_3->id, "Offset added against debit_3");
1458 is( $offset->type, 'APPLY', "APPLY used for offset_type" );
1459 is( $offset->amount * 1, -5, 'Correct amount offset against debit_3' );
1461 my $debit_5 = $account->add_debit( { amount => 5, interface => 'commandline', type => 'OVERDUE' } );
1462 $debits = $account->outstanding_debits();
1463 is($debits->count, 2, "New debit added");
1464 $payin_params->{amount} = 2.50;
1465 $payin_params->{debits} = [$debit_5];
1466 $payin = $account->payin_amount($payin_params);
1468 $debits = $account->outstanding_debits();
1469 is($debits->count, 2, "Second debit not fully paid off");
1470 is($debits->total_outstanding + 0, 12.50, "12.50 outstanding debit remaining");
1471 $debit_4->discard_changes;
1472 $debit_5->discard_changes;
1473 is($debit_4->amountoutstanding + 0, 10, "Debit 4 unaffected when debit_5 was passed to payin_amount");
1474 is($debit_5->amountoutstanding + 0, 2.50, "Debit 5 correctly reduced when payin_amount called with debit_5 passed");
1476 $offsets = Koha::Account::Offsets->search( { credit_id => $payin->id } );
1477 is( $offsets->count, 2, 'Two offsets generated' );
1478 $offset = $offsets->next;
1479 is( $offset->type, 'CREATE', 'CREATE offset added for payin line' );
1480 is( $offset->amount * 1, 2.50, 'Correct offset amount recorded' );
1481 $offset = $offsets->next;
1482 is( $offset->debit_id, $debit_5->id, "Offset added against debit_5");
1483 is( $offset->type, 'APPLY', "APPLY used for offset_type" );
1484 is( $offset->amount * 1, -2.50, 'Correct amount offset against debit_5' );
1486 $schema->storage->txn_rollback;