]> git.koha-community.org Git - koha.git/blob - t/db_dependent/Koha/Account/Line.t
Bug 28482: Unit test
[koha.git] / t / db_dependent / Koha / Account / Line.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 => 13;
23 use Test::Exception;
24 use Test::MockModule;
25
26 use DateTime;
27
28 use C4::Circulation qw/AddIssue AddReturn/;
29 use Koha::Account;
30 use Koha::Account::Lines;
31 use Koha::Account::Offsets;
32 use Koha::Items;
33 use Koha::DateUtils qw( dt_from_string );
34
35 use t::lib::Mocks;
36 use t::lib::TestBuilder;
37
38 my $schema = Koha::Database->new->schema;
39 my $builder = t::lib::TestBuilder->new;
40
41 subtest 'patron() tests' => sub {
42
43     plan tests => 3;
44
45     $schema->storage->txn_begin;
46
47     my $library = $builder->build( { source => 'Branch' } );
48     my $patron = $builder->build( { source => 'Borrower' } );
49
50     my $line = Koha::Account::Line->new(
51     {
52         borrowernumber => $patron->{borrowernumber},
53         debit_type_code    => "OVERDUE",
54         status         => "RETURNED",
55         amount         => 10,
56         interface      => 'commandline',
57     })->store;
58
59     my $account_line_patron = $line->patron;
60     is( ref( $account_line_patron ), 'Koha::Patron', 'Koha::Account::Line->patron should return a Koha::Patron' );
61     is( $line->borrowernumber, $account_line_patron->borrowernumber, 'Koha::Account::Line->patron should return the correct borrower' );
62
63     $line->borrowernumber(undef)->store;
64     is( $line->patron, undef, 'Koha::Account::Line->patron should return undef if no patron linked' );
65
66     $schema->storage->txn_rollback;
67 };
68
69 subtest 'item() tests' => sub {
70
71     plan tests => 3;
72
73     $schema->storage->txn_begin;
74
75     my $library = $builder->build( { source => 'Branch' } );
76     my $biblioitem = $builder->build( { source => 'Biblioitem' } );
77     my $patron = $builder->build( { source => 'Borrower' } );
78     my $item = Koha::Item->new(
79     {
80         biblionumber     => $biblioitem->{biblionumber},
81         biblioitemnumber => $biblioitem->{biblioitemnumber},
82         homebranch       => $library->{branchcode},
83         holdingbranch    => $library->{branchcode},
84         barcode          => 'some_barcode_12',
85         itype            => 'BK',
86     })->store;
87
88     my $line = Koha::Account::Line->new(
89     {
90         borrowernumber => $patron->{borrowernumber},
91         itemnumber     => $item->itemnumber,
92         debit_type_code    => "OVERDUE",
93         status         => "RETURNED",
94         amount         => 10,
95         interface      => 'commandline',
96     })->store;
97
98     my $account_line_item = $line->item;
99     is( ref( $account_line_item ), 'Koha::Item', 'Koha::Account::Line->item should return a Koha::Item' );
100     is( $line->itemnumber, $account_line_item->itemnumber, 'Koha::Account::Line->item should return the correct item' );
101
102     $line->itemnumber(undef)->store;
103     is( $line->item, undef, 'Koha::Account::Line->item should return undef if no item linked' );
104
105     $schema->storage->txn_rollback;
106 };
107
108 subtest 'library() tests' => sub {
109
110     plan tests => 4;
111
112     $schema->storage->txn_begin;
113
114     my $library = $builder->build_object( { class => 'Koha::Libraries' } );
115     my $patron  = $builder->build( { source => 'Borrower' } );
116
117     my $line = Koha::Account::Line->new(
118         {
119             borrowernumber  => $patron->{borrowernumber},
120             branchcode      => $library->branchcode,
121             debit_type_code => "OVERDUE",
122             status          => "RETURNED",
123             amount          => 10,
124             interface       => 'commandline',
125         }
126     )->store;
127
128     my $account_line_library = $line->library;
129     is( ref($account_line_library),
130         'Koha::Library',
131         'Koha::Account::Line->library should return a Koha::Library' );
132     is(
133         $line->branchcode,
134         $account_line_library->branchcode,
135         'Koha::Account::Line->library should return the correct library'
136     );
137
138     # Test ON DELETE SET NULL
139     $library->delete;
140     my $found = Koha::Account::Lines->find( $line->accountlines_id );
141     ok( $found, "Koha::Account::Line not deleted when the linked library is deleted" );
142
143     is( $found->library, undef,
144 'Koha::Account::Line->library should return undef if linked library has been deleted'
145     );
146
147     $schema->storage->txn_rollback;
148 };
149
150 subtest 'is_credit() and is_debit() tests' => sub {
151
152     plan tests => 4;
153
154     $schema->storage->txn_begin;
155
156     my $patron  = $builder->build_object({ class => 'Koha::Patrons' });
157     my $account = $patron->account;
158
159     my $credit = $account->add_credit({ amount => 100, user_id => $patron->id, interface => 'commandline' });
160
161     ok( $credit->is_credit, 'is_credit detects credits' );
162     ok( !$credit->is_debit, 'is_debit detects credits' );
163
164     my $debit = Koha::Account::Line->new(
165     {
166         borrowernumber => $patron->id,
167         debit_type_code    => "OVERDUE",
168         status         => "RETURNED",
169         amount         => 10,
170         interface      => 'commandline',
171     })->store;
172
173     ok( !$debit->is_credit, 'is_credit detects debits' );
174     ok( $debit->is_debit, 'is_debit detects debits');
175
176     $schema->storage->txn_rollback;
177 };
178
179 subtest 'apply() tests' => sub {
180
181     plan tests => 27;
182
183     $schema->storage->txn_begin;
184
185     my $patron  = $builder->build_object( { class => 'Koha::Patrons' } );
186     my $account = $patron->account;
187
188     my $credit = $account->add_credit( { amount => 100, user_id => $patron->id, interface => 'commandline' } );
189
190     my $debit_1 = Koha::Account::Line->new(
191         {   borrowernumber    => $patron->id,
192             debit_type_code       => "OVERDUE",
193             status            => "RETURNED",
194             amount            => 10,
195             amountoutstanding => 10,
196             interface         => 'commandline',
197         }
198     )->store;
199
200     my $debit_2 = Koha::Account::Line->new(
201         {   borrowernumber    => $patron->id,
202             debit_type_code       => "OVERDUE",
203             status            => "RETURNED",
204             amount            => 100,
205             amountoutstanding => 100,
206             interface         => 'commandline',
207         }
208     )->store;
209
210     $credit->discard_changes;
211     $debit_1->discard_changes;
212
213     my $debits = Koha::Account::Lines->search({ accountlines_id => $debit_1->id });
214     my $remaining_credit = $credit->apply( { debits => [ $debits->as_list ], offset_type => 'Manual Credit' } );
215     is( $remaining_credit * 1, 90, 'Remaining credit is correctly calculated' );
216     $credit->discard_changes;
217     is( $credit->amountoutstanding * -1, $remaining_credit, 'Remaining credit correctly stored' );
218
219     # re-read debit info
220     $debit_1->discard_changes;
221     is( $debit_1->amountoutstanding * 1, 0, 'Debit has been cancelled' );
222
223     my $offsets = Koha::Account::Offsets->search( { credit_id => $credit->id, debit_id => $debit_1->id } );
224     is( $offsets->count, 1, 'Only one offset is generated' );
225     my $THE_offset = $offsets->next;
226     is( $THE_offset->amount * 1, -10, 'Amount was calculated correctly (less than the available credit)' );
227     is( $THE_offset->type, 'Manual Credit', 'Passed type stored correctly' );
228
229     $debits = Koha::Account::Lines->search({ accountlines_id => $debit_2->id });
230     $remaining_credit = $credit->apply( { debits => [ $debits->as_list ] } );
231     is( $remaining_credit, 0, 'No remaining credit left' );
232     $credit->discard_changes;
233     is( $credit->amountoutstanding * 1, 0, 'No outstanding credit' );
234     $debit_2->discard_changes;
235     is( $debit_2->amountoutstanding * 1, 10, 'Outstanding amount decremented correctly' );
236
237     $offsets = Koha::Account::Offsets->search( { credit_id => $credit->id, debit_id => $debit_2->id } );
238     is( $offsets->count, 1, 'Only one offset is generated' );
239     $THE_offset = $offsets->next;
240     is( $THE_offset->amount * 1, -90, 'Amount was calculated correctly (less than the available credit)' );
241     is( $THE_offset->type, 'Credit Applied', 'Defaults to \'Credit Applied\' offset type' );
242
243     $debits = Koha::Account::Lines->search({ accountlines_id => $debit_1->id });
244     throws_ok
245         { $credit->apply({ debits => [ $debits->as_list ] }); }
246         'Koha::Exceptions::Account::NoAvailableCredit',
247         '->apply() can only be used with outstanding credits';
248
249     $debits = Koha::Account::Lines->search({ accountlines_id => $credit->id });
250     throws_ok
251         { $debit_1->apply({ debits => [ $debits->as_list ] }); }
252         'Koha::Exceptions::Account::IsNotCredit',
253         '->apply() can only be used with credits';
254
255     $debits = Koha::Account::Lines->search({ accountlines_id => $credit->id });
256     my $credit_3 = $account->add_credit({ amount => 1, interface => 'commandline' });
257     throws_ok
258         { $credit_3->apply({ debits => [ $debits->as_list ] }); }
259         'Koha::Exceptions::Account::IsNotDebit',
260         '->apply() can only be applied to credits';
261
262     my $credit_2 = $account->add_credit({ amount => 20, interface => 'commandline' });
263     my $debit_3  = Koha::Account::Line->new(
264         {   borrowernumber    => $patron->id,
265             debit_type_code       => "OVERDUE",
266             status            => "RETURNED",
267             amount            => 100,
268             amountoutstanding => 100,
269             interface         => 'commandline',
270         }
271     )->store;
272
273     $debits = Koha::Account::Lines->search({ accountlines_id => { -in => [ $debit_1->id, $debit_2->id, $debit_3->id, $credit->id ] } });
274     throws_ok {
275         $credit_2->apply( { debits => [ $debits->as_list ], offset_type => 'Manual Credit' } ); }
276         'Koha::Exceptions::Account::IsNotDebit',
277         '->apply() rolls back if any of the passed lines is not a debit';
278
279     is( $debit_1->discard_changes->amountoutstanding * 1,   0, 'No changes to already cancelled debit' );
280     is( $debit_2->discard_changes->amountoutstanding * 1,  10, 'Debit cancelled' );
281     is( $debit_3->discard_changes->amountoutstanding * 1, 100, 'Outstanding amount correctly calculated' );
282     is( $credit_2->discard_changes->amountoutstanding * -1, 20, 'No changes made' );
283
284     $debits = Koha::Account::Lines->search({ accountlines_id => { -in => [ $debit_1->id, $debit_2->id, $debit_3->id ] } });
285     $remaining_credit = $credit_2->apply( { debits => [ $debits->as_list ], offset_type => 'Manual Credit' } );
286
287     is( $debit_1->discard_changes->amountoutstanding * 1,  0, 'No changes to already cancelled debit' );
288     is( $debit_2->discard_changes->amountoutstanding * 1,  0, 'Debit cancelled' );
289     is( $debit_3->discard_changes->amountoutstanding * 1, 90, 'Outstanding amount correctly calculated' );
290     is( $credit_2->discard_changes->amountoutstanding * 1, 0, 'No remaining credit' );
291
292     my $library  = $builder->build_object( { class => 'Koha::Libraries' } );
293     my $biblio = $builder->build_sample_biblio();
294     my $item =
295         $builder->build_sample_item( { biblionumber => $biblio->biblionumber } );
296     my $now = dt_from_string();
297     my $seven_weeks = DateTime::Duration->new(weeks => 7);
298     my $five_weeks = DateTime::Duration->new(weeks => 5);
299     my $seven_weeks_ago = $now - $seven_weeks;
300     my $five_weeks_ago = $now - $five_weeks;
301
302     my $checkout = Koha::Checkout->new(
303         {
304             borrowernumber => $patron->id,
305             itemnumber     => $item->id,
306             date_due       => $five_weeks_ago,
307             branchcode     => $library->id,
308             issuedate      => $seven_weeks_ago
309         }
310     )->store();
311
312     my $accountline = Koha::Account::Line->new(
313         {
314             issue_id       => $checkout->id,
315             borrowernumber => $patron->id,
316             itemnumber     => $item->id,
317             branchcode     => $library->id,
318             date           => \'NOW()',
319             debit_type_code => 'OVERDUE',
320             status         => 'UNRETURNED',
321             interface      => 'cli',
322             amount => '1',
323             amountoutstanding => '1',
324         }
325     )->store();
326
327     # Enable renewing upon fine payment
328     t::lib::Mocks::mock_preference( 'RenewAccruingItemWhenPaid', 1 );
329     my $called = 0;
330     my $module = new Test::MockModule('C4::Circulation');
331     $module->mock('AddRenewal', sub { $called = 1; });
332     $module->mock('CanBookBeRenewed', sub { return 1; });
333     my $credit_forgive = $account->add_credit(
334         {
335             amount    => 1,
336             user_id   => $patron->id,
337             interface => 'cli',
338             type      => 'FORGIVEN'
339         }
340     );
341     my $debits_renew = Koha::Account::Lines->search({ accountlines_id => $accountline->id })->as_list;
342     $credit_forgive->apply( { debits => $debits_renew, offset_type => 'Forgiven' } );
343     is( $called, 0, 'C4::Circulation::AddRenew NOT called when RenewAccruingItemWhenPaid enabled but credit type is "FORGIVEN"' );
344
345     $accountline = Koha::Account::Line->new(
346         {
347             issue_id       => $checkout->id,
348             borrowernumber => $patron->id,
349             itemnumber     => $item->id,
350             branchcode     => $library->id,
351             date           => \'NOW()',
352             debit_type_code => 'OVERDUE',
353             status         => 'UNRETURNED',
354             interface      => 'cli',
355             amount => '1',
356             amountoutstanding => '1',
357         }
358     )->store();
359     my $credit_renew = $account->add_credit({ amount => 100, user_id => $patron->id, interface => 'commandline' });
360     $debits_renew = Koha::Account::Lines->search({ accountlines_id => $accountline->id })->as_list;
361     $credit_renew->apply( { debits => $debits_renew, offset_type => 'Manual Credit' } );
362     is( $called, 1, 'RenewAccruingItemWhenPaid causes C4::Circulation::AddRenew to be called when appropriate' );
363
364     t::lib::Mocks::mock_preference( 'MarkLostItemsAsReturned', 'onpayment');
365     my $loser  = $builder->build_object( { class => 'Koha::Patrons' } );
366     my $loser_account = $loser->account;
367
368     my $lost_item = $builder->build_sample_item();
369     my $lost_checkout = Koha::Checkout->new(
370         {
371             borrowernumber => $loser->id,
372             itemnumber     => $lost_item->id,
373             date_due       => $five_weeks_ago,
374             branchcode     => $library->id,
375             issuedate      => $seven_weeks_ago
376         }
377     )->store();
378
379     $lost_item->itemlost(1)->store;
380     my $processing_fee = Koha::Account::Line->new(
381         {
382             issue_id       => $lost_checkout->id,
383             borrowernumber => $loser->id,
384             itemnumber     => $lost_item->id,
385             branchcode     => $library->id,
386             date           => \'NOW()',
387             debit_type_code => 'PROCESSING',
388             status         => undef,
389             interface      => 'intranet',
390             amount => '15',
391             amountoutstanding => '15',
392         }
393     )->store();
394     my $lost_fee = Koha::Account::Line->new(
395         {
396             issue_id       => $lost_checkout->id,
397             borrowernumber => $loser->id,
398             itemnumber     => $lost_item->id,
399             branchcode     => $library->id,
400             date           => \'NOW()',
401             debit_type_code => 'LOST',
402             status         => undef,
403             interface      => 'intranet',
404             amount => '12.63',
405             amountoutstanding => '12.63',
406         }
407     )->store();
408     my $pay_lost = $loser_account->add_credit({ amount => 27.630000, user_id => $loser->id, interface => 'intranet' });
409     my $pay_lines = [ $processing_fee, $lost_fee ];
410     $pay_lost->apply( { debits => $pay_lines, offset_type => 'Credit applied' } );
411
412     is( $loser->checkouts->next, undef, "Item has been returned");
413
414     $schema->storage->txn_rollback;
415 };
416
417 subtest 'Keep account info when related patron, staff, item or cash_register is deleted' => sub {
418
419     plan tests => 4;
420
421     $schema->storage->txn_begin;
422
423     my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
424     my $staff = $builder->build_object( { class => 'Koha::Patrons' } );
425     my $item = $builder->build_object({ class => 'Koha::Items' });
426     my $issue = $builder->build_object(
427         {
428             class => 'Koha::Checkouts',
429             value => { itemnumber => $item->itemnumber }
430         }
431     );
432     my $register = $builder->build_object({ class => 'Koha::Cash::Registers' });
433
434     my $line = Koha::Account::Line->new(
435     {
436         borrowernumber => $patron->borrowernumber,
437         manager_id     => $staff->borrowernumber,
438         itemnumber     => $item->itemnumber,
439         debit_type_code    => "OVERDUE",
440         status         => "RETURNED",
441         amount         => 10,
442         interface      => 'commandline',
443         register_id    => $register->id
444     })->store;
445
446     $issue->delete;
447     $item->delete;
448     $line = $line->get_from_storage;
449     is( $line->itemnumber, undef, "The account line should not be deleted when the related item is delete");
450
451     $staff->delete;
452     $line = $line->get_from_storage;
453     is( $line->manager_id, undef, "The account line should not be deleted when the related staff is delete");
454
455     $patron->delete;
456     $line = $line->get_from_storage;
457     is( $line->borrowernumber, undef, "The account line should not be deleted when the related patron is delete");
458
459     $register->delete;
460     $line = $line->get_from_storage;
461     is( $line->register_id, undef, "The account line should not be deleted when the related cash register is delete");
462
463     $schema->storage->txn_rollback;
464 };
465
466 subtest 'Renewal related tests' => sub {
467
468     plan tests => 8;
469
470     $schema->storage->txn_begin;
471
472     my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
473     my $staff = $builder->build_object( { class => 'Koha::Patrons' } );
474     my $item = $builder->build_object({ class => 'Koha::Items' });
475     my $issue = $builder->build_object(
476         {
477             class => 'Koha::Checkouts',
478             value => {
479                 itemnumber      => $item->itemnumber,
480                 onsite_checkout => 0,
481                 renewals        => 99,
482                 auto_renew      => 0
483             }
484         }
485     );
486     my $line = Koha::Account::Line->new(
487     {
488         borrowernumber    => $patron->borrowernumber,
489         manager_id        => $staff->borrowernumber,
490         itemnumber        => $item->itemnumber,
491         debit_type_code   => "OVERDUE",
492         status            => "UNRETURNED",
493         amountoutstanding => 0,
494         interface         => 'commandline',
495     })->store;
496
497     is( $line->is_renewable, 1, "Item is returned as renewable when it meets the conditions" );
498     $line->amountoutstanding(5);
499     is( $line->is_renewable, 0, "Item is returned as unrenewable when it has outstanding fine" );
500     $line->amountoutstanding(0);
501     $line->debit_type_code("VOID");
502     is( $line->is_renewable, 0, "Item is returned as unrenewable when it has the wrong account type" );
503     $line->debit_type_code("OVERDUE");
504     $line->status("RETURNED");
505     is( $line->is_renewable, 0, "Item is returned as unrenewable when it has the wrong account status" );
506
507
508     t::lib::Mocks::mock_preference( 'RenewAccruingItemWhenPaid', 0 );
509     is ($line->renew_item({ interface => 'intranet' }), undef, 'Attempt to renew fails when syspref is not set');
510     t::lib::Mocks::mock_preference( 'RenewAccruingItemWhenPaid', 1 );
511     t::lib::Mocks::mock_preference( 'RenewAccruingItemInOpac', 0 );
512     is ($line->renew_item({ interface => 'opac' }), undef, 'Attempt to renew fails when syspref is not set - OPAC');
513     t::lib::Mocks::mock_preference( 'RenewAccruingItemInOpac', 1 );
514     is_deeply(
515         $line->renew_item({ interface => 'intranet' }),
516         {
517             itemnumber => $item->itemnumber,
518             error      => 'too_many',
519             success    => 0
520         },
521         'Attempt to renew fails when CanBookBeRenewed returns false'
522     );
523     $issue->delete;
524     $issue = $builder->build_object(
525         {
526             class => 'Koha::Checkouts',
527             value => {
528                 itemnumber      => $item->itemnumber,
529                 onsite_checkout => 0,
530                 renewals        => 0,
531                 auto_renew      => 0
532             }
533         }
534     );
535     my $called = 0;
536     my $module = new Test::MockModule('C4::Circulation');
537     $module->mock('AddRenewal', sub { $called = 1; });
538     $module->mock('CanBookBeRenewed', sub { return 1; });
539     $line->renew_item;
540     is( $called, 1, 'Attempt to renew succeeds when conditions are met' );
541
542     $schema->storage->txn_rollback;
543 };
544
545 subtest 'adjust() tests' => sub {
546
547     plan tests => 29;
548
549     $schema->storage->txn_begin;
550
551     # count logs before any actions
552     my $action_logs = $schema->resultset('ActionLog')->search()->count;
553
554     # Disable logs
555     t::lib::Mocks::mock_preference( 'FinesLog', 0 );
556
557     my $patron  = $builder->build_object( { class => 'Koha::Patrons' } );
558     my $account = $patron->account;
559
560     my $debit_1 = Koha::Account::Line->new(
561         {   borrowernumber    => $patron->id,
562             debit_type_code       => "OVERDUE",
563             status            => "RETURNED",
564             amount            => 10,
565             amountoutstanding => 10,
566             interface         => 'commandline',
567         }
568     )->store;
569
570     my $debit_2 = Koha::Account::Line->new(
571         {   borrowernumber    => $patron->id,
572             debit_type_code       => "OVERDUE",
573             status            => "UNRETURNED",
574             amount            => 100,
575             amountoutstanding => 100,
576             interface         => 'commandline'
577         }
578     )->store;
579
580     my $credit = $account->add_credit( { amount => 40, user_id => $patron->id, interface => 'commandline' } );
581
582     throws_ok { $debit_1->adjust( { amount => 50, type => 'bad', interface => 'commandline' } ) }
583     qr/Update type not recognised/, 'Exception thrown for unrecognised type';
584
585     throws_ok { $debit_1->adjust( { amount => 50, type => 'overdue_update', interface => 'commandline' } ) }
586     qr/Update type not allowed on this debit_type/,
587       'Exception thrown for type conflict';
588
589     # Increment an unpaid fine
590     $debit_2->adjust( { amount => 150, type => 'overdue_update', interface => 'commandline' } )->discard_changes;
591
592     is( $debit_2->amount * 1, 150, 'Fine amount was updated in full' );
593     is( $debit_2->amountoutstanding * 1, 150, 'Fine amountoutstanding was update in full' );
594     isnt( $debit_2->date, undef, 'Date has been set' );
595
596     my $offsets = Koha::Account::Offsets->search( { debit_id => $debit_2->id } );
597     is( $offsets->count, 1, 'An offset is generated for the increment' );
598     my $THIS_offset = $offsets->next;
599     is( $THIS_offset->amount * 1, 50, 'Amount was calculated correctly (increment by 50)' );
600     is( $THIS_offset->type, 'OVERDUE_INCREASE', 'Adjust type stored correctly' );
601
602     is( $schema->resultset('ActionLog')->count(), $action_logs + 0, 'No log was added' );
603
604     # Update fine to partially paid
605     my $debits = Koha::Account::Lines->search({ accountlines_id => $debit_2->id });
606     $credit->apply( { debits => [ $debits->as_list ], offset_type => 'Manual Credit' } );
607
608     $debit_2->discard_changes;
609     is( $debit_2->amount * 1, 150, 'Fine amount unaffected by partial payment' );
610     is( $debit_2->amountoutstanding * 1, 110, 'Fine amountoutstanding updated by partial payment' );
611
612     # Enable logs
613     t::lib::Mocks::mock_preference( 'FinesLog', 1 );
614
615     # Increment the partially paid fine
616     $debit_2->adjust( { amount => 160, type => 'overdue_update', interface => 'commandline' } )->discard_changes;
617
618     is( $debit_2->amount * 1, 160, 'Fine amount was updated in full' );
619     is( $debit_2->amountoutstanding * 1, 120, 'Fine amountoutstanding was updated by difference' );
620
621     $offsets = Koha::Account::Offsets->search( { debit_id => $debit_2->id } );
622     is( $offsets->count, 3, 'An offset is generated for the increment' );
623     $THIS_offset = $offsets->last;
624     is( $THIS_offset->amount * 1, 10, 'Amount was calculated correctly (increment by 10)' );
625     is( $THIS_offset->type, 'OVERDUE_INCREASE', 'Adjust type stored correctly' );
626
627     is( $schema->resultset('ActionLog')->count(), $action_logs + 1, 'Log was added' );
628
629     # Decrement the partially paid fine, less than what was paid
630     $debit_2->adjust( { amount => 50, type => 'overdue_update', interface => 'commandline' } )->discard_changes;
631
632     is( $debit_2->amount * 1, 50, 'Fine amount was updated in full' );
633     is( $debit_2->amountoutstanding * 1, 10, 'Fine amountoutstanding was updated by difference' );
634
635     $offsets = Koha::Account::Offsets->search( { debit_id => $debit_2->id } );
636     is( $offsets->count, 4, 'An offset is generated for the decrement' );
637     $THIS_offset = $offsets->last;
638     is( $THIS_offset->amount * 1, -110, 'Amount was calculated correctly (decrement by 110)' );
639     is( $THIS_offset->type, 'OVERDUE_DECREASE', 'Adjust type stored correctly' );
640
641     # Decrement the partially paid fine, more than what was paid
642     $debit_2->adjust( { amount => 30, type => 'overdue_update', interface => 'commandline' } )->discard_changes;
643     is( $debit_2->amount * 1, 30, 'Fine amount was updated in full' );
644     is( $debit_2->amountoutstanding * 1, 0, 'Fine amountoutstanding was zeroed (payment was 40)' );
645
646     $offsets = Koha::Account::Offsets->search( { debit_id => $debit_2->id } );
647     is( $offsets->count, 5, 'An offset is generated for the decrement' );
648     $THIS_offset = $offsets->last;
649     is( $THIS_offset->amount * 1, -20, 'Amount was calculated correctly (decrement by 20)' );
650     is( $THIS_offset->type, 'OVERDUE_DECREASE', 'Adjust type stored correctly' );
651
652     my $overpayment_refund = $account->lines->last;
653     is( $overpayment_refund->amount * 1, -10, 'A new credit has been added' );
654     is( $overpayment_refund->description, 'Overpayment refund', 'Credit generated with the expected description' );
655
656     $schema->storage->txn_rollback;
657 };
658
659 subtest 'checkout() tests' => sub {
660     plan tests => 6;
661
662     $schema->storage->txn_begin;
663
664     my $library = $builder->build_object( { class => 'Koha::Libraries' } );
665     my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
666     my $item = $builder->build_sample_item;
667     my $account = $patron->account;
668
669     t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode });
670     my $checkout = AddIssue( $patron->unblessed, $item->barcode );
671
672     my $line = $account->add_debit({
673         amount    => 10,
674         interface => 'commandline',
675         item_id   => $item->itemnumber,
676         issue_id  => $checkout->issue_id,
677         type      => 'OVERDUE',
678         status    => 'UNRETURNED'
679     });
680
681     my $line_checkout = $line->checkout;
682     is( ref($line_checkout), 'Koha::Checkout', 'Result type is correct' );
683     is( $line_checkout->issue_id, $checkout->issue_id, 'Koha::Account::Line->checkout should return the correct checkout');
684
685     # Prevent re-calculation of fines at check-in for the test; Since bug 8338 the recalculation would result in a '0'
686     # fine which would subsequently be removed by _FixOverduesOnReturn
687     t::lib::Mocks::mock_preference( 'finesMode', 'off' );
688
689     my ( $returned, undef, $old_checkout) = C4::Circulation::AddReturn( $item->barcode, $library->branchcode );
690     is( $returned, 1, 'The item should have been returned' );
691
692     $line = $line->get_from_storage;
693     my $old_line_checkout = $line->checkout;
694     is( ref($old_line_checkout), 'Koha::Old::Checkout', 'Result type is correct' );
695     is( $old_line_checkout->issue_id, $old_checkout->issue_id, 'Koha::Account::Line->checkout should return the correct old_checkout' );
696
697     $line->issue_id(undef)->store;
698     is( $line->checkout, undef, 'Koha::Account::Line->checkout should return undef if no checkout linked' );
699
700     $schema->storage->txn_rollback;
701 };
702
703 subtest 'credits() and debits() tests' => sub {
704     plan tests => 10;
705
706     $schema->storage->txn_begin;
707
708     my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
709     my $account = $patron->account;
710
711     my $debit1 = $account->add_debit({
712         amount    => 8,
713         interface => 'commandline',
714         type      => 'ACCOUNT',
715     });
716     my $debit2 = $account->add_debit({
717         amount    => 12,
718         interface => 'commandline',
719         type      => 'ACCOUNT',
720     });
721     my $credit1 = $account->add_credit({
722         amount    => 5,
723         interface => 'commandline',
724         type      => 'CREDIT',
725     });
726     my $credit2 = $account->add_credit({
727         amount    => 10,
728         interface => 'commandline',
729         type      => 'CREDIT',
730     });
731
732     $credit1->apply({ debits => [ $debit1 ] });
733     $credit2->apply({ debits => [ $debit1, $debit2 ] });
734
735     my $credits = $debit1->credits;
736     is($credits->count, 2, '2 Credits applied to debit 1');
737     my $credit = $credits->next;
738     is($credit->amount + 0, -5, 'Correct first credit');
739     $credit = $credits->next;
740     is($credit->amount + 0, -10, 'Correct second credit');
741
742     $credits = $debit2->credits;
743     is($credits->count, 1, '1 Credits applied to debit 2');
744     $credit = $credits->next;
745     is($credit->amount + 0, -10, 'Correct first credit');
746
747     my $debits = $credit1->debits;
748     is($debits->count, 1, 'Credit 1 applied to 1 debit');
749     my $debit = $debits->next;
750     is($debit->amount + 0, 8, 'Correct first debit');
751
752     $debits = $credit2->debits;
753     is($debits->count, 2, 'Credit 2 applied to 2 debits');
754     $debit = $debits->next;
755     is($debit->amount + 0, 8, 'Correct first debit');
756     $debit = $debits->next;
757     is($debit->amount + 0, 12, 'Correct second debit');
758
759     $schema->storage->txn_rollback;
760 };
761
762 subtest "void() tests" => sub {
763
764     plan tests => 16;
765
766     $schema->storage->txn_begin;
767
768     # Create a borrower
769     my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
770     my $branchcode   = $builder->build({ source => 'Branch' })->{ branchcode };
771
772     my $borrower = Koha::Patron->new( {
773         cardnumber => 'dariahall',
774         surname => 'Hall',
775         firstname => 'Daria',
776     } );
777     $borrower->categorycode( $categorycode );
778     $borrower->branchcode( $branchcode );
779     $borrower->store;
780
781     my $account = Koha::Account->new({ patron_id => $borrower->id });
782
783     my $line1 = Koha::Account::Line->new(
784         {
785             borrowernumber    => $borrower->borrowernumber,
786             amount            => 10,
787             amountoutstanding => 10,
788             interface         => 'commandline',
789             debit_type_code   => 'OVERDUE'
790         }
791     )->store();
792     my $line2 = Koha::Account::Line->new(
793         {
794             borrowernumber    => $borrower->borrowernumber,
795             amount            => 20,
796             amountoutstanding => 20,
797             interface         => 'commandline',
798             debit_type_code   => 'OVERDUE'
799         }
800     )->store();
801
802     is( $account->balance(), 30, "Account balance is 30" );
803     is( $line1->amountoutstanding, 10, 'First fee has amount outstanding of 10' );
804     is( $line2->amountoutstanding, 20, 'Second fee has amount outstanding of 20' );
805
806     my $id = $account->pay(
807         {
808             lines  => [$line1, $line2],
809             amount => 30,
810         }
811     )->{payment_id};
812
813     my $account_payment = Koha::Account::Lines->find( $id );
814
815     is( $account->balance(), 0, "Account balance is 0" );
816
817     $line1->_result->discard_changes();
818     $line2->_result->discard_changes();
819     is( $line1->amountoutstanding+0, 0, 'First fee has amount outstanding of 0' );
820     is( $line2->amountoutstanding+0, 0, 'Second fee has amount outstanding of 0' );
821
822     my $ret = $account_payment->void();
823
824     is( ref($ret), 'Koha::Account::Line', 'Void returns the account line' );
825     is( $account->balance(), 30, "Account balance is again 30" );
826
827     $account_payment->_result->discard_changes();
828     $line1->_result->discard_changes();
829     $line2->_result->discard_changes();
830
831     is( $account_payment->credit_type_code, 'PAYMENT', 'Voided payment credit_type_code is still PAYMENT' );
832     is( $account_payment->status, 'VOID', 'Voided payment status is VOID' );
833     is( $account_payment->amount+0, 0, 'Voided payment amount is 0' );
834     is( $account_payment->amountoutstanding+0, 0, 'Voided payment amount outstanding is 0' );
835
836     is( $line1->amountoutstanding+0, 10, 'First fee again has amount outstanding of 10' );
837     is( $line2->amountoutstanding+0, 20, 'Second fee again has amount outstanding of 20' );
838
839     # Accountlines that are not credits should be un-voidable
840     my $line1_pre = $line1->unblessed();
841     $ret = $line1->void();
842     $line1->_result->discard_changes();
843     my $line1_post = $line1->unblessed();
844     is( $ret, undef, 'Attempted void on non-credit returns undef' );
845     is_deeply( $line1_pre, $line1_post, 'Non-credit account line cannot be voided' );
846
847     $schema->storage->txn_rollback;
848 };
849
850 subtest "payout() tests" => sub {
851
852     plan tests => 18;
853
854     $schema->storage->txn_begin;
855
856     # Create a borrower
857     my $categorycode =
858       $builder->build( { source => 'Category' } )->{categorycode};
859     my $branchcode = $builder->build( { source => 'Branch' } )->{branchcode};
860
861     my $borrower = Koha::Patron->new(
862         {
863             cardnumber => 'dariahall',
864             surname    => 'Hall',
865             firstname  => 'Daria',
866         }
867     );
868     $borrower->categorycode($categorycode);
869     $borrower->branchcode($branchcode);
870     $borrower->store;
871
872     my $staff = Koha::Patron->new(
873         {
874             cardnumber => 'bobby',
875             surname    => 'Bloggs',
876             firstname  => 'Bobby',
877         }
878     );
879     $staff->categorycode($categorycode);
880     $staff->branchcode($branchcode);
881     $staff->store;
882
883     my $account = Koha::Account->new( { patron_id => $borrower->id } );
884
885     my $debit1 = Koha::Account::Line->new(
886         {
887             borrowernumber    => $borrower->borrowernumber,
888             amount            => 10,
889             amountoutstanding => 10,
890             interface         => 'commandline',
891             debit_type_code   => 'OVERDUE'
892         }
893     )->store();
894     my $credit1 = Koha::Account::Line->new(
895         {
896             borrowernumber    => $borrower->borrowernumber,
897             amount            => -20,
898             amountoutstanding => -20,
899             interface         => 'commandline',
900             credit_type_code  => 'CREDIT'
901         }
902     )->store();
903
904     is( $account->balance(), -10, "Account balance is -10" );
905     is( $debit1->amountoutstanding + 0,
906         10, 'Overdue fee has an amount outstanding of 10' );
907     is( $credit1->amountoutstanding + 0,
908         -20, 'Credit has an amount outstanding of -20' );
909
910     my $pay_params = {
911         interface   => 'intranet',
912         staff_id    => $staff->borrowernumber,
913         branch      => $branchcode,
914         payout_type => 'CASH',
915         amount      => 20
916     };
917
918     throws_ok { $debit1->payout($pay_params); }
919     'Koha::Exceptions::Account::IsNotCredit',
920       '->payout() can only be used with credits';
921
922     my @required =
923       ( 'interface', 'staff_id', 'branch', 'payout_type', 'amount' );
924     for my $required (@required) {
925         my $params = {%$pay_params};
926         delete( $params->{$required} );
927         throws_ok {
928             $credit1->payout($params);
929         }
930         'Koha::Exceptions::MissingParameter',
931           "->payout() requires the `$required` parameter is passed";
932     }
933
934     throws_ok {
935         $credit1->payout(
936             {
937                 interface   => 'intranet',
938                 staff_id    => $staff->borrowernumber,
939                 branch      => $branchcode,
940                 payout_type => 'CASH',
941                 amount      => 25
942             }
943         );
944     }
945     'Koha::Exceptions::ParameterTooHigh',
946       '->payout() cannot pay out more than the amountoutstanding';
947
948     t::lib::Mocks::mock_preference( 'UseCashRegisters', 1 );
949     throws_ok {
950         $credit1->payout(
951             {
952                 interface   => 'intranet',
953                 staff_id    => $staff->borrowernumber,
954                 branch      => $branchcode,
955                 payout_type => 'CASH',
956                 amount      => 10
957             }
958         );
959     }
960     'Koha::Exceptions::Account::RegisterRequired',
961       '->payout() requires a cash_register if payout_type is `CASH`';
962
963     t::lib::Mocks::mock_preference( 'UseCashRegisters', 0 );
964     my $payout = $credit1->payout(
965         {
966             interface   => 'intranet',
967             staff_id    => $staff->borrowernumber,
968             branch      => $branchcode,
969             payout_type => 'CASH',
970             amount      => 10
971         }
972     );
973
974     is( ref($payout), 'Koha::Account::Line',
975         '->payout() returns a Koha::Account::Line' );
976     is( $payout->amount() + 0,            10, "Payout amount is 10" );
977     is( $payout->amountoutstanding() + 0, 0,  "Payout amountoutstanding is 0" );
978     is( $account->balance() + 0,          0,  "Account balance is 0" );
979     is( $debit1->amountoutstanding + 0,
980         10, 'Overdue fee still has an amount outstanding of 10' );
981     is( $credit1->amountoutstanding + 0,
982         -10, 'Credit has an new amount outstanding of -10' );
983     is( $credit1->status(), 'PAID', "Credit has a new status of PAID" );
984
985     $schema->storage->txn_rollback;
986 };
987
988 subtest "reduce() tests" => sub {
989
990     plan tests => 27;
991
992     $schema->storage->txn_begin;
993
994     # Create a borrower
995     my $categorycode =
996       $builder->build( { source => 'Category' } )->{categorycode};
997     my $branchcode = $builder->build( { source => 'Branch' } )->{branchcode};
998
999     my $borrower = Koha::Patron->new(
1000         {
1001             cardnumber => 'dariahall',
1002             surname    => 'Hall',
1003             firstname  => 'Daria',
1004         }
1005     );
1006     $borrower->categorycode($categorycode);
1007     $borrower->branchcode($branchcode);
1008     $borrower->store;
1009
1010     my $staff = Koha::Patron->new(
1011         {
1012             cardnumber => 'bobby',
1013             surname    => 'Bloggs',
1014             firstname  => 'Bobby',
1015         }
1016     );
1017     $staff->categorycode($categorycode);
1018     $staff->branchcode($branchcode);
1019     $staff->store;
1020
1021     my $account = Koha::Account->new( { patron_id => $borrower->id } );
1022
1023     my $debit1 = Koha::Account::Line->new(
1024         {
1025             borrowernumber    => $borrower->borrowernumber,
1026             amount            => 20,
1027             amountoutstanding => 20,
1028             interface         => 'commandline',
1029             debit_type_code   => 'LOST'
1030         }
1031     )->store();
1032     my $credit1 = Koha::Account::Line->new(
1033         {
1034             borrowernumber    => $borrower->borrowernumber,
1035             amount            => -20,
1036             amountoutstanding => -20,
1037             interface         => 'commandline',
1038             credit_type_code  => 'CREDIT'
1039         }
1040     )->store();
1041
1042     is( $account->balance(), 0, "Account balance is 0" );
1043     is( $debit1->amountoutstanding,
1044         20, 'Overdue fee has an amount outstanding of 20' );
1045     is( $credit1->amountoutstanding,
1046         -20, 'Credit has an amount outstanding of -20' );
1047
1048     my $reduce_params = {
1049         interface      => 'commandline',
1050         reduction_type => 'REFUND',
1051         amount         => 5,
1052         staff_id       => $staff->borrowernumber,
1053         branch         => $branchcode
1054     };
1055
1056     throws_ok { $credit1->reduce($reduce_params); }
1057     'Koha::Exceptions::Account::IsNotDebit',
1058       '->reduce() can only be used with debits';
1059
1060     my @required = ( 'interface', 'reduction_type', 'amount' );
1061     for my $required (@required) {
1062         my $params = {%$reduce_params};
1063         delete( $params->{$required} );
1064         throws_ok {
1065             $debit1->reduce($params);
1066         }
1067         'Koha::Exceptions::MissingParameter',
1068           "->reduce() requires the `$required` parameter is passed";
1069     }
1070
1071     $reduce_params->{interface} = 'intranet';
1072     my @dependant_required = ( 'staff_id', 'branch' );
1073     for my $d (@dependant_required) {
1074         my $params = {%$reduce_params};
1075         delete( $params->{$d} );
1076         throws_ok {
1077             $debit1->reduce($params);
1078         }
1079         'Koha::Exceptions::MissingParameter',
1080 "->reduce() requires the `$d` parameter is passed when interface is intranet";
1081     }
1082
1083     throws_ok {
1084         $debit1->reduce(
1085             {
1086                 interface      => 'intranet',
1087                 staff_id       => $staff->borrowernumber,
1088                 branch         => $branchcode,
1089                 reduction_type => 'REFUND',
1090                 amount         => 25
1091             }
1092         );
1093     }
1094     'Koha::Exceptions::ParameterTooHigh',
1095       '->reduce() cannot reduce more than original amount';
1096
1097     # Partial Reduction
1098     # (Refund 5 on debt of 20)
1099     my $reduction = $debit1->reduce($reduce_params);
1100
1101     is( ref($reduction), 'Koha::Account::Line',
1102         '->reduce() returns a Koha::Account::Line' );
1103     is( $reduction->amount() * 1, -5, "Reduce amount is -5" );
1104     is( $reduction->amountoutstanding() * 1,
1105         0, "Reduce amountoutstanding is 0" );
1106     is( $debit1->amountoutstanding() * 1,
1107         15, "Debit amountoutstanding reduced by 5 to 15" );
1108     is( $account->balance() * 1, -5,        "Account balance is -5" );
1109     is( $reduction->status(),    'APPLIED', "Reduction status is 'APPLIED'" );
1110
1111     my $offsets = Koha::Account::Offsets->search(
1112         { credit_id => $reduction->id, debit_id => $debit1->id } );
1113     is( $offsets->count, 1, 'Only one offset is generated' );
1114     my $THE_offset = $offsets->next;
1115     is( $THE_offset->amount * 1,
1116         -5, 'Correct amount was applied against debit' );
1117     is( $THE_offset->type, 'REFUND', "Offset type set to 'REFUND'" );
1118
1119     # Zero offset created when zero outstanding
1120     # (Refund another 5 on paid debt of 20)
1121     $credit1->apply( { debits => [$debit1] } );
1122     is( $debit1->amountoutstanding + 0,
1123         0, 'Debit1 amountoutstanding reduced to 0' );
1124     $reduction = $debit1->reduce($reduce_params);
1125     is( $reduction->amount() * 1, -5, "Reduce amount is -5" );
1126     is( $reduction->amountoutstanding() * 1,
1127         -5, "Reduce amountoutstanding is -5" );
1128
1129     $offsets = Koha::Account::Offsets->search(
1130         { credit_id => $reduction->id, debit_id => $debit1->id } );
1131     is( $offsets->count, 1, 'Only one new offset is generated' );
1132     $THE_offset = $offsets->next;
1133     is( $THE_offset->amount * 1,
1134         0, 'Zero offset created for already paid off debit' );
1135     is( $THE_offset->type, 'REFUND', "Offset type set to 'REFUND'" );
1136
1137     # Compound reduction should not allow more than original amount
1138     # (Reduction of 5 + 5 + 20 > 20)
1139     $reduce_params->{amount} = 20;
1140     throws_ok {
1141         $debit1->reduce($reduce_params);
1142     }
1143     'Koha::Exceptions::ParameterTooHigh',
1144 '->reduce cannot reduce more than the original amount (combined reductions test)';
1145
1146     # Throw exception if attempting to reduce a payout
1147     my $payout = $reduction->payout(
1148         {
1149             interface   => 'intranet',
1150             staff_id    => $staff->borrowernumber,
1151             branch      => $branchcode,
1152             payout_type => 'CASH',
1153             amount      => 5
1154         }
1155     );
1156     throws_ok {
1157         $payout->reduce($reduce_params);
1158     }
1159     'Koha::Exceptions::Account::IsNotDebit',
1160       '->reduce() cannot be used on a payout debit';
1161
1162     $schema->storage->txn_rollback;
1163 };
1164
1165 1;