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;
28 use_ok('C4::Accounts');
29 use_ok('Koha::Object');
30 use_ok('Koha::Patron');
31 use_ok('Data::Dumper');
34 can_ok( 'C4::Accounts',
45 recordpayment_selectaccts
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 }
107 my $borrower = Koha::Patron->new( { firstname => 'Test', surname => 'Patron', categorycode => 'PT', 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 "recordpayment() 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 $sth = $dbh->prepare(
154 "INSERT INTO accountlines (
159 $sth->execute($borrower->borrowernumber, '100');
160 $sth->execute($borrower->borrowernumber, '200');
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 # Testing recordpayment -------------------------
168 # There is $100 in the account
169 $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
170 my $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
172 for my $line ( @$amountoutstanding ) {
173 $amountleft += $line;
175 ok($amountleft == 300, 'The account has 300$ as expected' );
177 # We make a $20 payment
178 my $borrowernumber = $borrower->borrowernumber;
181 my $payment_note = '$20.00 payment note';
182 recordpayment($borrowernumber, $data, $sys_paytype, $payment_note);
183 # There is now $280 in the account
184 $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
185 $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
187 for my $line ( @$amountoutstanding ) {
188 $amountleft += $line;
190 ok($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 recordpayment($borrowernumber, $data, $sys_paytype, $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 ok($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 recordpayment($borrowernumber, $data, $sys_paytype, $payment_note);
219 # There is now $160 in the account
220 $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
221 $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
223 for my $line ( @$amountoutstanding ) {
224 $amountleft += $line;
226 ok($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 recordpayment($borrowernumber, $data, $sys_paytype, $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 ok($amountleft == -40, 'The account has -$40 as expected, (credit situation)' );
245 # Is the payment note well registered
246 $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
247 $sth->execute($borrower->borrowernumber);
248 $note = $sth->fetchrow_array;
249 is($note,'$200.00 payment note', '$200.00 payment note is registered');
252 subtest "makepayment() tests" => sub {
257 my $category = $builder->build({ source => 'Category' })->{ categorycode };
258 my $branch = $builder->build({ source => 'Branch' })->{ branchcode };
259 $branchcode = $branch;
260 my $borrowernumber = $builder->build({
261 source => 'Borrower',
262 value => { categorycode => $category,
263 branchcode => $branch }
264 })->{ borrowernumber };
267 my $accountline = $builder->build({ source => 'Accountline',
268 value => { borrowernumber => $borrowernumber,
270 amountoutstanding => $amount }
273 my $rs = $schema->resultset('Accountline')->search({
274 borrowernumber => $borrowernumber
277 is( $rs->count(), 1, 'Accountline created' );
279 # make the full payment
281 $accountline->{ accountlines_id }, $borrowernumber,
282 $accountline->{ accountno }, $amount,
283 $borrowernumber, $branch, 'A payment note' );
285 # TODO: someone should write actual tests for makepayment()
287 my $stat = $schema->resultset('Statistic')->search({
290 }, { order_by => { -desc => 'datetime' } })->next();
292 ok( defined $stat, "There's a payment log that matches the branch" );
295 skip "No statistic logged", 4 unless defined $stat;
297 is( $stat->type, 'payment', "Correct statistic type" );
298 is( $stat->branch, $branch, "Correct branch logged to statistics" );
299 is( $stat->borrowernumber, $borrowernumber, "Correct borrowernumber logged to statistics" );
300 is( $stat->value, "$amount" . "\.0000", "Correct amount logged to statistics" );
304 subtest "makepartialpayment() tests" => sub {
309 my $category = $builder->build({ source => 'Category' })->{ categorycode };
310 my $branch = $builder->build({ source => 'Branch' })->{ branchcode };
311 $branchcode = $branch;
312 my $borrowernumber = $builder->build({
313 source => 'Borrower',
314 value => { categorycode => $category,
315 branchcode => $branch }
316 })->{ borrowernumber };
319 my $partialamount = 60;
320 my $accountline = $builder->build({ source => 'Accountline',
321 value => { borrowernumber => $borrowernumber,
323 amountoutstanding => $amount }
326 my $rs = $schema->resultset('Accountline')->search({
327 borrowernumber => $borrowernumber
330 is( $rs->count(), 1, 'Accountline created' );
332 # make the full payment
334 $accountline->{ accountlines_id }, $borrowernumber,
335 $accountline->{ accountno }, $partialamount,
336 $borrowernumber, $branch, 'A payment note' );
338 # TODO: someone should write actual tests for makepartialpayment()
340 my $stat = $schema->resultset('Statistic')->search({
343 }, { order_by => { -desc => 'datetime' } })->next();
345 ok( defined $stat, "There's a payment log that matches the branch" );
348 skip "No statistic logged", 4 unless defined $stat;
350 is( $stat->type, 'payment', "Correct statistic type" );
351 is( $stat->branch, $branch, "Correct branch logged to statistics" );
352 is( $stat->borrowernumber, $borrowernumber, "Correct borrowernumber logged to statistics" );
353 is( $stat->value, "$partialamount" . "\.0000", "Correct amount logged to statistics" );