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