4 # Copyright 2015 BibLibre
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 3 of the License, or (at your option) any later
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, see <http://www.gnu.org/licenses>.
22 use Test
::More tests
=> 19;
26 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 recordpayment_selectaccts
52 purge_zero_balance_fees )
55 my $schema = Koha
::Database
->new->schema;
56 $schema->storage->txn_begin;
57 my $dbh = C4
::Context
->dbh;
59 my $builder = t
::lib
::TestBuilder
->new;
60 my $library = $builder->build( { source
=> 'Branch' } );
62 $dbh->do(q
|DELETE FROM accountlines
|);
63 $dbh->do(q
|DELETE FROM issues
|);
64 $dbh->do(q
|DELETE FROM borrowers
|);
66 my $branchcode = $library->{branchcode
};
69 my $context = new Test
::MockModule
('C4::Context');
70 $context->mock( 'userenv', sub {
74 branch
=> $branchcode,
78 # Testing purge_zero_balance_fees
80 # The 3rd value in the insert is 'days ago' --
85 my $sth = $dbh->prepare(
86 "INSERT INTO accountlines (
92 VALUES ( ?, ?, (select date_sub(CURRENT_DATE, INTERVAL ? DAY) ), ? )"
98 { amount
=> 0 , days_ago
=> 0 , description
=>'purge_zero_balance_fees should not delete 0 balance fees with date today' , delete => 0 } ,
99 { amount
=> 0 , days_ago
=> $days - 1 , description
=>'purge_zero_balance_fees should not delete 0 balance fees with date before threshold day' , delete => 0 } ,
100 { amount
=> 0 , days_ago
=> $days , description
=>'purge_zero_balance_fees should not delete 0 balance fees with date on threshold day' , delete => 0 } ,
101 { amount
=> 0 , days_ago
=> $days + 1 , description
=>'purge_zero_balance_fees should delete 0 balance fees with date after threshold day' , delete => 1 } ,
102 { amount
=> undef , days_ago
=> $days + 1 , description
=>'purge_zero_balance_fees should delete NULL balance fees with date after threshold day' , delete => 1 } ,
103 { amount
=> 5 , days_ago
=> $days - 1 , description
=>'purge_zero_balance_fees should not delete fees with positive amout owed before threshold day' , delete => 0 } ,
104 { amount
=> 5 , days_ago
=> $days , description
=>'purge_zero_balance_fees should not delete fees with positive amout owed on threshold day' , delete => 0 } ,
105 { amount
=> 5 , days_ago
=> $days + 1 , description
=>'purge_zero_balance_fees should not delete fees with positive amout owed after threshold day' , delete => 0 } ,
106 { amount
=> -5 , days_ago
=> $days - 1 , description
=>'purge_zero_balance_fees should not delete fees with negative amout owed before threshold day' , delete => 0 } ,
107 { amount
=> -5 , days_ago
=> $days , description
=>'purge_zero_balance_fees should not delete fees with negative amout owed on threshold day' , delete => 0 } ,
108 { amount
=> -5 , days_ago
=> $days + 1 , description
=>'purge_zero_balance_fees should not delete fees with negative amout owed after threshold day' , delete => 0 }
111 my $borrower = Koha
::Patron
->new( { firstname
=> 'Test', surname
=> 'Patron', categorycode
=> 'PT', branchcode
=> $branchcode } )->store();
113 for my $data ( @test_data ) {
114 $sth->execute($borrower->borrowernumber, $data->{amount
}, $data->{days_ago
}, $data->{description
});
117 purge_zero_balance_fees
( $days );
119 $sth = $dbh->prepare(
120 "select count(*) = 0 as deleted
122 where description = ?"
126 sub is_delete_correct
{
127 my $should_delete = shift;
128 my $description = shift;
129 $sth->execute( $description );
130 my $test = $sth->fetchrow_hashref();
131 is
( $test->{deleted
}, $should_delete, $description )
134 for my $data (@test_data) {
135 is_delete_correct
( $data->{delete}, $data->{description
});
138 $dbh->do(q
|DELETE FROM accountlines
|);
140 subtest
"Koha::Account::pay tests" => sub {
145 my $categorycode = $builder->build({ source
=> 'Category' })->{ categorycode
};
146 my $branchcode = $builder->build({ source
=> 'Branch' })->{ branchcode
};
148 my $borrower = Koha
::Patron
->new( {
149 cardnumber
=> '1234567890',
151 firstname
=> 'Marty',
153 $borrower->categorycode( $categorycode );
154 $borrower->branchcode( $branchcode );
157 my $account = Koha
::Account
->new({ patron_id
=> $borrower->id });
159 my $line1 = Koha
::Account
::Line
->new({ borrowernumber
=> $borrower->borrowernumber, amountoutstanding
=> 100 })->store();
160 my $line2 = Koha
::Account
::Line
->new({ borrowernumber
=> $borrower->borrowernumber, amountoutstanding
=> 200 })->store();
162 $sth = $dbh->prepare("SELECT count(*) FROM accountlines");
164 my $count = $sth->fetchrow_array;
165 is
($count, 2, 'There is 2 lines as expected');
167 # There is $100 in the account
168 $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
169 my $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
171 for my $line ( @
$amountoutstanding ) {
172 $amountleft += $line;
174 is
($amountleft, 300, 'The account has 300$ as expected' );
176 # We make a $20 payment
177 my $borrowernumber = $borrower->borrowernumber;
179 my $payment_note = '$20.00 payment note';
180 $account->pay( { amount
=> $data, note
=> $payment_note } );
182 # There is now $280 in the account
183 $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
184 $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
186 for my $line ( @
$amountoutstanding ) {
187 $amountleft += $line;
189 is
($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');
197 # We make a -$30 payment (a NEGATIVE payment)
199 $payment_note = '-$30.00 payment note';
200 $account->pay( { amount
=> $data, note
=> $payment_note } );
202 # There is now $310 in the account
203 $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
204 $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
206 for my $line ( @
$amountoutstanding ) {
207 $amountleft += $line;
209 is
($amountleft, 310, 'The account has $310 as expected' );
210 # Is the payment note well registered
211 $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
212 $sth->execute($borrower->borrowernumber);
213 $note = $sth->fetchrow_array;
214 is
($note,'-$30.00 payment note', '-$30.00 payment note is registered');
216 #We make a $150 payment ( > 1stLine )
218 $payment_note = '$150.00 payment note';
219 $account->pay( { amount
=> $data, note
=> $payment_note } );
221 # There is now $160 in the account
222 $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
223 $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
225 for my $line ( @
$amountoutstanding ) {
226 $amountleft += $line;
228 is
($amountleft, 160, 'The account has $160 as expected' );
230 # Is the payment note well registered
231 $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
232 $sth->execute($borrower->borrowernumber);
233 $note = $sth->fetchrow_array;
234 is
($note,'$150.00 payment note', '$150.00 payment note is registered');
236 #We make a $200 payment ( > amountleft )
238 $payment_note = '$200.00 payment note';
239 $account->pay( { amount
=> $data, note
=> $payment_note } );
241 # There is now -$40 in the account
242 $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
243 $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
245 for my $line ( @
$amountoutstanding ) {
246 $amountleft += $line;
248 is
($amountleft, -40, 'The account has -$40 as expected, (credit situation)' );
250 # Is the payment note well registered
251 $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
252 $sth->execute($borrower->borrowernumber);
253 $note = $sth->fetchrow_array;
254 is
($note,'$200.00 payment note', '$200.00 payment note is registered');
256 my $line3 = Koha
::Account
::Line
->new({ borrowernumber
=> $borrower->borrowernumber, amountoutstanding
=> 42, accounttype
=> 'TEST' })->store();
257 my $payment_id = $account->pay( { accountlines_id
=> $line3->id, amount
=> 42 } );
258 my $payment = Koha
::Account
::Lines
->find( $payment_id );
259 is
( $payment->amount(), '-42.000000', "Payment paid the specified fine" );
260 $line3 = Koha
::Account
::Lines
->find( $line3->id );
261 is
( $line3->amountoutstanding, '0.000000', "Specified fine is paid" );
264 subtest
"makepayment() tests" => sub {
269 my $category = $builder->build({ source
=> 'Category' })->{ categorycode
};
270 my $branch = $builder->build({ source
=> 'Branch' })->{ branchcode
};
271 $branchcode = $branch;
272 my $borrowernumber = $builder->build({
273 source
=> 'Borrower',
274 value
=> { categorycode
=> $category,
275 branchcode
=> $branch }
276 })->{ borrowernumber
};
279 my $accountline = $builder->build({ source
=> 'Accountline',
280 value
=> { borrowernumber
=> $borrowernumber,
282 amountoutstanding
=> $amount }
285 my $rs = $schema->resultset('Accountline')->search({
286 borrowernumber
=> $borrowernumber
289 is
( $rs->count(), 1, 'Accountline created' );
291 # make the full payment
293 $accountline->{ accountlines_id
}, $borrowernumber,
294 $accountline->{ accountno
}, $amount,
295 $borrowernumber, $branch, 'A payment note' );
297 # TODO: someone should write actual tests for makepayment()
299 my $stat = $schema->resultset('Statistic')->search({
302 }, { order_by
=> { -desc
=> 'datetime' } })->next();
304 ok
( defined $stat, "There's a payment log that matches the branch" );
307 skip
"No statistic logged", 4 unless defined $stat;
309 is
( $stat->type, 'payment', "Correct statistic type" );
310 is
( $stat->branch, $branch, "Correct branch logged to statistics" );
311 is
( $stat->borrowernumber, $borrowernumber, "Correct borrowernumber logged to statistics" );
312 is
( $stat->value, "$amount" . "\.0000", "Correct amount logged to statistics" );
316 subtest
"makepartialpayment() tests" => sub {
321 my $category = $builder->build({ source
=> 'Category' })->{ categorycode
};
322 my $branch = $builder->build({ source
=> 'Branch' })->{ branchcode
};
323 $branchcode = $branch;
324 my $borrowernumber = $builder->build({
325 source
=> 'Borrower',
326 value
=> { categorycode
=> $category,
327 branchcode
=> $branch }
328 })->{ borrowernumber
};
331 my $partialamount = 60;
332 my $accountline = $builder->build({ source
=> 'Accountline',
333 value
=> { borrowernumber
=> $borrowernumber,
335 amountoutstanding
=> $amount }
338 my $rs = $schema->resultset('Accountline')->search({
339 borrowernumber
=> $borrowernumber
342 is
( $rs->count(), 1, 'Accountline created' );
344 # make the full payment
346 $accountline->{ accountlines_id
}, $borrowernumber,
347 $accountline->{ accountno
}, $partialamount,
348 $borrowernumber, $branch, 'A payment note' );
350 # TODO: someone should write actual tests for makepartialpayment()
352 my $stat = $schema->resultset('Statistic')->search({
355 }, { order_by
=> { -desc
=> 'datetime' } })->next();
357 ok
( defined $stat, "There's a payment log that matches the branch" );
360 skip
"No statistic logged", 4 unless defined $stat;
362 is
( $stat->type, 'payment', "Correct statistic type" );
363 is
( $stat->branch, $branch, "Correct branch logged to statistics" );
364 is
( $stat->borrowernumber, $borrowernumber, "Correct borrowernumber logged to statistics" );
365 is
( $stat->value, "$partialamount" . "\.0000", "Correct amount logged to statistics" );