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>.
22 use Test::More tests => 15;
26 use_ok('C4::Accounts');
27 use_ok('Koha::Object');
28 use_ok('Koha::Borrower');
29 use_ok('Data::Dumper');
32 can_ok( 'C4::Accounts',
44 recordpayment_selectaccts
49 my $dbh = C4::Context->dbh;
52 $dbh->do(q|DELETE FROM accountlines|);
53 $dbh->do(q|DELETE FROM borrowers|);
54 $dbh->do(q|DELETE FROM issues|);
57 local $SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /redefined/ };
59 *C4::Context::userenv = \&Mock_userenv;
60 $userenv = { flags => 1, id => 'my_userid', branch => 'CPL' };
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();
66 my $borrower = Koha::Borrower->new( {
67 cardnumber => '1234567890',
71 $borrower->categorycode( $categorycode );
72 $borrower->branchcode( $branchcode );
75 my $sth = $dbh->prepare(
76 "INSERT INTO accountlines (
81 $sth->execute($borrower->borrowernumber, '100');
82 $sth->execute($borrower->borrowernumber, '200');
84 $sth = $dbh->prepare("SELECT count(*) FROM accountlines");
86 my $count = $sth->fetchrow_array;
87 is ($count, 2, 'There is 2 lines as expected');
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);
94 for my $line ( @$amountoutstanding ) {
97 ok($amountleft == 300, 'The account has 300$ as expected' );
99 # We make a $20 payment
100 my $borrowernumber = $borrower->borrowernumber;
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);
109 for my $line ( @$amountoutstanding ) {
110 $amountleft += $line;
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');
119 # We make a -$30 payment (a NEGATIVE payment)
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);
127 for my $line ( @$amountoutstanding ) {
128 $amountleft += $line;
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');
137 #We make a $150 payment ( > 1stLine )
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);
145 for my $line ( @$amountoutstanding ) {
146 $amountleft += $line;
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');
155 #We make a $200 payment ( > amountleft )
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);
163 for my $line ( @$amountoutstanding ) {
164 $amountleft += $line;
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');
178 # Sub -------------------------------------------
180 # C4::Context->userenv