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