Bug 23805: Update 'Pay' to 'PAYMENT' for consistency
[koha.git] / t / db_dependent / Koha / Account.t
1 #!/usr/bin/perl
2
3 # Copyright 2018 Koha Development team
4 #
5 # This file is part of Koha
6 #
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.
11 #
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.
16 #
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>
19
20 use Modern::Perl;
21
22 use Test::More tests => 11;
23 use Test::MockModule;
24 use Test::Exception;
25
26 use Koha::Account;
27 use Koha::Account::Lines;
28 use Koha::Account::Offsets;
29
30
31 use t::lib::Mocks;
32 use t::lib::TestBuilder;
33
34 my $schema  = Koha::Database->new->schema;
35 $schema->storage->dbh->{PrintError} = 0;
36 my $builder = t::lib::TestBuilder->new;
37 C4::Context->interface('commandline');
38
39 subtest 'new' => sub {
40
41     plan tests => 2;
42
43     $schema->storage->txn_begin;
44
45     throws_ok { Koha::Account->new(); } qr/No patron id passed in!/, 'Croaked on bad call to new';
46
47     my $patron  = $builder->build_object({ class => 'Koha::Patrons' });
48     my $account = Koha::Account->new( { patron_id => $patron->borrowernumber } );
49     is( defined $account, 1, "Account is defined" );
50
51     $schema->storage->txn_rollback;
52 };
53
54 subtest 'outstanding_debits() tests' => sub {
55
56     plan tests => 22;
57
58     $schema->storage->txn_begin;
59
60     my $patron  = $builder->build_object({ class => 'Koha::Patrons' });
61     my $account = $patron->account;
62
63     my @generated_lines;
64     push @generated_lines, $account->add_debit({ amount => 1, interface => 'commandline', type => 'OVERDUE' });
65     push @generated_lines, $account->add_debit({ amount => 2, interface => 'commandline', type => 'OVERDUE' });
66     push @generated_lines, $account->add_debit({ amount => 3, interface => 'commandline', type => 'OVERDUE' });
67     push @generated_lines, $account->add_debit({ amount => 4, interface => 'commandline', type => 'OVERDUE' });
68
69     my $lines     = $account->outstanding_debits();
70     my @lines_arr = $account->outstanding_debits();
71
72     is( ref($lines), 'Koha::Account::Lines', 'Called in scalar context, outstanding_debits returns a Koha::Account::Lines object' );
73     is( $lines->total_outstanding, 10, 'Outstandig debits total is correctly calculated' );
74
75     my $i = 0;
76     foreach my $line ( @{ $lines->as_list } ) {
77         my $fetched_line = Koha::Account::Lines->find( $generated_lines[$i]->id );
78         is_deeply( $line->unblessed, $fetched_line->unblessed, "Fetched line matches the generated one ($i)" );
79         is_deeply( $lines_arr[$i]->unblessed, $fetched_line->unblessed, "Fetched line matches the generated one ($i)" );
80         is( ref($lines_arr[$i]), 'Koha::Account::Line', 'outstanding_debits returns a list of Koha::Account::Line objects in list context' );
81         $i++;
82     }
83     my $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
84     Koha::Account::Line->new(
85         {
86             borrowernumber    => $patron_2->id,
87             amountoutstanding => -2,
88             interface         => 'commandline',
89             credit_type_code  => 'PAYMENT'
90         }
91     )->store;
92     my $just_one = Koha::Account::Line->new(
93         {
94             borrowernumber    => $patron_2->id,
95             amount            => 3,
96             amountoutstanding => 3,
97             interface         => 'commandline',
98             debit_type_code   => 'OVERDUE'
99         }
100     )->store;
101     Koha::Account::Line->new(
102         {
103             borrowernumber    => $patron_2->id,
104             amount            => -6,
105             amountoutstanding => -6,
106             interface         => 'commandline',
107             credit_type_code  => 'PAYMENT'
108         }
109     )->store;
110     $lines = $patron_2->account->outstanding_debits();
111     is( $lines->total_outstanding, 3, "Total if some outstanding debits and some credits is only debits" );
112     is( $lines->count, 1, "With 1 outstanding debits, we get back a Lines object with 1 lines" );
113     my $the_line = Koha::Account::Lines->find( $just_one->id );
114     is_deeply( $the_line->unblessed, $lines->next->unblessed, "We get back the one correct line");
115
116     my $patron_3  = $builder->build_object({ class => 'Koha::Patrons' });
117     my $account_3 = $patron_3->account;
118     $account_3->add_credit( { amount => 2,   interface => 'commandline' } );
119     $account_3->add_credit( { amount => 20,  interface => 'commandline' } );
120     $account_3->add_credit( { amount => 200, interface => 'commandline' } );
121     $lines = $account_3->outstanding_debits();
122     is( $lines->total_outstanding, 0, "Total if no outstanding debits total is 0" );
123     is( $lines->count, 0, "With 0 outstanding debits, we get back a Lines object with 0 lines" );
124
125     my $patron_4  = $builder->build_object({ class => 'Koha::Patrons' });
126     my $account_4 = $patron_4->account;
127     $lines = $account_4->outstanding_debits();
128     is( $lines->total_outstanding, 0, "Total if no outstanding debits is 0" );
129     is( $lines->count, 0, "With no outstanding debits, we get back a Lines object with 0 lines" );
130
131     # create a pathological credit with amountoutstanding > 0 (BZ 14591)
132     Koha::Account::Line->new(
133         {
134             borrowernumber    => $patron_4->id,
135             amount            => -3,
136             amountoutstanding => 3,
137             interface         => 'commandline',
138             credit_type_code  => 'PAYMENT'
139         }
140     )->store();
141     $lines = $account_4->outstanding_debits();
142     is( $lines->count, 0, 'No credits are confused with debits because of the amountoutstanding value' );
143
144     $schema->storage->txn_rollback;
145 };
146
147 subtest 'outstanding_credits() tests' => sub {
148
149     plan tests => 17;
150
151     $schema->storage->txn_begin;
152
153     my $patron  = $builder->build_object({ class => 'Koha::Patrons' });
154     my $account = $patron->account;
155
156     my @generated_lines;
157     push @generated_lines, $account->add_credit({ amount => 1, interface => 'commandline' });
158     push @generated_lines, $account->add_credit({ amount => 2, interface => 'commandline' });
159     push @generated_lines, $account->add_credit({ amount => 3, interface => 'commandline' });
160     push @generated_lines, $account->add_credit({ amount => 4, interface => 'commandline' });
161
162     my $lines     = $account->outstanding_credits();
163     my @lines_arr = $account->outstanding_credits();
164
165     is( ref($lines), 'Koha::Account::Lines', 'Called in scalar context, outstanding_credits returns a Koha::Account::Lines object' );
166     is( $lines->total_outstanding, -10, 'Outstandig credits total is correctly calculated' );
167
168     my $i = 0;
169     foreach my $line ( @{ $lines->as_list } ) {
170         my $fetched_line = Koha::Account::Lines->find( $generated_lines[$i]->id );
171         is_deeply( $line->unblessed, $fetched_line->unblessed, "Fetched line matches the generated one ($i)" );
172         is_deeply( $lines_arr[$i]->unblessed, $fetched_line->unblessed, "Fetched line matches the generated one ($i)" );
173         is( ref($lines_arr[$i]), 'Koha::Account::Line', 'outstanding_debits returns a list of Koha::Account::Line objects in list context' );
174         $i++;
175     }
176
177     my $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
178     $account  = $patron_2->account;
179     $lines       = $account->outstanding_credits();
180     is( $lines->total_outstanding, 0, "Total if no outstanding credits is 0" );
181     is( $lines->count, 0, "With no outstanding credits, we get back a Lines object with 0 lines" );
182
183     # create a pathological debit with amountoutstanding < 0 (BZ 14591)
184     Koha::Account::Line->new(
185         {
186             borrowernumber    => $patron_2->id,
187             amount            => 2,
188             amountoutstanding => -3,
189             interface         => 'commandline',
190             debit_type_code   => 'OVERDUE'
191         }
192     )->store();
193     $lines = $account->outstanding_credits();
194     is( $lines->count, 0, 'No debits are confused with credits because of the amountoutstanding value' );
195
196     $schema->storage->txn_rollback;
197 };
198
199 subtest 'add_credit() tests' => sub {
200
201     plan tests => 17;
202
203     $schema->storage->txn_begin;
204
205     # delete logs and statistics
206     my $action_logs = $schema->resultset('ActionLog')->search()->count;
207     my $statistics = $schema->resultset('Statistic')->search()->count;
208
209     my $patron  = $builder->build_object( { class => 'Koha::Patrons' } );
210     my $account = Koha::Account->new( { patron_id => $patron->borrowernumber } );
211
212     is( $account->balance, 0, 'Patron has no balance' );
213
214     # Disable logs
215     t::lib::Mocks::mock_preference( 'FinesLog', 0 );
216
217     throws_ok {
218         $account->add_credit(
219             {   amount      => 25,
220                 description => 'Payment of 25',
221                 library_id  => $patron->branchcode,
222                 note        => 'not really important',
223                 type        => 'PAYMENT',
224                 user_id     => $patron->id
225             }
226         );
227     }
228     'Koha::Exceptions::MissingParameter', 'Exception thrown if interface parameter missing';
229
230     my $line_1 = $account->add_credit(
231         {   amount      => 25,
232             description => 'Payment of 25',
233             library_id  => $patron->branchcode,
234             note        => 'not really important',
235             type        => 'PAYMENT',
236             user_id     => $patron->id,
237             interface   => 'commandline'
238         }
239     );
240
241     is( $account->balance, -25, 'Patron has a balance of -25' );
242     is( $schema->resultset('ActionLog')->count(), $action_logs + 0, 'No log was added' );
243     is( $schema->resultset('Statistic')->count(), $statistics + 1, 'Action added to statistics' );
244     is( $line_1->credit_type_code, $Koha::Account::account_type_credit->{'PAYMENT'}, 'Account type is correctly set' );
245
246     # Enable logs
247     t::lib::Mocks::mock_preference( 'FinesLog', 1 );
248
249     my $line_2 = $account->add_credit(
250         {   amount      => 37,
251             description => 'Payment of 37',
252             library_id  => $patron->branchcode,
253             note        => 'not really important',
254             user_id     => $patron->id,
255             interface   => 'commandline'
256         }
257     );
258
259     is( $account->balance, -62, 'Patron has a balance of -25' );
260     is( $schema->resultset('ActionLog')->count(), $action_logs + 1, 'Log was added' );
261     is( $schema->resultset('Statistic')->count(), $statistics + 2, 'Action added to statistics' );
262     is( $line_2->credit_type_code, $Koha::Account::account_type_credit->{'PAYMENT'}, 'Account type is correctly set' );
263
264     # offsets have the credit_id set to accountlines_id, and debit_id is undef
265     my $offset_1 = Koha::Account::Offsets->search({ credit_id => $line_1->id })->next;
266     my $offset_2 = Koha::Account::Offsets->search({ credit_id => $line_2->id })->next;
267
268     is( $offset_1->credit_id, $line_1->id, 'No debit_id is set for credits' );
269     is( $offset_1->debit_id, undef, 'No debit_id is set for credits' );
270     is( $offset_2->credit_id, $line_2->id, 'No debit_id is set for credits' );
271     is( $offset_2->debit_id, undef, 'No debit_id is set for credits' );
272
273     my $line_3 = $account->add_credit(
274         {
275             amount      => 20,
276             description => 'Manual credit applied',
277             library_id  => $patron->branchcode,
278             user_id     => $patron->id,
279             type        => 'FORGIVEN',
280             interface   => 'commandline'
281         }
282     );
283
284     is( $schema->resultset('ActionLog')->count(), $action_logs + 2, 'Log was added' );
285     is( $schema->resultset('Statistic')->count(), $statistics + 2, 'No action added to statistics, because of credit type' );
286
287     # Enable cash registers
288     t::lib::Mocks::mock_preference( 'UseCashRegisters', 1 );
289     throws_ok {
290         $account->add_credit(
291             {
292                 amount       => 20,
293                 description  => 'Cash payment without cash register',
294                 library_id   => $patron->branchcode,
295                 user_id      => $patron->id,
296                 payment_type => 'CASH',
297                 interface    => 'intranet'
298             }
299         );
300     }
301     'Koha::Exceptions::Account::RegisterRequired',
302       'Exception thrown for UseCashRegisters:1 + payment_type:CASH + cash_register:undef';
303
304     # Disable cash registers
305     t::lib::Mocks::mock_preference( 'UseCashRegisters', 1 );
306
307     $schema->storage->txn_rollback;
308 };
309
310 subtest 'add_debit() tests' => sub {
311
312     plan tests => 14;
313
314     $schema->storage->txn_begin;
315
316     # delete logs and statistics
317     my $action_logs = $schema->resultset('ActionLog')->search()->count;
318     my $statistics  = $schema->resultset('Statistic')->search()->count;
319
320     my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
321     my $account =
322       Koha::Account->new( { patron_id => $patron->borrowernumber } );
323
324     is( $account->balance, 0, 'Patron has no balance' );
325
326     throws_ok {
327     $account->add_debit(
328         {
329             amount      => -5,
330             description => 'amount validation failure',
331             library_id  => $patron->branchcode,
332             note        => 'this should fail anyway',
333             type        => 'RENT',
334             user_id     => $patron->id,
335             interface   => 'commandline'
336         }
337     ); } 'Koha::Exceptions::Account::AmountNotPositive', 'Expected validation exception thrown (amount)';
338
339     throws_ok {
340     $account->add_debit(
341         {
342             amount      => 5,
343             description => 'type validation failure',
344             library_id  => $patron->branchcode,
345             note        => 'this should fail anyway',
346             type        => 'failure',
347             user_id     => $patron->id,
348             interface   => 'commandline'
349         }
350     ); } 'Koha::Exceptions::Account::UnrecognisedType', 'Expected validation exception thrown (type)';
351
352     throws_ok {
353     $account->add_debit(
354         {
355             amount      => 25,
356             description => 'Rental charge of 25',
357             library_id  => $patron->branchcode,
358             note        => 'not really important',
359             type        => 'RENT',
360             user_id     => $patron->id
361         }
362     ); } 'Koha::Exceptions::MissingParameter', 'Exception thrown if interface parameter missing';
363
364     # Disable logs
365     t::lib::Mocks::mock_preference( 'FinesLog', 0 );
366
367     my $line_1 = $account->add_debit(
368         {
369             amount      => 25,
370             description => 'Rental charge of 25',
371             library_id  => $patron->branchcode,
372             note        => 'not really important',
373             type        => 'RENT',
374             user_id     => $patron->id,
375             interface   => 'commandline'
376         }
377     );
378
379     is( $account->balance, 25, 'Patron has a balance of 25' );
380     is(
381         $schema->resultset('ActionLog')->count(),
382         $action_logs + 0,
383         'No log was added'
384     );
385     is(
386         $line_1->debit_type_code,
387         'RENT',
388         'Account type is correctly set'
389     );
390
391     # Enable logs
392     t::lib::Mocks::mock_preference( 'FinesLog', 1 );
393
394     my $line_2   = $account->add_debit(
395         {
396             amount      => 37,
397             description => 'Rental charge of 37',
398             library_id  => $patron->branchcode,
399             note        => 'not really important',
400             type        => 'RENT',
401             user_id     => $patron->id,
402             interface   => 'commandline'
403         }
404     );
405
406     is( $account->balance, 62, 'Patron has a balance of 62' );
407     is(
408         $schema->resultset('ActionLog')->count(),
409         $action_logs + 1,
410         'Log was added'
411     );
412     is(
413         $line_2->debit_type_code,
414         'RENT',
415         'Account type is correctly set'
416     );
417
418     # offsets have the debit_id set to accountlines_id, and credit_id is undef
419     my $offset_1 =
420       Koha::Account::Offsets->search( { debit_id => $line_1->id } )->next;
421     my $offset_2 =
422       Koha::Account::Offsets->search( { debit_id => $line_2->id } )->next;
423
424     is( $offset_1->debit_id,  $line_1->id, 'debit_id is set for debit 1' );
425     is( $offset_1->credit_id, undef,       'credit_id is not set for debit 1' );
426     is( $offset_2->debit_id,  $line_2->id, 'debit_id is set for debit 2' );
427     is( $offset_2->credit_id, undef,       'credit_id is not set for debit 2' );
428
429     $schema->storage->txn_rollback;
430 };
431
432 subtest 'lines() tests' => sub {
433
434     plan tests => 1;
435
436     $schema->storage->txn_begin;
437
438     my $patron  = $builder->build_object({ class => 'Koha::Patrons' });
439     my $account = $patron->account;
440
441     # Add Credits
442     $account->add_credit({ amount => 1, interface => 'commandline' });
443     $account->add_credit({ amount => 2, interface => 'commandline' });
444     $account->add_credit({ amount => 3, interface => 'commandline' });
445     $account->add_credit({ amount => 4, interface => 'commandline' });
446
447     # Add Debits
448     $account->add_debit({ amount => 1, interface => 'commandline', type => 'OVERDUE' });
449     $account->add_debit({ amount => 2, interface => 'commandline', type => 'OVERDUE' });
450     $account->add_debit({ amount => 3, interface => 'commandline', type => 'OVERDUE' });
451     $account->add_debit({ amount => 4, interface => 'commandline', type => 'OVERDUE' });
452
453     # Paid Off
454     $account->add_credit( { amount => 1, interface => 'commandline' } )
455         ->apply( { debits => [ $account->outstanding_debits->as_list ] } );
456
457     my $lines = $account->lines;
458     is( $lines->_resultset->count, 9, "All accountlines (debits, credits and paid off) were fetched");
459
460     $schema->storage->txn_rollback;
461 };
462
463 subtest 'reconcile_balance' => sub {
464
465     plan tests => 4;
466
467     subtest 'more credit than debit' => sub {
468
469         plan tests => 6;
470
471         $schema->storage->txn_begin;
472
473         my $patron  = $builder->build_object({ class => 'Koha::Patrons' });
474         my $account = $patron->account;
475
476         # Add Credits
477         $account->add_credit({ amount => 1, interface => 'commandline' });
478         $account->add_credit({ amount => 2, interface => 'commandline' });
479         $account->add_credit({ amount => 3, interface => 'commandline' });
480         $account->add_credit({ amount => 4, interface => 'commandline' });
481         $account->add_credit({ amount => 5, interface => 'commandline' });
482
483         # Add Debits
484         $account->add_debit({ amount => 1, interface => 'commandline', type => 'OVERDUE' });
485         $account->add_debit({ amount => 2, interface => 'commandline', type => 'OVERDUE' });
486         $account->add_debit({ amount => 3, interface => 'commandline', type => 'OVERDUE' });
487         $account->add_debit({ amount => 4, interface => 'commandline', type => 'OVERDUE' });
488
489         # Paid Off
490         Koha::Account::Line->new(
491             {
492                 borrowernumber    => $patron->id,
493                 amount            => 1,
494                 amountoutstanding => 0,
495                 interface         => 'commandline',
496                 debit_type_code   => 'OVERDUE'
497             }
498         )->store;
499         Koha::Account::Line->new(
500             {
501                 borrowernumber    => $patron->id,
502                 amount            => 1,
503                 amountoutstanding => 0,
504                 interface         => 'commandline',
505                 debit_type_code   => 'OVERDUE'
506             }
507         )->store;
508
509         is( $account->balance(), -5, "Account balance is -5" );
510         is( $account->outstanding_debits->total_outstanding, 10, 'Outstanding debits sum 10' );
511         is( $account->outstanding_credits->total_outstanding, -15, 'Outstanding credits sum -15' );
512
513         $account->reconcile_balance();
514
515         is( $account->balance(), -5, "Account balance is -5" );
516         is( $account->outstanding_debits->total_outstanding, 0, 'No outstanding debits' );
517         is( $account->outstanding_credits->total_outstanding, -5, 'Outstanding credits sum -5' );
518
519         $schema->storage->txn_rollback;
520     };
521
522     subtest 'same debit as credit' => sub {
523
524         plan tests => 6;
525
526         $schema->storage->txn_begin;
527
528         my $patron  = $builder->build_object({ class => 'Koha::Patrons' });
529         my $account = $patron->account;
530
531         # Add Credits
532         $account->add_credit({ amount => 1, interface => 'commandline' });
533         $account->add_credit({ amount => 2, interface => 'commandline' });
534         $account->add_credit({ amount => 3, interface => 'commandline' });
535         $account->add_credit({ amount => 4, interface => 'commandline' });
536
537         # Add Debits
538         $account->add_debit({ amount => 1, interface => 'commandline', type => 'OVERDUE' });
539         $account->add_debit({ amount => 2, interface => 'commandline', type => 'OVERDUE' });
540         $account->add_debit({ amount => 3, interface => 'commandline', type => 'OVERDUE' });
541         $account->add_debit({ amount => 4, interface => 'commandline', type => 'OVERDUE' });
542
543         # Paid Off
544         Koha::Account::Line->new(
545             {
546                 borrowernumber    => $patron->id,
547                 amount            => 1,
548                 amountoutstanding => 0,
549                 interface         => 'commandline',
550                 debit_type_code   => 'OVERDUE'
551             }
552         )->store;
553         Koha::Account::Line->new(
554             {
555                 borrowernumber    => $patron->id,
556                 amount            => 1,
557                 amountoutstanding => 0,
558                 interface         => 'commandline',
559                 debit_type_code   => 'OVERDUE'
560             }
561         )->store;
562
563         is( $account->balance(), 0, "Account balance is 0" );
564         is( $account->outstanding_debits->total_outstanding, 10, 'Outstanding debits sum 10' );
565         is( $account->outstanding_credits->total_outstanding, -10, 'Outstanding credits sum -10' );
566
567         $account->reconcile_balance();
568
569         is( $account->balance(), 0, "Account balance is 0" );
570         is( $account->outstanding_debits->total_outstanding, 0, 'No outstanding debits' );
571         is( $account->outstanding_credits->total_outstanding, 0, 'Outstanding credits sum 0' );
572
573         $schema->storage->txn_rollback;
574     };
575
576     subtest 'more debit than credit' => sub {
577
578         plan tests => 6;
579
580         $schema->storage->txn_begin;
581
582         my $patron  = $builder->build_object({ class => 'Koha::Patrons' });
583         my $account = $patron->account;
584
585         # Add Credits
586         $account->add_credit({ amount => 1, interface => 'commandline' });
587         $account->add_credit({ amount => 2, interface => 'commandline' });
588         $account->add_credit({ amount => 3, interface => 'commandline' });
589         $account->add_credit({ amount => 4, interface => 'commandline' });
590
591         # Add Debits
592         $account->add_debit({ amount => 1, interface => 'commandline', type => 'OVERDUE' });
593         $account->add_debit({ amount => 2, interface => 'commandline', type => 'OVERDUE' });
594         $account->add_debit({ amount => 3, interface => 'commandline', type => 'OVERDUE' });
595         $account->add_debit({ amount => 4, interface => 'commandline', type => 'OVERDUE' });
596         $account->add_debit({ amount => 5, interface => 'commandline', type => 'OVERDUE' });
597
598         # Paid Off
599         Koha::Account::Line->new(
600             {
601                 borrowernumber    => $patron->id,
602                 amount            => 1,
603                 amountoutstanding => 0,
604                 interface         => 'commandline',
605                 debit_type_code   => 'OVERDUE'
606             }
607         )->store;
608         Koha::Account::Line->new(
609             {
610                 borrowernumber    => $patron->id,
611                 amount            => 1,
612                 amountoutstanding => 0,
613                 interface         => 'commandline',
614                 debit_type_code   => 'OVERDUE'
615             }
616         )->store;
617
618         is( $account->balance(), 5, "Account balance is 5" );
619         is( $account->outstanding_debits->total_outstanding, 15, 'Outstanding debits sum 15' );
620         is( $account->outstanding_credits->total_outstanding, -10, 'Outstanding credits sum -10' );
621
622         $account->reconcile_balance();
623
624         is( $account->balance(), 5, "Account balance is 5" );
625         is( $account->outstanding_debits->total_outstanding, 5, 'Outstanding debits sum 5' );
626         is( $account->outstanding_credits->total_outstanding, 0, 'Outstanding credits sum 0' );
627
628         $schema->storage->txn_rollback;
629     };
630
631     subtest 'credits are applied to older debits first' => sub {
632
633         plan tests => 9;
634
635         $schema->storage->txn_begin;
636
637         my $patron  = $builder->build_object({ class => 'Koha::Patrons' });
638         my $account = $patron->account;
639
640         # Add Credits
641         $account->add_credit({ amount => 1, interface => 'commandline' });
642         $account->add_credit({ amount => 3, interface => 'commandline' });
643
644         # Add Debits
645         my $debit_1 = $account->add_debit({ amount => 1, interface => 'commandline', type => 'OVERDUE' });
646         my $debit_2 = $account->add_debit({ amount => 2, interface => 'commandline', type => 'OVERDUE' });
647         my $debit_3 = $account->add_debit({ amount => 3, interface => 'commandline', type => 'OVERDUE' });
648
649         is( $account->balance(), 2, "Account balance is 2" );
650         is( $account->outstanding_debits->total_outstanding, 6, 'Outstanding debits sum 6' );
651         is( $account->outstanding_credits->total_outstanding, -4, 'Outstanding credits sum -4' );
652
653         $account->reconcile_balance();
654
655         is( $account->balance(), 2, "Account balance is 2" );
656         is( $account->outstanding_debits->total_outstanding, 2, 'Outstanding debits sum 2' );
657         is( $account->outstanding_credits->total_outstanding, 0, 'Outstanding credits sum 0' );
658
659         $debit_1->discard_changes;
660         is( $debit_1->amountoutstanding + 0, 0, 'Old debit payed' );
661         $debit_2->discard_changes;
662         is( $debit_2->amountoutstanding + 0, 0, 'Old debit payed' );
663         $debit_3->discard_changes;
664         is( $debit_3->amountoutstanding + 0, 2, 'Newest debit only partially payed' );
665
666         $schema->storage->txn_rollback;
667     };
668 };
669
670 subtest 'pay() tests' => sub {
671
672     plan tests => 3;
673
674     $schema->storage->txn_begin;
675
676     my $patron  = $builder->build_object({ class => 'Koha::Patrons' });
677     my $library = $builder->build_object({ class => 'Koha::Libraries' });
678     my $account = $patron->account;
679
680     my $context = Test::MockModule->new('C4::Context');
681     $context->mock( 'userenv', { branch => $library->id } );
682
683     my $credit_1_id = $account->pay({ amount => 200 });
684     my $credit_1    = Koha::Account::Lines->find( $credit_1_id );
685
686     is( $credit_1->branchcode, undef, 'No branchcode is set if library_id was not passed' );
687
688     my $credit_2_id = $account->pay({ amount => 150, library_id => $library->id });
689     my $credit_2    = Koha::Account::Lines->find( $credit_2_id );
690
691     is( $credit_2->branchcode, $library->id, 'branchcode set because library_id was passed' );
692
693     # Enable cash registers
694     t::lib::Mocks::mock_preference( 'UseCashRegisters', 1 );
695     throws_ok {
696         $account->pay(
697             {
698                 amount       => 20,
699                 payment_type => 'CASH',
700                 interface    => 'intranet'
701             }
702         );
703     }
704     'Koha::Exceptions::Account::RegisterRequired',
705       'Exception thrown for UseCashRegisters:1 + payment_type:CASH + cash_register:undef';
706
707     # Disable cash registers
708     t::lib::Mocks::mock_preference( 'UseCashRegisters', 1 );
709
710     $schema->storage->txn_rollback;
711 };
712
713 subtest 'pay() handles lost items when paying a specific lost fee' => sub {
714
715     plan tests => 4;
716
717     $schema->storage->txn_begin;
718
719     my $patron  = $builder->build_object( { class => 'Koha::Patrons' } );
720     my $library = $builder->build_object( { class => 'Koha::Libraries' } );
721     my $account = $patron->account;
722
723     my $context = Test::MockModule->new('C4::Context');
724     $context->mock( 'userenv', { branch => $library->id } );
725
726     my $biblio = $builder->build_sample_biblio();
727     my $item =
728       $builder->build_sample_item( { biblionumber => $biblio->biblionumber } );
729
730     my $checkout = Koha::Checkout->new(
731         {
732             borrowernumber => $patron->id,
733             itemnumber     => $item->id,
734             date_due       => \'NOW()',
735             branchcode     => $patron->branchcode,
736             issuedate      => \'NOW()',
737         }
738     )->store();
739
740     $item->itemlost('1')->store();
741
742     my $accountline = Koha::Account::Line->new(
743         {
744             issue_id       => $checkout->id,
745             borrowernumber => $patron->id,
746             itemnumber     => $item->id,
747             date           => \'NOW()',
748             debit_type_code    => 'LOST',
749             interface      => 'cli',
750             amount => '1',
751             amountoutstanding => '1',
752         }
753     )->store();
754
755     $account->pay(
756         {
757             amount     => "0.500000",
758             library_id => $library->id,
759             lines      => [$accountline],
760         }
761     );
762
763     $accountline = Koha::Account::Lines->find( $accountline->id );
764     is( $accountline->amountoutstanding, '0.500000', 'Account line was paid down by half' );
765
766     $checkout = Koha::Checkouts->find( $checkout->id );
767     ok( $checkout, 'Item still checked out to patron' );
768
769     $account->pay(
770         {
771             amount     => "0.500000",
772             library_id => $library->id,
773             lines      => [$accountline],
774         }
775     );
776
777     $accountline = Koha::Account::Lines->find( $accountline->id );
778     is( $accountline->amountoutstanding, '0.000000', 'Account line was paid down by half' );
779
780     $checkout = Koha::Checkouts->find( $checkout->id );
781     ok( !$checkout, 'Item was removed from patron account' );
782
783     $schema->storage->txn_rollback;
784 };
785
786 subtest 'pay() handles lost items when paying by amount ( not specifying the lost fee )' => sub {
787
788     plan tests => 4;
789
790     $schema->storage->txn_begin;
791
792     my $patron  = $builder->build_object( { class => 'Koha::Patrons' } );
793     my $library = $builder->build_object( { class => 'Koha::Libraries' } );
794     my $account = $patron->account;
795
796     my $context = Test::MockModule->new('C4::Context');
797     $context->mock( 'userenv', { branch => $library->id } );
798
799     my $biblio = $builder->build_sample_biblio();
800     my $item =
801       $builder->build_sample_item( { biblionumber => $biblio->biblionumber } );
802
803     my $checkout = Koha::Checkout->new(
804         {
805             borrowernumber => $patron->id,
806             itemnumber     => $item->id,
807             date_due       => \'NOW()',
808             branchcode     => $patron->branchcode,
809             issuedate      => \'NOW()',
810         }
811     )->store();
812
813     $item->itemlost('1')->store();
814
815     my $accountline = Koha::Account::Line->new(
816         {
817             issue_id       => $checkout->id,
818             borrowernumber => $patron->id,
819             itemnumber     => $item->id,
820             date           => \'NOW()',
821             debit_type_code    => 'LOST',
822             interface      => 'cli',
823             amount => '1',
824             amountoutstanding => '1',
825         }
826     )->store();
827
828     $account->pay(
829         {
830             amount     => "0.500000",
831             library_id => $library->id,
832         }
833     );
834
835     $accountline = Koha::Account::Lines->find( $accountline->id );
836     is( $accountline->amountoutstanding, '0.500000', 'Account line was paid down by half' );
837
838     $checkout = Koha::Checkouts->find( $checkout->id );
839     ok( $checkout, 'Item still checked out to patron' );
840
841     $account->pay(
842         {
843             amount     => "0.500000",
844             library_id => $library->id,
845         }
846     );
847
848     $accountline = Koha::Account::Lines->find( $accountline->id );
849     is( $accountline->amountoutstanding, '0.000000', 'Account line was paid down by half' );
850
851     $checkout = Koha::Checkouts->find( $checkout->id );
852     ok( !$checkout, 'Item was removed from patron account' );
853
854     $schema->storage->txn_rollback;
855 };
856
857 subtest 'Koha::Account::Line::apply() handles lost items' => sub {
858
859     plan tests => 4;
860
861     $schema->storage->txn_begin;
862
863     my $patron  = $builder->build_object( { class => 'Koha::Patrons' } );
864     my $library = $builder->build_object( { class => 'Koha::Libraries' } );
865     my $account = $patron->account;
866
867     my $context = Test::MockModule->new('C4::Context');
868     $context->mock( 'userenv', { branch => $library->id } );
869
870     my $biblio = $builder->build_sample_biblio();
871     my $item =
872       $builder->build_sample_item( { biblionumber => $biblio->biblionumber } );
873
874     my $checkout = Koha::Checkout->new(
875         {
876             borrowernumber => $patron->id,
877             itemnumber     => $item->id,
878             date_due       => \'NOW()',
879             branchcode     => $patron->branchcode,
880             issuedate      => \'NOW()',
881         }
882     )->store();
883
884     $item->itemlost('1')->store();
885
886     my $debit = Koha::Account::Line->new(
887         {
888             issue_id          => $checkout->id,
889             borrowernumber    => $patron->id,
890             itemnumber        => $item->id,
891             date              => \'NOW()',
892             debit_type_code       => 'LOST',
893             interface         => 'cli',
894             amount            => '1',
895             amountoutstanding => '1',
896         }
897     )->store();
898
899     my $credit = Koha::Account::Line->new(
900         {
901             borrowernumber    => $patron->id,
902             date              => '1900-01-01',
903             amount            => "-0.500000",
904             amountoutstanding => "-0.500000",
905             interface         => 'commandline',
906             credit_type_code  => 'PAYMENT'
907         }
908     )->store();
909     my $debits = $account->outstanding_debits;
910     $credit->apply({ debits => [ $debits->as_list ] });
911
912     $debit = Koha::Account::Lines->find( $debit->id );
913     is( $debit->amountoutstanding, '0.500000', 'Account line was paid down by half' );
914
915     $checkout = Koha::Checkouts->find( $checkout->id );
916     ok( $checkout, 'Item still checked out to patron' );
917
918     $credit = Koha::Account::Line->new(
919         {
920             borrowernumber    => $patron->id,
921             date              => '1900-01-01',
922             amount            => "-0.500000",
923             amountoutstanding => "-0.500000",
924             interface         => 'commandline',
925             credit_type_code  => 'PAYMENT'
926         }
927     )->store();
928     $debits = $account->outstanding_debits;
929     $credit->apply({ debits => [ $debits->as_list ] });
930
931     $debit = Koha::Account::Lines->find( $debit->id );
932     is( $debit->amountoutstanding, '0.000000', 'Account line was paid down by half' );
933
934     $checkout = Koha::Checkouts->find( $checkout->id );
935     ok( !$checkout, 'Item was removed from patron account' );
936
937     $schema->storage->txn_rollback;
938 };