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