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 => 33;
25 use t::lib::TestBuilder;
29 use Koha::Account::DebitTypes;
30 use Koha::Account::Lines;
31 use Koha::Account::Offsets;
32 use Koha::Notice::Messages;
33 use Koha::Notice::Templates;
34 use Koha::DateUtils qw( dt_from_string );
36 use C4::Circulation qw( MarkIssueReturned );
39 use_ok('C4::Accounts');
40 use_ok('Koha::Object');
41 use_ok('Koha::Patron');
42 use_ok('Data::Dumper');
45 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,
74 $context->mock( 'interface', sub { return "commandline" } );
75 my $userenv_branchcode = $branchcode;
78 my $itemtype = $builder->build( { source => 'Itemtype' } );
79 my $item = $builder->build( { source => 'Item', value => { itype => $itemtype->{itemtype} } } );
80 my $patron = $builder->build( { source => 'Borrower' } );
81 my $amount = '5.000000';
82 my $description = "Test fee!";
84 my $note = 'Test note!';
86 C4::Accounts::manualinvoice( $patron->{borrowernumber},
87 $item->{itemnumber}, $description, $type, $amount, $note )
89 qr/C4::Accounts::manualinvoice is deprecated in favor of Koha::Account->add_debit/,
90 "deprecation warning received for manualinvoice";
91 my ($accountline) = Koha::Account::Lines->search(
93 borrowernumber => $patron->{borrowernumber}
96 is( $accountline->debit_type_code, $type, 'Debit type set correctly for manualinvoice' );
97 is( $accountline->amount, $amount, 'Accountline amount set correctly for manualinvoice' );
98 ok( $accountline->description =~ /^$description/, 'Accountline description set correctly for manualinvoice' );
99 is( $accountline->note, $note, 'Accountline note set correctly for manualinvoice' );
100 is( $accountline->branchcode, $branchcode, 'Accountline branchcode set correctly for manualinvoice' );
102 $dbh->do(q|DELETE FROM accountlines|);
104 # Testing purge_zero_balance_fees
106 # The 3rd value in the insert is 'days ago' --
111 my $sth = $dbh->prepare(
112 "INSERT INTO accountlines (
119 VALUES ( ?, ?, (select date_sub(CURRENT_DATE, INTERVAL ? DAY) ), ?, ? )"
125 { amount => 0 , days_ago => 0 , description =>'purge_zero_balance_fees should not delete 0 balance fees with date today' , delete => 0 } ,
126 { amount => 0 , days_ago => $days - 1 , description =>'purge_zero_balance_fees should not delete 0 balance fees with date before threshold day' , delete => 0 } ,
127 { amount => 0 , days_ago => $days , description =>'purge_zero_balance_fees should not delete 0 balance fees with date on threshold day' , delete => 0 } ,
128 { amount => 0 , days_ago => $days + 1 , description =>'purge_zero_balance_fees should delete 0 balance fees with date after threshold day' , delete => 1 } ,
129 { amount => undef , days_ago => $days + 1 , description =>'purge_zero_balance_fees should delete NULL balance fees with date after threshold day' , delete => 1 } ,
130 { amount => 5 , days_ago => $days - 1 , description =>'purge_zero_balance_fees should not delete fees with positive amout owed before threshold day' , delete => 0 } ,
131 { amount => 5 , days_ago => $days , description =>'purge_zero_balance_fees should not delete fees with positive amout owed on threshold day' , delete => 0 } ,
132 { amount => 5 , days_ago => $days + 1 , description =>'purge_zero_balance_fees should not delete fees with positive amout owed after threshold day' , delete => 0 } ,
133 { amount => -5 , days_ago => $days - 1 , description =>'purge_zero_balance_fees should not delete fees with negative amout owed before threshold day' , delete => 0 } ,
134 { amount => -5 , days_ago => $days , description =>'purge_zero_balance_fees should not delete fees with negative amout owed on threshold day' , delete => 0 } ,
135 { amount => -5 , days_ago => $days + 1 , description =>'purge_zero_balance_fees should not delete fees with negative amout owed after threshold day' , delete => 0 }
137 my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
138 my $borrower = Koha::Patron->new( { firstname => 'Test', surname => 'Patron', categorycode => $categorycode, branchcode => $branchcode } )->store();
140 for my $data ( @test_data ) {
141 $sth->execute($borrower->borrowernumber, $data->{amount}, $data->{days_ago}, $data->{description}, 'commandline');
144 purge_zero_balance_fees( $days );
146 $sth = $dbh->prepare(
147 "select count(*) = 0 as deleted
149 where description = ?"
153 sub is_delete_correct {
154 my $should_delete = shift;
155 my $description = shift;
156 $sth->execute( $description );
157 my $test = $sth->fetchrow_hashref();
158 is( $test->{deleted}, $should_delete, $description )
161 for my $data (@test_data) {
162 is_delete_correct( $data->{delete}, $data->{description});
165 $dbh->do(q|DELETE FROM accountlines|);
167 subtest "Koha::Account::pay tests" => sub {
172 my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
173 my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode };
175 my $borrower = Koha::Patron->new( {
176 cardnumber => '1234567890',
178 firstname => 'Marty',
180 $borrower->categorycode( $categorycode );
181 $borrower->branchcode( $branchcode );
184 my $account = Koha::Account->new({ patron_id => $borrower->id });
186 my $line1 = $account->add_debit({ type => 'ACCOUNT', amount => 100, interface => 'commandline' });
187 my $line2 = $account->add_debit({ type => 'ACCOUNT', amount => 200, interface => 'commandline' });
189 $sth = $dbh->prepare("SELECT count(*) FROM accountlines");
191 my $count = $sth->fetchrow_array;
192 is($count, 2, 'There is 2 lines as expected');
194 # There is $100 in the account
195 $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
196 my $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
198 for my $line ( @$amountoutstanding ) {
199 $amountleft += $line;
201 is($amountleft, 300, 'The account has 300$ as expected' );
203 # We make a $20 payment
204 my $borrowernumber = $borrower->borrowernumber;
206 my $payment_note = '$20.00 payment note';
207 my $id = $account->pay( { amount => $data, note => $payment_note, payment_type => "TEST_TYPE" } );
209 my $accountline = Koha::Account::Lines->find( $id );
210 is( $accountline->payment_type, "TEST_TYPE", "Payment type passed into pay is set in account line correctly" );
212 # There is now $280 in the account
213 $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
214 $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
216 for my $line ( @$amountoutstanding ) {
217 $amountleft += $line;
219 is($amountleft, 280, 'The account has $280 as expected' );
221 # Is the payment note well registered
222 $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
223 $sth->execute($borrower->borrowernumber);
224 my $note = $sth->fetchrow_array;
225 is($note,'$20.00 payment note', '$20.00 payment note is registered');
227 # We make a -$30 payment (a NEGATIVE payment)
229 $payment_note = '-$30.00 payment note';
230 $account->pay( { amount => $data, note => $payment_note } );
232 # There is now $310 in the account
233 $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
234 $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
236 for my $line ( @$amountoutstanding ) {
237 $amountleft += $line;
239 is($amountleft, 310, 'The account has $310 as expected' );
240 # Is the payment note well registered
241 $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
242 $sth->execute($borrower->borrowernumber);
243 $note = $sth->fetchrow_array;
244 is($note,'-$30.00 payment note', '-$30.00 payment note is registered');
246 #We make a $150 payment ( > 1stLine )
248 $payment_note = '$150.00 payment note';
249 $account->pay( { amount => $data, note => $payment_note } );
251 # There is now $160 in the account
252 $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
253 $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
255 for my $line ( @$amountoutstanding ) {
256 $amountleft += $line;
258 is($amountleft, 160, 'The account has $160 as expected' );
260 # Is the payment note well registered
261 $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
262 $sth->execute($borrower->borrowernumber);
263 $note = $sth->fetchrow_array;
264 is($note,'$150.00 payment note', '$150.00 payment note is registered');
266 #We make a $200 payment ( > amountleft )
268 $payment_note = '$200.00 payment note';
269 $account->pay( { amount => $data, note => $payment_note } );
271 # There is now -$40 in the account
272 $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
273 $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
275 for my $line ( @$amountoutstanding ) {
276 $amountleft += $line;
278 is($amountleft, -40, 'The account has -$40 as expected, (credit situation)' );
280 # Is the payment note well registered
281 $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
282 $sth->execute($borrower->borrowernumber);
283 $note = $sth->fetchrow_array;
284 is($note,'$200.00 payment note', '$200.00 payment note is registered');
286 my $line3 = $account->add_debit({ type => 'ACCOUNT', amount => 42, interface => 'commandline' });
287 my $payment_id = $account->pay( { lines => [$line3], amount => 42 } );
288 my $payment = Koha::Account::Lines->find( $payment_id );
289 is( $payment->amount(), '-42.000000', "Payment paid the specified fine" );
290 $line3 = Koha::Account::Lines->find( $line3->id );
291 is( $line3->amountoutstanding, '0.000000', "Specified fine is paid" );
292 is( $payment->branchcode, undef, 'branchcode passed, then undef' );
295 subtest "Koha::Account::pay particular line tests" => sub {
300 my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
301 my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode };
303 my $borrower = Koha::Patron->new( {
304 cardnumber => 'kylemhall',
308 $borrower->categorycode( $categorycode );
309 $borrower->branchcode( $branchcode );
312 my $account = Koha::Account->new({ patron_id => $borrower->id });
314 my $line1 = $account->add_debit({ type => 'ACCOUNT', amount => 1, interface => 'commandline' });
315 my $line2 = $account->add_debit({ type => 'ACCOUNT', amount => 2, interface => 'commandline' });
316 my $line3 = $account->add_debit({ type => 'ACCOUNT', amount => 3, interface => 'commandline' });
317 my $line4 = $account->add_debit({ type => 'ACCOUNT', amount => 4, interface => 'commandline' });
319 is( $account->balance(), 10, "Account balance is 10" );
323 lines => [$line2, $line3, $line4],
328 $_->_result->discard_changes foreach ( $line1, $line2, $line3, $line4 );
330 # Line1 is not paid at all, as it was not passed in the lines param
331 is( $line1->amountoutstanding, "1.000000", "Line 1 was not paid" );
332 # Line2 was paid in full, as it was the first in the lines list
333 is( $line2->amountoutstanding, "0.000000", "Line 2 was paid in full" );
334 # Line3 was paid partially, as the remaining balance did not cover it entirely
335 is( $line3->amountoutstanding, "1.000000", "Line 3 was paid to 1.00" );
336 # Line4 was not paid at all, as the payment was all used up by that point
337 is( $line4->amountoutstanding, "4.000000", "Line 4 was not paid" );
340 subtest "Koha::Account::pay writeoff tests" => sub {
345 my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
346 my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode };
348 my $borrower = Koha::Patron->new( {
349 cardnumber => 'chelseahall',
351 firstname => 'Chelsea',
353 $borrower->categorycode( $categorycode );
354 $borrower->branchcode( $branchcode );
357 my $account = Koha::Account->new({ patron_id => $borrower->id });
359 my $line = $account->add_debit({ type => 'ACCOUNT', amount => 42, interface => 'commandline' });
361 is( $account->balance(), 42, "Account balance is 42" );
363 my $id = $account->pay(
371 $line->_result->discard_changes();
373 is( $line->amountoutstanding, "0.000000", "Line was written off" );
375 my $writeoff = Koha::Account::Lines->find( $id );
377 is( $writeoff->accounttype, 'W', 'Type is correct for writeoff' );
378 is( $writeoff->description, 'Writeoff', 'Description is correct' );
379 is( $writeoff->amount, '-42.000000', 'Amount is correct' );
382 subtest "More Koha::Account::pay tests" => sub {
387 my $category = $builder->build({ source => 'Category' })->{ categorycode };
388 my $branch = $builder->build({ source => 'Branch' })->{ branchcode };
389 $branchcode = $branch;
390 my $borrowernumber = $builder->build({
391 source => 'Borrower',
392 value => { categorycode => $category,
393 branchcode => $branch }
394 })->{ borrowernumber };
397 my $accountline = $builder->build(
399 source => 'Accountline',
401 borrowernumber => $borrowernumber,
403 amountoutstanding => $amount,
404 accounttype => undef,
409 my $rs = $schema->resultset('Accountline')->search({
410 borrowernumber => $borrowernumber
413 is( $rs->count(), 1, 'Accountline created' );
415 my $account = Koha::Account->new( { patron_id => $borrowernumber } );
416 my $line = Koha::Account::Lines->find( $accountline->{ accountlines_id } );
417 # make the full payment
418 $account->pay({ lines => [$line], amount => $amount, library_id => $branch, note => 'A payment note' });
420 my $offset = Koha::Account::Offsets->search({ debit_id => $accountline->{accountlines_id} })->next();
421 is( $offset->amount(), '-100.000000', 'Offset amount is -100.00' );
422 is( $offset->type(), 'Payment', 'Offset type is Payment' );
424 my $stat = $schema->resultset('Statistic')->search({
427 }, { order_by => { -desc => 'datetime' } })->next();
429 ok( defined $stat, "There's a payment log that matches the branch" );
432 skip "No statistic logged", 4 unless defined $stat;
434 is( $stat->type, 'payment', "Correct statistic type" );
435 is( $stat->branch, $branch, "Correct branch logged to statistics" );
436 is( $stat->borrowernumber, $borrowernumber, "Correct borrowernumber logged to statistics" );
437 is( $stat->value+0, $amount, "Correct amount logged to statistics" );
441 subtest "Even more Koha::Account::pay tests" => sub {
446 my $category = $builder->build({ source => 'Category' })->{ categorycode };
447 my $branch = $builder->build({ source => 'Branch' })->{ branchcode };
448 $branchcode = $branch;
449 my $borrowernumber = $builder->build({
450 source => 'Borrower',
451 value => { categorycode => $category,
452 branchcode => $branch }
453 })->{ borrowernumber };
456 my $partialamount = 60;
457 my $accountline = $builder->build(
459 source => 'Accountline',
461 borrowernumber => $borrowernumber,
463 amountoutstanding => $amount,
464 accounttype => undef,
469 my $rs = $schema->resultset('Accountline')->search({
470 borrowernumber => $borrowernumber
473 is( $rs->count(), 1, 'Accountline created' );
475 my $account = Koha::Account->new( { patron_id => $borrowernumber } );
476 my $line = Koha::Account::Lines->find( $accountline->{ accountlines_id } );
477 # make the full payment
478 $account->pay({ lines => [$line], amount => $partialamount, library_id => $branch, note => 'A payment note' });
480 my $offset = Koha::Account::Offsets->search( { debit_id => $accountline->{ accountlines_id } } )->next();
481 is( $offset->amount, '-60.000000', 'Offset amount is -60.00' );
482 is( $offset->type, 'Payment', 'Offset type is payment' );
484 my $stat = $schema->resultset('Statistic')->search({
487 }, { order_by => { -desc => 'datetime' } })->next();
489 ok( defined $stat, "There's a payment log that matches the branch" );
492 skip "No statistic logged", 4 unless defined $stat;
494 is( $stat->type, 'payment', "Correct statistic type" );
495 is( $stat->branch, $branch, "Correct branch logged to statistics" );
496 is( $stat->borrowernumber, $borrowernumber, "Correct borrowernumber logged to statistics" );
497 is( $stat->value+0, $partialamount, "Correct amount logged to statistics" );
501 subtest 'balance' => sub {
504 my $patron = $builder->build({source => 'Borrower'});
505 $patron = Koha::Patrons->find( $patron->{borrowernumber} );
506 my $account = $patron->account;
507 is( $account->balance, 0, 'balance should return 0 if the patron does not have fines' );
509 my $accountline_1 = $builder->build(
511 source => 'Accountline',
513 borrowernumber => $patron->borrowernumber,
515 amountoutstanding => 42,
516 accounttype => undef,
520 my $accountline_2 = $builder->build(
522 source => 'Accountline',
524 borrowernumber => $patron->borrowernumber,
526 amountoutstanding => -13,
527 debit_type_code => undef,
532 my $balance = $patron->account->balance;
533 is( int($balance), 29, 'balance should return the correct value');
538 subtest "C4::Accounts::chargelostitem tests" => sub {
541 my $branch = $builder->build( { source => 'Branch' } );
542 my $branchcode = $branch->{branchcode};
544 my $staff = $builder->build( { source => 'Borrower' } );
545 my $staff_id = $staff->{borrowernumber};
547 my $module = Test::MockModule->new('C4::Context');
554 branch => $branchcode,
559 my $itype_no_replace_no_fee = $builder->build({ source => 'Itemtype', value => {
561 defaultreplacecost => undef,
564 my $itype_replace_no_fee = $builder->build({ source => 'Itemtype', value => {
566 defaultreplacecost => 16.32,
569 my $itype_no_replace_fee = $builder->build({ source => 'Itemtype', value => {
571 defaultreplacecost => undef,
574 my $itype_replace_fee = $builder->build({ source => 'Itemtype', value => {
576 defaultreplacecost => 4.08,
579 my $cli_borrowernumber = $builder->build({ source => 'Borrower' })->{'borrowernumber'};
580 my $cli_itemnumber1 = $builder->build({ source => 'Item', value => { itype => $itype_no_replace_no_fee->{itemtype} } })->{'itemnumber'};
581 my $cli_itemnumber2 = $builder->build({ source => 'Item', value => { itype => $itype_replace_no_fee->{itemtype} } })->{'itemnumber'};
582 my $cli_itemnumber3 = $builder->build({ source => 'Item', value => { itype => $itype_no_replace_fee->{itemtype} } })->{'itemnumber'};
583 my $cli_itemnumber4 = $builder->build({ source => 'Item', value => { itype => $itype_replace_fee->{itemtype} } })->{'itemnumber'};
585 my $cli_issue_id_1 = $builder->build({ source => 'Issue', value => { borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1 } })->{issue_id};
586 my $cli_issue_id_2 = $builder->build({ source => 'Issue', value => { borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2 } })->{issue_id};
587 my $cli_issue_id_3 = $builder->build({ source => 'Issue', value => { borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3 } })->{issue_id};
588 my $cli_issue_id_4 = $builder->build({ source => 'Issue', value => { borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4 } })->{issue_id};
589 my $cli_issue_id_4X = undef;
594 subtest "fee application tests" => sub {
597 t::lib::Mocks::mock_preference('item-level_itypes', '1');
598 t::lib::Mocks::mock_preference('useDefaultReplacementCost', '0');
600 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber1, 0, "Perdedor");
601 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, debit_type_code => 'LOST' });
602 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, debit_type_code => 'PROCESSING' });
603 ok( !$lostfine, "No lost fine if no replacementcost or default when pref off");
604 ok( !$procfee, "No processing fee if no processing fee");
605 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber1, 6.12, "Perdedor");
606 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, debit_type_code => 'LOST' });
607 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, debit_type_code => 'PROCESSING' });
608 ok( $lostfine->amount == 6.12, "Lost fine equals replacementcost when pref off and no default set");
609 ok( !$procfee, "No processing fee if no processing fee");
612 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber2, 0, "Perdedor");
613 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, debit_type_code => 'LOST' });
614 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, debit_type_code => 'PROCESSING' });
615 ok( !$lostfine, "No lost fine if no replacementcost but default set when pref off");
616 ok( !$procfee, "No processing fee if no processing fee");
617 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber2, 6.12, "Perdedor");
618 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, debit_type_code => 'LOST' });
619 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, debit_type_code => 'PROCESSING' });
620 ok( $lostfine->amount == 6.12 , "Lost fine equals replacementcost when pref off and default set");
621 ok( !$procfee, "No processing fee if no processing fee");
624 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber3, 0, "Perdedor");
625 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, debit_type_code => 'LOST' });
626 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, debit_type_code => 'PROCESSING' });
627 ok( !$lostfine, "No lost fine if no replacementcost and no default set when pref off");
628 ok( $procfee->amount == 8.16, "Processing fee if processing fee");
629 is( $procfee->issue_id, $cli_issue_id_3, "Processing fee issue id is correct" );
631 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber3, 6.12, "Perdedor");
632 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, debit_type_code => 'LOST' });
633 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, debit_type_code => 'PROCESSING' });
634 ok( $lostfine->amount == 6.12 , "Lost fine equals replacementcost when pref off and no default set");
635 ok( $procfee->amount == 8.16, "Processing fee if processing fee");
636 is( $procfee->issue_id, $cli_issue_id_3, "Processing fee issue id is correct" );
640 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 0, "Perdedor");
641 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'LOST' });
642 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'PROCESSING' });
643 ok( !$lostfine, "No lost fine if no replacementcost but default set when pref off");
644 ok( $procfee->amount == 2.04, "Processing fee if processing fee");
645 is( $procfee->issue_id, $cli_issue_id_4, "Processing fee issue id is correct" );
647 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 6.12, "Perdedor");
648 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'LOST' });
649 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'PROCESSING' });
650 ok( $lostfine->amount == 6.12 , "Lost fine equals replacementcost when pref off and default set");
651 ok( $procfee->amount == 2.04, "Processing fee if processing fee");
652 is( $procfee->issue_id, $cli_issue_id_4, "Processing fee issue id is correct" );
656 t::lib::Mocks::mock_preference('useDefaultReplacementCost', '1');
658 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber1, 0, "Perdedor");
659 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, debit_type_code => 'LOST' });
660 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, debit_type_code => 'PROCESSING' });
661 ok( !$lostfine, "No lost fine if no replacementcost or default when pref on");
662 ok( !$procfee, "No processing fee if no processing fee");
663 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber1, 6.12, "Perdedor");
664 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, debit_type_code => 'LOST' });
665 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, debit_type_code => 'PROCESSING' });
666 is( $lostfine->amount, "6.120000", "Lost fine equals replacementcost when pref on and no default set");
667 ok( !$procfee, "No processing fee if no processing fee");
669 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber2, 0, "Perdedor");
670 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, debit_type_code => 'LOST' });
671 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, debit_type_code => 'PROCESSING' });
672 is( $lostfine->amount(), "16.320000", "Lost fine is default if no replacementcost but default set when pref on");
673 ok( !$procfee, "No processing fee if no processing fee");
675 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber2, 6.12, "Perdedor");
676 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, debit_type_code => 'LOST' });
677 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, debit_type_code => 'PROCESSING' });
678 is( $lostfine->amount, "6.120000" , "Lost fine equals replacementcost when pref on and default set");
679 ok( !$procfee, "No processing fee if no processing fee");
681 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber3, 0, "Perdedor");
682 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, debit_type_code => 'LOST' });
683 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, debit_type_code => 'PROCESSING' });
684 ok( !$lostfine, "No lost fine if no replacementcost and default not set when pref on");
685 is( $procfee->amount, "8.160000", "Processing fee if processing fee");
686 is( $procfee->issue_id, $cli_issue_id_3, "Processing fee issue id is correct" );
688 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber3, 6.12, "Perdedor");
689 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, debit_type_code => 'LOST' });
690 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, debit_type_code => 'PROCESSING' });
691 is( $lostfine->amount, "6.120000", "Lost fine equals replacementcost when pref on and no default set");
692 is( $procfee->amount, "8.160000", "Processing fee if processing fee");
693 is( $procfee->issue_id, $cli_issue_id_3, "Processing fee issue id is correct" );
695 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 0, "Perdedor");
696 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'LOST' });
697 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'PROCESSING' });
698 is( $lostfine->amount, "4.080000", "Lost fine is default if no replacementcost but default set when pref on");
699 is( $procfee->amount, "2.040000", "Processing fee if processing fee");
700 is( $procfee->issue_id, $cli_issue_id_4, "Processing fee issue id is correct" );
703 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 6.12, "Perdedor");
704 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'LOST' });
705 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'PROCESSING' });
706 is( $lostfine->amount, "6.120000", "Lost fine equals replacementcost when pref on and default set");
707 is( $procfee->amount, "2.040000", "Processing fee if processing fee");
708 is( $procfee->issue_id, $cli_issue_id_4, "Processing fee issue id is correct" );
709 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 6.12, "Perdedor");
710 my $lostfines = Koha::Account::Lines->search({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'LOST' });
711 my $procfees = Koha::Account::Lines->search({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'PROCESSING' });
712 ok( $lostfines->count == 1 , "Lost fine cannot be double charged for the same issue_id");
713 ok( $procfees->count == 1, "Processing fee cannot be double charged for the same issue_id");
714 MarkIssueReturned($cli_borrowernumber, $cli_itemnumber4);
715 $cli_issue_id_4X = $builder->build({ source => 'Issue', value => { borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4 } })->{issue_id};
716 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 6.12, "Perdedor");
717 $lostfines = Koha::Account::Lines->search({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'LOST' });
718 $procfees = Koha::Account::Lines->search({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'PROCESSING' });
719 ok( $lostfines->count == 2 , "Lost fine can be charged twice for the same item if they are distinct issue_id's");
720 ok( $procfees->count == 2, "Processing fee can be charged twice for the same item if they are distinct issue_id's");
721 $lostfines->delete();
725 subtest "basic fields tests" => sub {
728 t::lib::Mocks::mock_preference('ProcessingFeeNote', 'Test Note');
729 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, '1.99', "Perdedor");
732 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'LOST' });
733 ok($lostfine, "Lost fine created");
734 is($lostfine->manager_id, $staff_id, "Lost fine manager_id set correctly");
735 is($lostfine->issue_id, $cli_issue_id_4X, "Lost fine issue_id set correctly");
736 is($lostfine->description, "Perdedor", "Lost fine issue_id set correctly");
737 is($lostfine->note, '', "Lost fine does not contain a note");
738 is($lostfine->branchcode, $branchcode, "Lost fine branchcode set correctly");
741 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'PROCESSING' });
742 ok($procfee, "Processing fee created");
743 is($procfee->manager_id, $staff_id, "Processing fee manager_id set correctly");
744 is($procfee->issue_id, $cli_issue_id_4X, "Processing fee issue_id set correctly");
745 is($procfee->description, "Perdedor", "Processing fee issue_id set correctly");
746 is($procfee->note, C4::Context->preference("ProcessingFeeNote"), "Processing fee contains note matching ProcessingFeeNote");
747 is($procfee->branchcode, $branchcode, "Processing fee branchcode set correctly");
752 subtest "FinesLog tests" => sub {
755 my $action_logs = $schema->resultset('ActionLog')->search()->count;
757 t::lib::Mocks::mock_preference( 'FinesLog', 0 );
758 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 0, "Perdedor");
759 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'LOST' });
760 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'PROCESSING' });
761 is( $schema->resultset('ActionLog')->count(), $action_logs + 0, 'No logs were added' );
765 t::lib::Mocks::mock_preference( 'FinesLog', 1 );
766 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 0, "Perdedor");
767 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'LOST' });
768 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'PROCESSING' });
769 is( $schema->resultset('ActionLog')->count(), $action_logs + 2, 'Logs were added' );
774 # Cleanup - this must be replaced with a transaction per subtest
775 Koha::Patrons->find($cli_borrowernumber)->checkouts->delete;
778 subtest "Koha::Account::non_issues_charges tests" => sub {
781 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
782 my $account = $patron->account;
784 my $today = dt_from_string;
790 description => 'a Res fee',
793 interface => 'commandline'
798 description => 'a Rental fee',
801 interface => 'commandline'
804 Koha::Account::DebitTypes->find_or_create(
807 description => 'Fee for copie',
811 Koha::Account::Line->new(
813 borrowernumber => $patron->borrowernumber,
815 description => 'a Manual invoice fee',
816 debit_type_code => 'Copie',
817 amountoutstanding => $manual,
818 interface => 'commandline'
823 t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge', 0 );
824 t::lib::Mocks::mock_preference( 'RentalsInNoissuesCharge', 0 );
825 t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge', 0 );
826 my ( $total, $non_issues_charges ) = ( $account->balance, $account->non_issues_charges );
827 my $other_charges = $total - $non_issues_charges;
830 $res + $rent + $manual,
831 'Total charges should be Res + Rent + Manual'
833 is( $non_issues_charges, 0,
834 'If 0|0|0 there should not have non issues charges' );
835 is( $other_charges, 15, 'If 0|0|0 there should only have other charges' );
837 t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge', 0 );
838 t::lib::Mocks::mock_preference( 'RentalsInNoissuesCharge', 0 );
839 t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge', 1 );
840 ( $total, $non_issues_charges ) = ( $account->balance, $account->non_issues_charges );
841 $other_charges = $total - $non_issues_charges;
844 $res + $rent + $manual,
845 'Total charges should be Res + Rent + Manual'
847 is( $non_issues_charges, $manual,
848 'If 0|0|1 Only Manual should be a non issue charge' );
852 'If 0|0|1 Res + Rent should be other charges'
855 t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge', 0 );
856 t::lib::Mocks::mock_preference( 'RentalsInNoissuesCharge', 1 );
857 t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge', 0 );
858 ( $total, $non_issues_charges ) = ( $account->balance, $account->non_issues_charges );
859 $other_charges = $total - $non_issues_charges;
862 $res + $rent + $manual,
863 'Total charges should be Res + Rent + Manual'
865 is( $non_issues_charges, $rent,
866 'If 0|1|0 Only Rental should be a non issue charge' );
870 'If 0|1|0 Rent + Manual should be other charges'
873 t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge', 0 );
874 t::lib::Mocks::mock_preference( 'RentalsInNoissuesCharge', 1 );
875 t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge', 1 );
876 ( $total, $non_issues_charges ) = ( $account->balance, $account->non_issues_charges );
877 $other_charges = $total - $non_issues_charges;
880 $res + $rent + $manual,
881 'Total charges should be Res + Rent + Manual'
886 'If 0|1|1 Rent + Manual should be non issues charges'
888 is( $other_charges, $res, 'If 0|1|1 there should only have other charges' );
890 t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge', 1 );
891 t::lib::Mocks::mock_preference( 'RentalsInNoissuesCharge', 0 );
892 t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge', 0 );
893 ( $total, $non_issues_charges ) = ( $account->balance, $account->non_issues_charges );
894 $other_charges = $total - $non_issues_charges;
897 $res + $rent + $manual,
898 'Total charges should be Res + Rent + Manual'
900 is( $non_issues_charges, $res,
901 'If 1|0|0 Only Res should be non issues charges' );
905 'If 1|0|0 Rent + Manual should be other charges'
908 t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge', 1 );
909 t::lib::Mocks::mock_preference( 'RentalsInNoissuesCharge', 1 );
910 t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge', 0 );
911 ( $total, $non_issues_charges ) = ( $account->balance, $account->non_issues_charges );
912 $other_charges = $total - $non_issues_charges;
915 $res + $rent + $manual,
916 'Total charges should be Res + Rent + Manual'
921 'If 1|1|0 Res + Rent should be non issues charges'
923 is( $other_charges, $manual,
924 'If 1|1|0 Only Manual should be other charges' );
926 t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge', 1 );
927 t::lib::Mocks::mock_preference( 'RentalsInNoissuesCharge', 1 );
928 t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge', 1 );
929 ( $total, $non_issues_charges ) = ( $account->balance, $account->non_issues_charges );
930 $other_charges = $total - $non_issues_charges;
933 $res + $rent + $manual,
934 'Total charges should be Res + Rent + Manual'
938 $res + $rent + $manual,
939 'If 1|1|1 Res + Rent + Manual should be non issues charges'
941 is( $other_charges, 0, 'If 1|1|1 there should not have any other charges' );
944 subtest "Koha::Account::non_issues_charges tests" => sub {
947 my $patron = $builder->build_object(
949 class => "Koha::Patrons",
953 categorycode => $categorycode,
954 branchcode => $branchcode
959 my $debit = Koha::Account::Line->new({ borrowernumber => $patron->id, date => '1900-01-01', amountoutstanding => 0, interface => 'commandline' })->store();
960 my $credit = Koha::Account::Line->new({ borrowernumber => $patron->id, date => '1900-01-01', amountoutstanding => -5, interface => 'commandline' })->store();
961 my $offset = Koha::Account::Offset->new({ credit_id => $credit->id, debit_id => $debit->id, type => 'Payment', amount => 0 })->store();
962 purge_zero_balance_fees( 1 );
963 my $debit_2 = Koha::Account::Lines->find( $debit->id );
964 my $credit_2 = Koha::Account::Lines->find( $credit->id );
965 ok( $debit_2, 'Debit was correctly not deleted when credit has balance' );
966 ok( $credit_2, 'Credit was correctly not deleted when credit has balance' );
967 is( Koha::Account::Lines->count({ borrowernumber => $patron->id }), 2, "The 2 account lines still exists" );
969 $debit = Koha::Account::Line->new({ borrowernumber => $patron->id, date => '1900-01-01', amountoutstanding => 5, interface => 'commanline' })->store();
970 $credit = Koha::Account::Line->new({ borrowernumber => $patron->id, date => '1900-01-01', amountoutstanding => 0, interface => 'commandline' })->store();
971 $offset = Koha::Account::Offset->new({ credit_id => $credit->id, debit_id => $debit->id, type => 'Payment', amount => 0 })->store();
972 purge_zero_balance_fees( 1 );
973 $debit_2 = $credit_2 = undef;
974 $debit_2 = Koha::Account::Lines->find( $debit->id );
975 $credit_2 = Koha::Account::Lines->find( $credit->id );
976 ok( $debit_2, 'Debit was correctly not deleted when debit has balance' );
977 ok( $credit_2, 'Credit was correctly not deleted when debit has balance' );
978 is( Koha::Account::Lines->count({ borrowernumber => $patron->id }), 2 + 2, "The 2 + 2 account lines still exists" );
980 $debit = Koha::Account::Line->new({ borrowernumber => $patron->id, date => '1900-01-01', amountoutstanding => 0, interface => 'commandline' })->store();
981 $credit = Koha::Account::Line->new({ borrowernumber => $patron->id, date => '1900-01-01', amountoutstanding => 0, interface => 'commandline' })->store();
982 $offset = Koha::Account::Offset->new({ credit_id => $credit->id, debit_id => $debit->id, type => 'Payment', amount => 0 })->store();
983 purge_zero_balance_fees( 1 );
984 $debit_2 = Koha::Account::Lines->find( $debit->id );
985 $credit_2 = Koha::Account::Lines->find( $credit->id );
986 ok( !$debit_2, 'Debit was correctly deleted' );
987 ok( !$credit_2, 'Credit was correctly deleted' );
988 is( Koha::Account::Lines->count({ borrowernumber => $patron->id }), 2 + 2, "The 2 + 2 account lines still exists, the last 2 have been deleted ok" );
992 subtest "Koha::Account::Offset credit & debit tests" => sub {
997 my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
998 my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode };
1000 my $borrower = Koha::Patron->new( {
1001 cardnumber => 'kyliehall',
1003 firstname => 'Kylie',
1005 $borrower->categorycode( $categorycode );
1006 $borrower->branchcode( $branchcode );
1009 my $account = Koha::Account->new({ patron_id => $borrower->id });
1011 my $line1 = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amount => 10, amountoutstanding => 10, interface => 'commandline' })->store();
1012 my $line2 = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amount => 20, amountoutstanding => 20, interface => 'commandline' })->store();
1014 my $id = $account->pay(
1016 lines => [$line1, $line2],
1021 # Test debit and credit methods for Koha::Account::Offset
1022 my $account_offset = Koha::Account::Offsets->find( { credit_id => $id, debit_id => $line1->id } );
1023 is( $account_offset->debit->id, $line1->id, "Koha::Account::Offset->debit gets correct accountline" );
1024 is( $account_offset->credit->id, $id, "Koha::Account::Offset->credit gets correct accountline" );
1026 $account_offset = Koha::Account::Offset->new(
1035 is( $account_offset->debit, undef, "Koha::Account::Offset->debit returns undef if no associated debit" );
1036 is( $account_offset->credit, undef, "Koha::Account::Offset->credit returns undef if no associated credit" );
1039 subtest "Payment notice tests" => sub {
1043 Koha::Account::Lines->delete();
1044 Koha::Patrons->delete();
1045 Koha::Notice::Messages->delete();
1047 my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
1048 my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode };
1050 my $borrower = Koha::Patron->new(
1052 cardnumber => 'chelseahall',
1054 firstname => 'Chelsea',
1055 email => 'chelsea@example.com',
1056 categorycode => $categorycode,
1057 branchcode => $branchcode,
1061 my $manager = $builder->build_object({ class => "Koha::Patrons" });
1062 my $context = new Test::MockModule('C4::Context');
1063 $context->mock( 'userenv', sub {
1065 number => $manager->borrowernumber,
1066 branch => $manager->branchcode,
1069 my $account = Koha::Account->new({ patron_id => $borrower->id });
1071 my $line = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amountoutstanding => 27, interface => 'commandline' })->store();
1073 my $letter = Koha::Notice::Templates->find( { code => 'ACCOUNT_PAYMENT' } );
1074 $letter->content('[%- USE Price -%]A payment of [% credit.amount * -1 | $Price %] has been applied to your account.');
1077 t::lib::Mocks::mock_preference('UseEmailReceipts', '0');
1078 my $id = $account->pay( { amount => 1 } );
1079 is( Koha::Notice::Messages->search()->count(), 0, 'Notice for payment not sent if UseEmailReceipts is disabled' );
1081 $id = $account->pay( { amount => 1, type => 'writeoff' } );
1082 is( Koha::Notice::Messages->search()->count(), 0, 'Notice for writeoff not sent if UseEmailReceipts is disabled' );
1084 t::lib::Mocks::mock_preference('UseEmailReceipts', '1');
1086 $id = $account->pay( { amount => 12 } );
1087 my $notice = Koha::Notice::Messages->search()->next();
1088 is( $notice->subject, 'Account payment', 'Notice subject is correct for payment' );
1089 is( $notice->letter_code, 'ACCOUNT_PAYMENT', 'Notice letter code is correct for payment' );
1090 is( $notice->content, 'A payment of 12.00 has been applied to your account.', 'Notice content is correct for payment' );
1093 $letter = Koha::Notice::Templates->find( { code => 'ACCOUNT_WRITEOFF' } );
1094 $letter->content('[%- USE Price -%]A writeoff of [% credit.amount * -1 | $Price %] has been applied to your account.');
1097 $id = $account->pay( { amount => 13, type => 'writeoff' } );
1098 $notice = Koha::Notice::Messages->search()->next();
1099 is( $notice->subject, 'Account writeoff', 'Notice subject is correct for payment' );
1100 is( $notice->letter_code, 'ACCOUNT_WRITEOFF', 'Notice letter code is correct for writeoff' );
1101 is( $notice->content, 'A writeoff of 13.00 has been applied to your account.', 'Notice content is correct for writeoff' );