Bug 17894 - Remove and replace WriteOffFee
[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 => 22;
22 use Test::MockModule;
23 use Test::Warn;
24
25 use t::lib::TestBuilder;
26
27 use Koha::Account;
28 use Koha::Account::Lines;
29 use Koha::Account::Line;
30
31 BEGIN {
32     use_ok('C4::Accounts');
33     use_ok('Koha::Object');
34     use_ok('Koha::Patron');
35     use_ok('Data::Dumper');
36 }
37
38 can_ok( 'C4::Accounts',
39     qw(
40         getnextacctno
41         chargelostitem
42         manualinvoice
43         getcharges
44         ModNote
45         getcredits
46         getrefunds
47         ReversePayment
48         purge_zero_balance_fees )
49 );
50
51 my $schema  = Koha::Database->new->schema;
52 $schema->storage->txn_begin;
53 my $dbh = C4::Context->dbh;
54
55 my $builder = t::lib::TestBuilder->new;
56 my $library = $builder->build( { source => 'Branch' } );
57
58 $dbh->do(q|DELETE FROM accountlines|);
59 $dbh->do(q|DELETE FROM issues|);
60 $dbh->do(q|DELETE FROM borrowers|);
61
62 my $branchcode = $library->{branchcode};
63 my $borrower_number;
64
65 my $context = new Test::MockModule('C4::Context');
66 $context->mock( 'userenv', sub {
67     return {
68         flags  => 1,
69         id     => 'my_userid',
70         branch => $branchcode,
71     };
72 });
73
74 # Testing purge_zero_balance_fees
75
76 # The 3rd value in the insert is 'days ago' --
77 # 0 => today
78 # 1 => yesterday
79 # etc.
80
81 my $sth = $dbh->prepare(
82     "INSERT INTO accountlines (
83          borrowernumber,
84          amountoutstanding,
85          date,
86          description
87      )
88      VALUES ( ?, ?, (select date_sub(CURRENT_DATE, INTERVAL ? DAY) ), ? )"
89 );
90
91 my $days = 5;
92
93 my @test_data = (
94     { amount => 0     , days_ago => 0         , description =>'purge_zero_balance_fees should not delete 0 balance fees with date today'                     , delete => 0 } ,
95     { amount => 0     , days_ago => $days - 1 , description =>'purge_zero_balance_fees should not delete 0 balance fees with date before threshold day'      , delete => 0 } ,
96     { amount => 0     , days_ago => $days     , description =>'purge_zero_balance_fees should not delete 0 balance fees with date on threshold day'          , delete => 0 } ,
97     { amount => 0     , days_ago => $days + 1 , description =>'purge_zero_balance_fees should delete 0 balance fees with date after threshold day'           , delete => 1 } ,
98     { amount => undef , days_ago => $days + 1 , description =>'purge_zero_balance_fees should delete NULL balance fees with date after threshold day'        , delete => 1 } ,
99     { amount => 5     , days_ago => $days - 1 , description =>'purge_zero_balance_fees should not delete fees with positive amout owed before threshold day'  , delete => 0 } ,
100     { amount => 5     , days_ago => $days     , description =>'purge_zero_balance_fees should not delete fees with positive amout owed on threshold day'      , delete => 0 } ,
101     { amount => 5     , days_ago => $days + 1 , description =>'purge_zero_balance_fees should not delete fees with positive amout owed after threshold day'   , delete => 0 } ,
102     { amount => -5    , days_ago => $days - 1 , description =>'purge_zero_balance_fees should not delete fees with negative amout owed before threshold day' , delete => 0 } ,
103     { amount => -5    , days_ago => $days     , description =>'purge_zero_balance_fees should not delete fees with negative amout owed on threshold day'     , delete => 0 } ,
104     { amount => -5    , days_ago => $days + 1 , description =>'purge_zero_balance_fees should not delete fees with negative amout owed after threshold day'  , delete => 0 }
105 );
106
107 my $borrower = Koha::Patron->new( { firstname => 'Test', surname => 'Patron', categorycode => 'PT', branchcode => $branchcode } )->store();
108
109 for my $data ( @test_data ) {
110     $sth->execute($borrower->borrowernumber, $data->{amount}, $data->{days_ago}, $data->{description});
111 }
112
113 purge_zero_balance_fees( $days );
114
115 $sth = $dbh->prepare(
116             "select count(*) = 0 as deleted
117              from accountlines
118              where description = ?"
119        );
120
121 #
122 sub is_delete_correct {
123     my $should_delete = shift;
124     my $description = shift;
125     $sth->execute( $description );
126     my $test = $sth->fetchrow_hashref();
127     is( $test->{deleted}, $should_delete, $description )
128 }
129
130 for my $data  (@test_data) {
131     is_delete_correct( $data->{delete}, $data->{description});
132 }
133
134 $dbh->do(q|DELETE FROM accountlines|);
135
136 subtest "Koha::Account::pay tests" => sub {
137
138     plan tests => 12;
139
140     # Create a borrower
141     my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
142     my $branchcode   = $builder->build({ source => 'Branch' })->{ branchcode };
143
144     my $borrower = Koha::Patron->new( {
145         cardnumber => '1234567890',
146         surname => 'McFly',
147         firstname => 'Marty',
148     } );
149     $borrower->categorycode( $categorycode );
150     $borrower->branchcode( $branchcode );
151     $borrower->store;
152
153     my $account = Koha::Account->new({ patron_id => $borrower->id });
154
155     my $line1 = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amountoutstanding => 100 })->store();
156     my $line2 = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amountoutstanding => 200 })->store();
157
158     $sth = $dbh->prepare("SELECT count(*) FROM accountlines");
159     $sth->execute;
160     my $count = $sth->fetchrow_array;
161     is($count, 2, 'There is 2 lines as expected');
162
163     # There is $100 in the account
164     $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
165     my $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
166     my $amountleft = 0;
167     for my $line ( @$amountoutstanding ) {
168         $amountleft += $line;
169     }
170     is($amountleft, 300, 'The account has 300$ as expected' );
171
172     # We make a $20 payment
173     my $borrowernumber = $borrower->borrowernumber;
174     my $data = '20.00';
175     my $payment_note = '$20.00 payment note';
176     $account->pay( { amount => $data, note => $payment_note } );
177
178     # There is now $280 in the account
179     $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
180     $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
181     $amountleft = 0;
182     for my $line ( @$amountoutstanding ) {
183         $amountleft += $line;
184     }
185     is($amountleft, 280, 'The account has $280 as expected' );
186
187     # Is the payment note well registered
188     $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
189     $sth->execute($borrower->borrowernumber);
190     my $note = $sth->fetchrow_array;
191     is($note,'$20.00 payment note', '$20.00 payment note is registered');
192
193     # We make a -$30 payment (a NEGATIVE payment)
194     $data = '-30.00';
195     $payment_note = '-$30.00 payment note';
196     $account->pay( { amount => $data, note => $payment_note } );
197
198     # There is now $310 in the account
199     $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
200     $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
201     $amountleft = 0;
202     for my $line ( @$amountoutstanding ) {
203         $amountleft += $line;
204     }
205     is($amountleft, 310, 'The account has $310 as expected' );
206     # Is the payment note well registered
207     $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
208     $sth->execute($borrower->borrowernumber);
209     $note = $sth->fetchrow_array;
210     is($note,'-$30.00 payment note', '-$30.00 payment note is registered');
211
212     #We make a $150 payment ( > 1stLine )
213     $data = '150.00';
214     $payment_note = '$150.00 payment note';
215     $account->pay( { amount => $data, note => $payment_note } );
216
217     # There is now $160 in the account
218     $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
219     $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
220     $amountleft = 0;
221     for my $line ( @$amountoutstanding ) {
222         $amountleft += $line;
223     }
224     is($amountleft, 160, 'The account has $160 as expected' );
225
226     # Is the payment note well registered
227     $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
228     $sth->execute($borrower->borrowernumber);
229     $note = $sth->fetchrow_array;
230     is($note,'$150.00 payment note', '$150.00 payment note is registered');
231
232     #We make a $200 payment ( > amountleft )
233     $data = '200.00';
234     $payment_note = '$200.00 payment note';
235     $account->pay( { amount => $data, note => $payment_note } );
236
237     # There is now -$40 in the account
238     $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
239     $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
240     $amountleft = 0;
241     for my $line ( @$amountoutstanding ) {
242         $amountleft += $line;
243     }
244     is($amountleft, -40, 'The account has -$40 as expected, (credit situation)' );
245
246     # Is the payment note well registered
247     $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
248     $sth->execute($borrower->borrowernumber);
249     $note = $sth->fetchrow_array;
250     is($note,'$200.00 payment note', '$200.00 payment note is registered');
251
252     my $line3 = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amountoutstanding => 42, accounttype => 'TEST' })->store();
253     my $payment_id = $account->pay( { lines => [$line3], amount => 42 } );
254     my $payment = Koha::Account::Lines->find( $payment_id );
255     is( $payment->amount(), '-42.000000', "Payment paid the specified fine" );
256     $line3 = Koha::Account::Lines->find( $line3->id );
257     is( $line3->amountoutstanding, '0.000000', "Specified fine is paid" );
258 };
259
260 subtest "Koha::Account::pay particular line tests" => sub {
261
262     plan tests => 5;
263
264     # Create a borrower
265     my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
266     my $branchcode   = $builder->build({ source => 'Branch' })->{ branchcode };
267
268     my $borrower = Koha::Patron->new( {
269         cardnumber => 'kylemhall',
270         surname => 'Hall',
271         firstname => 'Kyle',
272     } );
273     $borrower->categorycode( $categorycode );
274     $borrower->branchcode( $branchcode );
275     $borrower->store;
276
277     my $account = Koha::Account->new({ patron_id => $borrower->id });
278
279     my $line1 = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amountoutstanding => 1 })->store();
280     my $line2 = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amountoutstanding => 2 })->store();
281     my $line3 = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amountoutstanding => 3 })->store();
282     my $line4 = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amountoutstanding => 4 })->store();
283
284     is( $account->balance(), "10.000000", "Account balance is 10" );
285
286     $account->pay(
287         {
288             lines => [$line2, $line3, $line4],
289             amount => 4,
290         }
291     );
292
293     $_->_result->discard_changes foreach ( $line1, $line2, $line3, $line4 );
294
295     # Line1 is not paid at all, as it was not passed in the lines param
296     is( $line1->amountoutstanding, "1.000000", "Line 1 was not paid" );
297     # Line2 was paid in full, as it was the first in the lines list
298     is( $line2->amountoutstanding, "0.000000", "Line 2 was paid in full" );
299     # Line3 was paid partially, as the remaining balance did not cover it entirely
300     is( $line3->amountoutstanding, "1.000000", "Line 3 was paid to 1.00" );
301     # Line4 was not paid at all, as the payment was all used up by that point
302     is( $line4->amountoutstanding, "4.000000", "Line 4 was not paid" );
303 };
304
305 subtest "Koha::Account::pay writeoff tests" => sub {
306
307     plan tests => 5;
308
309     # Create a borrower
310     my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
311     my $branchcode   = $builder->build({ source => 'Branch' })->{ branchcode };
312
313     my $borrower = Koha::Patron->new( {
314         cardnumber => 'chelseahall',
315         surname => 'Hall',
316         firstname => 'Chelsea',
317     } );
318     $borrower->categorycode( $categorycode );
319     $borrower->branchcode( $branchcode );
320     $borrower->store;
321
322     my $account = Koha::Account->new({ patron_id => $borrower->id });
323
324     my $line = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amountoutstanding => 42 })->store();
325
326     is( $account->balance(), "42.000000", "Account balance is 42" );
327
328     my $id = $account->pay(
329         {
330             lines  => [$line],
331             amount => 42,
332             type   => 'writeoff',
333         }
334     );
335
336     $line->_result->discard_changes();
337
338     is( $line->amountoutstanding, "0.000000", "Line was written off" );
339
340     my $writeoff = Koha::Account::Lines->find( $id );
341
342     is( $writeoff->accounttype, 'W', 'Type is correct' );
343     is( $writeoff->description, 'Writeoff', 'Description is correct' );
344     is( $writeoff->amount, '-42.000000', 'Amount is correct' );
345 };
346
347 subtest "More Koha::Account::pay tests" => sub {
348
349     plan tests => 6;
350
351     # Create a borrower
352     my $category   = $builder->build({ source => 'Category' })->{ categorycode };
353     my $branch     = $builder->build({ source => 'Branch' })->{ branchcode };
354     $branchcode = $branch;
355     my $borrowernumber = $builder->build({
356         source => 'Borrower',
357         value  => { categorycode => $category,
358                     branchcode   => $branch }
359     })->{ borrowernumber };
360
361     my $amount = 100;
362     my $accountline = $builder->build({ source => 'Accountline',
363         value  => { borrowernumber => $borrowernumber,
364                     amount => $amount,
365                     amountoutstanding => $amount }
366     });
367
368     my $rs = $schema->resultset('Accountline')->search({
369         borrowernumber => $borrowernumber
370     });
371
372     is( $rs->count(), 1, 'Accountline created' );
373
374     my $account = Koha::Account->new( { patron_id => $borrowernumber } );
375     my $line = Koha::Account::Lines->find( $accountline->{ accountlines_id } );
376     # make the full payment
377     $account->pay({ lines => [$line], amount => $amount, library_id => $branch, note => 'A payment note' });
378
379     my $stat = $schema->resultset('Statistic')->search({
380         branch  => $branch,
381         type    => 'payment'
382     }, { order_by => { -desc => 'datetime' } })->next();
383
384     ok( defined $stat, "There's a payment log that matches the branch" );
385
386     SKIP: {
387         skip "No statistic logged", 4 unless defined $stat;
388
389         is( $stat->type, 'payment', "Correct statistic type" );
390         is( $stat->branch, $branch, "Correct branch logged to statistics" );
391         is( $stat->borrowernumber, $borrowernumber, "Correct borrowernumber logged to statistics" );
392         is( $stat->value, "$amount" . "\.0000", "Correct amount logged to statistics" );
393     }
394 };
395
396 subtest "Even more Koha::Account::pay tests" => sub {
397
398     plan tests => 6;
399
400     # Create a borrower
401     my $category   = $builder->build({ source => 'Category' })->{ categorycode };
402     my $branch     = $builder->build({ source => 'Branch' })->{ branchcode };
403     $branchcode = $branch;
404     my $borrowernumber = $builder->build({
405         source => 'Borrower',
406         value  => { categorycode => $category,
407                     branchcode   => $branch }
408     })->{ borrowernumber };
409
410     my $amount = 100;
411     my $partialamount = 60;
412     my $accountline = $builder->build({ source => 'Accountline',
413         value  => { borrowernumber => $borrowernumber,
414                     amount => $amount,
415                     amountoutstanding => $amount }
416     });
417
418     my $rs = $schema->resultset('Accountline')->search({
419         borrowernumber => $borrowernumber
420     });
421
422     is( $rs->count(), 1, 'Accountline created' );
423
424     my $account = Koha::Account->new( { patron_id => $borrowernumber } );
425     my $line = Koha::Account::Lines->find( $accountline->{ accountlines_id } );
426     # make the full payment
427     $account->pay({ lines => [$line], amount => $partialamount, library_id => $branch, note => 'A payment note' });
428
429     my $stat = $schema->resultset('Statistic')->search({
430         branch  => $branch,
431         type    => 'payment'
432     }, { order_by => { -desc => 'datetime' } })->next();
433
434     ok( defined $stat, "There's a payment log that matches the branch" );
435
436     SKIP: {
437         skip "No statistic logged", 4 unless defined $stat;
438
439         is( $stat->type, 'payment', "Correct statistic type" );
440         is( $stat->branch, $branch, "Correct branch logged to statistics" );
441         is( $stat->borrowernumber, $borrowernumber, "Correct borrowernumber logged to statistics" );
442         is( $stat->value, "$partialamount" . "\.0000", "Correct amount logged to statistics" );
443     }
444 };
445
446 subtest 'balance' => sub {
447     plan tests => 2;
448
449     my $patron = $builder->build({source => 'Borrower'});
450     $patron = Koha::Patrons->find( $patron->{borrowernumber} );
451     my $account = $patron->account;
452     is( $account->balance, 0, 'balance should return 0 if the patron does not have fines' );
453
454     my $accountline_1 = $builder->build(
455         {
456             source => 'Accountline',
457             value  => {
458                 borrowernumber    => $patron->borrowernumber,
459                 amount            => 42,
460                 amountoutstanding => 42
461             }
462         }
463     );
464     my $accountline_2 = $builder->build(
465         {
466             source => 'Accountline',
467             value  => {
468                 borrowernumber    => $patron->borrowernumber,
469                 amount            => -13,
470                 amountoutstanding => -13
471             }
472         }
473     );
474
475     my $balance = $patron->account->balance;
476     is( int($balance), 29, 'balance should return the correct value');
477
478     $patron->delete;
479 };
480
481 1;