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 (
121 VALUES ( ?, ?, (select date_sub(CURRENT_DATE, INTERVAL ? DAY) ), ?, ?, ?, ? )"
127 { amount => 0 , days_ago => 0 , description =>'purge_zero_balance_fees should not delete 0 balance fees with date today' , delete => 0 } ,
128 { amount => 0 , days_ago => $days - 1 , description =>'purge_zero_balance_fees should not delete 0 balance fees with date before threshold day' , delete => 0 } ,
129 { amount => 0 , days_ago => $days , description =>'purge_zero_balance_fees should not delete 0 balance fees with date on threshold day' , delete => 0 } ,
130 { amount => 0 , days_ago => $days + 1 , description =>'purge_zero_balance_fees should delete 0 balance fees with date after threshold day' , delete => 1 } ,
131 { amount => undef , days_ago => $days + 1 , description =>'purge_zero_balance_fees should delete NULL balance fees with date after threshold day' , delete => 1 } ,
132 { amount => 5 , days_ago => $days - 1 , description =>'purge_zero_balance_fees should not delete fees with positive amout owed before threshold day' , delete => 0 } ,
133 { amount => 5 , days_ago => $days , description =>'purge_zero_balance_fees should not delete fees with positive amout owed on threshold day' , delete => 0 } ,
134 { amount => 5 , days_ago => $days + 1 , description =>'purge_zero_balance_fees should not delete fees with positive amout owed after threshold day' , delete => 0 } ,
135 { amount => -5 , days_ago => $days - 1 , description =>'purge_zero_balance_fees should not delete fees with negative amout owed before threshold day' , delete => 0 } ,
136 { amount => -5 , days_ago => $days , description =>'purge_zero_balance_fees should not delete fees with negative amout owed on threshold day' , delete => 0 } ,
137 { amount => -5 , days_ago => $days + 1 , description =>'purge_zero_balance_fees should not delete fees with negative amout owed after threshold day' , delete => 0 }
139 my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
140 my $borrower = Koha::Patron->new( { firstname => 'Test', surname => 'Patron', categorycode => $categorycode, branchcode => $branchcode } )->store();
142 for my $data ( @test_data ) {
143 $sth->execute($borrower->borrowernumber, $data->{amount}, $data->{days_ago}, $data->{description}, 'commandline', $data->{amount} > 0 ? 'W' : undef, $data->{amount} >= 0 ? undef : 'OVERDUE' );
146 purge_zero_balance_fees( $days );
148 $sth = $dbh->prepare(
149 "select count(*) = 0 as deleted
151 where description = ?"
155 sub is_delete_correct {
156 my $should_delete = shift;
157 my $description = shift;
158 $sth->execute( $description );
159 my $test = $sth->fetchrow_hashref();
160 is( $test->{deleted}, $should_delete, $description )
163 for my $data (@test_data) {
164 is_delete_correct( $data->{delete}, $data->{description});
167 $dbh->do(q|DELETE FROM accountlines|);
169 subtest "Koha::Account::pay tests" => sub {
174 my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
175 my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode };
177 my $borrower = Koha::Patron->new( {
178 cardnumber => '1234567890',
180 firstname => 'Marty',
182 $borrower->categorycode( $categorycode );
183 $borrower->branchcode( $branchcode );
186 my $account = Koha::Account->new({ patron_id => $borrower->id });
188 my $line1 = $account->add_debit({ type => 'ACCOUNT', amount => 100, interface => 'commandline' });
189 my $line2 = $account->add_debit({ type => 'ACCOUNT', amount => 200, interface => 'commandline' });
191 $sth = $dbh->prepare("SELECT count(*) FROM accountlines");
193 my $count = $sth->fetchrow_array;
194 is($count, 2, 'There is 2 lines as expected');
196 # There is $100 in the account
197 $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
198 my $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
200 for my $line ( @$amountoutstanding ) {
201 $amountleft += $line;
203 is($amountleft, 300, 'The account has 300$ as expected' );
205 # We make a $20 payment
206 my $borrowernumber = $borrower->borrowernumber;
208 my $payment_note = '$20.00 payment note';
209 my $id = $account->pay( { amount => $data, note => $payment_note, payment_type => "TEST_TYPE" } );
211 my $accountline = Koha::Account::Lines->find( $id );
212 is( $accountline->payment_type, "TEST_TYPE", "Payment type passed into pay is set in account line correctly" );
214 # There is now $280 in the account
215 $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
216 $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
218 for my $line ( @$amountoutstanding ) {
219 $amountleft += $line;
221 is($amountleft, 280, 'The account has $280 as expected' );
223 # Is the payment note well registered
224 $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
225 $sth->execute($borrower->borrowernumber);
226 my $note = $sth->fetchrow_array;
227 is($note,'$20.00 payment note', '$20.00 payment note is registered');
229 # We make a -$30 payment (a NEGATIVE payment)
231 $payment_note = '-$30.00 payment note';
232 $account->pay( { amount => $data, note => $payment_note } );
234 # There is now $310 in the account
235 $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
236 $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
238 for my $line ( @$amountoutstanding ) {
239 $amountleft += $line;
241 is($amountleft, 310, 'The account has $310 as expected' );
242 # Is the payment note well registered
243 $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
244 $sth->execute($borrower->borrowernumber);
245 $note = $sth->fetchrow_array;
246 is($note,'-$30.00 payment note', '-$30.00 payment note is registered');
248 #We make a $150 payment ( > 1stLine )
250 $payment_note = '$150.00 payment note';
251 $account->pay( { amount => $data, note => $payment_note } );
253 # There is now $160 in the account
254 $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
255 $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
257 for my $line ( @$amountoutstanding ) {
258 $amountleft += $line;
260 is($amountleft, 160, 'The account has $160 as expected' );
262 # Is the payment note well registered
263 $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
264 $sth->execute($borrower->borrowernumber);
265 $note = $sth->fetchrow_array;
266 is($note,'$150.00 payment note', '$150.00 payment note is registered');
268 #We make a $200 payment ( > amountleft )
270 $payment_note = '$200.00 payment note';
271 $account->pay( { amount => $data, note => $payment_note } );
273 # There is now -$40 in the account
274 $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
275 $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
277 for my $line ( @$amountoutstanding ) {
278 $amountleft += $line;
280 is($amountleft, -40, 'The account has -$40 as expected, (credit situation)' );
282 # Is the payment note well registered
283 $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
284 $sth->execute($borrower->borrowernumber);
285 $note = $sth->fetchrow_array;
286 is($note,'$200.00 payment note', '$200.00 payment note is registered');
288 my $line3 = $account->add_debit({ type => 'ACCOUNT', amount => 42, interface => 'commandline' });
289 my $payment_id = $account->pay( { lines => [$line3], amount => 42 } );
290 my $payment = Koha::Account::Lines->find( $payment_id );
291 is( $payment->amount(), '-42.000000', "Payment paid the specified fine" );
292 $line3 = Koha::Account::Lines->find( $line3->id );
293 is( $line3->amountoutstanding, '0.000000', "Specified fine is paid" );
294 is( $payment->branchcode, undef, 'branchcode passed, then undef' );
297 subtest "Koha::Account::pay particular line tests" => sub {
302 my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
303 my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode };
305 my $borrower = Koha::Patron->new( {
306 cardnumber => 'kylemhall',
310 $borrower->categorycode( $categorycode );
311 $borrower->branchcode( $branchcode );
314 my $account = Koha::Account->new({ patron_id => $borrower->id });
316 my $line1 = $account->add_debit({ type => 'ACCOUNT', amount => 1, interface => 'commandline' });
317 my $line2 = $account->add_debit({ type => 'ACCOUNT', amount => 2, interface => 'commandline' });
318 my $line3 = $account->add_debit({ type => 'ACCOUNT', amount => 3, interface => 'commandline' });
319 my $line4 = $account->add_debit({ type => 'ACCOUNT', amount => 4, interface => 'commandline' });
321 is( $account->balance(), 10, "Account balance is 10" );
325 lines => [$line2, $line3, $line4],
330 $_->_result->discard_changes foreach ( $line1, $line2, $line3, $line4 );
332 # Line1 is not paid at all, as it was not passed in the lines param
333 is( $line1->amountoutstanding, "1.000000", "Line 1 was not paid" );
334 # Line2 was paid in full, as it was the first in the lines list
335 is( $line2->amountoutstanding, "0.000000", "Line 2 was paid in full" );
336 # Line3 was paid partially, as the remaining balance did not cover it entirely
337 is( $line3->amountoutstanding, "1.000000", "Line 3 was paid to 1.00" );
338 # Line4 was not paid at all, as the payment was all used up by that point
339 is( $line4->amountoutstanding, "4.000000", "Line 4 was not paid" );
342 subtest "Koha::Account::pay writeoff tests" => sub {
347 my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
348 my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode };
350 my $borrower = Koha::Patron->new( {
351 cardnumber => 'chelseahall',
353 firstname => 'Chelsea',
355 $borrower->categorycode( $categorycode );
356 $borrower->branchcode( $branchcode );
359 my $account = Koha::Account->new({ patron_id => $borrower->id });
361 my $line = $account->add_debit({ type => 'ACCOUNT', amount => 42, interface => 'commandline' });
363 is( $account->balance(), 42, "Account balance is 42" );
365 my $id = $account->pay(
373 $line->_result->discard_changes();
375 is( $line->amountoutstanding, "0.000000", "Line was written off" );
377 my $writeoff = Koha::Account::Lines->find( $id );
379 is( $writeoff->accounttype, 'W', 'Type is correct for writeoff' );
380 is( $writeoff->description, 'Writeoff', 'Description is correct' );
381 is( $writeoff->amount, '-42.000000', 'Amount is correct' );
384 subtest "More Koha::Account::pay tests" => sub {
389 my $category = $builder->build({ source => 'Category' })->{ categorycode };
390 my $branch = $builder->build({ source => 'Branch' })->{ branchcode };
391 $branchcode = $branch;
392 my $borrowernumber = $builder->build({
393 source => 'Borrower',
394 value => { categorycode => $category,
395 branchcode => $branch }
396 })->{ borrowernumber };
399 my $accountline = $builder->build(
401 source => 'Accountline',
403 borrowernumber => $borrowernumber,
405 amountoutstanding => $amount,
406 accounttype => undef,
411 my $rs = $schema->resultset('Accountline')->search({
412 borrowernumber => $borrowernumber
415 is( $rs->count(), 1, 'Accountline created' );
417 my $account = Koha::Account->new( { patron_id => $borrowernumber } );
418 my $line = Koha::Account::Lines->find( $accountline->{ accountlines_id } );
419 # make the full payment
420 $account->pay({ lines => [$line], amount => $amount, library_id => $branch, note => 'A payment note' });
422 my $offset = Koha::Account::Offsets->search({ debit_id => $accountline->{accountlines_id} })->next();
423 is( $offset->amount(), '-100.000000', 'Offset amount is -100.00' );
424 is( $offset->type(), 'Payment', 'Offset type is Payment' );
426 my $stat = $schema->resultset('Statistic')->search({
429 }, { order_by => { -desc => 'datetime' } })->next();
431 ok( defined $stat, "There's a payment log that matches the branch" );
434 skip "No statistic logged", 4 unless defined $stat;
436 is( $stat->type, 'payment', "Correct statistic type" );
437 is( $stat->branch, $branch, "Correct branch logged to statistics" );
438 is( $stat->borrowernumber, $borrowernumber, "Correct borrowernumber logged to statistics" );
439 is( $stat->value+0, $amount, "Correct amount logged to statistics" );
443 subtest "Even more Koha::Account::pay tests" => sub {
448 my $category = $builder->build({ source => 'Category' })->{ categorycode };
449 my $branch = $builder->build({ source => 'Branch' })->{ branchcode };
450 $branchcode = $branch;
451 my $borrowernumber = $builder->build({
452 source => 'Borrower',
453 value => { categorycode => $category,
454 branchcode => $branch }
455 })->{ borrowernumber };
458 my $partialamount = 60;
459 my $accountline = $builder->build(
461 source => 'Accountline',
463 borrowernumber => $borrowernumber,
465 amountoutstanding => $amount,
466 accounttype => undef,
471 my $rs = $schema->resultset('Accountline')->search({
472 borrowernumber => $borrowernumber
475 is( $rs->count(), 1, 'Accountline created' );
477 my $account = Koha::Account->new( { patron_id => $borrowernumber } );
478 my $line = Koha::Account::Lines->find( $accountline->{ accountlines_id } );
479 # make the full payment
480 $account->pay({ lines => [$line], amount => $partialamount, library_id => $branch, note => 'A payment note' });
482 my $offset = Koha::Account::Offsets->search( { debit_id => $accountline->{ accountlines_id } } )->next();
483 is( $offset->amount, '-60.000000', 'Offset amount is -60.00' );
484 is( $offset->type, 'Payment', 'Offset type is payment' );
486 my $stat = $schema->resultset('Statistic')->search({
489 }, { order_by => { -desc => 'datetime' } })->next();
491 ok( defined $stat, "There's a payment log that matches the branch" );
494 skip "No statistic logged", 4 unless defined $stat;
496 is( $stat->type, 'payment', "Correct statistic type" );
497 is( $stat->branch, $branch, "Correct branch logged to statistics" );
498 is( $stat->borrowernumber, $borrowernumber, "Correct borrowernumber logged to statistics" );
499 is( $stat->value+0, $partialamount, "Correct amount logged to statistics" );
503 subtest 'balance' => sub {
506 my $patron = $builder->build({source => 'Borrower'});
507 $patron = Koha::Patrons->find( $patron->{borrowernumber} );
508 my $account = $patron->account;
509 is( $account->balance, 0, 'balance should return 0 if the patron does not have fines' );
511 my $accountline_1 = $builder->build(
513 source => 'Accountline',
515 borrowernumber => $patron->borrowernumber,
517 amountoutstanding => 42,
518 accounttype => undef,
522 my $accountline_2 = $builder->build(
524 source => 'Accountline',
526 borrowernumber => $patron->borrowernumber,
528 amountoutstanding => -13,
529 debit_type_code => undef,
534 my $balance = $patron->account->balance;
535 is( int($balance), 29, 'balance should return the correct value');
540 subtest "C4::Accounts::chargelostitem tests" => sub {
543 my $branch = $builder->build( { source => 'Branch' } );
544 my $branchcode = $branch->{branchcode};
546 my $staff = $builder->build( { source => 'Borrower' } );
547 my $staff_id = $staff->{borrowernumber};
549 my $module = Test::MockModule->new('C4::Context');
556 branch => $branchcode,
561 my $itype_no_replace_no_fee = $builder->build({ source => 'Itemtype', value => {
563 defaultreplacecost => undef,
566 my $itype_replace_no_fee = $builder->build({ source => 'Itemtype', value => {
568 defaultreplacecost => 16.32,
571 my $itype_no_replace_fee = $builder->build({ source => 'Itemtype', value => {
573 defaultreplacecost => undef,
576 my $itype_replace_fee = $builder->build({ source => 'Itemtype', value => {
578 defaultreplacecost => 4.08,
581 my $cli_borrowernumber = $builder->build({ source => 'Borrower' })->{'borrowernumber'};
582 my $cli_itemnumber1 = $builder->build({ source => 'Item', value => { itype => $itype_no_replace_no_fee->{itemtype} } })->{'itemnumber'};
583 my $cli_itemnumber2 = $builder->build({ source => 'Item', value => { itype => $itype_replace_no_fee->{itemtype} } })->{'itemnumber'};
584 my $cli_itemnumber3 = $builder->build({ source => 'Item', value => { itype => $itype_no_replace_fee->{itemtype} } })->{'itemnumber'};
585 my $cli_itemnumber4 = $builder->build({ source => 'Item', value => { itype => $itype_replace_fee->{itemtype} } })->{'itemnumber'};
587 my $cli_issue_id_1 = $builder->build({ source => 'Issue', value => { borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1 } })->{issue_id};
588 my $cli_issue_id_2 = $builder->build({ source => 'Issue', value => { borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2 } })->{issue_id};
589 my $cli_issue_id_3 = $builder->build({ source => 'Issue', value => { borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3 } })->{issue_id};
590 my $cli_issue_id_4 = $builder->build({ source => 'Issue', value => { borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4 } })->{issue_id};
591 my $cli_issue_id_4X = undef;
596 subtest "fee application tests" => sub {
599 t::lib::Mocks::mock_preference('item-level_itypes', '1');
600 t::lib::Mocks::mock_preference('useDefaultReplacementCost', '0');
602 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber1, 0, "Perdedor");
603 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, debit_type_code => 'LOST' });
604 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, debit_type_code => 'PROCESSING' });
605 ok( !$lostfine, "No lost fine if no replacementcost or default when pref off");
606 ok( !$procfee, "No processing fee if no processing fee");
607 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber1, 6.12, "Perdedor");
608 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, debit_type_code => 'LOST' });
609 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, debit_type_code => 'PROCESSING' });
610 ok( $lostfine->amount == 6.12, "Lost fine equals replacementcost when pref off and no default set");
611 ok( !$procfee, "No processing fee if no processing fee");
614 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber2, 0, "Perdedor");
615 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, debit_type_code => 'LOST' });
616 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, debit_type_code => 'PROCESSING' });
617 ok( !$lostfine, "No lost fine if no replacementcost but default set when pref off");
618 ok( !$procfee, "No processing fee if no processing fee");
619 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber2, 6.12, "Perdedor");
620 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, debit_type_code => 'LOST' });
621 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, debit_type_code => 'PROCESSING' });
622 ok( $lostfine->amount == 6.12 , "Lost fine equals replacementcost when pref off and default set");
623 ok( !$procfee, "No processing fee if no processing fee");
626 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber3, 0, "Perdedor");
627 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, debit_type_code => 'LOST' });
628 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, debit_type_code => 'PROCESSING' });
629 ok( !$lostfine, "No lost fine if no replacementcost and no default set when pref off");
630 ok( $procfee->amount == 8.16, "Processing fee if processing fee");
631 is( $procfee->issue_id, $cli_issue_id_3, "Processing fee issue id is correct" );
633 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber3, 6.12, "Perdedor");
634 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, debit_type_code => 'LOST' });
635 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, debit_type_code => 'PROCESSING' });
636 ok( $lostfine->amount == 6.12 , "Lost fine equals replacementcost when pref off and no default set");
637 ok( $procfee->amount == 8.16, "Processing fee if processing fee");
638 is( $procfee->issue_id, $cli_issue_id_3, "Processing fee issue id is correct" );
642 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 0, "Perdedor");
643 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'LOST' });
644 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'PROCESSING' });
645 ok( !$lostfine, "No lost fine if no replacementcost but default set when pref off");
646 ok( $procfee->amount == 2.04, "Processing fee if processing fee");
647 is( $procfee->issue_id, $cli_issue_id_4, "Processing fee issue id is correct" );
649 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 6.12, "Perdedor");
650 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'LOST' });
651 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'PROCESSING' });
652 ok( $lostfine->amount == 6.12 , "Lost fine equals replacementcost when pref off and default set");
653 ok( $procfee->amount == 2.04, "Processing fee if processing fee");
654 is( $procfee->issue_id, $cli_issue_id_4, "Processing fee issue id is correct" );
658 t::lib::Mocks::mock_preference('useDefaultReplacementCost', '1');
660 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber1, 0, "Perdedor");
661 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, debit_type_code => 'LOST' });
662 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, debit_type_code => 'PROCESSING' });
663 ok( !$lostfine, "No lost fine if no replacementcost or default when pref on");
664 ok( !$procfee, "No processing fee if no processing fee");
665 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber1, 6.12, "Perdedor");
666 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, debit_type_code => 'LOST' });
667 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, debit_type_code => 'PROCESSING' });
668 is( $lostfine->amount, "6.120000", "Lost fine equals replacementcost when pref on and no default set");
669 ok( !$procfee, "No processing fee if no processing fee");
671 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber2, 0, "Perdedor");
672 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, debit_type_code => 'LOST' });
673 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, debit_type_code => 'PROCESSING' });
674 is( $lostfine->amount(), "16.320000", "Lost fine is default if no replacementcost but default set when pref on");
675 ok( !$procfee, "No processing fee if no processing fee");
677 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber2, 6.12, "Perdedor");
678 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, debit_type_code => 'LOST' });
679 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, debit_type_code => 'PROCESSING' });
680 is( $lostfine->amount, "6.120000" , "Lost fine equals replacementcost when pref on and default set");
681 ok( !$procfee, "No processing fee if no processing fee");
683 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber3, 0, "Perdedor");
684 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, debit_type_code => 'LOST' });
685 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, debit_type_code => 'PROCESSING' });
686 ok( !$lostfine, "No lost fine if no replacementcost and default not set when pref on");
687 is( $procfee->amount, "8.160000", "Processing fee if processing fee");
688 is( $procfee->issue_id, $cli_issue_id_3, "Processing fee issue id is correct" );
690 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber3, 6.12, "Perdedor");
691 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, debit_type_code => 'LOST' });
692 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, debit_type_code => 'PROCESSING' });
693 is( $lostfine->amount, "6.120000", "Lost fine equals replacementcost when pref on and no default set");
694 is( $procfee->amount, "8.160000", "Processing fee if processing fee");
695 is( $procfee->issue_id, $cli_issue_id_3, "Processing fee issue id is correct" );
697 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 0, "Perdedor");
698 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'LOST' });
699 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'PROCESSING' });
700 is( $lostfine->amount, "4.080000", "Lost fine is default if no replacementcost but default set when pref on");
701 is( $procfee->amount, "2.040000", "Processing fee if processing fee");
702 is( $procfee->issue_id, $cli_issue_id_4, "Processing fee issue id is correct" );
705 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 6.12, "Perdedor");
706 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'LOST' });
707 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'PROCESSING' });
708 is( $lostfine->amount, "6.120000", "Lost fine equals replacementcost when pref on and default set");
709 is( $procfee->amount, "2.040000", "Processing fee if processing fee");
710 is( $procfee->issue_id, $cli_issue_id_4, "Processing fee issue id is correct" );
711 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 6.12, "Perdedor");
712 my $lostfines = Koha::Account::Lines->search({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'LOST' });
713 my $procfees = Koha::Account::Lines->search({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'PROCESSING' });
714 ok( $lostfines->count == 1 , "Lost fine cannot be double charged for the same issue_id");
715 ok( $procfees->count == 1, "Processing fee cannot be double charged for the same issue_id");
716 MarkIssueReturned($cli_borrowernumber, $cli_itemnumber4);
717 $cli_issue_id_4X = $builder->build({ source => 'Issue', value => { borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4 } })->{issue_id};
718 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 6.12, "Perdedor");
719 $lostfines = Koha::Account::Lines->search({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'LOST' });
720 $procfees = Koha::Account::Lines->search({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'PROCESSING' });
721 ok( $lostfines->count == 2 , "Lost fine can be charged twice for the same item if they are distinct issue_id's");
722 ok( $procfees->count == 2, "Processing fee can be charged twice for the same item if they are distinct issue_id's");
723 $lostfines->delete();
727 subtest "basic fields tests" => sub {
730 t::lib::Mocks::mock_preference('ProcessingFeeNote', 'Test Note');
731 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, '1.99', "Perdedor");
734 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'LOST' });
735 ok($lostfine, "Lost fine created");
736 is($lostfine->manager_id, $staff_id, "Lost fine manager_id set correctly");
737 is($lostfine->issue_id, $cli_issue_id_4X, "Lost fine issue_id set correctly");
738 is($lostfine->description, "Perdedor", "Lost fine issue_id set correctly");
739 is($lostfine->note, '', "Lost fine does not contain a note");
740 is($lostfine->branchcode, $branchcode, "Lost fine branchcode set correctly");
743 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'PROCESSING' });
744 ok($procfee, "Processing fee created");
745 is($procfee->manager_id, $staff_id, "Processing fee manager_id set correctly");
746 is($procfee->issue_id, $cli_issue_id_4X, "Processing fee issue_id set correctly");
747 is($procfee->description, "Perdedor", "Processing fee issue_id set correctly");
748 is($procfee->note, C4::Context->preference("ProcessingFeeNote"), "Processing fee contains note matching ProcessingFeeNote");
749 is($procfee->branchcode, $branchcode, "Processing fee branchcode set correctly");
754 subtest "FinesLog tests" => sub {
757 my $action_logs = $schema->resultset('ActionLog')->search()->count;
759 t::lib::Mocks::mock_preference( 'FinesLog', 0 );
760 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 0, "Perdedor");
761 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'LOST' });
762 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'PROCESSING' });
763 is( $schema->resultset('ActionLog')->count(), $action_logs + 0, 'No logs were added' );
767 t::lib::Mocks::mock_preference( 'FinesLog', 1 );
768 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 0, "Perdedor");
769 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'LOST' });
770 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'PROCESSING' });
771 is( $schema->resultset('ActionLog')->count(), $action_logs + 2, 'Logs were added' );
776 # Cleanup - this must be replaced with a transaction per subtest
777 Koha::Patrons->find($cli_borrowernumber)->checkouts->delete;
780 subtest "Koha::Account::non_issues_charges tests" => sub {
783 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
784 my $account = $patron->account;
786 my $today = dt_from_string;
792 description => 'a Res fee',
795 interface => 'commandline'
800 description => 'a Rental fee',
803 interface => 'commandline'
806 Koha::Account::DebitTypes->find_or_create(
809 description => 'Fee for copie',
813 Koha::Account::Line->new(
815 borrowernumber => $patron->borrowernumber,
817 description => 'a Manual invoice fee',
818 debit_type_code => 'Copie',
819 amountoutstanding => $manual,
820 interface => 'commandline'
825 t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge', 0 );
826 t::lib::Mocks::mock_preference( 'RentalsInNoissuesCharge', 0 );
827 t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge', 0 );
828 my ( $total, $non_issues_charges ) = ( $account->balance, $account->non_issues_charges );
829 my $other_charges = $total - $non_issues_charges;
832 $res + $rent + $manual,
833 'Total charges should be Res + Rent + Manual'
835 is( $non_issues_charges, 0,
836 'If 0|0|0 there should not have non issues charges' );
837 is( $other_charges, 15, 'If 0|0|0 there should only have other charges' );
839 t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge', 0 );
840 t::lib::Mocks::mock_preference( 'RentalsInNoissuesCharge', 0 );
841 t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge', 1 );
842 ( $total, $non_issues_charges ) = ( $account->balance, $account->non_issues_charges );
843 $other_charges = $total - $non_issues_charges;
846 $res + $rent + $manual,
847 'Total charges should be Res + Rent + Manual'
849 is( $non_issues_charges, $manual,
850 'If 0|0|1 Only Manual should be a non issue charge' );
854 'If 0|0|1 Res + Rent should be other charges'
857 t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge', 0 );
858 t::lib::Mocks::mock_preference( 'RentalsInNoissuesCharge', 1 );
859 t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge', 0 );
860 ( $total, $non_issues_charges ) = ( $account->balance, $account->non_issues_charges );
861 $other_charges = $total - $non_issues_charges;
864 $res + $rent + $manual,
865 'Total charges should be Res + Rent + Manual'
867 is( $non_issues_charges, $rent,
868 'If 0|1|0 Only Rental should be a non issue charge' );
872 'If 0|1|0 Rent + Manual should be other charges'
875 t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge', 0 );
876 t::lib::Mocks::mock_preference( 'RentalsInNoissuesCharge', 1 );
877 t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge', 1 );
878 ( $total, $non_issues_charges ) = ( $account->balance, $account->non_issues_charges );
879 $other_charges = $total - $non_issues_charges;
882 $res + $rent + $manual,
883 'Total charges should be Res + Rent + Manual'
888 'If 0|1|1 Rent + Manual should be non issues charges'
890 is( $other_charges, $res, 'If 0|1|1 there should only have other charges' );
892 t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge', 1 );
893 t::lib::Mocks::mock_preference( 'RentalsInNoissuesCharge', 0 );
894 t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge', 0 );
895 ( $total, $non_issues_charges ) = ( $account->balance, $account->non_issues_charges );
896 $other_charges = $total - $non_issues_charges;
899 $res + $rent + $manual,
900 'Total charges should be Res + Rent + Manual'
902 is( $non_issues_charges, $res,
903 'If 1|0|0 Only Res should be non issues charges' );
907 'If 1|0|0 Rent + Manual should be other charges'
910 t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge', 1 );
911 t::lib::Mocks::mock_preference( 'RentalsInNoissuesCharge', 1 );
912 t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge', 0 );
913 ( $total, $non_issues_charges ) = ( $account->balance, $account->non_issues_charges );
914 $other_charges = $total - $non_issues_charges;
917 $res + $rent + $manual,
918 'Total charges should be Res + Rent + Manual'
923 'If 1|1|0 Res + Rent should be non issues charges'
925 is( $other_charges, $manual,
926 'If 1|1|0 Only Manual should be other charges' );
928 t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge', 1 );
929 t::lib::Mocks::mock_preference( 'RentalsInNoissuesCharge', 1 );
930 t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge', 1 );
931 ( $total, $non_issues_charges ) = ( $account->balance, $account->non_issues_charges );
932 $other_charges = $total - $non_issues_charges;
935 $res + $rent + $manual,
936 'Total charges should be Res + Rent + Manual'
940 $res + $rent + $manual,
941 'If 1|1|1 Res + Rent + Manual should be non issues charges'
943 is( $other_charges, 0, 'If 1|1|1 there should not have any other charges' );
946 subtest "Koha::Account::non_issues_charges tests" => sub {
949 my $patron = $builder->build_object(
951 class => "Koha::Patrons",
955 categorycode => $categorycode,
956 branchcode => $branchcode
961 my $debit = Koha::Account::Line->new(
963 borrowernumber => $patron->id,
964 date => '1900-01-01',
965 amountoutstanding => 0,
966 interface => 'commandline',
967 debit_type_code => 'LOST'
970 my $credit = Koha::Account::Line->new(
972 borrowernumber => $patron->id,
973 date => '1900-01-01',
974 amountoutstanding => -5,
975 interface => 'commandline',
979 my $offset = Koha::Account::Offset->new(
981 credit_id => $credit->id,
982 debit_id => $debit->id,
987 purge_zero_balance_fees( 1 );
988 my $debit_2 = Koha::Account::Lines->find( $debit->id );
989 my $credit_2 = Koha::Account::Lines->find( $credit->id );
990 ok( $debit_2, 'Debit was correctly not deleted when credit has balance' );
991 ok( $credit_2, 'Credit was correctly not deleted when credit has balance' );
992 is( Koha::Account::Lines->count({ borrowernumber => $patron->id }), 2, "The 2 account lines still exists" );
994 $debit = Koha::Account::Line->new(
996 borrowernumber => $patron->id,
997 date => '1900-01-01',
998 amountoutstanding => 5,
999 interface => 'commanline',
1000 debit_type_code => 'LOST'
1003 $credit = Koha::Account::Line->new(
1005 borrowernumber => $patron->id,
1006 date => '1900-01-01',
1007 amountoutstanding => 0,
1008 interface => 'commandline',
1009 accounttype => 'Pay'
1012 $offset = Koha::Account::Offset->new(
1014 credit_id => $credit->id,
1015 debit_id => $debit->id,
1020 purge_zero_balance_fees( 1 );
1021 $debit_2 = $credit_2 = undef;
1022 $debit_2 = Koha::Account::Lines->find( $debit->id );
1023 $credit_2 = Koha::Account::Lines->find( $credit->id );
1024 ok( $debit_2, 'Debit was correctly not deleted when debit has balance' );
1025 ok( $credit_2, 'Credit was correctly not deleted when debit has balance' );
1026 is( Koha::Account::Lines->count({ borrowernumber => $patron->id }), 2 + 2, "The 2 + 2 account lines still exists" );
1028 $debit = Koha::Account::Line->new(
1030 borrowernumber => $patron->id,
1031 date => '1900-01-01',
1032 amountoutstanding => 0,
1033 interface => 'commandline',
1034 debit_type_code => 'LOST'
1037 $credit = Koha::Account::Line->new(
1039 borrowernumber => $patron->id,
1040 date => '1900-01-01',
1041 amountoutstanding => 0,
1042 interface => 'commandline',
1043 accounttype => 'Pay'
1046 $offset = Koha::Account::Offset->new(
1048 credit_id => $credit->id,
1049 debit_id => $debit->id,
1054 purge_zero_balance_fees( 1 );
1055 $debit_2 = Koha::Account::Lines->find( $debit->id );
1056 $credit_2 = Koha::Account::Lines->find( $credit->id );
1057 ok( !$debit_2, 'Debit was correctly deleted' );
1058 ok( !$credit_2, 'Credit was correctly deleted' );
1059 is( Koha::Account::Lines->count({ borrowernumber => $patron->id }), 2 + 2, "The 2 + 2 account lines still exists, the last 2 have been deleted ok" );
1063 subtest "Koha::Account::Offset credit & debit tests" => sub {
1068 my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
1069 my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode };
1071 my $borrower = Koha::Patron->new( {
1072 cardnumber => 'kyliehall',
1074 firstname => 'Kylie',
1076 $borrower->categorycode( $categorycode );
1077 $borrower->branchcode( $branchcode );
1080 my $account = Koha::Account->new({ patron_id => $borrower->id });
1082 my $line1 = Koha::Account::Line->new(
1084 borrowernumber => $borrower->borrowernumber,
1086 amountoutstanding => 10,
1087 interface => 'commandline',
1088 debit_type_code => 'LOST'
1091 my $line2 = Koha::Account::Line->new(
1093 borrowernumber => $borrower->borrowernumber,
1095 amountoutstanding => 20,
1096 interface => 'commandline',
1097 debit_type_code => 'LOST'
1101 my $id = $account->pay(
1103 lines => [$line1, $line2],
1108 # Test debit and credit methods for Koha::Account::Offset
1109 my $account_offset = Koha::Account::Offsets->find( { credit_id => $id, debit_id => $line1->id } );
1110 is( $account_offset->debit->id, $line1->id, "Koha::Account::Offset->debit gets correct accountline" );
1111 is( $account_offset->credit->id, $id, "Koha::Account::Offset->credit gets correct accountline" );
1113 $account_offset = Koha::Account::Offset->new(
1122 is( $account_offset->debit, undef, "Koha::Account::Offset->debit returns undef if no associated debit" );
1123 is( $account_offset->credit, undef, "Koha::Account::Offset->credit returns undef if no associated credit" );
1126 subtest "Payment notice tests" => sub {
1130 Koha::Account::Lines->delete();
1131 Koha::Patrons->delete();
1132 Koha::Notice::Messages->delete();
1134 my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
1135 my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode };
1137 my $borrower = Koha::Patron->new(
1139 cardnumber => 'chelseahall',
1141 firstname => 'Chelsea',
1142 email => 'chelsea@example.com',
1143 categorycode => $categorycode,
1144 branchcode => $branchcode,
1148 my $manager = $builder->build_object({ class => "Koha::Patrons" });
1149 my $context = new Test::MockModule('C4::Context');
1150 $context->mock( 'userenv', sub {
1152 number => $manager->borrowernumber,
1153 branch => $manager->branchcode,
1156 my $account = Koha::Account->new({ patron_id => $borrower->id });
1158 my $line = Koha::Account::Line->new(
1160 borrowernumber => $borrower->borrowernumber,
1161 amountoutstanding => 27,
1162 interface => 'commandline',
1163 debit_type_code => 'LOST'
1167 my $letter = Koha::Notice::Templates->find( { code => 'ACCOUNT_PAYMENT' } );
1168 $letter->content('[%- USE Price -%]A payment of [% credit.amount * -1 | $Price %] has been applied to your account.');
1171 t::lib::Mocks::mock_preference('UseEmailReceipts', '0');
1172 my $id = $account->pay( { amount => 1 } );
1173 is( Koha::Notice::Messages->search()->count(), 0, 'Notice for payment not sent if UseEmailReceipts is disabled' );
1175 $id = $account->pay( { amount => 1, type => 'writeoff' } );
1176 is( Koha::Notice::Messages->search()->count(), 0, 'Notice for writeoff not sent if UseEmailReceipts is disabled' );
1178 t::lib::Mocks::mock_preference('UseEmailReceipts', '1');
1180 $id = $account->pay( { amount => 12 } );
1181 my $notice = Koha::Notice::Messages->search()->next();
1182 is( $notice->subject, 'Account payment', 'Notice subject is correct for payment' );
1183 is( $notice->letter_code, 'ACCOUNT_PAYMENT', 'Notice letter code is correct for payment' );
1184 is( $notice->content, 'A payment of 12.00 has been applied to your account.', 'Notice content is correct for payment' );
1187 $letter = Koha::Notice::Templates->find( { code => 'ACCOUNT_WRITEOFF' } );
1188 $letter->content('[%- USE Price -%]A writeoff of [% credit.amount * -1 | $Price %] has been applied to your account.');
1191 $id = $account->pay( { amount => 13, type => 'writeoff' } );
1192 $notice = Koha::Notice::Messages->search()->next();
1193 is( $notice->subject, 'Account writeoff', 'Notice subject is correct for payment' );
1194 is( $notice->letter_code, 'ACCOUNT_WRITEOFF', 'Notice letter code is correct for writeoff' );
1195 is( $notice->content, 'A writeoff of 13.00 has been applied to your account.', 'Notice content is correct for writeoff' );