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