|
Lines 19-183
Link Here
|
| 19 |
use strict; |
19 |
use strict; |
| 20 |
use warnings; |
20 |
use warnings; |
| 21 |
|
21 |
|
| 22 |
use Test::More tests => 15; |
22 |
use Test::More tests => 19; |
| 23 |
use Test::Warn; |
23 |
|
|
|
24 |
use C4::Context; |
| 25 |
|
| 26 |
my $dbh = C4::Context->dbh; |
| 27 |
$dbh->{RaiseError}=1; |
| 28 |
$dbh->{AutoCommit}=0; |
| 24 |
|
29 |
|
| 25 |
BEGIN { |
30 |
BEGIN { |
| 26 |
use_ok('C4::Accounts'); |
31 |
use_ok('Koha::Database'); |
| 27 |
use_ok('Koha::Object'); |
32 |
use_ok('Koha::Accounts'); |
| 28 |
use_ok('Koha::Borrower'); |
33 |
use_ok('Koha::Accounts::DebitTypes'); |
| 29 |
use_ok('Data::Dumper'); |
34 |
use_ok('Koha::Accounts::CreditTypes'); |
| 30 |
} |
35 |
} |
| 31 |
|
36 |
|
| 32 |
can_ok( 'C4::Accounts', |
37 |
## Intial Setup ## |
| 33 |
qw( recordpayment |
38 |
my $borrower = Koha::Database->new()->schema->resultset('Borrower')->create( |
| 34 |
makepayment |
39 |
{ |
| 35 |
getnextacctno |
40 |
surname => 'Test', |
|
|
41 |
categorycode => 'S', |
| 42 |
branchcode => 'MPL', |
| 43 |
account_balance => 0, |
| 44 |
} |
| 45 |
); |
| 36 |
|
46 |
|
| 37 |
chargelostitem |
47 |
my $biblio = |
| 38 |
manualinvoice |
48 |
Koha::Database->new()->schema->resultset('Biblio') |
| 39 |
getcharges |
49 |
->create( { title => "Test Record" } ); |
| 40 |
ModNote |
50 |
my $biblioitem = |
| 41 |
getcredits |
51 |
Koha::Database->new()->schema->resultset('Biblioitem') |
| 42 |
getrefunds |
52 |
->create( { biblionumber => $biblio->biblionumber() } ); |
| 43 |
ReversePayment |
53 |
my $item = Koha::Database->new()->schema->resultset('Item')->create( |
| 44 |
recordpayment_selectaccts |
54 |
{ |
| 45 |
makepartialpayment |
55 |
biblionumber => $biblio->biblionumber(), |
| 46 |
WriteOffFee ) |
56 |
biblioitemnumber => $biblioitem->biblioitemnumber(), |
|
|
57 |
replacementprice => 25.00, |
| 58 |
barcode => q{TEST_ITEM_BARCODE} |
| 59 |
} |
| 47 |
); |
60 |
); |
| 48 |
|
61 |
|
| 49 |
my $dbh = C4::Context->dbh; |
62 |
my $issue = Koha::Database->new()->schema->resultset('Issue')->create( |
| 50 |
$dbh->{RaiseError}=1; |
63 |
{ |
| 51 |
$dbh->{AutoCommit}=0; |
64 |
borrowernumber => $borrower->borrowernumber(), |
| 52 |
$dbh->do(q|DELETE FROM accountlines|); |
65 |
itemnumber => $item->itemnumber(), |
| 53 |
$dbh->do(q|DELETE FROM borrowers|); |
66 |
} |
| 54 |
$dbh->do(q|DELETE FROM issues|); |
67 |
); |
| 55 |
|
68 |
## END initial setup |
| 56 |
# Mock userenv |
|
|
| 57 |
local $SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /redefined/ }; |
| 58 |
my $userenv; |
| 59 |
*C4::Context::userenv = \&Mock_userenv; |
| 60 |
$userenv = { flags => 1, id => 'my_userid', branch => 'CPL' }; |
| 61 |
|
| 62 |
# A Borrower for the tests ---------------------- |
| 63 |
my $categorycode = Koha::Database->new()->schema()->resultset('Category')->first()->categorycode(); |
| 64 |
my $branchcode = Koha::Database->new()->schema()->resultset('Branch')->first()->branchcode(); |
| 65 |
|
| 66 |
my $borrower = Koha::Borrower->new( { |
| 67 |
cardnumber => '1234567890', |
| 68 |
surname => 'McFly', |
| 69 |
firstname => 'Marty', |
| 70 |
} ); |
| 71 |
$borrower->categorycode( $categorycode ); |
| 72 |
$borrower->branchcode( $branchcode ); |
| 73 |
$borrower->store; |
| 74 |
|
| 75 |
my $sth = $dbh->prepare( |
| 76 |
"INSERT INTO accountlines ( |
| 77 |
borrowernumber, |
| 78 |
amountoutstanding ) |
| 79 |
VALUES ( ?, ? )" |
| 80 |
); |
| 81 |
$sth->execute($borrower->borrowernumber, '100'); |
| 82 |
$sth->execute($borrower->borrowernumber, '200'); |
| 83 |
|
| 84 |
$sth = $dbh->prepare("SELECT count(*) FROM accountlines"); |
| 85 |
$sth->execute; |
| 86 |
my $count = $sth->fetchrow_array; |
| 87 |
is ($count, 2, 'There is 2 lines as expected'); |
| 88 |
|
| 89 |
# Testing recordpayment ------------------------- |
| 90 |
# There is $100 in the account |
| 91 |
$sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?"); |
| 92 |
my $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber); |
| 93 |
my $amountleft = 0; |
| 94 |
for my $line ( @$amountoutstanding ) { |
| 95 |
$amountleft += $line; |
| 96 |
} |
| 97 |
ok($amountleft == 300, 'The account has 300$ as expected' ); |
| 98 |
|
| 99 |
# We make a $20 payment |
| 100 |
my $borrowernumber = $borrower->borrowernumber; |
| 101 |
my $data = '20.00'; |
| 102 |
my $sys_paytype; |
| 103 |
my $payment_note = '$20.00 payment note'; |
| 104 |
recordpayment($borrowernumber, $data, $sys_paytype, $payment_note); |
| 105 |
# There is now $280 in the account |
| 106 |
$sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?"); |
| 107 |
$amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber); |
| 108 |
$amountleft = 0; |
| 109 |
for my $line ( @$amountoutstanding ) { |
| 110 |
$amountleft += $line; |
| 111 |
} |
| 112 |
ok($amountleft == 280, 'The account has $280 as expected' ); |
| 113 |
# Is the payment note well registered |
| 114 |
$sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1"); |
| 115 |
$sth->execute($borrower->borrowernumber); |
| 116 |
my $note = $sth->fetchrow_array; |
| 117 |
is($note,'$20.00 payment note', '$20.00 payment note is registered'); |
| 118 |
|
| 119 |
# We make a -$30 payment (a NEGATIVE payment) |
| 120 |
$data = '-30.00'; |
| 121 |
$payment_note = '-$30.00 payment note'; |
| 122 |
recordpayment($borrowernumber, $data, $sys_paytype, $payment_note); |
| 123 |
# There is now $310 in the account |
| 124 |
$sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?"); |
| 125 |
$amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber); |
| 126 |
$amountleft = 0; |
| 127 |
for my $line ( @$amountoutstanding ) { |
| 128 |
$amountleft += $line; |
| 129 |
} |
| 130 |
ok($amountleft == 310, 'The account has $310 as expected' ); |
| 131 |
# Is the payment note well registered |
| 132 |
$sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1"); |
| 133 |
$sth->execute($borrower->borrowernumber); |
| 134 |
$note = $sth->fetchrow_array; |
| 135 |
is($note,'-$30.00 payment note', '-$30.00 payment note is registered'); |
| 136 |
|
| 137 |
#We make a $150 payment ( > 1stLine ) |
| 138 |
$data = '150.00'; |
| 139 |
$payment_note = '$150.00 payment note'; |
| 140 |
recordpayment($borrowernumber, $data, $sys_paytype, $payment_note); |
| 141 |
# There is now $160 in the account |
| 142 |
$sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?"); |
| 143 |
$amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber); |
| 144 |
$amountleft = 0; |
| 145 |
for my $line ( @$amountoutstanding ) { |
| 146 |
$amountleft += $line; |
| 147 |
} |
| 148 |
ok($amountleft == 160, 'The account has $160 as expected' ); |
| 149 |
# Is the payment note well registered |
| 150 |
$sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1"); |
| 151 |
$sth->execute($borrower->borrowernumber); |
| 152 |
$note = $sth->fetchrow_array; |
| 153 |
is($note,'$150.00 payment note', '$150.00 payment note is registered'); |
| 154 |
|
| 155 |
#We make a $200 payment ( > amountleft ) |
| 156 |
$data = '200.00'; |
| 157 |
$payment_note = '$200.00 payment note'; |
| 158 |
recordpayment($borrowernumber, $data, $sys_paytype, $payment_note); |
| 159 |
# There is now -$40 in the account |
| 160 |
$sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?"); |
| 161 |
$amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber); |
| 162 |
$amountleft = 0; |
| 163 |
for my $line ( @$amountoutstanding ) { |
| 164 |
$amountleft += $line; |
| 165 |
} |
| 166 |
ok($amountleft == -40, 'The account has -$40 as expected, (credit situation)' ); |
| 167 |
# Is the payment note well registered |
| 168 |
$sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1"); |
| 169 |
$sth->execute($borrower->borrowernumber); |
| 170 |
$note = $sth->fetchrow_array; |
| 171 |
is($note,'$200.00 payment note', '$200.00 payment note is registered'); |
| 172 |
|
69 |
|
|
|
70 |
ok( Koha::Accounts::DebitTypes::Fine eq 'FINE', 'Test DebitTypes::Fine' ); |
| 71 |
ok( Koha::Accounts::DebitTypes::Lost eq 'LOST', 'Test DebitTypes::Lost' ); |
| 72 |
ok( |
| 73 |
Koha::Accounts::DebitTypes::IsValid('FINE'), |
| 74 |
'Test DebitTypes::IsValid with valid debit type' |
| 75 |
); |
| 76 |
ok( |
| 77 |
!Koha::Accounts::DebitTypes::IsValid('Not A Valid Fee Type'), |
| 78 |
'Test DebitTypes::IsValid with an invalid debit type' |
| 79 |
); |
| 80 |
my $authorised_value = |
| 81 |
Koha::Database->new()->schema->resultset('AuthorisedValue')->create( |
| 82 |
{ |
| 83 |
category => 'MANUAL_INV', |
| 84 |
authorised_value => 'TEST', |
| 85 |
lib => 'Test', |
| 86 |
} |
| 87 |
); |
| 88 |
ok( Koha::Accounts::DebitTypes::IsValid('TEST'), |
| 89 |
'Test DebitTypes::IsValid with valid authorised value debit type' ); |
| 90 |
$authorised_value->delete(); |
| 91 |
|
| 92 |
my $debit = AddDebit( |
| 93 |
{ |
| 94 |
borrower => $borrower, |
| 95 |
amount => 5.00, |
| 96 |
type => Koha::Accounts::DebitTypes::Fine, |
| 97 |
branchcode => 'MPL', |
| 98 |
} |
| 99 |
); |
| 100 |
ok( $debit, "AddDebit returned a valid debit id " . $debit->id() ); |
| 173 |
|
101 |
|
|
|
102 |
ok( |
| 103 |
$borrower->account_balance() == 5.00, |
| 104 |
"Borrower's account balance updated correctly. Should be 5.00, is " . $borrower->account_balance() |
| 105 |
); |
| 174 |
|
106 |
|
| 175 |
$dbh->rollback; |
107 |
my $debit2 = AddDebit( |
|
|
108 |
{ |
| 109 |
borrower => $borrower, |
| 110 |
amount => 7.00, |
| 111 |
type => Koha::Accounts::DebitTypes::Fine, |
| 112 |
branchcode => 'MPL', |
| 113 |
} |
| 114 |
); |
| 115 |
|
| 116 |
my $credit = AddCredit( |
| 117 |
{ |
| 118 |
borrower => $borrower, |
| 119 |
type => Koha::Accounts::CreditTypes::Payment, |
| 120 |
amount => 9.00, |
| 121 |
branchcode => 'MPL', |
| 122 |
} |
| 123 |
); |
| 124 |
|
| 125 |
RecalculateAccountBalance( { borrower => $borrower } ); |
| 126 |
ok( |
| 127 |
sprintf( "%.2f", $borrower->account_balance() ) eq "3.00", |
| 128 |
"RecalculateAccountBalance updated balance correctly." |
| 129 |
); |
| 130 |
|
| 131 |
Koha::Database->new()->schema->resultset('AccountCredit')->create( |
| 132 |
{ |
| 133 |
borrowernumber => $borrower->borrowernumber(), |
| 134 |
type => Koha::Accounts::CreditTypes::Payment, |
| 135 |
amount_paid => 3.00, |
| 136 |
amount_remaining => 3.00, |
| 137 |
} |
| 138 |
); |
| 139 |
NormalizeBalances( { borrower => $borrower } ); |
| 140 |
ok( |
| 141 |
$borrower->account_balance() == 0.00, |
| 142 |
"NormalizeBalances updated balance correctly." |
| 143 |
); |
| 144 |
|
| 145 |
# Adding advance credit with no balance due |
| 146 |
$credit = AddCredit( |
| 147 |
{ |
| 148 |
borrower => $borrower, |
| 149 |
type => Koha::Accounts::CreditTypes::Payment, |
| 150 |
amount => 9.00, |
| 151 |
branchcode => 'MPL', |
| 152 |
} |
| 153 |
); |
| 154 |
ok( |
| 155 |
$borrower->account_balance() == -9, |
| 156 |
'Adding a $9 credit for borrower with 0 balance results in a -9 dollar account balance' |
| 157 |
); |
| 176 |
|
158 |
|
|
|
159 |
my $debit3 = AddDebit( |
| 160 |
{ |
| 161 |
borrower => $borrower, |
| 162 |
amount => 5.00, |
| 163 |
type => Koha::Accounts::DebitTypes::Fine, |
| 164 |
branchcode => 'MPL', |
| 165 |
} |
| 166 |
); |
| 167 |
ok( |
| 168 |
$borrower->account_balance() == -4, |
| 169 |
'Adding a $5 debit when the balance is negative results in the debit being automatically paid, resulting in a balance of -4' |
| 170 |
); |
| 177 |
|
171 |
|
| 178 |
# Sub ------------------------------------------- |
172 |
my $debit4 = AddDebit( |
|
|
173 |
{ |
| 174 |
borrower => $borrower, |
| 175 |
amount => 6.00, |
| 176 |
type => Koha::Accounts::DebitTypes::Fine, |
| 177 |
branchcode => 'MPL', |
| 178 |
} |
| 179 |
); |
| 180 |
ok( |
| 181 |
$borrower->account_balance() == 2, |
| 182 |
'Adding another debit ( 6.00 ) more than the negative account balance results in a partial credit and a balance due of 2.00' |
| 183 |
); |
| 184 |
$credit = AddCredit( |
| 185 |
{ |
| 186 |
borrower => $borrower, |
| 187 |
type => Koha::Accounts::CreditTypes::WriteOff, |
| 188 |
amount => 2.00, |
| 189 |
branchcode => 'MPL', |
| 190 |
debit_id => $debit4->debit_id(), |
| 191 |
} |
| 192 |
); |
| 193 |
ok( $borrower->account_balance() == 0, |
| 194 |
'WriteOff of remaining 2.00 balance succeeds' ); |
| 195 |
|
| 196 |
my $debit5 = DebitLostItem( |
| 197 |
{ |
| 198 |
borrower => $borrower, |
| 199 |
issue => $issue, |
| 200 |
} |
| 201 |
); |
| 202 |
ok( $borrower->account_balance() == 25, |
| 203 |
'DebitLostItem adds debit for replacement price of item' ); |
| 204 |
|
| 205 |
my $lost_credit = |
| 206 |
CreditLostItem( { borrower => $borrower, debit => $debit5 } ); |
| 207 |
ok( |
| 208 |
$borrower->account_balance() == 0, |
| 209 |
'CreditLostItem adds credit for same about as the debit for the lost tiem' |
| 210 |
); |
| 179 |
|
211 |
|
| 180 |
# C4::Context->userenv |
212 |
## Post test cleanup ## |
| 181 |
sub Mock_userenv { |
213 |
$issue->delete(); |
| 182 |
return $userenv; |
214 |
$item->delete(); |
| 183 |
} |
215 |
$biblio->delete(); |
|
|
216 |
$borrower->delete(); |