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