use Modern::Perl;
-use Test::More tests => 13;
+use Test::More tests => 14;
use Test::Exception;
use Test::MockModule;
use DateTime;
-use C4::Circulation qw/AddIssue AddReturn/;
+use C4::Circulation qw( AddRenewal CanBookBeRenewed LostItem AddIssue AddReturn );
use Koha::Account;
use Koha::Account::Lines;
use Koha::Account::Offsets;
subtest 'apply() tests' => sub {
- plan tests => 25;
+ plan tests => 31;
$schema->storage->txn_begin;
$debit_1->discard_changes;
my $debits = Koha::Account::Lines->search({ accountlines_id => $debit_1->id });
- my $remaining_credit = $credit->apply( { debits => [ $debits->as_list ], offset_type => 'Manual Credit' } );
- is( $remaining_credit * 1, 90, 'Remaining credit is correctly calculated' );
- $credit->discard_changes;
- is( $credit->amountoutstanding * -1, $remaining_credit, 'Remaining credit correctly stored' );
+ $credit = $credit->apply( { debits => [ $debits->as_list ], offset_type => 'Manual Credit' } );
+ is( ref($credit), 'Koha::Account::Line', '->apply returns the updated Koha::Account::Line credit object');
+ is( $credit->amountoutstanding * -1, 90, 'Remaining credit is correctly calculated' );
# re-read debit info
$debit_1->discard_changes;
is( $THE_offset->type, 'Manual Credit', 'Passed type stored correctly' );
$debits = Koha::Account::Lines->search({ accountlines_id => $debit_2->id });
- $remaining_credit = $credit->apply( { debits => [ $debits->as_list ] } );
- is( $remaining_credit, 0, 'No remaining credit left' );
- $credit->discard_changes;
- is( $credit->amountoutstanding * 1, 0, 'No outstanding credit' );
+ $credit = $credit->apply( { debits => [ $debits->as_list ] } );
+ is( $credit->amountoutstanding * 1, 0, 'No remaining credit' );
$debit_2->discard_changes;
is( $debit_2->amountoutstanding * 1, 10, 'Outstanding amount decremented correctly' );
is( $credit_2->discard_changes->amountoutstanding * -1, 20, 'No changes made' );
$debits = Koha::Account::Lines->search({ accountlines_id => { -in => [ $debit_1->id, $debit_2->id, $debit_3->id ] } });
- $remaining_credit = $credit_2->apply( { debits => [ $debits->as_list ], offset_type => 'Manual Credit' } );
+ $credit_2 = $credit_2->apply( { debits => [ $debits->as_list ], offset_type => 'Manual Credit' } );
is( $debit_1->discard_changes->amountoutstanding * 1, 0, 'No changes to already cancelled debit' );
is( $debit_2->discard_changes->amountoutstanding * 1, 0, 'Debit cancelled' );
is( $debit_3->discard_changes->amountoutstanding * 1, 90, 'Outstanding amount correctly calculated' );
- is( $credit_2->discard_changes->amountoutstanding * 1, 0, 'No remaining credit' );
+ is( $credit_2->amountoutstanding * 1, 0, 'No remaining credit' );
my $library = $builder->build_object( { class => 'Koha::Libraries' } );
my $biblio = $builder->build_sample_biblio();
my $module = Test::MockModule->new('C4::Circulation');
$module->mock('AddRenewal', sub { $called = 1; });
$module->mock('CanBookBeRenewed', sub { return 1; });
- my $credit_renew = $account->add_credit({ amount => 100, user_id => $patron->id, interface => 'commandline' });
+ my $credit_forgive = $account->add_credit(
+ {
+ amount => 1,
+ user_id => $patron->id,
+ interface => 'cli',
+ type => 'FORGIVEN'
+ }
+ );
my $debits_renew = Koha::Account::Lines->search({ accountlines_id => $accountline->id })->as_list;
- $credit_renew->apply( { debits => $debits_renew, offset_type => 'Manual Credit' } );
+ $credit_forgive = $credit_forgive->apply( { debits => $debits_renew, offset_type => 'Forgiven' } );
+ is( $called, 0, 'C4::Circulation::AddRenew NOT called when RenewAccruingItemWhenPaid enabled but credit type is "FORGIVEN"' );
+ $accountline = Koha::Account::Line->new(
+ {
+ issue_id => $checkout->id,
+ borrowernumber => $patron->id,
+ itemnumber => $item->id,
+ branchcode => $library->id,
+ date => \'NOW()',
+ debit_type_code => 'OVERDUE',
+ status => 'UNRETURNED',
+ interface => 'cli',
+ amount => '1',
+ amountoutstanding => '1',
+ }
+ )->store();
+ my $credit_renew = $account->add_credit({ amount => 100, user_id => $patron->id, interface => 'commandline' });
+ $debits_renew = Koha::Account::Lines->search({ accountlines_id => $accountline->id })->as_list;
+ $credit_renew = $credit_renew->apply( { debits => $debits_renew, offset_type => 'Manual Credit' } );
is( $called, 1, 'RenewAccruingItemWhenPaid causes C4::Circulation::AddRenew to be called when appropriate' );
+ my @messages = @{$credit_renew->messages};
+ is( $messages[0]->type, 'info', 'Info message added for renewal' );
+ is( $messages[0]->message, 'renewal', 'Message is "renewal"' );
+ is( $messages[0]->payload->{itemnumber}, $item->id, 'itemnumber found in payload' );
+ is( $messages[0]->payload->{due_date}, 1, 'due_date key in payload' );
+ is( $messages[0]->payload->{success}, 1, "'success' key in payload" );
+
+ t::lib::Mocks::mock_preference( 'MarkLostItemsAsReturned', 'onpayment');
+ my $loser = $builder->build_object( { class => 'Koha::Patrons' } );
+ my $loser_account = $loser->account;
+
+ my $lost_item = $builder->build_sample_item();
+ my $lost_checkout = Koha::Checkout->new(
+ {
+ borrowernumber => $loser->id,
+ itemnumber => $lost_item->id,
+ date_due => $five_weeks_ago,
+ branchcode => $library->id,
+ issuedate => $seven_weeks_ago
+ }
+ )->store();
+
+ $lost_item->itemlost(1)->store;
+ my $processing_fee = Koha::Account::Line->new(
+ {
+ issue_id => $lost_checkout->id,
+ borrowernumber => $loser->id,
+ itemnumber => $lost_item->id,
+ branchcode => $library->id,
+ date => \'NOW()',
+ debit_type_code => 'PROCESSING',
+ status => undef,
+ interface => 'intranet',
+ amount => '15',
+ amountoutstanding => '15',
+ }
+ )->store();
+ my $lost_fee = Koha::Account::Line->new(
+ {
+ issue_id => $lost_checkout->id,
+ borrowernumber => $loser->id,
+ itemnumber => $lost_item->id,
+ branchcode => $library->id,
+ date => \'NOW()',
+ debit_type_code => 'LOST',
+ status => undef,
+ interface => 'intranet',
+ amount => '12.63',
+ amountoutstanding => '12.63',
+ }
+ )->store();
+ my $pay_lost = $loser_account->add_credit({ amount => 27.630000, user_id => $loser->id, interface => 'intranet' });
+ my $pay_lines = [ $processing_fee, $lost_fee ];
+ $pay_lost->apply( { debits => $pay_lines, offset_type => 'Credit applied' } );
+
+ is( $loser->checkouts->next, undef, "Item has been returned");
+
+
+
$schema->storage->txn_rollback;
};
subtest 'Renewal related tests' => sub {
- plan tests => 7;
+ plan tests => 8;
$schema->storage->txn_begin;
interface => 'commandline',
})->store;
- is( $line->renewable, 1, "Item is returned as renewable when it meets the conditions" );
+ is( $line->is_renewable, 1, "Item is returned as renewable when it meets the conditions" );
$line->amountoutstanding(5);
- is( $line->renewable, 0, "Item is returned as unrenewable when it has outstanding fine" );
+ is( $line->is_renewable, 0, "Item is returned as unrenewable when it has outstanding fine" );
$line->amountoutstanding(0);
$line->debit_type_code("VOID");
- is( $line->renewable, 0, "Item is returned as unrenewable when it has the wrong account type" );
+ is( $line->is_renewable, 0, "Item is returned as unrenewable when it has the wrong account type" );
$line->debit_type_code("OVERDUE");
$line->status("RETURNED");
- is( $line->renewable, 0, "Item is returned as unrenewable when it has the wrong account status" );
+ is( $line->is_renewable, 0, "Item is returned as unrenewable when it has the wrong account status" );
t::lib::Mocks::mock_preference( 'RenewAccruingItemWhenPaid', 0 );
- is ($line->renew_item, undef, 'Attempt to renew fails when syspref is not set');
+ is ($line->renew_item({ interface => 'intranet' }), undef, 'Attempt to renew fails when syspref is not set');
t::lib::Mocks::mock_preference( 'RenewAccruingItemWhenPaid', 1 );
+ t::lib::Mocks::mock_preference( 'RenewAccruingItemInOpac', 0 );
+ is ($line->renew_item({ interface => 'opac' }), undef, 'Attempt to renew fails when syspref is not set - OPAC');
+ t::lib::Mocks::mock_preference( 'RenewAccruingItemInOpac', 1 );
is_deeply(
- $line->renew_item,
+ $line->renew_item({ interface => 'intranet' }),
{
itemnumber => $item->itemnumber,
error => 'too_many',
item_id => $item->itemnumber,
issue_id => $checkout->issue_id,
type => 'OVERDUE',
+ status => 'UNRETURNED'
});
my $line_checkout = $line->checkout;
is( ref($line_checkout), 'Koha::Checkout', 'Result type is correct' );
is( $line_checkout->issue_id, $checkout->issue_id, 'Koha::Account::Line->checkout should return the correct checkout');
+ # Prevent re-calculation of fines at check-in for the test; Since bug 8338 the recalculation would result in a '0'
+ # fine which would subsequently be removed by _FixOverduesOnReturn
+ t::lib::Mocks::mock_preference( 'finesMode', 'off' );
+
my ( $returned, undef, $old_checkout) = C4::Circulation::AddReturn( $item->barcode, $library->branchcode );
is( $returned, 1, 'The item should have been returned' );
subtest "void() tests" => sub {
- plan tests => 16;
+ plan tests => 23;
$schema->storage->txn_begin;
is( $line1->amountoutstanding+0, 0, 'First fee has amount outstanding of 0' );
is( $line2->amountoutstanding+0, 0, 'Second fee has amount outstanding of 0' );
- my $ret = $account_payment->void();
+ throws_ok {
+ $line1->void( { interface => 'test' } );
+ }
+ 'Koha::Exceptions::Account::IsNotCredit',
+ '->void() can only be used with credits';
+
+ throws_ok {
+ $account_payment->void();
+ }
+ 'Koha::Exceptions::MissingParameter',
+ "->void() requires the `interface` parameter is passed";
+
+ throws_ok {
+ $account_payment->void( { interface => 'intranet' } );
+ }
+ 'Koha::Exceptions::MissingParameter',
+ "->void() requires the `staff_id` parameter is passed when `interface` equals 'intranet'";
+ throws_ok {
+ $account_payment->void( { interface => 'intranet', staff_id => $borrower->borrowernumber } );
+ }
+ 'Koha::Exceptions::MissingParameter',
+ "->void() requires the `branch` parameter is passed when `interface` equals 'intranet'";
+
+ my $void = $account_payment->void({ interface => 'test' });
- is( ref($ret), 'Koha::Account::Line', 'Void returns the account line' );
+ is( ref($void), 'Koha::Account::Line', 'Void returns the account line' );
+ is( $void->debit_type_code, 'VOID', 'Void returns the VOID account line' );
+ is( $void->manager_id, undef, 'Void proceeds without manager_id OK if interface is not "intranet"' );
+ is( $void->branchcode, undef, 'Void proceeds without branchcode OK if interface is not "intranet"' );
is( $account->balance(), 30, "Account balance is again 30" );
$account_payment->_result->discard_changes();
is( $account_payment->credit_type_code, 'PAYMENT', 'Voided payment credit_type_code is still PAYMENT' );
is( $account_payment->status, 'VOID', 'Voided payment status is VOID' );
- is( $account_payment->amount+0, 0, 'Voided payment amount is 0' );
+ is( $account_payment->amount+0, -30, 'Voided payment amount is still -30' );
is( $account_payment->amountoutstanding+0, 0, 'Voided payment amount outstanding is 0' );
is( $line1->amountoutstanding+0, 10, 'First fee again has amount outstanding of 10' );
is( $line2->amountoutstanding+0, 20, 'Second fee again has amount outstanding of 20' );
- # Accountlines that are not credits should be un-voidable
- my $line1_pre = $line1->unblessed();
- $ret = $line1->void();
- $line1->_result->discard_changes();
- my $line1_post = $line1->unblessed();
- is( $ret, undef, 'Attempted void on non-credit returns undef' );
- is_deeply( $line1_pre, $line1_post, 'Non-credit account line cannot be voided' );
+ my $credit2 = $account->add_credit( { interface => 'test', amount => 10 } );
+ $void = $credit2->void(
+ {
+ interface => 'intranet',
+ staff_id => $borrower->borrowernumber,
+ branch => $branchcode
+ }
+ );
+ is( $void->manager_id, $borrower->borrowernumber, "->void stores the manager_id when it's passed");
+ is( $void->branchcode, $branchcode, "->void stores the branchcode when it's passed");
$schema->storage->txn_rollback;
};
subtest "reduce() tests" => sub {
- plan tests => 27;
+ plan tests => 29;
$schema->storage->txn_begin;
my $reduce_params = {
interface => 'commandline',
- reduction_type => 'REFUND',
+ reduction_type => 'DISCOUNT',
amount => 5,
staff_id => $staff->borrowernumber,
branch => $branchcode
'->reduce() cannot reduce more than original amount';
# Partial Reduction
- # (Refund 5 on debt of 20)
+ # (Discount 5 on debt of 20)
my $reduction = $debit1->reduce($reduce_params);
is( ref($reduction), 'Koha::Account::Line',
0, "Reduce amountoutstanding is 0" );
is( $debit1->amountoutstanding() * 1,
15, "Debit amountoutstanding reduced by 5 to 15" );
+ is( $debit1->status(), 'DISCOUNTED', "Debit status updated to DISCOUNTED");
is( $account->balance() * 1, -5, "Account balance is -5" );
is( $reduction->status(), 'APPLIED', "Reduction status is 'APPLIED'" );
my $THE_offset = $offsets->next;
is( $THE_offset->amount * 1,
-5, 'Correct amount was applied against debit' );
- is( $THE_offset->type, 'REFUND', "Offset type set to 'REFUND'" );
+ is( $THE_offset->type, 'DISCOUNT', "Offset type set to 'DISCOUNT'" );
# Zero offset created when zero outstanding
# (Refund another 5 on paid debt of 20)
$credit1->apply( { debits => [$debit1] } );
is( $debit1->amountoutstanding + 0,
0, 'Debit1 amountoutstanding reduced to 0' );
+ $reduce_params->{reduction_type} = 'REFUND';
$reduction = $debit1->reduce($reduce_params);
is( $reduction->amount() * 1, -5, "Reduce amount is -5" );
is( $reduction->amountoutstanding() * 1,
-5, "Reduce amountoutstanding is -5" );
+ is( $debit1->status(), 'REFUNDED', "Debit status updated to REFUNDED");
$offsets = Koha::Account::Offsets->search(
{ credit_id => $reduction->id, debit_id => $debit1->id } );
$schema->storage->txn_rollback;
};
+subtest "cancel() tests" => sub {
+ plan tests => 16;
+
+ $schema->storage->txn_begin;
+
+ my $library = $builder->build_object( { class => 'Koha::Libraries' });
+ my $patron = $builder->build_object({ class => 'Koha::Patrons', value => { branchcode => $library->branchcode } });
+ my $staff = $builder->build_object({ class => 'Koha::Patrons', value => { branchcode => $library->branchcode } });
+
+ t::lib::Mocks::mock_userenv({ patron => $patron });
+
+ my $account = Koha::Account->new( { patron_id => $patron->borrowernumber } );
+
+ my $debit1 = Koha::Account::Line->new(
+ {
+ borrowernumber => $patron->borrowernumber,
+ amount => 10,
+ amountoutstanding => 10,
+ interface => 'commandline',
+ debit_type_code => 'OVERDUE',
+ }
+ )->store();
+ my $debit2 = Koha::Account::Line->new(
+ {
+ borrowernumber => $patron->borrowernumber,
+ amount => 20,
+ amountoutstanding => 20,
+ interface => 'commandline',
+ debit_type_code => 'OVERDUE',
+ }
+ )->store();
+
+ my $ret = $account->pay(
+ {
+ lines => [$debit2],
+ amount => 5,
+ }
+ );
+ my $credit = Koha::Account::Lines->find({ accountlines_id => $ret->{payment_id} });
+
+ is( $account->balance(), 25, "Account balance is 25" );
+ is( $debit1->amountoutstanding + 0,
+ 10, 'First fee has amount outstanding of 10' );
+ is( $debit2->amountoutstanding + 0,
+ 15, 'Second fee has amount outstanding of 15' );
+ throws_ok {
+ $credit->cancel(
+ { staff_id => $staff->borrowernumber, branch => $library->branchcode } );
+ }
+ 'Koha::Exceptions::Account::IsNotDebit',
+ '->cancel() can only be used with debits';
+
+ throws_ok {
+ $debit1->reduce( { staff_id => $staff->borrowernumber } );
+ }
+ 'Koha::Exceptions::MissingParameter',
+ "->cancel() requires the `branch` parameter is passed";
+ throws_ok {
+ $debit1->reduce( { branch => $library->branchcode } );
+ }
+ 'Koha::Exceptions::MissingParameter',
+ "->cancel() requires the `staff_id` parameter is passed";
+
+ throws_ok {
+ $debit2->cancel(
+ { staff_id => $staff->borrowernumber, branch => $library->branchcode } );
+ }
+ 'Koha::Exceptions::Account',
+ '->cancel() can only be used with debits that have not been offset';
+
+ my $cancellation = $debit1->cancel(
+ { staff_id => $staff->borrowernumber, branch => $library->branchcode } );
+ is( ref($cancellation), 'Koha::Account::Line',
+ 'Cancel returns an account line' );
+ is(
+ $cancellation->amount() * 1,
+ $debit1->amount * -1,
+ "Cancellation amount is " . $debit1->amount
+ );
+ is( $cancellation->amountoutstanding() * 1,
+ 0, "Cancellation amountoutstanding is 0" );
+ is( $debit1->amountoutstanding() * 1,
+ 0, "Debit amountoutstanding reduced to 0" );
+ is( $debit1->status(), 'CANCELLED', "Debit status updated to CANCELLED" );
+ is( $account->balance() * 1, 15, "Account balance is 15" );
+
+ my $offsets = Koha::Account::Offsets->search(
+ { credit_id => $cancellation->id, debit_id => $debit1->id } );
+ is( $offsets->count, 1, 'Only one offset is generated' );
+ my $THE_offset = $offsets->next;
+ is( $THE_offset->amount * 1,
+ -10, 'Correct amount was applied against debit' );
+ is( $THE_offset->type, 'CANCELLATION',
+ "Offset type set to 'CANCELLATION'" );
+
+ $schema->storage->txn_rollback;
+};
+
1;