Bug 21683: Remove accountlines.accountno
[koha.git] / t / db_dependent / Accounts.t
blob6af3a3d1c09d5ac7a706ab5ff5addde95d231ca8
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 => 34;
22 use Test::MockModule;
23 use Test::Warn;
25 use t::lib::TestBuilder;
26 use t::lib::Mocks;
28 use Koha::Account;
29 use Koha::Account::Lines;
30 use Koha::Account::Offsets;
31 use Koha::Notice::Messages;
32 use Koha::Notice::Templates;
33 use Koha::DateUtils qw( dt_from_string );
35 BEGIN {
36 use_ok('C4::Accounts');
37 use_ok('Koha::Object');
38 use_ok('Koha::Patron');
39 use_ok('Data::Dumper');
42 can_ok( 'C4::Accounts',
43 qw(
44 chargelostitem
45 manualinvoice
46 purge_zero_balance_fees )
49 my $schema = Koha::Database->new->schema;
50 $schema->storage->txn_begin;
51 my $dbh = C4::Context->dbh;
53 my $builder = t::lib::TestBuilder->new;
54 my $library = $builder->build( { source => 'Branch' } );
56 $dbh->do(q|DELETE FROM accountlines|);
57 $dbh->do(q|DELETE FROM issues|);
58 $dbh->do(q|DELETE FROM borrowers|);
60 my $branchcode = $library->{branchcode};
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 });
71 my $userenv_branchcode = $branchcode;
73 # Test manualinvoice
74 my $itemtype = $builder->build( { source => 'Itemtype' } );
75 my $item = $builder->build( { source => 'Item', value => { itype => $itemtype->{itemtype} } } );
76 my $patron = $builder->build( { source => 'Borrower' } );
77 my $amount = '5.000000';
78 my $description = "Test fee!";
79 my $type = 'L';
80 my $note = 'Test note!';
81 warning_like {
82 C4::Accounts::manualinvoice( $patron->{borrowernumber},
83 $item->{itemnumber}, $description, $type, $amount, $note )
85 qr/C4::Accounts::manualinvoice is deprecated in favor of Koha::Account->add_debit/,
86 "deprecation warning received for manualinvoice";
87 my ($accountline) = Koha::Account::Lines->search(
89 borrowernumber => $patron->{borrowernumber}
92 is( $accountline->accounttype, $type, 'Accountline type set correctly for manualinvoice' );
93 is( $accountline->amount, $amount, 'Accountline amount set correctly for manualinvoice' );
94 ok( $accountline->description =~ /^$description/, 'Accountline description set correctly for manualinvoice' );
95 is( $accountline->note, $note, 'Accountline note set correctly for manualinvoice' );
96 is( $accountline->branchcode, $branchcode, 'Accountline branchcode set correctly for manualinvoice' );
98 $dbh->do(q|DELETE FROM accountlines|);
100 # Testing purge_zero_balance_fees
102 # The 3rd value in the insert is 'days ago' --
103 # 0 => today
104 # 1 => yesterday
105 # etc.
107 my $sth = $dbh->prepare(
108 "INSERT INTO accountlines (
109 borrowernumber,
110 amountoutstanding,
111 date,
112 description
114 VALUES ( ?, ?, (select date_sub(CURRENT_DATE, INTERVAL ? DAY) ), ? )"
117 my $days = 5;
119 my @test_data = (
120 { amount => 0 , days_ago => 0 , description =>'purge_zero_balance_fees should not delete 0 balance fees with date today' , delete => 0 } ,
121 { amount => 0 , days_ago => $days - 1 , description =>'purge_zero_balance_fees should not delete 0 balance fees with date before threshold day' , delete => 0 } ,
122 { amount => 0 , days_ago => $days , description =>'purge_zero_balance_fees should not delete 0 balance fees with date on threshold day' , delete => 0 } ,
123 { amount => 0 , days_ago => $days + 1 , description =>'purge_zero_balance_fees should delete 0 balance fees with date after threshold day' , delete => 1 } ,
124 { amount => undef , days_ago => $days + 1 , description =>'purge_zero_balance_fees should delete NULL balance fees with date after threshold day' , delete => 1 } ,
125 { amount => 5 , days_ago => $days - 1 , description =>'purge_zero_balance_fees should not delete fees with positive amout owed before threshold day' , delete => 0 } ,
126 { amount => 5 , days_ago => $days , description =>'purge_zero_balance_fees should not delete fees with positive amout owed on threshold day' , delete => 0 } ,
127 { amount => 5 , days_ago => $days + 1 , description =>'purge_zero_balance_fees should not delete fees with positive amout owed after threshold day' , delete => 0 } ,
128 { amount => -5 , days_ago => $days - 1 , description =>'purge_zero_balance_fees should not delete fees with negative amout owed before threshold day' , delete => 0 } ,
129 { amount => -5 , days_ago => $days , description =>'purge_zero_balance_fees should not delete fees with negative amout owed on threshold day' , delete => 0 } ,
130 { amount => -5 , days_ago => $days + 1 , description =>'purge_zero_balance_fees should not delete fees with negative amout owed after threshold day' , delete => 0 }
132 my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
133 my $borrower = Koha::Patron->new( { firstname => 'Test', surname => 'Patron', categorycode => $categorycode, branchcode => $branchcode } )->store();
135 for my $data ( @test_data ) {
136 $sth->execute($borrower->borrowernumber, $data->{amount}, $data->{days_ago}, $data->{description});
139 purge_zero_balance_fees( $days );
141 $sth = $dbh->prepare(
142 "select count(*) = 0 as deleted
143 from accountlines
144 where description = ?"
148 sub is_delete_correct {
149 my $should_delete = shift;
150 my $description = shift;
151 $sth->execute( $description );
152 my $test = $sth->fetchrow_hashref();
153 is( $test->{deleted}, $should_delete, $description )
156 for my $data (@test_data) {
157 is_delete_correct( $data->{delete}, $data->{description});
160 $dbh->do(q|DELETE FROM accountlines|);
162 subtest "Koha::Account::pay tests" => sub {
164 plan tests => 14;
166 # Create a borrower
167 my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
168 my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode };
170 my $borrower = Koha::Patron->new( {
171 cardnumber => '1234567890',
172 surname => 'McFly',
173 firstname => 'Marty',
174 } );
175 $borrower->categorycode( $categorycode );
176 $borrower->branchcode( $branchcode );
177 $borrower->store;
179 my $account = Koha::Account->new({ patron_id => $borrower->id });
181 my $line1 = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amountoutstanding => 100 })->store();
182 my $line2 = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amountoutstanding => 200 })->store();
184 $sth = $dbh->prepare("SELECT count(*) FROM accountlines");
185 $sth->execute;
186 my $count = $sth->fetchrow_array;
187 is($count, 2, 'There is 2 lines as expected');
189 # There is $100 in the account
190 $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
191 my $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
192 my $amountleft = 0;
193 for my $line ( @$amountoutstanding ) {
194 $amountleft += $line;
196 is($amountleft, 300, 'The account has 300$ as expected' );
198 # We make a $20 payment
199 my $borrowernumber = $borrower->borrowernumber;
200 my $data = '20.00';
201 my $payment_note = '$20.00 payment note';
202 my $id = $account->pay( { amount => $data, note => $payment_note, payment_type => "TEST_TYPE" } );
204 my $accountline = Koha::Account::Lines->find( $id );
205 is( $accountline->payment_type, "TEST_TYPE", "Payment type passed into pay is set in account line correctly" );
207 # There is now $280 in the account
208 $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
209 $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
210 $amountleft = 0;
211 for my $line ( @$amountoutstanding ) {
212 $amountleft += $line;
214 is($amountleft, 280, 'The account has $280 as expected' );
216 # Is the payment note well registered
217 $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
218 $sth->execute($borrower->borrowernumber);
219 my $note = $sth->fetchrow_array;
220 is($note,'$20.00 payment note', '$20.00 payment note is registered');
222 # We make a -$30 payment (a NEGATIVE payment)
223 $data = '-30.00';
224 $payment_note = '-$30.00 payment note';
225 $account->pay( { amount => $data, note => $payment_note } );
227 # There is now $310 in the account
228 $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
229 $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
230 $amountleft = 0;
231 for my $line ( @$amountoutstanding ) {
232 $amountleft += $line;
234 is($amountleft, 310, 'The account has $310 as expected' );
235 # Is the payment note well registered
236 $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
237 $sth->execute($borrower->borrowernumber);
238 $note = $sth->fetchrow_array;
239 is($note,'-$30.00 payment note', '-$30.00 payment note is registered');
241 #We make a $150 payment ( > 1stLine )
242 $data = '150.00';
243 $payment_note = '$150.00 payment note';
244 $account->pay( { amount => $data, note => $payment_note } );
246 # There is now $160 in the account
247 $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
248 $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
249 $amountleft = 0;
250 for my $line ( @$amountoutstanding ) {
251 $amountleft += $line;
253 is($amountleft, 160, 'The account has $160 as expected' );
255 # Is the payment note well registered
256 $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
257 $sth->execute($borrower->borrowernumber);
258 $note = $sth->fetchrow_array;
259 is($note,'$150.00 payment note', '$150.00 payment note is registered');
261 #We make a $200 payment ( > amountleft )
262 $data = '200.00';
263 $payment_note = '$200.00 payment note';
264 $account->pay( { amount => $data, note => $payment_note } );
266 # There is now -$40 in the account
267 $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
268 $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
269 $amountleft = 0;
270 for my $line ( @$amountoutstanding ) {
271 $amountleft += $line;
273 is($amountleft, -40, 'The account has -$40 as expected, (credit situation)' );
275 # Is the payment note well registered
276 $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
277 $sth->execute($borrower->borrowernumber);
278 $note = $sth->fetchrow_array;
279 is($note,'$200.00 payment note', '$200.00 payment note is registered');
281 my $line3 = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amountoutstanding => 42, accounttype => 'TEST' })->store();
282 my $payment_id = $account->pay( { lines => [$line3], amount => 42 } );
283 my $payment = Koha::Account::Lines->find( $payment_id );
284 is( $payment->amount(), '-42.000000', "Payment paid the specified fine" );
285 $line3 = Koha::Account::Lines->find( $line3->id );
286 is( $line3->amountoutstanding, '0.000000', "Specified fine is paid" );
287 is( $payment->branchcode, undef, 'branchcode passed, then undef' );
290 subtest "Koha::Account::pay particular line tests" => sub {
292 plan tests => 5;
294 # Create a borrower
295 my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
296 my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode };
298 my $borrower = Koha::Patron->new( {
299 cardnumber => 'kylemhall',
300 surname => 'Hall',
301 firstname => 'Kyle',
302 } );
303 $borrower->categorycode( $categorycode );
304 $borrower->branchcode( $branchcode );
305 $borrower->store;
307 my $account = Koha::Account->new({ patron_id => $borrower->id });
309 my $line1 = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amountoutstanding => 1 })->store();
310 my $line2 = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amountoutstanding => 2 })->store();
311 my $line3 = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amountoutstanding => 3 })->store();
312 my $line4 = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amountoutstanding => 4 })->store();
314 is( $account->balance(), 10, "Account balance is 10" );
316 $account->pay(
318 lines => [$line2, $line3, $line4],
319 amount => 4,
323 $_->_result->discard_changes foreach ( $line1, $line2, $line3, $line4 );
325 # Line1 is not paid at all, as it was not passed in the lines param
326 is( $line1->amountoutstanding, "1.000000", "Line 1 was not paid" );
327 # Line2 was paid in full, as it was the first in the lines list
328 is( $line2->amountoutstanding, "0.000000", "Line 2 was paid in full" );
329 # Line3 was paid partially, as the remaining balance did not cover it entirely
330 is( $line3->amountoutstanding, "1.000000", "Line 3 was paid to 1.00" );
331 # Line4 was not paid at all, as the payment was all used up by that point
332 is( $line4->amountoutstanding, "4.000000", "Line 4 was not paid" );
335 subtest "Koha::Account::pay writeoff tests" => sub {
337 plan tests => 5;
339 # Create a borrower
340 my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
341 my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode };
343 my $borrower = Koha::Patron->new( {
344 cardnumber => 'chelseahall',
345 surname => 'Hall',
346 firstname => 'Chelsea',
347 } );
348 $borrower->categorycode( $categorycode );
349 $borrower->branchcode( $branchcode );
350 $borrower->store;
352 my $account = Koha::Account->new({ patron_id => $borrower->id });
354 my $line = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amountoutstanding => 42 })->store();
356 is( $account->balance(), 42, "Account balance is 42" );
358 my $id = $account->pay(
360 lines => [$line],
361 amount => 42,
362 type => 'writeoff',
366 $line->_result->discard_changes();
368 is( $line->amountoutstanding, "0.000000", "Line was written off" );
370 my $writeoff = Koha::Account::Lines->find( $id );
372 is( $writeoff->accounttype, 'W', 'Type is correct' );
373 is( $writeoff->description, 'Writeoff', 'Description is correct' );
374 is( $writeoff->amount, '-42.000000', 'Amount is correct' );
377 subtest "More Koha::Account::pay tests" => sub {
379 plan tests => 8;
381 # Create a borrower
382 my $category = $builder->build({ source => 'Category' })->{ categorycode };
383 my $branch = $builder->build({ source => 'Branch' })->{ branchcode };
384 $branchcode = $branch;
385 my $borrowernumber = $builder->build({
386 source => 'Borrower',
387 value => { categorycode => $category,
388 branchcode => $branch }
389 })->{ borrowernumber };
391 my $amount = 100;
392 my $accountline = $builder->build({ source => 'Accountline',
393 value => { borrowernumber => $borrowernumber,
394 amount => $amount,
395 amountoutstanding => $amount }
398 my $rs = $schema->resultset('Accountline')->search({
399 borrowernumber => $borrowernumber
402 is( $rs->count(), 1, 'Accountline created' );
404 my $account = Koha::Account->new( { patron_id => $borrowernumber } );
405 my $line = Koha::Account::Lines->find( $accountline->{ accountlines_id } );
406 # make the full payment
407 $account->pay({ lines => [$line], amount => $amount, library_id => $branch, note => 'A payment note' });
409 my $offset = Koha::Account::Offsets->search({ debit_id => $accountline->{accountlines_id} })->next();
410 is( $offset->amount(), '-100.000000', 'Offset amount is -100.00' );
411 is( $offset->type(), 'Payment', 'Offset type is Payment' );
413 my $stat = $schema->resultset('Statistic')->search({
414 branch => $branch,
415 type => 'payment'
416 }, { order_by => { -desc => 'datetime' } })->next();
418 ok( defined $stat, "There's a payment log that matches the branch" );
420 SKIP: {
421 skip "No statistic logged", 4 unless defined $stat;
423 is( $stat->type, 'payment', "Correct statistic type" );
424 is( $stat->branch, $branch, "Correct branch logged to statistics" );
425 is( $stat->borrowernumber, $borrowernumber, "Correct borrowernumber logged to statistics" );
426 is( $stat->value+0, $amount, "Correct amount logged to statistics" );
430 subtest "Even more Koha::Account::pay tests" => sub {
432 plan tests => 8;
434 # Create a borrower
435 my $category = $builder->build({ source => 'Category' })->{ categorycode };
436 my $branch = $builder->build({ source => 'Branch' })->{ branchcode };
437 $branchcode = $branch;
438 my $borrowernumber = $builder->build({
439 source => 'Borrower',
440 value => { categorycode => $category,
441 branchcode => $branch }
442 })->{ borrowernumber };
444 my $amount = 100;
445 my $partialamount = 60;
446 my $accountline = $builder->build({ source => 'Accountline',
447 value => { borrowernumber => $borrowernumber,
448 amount => $amount,
449 amountoutstanding => $amount }
452 my $rs = $schema->resultset('Accountline')->search({
453 borrowernumber => $borrowernumber
456 is( $rs->count(), 1, 'Accountline created' );
458 my $account = Koha::Account->new( { patron_id => $borrowernumber } );
459 my $line = Koha::Account::Lines->find( $accountline->{ accountlines_id } );
460 # make the full payment
461 $account->pay({ lines => [$line], amount => $partialamount, library_id => $branch, note => 'A payment note' });
463 my $offset = Koha::Account::Offsets->search( { debit_id => $accountline->{ accountlines_id } } )->next();
464 is( $offset->amount, '-60.000000', 'Offset amount is -60.00' );
465 is( $offset->type, 'Payment', 'Offset type is payment' );
467 my $stat = $schema->resultset('Statistic')->search({
468 branch => $branch,
469 type => 'payment'
470 }, { order_by => { -desc => 'datetime' } })->next();
472 ok( defined $stat, "There's a payment log that matches the branch" );
474 SKIP: {
475 skip "No statistic logged", 4 unless defined $stat;
477 is( $stat->type, 'payment', "Correct statistic type" );
478 is( $stat->branch, $branch, "Correct branch logged to statistics" );
479 is( $stat->borrowernumber, $borrowernumber, "Correct borrowernumber logged to statistics" );
480 is( $stat->value+0, $partialamount, "Correct amount logged to statistics" );
484 subtest 'balance' => sub {
485 plan tests => 2;
487 my $patron = $builder->build({source => 'Borrower'});
488 $patron = Koha::Patrons->find( $patron->{borrowernumber} );
489 my $account = $patron->account;
490 is( $account->balance, 0, 'balance should return 0 if the patron does not have fines' );
492 my $accountline_1 = $builder->build(
494 source => 'Accountline',
495 value => {
496 borrowernumber => $patron->borrowernumber,
497 amount => 42,
498 amountoutstanding => 42
502 my $accountline_2 = $builder->build(
504 source => 'Accountline',
505 value => {
506 borrowernumber => $patron->borrowernumber,
507 amount => -13,
508 amountoutstanding => -13
513 my $balance = $patron->account->balance;
514 is( int($balance), 29, 'balance should return the correct value');
516 $patron->delete;
519 subtest "C4::Accounts::chargelostitem tests" => sub {
520 plan tests => 3;
522 my $branch = $builder->build( { source => 'Branch' } );
523 my $branchcode = $branch->{branchcode};
525 my $staff = $builder->build( { source => 'Borrower' } );
526 my $staff_id = $staff->{borrowernumber};
528 my $module = Test::MockModule->new('C4::Context');
529 $module->mock(
530 'userenv',
531 sub {
532 return {
533 flags => 1,
534 number => $staff_id,
535 branch => $branchcode,
540 my $itype_no_replace_no_fee = $builder->build({ source => 'Itemtype', value => {
541 rentalcharge => 0,
542 defaultreplacecost => undef,
543 processfee => undef,
544 }});
545 my $itype_replace_no_fee = $builder->build({ source => 'Itemtype', value => {
546 rentalcharge => 0,
547 defaultreplacecost => 16.32,
548 processfee => undef,
549 }});
550 my $itype_no_replace_fee = $builder->build({ source => 'Itemtype', value => {
551 rentalcharge => 0,
552 defaultreplacecost => undef,
553 processfee => 8.16,
554 }});
555 my $itype_replace_fee = $builder->build({ source => 'Itemtype', value => {
556 rentalcharge => 0,
557 defaultreplacecost => 4.08,
558 processfee => 2.04,
559 }});
560 my $cli_borrowernumber = $builder->build({ source => 'Borrower' })->{'borrowernumber'};
561 my $cli_itemnumber1 = $builder->build({ source => 'Item', value => { itype => $itype_no_replace_no_fee->{itemtype} } })->{'itemnumber'};
562 my $cli_itemnumber2 = $builder->build({ source => 'Item', value => { itype => $itype_replace_no_fee->{itemtype} } })->{'itemnumber'};
563 my $cli_itemnumber3 = $builder->build({ source => 'Item', value => { itype => $itype_no_replace_fee->{itemtype} } })->{'itemnumber'};
564 my $cli_itemnumber4 = $builder->build({ source => 'Item', value => { itype => $itype_replace_fee->{itemtype} } })->{'itemnumber'};
566 my $cli_issue_id_1 = $builder->build({ source => 'Issue', value => { borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1 } })->{issue_id};
567 my $cli_issue_id_2 = $builder->build({ source => 'Issue', value => { borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2 } })->{issue_id};
568 my $cli_issue_id_3 = $builder->build({ source => 'Issue', value => { borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3 } })->{issue_id};
569 my $cli_issue_id_4 = $builder->build({ source => 'Issue', value => { borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4 } })->{issue_id};
571 my $lostfine;
572 my $procfee;
574 subtest "fee application tests" => sub {
575 plan tests => 40;
577 t::lib::Mocks::mock_preference('item-level_itypes', '1');
578 t::lib::Mocks::mock_preference('useDefaultReplacementCost', '0');
580 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber1, 0, "Perdedor");
581 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, accounttype => 'L' });
582 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, accounttype => 'PF' });
583 ok( !$lostfine, "No lost fine if no replacementcost or default when pref off");
584 ok( !$procfee, "No processing fee if no processing fee");
585 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber1, 6.12, "Perdedor");
586 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, accounttype => 'L' });
587 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, accounttype => 'PF' });
588 ok( $lostfine->amount == 6.12, "Lost fine equals replacementcost when pref off and no default set");
589 ok( !$procfee, "No processing fee if no processing fee");
590 $lostfine->delete();
592 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber2, 0, "Perdedor");
593 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, accounttype => 'L' });
594 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, accounttype => 'PF' });
595 ok( !$lostfine, "No lost fine if no replacementcost but default set when pref off");
596 ok( !$procfee, "No processing fee if no processing fee");
597 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber2, 6.12, "Perdedor");
598 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, accounttype => 'L' });
599 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, accounttype => 'PF' });
600 ok( $lostfine->amount == 6.12 , "Lost fine equals replacementcost when pref off and default set");
601 ok( !$procfee, "No processing fee if no processing fee");
602 $lostfine->delete();
604 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber3, 0, "Perdedor");
605 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, accounttype => 'L' });
606 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, accounttype => 'PF' });
607 ok( !$lostfine, "No lost fine if no replacementcost and no default set when pref off");
608 ok( $procfee->amount == 8.16, "Processing fee if processing fee");
609 is( $procfee->issue_id, $cli_issue_id_3, "Processing fee issue id is correct" );
610 $procfee->delete();
611 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber3, 6.12, "Perdedor");
612 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, accounttype => 'L' });
613 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, accounttype => 'PF' });
614 ok( $lostfine->amount == 6.12 , "Lost fine equals replacementcost when pref off and no default set");
615 ok( $procfee->amount == 8.16, "Processing fee if processing fee");
616 is( $procfee->issue_id, $cli_issue_id_3, "Processing fee issue id is correct" );
617 $lostfine->delete();
618 $procfee->delete();
620 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 0, "Perdedor");
621 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'L' });
622 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'PF' });
623 ok( !$lostfine, "No lost fine if no replacementcost but default set when pref off");
624 ok( $procfee->amount == 2.04, "Processing fee if processing fee");
625 is( $procfee->issue_id, $cli_issue_id_4, "Processing fee issue id is correct" );
626 $procfee->delete();
627 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 6.12, "Perdedor");
628 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'L' });
629 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'PF' });
630 ok( $lostfine->amount == 6.12 , "Lost fine equals replacementcost when pref off and default set");
631 ok( $procfee->amount == 2.04, "Processing fee if processing fee");
632 is( $procfee->issue_id, $cli_issue_id_4, "Processing fee issue id is correct" );
633 $lostfine->delete();
634 $procfee->delete();
636 t::lib::Mocks::mock_preference('useDefaultReplacementCost', '1');
638 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber1, 0, "Perdedor");
639 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, accounttype => 'L' });
640 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, accounttype => 'PF' });
641 ok( !$lostfine, "No lost fine if no replacementcost or default when pref on");
642 ok( !$procfee, "No processing fee if no processing fee");
643 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber1, 6.12, "Perdedor");
644 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, accounttype => 'L' });
645 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, accounttype => 'PF' });
646 is( $lostfine->amount, "6.120000", "Lost fine equals replacementcost when pref on and no default set");
647 ok( !$procfee, "No processing fee if no processing fee");
649 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber2, 0, "Perdedor");
650 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, accounttype => 'L' });
651 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, accounttype => 'PF' });
652 is( $lostfine->amount(), "16.320000", "Lost fine is default if no replacementcost but default set when pref on");
653 ok( !$procfee, "No processing fee if no processing fee");
654 $lostfine->delete();
655 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber2, 6.12, "Perdedor");
656 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, accounttype => 'L' });
657 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, accounttype => 'PF' });
658 is( $lostfine->amount, "6.120000" , "Lost fine equals replacementcost when pref on and default set");
659 ok( !$procfee, "No processing fee if no processing fee");
661 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber3, 0, "Perdedor");
662 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, accounttype => 'L' });
663 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, accounttype => 'PF' });
664 ok( !$lostfine, "No lost fine if no replacementcost and default not set when pref on");
665 is( $procfee->amount, "8.160000", "Processing fee if processing fee");
666 is( $procfee->issue_id, $cli_issue_id_3, "Processing fee issue id is correct" );
667 $procfee->delete();
668 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber3, 6.12, "Perdedor");
669 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, accounttype => 'L' });
670 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, accounttype => 'PF' });
671 is( $lostfine->amount, "6.120000", "Lost fine equals replacementcost when pref on and no default set");
672 is( $procfee->amount, "8.160000", "Processing fee if processing fee");
673 is( $procfee->issue_id, $cli_issue_id_3, "Processing fee issue id is correct" );
675 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 0, "Perdedor");
676 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'L' });
677 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'PF' });
678 is( $lostfine->amount, "4.080000", "Lost fine is default if no replacementcost but default set when pref on");
679 is( $procfee->amount, "2.040000", "Processing fee if processing fee");
680 is( $procfee->issue_id, $cli_issue_id_4, "Processing fee issue id is correct" );
681 $lostfine->delete();
682 $procfee->delete();
683 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 6.12, "Perdedor");
684 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'L' });
685 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'PF' });
686 is( $lostfine->amount, "6.120000", "Lost fine equals replacementcost when pref on and default set");
687 is( $procfee->amount, "2.040000", "Processing fee if processing fee");
688 is( $procfee->issue_id, $cli_issue_id_4, "Processing fee issue id is correct" );
689 $lostfine->delete();
690 $procfee->delete();
693 subtest "basic fields tests" => sub {
694 plan tests => 12;
696 t::lib::Mocks::mock_preference('ProcessingFeeNote', 'Test Note');
697 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, '1.99', "Perdedor");
699 # Lost Item Fee
700 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'L' });
701 ok($lostfine, "Lost fine created");
702 is($lostfine->manager_id, $staff_id, "Lost fine manager_id set correctly");
703 is($lostfine->issue_id, $cli_issue_id_4, "Lost fine issue_id set correctly");
704 is($lostfine->description, "Perdedor", "Lost fine issue_id set correctly");
705 is($lostfine->note, '', "Lost fine does not contain a note");
706 is($lostfine->branchcode, $branchcode, "Lost fine branchcode set correctly");
708 # Processing Fee
709 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'PF' });
710 ok($procfee, "Processing fee created");
711 is($procfee->manager_id, $staff_id, "Processing fee manager_id set correctly");
712 is($procfee->issue_id, $cli_issue_id_4, "Processing fee issue_id set correctly");
713 is($procfee->description, "Perdedor", "Processing fee issue_id set correctly");
714 is($procfee->note, C4::Context->preference("ProcessingFeeNote"), "Processing fee contains note matching ProcessingFeeNote");
715 is($procfee->branchcode, $branchcode, "Processing fee branchcode set correctly");
716 $lostfine->delete();
717 $procfee->delete();
720 subtest "FinesLog tests" => sub {
721 plan tests => 2;
723 my $action_logs = $schema->resultset('ActionLog')->search()->count;
725 t::lib::Mocks::mock_preference( 'FinesLog', 0 );
726 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 0, "Perdedor");
727 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'L' });
728 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'PF' });
729 is( $schema->resultset('ActionLog')->count(), $action_logs + 0, 'No logs were added' );
730 $lostfine->delete();
731 $procfee->delete();
733 t::lib::Mocks::mock_preference( 'FinesLog', 1 );
734 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 0, "Perdedor");
735 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'L' });
736 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'PF' });
737 is( $schema->resultset('ActionLog')->count(), $action_logs + 2, 'Logs were added' );
738 $lostfine->delete();
739 $procfee->delete();
742 # Cleanup - this must be replaced with a transaction per subtest
743 Koha::Patrons->find($cli_borrowernumber)->checkouts->delete;
746 subtest "Koha::Account::non_issues_charges tests" => sub {
747 plan tests => 21;
749 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
751 my $today = dt_from_string;
752 my $res = 3;
753 my $rent = 5;
754 my $manual = 7;
755 Koha::Account::Line->new(
757 borrowernumber => $patron->borrowernumber,
758 date => $today,
759 description => 'a Res fee',
760 accounttype => 'Res',
761 amountoutstanding => $res,
763 )->store;
764 Koha::Account::Line->new(
766 borrowernumber => $patron->borrowernumber,
767 date => $today,
768 description => 'a Rental fee',
769 accounttype => 'Rent',
770 amountoutstanding => $rent,
772 )->store;
773 Koha::Account::Line->new(
775 borrowernumber => $patron->borrowernumber,
776 date => $today,
777 description => 'a Manual invoice fee',
778 accounttype => 'Copie',
779 amountoutstanding => $manual,
781 )->store;
782 Koha::AuthorisedValue->new(
784 category => 'MANUAL_INV',
785 authorised_value => 'Copie',
786 lib => 'Fee for copie',
788 )->store;
790 my $account = $patron->account;
792 t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge', 0 );
793 t::lib::Mocks::mock_preference( 'RentalsInNoissuesCharge', 0 );
794 t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge', 0 );
795 my ( $total, $non_issues_charges ) = ( $account->balance, $account->non_issues_charges );
796 my $other_charges = $total - $non_issues_charges;
798 $account->balance,
799 $res + $rent + $manual,
800 'Total charges should be Res + Rent + Manual'
802 is( $non_issues_charges, 0,
803 'If 0|0|0 there should not have non issues charges' );
804 is( $other_charges, 15, 'If 0|0|0 there should only have other charges' );
806 t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge', 0 );
807 t::lib::Mocks::mock_preference( 'RentalsInNoissuesCharge', 0 );
808 t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge', 1 );
809 ( $total, $non_issues_charges ) = ( $account->balance, $account->non_issues_charges );
810 $other_charges = $total - $non_issues_charges;
812 $total,
813 $res + $rent + $manual,
814 'Total charges should be Res + Rent + Manual'
816 is( $non_issues_charges, $manual,
817 'If 0|0|1 Only Manual should be a non issue charge' );
819 $other_charges,
820 $res + $rent,
821 'If 0|0|1 Res + Rent should be other charges'
824 t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge', 0 );
825 t::lib::Mocks::mock_preference( 'RentalsInNoissuesCharge', 1 );
826 t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge', 0 );
827 ( $total, $non_issues_charges ) = ( $account->balance, $account->non_issues_charges );
828 $other_charges = $total - $non_issues_charges;
830 $total,
831 $res + $rent + $manual,
832 'Total charges should be Res + Rent + Manual'
834 is( $non_issues_charges, $rent,
835 'If 0|1|0 Only Rental should be a non issue charge' );
837 $other_charges,
838 $res + $manual,
839 'If 0|1|0 Rent + Manual should be other charges'
842 t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge', 0 );
843 t::lib::Mocks::mock_preference( 'RentalsInNoissuesCharge', 1 );
844 t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge', 1 );
845 ( $total, $non_issues_charges ) = ( $account->balance, $account->non_issues_charges );
846 $other_charges = $total - $non_issues_charges;
848 $total,
849 $res + $rent + $manual,
850 'Total charges should be Res + Rent + Manual'
853 $non_issues_charges,
854 $rent + $manual,
855 'If 0|1|1 Rent + Manual should be non issues charges'
857 is( $other_charges, $res, 'If 0|1|1 there should only have other charges' );
859 t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge', 1 );
860 t::lib::Mocks::mock_preference( 'RentalsInNoissuesCharge', 0 );
861 t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge', 0 );
862 ( $total, $non_issues_charges ) = ( $account->balance, $account->non_issues_charges );
863 $other_charges = $total - $non_issues_charges;
865 $total,
866 $res + $rent + $manual,
867 'Total charges should be Res + Rent + Manual'
869 is( $non_issues_charges, $res,
870 'If 1|0|0 Only Res should be non issues charges' );
872 $other_charges,
873 $rent + $manual,
874 'If 1|0|0 Rent + Manual should be other charges'
877 t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge', 1 );
878 t::lib::Mocks::mock_preference( 'RentalsInNoissuesCharge', 1 );
879 t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge', 0 );
880 ( $total, $non_issues_charges ) = ( $account->balance, $account->non_issues_charges );
881 $other_charges = $total - $non_issues_charges;
883 $total,
884 $res + $rent + $manual,
885 'Total charges should be Res + Rent + Manual'
888 $non_issues_charges,
889 $res + $rent,
890 'If 1|1|0 Res + Rent should be non issues charges'
892 is( $other_charges, $manual,
893 'If 1|1|0 Only Manual should be other charges' );
895 t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge', 1 );
896 t::lib::Mocks::mock_preference( 'RentalsInNoissuesCharge', 1 );
897 t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge', 1 );
898 ( $total, $non_issues_charges ) = ( $account->balance, $account->non_issues_charges );
899 $other_charges = $total - $non_issues_charges;
901 $total,
902 $res + $rent + $manual,
903 'Total charges should be Res + Rent + Manual'
906 $non_issues_charges,
907 $res + $rent + $manual,
908 'If 1|1|1 Res + Rent + Manual should be non issues charges'
910 is( $other_charges, 0, 'If 1|1|1 there should not have any other charges' );
913 subtest "Koha::Account::non_issues_charges tests" => sub {
914 plan tests => 9;
916 my $patron = $builder->build_object(
918 class => "Koha::Patrons",
919 value => {
920 firstname => 'Test',
921 surname => 'Patron',
922 categorycode => $categorycode,
923 branchcode => $branchcode
928 my $debit = Koha::Account::Line->new({ borrowernumber => $patron->id, date => '1900-01-01', amountoutstanding => 0 })->store();
929 my $credit = Koha::Account::Line->new({ borrowernumber => $patron->id, date => '1900-01-01', amountoutstanding => -5 })->store();
930 my $offset = Koha::Account::Offset->new({ credit_id => $credit->id, debit_id => $debit->id, type => 'Payment', amount => 0 })->store();
931 purge_zero_balance_fees( 1 );
932 my $debit_2 = Koha::Account::Lines->find( $debit->id );
933 my $credit_2 = Koha::Account::Lines->find( $credit->id );
934 ok( $debit_2, 'Debit was correctly not deleted when credit has balance' );
935 ok( $credit_2, 'Credit was correctly not deleted when credit has balance' );
936 is( Koha::Account::Lines->count({ borrowernumber => $patron->id }), 2, "The 2 account lines still exists" );
938 $debit = Koha::Account::Line->new({ borrowernumber => $patron->id, date => '1900-01-01', amountoutstanding => 5 })->store();
939 $credit = Koha::Account::Line->new({ borrowernumber => $patron->id, date => '1900-01-01', amountoutstanding => 0 })->store();
940 $offset = Koha::Account::Offset->new({ credit_id => $credit->id, debit_id => $debit->id, type => 'Payment', amount => 0 })->store();
941 purge_zero_balance_fees( 1 );
942 $debit_2 = $credit_2 = undef;
943 $debit_2 = Koha::Account::Lines->find( $debit->id );
944 $credit_2 = Koha::Account::Lines->find( $credit->id );
945 ok( $debit_2, 'Debit was correctly not deleted when debit has balance' );
946 ok( $credit_2, 'Credit was correctly not deleted when debit has balance' );
947 is( Koha::Account::Lines->count({ borrowernumber => $patron->id }), 2 + 2, "The 2 + 2 account lines still exists" );
949 $debit = Koha::Account::Line->new({ borrowernumber => $patron->id, date => '1900-01-01', amountoutstanding => 0 })->store();
950 $credit = Koha::Account::Line->new({ borrowernumber => $patron->id, date => '1900-01-01', amountoutstanding => 0 })->store();
951 $offset = Koha::Account::Offset->new({ credit_id => $credit->id, debit_id => $debit->id, type => 'Payment', amount => 0 })->store();
952 purge_zero_balance_fees( 1 );
953 $debit_2 = Koha::Account::Lines->find( $debit->id );
954 $credit_2 = Koha::Account::Lines->find( $credit->id );
955 ok( !$debit_2, 'Debit was correctly deleted' );
956 ok( !$credit_2, 'Credit was correctly deleted' );
957 is( Koha::Account::Lines->count({ borrowernumber => $patron->id }), 2 + 2, "The 2 + 2 account lines still exists, the last 2 have been deleted ok" );
960 subtest "Koha::Account::Line::void tests" => sub {
962 plan tests => 15;
964 # Create a borrower
965 my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
966 my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode };
968 my $borrower = Koha::Patron->new( {
969 cardnumber => 'dariahall',
970 surname => 'Hall',
971 firstname => 'Daria',
972 } );
973 $borrower->categorycode( $categorycode );
974 $borrower->branchcode( $branchcode );
975 $borrower->store;
977 my $account = Koha::Account->new({ patron_id => $borrower->id });
979 my $line1 = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amount => 10, amountoutstanding => 10 })->store();
980 my $line2 = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amount => 20, amountoutstanding => 20 })->store();
982 is( $account->balance(), 30, "Account balance is 30" );
983 is( $line1->amountoutstanding, 10, 'First fee has amount outstanding of 10' );
984 is( $line2->amountoutstanding, 20, 'Second fee has amount outstanding of 20' );
986 my $id = $account->pay(
988 lines => [$line1, $line2],
989 amount => 30,
993 my $account_payment = Koha::Account::Lines->find( $id );
995 is( $account->balance(), 0, "Account balance is 0" );
997 $line1->_result->discard_changes();
998 $line2->_result->discard_changes();
999 is( $line1->amountoutstanding+0, 0, 'First fee has amount outstanding of 0' );
1000 is( $line2->amountoutstanding+0, 0, 'Second fee has amount outstanding of 0' );
1002 my $ret = $account_payment->void();
1004 is( ref($ret), 'Koha::Account::Line', 'Void returns the account line' );
1005 is( $account->balance(), 30, "Account balance is again 30" );
1007 $account_payment->_result->discard_changes();
1008 $line1->_result->discard_changes();
1009 $line2->_result->discard_changes();
1011 is( $account_payment->accounttype, 'VOID', 'Voided payment accounttype is VOID' );
1012 is( $account_payment->amount+0, 0, 'Voided payment amount is 0' );
1013 is( $account_payment->amountoutstanding+0, 0, 'Voided payment amount outstanding is 0' );
1015 is( $line1->amountoutstanding+0, 10, 'First fee again has amount outstanding of 10' );
1016 is( $line2->amountoutstanding+0, 20, 'Second fee again has amount outstanding of 20' );
1018 # Accountlines that are not credits should be un-voidable
1019 my $line1_pre = $line1->unblessed();
1020 $ret = $line1->void();
1021 $line1->_result->discard_changes();
1022 my $line1_post = $line1->unblessed();
1023 is( $ret, undef, 'Attempted void on non-credit returns undef' );
1024 is_deeply( $line1_pre, $line1_post, 'Non-credit account line cannot be voided' )
1027 subtest "Koha::Account::Offset credit & debit tests" => sub {
1029 plan tests => 4;
1031 # Create a borrower
1032 my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
1033 my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode };
1035 my $borrower = Koha::Patron->new( {
1036 cardnumber => 'kyliehall',
1037 surname => 'Hall',
1038 firstname => 'Kylie',
1039 } );
1040 $borrower->categorycode( $categorycode );
1041 $borrower->branchcode( $branchcode );
1042 $borrower->store;
1044 my $account = Koha::Account->new({ patron_id => $borrower->id });
1046 my $line1 = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amount => 10, amountoutstanding => 10 })->store();
1047 my $line2 = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amount => 20, amountoutstanding => 20 })->store();
1049 my $id = $account->pay(
1051 lines => [$line1, $line2],
1052 amount => 30,
1056 # Test debit and credit methods for Koha::Account::Offset
1057 my $account_offset = Koha::Account::Offsets->find( { credit_id => $id, debit_id => $line1->id } );
1058 is( $account_offset->debit->id, $line1->id, "Koha::Account::Offset->debit gets correct accountline" );
1059 is( $account_offset->credit->id, $id, "Koha::Account::Offset->credit gets correct accountline" );
1061 $account_offset = Koha::Account::Offset->new(
1063 credit_id => undef,
1064 debit_id => undef,
1065 type => 'Payment',
1066 amount => 0,
1068 )->store();
1070 is( $account_offset->debit, undef, "Koha::Account::Offset->debit returns undef if no associated debit" );
1071 is( $account_offset->credit, undef, "Koha::Account::Offset->credit returns undef if no associated credit" );
1074 subtest "Payment notice tests" => sub {
1076 plan tests => 8;
1078 Koha::Account::Lines->delete();
1079 Koha::Patrons->delete();
1080 Koha::Notice::Messages->delete();
1081 # Create a borrower
1082 my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
1083 my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode };
1085 my $borrower = Koha::Patron->new(
1087 cardnumber => 'chelseahall',
1088 surname => 'Hall',
1089 firstname => 'Chelsea',
1090 email => 'chelsea@example.com',
1091 categorycode => $categorycode,
1092 branchcode => $branchcode,
1094 )->store();
1096 my $account = Koha::Account->new({ patron_id => $borrower->id });
1098 my $line = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amountoutstanding => 27 })->store();
1100 my $letter = Koha::Notice::Templates->find( { code => 'ACCOUNT_PAYMENT' } );
1101 $letter->content('[%- USE Price -%]A payment of [% credit.amount * -1 | $Price %] has been applied to your account.');
1102 $letter->store();
1104 t::lib::Mocks::mock_preference('UseEmailReceipts', '0');
1106 my $id = $account->pay( { amount => 1 } );
1107 is( Koha::Notice::Messages->search()->count(), 0, 'Notice for payment not sent if UseEmailReceipts is disabled' );
1109 $id = $account->pay( { amount => 1, type => 'writeoff' } );
1110 is( Koha::Notice::Messages->search()->count(), 0, 'Notice for writeoff not sent if UseEmailReceipts is disabled' );
1112 t::lib::Mocks::mock_preference('UseEmailReceipts', '1');
1114 $id = $account->pay( { amount => 12 } );
1115 my $notice = Koha::Notice::Messages->search()->next();
1116 is( $notice->subject, 'Account payment', 'Notice subject is correct for payment' );
1117 is( $notice->letter_code, 'ACCOUNT_PAYMENT', 'Notice letter code is correct for payment' );
1118 is( $notice->content, 'A payment of 12.00 has been applied to your account.', 'Notice content is correct for payment' );
1119 $notice->delete();
1121 $letter = Koha::Notice::Templates->find( { code => 'ACCOUNT_WRITEOFF' } );
1122 $letter->content('[%- USE Price -%]A writeoff of [% credit.amount * -1 | $Price %] has been applied to your account.');
1123 $letter->store();
1125 $id = $account->pay( { amount => 13, type => 'writeoff' } );
1126 $notice = Koha::Notice::Messages->search()->next();
1127 is( $notice->subject, 'Account writeoff', 'Notice subject is correct for payment' );
1128 is( $notice->letter_code, 'ACCOUNT_WRITEOFF', 'Notice letter code is correct for writeoff' );
1129 is( $notice->content, 'A writeoff of 13.00 has been applied to your account.', 'Notice content is correct for writeoff' );