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