View | Details | Raw Unified | Return to bug 6427
Collapse All | Expand All

(-)a/t/db_dependent/Accounts.t (-149 / +182 lines)
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();
(-)a/t/db_dependent/Circulation.t (-10 / +26 lines)
Lines 170-176 $dbh->do( Link Here
170
);
170
);
171
171
172
# Test C4::Circulation::ProcessOfflinePayment
172
# Test C4::Circulation::ProcessOfflinePayment
173
my $sth = C4::Context->dbh->prepare("SELECT COUNT(*) FROM accountlines WHERE amount = '-123.45' AND accounttype = 'Pay'");
173
my $sth = C4::Context->dbh->prepare("SELECT COUNT(*) FROM account_credits WHERE amount_paid = '123.45' AND type = 'PAYMENT'");
174
$sth->execute();
174
$sth->execute();
175
my ( $original_count ) = $sth->fetchrow_array();
175
my ( $original_count ) = $sth->fetchrow_array();
176
176
Lines 183-191 my ( $new_count ) = $sth->fetchrow_array(); Link Here
183
183
184
ok( $new_count == $original_count  + 1, 'ProcessOfflinePayment makes payment correctly' );
184
ok( $new_count == $original_count  + 1, 'ProcessOfflinePayment makes payment correctly' );
185
185
186
C4::Context->dbh->do("DELETE FROM accountlines WHERE borrowernumber IN ( SELECT borrowernumber FROM borrowers WHERE cardnumber = '99999999999' )");
186
C4::Context->dbh->do("DELETE FROM account_credits");
187
C4::Context->dbh->do("DELETE FROM account_debits");
187
C4::Context->dbh->do("DELETE FROM borrowers WHERE cardnumber = '99999999999'");
188
C4::Context->dbh->do("DELETE FROM borrowers WHERE cardnumber = '99999999999'");
188
C4::Context->dbh->do("DELETE FROM accountlines");
189
{
189
{
190
# CanBookBeRenewed tests
190
# CanBookBeRenewed tests
191
191
Lines 429-436 C4::Context->dbh->do("DELETE FROM accountlines"); Link Here
429
    C4::Context->set_preference('WhenLostForgiveFine','1');
429
    C4::Context->set_preference('WhenLostForgiveFine','1');
430
    C4::Context->set_preference('WhenLostChargeReplacementFee','1');
430
    C4::Context->set_preference('WhenLostChargeReplacementFee','1');
431
431
432
    C4::Overdues::UpdateFine( $itemnumber, $renewing_borrower->{borrowernumber},
432
    C4::Overdues::UpdateFine(
433
        15.00, q{}, Koha::DateUtils::output_pref($datedue) );
433
        {
434
            itemnumber     => $itemnumber,
435
            borrowernumber => $renewing_borrower->{borrowernumber},
436
            amount         => 15.00,
437
            due            => Koha::DateUtils::output_pref($datedue),
438
            issue_id       => GetItemIssue($itemnumber)->{issue_id}
439
        }
440
    );
434
441
435
    LostItem( $itemnumber, 1 );
442
    LostItem( $itemnumber, 1 );
436
443
Lines 438-456 C4::Context->dbh->do("DELETE FROM accountlines"); Link Here
438
    ok( !$item->onloan(), "Lost item marked as returned has false onloan value" );
445
    ok( !$item->onloan(), "Lost item marked as returned has false onloan value" );
439
446
440
    my $total_due = $dbh->selectrow_array(
447
    my $total_due = $dbh->selectrow_array(
441
        'SELECT SUM( amountoutstanding ) FROM accountlines WHERE borrowernumber = ?',
448
        'SELECT account_balance FROM borrowers WHERE borrowernumber = ?',
442
        undef, $renewing_borrower->{borrowernumber}
449
        undef, $renewing_borrower->{borrowernumber}
443
    );
450
    );
444
451
445
    ok( $total_due == 12, 'Borrower only charged replacement fee with both WhenLostForgiveFine and WhenLostChargeReplacementFee enabled' );
452
    ok( $total_due == 12, 'Borrower only charged replacement fee with both WhenLostForgiveFine and WhenLostChargeReplacementFee enabled' );
446
453
447
    C4::Context->dbh->do("DELETE FROM accountlines");
454
    C4::Context->dbh->do("DELETE FROM account_credits");
455
    C4::Context->dbh->do("DELETE FROM account_debits");
456
    C4::Context->dbh->do("UPDATE borrowers SET account_balance = 0");
448
457
449
    C4::Context->set_preference('WhenLostForgiveFine','0');
458
    C4::Context->set_preference('WhenLostForgiveFine','0');
450
    C4::Context->set_preference('WhenLostChargeReplacementFee','0');
459
    C4::Context->set_preference('WhenLostChargeReplacementFee','0');
451
460
452
    C4::Overdues::UpdateFine( $itemnumber2, $renewing_borrower->{borrowernumber},
461
    C4::Overdues::UpdateFine(
453
        15.00, q{}, Koha::DateUtils::output_pref($datedue) );
462
        {
463
            itemnumber     => $itemnumber2,
464
            borrowernumber => $renewing_borrower->{borrowernumber},
465
            amount         => 15.00,
466
            due            => Koha::DateUtils::output_pref($datedue),
467
            issue_id       => GetItemIssue($itemnumber2)->{issue_id},
468
        }
469
    );
454
470
455
    LostItem( $itemnumber2, 0 );
471
    LostItem( $itemnumber2, 0 );
456
472
Lines 458-464 C4::Context->dbh->do("DELETE FROM accountlines"); Link Here
458
    ok( $item2->onloan(), "Lost item *not* marked as returned has true onloan value" );
474
    ok( $item2->onloan(), "Lost item *not* marked as returned has true onloan value" );
459
475
460
    $total_due = $dbh->selectrow_array(
476
    $total_due = $dbh->selectrow_array(
461
        'SELECT SUM( amountoutstanding ) FROM accountlines WHERE borrowernumber = ?',
477
        'SELECT account_balance FROM borrowers WHERE borrowernumber = ?',
462
        undef, $renewing_borrower->{borrowernumber}
478
        undef, $renewing_borrower->{borrowernumber}
463
    );
479
    );
464
480
(-)a/t/db_dependent/Members/AddEnrolmentFeeIfNeeded.t (-4 / +3 lines)
Lines 40-56 my %borrower_data = ( Link Here
40
my $borrowernumber = C4::Members::AddMember( %borrower_data );
40
my $borrowernumber = C4::Members::AddMember( %borrower_data );
41
$borrower_data{borrowernumber} = $borrowernumber;
41
$borrower_data{borrowernumber} = $borrowernumber;
42
42
43
my ( $total ) = C4::Members::GetMemberAccountRecords( $borrowernumber );
43
my ( $total ) = C4::Members::GetMemberAccountBalance( $borrowernumber );
44
is( $total, $enrolmentfee_K, "New kid pay $enrolmentfee_K" );
44
is( $total, $enrolmentfee_K, "New kid pay $enrolmentfee_K" );
45
45
46
$borrower_data{categorycode} = 'J';
46
$borrower_data{categorycode} = 'J';
47
C4::Members::ModMember( %borrower_data );
47
C4::Members::ModMember( %borrower_data );
48
( $total ) = C4::Members::GetMemberAccountRecords( $borrowernumber );
48
( $total ) = C4::Members::GetMemberAccountBalance( $borrowernumber );
49
is( $total, $enrolmentfee_K + $enrolmentfee_J, "Kid growing and become a juvenile, he should pay " . ( $enrolmentfee_K + $enrolmentfee_J ) );
49
is( $total, $enrolmentfee_K + $enrolmentfee_J, "Kid growing and become a juvenile, he should pay " . ( $enrolmentfee_K + $enrolmentfee_J ) );
50
50
51
# Check with calling directly AddEnrolmentFeeIfNeeded
51
# Check with calling directly AddEnrolmentFeeIfNeeded
52
C4::Members::AddEnrolmentFeeIfNeeded( 'YA', $borrowernumber );
52
C4::Members::AddEnrolmentFeeIfNeeded( 'YA', $borrowernumber );
53
( $total ) = C4::Members::GetMemberAccountRecords( $borrowernumber );
53
( $total ) = C4::Members::GetMemberAccountBalance( $borrowernumber );
54
is( $total, $enrolmentfee_K + $enrolmentfee_J + $enrolmentfee_YA, "Juvenile growing and become an young adult, he should pay " . ( $enrolmentfee_K + $enrolmentfee_J + $enrolmentfee_YA ) );
54
is( $total, $enrolmentfee_K + $enrolmentfee_J + $enrolmentfee_YA, "Juvenile growing and become an young adult, he should pay " . ( $enrolmentfee_K + $enrolmentfee_J + $enrolmentfee_YA ) );
55
55
56
$dbh->rollback;
56
$dbh->rollback;
57
- 

Return to bug 6427