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