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