Bug 15758: Koha::Libraries - Do not select an option if selected is defined
[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 my $builder = t::lib::TestBuilder->new;
56 my $library = $builder->build({ source => 'Branch' });
57
58 $dbh->do(q|DELETE FROM accountlines|);
59 $dbh->do(q|DELETE FROM issues|);
60 $dbh->do(q|DELETE FROM borrowers|);
61
62 my $branchcode = $library->{branchcode};
63 my $borrower_number;
64
65 my $context = new Test::MockModule('C4::Context');
66 $context->mock( 'userenv', sub {
67     return {
68         flags  => 1,
69         id     => 'my_userid',
70         branch => $branchcode,
71     };
72 });
73
74 # Testing purge_zero_balance_fees
75
76 # The 3rd value in the insert is 'days ago' --
77 # 0 => today
78 # 1 => yesterday
79 # etc.
80
81 my $sth = $dbh->prepare(
82     "INSERT INTO accountlines (
83          borrowernumber,
84          amountoutstanding,
85          date,
86          description
87      )
88      VALUES ( ?, ?, (select date_sub(CURRENT_DATE, INTERVAL ? DAY) ), ? )"
89 );
90
91 my $days = 5;
92
93 my @test_data = (
94     { amount => 0     , days_ago => 0         , description =>'purge_zero_balance_fees should not delete 0 balance fees with date today'                     , delete => 0 } ,
95     { amount => 0     , days_ago => $days - 1 , description =>'purge_zero_balance_fees should not delete 0 balance fees with date before threshold day'      , delete => 0 } ,
96     { amount => 0     , days_ago => $days     , description =>'purge_zero_balance_fees should not delete 0 balance fees with date on threshold day'          , delete => 0 } ,
97     { amount => 0     , days_ago => $days + 1 , description =>'purge_zero_balance_fees should delete 0 balance fees with date after threshold day'           , delete => 1 } ,
98     { amount => undef , days_ago => $days + 1 , description =>'purge_zero_balance_fees should delete NULL balance fees with date after threshold day'        , delete => 1 } ,
99     { amount => 5     , days_ago => $days - 1 , description =>'purge_zero_balance_fees should not delete fees with positive amout owed before threshold day'  , delete => 0 } ,
100     { amount => 5     , days_ago => $days     , description =>'purge_zero_balance_fees should not delete fees with positive amout owed on threshold day'      , delete => 0 } ,
101     { amount => 5     , days_ago => $days + 1 , description =>'purge_zero_balance_fees should not delete fees with positive amout owed after threshold day'   , delete => 0 } ,
102     { amount => -5    , days_ago => $days - 1 , description =>'purge_zero_balance_fees should not delete fees with negative amout owed before threshold day' , delete => 0 } ,
103     { amount => -5    , days_ago => $days     , description =>'purge_zero_balance_fees should not delete fees with negative amout owed on threshold day'     , delete => 0 } ,
104     { amount => -5    , days_ago => $days + 1 , description =>'purge_zero_balance_fees should not delete fees with negative amout owed after threshold day'  , delete => 0 }
105 );
106
107 my $borrower = Koha::Patron->new( { firstname => 'Test', surname => 'Patron', categorycode => 'PT', branchcode => $branchcode } )->store();
108
109 for my $data ( @test_data ) {
110     $sth->execute($borrower->borrowernumber, $data->{amount}, $data->{days_ago}, $data->{description});
111 }
112
113 purge_zero_balance_fees( $days );
114
115 $sth = $dbh->prepare(
116             "select count(*) = 0 as deleted
117              from accountlines
118              where description = ?"
119        );
120
121 #
122 sub is_delete_correct {
123     my $should_delete = shift;
124     my $description = shift;
125     $sth->execute( $description );
126     my $test = $sth->fetchrow_hashref();
127     is( $test->{deleted}, $should_delete, $description )
128 }
129
130 for my $data  (@test_data) {
131     is_delete_correct( $data->{delete}, $data->{description});
132 }
133
134 $dbh->do(q|DELETE FROM accountlines|);
135
136 subtest "recordpayment() tests" => sub {
137
138     plan tests => 10;
139
140     # Create a borrower
141     my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
142     my $branchcode   = $builder->build({ source => 'Branch' })->{ branchcode };
143
144     my $borrower = Koha::Patron->new( {
145         cardnumber => '1234567890',
146         surname => 'McFly',
147         firstname => 'Marty',
148     } );
149     $borrower->categorycode( $categorycode );
150     $borrower->branchcode( $branchcode );
151     $borrower->store;
152
153     my $sth = $dbh->prepare(
154         "INSERT INTO accountlines (
155             borrowernumber,
156             amountoutstanding )
157         VALUES ( ?, ? )"
158     );
159     $sth->execute($borrower->borrowernumber, '100');
160     $sth->execute($borrower->borrowernumber, '200');
161
162     $sth = $dbh->prepare("SELECT count(*) FROM accountlines");
163     $sth->execute;
164     my $count = $sth->fetchrow_array;
165     is ($count, 2, 'There is 2 lines as expected');
166
167     # Testing recordpayment -------------------------
168     # There is $100 in the account
169     $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
170     my $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
171     my $amountleft = 0;
172     for my $line ( @$amountoutstanding ) {
173         $amountleft += $line;
174     }
175     ok($amountleft == 300, 'The account has 300$ as expected' );
176
177     # We make a $20 payment
178     my $borrowernumber = $borrower->borrowernumber;
179     my $data = '20.00';
180     my $sys_paytype;
181     my $payment_note = '$20.00 payment note';
182     recordpayment($borrowernumber, $data, $sys_paytype, $payment_note);
183     # There is now $280 in the account
184     $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
185     $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
186     $amountleft = 0;
187     for my $line ( @$amountoutstanding ) {
188         $amountleft += $line;
189     }
190     ok($amountleft == 280, 'The account has $280 as expected' );
191     # Is the payment note well registered
192     $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
193     $sth->execute($borrower->borrowernumber);
194     my $note = $sth->fetchrow_array;
195     is($note,'$20.00 payment note', '$20.00 payment note is registered');
196
197     # We make a -$30 payment (a NEGATIVE payment)
198     $data = '-30.00';
199     $payment_note = '-$30.00 payment note';
200     recordpayment($borrowernumber, $data, $sys_paytype, $payment_note);
201     # There is now $310 in the account
202     $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
203     $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
204     $amountleft = 0;
205     for my $line ( @$amountoutstanding ) {
206         $amountleft += $line;
207     }
208     ok($amountleft == 310, 'The account has $310 as expected' );
209     # Is the payment note well registered
210     $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
211     $sth->execute($borrower->borrowernumber);
212     $note = $sth->fetchrow_array;
213     is($note,'-$30.00 payment note', '-$30.00 payment note is registered');
214
215     #We make a $150 payment ( > 1stLine )
216     $data = '150.00';
217     $payment_note = '$150.00 payment note';
218     recordpayment($borrowernumber, $data, $sys_paytype, $payment_note);
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     ok($amountleft == 160, 'The account has $160 as expected' );
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     recordpayment($borrowernumber, $data, $sys_paytype, $payment_note);
237     # There is now -$40 in the account
238     $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
239     $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
240     $amountleft = 0;
241     for my $line ( @$amountoutstanding ) {
242         $amountleft += $line;
243     }
244     ok($amountleft == -40, 'The account has -$40 as expected, (credit situation)' );
245     # Is the payment note well registered
246     $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
247     $sth->execute($borrower->borrowernumber);
248     $note = $sth->fetchrow_array;
249     is($note,'$200.00 payment note', '$200.00 payment note is registered');
250 };
251
252 subtest "makepayment() tests" => sub {
253
254     plan tests => 6;
255
256     # Create a borrower
257     my $category   = $builder->build({ source => 'Category' })->{ categorycode };
258     my $branch     = $builder->build({ source => 'Branch' })->{ branchcode };
259     $branchcode = $branch;
260     my $borrowernumber = $builder->build({
261         source => 'Borrower',
262         value  => { categorycode => $category,
263                     branchcode   => $branch }
264     })->{ borrowernumber };
265
266     my $amount = 100;
267     my $accountline = $builder->build({ source => 'Accountline',
268         value  => { borrowernumber => $borrowernumber,
269                     amount => $amount,
270                     amountoutstanding => $amount }
271     });
272
273     my $rs = $schema->resultset('Accountline')->search({
274         borrowernumber => $borrowernumber
275     });
276
277     is( $rs->count(), 1, 'Accountline created' );
278
279     # make the full payment
280     makepayment(
281         $accountline->{ accountlines_id }, $borrowernumber,
282         $accountline->{ accountno },       $amount,
283         $borrowernumber, $branch, 'A payment note' );
284
285     # TODO: someone should write actual tests for makepayment()
286
287     my $stat = $schema->resultset('Statistic')->search({
288         branch  => $branch,
289         type    => 'payment'
290     }, { order_by => { -desc => 'datetime' } })->next();
291
292     ok( defined $stat, "There's a payment log that matches the branch" );
293
294     SKIP: {
295         skip "No statistic logged", 4 unless defined $stat;
296
297         is( $stat->type, 'payment', "Correct statistic type" );
298         is( $stat->branch, $branch, "Correct branch logged to statistics" );
299         is( $stat->borrowernumber, $borrowernumber, "Correct borrowernumber logged to statistics" );
300         is( $stat->value, "$amount" . "\.0000", "Correct amount logged to statistics" );
301     }
302 };
303
304 subtest "makepartialpayment() tests" => sub {
305
306     plan tests => 6;
307
308     # Create a borrower
309     my $category   = $builder->build({ source => 'Category' })->{ categorycode };
310     my $branch     = $builder->build({ source => 'Branch' })->{ branchcode };
311     $branchcode = $branch;
312     my $borrowernumber = $builder->build({
313         source => 'Borrower',
314         value  => { categorycode => $category,
315                     branchcode   => $branch }
316     })->{ borrowernumber };
317
318     my $amount = 100;
319     my $partialamount = 60;
320     my $accountline = $builder->build({ source => 'Accountline',
321         value  => { borrowernumber => $borrowernumber,
322                     amount => $amount,
323                     amountoutstanding => $amount }
324     });
325
326     my $rs = $schema->resultset('Accountline')->search({
327         borrowernumber => $borrowernumber
328     });
329
330     is( $rs->count(), 1, 'Accountline created' );
331
332     # make the full payment
333     makepartialpayment(
334         $accountline->{ accountlines_id }, $borrowernumber,
335         $accountline->{ accountno },       $partialamount,
336         $borrowernumber, $branch, 'A payment note' );
337
338     # TODO: someone should write actual tests for makepartialpayment()
339
340     my $stat = $schema->resultset('Statistic')->search({
341         branch  => $branch,
342         type    => 'payment'
343     }, { order_by => { -desc => 'datetime' } })->next();
344
345     ok( defined $stat, "There's a payment log that matches the branch" );
346
347     SKIP: {
348         skip "No statistic logged", 4 unless defined $stat;
349
350         is( $stat->type, 'payment', "Correct statistic type" );
351         is( $stat->branch, $branch, "Correct branch logged to statistics" );
352         is( $stat->borrowernumber, $borrowernumber, "Correct borrowernumber logged to statistics" );
353         is( $stat->value, "$partialamount" . "\.0000", "Correct amount logged to statistics" );
354     }
355 };
356
357 1;