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 my $builder = t::lib::TestBuilder->new;
36 C4::Context->interface('commandline');
38 subtest 'new' => sub {
42 $schema->storage->txn_begin;
44 throws_ok { Koha::Account->new(); } qr/No patron id passed in!/, 'Croaked on bad call to new';
46 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
47 my $account = Koha::Account->new( { patron_id => $patron->borrowernumber } );
48 is( defined $account, 1, "Account is defined" );
50 $schema->storage->txn_rollback;
53 subtest 'outstanding_debits() tests' => sub {
57 $schema->storage->txn_begin;
59 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
60 my $account = $patron->account;
63 push @generated_lines, $account->add_debit({ amount => 1, interface => 'commandline', type => 'overdue' });
64 push @generated_lines, $account->add_debit({ amount => 2, interface => 'commandline', type => 'overdue' });
65 push @generated_lines, $account->add_debit({ amount => 3, interface => 'commandline', type => 'overdue' });
66 push @generated_lines, $account->add_debit({ amount => 4, interface => 'commandline', type => 'overdue' });
68 my $lines = $account->outstanding_debits();
69 my @lines_arr = $account->outstanding_debits();
71 is( ref($lines), 'Koha::Account::Lines', 'Called in scalar context, outstanding_debits returns a Koha::Account::Lines object' );
72 is( $lines->total_outstanding, 10, 'Outstandig debits total is correctly calculated' );
75 foreach my $line ( @{ $lines->as_list } ) {
76 my $fetched_line = Koha::Account::Lines->find( $generated_lines[$i]->id );
77 is_deeply( $line->unblessed, $fetched_line->unblessed, "Fetched line matches the generated one ($i)" );
78 is_deeply( $lines_arr[$i]->unblessed, $fetched_line->unblessed, "Fetched line matches the generated one ($i)" );
79 is( ref($lines_arr[$i]), 'Koha::Account::Line', 'outstanding_debits returns a list of Koha::Account::Line objects in list context' );
82 my $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
83 Koha::Account::Line->new({ borrowernumber => $patron_2->id, amountoutstanding => -2, interface => 'commandline' })->store;
84 my $just_one = Koha::Account::Line->new({ borrowernumber => $patron_2->id, amount => 3, amountoutstanding => 3, interface => 'commandline' })->store;
85 Koha::Account::Line->new({ borrowernumber => $patron_2->id, amount => -6, amountoutstanding => -6, interface => 'commandline' })->store;
86 $lines = $patron_2->account->outstanding_debits();
87 is( $lines->total_outstanding, 3, "Total if some outstanding debits and some credits is only debits" );
88 is( $lines->count, 1, "With 1 outstanding debits, we get back a Lines object with 1 lines" );
89 my $the_line = Koha::Account::Lines->find( $just_one->id );
90 is_deeply( $the_line->unblessed, $lines->next->unblessed, "We get back the one correct line");
92 my $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
93 my $account_3 = $patron_3->account;
94 $account_3->add_credit( { amount => 2, interface => 'commandline' } );
95 $account_3->add_credit( { amount => 20, interface => 'commandline' } );
96 $account_3->add_credit( { amount => 200, interface => 'commandline' } );
97 $lines = $account_3->outstanding_debits();
98 is( $lines->total_outstanding, 0, "Total if no outstanding debits total is 0" );
99 is( $lines->count, 0, "With 0 outstanding debits, we get back a Lines object with 0 lines" );
101 my $patron_4 = $builder->build_object({ class => 'Koha::Patrons' });
102 my $account_4 = $patron_4->account;
103 $lines = $account_4->outstanding_debits();
104 is( $lines->total_outstanding, 0, "Total if no outstanding debits is 0" );
105 is( $lines->count, 0, "With no outstanding debits, we get back a Lines object with 0 lines" );
107 # create a pathological credit with amountoutstanding > 0 (BZ 14591)
108 Koha::Account::Line->new({ borrowernumber => $patron_4->id, amount => -3, amountoutstanding => 3, interface => 'commandline' })->store();
109 $lines = $account_4->outstanding_debits();
110 is( $lines->count, 0, 'No credits are confused with debits because of the amountoutstanding value' );
112 $schema->storage->txn_rollback;
115 subtest 'outstanding_credits() tests' => sub {
119 $schema->storage->txn_begin;
121 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
122 my $account = $patron->account;
125 push @generated_lines, $account->add_credit({ amount => 1, interface => 'commandline' });
126 push @generated_lines, $account->add_credit({ amount => 2, interface => 'commandline' });
127 push @generated_lines, $account->add_credit({ amount => 3, interface => 'commandline' });
128 push @generated_lines, $account->add_credit({ amount => 4, interface => 'commandline' });
130 my $lines = $account->outstanding_credits();
131 my @lines_arr = $account->outstanding_credits();
133 is( ref($lines), 'Koha::Account::Lines', 'Called in scalar context, outstanding_credits returns a Koha::Account::Lines object' );
134 is( $lines->total_outstanding, -10, 'Outstandig credits total is correctly calculated' );
137 foreach my $line ( @{ $lines->as_list } ) {
138 my $fetched_line = Koha::Account::Lines->find( $generated_lines[$i]->id );
139 is_deeply( $line->unblessed, $fetched_line->unblessed, "Fetched line matches the generated one ($i)" );
140 is_deeply( $lines_arr[$i]->unblessed, $fetched_line->unblessed, "Fetched line matches the generated one ($i)" );
141 is( ref($lines_arr[$i]), 'Koha::Account::Line', 'outstanding_debits returns a list of Koha::Account::Line objects in list context' );
145 my $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
146 $account = $patron_2->account;
147 $lines = $account->outstanding_credits();
148 is( $lines->total_outstanding, 0, "Total if no outstanding credits is 0" );
149 is( $lines->count, 0, "With no outstanding credits, we get back a Lines object with 0 lines" );
151 # create a pathological debit with amountoutstanding < 0 (BZ 14591)
152 Koha::Account::Line->new({ borrowernumber => $patron_2->id, amount => 2, amountoutstanding => -3, interface => 'commandline' })->store();
153 $lines = $account->outstanding_credits();
154 is( $lines->count, 0, 'No debits are confused with credits because of the amountoutstanding value' );
156 $schema->storage->txn_rollback;
159 subtest 'add_credit() tests' => sub {
163 $schema->storage->txn_begin;
165 # delete logs and statistics
166 my $action_logs = $schema->resultset('ActionLog')->search()->count;
167 my $statistics = $schema->resultset('Statistic')->search()->count;
169 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
170 my $account = Koha::Account->new( { patron_id => $patron->borrowernumber } );
172 is( $account->balance, 0, 'Patron has no balance' );
175 t::lib::Mocks::mock_preference( 'FinesLog', 0 );
178 $account->add_credit(
180 description => 'Payment of 25',
181 library_id => $patron->branchcode,
182 note => 'not really important',
184 user_id => $patron->id
188 'Koha::Exceptions::MissingParameter', 'Exception thrown if interface parameter missing';
190 my $line_1 = $account->add_credit(
192 description => 'Payment of 25',
193 library_id => $patron->branchcode,
194 note => 'not really important',
196 user_id => $patron->id,
197 interface => 'commandline'
201 is( $account->balance, -25, 'Patron has a balance of -25' );
202 is( $schema->resultset('ActionLog')->count(), $action_logs + 0, 'No log was added' );
203 is( $schema->resultset('Statistic')->count(), $statistics + 1, 'Action added to statistics' );
204 is( $line_1->accounttype, $Koha::Account::account_type_credit->{'payment'}, 'Account type is correctly set' );
207 t::lib::Mocks::mock_preference( 'FinesLog', 1 );
209 my $line_2 = $account->add_credit(
211 description => 'Payment of 37',
212 library_id => $patron->branchcode,
213 note => 'not really important',
214 user_id => $patron->id,
215 interface => 'commandline'
219 is( $account->balance, -62, 'Patron has a balance of -25' );
220 is( $schema->resultset('ActionLog')->count(), $action_logs + 1, 'Log was added' );
221 is( $schema->resultset('Statistic')->count(), $statistics + 2, 'Action added to statistics' );
222 is( $line_2->accounttype, $Koha::Account::account_type_credit->{'payment'}, 'Account type is correctly set' );
224 # offsets have the credit_id set to accountlines_id, and debit_id is undef
225 my $offset_1 = Koha::Account::Offsets->search({ credit_id => $line_1->id })->next;
226 my $offset_2 = Koha::Account::Offsets->search({ credit_id => $line_2->id })->next;
228 is( $offset_1->credit_id, $line_1->id, 'No debit_id is set for credits' );
229 is( $offset_1->debit_id, undef, 'No debit_id is set for credits' );
230 is( $offset_2->credit_id, $line_2->id, 'No debit_id is set for credits' );
231 is( $offset_2->debit_id, undef, 'No debit_id is set for credits' );
233 my $line_3 = $account->add_credit(
235 description => 'Manual credit applied',
236 library_id => $patron->branchcode,
237 user_id => $patron->id,
239 interface => 'commandline'
243 is( $schema->resultset('ActionLog')->count(), $action_logs + 2, 'Log was added' );
244 is( $schema->resultset('Statistic')->count(), $statistics + 2, 'No action added to statistics, because of credit type' );
246 $schema->storage->txn_rollback;
249 subtest 'add_debit() tests' => sub {
253 $schema->storage->txn_begin;
255 # delete logs and statistics
256 my $action_logs = $schema->resultset('ActionLog')->search()->count;
257 my $statistics = $schema->resultset('Statistic')->search()->count;
259 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
261 Koha::Account->new( { patron_id => $patron->borrowernumber } );
263 is( $account->balance, 0, 'Patron has no balance' );
269 description => 'amount validation failure',
270 library_id => $patron->branchcode,
271 note => 'this should fail anyway',
273 user_id => $patron->id,
274 interface => 'commandline'
276 ); } 'Koha::Exceptions::Account::AmountNotPositive', 'Expected validation exception thrown (amount)';
282 description => 'type validation failure',
283 library_id => $patron->branchcode,
284 note => 'this should fail anyway',
286 user_id => $patron->id,
287 interface => 'commandline'
289 ); } 'Koha::Exceptions::Account::UnrecognisedType', 'Expected validation exception thrown (type)';
295 description => 'Rental charge of 25',
296 library_id => $patron->branchcode,
297 note => 'not really important',
299 user_id => $patron->id
301 ); } 'Koha::Exceptions::MissingParameter', 'Exception thrown if interface parameter missing';
304 t::lib::Mocks::mock_preference( 'FinesLog', 0 );
306 my $line_1 = $account->add_debit(
309 description => 'Rental charge of 25',
310 library_id => $patron->branchcode,
311 note => 'not really important',
313 user_id => $patron->id,
314 interface => 'commandline'
318 is( $account->balance, 25, 'Patron has a balance of 25' );
320 $schema->resultset('ActionLog')->count(),
325 $line_1->accounttype,
326 $Koha::Account::account_type_debit->{'rent'},
327 'Account type is correctly set'
331 t::lib::Mocks::mock_preference( 'FinesLog', 1 );
333 my $line_2 = $account->add_debit(
336 description => 'Rental charge of 37',
337 library_id => $patron->branchcode,
338 note => 'not really important',
340 user_id => $patron->id,
341 interface => 'commandline'
345 is( $account->balance, 62, 'Patron has a balance of 62' );
347 $schema->resultset('ActionLog')->count(),
352 $line_2->accounttype,
353 $Koha::Account::account_type_debit->{'rent'},
354 'Account type is correctly set'
357 # offsets have the debit_id set to accountlines_id, and credit_id is undef
359 Koha::Account::Offsets->search( { debit_id => $line_1->id } )->next;
361 Koha::Account::Offsets->search( { debit_id => $line_2->id } )->next;
363 is( $offset_1->debit_id, $line_1->id, 'debit_id is set for debit 1' );
364 is( $offset_1->credit_id, undef, 'credit_id is not set for debit 1' );
365 is( $offset_2->debit_id, $line_2->id, 'debit_id is set for debit 2' );
366 is( $offset_2->credit_id, undef, 'credit_id is not set for debit 2' );
368 $schema->storage->txn_rollback;
371 subtest 'lines() tests' => sub {
375 $schema->storage->txn_begin;
377 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
378 my $account = $patron->account;
381 $account->add_credit({ amount => 1, interface => 'commandline' });
382 $account->add_credit({ amount => 2, interface => 'commandline' });
383 $account->add_credit({ amount => 3, interface => 'commandline' });
384 $account->add_credit({ amount => 4, interface => 'commandline' });
387 $account->add_debit({ amount => 1, interface => 'commandline', type => 'overdue' });
388 $account->add_debit({ amount => 2, interface => 'commandline', type => 'overdue' });
389 $account->add_debit({ amount => 3, interface => 'commandline', type => 'overdue' });
390 $account->add_debit({ amount => 4, interface => 'commandline', type => 'overdue' });
393 $account->add_credit( { amount => 1, interface => 'commandline' } )
394 ->apply( { debits => [ $account->outstanding_debits->as_list ] } );
396 my $lines = $account->lines;
397 is( $lines->_resultset->count, 9, "All accountlines (debits, credits and paid off) were fetched");
399 $schema->storage->txn_rollback;
402 subtest 'reconcile_balance' => sub {
406 subtest 'more credit than debit' => sub {
410 $schema->storage->txn_begin;
412 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
413 my $account = $patron->account;
416 $account->add_credit({ amount => 1, interface => 'commandline' });
417 $account->add_credit({ amount => 2, interface => 'commandline' });
418 $account->add_credit({ amount => 3, interface => 'commandline' });
419 $account->add_credit({ amount => 4, interface => 'commandline' });
420 $account->add_credit({ amount => 5, interface => 'commandline' });
423 $account->add_debit({ amount => 1, interface => 'commandline', type => 'overdue' });
424 $account->add_debit({ amount => 2, interface => 'commandline', type => 'overdue' });
425 $account->add_debit({ amount => 3, interface => 'commandline', type => 'overdue' });
426 $account->add_debit({ amount => 4, interface => 'commandline', type => 'overdue' });
429 Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 1, amountoutstanding => 0, interface => 'commandline' })->store;
430 Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 1, amountoutstanding => 0, interface => 'commandline' })->store;
432 is( $account->balance(), -5, "Account balance is -5" );
433 is( $account->outstanding_debits->total_outstanding, 10, 'Outstanding debits sum 10' );
434 is( $account->outstanding_credits->total_outstanding, -15, 'Outstanding credits sum -15' );
436 $account->reconcile_balance();
438 is( $account->balance(), -5, "Account balance is -5" );
439 is( $account->outstanding_debits->total_outstanding, 0, 'No outstanding debits' );
440 is( $account->outstanding_credits->total_outstanding, -5, 'Outstanding credits sum -5' );
442 $schema->storage->txn_rollback;
445 subtest 'same debit as credit' => sub {
449 $schema->storage->txn_begin;
451 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
452 my $account = $patron->account;
455 $account->add_credit({ amount => 1, interface => 'commandline' });
456 $account->add_credit({ amount => 2, interface => 'commandline' });
457 $account->add_credit({ amount => 3, interface => 'commandline' });
458 $account->add_credit({ amount => 4, interface => 'commandline' });
461 $account->add_debit({ amount => 1, interface => 'commandline', type => 'overdue' });
462 $account->add_debit({ amount => 2, interface => 'commandline', type => 'overdue' });
463 $account->add_debit({ amount => 3, interface => 'commandline', type => 'overdue' });
464 $account->add_debit({ amount => 4, interface => 'commandline', type => 'overdue' });
467 Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 1, amountoutstanding => 0, interface => 'commandline' })->store;
468 Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 1, amountoutstanding => 0, interface => 'commandline' })->store;
470 is( $account->balance(), 0, "Account balance is 0" );
471 is( $account->outstanding_debits->total_outstanding, 10, 'Outstanding debits sum 10' );
472 is( $account->outstanding_credits->total_outstanding, -10, 'Outstanding credits sum -10' );
474 $account->reconcile_balance();
476 is( $account->balance(), 0, "Account balance is 0" );
477 is( $account->outstanding_debits->total_outstanding, 0, 'No outstanding debits' );
478 is( $account->outstanding_credits->total_outstanding, 0, 'Outstanding credits sum 0' );
480 $schema->storage->txn_rollback;
483 subtest 'more debit than credit' => sub {
487 $schema->storage->txn_begin;
489 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
490 my $account = $patron->account;
493 $account->add_credit({ amount => 1, interface => 'commandline' });
494 $account->add_credit({ amount => 2, interface => 'commandline' });
495 $account->add_credit({ amount => 3, interface => 'commandline' });
496 $account->add_credit({ amount => 4, 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' });
503 $account->add_debit({ amount => 5, interface => 'commandline', type => 'overdue' });
506 Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 1, amountoutstanding => 0, interface => 'commandline' })->store;
507 Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 1, amountoutstanding => 0, interface => 'commandline' })->store;
509 is( $account->balance(), 5, "Account balance is 5" );
510 is( $account->outstanding_debits->total_outstanding, 15, 'Outstanding debits sum 15' );
511 is( $account->outstanding_credits->total_outstanding, -10, 'Outstanding credits sum -10' );
513 $account->reconcile_balance();
515 is( $account->balance(), 5, "Account balance is 5" );
516 is( $account->outstanding_debits->total_outstanding, 5, 'Outstanding debits sum 5' );
517 is( $account->outstanding_credits->total_outstanding, 0, 'Outstanding credits sum 0' );
519 $schema->storage->txn_rollback;
522 subtest 'credits are applied to older debits first' => 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 => 3, interface => 'commandline' });
536 my $debit_1 = $account->add_debit({ amount => 1, interface => 'commandline', type => 'overdue' });
537 my $debit_2 = $account->add_debit({ amount => 2, interface => 'commandline', type => 'overdue' });
538 my $debit_3 = $account->add_debit({ amount => 3, interface => 'commandline', type => 'overdue' });
540 is( $account->balance(), 2, "Account balance is 2" );
541 is( $account->outstanding_debits->total_outstanding, 6, 'Outstanding debits sum 6' );
542 is( $account->outstanding_credits->total_outstanding, -4, 'Outstanding credits sum -4' );
544 $account->reconcile_balance();
546 is( $account->balance(), 2, "Account balance is 2" );
547 is( $account->outstanding_debits->total_outstanding, 2, 'Outstanding debits sum 2' );
548 is( $account->outstanding_credits->total_outstanding, 0, 'Outstanding credits sum 0' );
550 $debit_1->discard_changes;
551 is( $debit_1->amountoutstanding + 0, 0, 'Old debit payed' );
552 $debit_2->discard_changes;
553 is( $debit_2->amountoutstanding + 0, 0, 'Old debit payed' );
554 $debit_3->discard_changes;
555 is( $debit_3->amountoutstanding + 0, 2, 'Newest debit only partially payed' );
557 $schema->storage->txn_rollback;
561 subtest 'pay() tests' => sub {
565 $schema->storage->txn_begin;
567 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
568 my $library = $builder->build_object({ class => 'Koha::Libraries' });
569 my $account = $patron->account;
571 my $context = Test::MockModule->new('C4::Context');
572 $context->mock( 'userenv', { branch => $library->id } );
574 my $credit_1_id = $account->pay({ amount => 200 });
575 my $credit_1 = Koha::Account::Lines->find( $credit_1_id );
577 is( $credit_1->branchcode, undef, 'No branchcode is set if library_id was not passed' );
579 my $credit_2_id = $account->pay({ amount => 150, library_id => $library->id });
580 my $credit_2 = Koha::Account::Lines->find( $credit_2_id );
582 is( $credit_2->branchcode, $library->id, 'branchcode set because library_id was passed' );
584 $schema->storage->txn_rollback;
587 subtest 'pay() handles lost items when paying a specific lost fee' => sub {
591 $schema->storage->txn_begin;
593 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
594 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
595 my $account = $patron->account;
597 my $context = Test::MockModule->new('C4::Context');
598 $context->mock( 'userenv', { branch => $library->id } );
600 my $biblio = $builder->build_sample_biblio();
602 $builder->build_sample_item( { biblionumber => $biblio->biblionumber } );
604 my $checkout = Koha::Checkout->new(
606 borrowernumber => $patron->id,
607 itemnumber => $item->id,
608 date_due => \'NOW()',
609 branchcode => $patron->branchcode,
610 issuedate => \'NOW()',
614 $item->itemlost('1')->store();
616 my $accountline = Koha::Account::Line->new(
618 issue_id => $checkout->id,
619 borrowernumber => $patron->id,
620 itemnumber => $item->id,
622 accounttype => 'LOST',
625 amountoutstanding => '1',
631 amount => "0.500000",
632 library_id => $library->id,
633 lines => [$accountline],
637 $accountline = Koha::Account::Lines->find( $accountline->id );
638 is( $accountline->amountoutstanding, '0.500000', 'Account line was paid down by half' );
640 $checkout = Koha::Checkouts->find( $checkout->id );
641 ok( $checkout, 'Item still checked out to patron' );
645 amount => "0.500000",
646 library_id => $library->id,
647 lines => [$accountline],
651 $accountline = Koha::Account::Lines->find( $accountline->id );
652 is( $accountline->amountoutstanding, '0.000000', 'Account line was paid down by half' );
654 $checkout = Koha::Checkouts->find( $checkout->id );
655 ok( !$checkout, 'Item was removed from patron account' );
657 $schema->storage->txn_rollback;
660 subtest 'pay() handles lost items when paying by amount ( not specifying the lost fee )' => sub {
664 $schema->storage->txn_begin;
666 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
667 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
668 my $account = $patron->account;
670 my $context = Test::MockModule->new('C4::Context');
671 $context->mock( 'userenv', { branch => $library->id } );
673 my $biblio = $builder->build_sample_biblio();
675 $builder->build_sample_item( { biblionumber => $biblio->biblionumber } );
677 my $checkout = Koha::Checkout->new(
679 borrowernumber => $patron->id,
680 itemnumber => $item->id,
681 date_due => \'NOW()',
682 branchcode => $patron->branchcode,
683 issuedate => \'NOW()',
687 $item->itemlost('1')->store();
689 my $accountline = Koha::Account::Line->new(
691 issue_id => $checkout->id,
692 borrowernumber => $patron->id,
693 itemnumber => $item->id,
695 accounttype => 'LOST',
698 amountoutstanding => '1',
704 amount => "0.500000",
705 library_id => $library->id,
709 $accountline = Koha::Account::Lines->find( $accountline->id );
710 is( $accountline->amountoutstanding, '0.500000', 'Account line was paid down by half' );
712 $checkout = Koha::Checkouts->find( $checkout->id );
713 ok( $checkout, 'Item still checked out to patron' );
717 amount => "0.500000",
718 library_id => $library->id,
722 $accountline = Koha::Account::Lines->find( $accountline->id );
723 is( $accountline->amountoutstanding, '0.000000', 'Account line was paid down by half' );
725 $checkout = Koha::Checkouts->find( $checkout->id );
726 ok( !$checkout, 'Item was removed from patron account' );
728 $schema->storage->txn_rollback;
731 subtest 'Koha::Account::Line::apply() handles lost items' => sub {
735 $schema->storage->txn_begin;
737 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
738 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
739 my $account = $patron->account;
741 my $context = Test::MockModule->new('C4::Context');
742 $context->mock( 'userenv', { branch => $library->id } );
744 my $biblio = $builder->build_sample_biblio();
746 $builder->build_sample_item( { biblionumber => $biblio->biblionumber } );
748 my $checkout = Koha::Checkout->new(
750 borrowernumber => $patron->id,
751 itemnumber => $item->id,
752 date_due => \'NOW()',
753 branchcode => $patron->branchcode,
754 issuedate => \'NOW()',
758 $item->itemlost('1')->store();
760 my $debit = Koha::Account::Line->new(
762 issue_id => $checkout->id,
763 borrowernumber => $patron->id,
764 itemnumber => $item->id,
766 accounttype => 'LOST',
769 amountoutstanding => '1',
773 my $credit = Koha::Account::Line->new(
775 borrowernumber => $patron->id,
776 date => '1900-01-01',
777 amount => "-0.500000",
778 amountoutstanding => "-0.500000",
779 interface => 'commandline'
782 my $debits = $account->outstanding_debits;
783 $credit->apply({ debits => [ $debits->as_list ] });
785 $debit = Koha::Account::Lines->find( $debit->id );
786 is( $debit->amountoutstanding, '0.500000', 'Account line was paid down by half' );
788 $checkout = Koha::Checkouts->find( $checkout->id );
789 ok( $checkout, 'Item still checked out to patron' );
791 $credit = Koha::Account::Line->new(
793 borrowernumber => $patron->id,
794 date => '1900-01-01',
795 amount => "-0.500000",
796 amountoutstanding => "-0.500000",
797 interface => 'commandline'
800 $debits = $account->outstanding_debits;
801 $credit->apply({ debits => [ $debits->as_list ] });
803 $debit = Koha::Account::Lines->find( $debit->id );
804 is( $debit->amountoutstanding, '0.000000', 'Account line was paid down by half' );
806 $checkout = Koha::Checkouts->find( $checkout->id );
807 ok( !$checkout, 'Item was removed from patron account' );
809 $schema->storage->txn_rollback;