3 # Copyright 2015 BibLibre
5 # This file is part of Koha.
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
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.
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>.
21 use Test::More tests => 30;
25 use t::lib::TestBuilder;
29 use Koha::Account::Lines;
30 use Koha::Account::Offsets;
31 use Koha::DateUtils qw( dt_from_string );
34 use_ok('C4::Accounts');
35 use_ok('Koha::Object');
36 use_ok('Koha::Patron');
37 use_ok('Data::Dumper');
40 can_ok( 'C4::Accounts',
50 purge_zero_balance_fees )
53 my $schema = Koha::Database->new->schema;
54 $schema->storage->txn_begin;
55 my $dbh = C4::Context->dbh;
57 my $builder = t::lib::TestBuilder->new;
58 my $library = $builder->build( { source => 'Branch' } );
60 $dbh->do(q|DELETE FROM accountlines|);
61 $dbh->do(q|DELETE FROM issues|);
62 $dbh->do(q|DELETE FROM borrowers|);
64 my $branchcode = $library->{branchcode};
67 my $context = new Test::MockModule('C4::Context');
68 $context->mock( 'userenv', sub {
72 branch => $branchcode,
76 # Testing purge_zero_balance_fees
78 # The 3rd value in the insert is 'days ago' --
83 my $sth = $dbh->prepare(
84 "INSERT INTO accountlines (
90 VALUES ( ?, ?, (select date_sub(CURRENT_DATE, INTERVAL ? DAY) ), ? )"
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 }
108 my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
109 my $borrower = Koha::Patron->new( { firstname => 'Test', surname => 'Patron', categorycode => $categorycode, branchcode => $branchcode } )->store();
111 for my $data ( @test_data ) {
112 $sth->execute($borrower->borrowernumber, $data->{amount}, $data->{days_ago}, $data->{description});
115 purge_zero_balance_fees( $days );
117 $sth = $dbh->prepare(
118 "select count(*) = 0 as deleted
120 where description = ?"
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 )
132 for my $data (@test_data) {
133 is_delete_correct( $data->{delete}, $data->{description});
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' );
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' );
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' );
167 $dbh->do(q|DELETE FROM accountlines|);
169 subtest "Koha::Account::pay tests" => sub {
174 my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
175 my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode };
177 my $borrower = Koha::Patron->new( {
178 cardnumber => '1234567890',
180 firstname => 'Marty',
182 $borrower->categorycode( $categorycode );
183 $borrower->branchcode( $branchcode );
186 my $account = Koha::Account->new({ patron_id => $borrower->id });
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();
191 $sth = $dbh->prepare("SELECT count(*) FROM accountlines");
193 my $count = $sth->fetchrow_array;
194 is($count, 2, 'There is 2 lines as expected');
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);
200 for my $line ( @$amountoutstanding ) {
201 $amountleft += $line;
203 is($amountleft, 300, 'The account has 300$ as expected' );
205 # We make a $20 payment
206 my $borrowernumber = $borrower->borrowernumber;
208 my $payment_note = '$20.00 payment note';
209 $account->pay( { amount => $data, note => $payment_note } );
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);
215 for my $line ( @$amountoutstanding ) {
216 $amountleft += $line;
218 is($amountleft, 280, 'The account has $280 as expected' );
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');
226 # We make a -$30 payment (a NEGATIVE payment)
228 $payment_note = '-$30.00 payment note';
229 $account->pay( { amount => $data, note => $payment_note } );
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);
235 for my $line ( @$amountoutstanding ) {
236 $amountleft += $line;
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');
245 #We make a $150 payment ( > 1stLine )
247 $payment_note = '$150.00 payment note';
248 $account->pay( { amount => $data, note => $payment_note } );
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);
254 for my $line ( @$amountoutstanding ) {
255 $amountleft += $line;
257 is($amountleft, 160, 'The account has $160 as expected' );
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');
265 #We make a $200 payment ( > amountleft )
267 $payment_note = '$200.00 payment note';
268 $account->pay( { amount => $data, note => $payment_note } );
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);
274 for my $line ( @$amountoutstanding ) {
275 $amountleft += $line;
277 is($amountleft, -40, 'The account has -$40 as expected, (credit situation)' );
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');
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" );
293 subtest "Koha::Account::pay particular line tests" => sub {
298 my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
299 my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode };
301 my $borrower = Koha::Patron->new( {
302 cardnumber => 'kylemhall',
306 $borrower->categorycode( $categorycode );
307 $borrower->branchcode( $branchcode );
310 my $account = Koha::Account->new({ patron_id => $borrower->id });
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();
317 is( $account->balance(), 10, "Account balance is 10" );
321 lines => [$line2, $line3, $line4],
326 $_->_result->discard_changes foreach ( $line1, $line2, $line3, $line4 );
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" );
338 subtest "Koha::Account::pay writeoff tests" => sub {
343 my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
344 my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode };
346 my $borrower = Koha::Patron->new( {
347 cardnumber => 'chelseahall',
349 firstname => 'Chelsea',
351 $borrower->categorycode( $categorycode );
352 $borrower->branchcode( $branchcode );
355 my $account = Koha::Account->new({ patron_id => $borrower->id });
357 my $line = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amountoutstanding => 42 })->store();
359 is( $account->balance(), 42, "Account balance is 42" );
361 my $id = $account->pay(
369 $line->_result->discard_changes();
371 is( $line->amountoutstanding, "0.000000", "Line was written off" );
373 my $writeoff = Koha::Account::Lines->find( $id );
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' );
380 subtest "More Koha::Account::pay tests" => sub {
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 };
395 my $accountline = $builder->build({ source => 'Accountline',
396 value => { borrowernumber => $borrowernumber,
398 amountoutstanding => $amount }
401 my $rs = $schema->resultset('Accountline')->search({
402 borrowernumber => $borrowernumber
405 is( $rs->count(), 1, 'Accountline created' );
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' });
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' );
416 my $stat = $schema->resultset('Statistic')->search({
419 }, { order_by => { -desc => 'datetime' } })->next();
421 ok( defined $stat, "There's a payment log that matches the branch" );
424 skip "No statistic logged", 4 unless defined $stat;
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" );
433 subtest "Even more Koha::Account::pay tests" => sub {
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 };
448 my $partialamount = 60;
449 my $accountline = $builder->build({ source => 'Accountline',
450 value => { borrowernumber => $borrowernumber,
452 amountoutstanding => $amount }
455 my $rs = $schema->resultset('Accountline')->search({
456 borrowernumber => $borrowernumber
459 is( $rs->count(), 1, 'Accountline created' );
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' });
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' );
470 my $stat = $schema->resultset('Statistic')->search({
473 }, { order_by => { -desc => 'datetime' } })->next();
475 ok( defined $stat, "There's a payment log that matches the branch" );
478 skip "No statistic logged", 4 unless defined $stat;
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" );
487 subtest 'balance' => sub {
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' );
495 my $accountline_1 = $builder->build(
497 source => 'Accountline',
499 borrowernumber => $patron->borrowernumber,
501 amountoutstanding => 42
505 my $accountline_2 = $builder->build(
507 source => 'Accountline',
509 borrowernumber => $patron->borrowernumber,
511 amountoutstanding => -13
516 my $balance = $patron->account->balance;
517 is( int($balance), 29, 'balance should return the correct value');
522 subtest "Koha::Account::chargelostitem tests" => sub {
528 my $itype_no_replace_no_fee = $builder->build({ source => 'Itemtype', value => {
530 defaultreplacecost => undef,
533 my $itype_replace_no_fee = $builder->build({ source => 'Itemtype', value => {
535 defaultreplacecost => 16.32,
538 my $itype_no_replace_fee = $builder->build({ source => 'Itemtype', value => {
540 defaultreplacecost => undef,
543 my $itype_replace_fee = $builder->build({ source => 'Itemtype', value => {
545 defaultreplacecost => 4.08,
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});
555 t::lib::Mocks::mock_preference('item-level_itypes', '1');
556 t::lib::Mocks::mock_preference('useDefaultReplacementCost', '0');
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");
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");
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");
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");
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");
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");
610 t::lib::Mocks::mock_preference('useDefaultReplacementCost', '1');
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");
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");
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");
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");
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");
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");
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");
661 subtest "Koha::Account::non_issues_charges tests" => sub {
664 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
666 my $today = dt_from_string;
670 Koha::Account::Line->new(
672 borrowernumber => $patron->borrowernumber,
675 description => 'a Res fee',
676 accounttype => 'Res',
677 amountoutstanding => $res,
680 Koha::Account::Line->new(
682 borrowernumber => $patron->borrowernumber,
685 description => 'a Rental fee',
686 accounttype => 'Rent',
687 amountoutstanding => $rent,
690 Koha::Account::Line->new(
692 borrowernumber => $patron->borrowernumber,
695 description => 'a Manual invoice fee',
696 accounttype => 'Copie',
697 amountoutstanding => $manual,
700 Koha::AuthorisedValue->new(
702 category => 'MANUAL_INV',
703 authorised_value => 'Copie',
704 lib => 'Fee for copie',
708 my $account = $patron->account;
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;
717 $res + $rent + $manual,
718 'Total charges should be Res + Rent + Manual'
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' );
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;
731 $res + $rent + $manual,
732 'Total charges should be Res + Rent + Manual'
734 is( $non_issues_charges, $manual,
735 'If 0|0|1 Only Manual should be a non issue charge' );
739 'If 0|0|1 Res + Rent should be other charges'
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;
749 $res + $rent + $manual,
750 'Total charges should be Res + Rent + Manual'
752 is( $non_issues_charges, $rent,
753 'If 0|1|0 Only Rental should be a non issue charge' );
757 'If 0|1|0 Rent + Manual should be other charges'
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;
767 $res + $rent + $manual,
768 'Total charges should be Res + Rent + Manual'
773 'If 0|1|1 Rent + Manual should be non issues charges'
775 is( $other_charges, $res, 'If 0|1|1 there should only have other charges' );
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;
784 $res + $rent + $manual,
785 'Total charges should be Res + Rent + Manual'
787 is( $non_issues_charges, $res,
788 'If 1|0|0 Only Res should be non issues charges' );
792 'If 1|0|0 Rent + Manual should be other charges'
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;
802 $res + $rent + $manual,
803 'Total charges should be Res + Rent + Manual'
808 'If 1|1|0 Res + Rent should be non issues charges'
810 is( $other_charges, $manual,
811 'If 1|1|0 Only Manual should be other charges' );
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;
820 $res + $rent + $manual,
821 'Total charges should be Res + Rent + Manual'
825 $res + $rent + $manual,
826 'If 1|1|1 Res + Rent + Manual should be non issues charges'
828 is( $other_charges, 0, 'If 1|1|1 there should not have any other charges' );