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,
64 my $account_line_item = $line->item;
65 is( ref( $account_line_item ), 'Koha::Item', 'Koha::Account::Line->item should return a Koha::Item' );
66 is( $line->itemnumber, $account_line_item->itemnumber, 'Koha::Account::Line->item should return the correct item' );
68 $line->itemnumber(undef)->store;
69 is( $line->item, undef, 'Koha::Account::Line->item should return undef if no item linked' );
71 $schema->storage->txn_rollback;
74 subtest 'total_outstanding() tests' => sub {
78 $schema->storage->txn_begin;
80 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
82 my $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
83 is( $lines->total_outstanding, 0, 'total_outstanding returns 0 if no lines (undef case)' );
85 my $debit_1 = Koha::Account::Line->new(
86 { borrowernumber => $patron->id,
89 amountoutstanding => 10
93 my $debit_2 = Koha::Account::Line->new(
94 { borrowernumber => $patron->id,
97 amountoutstanding => 10
101 $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
102 is( $lines->total_outstanding, 20, 'total_outstanding sums correctly' );
104 my $credit_1 = Koha::Account::Line->new(
105 { borrowernumber => $patron->id,
108 amountoutstanding => -10
112 $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
113 is( $lines->total_outstanding, 10, 'total_outstanding sums correctly' );
115 my $credit_2 = Koha::Account::Line->new(
116 { borrowernumber => $patron->id,
119 amountoutstanding => -10
123 $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
124 is( $lines->total_outstanding, 0, 'total_outstanding sums correctly' );
126 my $credit_3 = Koha::Account::Line->new(
127 { borrowernumber => $patron->id,
130 amountoutstanding => -100
134 $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
135 is( $lines->total_outstanding, -100, 'total_outstanding sums correctly' );
137 $schema->storage->txn_rollback;
140 subtest 'is_credit() and is_debit() tests' => sub {
144 $schema->storage->txn_begin;
146 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
147 my $account = $patron->account;
149 my $credit = $account->add_credit({ amount => 100, user_id => $patron->id });
151 ok( $credit->is_credit, 'is_credit detects credits' );
152 ok( !$credit->is_debit, 'is_debit detects credits' );
154 my $debit = Koha::Account::Line->new(
156 borrowernumber => $patron->id,
161 ok( !$debit->is_credit, 'is_credit detects debits' );
162 ok( $debit->is_debit, 'is_debit detects debits');
164 $schema->storage->txn_rollback;
167 subtest 'apply() tests' => sub {
171 $schema->storage->txn_begin;
173 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
174 my $account = $patron->account;
176 my $credit = $account->add_credit( { amount => 100, user_id => $patron->id } );
178 my $debit_1 = Koha::Account::Line->new(
179 { borrowernumber => $patron->id,
182 amountoutstanding => 10
186 my $debit_2 = Koha::Account::Line->new(
187 { borrowernumber => $patron->id,
190 amountoutstanding => 100
194 $credit->discard_changes;
195 $debit_1->discard_changes;
197 my $debits = Koha::Account::Lines->search({ accountlines_id => $debit_1->id });
198 my $remaining_credit = $credit->apply( { debits => $debits, offset_type => 'Manual Credit' } );
199 is( $remaining_credit * 1, 90, 'Remaining credit is correctly calculated' );
200 $credit->discard_changes;
201 is( $credit->amountoutstanding * -1, $remaining_credit, 'Remaining credit correctly stored' );
204 $debit_1->discard_changes;
205 is( $debit_1->amountoutstanding * 1, 0, 'Debit has been cancelled' );
207 my $offsets = Koha::Account::Offsets->search( { credit_id => $credit->id, debit_id => $debit_1->id } );
208 is( $offsets->count, 1, 'Only one offset is generated' );
209 my $THE_offset = $offsets->next;
210 is( $THE_offset->amount * 1, -10, 'Amount was calculated correctly (less than the available credit)' );
211 is( $THE_offset->type, 'Manual Credit', 'Passed type stored correctly' );
213 $debits = Koha::Account::Lines->search({ accountlines_id => $debit_2->id });
214 $remaining_credit = $credit->apply( { debits => $debits } );
215 is( $remaining_credit, 0, 'No remaining credit left' );
216 $credit->discard_changes;
217 is( $credit->amountoutstanding * 1, 0, 'No outstanding credit' );
218 $debit_2->discard_changes;
219 is( $debit_2->amountoutstanding * 1, 10, 'Outstanding amount decremented correctly' );
221 $offsets = Koha::Account::Offsets->search( { credit_id => $credit->id, debit_id => $debit_2->id } );
222 is( $offsets->count, 1, 'Only one offset is generated' );
223 $THE_offset = $offsets->next;
224 is( $THE_offset->amount * 1, -90, 'Amount was calculated correctly (less than the available credit)' );
225 is( $THE_offset->type, 'Credit Applied', 'Defaults to \'Credit Applied\' offset type' );
227 $debits = Koha::Account::Lines->search({ accountlines_id => $debit_1->id });
229 { $credit->apply({ debits => $debits }); }
230 'Koha::Exceptions::Account::NoAvailableCredit',
231 '->apply() can only be used with outstanding credits';
233 $debits = Koha::Account::Lines->search({ accountlines_id => $credit->id });
235 { $debit_1->apply({ debits => $debits }); }
236 'Koha::Exceptions::Account::IsNotCredit',
237 '->apply() can only be used with credits';
239 $debits = Koha::Account::Lines->search({ accountlines_id => $credit->id });
240 my $credit_3 = $account->add_credit({ amount => 1 });
242 { $credit_3->apply({ debits => $debits }); }
243 'Koha::Exceptions::Account::IsNotDebit',
244 '->apply() can only be applied to credits';
246 my $credit_2 = $account->add_credit({ amount => 20 });
247 my $debit_3 = Koha::Account::Line->new(
248 { borrowernumber => $patron->id,
251 amountoutstanding => 100
255 $debits = Koha::Account::Lines->search({ accountlines_id => { -in => [ $debit_1->id, $debit_2->id, $debit_3->id, $credit->id ] } });
257 $credit_2->apply( { debits => $debits, offset_type => 'Manual Credit' } ); }
258 'Koha::Exceptions::Account::IsNotDebit',
259 '->apply() rolls back if any of the passed lines is not a debit';
261 is( $debit_1->discard_changes->amountoutstanding * 1, 0, 'No changes to already cancelled debit' );
262 is( $debit_2->discard_changes->amountoutstanding * 1, 10, 'Debit cancelled' );
263 is( $debit_3->discard_changes->amountoutstanding * 1, 100, 'Outstanding amount correctly calculated' );
264 is( $credit_2->discard_changes->amountoutstanding * -1, 20, 'No changes made' );
266 $debits = Koha::Account::Lines->search({ accountlines_id => { -in => [ $debit_1->id, $debit_2->id, $debit_3->id ] } });
267 $remaining_credit = $credit_2->apply( { debits => $debits, offset_type => 'Manual Credit' } );
269 is( $debit_1->discard_changes->amountoutstanding * 1, 0, 'No changes to already cancelled debit' );
270 is( $debit_2->discard_changes->amountoutstanding * 1, 0, 'Debit cancelled' );
271 is( $debit_3->discard_changes->amountoutstanding * 1, 90, 'Outstanding amount correctly calculated' );
272 is( $credit_2->discard_changes->amountoutstanding * 1, 0, 'No remaining credit' );
274 $schema->storage->txn_rollback;
277 subtest 'Keep account info when related patron, staff or item is deleted' => sub {
281 $schema->storage->txn_begin;
283 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
284 my $staff = $builder->build_object( { class => 'Koha::Patrons' } );
285 my $item = $builder->build_object({ class => 'Koha::Items' });
286 my $issue = $builder->build_object(
288 class => 'Koha::Checkouts',
289 value => { itemnumber => $item->itemnumber }
292 my $line = Koha::Account::Line->new(
294 borrowernumber => $patron->borrowernumber,
295 manager_id => $staff->borrowernumber,
296 itemnumber => $item->itemnumber,
303 $line = $line->get_from_storage;
304 is( $line->itemnumber, undef, "The account line should not be deleted when the related item is delete");
307 $line = $line->get_from_storage;
308 is( $line->manager_id, undef, "The account line should not be deleted when the related staff is delete");
311 $line = $line->get_from_storage;
312 is( $line->borrowernumber, undef, "The account line should not be deleted when the related patron is delete");
314 $schema->storage->txn_rollback;
317 subtest 'adjust() tests' => sub {
321 $schema->storage->txn_begin;
323 # count logs before any actions
324 my $action_logs = $schema->resultset('ActionLog')->search()->count;
327 t::lib::Mocks::mock_preference( 'FinesLog', 0 );
329 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
330 my $account = $patron->account;
332 my $debit_1 = Koha::Account::Line->new(
333 { borrowernumber => $patron->id,
336 amountoutstanding => 10
340 my $debit_2 = Koha::Account::Line->new(
341 { borrowernumber => $patron->id,
344 amountoutstanding => 100
348 my $credit = $account->add_credit( { amount => 40, user_id => $patron->id } );
350 throws_ok { $debit_1->adjust( { amount => 50, type => 'bad' } ) }
351 qr/Update type not recognised/, 'Exception thrown for unrecognised type';
353 throws_ok { $debit_1->adjust( { amount => 50, type => 'fine_update' } ) }
354 qr/Update type not allowed on this accounttype/,
355 'Exception thrown for type conflict';
357 # Increment an unpaid fine
358 $debit_2->adjust( { amount => 150, type => 'fine_update' } )->discard_changes;
360 is( $debit_2->amount * 1, 150, 'Fine amount was updated in full' );
361 is( $debit_2->amountoutstanding * 1, 150, 'Fine amountoutstanding was update in full' );
362 isnt( $debit_2->date, undef, 'Date has been set' );
364 my $offsets = Koha::Account::Offsets->search( { debit_id => $debit_2->id } );
365 is( $offsets->count, 1, 'An offset is generated for the increment' );
366 my $THIS_offset = $offsets->next;
367 is( $THIS_offset->amount * 1, 50, 'Amount was calculated correctly (increment by 50)' );
368 is( $THIS_offset->type, 'fine_increase', 'Adjust type stored correctly' );
370 is( $schema->resultset('ActionLog')->count(), $action_logs + 0, 'No log was added' );
372 # Update fine to partially paid
373 my $debits = Koha::Account::Lines->search({ accountlines_id => $debit_2->id });
374 $credit->apply( { debits => $debits, offset_type => 'Manual Credit' } );
376 $debit_2->discard_changes;
377 is( $debit_2->amount * 1, 150, 'Fine amount unaffected by partial payment' );
378 is( $debit_2->amountoutstanding * 1, 110, 'Fine amountoutstanding updated by partial payment' );
381 t::lib::Mocks::mock_preference( 'FinesLog', 1 );
383 # Increment the partially paid fine
384 $debit_2->adjust( { amount => 160, type => 'fine_update' } )->discard_changes;
386 is( $debit_2->amount * 1, 160, 'Fine amount was updated in full' );
387 is( $debit_2->amountoutstanding * 1, 120, 'Fine amountoutstanding was updated by difference' );
389 $offsets = Koha::Account::Offsets->search( { debit_id => $debit_2->id } );
390 is( $offsets->count, 3, 'An offset is generated for the increment' );
391 $THIS_offset = $offsets->last;
392 is( $THIS_offset->amount * 1, 10, 'Amount was calculated correctly (increment by 10)' );
393 is( $THIS_offset->type, 'fine_increase', 'Adjust type stored correctly' );
395 is( $schema->resultset('ActionLog')->count(), $action_logs + 1, 'Log was added' );
397 # Decrement the partially paid fine, less than what was paid
398 $debit_2->adjust( { amount => 50, type => 'fine_update' } )->discard_changes;
400 is( $debit_2->amount * 1, 50, 'Fine amount was updated in full' );
401 is( $debit_2->amountoutstanding * 1, 10, 'Fine amountoutstanding was updated by difference' );
403 $offsets = Koha::Account::Offsets->search( { debit_id => $debit_2->id } );
404 is( $offsets->count, 4, 'An offset is generated for the decrement' );
405 $THIS_offset = $offsets->last;
406 is( $THIS_offset->amount * 1, -110, 'Amount was calculated correctly (decrement by 110)' );
407 is( $THIS_offset->type, 'fine_decrease', 'Adjust type stored correctly' );
409 # Decrement the partially paid fine, more than what was paid
410 $debit_2->adjust( { amount => 30, type => 'fine_update' } )->discard_changes;
411 is( $debit_2->amount * 1, 30, 'Fine amount was updated in full' );
412 is( $debit_2->amountoutstanding * 1, 0, 'Fine amountoutstanding was zeroed (payment was 40)' );
414 $offsets = Koha::Account::Offsets->search( { debit_id => $debit_2->id } );
415 is( $offsets->count, 5, 'An offset is generated for the decrement' );
416 $THIS_offset = $offsets->last;
417 is( $THIS_offset->amount * 1, -20, 'Amount was calculated correctly (decrement by 20)' );
418 is( $THIS_offset->type, 'fine_decrease', 'Adjust type stored correctly' );
420 my $overpayment_refund = $account->lines->last;
421 is( $overpayment_refund->amount * 1, -10, 'A new credit has been added' );
422 is( $overpayment_refund->description, 'Overpayment refund', 'Credit generated with the expected description' );
424 $schema->storage->txn_rollback;
427 subtest 'checkout() tests' => sub {
430 $schema->storage->txn_begin;
432 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
433 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
434 my $item = $builder->build_sample_item;
435 my $account = $patron->account;
437 t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode });
438 my $checkout = AddIssue( $patron->unblessed, $item->barcode );
440 my $line = $account->add_debit({
442 item_id => $item->itemnumber,
443 issue_id => $checkout->issue_id,
447 my $line_checkout = $line->checkout;
448 is( ref($line_checkout), 'Koha::Checkout', 'Result type is correct' );
449 is( $line_checkout->issue_id, $checkout->issue_id, 'Koha::Account::Line->checkout should return the correct checkout');
451 my ( $returned, undef, $old_checkout) = C4::Circulation::AddReturn( $item->barcode, $library->branchcode );
452 is( $returned, 1, 'The item should have been returned' );
454 $line = $line->get_from_storage;
455 my $old_line_checkout = $line->checkout;
456 is( ref($old_line_checkout), 'Koha::Old::Checkout', 'Result type is correct' );
457 is( $old_line_checkout->issue_id, $old_checkout->issue_id, 'Koha::Account::Line->checkout should return the correct old_checkout' );
459 $line->issue_id(undef)->store;
460 is( $line->checkout, undef, 'Koha::Account::Line->checkout should return undef if no checkout linked' );
462 $schema->storage->txn_rollback;