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 => 8;
25 use t::lib::TestBuilder;
28 use_ok('C4::Accounts');
29 use_ok('Koha::Object');
30 use_ok('Koha::Borrower');
31 use_ok('Data::Dumper');
34 can_ok( 'C4::Accounts',
45 recordpayment_selectaccts
50 my $schema = Koha::Database->new->schema;
51 $schema->storage->txn_begin;
53 my $builder = t::lib::TestBuilder->new();
55 my $dbh = C4::Context->dbh;
58 $dbh->do(q|DELETE FROM accountlines|);
59 $dbh->do(q|DELETE FROM issues|);
60 $dbh->do(q|DELETE FROM borrowers|);
62 my $branchcode = 'CPL';
65 my $context = new Test::MockModule('C4::Context');
66 $context->mock( 'userenv', sub {
70 branch => $branchcode,
75 subtest "recordpayment() tests" => sub {
80 my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
81 my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode };
83 my $borrower = Koha::Borrower->new( {
84 cardnumber => '1234567890',
88 $borrower->categorycode( $categorycode );
89 $borrower->branchcode( $branchcode );
92 my $sth = $dbh->prepare(
93 "INSERT INTO accountlines (
98 $sth->execute($borrower->borrowernumber, '100');
99 $sth->execute($borrower->borrowernumber, '200');
101 $sth = $dbh->prepare("SELECT count(*) FROM accountlines");
103 my $count = $sth->fetchrow_array;
104 is ($count, 2, 'There is 2 lines as expected');
106 # Testing recordpayment -------------------------
107 # There is $100 in the account
108 $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
109 my $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
111 for my $line ( @$amountoutstanding ) {
112 $amountleft += $line;
114 ok($amountleft == 300, 'The account has 300$ as expected' );
116 # We make a $20 payment
117 my $borrowernumber = $borrower->borrowernumber;
120 my $payment_note = '$20.00 payment note';
121 recordpayment($borrowernumber, $data, $sys_paytype, $payment_note);
122 # There is now $280 in the account
123 $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
124 $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
126 for my $line ( @$amountoutstanding ) {
127 $amountleft += $line;
129 ok($amountleft == 280, 'The account has $280 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 my $note = $sth->fetchrow_array;
134 is($note,'$20.00 payment note', '$20.00 payment note is registered');
136 # We make a -$30 payment (a NEGATIVE payment)
138 $payment_note = '-$30.00 payment note';
139 recordpayment($borrowernumber, $data, $sys_paytype, $payment_note);
140 # There is now $310 in the account
141 $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
142 $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
144 for my $line ( @$amountoutstanding ) {
145 $amountleft += $line;
147 ok($amountleft == 310, 'The account has $310 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,'-$30.00 payment note', '-$30.00 payment note is registered');
154 #We make a $150 payment ( > 1stLine )
156 $payment_note = '$150.00 payment note';
157 recordpayment($borrowernumber, $data, $sys_paytype, $payment_note);
158 # There is now $160 in the account
159 $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
160 $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
162 for my $line ( @$amountoutstanding ) {
163 $amountleft += $line;
165 ok($amountleft == 160, 'The account has $160 as expected' );
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,'$150.00 payment note', '$150.00 payment note is registered');
172 #We make a $200 payment ( > amountleft )
174 $payment_note = '$200.00 payment note';
175 recordpayment($borrowernumber, $data, $sys_paytype, $payment_note);
176 # There is now -$40 in the account
177 $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
178 $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
180 for my $line ( @$amountoutstanding ) {
181 $amountleft += $line;
183 ok($amountleft == -40, 'The account has -$40 as expected, (credit situation)' );
184 # Is the payment note well registered
185 $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
186 $sth->execute($borrower->borrowernumber);
187 $note = $sth->fetchrow_array;
188 is($note,'$200.00 payment note', '$200.00 payment note is registered');
191 subtest "makepayment() tests" => sub {
196 my $category = $builder->build({ source => 'Category' })->{ categorycode };
197 my $branch = $builder->build({ source => 'Branch' })->{ branchcode };
198 $branchcode = $branch;
199 my $borrowernumber = $builder->build({
200 source => 'Borrower',
201 value => { categorycode => $category,
202 branchcode => $branch }
203 })->{ borrowernumber };
206 my $accountline = $builder->build({ source => 'Accountline',
207 value => { borrowernumber => $borrowernumber,
209 amountoutstanding => $amount }
212 my $rs = $schema->resultset('Accountline')->search({
213 borrowernumber => $borrowernumber
216 is( $rs->count(), 1, 'Accountline created' );
218 # make the full payment
220 $accountline->{ accountlines_id }, $borrowernumber,
221 $accountline->{ accountno }, $amount,
222 $borrowernumber, $branch, 'A payment note' );
224 # TODO: someone should write actual tests for makepayment()
226 my $stat = $schema->resultset('Statistic')->search({
229 }, { order_by => { -desc => 'datetime' } })->next();
231 ok( defined $stat, "There's a payment log that matches the branch" );
234 skip "No statistic logged", 4 unless defined $stat;
236 is( $stat->type, 'payment', "Correct statistic type" );
237 is( $stat->branch, $branch, "Correct branch logged to statistics" );
238 is( $stat->borrowernumber, $borrowernumber, "Correct borrowernumber logged to statistics" );
239 is( $stat->value, "$amount" . "\.0000", "Correct amount logged to statistics" );
243 subtest "makepartialpayment() tests" => sub {
248 my $category = $builder->build({ source => 'Category' })->{ categorycode };
249 my $branch = $builder->build({ source => 'Branch' })->{ branchcode };
250 $branchcode = $branch;
251 my $borrowernumber = $builder->build({
252 source => 'Borrower',
253 value => { categorycode => $category,
254 branchcode => $branch }
255 })->{ borrowernumber };
258 my $partialamount = 60;
259 my $accountline = $builder->build({ source => 'Accountline',
260 value => { borrowernumber => $borrowernumber,
262 amountoutstanding => $amount }
265 my $rs = $schema->resultset('Accountline')->search({
266 borrowernumber => $borrowernumber
269 is( $rs->count(), 1, 'Accountline created' );
271 # make the full payment
273 $accountline->{ accountlines_id }, $borrowernumber,
274 $accountline->{ accountno }, $partialamount,
275 $borrowernumber, $branch, 'A payment note' );
277 # TODO: someone should write actual tests for makepartialpayment()
279 my $stat = $schema->resultset('Statistic')->search({
282 }, { order_by => { -desc => 'datetime' } })->next();
284 ok( defined $stat, "There's a payment log that matches the branch" );
287 skip "No statistic logged", 4 unless defined $stat;
289 is( $stat->type, 'payment', "Correct statistic type" );
290 is( $stat->branch, $branch, "Correct branch logged to statistics" );
291 is( $stat->borrowernumber, $borrowernumber, "Correct borrowernumber logged to statistics" );
292 is( $stat->value, "$partialamount" . "\.0000", "Correct amount logged to statistics" );