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 => 25;
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|);
138 subtest "Koha::Account::pay tests" => sub {
143 my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
144 my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode };
146 my $borrower = Koha::Patron->new( {
147 cardnumber => '1234567890',
149 firstname => 'Marty',
151 $borrower->categorycode( $categorycode );
152 $borrower->branchcode( $branchcode );
155 my $account = Koha::Account->new({ patron_id => $borrower->id });
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();
160 $sth = $dbh->prepare("SELECT count(*) FROM accountlines");
162 my $count = $sth->fetchrow_array;
163 is($count, 2, 'There is 2 lines as expected');
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);
169 for my $line ( @$amountoutstanding ) {
170 $amountleft += $line;
172 is($amountleft, 300, 'The account has 300$ as expected' );
174 # We make a $20 payment
175 my $borrowernumber = $borrower->borrowernumber;
177 my $payment_note = '$20.00 payment note';
178 my $id = $account->pay( { amount => $data, note => $payment_note, payment_type => "TEST_TYPE" } );
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" );
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);
187 for my $line ( @$amountoutstanding ) {
188 $amountleft += $line;
190 is($amountleft, 280, 'The account has $280 as expected' );
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');
198 # We make a -$30 payment (a NEGATIVE payment)
200 $payment_note = '-$30.00 payment note';
201 $account->pay( { amount => $data, note => $payment_note } );
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);
207 for my $line ( @$amountoutstanding ) {
208 $amountleft += $line;
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');
217 #We make a $150 payment ( > 1stLine )
219 $payment_note = '$150.00 payment note';
220 $account->pay( { amount => $data, note => $payment_note } );
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);
226 for my $line ( @$amountoutstanding ) {
227 $amountleft += $line;
229 is($amountleft, 160, 'The account has $160 as expected' );
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');
237 #We make a $200 payment ( > amountleft )
239 $payment_note = '$200.00 payment note';
240 $account->pay( { amount => $data, note => $payment_note } );
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);
246 for my $line ( @$amountoutstanding ) {
247 $amountleft += $line;
249 is($amountleft, -40, 'The account has -$40 as expected, (credit situation)' );
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');
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" );
265 subtest "Koha::Account::pay particular line tests" => sub {
270 my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
271 my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode };
273 my $borrower = Koha::Patron->new( {
274 cardnumber => 'kylemhall',
278 $borrower->categorycode( $categorycode );
279 $borrower->branchcode( $branchcode );
282 my $account = Koha::Account->new({ patron_id => $borrower->id });
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();
289 is( $account->balance(), 10, "Account balance is 10" );
293 lines => [$line2, $line3, $line4],
298 $_->_result->discard_changes foreach ( $line1, $line2, $line3, $line4 );
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" );
310 subtest "Koha::Account::pay writeoff tests" => sub {
315 my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
316 my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode };
318 my $borrower = Koha::Patron->new( {
319 cardnumber => 'chelseahall',
321 firstname => 'Chelsea',
323 $borrower->categorycode( $categorycode );
324 $borrower->branchcode( $branchcode );
327 my $account = Koha::Account->new({ patron_id => $borrower->id });
329 my $line = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amountoutstanding => 42 })->store();
331 is( $account->balance(), 42, "Account balance is 42" );
333 my $id = $account->pay(
341 $line->_result->discard_changes();
343 is( $line->amountoutstanding, "0.000000", "Line was written off" );
345 my $writeoff = Koha::Account::Lines->find( $id );
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' );
352 subtest "More Koha::Account::pay tests" => sub {
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 };
367 my $accountline = $builder->build({ source => 'Accountline',
368 value => { borrowernumber => $borrowernumber,
370 amountoutstanding => $amount }
373 my $rs = $schema->resultset('Accountline')->search({
374 borrowernumber => $borrowernumber
377 is( $rs->count(), 1, 'Accountline created' );
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' });
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' );
388 my $stat = $schema->resultset('Statistic')->search({
391 }, { order_by => { -desc => 'datetime' } })->next();
393 ok( defined $stat, "There's a payment log that matches the branch" );
396 skip "No statistic logged", 4 unless defined $stat;
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" );
405 subtest "Even more Koha::Account::pay tests" => sub {
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 };
420 my $partialamount = 60;
421 my $accountline = $builder->build({ source => 'Accountline',
422 value => { borrowernumber => $borrowernumber,
424 amountoutstanding => $amount }
427 my $rs = $schema->resultset('Accountline')->search({
428 borrowernumber => $borrowernumber
431 is( $rs->count(), 1, 'Accountline created' );
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' });
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' );
442 my $stat = $schema->resultset('Statistic')->search({
445 }, { order_by => { -desc => 'datetime' } })->next();
447 ok( defined $stat, "There's a payment log that matches the branch" );
450 skip "No statistic logged", 4 unless defined $stat;
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" );
459 subtest 'balance' => sub {
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' );
467 my $accountline_1 = $builder->build(
469 source => 'Accountline',
471 borrowernumber => $patron->borrowernumber,
473 amountoutstanding => 42
477 my $accountline_2 = $builder->build(
479 source => 'Accountline',
481 borrowernumber => $patron->borrowernumber,
483 amountoutstanding => -13
488 my $balance = $patron->account->balance;
489 is( int($balance), 29, 'balance should return the correct value');
494 subtest "Koha::Account::chargelostitem tests" => sub {
500 my $itype_no_replace_no_fee = $builder->build({ source => 'Itemtype', value => {
502 defaultreplacecost => undef,
505 my $itype_replace_no_fee = $builder->build({ source => 'Itemtype', value => {
507 defaultreplacecost => 16.32,
510 my $itype_no_replace_fee = $builder->build({ source => 'Itemtype', value => {
512 defaultreplacecost => undef,
515 my $itype_replace_fee = $builder->build({ source => 'Itemtype', value => {
517 defaultreplacecost => 4.08,
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});
527 t::lib::Mocks::mock_preference('item-level_itypes', '1');
528 t::lib::Mocks::mock_preference('useDefaultReplacementCost', '0');
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");
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");
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");
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");
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");
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");
582 t::lib::Mocks::mock_preference('useDefaultReplacementCost', '1');
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");
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");
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");
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");
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");
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");
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");
633 subtest "Koha::Account::non_issues_charges tests" => sub {
636 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
638 my $today = dt_from_string;
642 Koha::Account::Line->new(
644 borrowernumber => $patron->borrowernumber,
647 description => 'a Res fee',
648 accounttype => 'Res',
649 amountoutstanding => $res,
652 Koha::Account::Line->new(
654 borrowernumber => $patron->borrowernumber,
657 description => 'a Rental fee',
658 accounttype => 'Rent',
659 amountoutstanding => $rent,
662 Koha::Account::Line->new(
664 borrowernumber => $patron->borrowernumber,
667 description => 'a Manual invoice fee',
668 accounttype => 'Copie',
669 amountoutstanding => $manual,
672 Koha::AuthorisedValue->new(
674 category => 'MANUAL_INV',
675 authorised_value => 'Copie',
676 lib => 'Fee for copie',
680 my $account = $patron->account;
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;
689 $res + $rent + $manual,
690 'Total charges should be Res + Rent + Manual'
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' );
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;
703 $res + $rent + $manual,
704 'Total charges should be Res + Rent + Manual'
706 is( $non_issues_charges, $manual,
707 'If 0|0|1 Only Manual should be a non issue charge' );
711 'If 0|0|1 Res + Rent should be other charges'
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;
721 $res + $rent + $manual,
722 'Total charges should be Res + Rent + Manual'
724 is( $non_issues_charges, $rent,
725 'If 0|1|0 Only Rental should be a non issue charge' );
729 'If 0|1|0 Rent + Manual should be other charges'
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;
739 $res + $rent + $manual,
740 'Total charges should be Res + Rent + Manual'
745 'If 0|1|1 Rent + Manual should be non issues charges'
747 is( $other_charges, $res, 'If 0|1|1 there should only have other charges' );
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;
756 $res + $rent + $manual,
757 'Total charges should be Res + Rent + Manual'
759 is( $non_issues_charges, $res,
760 'If 1|0|0 Only Res should be non issues charges' );
764 'If 1|0|0 Rent + Manual should be other charges'
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;
774 $res + $rent + $manual,
775 'Total charges should be Res + Rent + Manual'
780 'If 1|1|0 Res + Rent should be non issues charges'
782 is( $other_charges, $manual,
783 'If 1|1|0 Only Manual should be other charges' );
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;
792 $res + $rent + $manual,
793 'Total charges should be Res + Rent + Manual'
797 $res + $rent + $manual,
798 'If 1|1|1 Res + Rent + Manual should be non issues charges'
800 is( $other_charges, 0, 'If 1|1|1 there should not have any other charges' );
803 subtest "Koha::Account::non_issues_charges tests" => sub {
806 my $patron = $builder->build_object(
808 class => "Koha::Patrons",
812 categorycode => $categorycode,
813 branchcode => $branchcode
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" );
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" );
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" );