f25bfee4102d2cede5a1702f165c304e5e597352
[koha-equinox.git] / t / db_dependent / Koha / Account / Lines.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 => 7;
23 use Test::Exception;
24
25 use C4::Circulation qw/AddIssue AddReturn/;
26 use Koha::Account;
27 use Koha::Account::Lines;
28 use Koha::Account::Offsets;
29 use Koha::Items;
30
31 use t::lib::Mocks;
32 use t::lib::TestBuilder;
33
34 my $schema = Koha::Database->new->schema;
35 my $builder = t::lib::TestBuilder->new;
36
37 subtest 'item() tests' => sub {
38
39     plan tests => 3;
40
41     $schema->storage->txn_begin;
42
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(
47     {
48         biblionumber     => $biblioitem->{biblionumber},
49         biblioitemnumber => $biblioitem->{biblioitemnumber},
50         homebranch       => $library->{branchcode},
51         holdingbranch    => $library->{branchcode},
52         barcode          => 'some_barcode_12',
53         itype            => 'BK',
54     })->store;
55
56     my $line = Koha::Account::Line->new(
57     {
58         borrowernumber => $patron->{borrowernumber},
59         itemnumber     => $item->itemnumber,
60         accounttype    => "F",
61         amount         => 10,
62         interface      => 'commandline',
63     })->store;
64
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' );
68
69     $line->itemnumber(undef)->store;
70     is( $line->item, undef, 'Koha::Account::Line->item should return undef if no item linked' );
71
72     $schema->storage->txn_rollback;
73 };
74
75 subtest 'total_outstanding() tests' => sub {
76
77     plan tests => 5;
78
79     $schema->storage->txn_begin;
80
81     my $patron  = $builder->build_object({ class => 'Koha::Patrons' });
82
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)' );
85
86     my $debit_1 = Koha::Account::Line->new(
87         {   borrowernumber    => $patron->id,
88             accounttype       => "F",
89             amount            => 10,
90             amountoutstanding => 10,
91             interface         => 'commandline',
92         }
93     )->store;
94
95     my $debit_2 = Koha::Account::Line->new(
96         {   borrowernumber    => $patron->id,
97             accounttype       => "F",
98             amount            => 10,
99             amountoutstanding => 10,
100             interface         => 'commandline',
101         }
102     )->store;
103
104     $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
105     is( $lines->total_outstanding, 20, 'total_outstanding sums correctly' );
106
107     my $credit_1 = Koha::Account::Line->new(
108         {   borrowernumber    => $patron->id,
109             accounttype       => "F",
110             amount            => -10,
111             amountoutstanding => -10,
112             interface         => 'commandline',
113         }
114     )->store;
115
116     $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
117     is( $lines->total_outstanding, 10, 'total_outstanding sums correctly' );
118
119     my $credit_2 = Koha::Account::Line->new(
120         {   borrowernumber    => $patron->id,
121             accounttype       => "F",
122             amount            => -10,
123             amountoutstanding => -10,
124             interface         => 'commandline',
125         }
126     )->store;
127
128     $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
129     is( $lines->total_outstanding, 0, 'total_outstanding sums correctly' );
130
131     my $credit_3 = Koha::Account::Line->new(
132         {   borrowernumber    => $patron->id,
133             accounttype       => "F",
134             amount            => -100,
135             amountoutstanding => -100,
136             interface         => 'commandline',
137         }
138     )->store;
139
140     $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
141     is( $lines->total_outstanding, -100, 'total_outstanding sums correctly' );
142
143     $schema->storage->txn_rollback;
144 };
145
146 subtest 'is_credit() and is_debit() tests' => sub {
147
148     plan tests => 4;
149
150     $schema->storage->txn_begin;
151
152     my $patron  = $builder->build_object({ class => 'Koha::Patrons' });
153     my $account = $patron->account;
154
155     my $credit = $account->add_credit({ amount => 100, user_id => $patron->id, interface => 'commandline' });
156
157     ok( $credit->is_credit, 'is_credit detects credits' );
158     ok( !$credit->is_debit, 'is_debit detects credits' );
159
160     my $debit = Koha::Account::Line->new(
161     {
162         borrowernumber => $patron->id,
163         accounttype    => "F",
164         amount         => 10,
165         interface      => 'commandline',
166     })->store;
167
168     ok( !$debit->is_credit, 'is_credit detects debits' );
169     ok( $debit->is_debit, 'is_debit detects debits');
170
171     $schema->storage->txn_rollback;
172 };
173
174 subtest 'apply() tests' => sub {
175
176     plan tests => 24;
177
178     $schema->storage->txn_begin;
179
180     my $patron  = $builder->build_object( { class => 'Koha::Patrons' } );
181     my $account = $patron->account;
182
183     my $credit = $account->add_credit( { amount => 100, user_id => $patron->id, interface => 'commandline' } );
184
185     my $debit_1 = Koha::Account::Line->new(
186         {   borrowernumber    => $patron->id,
187             accounttype       => "F",
188             amount            => 10,
189             amountoutstanding => 10,
190             interface         => 'commandline',
191         }
192     )->store;
193
194     my $debit_2 = Koha::Account::Line->new(
195         {   borrowernumber    => $patron->id,
196             accounttype       => "F",
197             amount            => 100,
198             amountoutstanding => 100,
199             interface         => 'commandline',
200         }
201     )->store;
202
203     $credit->discard_changes;
204     $debit_1->discard_changes;
205
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' );
211
212     # re-read debit info
213     $debit_1->discard_changes;
214     is( $debit_1->amountoutstanding * 1, 0, 'Debit has been cancelled' );
215
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' );
221
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' );
229
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' );
235
236     $debits = Koha::Account::Lines->search({ accountlines_id => $debit_1->id });
237     throws_ok
238         { $credit->apply({ debits => $debits }); }
239         'Koha::Exceptions::Account::NoAvailableCredit',
240         '->apply() can only be used with outstanding credits';
241
242     $debits = Koha::Account::Lines->search({ accountlines_id => $credit->id });
243     throws_ok
244         { $debit_1->apply({ debits => $debits }); }
245         'Koha::Exceptions::Account::IsNotCredit',
246         '->apply() can only be used with credits';
247
248     $debits = Koha::Account::Lines->search({ accountlines_id => $credit->id });
249     my $credit_3 = $account->add_credit({ amount => 1, interface => 'commandline' });
250     throws_ok
251         { $credit_3->apply({ debits => $debits }); }
252         'Koha::Exceptions::Account::IsNotDebit',
253         '->apply() can only be applied to credits';
254
255     my $credit_2 = $account->add_credit({ amount => 20, interface => 'commandline' });
256     my $debit_3  = Koha::Account::Line->new(
257         {   borrowernumber    => $patron->id,
258             accounttype       => "F",
259             amount            => 100,
260             amountoutstanding => 100,
261             interface         => 'commandline',
262         }
263     )->store;
264
265     $debits = Koha::Account::Lines->search({ accountlines_id => { -in => [ $debit_1->id, $debit_2->id, $debit_3->id, $credit->id ] } });
266     throws_ok {
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';
270
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' );
275
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' } );
278
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' );
283
284     $schema->storage->txn_rollback;
285 };
286
287 subtest 'Keep account info when related patron, staff or item is deleted' => sub {
288
289     plan tests => 3;
290
291     $schema->storage->txn_begin;
292
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(
297         {
298             class => 'Koha::Checkouts',
299             value => { itemnumber => $item->itemnumber }
300         }
301     );
302     my $line = Koha::Account::Line->new(
303     {
304         borrowernumber => $patron->borrowernumber,
305         manager_id     => $staff->borrowernumber,
306         itemnumber     => $item->itemnumber,
307         accounttype    => "F",
308         amount         => 10,
309         interface      => 'commandline',
310     })->store;
311
312     $issue->delete;
313     $item->delete;
314     $line = $line->get_from_storage;
315     is( $line->itemnumber, undef, "The account line should not be deleted when the related item is delete");
316
317     $staff->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");
320
321     $patron->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");
324
325     $schema->storage->txn_rollback;
326 };
327
328 subtest 'adjust() tests' => sub {
329
330     plan tests => 29;
331
332     $schema->storage->txn_begin;
333
334     # count logs before any actions
335     my $action_logs = $schema->resultset('ActionLog')->search()->count;
336
337     # Disable logs
338     t::lib::Mocks::mock_preference( 'FinesLog', 0 );
339
340     my $patron  = $builder->build_object( { class => 'Koha::Patrons' } );
341     my $account = $patron->account;
342
343     my $debit_1 = Koha::Account::Line->new(
344         {   borrowernumber    => $patron->id,
345             accounttype       => "F",
346             amount            => 10,
347             amountoutstanding => 10,
348             interface         => 'commandline',
349         }
350     )->store;
351
352     my $debit_2 = Koha::Account::Line->new(
353         {   borrowernumber    => $patron->id,
354             accounttype       => "FU",
355             amount            => 100,
356             amountoutstanding => 100,
357             interface         => 'commandline'
358         }
359     )->store;
360
361     my $credit = $account->add_credit( { amount => 40, user_id => $patron->id, interface => 'commandline' } );
362
363     throws_ok { $debit_1->adjust( { amount => 50, type => 'bad', interface => 'commandline' } ) }
364     qr/Update type not recognised/, 'Exception thrown for unrecognised type';
365
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';
369
370     # Increment an unpaid fine
371     $debit_2->adjust( { amount => 150, type => 'fine_update', interface => 'commandline' } )->discard_changes;
372
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' );
376
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' );
382
383     is( $schema->resultset('ActionLog')->count(), $action_logs + 0, 'No log was added' );
384
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' } );
388
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' );
392
393     # Enable logs
394     t::lib::Mocks::mock_preference( 'FinesLog', 1 );
395
396     # Increment the partially paid fine
397     $debit_2->adjust( { amount => 160, type => 'fine_update', interface => 'commandline' } )->discard_changes;
398
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' );
401
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' );
407
408     is( $schema->resultset('ActionLog')->count(), $action_logs + 1, 'Log was added' );
409
410     # Decrement the partially paid fine, less than what was paid
411     $debit_2->adjust( { amount => 50, type => 'fine_update', interface => 'commandline' } )->discard_changes;
412
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' );
415
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' );
421
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)' );
426
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' );
432
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' );
436
437     $schema->storage->txn_rollback;
438 };
439
440 subtest 'checkout() tests' => sub {
441     plan tests => 6;
442
443     $schema->storage->txn_begin;
444
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;
449
450     t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode });
451     my $checkout = AddIssue( $patron->unblessed, $item->barcode );
452
453     my $line = $account->add_debit({
454         amount    => 10,
455         interface => 'commandline',
456         item_id   => $item->itemnumber,
457         issue_id  => $checkout->issue_id,
458         type      => 'fine',
459     });
460
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');
464
465     my ( $returned, undef, $old_checkout) = C4::Circulation::AddReturn( $item->barcode, $library->branchcode );
466     is( $returned, 1, 'The item should have been returned' );
467
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' );
472
473     $line->issue_id(undef)->store;
474     is( $line->checkout, undef, 'Koha::Account::Line->checkout should return undef if no checkout linked' );
475
476     $schema->storage->txn_rollback;
477 };
478
479 1;