use C4::Circulation qw( MarkIssueReturned );
BEGIN {
- use_ok('C4::Accounts');
+ use_ok('C4::Accounts', qw( chargelostitem purge_zero_balance_fees ));
use_ok('Koha::Object');
use_ok('Koha::Patron');
use_ok('Data::Dumper');
$dbh->do(q|DELETE FROM accountlines|);
-subtest "Koha::Account::pay tests" => sub {
+subtest "Koha::Account::pay - always AutoReconcile + notes tests" => sub {
- plan tests => 13;
+ plan tests => 17;
# Create a borrower
my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
# There is $100 in the account
$sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
- my $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
- my $amountleft = 0;
- for my $line ( @$amountoutstanding ) {
- $amountleft += $line;
- }
- is($amountleft, 300, 'The account has 300$ as expected' );
+ my $outstanding_debt = $account->outstanding_debits->total_outstanding;
+ is($outstanding_debt, 300, 'The account has $300 outstanding debt as expected' );
+ my $outstanding_credit = $account->outstanding_credits->total_outstanding;
+ is($outstanding_credit, 0, 'The account has $0 outstanding credit as expected' );
# We make a $20 payment
my $borrowernumber = $borrower->borrowernumber;
is( $accountline->payment_type, "TEST_TYPE", "Payment type passed into pay is set in account line correctly" );
# There is now $280 in the account
- $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
- $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
- $amountleft = 0;
- for my $line ( @$amountoutstanding ) {
- $amountleft += $line;
- }
- is($amountleft, 280, 'The account has $280 as expected' );
+ $outstanding_debt = $account->outstanding_debits->total_outstanding;
+ is($outstanding_debt, 280, 'The account has $280 outstanding debt as expected' );
+ $outstanding_credit = $account->outstanding_credits->total_outstanding;
+ is($outstanding_credit, 0, 'The account has $0 outstanding credit as expected' );
# Is the payment note well registered
- $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
- $sth->execute($borrower->borrowernumber);
- my $note = $sth->fetchrow_array;
- is($note,'$20.00 payment note', '$20.00 payment note is registered');
+ is($accountline->note,'$20.00 payment note', '$20.00 payment note is registered');
# We attempt to make a -$30 payment (a NEGATIVE payment)
$data = '-30.00';
$payment_note = '-$30.00 payment note';
- throws_ok { $account->pay( { amount => $data, note => $payment_note } ) } qr//, 'Croaked on call to pay with negative amount';
+ throws_ok { $account->pay( { amount => $data, note => $payment_note } ) }
+ 'Koha::Exceptions::Account::AmountNotPositive',
+ 'Croaked on call to pay with negative amount';
#We make a $150 payment ( > 1stLine )
$data = '150.00';
$payment_note = '$150.00 payment note';
- $account->pay( { amount => $data, note => $payment_note } );
+ $id = $account->pay( { amount => $data, note => $payment_note } )->{payment_id};
# There is now $130 in the account
- $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
- $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
- $amountleft = 0;
- for my $line ( @$amountoutstanding ) {
- $amountleft += $line;
- }
- is($amountleft, 130, 'The account has $130 as expected' );
+ $outstanding_debt = $account->outstanding_debits->total_outstanding;
+ is($outstanding_debt, 130, 'The account has $130 outstanding debt as expected' );
+ $outstanding_credit = $account->outstanding_credits->total_outstanding;
+ is($outstanding_credit, 0, 'The account has $0 outstanding credit as expected' );
# Is the payment note well registered
- $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
- $sth->execute($borrower->borrowernumber);
- $note = $sth->fetchrow_array;
- is($note,'$150.00 payment note', '$150.00 payment note is registered');
+ $accountline = Koha::Account::Lines->find( $id );
+ is($accountline->note,'$150.00 payment note', '$150.00 payment note is registered');
#We make a $200 payment ( > amountleft )
$data = '200.00';
$payment_note = '$200.00 payment note';
- $account->pay( { amount => $data, note => $payment_note } );
+ $id = $account->pay( { amount => $data, note => $payment_note } )->{payment_id};
# There is now -$70 in the account
- $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
- $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
- $amountleft = 0;
- for my $line ( @$amountoutstanding ) {
- $amountleft += $line;
- }
- is($amountleft, -70, 'The account has -$70 as expected, (credit situation)' );
+ $outstanding_debt = $account->outstanding_debits->total_outstanding;
+ is($outstanding_debt, 0, 'The account has $0 outstanding debt as expected' );
+ $outstanding_credit = $account->outstanding_credits->total_outstanding;
+ is($outstanding_credit, -70, 'The account has -$70 outstanding credit as expected' );
# Is the payment note well registered
- $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
- $sth->execute($borrower->borrowernumber);
- $note = $sth->fetchrow_array;
- is($note,'$200.00 payment note', '$200.00 payment note is registered');
+ $accountline = Koha::Account::Lines->find( $id );
+ is($accountline->note,'$200.00 payment note', '$200.00 payment note is registered');
my $line3 = $account->add_debit({ type => 'ACCOUNT', amount => 42, interface => 'commandline' });
- my $payment_id = $account->pay( { lines => [$line3], amount => 42 } )->{payment_id};
- my $payment = Koha::Account::Lines->find( $payment_id );
- is( $payment->amount()+0, -42, "Payment paid the specified fine" );
+ $id = $account->pay( { lines => [$line3], amount => 42 } )->{payment_id};
+ $accountline = Koha::Account::Lines->find( $id );
+ is( $accountline->amount()+0, -42, "Payment paid the specified fine" );
$line3 = Koha::Account::Lines->find( $line3->id );
is( $line3->amountoutstanding+0, 0, "Specified fine is paid" );
- is( $payment->branchcode, undef, 'branchcode passed, then undef' );
+ is( $accountline->branchcode, undef, 'branchcode passed, then undef' );
};
subtest "Koha::Account::pay particular line tests" => sub {
subtest "More Koha::Account::pay tests" => sub {
- plan tests => 8;
+ plan tests => 12;
# Create a borrower
my $category = $builder->build({ source => 'Category' })->{ categorycode };
my $account = Koha::Account->new( { patron_id => $borrowernumber } );
my $line = Koha::Account::Lines->find( $accountline->{ accountlines_id } );
# make the full payment
- $account->pay({ lines => [$line], amount => $amount, library_id => $branch, note => 'A payment note' });
+ my $payment = $account->pay(
+ {
+ lines => [$line],
+ amount => $amount,
+ library_id => $branch,
+ note => 'A payment note'
+ }
+ );
- my $offset = Koha::Account::Offsets->search({ debit_id => $accountline->{accountlines_id} })->next();
- is( $offset->amount+0, -100, 'Offset amount is -100.00' );
- is( $offset->type(), 'Payment', 'Offset type is Payment' );
+ my $offsets = Koha::Account::Offsets->search({ credit_id => $payment->{payment_id} });
+ is( $offsets->count, 2, 'Two offsets recorded');
+ my $offset = $offsets->next;
+ is( $offset->type(), 'CREATE', 'First offset type is CREATE' );
+ is( $offset->amount+0, 100, 'First offset amount is 100.00' );
+ $offset = $offsets->next;
+ is( $offset->type(), 'APPLY', 'Second offset type is APPLY' );
+ is( $offset->amount+0, -100, 'Second offset amount is -100.00' );
+ is( $offset->debit_id, $accountline->{accountlines_id}, 'Second offset is against the right accountline');
my $stat = $schema->resultset('Statistic')->search({
branch => $branch,
subtest "Even more Koha::Account::pay tests" => sub {
- plan tests => 8;
+ plan tests => 12;
# Create a borrower
my $category = $builder->build({ source => 'Category' })->{ categorycode };
my $account = Koha::Account->new( { patron_id => $borrowernumber } );
my $line = Koha::Account::Lines->find( $accountline->{ accountlines_id } );
# make the full payment
- $account->pay({ lines => [$line], amount => $partialamount, library_id => $branch, note => 'A payment note' });
+ my $payment = $account->pay(
+ {
+ lines => [$line],
+ amount => $partialamount,
+ library_id => $branch,
+ note => 'A payment note'
+ }
+ );
- my $offset = Koha::Account::Offsets->search( { debit_id => $accountline->{ accountlines_id } } )->next();
- is( $offset->amount+0, -60, 'Offset amount is -60.00' );
- is( $offset->type, 'Payment', 'Offset type is payment' );
+ my $offsets = Koha::Account::Offsets->search({ credit_id => $payment->{payment_id} });
+ is( $offsets->count, 2, 'Two offsets recorded');
+ my $offset = $offsets->next;
+ is( $offset->type(), 'CREATE', 'First offset type is CREATE' );
+ is( $offset->amount+0, 60, 'First offset amount is 60.00' );
+ $offset = $offsets->next;
+ is( $offset->type(), 'APPLY', 'Second offset type is APPLY' );
+ is( $offset->amount+0, -60, 'Second offset amount is -60.00' );
+ is( $offset->debit_id, $accountline->{accountlines_id}, 'Second offset is against the right accountline');
my $stat = $schema->resultset('Statistic')->search({
branch => $branch,
my $cli_itemnumber2 = $builder->build_sample_item({ itype => $itype_replace_no_fee->{itemtype} })->itemnumber;
my $cli_itemnumber3 = $builder->build_sample_item({ itype => $itype_no_replace_fee->{itemtype} })->itemnumber;
my $cli_itemnumber4 = $builder->build_sample_item({ itype => $itype_replace_fee->{itemtype} })->itemnumber;
+ my $cli_itemnumber5 = $builder->build_sample_item({ itype => $itype_replace_fee->{itemtype} })->itemnumber;
+ my $cli_bundle1 = $builder->build_sample_item({ itype => $itype_no_replace_no_fee->{itemtype} })->itemnumber;
+ $schema->resultset('ItemBundle')->create( { host => $cli_bundle1, item => $cli_itemnumber5 } );
my $cli_issue_id_1 = $builder->build({ source => 'Issue', value => { borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1 } })->{issue_id};
my $cli_issue_id_2 = $builder->build({ source => 'Issue', value => { borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2 } })->{issue_id};
my $cli_issue_id_3 = $builder->build({ source => 'Issue', value => { borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3 } })->{issue_id};
my $cli_issue_id_4 = $builder->build({ source => 'Issue', value => { borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4 } })->{issue_id};
my $cli_issue_id_4X = undef;
+ my $cli_bundle_issue = $builder->build({ source => 'Issue', value => { borrowernumber => $cli_borrowernumber, itemnumber => $cli_bundle1 } })->{issue_id};
my $lostfine;
my $procfee;
subtest "fee application tests" => sub {
- plan tests => 44;
+ plan tests => 48;
t::lib::Mocks::mock_preference('item-level_itypes', '1');
t::lib::Mocks::mock_preference('useDefaultReplacementCost', '0');
ok( $procfees->count == 2, "Processing fee can be charged twice for the same item if they are distinct issue_id's");
$lostfines->delete();
$procfees->delete();
+
+ C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber5, 6.12, "Bundle");
+ $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber5, debit_type_code => 'LOST' });
+ $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber5, debit_type_code => 'PROCESSING' });
+ is( $lostfine->amount, "6.120000", "Lost fine equals replacementcost when pref on and default set (Bundle)");
+ is( $procfee->amount, "2.040000", "Processing fee if processing fee (Bundle)");
+ is( $lostfine->issue_id, $cli_bundle_issue, "Lost fine issue id matched to bundle issue");
+ is( $procfee->issue_id, $cli_bundle_issue, "Processing fee issue id matched to bundle issue");
+ $lostfine->delete();
+ $procfee->delete();
};
subtest "basic fields tests" => sub {
{
credit_id => $credit->id,
debit_id => $debit->id,
- type => 'Payment',
+ type => 'APPLY',
amount => 0
}
)->store();
{
credit_id => $credit->id,
debit_id => $debit->id,
- type => 'Payment',
+ type => 'APPLY',
amount => 0
}
)->store();
{
credit_id => $credit->id,
debit_id => $debit->id,
- type => 'Payment',
+ type => 'APPLY',
amount => 0
}
)->store();
{
credit_id => undef,
debit_id => undef,
- type => 'Payment',
+ type => 'CREATE',
amount => 0,
}
)->store();