Bug 19382: Adjust comment in test
[koha.git] / t / db_dependent / Koha / Account.t
blobdd54fff6012b3d0bdabd8403bf89a1875dcbd6f5
1 #!/usr/bin/perl
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>
20 use Modern::Perl;
22 use Test::More tests => 13;
23 use Test::MockModule;
24 use Test::Exception;
26 use DateTime;
28 use Koha::Account;
29 use Koha::Account::CreditTypes;
30 use Koha::Account::Lines;
31 use Koha::Account::Offsets;
32 use Koha::DateUtils qw( dt_from_string );
34 use t::lib::Mocks;
35 use t::lib::TestBuilder;
37 my $schema = Koha::Database->new->schema;
38 $schema->storage->dbh->{PrintError} = 0;
39 my $builder = t::lib::TestBuilder->new;
40 C4::Context->interface('commandline');
42 subtest 'new' => sub {
44 plan tests => 2;
46 $schema->storage->txn_begin;
48 throws_ok { Koha::Account->new(); } qr/No patron id passed in!/, 'Croaked on bad call to new';
50 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
51 my $account = Koha::Account->new( { patron_id => $patron->borrowernumber } );
52 is( defined $account, 1, "Account is defined" );
54 $schema->storage->txn_rollback;
57 subtest 'outstanding_debits() tests' => sub {
59 plan tests => 22;
61 $schema->storage->txn_begin;
63 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
64 my $account = $patron->account;
66 my @generated_lines;
67 push @generated_lines, $account->add_debit({ amount => 1, interface => 'commandline', type => 'OVERDUE' });
68 push @generated_lines, $account->add_debit({ amount => 2, interface => 'commandline', type => 'OVERDUE' });
69 push @generated_lines, $account->add_debit({ amount => 3, interface => 'commandline', type => 'OVERDUE' });
70 push @generated_lines, $account->add_debit({ amount => 4, interface => 'commandline', type => 'OVERDUE' });
72 my $lines = $account->outstanding_debits();
73 my @lines_arr = $account->outstanding_debits();
75 is( ref($lines), 'Koha::Account::Lines', 'Called in scalar context, outstanding_debits returns a Koha::Account::Lines object' );
76 is( $lines->total_outstanding, 10, 'Outstandig debits total is correctly calculated' );
78 my $i = 0;
79 foreach my $line ( @{ $lines->as_list } ) {
80 my $fetched_line = Koha::Account::Lines->find( $generated_lines[$i]->id );
81 is_deeply( $line->unblessed, $fetched_line->unblessed, "Fetched line matches the generated one ($i)" );
82 is_deeply( $lines_arr[$i]->unblessed, $fetched_line->unblessed, "Fetched line matches the generated one ($i)" );
83 is( ref($lines_arr[$i]), 'Koha::Account::Line', 'outstanding_debits returns a list of Koha::Account::Line objects in list context' );
84 $i++;
86 my $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
87 Koha::Account::Line->new(
89 borrowernumber => $patron_2->id,
90 amountoutstanding => -2,
91 interface => 'commandline',
92 credit_type_code => 'PAYMENT'
94 )->store;
95 my $just_one = Koha::Account::Line->new(
97 borrowernumber => $patron_2->id,
98 amount => 3,
99 amountoutstanding => 3,
100 interface => 'commandline',
101 debit_type_code => 'OVERDUE'
103 )->store;
104 Koha::Account::Line->new(
106 borrowernumber => $patron_2->id,
107 amount => -6,
108 amountoutstanding => -6,
109 interface => 'commandline',
110 credit_type_code => 'PAYMENT'
112 )->store;
113 $lines = $patron_2->account->outstanding_debits();
114 is( $lines->total_outstanding, 3, "Total if some outstanding debits and some credits is only debits" );
115 is( $lines->count, 1, "With 1 outstanding debits, we get back a Lines object with 1 lines" );
116 my $the_line = Koha::Account::Lines->find( $just_one->id );
117 is_deeply( $the_line->unblessed, $lines->next->unblessed, "We get back the one correct line");
119 my $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
120 my $account_3 = $patron_3->account;
121 $account_3->add_credit( { amount => 2, interface => 'commandline' } );
122 $account_3->add_credit( { amount => 20, interface => 'commandline' } );
123 $account_3->add_credit( { amount => 200, interface => 'commandline' } );
124 $lines = $account_3->outstanding_debits();
125 is( $lines->total_outstanding, 0, "Total if no outstanding debits total is 0" );
126 is( $lines->count, 0, "With 0 outstanding debits, we get back a Lines object with 0 lines" );
128 my $patron_4 = $builder->build_object({ class => 'Koha::Patrons' });
129 my $account_4 = $patron_4->account;
130 $lines = $account_4->outstanding_debits();
131 is( $lines->total_outstanding, 0, "Total if no outstanding debits is 0" );
132 is( $lines->count, 0, "With no outstanding debits, we get back a Lines object with 0 lines" );
134 # create a pathological credit with amountoutstanding > 0 (BZ 14591)
135 Koha::Account::Line->new(
137 borrowernumber => $patron_4->id,
138 amount => -3,
139 amountoutstanding => 3,
140 interface => 'commandline',
141 credit_type_code => 'PAYMENT'
143 )->store();
144 $lines = $account_4->outstanding_debits();
145 is( $lines->count, 0, 'No credits are confused with debits because of the amountoutstanding value' );
147 $schema->storage->txn_rollback;
150 subtest 'outstanding_credits() tests' => sub {
152 plan tests => 17;
154 $schema->storage->txn_begin;
156 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
157 my $account = $patron->account;
159 my @generated_lines;
160 push @generated_lines, $account->add_credit({ amount => 1, interface => 'commandline' });
161 push @generated_lines, $account->add_credit({ amount => 2, interface => 'commandline' });
162 push @generated_lines, $account->add_credit({ amount => 3, interface => 'commandline' });
163 push @generated_lines, $account->add_credit({ amount => 4, interface => 'commandline' });
165 my $lines = $account->outstanding_credits();
166 my @lines_arr = $account->outstanding_credits();
168 is( ref($lines), 'Koha::Account::Lines', 'Called in scalar context, outstanding_credits returns a Koha::Account::Lines object' );
169 is( $lines->total_outstanding, -10, 'Outstandig credits total is correctly calculated' );
171 my $i = 0;
172 foreach my $line ( @{ $lines->as_list } ) {
173 my $fetched_line = Koha::Account::Lines->find( $generated_lines[$i]->id );
174 is_deeply( $line->unblessed, $fetched_line->unblessed, "Fetched line matches the generated one ($i)" );
175 is_deeply( $lines_arr[$i]->unblessed, $fetched_line->unblessed, "Fetched line matches the generated one ($i)" );
176 is( ref($lines_arr[$i]), 'Koha::Account::Line', 'outstanding_debits returns a list of Koha::Account::Line objects in list context' );
177 $i++;
180 my $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
181 $account = $patron_2->account;
182 $lines = $account->outstanding_credits();
183 is( $lines->total_outstanding, 0, "Total if no outstanding credits is 0" );
184 is( $lines->count, 0, "With no outstanding credits, we get back a Lines object with 0 lines" );
186 # create a pathological debit with amountoutstanding < 0 (BZ 14591)
187 Koha::Account::Line->new(
189 borrowernumber => $patron_2->id,
190 amount => 2,
191 amountoutstanding => -3,
192 interface => 'commandline',
193 debit_type_code => 'OVERDUE'
195 )->store();
196 $lines = $account->outstanding_credits();
197 is( $lines->count, 0, 'No debits are confused with credits because of the amountoutstanding value' );
199 $schema->storage->txn_rollback;
202 subtest 'add_credit() tests' => sub {
204 plan tests => 17;
206 $schema->storage->txn_begin;
208 # delete logs and statistics
209 my $action_logs = $schema->resultset('ActionLog')->search()->count;
210 my $statistics = $schema->resultset('Statistic')->search()->count;
212 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
213 my $account = Koha::Account->new( { patron_id => $patron->borrowernumber } );
215 is( $account->balance, 0, 'Patron has no balance' );
217 # Disable logs
218 t::lib::Mocks::mock_preference( 'FinesLog', 0 );
220 throws_ok {
221 $account->add_credit(
222 { amount => 25,
223 description => 'Payment of 25',
224 library_id => $patron->branchcode,
225 note => 'not really important',
226 type => 'PAYMENT',
227 user_id => $patron->id
231 'Koha::Exceptions::MissingParameter', 'Exception thrown if interface parameter missing';
233 my $line_1 = $account->add_credit(
234 { amount => 25,
235 description => 'Payment of 25',
236 library_id => $patron->branchcode,
237 note => 'not really important',
238 type => 'PAYMENT',
239 user_id => $patron->id,
240 interface => 'commandline'
244 is( $account->balance, -25, 'Patron has a balance of -25' );
245 is( $schema->resultset('ActionLog')->count(), $action_logs + 0, 'No log was added' );
246 is( $schema->resultset('Statistic')->count(), $statistics + 1, 'Action added to statistics' );
247 is( $line_1->credit_type_code, 'PAYMENT', 'Account type is correctly set' );
249 # Enable logs
250 t::lib::Mocks::mock_preference( 'FinesLog', 1 );
252 my $line_2 = $account->add_credit(
253 { amount => 37,
254 description => 'Payment of 37',
255 library_id => $patron->branchcode,
256 note => 'not really important',
257 user_id => $patron->id,
258 interface => 'commandline'
262 is( $account->balance, -62, 'Patron has a balance of -25' );
263 is( $schema->resultset('ActionLog')->count(), $action_logs + 1, 'Log was added' );
264 is( $schema->resultset('Statistic')->count(), $statistics + 2, 'Action added to statistics' );
265 is( $line_2->credit_type_code, 'PAYMENT', 'Account type is correctly set' );
267 # offsets have the credit_id set to accountlines_id, and debit_id is undef
268 my $offset_1 = Koha::Account::Offsets->search({ credit_id => $line_1->id })->next;
269 my $offset_2 = Koha::Account::Offsets->search({ credit_id => $line_2->id })->next;
271 is( $offset_1->credit_id, $line_1->id, 'No debit_id is set for credits' );
272 is( $offset_1->debit_id, undef, 'No debit_id is set for credits' );
273 is( $offset_2->credit_id, $line_2->id, 'No debit_id is set for credits' );
274 is( $offset_2->debit_id, undef, 'No debit_id is set for credits' );
276 my $line_3 = $account->add_credit(
278 amount => 20,
279 description => 'Manual credit applied',
280 library_id => $patron->branchcode,
281 user_id => $patron->id,
282 type => 'FORGIVEN',
283 interface => 'commandline'
287 is( $schema->resultset('ActionLog')->count(), $action_logs + 2, 'Log was added' );
288 is( $schema->resultset('Statistic')->count(), $statistics + 2, 'No action added to statistics, because of credit type' );
290 # Enable cash registers
291 t::lib::Mocks::mock_preference( 'UseCashRegisters', 1 );
292 throws_ok {
293 $account->add_credit(
295 amount => 20,
296 description => 'Cash payment without cash register',
297 library_id => $patron->branchcode,
298 user_id => $patron->id,
299 payment_type => 'CASH',
300 interface => 'intranet'
304 'Koha::Exceptions::Account::RegisterRequired',
305 'Exception thrown for UseCashRegisters:1 + payment_type:CASH + cash_register:undef';
307 # Disable cash registers
308 t::lib::Mocks::mock_preference( 'UseCashRegisters', 1 );
310 $schema->storage->txn_rollback;
313 subtest 'add_debit() tests' => sub {
315 plan tests => 14;
317 $schema->storage->txn_begin;
319 # delete logs and statistics
320 my $action_logs = $schema->resultset('ActionLog')->search()->count;
321 my $statistics = $schema->resultset('Statistic')->search()->count;
323 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
324 my $account =
325 Koha::Account->new( { patron_id => $patron->borrowernumber } );
327 is( $account->balance, 0, 'Patron has no balance' );
329 throws_ok {
330 $account->add_debit(
332 amount => -5,
333 description => 'amount validation failure',
334 library_id => $patron->branchcode,
335 note => 'this should fail anyway',
336 type => 'RENT',
337 user_id => $patron->id,
338 interface => 'commandline'
340 ); } 'Koha::Exceptions::Account::AmountNotPositive', 'Expected validation exception thrown (amount)';
342 throws_ok {
343 local *STDERR;
344 open STDERR, '>', '/dev/null';
345 $account->add_debit(
347 amount => 5,
348 description => 'type validation failure',
349 library_id => $patron->branchcode,
350 note => 'this should fail anyway',
351 type => 'failure',
352 user_id => $patron->id,
353 interface => 'commandline'
356 close STDERR;
358 'Koha::Exceptions::Account::UnrecognisedType',
359 'Expected validation exception thrown (type)';
361 throws_ok {
362 $account->add_debit(
364 amount => 25,
365 description => 'Rental charge of 25',
366 library_id => $patron->branchcode,
367 note => 'not really important',
368 type => 'RENT',
369 user_id => $patron->id
371 ); } 'Koha::Exceptions::MissingParameter', 'Exception thrown if interface parameter missing';
373 # Disable logs
374 t::lib::Mocks::mock_preference( 'FinesLog', 0 );
376 my $line_1 = $account->add_debit(
378 amount => 25,
379 description => 'Rental charge of 25',
380 library_id => $patron->branchcode,
381 note => 'not really important',
382 type => 'RENT',
383 user_id => $patron->id,
384 interface => 'commandline'
388 is( $account->balance, 25, 'Patron has a balance of 25' );
390 $schema->resultset('ActionLog')->count(),
391 $action_logs + 0,
392 'No log was added'
395 $line_1->debit_type_code,
396 'RENT',
397 'Account type is correctly set'
400 # Enable logs
401 t::lib::Mocks::mock_preference( 'FinesLog', 1 );
403 my $line_2 = $account->add_debit(
405 amount => 37,
406 description => 'Rental charge of 37',
407 library_id => $patron->branchcode,
408 note => 'not really important',
409 type => 'RENT',
410 user_id => $patron->id,
411 interface => 'commandline'
415 is( $account->balance, 62, 'Patron has a balance of 62' );
417 $schema->resultset('ActionLog')->count(),
418 $action_logs + 1,
419 'Log was added'
422 $line_2->debit_type_code,
423 'RENT',
424 'Account type is correctly set'
427 # offsets have the debit_id set to accountlines_id, and credit_id is undef
428 my $offset_1 =
429 Koha::Account::Offsets->search( { debit_id => $line_1->id } )->next;
430 my $offset_2 =
431 Koha::Account::Offsets->search( { debit_id => $line_2->id } )->next;
433 is( $offset_1->debit_id, $line_1->id, 'debit_id is set for debit 1' );
434 is( $offset_1->credit_id, undef, 'credit_id is not set for debit 1' );
435 is( $offset_2->debit_id, $line_2->id, 'debit_id is set for debit 2' );
436 is( $offset_2->credit_id, undef, 'credit_id is not set for debit 2' );
438 $schema->storage->txn_rollback;
441 subtest 'lines() tests' => sub {
443 plan tests => 1;
445 $schema->storage->txn_begin;
447 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
448 my $account = $patron->account;
450 # Add Credits
451 $account->add_credit({ amount => 1, interface => 'commandline' });
452 $account->add_credit({ amount => 2, interface => 'commandline' });
453 $account->add_credit({ amount => 3, interface => 'commandline' });
454 $account->add_credit({ amount => 4, interface => 'commandline' });
456 # Add Debits
457 $account->add_debit({ amount => 1, interface => 'commandline', type => 'OVERDUE' });
458 $account->add_debit({ amount => 2, interface => 'commandline', type => 'OVERDUE' });
459 $account->add_debit({ amount => 3, interface => 'commandline', type => 'OVERDUE' });
460 $account->add_debit({ amount => 4, interface => 'commandline', type => 'OVERDUE' });
462 # Paid Off
463 $account->add_credit( { amount => 1, interface => 'commandline' } )
464 ->apply( { debits => [ $account->outstanding_debits->as_list ] } );
466 my $lines = $account->lines;
467 is( $lines->_resultset->count, 9, "All accountlines (debits, credits and paid off) were fetched");
469 $schema->storage->txn_rollback;
472 subtest 'reconcile_balance' => sub {
474 plan tests => 4;
476 subtest 'more credit than debit' => sub {
478 plan tests => 6;
480 $schema->storage->txn_begin;
482 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
483 my $account = $patron->account;
485 # Add Credits
486 $account->add_credit({ amount => 1, interface => 'commandline' });
487 $account->add_credit({ amount => 2, interface => 'commandline' });
488 $account->add_credit({ amount => 3, interface => 'commandline' });
489 $account->add_credit({ amount => 4, interface => 'commandline' });
490 $account->add_credit({ amount => 5, interface => 'commandline' });
492 # Add Debits
493 $account->add_debit({ amount => 1, interface => 'commandline', type => 'OVERDUE' });
494 $account->add_debit({ amount => 2, interface => 'commandline', type => 'OVERDUE' });
495 $account->add_debit({ amount => 3, interface => 'commandline', type => 'OVERDUE' });
496 $account->add_debit({ amount => 4, interface => 'commandline', type => 'OVERDUE' });
498 # Paid Off
499 Koha::Account::Line->new(
501 borrowernumber => $patron->id,
502 amount => 1,
503 amountoutstanding => 0,
504 interface => 'commandline',
505 debit_type_code => 'OVERDUE'
507 )->store;
508 Koha::Account::Line->new(
510 borrowernumber => $patron->id,
511 amount => 1,
512 amountoutstanding => 0,
513 interface => 'commandline',
514 debit_type_code => 'OVERDUE'
516 )->store;
518 is( $account->balance(), -5, "Account balance is -5" );
519 is( $account->outstanding_debits->total_outstanding, 10, 'Outstanding debits sum 10' );
520 is( $account->outstanding_credits->total_outstanding, -15, 'Outstanding credits sum -15' );
522 $account->reconcile_balance();
524 is( $account->balance(), -5, "Account balance is -5" );
525 is( $account->outstanding_debits->total_outstanding, 0, 'No outstanding debits' );
526 is( $account->outstanding_credits->total_outstanding, -5, 'Outstanding credits sum -5' );
528 $schema->storage->txn_rollback;
531 subtest 'same debit as credit' => sub {
533 plan tests => 6;
535 $schema->storage->txn_begin;
537 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
538 my $account = $patron->account;
540 # Add Credits
541 $account->add_credit({ amount => 1, interface => 'commandline' });
542 $account->add_credit({ amount => 2, interface => 'commandline' });
543 $account->add_credit({ amount => 3, interface => 'commandline' });
544 $account->add_credit({ amount => 4, interface => 'commandline' });
546 # Add Debits
547 $account->add_debit({ amount => 1, interface => 'commandline', type => 'OVERDUE' });
548 $account->add_debit({ amount => 2, interface => 'commandline', type => 'OVERDUE' });
549 $account->add_debit({ amount => 3, interface => 'commandline', type => 'OVERDUE' });
550 $account->add_debit({ amount => 4, interface => 'commandline', type => 'OVERDUE' });
552 # Paid Off
553 Koha::Account::Line->new(
555 borrowernumber => $patron->id,
556 amount => 1,
557 amountoutstanding => 0,
558 interface => 'commandline',
559 debit_type_code => 'OVERDUE'
561 )->store;
562 Koha::Account::Line->new(
564 borrowernumber => $patron->id,
565 amount => 1,
566 amountoutstanding => 0,
567 interface => 'commandline',
568 debit_type_code => 'OVERDUE'
570 )->store;
572 is( $account->balance(), 0, "Account balance is 0" );
573 is( $account->outstanding_debits->total_outstanding, 10, 'Outstanding debits sum 10' );
574 is( $account->outstanding_credits->total_outstanding, -10, 'Outstanding credits sum -10' );
576 $account->reconcile_balance();
578 is( $account->balance(), 0, "Account balance is 0" );
579 is( $account->outstanding_debits->total_outstanding, 0, 'No outstanding debits' );
580 is( $account->outstanding_credits->total_outstanding, 0, 'Outstanding credits sum 0' );
582 $schema->storage->txn_rollback;
585 subtest 'more debit than credit' => sub {
587 plan tests => 6;
589 $schema->storage->txn_begin;
591 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
592 my $account = $patron->account;
594 # Add Credits
595 $account->add_credit({ amount => 1, interface => 'commandline' });
596 $account->add_credit({ amount => 2, interface => 'commandline' });
597 $account->add_credit({ amount => 3, interface => 'commandline' });
598 $account->add_credit({ amount => 4, interface => 'commandline' });
600 # Add Debits
601 $account->add_debit({ amount => 1, interface => 'commandline', type => 'OVERDUE' });
602 $account->add_debit({ amount => 2, interface => 'commandline', type => 'OVERDUE' });
603 $account->add_debit({ amount => 3, interface => 'commandline', type => 'OVERDUE' });
604 $account->add_debit({ amount => 4, interface => 'commandline', type => 'OVERDUE' });
605 $account->add_debit({ amount => 5, interface => 'commandline', type => 'OVERDUE' });
607 # Paid Off
608 Koha::Account::Line->new(
610 borrowernumber => $patron->id,
611 amount => 1,
612 amountoutstanding => 0,
613 interface => 'commandline',
614 debit_type_code => 'OVERDUE'
616 )->store;
617 Koha::Account::Line->new(
619 borrowernumber => $patron->id,
620 amount => 1,
621 amountoutstanding => 0,
622 interface => 'commandline',
623 debit_type_code => 'OVERDUE'
625 )->store;
627 is( $account->balance(), 5, "Account balance is 5" );
628 is( $account->outstanding_debits->total_outstanding, 15, 'Outstanding debits sum 15' );
629 is( $account->outstanding_credits->total_outstanding, -10, 'Outstanding credits sum -10' );
631 $account->reconcile_balance();
633 is( $account->balance(), 5, "Account balance is 5" );
634 is( $account->outstanding_debits->total_outstanding, 5, 'Outstanding debits sum 5' );
635 is( $account->outstanding_credits->total_outstanding, 0, 'Outstanding credits sum 0' );
637 $schema->storage->txn_rollback;
640 subtest 'credits are applied to older debits first' => sub {
642 plan tests => 9;
644 $schema->storage->txn_begin;
646 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
647 my $account = $patron->account;
649 # Add Credits
650 $account->add_credit({ amount => 1, interface => 'commandline' });
651 $account->add_credit({ amount => 3, interface => 'commandline' });
653 # Add Debits
654 my $debit_1 = $account->add_debit({ amount => 1, interface => 'commandline', type => 'OVERDUE' });
655 my $debit_2 = $account->add_debit({ amount => 2, interface => 'commandline', type => 'OVERDUE' });
656 my $debit_3 = $account->add_debit({ amount => 3, interface => 'commandline', type => 'OVERDUE' });
658 is( $account->balance(), 2, "Account balance is 2" );
659 is( $account->outstanding_debits->total_outstanding, 6, 'Outstanding debits sum 6' );
660 is( $account->outstanding_credits->total_outstanding, -4, 'Outstanding credits sum -4' );
662 $account->reconcile_balance();
664 is( $account->balance(), 2, "Account balance is 2" );
665 is( $account->outstanding_debits->total_outstanding, 2, 'Outstanding debits sum 2' );
666 is( $account->outstanding_credits->total_outstanding, 0, 'Outstanding credits sum 0' );
668 $debit_1->discard_changes;
669 is( $debit_1->amountoutstanding + 0, 0, 'Old debit payed' );
670 $debit_2->discard_changes;
671 is( $debit_2->amountoutstanding + 0, 0, 'Old debit payed' );
672 $debit_3->discard_changes;
673 is( $debit_3->amountoutstanding + 0, 2, 'Newest debit only partially payed' );
675 $schema->storage->txn_rollback;
679 subtest 'pay() tests' => sub {
681 plan tests => 5;
683 $schema->storage->txn_begin;
685 # Disable renewing upon fine payment
686 t::lib::Mocks::mock_preference( 'RenewAccruingItemWhenPaid', 0 );
688 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
689 my $library = $builder->build_object({ class => 'Koha::Libraries' });
690 my $account = $patron->account;
692 my $context = Test::MockModule->new('C4::Context');
693 $context->mock( 'userenv', { branch => $library->id } );
695 my $credit_1_id = $account->pay({ amount => 200 })->{payment_id};
696 my $credit_1 = Koha::Account::Lines->find( $credit_1_id );
698 is( $credit_1->branchcode, undef, 'No branchcode is set if library_id was not passed' );
700 my $credit_2_id = $account->pay({ amount => 150, library_id => $library->id })->{payment_id};
701 my $credit_2 = Koha::Account::Lines->find( $credit_2_id );
703 is( $credit_2->branchcode, $library->id, 'branchcode set because library_id was passed' );
705 # Enable cash registers
706 t::lib::Mocks::mock_preference( 'UseCashRegisters', 1 );
707 throws_ok {
708 $account->pay(
710 amount => 20,
711 payment_type => 'CASH',
712 interface => 'intranet'
716 'Koha::Exceptions::Account::RegisterRequired',
717 'Exception thrown for UseCashRegisters:1 + payment_type:CASH + cash_register:undef';
719 # Disable cash registers
720 t::lib::Mocks::mock_preference( 'UseCashRegisters', 1 );
722 # Undef userenv
723 $context->mock( 'userenv', undef );
724 my $result = $account->pay(
726 amount => 20,
727 payment_Type => 'CASH',
728 interface => 'intranet'
731 ok($result, "Koha::Account->pay functions without a userenv");
732 my $payment = Koha::Account::Lines->find({accountlines_id => $result->{payment_id}});
733 is($payment->manager_id, undef, "manager_id left undefined when no userenv found");
735 $schema->storage->txn_rollback;
738 subtest 'pay() handles lost items when paying a specific lost fee' => sub {
740 plan tests => 5;
742 $schema->storage->txn_begin;
744 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
745 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
746 my $account = $patron->account;
748 my $context = Test::MockModule->new('C4::Context');
749 $context->mock( 'userenv', { branch => $library->id } );
751 my $biblio = $builder->build_sample_biblio();
752 my $item =
753 $builder->build_sample_item( { biblionumber => $biblio->biblionumber } );
755 my $checkout = Koha::Checkout->new(
757 borrowernumber => $patron->id,
758 itemnumber => $item->id,
759 date_due => \'NOW()',
760 branchcode => $patron->branchcode,
761 issuedate => \'NOW()',
763 )->store();
765 $item->itemlost('1')->store();
767 my $accountline = Koha::Account::Line->new(
769 issue_id => $checkout->id,
770 borrowernumber => $patron->id,
771 itemnumber => $item->id,
772 date => \'NOW()',
773 debit_type_code => 'LOST',
774 interface => 'cli',
775 amount => '1',
776 amountoutstanding => '1',
778 )->store();
780 $account->pay(
782 amount => .5,
783 library_id => $library->id,
784 lines => [$accountline],
788 $accountline = Koha::Account::Lines->find( $accountline->id );
789 is( $accountline->amountoutstanding+0, .5, 'Account line was paid down by half' );
791 $checkout = Koha::Checkouts->find( $checkout->id );
792 ok( $checkout, 'Item still checked out to patron' );
794 $account->pay(
796 amount => 0.5,
797 library_id => $library->id,
798 lines => [$accountline],
802 $accountline = Koha::Account::Lines->find( $accountline->id );
803 is( $accountline->amountoutstanding+0, 0, 'Account line was paid down by half' );
805 $checkout = Koha::Checkouts->find( $checkout->id );
806 ok( !$checkout, 'Item was removed from patron account' );
808 subtest 'item was not checked out to the same patron' => sub {
809 plan tests => 1;
811 my $patron_2 = $builder->build_object(
813 class => 'Koha::Patrons',
814 value => { branchcode => $library->branchcode }
817 $item->itemlost('1')->store();
818 C4::Accounts::chargelostitem( $patron->borrowernumber, $item->itemnumber, 5, "lost" );
819 my $accountline = Koha::Account::Lines->search(
821 borrowernumber => $patron->borrowernumber,
822 itemnumber => $item->itemnumber,
823 debit_type_code => 'LOST'
825 )->next;
826 my $checkout = Koha::Checkout->new(
828 borrowernumber => $patron_2->borrowernumber,
829 itemnumber => $item->itemnumber,
830 date_due => \'NOW()',
831 branchcode => $patron_2->branchcode,
832 issuedate => \'NOW()',
834 )->store();
836 $patron->account->pay(
838 amount => 5,
839 library_id => $library->branchcode,
840 lines => [$accountline],
845 Koha::Checkouts->find( $checkout->issue_id ),
846 'If the item is checked out to another patron, a lost item should not be returned if lost fee is paid'
851 $schema->storage->txn_rollback;
854 subtest 'pay() handles lost items when paying by amount ( not specifying the lost fee )' => sub {
856 plan tests => 4;
858 $schema->storage->txn_begin;
860 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
861 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
862 my $account = $patron->account;
864 my $context = Test::MockModule->new('C4::Context');
865 $context->mock( 'userenv', { branch => $library->id } );
867 my $biblio = $builder->build_sample_biblio();
868 my $item =
869 $builder->build_sample_item( { biblionumber => $biblio->biblionumber } );
871 my $checkout = Koha::Checkout->new(
873 borrowernumber => $patron->id,
874 itemnumber => $item->id,
875 date_due => \'NOW()',
876 branchcode => $patron->branchcode,
877 issuedate => \'NOW()',
879 )->store();
881 $item->itemlost('1')->store();
883 my $accountline = Koha::Account::Line->new(
885 issue_id => $checkout->id,
886 borrowernumber => $patron->id,
887 itemnumber => $item->id,
888 date => \'NOW()',
889 debit_type_code => 'LOST',
890 interface => 'cli',
891 amount => '1',
892 amountoutstanding => '1',
894 )->store();
896 $account->pay(
898 amount => .5,
899 library_id => $library->id,
903 $accountline = Koha::Account::Lines->find( $accountline->id );
904 is( $accountline->amountoutstanding+0, .5, 'Account line was paid down by half' );
906 $checkout = Koha::Checkouts->find( $checkout->id );
907 ok( $checkout, 'Item still checked out to patron' );
909 $account->pay(
911 amount => .5,,
912 library_id => $library->id,
916 $accountline = Koha::Account::Lines->find( $accountline->id );
917 is( $accountline->amountoutstanding+0, 0, 'Account line was paid down by half' );
919 $checkout = Koha::Checkouts->find( $checkout->id );
920 ok( !$checkout, 'Item was removed from patron account' );
922 $schema->storage->txn_rollback;
925 subtest 'pay() renews items when appropriate' => sub {
927 plan tests => 1;
929 $schema->storage->txn_begin;
931 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
932 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
933 my $account = $patron->account;
935 my $context = Test::MockModule->new('C4::Context');
936 $context->mock( 'userenv', { branch => $library->id } );
938 my $biblio = $builder->build_sample_biblio();
939 my $item =
940 $builder->build_sample_item( { biblionumber => $biblio->biblionumber } );
942 my $now = dt_from_string();
943 my $seven_weeks = DateTime::Duration->new(weeks => 7);
944 my $five_weeks = DateTime::Duration->new(weeks => 5);
945 my $seven_weeks_ago = $now - $seven_weeks;
946 my $five_weeks_ago = $now - $five_weeks;
948 my $checkout = Koha::Checkout->new(
950 borrowernumber => $patron->id,
951 itemnumber => $item->id,
952 date_due => $five_weeks_ago,
953 branchcode => $patron->branchcode,
954 issuedate => $seven_weeks_ago
956 )->store();
958 my $accountline = Koha::Account::Line->new(
960 issue_id => $checkout->id,
961 borrowernumber => $patron->id,
962 itemnumber => $item->id,
963 date => \'NOW()',
964 debit_type_code => 'OVERDUE',
965 status => 'UNRETURNED',
966 interface => 'cli',
967 amount => '1',
968 amountoutstanding => '1',
970 )->store();
972 # Enable renewing upon fine payment
973 t::lib::Mocks::mock_preference( 'RenewAccruingItemWhenPaid', 1 );
974 my $called = 0;
975 my $module = new Test::MockModule('C4::Circulation');
976 $module->mock('AddRenewal', sub { $called = 1; });
977 $module->mock('CanBookBeRenewed', sub { return 1; });
978 $account->pay(
980 amount => '1',
981 library_id => $library->id,
985 is( $called, 1, 'RenewAccruingItemWhenPaid causes C4::Circulation::AddRenew to be called when appropriate' );
987 $schema->storage->txn_rollback;
990 subtest 'Koha::Account::Line::apply() handles lost items' => sub {
992 plan tests => 4;
994 $schema->storage->txn_begin;
996 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
997 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
998 my $account = $patron->account;
1000 my $context = Test::MockModule->new('C4::Context');
1001 $context->mock( 'userenv', { branch => $library->id } );
1003 my $biblio = $builder->build_sample_biblio();
1004 my $item =
1005 $builder->build_sample_item( { biblionumber => $biblio->biblionumber } );
1007 my $checkout = Koha::Checkout->new(
1009 borrowernumber => $patron->id,
1010 itemnumber => $item->id,
1011 date_due => \'NOW()',
1012 branchcode => $patron->branchcode,
1013 issuedate => \'NOW()',
1015 )->store();
1017 $item->itemlost('1')->store();
1019 my $debit = Koha::Account::Line->new(
1021 issue_id => $checkout->id,
1022 borrowernumber => $patron->id,
1023 itemnumber => $item->id,
1024 date => \'NOW()',
1025 debit_type_code => 'LOST',
1026 interface => 'cli',
1027 amount => '1',
1028 amountoutstanding => '1',
1030 )->store();
1032 my $credit = Koha::Account::Line->new(
1034 borrowernumber => $patron->id,
1035 date => '1970-01-01 00:00:01',
1036 amount => -.5,
1037 amountoutstanding => -.5,
1038 interface => 'commandline',
1039 credit_type_code => 'PAYMENT'
1041 )->store();
1042 my $debits = $account->outstanding_debits;
1043 $credit->apply({ debits => [ $debits->as_list ] });
1045 $debit = Koha::Account::Lines->find( $debit->id );
1046 is( $debit->amountoutstanding+0, .5, 'Account line was paid down by half' );
1048 $checkout = Koha::Checkouts->find( $checkout->id );
1049 ok( $checkout, 'Item still checked out to patron' );
1051 $credit = Koha::Account::Line->new(
1053 borrowernumber => $patron->id,
1054 date => '1970-01-01 00:00:01',
1055 amount => -.5,
1056 amountoutstanding => -.5,
1057 interface => 'commandline',
1058 credit_type_code => 'PAYMENT'
1060 )->store();
1061 $debits = $account->outstanding_debits;
1062 $credit->apply({ debits => [ $debits->as_list ] });
1064 $debit = Koha::Account::Lines->find( $debit->id );
1065 is( $debit->amountoutstanding+0, 0, 'Account line was paid down by half' );
1067 $checkout = Koha::Checkouts->find( $checkout->id );
1068 ok( !$checkout, 'Item was removed from patron account' );
1070 $schema->storage->txn_rollback;
1073 subtest 'Koha::Account::pay() generates credit number (Koha::Account::Line->store)' => sub {
1074 plan tests => 38;
1076 $schema->storage->txn_begin;
1078 Koha::Account::Lines->delete();
1080 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
1081 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
1082 my $account = $patron->account;
1084 #t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode });
1085 my $context = Test::MockModule->new('C4::Context');
1086 $context->mock( 'userenv', { branch => $library->id } );
1088 my $now = dt_from_string;
1089 my $year = $now->year;
1090 my $month = $now->month;
1091 my ($accountlines_id, $accountline);
1093 my $credit_type = Koha::Account::CreditTypes->find('PAYMENT');
1094 $credit_type->credit_number_enabled(1);
1095 $credit_type->store();
1097 t::lib::Mocks::mock_preference('AutoCreditNumber', '');
1098 $accountlines_id = $account->pay({ amount => '1.00', library_id => $library->id })->{payment_id};
1099 $accountline = Koha::Account::Lines->find($accountlines_id);
1100 is($accountline->credit_number, undef, 'No credit number is generated when syspref is off');
1102 t::lib::Mocks::mock_preference('AutoCreditNumber', 'incremental');
1103 for my $i (1..11) {
1104 $accountlines_id = $account->pay({ amount => '1.00', library_id => $library->id })->{payment_id};
1105 $accountline = Koha::Account::Lines->find($accountlines_id);
1106 is($accountline->credit_number, $i, "Incremental format credit number added for payments: $i");
1108 $accountlines_id = $account->pay({ type => 'WRITEOFF', amount => '1.00', library_id => $library->id })->{payment_id};
1109 $accountline = Koha::Account::Lines->find($accountlines_id);
1110 is($accountline->credit_number, undef, "Incremental credit number not added for writeoff");
1112 t::lib::Mocks::mock_preference('AutoCreditNumber', 'annual');
1113 for my $i (1..11) {
1114 $accountlines_id = $account->pay({ amount => '1.00', library_id => $library->id })->{payment_id};
1115 $accountline = Koha::Account::Lines->find($accountlines_id);
1116 is($accountline->credit_number, sprintf('%s-%04d', $year, $i), "Annual format credit number added for payments: " . sprintf('%s-%04d', $year, $i));
1118 $accountlines_id = $account->pay({ type => 'WRITEOFF', amount => '1.00', library_id => $library->id })->{payment_id};
1119 $accountline = Koha::Account::Lines->find($accountlines_id);
1120 is($accountline->credit_number, undef, "Annual format credit number not aded for writeoff");
1122 t::lib::Mocks::mock_preference('AutoCreditNumber', 'branchyyyymmincr');
1123 for my $i (1..11) {
1124 $accountlines_id = $account->pay({ amount => '1.00', library_id => $library->id })->{payment_id};
1125 $accountline = Koha::Account::Lines->find($accountlines_id);
1126 is($accountline->credit_number, sprintf('%s%d%02d%04d', $library->id, $year, $month, $i), "branchyyyymmincr format credit number added for payment: " . sprintf('%s%d%02d%04d', $library->id, $year, $month, $i));
1128 $accountlines_id = $account->pay({ type => 'WRITEOFF', amount => '1.00', library_id => $library->id })->{payment_id};
1129 $accountline = Koha::Account::Lines->find($accountlines_id);
1130 is($accountline->credit_number, undef, "branchyyyymmincr format credit number not added for writeoff");
1132 throws_ok {
1133 Koha::Account::Line->new(
1135 interface => 'test',
1136 amount => -1,
1137 credit_type_code => $credit_type->code,
1138 credit_number => 42
1140 )->store;
1142 'Koha::Exceptions::Account',
1143 "Exception thrown when AutoCreditNumber is enabled but credit_number is already defined";
1145 $schema->storage->txn_rollback;