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 => 28;
25 use t::lib::TestBuilder;
29 use Koha::Account::Lines;
30 use Koha::Account::Offsets;
31 use Koha::Notice::Messages;
32 use Koha::Notice::Templates;
33 use Koha::DateUtils qw( dt_from_string );
36 use_ok('C4::Accounts');
37 use_ok('Koha::Object');
38 use_ok('Koha::Patron');
39 use_ok('Data::Dumper');
42 can_ok( 'C4::Accounts',
48 purge_zero_balance_fees )
51 my $schema = Koha::Database->new->schema;
52 $schema->storage->txn_begin;
53 my $dbh = C4::Context->dbh;
55 my $builder = t::lib::TestBuilder->new;
56 my $library = $builder->build( { source => 'Branch' } );
58 $dbh->do(q|DELETE FROM accountlines|);
59 $dbh->do(q|DELETE FROM issues|);
60 $dbh->do(q|DELETE FROM borrowers|);
62 my $branchcode = $library->{branchcode};
65 my $context = new Test::MockModule('C4::Context');
66 $context->mock( 'userenv', sub {
70 branch => $branchcode,
74 # Testing purge_zero_balance_fees
76 # The 3rd value in the insert is 'days ago' --
81 my $sth = $dbh->prepare(
82 "INSERT INTO accountlines (
88 VALUES ( ?, ?, (select date_sub(CURRENT_DATE, INTERVAL ? DAY) ), ? )"
94 { amount => 0 , days_ago => 0 , description =>'purge_zero_balance_fees should not delete 0 balance fees with date today' , delete => 0 } ,
95 { amount => 0 , days_ago => $days - 1 , description =>'purge_zero_balance_fees should not delete 0 balance fees with date before threshold day' , delete => 0 } ,
96 { amount => 0 , days_ago => $days , description =>'purge_zero_balance_fees should not delete 0 balance fees with date on threshold day' , delete => 0 } ,
97 { amount => 0 , days_ago => $days + 1 , description =>'purge_zero_balance_fees should delete 0 balance fees with date after threshold day' , delete => 1 } ,
98 { amount => undef , days_ago => $days + 1 , description =>'purge_zero_balance_fees should delete NULL balance fees with date after threshold day' , delete => 1 } ,
99 { amount => 5 , days_ago => $days - 1 , description =>'purge_zero_balance_fees should not delete fees with positive amout owed before threshold day' , delete => 0 } ,
100 { amount => 5 , days_ago => $days , description =>'purge_zero_balance_fees should not delete fees with positive amout owed on threshold day' , delete => 0 } ,
101 { amount => 5 , days_ago => $days + 1 , description =>'purge_zero_balance_fees should not delete fees with positive amout owed after threshold day' , delete => 0 } ,
102 { amount => -5 , days_ago => $days - 1 , description =>'purge_zero_balance_fees should not delete fees with negative amout owed before threshold day' , delete => 0 } ,
103 { amount => -5 , days_ago => $days , description =>'purge_zero_balance_fees should not delete fees with negative amout owed on threshold day' , delete => 0 } ,
104 { amount => -5 , days_ago => $days + 1 , description =>'purge_zero_balance_fees should not delete fees with negative amout owed after threshold day' , delete => 0 }
106 my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
107 my $borrower = Koha::Patron->new( { firstname => 'Test', surname => 'Patron', categorycode => $categorycode, branchcode => $branchcode } )->store();
109 for my $data ( @test_data ) {
110 $sth->execute($borrower->borrowernumber, $data->{amount}, $data->{days_ago}, $data->{description});
113 purge_zero_balance_fees( $days );
115 $sth = $dbh->prepare(
116 "select count(*) = 0 as deleted
118 where description = ?"
122 sub is_delete_correct {
123 my $should_delete = shift;
124 my $description = shift;
125 $sth->execute( $description );
126 my $test = $sth->fetchrow_hashref();
127 is( $test->{deleted}, $should_delete, $description )
130 for my $data (@test_data) {
131 is_delete_correct( $data->{delete}, $data->{description});
134 $dbh->do(q|DELETE FROM accountlines|);
136 subtest "Koha::Account::pay tests" => sub {
141 my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
142 my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode };
144 my $borrower = Koha::Patron->new( {
145 cardnumber => '1234567890',
147 firstname => 'Marty',
149 $borrower->categorycode( $categorycode );
150 $borrower->branchcode( $branchcode );
153 my $account = Koha::Account->new({ patron_id => $borrower->id });
155 my $line1 = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amountoutstanding => 100 })->store();
156 my $line2 = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amountoutstanding => 200 })->store();
158 $sth = $dbh->prepare("SELECT count(*) FROM accountlines");
160 my $count = $sth->fetchrow_array;
161 is($count, 2, 'There is 2 lines as expected');
163 # There is $100 in the account
164 $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
165 my $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
167 for my $line ( @$amountoutstanding ) {
168 $amountleft += $line;
170 is($amountleft, 300, 'The account has 300$ as expected' );
172 # We make a $20 payment
173 my $borrowernumber = $borrower->borrowernumber;
175 my $payment_note = '$20.00 payment note';
176 my $id = $account->pay( { amount => $data, note => $payment_note, payment_type => "TEST_TYPE" } );
178 my $accountline = Koha::Account::Lines->find( $id );
179 is( $accountline->payment_type, "TEST_TYPE", "Payment type passed into pay is set in account line correctly" );
181 # There is now $280 in the account
182 $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
183 $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
185 for my $line ( @$amountoutstanding ) {
186 $amountleft += $line;
188 is($amountleft, 280, 'The account has $280 as expected' );
190 # Is the payment note well registered
191 $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
192 $sth->execute($borrower->borrowernumber);
193 my $note = $sth->fetchrow_array;
194 is($note,'$20.00 payment note', '$20.00 payment note is registered');
196 # We make a -$30 payment (a NEGATIVE payment)
198 $payment_note = '-$30.00 payment note';
199 $account->pay( { amount => $data, note => $payment_note } );
201 # There is now $310 in the account
202 $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
203 $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
205 for my $line ( @$amountoutstanding ) {
206 $amountleft += $line;
208 is($amountleft, 310, 'The account has $310 as expected' );
209 # Is the payment note well registered
210 $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
211 $sth->execute($borrower->borrowernumber);
212 $note = $sth->fetchrow_array;
213 is($note,'-$30.00 payment note', '-$30.00 payment note is registered');
215 #We make a $150 payment ( > 1stLine )
217 $payment_note = '$150.00 payment note';
218 $account->pay( { amount => $data, note => $payment_note } );
220 # There is now $160 in the account
221 $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
222 $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
224 for my $line ( @$amountoutstanding ) {
225 $amountleft += $line;
227 is($amountleft, 160, 'The account has $160 as expected' );
229 # Is the payment note well registered
230 $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
231 $sth->execute($borrower->borrowernumber);
232 $note = $sth->fetchrow_array;
233 is($note,'$150.00 payment note', '$150.00 payment note is registered');
235 #We make a $200 payment ( > amountleft )
237 $payment_note = '$200.00 payment note';
238 $account->pay( { amount => $data, note => $payment_note } );
240 # There is now -$40 in the account
241 $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
242 $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
244 for my $line ( @$amountoutstanding ) {
245 $amountleft += $line;
247 is($amountleft, -40, 'The account has -$40 as expected, (credit situation)' );
249 # Is the payment note well registered
250 $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
251 $sth->execute($borrower->borrowernumber);
252 $note = $sth->fetchrow_array;
253 is($note,'$200.00 payment note', '$200.00 payment note is registered');
255 my $line3 = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amountoutstanding => 42, accounttype => 'TEST' })->store();
256 my $payment_id = $account->pay( { lines => [$line3], amount => 42 } );
257 my $payment = Koha::Account::Lines->find( $payment_id );
258 is( $payment->amount(), '-42.000000', "Payment paid the specified fine" );
259 $line3 = Koha::Account::Lines->find( $line3->id );
260 is( $line3->amountoutstanding, '0.000000', "Specified fine is paid" );
263 subtest "Koha::Account::pay particular line tests" => sub {
268 my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
269 my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode };
271 my $borrower = Koha::Patron->new( {
272 cardnumber => 'kylemhall',
276 $borrower->categorycode( $categorycode );
277 $borrower->branchcode( $branchcode );
280 my $account = Koha::Account->new({ patron_id => $borrower->id });
282 my $line1 = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amountoutstanding => 1 })->store();
283 my $line2 = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amountoutstanding => 2 })->store();
284 my $line3 = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amountoutstanding => 3 })->store();
285 my $line4 = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amountoutstanding => 4 })->store();
287 is( $account->balance(), 10, "Account balance is 10" );
291 lines => [$line2, $line3, $line4],
296 $_->_result->discard_changes foreach ( $line1, $line2, $line3, $line4 );
298 # Line1 is not paid at all, as it was not passed in the lines param
299 is( $line1->amountoutstanding, "1.000000", "Line 1 was not paid" );
300 # Line2 was paid in full, as it was the first in the lines list
301 is( $line2->amountoutstanding, "0.000000", "Line 2 was paid in full" );
302 # Line3 was paid partially, as the remaining balance did not cover it entirely
303 is( $line3->amountoutstanding, "1.000000", "Line 3 was paid to 1.00" );
304 # Line4 was not paid at all, as the payment was all used up by that point
305 is( $line4->amountoutstanding, "4.000000", "Line 4 was not paid" );
308 subtest "Koha::Account::pay writeoff tests" => sub {
313 my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
314 my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode };
316 my $borrower = Koha::Patron->new( {
317 cardnumber => 'chelseahall',
319 firstname => 'Chelsea',
321 $borrower->categorycode( $categorycode );
322 $borrower->branchcode( $branchcode );
325 my $account = Koha::Account->new({ patron_id => $borrower->id });
327 my $line = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amountoutstanding => 42 })->store();
329 is( $account->balance(), 42, "Account balance is 42" );
331 my $id = $account->pay(
339 $line->_result->discard_changes();
341 is( $line->amountoutstanding, "0.000000", "Line was written off" );
343 my $writeoff = Koha::Account::Lines->find( $id );
345 is( $writeoff->accounttype, 'W', 'Type is correct' );
346 is( $writeoff->description, 'Writeoff', 'Description is correct' );
347 is( $writeoff->amount, '-42.000000', 'Amount is correct' );
350 subtest "More Koha::Account::pay tests" => sub {
355 my $category = $builder->build({ source => 'Category' })->{ categorycode };
356 my $branch = $builder->build({ source => 'Branch' })->{ branchcode };
357 $branchcode = $branch;
358 my $borrowernumber = $builder->build({
359 source => 'Borrower',
360 value => { categorycode => $category,
361 branchcode => $branch }
362 })->{ borrowernumber };
365 my $accountline = $builder->build({ source => 'Accountline',
366 value => { borrowernumber => $borrowernumber,
368 amountoutstanding => $amount }
371 my $rs = $schema->resultset('Accountline')->search({
372 borrowernumber => $borrowernumber
375 is( $rs->count(), 1, 'Accountline created' );
377 my $account = Koha::Account->new( { patron_id => $borrowernumber } );
378 my $line = Koha::Account::Lines->find( $accountline->{ accountlines_id } );
379 # make the full payment
380 $account->pay({ lines => [$line], amount => $amount, library_id => $branch, note => 'A payment note' });
382 my $offset = Koha::Account::Offsets->search({ debit_id => $accountline->{accountlines_id} })->next();
383 is( $offset->amount(), '-100.000000', 'Offset amount is -100.00' );
384 is( $offset->type(), 'Payment', 'Offset type is Payment' );
386 my $stat = $schema->resultset('Statistic')->search({
389 }, { order_by => { -desc => 'datetime' } })->next();
391 ok( defined $stat, "There's a payment log that matches the branch" );
394 skip "No statistic logged", 4 unless defined $stat;
396 is( $stat->type, 'payment', "Correct statistic type" );
397 is( $stat->branch, $branch, "Correct branch logged to statistics" );
398 is( $stat->borrowernumber, $borrowernumber, "Correct borrowernumber logged to statistics" );
399 is( $stat->value+0, $amount, "Correct amount logged to statistics" );
403 subtest "Even more Koha::Account::pay tests" => sub {
408 my $category = $builder->build({ source => 'Category' })->{ categorycode };
409 my $branch = $builder->build({ source => 'Branch' })->{ branchcode };
410 $branchcode = $branch;
411 my $borrowernumber = $builder->build({
412 source => 'Borrower',
413 value => { categorycode => $category,
414 branchcode => $branch }
415 })->{ borrowernumber };
418 my $partialamount = 60;
419 my $accountline = $builder->build({ source => 'Accountline',
420 value => { borrowernumber => $borrowernumber,
422 amountoutstanding => $amount }
425 my $rs = $schema->resultset('Accountline')->search({
426 borrowernumber => $borrowernumber
429 is( $rs->count(), 1, 'Accountline created' );
431 my $account = Koha::Account->new( { patron_id => $borrowernumber } );
432 my $line = Koha::Account::Lines->find( $accountline->{ accountlines_id } );
433 # make the full payment
434 $account->pay({ lines => [$line], amount => $partialamount, library_id => $branch, note => 'A payment note' });
436 my $offset = Koha::Account::Offsets->search( { debit_id => $accountline->{ accountlines_id } } )->next();
437 is( $offset->amount, '-60.000000', 'Offset amount is -60.00' );
438 is( $offset->type, 'Payment', 'Offset type is payment' );
440 my $stat = $schema->resultset('Statistic')->search({
443 }, { order_by => { -desc => 'datetime' } })->next();
445 ok( defined $stat, "There's a payment log that matches the branch" );
448 skip "No statistic logged", 4 unless defined $stat;
450 is( $stat->type, 'payment', "Correct statistic type" );
451 is( $stat->branch, $branch, "Correct branch logged to statistics" );
452 is( $stat->borrowernumber, $borrowernumber, "Correct borrowernumber logged to statistics" );
453 is( $stat->value+0, $partialamount, "Correct amount logged to statistics" );
457 subtest 'balance' => sub {
460 my $patron = $builder->build({source => 'Borrower'});
461 $patron = Koha::Patrons->find( $patron->{borrowernumber} );
462 my $account = $patron->account;
463 is( $account->balance, 0, 'balance should return 0 if the patron does not have fines' );
465 my $accountline_1 = $builder->build(
467 source => 'Accountline',
469 borrowernumber => $patron->borrowernumber,
471 amountoutstanding => 42
475 my $accountline_2 = $builder->build(
477 source => 'Accountline',
479 borrowernumber => $patron->borrowernumber,
481 amountoutstanding => -13
486 my $balance = $patron->account->balance;
487 is( int($balance), 29, 'balance should return the correct value');
492 subtest "Koha::Account::chargelostitem tests" => sub {
498 my $itype_no_replace_no_fee = $builder->build({ source => 'Itemtype', value => {
500 defaultreplacecost => undef,
503 my $itype_replace_no_fee = $builder->build({ source => 'Itemtype', value => {
505 defaultreplacecost => 16.32,
508 my $itype_no_replace_fee = $builder->build({ source => 'Itemtype', value => {
510 defaultreplacecost => undef,
513 my $itype_replace_fee = $builder->build({ source => 'Itemtype', value => {
515 defaultreplacecost => 4.08,
518 my $cli_borrowernumber = $builder->build({ source => 'Borrower' })->{'borrowernumber'};
519 my $cli_itemnumber1 = $builder->build({ source => 'Item', value => { itype => $itype_no_replace_no_fee->{itemtype} } })->{'itemnumber'};
520 my $cli_itemnumber2 = $builder->build({ source => 'Item', value => { itype => $itype_replace_no_fee->{itemtype} } })->{'itemnumber'};
521 my $cli_itemnumber3 = $builder->build({ source => 'Item', value => { itype => $itype_no_replace_fee->{itemtype} } })->{'itemnumber'};
522 my $cli_itemnumber4 = $builder->build({ source => 'Item', value => { itype => $itype_replace_fee->{itemtype} } })->{'itemnumber'};
523 my $duck = Koha::Items->find({itemnumber=>$cli_itemnumber1});
525 t::lib::Mocks::mock_preference('item-level_itypes', '1');
526 t::lib::Mocks::mock_preference('useDefaultReplacementCost', '0');
528 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber1, 0, "Perdedor");
529 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, accounttype => 'L' });
530 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, accounttype => 'PF' });
531 ok( !$lostfine, "No lost fine if no replacementcost or default when pref off");
532 ok( !$procfee, "No processing fee if no processing fee");
533 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber1, 6.12, "Perdedor");
534 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, accounttype => 'L' });
535 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, accounttype => 'PF' });
536 ok( $lostfine->amount == 6.12, "Lost fine equals replacementcost when pref off and no default set");
537 ok( !$procfee, "No processing fee if no processing fee");
540 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber2, 0, "Perdedor");
541 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, accounttype => 'L' });
542 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, accounttype => 'PF' });
543 ok( !$lostfine, "No lost fine if no replacementcost but default set when pref off");
544 ok( !$procfee, "No processing fee if no processing fee");
545 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber2, 6.12, "Perdedor");
546 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, accounttype => 'L' });
547 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, accounttype => 'PF' });
548 ok( $lostfine->amount == 6.12 , "Lost fine equals replacementcost when pref off and default set");
549 ok( !$procfee, "No processing fee if no processing fee");
552 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber3, 0, "Perdedor");
553 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, accounttype => 'L' });
554 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, accounttype => 'PF' });
555 ok( !$lostfine, "No lost fine if no replacementcost and no default set when pref off");
556 ok( $procfee->amount == 8.16, "Processing fee if processing fee");
558 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber3, 6.12, "Perdedor");
559 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, accounttype => 'L' });
560 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, accounttype => 'PF' });
561 ok( $lostfine->amount == 6.12 , "Lost fine equals replacementcost when pref off and no default set");
562 ok( $procfee->amount == 8.16, "Processing fee if processing fee");
566 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 0, "Perdedor");
567 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'L' });
568 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'PF' });
569 ok( !$lostfine, "No lost fine if no replacementcost but default set when pref off");
570 ok( $procfee->amount == 2.04, "Processing fee if processing fee");
572 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 6.12, "Perdedor");
573 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'L' });
574 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'PF' });
575 ok( $lostfine->amount == 6.12 , "Lost fine equals replacementcost when pref off and default set");
576 ok( $procfee->amount == 2.04, "Processing fee if processing fee");
580 t::lib::Mocks::mock_preference('useDefaultReplacementCost', '1');
582 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber1, 0, "Perdedor");
583 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, accounttype => 'L' });
584 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, accounttype => 'PF' });
585 ok( !$lostfine, "No lost fine if no replacementcost or default when pref on");
586 ok( !$procfee, "No processing fee if no processing fee");
587 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber1, 6.12, "Perdedor");
588 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, accounttype => 'L' });
589 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, accounttype => 'PF' });
590 is( $lostfine->amount, "6.120000", "Lost fine equals replacementcost when pref on and no default set");
591 ok( !$procfee, "No processing fee if no processing fee");
593 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber2, 0, "Perdedor");
594 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, accounttype => 'L' });
595 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, accounttype => 'PF' });
596 is( $lostfine->amount(), "16.320000", "Lost fine is default if no replacementcost but default set when pref on");
597 ok( !$procfee, "No processing fee if no processing fee");
599 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber2, 6.12, "Perdedor");
600 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, accounttype => 'L' });
601 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, accounttype => 'PF' });
602 is( $lostfine->amount, "6.120000" , "Lost fine equals replacementcost when pref on and default set");
603 ok( !$procfee, "No processing fee if no processing fee");
605 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber3, 0, "Perdedor");
606 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, accounttype => 'L' });
607 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, accounttype => 'PF' });
608 ok( !$lostfine, "No lost fine if no replacementcost and default not set when pref on");
609 is( $procfee->amount, "8.160000", "Processing fee if processing fee");
611 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber3, 6.12, "Perdedor");
612 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, accounttype => 'L' });
613 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, accounttype => 'PF' });
614 is( $lostfine->amount, "6.120000", "Lost fine equals replacementcost when pref on and no default set");
615 is( $procfee->amount, "8.160000", "Processing fee if processing fee");
617 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 0, "Perdedor");
618 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'L' });
619 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'PF' });
620 is( $lostfine->amount, "4.080000", "Lost fine is default if no replacementcost but default set when pref on");
621 is( $procfee->amount, "2.040000", "Processing fee if processing fee");
624 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 6.12, "Perdedor");
625 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'L' });
626 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'PF' });
627 is( $lostfine->amount, "6.120000", "Lost fine equals replacementcost when pref on and default set");
628 is( $procfee->amount, "2.040000", "Processing fee if processing fee");
631 subtest "Koha::Account::non_issues_charges tests" => sub {
634 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
636 my $today = dt_from_string;
640 Koha::Account::Line->new(
642 borrowernumber => $patron->borrowernumber,
645 description => 'a Res fee',
646 accounttype => 'Res',
647 amountoutstanding => $res,
650 Koha::Account::Line->new(
652 borrowernumber => $patron->borrowernumber,
655 description => 'a Rental fee',
656 accounttype => 'Rent',
657 amountoutstanding => $rent,
660 Koha::Account::Line->new(
662 borrowernumber => $patron->borrowernumber,
665 description => 'a Manual invoice fee',
666 accounttype => 'Copie',
667 amountoutstanding => $manual,
670 Koha::AuthorisedValue->new(
672 category => 'MANUAL_INV',
673 authorised_value => 'Copie',
674 lib => 'Fee for copie',
678 my $account = $patron->account;
680 t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge', 0 );
681 t::lib::Mocks::mock_preference( 'RentalsInNoissuesCharge', 0 );
682 t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge', 0 );
683 my ( $total, $non_issues_charges ) = ( $account->balance, $account->non_issues_charges );
684 my $other_charges = $total - $non_issues_charges;
687 $res + $rent + $manual,
688 'Total charges should be Res + Rent + Manual'
690 is( $non_issues_charges, 0,
691 'If 0|0|0 there should not have non issues charges' );
692 is( $other_charges, 15, 'If 0|0|0 there should only have other charges' );
694 t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge', 0 );
695 t::lib::Mocks::mock_preference( 'RentalsInNoissuesCharge', 0 );
696 t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge', 1 );
697 ( $total, $non_issues_charges ) = ( $account->balance, $account->non_issues_charges );
698 $other_charges = $total - $non_issues_charges;
701 $res + $rent + $manual,
702 'Total charges should be Res + Rent + Manual'
704 is( $non_issues_charges, $manual,
705 'If 0|0|1 Only Manual should be a non issue charge' );
709 'If 0|0|1 Res + Rent should be other charges'
712 t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge', 0 );
713 t::lib::Mocks::mock_preference( 'RentalsInNoissuesCharge', 1 );
714 t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge', 0 );
715 ( $total, $non_issues_charges ) = ( $account->balance, $account->non_issues_charges );
716 $other_charges = $total - $non_issues_charges;
719 $res + $rent + $manual,
720 'Total charges should be Res + Rent + Manual'
722 is( $non_issues_charges, $rent,
723 'If 0|1|0 Only Rental should be a non issue charge' );
727 'If 0|1|0 Rent + Manual should be other charges'
730 t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge', 0 );
731 t::lib::Mocks::mock_preference( 'RentalsInNoissuesCharge', 1 );
732 t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge', 1 );
733 ( $total, $non_issues_charges ) = ( $account->balance, $account->non_issues_charges );
734 $other_charges = $total - $non_issues_charges;
737 $res + $rent + $manual,
738 'Total charges should be Res + Rent + Manual'
743 'If 0|1|1 Rent + Manual should be non issues charges'
745 is( $other_charges, $res, 'If 0|1|1 there should only have other charges' );
747 t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge', 1 );
748 t::lib::Mocks::mock_preference( 'RentalsInNoissuesCharge', 0 );
749 t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge', 0 );
750 ( $total, $non_issues_charges ) = ( $account->balance, $account->non_issues_charges );
751 $other_charges = $total - $non_issues_charges;
754 $res + $rent + $manual,
755 'Total charges should be Res + Rent + Manual'
757 is( $non_issues_charges, $res,
758 'If 1|0|0 Only Res should be non issues charges' );
762 'If 1|0|0 Rent + Manual should be other charges'
765 t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge', 1 );
766 t::lib::Mocks::mock_preference( 'RentalsInNoissuesCharge', 1 );
767 t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge', 0 );
768 ( $total, $non_issues_charges ) = ( $account->balance, $account->non_issues_charges );
769 $other_charges = $total - $non_issues_charges;
772 $res + $rent + $manual,
773 'Total charges should be Res + Rent + Manual'
778 'If 1|1|0 Res + Rent should be non issues charges'
780 is( $other_charges, $manual,
781 'If 1|1|0 Only Manual should be other charges' );
783 t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge', 1 );
784 t::lib::Mocks::mock_preference( 'RentalsInNoissuesCharge', 1 );
785 t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge', 1 );
786 ( $total, $non_issues_charges ) = ( $account->balance, $account->non_issues_charges );
787 $other_charges = $total - $non_issues_charges;
790 $res + $rent + $manual,
791 'Total charges should be Res + Rent + Manual'
795 $res + $rent + $manual,
796 'If 1|1|1 Res + Rent + Manual should be non issues charges'
798 is( $other_charges, 0, 'If 1|1|1 there should not have any other charges' );
801 subtest "Koha::Account::non_issues_charges tests" => sub {
804 my $patron = $builder->build_object(
806 class => "Koha::Patrons",
810 categorycode => $categorycode,
811 branchcode => $branchcode
816 my $debit = Koha::Account::Line->new({ borrowernumber => $patron->id, date => '1900-01-01', amountoutstanding => 0 })->store();
817 my $credit = Koha::Account::Line->new({ borrowernumber => $patron->id, date => '1900-01-01', amountoutstanding => -5 })->store();
818 my $offset = Koha::Account::Offset->new({ credit_id => $credit->id, debit_id => $debit->id, type => 'Payment', amount => 0 })->store();
819 purge_zero_balance_fees( 1 );
820 my $debit_2 = Koha::Account::Lines->find( $debit->id );
821 my $credit_2 = Koha::Account::Lines->find( $credit->id );
822 ok( $debit_2, 'Debit was correctly not deleted when credit has balance' );
823 ok( $credit_2, 'Credit was correctly not deleted when credit has balance' );
824 is( Koha::Account::Lines->count({ borrowernumber => $patron->id }), 2, "The 2 account lines still exists" );
826 $debit = Koha::Account::Line->new({ borrowernumber => $patron->id, date => '1900-01-01', amountoutstanding => 5 })->store();
827 $credit = Koha::Account::Line->new({ borrowernumber => $patron->id, date => '1900-01-01', amountoutstanding => 0 })->store();
828 $offset = Koha::Account::Offset->new({ credit_id => $credit->id, debit_id => $debit->id, type => 'Payment', amount => 0 })->store();
829 purge_zero_balance_fees( 1 );
830 $debit_2 = $credit_2 = undef;
831 $debit_2 = Koha::Account::Lines->find( $debit->id );
832 $credit_2 = Koha::Account::Lines->find( $credit->id );
833 ok( $debit_2, 'Debit was correctly not deleted when debit has balance' );
834 ok( $credit_2, 'Credit was correctly not deleted when debit has balance' );
835 is( Koha::Account::Lines->count({ borrowernumber => $patron->id }), 2 + 2, "The 2 + 2 account lines still exists" );
837 $debit = Koha::Account::Line->new({ borrowernumber => $patron->id, date => '1900-01-01', amountoutstanding => 0 })->store();
838 $credit = Koha::Account::Line->new({ borrowernumber => $patron->id, date => '1900-01-01', amountoutstanding => 0 })->store();
839 $offset = Koha::Account::Offset->new({ credit_id => $credit->id, debit_id => $debit->id, type => 'Payment', amount => 0 })->store();
840 purge_zero_balance_fees( 1 );
841 $debit_2 = Koha::Account::Lines->find( $debit->id );
842 $credit_2 = Koha::Account::Lines->find( $credit->id );
843 ok( !$debit_2, 'Debit was correctly deleted' );
844 ok( !$credit_2, 'Credit was correctly deleted' );
845 is( Koha::Account::Lines->count({ borrowernumber => $patron->id }), 2 + 2, "The 2 + 2 account lines still exists, the last 2 have been deleted ok" );
848 subtest "Koha::Account::Line::void tests" => sub {
853 my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
854 my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode };
856 my $borrower = Koha::Patron->new( {
857 cardnumber => 'dariahall',
859 firstname => 'Daria',
861 $borrower->categorycode( $categorycode );
862 $borrower->branchcode( $branchcode );
865 my $account = Koha::Account->new({ patron_id => $borrower->id });
867 my $line1 = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amount => 10, amountoutstanding => 10 })->store();
868 my $line2 = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amount => 20, amountoutstanding => 20 })->store();
870 is( $account->balance(), 30, "Account balance is 30" );
871 is( $line1->amountoutstanding, 10, 'First fee has amount outstanding of 10' );
872 is( $line2->amountoutstanding, 20, 'Second fee has amount outstanding of 20' );
874 my $id = $account->pay(
876 lines => [$line1, $line2],
881 my $account_payment = Koha::Account::Lines->find( $id );
883 is( $account->balance(), 0, "Account balance is 0" );
885 $line1->_result->discard_changes();
886 $line2->_result->discard_changes();
887 is( $line1->amountoutstanding+0, 0, 'First fee has amount outstanding of 0' );
888 is( $line2->amountoutstanding+0, 0, 'Second fee has amount outstanding of 0' );
890 my $ret = $account_payment->void();
892 is( ref($ret), 'Koha::Account::Line', 'Void returns the account line' );
893 is( $account->balance(), 30, "Account balance is again 30" );
895 $account_payment->_result->discard_changes();
896 $line1->_result->discard_changes();
897 $line2->_result->discard_changes();
899 is( $account_payment->accounttype, 'VOID', 'Voided payment accounttype is VOID' );
900 is( $account_payment->amount+0, 0, 'Voided payment amount is 0' );
901 is( $account_payment->amountoutstanding+0, 0, 'Voided payment amount outstanding is 0' );
903 is( $line1->amountoutstanding+0, 10, 'First fee again has amount outstanding of 10' );
904 is( $line2->amountoutstanding+0, 20, 'Second fee again has amount outstanding of 20' );
906 # Accountlines that are not credits should be un-voidable
907 my $line1_pre = $line1->unblessed();
908 $ret = $line1->void();
909 $line1->_result->discard_changes();
910 my $line1_post = $line1->unblessed();
911 is( $ret, undef, 'Attempted void on non-credit returns undef' );
912 is_deeply( $line1_pre, $line1_post, 'Non-credit account line cannot be voided' )
915 subtest "Koha::Account::Offset credit & debit tests" => sub {
920 my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
921 my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode };
923 my $borrower = Koha::Patron->new( {
924 cardnumber => 'kyliehall',
926 firstname => 'Kylie',
928 $borrower->categorycode( $categorycode );
929 $borrower->branchcode( $branchcode );
932 my $account = Koha::Account->new({ patron_id => $borrower->id });
934 my $line1 = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amount => 10, amountoutstanding => 10 })->store();
935 my $line2 = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amount => 20, amountoutstanding => 20 })->store();
937 my $id = $account->pay(
939 lines => [$line1, $line2],
944 # Test debit and credit methods for Koha::Account::Offset
945 my $account_offset = Koha::Account::Offsets->find( { credit_id => $id, debit_id => $line1->id } );
946 is( $account_offset->debit->id, $line1->id, "Koha::Account::Offset->debit gets correct accountline" );
947 is( $account_offset->credit->id, $id, "Koha::Account::Offset->credit gets correct accountline" );
949 $account_offset = Koha::Account::Offset->new(
958 is( $account_offset->debit, undef, "Koha::Account::Offset->debit returns undef if no associated debit" );
959 is( $account_offset->credit, undef, "Koha::Account::Offset->credit returns undef if no associated credit" );
962 subtest "Payment notice tests" => sub {
966 Koha::Account::Lines->delete();
967 Koha::Patrons->delete();
968 Koha::Notice::Messages->delete();
970 my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
971 my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode };
973 my $borrower = Koha::Patron->new(
975 cardnumber => 'chelseahall',
977 firstname => 'Chelsea',
978 email => 'chelsea@example.com',
979 categorycode => $categorycode,
980 branchcode => $branchcode,
984 my $account = Koha::Account->new({ patron_id => $borrower->id });
986 my $line = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amountoutstanding => 27 })->store();
988 my $letter = Koha::Notice::Templates->find( { code => 'ACCOUNT_PAYMENT' } );
989 $letter->content('[%- USE Price -%]A payment of [% credit.amount * -1 | $Price %] has been applied to your account.');
992 t::lib::Mocks::mock_preference('UseEmailReceipts', '0');
994 my $id = $account->pay( { amount => 1 } );
995 is( Koha::Notice::Messages->search()->count(), 0, 'Notice for payment not sent if UseEmailReceipts is disabled' );
997 $id = $account->pay( { amount => 1, type => 'writeoff' } );
998 is( Koha::Notice::Messages->search()->count(), 0, 'Notice for writeoff not sent if UseEmailReceipts is disabled' );
1000 t::lib::Mocks::mock_preference('UseEmailReceipts', '1');
1002 $id = $account->pay( { amount => 12 } );
1003 my $notice = Koha::Notice::Messages->search()->next();
1004 is( $notice->subject, 'Account payment', 'Notice subject is correct for payment' );
1005 is( $notice->letter_code, 'ACCOUNT_PAYMENT', 'Notice letter code is correct for payment' );
1006 is( $notice->content, 'A payment of 12.00 has been applied to your account.', 'Notice content is correct for payment' );
1009 $letter = Koha::Notice::Templates->find( { code => 'ACCOUNT_WRITEOFF' } );
1010 $letter->content('[%- USE Price -%]A writeoff of [% credit.amount * -1 | $Price %] has been applied to your account.');
1013 $id = $account->pay( { amount => 13, type => 'writeoff' } );
1014 $notice = Koha::Notice::Messages->search()->next();
1015 is( $notice->subject, 'Account writeoff', 'Notice subject is correct for payment' );
1016 is( $notice->letter_code, 'ACCOUNT_WRITEOFF', 'Notice letter code is correct for writeoff' );
1017 is( $notice->content, 'A writeoff of 13.00 has been applied to your account.', 'Notice content is correct for writeoff' );