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 => 8;
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 => 'fine' });
64 push @generated_lines, $account->add_debit({ amount => 2, interface => 'commandline', type => 'fine' });
65 push @generated_lines, $account->add_debit({ amount => 3, interface => 'commandline', type => 'fine' });
66 push @generated_lines, $account->add_debit({ amount => 4, interface => 'commandline', type => 'fine' });
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 );
210 my $line_2 = $account->add_credit(
212 description => 'Payment of 37',
213 library_id => $patron->branchcode,
214 note => 'not really important',
215 user_id => $patron->id,
217 interface => 'commandline'
221 is( $account->balance, -62, 'Patron has a balance of -25' );
222 is( $schema->resultset('ActionLog')->count(), $action_logs + 1, 'Log was added' );
223 is( $schema->resultset('Statistic')->count(), $statistics + 2, 'Action added to statistics' );
224 is( $line_2->accounttype, $Koha::Account::account_type_credit->{'payment'} . $sip_code, 'Account type is correctly set' );
226 # offsets have the credit_id set to accountlines_id, and debit_id is undef
227 my $offset_1 = Koha::Account::Offsets->search({ credit_id => $line_1->id })->next;
228 my $offset_2 = Koha::Account::Offsets->search({ credit_id => $line_2->id })->next;
230 is( $offset_1->credit_id, $line_1->id, 'No debit_id is set for credits' );
231 is( $offset_1->debit_id, undef, 'No debit_id is set for credits' );
232 is( $offset_2->credit_id, $line_2->id, 'No debit_id is set for credits' );
233 is( $offset_2->debit_id, undef, 'No debit_id is set for credits' );
235 my $line_3 = $account->add_credit(
237 description => 'Manual credit applied',
238 library_id => $patron->branchcode,
239 user_id => $patron->id,
241 interface => 'commandline'
245 is( $schema->resultset('ActionLog')->count(), $action_logs + 2, 'Log was added' );
246 is( $schema->resultset('Statistic')->count(), $statistics + 2, 'No action added to statistics, because of credit type' );
248 $schema->storage->txn_rollback;
251 subtest 'add_debit() tests' => sub {
255 $schema->storage->txn_begin;
257 # delete logs and statistics
258 my $action_logs = $schema->resultset('ActionLog')->search()->count;
259 my $statistics = $schema->resultset('Statistic')->search()->count;
261 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
263 Koha::Account->new( { patron_id => $patron->borrowernumber } );
265 is( $account->balance, 0, 'Patron has no balance' );
271 description => 'amount validation failure',
272 library_id => $patron->branchcode,
273 note => 'this should fail anyway',
275 user_id => $patron->id,
276 interface => 'commandline'
278 ); } 'Koha::Exceptions::Account::AmountNotPositive', 'Expected validation exception thrown (amount)';
284 description => 'type validation failure',
285 library_id => $patron->branchcode,
286 note => 'this should fail anyway',
288 user_id => $patron->id,
289 interface => 'commandline'
291 ); } 'Koha::Exceptions::Account::UnrecognisedType', 'Expected validation exception thrown (type)';
297 description => 'Rental charge of 25',
298 library_id => $patron->branchcode,
299 note => 'not really important',
301 user_id => $patron->id
303 ); } 'Koha::Exceptions::MissingParameter', 'Exception thrown if interface parameter missing';
306 t::lib::Mocks::mock_preference( 'FinesLog', 0 );
308 my $line_1 = $account->add_debit(
311 description => 'Rental charge of 25',
312 library_id => $patron->branchcode,
313 note => 'not really important',
315 user_id => $patron->id,
316 interface => 'commandline'
320 is( $account->balance, 25, 'Patron has a balance of 25' );
322 $schema->resultset('ActionLog')->count(),
327 $line_1->accounttype,
328 $Koha::Account::account_type_debit->{'rent'},
329 'Account type is correctly set'
333 t::lib::Mocks::mock_preference( 'FinesLog', 1 );
336 my $line_2 = $account->add_debit(
339 description => 'Rental charge of 37',
340 library_id => $patron->branchcode,
341 note => 'not really important',
343 user_id => $patron->id,
344 interface => 'commandline'
348 is( $account->balance, 62, 'Patron has a balance of 62' );
350 $schema->resultset('ActionLog')->count(),
355 $line_2->accounttype,
356 $Koha::Account::account_type_debit->{'rent'},
357 'Account type is correctly set'
360 # offsets have the debit_id set to accountlines_id, and credit_id is undef
362 Koha::Account::Offsets->search( { debit_id => $line_1->id } )->next;
364 Koha::Account::Offsets->search( { debit_id => $line_2->id } )->next;
366 is( $offset_1->debit_id, $line_1->id, 'debit_id is set for debit 1' );
367 is( $offset_1->credit_id, undef, 'credit_id is not set for debit 1' );
368 is( $offset_2->debit_id, $line_2->id, 'debit_id is set for debit 2' );
369 is( $offset_2->credit_id, undef, 'credit_id is not set for debit 2' );
371 $schema->storage->txn_rollback;
374 subtest 'lines() tests' => sub {
378 $schema->storage->txn_begin;
380 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
381 my $account = $patron->account;
384 $account->add_credit({ amount => 1, interface => 'commandline' });
385 $account->add_credit({ amount => 2, interface => 'commandline' });
386 $account->add_credit({ amount => 3, interface => 'commandline' });
387 $account->add_credit({ amount => 4, interface => 'commandline' });
390 $account->add_debit({ amount => 1, interface => 'commandline', type => 'fine' });
391 $account->add_debit({ amount => 2, interface => 'commandline', type => 'fine' });
392 $account->add_debit({ amount => 3, interface => 'commandline', type => 'fine' });
393 $account->add_debit({ amount => 4, interface => 'commandline', type => 'fine' });
396 $account->add_credit( { amount => 1, interface => 'commandline' } )
397 ->apply( { debits => scalar $account->outstanding_debits } );
399 my $lines = $account->lines;
400 is( $lines->_resultset->count, 9, "All accountlines (debits, credits and paid off) were fetched");
402 $schema->storage->txn_rollback;
405 subtest 'reconcile_balance' => sub {
409 subtest 'more credit than debit' => sub {
413 $schema->storage->txn_begin;
415 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
416 my $account = $patron->account;
419 $account->add_credit({ amount => 1, interface => 'commandline' });
420 $account->add_credit({ amount => 2, interface => 'commandline' });
421 $account->add_credit({ amount => 3, interface => 'commandline' });
422 $account->add_credit({ amount => 4, interface => 'commandline' });
423 $account->add_credit({ amount => 5, interface => 'commandline' });
426 $account->add_debit({ amount => 1, interface => 'commandline', type => 'fine' });
427 $account->add_debit({ amount => 2, interface => 'commandline', type => 'fine' });
428 $account->add_debit({ amount => 3, interface => 'commandline', type => 'fine' });
429 $account->add_debit({ amount => 4, interface => 'commandline', type => 'fine' });
432 Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 1, amountoutstanding => 0, interface => 'commandline' })->store;
433 Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 1, amountoutstanding => 0, interface => 'commandline' })->store;
435 is( $account->balance(), -5, "Account balance is -5" );
436 is( $account->outstanding_debits->total_outstanding, 10, 'Outstanding debits sum 10' );
437 is( $account->outstanding_credits->total_outstanding, -15, 'Outstanding credits sum -15' );
439 $account->reconcile_balance();
441 is( $account->balance(), -5, "Account balance is -5" );
442 is( $account->outstanding_debits->total_outstanding, 0, 'No outstanding debits' );
443 is( $account->outstanding_credits->total_outstanding, -5, 'Outstanding credits sum -5' );
445 $schema->storage->txn_rollback;
448 subtest 'same debit as credit' => sub {
452 $schema->storage->txn_begin;
454 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
455 my $account = $patron->account;
458 $account->add_credit({ amount => 1, interface => 'commandline' });
459 $account->add_credit({ amount => 2, interface => 'commandline' });
460 $account->add_credit({ amount => 3, interface => 'commandline' });
461 $account->add_credit({ amount => 4, interface => 'commandline' });
464 $account->add_debit({ amount => 1, interface => 'commandline', type => 'fine' });
465 $account->add_debit({ amount => 2, interface => 'commandline', type => 'fine' });
466 $account->add_debit({ amount => 3, interface => 'commandline', type => 'fine' });
467 $account->add_debit({ amount => 4, interface => 'commandline', type => 'fine' });
470 Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 1, amountoutstanding => 0, interface => 'commandline' })->store;
471 Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 1, amountoutstanding => 0, interface => 'commandline' })->store;
473 is( $account->balance(), 0, "Account balance is 0" );
474 is( $account->outstanding_debits->total_outstanding, 10, 'Outstanding debits sum 10' );
475 is( $account->outstanding_credits->total_outstanding, -10, 'Outstanding credits sum -10' );
477 $account->reconcile_balance();
479 is( $account->balance(), 0, "Account balance is 0" );
480 is( $account->outstanding_debits->total_outstanding, 0, 'No outstanding debits' );
481 is( $account->outstanding_credits->total_outstanding, 0, 'Outstanding credits sum 0' );
483 $schema->storage->txn_rollback;
486 subtest 'more debit than credit' => sub {
490 $schema->storage->txn_begin;
492 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
493 my $account = $patron->account;
496 $account->add_credit({ amount => 1, interface => 'commandline' });
497 $account->add_credit({ amount => 2, interface => 'commandline' });
498 $account->add_credit({ amount => 3, interface => 'commandline' });
499 $account->add_credit({ amount => 4, interface => 'commandline' });
502 $account->add_debit({ amount => 1, interface => 'commandline', type => 'fine' });
503 $account->add_debit({ amount => 2, interface => 'commandline', type => 'fine' });
504 $account->add_debit({ amount => 3, interface => 'commandline', type => 'fine' });
505 $account->add_debit({ amount => 4, interface => 'commandline', type => 'fine' });
506 $account->add_debit({ amount => 5, interface => 'commandline', type => 'fine' });
509 Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 1, amountoutstanding => 0, interface => 'commandline' })->store;
510 Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 1, amountoutstanding => 0, interface => 'commandline' })->store;
512 is( $account->balance(), 5, "Account balance is 5" );
513 is( $account->outstanding_debits->total_outstanding, 15, 'Outstanding debits sum 15' );
514 is( $account->outstanding_credits->total_outstanding, -10, 'Outstanding credits sum -10' );
516 $account->reconcile_balance();
518 is( $account->balance(), 5, "Account balance is 5" );
519 is( $account->outstanding_debits->total_outstanding, 5, 'Outstanding debits sum 5' );
520 is( $account->outstanding_credits->total_outstanding, 0, 'Outstanding credits sum 0' );
522 $schema->storage->txn_rollback;
525 subtest 'credits are applied to older debits first' => sub {
529 $schema->storage->txn_begin;
531 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
532 my $account = $patron->account;
535 $account->add_credit({ amount => 1, interface => 'commandline' });
536 $account->add_credit({ amount => 3, interface => 'commandline' });
539 my $debit_1 = $account->add_debit({ amount => 1, interface => 'commandline', type => 'fine' });
540 my $debit_2 = $account->add_debit({ amount => 2, interface => 'commandline', type => 'fine' });
541 my $debit_3 = $account->add_debit({ amount => 3, interface => 'commandline', type => 'fine' });
543 is( $account->balance(), 2, "Account balance is 2" );
544 is( $account->outstanding_debits->total_outstanding, 6, 'Outstanding debits sum 6' );
545 is( $account->outstanding_credits->total_outstanding, -4, 'Outstanding credits sum -4' );
547 $account->reconcile_balance();
549 is( $account->balance(), 2, "Account balance is 2" );
550 is( $account->outstanding_debits->total_outstanding, 2, 'Outstanding debits sum 2' );
551 is( $account->outstanding_credits->total_outstanding, 0, 'Outstanding credits sum 0' );
553 $debit_1->discard_changes;
554 is( $debit_1->amountoutstanding + 0, 0, 'Old debit payed' );
555 $debit_2->discard_changes;
556 is( $debit_2->amountoutstanding + 0, 0, 'Old debit payed' );
557 $debit_3->discard_changes;
558 is( $debit_3->amountoutstanding + 0, 2, 'Newest debit only partially payed' );
560 $schema->storage->txn_rollback;
564 subtest 'pay() tests' => sub {
568 $schema->storage->txn_begin;
570 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
571 my $library = $builder->build_object({ class => 'Koha::Libraries' });
572 my $account = $patron->account;
574 my $context = Test::MockModule->new('C4::Context');
575 $context->mock( 'userenv', { branch => $library->id } );
577 my $credit_1_id = $account->pay({ amount => 200 });
578 my $credit_1 = Koha::Account::Lines->find( $credit_1_id );
580 is( $credit_1->branchcode, undef, 'No branchcode is set if library_id was not passed' );
582 my $credit_2_id = $account->pay({ amount => 150, library_id => $library->id });
583 my $credit_2 = Koha::Account::Lines->find( $credit_2_id );
585 is( $credit_2->branchcode, $library->id, 'branchcode set because library_id was passed' );
587 $schema->storage->txn_rollback;