Bug 14402: Make purge_zero_balance_fees() delete fees with NULL balance.
[srvgit] / t / db_dependent / Accounts.t
1 #!/usr/bin/perl
2
3 # Copyright 2015 BibLibre
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 3 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, see <http://www.gnu.org/licenses>.
18
19 use Modern::Perl;
20
21 use Test::More tests => 19;
22 use Test::MockModule;
23 use Test::Warn;
24
25 use t::lib::TestBuilder;
26
27 BEGIN {
28     use_ok('C4::Accounts');
29     use_ok('Koha::Object');
30     use_ok('Koha::Borrower');
31     use_ok('Data::Dumper');
32 }
33
34 can_ok( 'C4::Accounts',
35     qw( recordpayment
36         makepayment
37         getnextacctno
38         chargelostitem
39         manualinvoice
40         getcharges
41         ModNote
42         getcredits
43         getrefunds
44         ReversePayment
45         recordpayment_selectaccts
46         makepartialpayment
47         WriteOffFee
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
57 my $library = $builder->build({
58     source => 'Branch',
59 });
60
61 $dbh->do(q|DELETE FROM accountlines|);
62 $dbh->do(q|DELETE FROM issues|);
63 $dbh->do(q|DELETE FROM borrowers|);
64
65 my $branchcode = $library->{branchcode};
66 my $borrower_number;
67
68 my $context = new Test::MockModule('C4::Context');
69 $context->mock( 'userenv', sub {
70     return {
71         flags  => 1,
72         id     => 'my_userid',
73         branch => $branchcode,
74     };
75 });
76
77 # Testing purge_zero_balance_fees
78
79 # The 3rd value in the insert is 'days ago' --
80 # 0 => today
81 # 1 => yesterday
82 # etc.
83
84 $sth = $dbh->prepare(
85     "INSERT INTO accountlines (
86          borrowernumber,
87          amountoutstanding,
88          date,
89          description
90      )
91      VALUES ( ?, ?, (select date_sub(CURRENT_DATE, INTERVAL ? DAY) ), ? )"
92 );
93
94 my $days = 5;
95
96 my @test_data = (
97     { amount => 0     , days_ago => 0         , description =>'purge_zero_balance_fees should not delete 0 balance fees with date today'                     , delete => 0 } ,
98     { amount => 0     , days_ago => $days - 1 , description =>'purge_zero_balance_fees should not delete 0 balance fees with date before threshold day'      , delete => 0 } ,
99     { amount => 0     , days_ago => $days     , description =>'purge_zero_balance_fees should not delete 0 balance fees with date on threshold day'          , delete => 0 } ,
100     { amount => 0     , days_ago => $days + 1 , description =>'purge_zero_balance_fees should delete 0 balance fees with date after threshold day'           , delete => 1 } ,
101     { amount => undef , days_ago => $days + 1 , description =>'purge_zero_balance_fees should delete NULL balance fees with date after threshold day'        , delete => 1 } ,
102     { amount => 5     , days_ago => $days - 1 , description =>'purge_zero_balance_fees should not delete fees with postive amout owed before threshold day'  , delete => 0 } ,
103     { amount => 5     , days_ago => $days     , description =>'purge_zero_balance_fees should not delete fees with postive amout owed on threshold day'      , delete => 0 } ,
104     { amount => 5     , days_ago => $days + 1 , description =>'purge_zero_balance_fees should not delete fees with postive amout owed after threshold day'   , delete => 0 } ,
105     { amount => -5    , days_ago => $days - 1 , description =>'purge_zero_balance_fees should not delete fees with negative amout owed before threshold day' , delete => 0 } ,
106     { amount => -5    , days_ago => $days     , description =>'purge_zero_balance_fees should not delete fees with negative amout owed on threshold day'     , delete => 0 } ,
107     { amount => -5    , days_ago => $days + 1 , description =>'purge_zero_balance_fees should not delete fees with negative amout owed after threshold day'  , delete => 0 }
108 );
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 subtest "recordpayment() tests" => sub {
136
137     plan tests => 10;
138
139     # Create a borrower
140     my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
141     my $branchcode   = $builder->build({ source => 'Branch' })->{ branchcode };
142
143     my $borrower = Koha::Borrower->new( {
144         cardnumber => '1234567890',
145         surname => 'McFly',
146         firstname => 'Marty',
147     } );
148     $borrower->categorycode( $categorycode );
149     $borrower->branchcode( $branchcode );
150     $borrower->store;
151
152     my $sth = $dbh->prepare(
153         "INSERT INTO accountlines (
154             borrowernumber,
155             amountoutstanding )
156         VALUES ( ?, ? )"
157     );
158     $sth->execute($borrower->borrowernumber, '100');
159     $sth->execute($borrower->borrowernumber, '200');
160
161     $sth = $dbh->prepare("SELECT count(*) FROM accountlines");
162     $sth->execute;
163     my $count = $sth->fetchrow_array;
164     is ($count, 2, 'There is 2 lines as expected');
165
166     # Testing recordpayment -------------------------
167     # There is $100 in the account
168     $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
169     my $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
170     my $amountleft = 0;
171     for my $line ( @$amountoutstanding ) {
172         $amountleft += $line;
173     }
174     ok($amountleft == 300, 'The account has 300$ as expected' );
175
176     # We make a $20 payment
177     my $borrowernumber = $borrower->borrowernumber;
178     my $data = '20.00';
179     my $sys_paytype;
180     my $payment_note = '$20.00 payment note';
181     recordpayment($borrowernumber, $data, $sys_paytype, $payment_note);
182     # There is now $280 in the account
183     $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
184     $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
185     $amountleft = 0;
186     for my $line ( @$amountoutstanding ) {
187         $amountleft += $line;
188     }
189     ok($amountleft == 280, 'The account has $280 as expected' );
190     # Is the payment note well registered
191     $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
192     $sth->execute($borrower->borrowernumber);
193     my $note = $sth->fetchrow_array;
194     is($note,'$20.00 payment note', '$20.00 payment note is registered');
195
196     # We make a -$30 payment (a NEGATIVE payment)
197     $data = '-30.00';
198     $payment_note = '-$30.00 payment note';
199     recordpayment($borrowernumber, $data, $sys_paytype, $payment_note);
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     ok($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     recordpayment($borrowernumber, $data, $sys_paytype, $payment_note);
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     ok($amountleft == 160, 'The account has $160 as expected' );
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     recordpayment($borrowernumber, $data, $sys_paytype, $payment_note);
236     # There is now -$40 in the account
237     $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
238     $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
239     $amountleft = 0;
240     for my $line ( @$amountoutstanding ) {
241         $amountleft += $line;
242     }
243     ok($amountleft == -40, 'The account has -$40 as expected, (credit situation)' );
244     # Is the payment note well registered
245     $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
246     $sth->execute($borrower->borrowernumber);
247     $note = $sth->fetchrow_array;
248     is($note,'$200.00 payment note', '$200.00 payment note is registered');
249 };
250
251 subtest "makepayment() tests" => sub {
252
253     plan tests => 6;
254
255     # Create a borrower
256     my $category   = $builder->build({ source => 'Category' })->{ categorycode };
257     my $branch     = $builder->build({ source => 'Branch' })->{ branchcode };
258     $branchcode = $branch;
259     my $borrowernumber = $builder->build({
260         source => 'Borrower',
261         value  => { categorycode => $category,
262                     branchcode   => $branch }
263     })->{ borrowernumber };
264
265     my $amount = 100;
266     my $accountline = $builder->build({ source => 'Accountline',
267         value  => { borrowernumber => $borrowernumber,
268                     amount => $amount,
269                     amountoutstanding => $amount }
270     });
271
272     my $rs = $schema->resultset('Accountline')->search({
273         borrowernumber => $borrowernumber
274     });
275
276     is( $rs->count(), 1, 'Accountline created' );
277
278     # make the full payment
279     makepayment(
280         $accountline->{ accountlines_id }, $borrowernumber,
281         $accountline->{ accountno },       $amount,
282         $borrowernumber, $branch, 'A payment note' );
283
284     # TODO: someone should write actual tests for makepayment()
285
286     my $stat = $schema->resultset('Statistic')->search({
287         branch  => $branch,
288         type    => 'payment'
289     }, { order_by => { -desc => 'datetime' } })->next();
290
291     ok( defined $stat, "There's a payment log that matches the branch" );
292
293     SKIP: {
294         skip "No statistic logged", 4 unless defined $stat;
295
296         is( $stat->type, 'payment', "Correct statistic type" );
297         is( $stat->branch, $branch, "Correct branch logged to statistics" );
298         is( $stat->borrowernumber, $borrowernumber, "Correct borrowernumber logged to statistics" );
299         is( $stat->value, "$amount" . "\.0000", "Correct amount logged to statistics" );
300     }
301 };
302
303 subtest "makepartialpayment() tests" => sub {
304
305     plan tests => 6;
306
307     # Create a borrower
308     my $category   = $builder->build({ source => 'Category' })->{ categorycode };
309     my $branch     = $builder->build({ source => 'Branch' })->{ branchcode };
310     $branchcode = $branch;
311     my $borrowernumber = $builder->build({
312         source => 'Borrower',
313         value  => { categorycode => $category,
314                     branchcode   => $branch }
315     })->{ borrowernumber };
316
317     my $amount = 100;
318     my $partialamount = 60;
319     my $accountline = $builder->build({ source => 'Accountline',
320         value  => { borrowernumber => $borrowernumber,
321                     amount => $amount,
322                     amountoutstanding => $amount }
323     });
324
325     my $rs = $schema->resultset('Accountline')->search({
326         borrowernumber => $borrowernumber
327     });
328
329     is( $rs->count(), 1, 'Accountline created' );
330
331     # make the full payment
332     makepartialpayment(
333         $accountline->{ accountlines_id }, $borrowernumber,
334         $accountline->{ accountno },       $partialamount,
335         $borrowernumber, $branch, 'A payment note' );
336
337     # TODO: someone should write actual tests for makepartialpayment()
338
339     my $stat = $schema->resultset('Statistic')->search({
340         branch  => $branch,
341         type    => 'payment'
342     }, { order_by => { -desc => 'datetime' } })->next();
343
344     ok( defined $stat, "There's a payment log that matches the branch" );
345
346     SKIP: {
347         skip "No statistic logged", 4 unless defined $stat;
348
349         is( $stat->type, 'payment', "Correct statistic type" );
350         is( $stat->branch, $branch, "Correct branch logged to statistics" );
351         is( $stat->borrowernumber, $borrowernumber, "Correct borrowernumber logged to statistics" );
352         is( $stat->value, "$partialamount" . "\.0000", "Correct amount logged to statistics" );
353     }
354 };
355
356 1;