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',
49 purge_zero_balance_fees )
52 my $schema = Koha
::Database
->new->schema;
53 $schema->storage->txn_begin;
54 my $dbh = C4
::Context
->dbh;
56 my $builder = t
::lib
::TestBuilder
->new;
57 my $library = $builder->build( { source
=> 'Branch' } );
59 $dbh->do(q
|DELETE FROM accountlines
|);
60 $dbh->do(q
|DELETE FROM issues
|);
61 $dbh->do(q
|DELETE FROM borrowers
|);
63 my $branchcode = $library->{branchcode
};
66 my $context = new Test
::MockModule
('C4::Context');
67 $context->mock( 'userenv', sub {
71 branch
=> $branchcode,
75 # Testing purge_zero_balance_fees
77 # The 3rd value in the insert is 'days ago' --
82 my $sth = $dbh->prepare(
83 "INSERT INTO accountlines (
89 VALUES ( ?, ?, (select date_sub(CURRENT_DATE, INTERVAL ? DAY) ), ? )"
95 { amount
=> 0 , days_ago
=> 0 , description
=>'purge_zero_balance_fees should not delete 0 balance fees with date today' , delete => 0 } ,
96 { amount
=> 0 , days_ago
=> $days - 1 , description
=>'purge_zero_balance_fees should not delete 0 balance fees with date before threshold day' , delete => 0 } ,
97 { amount
=> 0 , days_ago
=> $days , description
=>'purge_zero_balance_fees should not delete 0 balance fees with date on threshold day' , delete => 0 } ,
98 { amount
=> 0 , days_ago
=> $days + 1 , description
=>'purge_zero_balance_fees should delete 0 balance fees with date after threshold day' , delete => 1 } ,
99 { amount
=> undef , days_ago
=> $days + 1 , description
=>'purge_zero_balance_fees should delete NULL balance fees with date after threshold day' , delete => 1 } ,
100 { amount
=> 5 , days_ago
=> $days - 1 , description
=>'purge_zero_balance_fees should not delete fees with positive amout owed before threshold day' , delete => 0 } ,
101 { amount
=> 5 , days_ago
=> $days , description
=>'purge_zero_balance_fees should not delete fees with positive amout owed on threshold day' , delete => 0 } ,
102 { amount
=> 5 , days_ago
=> $days + 1 , description
=>'purge_zero_balance_fees should not delete fees with positive amout owed after threshold day' , delete => 0 } ,
103 { amount
=> -5 , days_ago
=> $days - 1 , description
=>'purge_zero_balance_fees should not delete fees with negative amout owed before threshold day' , delete => 0 } ,
104 { amount
=> -5 , days_ago
=> $days , description
=>'purge_zero_balance_fees should not delete fees with negative amout owed on threshold day' , delete => 0 } ,
105 { amount
=> -5 , days_ago
=> $days + 1 , description
=>'purge_zero_balance_fees should not delete fees with negative amout owed after threshold day' , delete => 0 }
107 my $categorycode = $builder->build({ source
=> 'Category' })->{categorycode
};
108 my $borrower = Koha
::Patron
->new( { firstname
=> 'Test', surname
=> 'Patron', categorycode
=> $categorycode, branchcode
=> $branchcode } )->store();
110 for my $data ( @test_data ) {
111 $sth->execute($borrower->borrowernumber, $data->{amount
}, $data->{days_ago
}, $data->{description
});
114 purge_zero_balance_fees
( $days );
116 $sth = $dbh->prepare(
117 "select count(*) = 0 as deleted
119 where description = ?"
123 sub is_delete_correct
{
124 my $should_delete = shift;
125 my $description = shift;
126 $sth->execute( $description );
127 my $test = $sth->fetchrow_hashref();
128 is
( $test->{deleted
}, $should_delete, $description )
131 for my $data (@test_data) {
132 is_delete_correct
( $data->{delete}, $data->{description
});
135 $dbh->do(q
|DELETE FROM accountlines
|);
137 subtest
"Koha::Account::pay tests" => sub {
142 my $categorycode = $builder->build({ source
=> 'Category' })->{ categorycode
};
143 my $branchcode = $builder->build({ source
=> 'Branch' })->{ branchcode
};
145 my $borrower = Koha
::Patron
->new( {
146 cardnumber
=> '1234567890',
148 firstname
=> 'Marty',
150 $borrower->categorycode( $categorycode );
151 $borrower->branchcode( $branchcode );
154 my $account = Koha
::Account
->new({ patron_id
=> $borrower->id });
156 my $line1 = Koha
::Account
::Line
->new({ borrowernumber
=> $borrower->borrowernumber, amountoutstanding
=> 100 })->store();
157 my $line2 = Koha
::Account
::Line
->new({ borrowernumber
=> $borrower->borrowernumber, amountoutstanding
=> 200 })->store();
159 $sth = $dbh->prepare("SELECT count(*) FROM accountlines");
161 my $count = $sth->fetchrow_array;
162 is
($count, 2, 'There is 2 lines as expected');
164 # There is $100 in the account
165 $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
166 my $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
168 for my $line ( @
$amountoutstanding ) {
169 $amountleft += $line;
171 is
($amountleft, 300, 'The account has 300$ as expected' );
173 # We make a $20 payment
174 my $borrowernumber = $borrower->borrowernumber;
176 my $payment_note = '$20.00 payment note';
177 my $id = $account->pay( { amount
=> $data, note
=> $payment_note, payment_type
=> "TEST_TYPE" } );
179 my $accountline = Koha
::Account
::Lines
->find( $id );
180 is
( $accountline->payment_type, "TEST_TYPE", "Payment type passed into pay is set in account line correctly" );
182 # There is now $280 in the account
183 $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
184 $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
186 for my $line ( @
$amountoutstanding ) {
187 $amountleft += $line;
189 is
($amountleft, 280, 'The account has $280 as expected' );
191 # Is the payment note well registered
192 $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
193 $sth->execute($borrower->borrowernumber);
194 my $note = $sth->fetchrow_array;
195 is
($note,'$20.00 payment note', '$20.00 payment note is registered');
197 # We make a -$30 payment (a NEGATIVE payment)
199 $payment_note = '-$30.00 payment note';
200 $account->pay( { amount
=> $data, note
=> $payment_note } );
202 # There is now $310 in the account
203 $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
204 $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
206 for my $line ( @
$amountoutstanding ) {
207 $amountleft += $line;
209 is
($amountleft, 310, 'The account has $310 as expected' );
210 # Is the payment note well registered
211 $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
212 $sth->execute($borrower->borrowernumber);
213 $note = $sth->fetchrow_array;
214 is
($note,'-$30.00 payment note', '-$30.00 payment note is registered');
216 #We make a $150 payment ( > 1stLine )
218 $payment_note = '$150.00 payment note';
219 $account->pay( { amount
=> $data, note
=> $payment_note } );
221 # There is now $160 in the account
222 $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
223 $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
225 for my $line ( @
$amountoutstanding ) {
226 $amountleft += $line;
228 is
($amountleft, 160, 'The account has $160 as expected' );
230 # Is the payment note well registered
231 $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
232 $sth->execute($borrower->borrowernumber);
233 $note = $sth->fetchrow_array;
234 is
($note,'$150.00 payment note', '$150.00 payment note is registered');
236 #We make a $200 payment ( > amountleft )
238 $payment_note = '$200.00 payment note';
239 $account->pay( { amount
=> $data, note
=> $payment_note } );
241 # There is now -$40 in the account
242 $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
243 $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
245 for my $line ( @
$amountoutstanding ) {
246 $amountleft += $line;
248 is
($amountleft, -40, 'The account has -$40 as expected, (credit situation)' );
250 # Is the payment note well registered
251 $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
252 $sth->execute($borrower->borrowernumber);
253 $note = $sth->fetchrow_array;
254 is
($note,'$200.00 payment note', '$200.00 payment note is registered');
256 my $line3 = Koha
::Account
::Line
->new({ borrowernumber
=> $borrower->borrowernumber, amountoutstanding
=> 42, accounttype
=> 'TEST' })->store();
257 my $payment_id = $account->pay( { lines
=> [$line3], amount
=> 42 } );
258 my $payment = Koha
::Account
::Lines
->find( $payment_id );
259 is
( $payment->amount(), '-42.000000', "Payment paid the specified fine" );
260 $line3 = Koha
::Account
::Lines
->find( $line3->id );
261 is
( $line3->amountoutstanding, '0.000000', "Specified fine is paid" );
264 subtest
"Koha::Account::pay particular line tests" => sub {
269 my $categorycode = $builder->build({ source
=> 'Category' })->{ categorycode
};
270 my $branchcode = $builder->build({ source
=> 'Branch' })->{ branchcode
};
272 my $borrower = Koha
::Patron
->new( {
273 cardnumber
=> 'kylemhall',
277 $borrower->categorycode( $categorycode );
278 $borrower->branchcode( $branchcode );
281 my $account = Koha
::Account
->new({ patron_id
=> $borrower->id });
283 my $line1 = Koha
::Account
::Line
->new({ borrowernumber
=> $borrower->borrowernumber, amountoutstanding
=> 1 })->store();
284 my $line2 = Koha
::Account
::Line
->new({ borrowernumber
=> $borrower->borrowernumber, amountoutstanding
=> 2 })->store();
285 my $line3 = Koha
::Account
::Line
->new({ borrowernumber
=> $borrower->borrowernumber, amountoutstanding
=> 3 })->store();
286 my $line4 = Koha
::Account
::Line
->new({ borrowernumber
=> $borrower->borrowernumber, amountoutstanding
=> 4 })->store();
288 is
( $account->balance(), 10, "Account balance is 10" );
292 lines
=> [$line2, $line3, $line4],
297 $_->_result->discard_changes foreach ( $line1, $line2, $line3, $line4 );
299 # Line1 is not paid at all, as it was not passed in the lines param
300 is
( $line1->amountoutstanding, "1.000000", "Line 1 was not paid" );
301 # Line2 was paid in full, as it was the first in the lines list
302 is
( $line2->amountoutstanding, "0.000000", "Line 2 was paid in full" );
303 # Line3 was paid partially, as the remaining balance did not cover it entirely
304 is
( $line3->amountoutstanding, "1.000000", "Line 3 was paid to 1.00" );
305 # Line4 was not paid at all, as the payment was all used up by that point
306 is
( $line4->amountoutstanding, "4.000000", "Line 4 was not paid" );
309 subtest
"Koha::Account::pay writeoff tests" => sub {
314 my $categorycode = $builder->build({ source
=> 'Category' })->{ categorycode
};
315 my $branchcode = $builder->build({ source
=> 'Branch' })->{ branchcode
};
317 my $borrower = Koha
::Patron
->new( {
318 cardnumber
=> 'chelseahall',
320 firstname
=> 'Chelsea',
322 $borrower->categorycode( $categorycode );
323 $borrower->branchcode( $branchcode );
326 my $account = Koha
::Account
->new({ patron_id
=> $borrower->id });
328 my $line = Koha
::Account
::Line
->new({ borrowernumber
=> $borrower->borrowernumber, amountoutstanding
=> 42 })->store();
330 is
( $account->balance(), 42, "Account balance is 42" );
332 my $id = $account->pay(
340 $line->_result->discard_changes();
342 is
( $line->amountoutstanding, "0.000000", "Line was written off" );
344 my $writeoff = Koha
::Account
::Lines
->find( $id );
346 is
( $writeoff->accounttype, 'W', 'Type is correct' );
347 is
( $writeoff->description, 'Writeoff', 'Description is correct' );
348 is
( $writeoff->amount, '-42.000000', 'Amount is correct' );
351 subtest
"More Koha::Account::pay tests" => sub {
356 my $category = $builder->build({ source
=> 'Category' })->{ categorycode
};
357 my $branch = $builder->build({ source
=> 'Branch' })->{ branchcode
};
358 $branchcode = $branch;
359 my $borrowernumber = $builder->build({
360 source
=> 'Borrower',
361 value
=> { categorycode
=> $category,
362 branchcode
=> $branch }
363 })->{ borrowernumber
};
366 my $accountline = $builder->build({ source
=> 'Accountline',
367 value
=> { borrowernumber
=> $borrowernumber,
369 amountoutstanding
=> $amount }
372 my $rs = $schema->resultset('Accountline')->search({
373 borrowernumber
=> $borrowernumber
376 is
( $rs->count(), 1, 'Accountline created' );
378 my $account = Koha
::Account
->new( { patron_id
=> $borrowernumber } );
379 my $line = Koha
::Account
::Lines
->find( $accountline->{ accountlines_id
} );
380 # make the full payment
381 $account->pay({ lines
=> [$line], amount
=> $amount, library_id
=> $branch, note
=> 'A payment note' });
383 my $offset = Koha
::Account
::Offsets
->search({ debit_id
=> $accountline->{accountlines_id
} })->next();
384 is
( $offset->amount(), '-100.000000', 'Offset amount is -100.00' );
385 is
( $offset->type(), 'Payment', 'Offset type is Payment' );
387 my $stat = $schema->resultset('Statistic')->search({
390 }, { order_by
=> { -desc
=> 'datetime' } })->next();
392 ok
( defined $stat, "There's a payment log that matches the branch" );
395 skip
"No statistic logged", 4 unless defined $stat;
397 is
( $stat->type, 'payment', "Correct statistic type" );
398 is
( $stat->branch, $branch, "Correct branch logged to statistics" );
399 is
( $stat->borrowernumber, $borrowernumber, "Correct borrowernumber logged to statistics" );
400 is
( $stat->value+0, $amount, "Correct amount logged to statistics" );
404 subtest
"Even more Koha::Account::pay tests" => sub {
409 my $category = $builder->build({ source
=> 'Category' })->{ categorycode
};
410 my $branch = $builder->build({ source
=> 'Branch' })->{ branchcode
};
411 $branchcode = $branch;
412 my $borrowernumber = $builder->build({
413 source
=> 'Borrower',
414 value
=> { categorycode
=> $category,
415 branchcode
=> $branch }
416 })->{ borrowernumber
};
419 my $partialamount = 60;
420 my $accountline = $builder->build({ source
=> 'Accountline',
421 value
=> { borrowernumber
=> $borrowernumber,
423 amountoutstanding
=> $amount }
426 my $rs = $schema->resultset('Accountline')->search({
427 borrowernumber
=> $borrowernumber
430 is
( $rs->count(), 1, 'Accountline created' );
432 my $account = Koha
::Account
->new( { patron_id
=> $borrowernumber } );
433 my $line = Koha
::Account
::Lines
->find( $accountline->{ accountlines_id
} );
434 # make the full payment
435 $account->pay({ lines
=> [$line], amount
=> $partialamount, library_id
=> $branch, note
=> 'A payment note' });
437 my $offset = Koha
::Account
::Offsets
->search( { debit_id
=> $accountline->{ accountlines_id
} } )->next();
438 is
( $offset->amount, '-60.000000', 'Offset amount is -60.00' );
439 is
( $offset->type, 'Payment', 'Offset type is payment' );
441 my $stat = $schema->resultset('Statistic')->search({
444 }, { order_by
=> { -desc
=> 'datetime' } })->next();
446 ok
( defined $stat, "There's a payment log that matches the branch" );
449 skip
"No statistic logged", 4 unless defined $stat;
451 is
( $stat->type, 'payment', "Correct statistic type" );
452 is
( $stat->branch, $branch, "Correct branch logged to statistics" );
453 is
( $stat->borrowernumber, $borrowernumber, "Correct borrowernumber logged to statistics" );
454 is
( $stat->value+0, $partialamount, "Correct amount logged to statistics" );
458 subtest
'balance' => sub {
461 my $patron = $builder->build({source
=> 'Borrower'});
462 $patron = Koha
::Patrons
->find( $patron->{borrowernumber
} );
463 my $account = $patron->account;
464 is
( $account->balance, 0, 'balance should return 0 if the patron does not have fines' );
466 my $accountline_1 = $builder->build(
468 source
=> 'Accountline',
470 borrowernumber
=> $patron->borrowernumber,
472 amountoutstanding
=> 42
476 my $accountline_2 = $builder->build(
478 source
=> 'Accountline',
480 borrowernumber
=> $patron->borrowernumber,
482 amountoutstanding
=> -13
487 my $balance = $patron->account->balance;
488 is
( int($balance), 29, 'balance should return the correct value');
493 subtest
"Koha::Account::chargelostitem tests" => sub {
499 my $itype_no_replace_no_fee = $builder->build({ source
=> 'Itemtype', value
=> {
501 defaultreplacecost
=> undef,
504 my $itype_replace_no_fee = $builder->build({ source
=> 'Itemtype', value
=> {
506 defaultreplacecost
=> 16.32,
509 my $itype_no_replace_fee = $builder->build({ source
=> 'Itemtype', value
=> {
511 defaultreplacecost
=> undef,
514 my $itype_replace_fee = $builder->build({ source
=> 'Itemtype', value
=> {
516 defaultreplacecost
=> 4.08,
519 my $cli_borrowernumber = $builder->build({ source
=> 'Borrower' })->{'borrowernumber'};
520 my $cli_itemnumber1 = $builder->build({ source
=> 'Item', value
=> { itype
=> $itype_no_replace_no_fee->{itemtype
} } })->{'itemnumber'};
521 my $cli_itemnumber2 = $builder->build({ source
=> 'Item', value
=> { itype
=> $itype_replace_no_fee->{itemtype
} } })->{'itemnumber'};
522 my $cli_itemnumber3 = $builder->build({ source
=> 'Item', value
=> { itype
=> $itype_no_replace_fee->{itemtype
} } })->{'itemnumber'};
523 my $cli_itemnumber4 = $builder->build({ source
=> 'Item', value
=> { itype
=> $itype_replace_fee->{itemtype
} } })->{'itemnumber'};
524 my $duck = Koha
::Items
->find({itemnumber
=>$cli_itemnumber1});
526 t
::lib
::Mocks
::mock_preference
('item-level_itypes', '1');
527 t
::lib
::Mocks
::mock_preference
('useDefaultReplacementCost', '0');
529 C4
::Accounts
::chargelostitem
( $cli_borrowernumber, $cli_itemnumber1, 0, "Perdedor");
530 $lostfine = Koha
::Account
::Lines
->find({ borrowernumber
=> $cli_borrowernumber, itemnumber
=> $cli_itemnumber1, accounttype
=> 'L' });
531 $procfee = Koha
::Account
::Lines
->find({ borrowernumber
=> $cli_borrowernumber, itemnumber
=> $cli_itemnumber1, accounttype
=> 'PF' });
532 ok
( !$lostfine, "No lost fine if no replacementcost or default when pref off");
533 ok
( !$procfee, "No processing fee if no processing fee");
534 C4
::Accounts
::chargelostitem
( $cli_borrowernumber, $cli_itemnumber1, 6.12, "Perdedor");
535 $lostfine = Koha
::Account
::Lines
->find({ borrowernumber
=> $cli_borrowernumber, itemnumber
=> $cli_itemnumber1, accounttype
=> 'L' });
536 $procfee = Koha
::Account
::Lines
->find({ borrowernumber
=> $cli_borrowernumber, itemnumber
=> $cli_itemnumber1, accounttype
=> 'PF' });
537 ok
( $lostfine->amount == 6.12, "Lost fine equals replacementcost when pref off and no default set");
538 ok
( !$procfee, "No processing fee if no processing fee");
541 C4
::Accounts
::chargelostitem
( $cli_borrowernumber, $cli_itemnumber2, 0, "Perdedor");
542 $lostfine = Koha
::Account
::Lines
->find({ borrowernumber
=> $cli_borrowernumber, itemnumber
=> $cli_itemnumber2, accounttype
=> 'L' });
543 $procfee = Koha
::Account
::Lines
->find({ borrowernumber
=> $cli_borrowernumber, itemnumber
=> $cli_itemnumber2, accounttype
=> 'PF' });
544 ok
( !$lostfine, "No lost fine if no replacementcost but default set when pref off");
545 ok
( !$procfee, "No processing fee if no processing fee");
546 C4
::Accounts
::chargelostitem
( $cli_borrowernumber, $cli_itemnumber2, 6.12, "Perdedor");
547 $lostfine = Koha
::Account
::Lines
->find({ borrowernumber
=> $cli_borrowernumber, itemnumber
=> $cli_itemnumber2, accounttype
=> 'L' });
548 $procfee = Koha
::Account
::Lines
->find({ borrowernumber
=> $cli_borrowernumber, itemnumber
=> $cli_itemnumber2, accounttype
=> 'PF' });
549 ok
( $lostfine->amount == 6.12 , "Lost fine equals replacementcost when pref off and default set");
550 ok
( !$procfee, "No processing fee if no processing fee");
553 C4
::Accounts
::chargelostitem
( $cli_borrowernumber, $cli_itemnumber3, 0, "Perdedor");
554 $lostfine = Koha
::Account
::Lines
->find({ borrowernumber
=> $cli_borrowernumber, itemnumber
=> $cli_itemnumber3, accounttype
=> 'L' });
555 $procfee = Koha
::Account
::Lines
->find({ borrowernumber
=> $cli_borrowernumber, itemnumber
=> $cli_itemnumber3, accounttype
=> 'PF' });
556 ok
( !$lostfine, "No lost fine if no replacementcost and no default set when pref off");
557 ok
( $procfee->amount == 8.16, "Processing fee if processing fee");
559 C4
::Accounts
::chargelostitem
( $cli_borrowernumber, $cli_itemnumber3, 6.12, "Perdedor");
560 $lostfine = Koha
::Account
::Lines
->find({ borrowernumber
=> $cli_borrowernumber, itemnumber
=> $cli_itemnumber3, accounttype
=> 'L' });
561 $procfee = Koha
::Account
::Lines
->find({ borrowernumber
=> $cli_borrowernumber, itemnumber
=> $cli_itemnumber3, accounttype
=> 'PF' });
562 ok
( $lostfine->amount == 6.12 , "Lost fine equals replacementcost when pref off and no default set");
563 ok
( $procfee->amount == 8.16, "Processing fee if processing fee");
567 C4
::Accounts
::chargelostitem
( $cli_borrowernumber, $cli_itemnumber4, 0, "Perdedor");
568 $lostfine = Koha
::Account
::Lines
->find({ borrowernumber
=> $cli_borrowernumber, itemnumber
=> $cli_itemnumber4, accounttype
=> 'L' });
569 $procfee = Koha
::Account
::Lines
->find({ borrowernumber
=> $cli_borrowernumber, itemnumber
=> $cli_itemnumber4, accounttype
=> 'PF' });
570 ok
( !$lostfine, "No lost fine if no replacementcost but default set when pref off");
571 ok
( $procfee->amount == 2.04, "Processing fee if processing fee");
573 C4
::Accounts
::chargelostitem
( $cli_borrowernumber, $cli_itemnumber4, 6.12, "Perdedor");
574 $lostfine = Koha
::Account
::Lines
->find({ borrowernumber
=> $cli_borrowernumber, itemnumber
=> $cli_itemnumber4, accounttype
=> 'L' });
575 $procfee = Koha
::Account
::Lines
->find({ borrowernumber
=> $cli_borrowernumber, itemnumber
=> $cli_itemnumber4, accounttype
=> 'PF' });
576 ok
( $lostfine->amount == 6.12 , "Lost fine equals replacementcost when pref off and default set");
577 ok
( $procfee->amount == 2.04, "Processing fee if processing fee");
581 t
::lib
::Mocks
::mock_preference
('useDefaultReplacementCost', '1');
583 C4
::Accounts
::chargelostitem
( $cli_borrowernumber, $cli_itemnumber1, 0, "Perdedor");
584 $lostfine = Koha
::Account
::Lines
->find({ borrowernumber
=> $cli_borrowernumber, itemnumber
=> $cli_itemnumber1, accounttype
=> 'L' });
585 $procfee = Koha
::Account
::Lines
->find({ borrowernumber
=> $cli_borrowernumber, itemnumber
=> $cli_itemnumber1, accounttype
=> 'PF' });
586 ok
( !$lostfine, "No lost fine if no replacementcost or default when pref on");
587 ok
( !$procfee, "No processing fee if no processing fee");
588 C4
::Accounts
::chargelostitem
( $cli_borrowernumber, $cli_itemnumber1, 6.12, "Perdedor");
589 $lostfine = Koha
::Account
::Lines
->find({ borrowernumber
=> $cli_borrowernumber, itemnumber
=> $cli_itemnumber1, accounttype
=> 'L' });
590 $procfee = Koha
::Account
::Lines
->find({ borrowernumber
=> $cli_borrowernumber, itemnumber
=> $cli_itemnumber1, accounttype
=> 'PF' });
591 is
( $lostfine->amount, "6.120000", "Lost fine equals replacementcost when pref on and no default set");
592 ok
( !$procfee, "No processing fee if no processing fee");
594 C4
::Accounts
::chargelostitem
( $cli_borrowernumber, $cli_itemnumber2, 0, "Perdedor");
595 $lostfine = Koha
::Account
::Lines
->find({ borrowernumber
=> $cli_borrowernumber, itemnumber
=> $cli_itemnumber2, accounttype
=> 'L' });
596 $procfee = Koha
::Account
::Lines
->find({ borrowernumber
=> $cli_borrowernumber, itemnumber
=> $cli_itemnumber2, accounttype
=> 'PF' });
597 is
( $lostfine->amount(), "16.320000", "Lost fine is default if no replacementcost but default set when pref on");
598 ok
( !$procfee, "No processing fee if no processing fee");
600 C4
::Accounts
::chargelostitem
( $cli_borrowernumber, $cli_itemnumber2, 6.12, "Perdedor");
601 $lostfine = Koha
::Account
::Lines
->find({ borrowernumber
=> $cli_borrowernumber, itemnumber
=> $cli_itemnumber2, accounttype
=> 'L' });
602 $procfee = Koha
::Account
::Lines
->find({ borrowernumber
=> $cli_borrowernumber, itemnumber
=> $cli_itemnumber2, accounttype
=> 'PF' });
603 is
( $lostfine->amount, "6.120000" , "Lost fine equals replacementcost when pref on and default set");
604 ok
( !$procfee, "No processing fee if no processing fee");
606 C4
::Accounts
::chargelostitem
( $cli_borrowernumber, $cli_itemnumber3, 0, "Perdedor");
607 $lostfine = Koha
::Account
::Lines
->find({ borrowernumber
=> $cli_borrowernumber, itemnumber
=> $cli_itemnumber3, accounttype
=> 'L' });
608 $procfee = Koha
::Account
::Lines
->find({ borrowernumber
=> $cli_borrowernumber, itemnumber
=> $cli_itemnumber3, accounttype
=> 'PF' });
609 ok
( !$lostfine, "No lost fine if no replacementcost and default not set when pref on");
610 is
( $procfee->amount, "8.160000", "Processing fee if processing fee");
612 C4
::Accounts
::chargelostitem
( $cli_borrowernumber, $cli_itemnumber3, 6.12, "Perdedor");
613 $lostfine = Koha
::Account
::Lines
->find({ borrowernumber
=> $cli_borrowernumber, itemnumber
=> $cli_itemnumber3, accounttype
=> 'L' });
614 $procfee = Koha
::Account
::Lines
->find({ borrowernumber
=> $cli_borrowernumber, itemnumber
=> $cli_itemnumber3, accounttype
=> 'PF' });
615 is
( $lostfine->amount, "6.120000", "Lost fine equals replacementcost when pref on and no default set");
616 is
( $procfee->amount, "8.160000", "Processing fee if processing fee");
618 C4
::Accounts
::chargelostitem
( $cli_borrowernumber, $cli_itemnumber4, 0, "Perdedor");
619 $lostfine = Koha
::Account
::Lines
->find({ borrowernumber
=> $cli_borrowernumber, itemnumber
=> $cli_itemnumber4, accounttype
=> 'L' });
620 $procfee = Koha
::Account
::Lines
->find({ borrowernumber
=> $cli_borrowernumber, itemnumber
=> $cli_itemnumber4, accounttype
=> 'PF' });
621 is
( $lostfine->amount, "4.080000", "Lost fine is default if no replacementcost but default set when pref on");
622 is
( $procfee->amount, "2.040000", "Processing fee if processing fee");
625 C4
::Accounts
::chargelostitem
( $cli_borrowernumber, $cli_itemnumber4, 6.12, "Perdedor");
626 $lostfine = Koha
::Account
::Lines
->find({ borrowernumber
=> $cli_borrowernumber, itemnumber
=> $cli_itemnumber4, accounttype
=> 'L' });
627 $procfee = Koha
::Account
::Lines
->find({ borrowernumber
=> $cli_borrowernumber, itemnumber
=> $cli_itemnumber4, accounttype
=> 'PF' });
628 is
( $lostfine->amount, "6.120000", "Lost fine equals replacementcost when pref on and default set");
629 is
( $procfee->amount, "2.040000", "Processing fee if processing fee");
632 subtest
"Koha::Account::non_issues_charges tests" => sub {
635 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
637 my $today = dt_from_string
;
641 Koha
::Account
::Line
->new(
643 borrowernumber
=> $patron->borrowernumber,
646 description
=> 'a Res fee',
647 accounttype
=> 'Res',
648 amountoutstanding
=> $res,
651 Koha
::Account
::Line
->new(
653 borrowernumber
=> $patron->borrowernumber,
656 description
=> 'a Rental fee',
657 accounttype
=> 'Rent',
658 amountoutstanding
=> $rent,
661 Koha
::Account
::Line
->new(
663 borrowernumber
=> $patron->borrowernumber,
666 description
=> 'a Manual invoice fee',
667 accounttype
=> 'Copie',
668 amountoutstanding
=> $manual,
671 Koha
::AuthorisedValue
->new(
673 category
=> 'MANUAL_INV',
674 authorised_value
=> 'Copie',
675 lib
=> 'Fee for copie',
679 my $account = $patron->account;
681 t
::lib
::Mocks
::mock_preference
( 'HoldsInNoissuesCharge', 0 );
682 t
::lib
::Mocks
::mock_preference
( 'RentalsInNoissuesCharge', 0 );
683 t
::lib
::Mocks
::mock_preference
( 'ManInvInNoissuesCharge', 0 );
684 my ( $total, $non_issues_charges ) = ( $account->balance, $account->non_issues_charges );
685 my $other_charges = $total - $non_issues_charges;
688 $res + $rent + $manual,
689 'Total charges should be Res + Rent + Manual'
691 is
( $non_issues_charges, 0,
692 'If 0|0|0 there should not have non issues charges' );
693 is
( $other_charges, 15, 'If 0|0|0 there should only have other charges' );
695 t
::lib
::Mocks
::mock_preference
( 'HoldsInNoissuesCharge', 0 );
696 t
::lib
::Mocks
::mock_preference
( 'RentalsInNoissuesCharge', 0 );
697 t
::lib
::Mocks
::mock_preference
( 'ManInvInNoissuesCharge', 1 );
698 ( $total, $non_issues_charges ) = ( $account->balance, $account->non_issues_charges );
699 $other_charges = $total - $non_issues_charges;
702 $res + $rent + $manual,
703 'Total charges should be Res + Rent + Manual'
705 is
( $non_issues_charges, $manual,
706 'If 0|0|1 Only Manual should be a non issue charge' );
710 'If 0|0|1 Res + Rent should be other charges'
713 t
::lib
::Mocks
::mock_preference
( 'HoldsInNoissuesCharge', 0 );
714 t
::lib
::Mocks
::mock_preference
( 'RentalsInNoissuesCharge', 1 );
715 t
::lib
::Mocks
::mock_preference
( 'ManInvInNoissuesCharge', 0 );
716 ( $total, $non_issues_charges ) = ( $account->balance, $account->non_issues_charges );
717 $other_charges = $total - $non_issues_charges;
720 $res + $rent + $manual,
721 'Total charges should be Res + Rent + Manual'
723 is
( $non_issues_charges, $rent,
724 'If 0|1|0 Only Rental should be a non issue charge' );
728 'If 0|1|0 Rent + Manual should be other charges'
731 t
::lib
::Mocks
::mock_preference
( 'HoldsInNoissuesCharge', 0 );
732 t
::lib
::Mocks
::mock_preference
( 'RentalsInNoissuesCharge', 1 );
733 t
::lib
::Mocks
::mock_preference
( 'ManInvInNoissuesCharge', 1 );
734 ( $total, $non_issues_charges ) = ( $account->balance, $account->non_issues_charges );
735 $other_charges = $total - $non_issues_charges;
738 $res + $rent + $manual,
739 'Total charges should be Res + Rent + Manual'
744 'If 0|1|1 Rent + Manual should be non issues charges'
746 is
( $other_charges, $res, 'If 0|1|1 there should only have other charges' );
748 t
::lib
::Mocks
::mock_preference
( 'HoldsInNoissuesCharge', 1 );
749 t
::lib
::Mocks
::mock_preference
( 'RentalsInNoissuesCharge', 0 );
750 t
::lib
::Mocks
::mock_preference
( 'ManInvInNoissuesCharge', 0 );
751 ( $total, $non_issues_charges ) = ( $account->balance, $account->non_issues_charges );
752 $other_charges = $total - $non_issues_charges;
755 $res + $rent + $manual,
756 'Total charges should be Res + Rent + Manual'
758 is
( $non_issues_charges, $res,
759 'If 1|0|0 Only Res should be non issues charges' );
763 'If 1|0|0 Rent + Manual should be other charges'
766 t
::lib
::Mocks
::mock_preference
( 'HoldsInNoissuesCharge', 1 );
767 t
::lib
::Mocks
::mock_preference
( 'RentalsInNoissuesCharge', 1 );
768 t
::lib
::Mocks
::mock_preference
( 'ManInvInNoissuesCharge', 0 );
769 ( $total, $non_issues_charges ) = ( $account->balance, $account->non_issues_charges );
770 $other_charges = $total - $non_issues_charges;
773 $res + $rent + $manual,
774 'Total charges should be Res + Rent + Manual'
779 'If 1|1|0 Res + Rent should be non issues charges'
781 is
( $other_charges, $manual,
782 'If 1|1|0 Only Manual should be other charges' );
784 t
::lib
::Mocks
::mock_preference
( 'HoldsInNoissuesCharge', 1 );
785 t
::lib
::Mocks
::mock_preference
( 'RentalsInNoissuesCharge', 1 );
786 t
::lib
::Mocks
::mock_preference
( 'ManInvInNoissuesCharge', 1 );
787 ( $total, $non_issues_charges ) = ( $account->balance, $account->non_issues_charges );
788 $other_charges = $total - $non_issues_charges;
791 $res + $rent + $manual,
792 'Total charges should be Res + Rent + Manual'
796 $res + $rent + $manual,
797 'If 1|1|1 Res + Rent + Manual should be non issues charges'
799 is
( $other_charges, 0, 'If 1|1|1 there should not have any other charges' );
802 subtest
"Koha::Account::non_issues_charges tests" => sub {
805 my $patron = $builder->build_object(
807 class => "Koha::Patrons",
811 categorycode
=> $categorycode,
812 branchcode
=> $branchcode
817 my $debit = Koha
::Account
::Line
->new({ borrowernumber
=> $patron->id, date
=> '1900-01-01', amountoutstanding
=> 0 })->store();
818 my $credit = Koha
::Account
::Line
->new({ borrowernumber
=> $patron->id, date
=> '1900-01-01', amountoutstanding
=> -5 })->store();
819 my $offset = Koha
::Account
::Offset
->new({ credit_id
=> $credit->id, debit_id
=> $debit->id, type
=> 'Payment' })->store();
820 purge_zero_balance_fees
( 1 );
821 my $debit_2 = Koha
::Account
::Lines
->find( $debit->id );
822 my $credit_2 = Koha
::Account
::Lines
->find( $credit->id );
823 ok
( $debit_2, 'Debit was correctly not deleted when credit has balance' );
824 ok
( $credit_2, 'Credit was correctly not deleted when credit has balance' );
825 is
( Koha
::Account
::Lines
->count({ borrowernumber
=> $patron->id }), 2, "The 2 account lines still exists" );
827 $debit = Koha
::Account
::Line
->new({ borrowernumber
=> $patron->id, date
=> '1900-01-01', amountoutstanding
=> 5 })->store();
828 $credit = Koha
::Account
::Line
->new({ borrowernumber
=> $patron->id, date
=> '1900-01-01', amountoutstanding
=> 0 })->store();
829 $offset = Koha
::Account
::Offset
->new({ credit_id
=> $credit->id, debit_id
=> $debit->id, type
=> 'Payment' })->store();
830 purge_zero_balance_fees
( 1 );
831 $debit_2 = $credit_2 = undef;
832 $debit_2 = Koha
::Account
::Lines
->find( $debit->id );
833 $credit_2 = Koha
::Account
::Lines
->find( $credit->id );
834 ok
( $debit_2, 'Debit was correctly not deleted when debit has balance' );
835 ok
( $credit_2, 'Credit was correctly not deleted when debit has balance' );
836 is
( Koha
::Account
::Lines
->count({ borrowernumber
=> $patron->id }), 2 + 2, "The 2 + 2 account lines still exists" );
838 $debit = Koha
::Account
::Line
->new({ borrowernumber
=> $patron->id, date
=> '1900-01-01', amountoutstanding
=> 0 })->store();
839 $credit = Koha
::Account
::Line
->new({ borrowernumber
=> $patron->id, date
=> '1900-01-01', amountoutstanding
=> 0 })->store();
840 $offset = Koha
::Account
::Offset
->new({ credit_id
=> $credit->id, debit_id
=> $debit->id, type
=> 'Payment' })->store();
841 purge_zero_balance_fees
( 1 );
842 $debit_2 = Koha
::Account
::Lines
->find( $debit->id );
843 $credit_2 = Koha
::Account
::Lines
->find( $credit->id );
844 ok
( !$debit_2, 'Debit was correctly deleted' );
845 ok
( !$credit_2, 'Credit was correctly deleted' );
846 is
( Koha
::Account
::Lines
->count({ borrowernumber
=> $patron->id }), 2 + 2, "The 2 + 2 account lines still exists, the last 2 have been deleted ok" );
849 subtest
"Koha::Account::Line::void tests" => sub {
854 my $categorycode = $builder->build({ source
=> 'Category' })->{ categorycode
};
855 my $branchcode = $builder->build({ source
=> 'Branch' })->{ branchcode
};
857 my $borrower = Koha
::Patron
->new( {
858 cardnumber
=> 'dariahall',
860 firstname
=> 'Daria',
862 $borrower->categorycode( $categorycode );
863 $borrower->branchcode( $branchcode );
866 my $account = Koha
::Account
->new({ patron_id
=> $borrower->id });
868 my $line1 = Koha
::Account
::Line
->new({ borrowernumber
=> $borrower->borrowernumber, amount
=> 10, amountoutstanding
=> 10 })->store();
869 my $line2 = Koha
::Account
::Line
->new({ borrowernumber
=> $borrower->borrowernumber, amount
=> 20, amountoutstanding
=> 20 })->store();
871 is
( $account->balance(), 30, "Account balance is 30" );
872 is
( $line1->amountoutstanding, 10, 'First fee has amount outstanding of 10' );
873 is
( $line2->amountoutstanding, 20, 'Second fee has amount outstanding of 20' );
875 my $id = $account->pay(
877 lines
=> [$line1, $line2],
882 my $account_payment = Koha
::Account
::Lines
->find( $id );
884 is
( $account->balance(), 0, "Account balance is 0" );
886 $line1->_result->discard_changes();
887 $line2->_result->discard_changes();
888 is
( $line1->amountoutstanding+0, 0, 'First fee has amount outstanding of 0' );
889 is
( $line2->amountoutstanding+0, 0, 'Second fee has amount outstanding of 0' );
891 my $ret = $account_payment->void();
893 is
( ref($ret), 'Koha::Account::Line', 'Void returns the account line' );
894 is
( $account->balance(), 30, "Account balance is again 30" );
896 $account_payment->_result->discard_changes();
897 $line1->_result->discard_changes();
898 $line2->_result->discard_changes();
900 is
( $account_payment->accounttype, 'VOID', 'Voided payment accounttype is VOID' );
901 is
( $account_payment->amount+0, 0, 'Voided payment amount is 0' );
902 is
( $account_payment->amountoutstanding+0, 0, 'Voided payment amount outstanding is 0' );
904 is
( $line1->amountoutstanding+0, 10, 'First fee again has amount outstanding of 10' );
905 is
( $line2->amountoutstanding+0, 20, 'Second fee again has amount outstanding of 20' );
907 # Accountlines that are not credits should be un-voidable
908 my $line1_pre = $line1->unblessed();
909 $ret = $line1->void();
910 $line1->_result->discard_changes();
911 my $line1_post = $line1->unblessed();
912 is
( $ret, undef, 'Attempted void on non-credit returns undef' );
913 is_deeply
( $line1_pre, $line1_post, 'Non-credit account line cannot be voided' )
916 subtest
"Koha::Account::Offset credit & debit tests" => sub {
921 my $categorycode = $builder->build({ source
=> 'Category' })->{ categorycode
};
922 my $branchcode = $builder->build({ source
=> 'Branch' })->{ branchcode
};
924 my $borrower = Koha
::Patron
->new( {
925 cardnumber
=> 'kyliehall',
927 firstname
=> 'Kylie',
929 $borrower->categorycode( $categorycode );
930 $borrower->branchcode( $branchcode );
933 my $account = Koha
::Account
->new({ patron_id
=> $borrower->id });
935 my $line1 = Koha
::Account
::Line
->new({ borrowernumber
=> $borrower->borrowernumber, amount
=> 10, amountoutstanding
=> 10 })->store();
936 my $line2 = Koha
::Account
::Line
->new({ borrowernumber
=> $borrower->borrowernumber, amount
=> 20, amountoutstanding
=> 20 })->store();
938 my $id = $account->pay(
940 lines
=> [$line1, $line2],
945 # Test debit and credit methods for Koha::Account::Offset
946 my $account_offset = Koha
::Account
::Offsets
->find( { credit_id
=> $id, debit_id
=> $line1->id } );
947 is
( $account_offset->debit->id, $line1->id, "Koha::Account::Offset->debit gets correct accountline" );
948 is
( $account_offset->credit->id, $id, "Koha::Account::Offset->credit gets correct accountline" );
950 $account_offset = Koha
::Account
::Offset
->new(
959 is
( $account_offset->debit, undef, "Koha::Account::Offset->debit returns undef if no associated debit" );
960 is
( $account_offset->credit, undef, "Koha::Account::Offset->credit returns undef if no associated credit" );
963 subtest
"Payment notice tests" => sub {
967 Koha
::Account
::Lines
->delete();
968 Koha
::Patrons
->delete();
969 Koha
::Notice
::Messages
->delete();
971 my $categorycode = $builder->build({ source
=> 'Category' })->{ categorycode
};
972 my $branchcode = $builder->build({ source
=> 'Branch' })->{ branchcode
};
974 my $borrower = Koha
::Patron
->new(
976 cardnumber
=> 'chelseahall',
978 firstname
=> 'Chelsea',
979 email
=> 'chelsea@example.com',
980 categorycode
=> $categorycode,
981 branchcode
=> $branchcode,
985 my $account = Koha
::Account
->new({ patron_id
=> $borrower->id });
987 my $line = Koha
::Account
::Line
->new({ borrowernumber
=> $borrower->borrowernumber, amountoutstanding
=> 27 })->store();
989 my $letter = Koha
::Notice
::Templates
->find( { code
=> 'ACCOUNT_PAYMENT' } );
990 $letter->content('[%- USE Price -%]A payment of [% credit.amount * -1 | $Price %] has been applied to your account.');
993 t
::lib
::Mocks
::mock_preference
('UseEmailReceipts', '0');
995 my $id = $account->pay( { amount
=> 1 } );
996 is
( Koha
::Notice
::Messages
->search()->count(), 0, 'Notice for payment not sent if UseEmailReceipts is disabled' );
998 $id = $account->pay( { amount
=> 1, type
=> 'writeoff' } );
999 is
( Koha
::Notice
::Messages
->search()->count(), 0, 'Notice for writeoff not sent if UseEmailReceipts is disabled' );
1001 t
::lib
::Mocks
::mock_preference
('UseEmailReceipts', '1');
1003 $id = $account->pay( { amount
=> 12 } );
1004 my $notice = Koha
::Notice
::Messages
->search()->next();
1005 is
( $notice->subject, 'Account payment', 'Notice subject is correct for payment' );
1006 is
( $notice->letter_code, 'ACCOUNT_PAYMENT', 'Notice letter code is correct for payment' );
1007 is
( $notice->content, 'A payment of 12.00 has been applied to your account.', 'Notice content is correct for payment' );
1010 $letter = Koha
::Notice
::Templates
->find( { code
=> 'ACCOUNT_WRITEOFF' } );
1011 $letter->content('[%- USE Price -%]A writeoff of [% credit.amount * -1 | $Price %] has been applied to your account.');
1014 $id = $account->pay( { amount
=> 13, type
=> 'writeoff' } );
1015 $notice = Koha
::Notice
::Messages
->search()->next();
1016 is
( $notice->subject, 'Account writeoff', 'Notice subject is correct for payment' );
1017 is
( $notice->letter_code, 'ACCOUNT_WRITEOFF', 'Notice letter code is correct for writeoff' );
1018 is
( $notice->content, 'A writeoff of 13.00 has been applied to your account.', 'Notice content is correct for writeoff' );