Bug 14878: Tests - Create the branchcodes when needed
[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 Modern::Perl;
20
21 use Test::More tests => 8;
22 use Test::MockModule;
23 use Test::Warn;
24
25 use t::lib::TestBuilder;
26
27 BEGIN {
28     use_ok('C4::Accounts');
29     use_ok('Koha::Object');
30     use_ok('Koha::Borrower');
31     use_ok('Data::Dumper');
32 }
33
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 )
48 );
49
50 my $schema  = Koha::Database->new->schema;
51 $schema->storage->txn_begin;
52 my $dbh = C4::Context->dbh;
53
54 my $builder = t::lib::TestBuilder->new();
55
56 my $library = $builder->build({
57     source => 'Branch',
58 });
59
60 $dbh->do(q|DELETE FROM accountlines|);
61 $dbh->do(q|DELETE FROM issues|);
62 $dbh->do(q|DELETE FROM borrowers|);
63
64 my $branchcode = $library->{branchcode};
65 my $borrower_number;
66
67 my $context = new Test::MockModule('C4::Context');
68 $context->mock( 'userenv', sub {
69     return {
70         flags  => 1,
71         id     => 'my_userid',
72         branch => $branchcode,
73     };
74 });
75
76
77 subtest "recordpayment() tests" => sub {
78
79     plan tests => 10;
80
81     # Create a borrower
82     my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
83     my $branchcode   = $builder->build({ source => 'Branch' })->{ branchcode };
84
85     my $borrower = Koha::Borrower->new( {
86         cardnumber => '1234567890',
87         surname => 'McFly',
88         firstname => 'Marty',
89     } );
90     $borrower->categorycode( $categorycode );
91     $borrower->branchcode( $branchcode );
92     $borrower->store;
93
94     my $sth = $dbh->prepare(
95         "INSERT INTO accountlines (
96             borrowernumber,
97             amountoutstanding )
98         VALUES ( ?, ? )"
99     );
100     $sth->execute($borrower->borrowernumber, '100');
101     $sth->execute($borrower->borrowernumber, '200');
102
103     $sth = $dbh->prepare("SELECT count(*) FROM accountlines");
104     $sth->execute;
105     my $count = $sth->fetchrow_array;
106     is ($count, 2, 'There is 2 lines as expected');
107
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);
112     my $amountleft = 0;
113     for my $line ( @$amountoutstanding ) {
114         $amountleft += $line;
115     }
116     ok($amountleft == 300, 'The account has 300$ as expected' );
117
118     # We make a $20 payment
119     my $borrowernumber = $borrower->borrowernumber;
120     my $data = '20.00';
121     my $sys_paytype;
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);
127     $amountleft = 0;
128     for my $line ( @$amountoutstanding ) {
129         $amountleft += $line;
130     }
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');
137
138     # We make a -$30 payment (a NEGATIVE payment)
139     $data = '-30.00';
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);
145     $amountleft = 0;
146     for my $line ( @$amountoutstanding ) {
147         $amountleft += $line;
148     }
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');
155
156     #We make a $150 payment ( > 1stLine )
157     $data = '150.00';
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);
163     $amountleft = 0;
164     for my $line ( @$amountoutstanding ) {
165         $amountleft += $line;
166     }
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');
173
174     #We make a $200 payment ( > amountleft )
175     $data = '200.00';
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);
181     $amountleft = 0;
182     for my $line ( @$amountoutstanding ) {
183         $amountleft += $line;
184     }
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');
191 };
192
193 subtest "makepayment() tests" => sub {
194
195     plan tests => 6;
196
197     # Create a borrower
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 };
206
207     my $amount = 100;
208     my $accountline = $builder->build({ source => 'Accountline',
209         value  => { borrowernumber => $borrowernumber,
210                     amount => $amount,
211                     amountoutstanding => $amount }
212     });
213
214     my $rs = $schema->resultset('Accountline')->search({
215         borrowernumber => $borrowernumber
216     });
217
218     is( $rs->count(), 1, 'Accountline created' );
219
220     # make the full payment
221     makepayment(
222         $accountline->{ accountlines_id }, $borrowernumber,
223         $accountline->{ accountno },       $amount,
224         $borrowernumber, $branch, 'A payment note' );
225
226     # TODO: someone should write actual tests for makepayment()
227
228     my $stat = $schema->resultset('Statistic')->search({
229         branch  => $branch,
230         type    => 'payment'
231     }, { order_by => { -desc => 'datetime' } })->next();
232
233     ok( defined $stat, "There's a payment log that matches the branch" );
234
235     SKIP: {
236         skip "No statistic logged", 4 unless defined $stat;
237
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" );
242     }
243 };
244
245 subtest "makepartialpayment() tests" => sub {
246
247     plan tests => 6;
248
249     # Create a borrower
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 };
258
259     my $amount = 100;
260     my $partialamount = 60;
261     my $accountline = $builder->build({ source => 'Accountline',
262         value  => { borrowernumber => $borrowernumber,
263                     amount => $amount,
264                     amountoutstanding => $amount }
265     });
266
267     my $rs = $schema->resultset('Accountline')->search({
268         borrowernumber => $borrowernumber
269     });
270
271     is( $rs->count(), 1, 'Accountline created' );
272
273     # make the full payment
274     makepartialpayment(
275         $accountline->{ accountlines_id }, $borrowernumber,
276         $accountline->{ accountno },       $partialamount,
277         $borrowernumber, $branch, 'A payment note' );
278
279     # TODO: someone should write actual tests for makepartialpayment()
280
281     my $stat = $schema->resultset('Statistic')->search({
282         branch  => $branch,
283         type    => 'payment'
284     }, { order_by => { -desc => 'datetime' } })->next();
285
286     ok( defined $stat, "There's a payment log that matches the branch" );
287
288     SKIP: {
289         skip "No statistic logged", 4 unless defined $stat;
290
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" );
295     }
296 };
297
298 1;