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

(-)a/t/db_dependent/Accounts.t (-141 / +253 lines)
Lines 16-27 Link Here
16
# You should have received a copy of the GNU General Public License along
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>.
17
# with Koha; if not, see <http://www.gnu.org/licenses>.
18
18
19
use strict;
19
use Modern::Perl;
20
use warnings;
21
20
22
use Test::More tests => 15;
21
use Test::More tests => 8;
22
use Test::MockModule;
23
use Test::Warn;
23
use Test::Warn;
24
24
25
use t::lib::TestBuilder;
26
25
BEGIN {
27
BEGIN {
26
    use_ok('C4::Accounts');
28
    use_ok('C4::Accounts');
27
    use_ok('Koha::Object');
29
    use_ok('Koha::Object');
Lines 29-51 BEGIN { Link Here
29
    use_ok('Data::Dumper');
31
    use_ok('Data::Dumper');
30
}
32
}
31
33
32
can_ok(	'C4::Accounts',
34
can_ok( 'C4::Accounts',
33
	qw(	recordpayment
35
    qw( recordpayment
34
		makepayment
36
        makepayment
35
		getnextacctno
37
        getnextacctno
36
38
        chargelostitem
37
		chargelostitem
39
        manualinvoice
38
		manualinvoice
40
        getcharges
39
		getcharges
41
        ModNote
40
		ModNote
42
        getcredits
41
		getcredits
43
        getrefunds
42
		getrefunds
44
        ReversePayment
43
		ReversePayment
45
        recordpayment_selectaccts
44
		recordpayment_selectaccts
46
        makepartialpayment
45
		makepartialpayment
47
        WriteOffFee )
46
		WriteOffFee	)
47
);
48
);
48
49
50
my $builder = t::lib::TestBuilder->new();
51
my $schema  = Koha::Database->new()->schema();
52
49
my $dbh = C4::Context->dbh;
53
my $dbh = C4::Context->dbh;
50
$dbh->{RaiseError}=1;
54
$dbh->{RaiseError}=1;
51
$dbh->{AutoCommit}=0;
55
$dbh->{AutoCommit}=0;
Lines 53-183 $dbh->do(q|DELETE FROM accountlines|); Link Here
53
$dbh->do(q|DELETE FROM borrowers|);
57
$dbh->do(q|DELETE FROM borrowers|);
54
$dbh->do(q|DELETE FROM issues|);
58
$dbh->do(q|DELETE FROM issues|);
55
59
56
# Mock userenv
60
my $branchcode = 'CPL';
57
local $SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /redefined/ };
61
my $borrower_number;
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
62
63
my $context = new Test::MockModule('C4::Context');
64
$context->mock( 'userenv', sub {
65
    return {
66
        flags  => 1,
67
        id     => 'my_userid',
68
        branch => $branchcode,
69
    };
70
});
173
71
174
72
175
$dbh->rollback;
73
subtest "recordpayment() tests" => sub {
74
75
    plan tests => 10;
76
77
    # Create a borrower
78
    my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
79
    my $branchcode   = $builder->build({ source => 'Branch' })->{ branchcode };
80
81
    my $borrower = Koha::Borrower->new( {
82
        cardnumber => '1234567890',
83
        surname => 'McFly',
84
        firstname => 'Marty',
85
    } );
86
    $borrower->categorycode( $categorycode );
87
    $borrower->branchcode( $branchcode );
88
    $borrower->store;
89
90
    my $sth = $dbh->prepare(
91
        "INSERT INTO accountlines (
92
            borrowernumber,
93
            amountoutstanding )
94
        VALUES ( ?, ? )"
95
    );
96
    $sth->execute($borrower->borrowernumber, '100');
97
    $sth->execute($borrower->borrowernumber, '200');
98
99
    $sth = $dbh->prepare("SELECT count(*) FROM accountlines");
100
    $sth->execute;
