Lines 208-214
subtest 'apply() tests' => sub {
Link Here
|
208 |
$debit_1->discard_changes; |
208 |
$debit_1->discard_changes; |
209 |
|
209 |
|
210 |
my $debits = Koha::Account::Lines->search({ accountlines_id => $debit_1->id }); |
210 |
my $debits = Koha::Account::Lines->search({ accountlines_id => $debit_1->id }); |
211 |
$credit = $credit->apply( { debits => [ $debits->as_list ], offset_type => 'Manual Credit' } ); |
211 |
$credit = $credit->apply( { debits => [ $debits->as_list ] } ); |
212 |
is( ref($credit), 'Koha::Account::Line', '->apply returns the updated Koha::Account::Line credit object'); |
212 |
is( ref($credit), 'Koha::Account::Line', '->apply returns the updated Koha::Account::Line credit object'); |
213 |
is( $credit->amountoutstanding * -1, 90, 'Remaining credit is correctly calculated' ); |
213 |
is( $credit->amountoutstanding * -1, 90, 'Remaining credit is correctly calculated' ); |
214 |
|
214 |
|
Lines 232-238
subtest 'apply() tests' => sub {
Link Here
|
232 |
is( $offsets->count, 1, 'Only one offset is generated' ); |
232 |
is( $offsets->count, 1, 'Only one offset is generated' ); |
233 |
$THE_offset = $offsets->next; |
233 |
$THE_offset = $offsets->next; |
234 |
is( $THE_offset->amount * 1, -90, 'Amount was calculated correctly (less than the available credit)' ); |
234 |
is( $THE_offset->amount * 1, -90, 'Amount was calculated correctly (less than the available credit)' ); |
235 |
is( $THE_offset->type, 'Credit Applied', 'Defaults to \'Credit Applied\' offset type' ); |
235 |
is( $THE_offset->type, 'APPLY', 'Defaults to \'APPLY\' offset type' ); |
236 |
|
236 |
|
237 |
$debits = Koha::Account::Lines->search({ accountlines_id => $debit_1->id }); |
237 |
$debits = Koha::Account::Lines->search({ accountlines_id => $debit_1->id }); |
238 |
throws_ok |
238 |
throws_ok |
Lines 266-272
subtest 'apply() tests' => sub {
Link Here
|
266 |
|
266 |
|
267 |
$debits = Koha::Account::Lines->search({ accountlines_id => { -in => [ $debit_1->id, $debit_2->id, $debit_3->id, $credit->id ] } }); |
267 |
$debits = Koha::Account::Lines->search({ accountlines_id => { -in => [ $debit_1->id, $debit_2->id, $debit_3->id, $credit->id ] } }); |
268 |
throws_ok { |
268 |
throws_ok { |
269 |
$credit_2->apply( { debits => [ $debits->as_list ], offset_type => 'Manual Credit' } ); } |
269 |
$credit_2->apply( { debits => [ $debits->as_list ] } |
270 |
'Koha::Exceptions::Account::IsNotDebit', |
270 |
'Koha::Exceptions::Account::IsNotDebit', |
271 |
'->apply() rolls back if any of the passed lines is not a debit'; |
271 |
'->apply() rolls back if any of the passed lines is not a debit'; |
272 |
|
272 |
|
Lines 276-282
subtest 'apply() tests' => sub {
Link Here
|
276 |
is( $credit_2->discard_changes->amountoutstanding * -1, 20, 'No changes made' ); |
276 |
is( $credit_2->discard_changes->amountoutstanding * -1, 20, 'No changes made' ); |
277 |
|
277 |
|
278 |
$debits = Koha::Account::Lines->search({ accountlines_id => { -in => [ $debit_1->id, $debit_2->id, $debit_3->id ] } }); |
278 |
$debits = Koha::Account::Lines->search({ accountlines_id => { -in => [ $debit_1->id, $debit_2->id, $debit_3->id ] } }); |
279 |
$credit_2 = $credit_2->apply( { debits => [ $debits->as_list ], offset_type => 'Manual Credit' } ); |
279 |
$credit_2 = $credit_2->apply( { debits => [ $debits->as_list ] } ); |
280 |
|
280 |
|
281 |
is( $debit_1->discard_changes->amountoutstanding * 1, 0, 'No changes to already cancelled debit' ); |
281 |
is( $debit_1->discard_changes->amountoutstanding * 1, 0, 'No changes to already cancelled debit' ); |
282 |
is( $debit_2->discard_changes->amountoutstanding * 1, 0, 'Debit cancelled' ); |
282 |
is( $debit_2->discard_changes->amountoutstanding * 1, 0, 'Debit cancelled' ); |
Lines 333-339
subtest 'apply() tests' => sub {
Link Here
|
333 |
} |
333 |
} |
334 |
); |
334 |
); |
335 |
my $debits_renew = Koha::Account::Lines->search({ accountlines_id => $accountline->id })->as_list; |
335 |
my $debits_renew = Koha::Account::Lines->search({ accountlines_id => $accountline->id })->as_list; |
336 |
$credit_forgive = $credit_forgive->apply( { debits => $debits_renew, offset_type => 'Forgiven' } ); |
336 |
$credit_forgive = $credit_forgive->apply( { debits => $debits_renew } ); |
337 |
is( $called, 0, 'C4::Circulation::AddRenew NOT called when RenewAccruingItemWhenPaid enabled but credit type is "FORGIVEN"' ); |
337 |
is( $called, 0, 'C4::Circulation::AddRenew NOT called when RenewAccruingItemWhenPaid enabled but credit type is "FORGIVEN"' ); |
338 |
|
338 |
|
339 |
$accountline = Koha::Account::Line->new( |
339 |
$accountline = Koha::Account::Line->new( |
Lines 352-358
subtest 'apply() tests' => sub {
Link Here
|
352 |
)->store(); |
352 |
)->store(); |
353 |
my $credit_renew = $account->add_credit({ amount => 100, user_id => $patron->id, interface => 'commandline' }); |
353 |
my $credit_renew = $account->add_credit({ amount => 100, user_id => $patron->id, interface => 'commandline' }); |
354 |
$debits_renew = Koha::Account::Lines->search({ accountlines_id => $accountline->id })->as_list; |
354 |
$debits_renew = Koha::Account::Lines->search({ accountlines_id => $accountline->id })->as_list; |
355 |
$credit_renew = $credit_renew->apply( { debits => $debits_renew, offset_type => 'Manual Credit' } ); |
355 |
$credit_renew = $credit_renew->apply( { debits => $debits_renew } ); |
356 |
is( $called, 1, 'RenewAccruingItemWhenPaid causes C4::Circulation::AddRenew to be called when appropriate' ); |
356 |
is( $called, 1, 'RenewAccruingItemWhenPaid causes C4::Circulation::AddRenew to be called when appropriate' ); |
357 |
|
357 |
|
358 |
my @messages = @{$credit_renew->messages}; |
358 |
my @messages = @{$credit_renew->messages}; |
Lines 606-612
subtest 'adjust() tests' => sub {
Link Here
|
606 |
|
606 |
|
607 |
# Update fine to partially paid |
607 |
# Update fine to partially paid |
608 |
my $debits = Koha::Account::Lines->search({ accountlines_id => $debit_2->id }); |
608 |
my $debits = Koha::Account::Lines->search({ accountlines_id => $debit_2->id }); |
609 |
$credit->apply( { debits => [ $debits->as_list ], offset_type => 'Manual Credit' } ); |
609 |
$credit->apply( { debits => [ $debits->as_list ] } ); |
610 |
|
610 |
|
611 |
$debit_2->discard_changes; |
611 |
$debit_2->discard_changes; |
612 |
is( $debit_2->amount * 1, 150, 'Fine amount unaffected by partial payment' ); |
612 |
is( $debit_2->amount * 1, 150, 'Fine amount unaffected by partial payment' ); |
Lines 1030-1036
subtest "payout() tests" => sub {
Link Here
|
1030 |
|
1030 |
|
1031 |
subtest "reduce() tests" => sub { |
1031 |
subtest "reduce() tests" => sub { |
1032 |
|
1032 |
|
1033 |
plan tests => 29; |
1033 |
plan tests => 34; |
1034 |
|
1034 |
|
1035 |
$schema->storage->txn_begin; |
1035 |
$schema->storage->txn_begin; |
1036 |
|
1036 |
|
Lines 1153-1164
subtest "reduce() tests" => sub {
Link Here
|
1153 |
is( $reduction->status(), 'APPLIED', "Reduction status is 'APPLIED'" ); |
1153 |
is( $reduction->status(), 'APPLIED', "Reduction status is 'APPLIED'" ); |
1154 |
|
1154 |
|
1155 |
my $offsets = Koha::Account::Offsets->search( |
1155 |
my $offsets = Koha::Account::Offsets->search( |
1156 |
{ credit_id => $reduction->id, debit_id => $debit1->id } ); |
1156 |
{ credit_id => $reduction->id } ); |
1157 |
is( $offsets->count, 1, 'Only one offset is generated' ); |
1157 |
is( $offsets->count, 2, 'Two offsets generated' ); |
1158 |
my $THE_offset = $offsets->next; |
1158 |
my $THE_offset = $offsets->next; |
|
|
1159 |
is( $THE_offset->type, 'CREATE', 'CREATE offset added for discount line'); |
1159 |
is( $THE_offset->amount * 1, |
1160 |
is( $THE_offset->amount * 1, |
1160 |
-5, 'Correct amount was applied against debit' ); |
1161 |
-5, 'Correct offset amount recorded'); |
1161 |
is( $THE_offset->type, 'DISCOUNT', "Offset type set to 'DISCOUNT'" ); |
1162 |
$THE_offset = $offsets->next; |
|
|
1163 |
is( $THE_offset->type, 'APPLY', "APPLY offset added for 'DISCOUNT'" ); |
1164 |
is( $THE_offset->amount * 1, -5, 'Correct amount offset against debt'); |
1165 |
is( $THE_offset->debit_id, $debit1->accountlines_id, 'APPLY offset recorded the correct debit_id'); |
1162 |
|
1166 |
|
1163 |
# Zero offset created when zero outstanding |
1167 |
# Zero offset created when zero outstanding |
1164 |
# (Refund another 5 on paid debt of 20) |
1168 |
# (Refund another 5 on paid debt of 20) |
Lines 1173-1184
subtest "reduce() tests" => sub {
Link Here
|
1173 |
is( $debit1->status(), 'REFUNDED', "Debit status updated to REFUNDED"); |
1177 |
is( $debit1->status(), 'REFUNDED', "Debit status updated to REFUNDED"); |
1174 |
|
1178 |
|
1175 |
$offsets = Koha::Account::Offsets->search( |
1179 |
$offsets = Koha::Account::Offsets->search( |
1176 |
{ credit_id => $reduction->id, debit_id => $debit1->id } ); |
1180 |
{ credit_id => $reduction->id } ); |
1177 |
is( $offsets->count, 1, 'Only one new offset is generated' ); |
1181 |
is( $offsets->count, 2, 'Two offsets generated' ); |
1178 |
$THE_offset = $offsets->next; |
1182 |
$THE_offset = $offsets->next; |
|
|
1183 |
is( $THE_offset->type, 'CREATE', 'CREATE offset added for refund line'); |
1184 |
is( $THE_offset->amount * 1, |
1185 |
-5, 'Correct offset amount recorded'); |
1186 |
$THE_offset = $offsets->next; |
1187 |
is( $THE_offset->type, 'APPLY', "APPLY offset added for 'REFUND'" ); |
1179 |
is( $THE_offset->amount * 1, |
1188 |
is( $THE_offset->amount * 1, |
1180 |
0, 'Zero offset created for already paid off debit' ); |
1189 |
0, 'Zero offset created for already paid off debit' ); |
1181 |
is( $THE_offset->type, 'REFUND', "Offset type set to 'REFUND'" ); |
|
|
1182 |
|
1190 |
|
1183 |
# Compound reduction should not allow more than original amount |
1191 |
# Compound reduction should not allow more than original amount |
1184 |
# (Reduction of 5 + 5 + 20 > 20) |
1192 |
# (Reduction of 5 + 5 + 20 > 20) |
Lines 1209-1215
subtest "reduce() tests" => sub {
Link Here
|
1209 |
}; |
1217 |
}; |
1210 |
|
1218 |
|
1211 |
subtest "cancel() tests" => sub { |
1219 |
subtest "cancel() tests" => sub { |
1212 |
plan tests => 16; |
1220 |
plan tests => 18; |
1213 |
|
1221 |
|
1214 |
$schema->storage->txn_begin; |
1222 |
$schema->storage->txn_begin; |
1215 |
|
1223 |
|
Lines 1295-1307
subtest "cancel() tests" => sub {
Link Here
|
1295 |
is( $account->balance() * 1, 15, "Account balance is 15" ); |
1303 |
is( $account->balance() * 1, 15, "Account balance is 15" ); |
1296 |
|
1304 |
|
1297 |
my $offsets = Koha::Account::Offsets->search( |
1305 |
my $offsets = Koha::Account::Offsets->search( |
1298 |
{ credit_id => $cancellation->id, debit_id => $debit1->id } ); |
1306 |
{ credit_id => $cancellation->id } ); |
1299 |
is( $offsets->count, 1, 'Only one offset is generated' ); |
1307 |
is( $offsets->count, 2, 'Two offsets are generated' ); |
1300 |
my $THE_offset = $offsets->next; |
1308 |
my $THE_offset = $offsets->next; |
|
|
1309 |
is( $THE_offset->type, 'CREATE', 'CREATE offset added for cancel line'); |
1310 |
is( $THE_offset->amount * 1, -10, 'Correct offset amount recorded' ); |
1311 |
$THE_offset = $offsets->next; |
1312 |
is( $THE_offset->type, 'APPLY', "APPLY offset added" ); |
1301 |
is( $THE_offset->amount * 1, |
1313 |
is( $THE_offset->amount * 1, |
1302 |
-10, 'Correct amount was applied against debit' ); |
1314 |
-10, 'Correct amount was applied against debit' ); |
1303 |
is( $THE_offset->type, 'CANCELLATION', |
|
|
1304 |
"Offset type set to 'CANCELLATION'" ); |
1305 |
|
1315 |
|
1306 |
$schema->storage->txn_rollback; |
1316 |
$schema->storage->txn_rollback; |
1307 |
}; |
1317 |
}; |