Bug 15168: (followup) remove useless diags
[koha.git] / t / db_dependent / Accounts.t
blob5b9c7640b41b332e6f4cdf6bf6f028e7affb121b
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 => 19;
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
48 purge_zero_balance_fees )
51 my $schema = Koha::Database->new->schema;
52 $schema->storage->txn_begin;
53 my $dbh = C4::Context->dbh;
55 my $builder = t::lib::TestBuilder->new();
57 my $library = $builder->build({
58 source => 'Branch',
59 });
61 $dbh->do(q|DELETE FROM accountlines|);
62 $dbh->do(q|DELETE FROM issues|);
63 $dbh->do(q|DELETE FROM borrowers|);
65 my $branchcode = $library->{branchcode};
66 my $borrower_number;
68 my $context = new Test::MockModule('C4::Context');
69 $context->mock( 'userenv', sub {
70 return {
71 flags => 1,
72 id => 'my_userid',
73 branch => $branchcode,
75 });
77 # Testing purge_zero_balance_fees
79 # The 3rd value in the insert is 'days ago' --
80 # 0 => today
81 # 1 => yesterday
82 # etc.
84 my $sth = $dbh->prepare(
85 "INSERT INTO accountlines (
86 borrowernumber,
87 amountoutstanding,
88 date,
89 description
91 VALUES ( ?, ?, (select date_sub(CURRENT_DATE, INTERVAL ? DAY) ), ? )"
94 my $days = 5;
96 my @test_data = (
97 { amount => 0 , days_ago => 0 , description =>'purge_zero_balance_fees should not delete 0 balance fees with date today' , delete => 0 } ,
98 { amount => 0 , days_ago => $days - 1 , description =>'purge_zero_balance_fees should not delete 0 balance fees with date before threshold day' , delete => 0 } ,
99 { amount => 0 , days_ago => $days , description =>'purge_zero_balance_fees should not delete 0 balance fees with date on threshold day' , delete => 0 } ,
100 { amount => 0 , days_ago => $days + 1 , description =>'purge_zero_balance_fees should delete 0 balance fees with date after threshold day' , delete => 1 } ,
101 { amount => undef , days_ago => $days + 1 , description =>'purge_zero_balance_fees should delete NULL balance fees with date after threshold day' , delete => 1 } ,
102 { amount => 5 , days_ago => $days - 1 , description =>'purge_zero_balance_fees should not delete fees with positive amout owed before threshold day' , delete => 0 } ,
103 { amount => 5 , days_ago => $days , description =>'purge_zero_balance_fees should not delete fees with positive amout owed on threshold day' , delete => 0 } ,
104 { amount => 5 , days_ago => $days + 1 , description =>'purge_zero_balance_fees should not delete fees with positive amout owed after threshold day' , delete => 0 } ,
105 { amount => -5 , days_ago => $days - 1 , description =>'purge_zero_balance_fees should not delete fees with negative amout owed before threshold day' , delete => 0 } ,
106 { amount => -5 , days_ago => $days , description =>'purge_zero_balance_fees should not delete fees with negative amout owed on threshold day' , delete => 0 } ,
107 { amount => -5 , days_ago => $days + 1 , description =>'purge_zero_balance_fees should not delete fees with negative amout owed after threshold day' , delete => 0 }
110 my $borrower = Koha::Borrower->new( { firstname => 'Test', surname => 'Patron', categorycode => 'PT', branchcode => 'MPL' } )->store();
112 for my $data ( @test_data ) {
113 $sth->execute($borrower->borrowernumber, $data->{amount}, $data->{days_ago}, $data->{description});
116 purge_zero_balance_fees( $days );
118 $sth = $dbh->prepare(
119 "select count(*) = 0 as deleted
120 from accountlines
121 where description = ?"
125 sub is_delete_correct {
126 my $should_delete = shift;
127 my $description = shift;
128 $sth->execute( $description );
129 my $test = $sth->fetchrow_hashref();
130 is( $test->{deleted}, $should_delete, $description )
133 for my $data (@test_data) {
134 is_delete_correct( $data->{delete}, $data->{description});
137 $dbh->do(q|DELETE FROM accountlines|);
139 subtest "recordpayment() tests" => sub {
141 plan tests => 10;
143 # Create a borrower
144 my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
145 my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode };
147 my $borrower = Koha::Borrower->new( {
148 cardnumber => '1234567890',
149 surname => 'McFly',
150 firstname => 'Marty',
151 } );
152 $borrower->categorycode( $categorycode );
153 $borrower->branchcode( $branchcode );
154 $borrower->store;
156 my $sth = $dbh->prepare(
157 "INSERT INTO accountlines (
158 borrowernumber,
159 amountoutstanding )
160 VALUES ( ?, ? )"
162 $sth->execute($borrower->borrowernumber, '100');
163 $sth->execute($borrower->borrowernumber, '200');
165 $sth = $dbh->prepare("SELECT count(*) FROM accountlines");
166 $sth->execute;
167 my $count = $sth->fetchrow_array;
168 is ($count, 2, 'There is 2 lines as expected');
170 # Testing recordpayment -------------------------
171 # There is $100 in the account
172 $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
173 my $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
174 my $amountleft = 0;
175 for my $line ( @$amountoutstanding ) {
176 $amountleft += $line;
178 ok($amountleft == 300, 'The account has 300$ as expected' );
180 # We make a $20 payment
181 my $borrowernumber = $borrower->borrowernumber;
182 my $data = '20.00';
183 my $sys_paytype;
184 my $payment_note = '$20.00 payment note';
185 recordpayment($borrowernumber, $data, $sys_paytype, $payment_note);
186 # There is now $280 in the account
187 $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
188 $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
189 $amountleft = 0;
190 for my $line ( @$amountoutstanding ) {
191 $amountleft += $line;
193 ok($amountleft == 280, 'The account has $280 as expected' );
194 # Is the payment note well registered
195 $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
196 $sth->execute($borrower->borrowernumber);
197 my $note = $sth->fetchrow_array;
198 is($note,'$20.00 payment note', '$20.00 payment note is registered');
200 # We make a -$30 payment (a NEGATIVE payment)
201 $data = '-30.00';
202 $payment_note = '-$30.00 payment note';
203 recordpayment($borrowernumber, $data, $sys_paytype, $payment_note);
204 # There is now $310 in the account
205 $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
206 $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
207 $amountleft = 0;
208 for my $line ( @$amountoutstanding ) {
209 $amountleft += $line;
211 ok($amountleft == 310, 'The account has $310 as expected' );
212 # Is the payment note well registered
213 $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
214 $sth->execute($borrower->borrowernumber);
215 $note = $sth->fetchrow_array;
216 is($note,'-$30.00 payment note', '-$30.00 payment note is registered');
218 #We make a $150 payment ( > 1stLine )
219 $data = '150.00';
220 $payment_note = '$150.00 payment note';
221 recordpayment($borrowernumber, $data, $sys_paytype, $payment_note);
222 # There is now $160 in the account
223 $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
224 $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
225 $amountleft = 0;
226 for my $line ( @$amountoutstanding ) {
227 $amountleft += $line;
229 ok($amountleft == 160, 'The account has $160 as expected' );
230 # Is the payment note well registered
231 $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
232 $sth->execute($borrower->borrowernumber);
233 $note = $sth->fetchrow_array;
234 is($note,'$150.00 payment note', '$150.00 payment note is registered');
236 #We make a $200 payment ( > amountleft )
237 $data = '200.00';
238 $payment_note = '$200.00 payment note';
239 recordpayment($borrowernumber, $data, $sys_paytype, $payment_note);
240 # There is now -$40 in the account
241 $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
242 $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
243 $amountleft = 0;
244 for my $line ( @$amountoutstanding ) {
245 $amountleft += $line;
247 ok($amountleft == -40, 'The account has -$40 as expected, (credit situation)' );
248 # Is the payment note well registered
249 $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
250 $sth->execute($borrower->borrowernumber);
251 $note = $sth->fetchrow_array;
252 is($note,'$200.00 payment note', '$200.00 payment note is registered');
255 subtest "makepayment() tests" => sub {
257 plan tests => 6;
259 # Create a borrower
260 my $category = $builder->build({ source => 'Category' })->{ categorycode };
261 my $branch = $builder->build({ source => 'Branch' })->{ branchcode };
262 $branchcode = $branch;
263 my $borrowernumber = $builder->build({
264 source => 'Borrower',
265 value => { categorycode => $category,
266 branchcode => $branch }
267 })->{ borrowernumber };
269 my $amount = 100;
270 my $accountline = $builder->build({ source => 'Accountline',
271 value => { borrowernumber => $borrowernumber,
272 amount => $amount,
273 amountoutstanding => $amount }
276 my $rs = $schema->resultset('Accountline')->search({
277 borrowernumber => $borrowernumber
280 is( $rs->count(), 1, 'Accountline created' );
282 # make the full payment
283 makepayment(
284 $accountline->{ accountlines_id }, $borrowernumber,
285 $accountline->{ accountno }, $amount,
286 $borrowernumber, $branch, 'A payment note' );
288 # TODO: someone should write actual tests for makepayment()
290 my $stat = $schema->resultset('Statistic')->search({
291 branch => $branch,
292 type => 'payment'
293 }, { order_by => { -desc => 'datetime' } })->next();
295 ok( defined $stat, "There's a payment log that matches the branch" );
297 SKIP: {
298 skip "No statistic logged", 4 unless defined $stat;
300 is( $stat->type, 'payment', "Correct statistic type" );
301 is( $stat->branch, $branch, "Correct branch logged to statistics" );
302 is( $stat->borrowernumber, $borrowernumber, "Correct borrowernumber logged to statistics" );
303 is( $stat->value, "$amount" . "\.0000", "Correct amount logged to statistics" );
307 subtest "makepartialpayment() tests" => sub {
309 plan tests => 6;
311 # Create a borrower
312 my $category = $builder->build({ source => 'Category' })->{ categorycode };
313 my $branch = $builder->build({ source => 'Branch' })->{ branchcode };
314 $branchcode = $branch;
315 my $borrowernumber = $builder->build({
316 source => 'Borrower',
317 value => { categorycode => $category,
318 branchcode => $branch }
319 })->{ borrowernumber };
321 my $amount = 100;
322 my $partialamount = 60;
323 my $accountline = $builder->build({ source => 'Accountline',
324 value => { borrowernumber => $borrowernumber,
325 amount => $amount,
326 amountoutstanding => $amount }
329 my $rs = $schema->resultset('Accountline')->search({
330 borrowernumber => $borrowernumber
333 is( $rs->count(), 1, 'Accountline created' );
335 # make the full payment
336 makepartialpayment(
337 $accountline->{ accountlines_id }, $borrowernumber,
338 $accountline->{ accountno }, $partialamount,
339 $borrowernumber, $branch, 'A payment note' );
341 # TODO: someone should write actual tests for makepartialpayment()
343 my $stat = $schema->resultset('Statistic')->search({
344 branch => $branch,
345 type => 'payment'
346 }, { order_by => { -desc => 'datetime' } })->next();
348 ok( defined $stat, "There's a payment log that matches the branch" );
350 SKIP: {
351 skip "No statistic logged", 4 unless defined $stat;
353 is( $stat->type, 'payment', "Correct statistic type" );
354 is( $stat->branch, $branch, "Correct branch logged to statistics" );
355 is( $stat->borrowernumber, $borrowernumber, "Correct borrowernumber logged to statistics" );
356 is( $stat->value, "$partialamount" . "\.0000", "Correct amount logged to statistics" );