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,
62 interface => 'commandline',
65 my $account_line_item = $line->item;
66 is( ref( $account_line_item ), 'Koha::Item', 'Koha::Account::Line->item should return a Koha::Item' );
67 is( $line->itemnumber, $account_line_item->itemnumber, 'Koha::Account::Line->item should return the correct item' );
69 $line->itemnumber(undef)->store;
70 is( $line->item, undef, 'Koha::Account::Line->item should return undef if no item linked' );
72 $schema->storage->txn_rollback;
75 subtest 'total_outstanding() tests' => sub {
79 $schema->storage->txn_begin;
81 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
83 my $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
84 is( $lines->total_outstanding, 0, 'total_outstanding returns 0 if no lines (undef case)' );
86 my $debit_1 = Koha::Account::Line->new(
87 { borrowernumber => $patron->id,
90 amountoutstanding => 10,
91 interface => 'commandline',
95 my $debit_2 = Koha::Account::Line->new(
96 { borrowernumber => $patron->id,
99 amountoutstanding => 10,
100 interface => 'commandline',
104 $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
105 is( $lines->total_outstanding, 20, 'total_outstanding sums correctly' );
107 my $credit_1 = Koha::Account::Line->new(
108 { borrowernumber => $patron->id,
111 amountoutstanding => -10,
112 interface => 'commandline',
116 $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
117 is( $lines->total_outstanding, 10, 'total_outstanding sums correctly' );
119 my $credit_2 = Koha::Account::Line->new(
120 { borrowernumber => $patron->id,
123 amountoutstanding => -10,
124 interface => 'commandline',
128 $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
129 is( $lines->total_outstanding, 0, 'total_outstanding sums correctly' );
131 my $credit_3 = Koha::Account::Line->new(
132 { borrowernumber => $patron->id,
135 amountoutstanding => -100,
136 interface => 'commandline',
140 $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
141 is( $lines->total_outstanding, -100, 'total_outstanding sums correctly' );
143 $schema->storage->txn_rollback;
146 subtest 'is_credit() and is_debit() tests' => sub {
150 $schema->storage->txn_begin;
152 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
153 my $account = $patron->account;
155 my $credit = $account->add_credit({ amount => 100, user_id => $patron->id, interface => 'commandline' });
157 ok( $credit->is_credit, 'is_credit detects credits' );
158 ok( !$credit->is_debit, 'is_debit detects credits' );
160 my $debit = Koha::Account::Line->new(
162 borrowernumber => $patron->id,
165 interface => 'commandline',
168 ok( !$debit->is_credit, 'is_credit detects debits' );
169 ok( $debit->is_debit, 'is_debit detects debits');
171 $schema->storage->txn_rollback;
174 subtest 'apply() tests' => sub {
178 $schema->storage->txn_begin;
180 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
181 my $account = $patron->account;
183 my $credit = $account->add_credit( { amount => 100, user_id => $patron->id, interface => 'commandline' } );
185 my $debit_1 = Koha::Account::Line->new(
186 { borrowernumber => $patron->id,
189 amountoutstanding => 10,
190 interface => 'commandline',
194 my $debit_2 = Koha::Account::Line->new(
195 { borrowernumber => $patron->id,
198 amountoutstanding => 100,
199 interface => 'commandline',
203 $credit->discard_changes;
204 $debit_1->discard_changes;
206 my $debits = Koha::Account::Lines->search({ accountlines_id => $debit_1->id });
207 my $remaining_credit = $credit->apply( { debits => $debits, offset_type => 'Manual Credit' } );
208 is( $remaining_credit * 1, 90, 'Remaining credit is correctly calculated' );
209 $credit->discard_changes;
210 is( $credit->amountoutstanding * -1, $remaining_credit, 'Remaining credit correctly stored' );
213 $debit_1->discard_changes;
214 is( $debit_1->amountoutstanding * 1, 0, 'Debit has been cancelled' );
216 my $offsets = Koha::Account::Offsets->search( { credit_id => $credit->id, debit_id => $debit_1->id } );
217 is( $offsets->count, 1, 'Only one offset is generated' );
218 my $THE_offset = $offsets->next;
219 is( $THE_offset->amount * 1, -10, 'Amount was calculated correctly (less than the available credit)' );
220 is( $THE_offset->type, 'Manual Credit', 'Passed type stored correctly' );
222 $debits = Koha::Account::Lines->search({ accountlines_id => $debit_2->id });
223 $remaining_credit = $credit->apply( { debits => $debits } );
224 is( $remaining_credit, 0, 'No remaining credit left' );
225 $credit->discard_changes;
226 is( $credit->amountoutstanding * 1, 0, 'No outstanding credit' );
227 $debit_2->discard_changes;
228 is( $debit_2->amountoutstanding * 1, 10, 'Outstanding amount decremented correctly' );
230 $offsets = Koha::Account::Offsets->search( { credit_id => $credit->id, debit_id => $debit_2->id } );
231 is( $offsets->count, 1, 'Only one offset is generated' );
232 $THE_offset = $offsets->next;
233 is( $THE_offset->amount * 1, -90, 'Amount was calculated correctly (less than the available credit)' );
234 is( $THE_offset->type, 'Credit Applied', 'Defaults to \'Credit Applied\' offset type' );
236 $debits = Koha::Account::Lines->search({ accountlines_id => $debit_1->id });
238 { $credit->apply({ debits => $debits }); }
239 'Koha::Exceptions::Account::NoAvailableCredit',
240 '->apply() can only be used with outstanding credits';
242 $debits = Koha::Account::Lines->search({ accountlines_id => $credit->id });
244 { $debit_1->apply({ debits => $debits }); }
245 'Koha::Exceptions::Account::IsNotCredit',
246 '->apply() can only be used with credits';
248 $debits = Koha::Account::Lines->search({ accountlines_id => $credit->id });
249 my $credit_3 = $account->add_credit({ amount => 1, interface => 'commandline' });
251 { $credit_3->apply({ debits => $debits }); }
252 'Koha::Exceptions::Account::IsNotDebit',
253 '->apply() can only be applied to credits';
255 my $credit_2 = $account->add_credit({ amount => 20, interface => 'commandline' });
256 my $debit_3 = Koha::Account::Line->new(
257 { borrowernumber => $patron->id,
260 amountoutstanding => 100,
261 interface => 'commandline',
265 $debits = Koha::Account::Lines->search({ accountlines_id => { -in => [ $debit_1->id, $debit_2->id, $debit_3->id, $credit->id ] } });
267 $credit_2->apply( { debits => $debits, offset_type => 'Manual Credit' } ); }
268 'Koha::Exceptions::Account::IsNotDebit',
269 '->apply() rolls back if any of the passed lines is not a debit';
271 is( $debit_1->discard_changes->amountoutstanding * 1, 0, 'No changes to already cancelled debit' );
272 is( $debit_2->discard_changes->amountoutstanding * 1, 10, 'Debit cancelled' );
273 is( $debit_3->discard_changes->amountoutstanding * 1, 100, 'Outstanding amount correctly calculated' );
274 is( $credit_2->discard_changes->amountoutstanding * -1, 20, 'No changes made' );
276 $debits = Koha::Account::Lines->search({ accountlines_id => { -in => [ $debit_1->id, $debit_2->id, $debit_3->id ] } });
277 $remaining_credit = $credit_2->apply( { debits => $debits, offset_type => 'Manual Credit' } );
279 is( $debit_1->discard_changes->amountoutstanding * 1, 0, 'No changes to already cancelled debit' );
280 is( $debit_2->discard_changes->amountoutstanding * 1, 0, 'Debit cancelled' );
281 is( $debit_3->discard_changes->amountoutstanding * 1, 90, 'Outstanding amount correctly calculated' );
282 is( $credit_2->discard_changes->amountoutstanding * 1, 0, 'No remaining credit' );
284 $schema->storage->txn_rollback;
287 subtest 'Keep account info when related patron, staff or item is deleted' => sub {
291 $schema->storage->txn_begin;
293 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
294 my $staff = $builder->build_object( { class => 'Koha::Patrons' } );
295 my $item = $builder->build_object({ class => 'Koha::Items' });
296 my $issue = $builder->build_object(
298 class => 'Koha::Checkouts',
299 value => { itemnumber => $item->itemnumber }
302 my $line = Koha::Account::Line->new(
304 borrowernumber => $patron->borrowernumber,
305 manager_id => $staff->borrowernumber,
306 itemnumber => $item->itemnumber,
309 interface => 'commandline',
314 $line = $line->get_from_storage;
315 is( $line->itemnumber, undef, "The account line should not be deleted when the related item is delete");
318 $line = $line->get_from_storage;
319 is( $line->manager_id, undef, "The account line should not be deleted when the related staff is delete");
322 $line = $line->get_from_storage;
323 is( $line->borrowernumber, undef, "The account line should not be deleted when the related patron is delete");
325 $schema->storage->txn_rollback;
328 subtest 'adjust() tests' => sub {
332 $schema->storage->txn_begin;
334 # count logs before any actions
335 my $action_logs = $schema->resultset('ActionLog')->search()->count;
338 t::lib::Mocks::mock_preference( 'FinesLog', 0 );
340 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
341 my $account = $patron->account;
343 my $debit_1 = Koha::Account::Line->new(
344 { borrowernumber => $patron->id,
347 amountoutstanding => 10,
348 interface => 'commandline',
352 my $debit_2 = Koha::Account::Line->new(
353 { borrowernumber => $patron->id,
356 amountoutstanding => 100,
357 interface => 'commandline'
361 my $credit = $account->add_credit( { amount => 40, user_id => $patron->id, interface => 'commandline' } );
363 throws_ok { $debit_1->adjust( { amount => 50, type => 'bad', interface => 'commandline' } ) }
364 qr/Update type not recognised/, 'Exception thrown for unrecognised type';
366 throws_ok { $debit_1->adjust( { amount => 50, type => 'fine_update', interface => 'commandline' } ) }
367 qr/Update type not allowed on this accounttype/,
368 'Exception thrown for type conflict';
370 # Increment an unpaid fine
371 $debit_2->adjust( { amount => 150, type => 'fine_update', interface => 'commandline' } )->discard_changes;
373 is( $debit_2->amount * 1, 150, 'Fine amount was updated in full' );
374 is( $debit_2->amountoutstanding * 1, 150, 'Fine amountoutstanding was update in full' );
375 isnt( $debit_2->date, undef, 'Date has been set' );
377 my $offsets = Koha::Account::Offsets->search( { debit_id => $debit_2->id } );
378 is( $offsets->count, 1, 'An offset is generated for the increment' );
379 my $THIS_offset = $offsets->next;
380 is( $THIS_offset->amount * 1, 50, 'Amount was calculated correctly (increment by 50)' );
381 is( $THIS_offset->type, 'fine_increase', 'Adjust type stored correctly' );
383 is( $schema->resultset('ActionLog')->count(), $action_logs + 0, 'No log was added' );
385 # Update fine to partially paid
386 my $debits = Koha::Account::Lines->search({ accountlines_id => $debit_2->id });
387 $credit->apply( { debits => $debits, offset_type => 'Manual Credit' } );
389 $debit_2->discard_changes;
390 is( $debit_2->amount * 1, 150, 'Fine amount unaffected by partial payment' );
391 is( $debit_2->amountoutstanding * 1, 110, 'Fine amountoutstanding updated by partial payment' );
394 t::lib::Mocks::mock_preference( 'FinesLog', 1 );
396 # Increment the partially paid fine
397 $debit_2->adjust( { amount => 160, type => 'fine_update', interface => 'commandline' } )->discard_changes;
399 is( $debit_2->amount * 1, 160, 'Fine amount was updated in full' );
400 is( $debit_2->amountoutstanding * 1, 120, 'Fine amountoutstanding was updated by difference' );
402 $offsets = Koha::Account::Offsets->search( { debit_id => $debit_2->id } );
403 is( $offsets->count, 3, 'An offset is generated for the increment' );
404 $THIS_offset = $offsets->last;
405 is( $THIS_offset->amount * 1, 10, 'Amount was calculated correctly (increment by 10)' );
406 is( $THIS_offset->type, 'fine_increase', 'Adjust type stored correctly' );
408 is( $schema->resultset('ActionLog')->count(), $action_logs + 1, 'Log was added' );
410 # Decrement the partially paid fine, less than what was paid
411 $debit_2->adjust( { amount => 50, type => 'fine_update', interface => 'commandline' } )->discard_changes;
413 is( $debit_2->amount * 1, 50, 'Fine amount was updated in full' );
414 is( $debit_2->amountoutstanding * 1, 10, 'Fine amountoutstanding was updated by difference' );
416 $offsets = Koha::Account::Offsets->search( { debit_id => $debit_2->id } );
417 is( $offsets->count, 4, 'An offset is generated for the decrement' );
418 $THIS_offset = $offsets->last;
419 is( $THIS_offset->amount * 1, -110, 'Amount was calculated correctly (decrement by 110)' );
420 is( $THIS_offset->type, 'fine_decrease', 'Adjust type stored correctly' );
422 # Decrement the partially paid fine, more than what was paid
423 $debit_2->adjust( { amount => 30, type => 'fine_update', interface => 'commandline' } )->discard_changes;
424 is( $debit_2->amount * 1, 30, 'Fine amount was updated in full' );
425 is( $debit_2->amountoutstanding * 1, 0, 'Fine amountoutstanding was zeroed (payment was 40)' );
427 $offsets = Koha::Account::Offsets->search( { debit_id => $debit_2->id } );
428 is( $offsets->count, 5, 'An offset is generated for the decrement' );
429 $THIS_offset = $offsets->last;
430 is( $THIS_offset->amount * 1, -20, 'Amount was calculated correctly (decrement by 20)' );
431 is( $THIS_offset->type, 'fine_decrease', 'Adjust type stored correctly' );
433 my $overpayment_refund = $account->lines->last;
434 is( $overpayment_refund->amount * 1, -10, 'A new credit has been added' );
435 is( $overpayment_refund->description, 'Overpayment refund', 'Credit generated with the expected description' );
437 $schema->storage->txn_rollback;
440 subtest 'checkout() tests' => sub {
443 $schema->storage->txn_begin;
445 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
446 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
447 my $item = $builder->build_sample_item;
448 my $account = $patron->account;
450 t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode });
451 my $checkout = AddIssue( $patron->unblessed, $item->barcode );
453 my $line = $account->add_debit({
455 interface => 'commandline',
456 item_id => $item->itemnumber,
457 issue_id => $checkout->issue_id,
461 my $line_checkout = $line->checkout;
462 is( ref($line_checkout), 'Koha::Checkout', 'Result type is correct' );
463 is( $line_checkout->issue_id, $checkout->issue_id, 'Koha::Account::Line->checkout should return the correct checkout');
465 my ( $returned, undef, $old_checkout) = C4::Circulation::AddReturn( $item->barcode, $library->branchcode );
466 is( $returned, 1, 'The item should have been returned' );
468 $line = $line->get_from_storage;
469 my $old_line_checkout = $line->checkout;
470 is( ref($old_line_checkout), 'Koha::Old::Checkout', 'Result type is correct' );
471 is( $old_line_checkout->issue_id, $old_checkout->issue_id, 'Koha::Account::Line->checkout should return the correct old_checkout' );
473 $line->issue_id(undef)->store;
474 is( $line->checkout, undef, 'Koha::Account::Line->checkout should return undef if no checkout linked' );
476 $schema->storage->txn_rollback;