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 my $branchcode = $library->{branchcode};
61 my $context = new Test::MockModule('C4::Context');
62 $context->mock( 'userenv', sub {
66 branch => $branchcode,
69 $context->mock( 'interface', sub { return "commandline" } );
70 my $userenv_branchcode = $branchcode;
73 my $itemtype = $builder->build( { source => 'Itemtype' } );
74 my $item = $builder->build( { source => 'Item', value => { itype => $itemtype->{itemtype} } } );
75 my $patron = $builder->build( { source => 'Borrower' } );
77 my $description = "Test fee!";
79 my $note = 'Test note!';
81 C4::Accounts::manualinvoice( $patron->{borrowernumber},
82 $item->{itemnumber}, $description, $type, $amount, $note )
84 qr/C4::Accounts::manualinvoice is deprecated in favor of Koha::Account->add_debit/,
85 "deprecation warning received for manualinvoice";
86 my ($accountline) = Koha::Account::Lines->search(
88 borrowernumber => $patron->{borrowernumber}
91 is( $accountline->debit_type_code, $type, 'Debit type set correctly for manualinvoice' );
92 is( $accountline->amount+0, $amount, 'Accountline amount set correctly for manualinvoice' );
93 ok( $accountline->description =~ /^$description/, 'Accountline description set correctly for manualinvoice' );
94 is( $accountline->note, $note, 'Accountline note set correctly for manualinvoice' );
95 is( $accountline->branchcode, $branchcode, 'Accountline branchcode set correctly for manualinvoice' );
97 $dbh->do(q|DELETE FROM accountlines|);
99 # Testing purge_zero_balance_fees
101 # The 3rd value in the insert is 'days ago' --
106 my $sth = $dbh->prepare(
107 "INSERT INTO accountlines (
116 VALUES ( ?, ?, (select date_sub(CURRENT_DATE, INTERVAL ? DAY) ), ?, ?, ?, ? )"
122 { amount => 0 , days_ago => 0 , description =>'purge_zero_balance_fees should not delete 0 balance fees with date today' , delete => 0, credit_type => undef, debit_type => 'OVERDUE' } ,
123 { amount => 0 , days_ago => $days - 1 , description =>'purge_zero_balance_fees should not delete 0 balance fees with date before threshold day' , delete => 0, credit_type => undef, debit_type => 'OVERDUE' } ,
124 { amount => 0 , days_ago => $days , description =>'purge_zero_balance_fees should not delete 0 balance fees with date on threshold day' , delete => 0, credit_type => undef, debit_type => 'OVERDUE' } ,
125 { amount => 0 , days_ago => $days + 1 , description =>'purge_zero_balance_fees should delete 0 balance fees with date after threshold day' , delete => 1, credit_type => undef, debit_type => 'OVERDUE' } ,
126 { amount => undef , days_ago => $days + 1 , description =>'purge_zero_balance_fees should delete NULL balance fees with date after threshold day' , delete => 1, credit_type => undef, debit_type => 'OVERDUE' } ,
127 { amount => 5 , days_ago => $days - 1 , description =>'purge_zero_balance_fees should not delete fees with positive amout owed before threshold day' , delete => 0, credit_type => undef, debit_type => 'OVERDUE' } ,
128 { amount => 5 , days_ago => $days , description =>'purge_zero_balance_fees should not delete fees with positive amout owed on threshold day' , delete => 0, credit_type => undef, debit_type => 'OVERDUE' } ,
129 { amount => 5 , days_ago => $days + 1 , description =>'purge_zero_balance_fees should not delete fees with positive amout owed after threshold day' , delete => 0, credit_type => undef, debit_type => 'OVERDUE' } ,
130 { amount => -5 , days_ago => $days - 1 , description =>'purge_zero_balance_fees should not delete fees with negative amout owed before threshold day' , delete => 0, credit_type => 'PAYMENT', debit_type => undef } ,
131 { amount => -5 , days_ago => $days , description =>'purge_zero_balance_fees should not delete fees with negative amout owed on threshold day' , delete => 0, credit_type => 'PAYMENT', debit_type => undef } ,
132 { amount => -5 , days_ago => $days + 1 , description =>'purge_zero_balance_fees should not delete fees with negative amout owed after threshold day' , delete => 0, credit_type => 'PAYMENT', debit_type => undef }
134 my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
135 my $borrower = Koha::Patron->new( { firstname => 'Test', surname => 'Patron', categorycode => $categorycode, branchcode => $branchcode } )->store();
137 for my $data ( @test_data ) {
139 $borrower->borrowernumber,
142 $data->{description},
144 $data->{credit_type},
149 purge_zero_balance_fees( $days );
151 $sth = $dbh->prepare(
152 "select count(*) = 0 as deleted
154 where description = ?"
158 sub is_delete_correct {
159 my $should_delete = shift;
160 my $description = shift;
161 $sth->execute( $description );
162 my $test = $sth->fetchrow_hashref();
163 is( $test->{deleted}, $should_delete, $description )
166 for my $data (@test_data) {
167 is_delete_correct( $data->{delete}, $data->{description});
170 $dbh->do(q|DELETE FROM accountlines|);
172 subtest "Koha::Account::pay tests" => sub {
177 my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
178 my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode };
180 my $borrower = Koha::Patron->new( {
181 cardnumber => '1234567890',
183 firstname => 'Marty',
185 $borrower->categorycode( $categorycode );
186 $borrower->branchcode( $branchcode );
189 my $account = Koha::Account->new({ patron_id => $borrower->id });
191 my $line1 = $account->add_debit({ type => 'ACCOUNT', amount => 100, interface => 'commandline' });
192 my $line2 = $account->add_debit({ type => 'ACCOUNT', amount => 200, interface => 'commandline' });
194 $sth = $dbh->prepare("SELECT count(*) FROM accountlines");
196 my $count = $sth->fetchrow_array;
197 is($count, 2, 'There is 2 lines as expected');
199 # There is $100 in the account
200 $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
201 my $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
203 for my $line ( @$amountoutstanding ) {
204 $amountleft += $line;
206 is($amountleft, 300, 'The account has 300$ as expected' );
208 # We make a $20 payment
209 my $borrowernumber = $borrower->borrowernumber;
211 my $payment_note = '$20.00 payment note';
212 my $id = $account->pay( { amount => $data, note => $payment_note, payment_type => "TEST_TYPE" } )->{payment_id};
214 my $accountline = Koha::Account::Lines->find( $id );
215 is( $accountline->payment_type, "TEST_TYPE", "Payment type passed into pay is set in account line correctly" );
217 # There is now $280 in the account
218 $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
219 $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
221 for my $line ( @$amountoutstanding ) {
222 $amountleft += $line;
224 is($amountleft, 280, 'The account has $280 as expected' );
226 # Is the payment note well registered
227 $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
228 $sth->execute($borrower->borrowernumber);
229 my $note = $sth->fetchrow_array;
230 is($note,'$20.00 payment note', '$20.00 payment note is registered');
232 # We make a -$30 payment (a NEGATIVE payment)
234 $payment_note = '-$30.00 payment note';
235 $account->pay( { amount => $data, note => $payment_note } );
237 # There is now $310 in the account
238 $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
239 $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
241 for my $line ( @$amountoutstanding ) {
242 $amountleft += $line;
244 is($amountleft, 310, 'The account has $310 as expected' );
245 # Is the payment note well registered
246 $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
247 $sth->execute($borrower->borrowernumber);
248 $note = $sth->fetchrow_array;
249 is($note,'-$30.00 payment note', '-$30.00 payment note is registered');
251 #We make a $150 payment ( > 1stLine )
253 $payment_note = '$150.00 payment note';
254 $account->pay( { amount => $data, note => $payment_note } );
256 # There is now $160 in the account
257 $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
258 $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
260 for my $line ( @$amountoutstanding ) {
261 $amountleft += $line;
263 is($amountleft, 160, 'The account has $160 as expected' );
265 # Is the payment note well registered
266 $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
267 $sth->execute($borrower->borrowernumber);
268 $note = $sth->fetchrow_array;
269 is($note,'$150.00 payment note', '$150.00 payment note is registered');
271 #We make a $200 payment ( > amountleft )
273 $payment_note = '$200.00 payment note';
274 $account->pay( { amount => $data, note => $payment_note } );
276 # There is now -$40 in the account
277 $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
278 $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
280 for my $line ( @$amountoutstanding ) {
281 $amountleft += $line;
283 is($amountleft, -40, 'The account has -$40 as expected, (credit situation)' );
285 # Is the payment note well registered
286 $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
287 $sth->execute($borrower->borrowernumber);
288 $note = $sth->fetchrow_array;
289 is($note,'$200.00 payment note', '$200.00 payment note is registered');
291 my $line3 = $account->add_debit({ type => 'ACCOUNT', amount => 42, interface => 'commandline' });
292 my $payment_id = $account->pay( { lines => [$line3], amount => 42 } )->{payment_id};
293 my $payment = Koha::Account::Lines->find( $payment_id );
294 is( $payment->amount()+0, -42, "Payment paid the specified fine" );
295 $line3 = Koha::Account::Lines->find( $line3->id );
296 is( $line3->amountoutstanding+0, 0, "Specified fine is paid" );
297 is( $payment->branchcode, undef, 'branchcode passed, then undef' );
300 subtest "Koha::Account::pay particular line tests" => sub {
305 my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
306 my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode };
308 my $borrower = Koha::Patron->new( {
309 cardnumber => 'kylemhall',
313 $borrower->categorycode( $categorycode );
314 $borrower->branchcode( $branchcode );
317 my $account = Koha::Account->new({ patron_id => $borrower->id });
319 my $line1 = $account->add_debit({ type => 'ACCOUNT', amount => 1, interface => 'commandline' });
320 my $line2 = $account->add_debit({ type => 'ACCOUNT', amount => 2, interface => 'commandline' });
321 my $line3 = $account->add_debit({ type => 'ACCOUNT', amount => 3, interface => 'commandline' });
322 my $line4 = $account->add_debit({ type => 'ACCOUNT', amount => 4, interface => 'commandline' });
324 is( $account->balance(), 10, "Account balance is 10" );
328 lines => [$line2, $line3, $line4],
333 $_->_result->discard_changes foreach ( $line1, $line2, $line3, $line4 );
335 # Line1 is not paid at all, as it was not passed in the lines param
336 is( $line1->amountoutstanding+0, 1, "Line 1 was not paid" );
337 # Line2 was paid in full, as it was the first in the lines list
338 is( $line2->amountoutstanding+0, 0, "Line 2 was paid in full" );
339 # Line3 was paid partially, as the remaining balance did not cover it entirely
340 is( $line3->amountoutstanding+0, 1, "Line 3 was paid to 1.00" );
341 # Line4 was not paid at all, as the payment was all used up by that point
342 is( $line4->amountoutstanding+0, 4, "Line 4 was not paid" );
345 subtest "Koha::Account::pay writeoff tests" => sub {
350 my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
351 my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode };
353 my $borrower = Koha::Patron->new( {
354 cardnumber => 'chelseahall',
356 firstname => 'Chelsea',
358 $borrower->categorycode( $categorycode );
359 $borrower->branchcode( $branchcode );
362 my $account = Koha::Account->new({ patron_id => $borrower->id });
364 my $line = $account->add_debit({ type => 'ACCOUNT', amount => 42, interface => 'commandline' });
366 is( $account->balance(), 42, "Account balance is 42" );
368 my $id = $account->pay(
376 $line->_result->discard_changes();
378 is( $line->amountoutstanding+0, 0, "Line was written off" );
380 my $writeoff = Koha::Account::Lines->find( $id );
382 is( $writeoff->credit_type_code, 'WRITEOFF', 'Type is correct for WRITEOFF' );
383 is( $writeoff->description, 'Writeoff', 'Description is correct' );
384 is( $writeoff->amount+0, -42, 'Amount is correct' );
387 subtest "More Koha::Account::pay tests" => sub {
392 my $category = $builder->build({ source => 'Category' })->{ categorycode };
393 my $branch = $builder->build({ source => 'Branch' })->{ branchcode };
394 $branchcode = $branch;
395 my $borrowernumber = $builder->build({
396 source => 'Borrower',
397 value => { categorycode => $category,
398 branchcode => $branch }
399 })->{ borrowernumber };
402 my $accountline = $builder->build(
404 source => 'Accountline',
406 borrowernumber => $borrowernumber,
408 amountoutstanding => $amount,
409 credit_type_code => undef,
414 my $rs = $schema->resultset('Accountline')->search({
415 borrowernumber => $borrowernumber
418 is( $rs->count(), 1, 'Accountline created' );
420 my $account = Koha::Account->new( { patron_id => $borrowernumber } );
421 my $line = Koha::Account::Lines->find( $accountline->{ accountlines_id } );
422 # make the full payment
423 $account->pay({ lines => [$line], amount => $amount, library_id => $branch, note => 'A payment note' });
425 my $offset = Koha::Account::Offsets->search({ debit_id => $accountline->{accountlines_id} })->next();
426 is( $offset->amount+0, -100, 'Offset amount is -100.00' );
427 is( $offset->type(), 'Payment', 'Offset type is Payment' );
429 my $stat = $schema->resultset('Statistic')->search({
432 }, { order_by => { -desc => 'datetime' } })->next();
434 ok( defined $stat, "There's a payment log that matches the branch" );
437 skip "No statistic logged", 4 unless defined $stat;
439 is( $stat->type, 'payment', "Correct statistic type" );
440 is( $stat->branch, $branch, "Correct branch logged to statistics" );
441 is( $stat->borrowernumber, $borrowernumber, "Correct borrowernumber logged to statistics" );
442 is( $stat->value+0, $amount, "Correct amount logged to statistics" );
446 subtest "Even more Koha::Account::pay tests" => sub {
451 my $category = $builder->build({ source => 'Category' })->{ categorycode };
452 my $branch = $builder->build({ source => 'Branch' })->{ branchcode };
453 $branchcode = $branch;
454 my $borrowernumber = $builder->build({
455 source => 'Borrower',
456 value => { categorycode => $category,
457 branchcode => $branch }
458 })->{ borrowernumber };
461 my $partialamount = 60;
462 my $accountline = $builder->build(
464 source => 'Accountline',
466 borrowernumber => $borrowernumber,
468 amountoutstanding => $amount,
469 credit_type_code => undef,
474 my $rs = $schema->resultset('Accountline')->search({
475 borrowernumber => $borrowernumber
478 is( $rs->count(), 1, 'Accountline created' );
480 my $account = Koha::Account->new( { patron_id => $borrowernumber } );
481 my $line = Koha::Account::Lines->find( $accountline->{ accountlines_id } );
482 # make the full payment
483 $account->pay({ lines => [$line], amount => $partialamount, library_id => $branch, note => 'A payment note' });
485 my $offset = Koha::Account::Offsets->search( { debit_id => $accountline->{ accountlines_id } } )->next();
486 is( $offset->amount+0, -60, 'Offset amount is -60.00' );
487 is( $offset->type, 'Payment', 'Offset type is payment' );
489 my $stat = $schema->resultset('Statistic')->search({
492 }, { order_by => { -desc => 'datetime' } })->next();
494 ok( defined $stat, "There's a payment log that matches the branch" );
497 skip "No statistic logged", 4 unless defined $stat;
499 is( $stat->type, 'payment', "Correct statistic type" );
500 is( $stat->branch, $branch, "Correct branch logged to statistics" );
501 is( $stat->borrowernumber, $borrowernumber, "Correct borrowernumber logged to statistics" );
502 is( $stat->value+0, $partialamount, "Correct amount logged to statistics" );
506 subtest 'balance' => sub {
509 my $patron = $builder->build({source => 'Borrower'});
510 $patron = Koha::Patrons->find( $patron->{borrowernumber} );
511 my $account = $patron->account;
512 is( $account->balance, 0, 'balance should return 0 if the patron does not have fines' );
514 my $accountline_1 = $builder->build(
516 source => 'Accountline',
518 borrowernumber => $patron->borrowernumber,
520 amountoutstanding => 42,
521 credit_type_code => undef,
525 my $accountline_2 = $builder->build(
527 source => 'Accountline',
529 borrowernumber => $patron->borrowernumber,
531 amountoutstanding => -13,
532 debit_type_code => undef,
537 my $balance = $patron->account->balance;
538 is( int($balance), 29, 'balance should return the correct value');
543 subtest "C4::Accounts::chargelostitem tests" => sub {
546 my $branch = $builder->build( { source => 'Branch' } );
547 my $branchcode = $branch->{branchcode};
549 my $staff = $builder->build( { source => 'Borrower' } );
550 my $staff_id = $staff->{borrowernumber};
552 my $module = Test::MockModule->new('C4::Context');
559 branch => $branchcode,
564 my $itype_no_replace_no_fee = $builder->build({ source => 'Itemtype', value => {
566 defaultreplacecost => undef,
569 my $itype_replace_no_fee = $builder->build({ source => 'Itemtype', value => {
571 defaultreplacecost => 16.32,
574 my $itype_no_replace_fee = $builder->build({ source => 'Itemtype', value => {
576 defaultreplacecost => undef,
579 my $itype_replace_fee = $builder->build({ source => 'Itemtype', value => {
581 defaultreplacecost => 4.08,
584 my $cli_borrowernumber = $builder->build({ source => 'Borrower' })->{'borrowernumber'};
585 my $cli_itemnumber1 = $builder->build({ source => 'Item', value => { itype => $itype_no_replace_no_fee->{itemtype} } })->{'itemnumber'};
586 my $cli_itemnumber2 = $builder->build({ source => 'Item', value => { itype => $itype_replace_no_fee->{itemtype} } })->{'itemnumber'};
587 my $cli_itemnumber3 = $builder->build({ source => 'Item', value => { itype => $itype_no_replace_fee->{itemtype} } })->{'itemnumber'};
588 my $cli_itemnumber4 = $builder->build({ source => 'Item', value => { itype => $itype_replace_fee->{itemtype} } })->{'itemnumber'};
590 my $cli_issue_id_1 = $builder->build({ source => 'Issue', value => { borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1 } })->{issue_id};
591 my $cli_issue_id_2 = $builder->build({ source => 'Issue', value => { borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2 } })->{issue_id};
592 my $cli_issue_id_3 = $builder->build({ source => 'Issue', value => { borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3 } })->{issue_id};
593 my $cli_issue_id_4 = $builder->build({ source => 'Issue', value => { borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4 } })->{issue_id};
594 my $cli_issue_id_4X = undef;
599 subtest "fee application tests" => sub {
602 t::lib::Mocks::mock_preference('item-level_itypes', '1');
603 t::lib::Mocks::mock_preference('useDefaultReplacementCost', '0');
605 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber1, 0, "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, "No lost fine if no replacementcost or default when pref off");
609 ok( !$procfee, "No processing fee if no processing fee");
610 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber1, 6.12, "Perdedor");
611 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, debit_type_code => 'LOST' });
612 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, debit_type_code => 'PROCESSING' });
613 ok( $lostfine->amount == 6.12, "Lost fine equals replacementcost when pref off and no default set");
614 ok( !$procfee, "No processing fee if no processing fee");
617 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber2, 0, "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, "No lost fine if no replacementcost but default set when pref off");
621 ok( !$procfee, "No processing fee if no processing fee");
622 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber2, 6.12, "Perdedor");
623 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, debit_type_code => 'LOST' });
624 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, debit_type_code => 'PROCESSING' });
625 ok( $lostfine->amount == 6.12 , "Lost fine equals replacementcost when pref off and default set");
626 ok( !$procfee, "No processing fee if no processing fee");
629 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber3, 0, "Perdedor");
630 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, debit_type_code => 'LOST' });
631 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, debit_type_code => 'PROCESSING' });
632 ok( !$lostfine, "No lost fine if no replacementcost and no default set when pref off");
633 ok( $procfee->amount == 8.16, "Processing fee if processing fee");
634 is( $procfee->issue_id, $cli_issue_id_3, "Processing fee issue id is correct" );
636 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber3, 6.12, "Perdedor");
637 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, debit_type_code => 'LOST' });
638 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, debit_type_code => 'PROCESSING' });
639 ok( $lostfine->amount == 6.12 , "Lost fine equals replacementcost when pref off and no default set");
640 ok( $procfee->amount == 8.16, "Processing fee if processing fee");
641 is( $procfee->issue_id, $cli_issue_id_3, "Processing fee issue id is correct" );
645 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 0, "Perdedor");
646 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'LOST' });
647 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'PROCESSING' });
648 ok( !$lostfine, "No lost fine if no replacementcost but default set when pref off");
649 ok( $procfee->amount == 2.04, "Processing fee if processing fee");
650 is( $procfee->issue_id, $cli_issue_id_4, "Processing fee issue id is correct" );
652 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 6.12, "Perdedor");
653 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'LOST' });
654 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'PROCESSING' });
655 ok( $lostfine->amount == 6.12 , "Lost fine equals replacementcost when pref off and default set");
656 ok( $procfee->amount == 2.04, "Processing fee if processing fee");
657 is( $procfee->issue_id, $cli_issue_id_4, "Processing fee issue id is correct" );
661 t::lib::Mocks::mock_preference('useDefaultReplacementCost', '1');
663 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber1, 0, "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 ok( !$lostfine, "No lost fine if no replacementcost or default when pref on");
667 ok( !$procfee, "No processing fee if no processing fee");
668 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber1, 6.12, "Perdedor");
669 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, debit_type_code => 'LOST' });
670 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, debit_type_code => 'PROCESSING' });
671 is( $lostfine->amount, "6.120000", "Lost fine equals replacementcost when pref on and no default set");
672 ok( !$procfee, "No processing fee if no processing fee");
674 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber2, 0, "Perdedor");
675 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, debit_type_code => 'LOST' });
676 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, debit_type_code => 'PROCESSING' });
677 is( $lostfine->amount(), "16.320000", "Lost fine is default if no replacementcost but default set when pref on");
678 ok( !$procfee, "No processing fee if no processing fee");
680 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber2, 6.12, "Perdedor");
681 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, debit_type_code => 'LOST' });
682 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, debit_type_code => 'PROCESSING' });
683 is( $lostfine->amount, "6.120000" , "Lost fine equals replacementcost when pref on and default set");
684 ok( !$procfee, "No processing fee if no processing fee");
686 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber3, 0, "Perdedor");
687 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, debit_type_code => 'LOST' });
688 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, debit_type_code => 'PROCESSING' });
689 ok( !$lostfine, "No lost fine if no replacementcost and default not set when pref on");
690 is( $procfee->amount, "8.160000", "Processing fee if processing fee");
691 is( $procfee->issue_id, $cli_issue_id_3, "Processing fee issue id is correct" );
693 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber3, 6.12, "Perdedor");
694 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, debit_type_code => 'LOST' });
695 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, debit_type_code => 'PROCESSING' });
696 is( $lostfine->amount, "6.120000", "Lost fine equals replacementcost when pref on and no default set");
697 is( $procfee->amount, "8.160000", "Processing fee if processing fee");
698 is( $procfee->issue_id, $cli_issue_id_3, "Processing fee issue id is correct" );
700 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 0, "Perdedor");
701 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'LOST' });
702 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'PROCESSING' });
703 is( $lostfine->amount, "4.080000", "Lost fine is default if no replacementcost but default set when pref on");
704 is( $procfee->amount, "2.040000", "Processing fee if processing fee");
705 is( $procfee->issue_id, $cli_issue_id_4, "Processing fee issue id is correct" );
708 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 6.12, "Perdedor");
709 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'LOST' });
710 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'PROCESSING' });
711 is( $lostfine->amount, "6.120000", "Lost fine equals replacementcost when pref on and default set");
712 is( $procfee->amount, "2.040000", "Processing fee if processing fee");
713 is( $procfee->issue_id, $cli_issue_id_4, "Processing fee issue id is correct" );
714 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 6.12, "Perdedor");
715 my $lostfines = Koha::Account::Lines->search({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'LOST' });
716 my $procfees = Koha::Account::Lines->search({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'PROCESSING' });
717 ok( $lostfines->count == 1 , "Lost fine cannot be double charged for the same issue_id");
718 ok( $procfees->count == 1, "Processing fee cannot be double charged for the same issue_id");
719 MarkIssueReturned($cli_borrowernumber, $cli_itemnumber4);
720 $cli_issue_id_4X = $builder->build({ source => 'Issue', value => { borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4 } })->{issue_id};
721 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 6.12, "Perdedor");
722 $lostfines = Koha::Account::Lines->search({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'LOST' });
723 $procfees = Koha::Account::Lines->search({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'PROCESSING' });
724 ok( $lostfines->count == 2 , "Lost fine can be charged twice for the same item if they are distinct issue_id's");
725 ok( $procfees->count == 2, "Processing fee can be charged twice for the same item if they are distinct issue_id's");
726 $lostfines->delete();
730 subtest "basic fields tests" => sub {
733 t::lib::Mocks::mock_preference('ProcessingFeeNote', 'Test Note');
734 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, '1.99', "Perdedor");
737 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'LOST' });
738 ok($lostfine, "Lost fine created");
739 is($lostfine->manager_id, $staff_id, "Lost fine manager_id set correctly");
740 is($lostfine->issue_id, $cli_issue_id_4X, "Lost fine issue_id set correctly");
741 is($lostfine->description, "Perdedor", "Lost fine issue_id set correctly");
742 is($lostfine->note, '', "Lost fine does not contain a note");
743 is($lostfine->branchcode, $branchcode, "Lost fine branchcode set correctly");
746 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'PROCESSING' });
747 ok($procfee, "Processing fee created");
748 is($procfee->manager_id, $staff_id, "Processing fee manager_id set correctly");
749 is($procfee->issue_id, $cli_issue_id_4X, "Processing fee issue_id set correctly");
750 is($procfee->description, "Perdedor", "Processing fee issue_id set correctly");
751 is($procfee->note, C4::Context->preference("ProcessingFeeNote"), "Processing fee contains note matching ProcessingFeeNote");
752 is($procfee->branchcode, $branchcode, "Processing fee branchcode set correctly");
757 subtest "FinesLog tests" => sub {
760 my $action_logs = $schema->resultset('ActionLog')->search()->count;
762 t::lib::Mocks::mock_preference( 'FinesLog', 0 );
763 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 0, "Perdedor");
764 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'LOST' });
765 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'PROCESSING' });
766 is( $schema->resultset('ActionLog')->count(), $action_logs + 0, 'No logs were added' );
770 t::lib::Mocks::mock_preference( 'FinesLog', 1 );
771 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 0, "Perdedor");
772 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'LOST' });
773 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'PROCESSING' });
774 is( $schema->resultset('ActionLog')->count(), $action_logs + 2, 'Logs were added' );
779 # Cleanup - this must be replaced with a transaction per subtest
780 Koha::Patrons->find($cli_borrowernumber)->checkouts->delete;
783 subtest "Koha::Account::non_issues_charges tests" => sub {
786 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
787 my $account = $patron->account;
794 description => 'a Res fee',
797 interface => 'commandline'
802 description => 'a Rental fee',
805 interface => 'commandline'
808 Koha::Account::DebitTypes->find_or_create(
811 description => 'Fee for copie',
815 Koha::Account::Line->new(
817 borrowernumber => $patron->borrowernumber,
818 description => 'a Manual invoice fee',
819 debit_type_code => 'Copie',
820 amountoutstanding => $manual,
821 interface => 'commandline'
826 t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge', 0 );
827 t::lib::Mocks::mock_preference( 'RentalsInNoissuesCharge', 0 );
828 t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge', 0 );
829 my ( $total, $non_issues_charges ) = ( $account->balance, $account->non_issues_charges );
830 my $other_charges = $total - $non_issues_charges;
833 $res + $rent + $manual,
834 'Total charges should be Res + Rent + Manual'
836 is( $non_issues_charges, 0,
837 'If 0|0|0 there should not have non issues charges' );
838 is( $other_charges, 15, 'If 0|0|0 there should only have other charges' );
840 t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge', 0 );
841 t::lib::Mocks::mock_preference( 'RentalsInNoissuesCharge', 0 );
842 t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge', 1 );
843 ( $total, $non_issues_charges ) = ( $account->balance, $account->non_issues_charges );
844 $other_charges = $total - $non_issues_charges;
847 $res + $rent + $manual,
848 'Total charges should be Res + Rent + Manual'
850 is( $non_issues_charges, $manual,
851 'If 0|0|1 Only Manual should be a non issue charge' );
855 'If 0|0|1 Res + Rent should be other charges'
858 t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge', 0 );
859 t::lib::Mocks::mock_preference( 'RentalsInNoissuesCharge', 1 );
860 t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge', 0 );
861 ( $total, $non_issues_charges ) = ( $account->balance, $account->non_issues_charges );
862 $other_charges = $total - $non_issues_charges;
865 $res + $rent + $manual,
866 'Total charges should be Res + Rent + Manual'
868 is( $non_issues_charges, $rent,
869 'If 0|1|0 Only Rental should be a non issue charge' );
873 'If 0|1|0 Rent + Manual should be other charges'
876 t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge', 0 );
877 t::lib::Mocks::mock_preference( 'RentalsInNoissuesCharge', 1 );
878 t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge', 1 );
879 ( $total, $non_issues_charges ) = ( $account->balance, $account->non_issues_charges );
880 $other_charges = $total - $non_issues_charges;
883 $res + $rent + $manual,
884 'Total charges should be Res + Rent + Manual'
889 'If 0|1|1 Rent + Manual should be non issues charges'
891 is( $other_charges, $res, 'If 0|1|1 there should only have other charges' );
893 t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge', 1 );
894 t::lib::Mocks::mock_preference( 'RentalsInNoissuesCharge', 0 );
895 t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge', 0 );
896 ( $total, $non_issues_charges ) = ( $account->balance, $account->non_issues_charges );
897 $other_charges = $total - $non_issues_charges;
900 $res + $rent + $manual,
901 'Total charges should be Res + Rent + Manual'
903 is( $non_issues_charges, $res,
904 'If 1|0|0 Only Res should be non issues charges' );
908 'If 1|0|0 Rent + Manual should be other charges'
911 t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge', 1 );
912 t::lib::Mocks::mock_preference( 'RentalsInNoissuesCharge', 1 );
913 t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge', 0 );
914 ( $total, $non_issues_charges ) = ( $account->balance, $account->non_issues_charges );
915 $other_charges = $total - $non_issues_charges;
918 $res + $rent + $manual,
919 'Total charges should be Res + Rent + Manual'
924 'If 1|1|0 Res + Rent should be non issues charges'
926 is( $other_charges, $manual,
927 'If 1|1|0 Only Manual should be other charges' );
929 t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge', 1 );
930 t::lib::Mocks::mock_preference( 'RentalsInNoissuesCharge', 1 );
931 t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge', 1 );
932 ( $total, $non_issues_charges ) = ( $account->balance, $account->non_issues_charges );
933 $other_charges = $total - $non_issues_charges;
936 $res + $rent + $manual,
937 'Total charges should be Res + Rent + Manual'
941 $res + $rent + $manual,
942 'If 1|1|1 Res + Rent + Manual should be non issues charges'
944 is( $other_charges, 0, 'If 1|1|1 there should not have any other charges' );
947 subtest "Koha::Account::non_issues_charges tests" => sub {
950 my $patron = $builder->build_object(
952 class => "Koha::Patrons",
956 categorycode => $categorycode,
957 branchcode => $branchcode
962 my $debit = Koha::Account::Line->new(
964 borrowernumber => $patron->id,
965 date => '1970-01-01 00:00:01',
966 amountoutstanding => 0,
967 interface => 'commandline',
968 debit_type_code => 'LOST'
971 my $credit = Koha::Account::Line->new(
973 borrowernumber => $patron->id,
974 date => '1970-01-01 00:00:01',
975 amountoutstanding => -5,
976 interface => 'commandline',
977 credit_type_code => 'PAYMENT'
980 my $offset = Koha::Account::Offset->new(
982 credit_id => $credit->id,
983 debit_id => $debit->id,
988 purge_zero_balance_fees( 1 );
989 my $debit_2 = Koha::Account::Lines->find( $debit->id );
990 my $credit_2 = Koha::Account::Lines->find( $credit->id );
991 ok( $debit_2, 'Debit was correctly not deleted when credit has balance' );
992 ok( $credit_2, 'Credit was correctly not deleted when credit has balance' );
993 is( Koha::Account::Lines->count({ borrowernumber => $patron->id }), 2, "The 2 account lines still exists" );
995 $debit = Koha::Account::Line->new(
997 borrowernumber => $patron->id,
998 date => '1970-01-01 00:00:01',
999 amountoutstanding => 5,
1000 interface => 'commanline',
1001 debit_type_code => 'LOST'
1004 $credit = Koha::Account::Line->new(
1006 borrowernumber => $patron->id,
1007 date => '1970-01-01 00:00:01',
1008 amountoutstanding => 0,
1009 interface => 'commandline',
1010 credit_type_code => 'PAYMENT'
1013 $offset = Koha::Account::Offset->new(
1015 credit_id => $credit->id,
1016 debit_id => $debit->id,
1021 purge_zero_balance_fees( 1 );
1022 $debit_2 = $credit_2 = undef;
1023 $debit_2 = Koha::Account::Lines->find( $debit->id );
1024 $credit_2 = Koha::Account::Lines->find( $credit->id );
1025 ok( $debit_2, 'Debit was correctly not deleted when debit has balance' );
1026 ok( $credit_2, 'Credit was correctly not deleted when debit has balance' );
1027 is( Koha::Account::Lines->count({ borrowernumber => $patron->id }), 2 + 2, "The 2 + 2 account lines still exists" );
1029 $debit = Koha::Account::Line->new(
1031 borrowernumber => $patron->id,
1032 date => '1970-01-01 00:00:01',
1033 amountoutstanding => 0,
1034 interface => 'commandline',
1035 debit_type_code => 'LOST'
1038 $credit = Koha::Account::Line->new(
1040 borrowernumber => $patron->id,
1041 date => '1970-01-01 00:00:01',
1042 amountoutstanding => 0,
1043 interface => 'commandline',
1044 credit_type_code => 'PAYMENT'
1047 $offset = Koha::Account::Offset->new(
1049 credit_id => $credit->id,
1050 debit_id => $debit->id,
1055 purge_zero_balance_fees( 1 );
1056 $debit_2 = Koha::Account::Lines->find( $debit->id );
1057 $credit_2 = Koha::Account::Lines->find( $credit->id );
1058 ok( !$debit_2, 'Debit was correctly deleted' );
1059 ok( !$credit_2, 'Credit was correctly deleted' );
1060 is( Koha::Account::Lines->count({ borrowernumber => $patron->id }), 2 + 2, "The 2 + 2 account lines still exists, the last 2 have been deleted ok" );
1064 subtest "Koha::Account::Offset credit & debit tests" => sub {
1069 my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
1070 my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode };
1072 my $borrower = Koha::Patron->new( {
1073 cardnumber => 'kyliehall',
1075 firstname => 'Kylie',
1077 $borrower->categorycode( $categorycode );
1078 $borrower->branchcode( $branchcode );
1081 my $account = Koha::Account->new({ patron_id => $borrower->id });
1083 my $line1 = Koha::Account::Line->new(
1085 borrowernumber => $borrower->borrowernumber,
1087 amountoutstanding => 10,
1088 interface => 'commandline',
1089 debit_type_code => 'LOST'
1092 my $line2 = Koha::Account::Line->new(
1094 borrowernumber => $borrower->borrowernumber,
1096 amountoutstanding => 20,
1097 interface => 'commandline',
1098 debit_type_code => 'LOST'
1102 my $id = $account->pay(
1104 lines => [$line1, $line2],
1109 # Test debit and credit methods for Koha::Account::Offset
1110 my $account_offset = Koha::Account::Offsets->find( { credit_id => $id, debit_id => $line1->id } );
1111 is( $account_offset->debit->id, $line1->id, "Koha::Account::Offset->debit gets correct accountline" );
1112 is( $account_offset->credit->id, $id, "Koha::Account::Offset->credit gets correct accountline" );
1114 $account_offset = Koha::Account::Offset->new(
1123 is( $account_offset->debit, undef, "Koha::Account::Offset->debit returns undef if no associated debit" );
1124 is( $account_offset->credit, undef, "Koha::Account::Offset->credit returns undef if no associated credit" );
1127 subtest "Payment notice tests" => sub {
1131 Koha::Notice::Messages->delete();
1133 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
1135 my $manager = $builder->build_object({ class => "Koha::Patrons" });
1136 my $context = new Test::MockModule('C4::Context');
1137 $context->mock( 'userenv', sub {
1139 number => $manager->borrowernumber,
1140 branch => $manager->branchcode,
1143 my $account = Koha::Account->new({ patron_id => $patron->borrowernumber });
1145 my $line = Koha::Account::Line->new(
1147 borrowernumber => $patron->borrowernumber,
1148 amountoutstanding => 27,
1149 interface => 'commandline',
1150 debit_type_code => 'LOST'
1154 my $letter = Koha::Notice::Templates->find( { code => 'ACCOUNT_PAYMENT' } );
1155 $letter->content('[%- USE Price -%]A payment of [% credit.amount * -1 | $Price %] has been applied to your account. Your [% branch.branchcode %]');
1158 t::lib::Mocks::mock_preference('UseEmailReceipts', '0');
1159 my $id = $account->pay( { amount => 1 } )->{payment_id};
1160 is( Koha::Notice::Messages->search()->count(), 0, 'Notice for payment not sent if UseEmailReceipts is disabled' );
1162 $id = $account->pay( { amount => 1, type => 'WRITEOFF' } )->{payment_id};
1163 is( Koha::Notice::Messages->search()->count(), 0, 'Notice for writeoff not sent if UseEmailReceipts is disabled' );
1165 t::lib::Mocks::mock_preference('UseEmailReceipts', '1');
1167 $id = $account->pay( { amount => 12, library_id => $branchcode } )->{payment_id};
1168 my $notice = Koha::Notice::Messages->search()->next();
1169 is( $notice->subject, 'Account payment', 'Notice subject is correct for payment' );
1170 is( $notice->letter_code, 'ACCOUNT_PAYMENT', 'Notice letter code is correct for payment' );
1171 is( $notice->content, "A payment of 12.00 has been applied to your account. Your $branchcode", 'Notice content is correct for payment' );
1174 $letter = Koha::Notice::Templates->find( { code => 'ACCOUNT_WRITEOFF' } );
1175 $letter->content('[%- USE Price -%]A writeoff of [% credit.amount * -1 | $Price %] has been applied to your account. Your [% branch.branchcode %]');
1178 $id = $account->pay( { amount => 13, type => 'WRITEOFF', library_id => $branchcode } )->{payment_id};
1179 $notice = Koha::Notice::Messages->search()->next();
1180 is( $notice->subject, 'Account writeoff', 'Notice subject is correct for payment' );
1181 is( $notice->letter_code, 'ACCOUNT_WRITEOFF', 'Notice letter code is correct for writeoff' );
1182 is( $notice->content, "A writeoff of 13.00 has been applied to your account. Your $branchcode", 'Notice content is correct for writeoff' );