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