3 # Copyright 2015 BibLibre
5 # This file is part of Koha.
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
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.
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>.
21 use Test
::More tests
=> 22;
25 use t
::lib
::TestBuilder
;
28 use Koha
::Account
::Lines
;
29 use Koha
::Account
::Line
;
32 use_ok
('C4::Accounts');
33 use_ok
('Koha::Object');
34 use_ok
('Koha::Patron');
35 use_ok
('Data::Dumper');
38 can_ok
( 'C4::Accounts',
48 purge_zero_balance_fees )
51 my $schema = Koha
::Database
->new->schema;
52 $schema->storage->txn_begin;
53 my $dbh = C4
::Context
->dbh;
55 my $builder = t
::lib
::TestBuilder
->new;
56 my $library = $builder->build( { source
=> 'Branch' } );
58 $dbh->do(q
|DELETE FROM accountlines
|);
59 $dbh->do(q
|DELETE FROM issues
|);
60 $dbh->do(q
|DELETE FROM borrowers
|);
62 my $branchcode = $library->{branchcode
};
65 my $context = new Test
::MockModule
('C4::Context');
66 $context->mock( 'userenv', sub {
70 branch
=> $branchcode,
74 # Testing purge_zero_balance_fees
76 # The 3rd value in the insert is 'days ago' --
81 my $sth = $dbh->prepare(
82 "INSERT INTO accountlines (
88 VALUES ( ?, ?, (select date_sub(CURRENT_DATE, INTERVAL ? DAY) ), ? )"
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 }
106 my $categorycode = $builder->build({ source
=> 'Category' })->{categorycode
};
107 my $borrower = Koha
::Patron
->new( { firstname
=> 'Test', surname
=> 'Patron', categorycode
=> $categorycode, branchcode
=> $branchcode } )->store();
109 for my $data ( @test_data ) {
110 $sth->execute($borrower->borrowernumber, $data->{amount
}, $data->{days_ago
}, $data->{description
});
113 purge_zero_balance_fees
( $days );
115 $sth = $dbh->prepare(
116 "select count(*) = 0 as deleted
118 where description = ?"
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 )
130 for my $data (@test_data) {
131 is_delete_correct
( $data->{delete}, $data->{description
});
134 $dbh->do(q
|DELETE FROM accountlines
|);
136 subtest
"Koha::Account::pay tests" => sub {
141 my $categorycode = $builder->build({ source
=> 'Category' })->{ categorycode
};
142 my $branchcode = $builder->build({ source
=> 'Branch' })->{ branchcode
};
144 my $borrower = Koha
::Patron
->new( {
145 cardnumber
=> '1234567890',
147 firstname
=> 'Marty',
149 $borrower->categorycode( $categorycode );
150 $borrower->branchcode( $branchcode );
153 my $account = Koha
::Account
->new({ patron_id
=> $borrower->id });
155 my $line1 = Koha
::Account
::Line
->new({ borrowernumber
=> $borrower->borrowernumber, amountoutstanding
=> 100 })->store();
156 my $line2 = Koha
::Account
::Line
->new({ borrowernumber
=> $borrower->borrowernumber, amountoutstanding
=> 200 })->store();
158 $sth = $dbh->prepare("SELECT count(*) FROM accountlines");
160 my $count = $sth->fetchrow_array;
161 is
($count, 2, 'There is 2 lines as expected');
163 # There is $100 in the account
164 $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
165 my $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
167 for my $line ( @
$amountoutstanding ) {
168 $amountleft += $line;
170 is
($amountleft, 300, 'The account has 300$ as expected' );
172 # We make a $20 payment
173 my $borrowernumber = $borrower->borrowernumber;
175 my $payment_note = '$20.00 payment note';
176 $account->pay( { amount
=> $data, note
=> $payment_note } );
178 # There is now $280 in the account
179 $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
180 $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
182 for my $line ( @
$amountoutstanding ) {
183 $amountleft += $line;
185 is
($amountleft, 280, 'The account has $280 as expected' );
187 # Is the payment note well registered
188 $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
189 $sth->execute($borrower->borrowernumber);
190 my $note = $sth->fetchrow_array;
191 is
($note,'$20.00 payment note', '$20.00 payment note is registered');
193 # We make a -$30 payment (a NEGATIVE payment)
195 $payment_note = '-$30.00 payment note';
196 $account->pay( { amount
=> $data, note
=> $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);
202 for my $line ( @
$amountoutstanding ) {
203 $amountleft += $line;
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');
212 #We make a $150 payment ( > 1stLine )
214 $payment_note = '$150.00 payment note';
215 $account->pay( { amount
=> $data, note
=> $payment_note } );
217 # There is now $160 in the account
218 $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
219 $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
221 for my $line ( @
$amountoutstanding ) {
222 $amountleft += $line;
224 is
($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');
232 #We make a $200 payment ( > amountleft )
234 $payment_note = '$200.00 payment note';
235 $account->pay( { amount
=> $data, note
=> $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);
241 for my $line ( @
$amountoutstanding ) {
242 $amountleft += $line;
244 is
($amountleft, -40, 'The account has -$40 as expected, (credit situation)' );
246 # Is the payment note well registered
247 $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
248 $sth->execute($borrower->borrowernumber);
249 $note = $sth->fetchrow_array;
250 is
($note,'$200.00 payment note', '$200.00 payment note is registered');
252 my $line3 = Koha
::Account
::Line
->new({ borrowernumber
=> $borrower->borrowernumber, amountoutstanding
=> 42, accounttype
=> 'TEST' })->store();
253 my $payment_id = $account->pay( { lines
=> [$line3], amount
=> 42 } );
254 my $payment = Koha
::Account
::Lines
->find( $payment_id );
255 is
( $payment->amount(), '-42.000000', "Payment paid the specified fine" );
256 $line3 = Koha
::Account
::Lines
->find( $line3->id );
257 is
( $line3->amountoutstanding, '0.000000', "Specified fine is paid" );
260 subtest
"Koha::Account::pay particular line tests" => sub {
265 my $categorycode = $builder->build({ source
=> 'Category' })->{ categorycode
};
266 my $branchcode = $builder->build({ source
=> 'Branch' })->{ branchcode
};
268 my $borrower = Koha
::Patron
->new( {
269 cardnumber
=> 'kylemhall',
273 $borrower->categorycode( $categorycode );
274 $borrower->branchcode( $branchcode );
277 my $account = Koha
::Account
->new({ patron_id
=> $borrower->id });
279 my $line1 = Koha
::Account
::Line
->new({ borrowernumber
=> $borrower->borrowernumber, amountoutstanding
=> 1 })->store();
280 my $line2 = Koha
::Account
::Line
->new({ borrowernumber
=> $borrower->borrowernumber, amountoutstanding
=> 2 })->store();
281 my $line3 = Koha
::Account
::Line
->new({ borrowernumber
=> $borrower->borrowernumber, amountoutstanding
=> 3 })->store();
282 my $line4 = Koha
::Account
::Line
->new({ borrowernumber
=> $borrower->borrowernumber, amountoutstanding
=> 4 })->store();
284 is
( $account->balance(), "10.000000", "Account balance is 10" );
288 lines
=> [$line2, $line3, $line4],
293 $_->_result->discard_changes foreach ( $line1, $line2, $line3, $line4 );
295 # Line1 is not paid at all, as it was not passed in the lines param
296 is
( $line1->amountoutstanding, "1.000000", "Line 1 was not paid" );
297 # Line2 was paid in full, as it was the first in the lines list
298 is
( $line2->amountoutstanding, "0.000000", "Line 2 was paid in full" );
299 # Line3 was paid partially, as the remaining balance did not cover it entirely
300 is
( $line3->amountoutstanding, "1.000000", "Line 3 was paid to 1.00" );
301 # Line4 was not paid at all, as the payment was all used up by that point
302 is
( $line4->amountoutstanding, "4.000000", "Line 4 was not paid" );
305 subtest
"Koha::Account::pay writeoff tests" => sub {
310 my $categorycode = $builder->build({ source
=> 'Category' })->{ categorycode
};
311 my $branchcode = $builder->build({ source
=> 'Branch' })->{ branchcode
};
313 my $borrower = Koha
::Patron
->new( {
314 cardnumber
=> 'chelseahall',
316 firstname
=> 'Chelsea',
318 $borrower->categorycode( $categorycode );
319 $borrower->branchcode( $branchcode );
322 my $account = Koha
::Account
->new({ patron_id
=> $borrower->id });
324 my $line = Koha
::Account
::Line
->new({ borrowernumber
=> $borrower->borrowernumber, amountoutstanding
=> 42 })->store();
326 is
( $account->balance(), "42.000000", "Account balance is 42" );
328 my $id = $account->pay(
336 $line->_result->discard_changes();
338 is
( $line->amountoutstanding, "0.000000", "Line was written off" );
340 my $writeoff = Koha
::Account
::Lines
->find( $id );
342 is
( $writeoff->accounttype, 'W', 'Type is correct' );
343 is
( $writeoff->description, 'Writeoff', 'Description is correct' );
344 is
( $writeoff->amount, '-42.000000', 'Amount is correct' );
347 subtest
"More Koha::Account::pay tests" => sub {
352 my $category = $builder->build({ source
=> 'Category' })->{ categorycode
};
353 my $branch = $builder->build({ source
=> 'Branch' })->{ branchcode
};
354 $branchcode = $branch;
355 my $borrowernumber = $builder->build({
356 source
=> 'Borrower',
357 value
=> { categorycode
=> $category,
358 branchcode
=> $branch }
359 })->{ borrowernumber
};
362 my $accountline = $builder->build({ source
=> 'Accountline',
363 value
=> { borrowernumber
=> $borrowernumber,
365 amountoutstanding
=> $amount }
368 my $rs = $schema->resultset('Accountline')->search({
369 borrowernumber
=> $borrowernumber
372 is
( $rs->count(), 1, 'Accountline created' );
374 my $account = Koha
::Account
->new( { patron_id
=> $borrowernumber } );
375 my $line = Koha
::Account
::Lines
->find( $accountline->{ accountlines_id
} );
376 # make the full payment
377 $account->pay({ lines
=> [$line], amount
=> $amount, library_id
=> $branch, note
=> 'A payment note' });
379 my $stat = $schema->resultset('Statistic')->search({
382 }, { order_by
=> { -desc
=> 'datetime' } })->next();
384 ok
( defined $stat, "There's a payment log that matches the branch" );
387 skip
"No statistic logged", 4 unless defined $stat;
389 is
( $stat->type, 'payment', "Correct statistic type" );
390 is
( $stat->branch, $branch, "Correct branch logged to statistics" );
391 is
( $stat->borrowernumber, $borrowernumber, "Correct borrowernumber logged to statistics" );
392 is
( $stat->value, "$amount" . "\.0000", "Correct amount logged to statistics" );
396 subtest
"Even more Koha::Account::pay tests" => sub {
401 my $category = $builder->build({ source
=> 'Category' })->{ categorycode
};
402 my $branch = $builder->build({ source
=> 'Branch' })->{ branchcode
};
403 $branchcode = $branch;
404 my $borrowernumber = $builder->build({
405 source
=> 'Borrower',
406 value
=> { categorycode
=> $category,
407 branchcode
=> $branch }
408 })->{ borrowernumber
};
411 my $partialamount = 60;
412 my $accountline = $builder->build({ source
=> 'Accountline',
413 value
=> { borrowernumber
=> $borrowernumber,
415 amountoutstanding
=> $amount }
418 my $rs = $schema->resultset('Accountline')->search({
419 borrowernumber
=> $borrowernumber
422 is
( $rs->count(), 1, 'Accountline created' );
424 my $account = Koha
::Account
->new( { patron_id
=> $borrowernumber } );
425 my $line = Koha
::Account
::Lines
->find( $accountline->{ accountlines_id
} );
426 # make the full payment
427 $account->pay({ lines
=> [$line], amount
=> $partialamount, library_id
=> $branch, note
=> 'A payment note' });
429 my $stat = $schema->resultset('Statistic')->search({
432 }, { order_by
=> { -desc
=> 'datetime' } })->next();
434 ok
( defined $stat, "There's a payment log that matches the branch" );
437 skip
"No statistic logged", 4 unless defined $stat;
439 is
( $stat->type, 'payment', "Correct statistic type" );
440 is
( $stat->branch, $branch, "Correct branch logged to statistics" );
441 is
( $stat->borrowernumber, $borrowernumber, "Correct borrowernumber logged to statistics" );
442 is
( $stat->value, "$partialamount" . "\.0000", "Correct amount logged to statistics" );
446 subtest
'balance' => sub {
449 my $patron = $builder->build({source
=> 'Borrower'});
450 $patron = Koha
::Patrons
->find( $patron->{borrowernumber
} );
451 my $account = $patron->account;
452 is
( $account->balance, 0, 'balance should return 0 if the patron does not have fines' );
454 my $accountline_1 = $builder->build(
456 source
=> 'Accountline',
458 borrowernumber
=> $patron->borrowernumber,
460 amountoutstanding
=> 42
464 my $accountline_2 = $builder->build(
466 source
=> 'Accountline',
468 borrowernumber
=> $patron->borrowernumber,
470 amountoutstanding
=> -13
475 my $balance = $patron->account->balance;
476 is
( int($balance), 29, 'balance should return the correct value');