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