Bug 20629: Remove ability to 'reverse' payments
[koha.git] / t / db_dependent / Accounts.t
1 #!/usr/bin/perl
2
3 # Copyright 2015 BibLibre
4 #
5 # This file is part of Koha.
6 #
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
10 # version.
11 #
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.
15 #
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>.
18
19 use Modern::Perl;
20
21 use Test::More tests => 28;
22 use Test::MockModule;
23 use Test::Warn;
24
25 use t::lib::TestBuilder;
26 use t::lib::Mocks;
27
28 use Koha::Account;
29 use Koha::Account::Lines;
30 use Koha::Account::Offsets;
31 use Koha::Notice::Messages;
32 use Koha::Notice::Templates;
33 use Koha::DateUtils qw( dt_from_string );
34
35 BEGIN {
36     use_ok('C4::Accounts');
37     use_ok('Koha::Object');
38     use_ok('Koha::Patron');
39     use_ok('Data::Dumper');
40 }
41
42 can_ok( 'C4::Accounts',
43     qw(
44         getnextacctno
45         chargelostitem
46         manualinvoice
47         getcharges
48         purge_zero_balance_fees )
49 );
50
51 my $schema  = Koha::Database->new->schema;
52 $schema->storage->txn_begin;
53 my $dbh = C4::Context->dbh;
54
55 my $builder = t::lib::TestBuilder->new;
56 my $library = $builder->build( { source => 'Branch' } );
57
58 $dbh->do(q|DELETE FROM accountlines|);
59 $dbh->do(q|DELETE FROM issues|);
60 $dbh->do(q|DELETE FROM borrowers|);
61
62 my $branchcode = $library->{branchcode};
63 my $borrower_number;
64
65 my $context = new Test::MockModule('C4::Context');
66 $context->mock( 'userenv', sub {
67     return {
68         flags  => 1,
69         id     => 'my_userid',
70         branch => $branchcode,
71     };
72 });
73
74 # Testing purge_zero_balance_fees
75
76 # The 3rd value in the insert is 'days ago' --
77 # 0 => today
78 # 1 => yesterday
79 # etc.
80
81 my $sth = $dbh->prepare(
82     "INSERT INTO accountlines (
83          borrowernumber,
84          amountoutstanding,
85          date,
86          description
87      )
88      VALUES ( ?, ?, (select date_sub(CURRENT_DATE, INTERVAL ? DAY) ), ? )"
89 );
90
91 my $days = 5;
92
93 my @test_data = (
94     { amount => 0     , days_ago => 0         , description =>'purge_zero_balance_fees should not delete 0 balance fees with date today'                     , delete => 0 } ,
95     { amount => 0     , days_ago => $days - 1 , description =>'purge_zero_balance_fees should not delete 0 balance fees with date before threshold day'      , delete => 0 } ,
96     { amount => 0     , days_ago => $days     , description =>'purge_zero_balance_fees should not delete 0 balance fees with date on threshold day'          , delete => 0 } ,
97     { amount => 0     , days_ago => $days + 1 , description =>'purge_zero_balance_fees should delete 0 balance fees with date after threshold day'           , delete => 1 } ,
98     { amount => undef , days_ago => $days + 1 , description =>'purge_zero_balance_fees should delete NULL balance fees with date after threshold day'        , delete => 1 } ,
99     { amount => 5     , days_ago => $days - 1 , description =>'purge_zero_balance_fees should not delete fees with positive amout owed before threshold day'  , delete => 0 } ,
100     { amount => 5     , days_ago => $days     , description =>'purge_zero_balance_fees should not delete fees with positive amout owed on threshold day'      , delete => 0 } ,
101     { amount => 5     , days_ago => $days + 1 , description =>'purge_zero_balance_fees should not delete fees with positive amout owed after threshold day'   , delete => 0 } ,
102     { amount => -5    , days_ago => $days - 1 , description =>'purge_zero_balance_fees should not delete fees with negative amout owed before threshold day' , delete => 0 } ,
103     { amount => -5    , days_ago => $days     , description =>'purge_zero_balance_fees should not delete fees with negative amout owed on threshold day'     , delete => 0 } ,
104     { amount => -5    , days_ago => $days + 1 , description =>'purge_zero_balance_fees should not delete fees with negative amout owed after threshold day'  , delete => 0 }
105 );
106 my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
107 my $borrower = Koha::Patron->new( { firstname => 'Test', surname => 'Patron', categorycode => $categorycode, branchcode => $branchcode } )->store();
108
109 for my $data ( @test_data ) {
110     $sth->execute($borrower->borrowernumber, $data->{amount}, $data->{days_ago}, $data->{description});
111 }
112
113 purge_zero_balance_fees( $days );
114
115 $sth = $dbh->prepare(
116             "select count(*) = 0 as deleted
117              from accountlines
118              where description = ?"
119        );
120
121 #
122 sub is_delete_correct {
123     my $should_delete = shift;
124     my $description = shift;
125     $sth->execute( $description );
126     my $test = $sth->fetchrow_hashref();
127     is( $test->{deleted}, $should_delete, $description )
128 }
129
130 for my $data  (@test_data) {
131     is_delete_correct( $data->{delete}, $data->{description});
132 }
133
134 $dbh->do(q|DELETE FROM accountlines|);
135
136 subtest "Koha::Account::pay tests" => sub {
137
138     plan tests => 13;
139
140     # Create a borrower
141     my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
142     my $branchcode   = $builder->build({ source => 'Branch' })->{ branchcode };
143
144     my $borrower = Koha::Patron->new( {
145         cardnumber => '1234567890',
146         surname => 'McFly',
147         firstname => 'Marty',
148     } );
149     $borrower->categorycode( $categorycode );
150     $borrower->branchcode( $branchcode );
151     $borrower->store;
152
153     my $account = Koha::Account->new({ patron_id => $borrower->id });
154
155     my $line1 = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amountoutstanding => 100 })->store();
156     my $line2 = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amountoutstanding => 200 })->store();
157
158     $sth = $dbh->prepare("SELECT count(*) FROM accountlines");
159     $sth->execute;
160     my $count = $sth->fetchrow_array;
161     is($count, 2, 'There is 2 lines as expected');
162
163     # There is $100 in the account
164     $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
165     my $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
166     my $amountleft = 0;
167     for my $line ( @$amountoutstanding ) {
168         $amountleft += $line;
169     }
170     is($amountleft, 300, 'The account has 300$ as expected' );
171
172     # We make a $20 payment
173     my $borrowernumber = $borrower->borrowernumber;
174     my $data = '20.00';
175     my $payment_note = '$20.00 payment note';
176     my $id = $account->pay( { amount => $data, note => $payment_note, payment_type => "TEST_TYPE" } );
177
178     my $accountline = Koha::Account::Lines->find( $id );
179     is( $accountline->payment_type, "TEST_TYPE", "Payment type passed into pay is set in account line correctly" );
180
181     # There is now $280 in the account
182     $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
183     $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
184     $amountleft = 0;
185     for my $line ( @$amountoutstanding ) {
186         $amountleft += $line;
187     }
188     is($amountleft, 280, 'The account has $280 as expected' );
189
190     # Is the payment note well registered
191     $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
192     $sth->execute($borrower->borrowernumber);
193     my $note = $sth->fetchrow_array;
194     is($note,'$20.00 payment note', '$20.00 payment note is registered');
195
196     # We make a -$30 payment (a NEGATIVE payment)
197     $data = '-30.00';
198     $payment_note = '-$30.00 payment note';
199     $account->pay( { amount => $data, note => $payment_note } );
200
201     # There is now $310 in the account
202     $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
203     $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
204     $amountleft = 0;
205     for my $line ( @$amountoutstanding ) {
206         $amountleft += $line;
207     }
208     is($amountleft, 310, 'The account has $310 as expected' );
209     # Is the payment note well registered
210     $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
211     $sth->execute($borrower->borrowernumber);
212     $note = $sth->fetchrow_array;
213     is($note,'-$30.00 payment note', '-$30.00 payment note is registered');
214
215     #We make a $150 payment ( > 1stLine )
216     $data = '150.00';
217     $payment_note = '$150.00 payment note';
218     $account->pay( { amount => $data, note => $payment_note } );
219
220     # There is now $160 in the account
221     $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
222     $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
223     $amountleft = 0;
224     for my $line ( @$amountoutstanding ) {
225         $amountleft += $line;
226     }
227     is($amountleft, 160, 'The account has $160 as expected' );
228
229     # Is the payment note well registered
230     $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
231     $sth->execute($borrower->borrowernumber);
232     $note = $sth->fetchrow_array;
233     is($note,'$150.00 payment note', '$150.00 payment note is registered');
234
235     #We make a $200 payment ( > amountleft )
236     $data = '200.00';
237     $payment_note = '$200.00 payment note';
238     $account->pay( { amount => $data, note => $payment_note } );
239
240     # There is now -$40 in the account
241     $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
242     $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
243     $amountleft = 0;
244     for my $line ( @$amountoutstanding ) {
245         $amountleft += $line;
246     }
247     is($amountleft, -40, 'The account has -$40 as expected, (credit situation)' );
248
249     # Is the payment note well registered
250     $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
251     $sth->execute($borrower->borrowernumber);
252     $note = $sth->fetchrow_array;
253     is($note,'$200.00 payment note', '$200.00 payment note is registered');
254
255     my $line3 = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amountoutstanding => 42, accounttype => 'TEST' })->store();
256     my $payment_id = $account->pay( { lines => [$line3], amount => 42 } );
257     my $payment = Koha::Account::Lines->find( $payment_id );
258     is( $payment->amount(), '-42.000000', "Payment paid the specified fine" );
259     $line3 = Koha::Account::Lines->find( $line3->id );
260     is( $line3->amountoutstanding, '0.000000', "Specified fine is paid" );
261 };
262
263 subtest "Koha::Account::pay particular line tests" => sub {
264
265     plan tests => 5;
266
267     # Create a borrower
268     my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
269     my $branchcode   = $builder->build({ source => 'Branch' })->{ branchcode };
270
271     my $borrower = Koha::Patron->new( {
272         cardnumber => 'kylemhall',
273         surname => 'Hall',
274         firstname => 'Kyle',
275     } );
276     $borrower->categorycode( $categorycode );
277     $borrower->branchcode( $branchcode );
278     $borrower->store;
279
280     my $account = Koha::Account->new({ patron_id => $borrower->id });
281
282     my $line1 = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amountoutstanding => 1 })->store();
283     my $line2 = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amountoutstanding => 2 })->store();
284     my $line3 = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amountoutstanding => 3 })->store();
285     my $line4 = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amountoutstanding => 4 })->store();
286
287     is( $account->balance(), 10, "Account balance is 10" );
288
289     $account->pay(
290         {
291             lines => [$line2, $line3, $line4],
292             amount => 4,
293         }
294     );
295
296     $_->_result->discard_changes foreach ( $line1, $line2, $line3, $line4 );
297
298     # Line1 is not paid at all, as it was not passed in the lines param
299     is( $line1->amountoutstanding, "1.000000", "Line 1 was not paid" );
300     # Line2 was paid in full, as it was the first in the lines list
301     is( $line2->amountoutstanding, "0.000000", "Line 2 was paid in full" );
302     # Line3 was paid partially, as the remaining balance did not cover it entirely
303     is( $line3->amountoutstanding, "1.000000", "Line 3 was paid to 1.00" );
304     # Line4 was not paid at all, as the payment was all used up by that point
305     is( $line4->amountoutstanding, "4.000000", "Line 4 was not paid" );
306 };
307
308 subtest "Koha::Account::pay writeoff tests" => sub {
309
310     plan tests => 5;
311
312     # Create a borrower
313     my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
314     my $branchcode   = $builder->build({ source => 'Branch' })->{ branchcode };
315
316     my $borrower = Koha::Patron->new( {
317         cardnumber => 'chelseahall',
318         surname => 'Hall',
319         firstname => 'Chelsea',
320     } );
321     $borrower->categorycode( $categorycode );
322     $borrower->branchcode( $branchcode );
323     $borrower->store;
324
325     my $account = Koha::Account->new({ patron_id => $borrower->id });
326
327     my $line = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amountoutstanding => 42 })->store();
328
329     is( $account->balance(), 42, "Account balance is 42" );
330
331     my $id = $account->pay(
332         {
333             lines  => [$line],
334             amount => 42,
335             type   => 'writeoff',
336         }
337     );
338
339     $line->_result->discard_changes();
340
341     is( $line->amountoutstanding, "0.000000", "Line was written off" );
342
343     my $writeoff = Koha::Account::Lines->find( $id );
344
345     is( $writeoff->accounttype, 'W', 'Type is correct' );
346     is( $writeoff->description, 'Writeoff', 'Description is correct' );
347     is( $writeoff->amount, '-42.000000', 'Amount is correct' );
348 };
349
350 subtest "More Koha::Account::pay tests" => sub {
351
352     plan tests => 8;
353
354     # Create a borrower
355     my $category   = $builder->build({ source => 'Category' })->{ categorycode };
356     my $branch     = $builder->build({ source => 'Branch' })->{ branchcode };
357     $branchcode = $branch;
358     my $borrowernumber = $builder->build({
359         source => 'Borrower',
360         value  => { categorycode => $category,
361                     branchcode   => $branch }
362     })->{ borrowernumber };
363
364     my $amount = 100;
365     my $accountline = $builder->build({ source => 'Accountline',
366         value  => { borrowernumber => $borrowernumber,
367                     amount => $amount,
368                     amountoutstanding => $amount }
369     });
370
371     my $rs = $schema->resultset('Accountline')->search({
372         borrowernumber => $borrowernumber
373     });
374
375     is( $rs->count(), 1, 'Accountline created' );
376
377     my $account = Koha::Account->new( { patron_id => $borrowernumber } );
378     my $line = Koha::Account::Lines->find( $accountline->{ accountlines_id } );
379     # make the full payment
380     $account->pay({ lines => [$line], amount => $amount, library_id => $branch, note => 'A payment note' });
381
382     my $offset = Koha::Account::Offsets->search({ debit_id => $accountline->{accountlines_id} })->next();
383     is( $offset->amount(), '-100.000000', 'Offset amount is -100.00' );
384     is( $offset->type(), 'Payment', 'Offset type is Payment' );
385
386     my $stat = $schema->resultset('Statistic')->search({
387         branch  => $branch,
388         type    => 'payment'
389     }, { order_by => { -desc => 'datetime' } })->next();
390
391     ok( defined $stat, "There's a payment log that matches the branch" );
392
393     SKIP: {
394         skip "No statistic logged", 4 unless defined $stat;
395
396         is( $stat->type, 'payment', "Correct statistic type" );
397         is( $stat->branch, $branch, "Correct branch logged to statistics" );
398         is( $stat->borrowernumber, $borrowernumber, "Correct borrowernumber logged to statistics" );
399         is( $stat->value+0, $amount, "Correct amount logged to statistics" );
400     }
401 };
402
403 subtest "Even more Koha::Account::pay tests" => sub {
404
405     plan tests => 8;
406
407     # Create a borrower
408     my $category   = $builder->build({ source => 'Category' })->{ categorycode };
409     my $branch     = $builder->build({ source => 'Branch' })->{ branchcode };
410     $branchcode = $branch;
411     my $borrowernumber = $builder->build({
412         source => 'Borrower',
413         value  => { categorycode => $category,
414                     branchcode   => $branch }
415     })->{ borrowernumber };
416
417     my $amount = 100;
418     my $partialamount = 60;
419     my $accountline = $builder->build({ source => 'Accountline',
420         value  => { borrowernumber => $borrowernumber,
421                     amount => $amount,
422                     amountoutstanding => $amount }
423     });
424
425     my $rs = $schema->resultset('Accountline')->search({
426         borrowernumber => $borrowernumber
427     });
428
429     is( $rs->count(), 1, 'Accountline created' );
430
431     my $account = Koha::Account->new( { patron_id => $borrowernumber } );
432     my $line = Koha::Account::Lines->find( $accountline->{ accountlines_id } );
433     # make the full payment
434     $account->pay({ lines => [$line], amount => $partialamount, library_id => $branch, note => 'A payment note' });
435
436     my $offset = Koha::Account::Offsets->search( { debit_id => $accountline->{ accountlines_id } } )->next();
437     is( $offset->amount, '-60.000000', 'Offset amount is -60.00' );
438     is( $offset->type, 'Payment', 'Offset type is payment' );
439
440     my $stat = $schema->resultset('Statistic')->search({
441         branch  => $branch,
442         type    => 'payment'
443     }, { order_by => { -desc => 'datetime' } })->next();
444
445     ok( defined $stat, "There's a payment log that matches the branch" );
446
447     SKIP: {
448         skip "No statistic logged", 4 unless defined $stat;
449
450         is( $stat->type, 'payment', "Correct statistic type" );
451         is( $stat->branch, $branch, "Correct branch logged to statistics" );
452         is( $stat->borrowernumber, $borrowernumber, "Correct borrowernumber logged to statistics" );
453         is( $stat->value+0, $partialamount, "Correct amount logged to statistics" );
454     }
455 };
456
457 subtest 'balance' => sub {
458     plan tests => 2;
459
460     my $patron = $builder->build({source => 'Borrower'});
461     $patron = Koha::Patrons->find( $patron->{borrowernumber} );
462     my $account = $patron->account;
463     is( $account->balance, 0, 'balance should return 0 if the patron does not have fines' );
464
465     my $accountline_1 = $builder->build(
466         {
467             source => 'Accountline',
468             value  => {
469                 borrowernumber    => $patron->borrowernumber,
470                 amount            => 42,
471                 amountoutstanding => 42
472             }
473         }
474     );
475     my $accountline_2 = $builder->build(
476         {
477             source => 'Accountline',
478             value  => {
479                 borrowernumber    => $patron->borrowernumber,
480                 amount            => -13,
481                 amountoutstanding => -13
482             }
483         }
484     );
485
486     my $balance = $patron->account->balance;
487     is( int($balance), 29, 'balance should return the correct value');
488
489     $patron->delete;
490 };
491
492 subtest "Koha::Account::chargelostitem tests" => sub {
493     plan tests => 32;
494
495     my $lostfine;
496     my $procfee;
497
498     my $itype_no_replace_no_fee = $builder->build({ source => 'Itemtype', value => {
499             rentalcharge => 0,
500             defaultreplacecost => undef,
501             processfee => undef,
502     }});
503     my $itype_replace_no_fee = $builder->build({ source => 'Itemtype', value => {
504             rentalcharge => 0,
505             defaultreplacecost => 16.32,
506             processfee => undef,
507     }});
508     my $itype_no_replace_fee = $builder->build({ source => 'Itemtype', value => {
509             rentalcharge => 0,
510             defaultreplacecost => undef,
511             processfee => 8.16,
512     }});
513     my $itype_replace_fee = $builder->build({ source => 'Itemtype', value => {
514             rentalcharge => 0,
515             defaultreplacecost => 4.08,
516             processfee => 2.04,
517     }});
518     my $cli_borrowernumber = $builder->build({ source => 'Borrower' })->{'borrowernumber'};
519     my $cli_itemnumber1 = $builder->build({ source => 'Item', value => { itype => $itype_no_replace_no_fee->{itemtype} } })->{'itemnumber'};
520     my $cli_itemnumber2 = $builder->build({ source => 'Item', value => { itype => $itype_replace_no_fee->{itemtype} } })->{'itemnumber'};
521     my $cli_itemnumber3 = $builder->build({ source => 'Item', value => { itype => $itype_no_replace_fee->{itemtype} } })->{'itemnumber'};
522     my $cli_itemnumber4 = $builder->build({ source => 'Item', value => { itype => $itype_replace_fee->{itemtype} } })->{'itemnumber'};
523     my $duck = Koha::Items->find({itemnumber=>$cli_itemnumber1});
524
525     t::lib::Mocks::mock_preference('item-level_itypes', '1');
526     t::lib::Mocks::mock_preference('useDefaultReplacementCost', '0');
527
528     C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber1, 0, "Perdedor");
529     $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, accounttype => 'L' });
530     $procfee  = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, accounttype => 'PF' });
531     ok( !$lostfine, "No lost fine if no replacementcost or default when pref off");
532     ok( !$procfee,  "No processing fee if no processing fee");
533     C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber1, 6.12, "Perdedor");
534     $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, accounttype => 'L' });
535     $procfee  = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, accounttype => 'PF' });
536     ok( $lostfine->amount == 6.12, "Lost fine equals replacementcost when pref off and no default set");
537     ok( !$procfee,  "No processing fee if no processing fee");
538     $lostfine->delete();
539
540     C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber2, 0, "Perdedor");
541     $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, accounttype => 'L' });
542     $procfee  = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, accounttype => 'PF' });
543     ok( !$lostfine, "No lost fine if no replacementcost but default set when pref off");
544     ok( !$procfee,  "No processing fee if no processing fee");
545     C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber2, 6.12, "Perdedor");
546     $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, accounttype => 'L' });
547     $procfee  = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, accounttype => 'PF' });
548     ok( $lostfine->amount == 6.12 , "Lost fine equals replacementcost when pref off and default set");
549     ok( !$procfee,  "No processing fee if no processing fee");
550     $lostfine->delete();
551
552     C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber3, 0, "Perdedor");
553     $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, accounttype => 'L' });
554     $procfee  = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, accounttype => 'PF' });
555     ok( !$lostfine, "No lost fine if no replacementcost and no default set when pref off");
556     ok( $procfee->amount == 8.16,  "Processing fee if processing fee");
557     $procfee->delete();
558     C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber3, 6.12, "Perdedor");
559     $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, accounttype => 'L' });
560     $procfee  = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, accounttype => 'PF' });
561     ok( $lostfine->amount == 6.12 , "Lost fine equals replacementcost when pref off and no default set");
562     ok( $procfee->amount == 8.16,  "Processing fee if processing fee");
563     $lostfine->delete();
564     $procfee->delete();
565
566     C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 0, "Perdedor");
567     $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'L' });
568     $procfee  = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'PF' });
569     ok( !$lostfine, "No lost fine if no replacementcost but default set when pref off");
570     ok( $procfee->amount == 2.04,  "Processing fee if processing fee");
571     $procfee->delete();
572     C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 6.12, "Perdedor");
573     $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'L' });
574     $procfee  = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'PF' });
575     ok( $lostfine->amount == 6.12 , "Lost fine equals replacementcost when pref off and default set");
576     ok( $procfee->amount == 2.04,  "Processing fee if processing fee");
577     $lostfine->delete();
578     $procfee->delete();
579
580     t::lib::Mocks::mock_preference('useDefaultReplacementCost', '1');
581
582     C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber1, 0, "Perdedor");
583     $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, accounttype => 'L' });
584     $procfee  = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, accounttype => 'PF' });
585     ok( !$lostfine, "No lost fine if no replacementcost or default when pref on");
586     ok( !$procfee,  "No processing fee if no processing fee");
587     C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber1, 6.12, "Perdedor");
588     $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, accounttype => 'L' });
589     $procfee  = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, accounttype => 'PF' });
590     is( $lostfine->amount, "6.120000", "Lost fine equals replacementcost when pref on and no default set");
591     ok( !$procfee,  "No processing fee if no processing fee");
592
593     C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber2, 0, "Perdedor");
594     $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, accounttype => 'L' });
595     $procfee  = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, accounttype => 'PF' });
596     is( $lostfine->amount(), "16.320000", "Lost fine is default if no replacementcost but default set when pref on");
597     ok( !$procfee,  "No processing fee if no processing fee");
598     $lostfine->delete();
599     C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber2, 6.12, "Perdedor");
600     $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, accounttype => 'L' });
601     $procfee  = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, accounttype => 'PF' });
602     is( $lostfine->amount, "6.120000" , "Lost fine equals replacementcost when pref on and default set");
603     ok( !$procfee,  "No processing fee if no processing fee");
604
605     C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber3, 0, "Perdedor");
606     $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, accounttype => 'L' });
607     $procfee  = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, accounttype => 'PF' });
608     ok( !$lostfine, "No lost fine if no replacementcost and default not set when pref on");
609     is( $procfee->amount, "8.160000",  "Processing fee if processing fee");
610     $procfee->delete();
611     C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber3, 6.12, "Perdedor");
612     $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, accounttype => 'L' });
613     $procfee  = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, accounttype => 'PF' });
614     is( $lostfine->amount, "6.120000", "Lost fine equals replacementcost when pref on and no default set");
615     is( $procfee->amount, "8.160000",  "Processing fee if processing fee");
616
617     C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 0, "Perdedor");
618     $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'L' });
619     $procfee  = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'PF' });
620     is( $lostfine->amount, "4.080000", "Lost fine is default if no replacementcost but default set when pref on");
621     is( $procfee->amount, "2.040000",  "Processing fee if processing fee");
622     $lostfine->delete();
623     $procfee->delete();
624     C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 6.12, "Perdedor");
625     $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'L' });
626     $procfee  = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'PF' });
627     is( $lostfine->amount, "6.120000", "Lost fine equals replacementcost when pref on and default set");
628     is( $procfee->amount, "2.040000",  "Processing fee if processing fee");
629 };
630
631 subtest "Koha::Account::non_issues_charges tests" => sub {
632     plan tests => 21;
633
634     my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
635
636     my $today  = dt_from_string;
637     my $res    = 3;
638     my $rent   = 5;
639     my $manual = 7;
640     Koha::Account::Line->new(
641         {
642             borrowernumber    => $patron->borrowernumber,
643             accountno         => 1,
644             date              => $today,
645             description       => 'a Res fee',
646             accounttype       => 'Res',
647             amountoutstanding => $res,
648         }
649     )->store;
650     Koha::Account::Line->new(
651         {
652             borrowernumber    => $patron->borrowernumber,
653             accountno         => 2,
654             date              => $today,
655             description       => 'a Rental fee',
656             accounttype       => 'Rent',
657             amountoutstanding => $rent,
658         }
659     )->store;
660     Koha::Account::Line->new(
661         {
662             borrowernumber    => $patron->borrowernumber,
663             accountno         => 3,
664             date              => $today,
665             description       => 'a Manual invoice fee',
666             accounttype       => 'Copie',
667             amountoutstanding => $manual,
668         }
669     )->store;
670     Koha::AuthorisedValue->new(
671         {
672             category         => 'MANUAL_INV',
673             authorised_value => 'Copie',
674             lib              => 'Fee for copie',
675         }
676     )->store;
677
678     my $account = $patron->account;
679
680     t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge',   0 );
681     t::lib::Mocks::mock_preference( 'RentalsInNoissuesCharge', 0 );
682     t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge',  0 );
683     my ( $total, $non_issues_charges ) = ( $account->balance, $account->non_issues_charges );
684     my $other_charges = $total - $non_issues_charges;
685     is(
686         $account->balance,
687         $res + $rent + $manual,
688         'Total charges should be Res + Rent + Manual'
689     );
690     is( $non_issues_charges, 0,
691         'If 0|0|0 there should not have non issues charges' );
692     is( $other_charges, 15, 'If 0|0|0 there should only have other charges' );
693
694     t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge',   0 );
695     t::lib::Mocks::mock_preference( 'RentalsInNoissuesCharge', 0 );
696     t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge',  1 );
697     ( $total, $non_issues_charges ) = ( $account->balance, $account->non_issues_charges );
698     $other_charges = $total - $non_issues_charges;
699     is(
700         $total,
701         $res + $rent + $manual,
702         'Total charges should be Res + Rent + Manual'
703     );
704     is( $non_issues_charges, $manual,
705         'If 0|0|1 Only Manual should be a non issue charge' );
706     is(
707         $other_charges,
708         $res + $rent,
709         'If 0|0|1 Res + Rent should be other charges'
710     );
711
712     t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge',   0 );
713     t::lib::Mocks::mock_preference( 'RentalsInNoissuesCharge', 1 );
714     t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge',  0 );
715     ( $total, $non_issues_charges ) = ( $account->balance, $account->non_issues_charges );
716     $other_charges = $total - $non_issues_charges;
717     is(
718         $total,
719         $res + $rent + $manual,
720         'Total charges should be Res + Rent + Manual'
721     );
722     is( $non_issues_charges, $rent,
723         'If 0|1|0 Only Rental should be a non issue charge' );
724     is(
725         $other_charges,
726         $res + $manual,
727         'If 0|1|0 Rent + Manual should be other charges'
728     );
729
730     t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge',   0 );
731     t::lib::Mocks::mock_preference( 'RentalsInNoissuesCharge', 1 );
732     t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge',  1 );
733     ( $total, $non_issues_charges ) = ( $account->balance, $account->non_issues_charges );
734     $other_charges = $total - $non_issues_charges;
735     is(
736         $total,
737         $res + $rent + $manual,
738         'Total charges should be Res + Rent + Manual'
739     );
740     is(
741         $non_issues_charges,
742         $rent + $manual,
743         'If 0|1|1 Rent + Manual should be non issues charges'
744     );
745     is( $other_charges, $res, 'If 0|1|1 there should only have other charges' );
746
747     t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge',   1 );
748     t::lib::Mocks::mock_preference( 'RentalsInNoissuesCharge', 0 );
749     t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge',  0 );
750     ( $total, $non_issues_charges ) = ( $account->balance, $account->non_issues_charges );
751     $other_charges = $total - $non_issues_charges;
752     is(
753         $total,
754         $res + $rent + $manual,
755         'Total charges should be Res + Rent + Manual'
756     );
757     is( $non_issues_charges, $res,
758         'If 1|0|0 Only Res should be non issues charges' );
759     is(
760         $other_charges,
761         $rent + $manual,
762         'If 1|0|0 Rent + Manual should be other charges'
763     );
764
765     t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge',   1 );
766     t::lib::Mocks::mock_preference( 'RentalsInNoissuesCharge', 1 );
767     t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge',  0 );
768     ( $total, $non_issues_charges ) = ( $account->balance, $account->non_issues_charges );
769     $other_charges = $total - $non_issues_charges;
770     is(
771         $total,
772         $res + $rent + $manual,
773         'Total charges should be Res + Rent + Manual'
774     );
775     is(
776         $non_issues_charges,
777         $res + $rent,
778         'If 1|1|0 Res + Rent should be non issues charges'
779     );
780     is( $other_charges, $manual,
781         'If 1|1|0 Only Manual should be other charges' );
782
783     t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge',   1 );
784     t::lib::Mocks::mock_preference( 'RentalsInNoissuesCharge', 1 );
785     t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge',  1 );
786     ( $total, $non_issues_charges ) = ( $account->balance, $account->non_issues_charges );
787     $other_charges = $total - $non_issues_charges;
788     is(
789         $total,
790         $res + $rent + $manual,
791         'Total charges should be Res + Rent + Manual'
792     );
793     is(
794         $non_issues_charges,
795         $res + $rent + $manual,
796         'If 1|1|1 Res + Rent + Manual should be non issues charges'
797     );
798     is( $other_charges, 0, 'If 1|1|1 there should not have any other charges' );
799 };
800
801 subtest "Koha::Account::non_issues_charges tests" => sub {
802     plan tests => 9;
803
804     my $patron = $builder->build_object(
805         {
806             class => "Koha::Patrons",
807             value => {
808                 firstname    => 'Test',
809                 surname      => 'Patron',
810                 categorycode => $categorycode,
811                 branchcode   => $branchcode
812             }
813         }
814     );
815
816     my $debit = Koha::Account::Line->new({ borrowernumber => $patron->id, date => '1900-01-01', amountoutstanding => 0 })->store();
817     my $credit = Koha::Account::Line->new({ borrowernumber => $patron->id, date => '1900-01-01', amountoutstanding => -5 })->store();
818     my $offset = Koha::Account::Offset->new({ credit_id => $credit->id, debit_id => $debit->id, type => 'Payment', amount => 0 })->store();
819     purge_zero_balance_fees( 1 );
820     my $debit_2 = Koha::Account::Lines->find( $debit->id );
821     my $credit_2 = Koha::Account::Lines->find( $credit->id );
822     ok( $debit_2, 'Debit was correctly not deleted when credit has balance' );
823     ok( $credit_2, 'Credit was correctly not deleted when credit has balance' );
824     is( Koha::Account::Lines->count({ borrowernumber => $patron->id }), 2, "The 2 account lines still exists" );
825
826     $debit = Koha::Account::Line->new({ borrowernumber => $patron->id, date => '1900-01-01', amountoutstanding => 5 })->store();
827     $credit = Koha::Account::Line->new({ borrowernumber => $patron->id, date => '1900-01-01', amountoutstanding => 0 })->store();
828     $offset = Koha::Account::Offset->new({ credit_id => $credit->id, debit_id => $debit->id, type => 'Payment', amount => 0 })->store();
829     purge_zero_balance_fees( 1 );
830     $debit_2 = $credit_2 = undef;
831     $debit_2 = Koha::Account::Lines->find( $debit->id );
832     $credit_2 = Koha::Account::Lines->find( $credit->id );
833     ok( $debit_2, 'Debit was correctly not deleted when debit has balance' );
834     ok( $credit_2, 'Credit was correctly not deleted when debit has balance' );
835     is( Koha::Account::Lines->count({ borrowernumber => $patron->id }), 2 + 2, "The 2 + 2 account lines still exists" );
836
837     $debit = Koha::Account::Line->new({ borrowernumber => $patron->id, date => '1900-01-01', amountoutstanding => 0 })->store();
838     $credit = Koha::Account::Line->new({ borrowernumber => $patron->id, date => '1900-01-01', amountoutstanding => 0 })->store();
839     $offset = Koha::Account::Offset->new({ credit_id => $credit->id, debit_id => $debit->id, type => 'Payment', amount => 0 })->store();
840     purge_zero_balance_fees( 1 );
841     $debit_2 = Koha::Account::Lines->find( $debit->id );
842     $credit_2 = Koha::Account::Lines->find( $credit->id );
843     ok( !$debit_2, 'Debit was correctly deleted' );
844     ok( !$credit_2, 'Credit was correctly deleted' );
845     is( Koha::Account::Lines->count({ borrowernumber => $patron->id }), 2 + 2, "The 2 + 2 account lines still exists, the last 2 have been deleted ok" );
846 };
847
848 subtest "Koha::Account::Line::void tests" => sub {
849
850     plan tests => 15;
851
852     # Create a borrower
853     my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
854     my $branchcode   = $builder->build({ source => 'Branch' })->{ branchcode };
855
856     my $borrower = Koha::Patron->new( {
857         cardnumber => 'dariahall',
858         surname => 'Hall',
859         firstname => 'Daria',
860     } );
861     $borrower->categorycode( $categorycode );
862     $borrower->branchcode( $branchcode );
863     $borrower->store;
864
865     my $account = Koha::Account->new({ patron_id => $borrower->id });
866
867     my $line1 = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amount => 10, amountoutstanding => 10 })->store();
868     my $line2 = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amount => 20, amountoutstanding => 20 })->store();
869
870     is( $account->balance(), 30, "Account balance is 30" );
871     is( $line1->amountoutstanding, 10, 'First fee has amount outstanding of 10' );
872     is( $line2->amountoutstanding, 20, 'Second fee has amount outstanding of 20' );
873
874     my $id = $account->pay(
875         {
876             lines  => [$line1, $line2],
877             amount => 30,
878         }
879     );
880
881     my $account_payment = Koha::Account::Lines->find( $id );
882
883     is( $account->balance(), 0, "Account balance is 0" );
884
885     $line1->_result->discard_changes();
886     $line2->_result->discard_changes();
887     is( $line1->amountoutstanding+0, 0, 'First fee has amount outstanding of 0' );
888     is( $line2->amountoutstanding+0, 0, 'Second fee has amount outstanding of 0' );
889
890     my $ret = $account_payment->void();
891
892     is( ref($ret), 'Koha::Account::Line', 'Void returns the account line' );
893     is( $account->balance(), 30, "Account balance is again 30" );
894
895     $account_payment->_result->discard_changes();
896     $line1->_result->discard_changes();
897     $line2->_result->discard_changes();
898
899     is( $account_payment->accounttype, 'VOID', 'Voided payment accounttype is VOID' );
900     is( $account_payment->amount+0, 0, 'Voided payment amount is 0' );
901     is( $account_payment->amountoutstanding+0, 0, 'Voided payment amount outstanding is 0' );
902
903     is( $line1->amountoutstanding+0, 10, 'First fee again has amount outstanding of 10' );
904     is( $line2->amountoutstanding+0, 20, 'Second fee again has amount outstanding of 20' );
905
906     # Accountlines that are not credits should be un-voidable
907     my $line1_pre = $line1->unblessed();
908     $ret = $line1->void();
909     $line1->_result->discard_changes();
910     my $line1_post = $line1->unblessed();
911     is( $ret, undef, 'Attempted void on non-credit returns undef' );
912     is_deeply( $line1_pre, $line1_post, 'Non-credit account line cannot be voided' )
913 };
914
915 subtest "Koha::Account::Offset credit & debit tests" => sub {
916
917     plan tests => 4;
918
919     # Create a borrower
920     my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
921     my $branchcode   = $builder->build({ source => 'Branch' })->{ branchcode };
922
923     my $borrower = Koha::Patron->new( {
924         cardnumber => 'kyliehall',
925         surname => 'Hall',
926         firstname => 'Kylie',
927     } );
928     $borrower->categorycode( $categorycode );
929     $borrower->branchcode( $branchcode );
930     $borrower->store;
931
932     my $account = Koha::Account->new({ patron_id => $borrower->id });
933
934     my $line1 = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amount => 10, amountoutstanding => 10 })->store();
935     my $line2 = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amount => 20, amountoutstanding => 20 })->store();
936
937     my $id = $account->pay(
938         {
939             lines  => [$line1, $line2],
940             amount => 30,
941         }
942     );
943
944     # Test debit and credit methods for Koha::Account::Offset
945     my $account_offset = Koha::Account::Offsets->find( { credit_id => $id, debit_id => $line1->id } );
946     is( $account_offset->debit->id, $line1->id, "Koha::Account::Offset->debit gets correct accountline" );
947     is( $account_offset->credit->id, $id, "Koha::Account::Offset->credit gets correct accountline" );
948
949     $account_offset = Koha::Account::Offset->new(
950         {
951             credit_id => undef,
952             debit_id  => undef,
953             type      => 'Payment',
954             amount    => 0,
955         }
956     )->store();
957
958     is( $account_offset->debit, undef, "Koha::Account::Offset->debit returns undef if no associated debit" );
959     is( $account_offset->credit, undef, "Koha::Account::Offset->credit returns undef if no associated credit" );
960 };
961
962 subtest "Payment notice tests" => sub {
963
964     plan tests => 8;
965
966     Koha::Account::Lines->delete();
967     Koha::Patrons->delete();
968     Koha::Notice::Messages->delete();
969     # Create a borrower
970     my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
971     my $branchcode   = $builder->build({ source => 'Branch' })->{ branchcode };
972
973     my $borrower = Koha::Patron->new(
974         {
975             cardnumber   => 'chelseahall',
976             surname      => 'Hall',
977             firstname    => 'Chelsea',
978             email        => 'chelsea@example.com',
979             categorycode => $categorycode,
980             branchcode   => $branchcode,
981         }
982     )->store();
983
984     my $account = Koha::Account->new({ patron_id => $borrower->id });
985
986     my $line = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amountoutstanding => 27 })->store();
987
988     my $letter = Koha::Notice::Templates->find( { code => 'ACCOUNT_PAYMENT' } );
989     $letter->content('[%- USE Price -%]A payment of [% credit.amount * -1 | $Price %] has been applied to your account.');
990     $letter->store();
991
992     t::lib::Mocks::mock_preference('UseEmailReceipts', '0');
993
994     my $id = $account->pay( { amount => 1 } );
995     is( Koha::Notice::Messages->search()->count(), 0, 'Notice for payment not sent if UseEmailReceipts is disabled' );
996
997     $id = $account->pay( { amount => 1, type => 'writeoff' } );
998     is( Koha::Notice::Messages->search()->count(), 0, 'Notice for writeoff not sent if UseEmailReceipts is disabled' );
999
1000     t::lib::Mocks::mock_preference('UseEmailReceipts', '1');
1001
1002     $id = $account->pay( { amount => 12 } );
1003     my $notice = Koha::Notice::Messages->search()->next();
1004     is( $notice->subject, 'Account payment', 'Notice subject is correct for payment' );
1005     is( $notice->letter_code, 'ACCOUNT_PAYMENT', 'Notice letter code is correct for payment' );
1006     is( $notice->content, 'A payment of 12.00 has been applied to your account.', 'Notice content is correct for payment' );
1007     $notice->delete();
1008
1009     $letter = Koha::Notice::Templates->find( { code => 'ACCOUNT_WRITEOFF' } );
1010     $letter->content('[%- USE Price -%]A writeoff of [% credit.amount * -1 | $Price %] has been applied to your account.');
1011     $letter->store();
1012
1013     $id = $account->pay( { amount => 13, type => 'writeoff' } );
1014     $notice = Koha::Notice::Messages->search()->next();
1015     is( $notice->subject, 'Account writeoff', 'Notice subject is correct for payment' );
1016     is( $notice->letter_code, 'ACCOUNT_WRITEOFF', 'Notice letter code is correct for writeoff' );
1017     is( $notice->content, 'A writeoff of 13.00 has been applied to your account.', 'Notice content is correct for writeoff' );
1018 };
1019
1020 1;