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',
49 purge_zero_balance_fees )
52 my $schema = Koha::Database->new->schema;
53 $schema->storage->txn_begin;
54 my $dbh = C4::Context->dbh;
56 my $builder = t::lib::TestBuilder->new;
57 my $library = $builder->build( { source => 'Branch' } );
59 $dbh->do(q|DELETE FROM accountlines|);
60 $dbh->do(q|DELETE FROM issues|);
61 $dbh->do(q|DELETE FROM borrowers|);
63 my $branchcode = $library->{branchcode};
66 my $context = new Test::MockModule('C4::Context');
67 $context->mock( 'userenv', sub {
71 branch => $branchcode,
75 # Testing purge_zero_balance_fees
77 # The 3rd value in the insert is 'days ago' --
82 my $sth = $dbh->prepare(
83 "INSERT INTO accountlines (
89 VALUES ( ?, ?, (select date_sub(CURRENT_DATE, INTERVAL ? DAY) ), ? )"
95 { amount => 0 , days_ago => 0 , description =>'purge_zero_balance_fees should not delete 0 balance fees with date today' , delete => 0 } ,
96 { amount => 0 , days_ago => $days - 1 , description =>'purge_zero_balance_fees should not delete 0 balance fees with date before threshold day' , delete => 0 } ,
97 { amount => 0 , days_ago => $days , description =>'purge_zero_balance_fees should not delete 0 balance fees with date on threshold day' , delete => 0 } ,
98 { amount => 0 , days_ago => $days + 1 , description =>'purge_zero_balance_fees should delete 0 balance fees with date after threshold day' , delete => 1 } ,
99 { amount => undef , days_ago => $days + 1 , description =>'purge_zero_balance_fees should delete NULL balance fees with date after threshold day' , delete => 1 } ,
100 { amount => 5 , days_ago => $days - 1 , description =>'purge_zero_balance_fees should not delete fees with positive amout owed before threshold day' , delete => 0 } ,
101 { amount => 5 , days_ago => $days , description =>'purge_zero_balance_fees should not delete fees with positive amout owed on threshold day' , delete => 0 } ,
102 { amount => 5 , days_ago => $days + 1 , description =>'purge_zero_balance_fees should not delete fees with positive amout owed after threshold day' , delete => 0 } ,
103 { amount => -5 , days_ago => $days - 1 , description =>'purge_zero_balance_fees should not delete fees with negative amout owed before threshold day' , delete => 0 } ,
104 { amount => -5 , days_ago => $days , description =>'purge_zero_balance_fees should not delete fees with negative amout owed on threshold day' , delete => 0 } ,
105 { 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 my $borrower = Koha::Patron->new( { firstname => 'Test', surname => 'Patron', categorycode => 'PT', branchcode => $branchcode } )->store();
110 for my $data ( @test_data ) {
111 $sth->execute($borrower->borrowernumber, $data->{amount}, $data->{days_ago}, $data->{description});
114 purge_zero_balance_fees( $days );
116 $sth = $dbh->prepare(
117 "select count(*) = 0 as deleted
119 where description = ?"
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 )
131 for my $data (@test_data) {
132 is_delete_correct( $data->{delete}, $data->{description});
135 $dbh->do(q|DELETE FROM accountlines|);
137 subtest "Koha::Account::pay tests" => sub {
142 my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
143 my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode };
145 my $borrower = Koha::Patron->new( {
146 cardnumber => '1234567890',
148 firstname => 'Marty',
150 $borrower->categorycode( $categorycode );
151 $borrower->branchcode( $branchcode );
154 my $account = Koha::Account->new({ patron_id => $borrower->id });
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();
159 $sth = $dbh->prepare("SELECT count(*) FROM accountlines");
161 my $count = $sth->fetchrow_array;
162 is($count, 2, 'There is 2 lines as expected');
164 # There is $100 in the account
165 $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
166 my $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
168 for my $line ( @$amountoutstanding ) {
169 $amountleft += $line;
171 is($amountleft, 300, 'The account has 300$ as expected' );
173 # We make a $20 payment
174 my $borrowernumber = $borrower->borrowernumber;
176 my $payment_note = '$20.00 payment note';
177 $account->pay( { amount => $data, note => $payment_note } );
179 # There is now $280 in the account
180 $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
181 $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
183 for my $line ( @$amountoutstanding ) {
184 $amountleft += $line;
186 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');
194 # We make a -$30 payment (a NEGATIVE payment)
196 $payment_note = '-$30.00 payment note';
197 $account->pay( { amount => $data, note => $payment_note } );
199 # There is now $310 in the account
200 $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
201 $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
203 for my $line ( @$amountoutstanding ) {
204 $amountleft += $line;
206 is($amountleft, 310, 'The account has $310 as expected' );
207 # Is the payment note well registered
208 $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
209 $sth->execute($borrower->borrowernumber);
210 $note = $sth->fetchrow_array;
211 is($note,'-$30.00 payment note', '-$30.00 payment note is registered');
213 #We make a $150 payment ( > 1stLine )
215 $payment_note = '$150.00 payment note';
216 $account->pay( { amount => $data, note => $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);
222 for my $line ( @$amountoutstanding ) {
223 $amountleft += $line;
225 is($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');
233 #We make a $200 payment ( > amountleft )
235 $payment_note = '$200.00 payment note';
236 $account->pay( { amount => $data, note => $payment_note } );
238 # There is now -$40 in the account
239 $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
240 $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
242 for my $line ( @$amountoutstanding ) {
243 $amountleft += $line;
245 is($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');
253 my $line3 = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amountoutstanding => 42, accounttype => 'TEST' })->store();
254 my $payment_id = $account->pay( { lines => [$line3], amount => 42 } );
255 my $payment = Koha::Account::Lines->find( $payment_id );
256 is( $payment->amount(), '-42.000000', "Payment paid the specified fine" );
257 $line3 = Koha::Account::Lines->find( $line3->id );
258 is( $line3->amountoutstanding, '0.000000', "Specified fine is paid" );
261 subtest "Koha::Account::pay particular line tests" => sub {
266 my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
267 my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode };
269 my $borrower = Koha::Patron->new( {
270 cardnumber => 'kylemhall',
274 $borrower->categorycode( $categorycode );
275 $borrower->branchcode( $branchcode );
278 my $account = Koha::Account->new({ patron_id => $borrower->id });
280 my $line1 = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amountoutstanding => 1 })->store();
281 my $line2 = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amountoutstanding => 2 })->store();
282 my $line3 = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amountoutstanding => 3 })->store();
283 my $line4 = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amountoutstanding => 4 })->store();
285 is( $account->balance(), "10.000000", "Account balance is 10" );
289 lines => [$line2, $line3, $line4],
294 $_->_result->discard_changes foreach ( $line1, $line2, $line3, $line4 );
296 # Line1 is not paid at all, as it was not passed in the lines param
297 is( $line1->amountoutstanding, "1.000000", "Line 1 was not paid" );
298 # Line2 was paid in full, as it was the first in the lines list
299 is( $line2->amountoutstanding, "0.000000", "Line 2 was paid in full" );
300 # Line3 was paid partially, as the remaining balance did not cover it entirely
301 is( $line3->amountoutstanding, "1.000000", "Line 3 was paid to 1.00" );
302 # Line4 was not paid at all, as the payment was all used up by that point
303 is( $line4->amountoutstanding, "4.000000", "Line 4 was not paid" );
306 subtest "Koha::Account::pay writeoff tests" => sub {
311 my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
312 my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode };
314 my $borrower = Koha::Patron->new( {
315 cardnumber => 'chelseahall',
317 firstname => 'Chelsea',
319 $borrower->categorycode( $categorycode );
320 $borrower->branchcode( $branchcode );
323 my $account = Koha::Account->new({ patron_id => $borrower->id });
325 my $line = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amountoutstanding => 42 })->store();
327 is( $account->balance(), "42.000000", "Account balance is 42" );
329 my $id = $account->pay(
337 $line->_result->discard_changes();
339 is( $line->amountoutstanding, "0.000000", "Line was written off" );
341 my $writeoff = Koha::Account::Lines->find( $id );
343 is( $writeoff->accounttype, 'W', 'Type is correct' );
344 is( $writeoff->description, 'Writeoff', 'Description is correct' );
345 is( $writeoff->amount, '-42.000000', 'Amount is correct' );
348 subtest "More Koha::Account::pay tests" => sub {
353 my $category = $builder->build({ source => 'Category' })->{ categorycode };
354 my $branch = $builder->build({ source => 'Branch' })->{ branchcode };
355 $branchcode = $branch;
356 my $borrowernumber = $builder->build({
357 source => 'Borrower',
358 value => { categorycode => $category,
359 branchcode => $branch }
360 })->{ borrowernumber };
363 my $accountline = $builder->build({ source => 'Accountline',
364 value => { borrowernumber => $borrowernumber,
366 amountoutstanding => $amount }
369 my $rs = $schema->resultset('Accountline')->search({
370 borrowernumber => $borrowernumber
373 is( $rs->count(), 1, 'Accountline created' );
375 my $account = Koha::Account->new( { patron_id => $borrowernumber } );
376 my $line = Koha::Account::Lines->find( $accountline->{ accountlines_id } );
377 # make the full payment
378 $account->pay({ lines => [$line], amount => $amount, library_id => $branch, note => 'A payment note' });
380 my $stat = $schema->resultset('Statistic')->search({
383 }, { order_by => { -desc => 'datetime' } })->next();
385 ok( defined $stat, "There's a payment log that matches the branch" );
388 skip "No statistic logged", 4 unless defined $stat;
390 is( $stat->type, 'payment', "Correct statistic type" );
391 is( $stat->branch, $branch, "Correct branch logged to statistics" );
392 is( $stat->borrowernumber, $borrowernumber, "Correct borrowernumber logged to statistics" );
393 is( $stat->value, "$amount" . "\.0000", "Correct amount logged to statistics" );
397 subtest "Even more Koha::Account::pay tests" => sub {
402 my $category = $builder->build({ source => 'Category' })->{ categorycode };
403 my $branch = $builder->build({ source => 'Branch' })->{ branchcode };
404 $branchcode = $branch;
405 my $borrowernumber = $builder->build({
406 source => 'Borrower',
407 value => { categorycode => $category,
408 branchcode => $branch }
409 })->{ borrowernumber };
412 my $partialamount = 60;
413 my $accountline = $builder->build({ source => 'Accountline',
414 value => { borrowernumber => $borrowernumber,
416 amountoutstanding => $amount }
419 my $rs = $schema->resultset('Accountline')->search({
420 borrowernumber => $borrowernumber
423 is( $rs->count(), 1, 'Accountline created' );
425 my $account = Koha::Account->new( { patron_id => $borrowernumber } );
426 my $line = Koha::Account::Lines->find( $accountline->{ accountlines_id } );
427 # make the full payment
428 $account->pay({ lines => [$line], amount => $partialamount, library_id => $branch, note => 'A payment note' });
430 my $stat = $schema->resultset('Statistic')->search({
433 }, { order_by => { -desc => 'datetime' } })->next();
435 ok( defined $stat, "There's a payment log that matches the branch" );
438 skip "No statistic logged", 4 unless defined $stat;
440 is( $stat->type, 'payment', "Correct statistic type" );
441 is( $stat->branch, $branch, "Correct branch logged to statistics" );
442 is( $stat->borrowernumber, $borrowernumber, "Correct borrowernumber logged to statistics" );
443 is( $stat->value, "$partialamount" . "\.0000", "Correct amount logged to statistics" );
447 subtest 'balance' => sub {
450 my $patron = $builder->build({source => 'Borrower'});
451 $patron = Koha::Patrons->find( $patron->{borrowernumber} );
452 my $account = $patron->account;
453 is( $account->balance, 0, 'balance should return 0 if the patron does not have fines' );
455 my $accountline_1 = $builder->build(
457 source => 'Accountline',
459 borrowernumber => $patron->borrowernumber,
461 amountoutstanding => 42
465 my $accountline_2 = $builder->build(
467 source => 'Accountline',
469 borrowernumber => $patron->borrowernumber,
471 amountoutstanding => -13
476 my $balance = $patron->account->balance;
477 is( int($balance), 29, 'balance should return the correct value');