|
Lines 19-25
Link Here
|
| 19 |
|
19 |
|
| 20 |
use Modern::Perl; |
20 |
use Modern::Perl; |
| 21 |
|
21 |
|
| 22 |
use Test::More tests => 4; |
22 |
use Test::More tests => 5; |
| 23 |
|
23 |
|
| 24 |
use Koha::Account; |
24 |
use Koha::Account; |
| 25 |
use Koha::Account::Lines; |
25 |
use Koha::Account::Lines; |
|
Lines 225-227
subtest 'lines() tests' => sub {
Link Here
|
| 225 |
|
225 |
|
| 226 |
$schema->storage->txn_rollback; |
226 |
$schema->storage->txn_rollback; |
| 227 |
}; |
227 |
}; |
| 228 |
- |
228 |
|
|
|
229 |
subtest 'normalize_balance' => sub { |
| 230 |
|
| 231 |
plan tests => 3; |
| 232 |
|
| 233 |
subtest 'more credit than debit' => sub { |
| 234 |
|
| 235 |
plan tests => 6; |
| 236 |
|
| 237 |
$schema->storage->txn_begin; |
| 238 |
|
| 239 |
my $patron = $builder->build_object({ class => 'Koha::Patrons' }); |
| 240 |
my $account = $patron->account; |
| 241 |
|
| 242 |
# Add Credits |
| 243 |
$account->add_credit({ amount => 1 }); |
| 244 |
$account->add_credit({ amount => 2 }); |
| 245 |
$account->add_credit({ amount => 3 }); |
| 246 |
$account->add_credit({ amount => 4 }); |
| 247 |
$account->add_credit({ amount => 5 }); |
| 248 |
|
| 249 |
# Add Debits TODO: replace for calls to add_debit when time comes |
| 250 |
Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 1, amountoutstanding => 1 })->store; |
| 251 |
Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 2, amountoutstanding => 2 })->store; |
| 252 |
Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 3, amountoutstanding => 3 })->store; |
| 253 |
Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 4, amountoutstanding => 4 })->store; |
| 254 |
|
| 255 |
# Paid Off |
| 256 |
Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 1, amountoutstanding => 0 })->store; |
| 257 |
Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 1, amountoutstanding => 0 })->store; |
| 258 |
|
| 259 |
is( $account->balance(), -5, "Account balance is -5" ); |
| 260 |
is( $account->outstanding_debits->total_outstanding, 10, 'Outstanding debits sum 10' ); |
| 261 |
is( $account->outstanding_credits->total_outstanding, -15, 'Outstanding credits sum -15' ); |
| 262 |
|
| 263 |
$account->normalize_balance(); |
| 264 |
|
| 265 |
is( $account->balance(), -5, "Account balance is -5" ); |
| 266 |
is( $account->outstanding_debits->total_outstanding, 0, 'No outstanding debits' ); |
| 267 |
is( $account->outstanding_credits->total_outstanding, -5, 'Outstanding credits sum -5' ); |
| 268 |
|
| 269 |
$schema->storage->txn_rollback; |
| 270 |
}; |
| 271 |
|
| 272 |
subtest 'same debit than credit' => sub { |
| 273 |
|
| 274 |
plan tests => 6; |
| 275 |
|
| 276 |
$schema->storage->txn_begin; |
| 277 |
|
| 278 |
my $patron = $builder->build_object({ class => 'Koha::Patrons' }); |
| 279 |
my $account = $patron->account; |
| 280 |
|
| 281 |
# Add Credits |
| 282 |
$account->add_credit({ amount => 1 }); |
| 283 |
$account->add_credit({ amount => 2 }); |
| 284 |
$account->add_credit({ amount => 3 }); |
| 285 |
$account->add_credit({ amount => 4 }); |
| 286 |
|
| 287 |
# Add Debits TODO: replace for calls to add_debit when time comes |
| 288 |
Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 1, amountoutstanding => 1 })->store; |
| 289 |
Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 2, amountoutstanding => 2 })->store; |
| 290 |
Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 3, amountoutstanding => 3 })->store; |
| 291 |
Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 4, amountoutstanding => 4 })->store; |
| 292 |
|
| 293 |
# Paid Off |
| 294 |
Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 1, amountoutstanding => 0 })->store; |
| 295 |
Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 1, amountoutstanding => 0 })->store; |
| 296 |
|
| 297 |
is( $account->balance(), 0, "Account balance is 0" ); |
| 298 |
is( $account->outstanding_debits->total_outstanding, 10, 'Outstanding debits sum 10' ); |
| 299 |
is( $account->outstanding_credits->total_outstanding, -10, 'Outstanding credits sum -10' ); |
| 300 |
|
| 301 |
$account->normalize_balance(); |
| 302 |
|
| 303 |
is( $account->balance(), 0, "Account balance is 0" ); |
| 304 |
is( $account->outstanding_debits->total_outstanding, 0, 'No outstanding debits' ); |
| 305 |
is( $account->outstanding_credits->total_outstanding, 0, 'Outstanding credits sum 0' ); |
| 306 |
|
| 307 |
$schema->storage->txn_rollback; |
| 308 |
}; |
| 309 |
|
| 310 |
subtest 'more debit than credit' => sub { |
| 311 |
|
| 312 |
plan tests => 6; |
| 313 |
|
| 314 |
$schema->storage->txn_begin; |
| 315 |
|
| 316 |
my $patron = $builder->build_object({ class => 'Koha::Patrons' }); |
| 317 |
my $account = $patron->account; |
| 318 |
|
| 319 |
# Add Credits |
| 320 |
$account->add_credit({ amount => 1 }); |
| 321 |
$account->add_credit({ amount => 2 }); |
| 322 |
$account->add_credit({ amount => 3 }); |
| 323 |
$account->add_credit({ amount => 4 }); |
| 324 |
|
| 325 |
# Add Debits TODO: replace for calls to add_debit when time comes |
| 326 |
Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 1, amountoutstanding => 1 })->store; |
| 327 |
Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 2, amountoutstanding => 2 })->store; |
| 328 |
Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 3, amountoutstanding => 3 })->store; |
| 329 |
Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 4, amountoutstanding => 4 })->store; |
| 330 |
Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 5, amountoutstanding => 5 })->store; |
| 331 |
|
| 332 |
# Paid Off |
| 333 |
Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 1, amountoutstanding => 0 })->store; |
| 334 |
Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 1, amountoutstanding => 0 })->store; |
| 335 |
|
| 336 |
is( $account->balance(), 5, "Account balance is 5" ); |
| 337 |
is( $account->outstanding_debits->total_outstanding, 15, 'Outstanding debits sum 15' ); |
| 338 |
is( $account->outstanding_credits->total_outstanding, -10, 'Outstanding credits sum -10' ); |
| 339 |
|
| 340 |
$account->normalize_balance(); |
| 341 |
|
| 342 |
is( $account->balance(), 5, "Account balance is 5" ); |
| 343 |
is( $account->outstanding_debits->total_outstanding, 5, 'Outstanding debits sum 5' ); |
| 344 |
is( $account->outstanding_credits->total_outstanding, 0, 'Outstanding credits sum 0' ); |
| 345 |
|
| 346 |
$schema->storage->txn_rollback; |
| 347 |
}; |
| 348 |
}; |