Bug 12847: Items issued today is considered as overdue
[koha.git] / t / db_dependent / Accounts.t
1 #!/usr/bin/perl
2
3 # Copyright 2015 BibLibre
4 #
5 # This file is part of Koha.
6 #
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
10 # version.
11 #
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.
15 #
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>.
18
19 use strict;
20 use warnings;
21
22 use Test::More tests => 15;
23 use Test::Warn;
24
25 BEGIN {
26     use_ok('C4::Accounts');
27     use_ok('Koha::Object');
28     use_ok('Koha::Borrower');
29     use_ok('Data::Dumper');
30 }
31
32 can_ok( 'C4::Accounts',
33         qw(     recordpayment
34                 makepayment
35                 getnextacctno
36
37                 chargelostitem
38                 manualinvoice
39                 getcharges
40                 ModNote
41                 getcredits
42                 getrefunds
43                 ReversePayment
44                 recordpayment_selectaccts
45                 makepartialpayment
46                 WriteOffFee     )
47 );
48
49 my $dbh = C4::Context->dbh;
50 $dbh->{RaiseError}=1;
51 $dbh->{AutoCommit}=0;
52 $dbh->do(q|DELETE FROM accountlines|);
53 $dbh->do(q|DELETE FROM borrowers|);
54 $dbh->do(q|DELETE FROM issues|);
55
56 # Mock userenv
57 local $SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /redefined/ };
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
173
174
175 $dbh->rollback;
176
177
178 # Sub -------------------------------------------
179
180 # C4::Context->userenv
181 sub Mock_userenv {
182     return $userenv;
183 }