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
=> 11;
27 use Koha
::Account
::Lines
;
28 use Koha
::Account
::Offsets
;
32 use t
::lib
::TestBuilder
;
34 my $schema = Koha
::Database
->new->schema;
35 my $builder = t
::lib
::TestBuilder
->new;
36 C4
::Context
->interface('commandline');
38 subtest
'new' => sub {
42 $schema->storage->txn_begin;
44 throws_ok
{ Koha
::Account
->new(); } qr/No patron id passed in!/, 'Croaked on bad call to new';
46 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
47 my $account = Koha
::Account
->new( { patron_id
=> $patron->borrowernumber } );
48 is
( defined $account, 1, "Account is defined" );
50 $schema->storage->txn_rollback;
53 subtest
'outstanding_debits() tests' => sub {
57 $schema->storage->txn_begin;
59 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
60 my $account = $patron->account;
63 push @generated_lines, $account->add_debit({ amount
=> 1, interface
=> 'commandline', type
=> 'OVERDUE' });
64 push @generated_lines, $account->add_debit({ amount
=> 2, interface
=> 'commandline', type
=> 'OVERDUE' });
65 push @generated_lines, $account->add_debit({ amount
=> 3, interface
=> 'commandline', type
=> 'OVERDUE' });
66 push @generated_lines, $account->add_debit({ amount
=> 4, interface
=> 'commandline', type
=> 'OVERDUE' });
68 my $lines = $account->outstanding_debits();
69 my @lines_arr = $account->outstanding_debits();
71 is
( ref($lines), 'Koha::Account::Lines', 'Called in scalar context, outstanding_debits returns a Koha::Account::Lines object' );
72 is
( $lines->total_outstanding, 10, 'Outstandig debits total is correctly calculated' );
75 foreach my $line ( @
{ $lines->as_list } ) {
76 my $fetched_line = Koha
::Account
::Lines
->find( $generated_lines[$i]->id );
77 is_deeply
( $line->unblessed, $fetched_line->unblessed, "Fetched line matches the generated one ($i)" );
78 is_deeply
( $lines_arr[$i]->unblessed, $fetched_line->unblessed, "Fetched line matches the generated one ($i)" );
79 is
( ref($lines_arr[$i]), 'Koha::Account::Line', 'outstanding_debits returns a list of Koha::Account::Line objects in list context' );
82 my $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
83 Koha
::Account
::Line
->new(
85 borrowernumber
=> $patron_2->id,
86 amountoutstanding
=> -2,
87 interface
=> 'commandline',
91 my $just_one = Koha
::Account
::Line
->new(
93 borrowernumber
=> $patron_2->id,
95 amountoutstanding
=> 3,
96 interface
=> 'commandline',
97 debit_type_code
=> 'OVERDUE'
100 Koha
::Account
::Line
->new(
102 borrowernumber
=> $patron_2->id,
104 amountoutstanding
=> -6,
105 interface
=> 'commandline',
109 $lines = $patron_2->account->outstanding_debits();
110 is
( $lines->total_outstanding, 3, "Total if some outstanding debits and some credits is only debits" );
111 is
( $lines->count, 1, "With 1 outstanding debits, we get back a Lines object with 1 lines" );
112 my $the_line = Koha
::Account
::Lines
->find( $just_one->id );
113 is_deeply
( $the_line->unblessed, $lines->next->unblessed, "We get back the one correct line");
115 my $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
116 my $account_3 = $patron_3->account;
117 $account_3->add_credit( { amount
=> 2, interface
=> 'commandline' } );
118 $account_3->add_credit( { amount
=> 20, interface
=> 'commandline' } );
119 $account_3->add_credit( { amount
=> 200, interface
=> 'commandline' } );
120 $lines = $account_3->outstanding_debits();
121 is
( $lines->total_outstanding, 0, "Total if no outstanding debits total is 0" );
122 is
( $lines->count, 0, "With 0 outstanding debits, we get back a Lines object with 0 lines" );
124 my $patron_4 = $builder->build_object({ class => 'Koha::Patrons' });
125 my $account_4 = $patron_4->account;
126 $lines = $account_4->outstanding_debits();
127 is
( $lines->total_outstanding, 0, "Total if no outstanding debits is 0" );
128 is
( $lines->count, 0, "With no outstanding debits, we get back a Lines object with 0 lines" );
130 # create a pathological credit with amountoutstanding > 0 (BZ 14591)
131 Koha
::Account
::Line
->new(
133 borrowernumber
=> $patron_4->id,
135 amountoutstanding
=> 3,
136 interface
=> 'commandline',
140 $lines = $account_4->outstanding_debits();
141 is
( $lines->count, 0, 'No credits are confused with debits because of the amountoutstanding value' );
143 $schema->storage->txn_rollback;
146 subtest
'outstanding_credits() tests' => sub {
150 $schema->storage->txn_begin;
152 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
153 my $account = $patron->account;
156 push @generated_lines, $account->add_credit({ amount
=> 1, interface
=> 'commandline' });
157 push @generated_lines, $account->add_credit({ amount
=> 2, interface
=> 'commandline' });
158 push @generated_lines, $account->add_credit({ amount
=> 3, interface
=> 'commandline' });
159 push @generated_lines, $account->add_credit({ amount
=> 4, interface
=> 'commandline' });
161 my $lines = $account->outstanding_credits();
162 my @lines_arr = $account->outstanding_credits();
164 is
( ref($lines), 'Koha::Account::Lines', 'Called in scalar context, outstanding_credits returns a Koha::Account::Lines object' );
165 is
( $lines->total_outstanding, -10, 'Outstandig credits total is correctly calculated' );
168 foreach my $line ( @
{ $lines->as_list } ) {
169 my $fetched_line = Koha
::Account
::Lines
->find( $generated_lines[$i]->id );
170 is_deeply
( $line->unblessed, $fetched_line->unblessed, "Fetched line matches the generated one ($i)" );
171 is_deeply
( $lines_arr[$i]->unblessed, $fetched_line->unblessed, "Fetched line matches the generated one ($i)" );
172 is
( ref($lines_arr[$i]), 'Koha::Account::Line', 'outstanding_debits returns a list of Koha::Account::Line objects in list context' );
176 my $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
177 $account = $patron_2->account;
178 $lines = $account->outstanding_credits();
179 is
( $lines->total_outstanding, 0, "Total if no outstanding credits is 0" );
180 is
( $lines->count, 0, "With no outstanding credits, we get back a Lines object with 0 lines" );
182 # create a pathological debit with amountoutstanding < 0 (BZ 14591)
183 Koha
::Account
::Line
->new(
185 borrowernumber
=> $patron_2->id,
187 amountoutstanding
=> -3,
188 interface
=> 'commandline',
189 debit_type_code
=> 'OVERDUE'
192 $lines = $account->outstanding_credits();
193 is
( $lines->count, 0, 'No debits are confused with credits because of the amountoutstanding value' );
195 $schema->storage->txn_rollback;
198 subtest
'add_credit() tests' => sub {
202 $schema->storage->txn_begin;
204 # delete logs and statistics
205 my $action_logs = $schema->resultset('ActionLog')->search()->count;
206 my $statistics = $schema->resultset('Statistic')->search()->count;
208 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
209 my $account = Koha
::Account
->new( { patron_id
=> $patron->borrowernumber } );
211 is
( $account->balance, 0, 'Patron has no balance' );
214 t
::lib
::Mocks
::mock_preference
( 'FinesLog', 0 );
217 $account->add_credit(
219 description
=> 'Payment of 25',
220 library_id
=> $patron->branchcode,
221 note
=> 'not really important',
223 user_id
=> $patron->id
227 'Koha::Exceptions::MissingParameter', 'Exception thrown if interface parameter missing';
229 my $line_1 = $account->add_credit(
231 description
=> 'Payment of 25',
232 library_id
=> $patron->branchcode,
233 note
=> 'not really important',
235 user_id
=> $patron->id,
236 interface
=> 'commandline'
240 is
( $account->balance, -25, 'Patron has a balance of -25' );
241 is
( $schema->resultset('ActionLog')->count(), $action_logs + 0, 'No log was added' );
242 is
( $schema->resultset('Statistic')->count(), $statistics + 1, 'Action added to statistics' );
243 is
( $line_1->accounttype, $Koha::Account
::account_type_credit
->{'payment'}, 'Account type is correctly set' );
246 t
::lib
::Mocks
::mock_preference
( 'FinesLog', 1 );
248 my $line_2 = $account->add_credit(
250 description
=> 'Payment of 37',
251 library_id
=> $patron->branchcode,
252 note
=> 'not really important',
253 user_id
=> $patron->id,
254 interface
=> 'commandline'
258 is
( $account->balance, -62, 'Patron has a balance of -25' );
259 is
( $schema->resultset('ActionLog')->count(), $action_logs + 1, 'Log was added' );
260 is
( $schema->resultset('Statistic')->count(), $statistics + 2, 'Action added to statistics' );
261 is
( $line_2->accounttype, $Koha::Account
::account_type_credit
->{'payment'}, 'Account type is correctly set' );
263 # offsets have the credit_id set to accountlines_id, and debit_id is undef
264 my $offset_1 = Koha
::Account
::Offsets
->search({ credit_id
=> $line_1->id })->next;
265 my $offset_2 = Koha
::Account
::Offsets
->search({ credit_id
=> $line_2->id })->next;
267 is
( $offset_1->credit_id, $line_1->id, 'No debit_id is set for credits' );
268 is
( $offset_1->debit_id, undef, 'No debit_id is set for credits' );
269 is
( $offset_2->credit_id, $line_2->id, 'No debit_id is set for credits' );
270 is
( $offset_2->debit_id, undef, 'No debit_id is set for credits' );
272 my $line_3 = $account->add_credit(
275 description
=> 'Manual credit applied',
276 library_id
=> $patron->branchcode,
277 user_id
=> $patron->id,
279 interface
=> 'commandline'
283 is
( $schema->resultset('ActionLog')->count(), $action_logs + 2, 'Log was added' );
284 is
( $schema->resultset('Statistic')->count(), $statistics + 2, 'No action added to statistics, because of credit type' );
286 # Enable cash registers
287 t
::lib
::Mocks
::mock_preference
( 'UseCashRegisters', 1 );
289 $account->add_credit(
292 description
=> 'Cash payment without cash register',
293 library_id
=> $patron->branchcode,
294 user_id
=> $patron->id,
295 payment_type
=> 'CASH',
296 interface
=> 'intranet'
300 'Koha::Exceptions::Account::RegisterRequired',
301 'Exception thrown for UseCashRegisters:1 + payment_type:CASH + cash_register:undef';
303 # Disable cash registers
304 t
::lib
::Mocks
::mock_preference
( 'UseCashRegisters', 1 );
306 $schema->storage->txn_rollback;
309 subtest
'add_debit() tests' => sub {
313 $schema->storage->txn_begin;
315 # delete logs and statistics
316 my $action_logs = $schema->resultset('ActionLog')->search()->count;
317 my $statistics = $schema->resultset('Statistic')->search()->count;
319 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
321 Koha
::Account
->new( { patron_id
=> $patron->borrowernumber } );
323 is
( $account->balance, 0, 'Patron has no balance' );
329 description
=> 'amount validation failure',
330 library_id
=> $patron->branchcode,
331 note
=> 'this should fail anyway',
333 user_id
=> $patron->id,
334 interface
=> 'commandline'
336 ); } 'Koha::Exceptions::Account::AmountNotPositive', 'Expected validation exception thrown (amount)';
342 description
=> 'type validation failure',
343 library_id
=> $patron->branchcode,
344 note
=> 'this should fail anyway',
346 user_id
=> $patron->id,
347 interface
=> 'commandline'
349 ); } 'Koha::Exceptions::Account::UnrecognisedType', 'Expected validation exception thrown (type)';
355 description
=> 'Rental charge of 25',
356 library_id
=> $patron->branchcode,
357 note
=> 'not really important',
359 user_id
=> $patron->id
361 ); } 'Koha::Exceptions::MissingParameter', 'Exception thrown if interface parameter missing';
364 t
::lib
::Mocks
::mock_preference
( 'FinesLog', 0 );
366 my $line_1 = $account->add_debit(
369 description
=> 'Rental charge of 25',
370 library_id
=> $patron->branchcode,
371 note
=> 'not really important',
373 user_id
=> $patron->id,
374 interface
=> 'commandline'
378 is
( $account->balance, 25, 'Patron has a balance of 25' );
380 $schema->resultset('ActionLog')->count(),
385 $line_1->debit_type_code,
387 'Account type is correctly set'
391 t
::lib
::Mocks
::mock_preference
( 'FinesLog', 1 );
393 my $line_2 = $account->add_debit(
396 description
=> 'Rental charge of 37',
397 library_id
=> $patron->branchcode,
398 note
=> 'not really important',
400 user_id
=> $patron->id,
401 interface
=> 'commandline'
405 is
( $account->balance, 62, 'Patron has a balance of 62' );
407 $schema->resultset('ActionLog')->count(),
412 $line_2->debit_type_code,
414 'Account type is correctly set'
417 # offsets have the debit_id set to accountlines_id, and credit_id is undef
419 Koha
::Account
::Offsets
->search( { debit_id
=> $line_1->id } )->next;
421 Koha
::Account
::Offsets
->search( { debit_id
=> $line_2->id } )->next;
423 is
( $offset_1->debit_id, $line_1->id, 'debit_id is set for debit 1' );
424 is
( $offset_1->credit_id, undef, 'credit_id is not set for debit 1' );
425 is
( $offset_2->debit_id, $line_2->id, 'debit_id is set for debit 2' );
426 is
( $offset_2->credit_id, undef, 'credit_id is not set for debit 2' );
428 $schema->storage->txn_rollback;
431 subtest
'lines() tests' => sub {
435 $schema->storage->txn_begin;
437 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
438 my $account = $patron->account;
441 $account->add_credit({ amount
=> 1, interface
=> 'commandline' });
442 $account->add_credit({ amount
=> 2, interface
=> 'commandline' });
443 $account->add_credit({ amount
=> 3, interface
=> 'commandline' });
444 $account->add_credit({ amount
=> 4, interface
=> 'commandline' });
447 $account->add_debit({ amount
=> 1, interface
=> 'commandline', type
=> 'OVERDUE' });
448 $account->add_debit({ amount
=> 2, interface
=> 'commandline', type
=> 'OVERDUE' });
449 $account->add_debit({ amount
=> 3, interface
=> 'commandline', type
=> 'OVERDUE' });
450 $account->add_debit({ amount
=> 4, interface
=> 'commandline', type
=> 'OVERDUE' });
453 $account->add_credit( { amount
=> 1, interface
=> 'commandline' } )
454 ->apply( { debits
=> [ $account->outstanding_debits->as_list ] } );
456 my $lines = $account->lines;
457 is
( $lines->_resultset->count, 9, "All accountlines (debits, credits and paid off) were fetched");
459 $schema->storage->txn_rollback;
462 subtest
'reconcile_balance' => sub {
466 subtest
'more credit than debit' => sub {
470 $schema->storage->txn_begin;
472 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
473 my $account = $patron->account;
476 $account->add_credit({ amount
=> 1, interface
=> 'commandline' });
477 $account->add_credit({ amount
=> 2, interface
=> 'commandline' });
478 $account->add_credit({ amount
=> 3, interface
=> 'commandline' });
479 $account->add_credit({ amount
=> 4, interface
=> 'commandline' });
480 $account->add_credit({ amount
=> 5, interface
=> 'commandline' });
483 $account->add_debit({ amount
=> 1, interface
=> 'commandline', type
=> 'OVERDUE' });
484 $account->add_debit({ amount
=> 2, interface
=> 'commandline', type
=> 'OVERDUE' });
485 $account->add_debit({ amount
=> 3, interface
=> 'commandline', type
=> 'OVERDUE' });
486 $account->add_debit({ amount
=> 4, interface
=> 'commandline', type
=> 'OVERDUE' });
489 Koha
::Account
::Line
->new(
491 borrowernumber
=> $patron->id,
493 amountoutstanding
=> 0,
494 interface
=> 'commandline',
495 debit_type_code
=> 'OVERDUE'
498 Koha
::Account
::Line
->new(
500 borrowernumber
=> $patron->id,
502 amountoutstanding
=> 0,
503 interface
=> 'commandline',
504 debit_type_code
=> 'OVERDUE'
508 is
( $account->balance(), -5, "Account balance is -5" );
509 is
( $account->outstanding_debits->total_outstanding, 10, 'Outstanding debits sum 10' );
510 is
( $account->outstanding_credits->total_outstanding, -15, 'Outstanding credits sum -15' );
512 $account->reconcile_balance();
514 is
( $account->balance(), -5, "Account balance is -5" );
515 is
( $account->outstanding_debits->total_outstanding, 0, 'No outstanding debits' );
516 is
( $account->outstanding_credits->total_outstanding, -5, 'Outstanding credits sum -5' );
518 $schema->storage->txn_rollback;
521 subtest
'same debit as credit' => sub {
525 $schema->storage->txn_begin;
527 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
528 my $account = $patron->account;
531 $account->add_credit({ amount
=> 1, interface
=> 'commandline' });
532 $account->add_credit({ amount
=> 2, interface
=> 'commandline' });
533 $account->add_credit({ amount
=> 3, interface
=> 'commandline' });
534 $account->add_credit({ amount
=> 4, interface
=> 'commandline' });
537 $account->add_debit({ amount
=> 1, interface
=> 'commandline', type
=> 'OVERDUE' });
538 $account->add_debit({ amount
=> 2, interface
=> 'commandline', type
=> 'OVERDUE' });
539 $account->add_debit({ amount
=> 3, interface
=> 'commandline', type
=> 'OVERDUE' });
540 $account->add_debit({ amount
=> 4, interface
=> 'commandline', type
=> 'OVERDUE' });
543 Koha
::Account
::Line
->new(
545 borrowernumber
=> $patron->id,
547 amountoutstanding
=> 0,
548 interface
=> 'commandline',
549 debit_type_code
=> 'OVERDUE'
552 Koha
::Account
::Line
->new(
554 borrowernumber
=> $patron->id,
556 amountoutstanding
=> 0,
557 interface
=> 'commandline',
558 debit_type_code
=> 'OVERDUE'
562 is
( $account->balance(), 0, "Account balance is 0" );
563 is
( $account->outstanding_debits->total_outstanding, 10, 'Outstanding debits sum 10' );
564 is
( $account->outstanding_credits->total_outstanding, -10, 'Outstanding credits sum -10' );
566 $account->reconcile_balance();
568 is
( $account->balance(), 0, "Account balance is 0" );
569 is
( $account->outstanding_debits->total_outstanding, 0, 'No outstanding debits' );
570 is
( $account->outstanding_credits->total_outstanding, 0, 'Outstanding credits sum 0' );
572 $schema->storage->txn_rollback;
575 subtest
'more debit than credit' => sub {
579 $schema->storage->txn_begin;
581 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
582 my $account = $patron->account;
585 $account->add_credit({ amount
=> 1, interface
=> 'commandline' });
586 $account->add_credit({ amount
=> 2, interface
=> 'commandline' });
587 $account->add_credit({ amount
=> 3, interface
=> 'commandline' });
588 $account->add_credit({ amount
=> 4, interface
=> 'commandline' });
591 $account->add_debit({ amount
=> 1, interface
=> 'commandline', type
=> 'OVERDUE' });
592 $account->add_debit({ amount
=> 2, interface
=> 'commandline', type
=> 'OVERDUE' });
593 $account->add_debit({ amount
=> 3, interface
=> 'commandline', type
=> 'OVERDUE' });
594 $account->add_debit({ amount
=> 4, interface
=> 'commandline', type
=> 'OVERDUE' });
595 $account->add_debit({ amount
=> 5, interface
=> 'commandline', type
=> 'OVERDUE' });
598 Koha
::Account
::Line
->new(
600 borrowernumber
=> $patron->id,
602 amountoutstanding
=> 0,
603 interface
=> 'commandline',
604 debit_type_code
=> 'OVERDUE'
607 Koha
::Account
::Line
->new(
609 borrowernumber
=> $patron->id,
611 amountoutstanding
=> 0,
612 interface
=> 'commandline',
613 debit_type_code
=> 'OVERDUE'
617 is
( $account->balance(), 5, "Account balance is 5" );
618 is
( $account->outstanding_debits->total_outstanding, 15, 'Outstanding debits sum 15' );
619 is
( $account->outstanding_credits->total_outstanding, -10, 'Outstanding credits sum -10' );
621 $account->reconcile_balance();
623 is
( $account->balance(), 5, "Account balance is 5" );
624 is
( $account->outstanding_debits->total_outstanding, 5, 'Outstanding debits sum 5' );
625 is
( $account->outstanding_credits->total_outstanding, 0, 'Outstanding credits sum 0' );
627 $schema->storage->txn_rollback;
630 subtest
'credits are applied to older debits first' => sub {
634 $schema->storage->txn_begin;
636 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
637 my $account = $patron->account;
640 $account->add_credit({ amount
=> 1, interface
=> 'commandline' });
641 $account->add_credit({ amount
=> 3, interface
=> 'commandline' });
644 my $debit_1 = $account->add_debit({ amount
=> 1, interface
=> 'commandline', type
=> 'OVERDUE' });
645 my $debit_2 = $account->add_debit({ amount
=> 2, interface
=> 'commandline', type
=> 'OVERDUE' });
646 my $debit_3 = $account->add_debit({ amount
=> 3, interface
=> 'commandline', type
=> 'OVERDUE' });
648 is
( $account->balance(), 2, "Account balance is 2" );
649 is
( $account->outstanding_debits->total_outstanding, 6, 'Outstanding debits sum 6' );
650 is
( $account->outstanding_credits->total_outstanding, -4, 'Outstanding credits sum -4' );
652 $account->reconcile_balance();
654 is
( $account->balance(), 2, "Account balance is 2" );
655 is
( $account->outstanding_debits->total_outstanding, 2, 'Outstanding debits sum 2' );
656 is
( $account->outstanding_credits->total_outstanding, 0, 'Outstanding credits sum 0' );
658 $debit_1->discard_changes;
659 is
( $debit_1->amountoutstanding + 0, 0, 'Old debit payed' );
660 $debit_2->discard_changes;
661 is
( $debit_2->amountoutstanding + 0, 0, 'Old debit payed' );
662 $debit_3->discard_changes;
663 is
( $debit_3->amountoutstanding + 0, 2, 'Newest debit only partially payed' );
665 $schema->storage->txn_rollback;
669 subtest
'pay() tests' => sub {
673 $schema->storage->txn_begin;
675 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
676 my $library = $builder->build_object({ class => 'Koha::Libraries' });
677 my $account = $patron->account;
679 my $context = Test
::MockModule
->new('C4::Context');
680 $context->mock( 'userenv', { branch
=> $library->id } );
682 my $credit_1_id = $account->pay({ amount
=> 200 });
683 my $credit_1 = Koha
::Account
::Lines
->find( $credit_1_id );
685 is
( $credit_1->branchcode, undef, 'No branchcode is set if library_id was not passed' );
687 my $credit_2_id = $account->pay({ amount
=> 150, library_id
=> $library->id });
688 my $credit_2 = Koha
::Account
::Lines
->find( $credit_2_id );
690 is
( $credit_2->branchcode, $library->id, 'branchcode set because library_id was passed' );
692 # Enable cash registers
693 t
::lib
::Mocks
::mock_preference
( 'UseCashRegisters', 1 );
698 payment_type
=> 'CASH',
699 interface
=> 'intranet'
703 'Koha::Exceptions::Account::RegisterRequired',
704 'Exception thrown for UseCashRegisters:1 + payment_type:CASH + cash_register:undef';
706 # Disable cash registers
707 t
::lib
::Mocks
::mock_preference
( 'UseCashRegisters', 1 );
709 $schema->storage->txn_rollback;
712 subtest
'pay() handles lost items when paying a specific lost fee' => sub {
716 $schema->storage->txn_begin;
718 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
719 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
720 my $account = $patron->account;
722 my $context = Test
::MockModule
->new('C4::Context');
723 $context->mock( 'userenv', { branch
=> $library->id } );
725 my $biblio = $builder->build_sample_biblio();
727 $builder->build_sample_item( { biblionumber
=> $biblio->biblionumber } );
729 my $checkout = Koha
::Checkout
->new(
731 borrowernumber
=> $patron->id,
732 itemnumber
=> $item->id,
733 date_due
=> \'NOW
()',
734 branchcode => $patron->branchcode,
735 issuedate => \'NOW()',
739 $item->itemlost('1')->store();
741 my $accountline = Koha
::Account
::Line
->new(
743 issue_id
=> $checkout->id,
744 borrowernumber
=> $patron->id,
745 itemnumber
=> $item->id,
747 debit_type_code => 'LOST
',
750 amountoutstanding => '1',
756 amount => "0.500000",
757 library_id => $library->id,
758 lines => [$accountline],
762 $accountline = Koha::Account::Lines->find( $accountline->id );
763 is( $accountline->amountoutstanding, '0.500000', 'Account line was paid down by half
' );
765 $checkout = Koha::Checkouts->find( $checkout->id );
766 ok( $checkout, 'Item still checked out to patron
' );
770 amount => "0.500000",
771 library_id => $library->id,
772 lines => [$accountline],
776 $accountline = Koha::Account::Lines->find( $accountline->id );
777 is( $accountline->amountoutstanding, '0.000000', 'Account line was paid down by half
' );
779 $checkout = Koha::Checkouts->find( $checkout->id );
780 ok( !$checkout, 'Item was removed from patron account
' );
782 $schema->storage->txn_rollback;
785 subtest 'pay
() handles lost items
when paying by amount
( not specifying the lost fee
)' => sub {
789 $schema->storage->txn_begin;
791 my $patron = $builder->build_object( { class => 'Koha
::Patrons
' } );
792 my $library = $builder->build_object( { class => 'Koha
::Libraries
' } );
793 my $account = $patron->account;
795 my $context = Test::MockModule->new('C4
::Context
');
796 $context->mock( 'userenv
', { branch => $library->id } );
798 my $biblio = $builder->build_sample_biblio();
800 $builder->build_sample_item( { biblionumber => $biblio->biblionumber } );
802 my $checkout = Koha::Checkout->new(
804 borrowernumber => $patron->id,
805 itemnumber => $item->id,
806 date_due => \'NOW()',
807 branchcode
=> $patron->branchcode,
808 issuedate
=> \'NOW
()',
812 $item->itemlost('1')->store();
814 my $accountline = Koha::Account::Line->new(
816 issue_id => $checkout->id,
817 borrowernumber => $patron->id,
818 itemnumber => $item->id,
820 debit_type_code
=> 'LOST',
823 amountoutstanding
=> '1',
829 amount
=> "0.500000",
830 library_id
=> $library->id,
834 $accountline = Koha
::Account
::Lines
->find( $accountline->id );
835 is
( $accountline->amountoutstanding, '0.500000', 'Account line was paid down by half' );
837 $checkout = Koha
::Checkouts
->find( $checkout->id );
838 ok
( $checkout, 'Item still checked out to patron' );
842 amount
=> "0.500000",
843 library_id
=> $library->id,
847 $accountline = Koha
::Account
::Lines
->find( $accountline->id );
848 is
( $accountline->amountoutstanding, '0.000000', 'Account line was paid down by half' );
850 $checkout = Koha
::Checkouts
->find( $checkout->id );
851 ok
( !$checkout, 'Item was removed from patron account' );
853 $schema->storage->txn_rollback;
856 subtest
'Koha::Account::Line::apply() handles lost items' => sub {
860 $schema->storage->txn_begin;
862 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
863 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
864 my $account = $patron->account;
866 my $context = Test
::MockModule
->new('C4::Context');
867 $context->mock( 'userenv', { branch
=> $library->id } );
869 my $biblio = $builder->build_sample_biblio();
871 $builder->build_sample_item( { biblionumber
=> $biblio->biblionumber } );
873 my $checkout = Koha
::Checkout
->new(
875 borrowernumber
=> $patron->id,
876 itemnumber
=> $item->id,
877 date_due
=> \'NOW
()',
878 branchcode => $patron->branchcode,
879 issuedate => \'NOW()',
883 $item->itemlost('1')->store();
885 my $debit = Koha
::Account
::Line
->new(
887 issue_id
=> $checkout->id,
888 borrowernumber
=> $patron->id,
889 itemnumber
=> $item->id,
891 debit_type_code => 'LOST
',
894 amountoutstanding => '1',
898 my $credit = Koha::Account::Line->new(
900 borrowernumber => $patron->id,
901 date => '1900-01-01',
902 amount => "-0.500000",
903 amountoutstanding => "-0.500000",
904 interface => 'commandline
',
908 my $debits = $account->outstanding_debits;
909 $credit->apply({ debits => [ $debits->as_list ] });
911 $debit = Koha::Account::Lines->find( $debit->id );
912 is( $debit->amountoutstanding, '0.500000', 'Account line was paid down by half
' );
914 $checkout = Koha::Checkouts->find( $checkout->id );
915 ok( $checkout, 'Item still checked out to patron
' );
917 $credit = Koha::Account::Line->new(
919 borrowernumber => $patron->id,
920 date => '1900-01-01',
921 amount => "-0.500000",
922 amountoutstanding => "-0.500000",
923 interface => 'commandline
',
927 $debits = $account->outstanding_debits;
928 $credit->apply({ debits => [ $debits->as_list ] });
930 $debit = Koha::Account::Lines->find( $debit->id );
931 is( $debit->amountoutstanding, '0.000000', 'Account line was paid down by half
' );
933 $checkout = Koha::Checkouts->find( $checkout->id );
934 ok( !$checkout, 'Item was removed from patron account
' );
936 $schema->storage->txn_rollback;