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