Bug 12001: Add tests
[koha.git] / t / db_dependent / Accounts.t
1 #!/usr/bin/perl
2
3 # Copyright 2015 BibLibre
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 3 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, see <http://www.gnu.org/licenses>.
18
19 use Modern::Perl;
20
21 use Test::More tests => 24;
22 use Test::MockModule;
23 use Test::Warn;
24
25 use t::lib::TestBuilder;
26 use t::lib::Mocks;
27
28 use C4::Members;
29 use Koha::Account;
30 use Koha::Account::Lines;
31 use Koha::Account::Offsets;
32 use Koha::DateUtils qw( dt_from_string );
33
34 BEGIN {
35     use_ok('C4::Accounts');
36     use_ok('Koha::Object');
37     use_ok('Koha::Patron');
38     use_ok('Data::Dumper');
39 }
40
41 can_ok( 'C4::Accounts',
42     qw(
43         getnextacctno
44         chargelostitem
45         manualinvoice
46         getcharges
47         ModNote
48         getcredits
49         getrefunds
50         ReversePayment
51         purge_zero_balance_fees )
52 );
53
54 my $schema  = Koha::Database->new->schema;
55 $schema->storage->txn_begin;
56 my $dbh = C4::Context->dbh;
57
58 my $builder = t::lib::TestBuilder->new;
59 my $library = $builder->build( { source => 'Branch' } );
60
61 $dbh->do(q|DELETE FROM accountlines|);
62 $dbh->do(q|DELETE FROM issues|);
63 $dbh->do(q|DELETE FROM borrowers|);
64
65 my $branchcode = $library->{branchcode};
66 my $borrower_number;
67
68 my $context = new Test::MockModule('C4::Context');
69 $context->mock( 'userenv', sub {
70     return {
71         flags  => 1,
72         id     => 'my_userid',
73         branch => $branchcode,
74     };
75 });
76
77 # Testing purge_zero_balance_fees
78
79 # The 3rd value in the insert is 'days ago' --
80 # 0 => today
81 # 1 => yesterday
82 # etc.
83
84 my $sth = $dbh->prepare(
85     "INSERT INTO accountlines (
86          borrowernumber,
87          amountoutstanding,
88          date,
89          description
90      )
91      VALUES ( ?, ?, (select date_sub(CURRENT_DATE, INTERVAL ? DAY) ), ? )"
92 );
93
94 my $days = 5;
95
96 my @test_data = (
97     { amount => 0     , days_ago => 0         , description =>'purge_zero_balance_fees should not delete 0 balance fees with date today'                     , delete => 0 } ,
98     { amount => 0     , days_ago => $days - 1 , description =>'purge_zero_balance_fees should not delete 0 balance fees with date before threshold day'      , delete => 0 } ,
99     { amount => 0     , days_ago => $days     , description =>'purge_zero_balance_fees should not delete 0 balance fees with date on threshold day'          , delete => 0 } ,
100     { amount => 0     , days_ago => $days + 1 , description =>'purge_zero_balance_fees should delete 0 balance fees with date after threshold day'           , delete => 1 } ,
101     { amount => undef , days_ago => $days + 1 , description =>'purge_zero_balance_fees should delete NULL balance fees with date after threshold day'        , delete => 1 } ,
102     { amount => 5     , days_ago => $days - 1 , description =>'purge_zero_balance_fees should not delete fees with positive amout owed before threshold day'  , delete => 0 } ,
103     { amount => 5     , days_ago => $days     , description =>'purge_zero_balance_fees should not delete fees with positive amout owed on threshold day'      , delete => 0 } ,
104     { amount => 5     , days_ago => $days + 1 , description =>'purge_zero_balance_fees should not delete fees with positive amout owed after threshold day'   , delete => 0 } ,
105     { amount => -5    , days_ago => $days - 1 , description =>'purge_zero_balance_fees should not delete fees with negative amout owed before threshold day' , delete => 0 } ,
106     { amount => -5    , days_ago => $days     , description =>'purge_zero_balance_fees should not delete fees with negative amout owed on threshold day'     , delete => 0 } ,
107     { amount => -5    , days_ago => $days + 1 , description =>'purge_zero_balance_fees should not delete fees with negative amout owed after threshold day'  , delete => 0 }
108 );
109 my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
110 my $borrower = Koha::Patron->new( { firstname => 'Test', surname => 'Patron', categorycode => $categorycode, branchcode => $branchcode } )->store();
111
112 for my $data ( @test_data ) {
113     $sth->execute($borrower->borrowernumber, $data->{amount}, $data->{days_ago}, $data->{description});
114 }
115
116 purge_zero_balance_fees( $days );
117
118 $sth = $dbh->prepare(
119             "select count(*) = 0 as deleted
120              from accountlines
121              where description = ?"
122        );
123
124 #
125 sub is_delete_correct {
126     my $should_delete = shift;
127     my $description = shift;
128     $sth->execute( $description );
129     my $test = $sth->fetchrow_hashref();
130     is( $test->{deleted}, $should_delete, $description )
131 }
132
133 for my $data  (@test_data) {
134     is_delete_correct( $data->{delete}, $data->{description});
135 }
136
137 $dbh->do(q|DELETE FROM accountlines|);
138
139 subtest "Koha::Account::pay tests" => sub {
140
141     plan tests => 12;
142
143     # Create a borrower
144     my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
145     my $branchcode   = $builder->build({ source => 'Branch' })->{ branchcode };
146
147     my $borrower = Koha::Patron->new( {
148         cardnumber => '1234567890',
149         surname => 'McFly',
150         firstname => 'Marty',
151     } );
152     $borrower->categorycode( $categorycode );
153     $borrower->branchcode( $branchcode );
154     $borrower->store;
155
156     my $account = Koha::Account->new({ patron_id => $borrower->id });
157
158     my $line1 = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amountoutstanding => 100 })->store();
159     my $line2 = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amountoutstanding => 200 })->store();
160
161     $sth = $dbh->prepare("SELECT count(*) FROM accountlines");
162     $sth->execute;
163     my $count = $sth->fetchrow_array;
164     is($count, 2, 'There is 2 lines as expected');
165
166     # There is $100 in the account
167     $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
168     my $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
169     my $amountleft = 0;
170     for my $line ( @$amountoutstanding ) {
171         $amountleft += $line;
172     }
173     is($amountleft, 300, 'The account has 300$ as expected' );
174
175     # We make a $20 payment
176     my $borrowernumber = $borrower->borrowernumber;
177     my $data = '20.00';
178     my $payment_note = '$20.00 payment note';
179     $account->pay( { amount => $data, note => $payment_note } );
180
181     # There is now $280 in the account
182     $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
183     $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
184     $amountleft = 0;
185     for my $line ( @$amountoutstanding ) {
186         $amountleft += $line;
187     }
188     is($amountleft, 280, 'The account has $280 as expected' );
189
190     # Is the payment note well registered
191     $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
192     $sth->execute($borrower->borrowernumber);
193     my $note = $sth->fetchrow_array;
194     is($note,'$20.00 payment note', '$20.00 payment note is registered');
195
196     # We make a -$30 payment (a NEGATIVE payment)
197     $data = '-30.00';
198     $payment_note = '-$30.00 payment note';
199     $account->pay( { amount => $data, note => $payment_note } );
200
201     # There is now $310 in the account
202     $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
203     $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
204     $amountleft = 0;
205     for my $line ( @$amountoutstanding ) {
206         $amountleft += $line;
207     }
208     is($amountleft, 310, 'The account has $310 as expected' );
209     # Is the payment note well registered
210     $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
211     $sth->execute($borrower->borrowernumber);
212     $note = $sth->fetchrow_array;
213     is($note,'-$30.00 payment note', '-$30.00 payment note is registered');
214
215     #We make a $150 payment ( > 1stLine )
216     $data = '150.00';
217     $payment_note = '$150.00 payment note';
218     $account->pay( { amount => $data, note => $payment_note } );
219
220     # There is now $160 in the account
221     $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
222     $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
223     $amountleft = 0;
224     for my $line ( @$amountoutstanding ) {
225         $amountleft += $line;
226     }
227     is($amountleft, 160, 'The account has $160 as expected' );
228
229     # Is the payment note well registered
230     $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
231     $sth->execute($borrower->borrowernumber);
232     $note = $sth->fetchrow_array;
233     is($note,'$150.00 payment note', '$150.00 payment note is registered');
234
235     #We make a $200 payment ( > amountleft )
236     $data = '200.00';
237     $payment_note = '$200.00 payment note';
238     $account->pay( { amount => $data, note => $payment_note } );
239
240     # There is now -$40 in the account
241     $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
242     $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
243     $amountleft = 0;
244     for my $line ( @$amountoutstanding ) {
245         $amountleft += $line;
246     }
247     is($amountleft, -40, 'The account has -$40 as expected, (credit situation)' );
248
249     # Is the payment note well registered
250     $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
251     $sth->execute($borrower->borrowernumber);
252     $note = $sth->fetchrow_array;
253     is($note,'$200.00 payment note', '$200.00 payment note is registered');
254
255     my $line3 = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amountoutstanding => 42, accounttype => 'TEST' })->store();
256     my $payment_id = $account->pay( { lines => [$line3], amount => 42 } );
257     my $payment = Koha::Account::Lines->find( $payment_id );
258     is( $payment->amount(), '-42.000000', "Payment paid the specified fine" );
259     $line3 = Koha::Account::Lines->find( $line3->id );
260     is( $line3->amountoutstanding, '0.000000', "Specified fine is paid" );
261 };
262
263 subtest "Koha::Account::pay particular line tests" => sub {
264
265     plan tests => 5;
266
267     # Create a borrower
268     my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
269     my $branchcode   = $builder->build({ source => 'Branch' })->{ branchcode };
270
271     my $borrower = Koha::Patron->new( {
272         cardnumber => 'kylemhall',
273         surname => 'Hall',
274         firstname => 'Kyle',
275     } );
276     $borrower->categorycode( $categorycode );
277     $borrower->branchcode( $branchcode );
278     $borrower->store;
279
280     my $account = Koha::Account->new({ patron_id => $borrower->id });
281
282     my $line1 = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amountoutstanding => 1 })->store();
283     my $line2 = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amountoutstanding => 2 })->store();
284     my $line3 = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amountoutstanding => 3 })->store();
285     my $line4 = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amountoutstanding => 4 })->store();
286
287     is( $account->balance(), "10.000000", "Account balance is 10" );
288
289     $account->pay(
290         {
291             lines => [$line2, $line3, $line4],
292             amount => 4,
293         }
294     );
295
296     $_->_result->discard_changes foreach ( $line1, $line2, $line3, $line4 );
297
298     # Line1 is not paid at all, as it was not passed in the lines param
299     is( $line1->amountoutstanding, "1.000000", "Line 1 was not paid" );
300     # Line2 was paid in full, as it was the first in the lines list
301     is( $line2->amountoutstanding, "0.000000", "Line 2 was paid in full" );
302     # Line3 was paid partially, as the remaining balance did not cover it entirely
303     is( $line3->amountoutstanding, "1.000000", "Line 3 was paid to 1.00" );
304     # Line4 was not paid at all, as the payment was all used up by that point
305     is( $line4->amountoutstanding, "4.000000", "Line 4 was not paid" );
306 };
307
308 subtest "Koha::Account::pay writeoff tests" => sub {
309
310     plan tests => 5;
311
312     # Create a borrower
313     my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
314     my $branchcode   = $builder->build({ source => 'Branch' })->{ branchcode };
315
316     my $borrower = Koha::Patron->new( {
317         cardnumber => 'chelseahall',
318         surname => 'Hall',
319         firstname => 'Chelsea',
320     } );
321     $borrower->categorycode( $categorycode );
322     $borrower->branchcode( $branchcode );
323     $borrower->store;
324
325     my $account = Koha::Account->new({ patron_id => $borrower->id });
326
327     my $line = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amountoutstanding => 42 })->store();
328
329     is( $account->balance(), "42.000000", "Account balance is 42" );
330
331     my $id = $account->pay(
332         {
333             lines  => [$line],
334             amount => 42,
335             type   => 'writeoff',
336         }
337     );
338
339     $line->_result->discard_changes();
340
341     is( $line->amountoutstanding, "0.000000", "Line was written off" );
342
343     my $writeoff = Koha::Account::Lines->find( $id );
344
345     is( $writeoff->accounttype, 'W', 'Type is correct' );
346     is( $writeoff->description, 'Writeoff', 'Description is correct' );
347     is( $writeoff->amount, '-42.000000', 'Amount is correct' );
348 };
349
350 subtest "More Koha::Account::pay tests" => sub {
351
352     plan tests => 8;
353
354     # Create a borrower
355     my $category   = $builder->build({ source => 'Category' })->{ categorycode };
356     my $branch     = $builder->build({ source => 'Branch' })->{ branchcode };
357     $branchcode = $branch;
358     my $borrowernumber = $builder->build({
359         source => 'Borrower',
360         value  => { categorycode => $category,
361                     branchcode   => $branch }
362     })->{ borrowernumber };
363
364     my $amount = 100;
365     my $accountline = $builder->build({ source => 'Accountline',
366         value  => { borrowernumber => $borrowernumber,
367                     amount => $amount,
368                     amountoutstanding => $amount }
369     });
370
371     my $rs = $schema->resultset('Accountline')->search({
372         borrowernumber => $borrowernumber
373     });
374
375     is( $rs->count(), 1, 'Accountline created' );
376
377     my $account = Koha::Account->new( { patron_id => $borrowernumber } );
378     my $line = Koha::Account::Lines->find( $accountline->{ accountlines_id } );
379     # make the full payment
380     $account->pay({ lines => [$line], amount => $amount, library_id => $branch, note => 'A payment note' });
381
382     my $offset = Koha::Account::Offsets->search({ debit_id => $accountline->{accountlines_id} })->next();
383     is( $offset->amount(), '-100.000000', 'Offset amount is -100.00' );
384     is( $offset->type(), 'Payment', 'Offset type is Payment' );
385
386     my $stat = $schema->resultset('Statistic')->search({
387         branch  => $branch,
388         type    => 'payment'
389     }, { order_by => { -desc => 'datetime' } })->next();
390
391     ok( defined $stat, "There's a payment log that matches the branch" );
392
393     SKIP: {
394         skip "No statistic logged", 4 unless defined $stat;
395
396         is( $stat->type, 'payment', "Correct statistic type" );
397         is( $stat->branch, $branch, "Correct branch logged to statistics" );
398         is( $stat->borrowernumber, $borrowernumber, "Correct borrowernumber logged to statistics" );
399         is( $stat->value, "$amount" . "\.0000", "Correct amount logged to statistics" );
400     }
401 };
402
403 subtest "Even more Koha::Account::pay tests" => sub {
404
405     plan tests => 8;
406
407     # Create a borrower
408     my $category   = $builder->build({ source => 'Category' })->{ categorycode };
409     my $branch     = $builder->build({ source => 'Branch' })->{ branchcode };
410     $branchcode = $branch;
411     my $borrowernumber = $builder->build({
412         source => 'Borrower',
413         value  => { categorycode => $category,
414                     branchcode   => $branch }
415     })->{ borrowernumber };
416
417     my $amount = 100;
418     my $partialamount = 60;
419     my $accountline = $builder->build({ source => 'Accountline',
420         value  => { borrowernumber => $borrowernumber,
421                     amount => $amount,
422                     amountoutstanding => $amount }
423     });
424
425     my $rs = $schema->resultset('Accountline')->search({
426         borrowernumber => $borrowernumber
427     });
428
429     is( $rs->count(), 1, 'Accountline created' );
430
431     my $account = Koha::Account->new( { patron_id => $borrowernumber } );
432     my $line = Koha::Account::Lines->find( $accountline->{ accountlines_id } );
433     # make the full payment
434     $account->pay({ lines => [$line], amount => $partialamount, library_id => $branch, note => 'A payment note' });
435
436     my $offset = Koha::Account::Offsets->search( { debit_id => $accountline->{ accountlines_id } } )->next();
437     is( $offset->amount, '-60.000000', 'Offset amount is -60.00' );
438     is( $offset->type, 'Payment', 'Offset type is payment' );
439
440     my $stat = $schema->resultset('Statistic')->search({
441         branch  => $branch,
442         type    => 'payment'
443     }, { order_by => { -desc => 'datetime' } })->next();
444
445     ok( defined $stat, "There's a payment log that matches the branch" );
446
447     SKIP: {
448         skip "No statistic logged", 4 unless defined $stat;
449
450         is( $stat->type, 'payment', "Correct statistic type" );
451         is( $stat->branch, $branch, "Correct branch logged to statistics" );
452         is( $stat->borrowernumber, $borrowernumber, "Correct borrowernumber logged to statistics" );
453         is( $stat->value, "$partialamount" . "\.0000", "Correct amount logged to statistics" );
454     }
455 };
456
457 subtest 'balance' => sub {
458     plan tests => 2;
459
460     my $patron = $builder->build({source => 'Borrower'});
461     $patron = Koha::Patrons->find( $patron->{borrowernumber} );
462     my $account = $patron->account;
463     is( $account->balance, 0, 'balance should return 0 if the patron does not have fines' );
464
465     my $accountline_1 = $builder->build(
466         {
467             source => 'Accountline',
468             value  => {
469                 borrowernumber    => $patron->borrowernumber,
470                 amount            => 42,
471                 amountoutstanding => 42
472             }
473         }
474     );
475     my $accountline_2 = $builder->build(
476         {
477             source => 'Accountline',
478             value  => {
479                 borrowernumber    => $patron->borrowernumber,
480                 amount            => -13,
481                 amountoutstanding => -13
482             }
483         }
484     );
485
486     my $balance = $patron->account->balance;
487     is( int($balance), 29, 'balance should return the correct value');
488
489     $patron->delete;
490 };
491
492 subtest "Koha::Account::chargelostitem tests" => sub {
493     plan tests => 32;
494
495     my $lostfine;
496     my $procfee;
497
498     my $itype_no_replace_no_fee = $builder->build({ source => 'Itemtype', value => {
499             rentalcharge => 0,
500             defaultreplacecost => undef,
501             processfee => undef,
502     }});
503     my $itype_replace_no_fee = $builder->build({ source => 'Itemtype', value => {
504             rentalcharge => 0,
505             defaultreplacecost => 16.32,
506             processfee => undef,
507     }});
508     my $itype_no_replace_fee = $builder->build({ source => 'Itemtype', value => {
509             rentalcharge => 0,
510             defaultreplacecost => undef,
511             processfee => 8.16,
512     }});
513     my $itype_replace_fee = $builder->build({ source => 'Itemtype', value => {
514             rentalcharge => 0,
515             defaultreplacecost => 4.08,
516             processfee => 2.04,
517     }});
518     my $cli_borrowernumber = $builder->build({ source => 'Borrower' })->{'borrowernumber'};
519     my $cli_itemnumber1 = $builder->build({ source => 'Item', value => { itype => $itype_no_replace_no_fee->{itemtype} } })->{'itemnumber'};
520     my $cli_itemnumber2 = $builder->build({ source => 'Item', value => { itype => $itype_replace_no_fee->{itemtype} } })->{'itemnumber'};
521     my $cli_itemnumber3 = $builder->build({ source => 'Item', value => { itype => $itype_no_replace_fee->{itemtype} } })->{'itemnumber'};
522     my $cli_itemnumber4 = $builder->build({ source => 'Item', value => { itype => $itype_replace_fee->{itemtype} } })->{'itemnumber'};
523     my $duck = Koha::Items->find({itemnumber=>$cli_itemnumber1});
524
525     t::lib::Mocks::mock_preference('item-level_itypes', '1');
526     t::lib::Mocks::mock_preference('useDefaultReplacementCost', '0');
527
528     C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber1, 0, "Perdedor");
529     $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, accounttype => 'L' });
530     $procfee  = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, accounttype => 'PF' });
531     ok( !$lostfine, "No lost fine if no replacementcost or default when pref off");
532     ok( !$procfee,  "No processing fee if no processing fee");
533     C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber1, 6.12, "Perdedor");
534     $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, accounttype => 'L' });
535     $procfee  = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, accounttype => 'PF' });
536     ok( $lostfine->amount == 6.12, "Lost fine equals replacementcost when pref off and no default set");
537     ok( !$procfee,  "No processing fee if no processing fee");
538     $lostfine->delete();
539
540     C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber2, 0, "Perdedor");
541     $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, accounttype => 'L' });
542     $procfee  = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, accounttype => 'PF' });
543     ok( !$lostfine, "No lost fine if no replacementcost but default set when pref off");
544     ok( !$procfee,  "No processing fee if no processing fee");
545     C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber2, 6.12, "Perdedor");
546     $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, accounttype => 'L' });
547     $procfee  = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, accounttype => 'PF' });
548     ok( $lostfine->amount == 6.12 , "Lost fine equals replacementcost when pref off and default set");
549     ok( !$procfee,  "No processing fee if no processing fee");
550     $lostfine->delete();
551
552     C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber3, 0, "Perdedor");
553     $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, accounttype => 'L' });
554     $procfee  = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, accounttype => 'PF' });
555     ok( !$lostfine, "No lost fine if no replacementcost and no default set when pref off");
556     ok( $procfee->amount == 8.16,  "Processing fee if processing fee");
557     $procfee->delete();
558     C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber3, 6.12, "Perdedor");
559     $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, accounttype => 'L' });
560     $procfee  = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, accounttype => 'PF' });
561     ok( $lostfine->amount == 6.12 , "Lost fine equals replacementcost when pref off and no default set");
562     ok( $procfee->amount == 8.16,  "Processing fee if processing fee");
563     $lostfine->delete();
564     $procfee->delete();
565
566     C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 0, "Perdedor");
567     $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'L' });
568     $procfee  = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'PF' });
569     ok( !$lostfine, "No lost fine if no replacementcost but default set when pref off");
570     ok( $procfee->amount == 2.04,  "Processing fee if processing fee");
571     $procfee->delete();
572     C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 6.12, "Perdedor");
573     $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'L' });
574     $procfee  = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'PF' });
575     ok( $lostfine->amount == 6.12 , "Lost fine equals replacementcost when pref off and default set");
576     ok( $procfee->amount == 2.04,  "Processing fee if processing fee");
577     $lostfine->delete();
578     $procfee->delete();
579
580     t::lib::Mocks::mock_preference('useDefaultReplacementCost', '1');
581
582     C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber1, 0, "Perdedor");
583     $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, accounttype => 'L' });
584     $procfee  = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, accounttype => 'PF' });
585     ok( !$lostfine, "No lost fine if no replacementcost or default when pref on");
586     ok( !$procfee,  "No processing fee if no processing fee");
587     C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber1, 6.12, "Perdedor");
588     $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, accounttype => 'L' });
589     $procfee  = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, accounttype => 'PF' });
590     is( $lostfine->amount, "6.120000", "Lost fine equals replacementcost when pref on and no default set");
591     ok( !$procfee,  "No processing fee if no processing fee");
592
593     C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber2, 0, "Perdedor");
594     $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, accounttype => 'L' });
595     $procfee  = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, accounttype => 'PF' });
596     is( $lostfine->amount(), "16.320000", "Lost fine is default if no replacementcost but default set when pref on");
597     ok( !$procfee,  "No processing fee if no processing fee");
598     $lostfine->delete();
599     C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber2, 6.12, "Perdedor");
600     $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, accounttype => 'L' });
601     $procfee  = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, accounttype => 'PF' });
602     is( $lostfine->amount, "6.120000" , "Lost fine equals replacementcost when pref on and default set");
603     ok( !$procfee,  "No processing fee if no processing fee");
604
605     C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber3, 0, "Perdedor");
606     $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, accounttype => 'L' });
607     $procfee  = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, accounttype => 'PF' });
608     ok( !$lostfine, "No lost fine if no replacementcost and default not set when pref on");
609     is( $procfee->amount, "8.160000",  "Processing fee if processing fee");
610     $procfee->delete();
611     C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber3, 6.12, "Perdedor");
612     $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, accounttype => 'L' });
613     $procfee  = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, accounttype => 'PF' });
614     is( $lostfine->amount, "6.120000", "Lost fine equals replacementcost when pref on and no default set");
615     is( $procfee->amount, "8.160000",  "Processing fee if processing fee");
616
617     C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 0, "Perdedor");
618     $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'L' });
619     $procfee  = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'PF' });
620     is( $lostfine->amount, "4.080000", "Lost fine is default if no replacementcost but default set when pref on");
621     is( $procfee->amount, "2.040000",  "Processing fee if processing fee");
622     $lostfine->delete();
623     $procfee->delete();
624     C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 6.12, "Perdedor");
625     $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'L' });
626     $procfee  = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'PF' });
627     is( $lostfine->amount, "6.120000", "Lost fine equals replacementcost when pref on and default set");
628     is( $procfee->amount, "2.040000",  "Processing fee if processing fee");
629 };
630
631 subtest "Koha::Account::non_issues_charges tests" => sub {
632     plan tests => 21;
633
634     my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
635
636     my $today  = dt_from_string;
637     my $res    = 3;
638     my $rent   = 5;
639     my $manual = 7;
640     Koha::Account::Line->new(
641         {
642             borrowernumber    => $patron->borrowernumber,
643             accountno         => 1,
644             date              => $today,
645             description       => 'a Res fee',
646             accounttype       => 'Res',
647             amountoutstanding => $res,
648         }
649     )->store;
650     Koha::Account::Line->new(
651         {
652             borrowernumber    => $patron->borrowernumber,
653             accountno         => 2,
654             date              => $today,
655             description       => 'a Rental fee',
656             accounttype       => 'Rent',
657             amountoutstanding => $rent,
658         }
659     )->store;
660     Koha::Account::Line->new(
661         {
662             borrowernumber    => $patron->borrowernumber,
663             accountno         => 3,
664             date              => $today,
665             description       => 'a Manual invoice fee',
666             accounttype       => 'Copie',
667             amountoutstanding => $manual,
668         }
669     )->store;
670     Koha::AuthorisedValue->new(
671         {
672             category         => 'MANUAL_INV',
673             authorised_value => 'Copie',
674             lib              => 'Fee for copie',
675         }
676     )->store;
677
678     t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge',   0 );
679     t::lib::Mocks::mock_preference( 'RentalsInNoissuesCharge', 0 );
680     t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge',  0 );
681     my ( $total, $non_issues_charges, $other_charges ) =
682       C4::Members::GetMemberAccountBalance( $patron->borrowernumber );
683     is(
684         $total,
685         $res + $rent + $manual,
686         'Total charges should be Res + Rent + Manual'
687     );
688     is( $non_issues_charges, 0,
689         'If 0|0|0 there should not have non issues charges' );
690     is( $other_charges, 15, 'If 0|0|0 there should only have other charges' );
691
692     t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge',   0 );
693     t::lib::Mocks::mock_preference( 'RentalsInNoissuesCharge', 0 );
694     t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge',  1 );
695     ( $total, $non_issues_charges, $other_charges ) =
696       C4::Members::GetMemberAccountBalance( $patron->borrowernumber );
697     is(
698         $total,
699         $res + $rent + $manual,
700         'Total charges should be Res + Rent + Manual'
701     );
702     is( $non_issues_charges, $manual,
703         'If 0|0|1 Only Manual should be a non issue charge' );
704     is(
705         $other_charges,
706         $res + $rent,
707         'If 0|0|1 Res + Rent should be other charges'
708     );
709
710     t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge',   0 );
711     t::lib::Mocks::mock_preference( 'RentalsInNoissuesCharge', 1 );
712     t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge',  0 );
713     ( $total, $non_issues_charges, $other_charges ) =
714       C4::Members::GetMemberAccountBalance( $patron->borrowernumber );
715     is(
716         $total,
717         $res + $rent + $manual,
718         'Total charges should be Res + Rent + Manual'
719     );
720     is( $non_issues_charges, $rent,
721         'If 0|1|0 Only Rental should be a non issue charge' );
722     is(
723         $other_charges,
724         $res + $manual,
725         'If 0|1|0 Rent + Manual should be other charges'
726     );
727
728     t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge',   0 );
729     t::lib::Mocks::mock_preference( 'RentalsInNoissuesCharge', 1 );
730     t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge',  1 );
731     ( $total, $non_issues_charges, $other_charges ) =
732       C4::Members::GetMemberAccountBalance( $patron->borrowernumber );
733     is(
734         $total,
735         $res + $rent + $manual,
736         'Total charges should be Res + Rent + Manual'
737     );
738     is(
739         $non_issues_charges,
740         $rent + $manual,
741         'If 0|1|1 Rent + Manual should be non issues charges'
742     );
743     is( $other_charges, $res, 'If 0|1|1 there should only have other charges' );
744
745     t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge',   1 );
746     t::lib::Mocks::mock_preference( 'RentalsInNoissuesCharge', 0 );
747     t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge',  0 );
748     ( $total, $non_issues_charges, $other_charges ) =
749       C4::Members::GetMemberAccountBalance( $patron->borrowernumber );
750     is(
751         $total,
752         $res + $rent + $manual,
753         'Total charges should be Res + Rent + Manual'
754     );
755     is( $non_issues_charges, $res,
756         'If 1|0|0 Only Res should be non issues charges' );
757     is(
758         $other_charges,
759         $rent + $manual,
760         'If 1|0|0 Rent + Manual should be other charges'
761     );
762
763     t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge',   1 );
764     t::lib::Mocks::mock_preference( 'RentalsInNoissuesCharge', 1 );
765     t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge',  0 );
766     ( $total, $non_issues_charges, $other_charges ) =
767       C4::Members::GetMemberAccountBalance( $patron->borrowernumber );
768     is(
769         $total,
770         $res + $rent + $manual,
771         'Total charges should be Res + Rent + Manual'
772     );
773     is(
774         $non_issues_charges,
775         $res + $rent,
776         'If 1|1|0 Res + Rent should be non issues charges'
777     );
778     is( $other_charges, $manual,
779         'If 1|1|0 Only Manual should be other charges' );
780
781     t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge',   1 );
782     t::lib::Mocks::mock_preference( 'RentalsInNoissuesCharge', 1 );
783     t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge',  1 );
784     ( $total, $non_issues_charges, $other_charges ) =
785       C4::Members::GetMemberAccountBalance( $patron->borrowernumber );
786     is(
787         $total,
788         $res + $rent + $manual,
789         'Total charges should be Res + Rent + Manual'
790     );
791     is(
792         $non_issues_charges,
793         $res + $rent + $manual,
794         'If 1|1|1 Res + Rent + Manual should be non issues charges'
795     );
796     is( $other_charges, 0, 'If 1|1|1 there should not have any other charges' );
797 };
798
799 1;