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