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 => 6;
26 use Koha::Account::Lines;
27 use Koha::Account::Offsets;
31 use t::lib::TestBuilder;
33 my $schema = Koha::Database->new->schema;
34 my $builder = t::lib::TestBuilder->new;
36 subtest 'item() tests' => sub {
40 $schema->storage->txn_begin;
42 my $library = $builder->build( { source => 'Branch' } );
43 my $biblioitem = $builder->build( { source => 'Biblioitem' } );
44 my $patron = $builder->build( { source => 'Borrower' } );
45 my $item = Koha::Item->new(
47 biblionumber => $biblioitem->{biblionumber},
48 biblioitemnumber => $biblioitem->{biblioitemnumber},
49 homebranch => $library->{branchcode},
50 holdingbranch => $library->{branchcode},
51 barcode => 'some_barcode_12',
55 my $line = Koha::Account::Line->new(
57 borrowernumber => $patron->{borrowernumber},
58 itemnumber => $item->itemnumber,
63 my $account_line_item = $line->item;
64 is( ref( $account_line_item ), 'Koha::Item', 'Koha::Account::Line->item should return a Koha::Item' );
65 is( $line->itemnumber, $account_line_item->itemnumber, 'Koha::Account::Line->item should return the correct item' );
67 $schema->storage->txn_rollback;
70 subtest 'total_outstanding() tests' => sub {
74 $schema->storage->txn_begin;
76 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
78 my $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
79 is( $lines->total_outstanding, 0, 'total_outstanding returns 0 if no lines (undef case)' );
81 my $debit_1 = Koha::Account::Line->new(
82 { borrowernumber => $patron->id,
85 amountoutstanding => 10
89 my $debit_2 = Koha::Account::Line->new(
90 { borrowernumber => $patron->id,
93 amountoutstanding => 10
97 $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
98 is( $lines->total_outstanding, 20, 'total_outstanding sums correctly' );
100 my $credit_1 = Koha::Account::Line->new(
101 { borrowernumber => $patron->id,
104 amountoutstanding => -10
108 $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
109 is( $lines->total_outstanding, 10, 'total_outstanding sums correctly' );
111 my $credit_2 = Koha::Account::Line->new(
112 { borrowernumber => $patron->id,
115 amountoutstanding => -10
119 $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
120 is( $lines->total_outstanding, 0, 'total_outstanding sums correctly' );
122 my $credit_3 = Koha::Account::Line->new(
123 { borrowernumber => $patron->id,
126 amountoutstanding => -100
130 $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
131 is( $lines->total_outstanding, -100, 'total_outstanding sums correctly' );
133 $schema->storage->txn_rollback;
136 subtest 'is_credit() and is_debit() tests' => sub {
140 $schema->storage->txn_begin;
142 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
143 my $account = $patron->account;
145 my $credit = $account->add_credit({ amount => 100, user_id => $patron->id });
147 ok( $credit->is_credit, 'is_credit detects credits' );
148 ok( !$credit->is_debit, 'is_debit detects credits' );
150 my $debit = Koha::Account::Line->new(
152 borrowernumber => $patron->id,
157 ok( !$debit->is_credit, 'is_credit detects debits' );
158 ok( $debit->is_debit, 'is_debit detects debits');
160 $schema->storage->txn_rollback;
163 subtest 'apply() tests' => sub {
167 $schema->storage->txn_begin;
169 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
170 my $account = $patron->account;
172 my $credit = $account->add_credit( { amount => 100, user_id => $patron->id } );
174 my $debit_1 = Koha::Account::Line->new(
175 { borrowernumber => $patron->id,
178 amountoutstanding => 10
182 my $debit_2 = Koha::Account::Line->new(
183 { borrowernumber => $patron->id,
186 amountoutstanding => 100
190 $credit->discard_changes;
191 $debit_1->discard_changes;
193 my $debits = Koha::Account::Lines->search({ accountlines_id => $debit_1->id });
194 my $remaining_credit = $credit->apply( { debits => $debits, offset_type => 'Manual Credit' } );
195 is( $remaining_credit * 1, 90, 'Remaining credit is correctly calculated' );
196 $credit->discard_changes;
197 is( $credit->amountoutstanding * -1, $remaining_credit, 'Remaining credit correctly stored' );
200 $debit_1->discard_changes;
201 is( $debit_1->amountoutstanding * 1, 0, 'Debit has been cancelled' );
203 my $offsets = Koha::Account::Offsets->search( { credit_id => $credit->id, debit_id => $debit_1->id } );
204 is( $offsets->count, 1, 'Only one offset is generated' );
205 my $THE_offset = $offsets->next;
206 is( $THE_offset->amount * 1, -10, 'Amount was calculated correctly (less than the available credit)' );
207 is( $THE_offset->type, 'Manual Credit', 'Passed type stored correctly' );
209 $debits = Koha::Account::Lines->search({ accountlines_id => $debit_2->id });
210 $remaining_credit = $credit->apply( { debits => $debits } );
211 is( $remaining_credit, 0, 'No remaining credit left' );
212 $credit->discard_changes;
213 is( $credit->amountoutstanding * 1, 0, 'No outstanding credit' );
214 $debit_2->discard_changes;
215 is( $debit_2->amountoutstanding * 1, 10, 'Outstanding amount decremented correctly' );
217 $offsets = Koha::Account::Offsets->search( { credit_id => $credit->id, debit_id => $debit_2->id } );
218 is( $offsets->count, 1, 'Only one offset is generated' );
219 $THE_offset = $offsets->next;
220 is( $THE_offset->amount * 1, -90, 'Amount was calculated correctly (less than the available credit)' );
221 is( $THE_offset->type, 'Credit Applied', 'Defaults to \'Credit Applied\' offset type' );
223 $debits = Koha::Account::Lines->search({ accountlines_id => $debit_1->id });
225 { $credit->apply({ debits => $debits }); }
226 'Koha::Exceptions::Account::NoAvailableCredit',
227 '->apply() can only be used with outstanding credits';
229 $debits = Koha::Account::Lines->search({ accountlines_id => $credit->id });
231 { $debit_1->apply({ debits => $debits }); }
232 'Koha::Exceptions::Account::IsNotCredit',
233 '->apply() can only be used with credits';
235 $debits = Koha::Account::Lines->search({ accountlines_id => $credit->id });
236 my $credit_3 = $account->add_credit({ amount => 1 });
238 { $credit_3->apply({ debits => $debits }); }
239 'Koha::Exceptions::Account::IsNotDebit',
240 '->apply() can only be applied to credits';
242 my $credit_2 = $account->add_credit({ amount => 20 });
243 my $debit_3 = Koha::Account::Line->new(
244 { borrowernumber => $patron->id,
247 amountoutstanding => 100
251 $debits = Koha::Account::Lines->search({ accountlines_id => { -in => [ $debit_1->id, $debit_2->id, $debit_3->id, $credit->id ] } });
253 $credit_2->apply( { debits => $debits, offset_type => 'Manual Credit' } ); }
254 'Koha::Exceptions::Account::IsNotDebit',
255 '->apply() rolls back if any of the passed lines is not a debit';
257 is( $debit_1->discard_changes->amountoutstanding * 1, 0, 'No changes to already cancelled debit' );
258 is( $debit_2->discard_changes->amountoutstanding * 1, 10, 'Debit cancelled' );
259 is( $debit_3->discard_changes->amountoutstanding * 1, 100, 'Outstanding amount correctly calculated' );
260 is( $credit_2->discard_changes->amountoutstanding * -1, 20, 'No changes made' );
262 $debits = Koha::Account::Lines->search({ accountlines_id => { -in => [ $debit_1->id, $debit_2->id, $debit_3->id ] } });
263 $remaining_credit = $credit_2->apply( { debits => $debits, offset_type => 'Manual Credit' } );
265 is( $debit_1->discard_changes->amountoutstanding * 1, 0, 'No changes to already cancelled debit' );
266 is( $debit_2->discard_changes->amountoutstanding * 1, 0, 'Debit cancelled' );
267 is( $debit_3->discard_changes->amountoutstanding * 1, 90, 'Outstanding amount correctly calculated' );
268 is( $credit_2->discard_changes->amountoutstanding * 1, 0, 'No remaining credit' );
270 $schema->storage->txn_rollback;
273 subtest 'Keep account info when a patron is deleted' => sub {
277 $schema->storage->txn_begin;
279 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
280 my $item = $builder->build_object({ class => 'Koha::Items' });
281 my $line = Koha::Account::Line->new(
283 borrowernumber => $patron->borrowernumber,
284 itemnumber => $item->itemnumber,
290 $line = $line->get_from_storage;
291 is( $line->itemnumber, undef, "The account line should not be deleted when the related item is delete");
294 $line = $line->get_from_storage;
295 is( $line->borrowernumber, undef, "The account line should not be deleted when the related patron is delete");
297 $schema->storage->txn_rollback;
300 subtest 'adjust() tests' => sub {
304 $schema->storage->txn_begin;
306 # count logs before any actions
307 my $action_logs = $schema->resultset('ActionLog')->search()->count;
310 t::lib::Mocks::mock_preference( 'FinesLog', 0 );
312 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
313 my $account = $patron->account;
315 my $debit_1 = Koha::Account::Line->new(
316 { borrowernumber => $patron->id,
319 amountoutstanding => 10
323 my $debit_2 = Koha::Account::Line->new(
324 { borrowernumber => $patron->id,
327 amountoutstanding => 100
331 my $credit = $account->add_credit( { amount => 40, user_id => $patron->id } );
333 throws_ok { $debit_1->adjust( { amount => 50, type => 'bad' } ) }
334 qr/Update type not recognised/, 'Exception thrown for unrecognised type';
336 throws_ok { $debit_1->adjust( { amount => 50, type => 'fine_increment' } ) }
337 qr/Update type not allowed on this accounttype/,
338 'Exception thrown for type conflict';
340 # Increment an unpaid fine
341 $debit_2->adjust( { amount => 150, type => 'fine_increment' } )->discard_changes;
343 is( $debit_2->amount * 1, 150, 'Fine amount was updated in full' );
344 is( $debit_2->amountoutstanding * 1, 150, 'Fine amountoutstanding was update in full' );
345 isnt( $debit_2->date, undef, 'Date has been set' );
346 is( $debit_2->lastincrement * 1, 50, 'lastincrement is the to the right value' );
348 my $offsets = Koha::Account::Offsets->search( { debit_id => $debit_2->id } );
349 is( $offsets->count, 1, 'An offset is generated for the increment' );
350 my $THIS_offset = $offsets->next;
351 is( $THIS_offset->amount * 1, 50, 'Amount was calculated correctly (increment by 50)' );
352 is( $THIS_offset->type, 'Fine Update', 'Adjust type stored correctly' );
354 is( $schema->resultset('ActionLog')->count(), $action_logs + 0, 'No log was added' );
356 # Update fine to partially paid
357 my $debits = Koha::Account::Lines->search({ accountlines_id => $debit_2->id });
358 $credit->apply( { debits => $debits, offset_type => 'Manual Credit' } );
360 $debit_2->discard_changes;
361 is( $debit_2->amount * 1, 150, 'Fine amount unaffected by partial payment' );
362 is( $debit_2->amountoutstanding * 1, 110, 'Fine amountoutstanding updated by partial payment' );
365 t::lib::Mocks::mock_preference( 'FinesLog', 1 );
367 # Increment the partially paid fine
368 $debit_2->adjust( { amount => 160, type => 'fine_increment' } )->discard_changes;
370 is( $debit_2->amount * 1, 160, 'Fine amount was updated in full' );
371 is( $debit_2->amountoutstanding * 1, 120, 'Fine amountoutstanding was updated by difference' );
372 is( $debit_2->lastincrement * 1, 10, 'lastincrement is the to the right value' );
374 $offsets = Koha::Account::Offsets->search( { debit_id => $debit_2->id } );
375 is( $offsets->count, 3, 'An offset is generated for the increment' );
376 $THIS_offset = $offsets->last;
377 is( $THIS_offset->amount * 1, 10, 'Amount was calculated correctly (increment by 10)' );
378 is( $THIS_offset->type, 'Fine Update', 'Adjust type stored correctly' );
380 is( $schema->resultset('ActionLog')->count(), $action_logs + 1, 'Log was added' );
382 # Decrement the partially paid fine, less than what was paid
383 $debit_2->adjust( { amount => 50, type => 'fine_increment' } )->discard_changes;
385 is( $debit_2->amount * 1, 50, 'Fine amount was updated in full' );
386 is( $debit_2->amountoutstanding * 1, 10, 'Fine amountoutstanding was updated by difference' );
387 is( $debit_2->lastincrement * 1, -110, 'lastincrement is the to the right value' );
389 $offsets = Koha::Account::Offsets->search( { debit_id => $debit_2->id } );
390 is( $offsets->count, 4, 'An offset is generated for the decrement' );
391 $THIS_offset = $offsets->last;
392 is( $THIS_offset->amount * 1, -110, 'Amount was calculated correctly (decrement by 110)' );
393 is( $THIS_offset->type, 'Fine Update', 'Adjust type stored correctly' );
395 # Decrement the partially paid fine, more than what was paid
396 $debit_2->adjust( { amount => 30, type => 'fine_increment' } )->discard_changes;
397 is( $debit_2->amount * 1, 30, 'Fine amount was updated in full' );
398 is( $debit_2->amountoutstanding * 1, 0, 'Fine amountoutstanding was zeroed (payment was 40)' );
399 is( $debit_2->lastincrement * 1, -20, 'lastincrement is the to the right value' );
401 $offsets = Koha::Account::Offsets->search( { debit_id => $debit_2->id } );
402 is( $offsets->count, 5, 'An offset is generated for the decrement' );
403 $THIS_offset = $offsets->last;
404 is( $THIS_offset->amount * 1, -20, 'Amount was calculated correctly (decrement by 20)' );
405 is( $THIS_offset->type, 'Fine Update', 'Adjust type stored correctly' );
407 my $overpayment_refund = $account->lines->last;
408 is( $overpayment_refund->amount * 1, -10, 'A new credit has been added' );
409 is( $overpayment_refund->description, 'Overpayment refund', 'Credit generated with the expected description' );
411 $schema->storage->txn_rollback;