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
=> 12;
29 use Koha
::Account
::Lines
;
30 use Koha
::Account
::Offsets
;
31 use Koha
::DateUtils
qw( dt_from_string );
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 {
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 {
60 $schema->storage->txn_begin;
62 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
63 my $account = $patron->account;
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' );
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' );
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'
94 my $just_one = Koha
::Account
::Line
->new(
96 borrowernumber
=> $patron_2->id,
98 amountoutstanding
=> 3,
99 interface
=> 'commandline',
100 debit_type_code
=> 'OVERDUE'
103 Koha
::Account
::Line
->new(
105 borrowernumber
=> $patron_2->id,
107 amountoutstanding
=> -6,
108 interface
=> 'commandline',
109 credit_type_code
=> 'PAYMENT'
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,
138 amountoutstanding
=> 3,
139 interface
=> 'commandline',
140 credit_type_code
=> 'PAYMENT'
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 {
153 $schema->storage->txn_begin;
155 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
156 my $account = $patron->account;
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' );
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' );
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,
190 amountoutstanding
=> -3,
191 interface
=> 'commandline',
192 debit_type_code
=> 'OVERDUE'
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 {
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' );
217 t
::lib
::Mocks
::mock_preference
( 'FinesLog', 0 );
220 $account->add_credit(
222 description
=> 'Payment of 25',
223 library_id
=> $patron->branchcode,
224 note
=> 'not really important',
226 user_id
=> $patron->id
230 'Koha::Exceptions::MissingParameter', 'Exception thrown if interface parameter missing';
232 my $line_1 = $account->add_credit(
234 description
=> 'Payment of 25',
235 library_id
=> $patron->branchcode,
236 note
=> 'not really important',
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' );
249 t
::lib
::Mocks
::mock_preference
( 'FinesLog', 1 );
251 my $line_2 = $account->add_credit(
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(
278 description
=> 'Manual credit applied',
279 library_id
=> $patron->branchcode,
280 user_id
=> $patron->id,
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 );
292 $account->add_credit(
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 {
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' } );
324 Koha
::Account
->new( { patron_id
=> $patron->borrowernumber } );
326 is
( $account->balance, 0, 'Patron has no balance' );
332 description
=> 'amount validation failure',
333 library_id
=> $patron->branchcode,
334 note
=> 'this should fail anyway',
336 user_id
=> $patron->id,
337 interface
=> 'commandline'
339 ); } 'Koha::Exceptions::Account::AmountNotPositive', 'Expected validation exception thrown (amount)';
345 description
=> 'type validation failure',
346 library_id
=> $patron->branchcode,
347 note
=> 'this should fail anyway',
349 user_id
=> $patron->id,
350 interface
=> 'commandline'
352 ); } 'Koha::Exceptions::Account::UnrecognisedType', 'Expected validation exception thrown (type)';
358 description
=> 'Rental charge of 25',
359 library_id
=> $patron->branchcode,
360 note
=> 'not really important',
362 user_id
=> $patron->id
364 ); } 'Koha::Exceptions::MissingParameter', 'Exception thrown if interface parameter missing';
367 t
::lib
::Mocks
::mock_preference
( 'FinesLog', 0 );
369 my $line_1 = $account->add_debit(
372 description
=> 'Rental charge of 25',
373 library_id
=> $patron->branchcode,
374 note
=> 'not really important',
376 user_id
=> $patron->id,
377 interface
=> 'commandline'
381 is
( $account->balance, 25, 'Patron has a balance of 25' );
383 $schema->resultset('ActionLog')->count(),
388 $line_1->debit_type_code,
390 'Account type is correctly set'
394 t
::lib
::Mocks
::mock_preference
( 'FinesLog', 1 );
396 my $line_2 = $account->add_debit(
399 description
=> 'Rental charge of 37',
400 library_id
=> $patron->branchcode,
401 note
=> 'not really important',
403 user_id
=> $patron->id,
404 interface
=> 'commandline'
408 is
( $account->balance, 62, 'Patron has a balance of 62' );
410 $schema->resultset('ActionLog')->count(),
415 $line_2->debit_type_code,
417 'Account type is correctly set'
420 # offsets have the debit_id set to accountlines_id, and credit_id is undef
422 Koha
::Account
::Offsets
->search( { debit_id
=> $line_1->id } )->next;
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 {
438 $schema->storage->txn_begin;
440 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
441 my $account = $patron->account;
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' });
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' });
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 {
469 subtest
'more credit than debit' => sub {
473 $schema->storage->txn_begin;
475 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
476 my $account = $patron->account;
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' });
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' });
492 Koha
::Account
::Line
->new(
494 borrowernumber
=> $patron->id,
496 amountoutstanding
=> 0,
497 interface
=> 'commandline',
498 debit_type_code
=> 'OVERDUE'
501 Koha
::Account
::Line
->new(
503 borrowernumber
=> $patron->id,
505 amountoutstanding
=> 0,
506 interface
=> 'commandline',
507 debit_type_code
=> 'OVERDUE'
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 {
528 $schema->storage->txn_begin;
530 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
531 my $account = $patron->account;
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' });
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' });
546 Koha
::Account
::Line
->new(
548 borrowernumber
=> $patron->id,
550 amountoutstanding
=> 0,
551 interface
=> 'commandline',
552 debit_type_code
=> 'OVERDUE'
555 Koha
::Account
::Line
->new(
557 borrowernumber
=> $patron->id,
559 amountoutstanding
=> 0,
560 interface
=> 'commandline',
561 debit_type_code
=> 'OVERDUE'
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 {
582 $schema->storage->txn_begin;
584 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
585 my $account = $patron->account;
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' });
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' });
601 Koha
::Account
::Line
->new(
603 borrowernumber
=> $patron->id,
605 amountoutstanding
=> 0,
606 interface
=> 'commandline',
607 debit_type_code
=> 'OVERDUE'
610 Koha
::Account
::Line
->new(
612 borrowernumber
=> $patron->id,
614 amountoutstanding
=> 0,
615 interface
=> 'commandline',
616 debit_type_code
=> 'OVERDUE'
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 {
637 $schema->storage->txn_begin;
639 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
640 my $account = $patron->account;
643 $account->add_credit({ amount
=> 1, interface
=> 'commandline' });
644 $account->add_credit({ amount
=> 3, interface
=> 'commandline' });
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 {
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 );
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 {
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();
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()',
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,
753 debit_type_code => 'LOST
',
756 amountoutstanding => '1',
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
' );
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 {
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
'
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
()',
816 $patron->account->pay(
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 {
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();
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
()',
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,
869 debit_type_code
=> 'LOST',
872 amountoutstanding
=> '1',
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' );
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 {
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();
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
938 my $accountline = Koha
::Account
::Line
->new(
940 issue_id
=> $checkout->id,
941 borrowernumber
=> $patron->id,
942 itemnumber
=> $item->id,
944 debit_type_code => 'OVERDUE
',
945 status => 'UNRETURNED
',
948 amountoutstanding => '1',
952 # Enable renewing upon fine payment
953 t::lib::Mocks::mock_preference( 'RenewAccruingItemWhenPaid
', 1 );
955 my $module = new Test::MockModule('C4
::Circulation
');
956 $module->mock('AddRenewal
', sub { $called = 1; });
957 $module->mock('CanBookBeRenewed
', sub { return 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 {
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();
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
()',
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,
1005 debit_type_code
=> 'LOST',
1008 amountoutstanding
=> '1',
1012 my $credit = Koha
::Account
::Line
->new(
1014 borrowernumber
=> $patron->id,
1015 date
=> '1970-01-01 00:00:01',
1017 amountoutstanding
=> -.5,
1018 interface
=> 'commandline',
1019 credit_type_code
=> 'PAYMENT'
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',
1036 amountoutstanding
=> -.5,
1037 interface
=> 'commandline',
1038 credit_type_code
=> 'PAYMENT'
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 {
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');
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');
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');
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;