Bug 16453: Make Elasticsearch tests be skipped if configuration entry missing
[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 => 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::Patron');
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 # this test needs a MPL branch; also create another one
56 my $builder = t::lib::TestBuilder->new();
57 $builder->build({ source => 'Branch', value => { branchcode => 'MPL' }});
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 "recordpayment() tests" => sub {
139
140     plan tests => 10;
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 $sth = $dbh->prepare(
156         "INSERT INTO accountlines (
157             borrowernumber,
158             amountoutstanding )
159         VALUES ( ?, ? )"
160     );
161     $sth->execute($borrower->borrowernumber, '100');
162     $sth->execute($borrower->borrowernumber, '200');
163
164     $sth = $dbh->prepare("SELECT count(*) FROM accountlines");
165     $sth->execute;
166     my $count = $sth->fetchrow_array;
167     is ($count, 2, 'There is 2 lines as expected');
168
169     # Testing recordpayment -------------------------
170     # There is $100 in the account
171     $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
172     my $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
173     my $amountleft = 0;
174     for my $line ( @$amountoutstanding ) {
175         $amountleft += $line;
176     }
177     ok($amountleft == 300, 'The account has 300$ as expected' );
178
179     # We make a $20 payment
180     my $borrowernumber = $borrower->borrowernumber;
181     my $data = '20.00';
182     my $sys_paytype;
183     my $payment_note = '$20.00 payment note';
184     recordpayment($borrowernumber, $data, $sys_paytype, $payment_note);
185     # There is now $280 in the account
186     $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
187     $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
188     $amountleft = 0;
189     for my $line ( @$amountoutstanding ) {
190         $amountleft += $line;
191     }
192     ok($amountleft == 280, 'The account has $280 as expected' );
193     # Is the payment note well registered
194     $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
195     $sth->execute($borrower->borrowernumber);
196     my $note = $sth->fetchrow_array;
197     is($note,'$20.00 payment note', '$20.00 payment note is registered');
198
199     # We make a -$30 payment (a NEGATIVE payment)
200     $data = '-30.00';
201     $payment_note = '-$30.00 payment note';
202     recordpayment($borrowernumber, $data, $sys_paytype, $payment_note);
203     # There is now $310 in the account
204     $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
205     $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
206     $amountleft = 0;
207     for my $line ( @$amountoutstanding ) {
208         $amountleft += $line;
209     }
210     ok($amountleft == 310, 'The account has $310 as expected' );
211     # Is the payment note well registered
212     $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
213     $sth->execute($borrower->borrowernumber);
214     $note = $sth->fetchrow_array;
215     is($note,'-$30.00 payment note', '-$30.00 payment note is registered');
216
217     #We make a $150 payment ( > 1stLine )
218     $data = '150.00';
219     $payment_note = '$150.00 payment note';
220     recordpayment($borrowernumber, $data, $sys_paytype, $payment_note);
221     # There is now $160 in the account
222     $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
223     $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
224     $amountleft = 0;
225     for my $line ( @$amountoutstanding ) {
226         $amountleft += $line;
227     }
228     ok($amountleft == 160, 'The account has $160 as expected' );
229     # Is the payment note well registered
230     $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
231     $sth->execute($borrower->borrowernumber);
232     $note = $sth->fetchrow_array;
233     is($note,'$150.00 payment note', '$150.00 payment note is registered');
234
235     #We make a $200 payment ( > amountleft )
236     $data = '200.00';
237     $payment_note = '$200.00 payment note';
238     recordpayment($borrowernumber, $data, $sys_paytype, $payment_note);
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     ok($amountleft == -40, 'The account has -$40 as expected, (credit situation)' );
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
254 subtest "makepayment() tests" => sub {
255
256     plan tests => 6;
257
258     # Create a borrower
259     my $category   = $builder->build({ source => 'Category' })->{ categorycode };
260     my $branch     = $builder->build({ source => 'Branch' })->{ branchcode };
261     $branchcode = $branch;
262     my $borrowernumber = $builder->build({
263         source => 'Borrower',
264         value  => { categorycode => $category,
265                     branchcode   => $branch }
266     })->{ borrowernumber };
267
268     my $amount = 100;
269     my $accountline = $builder->build({ source => 'Accountline',
270         value  => { borrowernumber => $borrowernumber,
271                     amount => $amount,
272                     amountoutstanding => $amount }
273     });
274
275     my $rs = $schema->resultset('Accountline')->search({
276         borrowernumber => $borrowernumber
277     });
278
279     is( $rs->count(), 1, 'Accountline created' );
280
281     # make the full payment
282     makepayment(
283         $accountline->{ accountlines_id }, $borrowernumber,
284         $accountline->{ accountno },       $amount,
285         $borrowernumber, $branch, 'A payment note' );
286
287     # TODO: someone should write actual tests for makepayment()
288
289     my $stat = $schema->resultset('Statistic')->search({
290         branch  => $branch,
291         type    => 'payment'
292     }, { order_by => { -desc => 'datetime' } })->next();
293
294     ok( defined $stat, "There's a payment log that matches the branch" );
295
296     SKIP: {
297         skip "No statistic logged", 4 unless defined $stat;
298
299         is( $stat->type, 'payment', "Correct statistic type" );
300         is( $stat->branch, $branch, "Correct branch logged to statistics" );
301         is( $stat->borrowernumber, $borrowernumber, "Correct borrowernumber logged to statistics" );
302         is( $stat->value, "$amount" . "\.0000", "Correct amount logged to statistics" );
303     }
304 };
305
306 subtest "makepartialpayment() tests" => sub {
307
308     plan tests => 6;
309
310     # Create a borrower
311     my $category   = $builder->build({ source => 'Category' })->{ categorycode };
312     my $branch     = $builder->build({ source => 'Branch' })->{ branchcode };
313     $branchcode = $branch;
314     my $borrowernumber = $builder->build({
315         source => 'Borrower',
316         value  => { categorycode => $category,
317                     branchcode   => $branch }
318     })->{ borrowernumber };
319
320     my $amount = 100;
321     my $partialamount = 60;
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     makepartialpayment(
336         $accountline->{ accountlines_id }, $borrowernumber,
337         $accountline->{ accountno },       $partialamount,
338         $borrowernumber, $branch, 'A payment note' );
339
340     # TODO: someone should write actual tests for makepartialpayment()
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, "$partialamount" . "\.0000", "Correct amount logged to statistics" );
356     }
357 };
358
359 1;