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