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;
37 subtest
'new' => sub {
41 $schema->storage->txn_begin;
43 throws_ok
{ Koha
::Account
->new(); } qr/No patron id passed in!/, 'Croaked on bad call to new';
45 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
46 my $account = Koha
::Account
->new( { patron_id
=> $patron->borrowernumber } );
47 is
( defined $account, 1, "Account is defined" );
49 $schema->storage->txn_rollback;
52 subtest
'outstanding_debits() tests' => sub {
56 $schema->storage->txn_begin;
58 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
59 my $account = $patron->account;
62 push @generated_lines, Koha
::Account
::Line
->new({ borrowernumber
=> $patron->id, amount
=> 1, amountoutstanding
=> 1 })->store;
63 push @generated_lines, Koha
::Account
::Line
->new({ borrowernumber
=> $patron->id, amount
=> 2, amountoutstanding
=> 2 })->store;
64 push @generated_lines, Koha
::Account
::Line
->new({ borrowernumber
=> $patron->id, amount
=> 3, amountoutstanding
=> 3 })->store;
65 push @generated_lines, Koha
::Account
::Line
->new({ borrowernumber
=> $patron->id, amount
=> 4, amountoutstanding
=> 4 })->store;
67 my $lines = $account->outstanding_debits();
68 my @lines_arr = $account->outstanding_debits();
70 is
( ref($lines), 'Koha::Account::Lines', 'Called in scalar context, outstanding_debits returns a Koha::Account::Lines object' );
71 is
( $lines->total_outstanding, 10, 'Outstandig debits total is correctly calculated' );
74 foreach my $line ( @
{ $lines->as_list } ) {
75 my $fetched_line = Koha
::Account
::Lines
->find( $generated_lines[$i]->id );
76 is_deeply
( $line->unblessed, $fetched_line->unblessed, "Fetched line matches the generated one ($i)" );
77 is_deeply
( $lines_arr[$i]->unblessed, $fetched_line->unblessed, "Fetched line matches the generated one ($i)" );
78 is
( ref($lines_arr[$i]), 'Koha::Account::Line', 'outstanding_debits returns a list of Koha::Account::Line objects in list context' );
81 my $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
82 Koha
::Account
::Line
->new({ borrowernumber
=> $patron_2->id, amountoutstanding
=> -2 })->store;
83 my $just_one = Koha
::Account
::Line
->new({ borrowernumber
=> $patron_2->id, amount
=> 3, amountoutstanding
=> 3 })->store;
84 Koha
::Account
::Line
->new({ borrowernumber
=> $patron_2->id, amount
=> -6, amountoutstanding
=> -6 })->store;
85 $lines = $patron_2->account->outstanding_debits();
86 is
( $lines->total_outstanding, 3, "Total if some outstanding debits and some credits is only debits" );
87 is
( $lines->count, 1, "With 1 outstanding debits, we get back a Lines object with 1 lines" );
88 my $the_line = Koha
::Account
::Lines
->find( $just_one->id );
89 is_deeply
( $the_line->unblessed, $lines->next->unblessed, "We get back the one correct line");
91 my $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
92 Koha
::Account
::Line
->new({ borrowernumber
=> $patron_2->id, amount
=> -2, amountoutstanding
=> -2 })->store;
93 Koha
::Account
::Line
->new({ borrowernumber
=> $patron_2->id, amount
=> -20, amountoutstanding
=> -20 })->store;
94 Koha
::Account
::Line
->new({ borrowernumber
=> $patron_2->id, amount
=> -200, amountoutstanding
=> -200 })->store;
95 $lines = $patron_3->account->outstanding_debits();
96 is
( $lines->total_outstanding, 0, "Total if no outstanding debits total is 0" );
97 is
( $lines->count, 0, "With 0 outstanding debits, we get back a Lines object with 0 lines" );
99 my $patron_4 = $builder->build_object({ class => 'Koha::Patrons' });
100 my $account_4 = $patron_4->account;
101 $lines = $account_4->outstanding_debits();
102 is
( $lines->total_outstanding, 0, "Total if no outstanding debits is 0" );
103 is
( $lines->count, 0, "With no outstanding debits, we get back a Lines object with 0 lines" );
105 # create a pathological credit with amountoutstanding > 0 (BZ 14591)
106 Koha
::Account
::Line
->new({ borrowernumber
=> $patron_4->id, amount
=> -3, amountoutstanding
=> 3 })->store();
107 $lines = $account_4->outstanding_debits();
108 is
( $lines->count, 0, 'No credits are confused with debits because of the amountoutstanding value' );
110 $schema->storage->txn_rollback;
113 subtest
'outstanding_credits() tests' => sub {
117 $schema->storage->txn_begin;
119 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
120 my $account = $patron->account;
123 push @generated_lines, $account->add_credit({ amount
=> 1 });
124 push @generated_lines, $account->add_credit({ amount
=> 2 });
125 push @generated_lines, $account->add_credit({ amount
=> 3 });
126 push @generated_lines, $account->add_credit({ amount
=> 4 });
128 my $lines = $account->outstanding_credits();
129 my @lines_arr = $account->outstanding_credits();
131 is
( ref($lines), 'Koha::Account::Lines', 'Called in scalar context, outstanding_credits returns a Koha::Account::Lines object' );
132 is
( $lines->total_outstanding, -10, 'Outstandig credits total is correctly calculated' );
135 foreach my $line ( @
{ $lines->as_list } ) {
136 my $fetched_line = Koha
::Account
::Lines
->find( $generated_lines[$i]->id );
137 is_deeply
( $line->unblessed, $fetched_line->unblessed, "Fetched line matches the generated one ($i)" );
138 is_deeply
( $lines_arr[$i]->unblessed, $fetched_line->unblessed, "Fetched line matches the generated one ($i)" );
139 is
( ref($lines_arr[$i]), 'Koha::Account::Line', 'outstanding_debits returns a list of Koha::Account::Line objects in list context' );
143 my $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
144 $account = $patron_2->account;
145 $lines = $account->outstanding_credits();
146 is
( $lines->total_outstanding, 0, "Total if no outstanding credits is 0" );
147 is
( $lines->count, 0, "With no outstanding credits, we get back a Lines object with 0 lines" );
149 # create a pathological debit with amountoutstanding < 0 (BZ 14591)
150 Koha
::Account
::Line
->new({ borrowernumber
=> $patron_2->id, amount
=> 2, amountoutstanding
=> -3 })->store();
151 $lines = $account->outstanding_credits();
152 is
( $lines->count, 0, 'No debits are confused with credits because of the amountoutstanding value' );
154 $schema->storage->txn_rollback;
157 subtest
'add_credit() tests' => sub {
161 $schema->storage->txn_begin;
163 # delete logs and statistics
164 my $action_logs = $schema->resultset('ActionLog')->search()->count;
165 my $statistics = $schema->resultset('Statistic')->search()->count;
167 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
168 my $account = Koha
::Account
->new( { patron_id
=> $patron->borrowernumber } );
170 is
( $account->balance, 0, 'Patron has no balance' );
173 t
::lib
::Mocks
::mock_preference
( 'FinesLog', 0 );
175 my $line_1 = $account->add_credit(
177 description
=> 'Payment of 25',
178 library_id
=> $patron->branchcode,
179 note
=> 'not really important',
181 user_id
=> $patron->id
185 is
( $account->balance, -25, 'Patron has a balance of -25' );
186 is
( $schema->resultset('ActionLog')->count(), $action_logs + 0, 'No log was added' );
187 is
( $schema->resultset('Statistic')->count(), $statistics + 1, 'Action added to statistics' );
188 is
( $line_1->accounttype, $Koha::Account
::account_type_credit
->{'payment'}, 'Account type is correctly set' );
191 t
::lib
::Mocks
::mock_preference
( 'FinesLog', 1 );
194 my $line_2 = $account->add_credit(
196 description
=> 'Payment of 37',
197 library_id
=> $patron->branchcode,
198 note
=> 'not really important',
199 user_id
=> $patron->id,
204 is
( $account->balance, -62, 'Patron has a balance of -25' );
205 is
( $schema->resultset('ActionLog')->count(), $action_logs + 1, 'Log was added' );
206 is
( $schema->resultset('Statistic')->count(), $statistics + 2, 'Action added to statistics' );
207 is
( $line_2->accounttype, $Koha::Account
::account_type_credit
->{'payment'} . $sip_code, 'Account type is correctly set' );
209 # offsets have the credit_id set to accountlines_id, and debit_id is undef
210 my $offset_1 = Koha
::Account
::Offsets
->search({ credit_id
=> $line_1->id })->next;
211 my $offset_2 = Koha
::Account
::Offsets
->search({ credit_id
=> $line_2->id })->next;
213 is
( $offset_1->credit_id, $line_1->id, 'No debit_id is set for credits' );
214 is
( $offset_1->debit_id, undef, 'No debit_id is set for credits' );
215 is
( $offset_2->credit_id, $line_2->id, 'No debit_id is set for credits' );
216 is
( $offset_2->debit_id, undef, 'No debit_id is set for credits' );
218 my $line_3 = $account->add_credit(
220 description
=> 'Manual credit applied',
221 library_id
=> $patron->branchcode,
222 user_id
=> $patron->id,
227 is
( $schema->resultset('ActionLog')->count(), $action_logs + 2, 'Log was added' );
228 is
( $schema->resultset('Statistic')->count(), $statistics + 2, 'No action added to statistics, because of credit type' );
230 $schema->storage->txn_rollback;
233 subtest
'add_debit() tests' => sub {
237 $schema->storage->txn_begin;
239 # delete logs and statistics
240 my $action_logs = $schema->resultset('ActionLog')->search()->count;
241 my $statistics = $schema->resultset('Statistic')->search()->count;
243 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
245 Koha
::Account
->new( { patron_id
=> $patron->borrowernumber } );
247 is
( $account->balance, 0, 'Patron has no balance' );
253 description
=> 'amount validation failure',
254 library_id
=> $patron->branchcode,
255 note
=> 'this should fail anyway',
257 user_id
=> $patron->id
259 ); } 'Koha::Exceptions::Account::AmountNotPositive', 'Expected validation exception thrown (amount)';
265 description
=> 'type validation failure',
266 library_id
=> $patron->branchcode,
267 note
=> 'this should fail anyway',
269 user_id
=> $patron->id
271 ); } 'Koha::Exceptions::Account::UnrecognisedType', 'Expected validation exception thrown (type)';
274 t
::lib
::Mocks
::mock_preference
( 'FinesLog', 0 );
276 my $line_1 = $account->add_debit(
279 description
=> 'Rental charge of 25',
280 library_id
=> $patron->branchcode,
281 note
=> 'not really important',
283 user_id
=> $patron->id
287 is
( $account->balance, 25, 'Patron has a balance of 25' );
289 $schema->resultset('ActionLog')->count(),
294 $line_1->accounttype,
295 $Koha::Account
::account_type_debit
->{'rent'},
296 'Account type is correctly set'
300 t
::lib
::Mocks
::mock_preference
( 'FinesLog', 1 );
303 my $line_2 = $account->add_debit(
306 description
=> 'Rental charge of 37',
307 library_id
=> $patron->branchcode,
308 note
=> 'not really important',
310 user_id
=> $patron->id,
314 is
( $account->balance, 62, 'Patron has a balance of 62' );
316 $schema->resultset('ActionLog')->count(),
321 $line_2->accounttype,
322 $Koha::Account
::account_type_debit
->{'rent'},
323 'Account type is correctly set'
326 # offsets have the debit_id set to accountlines_id, and credit_id is undef
328 Koha
::Account
::Offsets
->search( { debit_id
=> $line_1->id } )->next;
330 Koha
::Account
::Offsets
->search( { debit_id
=> $line_2->id } )->next;
332 is
( $offset_1->debit_id, $line_1->id, 'debit_id is set for debit 1' );
333 is
( $offset_1->credit_id, undef, 'credit_id is not set for debit 1' );
334 is
( $offset_2->debit_id, $line_2->id, 'debit_id is set for debit 2' );
335 is
( $offset_2->credit_id, undef, 'credit_id is not set for debit 2' );
337 $schema->storage->txn_rollback;
340 subtest
'lines() tests' => sub {
344 $schema->storage->txn_begin;
346 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
347 my $account = $patron->account;
352 push @generated_lines, $account->add_credit({ amount
=> 1 });
353 push @generated_lines, $account->add_credit({ amount
=> 2 });
354 push @generated_lines, $account->add_credit({ amount
=> 3 });
355 push @generated_lines, $account->add_credit({ amount
=> 4 });
358 push @generated_lines, Koha
::Account
::Line
->new({ borrowernumber
=> $patron->id, amountoutstanding
=> 1 })->store;
359 push @generated_lines, Koha
::Account
::Line
->new({ borrowernumber
=> $patron->id, amountoutstanding
=> 2 })->store;
360 push @generated_lines, Koha
::Account
::Line
->new({ borrowernumber
=> $patron->id, amountoutstanding
=> 3 })->store;
361 push @generated_lines, Koha
::Account
::Line
->new({ borrowernumber
=> $patron->id, amountoutstanding
=> 4 })->store;
364 push @generated_lines, Koha
::Account
::Line
->new({ borrowernumber
=> $patron->id, amountoutstanding
=> 0 })->store;
365 push @generated_lines, Koha
::Account
::Line
->new({ borrowernumber
=> $patron->id, amountoutstanding
=> 0 })->store;
367 my $lines = $account->lines;
368 is
( $lines->_resultset->count, 10, "All accountlines (debits, credits and paid off) were fetched");
370 $schema->storage->txn_rollback;
373 subtest
'reconcile_balance' => sub {
377 subtest
'more credit than debit' => sub {
381 $schema->storage->txn_begin;
383 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
384 my $account = $patron->account;
387 $account->add_credit({ amount
=> 1 });
388 $account->add_credit({ amount
=> 2 });
389 $account->add_credit({ amount
=> 3 });
390 $account->add_credit({ amount
=> 4 });
391 $account->add_credit({ amount
=> 5 });
393 # Add Debits TODO: replace for calls to add_debit when time comes
394 Koha
::Account
::Line
->new({ borrowernumber
=> $patron->id, amount
=> 1, amountoutstanding
=> 1 })->store;
395 Koha
::Account
::Line
->new({ borrowernumber
=> $patron->id, amount
=> 2, amountoutstanding
=> 2 })->store;
396 Koha
::Account
::Line
->new({ borrowernumber
=> $patron->id, amount
=> 3, amountoutstanding
=> 3 })->store;
397 Koha
::Account
::Line
->new({ borrowernumber
=> $patron->id, amount
=> 4, amountoutstanding
=> 4 })->store;
400 Koha
::Account
::Line
->new({ borrowernumber
=> $patron->id, amount
=> 1, amountoutstanding
=> 0 })->store;
401 Koha
::Account
::Line
->new({ borrowernumber
=> $patron->id, amount
=> 1, amountoutstanding
=> 0 })->store;
403 is
( $account->balance(), -5, "Account balance is -5" );
404 is
( $account->outstanding_debits->total_outstanding, 10, 'Outstanding debits sum 10' );
405 is
( $account->outstanding_credits->total_outstanding, -15, 'Outstanding credits sum -15' );
407 $account->reconcile_balance();
409 is
( $account->balance(), -5, "Account balance is -5" );
410 is
( $account->outstanding_debits->total_outstanding, 0, 'No outstanding debits' );
411 is
( $account->outstanding_credits->total_outstanding, -5, 'Outstanding credits sum -5' );
413 $schema->storage->txn_rollback;
416 subtest
'same debit as credit' => sub {
420 $schema->storage->txn_begin;
422 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
423 my $account = $patron->account;
426 $account->add_credit({ amount
=> 1 });
427 $account->add_credit({ amount
=> 2 });
428 $account->add_credit({ amount
=> 3 });
429 $account->add_credit({ amount
=> 4 });
431 # Add Debits TODO: replace for calls to add_debit when time comes
432 Koha
::Account
::Line
->new({ borrowernumber
=> $patron->id, amount
=> 1, amountoutstanding
=> 1 })->store;
433 Koha
::Account
::Line
->new({ borrowernumber
=> $patron->id, amount
=> 2, amountoutstanding
=> 2 })->store;
434 Koha
::Account
::Line
->new({ borrowernumber
=> $patron->id, amount
=> 3, amountoutstanding
=> 3 })->store;
435 Koha
::Account
::Line
->new({ borrowernumber
=> $patron->id, amount
=> 4, amountoutstanding
=> 4 })->store;
438 Koha
::Account
::Line
->new({ borrowernumber
=> $patron->id, amount
=> 1, amountoutstanding
=> 0 })->store;
439 Koha
::Account
::Line
->new({ borrowernumber
=> $patron->id, amount
=> 1, amountoutstanding
=> 0 })->store;
441 is
( $account->balance(), 0, "Account balance is 0" );
442 is
( $account->outstanding_debits->total_outstanding, 10, 'Outstanding debits sum 10' );
443 is
( $account->outstanding_credits->total_outstanding, -10, 'Outstanding credits sum -10' );
445 $account->reconcile_balance();
447 is
( $account->balance(), 0, "Account balance is 0" );
448 is
( $account->outstanding_debits->total_outstanding, 0, 'No outstanding debits' );
449 is
( $account->outstanding_credits->total_outstanding, 0, 'Outstanding credits sum 0' );
451 $schema->storage->txn_rollback;
454 subtest
'more debit than credit' => sub {
458 $schema->storage->txn_begin;
460 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
461 my $account = $patron->account;
464 $account->add_credit({ amount
=> 1 });
465 $account->add_credit({ amount
=> 2 });
466 $account->add_credit({ amount
=> 3 });
467 $account->add_credit({ amount
=> 4 });
469 # Add Debits TODO: replace for calls to add_debit when time comes
470 Koha
::Account
::Line
->new({ borrowernumber
=> $patron->id, amount
=> 1, amountoutstanding
=> 1 })->store;
471 Koha
::Account
::Line
->new({ borrowernumber
=> $patron->id, amount
=> 2, amountoutstanding
=> 2 })->store;
472 Koha
::Account
::Line
->new({ borrowernumber
=> $patron->id, amount
=> 3, amountoutstanding
=> 3 })->store;
473 Koha
::Account
::Line
->new({ borrowernumber
=> $patron->id, amount
=> 4, amountoutstanding
=> 4 })->store;
474 Koha
::Account
::Line
->new({ borrowernumber
=> $patron->id, amount
=> 5, amountoutstanding
=> 5 })->store;
477 Koha
::Account
::Line
->new({ borrowernumber
=> $patron->id, amount
=> 1, amountoutstanding
=> 0 })->store;
478 Koha
::Account
::Line
->new({ borrowernumber
=> $patron->id, amount
=> 1, amountoutstanding
=> 0 })->store;
480 is
( $account->balance(), 5, "Account balance is 5" );
481 is
( $account->outstanding_debits->total_outstanding, 15, 'Outstanding debits sum 15' );
482 is
( $account->outstanding_credits->total_outstanding, -10, 'Outstanding credits sum -10' );
484 $account->reconcile_balance();
486 is
( $account->balance(), 5, "Account balance is 5" );
487 is
( $account->outstanding_debits->total_outstanding, 5, 'Outstanding debits sum 5' );
488 is
( $account->outstanding_credits->total_outstanding, 0, 'Outstanding credits sum 0' );
490 $schema->storage->txn_rollback;
493 subtest
'credits are applied to older debits first' => sub {
497 $schema->storage->txn_begin;
499 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
500 my $account = $patron->account;
503 $account->add_credit({ amount
=> 1 });
504 $account->add_credit({ amount
=> 3 });
506 # Add Debits TODO: replace for calls to add_debit when time comes
507 my $debit_1 = Koha
::Account
::Line
->new({ borrowernumber
=> $patron->id, amount
=> 1, amountoutstanding
=> 1 })->store;
508 my $debit_2 = Koha
::Account
::Line
->new({ borrowernumber
=> $patron->id, amount
=> 2, amountoutstanding
=> 2 })->store;
509 my $debit_3 = Koha
::Account
::Line
->new({ borrowernumber
=> $patron->id, amount
=> 3, amountoutstanding
=> 3 })->store;
511 is
( $account->balance(), 2, "Account balance is 2" );
512 is
( $account->outstanding_debits->total_outstanding, 6, 'Outstanding debits sum 6' );
513 is
( $account->outstanding_credits->total_outstanding, -4, 'Outstanding credits sum -4' );
515 $account->reconcile_balance();
517 is
( $account->balance(), 2, "Account balance is 2" );
518 is
( $account->outstanding_debits->total_outstanding, 2, 'Outstanding debits sum 2' );
519 is
( $account->outstanding_credits->total_outstanding, 0, 'Outstanding credits sum 0' );
521 $debit_1->discard_changes;
522 is
( $debit_1->amountoutstanding + 0, 0, 'Old debit payed' );
523 $debit_2->discard_changes;
524 is
( $debit_2->amountoutstanding + 0, 0, 'Old debit payed' );
525 $debit_3->discard_changes;
526 is
( $debit_3->amountoutstanding + 0, 2, 'Newest debit only partially payed' );
528 $schema->storage->txn_rollback;
532 subtest
'pay() tests' => sub {
536 $schema->storage->txn_begin;
538 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
539 my $library = $builder->build_object({ class => 'Koha::Libraries' });
540 my $account = $patron->account;
542 my $context = Test
::MockModule
->new('C4::Context');
543 $context->mock( 'userenv', { branch
=> $library->id } );
545 my $credit_1_id = $account->pay({ amount
=> 200 });
546 my $credit_1 = Koha
::Account
::Lines
->find( $credit_1_id );
548 is
( $credit_1->branchcode, undef, 'No branchcode is set if library_id was not passed' );
550 my $credit_2_id = $account->pay({ amount
=> 150, library_id
=> $library->id });
551 my $credit_2 = Koha
::Account
::Lines
->find( $credit_2_id );
553 is
( $credit_2->branchcode, $library->id, 'branchcode set because library_id was passed' );
555 $schema->storage->txn_rollback;