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 => 11;
27 use Koha::Account::Lines;
28 use Koha::Account::Offsets;
32 use t::lib::TestBuilder;
34 my $schema = Koha::Database->new->schema;
35 $schema->storage->dbh->{PrintError} = 0;
36 my $builder = t::lib::TestBuilder->new;
37 C4::Context->interface('commandline');
39 subtest 'new' => sub {
43 $schema->storage->txn_begin;
45 throws_ok { Koha::Account->new(); } qr/No patron id passed in!/, 'Croaked on bad call to new';
47 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
48 my $account = Koha::Account->new( { patron_id => $patron->borrowernumber } );
49 is( defined $account, 1, "Account is defined" );
51 $schema->storage->txn_rollback;
54 subtest 'outstanding_debits() tests' => sub {
58 $schema->storage->txn_begin;
60 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
61 my $account = $patron->account;
64 push @generated_lines, $account->add_debit({ amount => 1, interface => 'commandline', type => 'OVERDUE' });
65 push @generated_lines, $account->add_debit({ amount => 2, interface => 'commandline', type => 'OVERDUE' });
66 push @generated_lines, $account->add_debit({ amount => 3, interface => 'commandline', type => 'OVERDUE' });
67 push @generated_lines, $account->add_debit({ amount => 4, interface => 'commandline', type => 'OVERDUE' });
69 my $lines = $account->outstanding_debits();
70 my @lines_arr = $account->outstanding_debits();
72 is( ref($lines), 'Koha::Account::Lines', 'Called in scalar context, outstanding_debits returns a Koha::Account::Lines object' );
73 is( $lines->total_outstanding, 10, 'Outstandig debits total is correctly calculated' );
76 foreach my $line ( @{ $lines->as_list } ) {
77 my $fetched_line = Koha::Account::Lines->find( $generated_lines[$i]->id );
78 is_deeply( $line->unblessed, $fetched_line->unblessed, "Fetched line matches the generated one ($i)" );
79 is_deeply( $lines_arr[$i]->unblessed, $fetched_line->unblessed, "Fetched line matches the generated one ($i)" );
80 is( ref($lines_arr[$i]), 'Koha::Account::Line', 'outstanding_debits returns a list of Koha::Account::Line objects in list context' );
83 my $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
84 Koha::Account::Line->new(
86 borrowernumber => $patron_2->id,
87 amountoutstanding => -2,
88 interface => 'commandline',
92 my $just_one = Koha::Account::Line->new(
94 borrowernumber => $patron_2->id,
96 amountoutstanding => 3,
97 interface => 'commandline',
98 debit_type_code => 'OVERDUE'
101 Koha::Account::Line->new(
103 borrowernumber => $patron_2->id,
105 amountoutstanding => -6,
106 interface => 'commandline',
110 $lines = $patron_2->account->outstanding_debits();
111 is( $lines->total_outstanding, 3, "Total if some outstanding debits and some credits is only debits" );
112 is( $lines->count, 1, "With 1 outstanding debits, we get back a Lines object with 1 lines" );
113 my $the_line = Koha::Account::Lines->find( $just_one->id );
114 is_deeply( $the_line->unblessed, $lines->next->unblessed, "We get back the one correct line");
116 my $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
117 my $account_3 = $patron_3->account;
118 $account_3->add_credit( { amount => 2, interface => 'commandline' } );
119 $account_3->add_credit( { amount => 20, interface => 'commandline' } );
120 $account_3->add_credit( { amount => 200, interface => 'commandline' } );
121 $lines = $account_3->outstanding_debits();
122 is( $lines->total_outstanding, 0, "Total if no outstanding debits total is 0" );
123 is( $lines->count, 0, "With 0 outstanding debits, we get back a Lines object with 0 lines" );
125 my $patron_4 = $builder->build_object({ class => 'Koha::Patrons' });
126 my $account_4 = $patron_4->account;
127 $lines = $account_4->outstanding_debits();
128 is( $lines->total_outstanding, 0, "Total if no outstanding debits is 0" );
129 is( $lines->count, 0, "With no outstanding debits, we get back a Lines object with 0 lines" );
131 # create a pathological credit with amountoutstanding > 0 (BZ 14591)
132 Koha::Account::Line->new(
134 borrowernumber => $patron_4->id,
136 amountoutstanding => 3,
137 interface => 'commandline',
141 $lines = $account_4->outstanding_debits();
142 is( $lines->count, 0, 'No credits are confused with debits because of the amountoutstanding value' );
144 $schema->storage->txn_rollback;
147 subtest 'outstanding_credits() tests' => sub {
151 $schema->storage->txn_begin;
153 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
154 my $account = $patron->account;
157 push @generated_lines, $account->add_credit({ amount => 1, interface => 'commandline' });
158 push @generated_lines, $account->add_credit({ amount => 2, interface => 'commandline' });
159 push @generated_lines, $account->add_credit({ amount => 3, interface => 'commandline' });
160 push @generated_lines, $account->add_credit({ amount => 4, interface => 'commandline' });
162 my $lines = $account->outstanding_credits();
163 my @lines_arr = $account->outstanding_credits();
165 is( ref($lines), 'Koha::Account::Lines', 'Called in scalar context, outstanding_credits returns a Koha::Account::Lines object' );
166 is( $lines->total_outstanding, -10, 'Outstandig credits total is correctly calculated' );
169 foreach my $line ( @{ $lines->as_list } ) {
170 my $fetched_line = Koha::Account::Lines->find( $generated_lines[$i]->id );
171 is_deeply( $line->unblessed, $fetched_line->unblessed, "Fetched line matches the generated one ($i)" );
172 is_deeply( $lines_arr[$i]->unblessed, $fetched_line->unblessed, "Fetched line matches the generated one ($i)" );
173 is( ref($lines_arr[$i]), 'Koha::Account::Line', 'outstanding_debits returns a list of Koha::Account::Line objects in list context' );
177 my $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
178 $account = $patron_2->account;
179 $lines = $account->outstanding_credits();
180 is( $lines->total_outstanding, 0, "Total if no outstanding credits is 0" );
181 is( $lines->count, 0, "With no outstanding credits, we get back a Lines object with 0 lines" );
183 # create a pathological debit with amountoutstanding < 0 (BZ 14591)
184 Koha::Account::Line->new(
186 borrowernumber => $patron_2->id,
188 amountoutstanding => -3,
189 interface => 'commandline',
190 debit_type_code => 'OVERDUE'
193 $lines = $account->outstanding_credits();
194 is( $lines->count, 0, 'No debits are confused with credits because of the amountoutstanding value' );
196 $schema->storage->txn_rollback;
199 subtest 'add_credit() tests' => sub {
203 $schema->storage->txn_begin;
205 # delete logs and statistics
206 my $action_logs = $schema->resultset('ActionLog')->search()->count;
207 my $statistics = $schema->resultset('Statistic')->search()->count;
209 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
210 my $account = Koha::Account->new( { patron_id => $patron->borrowernumber } );
212 is( $account->balance, 0, 'Patron has no balance' );
215 t::lib::Mocks::mock_preference( 'FinesLog', 0 );
218 $account->add_credit(
220 description => 'Payment of 25',
221 library_id => $patron->branchcode,
222 note => 'not really important',
224 user_id => $patron->id
228 'Koha::Exceptions::MissingParameter', 'Exception thrown if interface parameter missing';
230 my $line_1 = $account->add_credit(
232 description => 'Payment of 25',
233 library_id => $patron->branchcode,
234 note => 'not really important',
236 user_id => $patron->id,
237 interface => 'commandline'
241 is( $account->balance, -25, 'Patron has a balance of -25' );
242 is( $schema->resultset('ActionLog')->count(), $action_logs + 0, 'No log was added' );
243 is( $schema->resultset('Statistic')->count(), $statistics + 1, 'Action added to statistics' );
244 is( $line_1->accounttype, $Koha::Account::account_type_credit->{'payment'}, 'Account type is correctly set' );
247 t::lib::Mocks::mock_preference( 'FinesLog', 1 );
249 my $line_2 = $account->add_credit(
251 description => 'Payment of 37',
252 library_id => $patron->branchcode,
253 note => 'not really important',
254 user_id => $patron->id,
255 interface => 'commandline'
259 is( $account->balance, -62, 'Patron has a balance of -25' );
260 is( $schema->resultset('ActionLog')->count(), $action_logs + 1, 'Log was added' );
261 is( $schema->resultset('Statistic')->count(), $statistics + 2, 'Action added to statistics' );
262 is( $line_2->accounttype, $Koha::Account::account_type_credit->{'payment'}, 'Account type is correctly set' );
264 # offsets have the credit_id set to accountlines_id, and debit_id is undef
265 my $offset_1 = Koha::Account::Offsets->search({ credit_id => $line_1->id })->next;
266 my $offset_2 = Koha::Account::Offsets->search({ credit_id => $line_2->id })->next;
268 is( $offset_1->credit_id, $line_1->id, 'No debit_id is set for credits' );
269 is( $offset_1->debit_id, undef, 'No debit_id is set for credits' );
270 is( $offset_2->credit_id, $line_2->id, 'No debit_id is set for credits' );
271 is( $offset_2->debit_id, undef, 'No debit_id is set for credits' );
273 my $line_3 = $account->add_credit(
276 description => 'Manual credit applied',
277 library_id => $patron->branchcode,
278 user_id => $patron->id,
280 interface => 'commandline'
284 is( $schema->resultset('ActionLog')->count(), $action_logs + 2, 'Log was added' );
285 is( $schema->resultset('Statistic')->count(), $statistics + 2, 'No action added to statistics, because of credit type' );
287 # Enable cash registers
288 t::lib::Mocks::mock_preference( 'UseCashRegisters', 1 );
290 $account->add_credit(
293 description => 'Cash payment without cash register',
294 library_id => $patron->branchcode,
295 user_id => $patron->id,
296 payment_type => 'CASH',
297 interface => 'intranet'
301 'Koha::Exceptions::Account::RegisterRequired',
302 'Exception thrown for UseCashRegisters:1 + payment_type:CASH + cash_register:undef';
304 # Disable cash registers
305 t::lib::Mocks::mock_preference( 'UseCashRegisters', 1 );
307 $schema->storage->txn_rollback;
310 subtest 'add_debit() tests' => sub {
314 $schema->storage->txn_begin;
316 # delete logs and statistics
317 my $action_logs = $schema->resultset('ActionLog')->search()->count;
318 my $statistics = $schema->resultset('Statistic')->search()->count;
320 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
322 Koha::Account->new( { patron_id => $patron->borrowernumber } );
324 is( $account->balance, 0, 'Patron has no balance' );
330 description => 'amount validation failure',
331 library_id => $patron->branchcode,
332 note => 'this should fail anyway',
334 user_id => $patron->id,
335 interface => 'commandline'
337 ); } 'Koha::Exceptions::Account::AmountNotPositive', 'Expected validation exception thrown (amount)';
343 description => 'type validation failure',
344 library_id => $patron->branchcode,
345 note => 'this should fail anyway',
347 user_id => $patron->id,
348 interface => 'commandline'
350 ); } 'Koha::Exceptions::Account::UnrecognisedType', 'Expected validation exception thrown (type)';
356 description => 'Rental charge of 25',
357 library_id => $patron->branchcode,
358 note => 'not really important',
360 user_id => $patron->id
362 ); } 'Koha::Exceptions::MissingParameter', 'Exception thrown if interface parameter missing';
365 t::lib::Mocks::mock_preference( 'FinesLog', 0 );
367 my $line_1 = $account->add_debit(
370 description => 'Rental charge of 25',
371 library_id => $patron->branchcode,
372 note => 'not really important',
374 user_id => $patron->id,
375 interface => 'commandline'
379 is( $account->balance, 25, 'Patron has a balance of 25' );
381 $schema->resultset('ActionLog')->count(),
386 $line_1->debit_type_code,
388 'Account type is correctly set'
392 t::lib::Mocks::mock_preference( 'FinesLog', 1 );
394 my $line_2 = $account->add_debit(
397 description => 'Rental charge of 37',
398 library_id => $patron->branchcode,
399 note => 'not really important',
401 user_id => $patron->id,
402 interface => 'commandline'
406 is( $account->balance, 62, 'Patron has a balance of 62' );
408 $schema->resultset('ActionLog')->count(),
413 $line_2->debit_type_code,
415 'Account type is correctly set'
418 # offsets have the debit_id set to accountlines_id, and credit_id is undef
420 Koha::Account::Offsets->search( { debit_id => $line_1->id } )->next;
422 Koha::Account::Offsets->search( { debit_id => $line_2->id } )->next;
424 is( $offset_1->debit_id, $line_1->id, 'debit_id is set for debit 1' );
425 is( $offset_1->credit_id, undef, 'credit_id is not set for debit 1' );
426 is( $offset_2->debit_id, $line_2->id, 'debit_id is set for debit 2' );
427 is( $offset_2->credit_id, undef, 'credit_id is not set for debit 2' );
429 $schema->storage->txn_rollback;
432 subtest 'lines() tests' => sub {
436 $schema->storage->txn_begin;
438 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
439 my $account = $patron->account;
442 $account->add_credit({ amount => 1, interface => 'commandline' });
443 $account->add_credit({ amount => 2, interface => 'commandline' });
444 $account->add_credit({ amount => 3, interface => 'commandline' });
445 $account->add_credit({ amount => 4, interface => 'commandline' });
448 $account->add_debit({ amount => 1, interface => 'commandline', type => 'OVERDUE' });
449 $account->add_debit({ amount => 2, interface => 'commandline', type => 'OVERDUE' });
450 $account->add_debit({ amount => 3, interface => 'commandline', type => 'OVERDUE' });
451 $account->add_debit({ amount => 4, interface => 'commandline', type => 'OVERDUE' });
454 $account->add_credit( { amount => 1, interface => 'commandline' } )
455 ->apply( { debits => [ $account->outstanding_debits->as_list ] } );
457 my $lines = $account->lines;
458 is( $lines->_resultset->count, 9, "All accountlines (debits, credits and paid off) were fetched");
460 $schema->storage->txn_rollback;
463 subtest 'reconcile_balance' => sub {
467 subtest 'more credit than debit' => sub {
471 $schema->storage->txn_begin;
473 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
474 my $account = $patron->account;
477 $account->add_credit({ amount => 1, interface => 'commandline' });
478 $account->add_credit({ amount => 2, interface => 'commandline' });
479 $account->add_credit({ amount => 3, interface => 'commandline' });
480 $account->add_credit({ amount => 4, interface => 'commandline' });
481 $account->add_credit({ amount => 5, interface => 'commandline' });
484 $account->add_debit({ amount => 1, interface => 'commandline', type => 'OVERDUE' });
485 $account->add_debit({ amount => 2, interface => 'commandline', type => 'OVERDUE' });
486 $account->add_debit({ amount => 3, interface => 'commandline', type => 'OVERDUE' });
487 $account->add_debit({ amount => 4, interface => 'commandline', type => 'OVERDUE' });
490 Koha::Account::Line->new(
492 borrowernumber => $patron->id,
494 amountoutstanding => 0,
495 interface => 'commandline',
496 debit_type_code => 'OVERDUE'
499 Koha::Account::Line->new(
501 borrowernumber => $patron->id,
503 amountoutstanding => 0,
504 interface => 'commandline',
505 debit_type_code => 'OVERDUE'
509 is( $account->balance(), -5, "Account balance is -5" );
510 is( $account->outstanding_debits->total_outstanding, 10, 'Outstanding debits sum 10' );
511 is( $account->outstanding_credits->total_outstanding, -15, 'Outstanding credits sum -15' );
513 $account->reconcile_balance();
515 is( $account->balance(), -5, "Account balance is -5" );
516 is( $account->outstanding_debits->total_outstanding, 0, 'No outstanding debits' );
517 is( $account->outstanding_credits->total_outstanding, -5, 'Outstanding credits sum -5' );
519 $schema->storage->txn_rollback;
522 subtest 'same debit as credit' => sub {
526 $schema->storage->txn_begin;
528 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
529 my $account = $patron->account;
532 $account->add_credit({ amount => 1, interface => 'commandline' });
533 $account->add_credit({ amount => 2, interface => 'commandline' });
534 $account->add_credit({ amount => 3, interface => 'commandline' });
535 $account->add_credit({ amount => 4, interface => 'commandline' });
538 $account->add_debit({ amount => 1, interface => 'commandline', type => 'OVERDUE' });
539 $account->add_debit({ amount => 2, interface => 'commandline', type => 'OVERDUE' });
540 $account->add_debit({ amount => 3, interface => 'commandline', type => 'OVERDUE' });
541 $account->add_debit({ amount => 4, interface => 'commandline', type => 'OVERDUE' });
544 Koha::Account::Line->new(
546 borrowernumber => $patron->id,
548 amountoutstanding => 0,
549 interface => 'commandline',
550 debit_type_code => 'OVERDUE'
553 Koha::Account::Line->new(
555 borrowernumber => $patron->id,
557 amountoutstanding => 0,
558 interface => 'commandline',
559 debit_type_code => 'OVERDUE'
563 is( $account->balance(), 0, "Account balance is 0" );
564 is( $account->outstanding_debits->total_outstanding, 10, 'Outstanding debits sum 10' );
565 is( $account->outstanding_credits->total_outstanding, -10, 'Outstanding credits sum -10' );
567 $account->reconcile_balance();
569 is( $account->balance(), 0, "Account balance is 0" );
570 is( $account->outstanding_debits->total_outstanding, 0, 'No outstanding debits' );
571 is( $account->outstanding_credits->total_outstanding, 0, 'Outstanding credits sum 0' );
573 $schema->storage->txn_rollback;
576 subtest 'more debit than credit' => sub {
580 $schema->storage->txn_begin;
582 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
583 my $account = $patron->account;
586 $account->add_credit({ amount => 1, interface => 'commandline' });
587 $account->add_credit({ amount => 2, interface => 'commandline' });
588 $account->add_credit({ amount => 3, interface => 'commandline' });
589 $account->add_credit({ amount => 4, interface => 'commandline' });
592 $account->add_debit({ amount => 1, interface => 'commandline', type => 'OVERDUE' });
593 $account->add_debit({ amount => 2, interface => 'commandline', type => 'OVERDUE' });
594 $account->add_debit({ amount => 3, interface => 'commandline', type => 'OVERDUE' });
595 $account->add_debit({ amount => 4, interface => 'commandline', type => 'OVERDUE' });
596 $account->add_debit({ amount => 5, interface => 'commandline', type => 'OVERDUE' });
599 Koha::Account::Line->new(
601 borrowernumber => $patron->id,
603 amountoutstanding => 0,
604 interface => 'commandline',
605 debit_type_code => 'OVERDUE'
608 Koha::Account::Line->new(
610 borrowernumber => $patron->id,
612 amountoutstanding => 0,
613 interface => 'commandline',
614 debit_type_code => 'OVERDUE'
618 is( $account->balance(), 5, "Account balance is 5" );
619 is( $account->outstanding_debits->total_outstanding, 15, 'Outstanding debits sum 15' );
620 is( $account->outstanding_credits->total_outstanding, -10, 'Outstanding credits sum -10' );
622 $account->reconcile_balance();
624 is( $account->balance(), 5, "Account balance is 5" );
625 is( $account->outstanding_debits->total_outstanding, 5, 'Outstanding debits sum 5' );
626 is( $account->outstanding_credits->total_outstanding, 0, 'Outstanding credits sum 0' );
628 $schema->storage->txn_rollback;
631 subtest 'credits are applied to older debits first' => sub {
635 $schema->storage->txn_begin;
637 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
638 my $account = $patron->account;
641 $account->add_credit({ amount => 1, interface => 'commandline' });
642 $account->add_credit({ amount => 3, interface => 'commandline' });
645 my $debit_1 = $account->add_debit({ amount => 1, interface => 'commandline', type => 'OVERDUE' });
646 my $debit_2 = $account->add_debit({ amount => 2, interface => 'commandline', type => 'OVERDUE' });
647 my $debit_3 = $account->add_debit({ amount => 3, interface => 'commandline', type => 'OVERDUE' });
649 is( $account->balance(), 2, "Account balance is 2" );
650 is( $account->outstanding_debits->total_outstanding, 6, 'Outstanding debits sum 6' );
651 is( $account->outstanding_credits->total_outstanding, -4, 'Outstanding credits sum -4' );
653 $account->reconcile_balance();
655 is( $account->balance(), 2, "Account balance is 2" );
656 is( $account->outstanding_debits->total_outstanding, 2, 'Outstanding debits sum 2' );
657 is( $account->outstanding_credits->total_outstanding, 0, 'Outstanding credits sum 0' );
659 $debit_1->discard_changes;
660 is( $debit_1->amountoutstanding + 0, 0, 'Old debit payed' );
661 $debit_2->discard_changes;
662 is( $debit_2->amountoutstanding + 0, 0, 'Old debit payed' );
663 $debit_3->discard_changes;
664 is( $debit_3->amountoutstanding + 0, 2, 'Newest debit only partially payed' );
666 $schema->storage->txn_rollback;
670 subtest 'pay() tests' => sub {
674 $schema->storage->txn_begin;
676 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
677 my $library = $builder->build_object({ class => 'Koha::Libraries' });
678 my $account = $patron->account;
680 my $context = Test::MockModule->new('C4::Context');
681 $context->mock( 'userenv', { branch => $library->id } );
683 my $credit_1_id = $account->pay({ amount => 200 });
684 my $credit_1 = Koha::Account::Lines->find( $credit_1_id );
686 is( $credit_1->branchcode, undef, 'No branchcode is set if library_id was not passed' );
688 my $credit_2_id = $account->pay({ amount => 150, library_id => $library->id });
689 my $credit_2 = Koha::Account::Lines->find( $credit_2_id );
691 is( $credit_2->branchcode, $library->id, 'branchcode set because library_id was passed' );
693 # Enable cash registers
694 t::lib::Mocks::mock_preference( 'UseCashRegisters', 1 );
699 payment_type => 'CASH',
700 interface => 'intranet'
704 'Koha::Exceptions::Account::RegisterRequired',
705 'Exception thrown for UseCashRegisters:1 + payment_type:CASH + cash_register:undef';
707 # Disable cash registers
708 t::lib::Mocks::mock_preference( 'UseCashRegisters', 1 );
710 $schema->storage->txn_rollback;
713 subtest 'pay() handles lost items when paying a specific lost fee' => sub {
717 $schema->storage->txn_begin;
719 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
720 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
721 my $account = $patron->account;
723 my $context = Test::MockModule->new('C4::Context');
724 $context->mock( 'userenv', { branch => $library->id } );
726 my $biblio = $builder->build_sample_biblio();
728 $builder->build_sample_item( { biblionumber => $biblio->biblionumber } );
730 my $checkout = Koha::Checkout->new(
732 borrowernumber => $patron->id,
733 itemnumber => $item->id,
734 date_due => \'NOW()',
735 branchcode => $patron->branchcode,
736 issuedate => \'NOW()',
740 $item->itemlost('1')->store();
742 my $accountline = Koha::Account::Line->new(
744 issue_id => $checkout->id,
745 borrowernumber => $patron->id,
746 itemnumber => $item->id,
748 debit_type_code => 'LOST',
751 amountoutstanding => '1',
757 amount => "0.500000",
758 library_id => $library->id,
759 lines => [$accountline],
763 $accountline = Koha::Account::Lines->find( $accountline->id );
764 is( $accountline->amountoutstanding, '0.500000', 'Account line was paid down by half' );
766 $checkout = Koha::Checkouts->find( $checkout->id );
767 ok( $checkout, 'Item still checked out to patron' );
771 amount => "0.500000",
772 library_id => $library->id,
773 lines => [$accountline],
777 $accountline = Koha::Account::Lines->find( $accountline->id );
778 is( $accountline->amountoutstanding, '0.000000', 'Account line was paid down by half' );
780 $checkout = Koha::Checkouts->find( $checkout->id );
781 ok( !$checkout, 'Item was removed from patron account' );
783 $schema->storage->txn_rollback;
786 subtest 'pay() handles lost items when paying by amount ( not specifying the lost fee )' => sub {
790 $schema->storage->txn_begin;
792 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
793 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
794 my $account = $patron->account;
796 my $context = Test::MockModule->new('C4::Context');
797 $context->mock( 'userenv', { branch => $library->id } );
799 my $biblio = $builder->build_sample_biblio();
801 $builder->build_sample_item( { biblionumber => $biblio->biblionumber } );
803 my $checkout = Koha::Checkout->new(
805 borrowernumber => $patron->id,
806 itemnumber => $item->id,
807 date_due => \'NOW()',
808 branchcode => $patron->branchcode,
809 issuedate => \'NOW()',
813 $item->itemlost('1')->store();
815 my $accountline = Koha::Account::Line->new(
817 issue_id => $checkout->id,
818 borrowernumber => $patron->id,
819 itemnumber => $item->id,
821 debit_type_code => 'LOST',
824 amountoutstanding => '1',
830 amount => "0.500000",
831 library_id => $library->id,
835 $accountline = Koha::Account::Lines->find( $accountline->id );
836 is( $accountline->amountoutstanding, '0.500000', 'Account line was paid down by half' );
838 $checkout = Koha::Checkouts->find( $checkout->id );
839 ok( $checkout, 'Item still checked out to patron' );
843 amount => "0.500000",
844 library_id => $library->id,
848 $accountline = Koha::Account::Lines->find( $accountline->id );
849 is( $accountline->amountoutstanding, '0.000000', 'Account line was paid down by half' );
851 $checkout = Koha::Checkouts->find( $checkout->id );
852 ok( !$checkout, 'Item was removed from patron account' );
854 $schema->storage->txn_rollback;
857 subtest 'Koha::Account::Line::apply() handles lost items' => sub {
861 $schema->storage->txn_begin;
863 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
864 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
865 my $account = $patron->account;
867 my $context = Test::MockModule->new('C4::Context');
868 $context->mock( 'userenv', { branch => $library->id } );
870 my $biblio = $builder->build_sample_biblio();
872 $builder->build_sample_item( { biblionumber => $biblio->biblionumber } );
874 my $checkout = Koha::Checkout->new(
876 borrowernumber => $patron->id,
877 itemnumber => $item->id,
878 date_due => \'NOW()',
879 branchcode => $patron->branchcode,
880 issuedate => \'NOW()',
884 $item->itemlost('1')->store();
886 my $debit = Koha::Account::Line->new(
888 issue_id => $checkout->id,
889 borrowernumber => $patron->id,
890 itemnumber => $item->id,
892 debit_type_code => 'LOST',
895 amountoutstanding => '1',
899 my $credit = Koha::Account::Line->new(
901 borrowernumber => $patron->id,
902 date => '1900-01-01',
903 amount => "-0.500000",
904 amountoutstanding => "-0.500000",
905 interface => 'commandline',
909 my $debits = $account->outstanding_debits;
910 $credit->apply({ debits => [ $debits->as_list ] });
912 $debit = Koha::Account::Lines->find( $debit->id );
913 is( $debit->amountoutstanding, '0.500000', 'Account line was paid down by half' );
915 $checkout = Koha::Checkouts->find( $checkout->id );
916 ok( $checkout, 'Item still checked out to patron' );
918 $credit = Koha::Account::Line->new(
920 borrowernumber => $patron->id,
921 date => '1900-01-01',
922 amount => "-0.500000",
923 amountoutstanding => "-0.500000",
924 interface => 'commandline',
928 $debits = $account->outstanding_debits;
929 $credit->apply({ debits => [ $debits->as_list ] });
931 $debit = Koha::Account::Lines->find( $debit->id );
932 is( $debit->amountoutstanding, '0.000000', 'Account line was paid down by half' );
934 $checkout = Koha::Checkouts->find( $checkout->id );
935 ok( !$checkout, 'Item was removed from patron account' );
937 $schema->storage->txn_rollback;