3 # Copyright 2018 Koha Development team
5 # This file is part of Koha
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.
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.
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>
22 use Test::More tests => 7;
25 use C4::Circulation qw/AddIssue AddReturn/;
27 use Koha::Account::Lines;
28 use Koha::Account::Offsets;
32 use t::lib::TestBuilder;
34 my $schema = Koha::Database->new->schema;
35 my $builder = t::lib::TestBuilder->new;
37 subtest 'item() tests' => sub {
41 $schema->storage->txn_begin;
43 my $library = $builder->build( { source => 'Branch' } );
44 my $biblioitem = $builder->build( { source => 'Biblioitem' } );
45 my $patron = $builder->build( { source => 'Borrower' } );
46 my $item = Koha::Item->new(
48 biblionumber => $biblioitem->{biblionumber},
49 biblioitemnumber => $biblioitem->{biblioitemnumber},
50 homebranch => $library->{branchcode},
51 holdingbranch => $library->{branchcode},
52 barcode => 'some_barcode_12',
56 my $line = Koha::Account::Line->new(
58 borrowernumber => $patron->{borrowernumber},
59 itemnumber => $item->itemnumber,
60 accounttype => "OVERDUE",
63 interface => 'commandline',
66 my $account_line_item = $line->item;
67 is( ref( $account_line_item ), 'Koha::Item', 'Koha::Account::Line->item should return a Koha::Item' );
68 is( $line->itemnumber, $account_line_item->itemnumber, 'Koha::Account::Line->item should return the correct item' );
70 $line->itemnumber(undef)->store;
71 is( $line->item, undef, 'Koha::Account::Line->item should return undef if no item linked' );
73 $schema->storage->txn_rollback;
76 subtest 'total_outstanding() tests' => sub {
80 $schema->storage->txn_begin;
82 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
84 my $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
85 is( $lines->total_outstanding, 0, 'total_outstanding returns 0 if no lines (undef case)' );
87 my $debit_1 = Koha::Account::Line->new(
88 { borrowernumber => $patron->id,
89 accounttype => "OVERDUE",
92 amountoutstanding => 10,
93 interface => 'commandline',
97 my $debit_2 = Koha::Account::Line->new(
98 { borrowernumber => $patron->id,
99 accounttype => "OVERDUE",
100 status => "RETURNED",
102 amountoutstanding => 10,
103 interface => 'commandline',
107 $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
108 is( $lines->total_outstanding, 20, 'total_outstanding sums correctly' );
110 my $credit_1 = Koha::Account::Line->new(
111 { borrowernumber => $patron->id,
112 accounttype => "OVERDUE",
113 status => "RETURNED",
115 amountoutstanding => -10,
116 interface => 'commandline',
120 $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
121 is( $lines->total_outstanding, 10, 'total_outstanding sums correctly' );
123 my $credit_2 = Koha::Account::Line->new(
124 { borrowernumber => $patron->id,
125 accounttype => "OVERDUE",
126 status => "RETURNED",
128 amountoutstanding => -10,
129 interface => 'commandline',
133 $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
134 is( $lines->total_outstanding, 0, 'total_outstanding sums correctly' );
136 my $credit_3 = Koha::Account::Line->new(
137 { borrowernumber => $patron->id,
138 accounttype => "OVERDUE",
139 status => "RETURNED",
141 amountoutstanding => -100,
142 interface => 'commandline',
146 $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
147 is( $lines->total_outstanding, -100, 'total_outstanding sums correctly' );
149 $schema->storage->txn_rollback;
152 subtest 'is_credit() and is_debit() tests' => sub {
156 $schema->storage->txn_begin;
158 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
159 my $account = $patron->account;
161 my $credit = $account->add_credit({ amount => 100, user_id => $patron->id, interface => 'commandline' });
163 ok( $credit->is_credit, 'is_credit detects credits' );
164 ok( !$credit->is_debit, 'is_debit detects credits' );
166 my $debit = Koha::Account::Line->new(
168 borrowernumber => $patron->id,
169 accounttype => "OVERDUE",
170 status => "RETURNED",
172 interface => 'commandline',
175 ok( !$debit->is_credit, 'is_credit detects debits' );
176 ok( $debit->is_debit, 'is_debit detects debits');
178 $schema->storage->txn_rollback;
181 subtest 'apply() tests' => sub {
185 $schema->storage->txn_begin;
187 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
188 my $account = $patron->account;
190 my $credit = $account->add_credit( { amount => 100, user_id => $patron->id, interface => 'commandline' } );
192 my $debit_1 = Koha::Account::Line->new(
193 { borrowernumber => $patron->id,
194 accounttype => "OVERDUE",
195 status => "RETURNED",
197 amountoutstanding => 10,
198 interface => 'commandline',
202 my $debit_2 = Koha::Account::Line->new(
203 { borrowernumber => $patron->id,
204 accounttype => "OVERDUE",
205 status => "RETURNED",
207 amountoutstanding => 100,
208 interface => 'commandline',
212 $credit->discard_changes;
213 $debit_1->discard_changes;
215 my $debits = Koha::Account::Lines->search({ accountlines_id => $debit_1->id });
216 my $remaining_credit = $credit->apply( { debits => $debits, offset_type => 'Manual Credit' } );
217 is( $remaining_credit * 1, 90, 'Remaining credit is correctly calculated' );
218 $credit->discard_changes;
219 is( $credit->amountoutstanding * -1, $remaining_credit, 'Remaining credit correctly stored' );
222 $debit_1->discard_changes;
223 is( $debit_1->amountoutstanding * 1, 0, 'Debit has been cancelled' );
225 my $offsets = Koha::Account::Offsets->search( { credit_id => $credit->id, debit_id => $debit_1->id } );
226 is( $offsets->count, 1, 'Only one offset is generated' );
227 my $THE_offset = $offsets->next;
228 is( $THE_offset->amount * 1, -10, 'Amount was calculated correctly (less than the available credit)' );
229 is( $THE_offset->type, 'Manual Credit', 'Passed type stored correctly' );
231 $debits = Koha::Account::Lines->search({ accountlines_id => $debit_2->id });
232 $remaining_credit = $credit->apply( { debits => $debits } );
233 is( $remaining_credit, 0, 'No remaining credit left' );
234 $credit->discard_changes;
235 is( $credit->amountoutstanding * 1, 0, 'No outstanding credit' );
236 $debit_2->discard_changes;
237 is( $debit_2->amountoutstanding * 1, 10, 'Outstanding amount decremented correctly' );
239 $offsets = Koha::Account::Offsets->search( { credit_id => $credit->id, debit_id => $debit_2->id } );
240 is( $offsets->count, 1, 'Only one offset is generated' );
241 $THE_offset = $offsets->next;
242 is( $THE_offset->amount * 1, -90, 'Amount was calculated correctly (less than the available credit)' );
243 is( $THE_offset->type, 'Credit Applied', 'Defaults to \'Credit Applied\' offset type' );
245 $debits = Koha::Account::Lines->search({ accountlines_id => $debit_1->id });
247 { $credit->apply({ debits => $debits }); }
248 'Koha::Exceptions::Account::NoAvailableCredit',
249 '->apply() can only be used with outstanding credits';
251 $debits = Koha::Account::Lines->search({ accountlines_id => $credit->id });
253 { $debit_1->apply({ debits => $debits }); }
254 'Koha::Exceptions::Account::IsNotCredit',
255 '->apply() can only be used with credits';
257 $debits = Koha::Account::Lines->search({ accountlines_id => $credit->id });
258 my $credit_3 = $account->add_credit({ amount => 1, interface => 'commandline' });
260 { $credit_3->apply({ debits => $debits }); }
261 'Koha::Exceptions::Account::IsNotDebit',
262 '->apply() can only be applied to credits';
264 my $credit_2 = $account->add_credit({ amount => 20, interface => 'commandline' });
265 my $debit_3 = Koha::Account::Line->new(
266 { borrowernumber => $patron->id,
267 accounttype => "OVERDUE",
268 status => "RETURNED",
270 amountoutstanding => 100,
271 interface => 'commandline',
275 $debits = Koha::Account::Lines->search({ accountlines_id => { -in => [ $debit_1->id, $debit_2->id, $debit_3->id, $credit->id ] } });
277 $credit_2->apply( { debits => $debits, offset_type => 'Manual Credit' } ); }
278 'Koha::Exceptions::Account::IsNotDebit',
279 '->apply() rolls back if any of the passed lines is not a debit';
281 is( $debit_1->discard_changes->amountoutstanding * 1, 0, 'No changes to already cancelled debit' );
282 is( $debit_2->discard_changes->amountoutstanding * 1, 10, 'Debit cancelled' );
283 is( $debit_3->discard_changes->amountoutstanding * 1, 100, 'Outstanding amount correctly calculated' );
284 is( $credit_2->discard_changes->amountoutstanding * -1, 20, 'No changes made' );
286 $debits = Koha::Account::Lines->search({ accountlines_id => { -in => [ $debit_1->id, $debit_2->id, $debit_3->id ] } });
287 $remaining_credit = $credit_2->apply( { debits => $debits, offset_type => 'Manual Credit' } );
289 is( $debit_1->discard_changes->amountoutstanding * 1, 0, 'No changes to already cancelled debit' );
290 is( $debit_2->discard_changes->amountoutstanding * 1, 0, 'Debit cancelled' );
291 is( $debit_3->discard_changes->amountoutstanding * 1, 90, 'Outstanding amount correctly calculated' );
292 is( $credit_2->discard_changes->amountoutstanding * 1, 0, 'No remaining credit' );
294 $schema->storage->txn_rollback;
297 subtest 'Keep account info when related patron, staff or item is deleted' => sub {
301 $schema->storage->txn_begin;
303 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
304 my $staff = $builder->build_object( { class => 'Koha::Patrons' } );
305 my $item = $builder->build_object({ class => 'Koha::Items' });
306 my $issue = $builder->build_object(
308 class => 'Koha::Checkouts',
309 value => { itemnumber => $item->itemnumber }
312 my $line = Koha::Account::Line->new(
314 borrowernumber => $patron->borrowernumber,
315 manager_id => $staff->borrowernumber,
316 itemnumber => $item->itemnumber,
317 accounttype => "OVERDUE",
318 status => "RETURNED",
320 interface => 'commandline',
325 $line = $line->get_from_storage;
326 is( $line->itemnumber, undef, "The account line should not be deleted when the related item is delete");
329 $line = $line->get_from_storage;
330 is( $line->manager_id, undef, "The account line should not be deleted when the related staff is delete");
333 $line = $line->get_from_storage;
334 is( $line->borrowernumber, undef, "The account line should not be deleted when the related patron is delete");
336 $schema->storage->txn_rollback;
339 subtest 'adjust() tests' => sub {
343 $schema->storage->txn_begin;
345 # count logs before any actions
346 my $action_logs = $schema->resultset('ActionLog')->search()->count;
349 t::lib::Mocks::mock_preference( 'FinesLog', 0 );
351 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
352 my $account = $patron->account;
354 my $debit_1 = Koha::Account::Line->new(
355 { borrowernumber => $patron->id,
356 accounttype => "OVERDUE",
357 status => "RETURNED",
359 amountoutstanding => 10,
360 interface => 'commandline',
364 my $debit_2 = Koha::Account::Line->new(
365 { borrowernumber => $patron->id,
366 accounttype => "OVERDUE",
367 status => "UNRETURNED",
369 amountoutstanding => 100,
370 interface => 'commandline'
374 my $credit = $account->add_credit( { amount => 40, user_id => $patron->id, interface => 'commandline' } );
376 throws_ok { $debit_1->adjust( { amount => 50, type => 'bad', interface => 'commandline' } ) }
377 qr/Update type not recognised/, 'Exception thrown for unrecognised type';
379 throws_ok { $debit_1->adjust( { amount => 50, type => 'overdue_update', interface => 'commandline' } ) }
380 qr/Update type not allowed on this accounttype/,
381 'Exception thrown for type conflict';
383 # Increment an unpaid fine
384 $debit_2->adjust( { amount => 150, type => 'overdue_update', interface => 'commandline' } )->discard_changes;
386 is( $debit_2->amount * 1, 150, 'Fine amount was updated in full' );
387 is( $debit_2->amountoutstanding * 1, 150, 'Fine amountoutstanding was update in full' );
388 isnt( $debit_2->date, undef, 'Date has been set' );
390 my $offsets = Koha::Account::Offsets->search( { debit_id => $debit_2->id } );
391 is( $offsets->count, 1, 'An offset is generated for the increment' );
392 my $THIS_offset = $offsets->next;
393 is( $THIS_offset->amount * 1, 50, 'Amount was calculated correctly (increment by 50)' );
394 is( $THIS_offset->type, 'OVERDUE_INCREASE', 'Adjust type stored correctly' );
396 is( $schema->resultset('ActionLog')->count(), $action_logs + 0, 'No log was added' );
398 # Update fine to partially paid
399 my $debits = Koha::Account::Lines->search({ accountlines_id => $debit_2->id });
400 $credit->apply( { debits => $debits, offset_type => 'Manual Credit' } );
402 $debit_2->discard_changes;
403 is( $debit_2->amount * 1, 150, 'Fine amount unaffected by partial payment' );
404 is( $debit_2->amountoutstanding * 1, 110, 'Fine amountoutstanding updated by partial payment' );
407 t::lib::Mocks::mock_preference( 'FinesLog', 1 );
409 # Increment the partially paid fine
410 $debit_2->adjust( { amount => 160, type => 'overdue_update', interface => 'commandline' } )->discard_changes;
412 is( $debit_2->amount * 1, 160, 'Fine amount was updated in full' );
413 is( $debit_2->amountoutstanding * 1, 120, 'Fine amountoutstanding was updated by difference' );
415 $offsets = Koha::Account::Offsets->search( { debit_id => $debit_2->id } );
416 is( $offsets->count, 3, 'An offset is generated for the increment' );
417 $THIS_offset = $offsets->last;
418 is( $THIS_offset->amount * 1, 10, 'Amount was calculated correctly (increment by 10)' );
419 is( $THIS_offset->type, 'OVERDUE_INCREASE', 'Adjust type stored correctly' );
421 is( $schema->resultset('ActionLog')->count(), $action_logs + 1, 'Log was added' );
423 # Decrement the partially paid fine, less than what was paid
424 $debit_2->adjust( { amount => 50, type => 'overdue_update', interface => 'commandline' } )->discard_changes;
426 is( $debit_2->amount * 1, 50, 'Fine amount was updated in full' );
427 is( $debit_2->amountoutstanding * 1, 10, 'Fine amountoutstanding was updated by difference' );
429 $offsets = Koha::Account::Offsets->search( { debit_id => $debit_2->id } );
430 is( $offsets->count, 4, 'An offset is generated for the decrement' );
431 $THIS_offset = $offsets->last;
432 is( $THIS_offset->amount * 1, -110, 'Amount was calculated correctly (decrement by 110)' );
433 is( $THIS_offset->type, 'OVERDUE_DECREASE', 'Adjust type stored correctly' );
435 # Decrement the partially paid fine, more than what was paid
436 $debit_2->adjust( { amount => 30, type => 'overdue_update', interface => 'commandline' } )->discard_changes;
437 is( $debit_2->amount * 1, 30, 'Fine amount was updated in full' );
438 is( $debit_2->amountoutstanding * 1, 0, 'Fine amountoutstanding was zeroed (payment was 40)' );
440 $offsets = Koha::Account::Offsets->search( { debit_id => $debit_2->id } );
441 is( $offsets->count, 5, 'An offset is generated for the decrement' );
442 $THIS_offset = $offsets->last;
443 is( $THIS_offset->amount * 1, -20, 'Amount was calculated correctly (decrement by 20)' );
444 is( $THIS_offset->type, 'OVERDUE_DECREASE', 'Adjust type stored correctly' );
446 my $overpayment_refund = $account->lines->last;
447 is( $overpayment_refund->amount * 1, -10, 'A new credit has been added' );
448 is( $overpayment_refund->description, 'Overpayment refund', 'Credit generated with the expected description' );
450 $schema->storage->txn_rollback;
453 subtest 'checkout() tests' => sub {
456 $schema->storage->txn_begin;
458 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
459 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
460 my $item = $builder->build_sample_item;
461 my $account = $patron->account;
463 t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode });
464 my $checkout = AddIssue( $patron->unblessed, $item->barcode );
466 my $line = $account->add_debit({
468 interface => 'commandline',
469 item_id => $item->itemnumber,
470 issue_id => $checkout->issue_id,
474 my $line_checkout = $line->checkout;
475 is( ref($line_checkout), 'Koha::Checkout', 'Result type is correct' );
476 is( $line_checkout->issue_id, $checkout->issue_id, 'Koha::Account::Line->checkout should return the correct checkout');
478 my ( $returned, undef, $old_checkout) = C4::Circulation::AddReturn( $item->barcode, $library->branchcode );
479 is( $returned, 1, 'The item should have been returned' );
481 $line = $line->get_from_storage;
482 my $old_line_checkout = $line->checkout;
483 is( ref($old_line_checkout), 'Koha::Old::Checkout', 'Result type is correct' );
484 is( $old_line_checkout->issue_id, $old_checkout->issue_id, 'Koha::Account::Line->checkout should return the correct old_checkout' );
486 $line->issue_id(undef)->store;
487 is( $line->checkout, undef, 'Koha::Account::Line->checkout should return undef if no checkout linked' );
489 $schema->storage->txn_rollback;