|
Lines 1-16
Link Here
|
| 1 |
#!/usr/bin/perl |
1 |
#!/usr/bin/perl |
|
|
2 |
|
| 3 |
# Copyright 2015 BibLibre |
| 4 |
# |
| 5 |
# This file is part of Koha. |
| 6 |
# |
| 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 |
| 10 |
# version. |
| 11 |
# |
| 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. |
| 2 |
# |
15 |
# |
| 3 |
# This Koha test module is a stub! |
16 |
# You should have received a copy of the GNU General Public License along |
| 4 |
# Add more tests here!!! |
17 |
# with Koha; if not, see <http://www.gnu.org/licenses>. |
| 5 |
|
18 |
|
| 6 |
use strict; |
19 |
use strict; |
| 7 |
use warnings; |
20 |
use warnings; |
| 8 |
|
21 |
|
| 9 |
use Test::More tests => 1; |
22 |
use Test::More tests => 15; |
|
|
23 |
use Test::Warn; |
| 10 |
|
24 |
|
| 11 |
BEGIN { |
25 |
BEGIN { |
| 12 |
use_ok('C4::Accounts'); |
26 |
use_ok('C4::Accounts'); |
|
|
27 |
use_ok('Koha::Object'); |
| 28 |
use_ok('Koha::Borrower'); |
| 29 |
use_ok('Data::Dumper'); |
| 13 |
} |
30 |
} |
| 14 |
|
31 |
|
|
|
32 |
can_ok( 'C4::Accounts', |
| 33 |
qw( recordpayment |
| 34 |
makepayment |
| 35 |
getnextacctno |
| 36 |
|
| 37 |
chargelostitem |
| 38 |
manualinvoice |
| 39 |
getcharges |
| 40 |
ModNote |
| 41 |
getcredits |
| 42 |
getrefunds |
| 43 |
ReversePayment |
| 44 |
recordpayment_selectaccts |
| 45 |
makepartialpayment |
| 46 |
WriteOffFee ) |
| 47 |
); |
| 48 |
|
| 49 |
my $dbh = C4::Context->dbh; |
| 50 |
$dbh->{RaiseError}=1; |
| 51 |
$dbh->{AutoCommit}=0; |
| 52 |
$dbh->do(q|DELETE FROM accountlines|); |
| 53 |
$dbh->do(q|DELETE FROM borrowers|); |
| 54 |
|
| 55 |
# Mock userenv |
| 56 |
local $SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /redefined/ }; |
| 57 |
my $userenv; |
| 58 |
*C4::Context::userenv = \&Mock_userenv; |
| 59 |
$userenv = { flags => 1, id => 'my_userid', branch => 'CPL' }; |
| 60 |
|
| 61 |
# A Borrower for the tests ---------------------- |
| 62 |
my $categorycode = Koha::Database->new()->schema()->resultset('Category')->first()->categorycode(); |
| 63 |
my $branchcode = Koha::Database->new()->schema()->resultset('Branch')->first()->branchcode(); |
| 64 |
|
| 65 |
my $borrower = Koha::Borrower->new( { |
| 66 |
cardnumber => '1234567890', |
| 67 |
surname => 'McFly', |
| 68 |
firstname => 'Marty', |
| 69 |
} ); |
| 70 |
$borrower->categorycode( $categorycode ); |
| 71 |
$borrower->branchcode( $branchcode ); |
| 72 |
$borrower->store; |
| 73 |
|
| 74 |
my $sth = $dbh->prepare( |
| 75 |
"INSERT INTO accountlines ( |
| 76 |
borrowernumber, |
| 77 |
amountoutstanding ) |
| 78 |
VALUES ( ?, ? )" |
| 79 |
); |
| 80 |
$sth->execute($borrower->borrowernumber, '100'); |
| 81 |
$sth->execute($borrower->borrowernumber, '200'); |
| 82 |
|
| 83 |
$sth = $dbh->prepare("SELECT count(*) FROM accountlines"); |
| 84 |
$sth->execute; |
| 85 |
my $count = $sth->fetchrow_array; |
| 86 |
is ($count, 2, 'There is 2 lines as expected'); |
| 87 |
|
| 88 |
# Testing recordpayment ------------------------- |
| 89 |
# There is $100 in the account |
| 90 |
$sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?"); |
| 91 |
my $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber); |
| 92 |
my $amountleft = 0; |
| 93 |
for my $line ( @$amountoutstanding ) { |
| 94 |
$amountleft += $line; |
| 95 |
} |
| 96 |
ok($amountleft == 300, 'The account has 300$ as expected' ); |
| 97 |
|
| 98 |
# We make a $20 payment |
| 99 |
my $borrowernumber = $borrower->borrowernumber; |
| 100 |
my $data = '20.00'; |
| 101 |
my $sys_paytype; |
| 102 |
my $payment_note = '$20.00 payment note'; |
| 103 |
recordpayment($borrowernumber, $data, $sys_paytype, $payment_note); |
| 104 |
# There is now $280 in the account |
| 105 |
$sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?"); |
| 106 |
$amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber); |
| 107 |
$amountleft = 0; |
| 108 |
for my $line ( @$amountoutstanding ) { |
| 109 |
$amountleft += $line; |
| 110 |
} |
| 111 |
ok($amountleft == 280, 'The account has $280 as expected' ); |
| 112 |
# Is the payment note well registered |
| 113 |
$sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1"); |
| 114 |
$sth->execute($borrower->borrowernumber); |
| 115 |
my $note = $sth->fetchrow_array; |
| 116 |
is($note,'$20.00 payment note', '$20.00 payment note is registered'); |
| 117 |
|
| 118 |
# We make a -$30 payment (a NEGATIVE payment) |
| 119 |
$data = '-30.00'; |
| 120 |
$payment_note = '-$30.00 payment note'; |
| 121 |
recordpayment($borrowernumber, $data, $sys_paytype, $payment_note); |
| 122 |
# There is now $310 in the account |
| 123 |
$sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?"); |
| 124 |
$amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber); |
| 125 |
$amountleft = 0; |
| 126 |
for my $line ( @$amountoutstanding ) { |
| 127 |
$amountleft += $line; |
| 128 |
} |
| 129 |
ok($amountleft == 310, 'The account has $310 as expected' ); |
| 130 |
# Is the payment note well registered |
| 131 |
$sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1"); |
| 132 |
$sth->execute($borrower->borrowernumber); |
| 133 |
$note = $sth->fetchrow_array; |
| 134 |
is($note,'-$30.00 payment note', '-$30.00 payment note is registered'); |
| 135 |
|
| 136 |
#We make a $150 payment ( > 1stLine ) |
| 137 |
$data = '150.00'; |
| 138 |
$payment_note = '$150.00 payment note'; |
| 139 |
recordpayment($borrowernumber, $data, $sys_paytype, $payment_note); |
| 140 |
# There is now $160 in the account |
| 141 |
$sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?"); |
| 142 |
$amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber); |
| 143 |
$amountleft = 0; |
| 144 |
for my $line ( @$amountoutstanding ) { |
| 145 |
$amountleft += $line; |
| 146 |
} |
| 147 |
ok($amountleft == 160, 'The account has $160 as expected' ); |
| 148 |
# Is the payment note well registered |
| 149 |
$sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1"); |
| 150 |
$sth->execute($borrower->borrowernumber); |
| 151 |
$note = $sth->fetchrow_array; |
| 152 |
is($note,'$150.00 payment note', '$150.00 payment note is registered'); |
| 153 |
|
| 154 |
#We make a $200 payment ( > amountleft ) |
| 155 |
$data = '200.00'; |
| 156 |
$payment_note = '$200.00 payment note'; |
| 157 |
recordpayment($borrowernumber, $data, $sys_paytype, $payment_note); |
| 158 |
# There is now -$40 in the account |
| 159 |
$sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?"); |
| 160 |
$amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber); |
| 161 |
$amountleft = 0; |
| 162 |
for my $line ( @$amountoutstanding ) { |
| 163 |
$amountleft += $line; |
| 164 |
} |
| 165 |
ok($amountleft == -40, 'The account has -$40 as expected, (credit situation)' ); |
| 166 |
# Is the payment note well registered |
| 167 |
$sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1"); |
| 168 |
$sth->execute($borrower->borrowernumber); |
| 169 |
$note = $sth->fetchrow_array; |
| 170 |
is($note,'$200.00 payment note', '$200.00 payment note is registered'); |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
$dbh->rollback; |
| 175 |
|
| 15 |
|
176 |
|
|
|
177 |
# Sub ------------------------------------------- |
| 16 |
|
178 |
|
| 17 |
- |
179 |
# C4::Context->userenv |
|
|
180 |
sub Mock_userenv { |
| 181 |
return $userenv; |
| 182 |
} |