Bug 14778: Make 3 tests pass
[koha.git] / t / db_dependent / Accounts.t
blobcefbb3bdbd7d048df39b755c8928a44c3a143ebb
1 #!/usr/bin/perl
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
10 # version.
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>.
19 use Modern::Perl;
21 use Test::More tests => 8;
22 use Test::MockModule;
23 use Test::Warn;
25 use t::lib::TestBuilder;
27 BEGIN {
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',
35 qw( recordpayment
36 makepayment
37 getnextacctno
38 chargelostitem
39 manualinvoice
40 getcharges
41 ModNote
42 getcredits
43 getrefunds
44 ReversePayment
45 recordpayment_selectaccts
46 makepartialpayment
47 WriteOffFee )
50 my $builder = t::lib::TestBuilder->new();
51 my $schema = Koha::Database->new()->schema();
53 my $dbh = C4::Context->dbh;
54 $dbh->{RaiseError}=1;
55 $dbh->{AutoCommit}=0;
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';
61 my $borrower_number;
63 my $context = new Test::MockModule('C4::Context');
64 $context->mock( 'userenv', sub {
65 return {
66 flags => 1,
67 id => 'my_userid',
68 branch => $branchcode,
70 });
73 subtest "recordpayment() tests" => sub {
75 plan tests => 10;
77 # Create a borrower
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',
83 surname => 'McFly',
84 firstname => 'Marty',
85 } );
86 $borrower->categorycode( $categorycode );
87 $borrower->branchcode( $branchcode );
88 $borrower->store;
90 my $sth = $dbh->prepare(
91 "INSERT INTO accountlines (
92 borrowernumber,
93 amountoutstanding )
94 VALUES ( ?, ? )"
96 $sth->execute($borrower->borrowernumber, '100');
97 $sth->execute($borrower->borrowernumber, '200');
99 $sth = $dbh->prepare("SELECT count(*) FROM accountlines");
100 $sth->execute;
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);
108 my $amountleft = 0;
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;
116 my $data = '20.00';
117 my $sys_paytype;
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);
123 $amountleft = 0;
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)
135 $data = '-30.00';
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);
141 $amountleft = 0;
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 )
153 $data = '150.00';
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);
159 $amountleft = 0;
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 )
171 $data = '200.00';
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);
177 $amountleft = 0;
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 {
191 plan tests => 6;
193 # Create a borrower
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 };
203 my $amount = 100;
204 my $accountline = $builder->build({ source => 'Accountline',
205 value => { borrowernumber => $borrowernumber,
206 amount => $amount,
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
217 makepayment(
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({
225 branch => $branch,
226 type => 'payment'
227 }, { order_by => { -desc => 'datetime' } })->next();
229 ok( defined $stat, "There's a payment log that matches the branch" );
231 SKIP: {
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 {
243 plan tests => 6;
245 # Create a borrower
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 };
255 my $amount = 100;
256 my $partialamount = 60;
257 my $accountline = $builder->build({ source => 'Accountline',
258 value => { borrowernumber => $borrowernumber,
259 amount => $amount,
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
270 makepartialpayment(
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({
278 branch => $branch,
279 type => 'payment'
280 }, { order_by => { -desc => 'datetime' } })->next();
282 ok( defined $stat, "There's a payment log that matches the branch" );
284 SKIP: {
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" );
294 $dbh->rollback;