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;
52 my $dbh = C4::Context->dbh;
54 my $builder = t::lib::TestBuilder->new();
56 my $library = $builder->build({
60 $dbh->do(q|DELETE FROM accountlines|);
61 $dbh->do(q|DELETE FROM issues|);
62 $dbh->do(q|DELETE FROM borrowers|);
64 my $branchcode = $library->{branchcode};
67 my $context = new Test::MockModule('C4::Context');
68 $context->mock( 'userenv', sub {
72 branch => $branchcode,
77 subtest "recordpayment() tests" => sub {
82 my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
83 my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode };
85 my $borrower = Koha::Borrower->new( {
86 cardnumber => '1234567890',
90 $borrower->categorycode( $categorycode );
91 $borrower->branchcode( $branchcode );
94 my $sth = $dbh->prepare(
95 "INSERT INTO accountlines (
100 $sth->execute($borrower->borrowernumber, '100');
101 $sth->execute($borrower->borrowernumber, '200');
103 $sth = $dbh->prepare("SELECT count(*) FROM accountlines");
105 my $count = $sth->fetchrow_array;
106 is ($count, 2, 'There is 2 lines as expected');
108 # Testing recordpayment -------------------------
109 # There is $100 in the account
110 $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
111 my $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
113 for my $line ( @$amountoutstanding ) {
114 $amountleft += $line;
116 ok($amountleft == 300, 'The account has 300$ as expected' );
118 # We make a $20 payment
119 my $borrowernumber = $borrower->borrowernumber;
122 my $payment_note = '$20.00 payment note';
123 recordpayment($borrowernumber, $data, $sys_paytype, $payment_note);
124 # There is now $280 in the account
125 $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
126 $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
128 for my $line ( @$amountoutstanding ) {
129 $amountleft += $line;
131 ok($amountleft == 280, 'The account has $280 as expected' );
132 # Is the payment note well registered
133 $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
134 $sth->execute($borrower->borrowernumber);
135 my $note = $sth->fetchrow_array;
136 is($note,'$20.00 payment note', '$20.00 payment note is registered');
138 # We make a -$30 payment (a NEGATIVE payment)
140 $payment_note = '-$30.00 payment note';
141 recordpayment($borrowernumber, $data, $sys_paytype, $payment_note);
142 # There is now $310 in the account
143 $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
144 $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
146 for my $line ( @$amountoutstanding ) {
147 $amountleft += $line;
149 ok($amountleft == 310, 'The account has $310 as expected' );
150 # Is the payment note well registered
151 $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
152 $sth->execute($borrower->borrowernumber);
153 $note = $sth->fetchrow_array;
154 is($note,'-$30.00 payment note', '-$30.00 payment note is registered');
156 #We make a $150 payment ( > 1stLine )
158 $payment_note = '$150.00 payment note';
159 recordpayment($borrowernumber, $data, $sys_paytype, $payment_note);
160 # There is now $160 in the account
161 $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
162 $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
164 for my $line ( @$amountoutstanding ) {
165 $amountleft += $line;
167 ok($amountleft == 160, 'The account has $160 as expected' );
168 # Is the payment note well registered
169 $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
170 $sth->execute($borrower->borrowernumber);
171 $note = $sth->fetchrow_array;
172 is($note,'$150.00 payment note', '$150.00 payment note is registered');
174 #We make a $200 payment ( > amountleft )
176 $payment_note = '$200.00 payment note';
177 recordpayment($borrowernumber, $data, $sys_paytype, $payment_note);
178 # There is now -$40 in the account
179 $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
180 $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
182 for my $line ( @$amountoutstanding ) {
183 $amountleft += $line;
185 ok($amountleft == -40, 'The account has -$40 as expected, (credit situation)' );
186 # Is the payment note well registered
187 $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
188 $sth->execute($borrower->borrowernumber);
189 $note = $sth->fetchrow_array;
190 is($note,'$200.00 payment note', '$200.00 payment note is registered');
193 subtest "makepayment() tests" => sub {
198 my $category = $builder->build({ source => 'Category' })->{ categorycode };
199 my $branch = $builder->build({ source => 'Branch' })->{ branchcode };
200 $branchcode = $branch;
201 my $borrowernumber = $builder->build({
202 source => 'Borrower',
203 value => { categorycode => $category,
204 branchcode => $branch }
205 })->{ borrowernumber };
208 my $accountline = $builder->build({ source => 'Accountline',
209 value => { borrowernumber => $borrowernumber,
211 amountoutstanding => $amount }
214 my $rs = $schema->resultset('Accountline')->search({
215 borrowernumber => $borrowernumber
218 is( $rs->count(), 1, 'Accountline created' );
220 # make the full payment
222 $accountline->{ accountlines_id }, $borrowernumber,
223 $accountline->{ accountno }, $amount,
224 $borrowernumber, $branch, 'A payment note' );
226 # TODO: someone should write actual tests for makepayment()
228 my $stat = $schema->resultset('Statistic')->search({
231 }, { order_by => { -desc => 'datetime' } })->next();
233 ok( defined $stat, "There's a payment log that matches the branch" );
236 skip "No statistic logged", 4 unless defined $stat;
238 is( $stat->type, 'payment', "Correct statistic type" );
239 is( $stat->branch, $branch, "Correct branch logged to statistics" );
240 is( $stat->borrowernumber, $borrowernumber, "Correct borrowernumber logged to statistics" );
241 is( $stat->value, "$amount" . "\.0000", "Correct amount logged to statistics" );
245 subtest "makepartialpayment() tests" => sub {
250 my $category = $builder->build({ source => 'Category' })->{ categorycode };
251 my $branch = $builder->build({ source => 'Branch' })->{ branchcode };
252 $branchcode = $branch;
253 my $borrowernumber = $builder->build({
254 source => 'Borrower',
255 value => { categorycode => $category,
256 branchcode => $branch }
257 })->{ borrowernumber };
260 my $partialamount = 60;
261 my $accountline = $builder->build({ source => 'Accountline',
262 value => { borrowernumber => $borrowernumber,
264 amountoutstanding => $amount }
267 my $rs = $schema->resultset('Accountline')->search({
268 borrowernumber => $borrowernumber
271 is( $rs->count(), 1, 'Accountline created' );
273 # make the full payment
275 $accountline->{ accountlines_id }, $borrowernumber,
276 $accountline->{ accountno }, $partialamount,
277 $borrowernumber, $branch, 'A payment note' );
279 # TODO: someone should write actual tests for makepartialpayment()
281 my $stat = $schema->resultset('Statistic')->search({
284 }, { order_by => { -desc => 'datetime' } })->next();
286 ok( defined $stat, "There's a payment log that matches the branch" );
289 skip "No statistic logged", 4 unless defined $stat;
291 is( $stat->type, 'payment', "Correct statistic type" );
292 is( $stat->branch, $branch, "Correct branch logged to statistics" );
293 is( $stat->borrowernumber, $borrowernumber, "Correct borrowernumber logged to statistics" );
294 is( $stat->value, "$partialamount" . "\.0000", "Correct amount logged to statistics" );