|
Lines 20-26
Link Here
|
| 20 |
use Modern::Perl; |
20 |
use Modern::Perl; |
| 21 |
|
21 |
|
| 22 |
use Test::NoWarnings; |
22 |
use Test::NoWarnings; |
| 23 |
use Test::More tests => 6; |
23 |
use Test::More tests => 11; |
| 24 |
|
24 |
|
| 25 |
use Test::Exception; |
25 |
use Test::Exception; |
| 26 |
|
26 |
|
|
Lines 29-34
use Koha::Account;
Link Here
|
| 29 |
use Koha::Account::CreditTypes; |
29 |
use Koha::Account::CreditTypes; |
| 30 |
use Koha::Account::DebitTypes; |
30 |
use Koha::Account::DebitTypes; |
| 31 |
|
31 |
|
|
|
32 |
use t::lib::Mocks; |
| 32 |
use t::lib::TestBuilder; |
33 |
use t::lib::TestBuilder; |
| 33 |
|
34 |
|
| 34 |
my $builder = t::lib::TestBuilder->new; |
35 |
my $builder = t::lib::TestBuilder->new; |
|
Lines 173-178
subtest 'cashup' => sub {
Link Here
|
| 173 |
|
174 |
|
| 174 |
$schema->storage->txn_begin; |
175 |
$schema->storage->txn_begin; |
| 175 |
|
176 |
|
|
|
177 |
# Ensure reconciliation notes are not required for these tests |
| 178 |
t::lib::Mocks::mock_preference( 'CashupReconciliationNoteRequired', 0 ); |
| 179 |
|
| 176 |
my $register = $builder->build_object( { class => 'Koha::Cash::Registers' } ); |
180 |
my $register = $builder->build_object( { class => 'Koha::Cash::Registers' } ); |
| 177 |
my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
181 |
my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
| 178 |
|
182 |
|
|
Lines 260-266
subtest 'cashup' => sub {
Link Here
|
| 260 |
subtest 'outstanding_accountlines' => sub { |
264 |
subtest 'outstanding_accountlines' => sub { |
| 261 |
plan tests => 6; |
265 |
plan tests => 6; |
| 262 |
|
266 |
|
| 263 |
my $accountlines = $register->outstanding_accountlines; |
267 |
$schema->storage->txn_begin; |
|
|
268 |
|
| 269 |
my $test_register = $builder->build_object( { class => 'Koha::Cash::Registers' } ); |
| 270 |
my $accountlines = $test_register->outstanding_accountlines; |
| 264 |
is( |
271 |
is( |
| 265 |
ref($accountlines), 'Koha::Account::Lines', |
272 |
ref($accountlines), 'Koha::Account::Lines', |
| 266 |
'Koha::Cash::Register->outstanding_accountlines should always return a Koha::Account::Lines set' |
273 |
'Koha::Cash::Register->outstanding_accountlines should always return a Koha::Account::Lines set' |
|
Lines 270-308
subtest 'cashup' => sub {
Link Here
|
| 270 |
'Koha::Cash::Register->outstanding_accountlines should always return the correct number of accountlines' |
277 |
'Koha::Cash::Register->outstanding_accountlines should always return the correct number of accountlines' |
| 271 |
); |
278 |
); |
| 272 |
|
279 |
|
|
|
280 |
my $test_patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
| 281 |
|
| 273 |
my $accountline1 = $builder->build_object( |
282 |
my $accountline1 = $builder->build_object( |
| 274 |
{ |
283 |
{ |
| 275 |
class => 'Koha::Account::Lines', |
284 |
class => 'Koha::Account::Lines', |
| 276 |
value => { register_id => $register->id, date => \'NOW() - INTERVAL 5 MINUTE' }, |
285 |
value => { |
|
|
286 |
register_id => $test_register->id, |
| 287 |
amount => -2.50, |
| 288 |
date => \'SYSDATE() - INTERVAL 5 MINUTE', |
| 289 |
payment_type => 'CASH' |
| 290 |
}, |
| 277 |
} |
291 |
} |
| 278 |
); |
292 |
); |
| 279 |
my $accountline2 = $builder->build_object( |
293 |
my $accountline2 = $builder->build_object( |
| 280 |
{ |
294 |
{ |
| 281 |
class => 'Koha::Account::Lines', |
295 |
class => 'Koha::Account::Lines', |
| 282 |
value => { register_id => $register->id, date => \'NOW() - INTERVAL 5 MINUTE' }, |
296 |
value => { |
|
|
297 |
register_id => $test_register->id, |
| 298 |
amount => -1.50, |
| 299 |
date => \'SYSDATE() - INTERVAL 5 MINUTE', |
| 300 |
payment_type => 'CASH' |
| 301 |
}, |
| 283 |
} |
302 |
} |
| 284 |
); |
303 |
); |
| 285 |
|
304 |
|
| 286 |
$accountlines = $register->outstanding_accountlines; |
305 |
$accountlines = $test_register->outstanding_accountlines; |
| 287 |
is( $accountlines->count, 2, 'No cashup, all accountlines returned' ); |
306 |
is( $accountlines->count, 2, 'No cashup, all accountlines returned' ); |
| 288 |
|
307 |
|
| 289 |
my $cashup3 = $register->add_cashup( { manager_id => $patron->id, amount => '2.50' } ); |
308 |
# Calculate expected amount for this cashup |
|
|
309 |
my $expected_amount = |
| 310 |
( $test_register->outstanding_accountlines->total( { payment_type => [ 'CASH', 'SIP00' ] } ) ) * -1; |
| 311 |
my $cashup3 = $test_register->add_cashup( { manager_id => $test_patron->id, amount => $expected_amount } ); |
| 290 |
|
312 |
|
| 291 |
$accountlines = $register->outstanding_accountlines; |
313 |
$accountlines = $test_register->outstanding_accountlines; |
| 292 |
is( $accountlines->count, 0, 'Cashup added, no accountlines returned' ); |
314 |
is( $accountlines->count, 0, 'Cashup added, no accountlines returned' ); |
| 293 |
|
315 |
|
| 294 |
my $accountline3 = $builder->build_object( |
316 |
my $accountline3 = $builder->build_object( |
| 295 |
{ |
317 |
{ |
| 296 |
class => 'Koha::Account::Lines', |
318 |
class => 'Koha::Account::Lines', |
| 297 |
value => { register_id => $register->id }, |
319 |
value => { |
|
|
320 |
register_id => $test_register->id, |
| 321 |
amount => 1.50, |
| 322 |
date => \'SYSDATE() + INTERVAL 5 MINUTE', |
| 323 |
payment_type => 'CASH' |
| 324 |
}, |
| 298 |
} |
325 |
} |
| 299 |
); |
326 |
); |
| 300 |
|
327 |
|
| 301 |
# Fake the cashup timestamp to make sure it's before the accountline we just added, |
328 |
$accountlines = $test_register->outstanding_accountlines; |
| 302 |
# we can't trust that these two actions are more than a second apart in a test |
|
|
| 303 |
$cashup3->timestamp( \'NOW() - INTERVAL 2 MINUTE' )->store; |
| 304 |
|
| 305 |
$accountlines = $register->outstanding_accountlines; |
| 306 |
is( |
329 |
is( |
| 307 |
$accountlines->count, 1, |
330 |
$accountlines->count, 1, |
| 308 |
'Accountline added, one accountline returned' |
331 |
'Accountline added, one accountline returned' |
|
Lines 311-365
subtest 'cashup' => sub {
Link Here
|
| 311 |
$accountlines->next->id, |
334 |
$accountlines->next->id, |
| 312 |
$accountline3->id, 'Correct accountline returned' |
335 |
$accountline3->id, 'Correct accountline returned' |
| 313 |
); |
336 |
); |
|
|
337 |
|
| 338 |
$schema->storage->txn_rollback; |
| 314 |
}; |
339 |
}; |
| 315 |
|
340 |
|
| 316 |
$schema->storage->txn_rollback; |
341 |
$schema->storage->txn_rollback; |
| 317 |
}; |
342 |
}; |
| 318 |
|
343 |
|
| 319 |
subtest 'cashup_reconciliation' => sub { |
344 |
subtest 'cashup_reconciliation' => sub { |
| 320 |
plan tests => 5; |
345 |
plan tests => 6; |
| 321 |
|
346 |
|
| 322 |
$schema->storage->txn_begin; |
347 |
$schema->storage->txn_begin; |
| 323 |
|
348 |
|
| 324 |
# Ensure required account types for reconciliation exist (they should already exist from mandatory data) |
349 |
# Ensure reconciliation notes are not required for these tests |
| 325 |
use Koha::Account::CreditTypes; |
350 |
t::lib::Mocks::mock_preference( 'CashupReconciliationNoteRequired', 0 ); |
| 326 |
use Koha::Account::DebitTypes; |
|
|
| 327 |
|
| 328 |
my $surplus_credit_type = Koha::Account::CreditTypes->find( { code => 'CASHUP_SURPLUS' } ); |
| 329 |
if ( !$surplus_credit_type ) { |
| 330 |
$surplus_credit_type = $builder->build_object( |
| 331 |
{ |
| 332 |
class => 'Koha::Account::CreditTypes', |
| 333 |
value => { |
| 334 |
code => 'CASHUP_SURPLUS', |
| 335 |
description => 'Cash register surplus found during cashup', |
| 336 |
can_be_added_manually => 0, |
| 337 |
credit_number_enabled => 0, |
| 338 |
is_system => 1, |
| 339 |
archived => 0, |
| 340 |
} |
| 341 |
} |
| 342 |
); |
| 343 |
} |
| 344 |
|
| 345 |
my $deficit_debit_type = Koha::Account::DebitTypes->find( { code => 'CASHUP_DEFICIT' } ); |
| 346 |
if ( !$deficit_debit_type ) { |
| 347 |
$deficit_debit_type = $builder->build_object( |
| 348 |
{ |
| 349 |
class => 'Koha::Account::DebitTypes', |
| 350 |
value => { |
| 351 |
code => 'CASHUP_DEFICIT', |
| 352 |
description => 'Cash register deficit found during cashup', |
| 353 |
can_be_invoiced => 0, |
| 354 |
can_be_sold => 0, |
| 355 |
default_amount => undef, |
| 356 |
is_system => 1, |
| 357 |
archived => 0, |
| 358 |
restricts_checkouts => 0, |
| 359 |
} |
| 360 |
} |
| 361 |
); |
| 362 |
} |
| 363 |
|
351 |
|
| 364 |
my $register = $builder->build_object( { class => 'Koha::Cash::Registers' } ); |
352 |
my $register = $builder->build_object( { class => 'Koha::Cash::Registers' } ); |
| 365 |
my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
353 |
my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
|
Lines 371-379
subtest 'cashup_reconciliation' => sub {
Link Here
|
| 371 |
value => { |
359 |
value => { |
| 372 |
register_id => $register->id, |
360 |
register_id => $register->id, |
| 373 |
borrowernumber => $patron->id, |
361 |
borrowernumber => $patron->id, |
| 374 |
amount => -10.00, # Credit (payment) |
362 |
amount => -10.00, # Credit (payment) |
| 375 |
credit_type_code => 'PAYMENT', |
363 |
credit_type_code => 'PAYMENT', |
| 376 |
debit_type_code => undef, |
364 |
debit_type_code => undef, |
|
|
365 |
payment_type => 'CASH', |
| 366 |
date => \'SYSDATE() - INTERVAL 1 MINUTE', |
| 367 |
timestamp => \'SYSDATE() - INTERVAL 1 MINUTE', |
| 377 |
} |
368 |
} |
| 378 |
} |
369 |
} |
| 379 |
); |
370 |
); |
|
Lines 383-402
subtest 'cashup_reconciliation' => sub {
Link Here
|
| 383 |
value => { |
374 |
value => { |
| 384 |
register_id => $register->id, |
375 |
register_id => $register->id, |
| 385 |
borrowernumber => $patron->id, |
376 |
borrowernumber => $patron->id, |
| 386 |
amount => -5.00, # Credit (payment) |
377 |
amount => -5.00, # Credit (payment) |
| 387 |
credit_type_code => 'PAYMENT', |
378 |
credit_type_code => 'PAYMENT', |
| 388 |
debit_type_code => undef, |
379 |
debit_type_code => undef, |
|
|
380 |
payment_type => 'CASH', |
| 381 |
date => \'SYSDATE() - INTERVAL 1 MINUTE', |
| 382 |
timestamp => \'SYSDATE() - INTERVAL 1 MINUTE', |
| 389 |
} |
383 |
} |
| 390 |
} |
384 |
} |
| 391 |
); |
385 |
); |
| 392 |
|
386 |
|
| 393 |
my $expected_amount = $register->outstanding_accountlines->total; # Should be -15.00 |
387 |
my $expected_amount = |
|
|
388 |
$register->outstanding_accountlines->total( { payment_type => [ 'CASH', 'SIP00' ] } ); # Should be -15.00 |
| 389 |
is( $expected_amount, -15.00, "Expected cash amount is calculated correctly" ); |
| 394 |
|
390 |
|
| 395 |
subtest 'balanced_cashup' => sub { |
391 |
subtest 'balanced_cashup' => sub { |
| 396 |
plan tests => 3; |
392 |
plan tests => 3; |
| 397 |
|
393 |
|
|
|
394 |
$schema->storage->txn_begin; |
| 395 |
|
| 398 |
# Test exact match - no surplus/deficit accountlines should be created |
396 |
# Test exact match - no surplus/deficit accountlines should be created |
| 399 |
my $amount = abs($expected_amount); # 15.00 actual matches 15.00 expected |
397 |
my $amount = abs($expected_amount); # 15.00 actual matches 15.00 expected |
| 400 |
|
398 |
|
| 401 |
my $cashup = $register->add_cashup( |
399 |
my $cashup = $register->add_cashup( |
| 402 |
{ |
400 |
{ |
|
Lines 420-429
subtest 'cashup_reconciliation' => sub {
Link Here
|
| 420 |
); |
418 |
); |
| 421 |
|
419 |
|
| 422 |
is( $reconciliation_lines->count, 0, 'No reconciliation accountlines created for balanced cashup' ); |
420 |
is( $reconciliation_lines->count, 0, 'No reconciliation accountlines created for balanced cashup' ); |
|
|
421 |
|
| 422 |
$schema->storage->txn_rollback; |
| 423 |
}; |
423 |
}; |
| 424 |
|
424 |
|
| 425 |
subtest 'surplus_cashup' => sub { |
425 |
subtest 'surplus_cashup' => sub { |
| 426 |
plan tests => 7; |
426 |
plan tests => 10; |
| 427 |
|
427 |
|
| 428 |
$schema->storage->txn_begin; |
428 |
$schema->storage->txn_begin; |
| 429 |
|
429 |
|
|
Lines 437-449
subtest 'cashup_reconciliation' => sub {
Link Here
|
| 437 |
amount => -20.00, # Credit (payment) |
437 |
amount => -20.00, # Credit (payment) |
| 438 |
credit_type_code => 'PAYMENT', |
438 |
credit_type_code => 'PAYMENT', |
| 439 |
debit_type_code => undef, |
439 |
debit_type_code => undef, |
|
|
440 |
payment_type => 'CASH', |
| 440 |
} |
441 |
} |
| 441 |
} |
442 |
} |
| 442 |
); |
443 |
); |
| 443 |
|
444 |
|
| 444 |
my $expected = abs( $register2->outstanding_accountlines->total ); # 20.00 |
445 |
my $expected = |
| 445 |
my $actual = 25.00; # 5.00 surplus |
446 |
abs( $register2->outstanding_accountlines->total( { payment_type => [ 'CASH', 'SIP00' ] } ) ); # 20.00 |
| 446 |
my $surplus = $actual - $expected; |
447 |
my $actual = 25.00; # 5.00 surplus |
|
|
448 |
my $surplus = $actual - $expected; |
| 447 |
|
449 |
|
| 448 |
my $cashup = $register2->add_cashup( |
450 |
my $cashup = $register2->add_cashup( |
| 449 |
{ |
451 |
{ |
|
Lines 488-493
subtest 'cashup_reconciliation' => sub {
Link Here
|
| 488 |
amount => -10.00, |
490 |
amount => -10.00, |
| 489 |
credit_type_code => 'PAYMENT', |
491 |
credit_type_code => 'PAYMENT', |
| 490 |
debit_type_code => undef, |
492 |
debit_type_code => undef, |
|
|
493 |
payment_type => 'CASH', |
| 491 |
} |
494 |
} |
| 492 |
} |
495 |
} |
| 493 |
); |
496 |
); |
|
Lines 520-526
subtest 'cashup_reconciliation' => sub {
Link Here
|
| 520 |
}; |
523 |
}; |
| 521 |
|
524 |
|
| 522 |
subtest 'deficit_cashup' => sub { |
525 |
subtest 'deficit_cashup' => sub { |
| 523 |
plan tests => 7; |
526 |
plan tests => 10; |
| 524 |
|
527 |
|
| 525 |
$schema->storage->txn_begin; |
528 |
$schema->storage->txn_begin; |
| 526 |
|
529 |
|
|
Lines 534-546
subtest 'cashup_reconciliation' => sub {
Link Here
|
| 534 |
amount => -30.00, # Credit (payment) |
537 |
amount => -30.00, # Credit (payment) |
| 535 |
credit_type_code => 'PAYMENT', |
538 |
credit_type_code => 'PAYMENT', |
| 536 |
debit_type_code => undef, |
539 |
debit_type_code => undef, |
|
|
540 |
payment_type => 'CASH', |
| 537 |
} |
541 |
} |
| 538 |
} |
542 |
} |
| 539 |
); |
543 |
); |
| 540 |
|
544 |
|
| 541 |
my $expected = abs( $register3->outstanding_accountlines->total ); # 30.00 |
545 |
my $expected = |
| 542 |
my $actual = 25.00; # 5.00 deficit |
546 |
abs( $register3->outstanding_accountlines->total( { payment_type => [ 'CASH', 'SIP00' ] } ) ); # 30.00 |
| 543 |
my $deficit = $expected - $actual; |
547 |
my $actual = 25.00; # 5.00 deficit |
|
|
548 |
my $deficit = $expected - $actual; |
| 544 |
|
549 |
|
| 545 |
my $cashup = $register3->add_cashup( |
550 |
my $cashup = $register3->add_cashup( |
| 546 |
{ |
551 |
{ |
|
Lines 585-590
subtest 'cashup_reconciliation' => sub {
Link Here
|
| 585 |
amount => -20.00, |
590 |
amount => -20.00, |
| 586 |
credit_type_code => 'PAYMENT', |
591 |
credit_type_code => 'PAYMENT', |
| 587 |
debit_type_code => undef, |
592 |
debit_type_code => undef, |
|
|
593 |
payment_type => 'CASH', |
| 588 |
} |
594 |
} |
| 589 |
} |
595 |
} |
| 590 |
); |
596 |
); |
|
Lines 631-636
subtest 'cashup_reconciliation' => sub {
Link Here
|
| 631 |
amount => -10.00, |
637 |
amount => -10.00, |
| 632 |
credit_type_code => 'PAYMENT', |
638 |
credit_type_code => 'PAYMENT', |
| 633 |
debit_type_code => undef, |
639 |
debit_type_code => undef, |
|
|
640 |
payment_type => 'CASH', |
| 634 |
} |
641 |
} |
| 635 |
} |
642 |
} |
| 636 |
); |
643 |
); |
|
Lines 683-688
subtest 'cashup_reconciliation' => sub {
Link Here
|
| 683 |
amount => -10.00, |
690 |
amount => -10.00, |
| 684 |
credit_type_code => 'PAYMENT', |
691 |
credit_type_code => 'PAYMENT', |
| 685 |
debit_type_code => undef, |
692 |
debit_type_code => undef, |
|
|
693 |
payment_type => 'CASH', |
| 686 |
} |
694 |
} |
| 687 |
} |
695 |
} |
| 688 |
); |
696 |
); |
|
Lines 722-727
subtest 'cashup_reconciliation' => sub {
Link Here
|
| 722 |
amount => -10.00, |
730 |
amount => -10.00, |
| 723 |
credit_type_code => 'PAYMENT', |
731 |
credit_type_code => 'PAYMENT', |
| 724 |
debit_type_code => undef, |
732 |
debit_type_code => undef, |
|
|
733 |
payment_type => 'CASH', |
| 725 |
} |
734 |
} |
| 726 |
} |
735 |
} |
| 727 |
); |
736 |
); |
|
Lines 751-753
subtest 'cashup_reconciliation' => sub {
Link Here
|
| 751 |
|
760 |
|
| 752 |
$schema->storage->txn_rollback; |
761 |
$schema->storage->txn_rollback; |
| 753 |
}; |
762 |
}; |
|
|
763 |
|
| 764 |
subtest 'two_phase_cashup_workflow' => sub { |
| 765 |
plan tests => 15; |
| 766 |
|
| 767 |
$schema->storage->txn_begin; |
| 768 |
|
| 769 |
# Create test data |
| 770 |
my $manager = $builder->build_object( { class => 'Koha::Patrons' } ); |
| 771 |
my $library = $builder->build_object( { class => 'Koha::Libraries' } ); |
| 772 |
my $register = $builder->build_object( |
| 773 |
{ |
| 774 |
class => 'Koha::Cash::Registers', |
| 775 |
value => { |
| 776 |
branch => $library->branchcode, |
| 777 |
starting_float => 0, |
| 778 |
} |
| 779 |
} |
| 780 |
); |
| 781 |
|
| 782 |
# Add some test transactions |
| 783 |
my $account_line1 = $builder->build_object( |
| 784 |
{ |
| 785 |
class => 'Koha::Account::Lines', |
| 786 |
value => { |
| 787 |
amount => 10.00, |
| 788 |
date => \'SYSDATE() - INTERVAL 1 MINUTE', |
| 789 |
register_id => undef, |
| 790 |
debit_type_code => 'OVERDUE', |
| 791 |
credit_type_code => undef, |
| 792 |
payment_type => undef, |
| 793 |
} |
| 794 |
} |
| 795 |
); |
| 796 |
|
| 797 |
my $account_line2 = $builder->build_object( |
| 798 |
{ |
| 799 |
class => 'Koha::Account::Lines', |
| 800 |
value => { |
| 801 |
amount => -5.00, |
| 802 |
date => \'SYSDATE() - INTERVAL 1 MINUTE', |
| 803 |
register_id => $register->id, |
| 804 |
debit_type_code => undef, |
| 805 |
credit_type_code => 'PAYMENT', |
| 806 |
payment_type => 'CASH' |
| 807 |
} |
| 808 |
} |
| 809 |
); |
| 810 |
|
| 811 |
# Test 1: start_cashup creates CASHUP_START action |
| 812 |
my $cashup_start = $register->start_cashup( { manager_id => $manager->id } ); |
| 813 |
|
| 814 |
is( |
| 815 |
ref $cashup_start, 'Koha::Cash::Register::Cashup', |
| 816 |
'start_cashup returns Cash::Register::Cashup object' |
| 817 |
); |
| 818 |
|
| 819 |
my $start_action = Koha::Cash::Register::Actions->search( |
| 820 |
{ |
| 821 |
register_id => $register->id, |
| 822 |
code => 'CASHUP_START' |
| 823 |
} |
| 824 |
)->next; |
| 825 |
|
| 826 |
ok( $start_action, 'CASHUP_START action created in database' ); |
| 827 |
is( $start_action->manager_id, $manager->id, 'CASHUP_START has correct manager_id' ); |
| 828 |
|
| 829 |
# Test 2: cashup_in_progress detects active cashup |
| 830 |
my $in_progress = $register->cashup_in_progress; |
| 831 |
ok( $in_progress, 'cashup_in_progress detects active cashup' ); |
| 832 |
is( $in_progress->id, $start_action->id, 'cashup_in_progress returns correct CASHUP_START action' ); |
| 833 |
|
| 834 |
# Test 3: Cannot start another cashup while one is in progress |
| 835 |
throws_ok { |
| 836 |
$register->start_cashup( { manager_id => $manager->id } ); |
| 837 |
} |
| 838 |
'Koha::Exceptions::Object::DuplicateID', |
| 839 |
'Cannot start second cashup while one is in progress'; |
| 840 |
|
| 841 |
# Test 4: outstanding_accountlines behavior during active cashup |
| 842 |
my $outstanding = $register->outstanding_accountlines; |
| 843 |
is( $outstanding->count, 0, 'outstanding_accountlines returns 0 during active cashup' ); |
| 844 |
|
| 845 |
# Test 5: Add transaction after cashup start (should appear in outstanding) |
| 846 |
my $account_line3 = $builder->build_object( |
| 847 |
{ |
| 848 |
class => 'Koha::Account::Lines', |
| 849 |
value => { |
| 850 |
amount => -8.00, |
| 851 |
date => \'SYSDATE() + INTERVAL 1 MINUTE', |
| 852 |
register_id => $register->id, |
| 853 |
debit_type_code => undef, |
| 854 |
credit_type_code => 'PAYMENT', |
| 855 |
payment_type => 'CASH', |
| 856 |
} |
| 857 |
} |
| 858 |
); |
| 859 |
|
| 860 |
# This new transaction should appear in outstanding (it's after CASHUP_START) |
| 861 |
$outstanding = $register->outstanding_accountlines; |
| 862 |
is( $outstanding->count, 1, 'New transaction after CASHUP_START appears in outstanding' ); |
| 863 |
|
| 864 |
# Test 6: outstanding_accountlines correctly handles session boundaries |
| 865 |
my $session_accountlines = $register->outstanding_accountlines; |
| 866 |
my $session_total = $session_accountlines->total; |
| 867 |
is( |
| 868 |
$session_total, -8.00, |
| 869 |
'outstanding_accountlines correctly calculates session totals with CASHUP_START cutoff' |
| 870 |
); |
| 871 |
|
| 872 |
# Test 7: Complete cashup with exact amount (no reconciliation) |
| 873 |
my $expected_cashup_amount = 5.00; # CASH PAYMENT prior to CASHUP_START |
| 874 |
my $cashup_complete = $register->add_cashup( |
| 875 |
{ |
| 876 |
manager_id => $manager->id, |
| 877 |
amount => $expected_cashup_amount |
| 878 |
} |
| 879 |
); |
| 880 |
|
| 881 |
is( |
| 882 |
ref $cashup_complete, 'Koha::Cash::Register::Cashup', |
| 883 |
'add_cashup returns Cashup object' |
| 884 |
); |
| 885 |
|
| 886 |
# Check no reconciliation lines were created |
| 887 |
my $surplus_lines = $cashup_complete->accountlines->search( |
| 888 |
{ |
| 889 |
register_id => $register->id, |
| 890 |
credit_type_code => 'CASHUP_SURPLUS' |
| 891 |
} |
| 892 |
); |
| 893 |
my $deficit_lines = $cashup_complete->accountlines->search( |
| 894 |
{ |
| 895 |
register_id => $register->id, |
| 896 |
debit_type_code => 'CASHUP_DEFICIT' |
| 897 |
} |
| 898 |
); |
| 899 |
|
| 900 |
is( $surplus_lines->count, 0, 'No surplus lines created for exact cashup' ); |
| 901 |
is( $deficit_lines->count, 0, 'No deficit lines created for exact cashup' ); |
| 902 |
|
| 903 |
# Test 8: cashup_in_progress returns undef after completion |
| 904 |
$in_progress = $register->cashup_in_progress; |
| 905 |
is( $in_progress, undef, 'cashup_in_progress returns undef after completion' ); |
| 906 |
|
| 907 |
# Test 9: outstanding_accountlines now includes new transaction |
| 908 |
$outstanding = $register->outstanding_accountlines; |
| 909 |
is( $outstanding->count, 1, 'outstanding_accountlines includes transaction after completion' ); |
| 910 |
is( $outstanding->total, -8.00, 'outstanding_accountlines total is correct after completion' ); |
| 911 |
|
| 912 |
$schema->storage->txn_rollback; |
| 913 |
}; |
| 914 |
|
| 915 |
subtest 'cashup_in_progress' => sub { |
| 916 |
plan tests => 6; |
| 917 |
|
| 918 |
$schema->storage->txn_begin; |
| 919 |
|
| 920 |
my $register = $builder->build_object( { class => 'Koha::Cash::Registers' } ); |
| 921 |
my $manager = $builder->build_object( { class => 'Koha::Patrons' } ); |
| 922 |
my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
| 923 |
|
| 924 |
# Test 1: No cashups ever performed |
| 925 |
subtest 'no_cashups_ever' => sub { |
| 926 |
plan tests => 1; |
| 927 |
|
| 928 |
my $in_progress = $register->cashup_in_progress; |
| 929 |
is( $in_progress, undef, 'cashup_in_progress returns undef when no cashups have ever been performed' ); |
| 930 |
}; |
| 931 |
|
| 932 |
# Test 2: Only quick cashups performed |
| 933 |
subtest 'only_quick_cashups' => sub { |
| 934 |
plan tests => 2; |
| 935 |
|
| 936 |
# Add cash for first quick cashup |
| 937 |
$builder->build_object( |
| 938 |
{ |
| 939 |
class => 'Koha::Account::Lines', |
| 940 |
value => { |
| 941 |
register_id => $register->id, |
| 942 |
borrowernumber => $patron->id, |
| 943 |
amount => -10.00, |
| 944 |
credit_type_code => 'PAYMENT', |
| 945 |
payment_type => 'CASH', |
| 946 |
} |
| 947 |
} |
| 948 |
); |
| 949 |
|
| 950 |
# Add a quick cashup |
| 951 |
my $quick_cashup = $register->add_cashup( { manager_id => $manager->id, amount => '10.00' } ); |
| 952 |
$quick_cashup->timestamp( \'NOW() - INTERVAL 30 MINUTE' )->store(); |
| 953 |
|
| 954 |
my $in_progress = $register->cashup_in_progress; |
| 955 |
is( $in_progress, undef, 'cashup_in_progress returns undef after quick cashup completion' ); |
| 956 |
|
| 957 |
# Add cash for second quick cashup |
| 958 |
$builder->build_object( |
| 959 |
{ |
| 960 |
class => 'Koha::Account::Lines', |
| 961 |
value => { |
| 962 |
register_id => $register->id, |
| 963 |
borrowernumber => $patron->id, |
| 964 |
amount => -5.00, |
| 965 |
credit_type_code => 'PAYMENT', |
| 966 |
payment_type => 'CASH', |
| 967 |
} |
| 968 |
} |
| 969 |
); |
| 970 |
|
| 971 |
# Add another quick cashup |
| 972 |
my $quick_cashup2 = $register->add_cashup( { manager_id => $manager->id, amount => '5.00' } ); |
| 973 |
|
| 974 |
$in_progress = $register->cashup_in_progress; |
| 975 |
is( $in_progress, undef, 'cashup_in_progress returns undef after multiple quick cashups' ); |
| 976 |
}; |
| 977 |
|
| 978 |
# Test 3: Multiple CASHUP_START actions |
| 979 |
subtest 'multiple_start_actions' => sub { |
| 980 |
plan tests => 2; |
| 981 |
|
| 982 |
my $register2 = $builder->build_object( { class => 'Koha::Cash::Registers' } ); |
| 983 |
my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
| 984 |
|
| 985 |
# Add cash transactions before starting cashup |
| 986 |
$builder->build_object( |
| 987 |
{ |
| 988 |
class => 'Koha::Account::Lines', |
| 989 |
value => { |
| 990 |
register_id => $register2->id, |
| 991 |
borrowernumber => $patron->id, |
| 992 |
amount => -5.00, |
| 993 |
credit_type_code => 'PAYMENT', |
| 994 |
payment_type => 'CASH', |
| 995 |
} |
| 996 |
} |
| 997 |
); |
| 998 |
|
| 999 |
# Create multiple CASHUP_START actions |
| 1000 |
my $start1 = $register2->start_cashup( { manager_id => $manager->id } ); |
| 1001 |
$start1->timestamp( \'NOW() - INTERVAL 60 MINUTE' )->store(); |
| 1002 |
|
| 1003 |
# Complete the first one |
| 1004 |
my $complete1 = $register2->add_cashup( { manager_id => $manager->id, amount => '1.00' } ); |
| 1005 |
$complete1->timestamp( \'NOW() - INTERVAL 50 MINUTE' )->store(); |
| 1006 |
|
| 1007 |
# Add more cash for second cashup |
| 1008 |
$builder->build_object( |
| 1009 |
{ |
| 1010 |
class => 'Koha::Account::Lines', |
| 1011 |
value => { |
| 1012 |
register_id => $register2->id, |
| 1013 |
borrowernumber => $patron->id, |
| 1014 |
amount => -3.00, |
| 1015 |
credit_type_code => 'PAYMENT', |
| 1016 |
payment_type => 'CASH', |
| 1017 |
} |
| 1018 |
} |
| 1019 |
); |
| 1020 |
|
| 1021 |
# Start another one |
| 1022 |
my $start2 = $register2->start_cashup( { manager_id => $manager->id } ); |
| 1023 |
|
| 1024 |
my $in_progress = $register2->cashup_in_progress; |
| 1025 |
is( ref($in_progress), 'Koha::Cash::Register::Action', 'Returns most recent CASHUP_START when multiple exist' ); |
| 1026 |
is( $in_progress->id, $start2->id, 'Returns the correct (most recent) CASHUP_START action' ); |
| 1027 |
}; |
| 1028 |
|
| 1029 |
# Test 4: Mixed quick and two-phase workflows |
| 1030 |
subtest 'mixed_workflows' => sub { |
| 1031 |
plan tests => 3; |
| 1032 |
|
| 1033 |
my $register3 = $builder->build_object( { class => 'Koha::Cash::Registers' } ); |
| 1034 |
my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
| 1035 |
|
| 1036 |
# Add cash for first quick cashup |
| 1037 |
$builder->build_object( |
| 1038 |
{ |
| 1039 |
class => 'Koha::Account::Lines', |
| 1040 |
value => { |
| 1041 |
register_id => $register3->id, |
| 1042 |
borrowernumber => $patron->id, |
| 1043 |
amount => -5.00, |
| 1044 |
credit_type_code => 'PAYMENT', |
| 1045 |
payment_type => 'CASH', |
| 1046 |
} |
| 1047 |
} |
| 1048 |
); |
| 1049 |
|
| 1050 |
# Quick cashup first |
| 1051 |
my $quick = $register3->add_cashup( { manager_id => $manager->id, amount => '5.00' } ); |
| 1052 |
$quick->timestamp( \'NOW() - INTERVAL 40 MINUTE' )->store(); |
| 1053 |
|
| 1054 |
# Add cash for two-phase cashup |
| 1055 |
$builder->build_object( |
| 1056 |
{ |
| 1057 |
class => 'Koha::Account::Lines', |
| 1058 |
value => { |
| 1059 |
register_id => $register3->id, |
| 1060 |
borrowernumber => $patron->id, |
| 1061 |
amount => -3.00, |
| 1062 |
credit_type_code => 'PAYMENT', |
| 1063 |
payment_type => 'CASH', |
| 1064 |
} |
| 1065 |
} |
| 1066 |
); |
| 1067 |
|
| 1068 |
# Start two-phase |
| 1069 |
my $start = $register3->start_cashup( { manager_id => $manager->id } ); |
| 1070 |
$start->timestamp( \'NOW() - INTERVAL 30 MINUTE' )->store(); |
| 1071 |
|
| 1072 |
my $in_progress = $register3->cashup_in_progress; |
| 1073 |
is( ref($in_progress), 'Koha::Cash::Register::Action', 'Detects two-phase in progress after quick cashup' ); |
| 1074 |
is( $in_progress->id, $start->id, 'Returns correct CASHUP_START after mixed workflow' ); |
| 1075 |
|
| 1076 |
# Complete two-phase |
| 1077 |
my $complete = $register3->add_cashup( { manager_id => $manager->id, amount => '3.00' } ); |
| 1078 |
|
| 1079 |
$in_progress = $register3->cashup_in_progress; |
| 1080 |
is( $in_progress, undef, 'Returns undef after completing two-phase in mixed workflow' ); |
| 1081 |
}; |
| 1082 |
|
| 1083 |
# Test 5: Timestamp edge cases |
| 1084 |
subtest 'timestamp_edge_cases' => sub { |
| 1085 |
plan tests => 2; |
| 1086 |
|
| 1087 |
my $register4 = $builder->build_object( { class => 'Koha::Cash::Registers' } ); |
| 1088 |
my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
| 1089 |
|
| 1090 |
# Add cash for cashup |
| 1091 |
$builder->build_object( |
| 1092 |
{ |
| 1093 |
class => 'Koha::Account::Lines', |
| 1094 |
value => { |
| 1095 |
register_id => $register4->id, |
| 1096 |
borrowernumber => $patron->id, |
| 1097 |
amount => -2.00, |
| 1098 |
credit_type_code => 'PAYMENT', |
| 1099 |
payment_type => 'CASH', |
| 1100 |
} |
| 1101 |
} |
| 1102 |
); |
| 1103 |
|
| 1104 |
# Create CASHUP_START |
| 1105 |
my $start = $register4->start_cashup( { manager_id => $manager->id } ); |
| 1106 |
my $start_time = $start->timestamp; |
| 1107 |
|
| 1108 |
# Create CASHUP with exactly the same timestamp (edge case) |
| 1109 |
my $complete = $register4->add_cashup( { manager_id => $manager->id, amount => '1.00' } ); |
| 1110 |
$complete->timestamp($start_time)->store(); |
| 1111 |
|
| 1112 |
my $in_progress = $register4->cashup_in_progress; |
| 1113 |
is( $in_progress, undef, 'Handles same timestamp edge case correctly' ); |
| 1114 |
|
| 1115 |
# Test with CASHUP timestamp slightly before CASHUP_START (edge case) |
| 1116 |
my $register5 = $builder->build_object( { class => 'Koha::Cash::Registers' } ); |
| 1117 |
|
| 1118 |
# Add cash for register5 |
| 1119 |
$builder->build_object( |
| 1120 |
{ |
| 1121 |
class => 'Koha::Account::Lines', |
| 1122 |
value => { |
| 1123 |
register_id => $register5->id, |
| 1124 |
borrowernumber => $patron->id, |
| 1125 |
amount => -2.00, |
| 1126 |
credit_type_code => 'PAYMENT', |
| 1127 |
payment_type => 'CASH', |
| 1128 |
} |
| 1129 |
} |
| 1130 |
); |
| 1131 |
|
| 1132 |
my $start2 = $register5->start_cashup( { manager_id => $manager->id } ); |
| 1133 |
|
| 1134 |
my $complete2 = $register5->add_cashup( { manager_id => $manager->id, amount => '1.00' } ); |
| 1135 |
$complete2->timestamp( \'NOW() - INTERVAL 1 MINUTE' )->store(); |
| 1136 |
|
| 1137 |
$in_progress = $register5->cashup_in_progress; |
| 1138 |
is( |
| 1139 |
ref($in_progress), 'Koha::Cash::Register::Action', |
| 1140 |
'Correctly identifies active cashup when completion is backdated' |
| 1141 |
); |
| 1142 |
}; |
| 1143 |
|
| 1144 |
# Test 6: Performance with many cashups |
| 1145 |
subtest 'performance_with_many_cashups' => sub { |
| 1146 |
plan tests => 1; |
| 1147 |
|
| 1148 |
my $register6 = $builder->build_object( { class => 'Koha::Cash::Registers' } ); |
| 1149 |
my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
| 1150 |
|
| 1151 |
# Add cash for the many quick cashups |
| 1152 |
for my $i ( 1 .. 10 ) { |
| 1153 |
$builder->build_object( |
| 1154 |
{ |
| 1155 |
class => 'Koha::Account::Lines', |
| 1156 |
value => { |
| 1157 |
register_id => $register6->id, |
| 1158 |
borrowernumber => $patron->id, |
| 1159 |
amount => -1.00, |
| 1160 |
credit_type_code => 'PAYMENT', |
| 1161 |
payment_type => 'CASH', |
| 1162 |
} |
| 1163 |
} |
| 1164 |
); |
| 1165 |
} |
| 1166 |
|
| 1167 |
# Create many quick cashups |
| 1168 |
for my $i ( 1 .. 10 ) { |
| 1169 |
my $cashup = $register6->add_cashup( { manager_id => $manager->id, amount => '1.00' } ); |
| 1170 |
my $timestamp = "NOW() - INTERVAL $i MINUTE"; |
| 1171 |
$cashup->timestamp( \$timestamp )->store(); |
| 1172 |
} |
| 1173 |
|
| 1174 |
# Add cash for two-phase cashup |
| 1175 |
$builder->build_object( |
| 1176 |
{ |
| 1177 |
class => 'Koha::Account::Lines', |
| 1178 |
value => { |
| 1179 |
register_id => $register6->id, |
| 1180 |
borrowernumber => $patron->id, |
| 1181 |
amount => -2.00, |
| 1182 |
credit_type_code => 'PAYMENT', |
| 1183 |
payment_type => 'CASH', |
| 1184 |
} |
| 1185 |
} |
| 1186 |
); |
| 1187 |
|
| 1188 |
# Start a two-phase cashup |
| 1189 |
my $start = $register6->start_cashup( { manager_id => $manager->id } ); |
| 1190 |
|
| 1191 |
my $in_progress = $register6->cashup_in_progress; |
| 1192 |
is( ref($in_progress), 'Koha::Cash::Register::Action', 'Performs correctly with many previous cashups' ); |
| 1193 |
}; |
| 1194 |
|
| 1195 |
$schema->storage->txn_rollback; |
| 1196 |
}; |
| 1197 |
|
| 1198 |
subtest 'start_cashup_parameter_validation' => sub { |
| 1199 |
plan tests => 5; |
| 1200 |
|
| 1201 |
$schema->storage->txn_begin; |
| 1202 |
|
| 1203 |
my $register = $builder->build_object( { class => 'Koha::Cash::Registers' } ); |
| 1204 |
my $manager = $builder->build_object( { class => 'Koha::Patrons' } ); |
| 1205 |
|
| 1206 |
# Test 1: Valid parameters |
| 1207 |
subtest 'valid_parameters' => sub { |
| 1208 |
plan tests => 3; |
| 1209 |
|
| 1210 |
my $register1 = $builder->build_object( { class => 'Koha::Cash::Registers' } ); |
| 1211 |
my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
| 1212 |
|
| 1213 |
# Add cash transaction before starting cashup |
| 1214 |
$builder->build_object( |
| 1215 |
{ |
| 1216 |
class => 'Koha::Account::Lines', |
| 1217 |
value => { |
| 1218 |
register_id => $register1->id, |
| 1219 |
borrowernumber => $patron->id, |
| 1220 |
amount => -5.00, |
| 1221 |
credit_type_code => 'PAYMENT', |
| 1222 |
payment_type => 'CASH', |
| 1223 |
} |
| 1224 |
} |
| 1225 |
); |
| 1226 |
|
| 1227 |
my $cashup_start = $register1->start_cashup( { manager_id => $manager->id } ); |
| 1228 |
|
| 1229 |
is( ref($cashup_start), 'Koha::Cash::Register::Cashup', 'start_cashup returns correct object type' ); |
| 1230 |
is( $cashup_start->manager_id, $manager->id, 'manager_id set correctly' ); |
| 1231 |
is( $cashup_start->code, 'CASHUP_START', 'code set correctly to CASHUP_START' ); |
| 1232 |
}; |
| 1233 |
|
| 1234 |
# Test 2: Missing manager_id |
| 1235 |
subtest 'missing_manager_id' => sub { |
| 1236 |
plan tests => 1; |
| 1237 |
|
| 1238 |
my $register2 = $builder->build_object( { class => 'Koha::Cash::Registers' } ); |
| 1239 |
|
| 1240 |
eval { $register2->start_cashup( {} ); }; |
| 1241 |
ok( $@, 'start_cashup fails when manager_id is missing' ); |
| 1242 |
}; |
| 1243 |
|
| 1244 |
# Test 3: Invalid manager_id |
| 1245 |
subtest 'invalid_manager_id' => sub { |
| 1246 |
plan tests => 1; |
| 1247 |
|
| 1248 |
my $register3 = $builder->build_object( { class => 'Koha::Cash::Registers' } ); |
| 1249 |
my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
| 1250 |
|
| 1251 |
# Add cash transaction before starting cashup |
| 1252 |
$builder->build_object( |
| 1253 |
{ |
| 1254 |
class => 'Koha::Account::Lines', |
| 1255 |
value => { |
| 1256 |
register_id => $register3->id, |
| 1257 |
borrowernumber => $patron->id, |
| 1258 |
amount => -5.00, |
| 1259 |
credit_type_code => 'PAYMENT', |
| 1260 |
payment_type => 'CASH', |
| 1261 |
} |
| 1262 |
} |
| 1263 |
); |
| 1264 |
|
| 1265 |
throws_ok { |
| 1266 |
$register3->start_cashup( { manager_id => 99999999 } ); |
| 1267 |
} |
| 1268 |
'Koha::Exceptions::Object::FKConstraint', 'start_cashup throws FK constraint exception with invalid manager_id'; |
| 1269 |
}; |
| 1270 |
|
| 1271 |
# Test 4: Duplicate start_cashup prevention |
| 1272 |
subtest 'duplicate_prevention' => sub { |
| 1273 |
plan tests => 2; |
| 1274 |
|
| 1275 |
my $register4 = $builder->build_object( { class => 'Koha::Cash::Registers' } ); |
| 1276 |
my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
| 1277 |
|
| 1278 |
# Add cash transaction before starting cashup |
| 1279 |
$builder->build_object( |
| 1280 |
{ |
| 1281 |
class => 'Koha::Account::Lines', |
| 1282 |
value => { |
| 1283 |
register_id => $register4->id, |
| 1284 |
borrowernumber => $patron->id, |
| 1285 |
amount => -5.00, |
| 1286 |
credit_type_code => 'PAYMENT', |
| 1287 |
payment_type => 'CASH', |
| 1288 |
} |
| 1289 |
} |
| 1290 |
); |
| 1291 |
|
| 1292 |
# First start should succeed |
| 1293 |
my $first_start = $register4->start_cashup( { manager_id => $manager->id } ); |
| 1294 |
ok( $first_start, 'First start_cashup succeeds' ); |
| 1295 |
|
| 1296 |
# Second start should fail |
| 1297 |
throws_ok { |
| 1298 |
$register4->start_cashup( { manager_id => $manager->id } ); |
| 1299 |
} |
| 1300 |
'Koha::Exceptions::Object::DuplicateID', |
| 1301 |
'Second start_cashup throws DuplicateID exception'; |
| 1302 |
}; |
| 1303 |
|
| 1304 |
# Test 5: Database transaction integrity |
| 1305 |
subtest 'transaction_integrity' => sub { |
| 1306 |
plan tests => 3; |
| 1307 |
|
| 1308 |
my $register5 = $builder->build_object( { class => 'Koha::Cash::Registers' } ); |
| 1309 |
|
| 1310 |
# Add some transactions to establish expected amount |
| 1311 |
my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
| 1312 |
my $account = $patron->account; |
| 1313 |
|
| 1314 |
my $fine = $account->add_debit( |
| 1315 |
{ |
| 1316 |
amount => '15.00', |
| 1317 |
type => 'OVERDUE', |
| 1318 |
interface => 'cron' |
| 1319 |
} |
| 1320 |
); |
| 1321 |
|
| 1322 |
my $payment = $account->pay( |
| 1323 |
{ |
| 1324 |
cash_register => $register5->id, |
| 1325 |
amount => '15.00', |
| 1326 |
credit_type => 'PAYMENT', |
| 1327 |
payment_type => 'CASH', |
| 1328 |
lines => [$fine] |
| 1329 |
} |
| 1330 |
); |
| 1331 |
|
| 1332 |
my $initial_action_count = $register5->_result->search_related('cash_register_actions')->count; |
| 1333 |
|
| 1334 |
my $start = $register5->start_cashup( { manager_id => $manager->id } ); |
| 1335 |
|
| 1336 |
# Verify action was created |
| 1337 |
my $final_action_count = $register5->_result->search_related('cash_register_actions')->count; |
| 1338 |
is( $final_action_count, $initial_action_count + 1, 'CASHUP_START action created in database' ); |
| 1339 |
|
| 1340 |
# Verify expected amount calculation (can be positive or negative, but not zero) |
| 1341 |
ok( $start->amount != 0, 'Expected amount calculated correctly' ); |
| 1342 |
|
| 1343 |
# Verify timestamp is set |
| 1344 |
ok( defined $start->timestamp, 'Timestamp is set on CASHUP_START action' ); |
| 1345 |
}; |
| 1346 |
|
| 1347 |
$schema->storage->txn_rollback; |
| 1348 |
}; |
| 1349 |
|
| 1350 |
subtest 'add_cashup' => sub { |
| 1351 |
plan tests => 5; |
| 1352 |
|
| 1353 |
$schema->storage->txn_begin; |
| 1354 |
|
| 1355 |
my $register = $builder->build_object( { class => 'Koha::Cash::Registers' } ); |
| 1356 |
my $manager = $builder->build_object( { class => 'Koha::Patrons' } ); |
| 1357 |
|
| 1358 |
# Test 1: Valid parameters |
| 1359 |
subtest 'valid_parameters' => sub { |
| 1360 |
plan tests => 3; |
| 1361 |
|
| 1362 |
my $register1 = $builder->build_object( { class => 'Koha::Cash::Registers' } ); |
| 1363 |
|
| 1364 |
my $cashup = $register1->add_cashup( { manager_id => $manager->id, amount => '10.00' } ); |
| 1365 |
|
| 1366 |
is( ref($cashup), 'Koha::Cash::Register::Cashup', 'add_cashup returns correct object type' ); |
| 1367 |
is( $cashup->manager_id, $manager->id, 'manager_id set correctly' ); |
| 1368 |
is( $cashup->amount + 0, 10, 'amount set correctly' ); |
| 1369 |
}; |
| 1370 |
|
| 1371 |
# Test 2: Missing required parameters |
| 1372 |
subtest 'missing_parameters' => sub { |
| 1373 |
plan tests => 3; |
| 1374 |
|
| 1375 |
my $register2 = $builder->build_object( { class => 'Koha::Cash::Registers' } ); |
| 1376 |
|
| 1377 |
# Missing manager_id |
| 1378 |
eval { $register2->add_cashup( { amount => '10.00' } ); }; |
| 1379 |
ok( $@, 'add_cashup fails when manager_id is missing' ); |
| 1380 |
|
| 1381 |
# Missing amount |
| 1382 |
eval { $register2->add_cashup( { manager_id => $manager->id } ); }; |
| 1383 |
ok( $@, 'add_cashup fails when amount is missing' ); |
| 1384 |
|
| 1385 |
# Missing both |
| 1386 |
eval { $register2->add_cashup( {} ); }; |
| 1387 |
ok( $@, 'add_cashup fails when both parameters are missing' ); |
| 1388 |
}; |
| 1389 |
|
| 1390 |
# Test 3: Invalid amount parameter |
| 1391 |
subtest 'invalid_amount' => sub { |
| 1392 |
plan tests => 6; |
| 1393 |
|
| 1394 |
my $register3 = $builder->build_object( { class => 'Koha::Cash::Registers' } ); |
| 1395 |
|
| 1396 |
# Zero amount is now valid (for non-cash transaction scenarios) |
| 1397 |
my $zero_cashup; |
| 1398 |
lives_ok { |
| 1399 |
$zero_cashup = $register3->add_cashup( { manager_id => $manager->id, amount => '0.00' } ); |
| 1400 |
} |
| 1401 |
'Zero amount is accepted for non-cash transaction scenarios'; |
| 1402 |
is( $zero_cashup->amount + 0, 0, 'Zero amount stored correctly' ); |
| 1403 |
|
| 1404 |
# Negative amount is now valid (for float deficits) |
| 1405 |
my $negative_cashup; |
| 1406 |
lives_ok { |
| 1407 |
$negative_cashup = $register3->add_cashup( { manager_id => $manager->id, amount => '-5.00' } ); |
| 1408 |
} |
| 1409 |
'Negative amount is accepted for float deficit scenarios'; |
| 1410 |
is( $negative_cashup->amount + 0, -5, 'Negative amount stored correctly' ); |
| 1411 |
|
| 1412 |
# Non-numeric amount |
| 1413 |
throws_ok { |
| 1414 |
$register3->add_cashup( { manager_id => $manager->id, amount => 'invalid' } ); |
| 1415 |
} |
| 1416 |
'Koha::Exceptions::Account::AmountNotPositive', |
| 1417 |
'Non-numeric amount throws AmountNotPositive exception'; |
| 1418 |
|
| 1419 |
# Empty string amount |
| 1420 |
throws_ok { |
| 1421 |
$register3->add_cashup( { manager_id => $manager->id, amount => '' } ); |
| 1422 |
} |
| 1423 |
'Koha::Exceptions::Account::AmountNotPositive', |
| 1424 |
'Empty string amount throws AmountNotPositive exception'; |
| 1425 |
}; |
| 1426 |
|
| 1427 |
# Test 4: Reconciliation note handling |
| 1428 |
subtest 'reconciliation_note_handling' => sub { |
| 1429 |
plan tests => 4; |
| 1430 |
|
| 1431 |
my $register4 = $builder->build_object( { class => 'Koha::Cash::Registers' } ); |
| 1432 |
my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
| 1433 |
my $account = $patron->account; |
| 1434 |
|
| 1435 |
# Create transaction to enable surplus creation |
| 1436 |
my $fine = $account->add_debit( |
| 1437 |
{ |
| 1438 |
amount => '10.00', |
| 1439 |
type => 'OVERDUE', |
| 1440 |
interface => 'cron' |
| 1441 |
} |
| 1442 |
); |
| 1443 |
|
| 1444 |
my $payment = $account->pay( |
| 1445 |
{ |
| 1446 |
cash_register => $register4->id, |
| 1447 |
amount => '10.00', |
| 1448 |
credit_type => 'PAYMENT', |
| 1449 |
payment_type => 'CASH', |
| 1450 |
lines => [$fine] |
| 1451 |
} |
| 1452 |
); |
| 1453 |
|
| 1454 |
# Test normal note |
| 1455 |
my $cashup1 = $register4->add_cashup( |
| 1456 |
{ |
| 1457 |
manager_id => $manager->id, |
| 1458 |
amount => '15.00', # Creates surplus |
| 1459 |
reconciliation_note => 'Found extra money in drawer' |
| 1460 |
} |
| 1461 |
); |
| 1462 |
|
| 1463 |
my $surplus1 = Koha::Account::Lines->search( |
| 1464 |
{ |
| 1465 |
register_id => $register4->id, |
| 1466 |
credit_type_code => 'CASHUP_SURPLUS' |
| 1467 |
} |
| 1468 |
)->next; |
| 1469 |
is( $surplus1->note, 'Found extra money in drawer', 'Normal reconciliation note stored correctly' ); |
| 1470 |
|
| 1471 |
# Test very long note (should be truncated) |
| 1472 |
my $register5 = $builder->build_object( { class => 'Koha::Cash::Registers' } ); |
| 1473 |
my $long_note = 'x' x 1500; # Longer than 1000 character limit |
| 1474 |
|
| 1475 |
my $fine2 = $account->add_debit( |
| 1476 |
{ |
| 1477 |
amount => '10.00', |
| 1478 |
type => 'OVERDUE', |
| 1479 |
interface => 'cron' |
| 1480 |
} |
| 1481 |
); |
| 1482 |
|
| 1483 |
my $payment2 = $account->pay( |
| 1484 |
{ |
| 1485 |
cash_register => $register5->id, |
| 1486 |
amount => '10.00', |
| 1487 |
credit_type => 'PAYMENT', |
| 1488 |
payment_type => 'CASH', |
| 1489 |
lines => [$fine2] |
| 1490 |
} |
| 1491 |
); |
| 1492 |
|
| 1493 |
my $cashup2 = $register5->add_cashup( |
| 1494 |
{ |
| 1495 |
manager_id => $manager->id, |
| 1496 |
amount => '15.00', |
| 1497 |
reconciliation_note => $long_note |
| 1498 |
} |
| 1499 |
); |
| 1500 |
|
| 1501 |
my $surplus2 = Koha::Account::Lines->search( |
| 1502 |
{ |
| 1503 |
register_id => $register5->id, |
| 1504 |
credit_type_code => 'CASHUP_SURPLUS' |
| 1505 |
} |
| 1506 |
)->next; |
| 1507 |
is( length( $surplus2->note ), 1000, 'Long reconciliation note truncated to 1000 characters' ); |
| 1508 |
|
| 1509 |
# Test whitespace-only note (should be undef) |
| 1510 |
my $register6 = $builder->build_object( { class => 'Koha::Cash::Registers' } ); |
| 1511 |
|
| 1512 |
my $fine3 = $account->add_debit( |
| 1513 |
{ |
| 1514 |
amount => '10.00', |
| 1515 |
type => 'OVERDUE', |
| 1516 |
interface => 'cron' |
| 1517 |
} |
| 1518 |
); |
| 1519 |
|
| 1520 |
my $payment3 = $account->pay( |
| 1521 |
{ |
| 1522 |
cash_register => $register6->id, |
| 1523 |
amount => '10.00', |
| 1524 |
credit_type => 'PAYMENT', |
| 1525 |
payment_type => 'CASH', |
| 1526 |
lines => [$fine3] |
| 1527 |
} |
| 1528 |
); |
| 1529 |
|
| 1530 |
my $cashup3 = $register6->add_cashup( |
| 1531 |
{ |
| 1532 |
manager_id => $manager->id, |
| 1533 |
amount => '15.00', |
| 1534 |
reconciliation_note => ' ' # Whitespace only |
| 1535 |
} |
| 1536 |
); |
| 1537 |
|
| 1538 |
my $surplus3 = Koha::Account::Lines->search( |
| 1539 |
{ |
| 1540 |
register_id => $register6->id, |
| 1541 |
credit_type_code => 'CASHUP_SURPLUS' |
| 1542 |
} |
| 1543 |
)->next; |
| 1544 |
is( $surplus3->note, undef, 'Whitespace-only reconciliation note stored as undef' ); |
| 1545 |
|
| 1546 |
# Test empty string note (should be undef) |
| 1547 |
my $register7 = $builder->build_object( { class => 'Koha::Cash::Registers' } ); |
| 1548 |
|
| 1549 |
my $fine4 = $account->add_debit( |
| 1550 |
{ |
| 1551 |
amount => '10.00', |
| 1552 |
type => 'OVERDUE', |
| 1553 |
interface => 'cron' |
| 1554 |
} |
| 1555 |
); |
| 1556 |
|
| 1557 |
my $payment4 = $account->pay( |
| 1558 |
{ |
| 1559 |
cash_register => $register7->id, |
| 1560 |
amount => '10.00', |
| 1561 |
credit_type => 'PAYMENT', |
| 1562 |
payment_type => 'CASH', |
| 1563 |
lines => [$fine4] |
| 1564 |
} |
| 1565 |
); |
| 1566 |
|
| 1567 |
my $cashup4 = $register7->add_cashup( |
| 1568 |
{ |
| 1569 |
manager_id => $manager->id, |
| 1570 |
amount => '15.00', |
| 1571 |
reconciliation_note => '' # Empty string |
| 1572 |
} |
| 1573 |
); |
| 1574 |
|
| 1575 |
my $surplus4 = Koha::Account::Lines->search( |
| 1576 |
{ |
| 1577 |
register_id => $register7->id, |
| 1578 |
credit_type_code => 'CASHUP_SURPLUS' |
| 1579 |
} |
| 1580 |
)->next; |
| 1581 |
is( $surplus4->note, undef, 'Empty string reconciliation note stored as undef' ); |
| 1582 |
}; |
| 1583 |
|
| 1584 |
# Test 5: Invalid manager_id |
| 1585 |
subtest 'invalid_manager_id' => sub { |
| 1586 |
plan tests => 1; |
| 1587 |
|
| 1588 |
my $register9 = $builder->build_object( { class => 'Koha::Cash::Registers' } ); |
| 1589 |
|
| 1590 |
throws_ok { |
| 1591 |
$register9->add_cashup( { manager_id => 99999999, amount => '10.00' } ); |
| 1592 |
} |
| 1593 |
'Koha::Exceptions::Object::FKConstraint', 'add_cashup throws FK constraint exception with invalid manager_id'; |
| 1594 |
}; |
| 1595 |
|
| 1596 |
$schema->storage->txn_rollback; |
| 1597 |
}; |
| 1598 |
|
| 1599 |
subtest 'required_reconciliation_note' => sub { |
| 1600 |
plan tests => 4; |
| 1601 |
|
| 1602 |
$schema->storage->txn_begin; |
| 1603 |
|
| 1604 |
my $register = $builder->build_object( { class => 'Koha::Cash::Registers' } ); |
| 1605 |
my $manager = $builder->build_object( { class => 'Koha::Patrons' } ); |
| 1606 |
my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
| 1607 |
my $account = $patron->account; |
| 1608 |
|
| 1609 |
# Create a cash transaction |
| 1610 |
my $fine = $account->add_debit( |
| 1611 |
{ |
| 1612 |
amount => '10.00', |
| 1613 |
type => 'OVERDUE', |
| 1614 |
interface => 'cron' |
| 1615 |
} |
| 1616 |
); |
| 1617 |
|
| 1618 |
my $payment = $account->pay( |
| 1619 |
{ |
| 1620 |
cash_register => $register->id, |
| 1621 |
amount => '10.00', |
| 1622 |
credit_type => 'PAYMENT', |
| 1623 |
payment_type => 'CASH', |
| 1624 |
lines => [$fine] |
| 1625 |
} |
| 1626 |
); |
| 1627 |
|
| 1628 |
# Enable the required note preference |
| 1629 |
t::lib::Mocks::mock_preference( 'CashupReconciliationNoteRequired', 1 ); |
| 1630 |
|
| 1631 |
# Test 1: Missing note with discrepancy throws exception |
| 1632 |
throws_ok { |
| 1633 |
$register->add_cashup( |
| 1634 |
{ |
| 1635 |
manager_id => $manager->id, |
| 1636 |
amount => '15.00' # Creates discrepancy |
| 1637 |
} |
| 1638 |
); |
| 1639 |
} |
| 1640 |
'Koha::Exceptions::MissingParameter', |
| 1641 |
'Missing reconciliation note with discrepancy throws MissingParameter exception when preference enabled'; |
| 1642 |
|
| 1643 |
# Test 2: Note provided with discrepancy succeeds |
| 1644 |
my $cashup1; |
| 1645 |
lives_ok { |
| 1646 |
$cashup1 = $register->add_cashup( |
| 1647 |
{ |
| 1648 |
manager_id => $manager->id, |
| 1649 |
amount => '15.00', |
| 1650 |
reconciliation_note => 'Found extra money' |
| 1651 |
} |
| 1652 |
); |
| 1653 |
} |
| 1654 |
'Cashup with note and discrepancy succeeds when preference enabled'; |
| 1655 |
|
| 1656 |
# Test 3: No note with no discrepancy succeeds (note only required for discrepancies) |
| 1657 |
my $register2 = $builder->build_object( { class => 'Koha::Cash::Registers' } ); |
| 1658 |
my $fine2 = $account->add_debit( |
| 1659 |
{ |
| 1660 |
amount => '20.00', |
| 1661 |
type => 'OVERDUE', |
| 1662 |
interface => 'cron' |
| 1663 |
} |
| 1664 |
); |
| 1665 |
|
| 1666 |
my $payment2 = $account->pay( |
| 1667 |
{ |
| 1668 |
cash_register => $register2->id, |
| 1669 |
amount => '20.00', |
| 1670 |
credit_type => 'PAYMENT', |
| 1671 |
payment_type => 'CASH', |
| 1672 |
lines => [$fine2] |
| 1673 |
} |
| 1674 |
); |
| 1675 |
|
| 1676 |
my $cashup2; |
| 1677 |
lives_ok { |
| 1678 |
$cashup2 = $register2->add_cashup( |
| 1679 |
{ |
| 1680 |
manager_id => $manager->id, |
| 1681 |
amount => '20.00' # Exact amount, no discrepancy |
| 1682 |
} |
| 1683 |
); |
| 1684 |
} |
| 1685 |
'Cashup without note succeeds when there is no discrepancy'; |
| 1686 |
|
| 1687 |
# Test 4: Preference disabled allows missing note even with discrepancy |
| 1688 |
t::lib::Mocks::mock_preference( 'CashupReconciliationNoteRequired', 0 ); |
| 1689 |
|
| 1690 |
my $register3 = $builder->build_object( { class => 'Koha::Cash::Registers' } ); |
| 1691 |
my $fine3 = $account->add_debit( |
| 1692 |
{ |
| 1693 |
amount => '10.00', |
| 1694 |
type => 'OVERDUE', |
| 1695 |
interface => 'cron' |
| 1696 |
} |
| 1697 |
); |
| 1698 |
|
| 1699 |
my $payment3 = $account->pay( |
| 1700 |
{ |
| 1701 |
cash_register => $register3->id, |
| 1702 |
amount => '10.00', |
| 1703 |
credit_type => 'PAYMENT', |
| 1704 |
payment_type => 'CASH', |
| 1705 |
lines => [$fine3] |
| 1706 |
} |
| 1707 |
); |
| 1708 |
|
| 1709 |
my $cashup3; |
| 1710 |
lives_ok { |
| 1711 |
$cashup3 = $register3->add_cashup( |
| 1712 |
{ |
| 1713 |
manager_id => $manager->id, |
| 1714 |
amount => '15.00' # Creates discrepancy |
| 1715 |
} |
| 1716 |
); |
| 1717 |
} |
| 1718 |
'Missing note with discrepancy succeeds when preference disabled'; |
| 1719 |
|
| 1720 |
$schema->storage->txn_rollback; |
| 1721 |
}; |