Bug 15895: Remove useless discard_changes calls
[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 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( recordpayment
39         makepayment
40         getnextacctno
41         chargelostitem
42         manualinvoice
43         getcharges
44         ModNote
45         getcredits
46         getrefunds
47         ReversePayment
48         recordpayment_selectaccts
49         makepartialpayment
50         WriteOffFee
51         purge_zero_balance_fees )
52 );
53
54 my $schema  = Koha::Database->new->schema;
55 $schema->storage->txn_begin;
56 my $dbh = C4::Context->dbh;
57
58 my $builder = t::lib::TestBuilder->new;
59 my $library = $builder->build({ source => 'Branch' });
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 my $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 positive amout owed before threshold day'  , delete => 0 } ,
103     { amount => 5     , days_ago => $days     , description =>'purge_zero_balance_fees should not delete fees with positive amout owed on threshold day'      , delete => 0 } ,
104     { amount => 5     , days_ago => $days + 1 , description =>'purge_zero_balance_fees should not delete fees with positive 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 my $borrower = Koha::Patron->new( { firstname => 'Test', surname => 'Patron', categorycode => 'PT', branchcode => $branchcode } )->store();
111
112 for my $data ( @test_data ) {
113     $sth->execute($borrower->borrowernumber, $data->{amount}, $data->{days_ago}, $data->{description});
114 }
115
116 purge_zero_balance_fees( $days );
117
118 $sth = $dbh->prepare(
119             "select count(*) = 0 as deleted
120              from accountlines
121              where description = ?"
122        );
123
124 #
125 sub is_delete_correct {
126     my $should_delete = shift;
127     my $description = shift;
128     $sth->execute( $description );
129     my $test = $sth->fetchrow_hashref();
130     is( $test->{deleted}, $should_delete, $description )
131 }
132
133 for my $data  (@test_data) {
134     is_delete_correct( $data->{delete}, $data->{description});
135 }
136
137 $dbh->do(q|DELETE FROM accountlines|);
138
139 subtest "recordpayment() tests" => sub {
140
141     plan tests => 10;
142
143     # Create a borrower
144     my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
145     my $branchcode   = $builder->build({ source => 'Branch' })->{ branchcode };
146
147     my $borrower = Koha::Patron->new( {
148         cardnumber => '1234567890',
149         surname => 'McFly',
150         firstname => 'Marty',
151     } );
152     $borrower->categorycode( $categorycode );
153     $borrower->branchcode( $branchcode );
154     $borrower->store;
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     # Testing recordpayment -------------------------
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 $sys_paytype;
178     my $payment_note = '$20.00 payment note';
179     recordpayment($borrowernumber, $data, $sys_paytype, $payment_note);
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     # 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     recordpayment($borrowernumber, $data, $sys_paytype, $payment_note);
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     recordpayment($borrowernumber, $data, $sys_paytype, $payment_note);
216     # There is now $160 in the account
217     $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
218     $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
219     $amountleft = 0;
220     for my $line ( @$amountoutstanding ) {
221         $amountleft += $line;
222     }
223     is($amountleft, 160, 'The account has $160 as expected' );
224     # Is the payment note well registered
225     $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
226     $sth->execute($borrower->borrowernumber);
227     $note = $sth->fetchrow_array;
228     is($note,'$150.00 payment note', '$150.00 payment note is registered');
229
230     #We make a $200 payment ( > amountleft )
231     $data = '200.00';
232     $payment_note = '$200.00 payment note';
233     recordpayment($borrowernumber, $data, $sys_paytype, $payment_note);
234     # There is now -$40 in the account
235     $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
236     $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
237     $amountleft = 0;
238     for my $line ( @$amountoutstanding ) {
239         $amountleft += $line;
240     }
241     is($amountleft, -40, 'The account has -$40 as expected, (credit situation)' );
242     # Is the payment note well registered
243     $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
244     $sth->execute($borrower->borrowernumber);
245     $note = $sth->fetchrow_array;
246     is($note,'$200.00 payment note', '$200.00 payment note is registered');
247 };
248
249 subtest "makepayment() tests" => sub {
250
251     plan tests => 6;
252
253     # Create a borrower
254     my $category   = $builder->build({ source => 'Category' })->{ categorycode };
255     my $branch     = $builder->build({ source => 'Branch' })->{ branchcode };
256     $branchcode = $branch;
257     my $borrowernumber = $builder->build({
258         source => 'Borrower',
259         value  => { categorycode => $category,
260                     branchcode   => $branch }
261     })->{ borrowernumber };
262
263     my $amount = 100;
264     my $accountline = $builder->build({ source => 'Accountline',
265         value  => { borrowernumber => $borrowernumber,
266                     amount => $amount,
267                     amountoutstanding => $amount }
268     });
269
270     my $rs = $schema->resultset('Accountline')->search({
271         borrowernumber => $borrowernumber
272     });
273
274     is( $rs->count(), 1, 'Accountline created' );
275
276     # make the full payment
277     makepayment(
278         $accountline->{ accountlines_id }, $borrowernumber,
279         $accountline->{ accountno },       $amount,
280         $borrowernumber, $branch, 'A payment note' );
281
282     # TODO: someone should write actual tests for makepayment()
283
284     my $stat = $schema->resultset('Statistic')->search({
285         branch  => $branch,
286         type    => 'payment'
287     }, { order_by => { -desc => 'datetime' } })->next();
288
289     ok( defined $stat, "There's a payment log that matches the branch" );
290
291     SKIP: {
292         skip "No statistic logged", 4 unless defined $stat;
293
294         is( $stat->type, 'payment', "Correct statistic type" );
295         is( $stat->branch, $branch, "Correct branch logged to statistics" );
296         is( $stat->borrowernumber, $borrowernumber, "Correct borrowernumber logged to statistics" );
297         is( $stat->value, "$amount" . "\.0000", "Correct amount logged to statistics" );
298     }
299 };
300
301 subtest "makepartialpayment() tests" => sub {
302
303     plan tests => 6;
304
305     # Create a borrower
306     my $category   = $builder->build({ source => 'Category' })->{ categorycode };
307     my $branch     = $builder->build({ source => 'Branch' })->{ branchcode };
308     $branchcode = $branch;
309     my $borrowernumber = $builder->build({
310         source => 'Borrower',
311         value  => { categorycode => $category,
312                     branchcode   => $branch }
313     })->{ borrowernumber };
314
315     my $amount = 100;
316     my $partialamount = 60;
317     my $accountline = $builder->build({ source => 'Accountline',
318         value  => { borrowernumber => $borrowernumber,
319                     amount => $amount,
320                     amountoutstanding => $amount }
321     });
322
323     my $rs = $schema->resultset('Accountline')->search({
324         borrowernumber => $borrowernumber
325     });
326
327     is( $rs->count(), 1, 'Accountline created' );
328
329     # make the full payment
330     makepartialpayment(
331         $accountline->{ accountlines_id }, $borrowernumber,
332         $accountline->{ accountno },       $partialamount,
333         $borrowernumber, $branch, 'A payment note' );
334
335     # TODO: someone should write actual tests for makepartialpayment()
336
337     my $stat = $schema->resultset('Statistic')->search({
338         branch  => $branch,
339         type    => 'payment'
340     }, { order_by => { -desc => 'datetime' } })->next();
341
342     ok( defined $stat, "There's a payment log that matches the branch" );
343
344     SKIP: {
345         skip "No statistic logged", 4 unless defined $stat;
346
347         is( $stat->type, 'payment', "Correct statistic type" );
348         is( $stat->branch, $branch, "Correct branch logged to statistics" );
349         is( $stat->borrowernumber, $borrowernumber, "Correct borrowernumber logged to statistics" );
350         is( $stat->value, "$partialamount" . "\.0000", "Correct amount logged to statistics" );
351     }
352 };
353
354 1;