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,
is( $line->issue_id, $issue->id, 'Account line issue id matches' );
my $offset = Koha::Account::Offsets->search({ debit_id => $line->id })->next();
- is( $offset->type, 'OVERDUE', 'Account offset type is Fine' );
+ is( $offset->type, 'CREATE', 'Account offset type is CREATE' );
is( $offset->amount+0, 15, 'Account offset amount is 15.00' );
t::lib::Mocks::mock_preference('WhenLostForgiveFine','0');
C4::Circulation::_FixOverduesOnReturn( $patron->{borrowernumber}, $item->itemnumber, 1, 'RETURNED' );
$accountline->_result()->discard_changes();
- my $offset = Koha::Account::Offsets->search({ debit_id => $accountline->id, type => 'Forgiven' })->next();
+ my $offset = Koha::Account::Offsets->search({ debit_id => $accountline->id, type => 'APPLY' })->next();
is( $accountline->amountoutstanding + 0, 0, 'Fine amountoutstanding has been reduced to 0' );
isnt( $accountline->status, 'UNRETURNED', 'Open fine ( account type OVERDUE ) has been closed out ( status not UNRETURNED )');
is( $accountline->debit_type_code, "LOST", "Debit type is LOST" );
my $offset = Koha::Account::Offsets->search({ debit_id => $accountline->id })->next();
-is( $offset->type, 'Lost Item', 'Got correct offset type' );
+is( $offset->type, 'CREATE', 'Got CREATE offset type' );
is( $offset->amount+0, 10, 'Got amount of $10.00' );
$schema->storage->txn_rollback;
my $offsets = Koha::Account::Offsets->search( { debit_id => $payout->id } );
is( $offsets->count, 4, 'Four offsets generated' );
my $offset = $offsets->next;
- is( $offset->type, 'PAYOUT', 'PAYOUT offset added for payout line' );
+ is( $offset->type, 'CREATE', 'CREATE offset added for payout line' );
is( $offset->amount * 1, 10, 'Correct offset amount recorded' );
$offset = $offsets->next;
is( $offset->credit_id, $credit_1->id, "Offset added against credit_1");
- is( $offset->type, 'PAYOUT', "PAYOUT used for offset_type" );
+ is( $offset->type, 'APPLY', "APPLY used for offset_type" );
is( $offset->amount * 1, -2, 'Correct amount offset against credit_1' );
$offset = $offsets->next;
is( $offset->credit_id, $credit_2->id, "Offset added against credit_2");
- is( $offset->type, 'PAYOUT', "PAYOUT used for offset_type" );
+ is( $offset->type, 'APPLY', "APPLY used for offset_type" );
is( $offset->amount * 1, -3, 'Correct amount offset against credit_2' );
$offset = $offsets->next;
is( $offset->credit_id, $credit_3->id, "Offset added against credit_3");
- is( $offset->type, 'PAYOUT', "PAYOUT used for offset_type" );
+ is( $offset->type, 'APPLY', "APPLY used for offset_type" );
is( $offset->amount * 1, -5, 'Correct amount offset against credit_3' );
my $credit_5 = $account->add_credit( { amount => 5, interface => 'commandline' } );
$offsets = Koha::Account::Offsets->search( { debit_id => $payout->id } );
is( $offsets->count, 2, 'Two offsets generated' );
$offset = $offsets->next;
- is( $offset->type, 'PAYOUT', 'PAYOUT offset added for payout line' );
+ is( $offset->type, 'CREATE', 'CREATE offset added for payout line' );
is( $offset->amount * 1, 2.50, 'Correct offset amount recorded' );
$offset = $offsets->next;
is( $offset->credit_id, $credit_5->id, "Offset added against credit_5");
- is( $offset->type, 'PAYOUT', "PAYOUT used for offset_type" );
+ is( $offset->type, 'APPLY', "APPLY used for offset_type" );
is( $offset->amount * 1, -2.50, 'Correct amount offset against credit_5' );
$schema->storage->txn_rollback;
my $offsets = Koha::Account::Offsets->search( { credit_id => $payin->id } );
is( $offsets->count, 4, 'Four offsets generated' );
my $offset = $offsets->next;
- is( $offset->type, 'Payment', 'Payment offset added for payin line' );
+ is( $offset->type, 'CREATE', 'CREATE offset added for payin line' );
is( $offset->amount * 1, -10, 'Correct offset amount recorded' );
$offset = $offsets->next;
is( $offset->debit_id, $debit_1->id, "Offset added against debit_1");
- is( $offset->type, 'Payment', "Payment used for offset_type" );
+ is( $offset->type, 'APPLY', "APPLY used for offset_type" );
is( $offset->amount * 1, -2, 'Correct amount offset against debit_1' );
$offset = $offsets->next;
is( $offset->debit_id, $debit_2->id, "Offset added against debit_2");
- is( $offset->type, 'Payment', "Payment used for offset_type" );
+ is( $offset->type, 'APPLY', "APPLY used for offset_type" );
is( $offset->amount * 1, -3, 'Correct amount offset against debit_2' );
$offset = $offsets->next;
is( $offset->debit_id, $debit_3->id, "Offset added against debit_3");
- is( $offset->type, 'Payment', "Payment used for offset_type" );
+ is( $offset->type, 'APPLY', "APPLY used for offset_type" );
is( $offset->amount * 1, -5, 'Correct amount offset against debit_3' );
my $debit_5 = $account->add_debit( { amount => 5, interface => 'commandline', type => 'OVERDUE' } );
$offsets = Koha::Account::Offsets->search( { credit_id => $payin->id } );
is( $offsets->count, 2, 'Two offsets generated' );
$offset = $offsets->next;
- is( $offset->type, 'Payment', 'Payment offset added for payin line' );
+ is( $offset->type, 'CREATE', 'CREATE offset added for payin line' );
is( $offset->amount * 1, -2.50, 'Correct offset amount recorded' );
$offset = $offsets->next;
is( $offset->debit_id, $debit_5->id, "Offset added against debit_5");
- is( $offset->type, 'Payment', "Payment used for offset_type" );
+ is( $offset->type, 'APPLY', "APPLY used for offset_type" );
is( $offset->amount * 1, -2.50, 'Correct amount offset against debit_5' );
$schema->storage->txn_rollback;
$debit_1->discard_changes;
my $debits = Koha::Account::Lines->search({ accountlines_id => $debit_1->id });
- $credit = $credit->apply( { debits => [ $debits->as_list ], offset_type => 'Manual Credit' } );
+ $credit = $credit->apply( { debits => [ $debits->as_list ] } );
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' );
is( $offsets->count, 1, 'Only one offset is generated' );
$THE_offset = $offsets->next;
is( $THE_offset->amount * 1, -90, 'Amount was calculated correctly (less than the available credit)' );
- is( $THE_offset->type, 'Credit Applied', 'Defaults to \'Credit Applied\' offset type' );
+ is( $THE_offset->type, 'APPLY', 'Defaults to \'APPLY\' offset type' );
$debits = Koha::Account::Lines->search({ accountlines_id => $debit_1->id });
throws_ok
$debits = Koha::Account::Lines->search({ accountlines_id => { -in => [ $debit_1->id, $debit_2->id, $debit_3->id, $credit->id ] } });
throws_ok {
- $credit_2->apply( { debits => [ $debits->as_list ], offset_type => 'Manual Credit' } ); }
+ $credit_2->apply( { debits => [ $debits->as_list ] }
'Koha::Exceptions::Account::IsNotDebit',
'->apply() rolls back if any of the passed lines is not a debit';
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 ] } });
- $credit_2 = $credit_2->apply( { debits => [ $debits->as_list ], offset_type => 'Manual Credit' } );
+ $credit_2 = $credit_2->apply( { debits => [ $debits->as_list ] } );
is( $debit_1->discard_changes->amountoutstanding * 1, 0, 'No changes to already cancelled debit' );
is( $debit_2->discard_changes->amountoutstanding * 1, 0, 'Debit cancelled' );
}
);
my $debits_renew = Koha::Account::Lines->search({ accountlines_id => $accountline->id })->as_list;
- $credit_forgive = $credit_forgive->apply( { debits => $debits_renew, offset_type => 'Forgiven' } );
+ $credit_forgive = $credit_forgive->apply( { debits => $debits_renew } );
is( $called, 0, 'C4::Circulation::AddRenew NOT called when RenewAccruingItemWhenPaid enabled but credit type is "FORGIVEN"' );
$accountline = Koha::Account::Line->new(
)->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' } );
+ $credit_renew = $credit_renew->apply( { debits => $debits_renew } );
is( $called, 1, 'RenewAccruingItemWhenPaid causes C4::Circulation::AddRenew to be called when appropriate' );
my @messages = @{$credit_renew->messages};
# Update fine to partially paid
my $debits = Koha::Account::Lines->search({ accountlines_id => $debit_2->id });
- $credit->apply( { debits => [ $debits->as_list ], offset_type => 'Manual Credit' } );
+ $credit->apply( { debits => [ $debits->as_list ] } );
$debit_2->discard_changes;
is( $debit_2->amount * 1, 150, 'Fine amount unaffected by partial payment' );
subtest "reduce() tests" => sub {
- plan tests => 29;
+ plan tests => 34;
$schema->storage->txn_begin;
is( $reduction->status(), 'APPLIED', "Reduction status is 'APPLIED'" );
my $offsets = Koha::Account::Offsets->search(
- { credit_id => $reduction->id, debit_id => $debit1->id } );
- is( $offsets->count, 1, 'Only one offset is generated' );
+ { credit_id => $reduction->id } );
+ is( $offsets->count, 2, 'Two offsets generated' );
my $THE_offset = $offsets->next;
+ is( $THE_offset->type, 'CREATE', 'CREATE offset added for discount line');
is( $THE_offset->amount * 1,
- -5, 'Correct amount was applied against debit' );
- is( $THE_offset->type, 'DISCOUNT', "Offset type set to 'DISCOUNT'" );
+ -5, 'Correct offset amount recorded');
+ $THE_offset = $offsets->next;
+ is( $THE_offset->type, 'APPLY', "APPLY offset added for 'DISCOUNT'" );
+ is( $THE_offset->amount * 1, -5, 'Correct amount offset against debt');
+ is( $THE_offset->debit_id, $debit1->accountlines_id, 'APPLY offset recorded the correct debit_id');
# Zero offset created when zero outstanding
# (Refund another 5 on paid debt of 20)
is( $debit1->status(), 'REFUNDED', "Debit status updated to REFUNDED");
$offsets = Koha::Account::Offsets->search(
- { credit_id => $reduction->id, debit_id => $debit1->id } );
- is( $offsets->count, 1, 'Only one new offset is generated' );
+ { credit_id => $reduction->id } );
+ is( $offsets->count, 2, 'Two offsets generated' );
$THE_offset = $offsets->next;
+ is( $THE_offset->type, 'CREATE', 'CREATE offset added for refund line');
+ is( $THE_offset->amount * 1,
+ -5, 'Correct offset amount recorded');
+ $THE_offset = $offsets->next;
+ is( $THE_offset->type, 'APPLY', "APPLY offset added for 'REFUND'" );
is( $THE_offset->amount * 1,
0, 'Zero offset created for already paid off debit' );
- is( $THE_offset->type, 'REFUND', "Offset type set to 'REFUND'" );
# Compound reduction should not allow more than original amount
# (Reduction of 5 + 5 + 20 > 20)
};
subtest "cancel() tests" => sub {
- plan tests => 16;
+ plan tests => 18;
$schema->storage->txn_begin;
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' );
+ { credit_id => $cancellation->id } );
+ is( $offsets->count, 2, 'Two offsets are generated' );
my $THE_offset = $offsets->next;
+ is( $THE_offset->type, 'CREATE', 'CREATE offset added for cancel line');
+ is( $THE_offset->amount * 1, -10, 'Correct offset amount recorded' );
+ $THE_offset = $offsets->next;
+ is( $THE_offset->type, 'APPLY', "APPLY offset added" );
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;
};
interface => 'test',
}
);
- $credit->apply(
- { debits => [ $debts->as_list ], offset_type => 'Writeoff' } );
+ $credit->apply( { debits => [ $debts->as_list ] } );
# Simulate item marked as found
$item->itemlost(0)->store;
interface => 'test',
}
);
- $credit->apply(
- { debits => [ $debts->as_list ], offset_type => 'Payment' } );
+ $credit->apply( { debits => [ $debts->as_list ] } );
# Simulate item marked as found
$item->itemlost(0)->store;
}
);
- $payment->apply(
- { debits => [$lost_fee_line], offset_type => 'Payment' } );
+ $payment->apply( { debits => [$lost_fee_line] } );
# Partially write off fee
my $write_off_amount = 25;
interface => 'test',
}
);
- $write_off->apply(
- { debits => [$lost_fee_line], offset_type => 'Writeoff' } );
+ $write_off->apply( { debits => [$lost_fee_line] } );
is(
$account->balance,
interface => 'test',
}
);
- $payment->apply(
- { debits => [$lost_fee_line], offset_type => 'Payment' } );
+ $payment->apply( { debits => [$lost_fee_line] } );
is(
$account->balance,
interface => 'test',
}
);
- $credit->apply(
- { debits => [ $debts->as_list ], offset_type => 'Payment' } );
+ $credit->apply( { debits => [ $debts->as_list ] } );
# Simulate item marked as found
$item->itemlost(0)->store;
interface => 'test',
}
);
- $credit->apply(
- { debits => [ $debts->as_list ], offset_type => 'Payment' } );
+ $credit->apply( { debits => [ $debts->as_list ] } );
# Fine not forgiven
my $overdue = $account->add_debit(
interface => 'test',
}
);
- $credit->apply(
- { debits => [ $debts->as_list ], offset_type => 'Payment' } );
+ $credit->apply( { debits => [ $debts->as_list ] } );
# Add overdue
my $overdue = $account->add_debit(
item_id => $item->itemnumber
}
);
- $credit->apply(
- { debits => [$overdue], offset_type => 'Forgiven' } );
+ $credit->apply( { debits => [$overdue] } );
# Simulate item marked as found
$item->itemlost(0)->store;
note => 'He paid!',
description => 'Finally!',
library_id => $patron->branchcode,
- account_type => 'PAYMENT',
- offset_type => 'Payment'
+ account_type => 'PAYMENT'
}
);