101
    my $count = $sth->fetchrow_array;
102
    is ($count, 2, 'There is 2 lines as expected');
103
104
    # Testing recordpayment -------------------------
105
    # There is $100 in the account
106
    $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
107
    my $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
108
    my $amountleft = 0;
109
    for my $line ( @$amountoutstanding ) {
110
        $amountleft += $line;
111
    }
112
    ok($amountleft == 300, 'The account has 300$ as expected' );
113
114
    # We make a $20 payment
115
    my $borrowernumber = $borrower->borrowernumber;
116
    my $data = '20.00';
117
    my $sys_paytype;
118
    my $payment_note = '$20.00 payment note';
119
    recordpayment($borrowernumber, $data, $sys_paytype, $payment_note);
120
    # There is now $280 in the account
121
    $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
122
    $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
123
    $amountleft = 0;
124
    for my $line ( @$amountoutstanding ) {
125
        $amountleft += $line;
126
    }
127
    ok($amountleft == 280, 'The account has $280 as expected' );
128
    # Is the payment note well registered
129
    $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
130
    $sth->execute($borrower->borrowernumber);
131
    my $note = $sth->fetchrow_array;
132
    is($note,'$20.00 payment note', '$20.00 payment note is registered');
133
134
    # We make a -$30 payment (a NEGATIVE payment)
135
    $data = '-30.00';
136
    $payment_note = '-$30.00 payment note';
137
    recordpayment($borrowernumber, $data, $sys_paytype, $payment_note);
138
    # There is now $310 in the account
139
    $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
140
    $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
141
    $amountleft = 0;
142
    for my $line ( @$amountoutstanding ) {
143
        $amountleft += $line;
144
    }
145
    ok($amountleft == 310, 'The account has $310 as expected' );
146
    # Is the payment note well registered
147
    $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
148
    $sth->execute($borrower->borrowernumber);
149
    $note = $sth->fetchrow_array;
150
    is($note,'-$30.00 payment note', '-$30.00 payment note is registered');
151
152
    #We make a $150 payment ( > 1stLine )
153
    $data = '150.00';
154
    $payment_note = '$150.00 payment note';
155
    recordpayment($borrowernumber, $data, $sys_paytype, $payment_note);
156
    # There is now $160 in the account
157
    $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
158
    $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
159
    $amountleft = 0;
160
    for my $line ( @$amountoutstanding ) {
161
        $amountleft += $line;
162
    }
163
    ok($amountleft == 160, 'The account has $160 as expected' );
164
    # Is the payment note well registered
165
    $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
166
    $sth->execute($borrower->borrowernumber);
167
    $note = $sth->fetchrow_array;
168
    is($note,'$150.00 payment note', '$150.00 payment note is registered');
169
170
    #We make a $200 payment ( > amountleft )
171
    $data = '200.00';
172
    $payment_note = '$200.00 payment note';
173
    recordpayment($borrowernumber, $data, $sys_paytype, $payment_note);
174
    # There is now -$40 in the account
175
    $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
176
    $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
177
    $amountleft = 0;
178
    for my $line ( @$amountoutstanding ) {
179
        $amountleft += $line;
180
    }
181
    ok($amountleft == -40, 'The account has -$40 as expected, (credit situation)' );
182
    # Is the payment note well registered
183
    $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
184
    $sth->execute($borrower->borrowernumber);
185
    $note = $sth->fetchrow_array;
186
    is($note,'$200.00 payment note', '$200.00 payment note is registered');
