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