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();
73 my @lines_arr = $account->outstanding_debits();
75 is( ref($lines), 'Koha::Account::Lines', 'Called in scalar context, outstanding_debits returns a Koha::Account::Lines object' );
76 is( $lines->total_outstanding, 10, 'Outstandig debits total is correctly calculated' );
79 foreach my $line ( @{ $lines->as_list } ) {
80 my $fetched_line = Koha::Account::Lines->find( $generated_lines[$i]->id );
81 is_deeply( $line->unblessed, $fetched_line->unblessed, "Fetched line matches the generated one ($i)" );
82 is_deeply( $lines_arr[$i]->unblessed, $fetched_line->unblessed, "Fetched line matches the generated one ($i)" );
83 is( ref($lines_arr[$i]), 'Koha::Account::Line', 'outstanding_debits returns a list of Koha::Account::Line objects in list context' );
86 my $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
87 Koha::Account::Line->new(
89 borrowernumber => $patron_2->id,
90 amountoutstanding => -2,
91 interface => 'commandline',
92 credit_type_code => 'PAYMENT'
95 my $just_one = Koha::Account::Line->new(
97 borrowernumber => $patron_2->id,
99 amountoutstanding => 3,
100 interface => 'commandline',
101 debit_type_code => 'OVERDUE'
104 Koha::Account::Line->new(
106 borrowernumber => $patron_2->id,
108 amountoutstanding => -6,
109 interface => 'commandline',
110 credit_type_code => 'PAYMENT'
113 $lines = $patron_2->account->outstanding_debits();
114 is( $lines->total_outstanding, 3, "Total if some outstanding debits and some credits is only debits" );
115 is( $lines->count, 1, "With 1 outstanding debits, we get back a Lines object with 1 lines" );
116 my $the_line = Koha::Account::Lines->find( $just_one->id );
117 is_deeply( $the_line->unblessed, $lines->next->unblessed, "We get back the one correct line");
119 my $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
120 my $account_3 = $patron_3->account;
121 $account_3->add_credit( { amount => 2, interface => 'commandline' } );
122 $account_3->add_credit( { amount => 20, interface => 'commandline' } );
123 $account_3->add_credit( { amount => 200, interface => 'commandline' } );
124 $lines = $account_3->outstanding_debits();
125 is( $lines->total_outstanding, 0, "Total if no outstanding debits total is 0" );
126 is( $lines->count, 0, "With 0 outstanding debits, we get back a Lines object with 0 lines" );
128 my $patron_4 = $builder->build_object({ class => 'Koha::Patrons' });
129 my $account_4 = $patron_4->account;
130 $lines = $account_4->outstanding_debits();
131 is( $lines->total_outstanding, 0, "Total if no outstanding debits is 0" );
132 is( $lines->count, 0, "With no outstanding debits, we get back a Lines object with 0 lines" );
134 # create a pathological credit with amountoutstanding > 0 (BZ 14591)
135 Koha::Account::Line->new(
137 borrowernumber => $patron_4->id,
139 amountoutstanding => 3,
140 interface => 'commandline',
141 credit_type_code => 'PAYMENT'
144 $lines = $account_4->outstanding_debits();
145 is( $lines->count, 0, 'No credits are confused with debits because of the amountoutstanding value' );
147 $schema->storage->txn_rollback;
150 subtest 'outstanding_credits() tests' => sub {
154 $schema->storage->txn_begin;
156 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
157 my $account = $patron->account;
160 push @generated_lines, $account->add_credit({ amount => 1, interface => 'commandline' });
161 push @generated_lines, $account->add_credit({ amount => 2, interface => 'commandline' });
162 push @generated_lines, $account->add_credit({ amount => 3, interface => 'commandline' });
163 push @generated_lines, $account->add_credit({ amount => 4, interface => 'commandline' });
165 my $lines = $account->outstanding_credits();
166 my @lines_arr = $account->outstanding_credits();
168 is( ref($lines), 'Koha::Account::Lines', 'Called in scalar context, outstanding_credits returns a Koha::Account::Lines object' );
169 is( $lines->total_outstanding, -10, 'Outstandig credits total is correctly calculated' );
172 foreach my $line ( @{ $lines->as_list } ) {
173 my $fetched_line = Koha::Account::Lines->find( $generated_lines[$i]->id );
174 is_deeply( $line->unblessed, $fetched_line->unblessed, "Fetched line matches the generated one ($i)" );
175 is_deeply( $lines_arr[$i]->unblessed, $fetched_line->unblessed, "Fetched line matches the generated one ($i)" );
176 is( ref($lines_arr[$i]), 'Koha::Account::Line', 'outstanding_debits returns a list of Koha::Account::Line objects in list context' );
180 my $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
181 $account = $patron_2->account;
182 $lines = $account->outstanding_credits();
183 is( $lines->total_outstanding, 0, "Total if no outstanding credits is 0" );
184 is( $lines->count, 0, "With no outstanding credits, we get back a Lines object with 0 lines" );
186 # create a pathological debit with amountoutstanding < 0 (BZ 14591)
187 Koha::Account::Line->new(
189 borrowernumber => $patron_2->id,
191 amountoutstanding => -3,
192 interface => 'commandline',
193 debit_type_code => 'OVERDUE'
196 $lines = $account->outstanding_credits();
197 is( $lines->count, 0, 'No debits are confused with credits because of the amountoutstanding value' );
199 $schema->storage->txn_rollback;
202 subtest 'add_credit() tests' => sub {
206 $schema->storage->txn_begin;
208 # delete logs and statistics
209 my $action_logs = $schema->resultset('ActionLog')->search()->count;
210 my $statistics = $schema->resultset('Statistic')->search()->count;
212 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
213 my $account = Koha::Account->new( { patron_id => $patron->borrowernumber } );
215 is( $account->balance, 0, 'Patron has no balance' );
218 t::lib::Mocks::mock_preference( 'FinesLog', 0 );
221 $account->add_credit(
224 description => 'Payment of 25',
225 library_id => $patron->branchcode,
226 note => 'not really important',
228 user_id => $patron->id
232 'Koha::Exceptions::MissingParameter', 'Exception thrown if interface parameter missing';
234 my $line_1 = $account->add_credit(
237 description => 'Payment of 25',
238 library_id => $patron->branchcode,
239 note => 'not really important',
241 user_id => $patron->id,
242 interface => 'commandline'
246 is( $account->balance, -25, 'Patron has a balance of -25' );
247 is( $schema->resultset('ActionLog')->count(), $action_logs + 0, 'No log was added' );
248 is( $schema->resultset('Statistic')->count(), $statistics + 1, 'Action added to statistics' );
249 is( $line_1->credit_type_code, 'PAYMENT', 'Account type is correctly set' );
250 ok( $line_1->amount < 0, 'Credit amount is stored as a negative' );
253 t::lib::Mocks::mock_preference( 'FinesLog', 1 );
255 my $line_2 = $account->add_credit(
257 description => 'Payment of 37',
258 library_id => $patron->branchcode,
259 note => 'not really important',
260 user_id => $patron->id,
261 interface => 'commandline'
265 is( $account->balance, -62, 'Patron has a balance of -25' );
266 is( $schema->resultset('ActionLog')->count(), $action_logs + 1, 'Log was added' );
267 is( $schema->resultset('Statistic')->count(), $statistics + 2, 'Action added to statistics' );
268 is( $line_2->credit_type_code, 'PAYMENT', 'Account type is correctly set' );
269 ok( $line_1->amount < 0, 'Credit amount is stored as a negative' );
271 # offsets have the credit_id set to accountlines_id, and debit_id is undef
272 my $offset_1 = Koha::Account::Offsets->search({ credit_id => $line_1->id })->next;
273 my $offset_2 = Koha::Account::Offsets->search({ credit_id => $line_2->id })->next;
275 is( $offset_1->credit_id, $line_1->id, 'No debit_id is set for credits' );
276 is( $offset_1->debit_id, undef, 'No debit_id is set for credits' );
277 ok( $offset_1->amount > 0, 'Credit creation offset is a positive' );
278 is( $offset_2->credit_id, $line_2->id, 'No debit_id is set for credits' );
279 is( $offset_2->debit_id, undef, 'No debit_id is set for credits' );
280 ok( $offset_2->amount > 0, 'Credit creation offset is a positive' );
282 my $line_3 = $account->add_credit(
285 description => 'Manual credit applied',
286 library_id => $patron->branchcode,
287 user_id => $patron->id,
289 interface => 'commandline'
293 is( $schema->resultset('ActionLog')->count(), $action_logs + 2, 'Log was added' );
294 is( $schema->resultset('Statistic')->count(), $statistics + 2, 'No action added to statistics, because of credit type' );
296 # Enable cash registers
297 t::lib::Mocks::mock_preference( 'UseCashRegisters', 1 );
299 $account->add_credit(
302 description => 'Cash payment without cash register',
303 library_id => $patron->branchcode,
304 user_id => $patron->id,
305 payment_type => 'CASH',
306 interface => 'intranet'
310 'Koha::Exceptions::Account::RegisterRequired',
311 'Exception thrown for UseCashRegisters:1 + payment_type:CASH + cash_register:undef';
313 # Disable cash registers
314 t::lib::Mocks::mock_preference( 'UseCashRegisters', 0 );
316 $schema->storage->txn_rollback;
319 subtest 'add_debit() tests' => sub {
323 $schema->storage->txn_begin;
325 # delete logs and statistics
326 my $action_logs = $schema->resultset('ActionLog')->search()->count;
327 my $statistics = $schema->resultset('Statistic')->search()->count;
329 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
331 Koha::Account->new( { patron_id => $patron->borrowernumber } );
333 is( $account->balance, 0, 'Patron has no balance' );
339 description => 'amount validation failure',
340 library_id => $patron->branchcode,
341 note => 'this should fail anyway',
343 user_id => $patron->id,
344 interface => 'commandline'
346 ); } 'Koha::Exceptions::Account::AmountNotPositive', 'Expected validation exception thrown (amount)';
350 open STDERR, '>', '/dev/null';
354 description => 'type validation failure',
355 library_id => $patron->branchcode,
356 note => 'this should fail anyway',
358 user_id => $patron->id,
359 interface => 'commandline'
364 'Koha::Exceptions::Account::UnrecognisedType',
365 'Expected validation exception thrown (type)';
371 description => 'Rental charge of 25',
372 library_id => $patron->branchcode,
373 note => 'not really important',
375 user_id => $patron->id
377 ); } 'Koha::Exceptions::MissingParameter', 'Exception thrown if interface parameter missing';
380 t::lib::Mocks::mock_preference( 'FinesLog', 0 );
382 my $line_1 = $account->add_debit(
385 description => 'Rental charge of 25',
386 library_id => $patron->branchcode,
387 note => 'not really important',
389 user_id => $patron->id,
390 interface => 'commandline'
394 is( $account->balance, 25, 'Patron has a balance of 25' );
396 $schema->resultset('ActionLog')->count(),
401 $line_1->debit_type_code,
403 'Account type is correctly set'
407 t::lib::Mocks::mock_preference( 'FinesLog', 1 );
409 my $line_2 = $account->add_debit(
412 description => 'Rental charge of 37',
413 library_id => $patron->branchcode,
414 note => 'not really important',
416 user_id => $patron->id,
417 interface => 'commandline'
421 is( $account->balance, 62, 'Patron has a balance of 62' );
423 $schema->resultset('ActionLog')->count(),
428 $line_2->debit_type_code,
430 'Account type is correctly set'
433 # offsets have the debit_id set to accountlines_id, and credit_id is undef
435 Koha::Account::Offsets->search( { debit_id => $line_1->id } )->next;
437 Koha::Account::Offsets->search( { debit_id => $line_2->id } )->next;
439 is( $offset_1->debit_id, $line_1->id, 'debit_id is set for debit 1' );
440 is( $offset_1->credit_id, undef, 'credit_id is not set for debit 1' );
441 is( $offset_2->debit_id, $line_2->id, 'debit_id is set for debit 2' );
442 is( $offset_2->credit_id, undef, 'credit_id is not set for debit 2' );
444 $schema->storage->txn_rollback;
447 subtest 'lines() tests' => sub {
451 $schema->storage->txn_begin;
453 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
454 my $account = $patron->account;
457 $account->add_credit({ amount => 1, interface => 'commandline' });
458 $account->add_credit({ amount => 2, interface => 'commandline' });
459 $account->add_credit({ amount => 3, interface => 'commandline' });
460 $account->add_credit({ amount => 4, interface => 'commandline' });
463 $account->add_debit({ amount => 1, interface => 'commandline', type => 'OVERDUE' });
464 $account->add_debit({ amount => 2, interface => 'commandline', type => 'OVERDUE' });
465 $account->add_debit({ amount => 3, interface => 'commandline', type => 'OVERDUE' });
466 $account->add_debit({ amount => 4, interface => 'commandline', type => 'OVERDUE' });
469 $account->add_credit( { amount => 1, interface => 'commandline' } )
470 ->apply( { debits => [ $account->outstanding_debits->as_list ] } );
472 my $lines = $account->lines;
473 is( $lines->_resultset->count, 9, "All accountlines (debits, credits and paid off) were fetched");
475 $schema->storage->txn_rollback;
478 subtest 'reconcile_balance' => sub {
482 subtest 'more credit than debit' => sub {
486 $schema->storage->txn_begin;
488 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
489 my $account = $patron->account;
492 $account->add_credit({ amount => 1, interface => 'commandline' });
493 $account->add_credit({ amount => 2, interface => 'commandline' });
494 $account->add_credit({ amount => 3, interface => 'commandline' });
495 $account->add_credit({ amount => 4, interface => 'commandline' });
496 $account->add_credit({ amount => 5, interface => 'commandline' });
499 $account->add_debit({ amount => 1, interface => 'commandline', type => 'OVERDUE' });
500 $account->add_debit({ amount => 2, interface => 'commandline', type => 'OVERDUE' });
501 $account->add_debit({ amount => 3, interface => 'commandline', type => 'OVERDUE' });
502 $account->add_debit({ amount => 4, interface => 'commandline', type => 'OVERDUE' });
505 Koha::Account::Line->new(
507 borrowernumber => $patron->id,
509 amountoutstanding => 0,
510 interface => 'commandline',
511 debit_type_code => 'OVERDUE'
514 Koha::Account::Line->new(
516 borrowernumber => $patron->id,
518 amountoutstanding => 0,
519 interface => 'commandline',
520 debit_type_code => 'OVERDUE'
524 is( $account->balance(), -5, "Account balance is -5" );
525 is( $account->outstanding_debits->total_outstanding, 10, 'Outstanding debits sum 10' );
526 is( $account->outstanding_credits->total_outstanding, -15, 'Outstanding credits sum -15' );
528 $account->reconcile_balance();
530 is( $account->balance(), -5, "Account balance is -5" );
531 is( $account->outstanding_debits->total_outstanding, 0, 'No outstanding debits' );
532 is( $account->outstanding_credits->total_outstanding, -5, 'Outstanding credits sum -5' );
534 $schema->storage->txn_rollback;
537 subtest 'same debit as credit' => sub {
541 $schema->storage->txn_begin;
543 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
544 my $account = $patron->account;
547 $account->add_credit({ amount => 1, interface => 'commandline' });
548 $account->add_credit({ amount => 2, interface => 'commandline' });
549 $account->add_credit({ amount => 3, interface => 'commandline' });
550 $account->add_credit({ amount => 4, interface => 'commandline' });
553 $account->add_debit({ amount => 1, interface => 'commandline', type => 'OVERDUE' });
554 $account->add_debit({ amount => 2, interface => 'commandline', type => 'OVERDUE' });
555 $account->add_debit({ amount => 3, interface => 'commandline', type => 'OVERDUE' });
556 $account->add_debit({ amount => 4, interface => 'commandline', type => 'OVERDUE' });
559 Koha::Account::Line->new(
561 borrowernumber => $patron->id,
563 amountoutstanding => 0,
564 interface => 'commandline',
565 debit_type_code => 'OVERDUE'
568 Koha::Account::Line->new(
570 borrowernumber => $patron->id,
572 amountoutstanding => 0,
573 interface => 'commandline',
574 debit_type_code => 'OVERDUE'
578 is( $account->balance(), 0, "Account balance is 0" );
579 is( $account->outstanding_debits->total_outstanding, 10, 'Outstanding debits sum 10' );
580 is( $account->outstanding_credits->total_outstanding, -10, 'Outstanding credits sum -10' );
582 $account->reconcile_balance();
584 is( $account->balance(), 0, "Account balance is 0" );
585 is( $account->outstanding_debits->total_outstanding, 0, 'No outstanding debits' );
586 is( $account->outstanding_credits->total_outstanding, 0, 'Outstanding credits sum 0' );
588 $schema->storage->txn_rollback;
591 subtest 'more debit than credit' => sub {
595 $schema->storage->txn_begin;
597 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
598 my $account = $patron->account;
601 $account->add_credit({ amount => 1, interface => 'commandline' });
602 $account->add_credit({ amount => 2, interface => 'commandline' });
603 $account->add_credit({ amount => 3, interface => 'commandline' });
604 $account->add_credit({ amount => 4, interface => 'commandline' });
607 $account->add_debit({ amount => 1, interface => 'commandline', type => 'OVERDUE' });
608 $account->add_debit({ amount => 2, interface => 'commandline', type => 'OVERDUE' });
609 $account->add_debit({ amount => 3, interface => 'commandline', type => 'OVERDUE' });
610 $account->add_debit({ amount => 4, interface => 'commandline', type => 'OVERDUE' });
611 $account->add_debit({ amount => 5, interface => 'commandline', type => 'OVERDUE' });
614 Koha::Account::Line->new(
616 borrowernumber => $patron->id,
618 amountoutstanding => 0,
619 interface => 'commandline',
620 debit_type_code => 'OVERDUE'
623 Koha::Account::Line->new(
625 borrowernumber => $patron->id,
627 amountoutstanding => 0,
628 interface => 'commandline',
629 debit_type_code => 'OVERDUE'
633 is( $account->balance(), 5, "Account balance is 5" );
634 is( $account->outstanding_debits->total_outstanding, 15, 'Outstanding debits sum 15' );
635 is( $account->outstanding_credits->total_outstanding, -10, 'Outstanding credits sum -10' );
637 $account->reconcile_balance();
639 is( $account->balance(), 5, "Account balance is 5" );
640 is( $account->outstanding_debits->total_outstanding, 5, 'Outstanding debits sum 5' );
641 is( $account->outstanding_credits->total_outstanding, 0, 'Outstanding credits sum 0' );
643 $schema->storage->txn_rollback;
646 subtest 'credits are applied to older debits first' => sub {
650 $schema->storage->txn_begin;
652 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
653 my $account = $patron->account;
656 $account->add_credit({ amount => 1, interface => 'commandline' });
657 $account->add_credit({ amount => 3, interface => 'commandline' });
660 my $debit_1 = $account->add_debit({ amount => 1, interface => 'commandline', type => 'OVERDUE' });
661 my $debit_2 = $account->add_debit({ amount => 2, interface => 'commandline', type => 'OVERDUE' });
662 my $debit_3 = $account->add_debit({ amount => 3, interface => 'commandline', type => 'OVERDUE' });
664 is( $account->balance(), 2, "Account balance is 2" );
665 is( $account->outstanding_debits->total_outstanding, 6, 'Outstanding debits sum 6' );
666 is( $account->outstanding_credits->total_outstanding, -4, 'Outstanding credits sum -4' );
668 $account->reconcile_balance();
670 is( $account->balance(), 2, "Account balance is 2" );
671 is( $account->outstanding_debits->total_outstanding, 2, 'Outstanding debits sum 2' );
672 is( $account->outstanding_credits->total_outstanding, 0, 'Outstanding credits sum 0' );
674 $debit_1->discard_changes;
675 is( $debit_1->amountoutstanding + 0, 0, 'Old debit payed' );
676 $debit_2->discard_changes;
677 is( $debit_2->amountoutstanding + 0, 0, 'Old debit payed' );
678 $debit_3->discard_changes;
679 is( $debit_3->amountoutstanding + 0, 2, 'Newest debit only partially payed' );
681 $schema->storage->txn_rollback;
685 subtest 'pay() tests' => sub {
689 $schema->storage->txn_begin;
691 # Disable renewing upon fine payment
692 t::lib::Mocks::mock_preference( 'RenewAccruingItemWhenPaid', 0 );
694 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
695 my $library = $builder->build_object({ class => 'Koha::Libraries' });
696 my $account = $patron->account;
698 my $context = Test::MockModule->new('C4::Context');
699 $context->mock( 'userenv', { branch => $library->id } );
701 my $credit_1_id = $account->pay({ amount => 200 })->{payment_id};
702 my $credit_1 = Koha::Account::Lines->find( $credit_1_id );
704 is( $credit_1->branchcode, undef, 'No branchcode is set if library_id was not passed' );
706 my $credit_2_id = $account->pay({ amount => 150, library_id => $library->id })->{payment_id};
707 my $credit_2 = Koha::Account::Lines->find( $credit_2_id );
709 is( $credit_2->branchcode, $library->id, 'branchcode set because library_id was passed' );
711 # Enable cash registers
712 t::lib::Mocks::mock_preference( 'UseCashRegisters', 1 );
717 payment_type => 'CASH',
718 interface => 'intranet'
722 'Koha::Exceptions::Account::RegisterRequired',
723 'Exception thrown for UseCashRegisters:1 + payment_type:CASH + cash_register:undef';
725 # Disable cash registers
726 t::lib::Mocks::mock_preference( 'UseCashRegisters', 0 );
729 $context->mock( 'userenv', undef );
730 my $result = $account->pay(
733 payment_Type => 'CASH',
734 interface => 'intranet'
737 ok($result, "Koha::Account->pay functions without a userenv");
738 my $payment = Koha::Account::Lines->find({accountlines_id => $result->{payment_id}});
739 is($payment->manager_id, undef, "manager_id left undefined when no userenv found");
741 subtest 'UseEmailReceipts tests' => sub {
745 t::lib::Mocks::mock_preference( 'UseEmailReceipts', 1 );
749 my $mocked_letters = Test::MockModule->new('C4::Letters');
750 # we want to test the params
751 $mocked_letters->mock( 'GetPreparedLetter', sub {
755 # we don't care about EnqueueLetter for now
756 $mocked_letters->mock( 'EnqueueLetter', sub {
760 $schema->storage->txn_begin;
762 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
763 my $account = $patron->account;
765 my $debit_1 = $account->add_debit( { amount => 5, interface => 'commandline', type => 'OVERDUE' } );
766 my $debit_2 = $account->add_debit( { amount => 10, interface => 'commandline', type => 'OVERDUE' } );
768 $account->pay({ amount => 6, lines => [ $debit_1, $debit_2 ] });
769 my @offsets = @{$params{substitute}{offsets}};
771 is( scalar @offsets, 2, 'Two offsets related to payment' );
772 is( ref($offsets[0]), 'Koha::Account::Offset', 'Type is correct' );
773 is( ref($offsets[1]), 'Koha::Account::Offset', 'Type is correct' );
774 is( $offsets[0]->type, 'APPLY', 'Only APPLY offsets are passed to the notice' );
775 is( $offsets[1]->type, 'APPLY', 'Only APPLY offsets are passed to the notice' );
777 $schema->storage->txn_rollback;
780 $schema->storage->txn_rollback;
783 subtest 'pay() handles lost items when paying a specific lost fee' => sub {
787 $schema->storage->txn_begin;
789 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
790 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
791 my $account = $patron->account;
793 my $context = Test::MockModule->new('C4::Context');
794 $context->mock( 'userenv', { branch => $library->id } );
796 my $biblio = $builder->build_sample_biblio();
798 $builder->build_sample_item( { biblionumber => $biblio->biblionumber } );
800 my $checkout = Koha::Checkout->new(
802 borrowernumber => $patron->id,
803 itemnumber => $item->id,
804 date_due => \'NOW()',
805 branchcode => $patron->branchcode,
806 issuedate => \'NOW()',
810 $item->itemlost('1')->store();
812 my $accountline = Koha::Account::Line->new(
814 issue_id => $checkout->id,
815 borrowernumber => $patron->id,
816 itemnumber => $item->id,
818 debit_type_code => 'LOST',
821 amountoutstanding => '1',
828 library_id => $library->id,
829 lines => [$accountline],
833 $accountline = Koha::Account::Lines->find( $accountline->id );
834 is( $accountline->amountoutstanding+0, .5, 'Account line was paid down by half' );
836 $checkout = Koha::Checkouts->find( $checkout->id );
837 ok( $checkout, 'Item still checked out to patron' );
842 library_id => $library->id,
843 lines => [$accountline],
847 $accountline = Koha::Account::Lines->find( $accountline->id );
848 is( $accountline->amountoutstanding+0, 0, 'Account line was paid down by half' );
850 $checkout = Koha::Checkouts->find( $checkout->id );
851 ok( !$checkout, 'Item was removed from patron account' );
853 subtest 'item was not checked out to the same patron' => sub {
856 my $patron_2 = $builder->build_object(
858 class => 'Koha::Patrons',
859 value => { branchcode => $library->branchcode }
862 $item->itemlost('1')->store();
863 C4::Accounts::chargelostitem( $patron->borrowernumber, $item->itemnumber, 5, "lost" );
864 my $accountline = Koha::Account::Lines->search(
866 borrowernumber => $patron->borrowernumber,
867 itemnumber => $item->itemnumber,
868 debit_type_code => 'LOST'
871 my $checkout = Koha::Checkout->new(
873 borrowernumber => $patron_2->borrowernumber,
874 itemnumber => $item->itemnumber,
875 date_due => \'NOW()',
876 branchcode => $patron_2->branchcode,
877 issuedate => \'NOW()',
881 $patron->account->pay(
884 library_id => $library->branchcode,
885 lines => [$accountline],
890 Koha::Checkouts->find( $checkout->issue_id ),
891 'If the item is checked out to another patron, a lost item should not be returned if lost fee is paid'
896 $schema->storage->txn_rollback;
899 subtest 'pay() handles lost items when paying by amount ( not specifying the lost fee )' => sub {
903 $schema->storage->txn_begin;
905 # Enable AccountAutoReconcile
906 t::lib::Mocks::mock_preference( 'AccountAutoReconcile', 1 );
908 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
909 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
910 my $account = $patron->account;
912 my $context = Test::MockModule->new('C4::Context');
913 $context->mock( 'userenv', { branch => $library->id } );
915 my $biblio = $builder->build_sample_biblio();
917 $builder->build_sample_item( { biblionumber => $biblio->biblionumber } );
919 my $checkout = Koha::Checkout->new(
921 borrowernumber => $patron->id,
922 itemnumber => $item->id,
923 date_due => \'NOW()',
924 branchcode => $patron->branchcode,
925 issuedate => \'NOW()',
929 $item->itemlost('1')->store();
931 my $accountline = Koha::Account::Line->new(
933 issue_id => $checkout->id,
934 borrowernumber => $patron->id,
935 itemnumber => $item->id,
937 debit_type_code => 'LOST',
940 amountoutstanding => '1',
947 library_id => $library->id,
951 $accountline = Koha::Account::Lines->find( $accountline->id );
952 is( $accountline->amountoutstanding+0, .5, 'Account line was paid down by half' );
954 $checkout = Koha::Checkouts->find( $checkout->id );
955 ok( $checkout, 'Item still checked out to patron' );
960 library_id => $library->id,
964 $accountline = Koha::Account::Lines->find( $accountline->id );
965 is( $accountline->amountoutstanding+0, 0, 'Account line was paid down by half' );
967 $checkout = Koha::Checkouts->find( $checkout->id );
968 ok( !$checkout, 'Item was removed from patron account' );
970 $schema->storage->txn_rollback;
973 subtest 'pay() renews items when appropriate' => sub {
977 $schema->storage->txn_begin;
979 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
980 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
981 my $account = $patron->account;
983 my $context = Test::MockModule->new('C4::Context');
984 $context->mock( 'userenv', { branch => $library->id } );
986 my $biblio = $builder->build_sample_biblio();
988 $builder->build_sample_item( { biblionumber => $biblio->biblionumber } );
990 my $now = dt_from_string();
991 my $seven_weeks = DateTime::Duration->new(weeks => 7);
992 my $five_weeks = DateTime::Duration->new(weeks => 5);
993 my $seven_weeks_ago = $now - $seven_weeks;
994 my $five_weeks_ago = $now - $five_weeks;
996 my $checkout = Koha::Checkout->new(
998 borrowernumber => $patron->id,
999 itemnumber => $item->id,
1000 date_due => $five_weeks_ago,
1001 branchcode => $patron->branchcode,
1002 issuedate => $seven_weeks_ago
1006 my $accountline = Koha::Account::Line->new(
1008 issue_id => $checkout->id,
1009 borrowernumber => $patron->id,
1010 itemnumber => $item->id,
1012 debit_type_code => 'OVERDUE',
1013 status => 'UNRETURNED',
1016 amountoutstanding => '1',
1020 # Enable renewing upon fine payment
1021 t::lib::Mocks::mock_preference( 'RenewAccruingItemWhenPaid', 1 );
1023 my $module = Test::MockModule->new('C4::Circulation');
1024 $module->mock('AddRenewal', sub { $called = 1; });
1025 $module->mock('CanBookBeRenewed', sub { return 1; });
1026 my $result = $account->pay(
1029 library_id => $library->id,
1033 is( $called, 1, 'RenewAccruingItemWhenPaid causes C4::Circulation::AddRenew to be called when appropriate' );
1034 is(ref($result->{renew_result}), 'ARRAY', "Pay result contains 'renew_result' ARRAY" );
1035 is( scalar @{$result->{renew_result}}, 1, "renew_result contains one renewal result" );
1036 is( $result->{renew_result}->[0]->{itemnumber}, $item->id, "renew_result contains itemnumber of renewed item" );
1038 # Reset test by adding a new overdue
1039 Koha::Account::Line->new(
1041 issue_id => $checkout->id,
1042 borrowernumber => $patron->id,
1043 itemnumber => $item->id,
1045 debit_type_code => 'OVERDUE',
1046 status => 'UNRETURNED',
1049 amountoutstanding => '1',
1054 t::lib::Mocks::mock_preference( 'RenewAccruingItemWhenPaid', 0 );
1055 $result = $account->pay(
1058 library_id => $library->id,
1062 is( $called, 0, 'C4::Circulation::AddRenew NOT called when RenewAccruingItemWhenPaid disabled' );
1063 is(ref($result->{renew_result}), 'ARRAY', "Pay result contains 'renew_result' ARRAY" );
1064 is( scalar @{$result->{renew_result}}, 0, "renew_result contains no renewal results" );
1066 $schema->storage->txn_rollback;
1069 subtest 'Koha::Account::Line::apply() handles lost items' => sub {
1073 $schema->storage->txn_begin;
1075 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
1076 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
1077 my $account = $patron->account;
1079 my $context = Test::MockModule->new('C4::Context');
1080 $context->mock( 'userenv', { branch => $library->id } );
1082 my $biblio = $builder->build_sample_biblio();
1084 $builder->build_sample_item( { biblionumber => $biblio->biblionumber } );
1086 my $checkout = Koha::Checkout->new(
1088 borrowernumber => $patron->id,
1089 itemnumber => $item->id,
1090 date_due => \'NOW()',
1091 branchcode => $patron->branchcode,
1092 issuedate => \'NOW()',
1096 $item->itemlost('1')->store();
1098 my $debit = Koha::Account::Line->new(
1100 issue_id => $checkout->id,
1101 borrowernumber => $patron->id,
1102 itemnumber => $item->id,
1104 debit_type_code => 'LOST',
1107 amountoutstanding => '1',
1111 my $credit = Koha::Account::Line->new(
1113 borrowernumber => $patron->id,
1114 date => '1970-01-01 00:00:01',
1116 amountoutstanding => -.5,
1117 interface => 'commandline',
1118 credit_type_code => 'PAYMENT'
1121 my $debits = $account->outstanding_debits;
1122 $credit->apply({ debits => [ $debits->as_list ] });
1124 $debit = Koha::Account::Lines->find( $debit->id );
1125 is( $debit->amountoutstanding+0, .5, 'Account line was paid down by half' );
1127 $checkout = Koha::Checkouts->find( $checkout->id );
1128 ok( $checkout, 'Item still checked out to patron' );
1130 $credit = Koha::Account::Line->new(
1132 borrowernumber => $patron->id,
1133 date => '1970-01-01 00:00:01',
1135 amountoutstanding => -.5,
1136 interface => 'commandline',
1137 credit_type_code => 'PAYMENT'
1140 $debits = $account->outstanding_debits;
1141 $credit->apply({ debits => [ $debits->as_list ] });
1143 $debit = Koha::Account::Lines->find( $debit->id );
1144 is( $debit->amountoutstanding+0, 0, 'Account line was paid down by half' );
1146 $checkout = Koha::Checkouts->find( $checkout->id );
1147 ok( !$checkout, 'Item was removed from patron account' );
1149 $schema->storage->txn_rollback;
1152 subtest 'Koha::Account::pay() generates credit number (Koha::Account::Line->store)' => sub {
1155 $schema->storage->txn_begin;
1157 Koha::Account::Lines->delete();
1159 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
1160 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
1161 my $account = $patron->account;
1163 #t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode });
1164 my $context = Test::MockModule->new('C4::Context');
1165 $context->mock( 'userenv', { branch => $library->id } );
1167 my $now = dt_from_string;
1168 my $year = $now->year;
1169 my $month = $now->month;
1170 my ($accountlines_id, $accountline);
1172 my $credit_type = Koha::Account::CreditTypes->find('PAYMENT');
1173 $credit_type->credit_number_enabled(1);
1174 $credit_type->store();
1176 t::lib::Mocks::mock_preference('AutoCreditNumber', '');
1177 $accountlines_id = $account->pay({ amount => '1.00', library_id => $library->id })->{payment_id};
1178 $accountline = Koha::Account::Lines->find($accountlines_id);
1179 is($accountline->credit_number, undef, 'No credit number is generated when syspref is off');
1181 t::lib::Mocks::mock_preference('AutoCreditNumber', 'incremental');
1183 $accountlines_id = $account->pay({ amount => '1.00', library_id => $library->id })->{payment_id};
1184 $accountline = Koha::Account::Lines->find($accountlines_id);
1185 is($accountline->credit_number, $i, "Incremental format credit number added for payments: $i");
1187 $accountlines_id = $account->pay({ type => 'WRITEOFF', amount => '1.00', library_id => $library->id })->{payment_id};
1188 $accountline = Koha::Account::Lines->find($accountlines_id);
1189 is($accountline->credit_number, undef, "Incremental credit number not added for writeoff");
1191 t::lib::Mocks::mock_preference('AutoCreditNumber', 'annual');
1193 $accountlines_id = $account->pay({ amount => '1.00', library_id => $library->id })->{payment_id};
1194 $accountline = Koha::Account::Lines->find($accountlines_id);
1195 is($accountline->credit_number, sprintf('%s-%04d', $year, $i), "Annual format credit number added for payments: " . sprintf('%s-%04d', $year, $i));
1197 $accountlines_id = $account->pay({ type => 'WRITEOFF', amount => '1.00', library_id => $library->id })->{payment_id};
1198 $accountline = Koha::Account::Lines->find($accountlines_id);
1199 is($accountline->credit_number, undef, "Annual format credit number not aded for writeoff");
1201 t::lib::Mocks::mock_preference('AutoCreditNumber', 'branchyyyymmincr');
1203 $accountlines_id = $account->pay({ amount => '1.00', library_id => $library->id })->{payment_id};
1204 $accountline = Koha::Account::Lines->find($accountlines_id);
1205 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));
1207 $accountlines_id = $account->pay({ type => 'WRITEOFF', amount => '1.00', library_id => $library->id })->{payment_id};
1208 $accountline = Koha::Account::Lines->find($accountlines_id);
1209 is($accountline->credit_number, undef, "branchyyyymmincr format credit number not added for writeoff");
1212 Koha::Account::Line->new(
1214 interface => 'test',
1216 credit_type_code => $credit_type->code,
1221 'Koha::Exceptions::Account',
1222 "Exception thrown when AutoCreditNumber is enabled but credit_number is already defined";
1224 $schema->storage->txn_rollback;
1227 subtest 'Koha::Account::payout_amount() tests' => sub {
1230 $schema->storage->txn_begin;
1232 # delete logs and statistics
1233 my $action_logs = $schema->resultset('ActionLog')->search()->count;
1234 my $statistics = $schema->resultset('Statistic')->search()->count;
1236 my $staff = $builder->build_object( { class => 'Koha::Patrons' } );
1237 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
1239 $builder->build_object( { class => 'Koha::Cash::Registers' } );
1240 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
1242 Koha::Account->new( { patron_id => $patron->borrowernumber } );
1244 is( $account->balance, 0, 'Test patron has no balance' );
1246 my $payout_params = {
1247 payout_type => 'CASH',
1248 branch => $library->id,
1249 register_id => $register->id,
1250 staff_id => $staff->id,
1251 interface => 'intranet',
1255 my @required_fields =
1256 ( 'interface', 'staff_id', 'branch', 'payout_type', 'amount' );
1257 for my $required_field (@required_fields) {
1258 my $this_payout = { %{$payout_params} };
1259 delete $this_payout->{$required_field};
1262 $account->payout_amount($this_payout);
1264 'Koha::Exceptions::MissingParameter',
1265 "Exception thrown if $required_field parameter missing";
1269 $account->payout_amount($payout_params);
1271 'Koha::Exceptions::Account::AmountNotPositive',
1272 'Expected validation exception thrown (amount not positive)';
1274 $payout_params->{amount} = 10;
1276 $account->payout_amount($payout_params);
1278 'Koha::Exceptions::ParameterTooHigh',
1279 'Expected validation exception thrown (amount greater than outstanding)';
1281 # Enable cash registers
1282 t::lib::Mocks::mock_preference( 'UseCashRegisters', 1 );
1284 $account->payout_amount($payout_params);
1286 'Koha::Exceptions::Account::RegisterRequired',
1287 'Exception thrown for UseCashRegisters:1 + payout_type:CASH + cash_register:undef';
1289 # Disable cash registers
1290 t::lib::Mocks::mock_preference( 'UseCashRegisters', 0 );
1292 # Add some outstanding credits
1293 my $credit_1 = $account->add_credit( { amount => 2, interface => 'commandline' } );
1294 my $credit_2 = $account->add_credit( { amount => 3, interface => 'commandline' } );
1295 my $credit_3 = $account->add_credit( { amount => 5, interface => 'commandline' } );
1296 my $credit_4 = $account->add_credit( { amount => 10, interface => 'commandline' } );
1297 my $credits = $account->outstanding_credits();
1298 is( $credits->count, 4, "Found 4 credits with outstanding amounts" );
1299 is( $credits->total_outstanding + 0, -20, "Total -20 outstanding credit" );
1301 my $payout = $account->payout_amount($payout_params);
1302 is(ref($payout), 'Koha::Account::Line', 'Return the Koha::Account::Line object for the payout');
1303 is($payout->amount + 0, 10, "Payout amount recorded correctly");
1304 is($payout->amountoutstanding + 0, 0, "Full amount was paid out");
1305 $credits = $account->outstanding_credits();
1306 is($credits->count, 1, "Payout was applied against oldest outstanding credits first");
1307 is($credits->total_outstanding + 0, -10, "Total of 10 outstanding credit remaining");
1309 my $offsets = Koha::Account::Offsets->search( { debit_id => $payout->id } );
1310 is( $offsets->count, 4, 'Four offsets generated' );
1311 my $offset = $offsets->next;
1312 is( $offset->type, 'CREATE', 'CREATE offset added for payout line' );
1313 is( $offset->amount * 1, 10, 'Correct offset amount recorded' );
1314 $offset = $offsets->next;
1315 is( $offset->credit_id, $credit_1->id, "Offset added against credit_1");
1316 is( $offset->type, 'APPLY', "APPLY used for offset_type" );
1317 is( $offset->amount * 1, -2, 'Correct amount offset against credit_1' );
1318 $offset = $offsets->next;
1319 is( $offset->credit_id, $credit_2->id, "Offset added against credit_2");
1320 is( $offset->type, 'APPLY', "APPLY used for offset_type" );
1321 is( $offset->amount * 1, -3, 'Correct amount offset against credit_2' );
1322 $offset = $offsets->next;
1323 is( $offset->credit_id, $credit_3->id, "Offset added against credit_3");
1324 is( $offset->type, 'APPLY', "APPLY used for offset_type" );
1325 is( $offset->amount * 1, -5, 'Correct amount offset against credit_3' );
1327 my $credit_5 = $account->add_credit( { amount => 5, interface => 'commandline' } );
1328 $credits = $account->outstanding_credits();
1329 is($credits->count, 2, "New credit added");
1330 $payout_params->{amount} = 2.50;
1331 $payout_params->{credits} = [$credit_5];
1332 $payout = $account->payout_amount($payout_params);
1334 $credits = $account->outstanding_credits();
1335 is($credits->count, 2, "Second credit not fully paid off");
1336 is($credits->total_outstanding + 0, -12.50, "12.50 outstanding credit remaining");
1337 $credit_4->discard_changes;
1338 $credit_5->discard_changes;
1339 is($credit_4->amountoutstanding + 0, -10, "Credit 4 unaffected when credit_5 was passed to payout_amount");
1340 is($credit_5->amountoutstanding + 0, -2.50, "Credit 5 correctly reduced when payout_amount called with credit_5 passed");
1342 $offsets = Koha::Account::Offsets->search( { debit_id => $payout->id } );
1343 is( $offsets->count, 2, 'Two offsets generated' );
1344 $offset = $offsets->next;
1345 is( $offset->type, 'CREATE', 'CREATE offset added for payout line' );
1346 is( $offset->amount * 1, 2.50, 'Correct offset amount recorded' );
1347 $offset = $offsets->next;
1348 is( $offset->credit_id, $credit_5->id, "Offset added against credit_5");
1349 is( $offset->type, 'APPLY', "APPLY used for offset_type" );
1350 is( $offset->amount * 1, -2.50, 'Correct amount offset against credit_5' );
1352 $schema->storage->txn_rollback;
1355 subtest 'Koha::Account::payin_amount() tests' => sub {
1358 $schema->storage->txn_begin;
1360 # delete logs and statistics
1361 my $action_logs = $schema->resultset('ActionLog')->search()->count;
1362 my $statistics = $schema->resultset('Statistic')->search()->count;
1364 my $staff = $builder->build_object( { class => 'Koha::Patrons' } );
1365 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
1367 $builder->build_object( { class => 'Koha::Cash::Registers' } );
1368 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
1370 Koha::Account->new( { patron_id => $patron->borrowernumber } );
1372 is( $account->balance, 0, 'Test patron has no balance' );
1374 my $payin_params = {
1376 payment_type => 'CASH',
1377 branch => $library->id,
1378 register_id => $register->id,
1379 staff_id => $staff->id,
1380 interface => 'intranet',
1384 my @required_fields =
1385 ( 'interface', 'amount', 'type' );
1386 for my $required_field (@required_fields) {
1387 my $this_payin = { %{$payin_params} };
1388 delete $this_payin->{$required_field};
1391 $account->payin_amount($this_payin);
1393 'Koha::Exceptions::MissingParameter',
1394 "Exception thrown if $required_field parameter missing";
1398 $account->payin_amount($payin_params);
1400 'Koha::Exceptions::Account::AmountNotPositive',
1401 'Expected validation exception thrown (amount not positive)';
1403 $payin_params->{amount} = 10;
1405 # Enable cash registers
1406 t::lib::Mocks::mock_preference( 'UseCashRegisters', 1 );
1408 $account->payin_amount($payin_params);
1410 'Koha::Exceptions::Account::RegisterRequired',
1411 'Exception thrown for UseCashRegisters:1 + payment_type:CASH + cash_register:undef';
1413 # Disable cash registers
1414 t::lib::Mocks::mock_preference( 'UseCashRegisters', 0 );
1416 # Enable AccountAutoReconcile
1417 t::lib::Mocks::mock_preference( 'AccountAutoReconcile', 1 );
1419 # Add some outstanding debits
1420 my $debit_1 = $account->add_debit( { amount => 2, interface => 'commandline', type => 'OVERDUE' } );
1421 my $debit_2 = $account->add_debit( { amount => 3, interface => 'commandline', type => 'OVERDUE' } );
1422 my $debit_3 = $account->add_debit( { amount => 5, interface => 'commandline', type => 'OVERDUE' } );
1423 my $debit_4 = $account->add_debit( { amount => 10, interface => 'commandline', type => 'OVERDUE' } );
1424 my $debits = $account->outstanding_debits();
1425 is( $debits->count, 4, "Found 4 debits with outstanding amounts" );
1426 is( $debits->total_outstanding + 0, 20, "Total 20 outstanding debit" );
1428 my $payin = $account->payin_amount($payin_params);
1429 is(ref($payin), 'Koha::Account::Line', 'Return the Koha::Account::Line object for the payin');
1430 is($payin->amount + 0, -10, "Payin amount recorded correctly");
1431 is($payin->amountoutstanding + 0, 0, "Full amount was used to pay debts");
1432 $debits = $account->outstanding_debits();
1433 is($debits->count, 1, "Payin was applied against oldest outstanding debits first");
1434 is($debits->total_outstanding + 0, 10, "Total of 10 outstanding debit remaining");
1436 my $offsets = Koha::Account::Offsets->search( { credit_id => $payin->id } );
1437 is( $offsets->count, 4, 'Four offsets generated' );
1438 my $offset = $offsets->next;
1439 is( $offset->type, 'CREATE', 'CREATE offset added for payin line' );
1440 is( $offset->amount * 1, 10, 'Correct offset amount recorded' );
1441 $offset = $offsets->next;
1442 is( $offset->debit_id, $debit_1->id, "Offset added against debit_1");
1443 is( $offset->type, 'APPLY', "APPLY used for offset_type" );
1444 is( $offset->amount * 1, -2, 'Correct amount offset against debit_1' );
1445 $offset = $offsets->next;
1446 is( $offset->debit_id, $debit_2->id, "Offset added against debit_2");
1447 is( $offset->type, 'APPLY', "APPLY used for offset_type" );
1448 is( $offset->amount * 1, -3, 'Correct amount offset against debit_2' );
1449 $offset = $offsets->next;
1450 is( $offset->debit_id, $debit_3->id, "Offset added against debit_3");
1451 is( $offset->type, 'APPLY', "APPLY used for offset_type" );
1452 is( $offset->amount * 1, -5, 'Correct amount offset against debit_3' );
1454 my $debit_5 = $account->add_debit( { amount => 5, interface => 'commandline', type => 'OVERDUE' } );
1455 $debits = $account->outstanding_debits();
1456 is($debits->count, 2, "New debit added");
1457 $payin_params->{amount} = 2.50;
1458 $payin_params->{debits} = [$debit_5];
1459 $payin = $account->payin_amount($payin_params);
1461 $debits = $account->outstanding_debits();
1462 is($debits->count, 2, "Second debit not fully paid off");
1463 is($debits->total_outstanding + 0, 12.50, "12.50 outstanding debit remaining");
1464 $debit_4->discard_changes;
1465 $debit_5->discard_changes;
1466 is($debit_4->amountoutstanding + 0, 10, "Debit 4 unaffected when debit_5 was passed to payin_amount");
1467 is($debit_5->amountoutstanding + 0, 2.50, "Debit 5 correctly reduced when payin_amount called with debit_5 passed");
1469 $offsets = Koha::Account::Offsets->search( { credit_id => $payin->id } );
1470 is( $offsets->count, 2, 'Two offsets generated' );
1471 $offset = $offsets->next;
1472 is( $offset->type, 'CREATE', 'CREATE offset added for payin line' );
1473 is( $offset->amount * 1, 2.50, 'Correct offset amount recorded' );
1474 $offset = $offsets->next;
1475 is( $offset->debit_id, $debit_5->id, "Offset added against debit_5");
1476 is( $offset->type, 'APPLY', "APPLY used for offset_type" );
1477 is( $offset->amount * 1, -2.50, 'Correct amount offset against debit_5' );
1479 $schema->storage->txn_rollback;