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 => 21;
25 use t::lib::TestBuilder;
27 use Koha::Account::Lines;
28 use Koha::Account::Line;
31 use_ok('C4::Accounts');
32 use_ok('Koha::Object');
33 use_ok('Koha::Patron');
34 use_ok('Data::Dumper');
37 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 "makepayment() tests" => sub {
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 };
321 my $accountline = $builder->build({ source => 'Accountline',
322 value => { borrowernumber => $borrowernumber,
324 amountoutstanding => $amount }
327 my $rs = $schema->resultset('Accountline')->search({
328 borrowernumber => $borrowernumber
331 is( $rs->count(), 1, 'Accountline created' );
333 # make the full payment
335 $accountline->{ accountlines_id }, $borrowernumber,
336 $accountline->{ accountno }, $amount,
337 $borrowernumber, $branch, 'A payment note' );
339 # TODO: someone should write actual tests for makepayment()
341 my $stat = $schema->resultset('Statistic')->search({
344 }, { order_by => { -desc => 'datetime' } })->next();
346 ok( defined $stat, "There's a payment log that matches the branch" );
349 skip "No statistic logged", 4 unless defined $stat;
351 is( $stat->type, 'payment', "Correct statistic type" );
352 is( $stat->branch, $branch, "Correct branch logged to statistics" );
353 is( $stat->borrowernumber, $borrowernumber, "Correct borrowernumber logged to statistics" );
354 is( $stat->value, "$amount" . "\.0000", "Correct amount logged to statistics" );
358 subtest "Even more Koha::Account::pay tests" => sub {
363 my $category = $builder->build({ source => 'Category' })->{ categorycode };
364 my $branch = $builder->build({ source => 'Branch' })->{ branchcode };
365 $branchcode = $branch;
366 my $borrowernumber = $builder->build({
367 source => 'Borrower',
368 value => { categorycode => $category,
369 branchcode => $branch }
370 })->{ borrowernumber };
373 my $partialamount = 60;
374 my $accountline = $builder->build({ source => 'Accountline',
375 value => { borrowernumber => $borrowernumber,
377 amountoutstanding => $amount }
380 my $rs = $schema->resultset('Accountline')->search({
381 borrowernumber => $borrowernumber
384 is( $rs->count(), 1, 'Accountline created' );
386 my $account = Koha::Account->new( { patron_id => $borrowernumber } );
387 my $line = Koha::Account::Lines->find( $accountline->{ accountlines_id } );
388 # make the full payment
389 $account->pay({ lines => [$line], amount => $partialamount, library_id => $branch, note => 'A payment note' });
391 my $stat = $schema->resultset('Statistic')->search({
394 }, { order_by => { -desc => 'datetime' } })->next();
396 ok( defined $stat, "There's a payment log that matches the branch" );
399 skip "No statistic logged", 4 unless defined $stat;
401 is( $stat->type, 'payment', "Correct statistic type" );
402 is( $stat->branch, $branch, "Correct branch logged to statistics" );
403 is( $stat->borrowernumber, $borrowernumber, "Correct borrowernumber logged to statistics" );
404 is( $stat->value, "$partialamount" . "\.0000", "Correct amount logged to statistics" );
408 subtest 'balance' => sub {
411 my $patron = $builder->build({source => 'Borrower'});
412 $patron = Koha::Patrons->find( $patron->{borrowernumber} );
413 my $account = $patron->account;
414 is( $account->balance, 0, 'balance should return 0 if the patron does not have fines' );
416 my $accountline_1 = $builder->build(
418 source => 'Accountline',
420 borrowernumber => $patron->borrowernumber,
422 amountoutstanding => 42
426 my $accountline_2 = $builder->build(
428 source => 'Accountline',
430 borrowernumber => $patron->borrowernumber,
432 amountoutstanding => -13
437 my $balance = $patron->account->balance;
438 is( int($balance), 29, 'balance should return the correct value');