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
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>.
21 use Test::More tests => 27;
26 use t::lib::TestBuilder;
30 use Koha::Account::DebitTypes;
31 use Koha::Account::Lines;
32 use Koha::Account::Offsets;
33 use Koha::Notice::Messages;
34 use Koha::Notice::Templates;
35 use Koha::DateUtils qw( dt_from_string );
37 use C4::Circulation qw( MarkIssueReturned );
40 use_ok('C4::Accounts', qw( chargelostitem purge_zero_balance_fees ));
41 use_ok('Koha::Object');
42 use_ok('Koha::Patron');
43 use_ok('Data::Dumper');
46 can_ok( 'C4::Accounts',
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 my $branchcode = $library->{branchcode};
61 my $context = Test::MockModule->new('C4::Context');
62 $context->mock( 'userenv', sub {
66 branch => $branchcode,
69 $context->mock( 'interface', sub { return "commandline" } );
70 my $userenv_branchcode = $branchcode;
72 # Testing purge_zero_balance_fees
74 # The 3rd value in the insert is 'days ago' --
79 my $sth = $dbh->prepare(
80 "INSERT INTO accountlines (
89 VALUES ( ?, ?, (select date_sub(CURRENT_DATE, INTERVAL ? DAY) ), ?, ?, ?, ? )"
95 { amount => 0 , days_ago => 0 , description =>'purge_zero_balance_fees should not delete 0 balance fees with date today' , delete => 0, credit_type => undef, debit_type => 'OVERDUE' } ,
96 { amount => 0 , days_ago => $days - 1 , description =>'purge_zero_balance_fees should not delete 0 balance fees with date before threshold day' , delete => 0, credit_type => undef, debit_type => 'OVERDUE' } ,
97 { amount => 0 , days_ago => $days , description =>'purge_zero_balance_fees should not delete 0 balance fees with date on threshold day' , delete => 0, credit_type => undef, debit_type => 'OVERDUE' } ,
98 { amount => 0 , days_ago => $days + 1 , description =>'purge_zero_balance_fees should delete 0 balance fees with date after threshold day' , delete => 1, credit_type => undef, debit_type => 'OVERDUE' } ,
99 { amount => undef , days_ago => $days + 1 , description =>'purge_zero_balance_fees should delete NULL balance fees with date after threshold day' , delete => 1, credit_type => undef, debit_type => 'OVERDUE' } ,
100 { amount => 5 , days_ago => $days - 1 , description =>'purge_zero_balance_fees should not delete fees with positive amout owed before threshold day' , delete => 0, credit_type => undef, debit_type => 'OVERDUE' } ,
101 { amount => 5 , days_ago => $days , description =>'purge_zero_balance_fees should not delete fees with positive amout owed on threshold day' , delete => 0, credit_type => undef, debit_type => 'OVERDUE' } ,
102 { amount => 5 , days_ago => $days + 1 , description =>'purge_zero_balance_fees should not delete fees with positive amout owed after threshold day' , delete => 0, credit_type => undef, debit_type => 'OVERDUE' } ,
103 { amount => -5 , days_ago => $days - 1 , description =>'purge_zero_balance_fees should not delete fees with negative amout owed before threshold day' , delete => 0, credit_type => 'PAYMENT', debit_type => undef } ,
104 { amount => -5 , days_ago => $days , description =>'purge_zero_balance_fees should not delete fees with negative amout owed on threshold day' , delete => 0, credit_type => 'PAYMENT', debit_type => undef } ,
105 { amount => -5 , days_ago => $days + 1 , description =>'purge_zero_balance_fees should not delete fees with negative amout owed after threshold day' , delete => 0, credit_type => 'PAYMENT', debit_type => undef }
107 my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
108 my $borrower = Koha::Patron->new( { firstname => 'Test', surname => 'Patron', categorycode => $categorycode, branchcode => $branchcode } )->store();
110 for my $data ( @test_data ) {
112 $borrower->borrowernumber,
115 $data->{description},
117 $data->{credit_type},
122 purge_zero_balance_fees( $days );
124 $sth = $dbh->prepare(
125 "select count(*) = 0 as deleted
127 where description = ?"
131 sub is_delete_correct {
132 my $should_delete = shift;
133 my $description = shift;
134 $sth->execute( $description );
135 my $test = $sth->fetchrow_hashref();
136 is( $test->{deleted}, $should_delete, $description )
139 for my $data (@test_data) {
140 is_delete_correct( $data->{delete}, $data->{description});
143 $dbh->do(q|DELETE FROM accountlines|);
145 subtest "Koha::Account::pay - always AutoReconcile + notes tests" => sub {
150 my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
151 my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode };
153 my $borrower = Koha::Patron->new( {
154 cardnumber => '1234567890',
156 firstname => 'Marty',
158 $borrower->categorycode( $categorycode );
159 $borrower->branchcode( $branchcode );
162 my $account = Koha::Account->new({ patron_id => $borrower->id });
164 my $line1 = $account->add_debit({ type => 'ACCOUNT', amount => 100, interface => 'commandline' });
165 my $line2 = $account->add_debit({ type => 'ACCOUNT', amount => 200, interface => 'commandline' });
167 $sth = $dbh->prepare("SELECT count(*) FROM accountlines");
169 my $count = $sth->fetchrow_array;
170 is($count, 2, 'There is 2 lines as expected');
172 # There is $100 in the account
173 $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
174 my $outstanding_debt = $account->outstanding_debits->total_outstanding;
175 is($outstanding_debt, 300, 'The account has $300 outstanding debt as expected' );
176 my $outstanding_credit = $account->outstanding_credits->total_outstanding;
177 is($outstanding_credit, 0, 'The account has $0 outstanding credit as expected' );
179 # We make a $20 payment
180 my $borrowernumber = $borrower->borrowernumber;
182 my $payment_note = '$20.00 payment note';
183 my $id = $account->pay( { amount => $data, note => $payment_note, payment_type => "TEST_TYPE" } )->{payment_id};
185 my $accountline = Koha::Account::Lines->find( $id );
186 is( $accountline->payment_type, "TEST_TYPE", "Payment type passed into pay is set in account line correctly" );
188 # There is now $280 in the account
189 $outstanding_debt = $account->outstanding_debits->total_outstanding;
190 is($outstanding_debt, 280, 'The account has $280 outstanding debt as expected' );
191 $outstanding_credit = $account->outstanding_credits->total_outstanding;
192 is($outstanding_credit, 0, 'The account has $0 outstanding credit as expected' );
194 # Is the payment note well registered
195 is($accountline->note,'$20.00 payment note', '$20.00 payment note is registered');
197 # We attempt to make a -$30 payment (a NEGATIVE payment)
199 $payment_note = '-$30.00 payment note';
200 throws_ok { $account->pay( { amount => $data, note => $payment_note } ) }
201 'Koha::Exceptions::Account::AmountNotPositive',
202 'Croaked on call to pay with negative amount';
204 #We make a $150 payment ( > 1stLine )
206 $payment_note = '$150.00 payment note';
207 $id = $account->pay( { amount => $data, note => $payment_note } )->{payment_id};
209 # There is now $130 in the account
210 $outstanding_debt = $account->outstanding_debits->total_outstanding;
211 is($outstanding_debt, 130, 'The account has $130 outstanding debt as expected' );
212 $outstanding_credit = $account->outstanding_credits->total_outstanding;
213 is($outstanding_credit, 0, 'The account has $0 outstanding credit as expected' );
215 # Is the payment note well registered
216 $accountline = Koha::Account::Lines->find( $id );
217 is($accountline->note,'$150.00 payment note', '$150.00 payment note is registered');
219 #We make a $200 payment ( > amountleft )
221 $payment_note = '$200.00 payment note';
222 $id = $account->pay( { amount => $data, note => $payment_note } )->{payment_id};
224 # There is now -$70 in the account
225 $outstanding_debt = $account->outstanding_debits->total_outstanding;
226 is($outstanding_debt, 0, 'The account has $0 outstanding debt as expected' );
227 $outstanding_credit = $account->outstanding_credits->total_outstanding;
228 is($outstanding_credit, -70, 'The account has -$70 outstanding credit as expected' );
230 # Is the payment note well registered
231 $accountline = Koha::Account::Lines->find( $id );
232 is($accountline->note,'$200.00 payment note', '$200.00 payment note is registered');
234 my $line3 = $account->add_debit({ type => 'ACCOUNT', amount => 42, interface => 'commandline' });
235 $id = $account->pay( { lines => [$line3], amount => 42 } )->{payment_id};
236 $accountline = Koha::Account::Lines->find( $id );
237 is( $accountline->amount()+0, -42, "Payment paid the specified fine" );
238 $line3 = Koha::Account::Lines->find( $line3->id );
239 is( $line3->amountoutstanding+0, 0, "Specified fine is paid" );
240 is( $accountline->branchcode, undef, 'branchcode passed, then undef' );
243 subtest "Koha::Account::pay particular line tests" => sub {
248 my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
249 my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode };
251 my $borrower = Koha::Patron->new( {
252 cardnumber => 'kylemhall',
256 $borrower->categorycode( $categorycode );
257 $borrower->branchcode( $branchcode );
260 my $account = Koha::Account->new({ patron_id => $borrower->id });
262 my $line1 = $account->add_debit({ type => 'ACCOUNT', amount => 1, interface => 'commandline' });
263 my $line2 = $account->add_debit({ type => 'ACCOUNT', amount => 2, interface => 'commandline' });
264 my $line3 = $account->add_debit({ type => 'ACCOUNT', amount => 3, interface => 'commandline' });
265 my $line4 = $account->add_debit({ type => 'ACCOUNT', amount => 4, interface => 'commandline' });
267 is( $account->balance(), 10, "Account balance is 10" );
271 lines => [$line2, $line3, $line4],
276 $_->_result->discard_changes foreach ( $line1, $line2, $line3, $line4 );
278 # Line1 is not paid at all, as it was not passed in the lines param
279 is( $line1->amountoutstanding+0, 1, "Line 1 was not paid" );
280 # Line2 was paid in full, as it was the first in the lines list
281 is( $line2->amountoutstanding+0, 0, "Line 2 was paid in full" );
282 # Line3 was paid partially, as the remaining balance did not cover it entirely
283 is( $line3->amountoutstanding+0, 1, "Line 3 was paid to 1.00" );
284 # Line4 was not paid at all, as the payment was all used up by that point
285 is( $line4->amountoutstanding+0, 4, "Line 4 was not paid" );
288 subtest "Koha::Account::pay writeoff tests" => sub {
293 my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
294 my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode };
296 my $borrower = Koha::Patron->new( {
297 cardnumber => 'chelseahall',
299 firstname => 'Chelsea',
301 $borrower->categorycode( $categorycode );
302 $borrower->branchcode( $branchcode );
305 my $account = Koha::Account->new({ patron_id => $borrower->id });
307 my $line = $account->add_debit({ type => 'ACCOUNT', amount => 42, interface => 'commandline' });
309 is( $account->balance(), 42, "Account balance is 42" );
311 my $id = $account->pay(
319 $line->_result->discard_changes();
321 is( $line->amountoutstanding+0, 0, "Line was written off" );
323 my $writeoff = Koha::Account::Lines->find( $id );
325 is( $writeoff->credit_type_code, 'WRITEOFF', 'Type is correct for WRITEOFF' );
326 is( $writeoff->description, '', 'Description is correct' );
327 is( $writeoff->amount+0, -42, 'Amount is correct' );
330 subtest "More Koha::Account::pay tests" => sub {
335 my $category = $builder->build({ source => 'Category' })->{ categorycode };
336 my $branch = $builder->build({ source => 'Branch' })->{ branchcode };
337 $branchcode = $branch;
338 my $borrowernumber = $builder->build({
339 source => 'Borrower',
340 value => { categorycode => $category,
341 branchcode => $branch }
342 })->{ borrowernumber };
345 my $accountline = $builder->build(
347 source => 'Accountline',
349 borrowernumber => $borrowernumber,
351 amountoutstanding => $amount,
352 credit_type_code => undef,
357 my $rs = $schema->resultset('Accountline')->search({
358 borrowernumber => $borrowernumber
361 is( $rs->count(), 1, 'Accountline created' );
363 my $account = Koha::Account->new( { patron_id => $borrowernumber } );
364 my $line = Koha::Account::Lines->find( $accountline->{ accountlines_id } );
365 # make the full payment
366 my $payment = $account->pay(
370 library_id => $branch,
371 note => 'A payment note'
375 my $offsets = Koha::Account::Offsets->search({ credit_id => $payment->{payment_id} });
376 is( $offsets->count, 2, 'Two offsets recorded');
377 my $offset = $offsets->next;
378 is( $offset->type(), 'CREATE', 'First offset type is CREATE' );
379 is( $offset->amount+0, 100, 'First offset amount is 100.00' );
380 $offset = $offsets->next;
381 is( $offset->type(), 'APPLY', 'Second offset type is APPLY' );
382 is( $offset->amount+0, -100, 'Second offset amount is -100.00' );
383 is( $offset->debit_id, $accountline->{accountlines_id}, 'Second offset is against the right accountline');
385 my $stat = $schema->resultset('Statistic')->search({
388 }, { order_by => { -desc => 'datetime' } })->next();
390 ok( defined $stat, "There's a payment log that matches the branch" );
393 skip "No statistic logged", 4 unless defined $stat;
395 is( $stat->type, 'payment', "Correct statistic type" );
396 is( $stat->branch, $branch, "Correct branch logged to statistics" );
397 is( $stat->borrowernumber, $borrowernumber, "Correct borrowernumber logged to statistics" );
398 is( $stat->value+0, -$amount, "Correct amount logged to statistics" );
402 subtest "Even more Koha::Account::pay tests" => sub {
407 my $category = $builder->build({ source => 'Category' })->{ categorycode };
408 my $branch = $builder->build({ source => 'Branch' })->{ branchcode };
409 $branchcode = $branch;
410 my $borrowernumber = $builder->build({
411 source => 'Borrower',
412 value => { categorycode => $category,
413 branchcode => $branch }
414 })->{ borrowernumber };
417 my $partialamount = 60;
418 my $accountline = $builder->build(
420 source => 'Accountline',
422 borrowernumber => $borrowernumber,
424 amountoutstanding => $amount,
425 credit_type_code => undef,
430 my $rs = $schema->resultset('Accountline')->search({
431 borrowernumber => $borrowernumber
434 is( $rs->count(), 1, 'Accountline created' );
436 my $account = Koha::Account->new( { patron_id => $borrowernumber } );
437 my $line = Koha::Account::Lines->find( $accountline->{ accountlines_id } );
438 # make the full payment
439 my $payment = $account->pay(
442 amount => $partialamount,
443 library_id => $branch,
444 note => 'A payment note'
448 my $offsets = Koha::Account::Offsets->search({ credit_id => $payment->{payment_id} });
449 is( $offsets->count, 2, 'Two offsets recorded');
450 my $offset = $offsets->next;
451 is( $offset->type(), 'CREATE', 'First offset type is CREATE' );
452 is( $offset->amount+0, 60, 'First offset amount is 60.00' );
453 $offset = $offsets->next;
454 is( $offset->type(), 'APPLY', 'Second offset type is APPLY' );
455 is( $offset->amount+0, -60, 'Second offset amount is -60.00' );
456 is( $offset->debit_id, $accountline->{accountlines_id}, 'Second offset is against the right accountline');
458 my $stat = $schema->resultset('Statistic')->search({
461 }, { order_by => { -desc => 'datetime' } })->next();
463 ok( defined $stat, "There's a payment log that matches the branch" );
466 skip "No statistic logged", 4 unless defined $stat;
468 is( $stat->type, 'payment', "Correct statistic type" );
469 is( $stat->branch, $branch, "Correct branch logged to statistics" );
470 is( $stat->borrowernumber, $borrowernumber, "Correct borrowernumber logged to statistics" );
471 is( $stat->value+0, -$partialamount, "Correct amount logged to statistics" );
475 subtest 'balance' => sub {
478 my $patron = $builder->build({source => 'Borrower'});
479 $patron = Koha::Patrons->find( $patron->{borrowernumber} );
480 my $account = $patron->account;
481 is( $account->balance, 0, 'balance should return 0 if the patron does not have fines' );
483 my $accountline_1 = $builder->build(
485 source => 'Accountline',
487 borrowernumber => $patron->borrowernumber,
489 amountoutstanding => 42,
490 credit_type_code => undef,
494 my $accountline_2 = $builder->build(
496 source => 'Accountline',
498 borrowernumber => $patron->borrowernumber,
500 amountoutstanding => -13,
501 debit_type_code => undef,
506 my $balance = $patron->account->balance;
507 is( int($balance), 29, 'balance should return the correct value');
512 subtest "C4::Accounts::chargelostitem tests" => sub {
515 my $branch = $builder->build( { source => 'Branch' } );
516 my $branchcode = $branch->{branchcode};
518 my $staff = $builder->build( { source => 'Borrower' } );
519 my $staff_id = $staff->{borrowernumber};
521 my $module = Test::MockModule->new('C4::Context');
528 branch => $branchcode,
533 my $itype_no_replace_no_fee = $builder->build({ source => 'Itemtype', value => {
535 defaultreplacecost => undef,
538 my $itype_replace_no_fee = $builder->build({ source => 'Itemtype', value => {
540 defaultreplacecost => 16.32,
543 my $itype_no_replace_fee = $builder->build({ source => 'Itemtype', value => {
545 defaultreplacecost => undef,
548 my $itype_replace_fee = $builder->build({ source => 'Itemtype', value => {
550 defaultreplacecost => 4.08,
553 my $cli_borrowernumber = $builder->build({ source => 'Borrower' })->{'borrowernumber'};
554 my $cli_itemnumber1 = $builder->build_sample_item({ itype => $itype_no_replace_no_fee->{itemtype} })->itemnumber;
555 my $cli_itemnumber2 = $builder->build_sample_item({ itype => $itype_replace_no_fee->{itemtype} })->itemnumber;
556 my $cli_itemnumber3 = $builder->build_sample_item({ itype => $itype_no_replace_fee->{itemtype} })->itemnumber;
557 my $cli_itemnumber4 = $builder->build_sample_item({ itype => $itype_replace_fee->{itemtype} })->itemnumber;
558 my $cli_itemnumber5 = $builder->build_sample_item({ itype => $itype_replace_fee->{itemtype} })->itemnumber;
559 my $cli_bundle1 = $builder->build_sample_item({ itype => $itype_no_replace_no_fee->{itemtype} })->itemnumber;
560 $schema->resultset('ItemBundle')->create( { host => $cli_bundle1, item => $cli_itemnumber5 } );
562 my $cli_issue_id_1 = $builder->build({ source => 'Issue', value => { borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1 } })->{issue_id};
563 my $cli_issue_id_2 = $builder->build({ source => 'Issue', value => { borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2 } })->{issue_id};
564 my $cli_issue_id_3 = $builder->build({ source => 'Issue', value => { borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3 } })->{issue_id};
565 my $cli_issue_id_4 = $builder->build({ source => 'Issue', value => { borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4 } })->{issue_id};
566 my $cli_issue_id_4X = undef;
567 my $cli_bundle_issue = $builder->build({ source => 'Issue', value => { borrowernumber => $cli_borrowernumber, itemnumber => $cli_bundle1 } })->{issue_id};
572 subtest "fee application tests" => sub {
575 t::lib::Mocks::mock_preference('item-level_itypes', '1');
576 t::lib::Mocks::mock_preference('useDefaultReplacementCost', '0');
578 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber1, 0, "Perdedor");
579 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, debit_type_code => 'LOST' });
580 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, debit_type_code => 'PROCESSING' });
581 ok( !$lostfine, "No lost fine if no replacementcost or default when pref off");
582 ok( !$procfee, "No processing fee if no processing fee");
583 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber1, 6.12, "Perdedor");
584 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, debit_type_code => 'LOST' });
585 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, debit_type_code => 'PROCESSING' });
586 ok( $lostfine->amount == 6.12, "Lost fine equals replacementcost when pref off and no default set");
587 ok( !$procfee, "No processing fee if no processing fee");
590 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber2, 0, "Perdedor");
591 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, debit_type_code => 'LOST' });
592 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, debit_type_code => 'PROCESSING' });
593 ok( !$lostfine, "No lost fine if no replacementcost but default set when pref off");
594 ok( !$procfee, "No processing fee if no processing fee");
595 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber2, 6.12, "Perdedor");
596 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, debit_type_code => 'LOST' });
597 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, debit_type_code => 'PROCESSING' });
598 ok( $lostfine->amount == 6.12 , "Lost fine equals replacementcost when pref off and default set");
599 ok( !$procfee, "No processing fee if no processing fee");
602 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber3, 0, "Perdedor");
603 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, debit_type_code => 'LOST' });
604 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, debit_type_code => 'PROCESSING' });
605 ok( !$lostfine, "No lost fine if no replacementcost and no default set when pref off");
606 ok( $procfee->amount == 8.16, "Processing fee if processing fee");
607 is( $procfee->issue_id, $cli_issue_id_3, "Processing fee issue id is correct" );
609 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber3, 6.12, "Perdedor");
610 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, debit_type_code => 'LOST' });
611 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, debit_type_code => 'PROCESSING' });
612 ok( $lostfine->amount == 6.12 , "Lost fine equals replacementcost when pref off and no default set");
613 ok( $procfee->amount == 8.16, "Processing fee if processing fee");
614 is( $procfee->issue_id, $cli_issue_id_3, "Processing fee issue id is correct" );
618 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 0, "Perdedor");
619 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'LOST' });
620 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'PROCESSING' });
621 ok( !$lostfine, "No lost fine if no replacementcost but default set when pref off");
622 ok( $procfee->amount == 2.04, "Processing fee if processing fee");
623 is( $procfee->issue_id, $cli_issue_id_4, "Processing fee issue id is correct" );
625 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 6.12, "Perdedor");
626 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'LOST' });
627 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'PROCESSING' });
628 ok( $lostfine->amount == 6.12 , "Lost fine equals replacementcost when pref off and default set");
629 ok( $procfee->amount == 2.04, "Processing fee if processing fee");
630 is( $procfee->issue_id, $cli_issue_id_4, "Processing fee issue id is correct" );
634 t::lib::Mocks::mock_preference('useDefaultReplacementCost', '1');
636 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber1, 0, "Perdedor");
637 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, debit_type_code => 'LOST' });
638 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, debit_type_code => 'PROCESSING' });
639 ok( !$lostfine, "No lost fine if no replacementcost or default when pref on");
640 ok( !$procfee, "No processing fee if no processing fee");
641 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber1, 6.12, "Perdedor");
642 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, debit_type_code => 'LOST' });
643 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, debit_type_code => 'PROCESSING' });
644 is( $lostfine->amount, "6.120000", "Lost fine equals replacementcost when pref on and no default set");
645 ok( !$procfee, "No processing fee if no processing fee");
647 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber2, 0, "Perdedor");
648 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, debit_type_code => 'LOST' });
649 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, debit_type_code => 'PROCESSING' });
650 is( $lostfine->amount(), "16.320000", "Lost fine is default if no replacementcost but default set when pref on");
651 ok( !$procfee, "No processing fee if no processing fee");
653 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber2, 6.12, "Perdedor");
654 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, debit_type_code => 'LOST' });
655 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, debit_type_code => 'PROCESSING' });
656 is( $lostfine->amount, "6.120000" , "Lost fine equals replacementcost when pref on and default set");
657 ok( !$procfee, "No processing fee if no processing fee");
659 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber3, 0, "Perdedor");
660 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, debit_type_code => 'LOST' });
661 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, debit_type_code => 'PROCESSING' });
662 ok( !$lostfine, "No lost fine if no replacementcost and default not set when pref on");
663 is( $procfee->amount, "8.160000", "Processing fee if processing fee");
664 is( $procfee->issue_id, $cli_issue_id_3, "Processing fee issue id is correct" );
666 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber3, 6.12, "Perdedor");
667 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, debit_type_code => 'LOST' });
668 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, debit_type_code => 'PROCESSING' });
669 is( $lostfine->amount, "6.120000", "Lost fine equals replacementcost when pref on and no default set");
670 is( $procfee->amount, "8.160000", "Processing fee if processing fee");
671 is( $procfee->issue_id, $cli_issue_id_3, "Processing fee issue id is correct" );
673 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 0, "Perdedor");
674 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'LOST' });
675 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'PROCESSING' });
676 is( $lostfine->amount, "4.080000", "Lost fine is default if no replacementcost but default set when pref on");
677 is( $procfee->amount, "2.040000", "Processing fee if processing fee");
678 is( $procfee->issue_id, $cli_issue_id_4, "Processing fee issue id is correct" );
681 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 6.12, "Perdedor");
682 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'LOST' });
683 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'PROCESSING' });
684 is( $lostfine->amount, "6.120000", "Lost fine equals replacementcost when pref on and default set");
685 is( $procfee->amount, "2.040000", "Processing fee if processing fee");
686 is( $procfee->issue_id, $cli_issue_id_4, "Processing fee issue id is correct" );
687 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 6.12, "Perdedor");
688 my $lostfines = Koha::Account::Lines->search({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'LOST' });
689 my $procfees = Koha::Account::Lines->search({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'PROCESSING' });
690 ok( $lostfines->count == 1 , "Lost fine cannot be double charged for the same issue_id");
691 ok( $procfees->count == 1, "Processing fee cannot be double charged for the same issue_id");
692 MarkIssueReturned($cli_borrowernumber, $cli_itemnumber4);
693 $cli_issue_id_4X = $builder->build({ source => 'Issue', value => { borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4 } })->{issue_id};
694 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 6.12, "Perdedor");
695 $lostfines = Koha::Account::Lines->search({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'LOST' });
696 $procfees = Koha::Account::Lines->search({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'PROCESSING' });
697 ok( $lostfines->count == 2 , "Lost fine can be charged twice for the same item if they are distinct issue_id's");
698 ok( $procfees->count == 2, "Processing fee can be charged twice for the same item if they are distinct issue_id's");
699 $lostfines->delete();
702 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber5, 6.12, "Bundle");
703 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber5, debit_type_code => 'LOST' });
704 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber5, debit_type_code => 'PROCESSING' });
705 is( $lostfine->amount, "6.120000", "Lost fine equals replacementcost when pref on and default set (Bundle)");
706 is( $procfee->amount, "2.040000", "Processing fee if processing fee (Bundle)");
707 is( $lostfine->issue_id, $cli_bundle_issue, "Lost fine issue id matched to bundle issue");
708 is( $procfee->issue_id, $cli_bundle_issue, "Processing fee issue id matched to bundle issue");
713 subtest "basic fields tests" => sub {
716 t::lib::Mocks::mock_preference('ProcessingFeeNote', 'Test Note');
717 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, '1.99', "Perdedor");
720 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'LOST' });
721 ok($lostfine, "Lost fine created");
722 is($lostfine->manager_id, $staff_id, "Lost fine manager_id set correctly");
723 is($lostfine->issue_id, $cli_issue_id_4X, "Lost fine issue_id set correctly");
724 is($lostfine->description, "Perdedor", "Lost fine issue_id set correctly");
725 is($lostfine->note, '', "Lost fine does not contain a note");
726 is($lostfine->branchcode, $branchcode, "Lost fine branchcode set correctly");
729 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'PROCESSING' });
730 ok($procfee, "Processing fee created");
731 is($procfee->manager_id, $staff_id, "Processing fee manager_id set correctly");
732 is($procfee->issue_id, $cli_issue_id_4X, "Processing fee issue_id set correctly");
733 is($procfee->description, "Perdedor", "Processing fee issue_id set correctly");
734 is($procfee->note, C4::Context->preference("ProcessingFeeNote"), "Processing fee contains note matching ProcessingFeeNote");
735 is($procfee->branchcode, $branchcode, "Processing fee branchcode set correctly");
740 subtest "FinesLog tests" => sub {
743 my $action_logs = $schema->resultset('ActionLog')->search()->count;
745 t::lib::Mocks::mock_preference( 'FinesLog', 0 );
746 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 0, "Perdedor");
747 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'LOST' });
748 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'PROCESSING' });
749 is( $schema->resultset('ActionLog')->count(), $action_logs + 0, 'No logs were added' );
753 t::lib::Mocks::mock_preference( 'FinesLog', 1 );
754 C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 0, "Perdedor");
755 $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'LOST' });
756 $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, debit_type_code => 'PROCESSING' });
757 is( $schema->resultset('ActionLog')->count(), $action_logs + 2, 'Logs were added' );
762 # Cleanup - this must be replaced with a transaction per subtest
763 Koha::Patrons->find($cli_borrowernumber)->checkouts->delete;
766 subtest "Koha::Account::non_issues_charges tests" => sub {
769 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
770 my $account = $patron->account;
778 description => 'a Res fee',
781 interface => 'commandline'
786 description => 'a Rental fee',
789 interface => 'commandline'
792 Koha::Account::DebitTypes->find_or_create(
795 description => 'Fee for copie',
799 Koha::Account::Line->new(
801 borrowernumber => $patron->borrowernumber,
802 description => 'a Manual invoice fee',
803 debit_type_code => 'Copie',
804 amountoutstanding => $manual,
805 interface => 'commandline'
809 my ( $total, $non_issues_charges ) = ( $account->balance, $account->non_issues_charges );
810 my $other_charges = $total - $non_issues_charges;
813 $res + $rent + $manual,
814 'Total charges should be Res + Rent + Manual'
816 is( $non_issues_charges, 15,
817 'All types should count towards the non issue charge' );
818 is( $other_charges, 0, 'There shouldn\'t be any non-included charges' );
820 Koha::Account::DebitTypes->find_or_create(
823 description => 'Charge for using the printer',
825 restricts_checkouts => 0
828 Koha::Account::Line->new(
830 borrowernumber => $patron->borrowernumber,
831 description => 'Non-restricting fee',
832 debit_type_code => 'Print',
833 amountoutstanding => $print,
834 interface => 'commandline'
838 my ( $new_total, $new_non_issues_charges ) = ( $account->balance, $account->non_issues_charges );
839 my $new_other_charges = $new_total - $new_non_issues_charges;
842 $res + $rent + $manual + $print,
843 'Total charges should be Res + Rent + Manual + Print'
845 is( $new_non_issues_charges, 15,
846 'All types except Print should count towards the non issue charge' );
847 is( $new_other_charges, 4, 'There should be non-included charges for Print' );
851 subtest "Koha::Account::non_issues_charges tests" => sub {
854 my $patron = $builder->build_object(
856 class => "Koha::Patrons",
860 categorycode => $categorycode,
861 branchcode => $branchcode
866 my $debit = Koha::Account::Line->new(
868 borrowernumber => $patron->id,
869 date => '1970-01-01 14:00:01',
870 amountoutstanding => 0,
871 interface => 'commandline',
872 debit_type_code => 'LOST'
875 my $credit = Koha::Account::Line->new(
877 borrowernumber => $patron->id,
878 date => '1970-01-01 14:00:01',
879 amountoutstanding => -5,
880 interface => 'commandline',
881 credit_type_code => 'PAYMENT'
884 my $offset = Koha::Account::Offset->new(
886 credit_id => $credit->id,
887 debit_id => $debit->id,
892 purge_zero_balance_fees( 1 );
893 my $debit_2 = Koha::Account::Lines->find( $debit->id );
894 my $credit_2 = Koha::Account::Lines->find( $credit->id );
895 ok( $debit_2, 'Debit was correctly not deleted when credit has balance' );
896 ok( $credit_2, 'Credit was correctly not deleted when credit has balance' );
897 is( Koha::Account::Lines->count({ borrowernumber => $patron->id }), 2, "The 2 account lines still exists" );
899 $debit = Koha::Account::Line->new(
901 borrowernumber => $patron->id,
902 date => '1970-01-01 14:00:01',
903 amountoutstanding => 5,
904 interface => 'commanline',
905 debit_type_code => 'LOST'
908 $credit = Koha::Account::Line->new(
910 borrowernumber => $patron->id,
911 date => '1970-01-01 14:00:01',
912 amountoutstanding => 0,
913 interface => 'commandline',
914 credit_type_code => 'PAYMENT'
917 $offset = Koha::Account::Offset->new(
919 credit_id => $credit->id,
920 debit_id => $debit->id,
925 purge_zero_balance_fees( 1 );
926 $debit_2 = $credit_2 = undef;
927 $debit_2 = Koha::Account::Lines->find( $debit->id );
928 $credit_2 = Koha::Account::Lines->find( $credit->id );
929 ok( $debit_2, 'Debit was correctly not deleted when debit has balance' );
930 ok( $credit_2, 'Credit was correctly not deleted when debit has balance' );
931 is( Koha::Account::Lines->count({ borrowernumber => $patron->id }), 2 + 2, "The 2 + 2 account lines still exists" );
933 $debit = Koha::Account::Line->new(
935 borrowernumber => $patron->id,
936 date => '1970-01-01 14:00:01',
937 amountoutstanding => 0,
938 interface => 'commandline',
939 debit_type_code => 'LOST'
942 $credit = Koha::Account::Line->new(
944 borrowernumber => $patron->id,
945 date => '1970-01-01 14:00:01',
946 amountoutstanding => 0,
947 interface => 'commandline',
948 credit_type_code => 'PAYMENT'
951 $offset = Koha::Account::Offset->new(
953 credit_id => $credit->id,
954 debit_id => $debit->id,
959 purge_zero_balance_fees( 1 );
960 $debit_2 = Koha::Account::Lines->find( $debit->id );
961 $credit_2 = Koha::Account::Lines->find( $credit->id );
962 ok( !$debit_2, 'Debit was correctly deleted' );
963 ok( !$credit_2, 'Credit was correctly deleted' );
964 is( Koha::Account::Lines->count({ borrowernumber => $patron->id }), 2 + 2, "The 2 + 2 account lines still exists, the last 2 have been deleted ok" );
968 subtest "Koha::Account::Offset credit & debit tests" => sub {
973 my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
974 my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode };
976 my $borrower = Koha::Patron->new( {
977 cardnumber => 'kyliehall',
979 firstname => 'Kylie',
981 $borrower->categorycode( $categorycode );
982 $borrower->branchcode( $branchcode );
985 my $account = Koha::Account->new({ patron_id => $borrower->id });
987 my $line1 = Koha::Account::Line->new(
989 borrowernumber => $borrower->borrowernumber,
991 amountoutstanding => 10,
992 interface => 'commandline',
993 debit_type_code => 'LOST'
996 my $line2 = Koha::Account::Line->new(
998 borrowernumber => $borrower->borrowernumber,
1000 amountoutstanding => 20,
1001 interface => 'commandline',
1002 debit_type_code => 'LOST'
1006 my $id = $account->pay(
1008 lines => [$line1, $line2],
1013 # Test debit and credit methods for Koha::Account::Offset
1014 my $account_offset = Koha::Account::Offsets->find( { credit_id => $id, debit_id => $line1->id } );
1015 is( $account_offset->debit->id, $line1->id, "Koha::Account::Offset->debit gets correct accountline" );
1016 is( $account_offset->credit->id, $id, "Koha::Account::Offset->credit gets correct accountline" );
1018 $account_offset = Koha::Account::Offset->new(
1027 is( $account_offset->debit, undef, "Koha::Account::Offset->debit returns undef if no associated debit" );
1028 is( $account_offset->credit, undef, "Koha::Account::Offset->credit returns undef if no associated credit" );
1031 subtest "Payment notice tests" => sub {
1035 Koha::Notice::Messages->delete();
1037 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
1039 my $manager = $builder->build_object({ class => "Koha::Patrons" });
1040 my $context = Test::MockModule->new('C4::Context');
1041 $context->mock( 'userenv', sub {
1043 number => $manager->borrowernumber,
1044 branch => $manager->branchcode,
1047 my $account = Koha::Account->new({ patron_id => $patron->borrowernumber });
1049 my $line = Koha::Account::Line->new(
1051 borrowernumber => $patron->borrowernumber,
1052 amountoutstanding => 27,
1053 interface => 'commandline',
1054 debit_type_code => 'LOST'
1058 my $letter = Koha::Notice::Templates->find( { code => 'ACCOUNT_PAYMENT' } );
1059 $letter->content('[%- USE Price -%]A payment of [% credit.amount * -1 | $Price %] has been applied to your account. Your [% branch.branchcode %]');
1062 t::lib::Mocks::mock_preference('UseEmailReceipts', '0');
1063 my $id = $account->pay( { amount => 1 } )->{payment_id};
1064 is( Koha::Notice::Messages->search()->count(), 0, 'Notice for payment not sent if UseEmailReceipts is disabled' );
1066 $id = $account->pay( { amount => 1, type => 'WRITEOFF' } )->{payment_id};
1067 is( Koha::Notice::Messages->search()->count(), 0, 'Notice for writeoff not sent if UseEmailReceipts is disabled' );
1069 t::lib::Mocks::mock_preference('UseEmailReceipts', '1');
1071 $id = $account->pay( { amount => 12, library_id => $branchcode } )->{payment_id};
1072 my $notice = Koha::Notice::Messages->search()->next();
1073 is( $notice->subject, 'Account payment', 'Notice subject is correct for payment' );
1074 is( $notice->letter_code, 'ACCOUNT_PAYMENT', 'Notice letter code is correct for payment' );
1075 is( $notice->content, "A payment of 12.00 has been applied to your account. Your $branchcode", 'Notice content is correct for payment' );
1078 $letter = Koha::Notice::Templates->find( { code => 'ACCOUNT_WRITEOFF' } );
1079 $letter->content('[%- USE Price -%]A writeoff of [% credit.amount * -1 | $Price %] has been applied to your account. Your [% branch.branchcode %]');
1082 $id = $account->pay( { amount => 13, type => 'WRITEOFF', library_id => $branchcode } )->{payment_id};
1083 $notice = Koha::Notice::Messages->search()->next();
1084 is( $notice->subject, 'Account writeoff', 'Notice subject is correct for payment' );
1085 is( $notice->letter_code, 'ACCOUNT_WRITEOFF', 'Notice letter code is correct for writeoff' );
1086 is( $notice->content, "A writeoff of 13.00 has been applied to your account. Your $branchcode", 'Notice content is correct for writeoff' );