Bug 29139: Add regression tests
[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 => 15;
23 use Test::MockModule;
24 use Test::Exception;
25
26 use DateTime;
27
28 use Koha::Account;
29 use Koha::Account::CreditTypes;
30 use Koha::Account::Lines;
31 use Koha::Account::Offsets;
32 use Koha::DateUtils qw( dt_from_string );
33
34 use t::lib::Mocks;
35 use t::lib::TestBuilder;
36
37 my $schema  = Koha::Database->new->schema;
38 $schema->storage->dbh->{PrintError} = 0;
39 my $builder = t::lib::TestBuilder->new;
40 C4::Context->interface('commandline');
41
42 subtest 'new' => sub {
43
44     plan tests => 2;
45
46     $schema->storage->txn_begin;
47
48     throws_ok { Koha::Account->new(); } qr/No patron id passed in!/, 'Croaked on bad call to new';
49
50     my $patron  = $builder->build_object({ class => 'Koha::Patrons' });
51     my $account = Koha::Account->new( { patron_id => $patron->borrowernumber } );
52     is( defined $account, 1, "Account is defined" );
53
54     $schema->storage->txn_rollback;
55 };
56
57 subtest 'outstanding_debits() tests' => sub {
58
59     plan tests => 22;
60
61     $schema->storage->txn_begin;
62
63     my $patron  = $builder->build_object({ class => 'Koha::Patrons' });
64     my $account = $patron->account;
65
66     my @generated_lines;
67     push @generated_lines, $account->add_debit({ amount => 1, interface => 'commandline', type => 'OVERDUE' });
68     push @generated_lines, $account->add_debit({ amount => 2, interface => 'commandline', type => 'OVERDUE' });
69     push @generated_lines, $account->add_debit({ amount => 3, interface => 'commandline', type => 'OVERDUE' });
70     push @generated_lines, $account->add_debit({ amount => 4, interface => 'commandline', type => 'OVERDUE' });
71
72     my $lines     = $account->outstanding_debits();
73     my @lines_arr = $account->outstanding_debits();
74
75     is( ref($lines), 'Koha::Account::Lines', 'Called in scalar context, outstanding_debits returns a Koha::Account::Lines object' );
76     is( $lines->total_outstanding, 10, 'Outstandig debits total is correctly calculated' );
77
78     my $i = 0;
79     foreach my $line ( @{ $lines->as_list } ) {
80         my $fetched_line = Koha::Account::Lines->find( $generated_lines[$i]->id );
81         is_deeply( $line->unblessed, $fetched_line->unblessed, "Fetched line matches the generated one ($i)" );
82         is_deeply( $lines_arr[$i]->unblessed, $fetched_line->unblessed, "Fetched line matches the generated one ($i)" );
83         is( ref($lines_arr[$i]), 'Koha::Account::Line', 'outstanding_debits returns a list of Koha::Account::Line objects in list context' );
84         $i++;
85     }
86     my $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
87     Koha::Account::Line->new(
88         {
89             borrowernumber    => $patron_2->id,
90             amountoutstanding => -2,
91             interface         => 'commandline',
92             credit_type_code  => 'PAYMENT'
93         }
94     )->store;
95     my $just_one = Koha::Account::Line->new(
96         {
97             borrowernumber    => $patron_2->id,
98             amount            => 3,
99             amountoutstanding => 3,
100             interface         => 'commandline',
101             debit_type_code   => 'OVERDUE'
102         }
103     )->store;
104     Koha::Account::Line->new(
105         {
106             borrowernumber    => $patron_2->id,
107             amount            => -6,
108             amountoutstanding => -6,
109             interface         => 'commandline',
110             credit_type_code  => 'PAYMENT'
111         }
112     )->store;
113     $lines = $patron_2->account->outstanding_debits();
114     is( $lines->total_outstanding, 3, "Total if some outstanding debits and some credits is only debits" );
115     is( $lines->count, 1, "With 1 outstanding debits, we get back a Lines object with 1 lines" );
116     my $the_line = Koha::Account::Lines->find( $just_one->id );
117     is_deeply( $the_line->unblessed, $lines->next->unblessed, "We get back the one correct line");
118
119     my $patron_3  = $builder->build_object({ class => 'Koha::Patrons' });
120     my $account_3 = $patron_3->account;
121     $account_3->add_credit( { amount => 2,   interface => 'commandline' } );
122     $account_3->add_credit( { amount => 20,  interface => 'commandline' } );
123     $account_3->add_credit( { amount => 200, interface => 'commandline' } );
124     $lines = $account_3->outstanding_debits();
125     is( $lines->total_outstanding, 0, "Total if no outstanding debits total is 0" );
126     is( $lines->count, 0, "With 0 outstanding debits, we get back a Lines object with 0 lines" );
127
128     my $patron_4  = $builder->build_object({ class => 'Koha::Patrons' });
129     my $account_4 = $patron_4->account;
130     $lines = $account_4->outstanding_debits();
131     is( $lines->total_outstanding, 0, "Total if no outstanding debits is 0" );
132     is( $lines->count, 0, "With no outstanding debits, we get back a Lines object with 0 lines" );
133
134     # create a pathological credit with amountoutstanding > 0 (BZ 14591)
135     Koha::Account::Line->new(
136         {
137             borrowernumber    => $patron_4->id,
138             amount            => -3,
139             amountoutstanding => 3,
140             interface         => 'commandline',
141             credit_type_code  => 'PAYMENT'
142         }
143     )->store();
144     $lines = $account_4->outstanding_debits();
145     is( $lines->count, 0, 'No credits are confused with debits because of the amountoutstanding value' );
146
147     $schema->storage->txn_rollback;
148 };
149
150 subtest 'outstanding_credits() tests' => sub {
151
152     plan tests => 17;
153
154     $schema->storage->txn_begin;
155
156     my $patron  = $builder->build_object({ class => 'Koha::Patrons' });
157     my $account = $patron->account;
158
159     my @generated_lines;
160     push @generated_lines, $account->add_credit({ amount => 1, interface => 'commandline' });
161     push @generated_lines, $account->add_credit({ amount => 2, interface => 'commandline' });
162     push @generated_lines, $account->add_credit({ amount => 3, interface => 'commandline' });
163     push @generated_lines, $account->add_credit({ amount => 4, interface => 'commandline' });
164
165     my $lines     = $account->outstanding_credits();
166     my @lines_arr = $account->outstanding_credits();
167
168     is( ref($lines), 'Koha::Account::Lines', 'Called in scalar context, outstanding_credits returns a Koha::Account::Lines object' );
169     is( $lines->total_outstanding, -10, 'Outstandig credits total is correctly calculated' );
170
171     my $i = 0;
172     foreach my $line ( @{ $lines->as_list } ) {
173         my $fetched_line = Koha::Account::Lines->find( $generated_lines[$i]->id );
174         is_deeply( $line->unblessed, $fetched_line->unblessed, "Fetched line matches the generated one ($i)" );
175         is_deeply( $lines_arr[$i]->unblessed, $fetched_line->unblessed, "Fetched line matches the generated one ($i)" );
176         is( ref($lines_arr[$i]), 'Koha::Account::Line', 'outstanding_debits returns a list of Koha::Account::Line objects in list context' );
177         $i++;
178     }
179
180     my $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
181     $account  = $patron_2->account;
182     $lines       = $account->outstanding_credits();
183     is( $lines->total_outstanding, 0, "Total if no outstanding credits is 0" );
184     is( $lines->count, 0, "With no outstanding credits, we get back a Lines object with 0 lines" );
185
186     # create a pathological debit with amountoutstanding < 0 (BZ 14591)
187     Koha::Account::Line->new(
188         {
189             borrowernumber    => $patron_2->id,
190             amount            => 2,
191             amountoutstanding => -3,
192             interface         => 'commandline',
193             debit_type_code   => 'OVERDUE'
194         }
195     )->store();
196     $lines = $account->outstanding_credits();
197     is( $lines->count, 0, 'No debits are confused with credits because of the amountoutstanding value' );
198
199     $schema->storage->txn_rollback;
200 };
201
202 subtest 'add_credit() tests' => sub {
203
204     plan tests => 21;
205
206     $schema->storage->txn_begin;
207
208     # delete logs and statistics
209     my $action_logs = $schema->resultset('ActionLog')->search()->count;
210     my $statistics = $schema->resultset('Statistic')->search()->count;
211
212     my $patron  = $builder->build_object( { class => 'Koha::Patrons' } );
213     my $account = Koha::Account->new( { patron_id => $patron->borrowernumber } );
214
215     is( $account->balance, 0, 'Patron has no balance' );
216
217     # Disable logs
218     t::lib::Mocks::mock_preference( 'FinesLog', 0 );
219
220     throws_ok {
221         $account->add_credit(
222             {
223                 amount      => 25,
224                 description => 'Payment of 25',
225                 library_id  => $patron->branchcode,
226                 note        => 'not really important',
227                 type        => 'PAYMENT',
228                 user_id     => $patron->id
229             }
230         );
231     }
232     'Koha::Exceptions::MissingParameter', 'Exception thrown if interface parameter missing';
233
234     my $line_1 = $account->add_credit(
235         {
236             amount      => 25,
237             description => 'Payment of 25',
238             library_id  => $patron->branchcode,
239             note        => 'not really important',
240             type        => 'PAYMENT',
241             user_id     => $patron->id,
242             interface   => 'commandline'
243         }
244     );
245
246     is( $account->balance, -25, 'Patron has a balance of -25' );
247     is( $schema->resultset('ActionLog')->count(), $action_logs + 0, 'No log was added' );
248     is( $schema->resultset('Statistic')->count(), $statistics + 1, 'Action added to statistics' );
249     is( $line_1->credit_type_code, 'PAYMENT', 'Account type is correctly set' );
250     ok( $line_1->amount < 0, 'Credit amount is stored as a negative' );
251
252     # Enable logs
253     t::lib::Mocks::mock_preference( 'FinesLog', 1 );
254
255     my $line_2 = $account->add_credit(
256         {   amount      => 37,
257             description => 'Payment of 37',
258             library_id  => $patron->branchcode,
259             note        => 'not really important',
260             user_id     => $patron->id,
261             interface   => 'commandline'
262         }
263     );
264
265     is( $account->balance, -62, 'Patron has a balance of -25' );
266     is( $schema->resultset('ActionLog')->count(), $action_logs + 1, 'Log was added' );
267     is( $schema->resultset('Statistic')->count(), $statistics + 2, 'Action added to statistics' );
268     is( $line_2->credit_type_code, 'PAYMENT', 'Account type is correctly set' );
269     ok( $line_1->amount < 0, 'Credit amount is stored as a negative' );
270
271     # offsets have the credit_id set to accountlines_id, and debit_id is undef
272     my $offset_1 = Koha::Account::Offsets->search({ credit_id => $line_1->id })->next;
273     my $offset_2 = Koha::Account::Offsets->search({ credit_id => $line_2->id })->next;
274
275     is( $offset_1->credit_id, $line_1->id, 'No debit_id is set for credits' );
276     is( $offset_1->debit_id, undef, 'No debit_id is set for credits' );
277     ok( $offset_1->amount > 0, 'Credit creation offset is a positive' );
278     is( $offset_2->credit_id, $line_2->id, 'No debit_id is set for credits' );
279     is( $offset_2->debit_id, undef, 'No debit_id is set for credits' );
280     ok( $offset_2->amount > 0, 'Credit creation offset is a positive' );
281
282     my $line_3 = $account->add_credit(
283         {
284             amount      => 20,
285             description => 'Manual credit applied',
286             library_id  => $patron->branchcode,
287             user_id     => $patron->id,
288             type        => 'FORGIVEN',
289             interface   => 'commandline'
290         }
291     );
292
293     is( $schema->resultset('ActionLog')->count(), $action_logs + 2, 'Log was added' );
294     is( $schema->resultset('Statistic')->count(), $statistics + 2, 'No action added to statistics, because of credit type' );
295
296     # Enable cash registers
297     t::lib::Mocks::mock_preference( 'UseCashRegisters', 1 );
298     throws_ok {
299         $account->add_credit(
300             {
301                 amount       => 20,
302                 description  => 'Cash payment without cash register',
303                 library_id   => $patron->branchcode,
304                 user_id      => $patron->id,
305                 payment_type => 'CASH',
306                 interface    => 'intranet'
307             }
308         );
309     }
310     'Koha::Exceptions::Account::RegisterRequired',
311       'Exception thrown for UseCashRegisters:1 + payment_type:CASH + cash_register:undef';
312
313     # Disable cash registers
314     t::lib::Mocks::mock_preference( 'UseCashRegisters', 0 );
315
316     $schema->storage->txn_rollback;
317 };
318
319 subtest 'add_debit() tests' => sub {
320
321     plan tests => 14;
322
323     $schema->storage->txn_begin;
324
325     # delete logs and statistics
326     my $action_logs = $schema->resultset('ActionLog')->search()->count;
327     my $statistics  = $schema->resultset('Statistic')->search()->count;
328
329     my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
330     my $account =
331       Koha::Account->new( { patron_id => $patron->borrowernumber } );
332
333     is( $account->balance, 0, 'Patron has no balance' );
334
335     throws_ok {
336     $account->add_debit(
337         {
338             amount      => -5,
339             description => 'amount validation failure',
340             library_id  => $patron->branchcode,
341             note        => 'this should fail anyway',
342             type        => 'RENT',
343             user_id     => $patron->id,
344             interface   => 'commandline'
345         }
346     ); } 'Koha::Exceptions::Account::AmountNotPositive', 'Expected validation exception thrown (amount)';
347
348     throws_ok {
349         local *STDERR;
350         open STDERR, '>', '/dev/null';
351         $account->add_debit(
352             {
353                 amount      => 5,
354                 description => 'type validation failure',
355                 library_id  => $patron->branchcode,
356                 note        => 'this should fail anyway',
357                 type        => 'failure',
358                 user_id     => $patron->id,
359                 interface   => 'commandline'
360             }
361         );
362         close STDERR;
363     }
364     'Koha::Exceptions::Account::UnrecognisedType',
365       'Expected validation exception thrown (type)';
366
367     throws_ok {
368     $account->add_debit(
369         {
370             amount      => 25,
371             description => 'Rental charge of 25',
372             library_id  => $patron->branchcode,
373             note        => 'not really important',
374             type        => 'RENT',
375             user_id     => $patron->id
376         }
377     ); } 'Koha::Exceptions::MissingParameter', 'Exception thrown if interface parameter missing';
378
379     # Disable logs
380     t::lib::Mocks::mock_preference( 'FinesLog', 0 );
381
382     my $line_1 = $account->add_debit(
383         {
384             amount      => 25,
385             description => 'Rental charge of 25',
386             library_id  => $patron->branchcode,
387             note        => 'not really important',
388             type        => 'RENT',
389             user_id     => $patron->id,
390             interface   => 'commandline'
391         }
392     );
393
394     is( $account->balance, 25, 'Patron has a balance of 25' );
395     is(
396         $schema->resultset('ActionLog')->count(),
397         $action_logs + 0,
398         'No log was added'
399     );
400     is(
401         $line_1->debit_type_code,
402         'RENT',
403         'Account type is correctly set'
404     );
405
406     # Enable logs
407     t::lib::Mocks::mock_preference( 'FinesLog', 1 );
408
409     my $line_2   = $account->add_debit(
410         {
411             amount      => 37,
412             description => 'Rental charge of 37',
413             library_id  => $patron->branchcode,
414             note        => 'not really important',
415             type        => 'RENT',
416             user_id     => $patron->id,
417             interface   => 'commandline'
418         }
419     );
420
421     is( $account->balance, 62, 'Patron has a balance of 62' );
422     is(
423         $schema->resultset('ActionLog')->count(),
424         $action_logs + 1,
425         'Log was added'
426     );
427     is(
428         $line_2->debit_type_code,
429         'RENT',
430         'Account type is correctly set'
431     );
432
433     # offsets have the debit_id set to accountlines_id, and credit_id is undef
434     my $offset_1 =
435       Koha::Account::Offsets->search( { debit_id => $line_1->id } )->next;
436     my $offset_2 =
437       Koha::Account::Offsets->search( { debit_id => $line_2->id } )->next;
438
439     is( $offset_1->debit_id,  $line_1->id, 'debit_id is set for debit 1' );
440     is( $offset_1->credit_id, undef,       'credit_id is not set for debit 1' );
441     is( $offset_2->debit_id,  $line_2->id, 'debit_id is set for debit 2' );
442     is( $offset_2->credit_id, undef,       'credit_id is not set for debit 2' );
443
444     $schema->storage->txn_rollback;
445 };
446
447 subtest 'lines() tests' => sub {
448
449     plan tests => 1;
450
451     $schema->storage->txn_begin;
452
453     my $patron  = $builder->build_object({ class => 'Koha::Patrons' });
454     my $account = $patron->account;
455
456     # Add Credits
457     $account->add_credit({ amount => 1, interface => 'commandline' });
458     $account->add_credit({ amount => 2, interface => 'commandline' });
459     $account->add_credit({ amount => 3, interface => 'commandline' });
460     $account->add_credit({ amount => 4, interface => 'commandline' });
461
462     # Add Debits
463     $account->add_debit({ amount => 1, interface => 'commandline', type => 'OVERDUE' });
464     $account->add_debit({ amount => 2, interface => 'commandline', type => 'OVERDUE' });
465     $account->add_debit({ amount => 3, interface => 'commandline', type => 'OVERDUE' });
466     $account->add_debit({ amount => 4, interface => 'commandline', type => 'OVERDUE' });
467
468     # Paid Off
469     $account->add_credit( { amount => 1, interface => 'commandline' } )
470         ->apply( { debits => [ $account->outstanding_debits->as_list ] } );
471
472     my $lines = $account->lines;
473     is( $lines->_resultset->count, 9, "All accountlines (debits, credits and paid off) were fetched");
474
475     $schema->storage->txn_rollback;
476 };
477
478 subtest 'reconcile_balance' => sub {
479
480     plan tests => 4;
481
482     subtest 'more credit than debit' => sub {
483
484         plan tests => 6;
485
486         $schema->storage->txn_begin;
487
488         my $patron  = $builder->build_object({ class => 'Koha::Patrons' });
489         my $account = $patron->account;
490
491         # Add Credits
492         $account->add_credit({ amount => 1, interface => 'commandline' });
493         $account->add_credit({ amount => 2, interface => 'commandline' });
494         $account->add_credit({ amount => 3, interface => 'commandline' });
495         $account->add_credit({ amount => 4, interface => 'commandline' });
496         $account->add_credit({ amount => 5, interface => 'commandline' });
497
498         # Add Debits
499         $account->add_debit({ amount => 1, interface => 'commandline', type => 'OVERDUE' });
500         $account->add_debit({ amount => 2, interface => 'commandline', type => 'OVERDUE' });
501         $account->add_debit({ amount => 3, interface => 'commandline', type => 'OVERDUE' });
502         $account->add_debit({ amount => 4, interface => 'commandline', type => 'OVERDUE' });
503
504         # Paid Off
505         Koha::Account::Line->new(
506             {
507                 borrowernumber    => $patron->id,
508                 amount            => 1,
509                 amountoutstanding => 0,
510                 interface         => 'commandline',
511                 debit_type_code   => 'OVERDUE'
512             }
513         )->store;
514         Koha::Account::Line->new(
515             {
516                 borrowernumber    => $patron->id,
517                 amount            => 1,
518                 amountoutstanding => 0,
519                 interface         => 'commandline',
520                 debit_type_code   => 'OVERDUE'
521             }
522         )->store;
523
524         is( $account->balance(), -5, "Account balance is -5" );
525         is( $account->outstanding_debits->total_outstanding, 10, 'Outstanding debits sum 10' );
526         is( $account->outstanding_credits->total_outstanding, -15, 'Outstanding credits sum -15' );
527
528         $account->reconcile_balance();
529
530         is( $account->balance(), -5, "Account balance is -5" );
531         is( $account->outstanding_debits->total_outstanding, 0, 'No outstanding debits' );
532         is( $account->outstanding_credits->total_outstanding, -5, 'Outstanding credits sum -5' );
533
534         $schema->storage->txn_rollback;
535     };
536
537     subtest 'same debit as credit' => sub {
538
539         plan tests => 6;
540
541         $schema->storage->txn_begin;
542
543         my $patron  = $builder->build_object({ class => 'Koha::Patrons' });
544         my $account = $patron->account;
545
546         # Add Credits
547         $account->add_credit({ amount => 1, interface => 'commandline' });
548         $account->add_credit({ amount => 2, interface => 'commandline' });
549         $account->add_credit({ amount => 3, interface => 'commandline' });
550         $account->add_credit({ amount => 4, interface => 'commandline' });
551
552         # Add Debits
553         $account->add_debit({ amount => 1, interface => 'commandline', type => 'OVERDUE' });
554         $account->add_debit({ amount => 2, interface => 'commandline', type => 'OVERDUE' });
555         $account->add_debit({ amount => 3, interface => 'commandline', type => 'OVERDUE' });
556         $account->add_debit({ amount => 4, interface => 'commandline', type => 'OVERDUE' });
557
558         # Paid Off
559         Koha::Account::Line->new(
560             {
561                 borrowernumber    => $patron->id,
562                 amount            => 1,
563                 amountoutstanding => 0,
564                 interface         => 'commandline',
565                 debit_type_code   => 'OVERDUE'
566             }
567         )->store;
568         Koha::Account::Line->new(
569             {
570                 borrowernumber    => $patron->id,
571                 amount            => 1,
572                 amountoutstanding => 0,
573                 interface         => 'commandline',
574                 debit_type_code   => 'OVERDUE'
575             }
576         )->store;
577
578         is( $account->balance(), 0, "Account balance is 0" );
579         is( $account->outstanding_debits->total_outstanding, 10, 'Outstanding debits sum 10' );
580         is( $account->outstanding_credits->total_outstanding, -10, 'Outstanding credits sum -10' );
581
582         $account->reconcile_balance();
583
584         is( $account->balance(), 0, "Account balance is 0" );
585         is( $account->outstanding_debits->total_outstanding, 0, 'No outstanding debits' );
586         is( $account->outstanding_credits->total_outstanding, 0, 'Outstanding credits sum 0' );
587
588         $schema->storage->txn_rollback;
589     };
590
591     subtest 'more debit than credit' => sub {
592
593         plan tests => 6;
594
595         $schema->storage->txn_begin;
596
597         my $patron  = $builder->build_object({ class => 'Koha::Patrons' });
598         my $account = $patron->account;
599
600         # Add Credits
601         $account->add_credit({ amount => 1, interface => 'commandline' });
602         $account->add_credit({ amount => 2, interface => 'commandline' });
603         $account->add_credit({ amount => 3, interface => 'commandline' });
604         $account->add_credit({ amount => 4, interface => 'commandline' });
605
606         # Add Debits
607         $account->add_debit({ amount => 1, interface => 'commandline', type => 'OVERDUE' });
608         $account->add_debit({ amount => 2, interface => 'commandline', type => 'OVERDUE' });
609         $account->add_debit({ amount => 3, interface => 'commandline', type => 'OVERDUE' });
610         $account->add_debit({ amount => 4, interface => 'commandline', type => 'OVERDUE' });
611         $account->add_debit({ amount => 5, interface => 'commandline', type => 'OVERDUE' });
612
613         # Paid Off
614         Koha::Account::Line->new(
615             {
616                 borrowernumber    => $patron->id,
617                 amount            => 1,
618                 amountoutstanding => 0,
619                 interface         => 'commandline',
620                 debit_type_code   => 'OVERDUE'
621             }
622         )->store;
623         Koha::Account::Line->new(
624             {
625                 borrowernumber    => $patron->id,
626                 amount            => 1,
627                 amountoutstanding => 0,
628                 interface         => 'commandline',
629                 debit_type_code   => 'OVERDUE'
630             }
631         )->store;
632
633         is( $account->balance(), 5, "Account balance is 5" );
634         is( $account->outstanding_debits->total_outstanding, 15, 'Outstanding debits sum 15' );
635         is( $account->outstanding_credits->total_outstanding, -10, 'Outstanding credits sum -10' );
636
637         $account->reconcile_balance();
638
639         is( $account->balance(), 5, "Account balance is 5" );
640         is( $account->outstanding_debits->total_outstanding, 5, 'Outstanding debits sum 5' );
641         is( $account->outstanding_credits->total_outstanding, 0, 'Outstanding credits sum 0' );
642
643         $schema->storage->txn_rollback;
644     };
645
646     subtest 'credits are applied to older debits first' => sub {
647
648         plan tests => 9;
649
650         $schema->storage->txn_begin;
651
652         my $patron  = $builder->build_object({ class => 'Koha::Patrons' });
653         my $account = $patron->account;
654
655         # Add Credits
656         $account->add_credit({ amount => 1, interface => 'commandline' });
657         $account->add_credit({ amount => 3, interface => 'commandline' });
658
659         # Add Debits
660         my $debit_1 = $account->add_debit({ amount => 1, interface => 'commandline', type => 'OVERDUE' });
661         my $debit_2 = $account->add_debit({ amount => 2, interface => 'commandline', type => 'OVERDUE' });
662         my $debit_3 = $account->add_debit({ amount => 3, interface => 'commandline', type => 'OVERDUE' });
663
664         is( $account->balance(), 2, "Account balance is 2" );
665         is( $account->outstanding_debits->total_outstanding, 6, 'Outstanding debits sum 6' );
666         is( $account->outstanding_credits->total_outstanding, -4, 'Outstanding credits sum -4' );
667
668         $account->reconcile_balance();
669
670         is( $account->balance(), 2, "Account balance is 2" );
671         is( $account->outstanding_debits->total_outstanding, 2, 'Outstanding debits sum 2' );
672         is( $account->outstanding_credits->total_outstanding, 0, 'Outstanding credits sum 0' );
673
674         $debit_1->discard_changes;
675         is( $debit_1->amountoutstanding + 0, 0, 'Old debit payed' );
676         $debit_2->discard_changes;
677         is( $debit_2->amountoutstanding + 0, 0, 'Old debit payed' );
678         $debit_3->discard_changes;
679         is( $debit_3->amountoutstanding + 0, 2, 'Newest debit only partially payed' );
680
681         $schema->storage->txn_rollback;
682     };
683 };
684
685 subtest 'pay() tests' => sub {
686
687     plan tests => 6;
688
689     $schema->storage->txn_begin;
690
691     # Disable renewing upon fine payment
692     t::lib::Mocks::mock_preference( 'RenewAccruingItemWhenPaid', 0 );
693
694     my $patron  = $builder->build_object({ class => 'Koha::Patrons' });
695     my $library = $builder->build_object({ class => 'Koha::Libraries' });
696     my $account = $patron->account;
697
698     my $context = Test::MockModule->new('C4::Context');
699     $context->mock( 'userenv', { branch => $library->id } );
700
701     my $credit_1_id = $account->pay({ amount => 200 })->{payment_id};
702     my $credit_1    = Koha::Account::Lines->find( $credit_1_id );
703
704     is( $credit_1->branchcode, undef, 'No branchcode is set if library_id was not passed' );
705
706     my $credit_2_id = $account->pay({ amount => 150, library_id => $library->id })->{payment_id};
707     my $credit_2    = Koha::Account::Lines->find( $credit_2_id );
708
709     is( $credit_2->branchcode, $library->id, 'branchcode set because library_id was passed' );
710
711     # Enable cash registers
712     t::lib::Mocks::mock_preference( 'UseCashRegisters', 1 );
713     throws_ok {
714         $account->pay(
715             {
716                 amount       => 20,
717                 payment_type => 'CASH',
718                 interface    => 'intranet'
719             }
720         );
721     }
722     'Koha::Exceptions::Account::RegisterRequired',
723       'Exception thrown for UseCashRegisters:1 + payment_type:CASH + cash_register:undef';
724
725     # Disable cash registers
726     t::lib::Mocks::mock_preference( 'UseCashRegisters', 0 );
727
728     # Undef userenv
729     $context->mock( 'userenv', undef );
730     my $result = $account->pay(
731         {
732             amount => 20,
733             payment_Type => 'CASH',
734             interface => 'intranet'
735         }
736     );
737     ok($result, "Koha::Account->pay functions without a userenv");
738     my $payment = Koha::Account::Lines->find({accountlines_id => $result->{payment_id}});
739     is($payment->manager_id, undef, "manager_id left undefined when no userenv found");
740
741     subtest 'UseEmailReceipts tests' => sub {
742
743         plan tests => 5;
744
745         t::lib::Mocks::mock_preference( 'UseEmailReceipts', 1 );
746
747         my %params;
748
749         my $mocked_letters = Test::MockModule->new('C4::Letters');
750         # we want to test the params
751         $mocked_letters->mock( 'GetPreparedLetter', sub {
752             %params = @_;
753             return 1;
754         });
755         # we don't care about EnqueueLetter for now
756         $mocked_letters->mock( 'EnqueueLetter', sub {
757             return 1;
758         });
759
760         $schema->storage->txn_begin;
761
762         my $patron  = $builder->build_object({ class => 'Koha::Patrons' });
763         my $account = $patron->account;
764
765         my $debit_1 = $account->add_debit( { amount => 5,  interface => 'commandline', type => 'OVERDUE' } );
766         my $debit_2 = $account->add_debit( { amount => 10,  interface => 'commandline', type => 'OVERDUE' } );
767
768         $account->pay({ amount => 6, lines => [ $debit_1, $debit_2 ] });
769         my @offsets = @{$params{substitute}{offsets}};
770
771         is( scalar @offsets, 2, 'Two offsets related to payment' );
772         is( ref($offsets[0]), 'Koha::Account::Offset', 'Type is correct' );
773         is( ref($offsets[1]), 'Koha::Account::Offset', 'Type is correct' );
774         is( $offsets[0]->type, 'APPLY', 'Only APPLY offsets are passed to the notice' );
775         is( $offsets[1]->type, 'APPLY', 'Only APPLY offsets are passed to the notice' );
776
777         $schema->storage->txn_rollback;
778     };
779
780     $schema->storage->txn_rollback;
781 };
782
783 subtest 'pay() handles lost items when paying a specific lost fee' => sub {
784
785     plan tests => 5;
786
787     $schema->storage->txn_begin;
788
789     my $patron  = $builder->build_object( { class => 'Koha::Patrons' } );
790     my $library = $builder->build_object( { class => 'Koha::Libraries' } );
791     my $account = $patron->account;
792
793     my $context = Test::MockModule->new('C4::Context');
794     $context->mock( 'userenv', { branch => $library->id } );
795
796     my $biblio = $builder->build_sample_biblio();
797     my $item =
798       $builder->build_sample_item( { biblionumber => $biblio->biblionumber } );
799
800     my $checkout = Koha::Checkout->new(
801         {
802             borrowernumber => $patron->id,
803             itemnumber     => $item->id,
804             date_due       => \'NOW()',
805             branchcode     => $patron->branchcode,
806             issuedate      => \'NOW()',
807         }
808     )->store();
809
810     $item->itemlost('1')->store();
811
812     my $accountline = Koha::Account::Line->new(
813         {
814             issue_id       => $checkout->id,
815             borrowernumber => $patron->id,
816             itemnumber     => $item->id,
817             date           => \'NOW()',
818             debit_type_code    => 'LOST',
819             interface      => 'cli',
820             amount => '1',
821             amountoutstanding => '1',
822         }
823     )->store();
824
825     $account->pay(
826         {
827             amount     => .5,
828             library_id => $library->id,
829             lines      => [$accountline],
830         }
831     );
832
833     $accountline = Koha::Account::Lines->find( $accountline->id );
834     is( $accountline->amountoutstanding+0, .5, 'Account line was paid down by half' );
835
836     $checkout = Koha::Checkouts->find( $checkout->id );
837     ok( $checkout, 'Item still checked out to patron' );
838
839     $account->pay(
840         {
841             amount     => 0.5,
842             library_id => $library->id,
843             lines      => [$accountline],
844         }
845     );
846
847     $accountline = Koha::Account::Lines->find( $accountline->id );
848     is( $accountline->amountoutstanding+0, 0, 'Account line was paid down by half' );
849
850     $checkout = Koha::Checkouts->find( $checkout->id );
851     ok( !$checkout, 'Item was removed from patron account' );
852
853     subtest 'item was not checked out to the same patron' => sub {
854         plan tests => 1;
855
856         my $patron_2 = $builder->build_object(
857             {
858                 class => 'Koha::Patrons',
859                 value => { branchcode => $library->branchcode }
860             }
861         );
862         $item->itemlost('1')->store();
863         C4::Accounts::chargelostitem( $patron->borrowernumber, $item->itemnumber, 5, "lost" );
864         my $accountline = Koha::Account::Lines->search(
865             {
866                 borrowernumber  => $patron->borrowernumber,
867                 itemnumber      => $item->itemnumber,
868                 debit_type_code => 'LOST'
869             }
870         )->next;
871         my $checkout = Koha::Checkout->new(
872             {
873                 borrowernumber => $patron_2->borrowernumber,
874                 itemnumber     => $item->itemnumber,
875                 date_due       => \'NOW()',
876                 branchcode     => $patron_2->branchcode,
877                 issuedate      => \'NOW()',
878             }
879         )->store();
880
881         $patron->account->pay(
882             {
883                 amount     => 5,
884                 library_id => $library->branchcode,
885                 lines      => [$accountline],
886             }
887         );
888
889         ok(
890             Koha::Checkouts->find( $checkout->issue_id ),
891             'If the item is checked out to another patron, a lost item should not be returned if lost fee is paid'
892         );
893
894     };
895
896     $schema->storage->txn_rollback;
897 };
898
899 subtest 'pay() handles lost items when paying by amount ( not specifying the lost fee )' => sub {
900
901     plan tests => 4;
902
903     $schema->storage->txn_begin;
904
905     # Enable AccountAutoReconcile
906     t::lib::Mocks::mock_preference( 'AccountAutoReconcile', 1 );
907
908     my $patron  = $builder->build_object( { class => 'Koha::Patrons' } );
909     my $library = $builder->build_object( { class => 'Koha::Libraries' } );
910     my $account = $patron->account;
911
912     my $context = Test::MockModule->new('C4::Context');
913     $context->mock( 'userenv', { branch => $library->id } );
914
915     my $biblio = $builder->build_sample_biblio();
916     my $item =
917       $builder->build_sample_item( { biblionumber => $biblio->biblionumber } );
918
919     my $checkout = Koha::Checkout->new(
920         {
921             borrowernumber => $patron->id,
922             itemnumber     => $item->id,
923             date_due       => \'NOW()',
924             branchcode     => $patron->branchcode,
925             issuedate      => \'NOW()',
926         }
927     )->store();
928
929     $item->itemlost('1')->store();
930
931     my $accountline = Koha::Account::Line->new(
932         {
933             issue_id       => $checkout->id,
934             borrowernumber => $patron->id,
935             itemnumber     => $item->id,
936             date           => \'NOW()',
937             debit_type_code    => 'LOST',
938             interface      => 'cli',
939             amount => '1',
940             amountoutstanding => '1',
941         }
942     )->store();
943
944     $account->pay(
945         {
946             amount     => .5,
947             library_id => $library->id,
948         }
949     );
950
951     $accountline = Koha::Account::Lines->find( $accountline->id );
952     is( $accountline->amountoutstanding+0, .5, 'Account line was paid down by half' );
953
954     $checkout = Koha::Checkouts->find( $checkout->id );
955     ok( $checkout, 'Item still checked out to patron' );
956
957     $account->pay(
958         {
959             amount     => .5,,
960             library_id => $library->id,
961         }
962     );
963
964     $accountline = Koha::Account::Lines->find( $accountline->id );
965     is( $accountline->amountoutstanding+0, 0, 'Account line was paid down by half' );
966
967     $checkout = Koha::Checkouts->find( $checkout->id );
968     ok( !$checkout, 'Item was removed from patron account' );
969
970     $schema->storage->txn_rollback;
971 };
972
973 subtest 'pay() renews items when appropriate' => sub {
974
975     plan tests => 7;
976
977     $schema->storage->txn_begin;
978
979     my $patron  = $builder->build_object( { class => 'Koha::Patrons' } );
980     my $library = $builder->build_object( { class => 'Koha::Libraries' } );
981     my $account = $patron->account;
982
983     my $context = Test::MockModule->new('C4::Context');
984     $context->mock( 'userenv', { branch => $library->id } );
985
986     my $biblio = $builder->build_sample_biblio();
987     my $item =
988       $builder->build_sample_item( { biblionumber => $biblio->biblionumber } );
989
990     my $now = dt_from_string();
991     my $seven_weeks = DateTime::Duration->new(weeks => 7);
992     my $five_weeks = DateTime::Duration->new(weeks => 5);
993     my $seven_weeks_ago = $now - $seven_weeks;
994     my $five_weeks_ago = $now - $five_weeks;
995
996     my $checkout = Koha::Checkout->new(
997         {
998             borrowernumber => $patron->id,
999             itemnumber     => $item->id,
1000             date_due       => $five_weeks_ago,
1001             branchcode     => $patron->branchcode,
1002             issuedate      => $seven_weeks_ago
1003         }
1004     )->store();
1005
1006     my $accountline = Koha::Account::Line->new(
1007         {
1008             issue_id       => $checkout->id,
1009             borrowernumber => $patron->id,
1010             itemnumber     => $item->id,
1011             date           => \'NOW()',
1012             debit_type_code => 'OVERDUE',
1013             status         => 'UNRETURNED',
1014             interface      => 'cli',
1015             amount => '1',
1016             amountoutstanding => '1',
1017         }
1018     )->store();
1019
1020     # Enable renewing upon fine payment
1021     t::lib::Mocks::mock_preference( 'RenewAccruingItemWhenPaid', 1 );
1022     my $called = 0;
1023     my $module = Test::MockModule->new('C4::Circulation');
1024     $module->mock('AddRenewal', sub { $called = 1; });
1025     $module->mock('CanBookBeRenewed', sub { return 1; });
1026     my $result = $account->pay(
1027         {
1028             amount     => '1',
1029             library_id => $library->id,
1030         }
1031     );
1032
1033     is( $called, 1, 'RenewAccruingItemWhenPaid causes C4::Circulation::AddRenew to be called when appropriate' );
1034     is(ref($result->{renew_result}), 'ARRAY', "Pay result contains 'renew_result' ARRAY" );
1035     is( scalar @{$result->{renew_result}}, 1, "renew_result contains one renewal result" );
1036     is( $result->{renew_result}->[0]->{itemnumber}, $item->id, "renew_result contains itemnumber of renewed item" );
1037
1038     # Reset test by adding a new overdue
1039     Koha::Account::Line->new(
1040         {
1041             issue_id       => $checkout->id,
1042             borrowernumber => $patron->id,
1043             itemnumber     => $item->id,
1044             date           => \'NOW()',
1045             debit_type_code => 'OVERDUE',
1046             status         => 'UNRETURNED',
1047             interface      => 'cli',
1048             amount => '1',
1049             amountoutstanding => '1',
1050         }
1051     )->store();
1052     $called = 0;
1053
1054     t::lib::Mocks::mock_preference( 'RenewAccruingItemWhenPaid', 0 );
1055     $result = $account->pay(
1056         {
1057             amount     => '1',
1058             library_id => $library->id,
1059         }
1060     );
1061
1062     is( $called, 0, 'C4::Circulation::AddRenew NOT called when RenewAccruingItemWhenPaid disabled' );
1063     is(ref($result->{renew_result}), 'ARRAY', "Pay result contains 'renew_result' ARRAY" );
1064     is( scalar @{$result->{renew_result}}, 0, "renew_result contains no renewal results" );
1065
1066     $schema->storage->txn_rollback;
1067 };
1068
1069 subtest 'Koha::Account::Line::apply() handles lost items' => sub {
1070
1071     plan tests => 4;
1072
1073     $schema->storage->txn_begin;
1074
1075     my $patron  = $builder->build_object( { class => 'Koha::Patrons' } );
1076     my $library = $builder->build_object( { class => 'Koha::Libraries' } );
1077     my $account = $patron->account;
1078
1079     my $context = Test::MockModule->new('C4::Context');
1080     $context->mock( 'userenv', { branch => $library->id } );
1081
1082     my $biblio = $builder->build_sample_biblio();
1083     my $item =
1084       $builder->build_sample_item( { biblionumber => $biblio->biblionumber } );
1085
1086     my $checkout = Koha::Checkout->new(
1087         {
1088             borrowernumber => $patron->id,
1089             itemnumber     => $item->id,
1090             date_due       => \'NOW()',
1091             branchcode     => $patron->branchcode,
1092             issuedate      => \'NOW()',
1093         }
1094     )->store();
1095
1096     $item->itemlost('1')->store();
1097
1098     my $debit = Koha::Account::Line->new(
1099         {
1100             issue_id          => $checkout->id,
1101             borrowernumber    => $patron->id,
1102             itemnumber        => $item->id,
1103             date              => \'NOW()',
1104             debit_type_code       => 'LOST',
1105             interface         => 'cli',
1106             amount            => '1',
1107             amountoutstanding => '1',
1108         }
1109     )->store();
1110
1111     my $credit = Koha::Account::Line->new(
1112         {
1113             borrowernumber    => $patron->id,
1114             date              => '1970-01-01 00:00:01',
1115             amount            => -.5,
1116             amountoutstanding => -.5,
1117             interface         => 'commandline',
1118             credit_type_code  => 'PAYMENT'
1119         }
1120     )->store();
1121     my $debits = $account->outstanding_debits;
1122     $credit->apply({ debits => [ $debits->as_list ] });
1123
1124     $debit = Koha::Account::Lines->find( $debit->id );
1125     is( $debit->amountoutstanding+0, .5, 'Account line was paid down by half' );
1126
1127     $checkout = Koha::Checkouts->find( $checkout->id );
1128     ok( $checkout, 'Item still checked out to patron' );
1129
1130     $credit = Koha::Account::Line->new(
1131         {
1132             borrowernumber    => $patron->id,
1133             date              => '1970-01-01 00:00:01',
1134             amount            => -.5,
1135             amountoutstanding => -.5,
1136             interface         => 'commandline',
1137             credit_type_code  => 'PAYMENT'
1138         }
1139     )->store();
1140     $debits = $account->outstanding_debits;
1141     $credit->apply({ debits => [ $debits->as_list ] });
1142
1143     $debit = Koha::Account::Lines->find( $debit->id );
1144     is( $debit->amountoutstanding+0, 0, 'Account line was paid down by half' );
1145
1146     $checkout = Koha::Checkouts->find( $checkout->id );
1147     ok( !$checkout, 'Item was removed from patron account' );
1148
1149     $schema->storage->txn_rollback;
1150 };
1151
1152 subtest 'Koha::Account::pay() generates credit number (Koha::Account::Line->store)' => sub {
1153     plan tests => 38;
1154
1155     $schema->storage->txn_begin;
1156
1157     Koha::Account::Lines->delete();
1158
1159     my $patron  = $builder->build_object( { class => 'Koha::Patrons' } );
1160     my $library = $builder->build_object( { class => 'Koha::Libraries' } );
1161     my $account = $patron->account;
1162
1163     #t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode });
1164     my $context = Test::MockModule->new('C4::Context');
1165     $context->mock( 'userenv', { branch => $library->id } );
1166
1167     my $now = dt_from_string;
1168     my $year = $now->year;
1169     my $month = $now->month;
1170     my ($accountlines_id, $accountline);
1171
1172     my $credit_type = Koha::Account::CreditTypes->find('PAYMENT');
1173     $credit_type->credit_number_enabled(1);
1174     $credit_type->store();
1175
1176     t::lib::Mocks::mock_preference('AutoCreditNumber', '');
1177     $accountlines_id = $account->pay({ amount => '1.00', library_id => $library->id })->{payment_id};
1178     $accountline = Koha::Account::Lines->find($accountlines_id);
1179     is($accountline->credit_number, undef, 'No credit number is generated when syspref is off');
1180
1181     t::lib::Mocks::mock_preference('AutoCreditNumber', 'incremental');
1182     for my $i (1..11) {
1183         $accountlines_id = $account->pay({ amount => '1.00', library_id => $library->id })->{payment_id};
1184         $accountline = Koha::Account::Lines->find($accountlines_id);
1185         is($accountline->credit_number, $i, "Incremental format credit number added for payments: $i");
1186     }
1187     $accountlines_id = $account->pay({ type => 'WRITEOFF', amount => '1.00', library_id => $library->id })->{payment_id};
1188     $accountline = Koha::Account::Lines->find($accountlines_id);
1189     is($accountline->credit_number, undef, "Incremental credit number not added for writeoff");
1190
1191     t::lib::Mocks::mock_preference('AutoCreditNumber', 'annual');
1192     for my $i (1..11) {
1193         $accountlines_id = $account->pay({ amount => '1.00', library_id => $library->id })->{payment_id};
1194         $accountline = Koha::Account::Lines->find($accountlines_id);
1195         is($accountline->credit_number, sprintf('%s-%04d', $year, $i), "Annual format credit number added for payments: " . sprintf('%s-%04d', $year, $i));
1196     }
1197     $accountlines_id = $account->pay({ type => 'WRITEOFF', amount => '1.00', library_id => $library->id })->{payment_id};
1198     $accountline = Koha::Account::Lines->find($accountlines_id);
1199     is($accountline->credit_number, undef, "Annual format credit number not aded for writeoff");
1200
1201     t::lib::Mocks::mock_preference('AutoCreditNumber', 'branchyyyymmincr');
1202     for my $i (1..11) {
1203         $accountlines_id = $account->pay({ amount => '1.00', library_id => $library->id })->{payment_id};
1204         $accountline = Koha::Account::Lines->find($accountlines_id);
1205         is($accountline->credit_number, sprintf('%s%d%02d%04d', $library->id, $year, $month, $i), "branchyyyymmincr format credit number added for payment: " . sprintf('%s%d%02d%04d', $library->id, $year, $month, $i));
1206     }
1207     $accountlines_id = $account->pay({ type => 'WRITEOFF', amount => '1.00', library_id => $library->id })->{payment_id};
1208     $accountline = Koha::Account::Lines->find($accountlines_id);
1209     is($accountline->credit_number, undef, "branchyyyymmincr format credit number not added for writeoff");
1210
1211     throws_ok {
1212         Koha::Account::Line->new(
1213             {
1214                 interface        => 'test',
1215                 amount           => -1,
1216                 credit_type_code => $credit_type->code,
1217                 credit_number    => 42
1218             }
1219         )->store;
1220     }
1221     'Koha::Exceptions::Account',
1222 "Exception thrown when AutoCreditNumber is enabled but credit_number is already defined";
1223
1224     $schema->storage->txn_rollback;
1225 };
1226
1227 subtest 'Koha::Account::payout_amount() tests' => sub {
1228     plan tests => 39;
1229
1230     $schema->storage->txn_begin;
1231
1232     # delete logs and statistics
1233     my $action_logs = $schema->resultset('ActionLog')->search()->count;
1234     my $statistics  = $schema->resultset('Statistic')->search()->count;
1235
1236     my $staff = $builder->build_object( { class => 'Koha::Patrons' } );
1237     my $library = $builder->build_object( { class => 'Koha::Libraries' } );
1238     my $register =
1239       $builder->build_object( { class => 'Koha::Cash::Registers' } );
1240     my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
1241     my $account =
1242       Koha::Account->new( { patron_id => $patron->borrowernumber } );
1243
1244     is( $account->balance, 0, 'Test patron has no balance' );
1245
1246     my $payout_params = {
1247         payout_type => 'CASH',
1248         branch      => $library->id,
1249         register_id => $register->id,
1250         staff_id    => $staff->id,
1251         interface   => 'intranet',
1252         amount      => -10,
1253     };
1254
1255     my @required_fields =
1256       ( 'interface', 'staff_id', 'branch', 'payout_type', 'amount' );
1257     for my $required_field (@required_fields) {
1258         my $this_payout = { %{$payout_params} };
1259         delete $this_payout->{$required_field};
1260
1261         throws_ok {
1262             $account->payout_amount($this_payout);
1263         }
1264         'Koha::Exceptions::MissingParameter',
1265           "Exception thrown if $required_field parameter missing";
1266     }
1267
1268     throws_ok {
1269         $account->payout_amount($payout_params);
1270     }
1271     'Koha::Exceptions::Account::AmountNotPositive',
1272       'Expected validation exception thrown (amount not positive)';
1273
1274     $payout_params->{amount} = 10;
1275     throws_ok {
1276         $account->payout_amount($payout_params);
1277     }
1278     'Koha::Exceptions::ParameterTooHigh',
1279       'Expected validation exception thrown (amount greater than outstanding)';
1280
1281     # Enable cash registers
1282     t::lib::Mocks::mock_preference( 'UseCashRegisters', 1 );
1283     throws_ok {
1284         $account->payout_amount($payout_params);
1285     }
1286     'Koha::Exceptions::Account::RegisterRequired',
1287 'Exception thrown for UseCashRegisters:1 + payout_type:CASH + cash_register:undef';
1288
1289     # Disable cash registers
1290     t::lib::Mocks::mock_preference( 'UseCashRegisters', 0 );
1291
1292     # Add some outstanding credits
1293     my $credit_1 = $account->add_credit( { amount => 2,  interface => 'commandline' } );
1294     my $credit_2 = $account->add_credit( { amount => 3,  interface => 'commandline' } );
1295     my $credit_3 = $account->add_credit( { amount => 5,  interface => 'commandline' } );
1296     my $credit_4 = $account->add_credit( { amount => 10, interface => 'commandline' } );
1297     my $credits = $account->outstanding_credits();
1298     is( $credits->count, 4, "Found 4 credits with outstanding amounts" );
1299     is( $credits->total_outstanding + 0, -20, "Total -20 outstanding credit" );
1300
1301     my $payout = $account->payout_amount($payout_params);
1302     is(ref($payout), 'Koha::Account::Line', 'Return the Koha::Account::Line object for the payout');
1303     is($payout->amount + 0, 10, "Payout amount recorded correctly");
1304     is($payout->amountoutstanding + 0, 0, "Full amount was paid out");
1305     $credits = $account->outstanding_credits();
1306     is($credits->count, 1, "Payout was applied against oldest outstanding credits first");
1307     is($credits->total_outstanding + 0, -10, "Total of 10 outstanding credit remaining");
1308
1309     my $offsets = Koha::Account::Offsets->search( { debit_id => $payout->id } );
1310     is( $offsets->count, 4, 'Four offsets generated' );
1311     my $offset = $offsets->next;
1312     is( $offset->type, 'CREATE', 'CREATE offset added for payout line' );
1313     is( $offset->amount * 1, 10, 'Correct offset amount recorded' );
1314     $offset = $offsets->next;
1315     is( $offset->credit_id, $credit_1->id, "Offset added against credit_1");
1316     is( $offset->type,       'APPLY', "APPLY used for offset_type" );
1317     is( $offset->amount * 1, -2,      'Correct amount offset against credit_1' );
1318     $offset = $offsets->next;
1319     is( $offset->credit_id, $credit_2->id, "Offset added against credit_2");
1320     is( $offset->type,       'APPLY', "APPLY used for offset_type" );
1321     is( $offset->amount * 1, -3,      'Correct amount offset against credit_2' );
1322     $offset = $offsets->next;
1323     is( $offset->credit_id, $credit_3->id, "Offset added against credit_3");
1324     is( $offset->type,       'APPLY', "APPLY used for offset_type" );
1325     is( $offset->amount * 1, -5,      'Correct amount offset against credit_3' );
1326
1327     my $credit_5 = $account->add_credit( { amount => 5, interface => 'commandline' } );
1328     $credits = $account->outstanding_credits();
1329     is($credits->count, 2, "New credit added");
1330     $payout_params->{amount} = 2.50;
1331     $payout_params->{credits} = [$credit_5];
1332     $payout = $account->payout_amount($payout_params);
1333
1334     $credits = $account->outstanding_credits();
1335     is($credits->count, 2, "Second credit not fully paid off");
1336     is($credits->total_outstanding + 0, -12.50, "12.50 outstanding credit remaining");
1337     $credit_4->discard_changes;
1338     $credit_5->discard_changes;
1339     is($credit_4->amountoutstanding + 0, -10, "Credit 4 unaffected when credit_5 was passed to payout_amount");
1340     is($credit_5->amountoutstanding + 0, -2.50, "Credit 5 correctly reduced when payout_amount called with credit_5 passed");
1341
1342     $offsets = Koha::Account::Offsets->search( { debit_id => $payout->id } );
1343     is( $offsets->count, 2, 'Two offsets generated' );
1344     $offset = $offsets->next;
1345     is( $offset->type, 'CREATE', 'CREATE offset added for payout line' );
1346     is( $offset->amount * 1, 2.50, 'Correct offset amount recorded' );
1347     $offset = $offsets->next;
1348     is( $offset->credit_id, $credit_5->id, "Offset added against credit_5");
1349     is( $offset->type,       'APPLY', "APPLY used for offset_type" );
1350     is( $offset->amount * 1, -2.50,      'Correct amount offset against credit_5' );
1351
1352     $schema->storage->txn_rollback;
1353 };
1354
1355 subtest 'Koha::Account::payin_amount() tests' => sub {
1356     plan tests => 36;
1357
1358     $schema->storage->txn_begin;
1359
1360     # delete logs and statistics
1361     my $action_logs = $schema->resultset('ActionLog')->search()->count;
1362     my $statistics  = $schema->resultset('Statistic')->search()->count;
1363
1364     my $staff = $builder->build_object( { class => 'Koha::Patrons' } );
1365     my $library = $builder->build_object( { class => 'Koha::Libraries' } );
1366     my $register =
1367       $builder->build_object( { class => 'Koha::Cash::Registers' } );
1368     my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
1369     my $account =
1370       Koha::Account->new( { patron_id => $patron->borrowernumber } );
1371
1372     is( $account->balance, 0, 'Test patron has no balance' );
1373
1374     my $payin_params = {
1375         type  => 'PAYMENT',
1376         payment_type => 'CASH',
1377         branch       => $library->id,
1378         register_id  => $register->id,
1379         staff_id     => $staff->id,
1380         interface    => 'intranet',
1381         amount       => -10,
1382     };
1383
1384     my @required_fields =
1385       ( 'interface', 'amount', 'type' );
1386     for my $required_field (@required_fields) {
1387         my $this_payin = { %{$payin_params} };
1388         delete $this_payin->{$required_field};
1389
1390         throws_ok {
1391             $account->payin_amount($this_payin);
1392         }
1393         'Koha::Exceptions::MissingParameter',
1394           "Exception thrown if $required_field parameter missing";
1395     }
1396
1397     throws_ok {
1398         $account->payin_amount($payin_params);
1399     }
1400     'Koha::Exceptions::Account::AmountNotPositive',
1401       'Expected validation exception thrown (amount not positive)';
1402
1403     $payin_params->{amount} = 10;
1404
1405     # Enable cash registers
1406     t::lib::Mocks::mock_preference( 'UseCashRegisters', 1 );
1407     throws_ok {
1408         $account->payin_amount($payin_params);
1409     }
1410     'Koha::Exceptions::Account::RegisterRequired',
1411 'Exception thrown for UseCashRegisters:1 + payment_type:CASH + cash_register:undef';
1412
1413     # Disable cash registers
1414     t::lib::Mocks::mock_preference( 'UseCashRegisters', 0 );
1415
1416     # Enable AccountAutoReconcile
1417     t::lib::Mocks::mock_preference( 'AccountAutoReconcile', 1 );
1418
1419     # Add some outstanding debits
1420     my $debit_1 = $account->add_debit( { amount => 2,  interface => 'commandline', type => 'OVERDUE' } );
1421     my $debit_2 = $account->add_debit( { amount => 3,  interface => 'commandline', type => 'OVERDUE' } );
1422     my $debit_3 = $account->add_debit( { amount => 5,  interface => 'commandline', type => 'OVERDUE' } );
1423     my $debit_4 = $account->add_debit( { amount => 10, interface => 'commandline', type => 'OVERDUE' } );
1424     my $debits = $account->outstanding_debits();
1425     is( $debits->count, 4, "Found 4 debits with outstanding amounts" );
1426     is( $debits->total_outstanding + 0, 20, "Total 20 outstanding debit" );
1427
1428     my $payin = $account->payin_amount($payin_params);
1429     is(ref($payin), 'Koha::Account::Line', 'Return the Koha::Account::Line object for the payin');
1430     is($payin->amount + 0, -10, "Payin amount recorded correctly");
1431     is($payin->amountoutstanding + 0, 0, "Full amount was used to pay debts");
1432     $debits = $account->outstanding_debits();
1433     is($debits->count, 1, "Payin was applied against oldest outstanding debits first");
1434     is($debits->total_outstanding + 0, 10, "Total of 10 outstanding debit remaining");
1435
1436     my $offsets = Koha::Account::Offsets->search( { credit_id => $payin->id } );
1437     is( $offsets->count, 4, 'Four offsets generated' );
1438     my $offset = $offsets->next;
1439     is( $offset->type, 'CREATE', 'CREATE offset added for payin line' );
1440     is( $offset->amount * 1, 10, 'Correct offset amount recorded' );
1441     $offset = $offsets->next;
1442     is( $offset->debit_id, $debit_1->id, "Offset added against debit_1");
1443     is( $offset->type,       'APPLY', "APPLY used for offset_type" );
1444     is( $offset->amount * 1, -2,      'Correct amount offset against debit_1' );
1445     $offset = $offsets->next;
1446     is( $offset->debit_id, $debit_2->id, "Offset added against debit_2");
1447     is( $offset->type,       'APPLY', "APPLY used for offset_type" );
1448     is( $offset->amount * 1, -3,      'Correct amount offset against debit_2' );
1449     $offset = $offsets->next;
1450     is( $offset->debit_id, $debit_3->id, "Offset added against debit_3");
1451     is( $offset->type,       'APPLY', "APPLY used for offset_type" );
1452     is( $offset->amount * 1, -5,      'Correct amount offset against debit_3' );
1453
1454     my $debit_5 = $account->add_debit( { amount => 5, interface => 'commandline', type => 'OVERDUE' } );
1455     $debits = $account->outstanding_debits();
1456     is($debits->count, 2, "New debit added");
1457     $payin_params->{amount} = 2.50;
1458     $payin_params->{debits} = [$debit_5];
1459     $payin = $account->payin_amount($payin_params);
1460
1461     $debits = $account->outstanding_debits();
1462     is($debits->count, 2, "Second debit not fully paid off");
1463     is($debits->total_outstanding + 0, 12.50, "12.50 outstanding debit remaining");
1464     $debit_4->discard_changes;
1465     $debit_5->discard_changes;
1466     is($debit_4->amountoutstanding + 0, 10, "Debit 4 unaffected when debit_5 was passed to payin_amount");
1467     is($debit_5->amountoutstanding + 0, 2.50, "Debit 5 correctly reduced when payin_amount called with debit_5 passed");
1468
1469     $offsets = Koha::Account::Offsets->search( { credit_id => $payin->id } );
1470     is( $offsets->count, 2, 'Two offsets generated' );
1471     $offset = $offsets->next;
1472     is( $offset->type, 'CREATE', 'CREATE offset added for payin line' );
1473     is( $offset->amount * 1, 2.50, 'Correct offset amount recorded' );
1474     $offset = $offsets->next;
1475     is( $offset->debit_id, $debit_5->id, "Offset added against debit_5");
1476     is( $offset->type,       'APPLY', "APPLY used for offset_type" );
1477     is( $offset->amount * 1, -2.50,      'Correct amount offset against debit_5' );
1478
1479     $schema->storage->txn_rollback;
1480 };