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 $builder = t::lib::TestBuilder->new();
51 my $schema = Koha::Database->new()->schema();
53 my $dbh = C4::Context->dbh;
56 $dbh->do(q|DELETE FROM accountlines|);
57 $dbh->do(q|DELETE FROM issues|);
58 $dbh->do(q|DELETE FROM borrowers|);
60 my $branchcode = 'CPL';
63 my $context = new Test::MockModule('C4::Context');
64 $context->mock( 'userenv', sub {
68 branch => $branchcode,
73 subtest "recordpayment() tests" => sub {
78 my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
79 my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode };
81 my $borrower = Koha::Borrower->new( {
82 cardnumber => '1234567890',
86 $borrower->categorycode( $categorycode );
87 $borrower->branchcode( $branchcode );
90 my $sth = $dbh->prepare(
91 "INSERT INTO accountlines (
96 $sth->execute($borrower->borrowernumber, '100');
97 $sth->execute($borrower->borrowernumber, '200');
99 $sth = $dbh->prepare("SELECT count(*) FROM accountlines");
101 my $count = $sth->fetchrow_array;
102 is ($count, 2, 'There is 2 lines as expected');
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);
109 for my $line ( @$amountoutstanding ) {
110 $amountleft += $line;
112 ok($amountleft == 300, 'The account has 300$ as expected' );
114 # We make a $20 payment
115 my $borrowernumber = $borrower->borrowernumber;
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);
124 for my $line ( @$amountoutstanding ) {
125 $amountleft += $line;
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');
134 # We make a -$30 payment (a NEGATIVE payment)
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);
142 for my $line ( @$amountoutstanding ) {
143 $amountleft += $line;
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');
152 #We make a $150 payment ( > 1stLine )
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);
160 for my $line ( @$amountoutstanding ) {
161 $amountleft += $line;
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');
170 #We make a $200 payment ( > amountleft )
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);
178 for my $line ( @$amountoutstanding ) {
179 $amountleft += $line;
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');
189 subtest "makepayment() tests" => sub {
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 };
204 my $accountline = $builder->build({ source => 'Accountline',
205 value => { borrowernumber => $borrowernumber,
207 amountoutstanding => $amount }
210 my $rs = $schema->resultset('Accountline')->search({
211 borrowernumber => $borrowernumber
214 is( $rs->count(), 1, 'Accountline created' );
216 # make the full payment
218 $accountline->{ accountlines_id }, $borrowernumber,
219 $accountline->{ accountno }, $amount,
220 $borrowernumber, $branch, 'A payment note' );
222 # TODO: someone should write actual tests for makepayment()
224 my $stat = $schema->resultset('Statistic')->search({
227 }, { order_by => { -desc => 'datetime' } })->next();
229 ok( defined $stat, "There's a payment log that matches the branch" );
232 skip "No statistic logged", 4 unless defined $stat;
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" );
241 subtest "makepartialpayment() tests" => sub {
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 };
256 my $partialamount = 60;
257 my $accountline = $builder->build({ source => 'Accountline',
258 value => { borrowernumber => $borrowernumber,
260 amountoutstanding => $amount }
263 my $rs = $schema->resultset('Accountline')->search({
264 borrowernumber => $borrowernumber
267 is( $rs->count(), 1, 'Accountline created' );
269 # make the full payment
271 $accountline->{ accountlines_id }, $borrowernumber,
272 $accountline->{ accountno }, $partialamount,
273 $borrowernumber, $branch, 'A payment note' );
275 # TODO: someone should write actual tests for makepartialpayment()
277 my $stat = $schema->resultset('Statistic')->search({
280 }, { order_by => { -desc => 'datetime' } })->next();
282 ok( defined $stat, "There's a payment log that matches the branch" );
285 skip "No statistic logged", 4 unless defined $stat;
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" );