Bug 23049: (QA follow-up) Fix for missing types in test inserts
[koha.git] / t / db_dependent / Accounts.t
blob0611f846e3940fa486a0a9adac98809e08692c4f
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 => 33;
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::DebitTypes;
30 use Koha::Account::Lines;
31 use Koha::Account::Offsets;
32 use Koha::Notice::Messages;
33 use Koha::Notice::Templates;
34 use Koha::DateUtils qw( dt_from_string );
36 use C4::Circulation qw( MarkIssueReturned );
38 BEGIN {
39 use_ok('C4::Accounts');
40 use_ok('Koha::Object');
41 use_ok('Koha::Patron');
42 use_ok('Data::Dumper');
45 can_ok( 'C4::Accounts',
46 qw(
47 chargelostitem
48 manualinvoice
49 purge_zero_balance_fees )
52 my $schema = Koha::Database->new->schema;
53 $schema->storage->txn_begin;
54 my $dbh = C4::Context->dbh;
56 my $builder = t::lib::TestBuilder->new;
57 my $library = $builder->build( { source => 'Branch' } );
59 $dbh->do(q|DELETE FROM accountlines|);
60 $dbh->do(q|DELETE FROM issues|);
61 $dbh->do(q|DELETE FROM borrowers|);
63 my $branchcode = $library->{branchcode};
64 my $borrower_number;
66 my $context = new Test::MockModule('C4::Context');
67 $context->mock( 'userenv', sub {
68 return {
69 flags => 1,
70 id => 'my_userid',
71 branch => $branchcode,
73 });
74 $context->mock( 'interface', sub { return "commandline" } );
75 my $userenv_branchcode = $branchcode;
77 # Test manualinvoice
78 my $itemtype = $builder->build( { source => 'Itemtype' } );
79 my $item = $builder->build( { source => 'Item', value => { itype => $itemtype->{itemtype} } } );
80 my $patron = $builder->build( { source => 'Borrower' } );
81 my $amount = '5.000000';
82 my $description = "Test fee!";
83 my $type = 'LOST';
84 my $note = 'Test note!';
85 warning_like {
86 C4::Accounts::manualinvoice( $patron->{borrowernumber},
87 $item->{itemnumber}, $description, $type, $amount, $note )
89 qr/C4::Accounts::manualinvoice is deprecated in favor of Koha::Account->add_debit/,
90 "deprecation warning received for manualinvoice";
91 my ($accountline) = Koha::Account::Lines->search(
93 borrowernumber => $patron->{borrowernumber}
96 is( $accountline->debit_type_code, $type, 'Debit type set correctly for manualinvoice' );
97 is( $accountline->amount, $amount, 'Accountline amount set correctly for manualinvoice' );
98 ok( $accountline->description =~ /^$description/, 'Accountline description set correctly for manualinvoice' );
99 is( $accountline->note, $note, 'Accountline note set correctly for manualinvoice' );
100 is( $accountline->branchcode, $branchcode, 'Accountline branchcode set correctly for manualinvoice' );
102 $dbh->do(q|DELETE FROM accountlines|);
104 # Testing purge_zero_balance_fees
106 # The 3rd value in the insert is 'days ago' --
107 # 0 => today
108 # 1 => yesterday
109 # etc.
111 my $sth = $dbh->prepare(
112 "INSERT INTO accountlines (
113 borrowernumber,
114 amountoutstanding,
115 date,
116 description,
117 interface,
118 accounttype,
119 debit_type_code
121 VALUES ( ?, ?, (select date_sub(CURRENT_DATE, INTERVAL ? DAY) ), ?, ?, ?, ? )"
124 my $days = 5;
126 my @test_data = (
127 { amount => 0 , days_ago => 0 , description =>'purge_zero_balance_fees should not delete 0 balance fees with date today' , delete => 0 } ,
128 { amount => 0 , days_ago => $days - 1 , description =>'purge_zero_balance_fees should not delete 0 balance fees with date before threshold day' , delete => 0 } ,
129 { amount => 0 , days_ago => $days , description =>'purge_zero_balance_fees should not delete 0 balance fees with date on threshold day' , delete => 0 } ,
130 { amount => 0 , days_ago => $days + 1 , description =>'purge_zero_balance_fees should delete 0 balance fees with date after threshold day' , delete => 1 } ,
131 { amount => undef , days_ago => $days + 1 , description =>'purge_zero_balance_fees should delete NULL balance fees with date after threshold day' , delete => 1 } ,
132 { amount => 5 , days_ago => $days - 1 , description =>'purge_zero_balance_fees should not delete fees with positive amout owed before threshold day' , delete => 0 } ,
133 { amount => 5 , days_ago => $days , description =>'purge_zero_balance_fees should not delete fees with positive amout owed on threshold day' , delete => 0 } ,
134 { amount => 5 , days_ago => $days + 1 , description =>'purge_zero_balance_fees should not delete fees with positive amout owed after threshold day' , delete => 0 } ,
135 { amount => -5 , days_ago => $days - 1 , description =>'purge_zero_balance_fees should not delete fees with negative amout owed before threshold day' , delete => 0 } ,
136 { amount => -5 , days_ago => $days , description =>'purge_zero_balance_fees should not delete fees with negative amout owed on threshold day' , delete => 0 } ,
137 { amount => -5 , days_ago => $days + 1 , description =>'purge_zero_balance_fees should not delete fees with negative amout owed after threshold day' , delete => 0 }
139 my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
140 my $borrower = Koha::Patron->new( { firstname => 'Test', surname => 'Patron', categorycode => $categorycode, branchcode => $branchcode } )->store();
142 for my $data ( @test_data ) {
143 $sth->execute($borrower->borrowernumber, $data->{amount}, $data->{days_ago}, $data->{description}, 'commandline', $data->{amount} > 0 ? 'W' : undef, $data->{amount} >= 0 ? undef : 'OVERDUE' );
146 purge_zero_balance_fees( $days );
148 $sth = $dbh->prepare(
149 "select count(*) = 0 as deleted
150 from accountlines
151 where description = ?"
155 sub is_delete_correct {
156 my $should_delete = shift;
157 my $description = shift;
158 $sth->execute( $description );
159 my $test = $sth->fetchrow_hashref();
160 is( $test->{deleted}, $should_delete, $description )
163 for my $data (@test_data) {
164 is_delete_correct( $data->{delete}, $data->{description});
167 $dbh->do(q|DELETE FROM accountlines|);
169 subtest "Koha::Account::pay tests" => sub {
171 plan tests => 14;
173 # Create a borrower
174 my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
175 my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode };
177 my $borrower = Koha::Patron->new( {
178 cardnumber => '1234567890',
179 surname => 'McFly',
180 firstname => 'Marty',
181 } );
182 $borrower->categorycode( $categorycode );
183 $borrower->branchcode( $branchcode );
184 $borrower->store;
186 my $account = Koha::Account->new({ patron_id => $borrower->id });
188 my $line1 = $account->add_debit({ type => 'ACCOUNT', amount => 100, interface => 'commandline' });
189 my $line2 = $account->add_debit({ type => 'ACCOUNT', amount => 200, interface => 'commandline' });
191 $sth = $dbh->prepare("SELECT count(*) FROM accountlines");
192 $sth->execute;
193 my $count = $sth->fetchrow_array;
194 is($count, 2, 'There is 2 lines as expected');
196 # There is $100 in the account
197 $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
198 my $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
199 my $amountleft = 0;
200 for my $line ( @$amountoutstanding ) {
201 $amountleft += $line;
203 is($amountleft, 300, 'The account has 300$ as expected' );
205 # We make a $20 payment
206 my $borrowernumber = $borrower->borrowernumber;
207 my $data = '20.00';
208 my $payment_note = '$20.00 payment note';
209 my $id = $account->pay( { amount => $data, note => $payment_note, payment_type => "TEST_TYPE" } );
211 my $accountline = Koha::Account::Lines->find( $id );
212 is( $accountline->payment_type, "TEST_TYPE", "Payment type passed into pay is set in account line correctly" );
214 # There is now $280 in the account
215 $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
216 $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
217 $amountleft = 0;
218 for my $line ( @$amountoutstanding ) {
219 $amountleft += $line;
221 is($amountleft, 280, 'The account has $280 as expected' );
223 # Is the payment note well registered
224 $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
225 $sth->execute($borrower->borrowernumber);
226 my $note = $sth->fetchrow_array;
227 is($note,'$20.00 payment note', '$20.00 payment note is registered');
229 # We make a -$30 payment (a NEGATIVE payment)
230 $data = '-30.00';
231 $payment_note = '-$30.00 payment note';
232 $account->pay( { amount => $data, note => $payment_note } );
234 # There is now $310 in the account
235 $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
236 $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
237 $amountleft = 0;
238 for my $line ( @$amountoutstanding ) {
239 $amountleft += $line;
241 is($amountleft, 310, 'The account has $310 as expected' );
242 # Is the payment note well registered
243 $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
244 $sth->execute($borrower->borrowernumber);
245 $note = $sth->fetchrow_array;
246 is($note,'-$30.00 payment note', '-$30.00 payment note is registered');
248 #We make a $150 payment ( > 1stLine )
249 $data = '150.00';
250 $payment_note = '$150.00 payment note';
251 $account->pay( { amount => $data, note => $payment_note } );
253 # There is now $160 in the account
254 $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
255 $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
256 $amountleft = 0;
257 for my $line ( @$amountoutstanding ) {
258 $amountleft += $line;
260 is($amountleft, 160, 'The account has $160 as expected' );
262 # Is the payment note well registered
263 $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
264 $sth->execute($borrower->borrowernumber);
265 $note = $sth->fetchrow_array;
266 is($note,'$150.00 payment note', '$150.00 payment note is registered');
268 #We make a $200 payment ( > amountleft )
269 $data = '200.00';
270 $payment_note = '$200.00 payment note';
271 $account->pay( { amount => $data, note => $payment_note } );
273 # There is now -$40 in the account
274 $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
275 $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
276 $amountleft = 0;
277 for my $line ( @$amountoutstanding ) {
278 $amountleft += $line;
280 is($amountleft, -40, 'The account has -$40 as expected, (credit situation)' );
282 # Is the payment note well registered
283 $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
284 $sth->execute($borrower->borrowernumber);
285 $note = $sth->fetchrow_array;
286 is($note,'$200.00 payment note', '$200.00 payment note is registered');
288 my $line3 = $account->add_debit({ type => 'ACCOUNT', amount => 42, interface => 'commandline' });
289 my $payment_id = $account->pay( { lines => [$line3], amount => 42 } );
290 my $payment = Koha::Account::Lines->find( $payment_id );
291 is( $payment->amount(), '-42.000000', "Payment paid the specified fine" );
292 $line3 = Koha::Account::Lines->find( $line3->id );
293 is( $line3->amountoutstanding, '0.000000', "Specified fine is paid" );
294 is( $payment->branchcode, undef, 'branchcode passed, then undef' );
297 subtest "Koha::Account::pay particular line tests" => sub {
299 plan tests => 5;
301 # Create a borrower
302 my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
303 my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode };
305 my $borrower = Koha::Patron->new( {
306 cardnumber => 'kylemhall',
307 surname => 'Hall',
308 firstname => 'Kyle',
309 } );
310 $borrower->categorycode( $categorycode );
311 $borrower->branchcode( $branchcode );
312 $borrower->store;
314 my $account = Koha::Account->new({ patron_id => $borrower->id });
316 my $line1 = $account->add_debit({ type => 'ACCOUNT', amount => 1, interface => 'commandline' });
317 my $line2 = $account->add_debit({ type => 'ACCOUNT', amount => 2, interface => 'commandline' });
318 my $line3 = $account->add_debit({ type => 'ACCOUNT', amount => 3, interface => 'commandline' });
319 my $line4 = $account->add_debit({ type => 'ACCOUNT', amount => 4, interface => 'commandline' });
321 is( $account->balance(), 10, "Account balance is 10" );
323 $account->pay(
325 lines => [$line2, $line3, $line4],
326 amount => 4,
330 $_->_result->discard_changes foreach ( $line1, $line2, $line3, $line4 );
332 # Line1 is not paid at all, as it was not passed in the lines param
333 is( $line1->amountoutstanding, "1.000000", "Line 1 was not paid" );
334 # Line2 was paid in full, as it was the first in the lines list
335 is( $line2->amountoutstanding, "0.000000", "Line 2 was paid in full" );
336 # Line3 was paid partially, as the remaining balance did not cover it entirely
337 is( $line3->amountoutstanding, "1.000000", "Line 3 was paid to 1.00" );
338 # Line4 was not paid at all, as the payment was all used up by that point
339 is( $line4->amountoutstanding, "4.000000", "Line 4 was not paid" );
342 subtest "Koha::Account::pay writeoff tests" => sub {
344 plan tests => 5;
346 # Create a borrower
347 my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
348 my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode };
350 my $borrower = Koha::Patron->new( {
351 cardnumber => 'chelseahall',
352 surname => 'Hall',
353 firstname => 'Chelsea',
354 } );
355 $borrower->categorycode( $categorycode );
356 $borrower->branchcode( $branchcode );
357 $borrower->store;
359 my $account = Koha::Account->new({ patron_id => $borrower->id });
361 my $line = $account->add_debit({ type => 'ACCOUNT', amount => 42, interface => 'commandline' });
363 is( $account->balance(), 42, "Account balance is 42" );
365 my $id = $account->pay(
367 lines => [$line],
368 amount => 42,
369 type => 'writeoff',
373 $line->_result->discard_changes();
375 is( $line->amountoutstanding, "0.000000", "Line was written off" );
377 my $writeoff = Koha::Account::Lines->find( $id );
379 is( $writeoff->accounttype, 'W', 'Type is correct for writeoff' );
380 is( $writeoff->description, 'Writeoff', 'Description is correct' );
381 is( $writeoff->amount, '-42.000000', 'Amount is correct' );
384 subtest "More Koha::Account::pay tests" => sub {
386 plan tests => 8;
388 # Create a borrower
389 my $category = $builder->build({ source => 'Category' })->{ categorycode };
390 my $branch = $builder->build({ source => 'Branch' })->{ branchcode };
391 $branchcode = $branch;
392 my $borrowernumber = $builder->build({
393 source => 'Borrower',
394 value => { categorycode => $category,
395 branchcode => $branch }
396 })->{ borrowernumber };
398 my $amount = 100;
399 my $accountline = $builder->build(
401 source => 'Accountline',
402 value => {
403 borrowernumber => $borrowernumber,
404 amount => $amount,
405 amountoutstanding => $amount,
406 accounttype => undef,
411 my $rs = $schema->resultset('Accountline')->search({
412 borrowernumber => $borrowernumber
415 is( $rs->count(), 1, 'Accountline created' );
417 my $account = Koha::Account->new( { patron_id => $borrowernumber } );
418 my $line = Koha::Account::Lines->find( $accountline->{ accountlines_id } );
419 # make the full payment
420 $account->pay({ lines => [$line], amount => $amount, library_id => $branch, note => 'A payment note' });
422 my $offset = Koha::Account::Offsets->search({ debit_id => $accountline->{accountlines_id} })->next();
423 is( $offset->amount(), '-100.000000', 'Offset amount is -100.00' );
424 is( $offset->type(), 'Payment', 'Offset type is Payment' );
426 my $stat = $schema->resultset('Statistic')->search({
427 branch => $branch,
428 type => 'payment'
429 }, { order_by => { -desc => 'datetime' } })->next();
431 ok( defined $stat, "There's a payment log that matches the branch" );
433 SKIP: {
434 skip "No statistic logged", 4 unless defined $stat;
436 is( $stat->type, 'payment', "Correct statistic type" );
437 is( $stat->branch, $branch, "Correct branch logged to statistics" );
438 is( $stat->borrowernumber, $borrowernumber, "Correct borrowernumber logged to statistics" );
439 is( $stat->value+0, $amount, "Correct amount logged to statistics" );
443 subtest "Even more Koha::Account::pay tests" => sub {
445 plan tests => 8;
447 # Create a borrower
448 my $category = $builder->build({ source => 'Category' })->{ categorycode };
449 my $branch = $builder->build({ source => 'Branch' })->{ branchcode };
450 $branchcode = $branch;
451 my $borrowernumber = $builder->build({
452 source => 'Borrower',
453 value => { categorycode => $category,
454 branchcode => $branch }
455 })->{ borrowernumber };
457 my $amount = 100;
458 my $partialamount = 60;
459 my $accountline = $builder->build(
461 source => 'Accountline',
462 value => {
463 borrowernumber => $borrowernumber,
464 amount => $amount,
465 amountoutstanding => $amount,
466 accounttype => undef,
471 my $rs = $schema->resultset('Accountline')->search({
472 borrowernumber => $borrowernumber
475 is( $rs->count(), 1, 'Accountline created' );
477 my $account = Koha::Account->new( { patron_id => $borrowernumber } );
478 my $line = Koha::Account::Lines->find( $accountline->{ accountlines_id } );
479 # make the full payment
480 $account->pay({ lines => [$line], amount => $partialamount, library_id => $branch, note => 'A payment note' });
482 my $offset = Koha::Account::Offsets->search( { debit_id => $accountline->{ accountlines_id } } )->next();
483 is( $offset->amount, '-60.000000', 'Offset amount is -60.00' );
484 is( $offset->type, 'Payment', 'Offset type is payment' );
486 my $stat = $schema->resultset('Statistic')->search({
487 branch => $branch,
488 type => 'payment'
489 }, { order_by => { -desc => 'datetime' } })->next();
491 ok( defined $stat, "There's a payment log that matches the branch" );
493 SKIP: {
494 skip "No statistic logged", 4 unless defined $stat;
496 is( $stat->type, 'payment', "Correct statistic type" );
497 is( $stat->branch, $branch, "Correct branch logged to statistics" );
498 is( $stat->borrowernumber, $borrowernumber, "Correct borrowernumber logged to statistics" );
499 is( $stat->value+0, $partialamount, "Correct amount logged to statistics" );
503 subtest 'balance' => sub {
504 plan tests => 2;
506 my $patron = $builder->build({source => 'Borrower'});
507 $patron = Koha::Patrons->find( $patron->{borrowernumber} );
508 my $account = $patron->account;
509 is( $account->balance, 0, 'balance should return 0 if the patron does not have fines' );
511 my $accountline_1 = $builder->build(
513 source => 'Accountline',
514 value => {
515 borrowernumber => $patron->borrowernumber,
516 amount => 42,
517 amountoutstanding => 42,
518 accounttype => undef,
522 my $accountline_2 = $builder->build(
524 source => 'Accountline',
525 value => {
526 borrowernumber => $patron->borrowernumber,
527 amount => -13,
528 amountoutstanding => -13,
529 debit_type_code => undef,
534 my $balance = $patron->account->balance;
535 is( int($balance), 29, 'balance should return the correct value');
537 $patron->delete;
540 subtest "C4::Accounts::chargelostitem tests" => sub {
541 plan tests => 3;
543 my $branch = $builder->build( { source => 'Branch' } );
544 my $branchcode = $branch->{branchcode};
546 my $staff = $builder->build( { source => 'Borrower' } );
547 my $staff_id = $staff->{borrowernumber};
549 my $module = Test::MockModule->new('C4::Context');
550 $module->mock(
551 'userenv',
552 sub {
553 return {
554 flags => 1,
555 number => $staff_id,
556 branch => $branchcode,
561 my $itype_no_replace_no_fee = $builder->build({ source => 'Itemtype', value => {
562 rentalcharge => 0,
563 defaultreplacecost => undef,
564 processfee => undef,
565 }});
566 my $itype_replace_no_fee = $builder->build({ source => 'Itemtype', value => {
567 rentalcharge => 0,
568 defaultreplacecost => 16.32,
569 processfee => undef,
570 }});
571 my $itype_no_replace_fee = $builder->build({ source => 'Itemtype', value => {
572 rentalcharge => 0,
573 defaultreplacecost => undef,
574 processfee => 8.16,
575 }});
576 my $itype_replace_fee = $builder->build({ source => 'Itemtype', value => {
577 rentalcharge => 0,
578 defaultreplacecost => 4.08,
579 processfee => 2.04,
580 }});
581 my $cli_borrowernumber = $builder->build({ source => 'Borrower' })->{'borrowernumber'};
582 my $cli_itemnumber1 = $builder->build({ source => 'Item', value => { itype => $itype_no_replace_no_fee->{itemtype} } })->{'itemnumber'};
583 my $cli_itemnumber2 = $builder->build({ source => 'Item', value => { itype => $itype_replace_no_fee->{itemtype} } })->{'itemnumber'};
584 my $cli_itemnumber3 = $builder->build({ source => 'Item', value => { itype => $itype_no_replace_fee->{itemtype} } })->{'itemnumber'};
585 my $cli_itemnumber4 = $builder->build({ source => 'Item', value => { itype => $itype_replace_fee->{itemtype} } })->{'itemnumber'};
587 my $cli_issue_id_1 = $builder->build({ source => 'Issue', value => { borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1 } })->{issue_id};
588 my $cli_issue_id_2 = $builder->build({ source => 'Issue', value => { borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2 } })->{issue_id};
589 my $cli_issue_id_3 = $builder->build({ source => 'Issue', value => { borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3 } })->{issue_id};
590 my $cli_issue_id_4 = $builder->build({ source => 'Issue', value => { borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4 } })->{issue_id};
591 my $cli_issue_id_4X = undef;
593 my $lostfine;
594 my $procfee;
596 subtest "fee application tests" => sub {
597 plan tests => 44;
599 t::lib::Mocks::mock_preference('item-level_itypes', '1');
600 t::lib::Mocks::mock_preference('useDefaultReplacementCost', '0');
602 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber1, 0, "Perdedor");
603 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, debit_type_code => 'LOST' });
604 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, debit_type_code => 'PROCESSING' });
605 ok( !$lostfine, "No lost fine if no replacementcost or default when pref off");
606 ok( !$procfee, "No processing fee if no processing fee");
607 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber1, 6.12, "Perdedor");
608 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, debit_type_code => 'LOST' });
609 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, debit_type_code => 'PROCESSING' });
610 ok( $lostfine->amount == 6.12, "Lost fine equals replacementcost when pref off and no default set");
611 ok( !$procfee, "No processing fee if no processing fee");
612 $lostfine->delete();
614 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber2, 0, "Perdedor");
615 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, debit_type_code => 'LOST' });
616 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, debit_type_code => 'PROCESSING' });
617 ok( !$lostfine, "No lost fine if no replacementcost but default set when pref off");
618 ok( !$procfee, "No processing fee if no processing fee");
619 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber2, 6.12, "Perdedor");
620 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, debit_type_code => 'LOST' });
621 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, debit_type_code => 'PROCESSING' });
622 ok( $lostfine->amount == 6.12 , "Lost fine equals replacementcost when pref off and default set");
623 ok( !$procfee, "No processing fee if no processing fee");
624 $lostfine->delete();
626 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber3, 0, "Perdedor");
627 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, debit_type_code => 'LOST' });
628 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, debit_type_code => 'PROCESSING' });
629 ok( !$lostfine, "No lost fine if no replacementcost and no default set when pref off");
630 ok( $procfee->amount == 8.16, "Processing fee if processing fee");
631 is( $procfee->issue_id, $cli_issue_id_3, "Processing fee issue id is correct" );
632 $procfee->delete();
633 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber3, 6.12, "Perdedor");
634 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, debit_type_code => 'LOST' });
635 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, debit_type_code => 'PROCESSING' });
636 ok( $lostfine->amount == 6.12 , "Lost fine equals replacementcost when pref off and no default set");
637 ok( $procfee->amount == 8.16, "Processing fee if processing fee");
638 is( $procfee->issue_id, $cli_issue_id_3, "Processing fee issue id is correct" );
639 $lostfine->delete();
640 $procfee->delete();
642 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 0, "Perdedor");
643 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'LOST' });
644 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'PROCESSING' });
645 ok( !$lostfine, "No lost fine if no replacementcost but default set when pref off");
646 ok( $procfee->amount == 2.04, "Processing fee if processing fee");
647 is( $procfee->issue_id, $cli_issue_id_4, "Processing fee issue id is correct" );
648 $procfee->delete();
649 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 6.12, "Perdedor");
650 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'LOST' });
651 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'PROCESSING' });
652 ok( $lostfine->amount == 6.12 , "Lost fine equals replacementcost when pref off and default set");
653 ok( $procfee->amount == 2.04, "Processing fee if processing fee");
654 is( $procfee->issue_id, $cli_issue_id_4, "Processing fee issue id is correct" );
655 $lostfine->delete();
656 $procfee->delete();
658 t::lib::Mocks::mock_preference('useDefaultReplacementCost', '1');
660 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber1, 0, "Perdedor");
661 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, debit_type_code => 'LOST' });
662 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, debit_type_code => 'PROCESSING' });
663 ok( !$lostfine, "No lost fine if no replacementcost or default when pref on");
664 ok( !$procfee, "No processing fee if no processing fee");
665 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber1, 6.12, "Perdedor");
666 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, debit_type_code => 'LOST' });
667 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, debit_type_code => 'PROCESSING' });
668 is( $lostfine->amount, "6.120000", "Lost fine equals replacementcost when pref on and no default set");
669 ok( !$procfee, "No processing fee if no processing fee");
671 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber2, 0, "Perdedor");
672 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, debit_type_code => 'LOST' });
673 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, debit_type_code => 'PROCESSING' });
674 is( $lostfine->amount(), "16.320000", "Lost fine is default if no replacementcost but default set when pref on");
675 ok( !$procfee, "No processing fee if no processing fee");
676 $lostfine->delete();
677 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber2, 6.12, "Perdedor");
678 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, debit_type_code => 'LOST' });
679 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, debit_type_code => 'PROCESSING' });
680 is( $lostfine->amount, "6.120000" , "Lost fine equals replacementcost when pref on and default set");
681 ok( !$procfee, "No processing fee if no processing fee");
683 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber3, 0, "Perdedor");
684 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, debit_type_code => 'LOST' });
685 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, debit_type_code => 'PROCESSING' });
686 ok( !$lostfine, "No lost fine if no replacementcost and default not set when pref on");
687 is( $procfee->amount, "8.160000", "Processing fee if processing fee");
688 is( $procfee->issue_id, $cli_issue_id_3, "Processing fee issue id is correct" );
689 $procfee->delete();
690 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber3, 6.12, "Perdedor");
691 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, debit_type_code => 'LOST' });
692 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, debit_type_code => 'PROCESSING' });
693 is( $lostfine->amount, "6.120000", "Lost fine equals replacementcost when pref on and no default set");
694 is( $procfee->amount, "8.160000", "Processing fee if processing fee");
695 is( $procfee->issue_id, $cli_issue_id_3, "Processing fee issue id is correct" );
697 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 0, "Perdedor");
698 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'LOST' });
699 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'PROCESSING' });
700 is( $lostfine->amount, "4.080000", "Lost fine is default if no replacementcost but default set when pref on");
701 is( $procfee->amount, "2.040000", "Processing fee if processing fee");
702 is( $procfee->issue_id, $cli_issue_id_4, "Processing fee issue id is correct" );
703 $lostfine->delete();
704 $procfee->delete();
705 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 6.12, "Perdedor");
706 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'LOST' });
707 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'PROCESSING' });
708 is( $lostfine->amount, "6.120000", "Lost fine equals replacementcost when pref on and default set");
709 is( $procfee->amount, "2.040000", "Processing fee if processing fee");
710 is( $procfee->issue_id, $cli_issue_id_4, "Processing fee issue id is correct" );
711 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 6.12, "Perdedor");
712 my $lostfines = Koha::Account::Lines->search({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'LOST' });
713 my $procfees = Koha::Account::Lines->search({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'PROCESSING' });
714 ok( $lostfines->count == 1 , "Lost fine cannot be double charged for the same issue_id");
715 ok( $procfees->count == 1, "Processing fee cannot be double charged for the same issue_id");
716 MarkIssueReturned($cli_borrowernumber, $cli_itemnumber4);
717 $cli_issue_id_4X = $builder->build({ source => 'Issue', value => { borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4 } })->{issue_id};
718 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 6.12, "Perdedor");
719 $lostfines = Koha::Account::Lines->search({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'LOST' });
720 $procfees = Koha::Account::Lines->search({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'PROCESSING' });
721 ok( $lostfines->count == 2 , "Lost fine can be charged twice for the same item if they are distinct issue_id's");
722 ok( $procfees->count == 2, "Processing fee can be charged twice for the same item if they are distinct issue_id's");
723 $lostfines->delete();
724 $procfees->delete();
727 subtest "basic fields tests" => sub {
728 plan tests => 12;
730 t::lib::Mocks::mock_preference('ProcessingFeeNote', 'Test Note');
731 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, '1.99', "Perdedor");
733 # Lost Item Fee
734 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'LOST' });
735 ok($lostfine, "Lost fine created");
736 is($lostfine->manager_id, $staff_id, "Lost fine manager_id set correctly");
737 is($lostfine->issue_id, $cli_issue_id_4X, "Lost fine issue_id set correctly");
738 is($lostfine->description, "Perdedor", "Lost fine issue_id set correctly");
739 is($lostfine->note, '', "Lost fine does not contain a note");
740 is($lostfine->branchcode, $branchcode, "Lost fine branchcode set correctly");
742 # Processing Fee
743 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'PROCESSING' });
744 ok($procfee, "Processing fee created");
745 is($procfee->manager_id, $staff_id, "Processing fee manager_id set correctly");
746 is($procfee->issue_id, $cli_issue_id_4X, "Processing fee issue_id set correctly");
747 is($procfee->description, "Perdedor", "Processing fee issue_id set correctly");
748 is($procfee->note, C4::Context->preference("ProcessingFeeNote"), "Processing fee contains note matching ProcessingFeeNote");
749 is($procfee->branchcode, $branchcode, "Processing fee branchcode set correctly");
750 $lostfine->delete();
751 $procfee->delete();
754 subtest "FinesLog tests" => sub {
755 plan tests => 2;
757 my $action_logs = $schema->resultset('ActionLog')->search()->count;
759 t::lib::Mocks::mock_preference( 'FinesLog', 0 );
760 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 0, "Perdedor");
761 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'LOST' });
762 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'PROCESSING' });
763 is( $schema->resultset('ActionLog')->count(), $action_logs + 0, 'No logs were added' );
764 $lostfine->delete();
765 $procfee->delete();
767 t::lib::Mocks::mock_preference( 'FinesLog', 1 );
768 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 0, "Perdedor");
769 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'LOST' });
770 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'PROCESSING' });
771 is( $schema->resultset('ActionLog')->count(), $action_logs + 2, 'Logs were added' );
772 $lostfine->delete();
773 $procfee->delete();
776 # Cleanup - this must be replaced with a transaction per subtest
777 Koha::Patrons->find($cli_borrowernumber)->checkouts->delete;
780 subtest "Koha::Account::non_issues_charges tests" => sub {
781 plan tests => 21;
783 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
784 my $account = $patron->account;
786 my $today = dt_from_string;
787 my $res = 3;
788 my $rent = 5;
789 my $manual = 7;
790 $account->add_debit(
792 description => 'a Res fee',
793 type => 'RESERVE',
794 amount => $res,
795 interface => 'commandline'
798 $account->add_debit(
800 description => 'a Rental fee',
801 type => 'RENT',
802 amount => $rent,
803 interface => 'commandline'
806 Koha::Account::DebitTypes->find_or_create(
808 code => 'Copie',
809 description => 'Fee for copie',
810 is_system => 0
812 )->store;
813 Koha::Account::Line->new(
815 borrowernumber => $patron->borrowernumber,
816 date => $today,
817 description => 'a Manual invoice fee',
818 debit_type_code => 'Copie',
819 amountoutstanding => $manual,
820 interface => 'commandline'
822 )->store;
825 t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge', 0 );
826 t::lib::Mocks::mock_preference( 'RentalsInNoissuesCharge', 0 );
827 t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge', 0 );
828 my ( $total, $non_issues_charges ) = ( $account->balance, $account->non_issues_charges );
829 my $other_charges = $total - $non_issues_charges;
831 $account->balance,
832 $res + $rent + $manual,
833 'Total charges should be Res + Rent + Manual'
835 is( $non_issues_charges, 0,
836 'If 0|0|0 there should not have non issues charges' );
837 is( $other_charges, 15, 'If 0|0|0 there should only have other charges' );
839 t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge', 0 );
840 t::lib::Mocks::mock_preference( 'RentalsInNoissuesCharge', 0 );
841 t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge', 1 );
842 ( $total, $non_issues_charges ) = ( $account->balance, $account->non_issues_charges );
843 $other_charges = $total - $non_issues_charges;
845 $total,
846 $res + $rent + $manual,
847 'Total charges should be Res + Rent + Manual'
849 is( $non_issues_charges, $manual,
850 'If 0|0|1 Only Manual should be a non issue charge' );
852 $other_charges,
853 $res + $rent,
854 'If 0|0|1 Res + Rent should be other charges'
857 t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge', 0 );
858 t::lib::Mocks::mock_preference( 'RentalsInNoissuesCharge', 1 );
859 t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge', 0 );
860 ( $total, $non_issues_charges ) = ( $account->balance, $account->non_issues_charges );
861 $other_charges = $total - $non_issues_charges;
863 $total,
864 $res + $rent + $manual,
865 'Total charges should be Res + Rent + Manual'
867 is( $non_issues_charges, $rent,
868 'If 0|1|0 Only Rental should be a non issue charge' );
870 $other_charges,
871 $res + $manual,
872 'If 0|1|0 Rent + Manual should be other charges'
875 t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge', 0 );
876 t::lib::Mocks::mock_preference( 'RentalsInNoissuesCharge', 1 );
877 t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge', 1 );
878 ( $total, $non_issues_charges ) = ( $account->balance, $account->non_issues_charges );
879 $other_charges = $total - $non_issues_charges;
881 $total,
882 $res + $rent + $manual,
883 'Total charges should be Res + Rent + Manual'
886 $non_issues_charges,
887 $rent + $manual,
888 'If 0|1|1 Rent + Manual should be non issues charges'
890 is( $other_charges, $res, 'If 0|1|1 there should only have other charges' );
892 t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge', 1 );
893 t::lib::Mocks::mock_preference( 'RentalsInNoissuesCharge', 0 );
894 t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge', 0 );
895 ( $total, $non_issues_charges ) = ( $account->balance, $account->non_issues_charges );
896 $other_charges = $total - $non_issues_charges;
898 $total,
899 $res + $rent + $manual,
900 'Total charges should be Res + Rent + Manual'
902 is( $non_issues_charges, $res,
903 'If 1|0|0 Only Res should be non issues charges' );
905 $other_charges,
906 $rent + $manual,
907 'If 1|0|0 Rent + Manual should be other charges'
910 t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge', 1 );
911 t::lib::Mocks::mock_preference( 'RentalsInNoissuesCharge', 1 );
912 t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge', 0 );
913 ( $total, $non_issues_charges ) = ( $account->balance, $account->non_issues_charges );
914 $other_charges = $total - $non_issues_charges;
916 $total,
917 $res + $rent + $manual,
918 'Total charges should be Res + Rent + Manual'
921 $non_issues_charges,
922 $res + $rent,
923 'If 1|1|0 Res + Rent should be non issues charges'
925 is( $other_charges, $manual,
926 'If 1|1|0 Only Manual should be other charges' );
928 t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge', 1 );
929 t::lib::Mocks::mock_preference( 'RentalsInNoissuesCharge', 1 );
930 t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge', 1 );
931 ( $total, $non_issues_charges ) = ( $account->balance, $account->non_issues_charges );
932 $other_charges = $total - $non_issues_charges;
934 $total,
935 $res + $rent + $manual,
936 'Total charges should be Res + Rent + Manual'
939 $non_issues_charges,
940 $res + $rent + $manual,
941 'If 1|1|1 Res + Rent + Manual should be non issues charges'
943 is( $other_charges, 0, 'If 1|1|1 there should not have any other charges' );
946 subtest "Koha::Account::non_issues_charges tests" => sub {
947 plan tests => 9;
949 my $patron = $builder->build_object(
951 class => "Koha::Patrons",
952 value => {
953 firstname => 'Test',
954 surname => 'Patron',
955 categorycode => $categorycode,
956 branchcode => $branchcode
961 my $debit = Koha::Account::Line->new(
963 borrowernumber => $patron->id,
964 date => '1900-01-01',
965 amountoutstanding => 0,
966 interface => 'commandline',
967 debit_type_code => 'LOST'
969 )->store();
970 my $credit = Koha::Account::Line->new(
972 borrowernumber => $patron->id,
973 date => '1900-01-01',
974 amountoutstanding => -5,
975 interface => 'commandline',
976 accounttype => 'Pay'
978 )->store();
979 my $offset = Koha::Account::Offset->new(
981 credit_id => $credit->id,
982 debit_id => $debit->id,
983 type => 'Payment',
984 amount => 0
986 )->store();
987 purge_zero_balance_fees( 1 );
988 my $debit_2 = Koha::Account::Lines->find( $debit->id );
989 my $credit_2 = Koha::Account::Lines->find( $credit->id );
990 ok( $debit_2, 'Debit was correctly not deleted when credit has balance' );
991 ok( $credit_2, 'Credit was correctly not deleted when credit has balance' );
992 is( Koha::Account::Lines->count({ borrowernumber => $patron->id }), 2, "The 2 account lines still exists" );
994 $debit = Koha::Account::Line->new(
996 borrowernumber => $patron->id,
997 date => '1900-01-01',
998 amountoutstanding => 5,
999 interface => 'commanline',
1000 debit_type_code => 'LOST'
1002 )->store();
1003 $credit = Koha::Account::Line->new(
1005 borrowernumber => $patron->id,
1006 date => '1900-01-01',
1007 amountoutstanding => 0,
1008 interface => 'commandline',
1009 accounttype => 'Pay'
1011 )->store();
1012 $offset = Koha::Account::Offset->new(
1014 credit_id => $credit->id,
1015 debit_id => $debit->id,
1016 type => 'Payment',
1017 amount => 0
1019 )->store();
1020 purge_zero_balance_fees( 1 );
1021 $debit_2 = $credit_2 = undef;
1022 $debit_2 = Koha::Account::Lines->find( $debit->id );
1023 $credit_2 = Koha::Account::Lines->find( $credit->id );
1024 ok( $debit_2, 'Debit was correctly not deleted when debit has balance' );
1025 ok( $credit_2, 'Credit was correctly not deleted when debit has balance' );
1026 is( Koha::Account::Lines->count({ borrowernumber => $patron->id }), 2 + 2, "The 2 + 2 account lines still exists" );
1028 $debit = Koha::Account::Line->new(
1030 borrowernumber => $patron->id,
1031 date => '1900-01-01',
1032 amountoutstanding => 0,
1033 interface => 'commandline',
1034 debit_type_code => 'LOST'
1036 )->store();
1037 $credit = Koha::Account::Line->new(
1039 borrowernumber => $patron->id,
1040 date => '1900-01-01',
1041 amountoutstanding => 0,
1042 interface => 'commandline',
1043 accounttype => 'Pay'
1045 )->store();
1046 $offset = Koha::Account::Offset->new(
1048 credit_id => $credit->id,
1049 debit_id => $debit->id,
1050 type => 'Payment',
1051 amount => 0
1053 )->store();
1054 purge_zero_balance_fees( 1 );
1055 $debit_2 = Koha::Account::Lines->find( $debit->id );
1056 $credit_2 = Koha::Account::Lines->find( $credit->id );
1057 ok( !$debit_2, 'Debit was correctly deleted' );
1058 ok( !$credit_2, 'Credit was correctly deleted' );
1059 is( Koha::Account::Lines->count({ borrowernumber => $patron->id }), 2 + 2, "The 2 + 2 account lines still exists, the last 2 have been deleted ok" );
1063 subtest "Koha::Account::Offset credit & debit tests" => sub {
1065 plan tests => 4;
1067 # Create a borrower
1068 my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
1069 my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode };
1071 my $borrower = Koha::Patron->new( {
1072 cardnumber => 'kyliehall',
1073 surname => 'Hall',
1074 firstname => 'Kylie',
1075 } );
1076 $borrower->categorycode( $categorycode );
1077 $borrower->branchcode( $branchcode );
1078 $borrower->store;
1080 my $account = Koha::Account->new({ patron_id => $borrower->id });
1082 my $line1 = Koha::Account::Line->new(
1084 borrowernumber => $borrower->borrowernumber,
1085 amount => 10,
1086 amountoutstanding => 10,
1087 interface => 'commandline',
1088 debit_type_code => 'LOST'
1090 )->store();
1091 my $line2 = Koha::Account::Line->new(
1093 borrowernumber => $borrower->borrowernumber,
1094 amount => 20,
1095 amountoutstanding => 20,
1096 interface => 'commandline',
1097 debit_type_code => 'LOST'
1099 )->store();
1101 my $id = $account->pay(
1103 lines => [$line1, $line2],
1104 amount => 30,
1108 # Test debit and credit methods for Koha::Account::Offset
1109 my $account_offset = Koha::Account::Offsets->find( { credit_id => $id, debit_id => $line1->id } );
1110 is( $account_offset->debit->id, $line1->id, "Koha::Account::Offset->debit gets correct accountline" );
1111 is( $account_offset->credit->id, $id, "Koha::Account::Offset->credit gets correct accountline" );
1113 $account_offset = Koha::Account::Offset->new(
1115 credit_id => undef,
1116 debit_id => undef,
1117 type => 'Payment',
1118 amount => 0,
1120 )->store();
1122 is( $account_offset->debit, undef, "Koha::Account::Offset->debit returns undef if no associated debit" );
1123 is( $account_offset->credit, undef, "Koha::Account::Offset->credit returns undef if no associated credit" );
1126 subtest "Payment notice tests" => sub {
1128 plan tests => 8;
1130 Koha::Account::Lines->delete();
1131 Koha::Patrons->delete();
1132 Koha::Notice::Messages->delete();
1133 # Create a borrower
1134 my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
1135 my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode };
1137 my $borrower = Koha::Patron->new(
1139 cardnumber => 'chelseahall',
1140 surname => 'Hall',
1141 firstname => 'Chelsea',
1142 email => 'chelsea@example.com',
1143 categorycode => $categorycode,
1144 branchcode => $branchcode,
1146 )->store();
1148 my $manager = $builder->build_object({ class => "Koha::Patrons" });
1149 my $context = new Test::MockModule('C4::Context');
1150 $context->mock( 'userenv', sub {
1151 return {
1152 number => $manager->borrowernumber,
1153 branch => $manager->branchcode,
1156 my $account = Koha::Account->new({ patron_id => $borrower->id });
1158 my $line = Koha::Account::Line->new(
1160 borrowernumber => $borrower->borrowernumber,
1161 amountoutstanding => 27,
1162 interface => 'commandline',
1163 debit_type_code => 'LOST'
1165 )->store();
1167 my $letter = Koha::Notice::Templates->find( { code => 'ACCOUNT_PAYMENT' } );
1168 $letter->content('[%- USE Price -%]A payment of [% credit.amount * -1 | $Price %] has been applied to your account.');
1169 $letter->store();
1171 t::lib::Mocks::mock_preference('UseEmailReceipts', '0');
1172 my $id = $account->pay( { amount => 1 } );
1173 is( Koha::Notice::Messages->search()->count(), 0, 'Notice for payment not sent if UseEmailReceipts is disabled' );
1175 $id = $account->pay( { amount => 1, type => 'writeoff' } );
1176 is( Koha::Notice::Messages->search()->count(), 0, 'Notice for writeoff not sent if UseEmailReceipts is disabled' );
1178 t::lib::Mocks::mock_preference('UseEmailReceipts', '1');
1180 $id = $account->pay( { amount => 12 } );
1181 my $notice = Koha::Notice::Messages->search()->next();
1182 is( $notice->subject, 'Account payment', 'Notice subject is correct for payment' );
1183 is( $notice->letter_code, 'ACCOUNT_PAYMENT', 'Notice letter code is correct for payment' );
1184 is( $notice->content, 'A payment of 12.00 has been applied to your account.', 'Notice content is correct for payment' );
1185 $notice->delete();
1187 $letter = Koha::Notice::Templates->find( { code => 'ACCOUNT_WRITEOFF' } );
1188 $letter->content('[%- USE Price -%]A writeoff of [% credit.amount * -1 | $Price %] has been applied to your account.');
1189 $letter->store();
1191 $id = $account->pay( { amount => 13, type => 'writeoff' } );
1192 $notice = Koha::Notice::Messages->search()->next();
1193 is( $notice->subject, 'Account writeoff', 'Notice subject is correct for payment' );
1194 is( $notice->letter_code, 'ACCOUNT_WRITEOFF', 'Notice letter code is correct for writeoff' );
1195 is( $notice->content, 'A writeoff of 13.00 has been applied to your account.', 'Notice content is correct for writeoff' );