187
};
188
189
subtest "makepayment() tests" => sub {
190
191
    plan tests => 6;
192
193
    # Create a borrower
194
    my $category   = $builder->build({ source => 'Category' })->{ categorycode };
195
    my $branch     = $builder->build({ source => 'Branch' })->{ branchcode };
196
    $branchcode = $branch;
197
    my $borrowernumber = $builder->build({
198
        source => 'Borrower',
199
        value  => { categorycode => $category,
200
                    branchcode   => $branch }
201
    })->{ borrowernumber };
176
202
203
    my $amount = 100;
204
    my $accountline = $builder->build({ source => 'Accountline',
205
        value  => { borrowernumber => $borrowernumber,
206
                    amount => $amount,
207
                    amountoutstanding => $amount }
208
    });
177
209
178
# Sub -------------------------------------------
210
    my $rs = $schema->resultset('Accountline')->search({
211
        borrowernumber => $borrowernumber
212
    });
213
214
    is( $rs->count(), 1, 'Accountline created' );
215
216
    # make the full payment
217
    makepayment(
218
        $accountline->{ accountlines_id }, $borrowernumber,
219
        $accountline->{ accountno },       $amount,
220
        $borrowernumber, $branch, 'A payment note' );
221
222
    # TODO: someone should write actual tests for makepayment()
223
224
    my $stat = $schema->resultset('Statistic')->search({
225
        branch  => $branch,
226
        type    => 'payment'
227
    }, { order_by => { -desc => 'datetime' } })->next();
228
229
    ok( defined $stat, "There's a payment log that matches the branch" );
230
231
    SKIP: {
232
        skip "No statistic logged", 4 unless defined $stat;
233
234
        is( $stat->type, 'payment', "Correct statistic type" );
235
        is( $stat->branch, $branch, "Correct branch logged to statistics" );
236
        is( $stat->borrowernumber, $borrowernumber, "Correct borrowernumber logged to statistics" );
237
        is( $stat->value, "$amount" . "\.0000", "Correct amount logged to statistics" );
238
    }
239
};
240
241
subtest "makepartialpayment() tests" => sub {
242
243
    plan tests => 6;
244
245
    # Create a borrower
246
    my $category   = $builder->build({ source => 'Category' })->{ categorycode };
247
    my $branch     = $builder->build({ source => 'Branch' })->{ branchcode };
248
    $branchcode = $branch;
249
    my $borrowernumber = $builder->build({
250
        source => 'Borrower',
251
        value  => { categorycode => $category,
252
                    branchcode   => $branch }
253
    })->{ borrowernumber };
254
255
    my $amount = 100;
256
    my $partialamount = 60;
257
    my $accountline = $builder->build({ source => 'Accountline',
258
        value  => { borrowernumber => $borrowernumber,
259
                    amount => $amount,
260
                    amountoutstanding => $amount }
261
    });
262
263
    my $rs = $schema->resultset('Accountline')->search({
264
        borrowernumber => $borrowernumber
265
    });
266
267
    is( $rs->count(), 1, 'Accountline created' );
268
269
    # make the full payment
270
    makepartialpayment(
271
        $accountline->{ accountlines_id }, $borrowernumber,
272
        $accountline->{ accountno },       $partialamount,
273
        $borrowernumber, $branch, 'A payment note' );
274
275
    # TODO: someone should write actual tests for makepartialpayment()
276
277
    my $stat = $schema->resultset('Statistic')->search({
278
        branch  => $branch,
279
        type    => 'payment'
280
    }, { order_by => { -desc => 'datetime' } })->next();
281
282
    ok( defined $stat, "There's a payment log that matches the branch" );
283
284
    SKIP: {
285
        skip "No statistic logged", 4 unless defined $stat;
286
287
        is( $stat->type, 'payment', "Correct statistic type" );
288
        is( $stat->branch, $branch, "Correct branch logged to statistics" );
289
        is( $stat->borrowernumber, $borrowernumber, "Correct borrowernumber logged to statistics" );
290
        is( $stat->value, "$partialamount" . "\.0000", "Correct amount logged to statistics" );
291
    }
292
};
293
294
$dbh->rollback;
179
295
180
# C4::Context->userenv
296
1;
181
sub Mock_userenv {
182
    return $userenv;
183
}
184
- 

Return to bug 11229