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
=> 13;
29 use Koha
::Account
::CreditTypes
;
30 use Koha
::Account
::Lines
;
31 use Koha
::Account
::Offsets
;
32 use Koha
::DateUtils
qw( dt_from_string );
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 {
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 {
61 $schema->storage->txn_begin;
63 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
64 my $account = $patron->account;
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' );
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' );
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'
95 my $just_one = Koha
::Account
::Line
->new(
97 borrowernumber
=> $patron_2->id,
99 amountoutstanding
=> 3,
100 interface
=> 'commandline',
101 debit_type_code
=> 'OVERDUE'
104 Koha
::Account
::Line
->new(
106 borrowernumber
=> $patron_2->id,
108 amountoutstanding
=> -6,
109 interface
=> 'commandline',
110 credit_type_code
=> 'PAYMENT'
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,
139 amountoutstanding
=> 3,
140 interface
=> 'commandline',
141 credit_type_code
=> 'PAYMENT'
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 {
154 $schema->storage->txn_begin;
156 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
157 my $account = $patron->account;
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' );
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' );
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,
191 amountoutstanding
=> -3,
192 interface
=> 'commandline',
193 debit_type_code
=> 'OVERDUE'
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 {
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' );
218 t
::lib
::Mocks
::mock_preference
( 'FinesLog', 0 );
221 $account->add_credit(
223 description
=> 'Payment of 25',
224 library_id
=> $patron->branchcode,
225 note
=> 'not really important',
227 user_id
=> $patron->id
231 'Koha::Exceptions::MissingParameter', 'Exception thrown if interface parameter missing';
233 my $line_1 = $account->add_credit(
235 description
=> 'Payment of 25',
236 library_id
=> $patron->branchcode,
237 note
=> 'not really important',
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' );
250 t
::lib
::Mocks
::mock_preference
( 'FinesLog', 1 );
252 my $line_2 = $account->add_credit(
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(
279 description
=> 'Manual credit applied',
280 library_id
=> $patron->branchcode,
281 user_id
=> $patron->id,
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 );
293 $account->add_credit(
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 {
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' } );
325 Koha
::Account
->new( { patron_id
=> $patron->borrowernumber } );
327 is
( $account->balance, 0, 'Patron has no balance' );
333 description
=> 'amount validation failure',
334 library_id
=> $patron->branchcode,
335 note
=> 'this should fail anyway',
337 user_id
=> $patron->id,
338 interface
=> 'commandline'
340 ); } 'Koha::Exceptions::Account::AmountNotPositive', 'Expected validation exception thrown (amount)';
344 open STDERR
, '>', '/dev/null';
348 description
=> 'type validation failure',
349 library_id
=> $patron->branchcode,
350 note
=> 'this should fail anyway',
352 user_id
=> $patron->id,
353 interface
=> 'commandline'
358 'Koha::Exceptions::Account::UnrecognisedType',
359 'Expected validation exception thrown (type)';
365 description
=> 'Rental charge of 25',
366 library_id
=> $patron->branchcode,
367 note
=> 'not really important',
369 user_id
=> $patron->id
371 ); } 'Koha::Exceptions::MissingParameter', 'Exception thrown if interface parameter missing';
374 t
::lib
::Mocks
::mock_preference
( 'FinesLog', 0 );
376 my $line_1 = $account->add_debit(
379 description
=> 'Rental charge of 25',
380 library_id
=> $patron->branchcode,
381 note
=> 'not really important',
383 user_id
=> $patron->id,
384 interface
=> 'commandline'
388 is
( $account->balance, 25, 'Patron has a balance of 25' );
390 $schema->resultset('ActionLog')->count(),
395 $line_1->debit_type_code,
397 'Account type is correctly set'
401 t
::lib
::Mocks
::mock_preference
( 'FinesLog', 1 );
403 my $line_2 = $account->add_debit(
406 description
=> 'Rental charge of 37',
407 library_id
=> $patron->branchcode,
408 note
=> 'not really important',
410 user_id
=> $patron->id,
411 interface
=> 'commandline'
415 is
( $account->balance, 62, 'Patron has a balance of 62' );
417 $schema->resultset('ActionLog')->count(),
422 $line_2->debit_type_code,
424 'Account type is correctly set'
427 # offsets have the debit_id set to accountlines_id, and credit_id is undef
429 Koha
::Account
::Offsets
->search( { debit_id
=> $line_1->id } )->next;
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 {
445 $schema->storage->txn_begin;
447 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
448 my $account = $patron->account;
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' });
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' });
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 {
476 subtest
'more credit than debit' => sub {
480 $schema->storage->txn_begin;
482 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
483 my $account = $patron->account;
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' });
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' });
499 Koha
::Account
::Line
->new(
501 borrowernumber
=> $patron->id,
503 amountoutstanding
=> 0,
504 interface
=> 'commandline',
505 debit_type_code
=> 'OVERDUE'
508 Koha
::Account
::Line
->new(
510 borrowernumber
=> $patron->id,
512 amountoutstanding
=> 0,
513 interface
=> 'commandline',
514 debit_type_code
=> 'OVERDUE'
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 {
535 $schema->storage->txn_begin;
537 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
538 my $account = $patron->account;
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' });
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' });
553 Koha
::Account
::Line
->new(
555 borrowernumber
=> $patron->id,
557 amountoutstanding
=> 0,
558 interface
=> 'commandline',
559 debit_type_code
=> 'OVERDUE'
562 Koha
::Account
::Line
->new(
564 borrowernumber
=> $patron->id,
566 amountoutstanding
=> 0,
567 interface
=> 'commandline',
568 debit_type_code
=> 'OVERDUE'
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 {
589 $schema->storage->txn_begin;
591 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
592 my $account = $patron->account;
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' });
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' });
608 Koha
::Account
::Line
->new(
610 borrowernumber
=> $patron->id,
612 amountoutstanding
=> 0,
613 interface
=> 'commandline',
614 debit_type_code
=> 'OVERDUE'
617 Koha
::Account
::Line
->new(
619 borrowernumber
=> $patron->id,
621 amountoutstanding
=> 0,
622 interface
=> 'commandline',
623 debit_type_code
=> 'OVERDUE'
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 {
644 $schema->storage->txn_begin;
646 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
647 my $account = $patron->account;
650 $account->add_credit({ amount
=> 1, interface
=> 'commandline' });
651 $account->add_credit({ amount
=> 3, interface
=> 'commandline' });
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 {
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 );
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 $schema->storage->txn_rollback;
725 subtest
'pay() handles lost items when paying a specific lost fee' => sub {
729 $schema->storage->txn_begin;
731 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
732 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
733 my $account = $patron->account;
735 my $context = Test
::MockModule
->new('C4::Context');
736 $context->mock( 'userenv', { branch
=> $library->id } );
738 my $biblio = $builder->build_sample_biblio();
740 $builder->build_sample_item( { biblionumber
=> $biblio->biblionumber } );
742 my $checkout = Koha
::Checkout
->new(
744 borrowernumber
=> $patron->id,
745 itemnumber
=> $item->id,
746 date_due
=> \'NOW
()',
747 branchcode => $patron->branchcode,
748 issuedate => \'NOW()',
752 $item->itemlost('1')->store();
754 my $accountline = Koha
::Account
::Line
->new(
756 issue_id
=> $checkout->id,
757 borrowernumber
=> $patron->id,
758 itemnumber
=> $item->id,
760 debit_type_code => 'LOST
',
763 amountoutstanding => '1',
770 library_id => $library->id,
771 lines => [$accountline],
775 $accountline = Koha::Account::Lines->find( $accountline->id );
776 is( $accountline->amountoutstanding+0, .5, 'Account line was paid down by half
' );
778 $checkout = Koha::Checkouts->find( $checkout->id );
779 ok( $checkout, 'Item still checked out to patron
' );
784 library_id => $library->id,
785 lines => [$accountline],
789 $accountline = Koha::Account::Lines->find( $accountline->id );
790 is( $accountline->amountoutstanding+0, 0, 'Account line was paid down by half
' );
792 $checkout = Koha::Checkouts->find( $checkout->id );
793 ok( !$checkout, 'Item was removed from patron account
' );
795 subtest 'item was
not checked out to the same patron
' => sub {
798 my $patron_2 = $builder->build_object(
800 class => 'Koha
::Patrons
',
801 value => { branchcode => $library->branchcode }
804 $item->itemlost('1')->store();
805 C4::Accounts::chargelostitem( $patron->borrowernumber, $item->itemnumber, 5, "lost" );
806 my $accountline = Koha::Account::Lines->search(
808 borrowernumber => $patron->borrowernumber,
809 itemnumber => $item->itemnumber,
810 debit_type_code => 'LOST
'
813 my $checkout = Koha::Checkout->new(
815 borrowernumber => $patron_2->borrowernumber,
816 itemnumber => $item->itemnumber,
817 date_due => \'NOW()',
818 branchcode
=> $patron_2->branchcode,
819 issuedate
=> \'NOW
()',
823 $patron->account->pay(
826 library_id => $library->branchcode,
827 lines => [$accountline],
832 Koha::Checkouts->find( $checkout->issue_id ),
833 'If the item is checked out to another patron
, a lost item should
not be returned
if lost fee is paid
'
838 $schema->storage->txn_rollback;
841 subtest 'pay
() handles lost items
when paying by amount
( not specifying the lost fee
)' => sub {
845 $schema->storage->txn_begin;
847 my $patron = $builder->build_object( { class => 'Koha
::Patrons
' } );
848 my $library = $builder->build_object( { class => 'Koha
::Libraries
' } );
849 my $account = $patron->account;
851 my $context = Test::MockModule->new('C4
::Context
');
852 $context->mock( 'userenv
', { branch => $library->id } );
854 my $biblio = $builder->build_sample_biblio();
856 $builder->build_sample_item( { biblionumber => $biblio->biblionumber } );
858 my $checkout = Koha::Checkout->new(
860 borrowernumber => $patron->id,
861 itemnumber => $item->id,
862 date_due => \'NOW()',
863 branchcode
=> $patron->branchcode,
864 issuedate
=> \'NOW
()',
868 $item->itemlost('1')->store();
870 my $accountline = Koha::Account::Line->new(
872 issue_id => $checkout->id,
873 borrowernumber => $patron->id,
874 itemnumber => $item->id,
876 debit_type_code
=> 'LOST',
879 amountoutstanding
=> '1',
886 library_id
=> $library->id,
890 $accountline = Koha
::Account
::Lines
->find( $accountline->id );
891 is
( $accountline->amountoutstanding+0, .5, 'Account line was paid down by half' );
893 $checkout = Koha
::Checkouts
->find( $checkout->id );
894 ok
( $checkout, 'Item still checked out to patron' );
899 library_id
=> $library->id,
903 $accountline = Koha
::Account
::Lines
->find( $accountline->id );
904 is
( $accountline->amountoutstanding+0, 0, 'Account line was paid down by half' );
906 $checkout = Koha
::Checkouts
->find( $checkout->id );
907 ok
( !$checkout, 'Item was removed from patron account' );
909 $schema->storage->txn_rollback;
912 subtest
'pay() renews items when appropriate' => sub {
916 $schema->storage->txn_begin;
918 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
919 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
920 my $account = $patron->account;
922 my $context = Test
::MockModule
->new('C4::Context');
923 $context->mock( 'userenv', { branch
=> $library->id } );
925 my $biblio = $builder->build_sample_biblio();
927 $builder->build_sample_item( { biblionumber
=> $biblio->biblionumber } );
929 my $now = dt_from_string
();
930 my $seven_weeks = DateTime
::Duration
->new(weeks
=> 7);
931 my $five_weeks = DateTime
::Duration
->new(weeks
=> 5);
932 my $seven_weeks_ago = $now - $seven_weeks;
933 my $five_weeks_ago = $now - $five_weeks;
935 my $checkout = Koha
::Checkout
->new(
937 borrowernumber
=> $patron->id,
938 itemnumber
=> $item->id,
939 date_due
=> $five_weeks_ago,
940 branchcode
=> $patron->branchcode,
941 issuedate
=> $seven_weeks_ago
945 my $accountline = Koha
::Account
::Line
->new(
947 issue_id
=> $checkout->id,
948 borrowernumber
=> $patron->id,
949 itemnumber
=> $item->id,
951 debit_type_code => 'OVERDUE
',
952 status => 'UNRETURNED
',
955 amountoutstanding => '1',
959 # Enable renewing upon fine payment
960 t::lib::Mocks::mock_preference( 'RenewAccruingItemWhenPaid
', 1 );
962 my $module = new Test::MockModule('C4
::Circulation
');
963 $module->mock('AddRenewal
', sub { $called = 1; });
964 $module->mock('CanBookBeRenewed
', sub { return 1; });
968 library_id => $library->id,
972 is( $called, 1, 'RenewAccruingItemWhenPaid causes C4
::Circulation
::AddRenew to be called
when appropriate
' );
974 $schema->storage->txn_rollback;
977 subtest 'Koha
::Account
::Line
::apply
() handles lost items
' => sub {
981 $schema->storage->txn_begin;
983 my $patron = $builder->build_object( { class => 'Koha
::Patrons
' } );
984 my $library = $builder->build_object( { class => 'Koha
::Libraries
' } );
985 my $account = $patron->account;
987 my $context = Test::MockModule->new('C4
::Context
');
988 $context->mock( 'userenv
', { branch => $library->id } );
990 my $biblio = $builder->build_sample_biblio();
992 $builder->build_sample_item( { biblionumber => $biblio->biblionumber } );
994 my $checkout = Koha::Checkout->new(
996 borrowernumber => $patron->id,
997 itemnumber => $item->id,
998 date_due => \'NOW()',
999 branchcode
=> $patron->branchcode,
1000 issuedate
=> \'NOW
()',
1004 $item->itemlost('1')->store();
1006 my $debit = Koha::Account::Line->new(
1008 issue_id => $checkout->id,
1009 borrowernumber => $patron->id,
1010 itemnumber => $item->id,
1012 debit_type_code
=> 'LOST',
1015 amountoutstanding
=> '1',
1019 my $credit = Koha
::Account
::Line
->new(
1021 borrowernumber
=> $patron->id,
1022 date
=> '1970-01-01 00:00:01',
1024 amountoutstanding
=> -.5,
1025 interface
=> 'commandline',
1026 credit_type_code
=> 'PAYMENT'
1029 my $debits = $account->outstanding_debits;
1030 $credit->apply({ debits
=> [ $debits->as_list ] });
1032 $debit = Koha
::Account
::Lines
->find( $debit->id );
1033 is
( $debit->amountoutstanding+0, .5, 'Account line was paid down by half' );
1035 $checkout = Koha
::Checkouts
->find( $checkout->id );
1036 ok
( $checkout, 'Item still checked out to patron' );
1038 $credit = Koha
::Account
::Line
->new(
1040 borrowernumber
=> $patron->id,
1041 date
=> '1970-01-01 00:00:01',
1043 amountoutstanding
=> -.5,
1044 interface
=> 'commandline',
1045 credit_type_code
=> 'PAYMENT'
1048 $debits = $account->outstanding_debits;
1049 $credit->apply({ debits
=> [ $debits->as_list ] });
1051 $debit = Koha
::Account
::Lines
->find( $debit->id );
1052 is
( $debit->amountoutstanding+0, 0, 'Account line was paid down by half' );
1054 $checkout = Koha
::Checkouts
->find( $checkout->id );
1055 ok
( !$checkout, 'Item was removed from patron account' );
1057 $schema->storage->txn_rollback;
1060 subtest
'Koha::Account::pay() generates credit number (Koha::Account::Line->store)' => sub {
1063 $schema->storage->txn_begin;
1065 Koha
::Account
::Lines
->delete();
1067 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
1068 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
1069 my $account = $patron->account;
1071 #t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode });
1072 my $context = Test
::MockModule
->new('C4::Context');
1073 $context->mock( 'userenv', { branch
=> $library->id } );
1075 my $now = dt_from_string
;
1076 my $year = $now->year;
1077 my $month = $now->month;
1078 my ($accountlines_id, $accountline);
1080 my $credit_type = Koha
::Account
::CreditTypes
->find('PAYMENT');
1081 $credit_type->credit_number_enabled(1);
1082 $credit_type->store();
1084 t
::lib
::Mocks
::mock_preference
('AutoCreditNumber', '');
1085 $accountlines_id = $account->pay({ amount
=> '1.00', library_id
=> $library->id })->{payment_id
};
1086 $accountline = Koha
::Account
::Lines
->find($accountlines_id);
1087 is
($accountline->credit_number, undef, 'No credit number is generated when syspref is off');
1089 t
::lib
::Mocks
::mock_preference
('AutoCreditNumber', 'incremental');
1091 $accountlines_id = $account->pay({ amount
=> '1.00', library_id
=> $library->id })->{payment_id
};
1092 $accountline = Koha
::Account
::Lines
->find($accountlines_id);
1093 is
($accountline->credit_number, $i, "Incremental format credit number added for payments: $i");
1095 $accountlines_id = $account->pay({ type
=> 'WRITEOFF', amount
=> '1.00', library_id
=> $library->id })->{payment_id
};
1096 $accountline = Koha
::Account
::Lines
->find($accountlines_id);
1097 is
($accountline->credit_number, undef, "Incremental credit number not added for writeoff");
1099 t
::lib
::Mocks
::mock_preference
('AutoCreditNumber', 'annual');
1101 $accountlines_id = $account->pay({ amount
=> '1.00', library_id
=> $library->id })->{payment_id
};
1102 $accountline = Koha
::Account
::Lines
->find($accountlines_id);
1103 is
($accountline->credit_number, sprintf('%s-%04d', $year, $i), "Annual format credit number added for payments: " . sprintf('%s-%04d', $year, $i));
1105 $accountlines_id = $account->pay({ type
=> 'WRITEOFF', amount
=> '1.00', library_id
=> $library->id })->{payment_id
};
1106 $accountline = Koha
::Account
::Lines
->find($accountlines_id);
1107 is
($accountline->credit_number, undef, "Annual format credit number not aded for writeoff");
1109 t
::lib
::Mocks
::mock_preference
('AutoCreditNumber', 'branchyyyymmincr');
1111 $accountlines_id = $account->pay({ amount
=> '1.00', library_id
=> $library->id })->{payment_id
};
1112 $accountline = Koha
::Account
::Lines
->find($accountlines_id);
1113 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));
1115 $accountlines_id = $account->pay({ type
=> 'WRITEOFF', amount
=> '1.00', library_id
=> $library->id })->{payment_id
};
1116 $accountline = Koha
::Account
::Lines
->find($accountlines_id);
1117 is
($accountline->credit_number, undef, "branchyyyymmincr format credit number not added for writeoff");
1120 Koha
::Account
::Line
->new(
1122 interface
=> 'test',
1124 credit_type_code
=> $credit_type->code,
1129 'Koha::Exceptions::Account',
1130 "Exception thrown when AutoCreditNumber is enabled but credit_number is already defined";
1132 $schema->storage->txn_rollback;