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
=> 19;
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',
48 recordpayment_selectaccts
51 purge_zero_balance_fees )
54 my $schema = Koha
::Database
->new->schema;
55 $schema->storage->txn_begin;
56 my $dbh = C4
::Context
->dbh;
58 my $builder = t
::lib
::TestBuilder
->new;
59 my $library = $builder->build({ source
=> 'Branch' });
61 $dbh->do(q
|DELETE FROM accountlines
|);
62 $dbh->do(q
|DELETE FROM issues
|);
63 $dbh->do(q
|DELETE FROM borrowers
|);
65 my $branchcode = $library->{branchcode
};
68 my $context = new Test
::MockModule
('C4::Context');
69 $context->mock( 'userenv', sub {
73 branch
=> $branchcode,
77 # Testing purge_zero_balance_fees
79 # The 3rd value in the insert is 'days ago' --
84 my $sth = $dbh->prepare(
85 "INSERT INTO accountlines (
91 VALUES ( ?, ?, (select date_sub(CURRENT_DATE, INTERVAL ? DAY) ), ? )"
97 { amount
=> 0 , days_ago
=> 0 , description
=>'purge_zero_balance_fees should not delete 0 balance fees with date today' , delete => 0 } ,
98 { amount
=> 0 , days_ago
=> $days - 1 , description
=>'purge_zero_balance_fees should not delete 0 balance fees with date before threshold day' , delete => 0 } ,
99 { amount
=> 0 , days_ago
=> $days , description
=>'purge_zero_balance_fees should not delete 0 balance fees with date on threshold day' , delete => 0 } ,
100 { amount
=> 0 , days_ago
=> $days + 1 , description
=>'purge_zero_balance_fees should delete 0 balance fees with date after threshold day' , delete => 1 } ,
101 { amount
=> undef , days_ago
=> $days + 1 , description
=>'purge_zero_balance_fees should delete NULL balance fees with date after threshold day' , delete => 1 } ,
102 { amount
=> 5 , days_ago
=> $days - 1 , description
=>'purge_zero_balance_fees should not delete fees with positive amout owed before threshold day' , delete => 0 } ,
103 { amount
=> 5 , days_ago
=> $days , description
=>'purge_zero_balance_fees should not delete fees with positive amout owed on threshold day' , delete => 0 } ,
104 { amount
=> 5 , days_ago
=> $days + 1 , description
=>'purge_zero_balance_fees should not delete fees with positive amout owed after threshold day' , delete => 0 } ,
105 { amount
=> -5 , days_ago
=> $days - 1 , description
=>'purge_zero_balance_fees should not delete fees with negative amout owed before threshold day' , delete => 0 } ,
106 { amount
=> -5 , days_ago
=> $days , description
=>'purge_zero_balance_fees should not delete fees with negative amout owed on threshold day' , delete => 0 } ,
107 { amount
=> -5 , days_ago
=> $days + 1 , description
=>'purge_zero_balance_fees should not delete fees with negative amout owed after threshold day' , delete => 0 }
110 my $borrower = Koha
::Patron
->new( { firstname
=> 'Test', surname
=> 'Patron', categorycode
=> 'PT', branchcode
=> $branchcode } )->store();
112 for my $data ( @test_data ) {
113 $sth->execute($borrower->borrowernumber, $data->{amount
}, $data->{days_ago
}, $data->{description
});
116 purge_zero_balance_fees
( $days );
118 $sth = $dbh->prepare(
119 "select count(*) = 0 as deleted
121 where description = ?"
125 sub is_delete_correct
{
126 my $should_delete = shift;
127 my $description = shift;
128 $sth->execute( $description );
129 my $test = $sth->fetchrow_hashref();
130 is
( $test->{deleted
}, $should_delete, $description )
133 for my $data (@test_data) {
134 is_delete_correct
( $data->{delete}, $data->{description
});
137 $dbh->do(q
|DELETE FROM accountlines
|);
139 subtest
"Koha::Account::pay tests" => sub {
144 my $categorycode = $builder->build({ source
=> 'Category' })->{ categorycode
};
145 my $branchcode = $builder->build({ source
=> 'Branch' })->{ branchcode
};
147 my $borrower = Koha
::Patron
->new( {
148 cardnumber
=> '1234567890',
150 firstname
=> 'Marty',
152 $borrower->categorycode( $categorycode );
153 $borrower->branchcode( $branchcode );
156 my $account = Koha
::Account
->new({ patron_id
=> $borrower->id });
158 my $line1 = Koha
::Account
::Line
->new({ borrowernumber
=> $borrower->borrowernumber, amountoutstanding
=> 100 })->store();
159 my $line2 = Koha
::Account
::Line
->new({ borrowernumber
=> $borrower->borrowernumber, amountoutstanding
=> 200 })->store();
161 $sth = $dbh->prepare("SELECT count(*) FROM accountlines");
163 my $count = $sth->fetchrow_array;
164 is
($count, 2, 'There is 2 lines as expected');
166 # There is $100 in the account
167 $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
168 my $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
170 for my $line ( @
$amountoutstanding ) {
171 $amountleft += $line;
173 is
($amountleft, 300, 'The account has 300$ as expected' );
175 # We make a $20 payment
176 my $borrowernumber = $borrower->borrowernumber;
178 my $payment_note = '$20.00 payment note';
179 $account->pay( { amount
=> $data, note
=> $payment_note } );
181 # There is now $280 in the account
182 $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
183 $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
185 for my $line ( @
$amountoutstanding ) {
186 $amountleft += $line;
188 is
($amountleft, 280, 'The account has $280 as expected' );
190 # Is the payment note well registered
191 $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
192 $sth->execute($borrower->borrowernumber);
193 my $note = $sth->fetchrow_array;
194 is
($note,'$20.00 payment note', '$20.00 payment note is registered');
196 # We make a -$30 payment (a NEGATIVE payment)
198 $payment_note = '-$30.00 payment note';
199 $account->pay( { amount
=> $data, note
=> $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);
205 for my $line ( @
$amountoutstanding ) {
206 $amountleft += $line;
208 is
($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');
215 #We make a $150 payment ( > 1stLine )
217 $payment_note = '$150.00 payment note';
218 $account->pay( { amount
=> $data, note
=> $payment_note } );
220 # There is now $160 in the account
221 $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
222 $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
224 for my $line ( @
$amountoutstanding ) {
225 $amountleft += $line;
227 is
($amountleft, 160, 'The account has $160 as expected' );
228 # Is the payment note well registered
229 $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
230 $sth->execute($borrower->borrowernumber);
231 $note = $sth->fetchrow_array;
232 is
($note,'$150.00 payment note', '$150.00 payment note is registered');
234 #We make a $200 payment ( > amountleft )
236 $payment_note = '$200.00 payment note';
237 $account->pay( { amount
=> $data, note
=> $payment_note } );
239 # There is now -$40 in the account
240 $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
241 $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
243 for my $line ( @
$amountoutstanding ) {
244 $amountleft += $line;
246 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');
254 subtest
"makepayment() tests" => sub {
259 my $category = $builder->build({ source
=> 'Category' })->{ categorycode
};
260 my $branch = $builder->build({ source
=> 'Branch' })->{ branchcode
};
261 $branchcode = $branch;
262 my $borrowernumber = $builder->build({
263 source
=> 'Borrower',
264 value
=> { categorycode
=> $category,
265 branchcode
=> $branch }
266 })->{ borrowernumber
};
269 my $accountline = $builder->build({ source
=> 'Accountline',
270 value
=> { borrowernumber
=> $borrowernumber,
272 amountoutstanding
=> $amount }
275 my $rs = $schema->resultset('Accountline')->search({
276 borrowernumber
=> $borrowernumber
279 is
( $rs->count(), 1, 'Accountline created' );
281 # make the full payment
283 $accountline->{ accountlines_id
}, $borrowernumber,
284 $accountline->{ accountno
}, $amount,
285 $borrowernumber, $branch, 'A payment note' );
287 # TODO: someone should write actual tests for makepayment()
289 my $stat = $schema->resultset('Statistic')->search({
292 }, { order_by
=> { -desc
=> 'datetime' } })->next();
294 ok
( defined $stat, "There's a payment log that matches the branch" );
297 skip
"No statistic logged", 4 unless defined $stat;
299 is
( $stat->type, 'payment', "Correct statistic type" );
300 is
( $stat->branch, $branch, "Correct branch logged to statistics" );
301 is
( $stat->borrowernumber, $borrowernumber, "Correct borrowernumber logged to statistics" );
302 is
( $stat->value, "$amount" . "\.0000", "Correct amount logged to statistics" );
306 subtest
"makepartialpayment() 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 $partialamount = 60;
322 my $accountline = $builder->build({ source
=> 'Accountline',
323 value
=> { borrowernumber
=> $borrowernumber,
325 amountoutstanding
=> $amount }
328 my $rs = $schema->resultset('Accountline')->search({
329 borrowernumber
=> $borrowernumber
332 is
( $rs->count(), 1, 'Accountline created' );
334 # make the full payment
336 $accountline->{ accountlines_id
}, $borrowernumber,
337 $accountline->{ accountno
}, $partialamount,
338 $borrowernumber, $branch, 'A payment note' );
340 # TODO: someone should write actual tests for makepartialpayment()
342 my $stat = $schema->resultset('Statistic')->search({
345 }, { order_by
=> { -desc
=> 'datetime' } })->next();
347 ok
( defined $stat, "There's a payment log that matches the branch" );
350 skip
"No statistic logged", 4 unless defined $stat;
352 is
( $stat->type, 'payment', "Correct statistic type" );
353 is
( $stat->branch, $branch, "Correct branch logged to statistics" );
354 is
( $stat->borrowernumber, $borrowernumber, "Correct borrowernumber logged to statistics" );
355 is
( $stat->value, "$partialamount" . "\.0000", "Correct amount logged to statistics" );