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