Bug 19036: Add ability to auto generate a number for account credits
[koha.git] / t / db_dependent / Koha / Account.t
blob9ebad520553f9e43ded3145b08aae4940c016863
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 => 12;
23 use Test::MockModule;
24 use Test::Exception;
26 use DateTime;
28 use Koha::Account;
29 use Koha::Account::Lines;
30 use Koha::Account::Offsets;
31 use Koha::DateUtils qw( dt_from_string );
33 use t::lib::Mocks;
34 use t::lib::TestBuilder;
36 my $schema = Koha::Database->new->schema;
37 $schema->storage->dbh->{PrintError} = 0;
38 my $builder = t::lib::TestBuilder->new;
39 C4::Context->interface('commandline');
41 subtest 'new' => sub {
43 plan tests => 2;
45 $schema->storage->txn_begin;
47 throws_ok { Koha::Account->new(); } qr/No patron id passed in!/, 'Croaked on bad call to new';
49 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
50 my $account = Koha::Account->new( { patron_id => $patron->borrowernumber } );
51 is( defined $account, 1, "Account is defined" );
53 $schema->storage->txn_rollback;
56 subtest 'outstanding_debits() tests' => sub {
58 plan tests => 22;
60 $schema->storage->txn_begin;
62 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
63 my $account = $patron->account;
65 my @generated_lines;
66 push @generated_lines, $account->add_debit({ amount => 1, interface => 'commandline', type => 'OVERDUE' });
67 push @generated_lines, $account->add_debit({ amount => 2, interface => 'commandline', type => 'OVERDUE' });
68 push @generated_lines, $account->add_debit({ amount => 3, interface => 'commandline', type => 'OVERDUE' });
69 push @generated_lines, $account->add_debit({ amount => 4, interface => 'commandline', type => 'OVERDUE' });
71 my $lines = $account->outstanding_debits();
72 my @lines_arr = $account->outstanding_debits();
74 is( ref($lines), 'Koha::Account::Lines', 'Called in scalar context, outstanding_debits returns a Koha::Account::Lines object' );
75 is( $lines->total_outstanding, 10, 'Outstandig debits total is correctly calculated' );
77 my $i = 0;
78 foreach my $line ( @{ $lines->as_list } ) {
79 my $fetched_line = Koha::Account::Lines->find( $generated_lines[$i]->id );
80 is_deeply( $line->unblessed, $fetched_line->unblessed, "Fetched line matches the generated one ($i)" );
81 is_deeply( $lines_arr[$i]->unblessed, $fetched_line->unblessed, "Fetched line matches the generated one ($i)" );
82 is( ref($lines_arr[$i]), 'Koha::Account::Line', 'outstanding_debits returns a list of Koha::Account::Line objects in list context' );
83 $i++;
85 my $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
86 Koha::Account::Line->new(
88 borrowernumber => $patron_2->id,
89 amountoutstanding => -2,
90 interface => 'commandline',
91 credit_type_code => 'PAYMENT'
93 )->store;
94 my $just_one = Koha::Account::Line->new(
96 borrowernumber => $patron_2->id,
97 amount => 3,
98 amountoutstanding => 3,
99 interface => 'commandline',
100 debit_type_code => 'OVERDUE'
102 )->store;
103 Koha::Account::Line->new(
105 borrowernumber => $patron_2->id,
106 amount => -6,
107 amountoutstanding => -6,
108 interface => 'commandline',
109 credit_type_code => 'PAYMENT'
111 )->store;
112 $lines = $patron_2->account->outstanding_debits();
113 is( $lines->total_outstanding, 3, "Total if some outstanding debits and some credits is only debits" );
114 is( $lines->count, 1, "With 1 outstanding debits, we get back a Lines object with 1 lines" );
115 my $the_line = Koha::Account::Lines->find( $just_one->id );
116 is_deeply( $the_line->unblessed, $lines->next->unblessed, "We get back the one correct line");
118 my $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
119 my $account_3 = $patron_3->account;
120 $account_3->add_credit( { amount => 2, interface => 'commandline' } );
121 $account_3->add_credit( { amount => 20, interface => 'commandline' } );
122 $account_3->add_credit( { amount => 200, interface => 'commandline' } );
123 $lines = $account_3->outstanding_debits();
124 is( $lines->total_outstanding, 0, "Total if no outstanding debits total is 0" );
125 is( $lines->count, 0, "With 0 outstanding debits, we get back a Lines object with 0 lines" );
127 my $patron_4 = $builder->build_object({ class => 'Koha::Patrons' });
128 my $account_4 = $patron_4->account;
129 $lines = $account_4->outstanding_debits();
130 is( $lines->total_outstanding, 0, "Total if no outstanding debits is 0" );
131 is( $lines->count, 0, "With no outstanding debits, we get back a Lines object with 0 lines" );
133 # create a pathological credit with amountoutstanding > 0 (BZ 14591)
134 Koha::Account::Line->new(
136 borrowernumber => $patron_4->id,
137 amount => -3,
138 amountoutstanding => 3,
139 interface => 'commandline',
140 credit_type_code => 'PAYMENT'
142 )->store();
143 $lines = $account_4->outstanding_debits();
144 is( $lines->count, 0, 'No credits are confused with debits because of the amountoutstanding value' );
146 $schema->storage->txn_rollback;
149 subtest 'outstanding_credits() tests' => sub {
151 plan tests => 17;
153 $schema->storage->txn_begin;
155 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
156 my $account = $patron->account;
158 my @generated_lines;
159 push @generated_lines, $account->add_credit({ amount => 1, interface => 'commandline' });
160 push @generated_lines, $account->add_credit({ amount => 2, interface => 'commandline' });
161 push @generated_lines, $account->add_credit({ amount => 3, interface => 'commandline' });
162 push @generated_lines, $account->add_credit({ amount => 4, interface => 'commandline' });
164 my $lines = $account->outstanding_credits();
165 my @lines_arr = $account->outstanding_credits();
167 is( ref($lines), 'Koha::Account::Lines', 'Called in scalar context, outstanding_credits returns a Koha::Account::Lines object' );
168 is( $lines->total_outstanding, -10, 'Outstandig credits total is correctly calculated' );
170 my $i = 0;
171 foreach my $line ( @{ $lines->as_list } ) {
172 my $fetched_line = Koha::Account::Lines->find( $generated_lines[$i]->id );
173 is_deeply( $line->unblessed, $fetched_line->unblessed, "Fetched line matches the generated one ($i)" );
174 is_deeply( $lines_arr[$i]->unblessed, $fetched_line->unblessed, "Fetched line matches the generated one ($i)" );
175 is( ref($lines_arr[$i]), 'Koha::Account::Line', 'outstanding_debits returns a list of Koha::Account::Line objects in list context' );
176 $i++;
179 my $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
180 $account = $patron_2->account;
181 $lines = $account->outstanding_credits();
182 is( $lines->total_outstanding, 0, "Total if no outstanding credits is 0" );
183 is( $lines->count, 0, "With no outstanding credits, we get back a Lines object with 0 lines" );
185 # create a pathological debit with amountoutstanding < 0 (BZ 14591)
186 Koha::Account::Line->new(
188 borrowernumber => $patron_2->id,
189 amount => 2,
190 amountoutstanding => -3,
191 interface => 'commandline',
192 debit_type_code => 'OVERDUE'
194 )->store();
195 $lines = $account->outstanding_credits();
196 is( $lines->count, 0, 'No debits are confused with credits because of the amountoutstanding value' );
198 $schema->storage->txn_rollback;
201 subtest 'add_credit() tests' => sub {
203 plan tests => 17;
205 $schema->storage->txn_begin;
207 # delete logs and statistics
208 my $action_logs = $schema->resultset('ActionLog')->search()->count;
209 my $statistics = $schema->resultset('Statistic')->search()->count;
211 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
212 my $account = Koha::Account->new( { patron_id => $patron->borrowernumber } );
214 is( $account->balance, 0, 'Patron has no balance' );
216 # Disable logs
217 t::lib::Mocks::mock_preference( 'FinesLog', 0 );
219 throws_ok {
220 $account->add_credit(
221 { amount => 25,
222 description => 'Payment of 25',
223 library_id => $patron->branchcode,
224 note => 'not really important',
225 type => 'PAYMENT',
226 user_id => $patron->id
230 'Koha::Exceptions::MissingParameter', 'Exception thrown if interface parameter missing';
232 my $line_1 = $account->add_credit(
233 { amount => 25,
234 description => 'Payment of 25',
235 library_id => $patron->branchcode,
236 note => 'not really important',
237 type => 'PAYMENT',
238 user_id => $patron->id,
239 interface => 'commandline'
243 is( $account->balance, -25, 'Patron has a balance of -25' );
244 is( $schema->resultset('ActionLog')->count(), $action_logs + 0, 'No log was added' );
245 is( $schema->resultset('Statistic')->count(), $statistics + 1, 'Action added to statistics' );
246 is( $line_1->credit_type_code, 'PAYMENT', 'Account type is correctly set' );
248 # Enable logs
249 t::lib::Mocks::mock_preference( 'FinesLog', 1 );
251 my $line_2 = $account->add_credit(
252 { amount => 37,
253 description => 'Payment of 37',
254 library_id => $patron->branchcode,
255 note => 'not really important',
256 user_id => $patron->id,
257 interface => 'commandline'
261 is( $account->balance, -62, 'Patron has a balance of -25' );
262 is( $schema->resultset('ActionLog')->count(), $action_logs + 1, 'Log was added' );
263 is( $schema->resultset('Statistic')->count(), $statistics + 2, 'Action added to statistics' );
264 is( $line_2->credit_type_code, 'PAYMENT', 'Account type is correctly set' );
266 # offsets have the credit_id set to accountlines_id, and debit_id is undef
267 my $offset_1 = Koha::Account::Offsets->search({ credit_id => $line_1->id })->next;
268 my $offset_2 = Koha::Account::Offsets->search({ credit_id => $line_2->id })->next;
270 is( $offset_1->credit_id, $line_1->id, 'No debit_id is set for credits' );
271 is( $offset_1->debit_id, undef, 'No debit_id is set for credits' );
272 is( $offset_2->credit_id, $line_2->id, 'No debit_id is set for credits' );
273 is( $offset_2->debit_id, undef, 'No debit_id is set for credits' );
275 my $line_3 = $account->add_credit(
277 amount => 20,
278 description => 'Manual credit applied',
279 library_id => $patron->branchcode,
280 user_id => $patron->id,
281 type => 'FORGIVEN',
282 interface => 'commandline'
286 is( $schema->resultset('ActionLog')->count(), $action_logs + 2, 'Log was added' );
287 is( $schema->resultset('Statistic')->count(), $statistics + 2, 'No action added to statistics, because of credit type' );
289 # Enable cash registers
290 t::lib::Mocks::mock_preference( 'UseCashRegisters', 1 );
291 throws_ok {
292 $account->add_credit(
294 amount => 20,
295 description => 'Cash payment without cash register',
296 library_id => $patron->branchcode,
297 user_id => $patron->id,
298 payment_type => 'CASH',
299 interface => 'intranet'
303 'Koha::Exceptions::Account::RegisterRequired',
304 'Exception thrown for UseCashRegisters:1 + payment_type:CASH + cash_register:undef';
306 # Disable cash registers
307 t::lib::Mocks::mock_preference( 'UseCashRegisters', 1 );
309 $schema->storage->txn_rollback;
312 subtest 'add_debit() tests' => sub {
314 plan tests => 14;
316 $schema->storage->txn_begin;
318 # delete logs and statistics
319 my $action_logs = $schema->resultset('ActionLog')->search()->count;
320 my $statistics = $schema->resultset('Statistic')->search()->count;
322 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
323 my $account =
324 Koha::Account->new( { patron_id => $patron->borrowernumber } );
326 is( $account->balance, 0, 'Patron has no balance' );
328 throws_ok {
329 $account->add_debit(
331 amount => -5,
332 description => 'amount validation failure',
333 library_id => $patron->branchcode,
334 note => 'this should fail anyway',
335 type => 'RENT',
336 user_id => $patron->id,
337 interface => 'commandline'
339 ); } 'Koha::Exceptions::Account::AmountNotPositive', 'Expected validation exception thrown (amount)';
341 throws_ok {
342 $account->add_debit(
344 amount => 5,
345 description => 'type validation failure',
346 library_id => $patron->branchcode,
347 note => 'this should fail anyway',
348 type => 'failure',
349 user_id => $patron->id,
350 interface => 'commandline'
352 ); } 'Koha::Exceptions::Account::UnrecognisedType', 'Expected validation exception thrown (type)';
354 throws_ok {
355 $account->add_debit(
357 amount => 25,
358 description => 'Rental charge of 25',
359 library_id => $patron->branchcode,
360 note => 'not really important',
361 type => 'RENT',
362 user_id => $patron->id
364 ); } 'Koha::Exceptions::MissingParameter', 'Exception thrown if interface parameter missing';
366 # Disable logs
367 t::lib::Mocks::mock_preference( 'FinesLog', 0 );
369 my $line_1 = $account->add_debit(
371 amount => 25,
372 description => 'Rental charge of 25',
373 library_id => $patron->branchcode,
374 note => 'not really important',
375 type => 'RENT',
376 user_id => $patron->id,
377 interface => 'commandline'
381 is( $account->balance, 25, 'Patron has a balance of 25' );
383 $schema->resultset('ActionLog')->count(),
384 $action_logs + 0,
385 'No log was added'
388 $line_1->debit_type_code,
389 'RENT',
390 'Account type is correctly set'
393 # Enable logs
394 t::lib::Mocks::mock_preference( 'FinesLog', 1 );
396 my $line_2 = $account->add_debit(
398 amount => 37,
399 description => 'Rental charge of 37',
400 library_id => $patron->branchcode,
401 note => 'not really important',
402 type => 'RENT',
403 user_id => $patron->id,
404 interface => 'commandline'
408 is( $account->balance, 62, 'Patron has a balance of 62' );
410 $schema->resultset('ActionLog')->count(),
411 $action_logs + 1,
412 'Log was added'
415 $line_2->debit_type_code,
416 'RENT',
417 'Account type is correctly set'
420 # offsets have the debit_id set to accountlines_id, and credit_id is undef
421 my $offset_1 =
422 Koha::Account::Offsets->search( { debit_id => $line_1->id } )->next;
423 my $offset_2 =
424 Koha::Account::Offsets->search( { debit_id => $line_2->id } )->next;
426 is( $offset_1->debit_id, $line_1->id, 'debit_id is set for debit 1' );
427 is( $offset_1->credit_id, undef, 'credit_id is not set for debit 1' );
428 is( $offset_2->debit_id, $line_2->id, 'debit_id is set for debit 2' );
429 is( $offset_2->credit_id, undef, 'credit_id is not set for debit 2' );
431 $schema->storage->txn_rollback;
434 subtest 'lines() tests' => sub {
436 plan tests => 1;
438 $schema->storage->txn_begin;
440 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
441 my $account = $patron->account;
443 # Add Credits
444 $account->add_credit({ amount => 1, interface => 'commandline' });
445 $account->add_credit({ amount => 2, interface => 'commandline' });
446 $account->add_credit({ amount => 3, interface => 'commandline' });
447 $account->add_credit({ amount => 4, interface => 'commandline' });
449 # Add Debits
450 $account->add_debit({ amount => 1, interface => 'commandline', type => 'OVERDUE' });
451 $account->add_debit({ amount => 2, interface => 'commandline', type => 'OVERDUE' });
452 $account->add_debit({ amount => 3, interface => 'commandline', type => 'OVERDUE' });
453 $account->add_debit({ amount => 4, interface => 'commandline', type => 'OVERDUE' });
455 # Paid Off
456 $account->add_credit( { amount => 1, interface => 'commandline' } )
457 ->apply( { debits => [ $account->outstanding_debits->as_list ] } );
459 my $lines = $account->lines;
460 is( $lines->_resultset->count, 9, "All accountlines (debits, credits and paid off) were fetched");
462 $schema->storage->txn_rollback;
465 subtest 'reconcile_balance' => sub {
467 plan tests => 4;
469 subtest 'more credit than debit' => sub {
471 plan tests => 6;
473 $schema->storage->txn_begin;
475 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
476 my $account = $patron->account;
478 # Add Credits
479 $account->add_credit({ amount => 1, interface => 'commandline' });
480 $account->add_credit({ amount => 2, interface => 'commandline' });
481 $account->add_credit({ amount => 3, interface => 'commandline' });
482 $account->add_credit({ amount => 4, interface => 'commandline' });
483 $account->add_credit({ amount => 5, interface => 'commandline' });
485 # Add Debits
486 $account->add_debit({ amount => 1, interface => 'commandline', type => 'OVERDUE' });
487 $account->add_debit({ amount => 2, interface => 'commandline', type => 'OVERDUE' });
488 $account->add_debit({ amount => 3, interface => 'commandline', type => 'OVERDUE' });
489 $account->add_debit({ amount => 4, interface => 'commandline', type => 'OVERDUE' });
491 # Paid Off
492 Koha::Account::Line->new(
494 borrowernumber => $patron->id,
495 amount => 1,
496 amountoutstanding => 0,
497 interface => 'commandline',
498 debit_type_code => 'OVERDUE'
500 )->store;
501 Koha::Account::Line->new(
503 borrowernumber => $patron->id,
504 amount => 1,
505 amountoutstanding => 0,
506 interface => 'commandline',
507 debit_type_code => 'OVERDUE'
509 )->store;
511 is( $account->balance(), -5, "Account balance is -5" );
512 is( $account->outstanding_debits->total_outstanding, 10, 'Outstanding debits sum 10' );
513 is( $account->outstanding_credits->total_outstanding, -15, 'Outstanding credits sum -15' );
515 $account->reconcile_balance();
517 is( $account->balance(), -5, "Account balance is -5" );
518 is( $account->outstanding_debits->total_outstanding, 0, 'No outstanding debits' );
519 is( $account->outstanding_credits->total_outstanding, -5, 'Outstanding credits sum -5' );
521 $schema->storage->txn_rollback;
524 subtest 'same debit as credit' => sub {
526 plan tests => 6;
528 $schema->storage->txn_begin;
530 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
531 my $account = $patron->account;
533 # Add Credits
534 $account->add_credit({ amount => 1, interface => 'commandline' });
535 $account->add_credit({ amount => 2, interface => 'commandline' });
536 $account->add_credit({ amount => 3, interface => 'commandline' });
537 $account->add_credit({ amount => 4, interface => 'commandline' });
539 # Add Debits
540 $account->add_debit({ amount => 1, interface => 'commandline', type => 'OVERDUE' });
541 $account->add_debit({ amount => 2, interface => 'commandline', type => 'OVERDUE' });
542 $account->add_debit({ amount => 3, interface => 'commandline', type => 'OVERDUE' });
543 $account->add_debit({ amount => 4, interface => 'commandline', type => 'OVERDUE' });
545 # Paid Off
546 Koha::Account::Line->new(
548 borrowernumber => $patron->id,
549 amount => 1,
550 amountoutstanding => 0,
551 interface => 'commandline',
552 debit_type_code => 'OVERDUE'
554 )->store;
555 Koha::Account::Line->new(
557 borrowernumber => $patron->id,
558 amount => 1,
559 amountoutstanding => 0,
560 interface => 'commandline',
561 debit_type_code => 'OVERDUE'
563 )->store;
565 is( $account->balance(), 0, "Account balance is 0" );
566 is( $account->outstanding_debits->total_outstanding, 10, 'Outstanding debits sum 10' );
567 is( $account->outstanding_credits->total_outstanding, -10, 'Outstanding credits sum -10' );
569 $account->reconcile_balance();
571 is( $account->balance(), 0, "Account balance is 0" );
572 is( $account->outstanding_debits->total_outstanding, 0, 'No outstanding debits' );
573 is( $account->outstanding_credits->total_outstanding, 0, 'Outstanding credits sum 0' );
575 $schema->storage->txn_rollback;
578 subtest 'more debit than credit' => sub {
580 plan tests => 6;
582 $schema->storage->txn_begin;
584 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
585 my $account = $patron->account;
587 # Add Credits
588 $account->add_credit({ amount => 1, interface => 'commandline' });
589 $account->add_credit({ amount => 2, interface => 'commandline' });
590 $account->add_credit({ amount => 3, interface => 'commandline' });
591 $account->add_credit({ amount => 4, interface => 'commandline' });
593 # Add Debits
594 $account->add_debit({ amount => 1, interface => 'commandline', type => 'OVERDUE' });
595 $account->add_debit({ amount => 2, interface => 'commandline', type => 'OVERDUE' });
596 $account->add_debit({ amount => 3, interface => 'commandline', type => 'OVERDUE' });
597 $account->add_debit({ amount => 4, interface => 'commandline', type => 'OVERDUE' });
598 $account->add_debit({ amount => 5, interface => 'commandline', type => 'OVERDUE' });
600 # Paid Off
601 Koha::Account::Line->new(
603 borrowernumber => $patron->id,
604 amount => 1,
605 amountoutstanding => 0,
606 interface => 'commandline',
607 debit_type_code => 'OVERDUE'
609 )->store;
610 Koha::Account::Line->new(
612 borrowernumber => $patron->id,
613 amount => 1,
614 amountoutstanding => 0,
615 interface => 'commandline',
616 debit_type_code => 'OVERDUE'
618 )->store;
620 is( $account->balance(), 5, "Account balance is 5" );
621 is( $account->outstanding_debits->total_outstanding, 15, 'Outstanding debits sum 15' );
622 is( $account->outstanding_credits->total_outstanding, -10, 'Outstanding credits sum -10' );
624 $account->reconcile_balance();
626 is( $account->balance(), 5, "Account balance is 5" );
627 is( $account->outstanding_debits->total_outstanding, 5, 'Outstanding debits sum 5' );
628 is( $account->outstanding_credits->total_outstanding, 0, 'Outstanding credits sum 0' );
630 $schema->storage->txn_rollback;
633 subtest 'credits are applied to older debits first' => sub {
635 plan tests => 9;
637 $schema->storage->txn_begin;
639 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
640 my $account = $patron->account;
642 # Add Credits
643 $account->add_credit({ amount => 1, interface => 'commandline' });
644 $account->add_credit({ amount => 3, interface => 'commandline' });
646 # Add Debits
647 my $debit_1 = $account->add_debit({ amount => 1, interface => 'commandline', type => 'OVERDUE' });
648 my $debit_2 = $account->add_debit({ amount => 2, interface => 'commandline', type => 'OVERDUE' });
649 my $debit_3 = $account->add_debit({ amount => 3, interface => 'commandline', type => 'OVERDUE' });
651 is( $account->balance(), 2, "Account balance is 2" );
652 is( $account->outstanding_debits->total_outstanding, 6, 'Outstanding debits sum 6' );
653 is( $account->outstanding_credits->total_outstanding, -4, 'Outstanding credits sum -4' );
655 $account->reconcile_balance();
657 is( $account->balance(), 2, "Account balance is 2" );
658 is( $account->outstanding_debits->total_outstanding, 2, 'Outstanding debits sum 2' );
659 is( $account->outstanding_credits->total_outstanding, 0, 'Outstanding credits sum 0' );
661 $debit_1->discard_changes;
662 is( $debit_1->amountoutstanding + 0, 0, 'Old debit payed' );
663 $debit_2->discard_changes;
664 is( $debit_2->amountoutstanding + 0, 0, 'Old debit payed' );
665 $debit_3->discard_changes;
666 is( $debit_3->amountoutstanding + 0, 2, 'Newest debit only partially payed' );
668 $schema->storage->txn_rollback;
672 subtest 'pay() tests' => sub {
674 plan tests => 3;
676 $schema->storage->txn_begin;
678 # Disable renewing upon fine payment
679 t::lib::Mocks::mock_preference( 'RenewAccruingItemWhenPaid', 0 );
681 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
682 my $library = $builder->build_object({ class => 'Koha::Libraries' });
683 my $account = $patron->account;
685 my $context = Test::MockModule->new('C4::Context');
686 $context->mock( 'userenv', { branch => $library->id } );
688 my $credit_1_id = $account->pay({ amount => 200 })->{payment_id};
689 my $credit_1 = Koha::Account::Lines->find( $credit_1_id );
691 is( $credit_1->branchcode, undef, 'No branchcode is set if library_id was not passed' );
693 my $credit_2_id = $account->pay({ amount => 150, library_id => $library->id })->{payment_id};
694 my $credit_2 = Koha::Account::Lines->find( $credit_2_id );
696 is( $credit_2->branchcode, $library->id, 'branchcode set because library_id was passed' );
698 # Enable cash registers
699 t::lib::Mocks::mock_preference( 'UseCashRegisters', 1 );
700 throws_ok {
701 $account->pay(
703 amount => 20,
704 payment_type => 'CASH',
705 interface => 'intranet'
709 'Koha::Exceptions::Account::RegisterRequired',
710 'Exception thrown for UseCashRegisters:1 + payment_type:CASH + cash_register:undef';
712 # Disable cash registers
713 t::lib::Mocks::mock_preference( 'UseCashRegisters', 1 );
715 $schema->storage->txn_rollback;
718 subtest 'pay() handles lost items when paying a specific lost fee' => sub {
720 plan tests => 5;
722 $schema->storage->txn_begin;
724 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
725 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
726 my $account = $patron->account;
728 my $context = Test::MockModule->new('C4::Context');
729 $context->mock( 'userenv', { branch => $library->id } );
731 my $biblio = $builder->build_sample_biblio();
732 my $item =
733 $builder->build_sample_item( { biblionumber => $biblio->biblionumber } );
735 my $checkout = Koha::Checkout->new(
737 borrowernumber => $patron->id,
738 itemnumber => $item->id,
739 date_due => \'NOW()',
740 branchcode => $patron->branchcode,
741 issuedate => \'NOW()',
743 )->store();
745 $item->itemlost('1')->store();
747 my $accountline = Koha::Account::Line->new(
749 issue_id => $checkout->id,
750 borrowernumber => $patron->id,
751 itemnumber => $item->id,
752 date => \'NOW()',
753 debit_type_code => 'LOST',
754 interface => 'cli',
755 amount => '1',
756 amountoutstanding => '1',
758 )->store();
760 $account->pay(
762 amount => .5,
763 library_id => $library->id,
764 lines => [$accountline],
768 $accountline = Koha::Account::Lines->find( $accountline->id );
769 is( $accountline->amountoutstanding+0, .5, 'Account line was paid down by half' );
771 $checkout = Koha::Checkouts->find( $checkout->id );
772 ok( $checkout, 'Item still checked out to patron' );
774 $account->pay(
776 amount => 0.5,
777 library_id => $library->id,
778 lines => [$accountline],
782 $accountline = Koha::Account::Lines->find( $accountline->id );
783 is( $accountline->amountoutstanding+0, 0, 'Account line was paid down by half' );
785 $checkout = Koha::Checkouts->find( $checkout->id );
786 ok( !$checkout, 'Item was removed from patron account' );
788 subtest 'item was not checked out to the same patron' => sub {
789 plan tests => 1;
791 my $patron_2 = $builder->build_object(
793 class => 'Koha::Patrons',
794 value => { branchcode => $library->branchcode }
797 $item->itemlost('1')->store();
798 C4::Accounts::chargelostitem( $patron->borrowernumber, $item->itemnumber, 5, "lost" );
799 my $accountline = Koha::Account::Lines->search(
801 borrowernumber => $patron->borrowernumber,
802 itemnumber => $item->itemnumber,
803 debit_type_code => 'LOST'
805 )->next;
806 my $checkout = Koha::Checkout->new(
808 borrowernumber => $patron_2->borrowernumber,
809 itemnumber => $item->itemnumber,
810 date_due => \'NOW()',
811 branchcode => $patron_2->branchcode,
812 issuedate => \'NOW()',
814 )->store();
816 $patron->account->pay(
818 amount => 5,
819 library_id => $library->branchcode,
820 lines => [$accountline],
825 Koha::Checkouts->find( $checkout->issue_id ),
826 'If the item is checked out to another patron, a lost item should not be returned if lost fee is paid'
831 $schema->storage->txn_rollback;
834 subtest 'pay() handles lost items when paying by amount ( not specifying the lost fee )' => sub {
836 plan tests => 4;
838 $schema->storage->txn_begin;
840 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
841 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
842 my $account = $patron->account;
844 my $context = Test::MockModule->new('C4::Context');
845 $context->mock( 'userenv', { branch => $library->id } );
847 my $biblio = $builder->build_sample_biblio();
848 my $item =
849 $builder->build_sample_item( { biblionumber => $biblio->biblionumber } );
851 my $checkout = Koha::Checkout->new(
853 borrowernumber => $patron->id,
854 itemnumber => $item->id,
855 date_due => \'NOW()',
856 branchcode => $patron->branchcode,
857 issuedate => \'NOW()',
859 )->store();
861 $item->itemlost('1')->store();
863 my $accountline = Koha::Account::Line->new(
865 issue_id => $checkout->id,
866 borrowernumber => $patron->id,
867 itemnumber => $item->id,
868 date => \'NOW()',
869 debit_type_code => 'LOST',
870 interface => 'cli',
871 amount => '1',
872 amountoutstanding => '1',
874 )->store();
876 $account->pay(
878 amount => .5,
879 library_id => $library->id,
883 $accountline = Koha::Account::Lines->find( $accountline->id );
884 is( $accountline->amountoutstanding+0, .5, 'Account line was paid down by half' );
886 $checkout = Koha::Checkouts->find( $checkout->id );
887 ok( $checkout, 'Item still checked out to patron' );
889 $account->pay(
891 amount => .5,,
892 library_id => $library->id,
896 $accountline = Koha::Account::Lines->find( $accountline->id );
897 is( $accountline->amountoutstanding+0, 0, 'Account line was paid down by half' );
899 $checkout = Koha::Checkouts->find( $checkout->id );
900 ok( !$checkout, 'Item was removed from patron account' );
902 $schema->storage->txn_rollback;
905 subtest 'pay() renews items when appropriate' => sub {
907 plan tests => 1;
909 $schema->storage->txn_begin;
911 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
912 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
913 my $account = $patron->account;
915 my $context = Test::MockModule->new('C4::Context');
916 $context->mock( 'userenv', { branch => $library->id } );
918 my $biblio = $builder->build_sample_biblio();
919 my $item =
920 $builder->build_sample_item( { biblionumber => $biblio->biblionumber } );
922 my $now = dt_from_string();
923 my $seven_weeks = DateTime::Duration->new(weeks => 7);
924 my $five_weeks = DateTime::Duration->new(weeks => 5);
925 my $seven_weeks_ago = $now - $seven_weeks;
926 my $five_weeks_ago = $now - $five_weeks;
928 my $checkout = Koha::Checkout->new(
930 borrowernumber => $patron->id,
931 itemnumber => $item->id,
932 date_due => $five_weeks_ago,
933 branchcode => $patron->branchcode,
934 issuedate => $seven_weeks_ago
936 )->store();
938 my $accountline = Koha::Account::Line->new(
940 issue_id => $checkout->id,
941 borrowernumber => $patron->id,
942 itemnumber => $item->id,
943 date => \'NOW()',
944 debit_type_code => 'OVERDUE',
945 status => 'UNRETURNED',
946 interface => 'cli',
947 amount => '1',
948 amountoutstanding => '1',
950 )->store();
952 # Enable renewing upon fine payment
953 t::lib::Mocks::mock_preference( 'RenewAccruingItemWhenPaid', 1 );
954 my $called = 0;
955 my $module = new Test::MockModule('C4::Circulation');
956 $module->mock('AddRenewal', sub { $called = 1; });
957 $module->mock('CanBookBeRenewed', sub { return 1; });
958 $account->pay(
960 amount => '1',
961 library_id => $library->id,
965 is( $called, 1, 'RenewAccruingItemWhenPaid causes C4::Circulation::AddRenew to be called when appropriate' );
967 $schema->storage->txn_rollback;
970 subtest 'Koha::Account::Line::apply() handles lost items' => sub {
972 plan tests => 4;
974 $schema->storage->txn_begin;
976 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
977 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
978 my $account = $patron->account;
980 my $context = Test::MockModule->new('C4::Context');
981 $context->mock( 'userenv', { branch => $library->id } );
983 my $biblio = $builder->build_sample_biblio();
984 my $item =
985 $builder->build_sample_item( { biblionumber => $biblio->biblionumber } );
987 my $checkout = Koha::Checkout->new(
989 borrowernumber => $patron->id,
990 itemnumber => $item->id,
991 date_due => \'NOW()',
992 branchcode => $patron->branchcode,
993 issuedate => \'NOW()',
995 )->store();
997 $item->itemlost('1')->store();
999 my $debit = Koha::Account::Line->new(
1001 issue_id => $checkout->id,
1002 borrowernumber => $patron->id,
1003 itemnumber => $item->id,
1004 date => \'NOW()',
1005 debit_type_code => 'LOST',
1006 interface => 'cli',
1007 amount => '1',
1008 amountoutstanding => '1',
1010 )->store();
1012 my $credit = Koha::Account::Line->new(
1014 borrowernumber => $patron->id,
1015 date => '1970-01-01 00:00:01',
1016 amount => -.5,
1017 amountoutstanding => -.5,
1018 interface => 'commandline',
1019 credit_type_code => 'PAYMENT'
1021 )->store();
1022 my $debits = $account->outstanding_debits;
1023 $credit->apply({ debits => [ $debits->as_list ] });
1025 $debit = Koha::Account::Lines->find( $debit->id );
1026 is( $debit->amountoutstanding+0, .5, 'Account line was paid down by half' );
1028 $checkout = Koha::Checkouts->find( $checkout->id );
1029 ok( $checkout, 'Item still checked out to patron' );
1031 $credit = Koha::Account::Line->new(
1033 borrowernumber => $patron->id,
1034 date => '1970-01-01 00:00:01',
1035 amount => -.5,
1036 amountoutstanding => -.5,
1037 interface => 'commandline',
1038 credit_type_code => 'PAYMENT'
1040 )->store();
1041 $debits = $account->outstanding_debits;
1042 $credit->apply({ debits => [ $debits->as_list ] });
1044 $debit = Koha::Account::Lines->find( $debit->id );
1045 is( $debit->amountoutstanding+0, 0, 'Account line was paid down by half' );
1047 $checkout = Koha::Checkouts->find( $checkout->id );
1048 ok( !$checkout, 'Item was removed from patron account' );
1050 $schema->storage->txn_rollback;
1053 subtest 'Koha::Account::pay() generates credit number' => sub {
1054 plan tests => 34;
1056 $schema->storage->txn_begin;
1058 Koha::Account::Lines->delete();
1060 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
1061 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
1062 my $account = $patron->account;
1064 my $context = Test::MockModule->new('C4::Context');
1065 $context->mock( 'userenv', { branch => $library->id } );
1067 my $now = DateTime->now;
1068 my $year = $now->year;
1069 my $month = $now->month;
1070 my ($accountlines_id, $accountline);
1072 t::lib::Mocks::mock_preference('AutoCreditNumber', '');
1073 $accountlines_id = $account->pay({ amount => '1.00', library_id => $library->id })->{payment_id};
1074 $accountline = Koha::Account::Lines->find($accountlines_id);
1075 is($accountline->credit_number, undef, 'No credit number is generated when syspref is off');
1077 t::lib::Mocks::mock_preference('AutoCreditNumber', 'incremental');
1078 for my $i (1..11) {
1079 $accountlines_id = $account->pay({ amount => '1.00', library_id => $library->id })->{payment_id};
1080 $accountline = Koha::Account::Lines->find($accountlines_id);
1081 is($accountline->credit_number, $i);
1084 t::lib::Mocks::mock_preference('AutoCreditNumber', 'annual');
1085 for my $i (1..11) {
1086 $accountlines_id = $account->pay({ amount => '1.00', library_id => $library->id })->{payment_id};
1087 $accountline = Koha::Account::Lines->find($accountlines_id);
1088 is($accountline->credit_number, sprintf('%s-%04d', $year, $i));
1091 t::lib::Mocks::mock_preference('AutoCreditNumber', 'branchyyyymmincr');
1092 for my $i (1..11) {
1093 $accountlines_id = $account->pay({ amount => '1.00', library_id => $library->id })->{payment_id};
1094 $accountline = Koha::Account::Lines->find($accountlines_id);
1095 is($accountline->credit_number, sprintf('%s%d%02d%04d', $library->id, $year, $month, $i));
1098 $schema->storage->txn_rollback;