|
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 => 10; |
| 24 |
|
24 |
|
| 25 |
use Test::Exception; |
25 |
use Test::Exception; |
| 26 |
|
26 |
|
|
Lines 260-266
subtest 'cashup' => sub {
Link Here
|
| 260 |
subtest 'outstanding_accountlines' => sub { |
260 |
subtest 'outstanding_accountlines' => sub { |
| 261 |
plan tests => 6; |
261 |
plan tests => 6; |
| 262 |
|
262 |
|
| 263 |
my $accountlines = $register->outstanding_accountlines; |
263 |
$schema->storage->txn_begin; |
|
|
264 |
|
| 265 |
my $test_register = $builder->build_object( { class => 'Koha::Cash::Registers' } ); |
| 266 |
my $accountlines = $test_register->outstanding_accountlines; |
| 264 |
is( |
267 |
is( |
| 265 |
ref($accountlines), 'Koha::Account::Lines', |
268 |
ref($accountlines), 'Koha::Account::Lines', |
| 266 |
'Koha::Cash::Register->outstanding_accountlines should always return a Koha::Account::Lines set' |
269 |
'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' |
273 |
'Koha::Cash::Register->outstanding_accountlines should always return the correct number of accountlines' |
| 271 |
); |
274 |
); |
| 272 |
|
275 |
|
|
|
276 |
my $test_patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
| 277 |
|
| 273 |
my $accountline1 = $builder->build_object( |
278 |
my $accountline1 = $builder->build_object( |
| 274 |
{ |
279 |
{ |
| 275 |
class => 'Koha::Account::Lines', |
280 |
class => 'Koha::Account::Lines', |
| 276 |
value => { register_id => $register->id, date => \'NOW() - INTERVAL 5 MINUTE' }, |
281 |
value => { |
|
|
282 |
register_id => $test_register->id, |
| 283 |
amount => -2.50, |
| 284 |
date => \'SYSDATE() - INTERVAL 5 MINUTE', |
| 285 |
payment_type => 'CASH' |
| 286 |
}, |
| 277 |
} |
287 |
} |
| 278 |
); |
288 |
); |
| 279 |
my $accountline2 = $builder->build_object( |
289 |
my $accountline2 = $builder->build_object( |
| 280 |
{ |
290 |
{ |
| 281 |
class => 'Koha::Account::Lines', |
291 |
class => 'Koha::Account::Lines', |
| 282 |
value => { register_id => $register->id, date => \'NOW() - INTERVAL 5 MINUTE' }, |
292 |
value => { |
|
|
293 |
register_id => $test_register->id, |
| 294 |
amount => -1.50, |
| 295 |
date => \'SYSDATE() - INTERVAL 5 MINUTE', |
| 296 |
payment_type => 'CASH' |
| 297 |
}, |
| 283 |
} |
298 |
} |
| 284 |
); |
299 |
); |
| 285 |
|
300 |
|
| 286 |
$accountlines = $register->outstanding_accountlines; |
301 |
$accountlines = $test_register->outstanding_accountlines; |
| 287 |
is( $accountlines->count, 2, 'No cashup, all accountlines returned' ); |
302 |
is( $accountlines->count, 2, 'No cashup, all accountlines returned' ); |
| 288 |
|
303 |
|
| 289 |
my $cashup3 = $register->add_cashup( { manager_id => $patron->id, amount => '2.50' } ); |
304 |
# Calculate expected amount for this cashup |
|
|
305 |
my $expected_amount = |
| 306 |
( $test_register->outstanding_accountlines->total( { payment_type => [ 'CASH', 'SIP00' ] } ) ) * -1; |
| 307 |
my $cashup3 = $test_register->add_cashup( { manager_id => $test_patron->id, amount => $expected_amount } ); |
| 290 |
|
308 |
|
| 291 |
$accountlines = $register->outstanding_accountlines; |
309 |
$accountlines = $test_register->outstanding_accountlines; |
| 292 |
is( $accountlines->count, 0, 'Cashup added, no accountlines returned' ); |
310 |
is( $accountlines->count, 0, 'Cashup added, no accountlines returned' ); |
| 293 |
|
311 |
|
| 294 |
my $accountline3 = $builder->build_object( |
312 |
my $accountline3 = $builder->build_object( |
| 295 |
{ |
313 |
{ |
| 296 |
class => 'Koha::Account::Lines', |
314 |
class => 'Koha::Account::Lines', |
| 297 |
value => { register_id => $register->id }, |
315 |
value => { |
|
|
316 |
register_id => $test_register->id, |
| 317 |
amount => 1.50, |
| 318 |
date => \'SYSDATE() + INTERVAL 5 MINUTE', |
| 319 |
payment_type => 'CASH' |
| 320 |
}, |
| 298 |
} |
321 |
} |
| 299 |
); |
322 |
); |
| 300 |
|
323 |
|
| 301 |
# Fake the cashup timestamp to make sure it's before the accountline we just added, |
324 |
$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( |
325 |
is( |
| 307 |
$accountlines->count, 1, |
326 |
$accountlines->count, 1, |
| 308 |
'Accountline added, one accountline returned' |
327 |
'Accountline added, one accountline returned' |
|
Lines 311-366
subtest 'cashup' => sub {
Link Here
|
| 311 |
$accountlines->next->id, |
330 |
$accountlines->next->id, |
| 312 |
$accountline3->id, 'Correct accountline returned' |
331 |
$accountline3->id, 'Correct accountline returned' |
| 313 |
); |
332 |
); |
|
|
333 |
|
| 334 |
$schema->storage->txn_rollback; |
| 314 |
}; |
335 |
}; |
| 315 |
|
336 |
|
| 316 |
$schema->storage->txn_rollback; |
337 |
$schema->storage->txn_rollback; |
| 317 |
}; |
338 |
}; |
| 318 |
|
339 |
|
| 319 |
subtest 'cashup_reconciliation' => sub { |
340 |
subtest 'cashup_reconciliation' => sub { |
| 320 |
plan tests => 5; |
341 |
plan tests => 6; |
| 321 |
|
342 |
|
| 322 |
$schema->storage->txn_begin; |
343 |
$schema->storage->txn_begin; |
| 323 |
|
344 |
|
| 324 |
# Ensure required account types for reconciliation exist (they should already exist from mandatory data) |
|
|
| 325 |
use Koha::Account::CreditTypes; |
| 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 |
|
| 364 |
my $register = $builder->build_object( { class => 'Koha::Cash::Registers' } ); |
345 |
my $register = $builder->build_object( { class => 'Koha::Cash::Registers' } ); |
| 365 |
my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
346 |
my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
| 366 |
|
347 |
|
|
Lines 371-379
subtest 'cashup_reconciliation' => sub {
Link Here
|
| 371 |
value => { |
352 |
value => { |
| 372 |
register_id => $register->id, |
353 |
register_id => $register->id, |
| 373 |
borrowernumber => $patron->id, |
354 |
borrowernumber => $patron->id, |
| 374 |
amount => -10.00, # Credit (payment) |
355 |
amount => -10.00, # Credit (payment) |
| 375 |
credit_type_code => 'PAYMENT', |
356 |
credit_type_code => 'PAYMENT', |
| 376 |
debit_type_code => undef, |
357 |
debit_type_code => undef, |
|
|
358 |
payment_type => 'CASH', |
| 359 |
date => \'SYSDATE() - INTERVAL 1 MINUTE', |
| 360 |
timestamp => \'SYSDATE() - INTERVAL 1 MINUTE', |
| 377 |
} |
361 |
} |
| 378 |
} |
362 |
} |
| 379 |
); |
363 |
); |
|
Lines 383-402
subtest 'cashup_reconciliation' => sub {
Link Here
|
| 383 |
value => { |
367 |
value => { |
| 384 |
register_id => $register->id, |
368 |
register_id => $register->id, |
| 385 |
borrowernumber => $patron->id, |
369 |
borrowernumber => $patron->id, |
| 386 |
amount => -5.00, # Credit (payment) |
370 |
amount => -5.00, # Credit (payment) |
| 387 |
credit_type_code => 'PAYMENT', |
371 |
credit_type_code => 'PAYMENT', |
| 388 |
debit_type_code => undef, |
372 |
debit_type_code => undef, |
|
|
373 |
payment_type => 'CASH', |
| 374 |
date => \'SYSDATE() - INTERVAL 1 MINUTE', |
| 375 |
timestamp => \'SYSDATE() - INTERVAL 1 MINUTE', |
| 389 |
} |
376 |
} |
| 390 |
} |
377 |
} |
| 391 |
); |
378 |
); |
| 392 |
|
379 |
|
| 393 |
my $expected_amount = $register->outstanding_accountlines->total; # Should be -15.00 |
380 |
my $expected_amount = |
|
|
381 |
$register->outstanding_accountlines->total( { payment_type => [ 'CASH', 'SIP00' ] } ); # Should be -15.00 |
| 382 |
is( $expected_amount, -15.00, "Expected cash amount is calculated correctly" ); |
| 394 |
|
383 |
|
| 395 |
subtest 'balanced_cashup' => sub { |
384 |
subtest 'balanced_cashup' => sub { |
| 396 |
plan tests => 3; |
385 |
plan tests => 3; |
| 397 |
|
386 |
|
|
|
387 |
$schema->storage->txn_begin; |
| 388 |
|
| 398 |
# Test exact match - no surplus/deficit accountlines should be created |
389 |
# Test exact match - no surplus/deficit accountlines should be created |
| 399 |
my $amount = abs($expected_amount); # 15.00 actual matches 15.00 expected |
390 |
my $amount = abs($expected_amount); # 15.00 actual matches 15.00 expected |
| 400 |
|
391 |
|
| 401 |
my $cashup = $register->add_cashup( |
392 |
my $cashup = $register->add_cashup( |
| 402 |
{ |
393 |
{ |
|
Lines 420-425
subtest 'cashup_reconciliation' => sub {
Link Here
|
| 420 |
); |
411 |
); |
| 421 |
|
412 |
|
| 422 |
is( $reconciliation_lines->count, 0, 'No reconciliation accountlines created for balanced cashup' ); |
413 |
is( $reconciliation_lines->count, 0, 'No reconciliation accountlines created for balanced cashup' ); |
|
|
414 |
|
| 415 |
$schema->storage->txn_rollback; |
| 423 |
}; |
416 |
}; |
| 424 |
|
417 |
|
| 425 |
subtest 'surplus_cashup' => sub { |
418 |
subtest 'surplus_cashup' => sub { |
|
Lines 437-449
subtest 'cashup_reconciliation' => sub {
Link Here
|
| 437 |
amount => -20.00, # Credit (payment) |
430 |
amount => -20.00, # Credit (payment) |
| 438 |
credit_type_code => 'PAYMENT', |
431 |
credit_type_code => 'PAYMENT', |
| 439 |
debit_type_code => undef, |
432 |
debit_type_code => undef, |
|
|
433 |
payment_type => 'CASH', |
| 440 |
} |
434 |
} |
| 441 |
} |
435 |
} |
| 442 |
); |
436 |
); |
| 443 |
|
437 |
|
| 444 |
my $expected = abs( $register2->outstanding_accountlines->total ); # 20.00 |
438 |
my $expected = |
| 445 |
my $actual = 25.00; # 5.00 surplus |
439 |
abs( $register2->outstanding_accountlines->total( { payment_type => [ 'CASH', 'SIP00' ] } ) ); # 20.00 |
| 446 |
my $surplus = $actual - $expected; |
440 |
my $actual = 25.00; # 5.00 surplus |
|
|
441 |
my $surplus = $actual - $expected; |
| 447 |
|
442 |
|
| 448 |
my $cashup = $register2->add_cashup( |
443 |
my $cashup = $register2->add_cashup( |
| 449 |
{ |
444 |
{ |
|
Lines 485-490
subtest 'cashup_reconciliation' => sub {
Link Here
|
| 485 |
amount => -10.00, |
480 |
amount => -10.00, |
| 486 |
credit_type_code => 'PAYMENT', |
481 |
credit_type_code => 'PAYMENT', |
| 487 |
debit_type_code => undef, |
482 |
debit_type_code => undef, |
|
|
483 |
payment_type => 'CASH', |
| 488 |
} |
484 |
} |
| 489 |
} |
485 |
} |
| 490 |
); |
486 |
); |
|
Lines 531-543
subtest 'cashup_reconciliation' => sub {
Link Here
|
| 531 |
amount => -30.00, # Credit (payment) |
527 |
amount => -30.00, # Credit (payment) |
| 532 |
credit_type_code => 'PAYMENT', |
528 |
credit_type_code => 'PAYMENT', |
| 533 |
debit_type_code => undef, |
529 |
debit_type_code => undef, |
|
|
530 |
payment_type => 'CASH', |
| 534 |
} |
531 |
} |
| 535 |
} |
532 |
} |
| 536 |
); |
533 |
); |
| 537 |
|
534 |
|
| 538 |
my $expected = abs( $register3->outstanding_accountlines->total ); # 30.00 |
535 |
my $expected = |
| 539 |
my $actual = 25.00; # 5.00 deficit |
536 |
abs( $register3->outstanding_accountlines->total( { payment_type => [ 'CASH', 'SIP00' ] } ) ); # 30.00 |
| 540 |
my $deficit = $expected - $actual; |
537 |
my $actual = 25.00; # 5.00 deficit |
|
|
538 |
my $deficit = $expected - $actual; |
| 541 |
|
539 |
|
| 542 |
my $cashup = $register3->add_cashup( |
540 |
my $cashup = $register3->add_cashup( |
| 543 |
{ |
541 |
{ |
|
Lines 579-584
subtest 'cashup_reconciliation' => sub {
Link Here
|
| 579 |
amount => -20.00, |
577 |
amount => -20.00, |
| 580 |
credit_type_code => 'PAYMENT', |
578 |
credit_type_code => 'PAYMENT', |
| 581 |
debit_type_code => undef, |
579 |
debit_type_code => undef, |
|
|
580 |
payment_type => 'CASH', |
| 582 |
} |
581 |
} |
| 583 |
} |
582 |
} |
| 584 |
); |
583 |
); |
|
Lines 625-630
subtest 'cashup_reconciliation' => sub {
Link Here
|
| 625 |
amount => -10.00, |
624 |
amount => -10.00, |
| 626 |
credit_type_code => 'PAYMENT', |
625 |
credit_type_code => 'PAYMENT', |
| 627 |
debit_type_code => undef, |
626 |
debit_type_code => undef, |
|
|
627 |
payment_type => 'CASH', |
| 628 |
} |
628 |
} |
| 629 |
} |
629 |
} |
| 630 |
); |
630 |
); |
|
Lines 677-682
subtest 'cashup_reconciliation' => sub {
Link Here
|
| 677 |
amount => -10.00, |
677 |
amount => -10.00, |
| 678 |
credit_type_code => 'PAYMENT', |
678 |
credit_type_code => 'PAYMENT', |
| 679 |
debit_type_code => undef, |
679 |
debit_type_code => undef, |
|
|
680 |
payment_type => 'CASH', |
| 680 |
} |
681 |
} |
| 681 |
} |
682 |
} |
| 682 |
); |
683 |
); |
|
Lines 716-721
subtest 'cashup_reconciliation' => sub {
Link Here
|
| 716 |
amount => -10.00, |
717 |
amount => -10.00, |
| 717 |
credit_type_code => 'PAYMENT', |
718 |
credit_type_code => 'PAYMENT', |
| 718 |
debit_type_code => undef, |
719 |
debit_type_code => undef, |
|
|
720 |
payment_type => 'CASH', |
| 719 |
} |
721 |
} |
| 720 |
} |
722 |
} |
| 721 |
); |
723 |
); |
|
Lines 745-747
subtest 'cashup_reconciliation' => sub {
Link Here
|
| 745 |
|
747 |
|
| 746 |
$schema->storage->txn_rollback; |
748 |
$schema->storage->txn_rollback; |
| 747 |
}; |
749 |
}; |
|
|
750 |
|
| 751 |
subtest 'two_phase_cashup_workflow' => sub { |
| 752 |
plan tests => 15; |
| 753 |
|
| 754 |
$schema->storage->txn_begin; |
| 755 |
|
| 756 |
# Create test data |
| 757 |
my $manager = $builder->build_object( { class => 'Koha::Patrons' } ); |
| 758 |
my $library = $builder->build_object( { class => 'Koha::Libraries' } ); |
| 759 |
my $register = $builder->build_object( |
| 760 |
{ |
| 761 |
class => 'Koha::Cash::Registers', |
| 762 |
value => { |
| 763 |
branch => $library->branchcode, |
| 764 |
starting_float => 0, |
| 765 |
} |
| 766 |
} |
| 767 |
); |
| 768 |
|
| 769 |
# Add some test transactions |
| 770 |
my $account_line1 = $builder->build_object( |
| 771 |
{ |
| 772 |
class => 'Koha::Account::Lines', |
| 773 |
value => { |
| 774 |
amount => 10.00, |
| 775 |
date => \'SYSDATE() - INTERVAL 1 MINUTE', |
| 776 |
register_id => undef, |
| 777 |
debit_type_code => 'OVERDUE', |
| 778 |
credit_type_code => undef, |
| 779 |
payment_type => undef, |
| 780 |
} |
| 781 |
} |
| 782 |
); |
| 783 |
|
| 784 |
my $account_line2 = $builder->build_object( |
| 785 |
{ |
| 786 |
class => 'Koha::Account::Lines', |
| 787 |
value => { |
| 788 |
amount => -5.00, |
| 789 |
date => \'SYSDATE() - INTERVAL 1 MINUTE', |
| 790 |
register_id => $register->id, |
| 791 |
debit_type_code => undef, |
| 792 |
credit_type_code => 'PAYMENT', |
| 793 |
payment_type => 'CASH' |
| 794 |
} |
| 795 |
} |
| 796 |
); |
| 797 |
|
| 798 |
# Test 1: start_cashup creates CASHUP_START action |
| 799 |
my $cashup_start = $register->start_cashup( { manager_id => $manager->id } ); |
| 800 |
|
| 801 |
is( |
| 802 |
ref $cashup_start, 'Koha::Cash::Register::Cashup', |
| 803 |
'start_cashup returns Cash::Register::Cashup object' |
| 804 |
); |
| 805 |
|
| 806 |
my $start_action = Koha::Cash::Register::Actions->search( |
| 807 |
{ |
| 808 |
register_id => $register->id, |
| 809 |
code => 'CASHUP_START' |
| 810 |
} |
| 811 |
)->next; |
| 812 |
|
| 813 |
ok( $start_action, 'CASHUP_START action created in database' ); |
| 814 |
is( $start_action->manager_id, $manager->id, 'CASHUP_START has correct manager_id' ); |
| 815 |
|
| 816 |
# Test 2: cashup_in_progress detects active cashup |
| 817 |
my $in_progress = $register->cashup_in_progress; |
| 818 |
ok( $in_progress, 'cashup_in_progress detects active cashup' ); |
| 819 |
is( $in_progress->id, $start_action->id, 'cashup_in_progress returns correct CASHUP_START action' ); |
| 820 |
|
| 821 |
# Test 3: Cannot start another cashup while one is in progress |
| 822 |
throws_ok { |
| 823 |
$register->start_cashup( { manager_id => $manager->id } ); |
| 824 |
} |
| 825 |
'Koha::Exceptions::Object::DuplicateID', |
| 826 |
'Cannot start second cashup while one is in progress'; |
| 827 |
|
| 828 |
# Test 4: outstanding_accountlines behavior during active cashup |
| 829 |
my $outstanding = $register->outstanding_accountlines; |
| 830 |
is( $outstanding->count, 0, 'outstanding_accountlines returns 0 during active cashup' ); |
| 831 |
|
| 832 |
# Test 5: Add transaction after cashup start (should appear in outstanding) |
| 833 |
my $account_line3 = $builder->build_object( |
| 834 |
{ |
| 835 |
class => 'Koha::Account::Lines', |
| 836 |
value => { |
| 837 |
amount => -8.00, |
| 838 |
date => \'SYSDATE() + INTERVAL 1 MINUTE', |
| 839 |
register_id => $register->id, |
| 840 |
debit_type_code => undef, |
| 841 |
credit_type_code => 'PAYMENT', |
| 842 |
payment_type => 'CASH', |
| 843 |
} |
| 844 |
} |
| 845 |
); |
| 846 |
|
| 847 |
# This new transaction should appear in outstanding (it's after CASHUP_START) |
| 848 |
$outstanding = $register->outstanding_accountlines; |
| 849 |
is( $outstanding->count, 1, 'New transaction after CASHUP_START appears in outstanding' ); |
| 850 |
|
| 851 |
# Test 6: outstanding_accountlines correctly handles session boundaries |
| 852 |
my $session_accountlines = $register->outstanding_accountlines; |
| 853 |
my $session_total = $session_accountlines->total; |
| 854 |
is( |
| 855 |
$session_total, -8.00, |
| 856 |
'outstanding_accountlines correctly calculates session totals with CASHUP_START cutoff' |
| 857 |
); |
| 858 |
|
| 859 |
# Test 7: Complete cashup with exact amount (no reconciliation) |
| 860 |
my $expected_cashup_amount = 5.00; # CASH PAYMENT prior to CASHUP_START |
| 861 |
my $cashup_complete = $register->add_cashup( |
| 862 |
{ |
| 863 |
manager_id => $manager->id, |
| 864 |
amount => $expected_cashup_amount |
| 865 |
} |
| 866 |
); |
| 867 |
|
| 868 |
is( |
| 869 |
ref $cashup_complete, 'Koha::Cash::Register::Cashup', |
| 870 |
'add_cashup returns Cashup object' |
| 871 |
); |
| 872 |
|
| 873 |
# Check no reconciliation lines were created |
| 874 |
my $surplus_lines = $cashup_complete->accountlines->search( |
| 875 |
{ |
| 876 |
register_id => $register->id, |
| 877 |
credit_type_code => 'CASHUP_SURPLUS' |
| 878 |
} |
| 879 |
); |
| 880 |
my $deficit_lines = $cashup_complete->accountlines->search( |
| 881 |
{ |
| 882 |
register_id => $register->id, |
| 883 |
debit_type_code => 'CASHUP_DEFICIT' |
| 884 |
} |
| 885 |
); |
| 886 |
|
| 887 |
is( $surplus_lines->count, 0, 'No surplus lines created for exact cashup' ); |
| 888 |
is( $deficit_lines->count, 0, 'No deficit lines created for exact cashup' ); |
| 889 |
|
| 890 |
# Test 8: cashup_in_progress returns undef after completion |
| 891 |
$in_progress = $register->cashup_in_progress; |
| 892 |
is( $in_progress, undef, 'cashup_in_progress returns undef after completion' ); |
| 893 |
|
| 894 |
# Test 9: outstanding_accountlines now includes new transaction |
| 895 |
$outstanding = $register->outstanding_accountlines; |
| 896 |
is( $outstanding->count, 1, 'outstanding_accountlines includes transaction after completion' ); |
| 897 |
is( $outstanding->total, -8.00, 'outstanding_accountlines total is correct after completion' ); |
| 898 |
|
| 899 |
$schema->storage->txn_rollback; |
| 900 |
}; |
| 901 |
|
| 902 |
subtest 'cashup_in_progress' => sub { |
| 903 |
plan tests => 6; |
| 904 |
|
| 905 |
$schema->storage->txn_begin; |
| 906 |
|
| 907 |
my $register = $builder->build_object( { class => 'Koha::Cash::Registers' } ); |
| 908 |
my $manager = $builder->build_object( { class => 'Koha::Patrons' } ); |
| 909 |
my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
| 910 |
|
| 911 |
# Test 1: No cashups ever performed |
| 912 |
subtest 'no_cashups_ever' => sub { |
| 913 |
plan tests => 1; |
| 914 |
|
| 915 |
my $in_progress = $register->cashup_in_progress; |
| 916 |
is( $in_progress, undef, 'cashup_in_progress returns undef when no cashups have ever been performed' ); |
| 917 |
}; |
| 918 |
|
| 919 |
# Test 2: Only quick cashups performed |
| 920 |
subtest 'only_quick_cashups' => sub { |
| 921 |
plan tests => 2; |
| 922 |
|
| 923 |
# Add cash for first quick cashup |
| 924 |
$builder->build_object( |
| 925 |
{ |
| 926 |
class => 'Koha::Account::Lines', |
| 927 |
value => { |
| 928 |
register_id => $register->id, |
| 929 |
borrowernumber => $patron->id, |
| 930 |
amount => -10.00, |
| 931 |
credit_type_code => 'PAYMENT', |
| 932 |
payment_type => 'CASH', |
| 933 |
} |
| 934 |
} |
| 935 |
); |
| 936 |
|
| 937 |
# Add a quick cashup |
| 938 |
my $quick_cashup = $register->add_cashup( { manager_id => $manager->id, amount => '10.00' } ); |
| 939 |
$quick_cashup->timestamp( \'NOW() - INTERVAL 30 MINUTE' )->store(); |
| 940 |
|
| 941 |
my $in_progress = $register->cashup_in_progress; |
| 942 |
is( $in_progress, undef, 'cashup_in_progress returns undef after quick cashup completion' ); |
| 943 |
|
| 944 |
# Add cash for second quick cashup |
| 945 |
$builder->build_object( |
| 946 |
{ |
| 947 |
class => 'Koha::Account::Lines', |
| 948 |
value => { |
| 949 |
register_id => $register->id, |
| 950 |
borrowernumber => $patron->id, |
| 951 |
amount => -5.00, |
| 952 |
credit_type_code => 'PAYMENT', |
| 953 |
payment_type => 'CASH', |
| 954 |
} |
| 955 |
} |
| 956 |
); |
| 957 |
|
| 958 |
# Add another quick cashup |
| 959 |
my $quick_cashup2 = $register->add_cashup( { manager_id => $manager->id, amount => '5.00' } ); |
| 960 |
|
| 961 |
$in_progress = $register->cashup_in_progress; |
| 962 |
is( $in_progress, undef, 'cashup_in_progress returns undef after multiple quick cashups' ); |
| 963 |
}; |
| 964 |
|
| 965 |
# Test 3: Multiple CASHUP_START actions |
| 966 |
subtest 'multiple_start_actions' => sub { |
| 967 |
plan tests => 2; |
| 968 |
|
| 969 |
my $register2 = $builder->build_object( { class => 'Koha::Cash::Registers' } ); |
| 970 |
my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
| 971 |
|
| 972 |
# Add cash transactions before starting cashup |
| 973 |
$builder->build_object( |
| 974 |
{ |
| 975 |
class => 'Koha::Account::Lines', |
| 976 |
value => { |
| 977 |
register_id => $register2->id, |
| 978 |
borrowernumber => $patron->id, |
| 979 |
amount => -5.00, |
| 980 |
credit_type_code => 'PAYMENT', |
| 981 |
payment_type => 'CASH', |
| 982 |
} |
| 983 |
} |
| 984 |
); |
| 985 |
|
| 986 |
# Create multiple CASHUP_START actions |
| 987 |
my $start1 = $register2->start_cashup( { manager_id => $manager->id } ); |
| 988 |
$start1->timestamp( \'NOW() - INTERVAL 60 MINUTE' )->store(); |
| 989 |
|
| 990 |
# Complete the first one |
| 991 |
my $complete1 = $register2->add_cashup( { manager_id => $manager->id, amount => '1.00' } ); |
| 992 |
$complete1->timestamp( \'NOW() - INTERVAL 50 MINUTE' )->store(); |
| 993 |
|
| 994 |
# Add more cash for second cashup |
| 995 |
$builder->build_object( |
| 996 |
{ |
| 997 |
class => 'Koha::Account::Lines', |
| 998 |
value => { |
| 999 |
register_id => $register2->id, |
| 1000 |
borrowernumber => $patron->id, |
| 1001 |
amount => -3.00, |
| 1002 |
credit_type_code => 'PAYMENT', |
| 1003 |
payment_type => 'CASH', |
| 1004 |
} |
| 1005 |
} |
| 1006 |
); |
| 1007 |
|
| 1008 |
# Start another one |
| 1009 |
my $start2 = $register2->start_cashup( { manager_id => $manager->id } ); |
| 1010 |
|
| 1011 |
my $in_progress = $register2->cashup_in_progress; |
| 1012 |
is( ref($in_progress), 'Koha::Cash::Register::Action', 'Returns most recent CASHUP_START when multiple exist' ); |
| 1013 |
is( $in_progress->id, $start2->id, 'Returns the correct (most recent) CASHUP_START action' ); |
| 1014 |
}; |
| 1015 |
|
| 1016 |
# Test 4: Mixed quick and two-phase workflows |
| 1017 |
subtest 'mixed_workflows' => sub { |
| 1018 |
plan tests => 3; |
| 1019 |
|
| 1020 |
my $register3 = $builder->build_object( { class => 'Koha::Cash::Registers' } ); |
| 1021 |
my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
| 1022 |
|
| 1023 |
# Add cash for first quick cashup |
| 1024 |
$builder->build_object( |
| 1025 |
{ |
| 1026 |
class => 'Koha::Account::Lines', |
| 1027 |
value => { |
| 1028 |
register_id => $register3->id, |
| 1029 |
borrowernumber => $patron->id, |
| 1030 |
amount => -5.00, |
| 1031 |
credit_type_code => 'PAYMENT', |
| 1032 |
payment_type => 'CASH', |
| 1033 |
} |
| 1034 |
} |
| 1035 |
); |
| 1036 |
|
| 1037 |
# Quick cashup first |
| 1038 |
my $quick = $register3->add_cashup( { manager_id => $manager->id, amount => '5.00' } ); |
| 1039 |
$quick->timestamp( \'NOW() - INTERVAL 40 MINUTE' )->store(); |
| 1040 |
|
| 1041 |
# Add cash for two-phase cashup |
| 1042 |
$builder->build_object( |
| 1043 |
{ |
| 1044 |
class => 'Koha::Account::Lines', |
| 1045 |
value => { |
| 1046 |
register_id => $register3->id, |
| 1047 |
borrowernumber => $patron->id, |
| 1048 |
amount => -3.00, |
| 1049 |
credit_type_code => 'PAYMENT', |
| 1050 |
payment_type => 'CASH', |
| 1051 |
} |
| 1052 |
} |
| 1053 |
); |
| 1054 |
|
| 1055 |
# Start two-phase |
| 1056 |
my $start = $register3->start_cashup( { manager_id => $manager->id } ); |
| 1057 |
$start->timestamp( \'NOW() - INTERVAL 30 MINUTE' )->store(); |
| 1058 |
|
| 1059 |
my $in_progress = $register3->cashup_in_progress; |
| 1060 |
is( ref($in_progress), 'Koha::Cash::Register::Action', 'Detects two-phase in progress after quick cashup' ); |
| 1061 |
is( $in_progress->id, $start->id, 'Returns correct CASHUP_START after mixed workflow' ); |
| 1062 |
|
| 1063 |
# Complete two-phase |
| 1064 |
my $complete = $register3->add_cashup( { manager_id => $manager->id, amount => '3.00' } ); |
| 1065 |
|
| 1066 |
$in_progress = $register3->cashup_in_progress; |
| 1067 |
is( $in_progress, undef, 'Returns undef after completing two-phase in mixed workflow' ); |
| 1068 |
}; |
| 1069 |
|
| 1070 |
# Test 5: Timestamp edge cases |
| 1071 |
subtest 'timestamp_edge_cases' => sub { |
| 1072 |
plan tests => 2; |
| 1073 |
|
| 1074 |
my $register4 = $builder->build_object( { class => 'Koha::Cash::Registers' } ); |
| 1075 |
my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
| 1076 |
|
| 1077 |
# Add cash for cashup |
| 1078 |
$builder->build_object( |
| 1079 |
{ |
| 1080 |
class => 'Koha::Account::Lines', |
| 1081 |
value => { |
| 1082 |
register_id => $register4->id, |
| 1083 |
borrowernumber => $patron->id, |
| 1084 |
amount => -2.00, |
| 1085 |
credit_type_code => 'PAYMENT', |
| 1086 |
payment_type => 'CASH', |
| 1087 |
} |
| 1088 |
} |
| 1089 |
); |
| 1090 |
|
| 1091 |
# Create CASHUP_START |
| 1092 |
my $start = $register4->start_cashup( { manager_id => $manager->id } ); |
| 1093 |
my $start_time = $start->timestamp; |
| 1094 |
|
| 1095 |
# Create CASHUP with exactly the same timestamp (edge case) |
| 1096 |
my $complete = $register4->add_cashup( { manager_id => $manager->id, amount => '1.00' } ); |
| 1097 |
$complete->timestamp($start_time)->store(); |
| 1098 |
|
| 1099 |
my $in_progress = $register4->cashup_in_progress; |
| 1100 |
is( $in_progress, undef, 'Handles same timestamp edge case correctly' ); |
| 1101 |
|
| 1102 |
# Test with CASHUP timestamp slightly before CASHUP_START (edge case) |
| 1103 |
my $register5 = $builder->build_object( { class => 'Koha::Cash::Registers' } ); |
| 1104 |
|
| 1105 |
# Add cash for register5 |
| 1106 |
$builder->build_object( |
| 1107 |
{ |
| 1108 |
class => 'Koha::Account::Lines', |
| 1109 |
value => { |
| 1110 |
register_id => $register5->id, |
| 1111 |
borrowernumber => $patron->id, |
| 1112 |
amount => -2.00, |
| 1113 |
credit_type_code => 'PAYMENT', |
| 1114 |
payment_type => 'CASH', |
| 1115 |
} |
| 1116 |
} |
| 1117 |
); |
| 1118 |
|
| 1119 |
my $start2 = $register5->start_cashup( { manager_id => $manager->id } ); |
| 1120 |
|
| 1121 |
my $complete2 = $register5->add_cashup( { manager_id => $manager->id, amount => '1.00' } ); |
| 1122 |
$complete2->timestamp( \'NOW() - INTERVAL 1 MINUTE' )->store(); |
| 1123 |
|
| 1124 |
$in_progress = $register5->cashup_in_progress; |
| 1125 |
is( |
| 1126 |
ref($in_progress), 'Koha::Cash::Register::Action', |
| 1127 |
'Correctly identifies active cashup when completion is backdated' |
| 1128 |
); |
| 1129 |
}; |
| 1130 |
|
| 1131 |
# Test 6: Performance with many cashups |
| 1132 |
subtest 'performance_with_many_cashups' => sub { |
| 1133 |
plan tests => 1; |
| 1134 |
|
| 1135 |
my $register6 = $builder->build_object( { class => 'Koha::Cash::Registers' } ); |
| 1136 |
my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
| 1137 |
|
| 1138 |
# Add cash for the many quick cashups |
| 1139 |
for my $i ( 1 .. 10 ) { |
| 1140 |
$builder->build_object( |
| 1141 |
{ |
| 1142 |
class => 'Koha::Account::Lines', |
| 1143 |
value => { |
| 1144 |
register_id => $register6->id, |
| 1145 |
borrowernumber => $patron->id, |
| 1146 |
amount => -1.00, |
| 1147 |
credit_type_code => 'PAYMENT', |
| 1148 |
payment_type => 'CASH', |
| 1149 |
} |
| 1150 |
} |
| 1151 |
); |
| 1152 |
} |
| 1153 |
|
| 1154 |
# Create many quick cashups |
| 1155 |
for my $i ( 1 .. 10 ) { |
| 1156 |
my $cashup = $register6->add_cashup( { manager_id => $manager->id, amount => '1.00' } ); |
| 1157 |
my $timestamp = "NOW() - INTERVAL $i MINUTE"; |
| 1158 |
$cashup->timestamp( \$timestamp )->store(); |
| 1159 |
} |
| 1160 |
|
| 1161 |
# Add cash for two-phase cashup |
| 1162 |
$builder->build_object( |
| 1163 |
{ |
| 1164 |
class => 'Koha::Account::Lines', |
| 1165 |
value => { |
| 1166 |
register_id => $register6->id, |
| 1167 |
borrowernumber => $patron->id, |
| 1168 |
amount => -2.00, |
| 1169 |
credit_type_code => 'PAYMENT', |
| 1170 |
payment_type => 'CASH', |
| 1171 |
} |
| 1172 |
} |
| 1173 |
); |
| 1174 |
|
| 1175 |
# Start a two-phase cashup |
| 1176 |
my $start = $register6->start_cashup( { manager_id => $manager->id } ); |
| 1177 |
|
| 1178 |
my $in_progress = $register6->cashup_in_progress; |
| 1179 |
is( ref($in_progress), 'Koha::Cash::Register::Action', 'Performs correctly with many previous cashups' ); |
| 1180 |
}; |
| 1181 |
|
| 1182 |
$schema->storage->txn_rollback; |
| 1183 |
}; |
| 1184 |
|
| 1185 |
subtest 'start_cashup_parameter_validation' => sub { |
| 1186 |
plan tests => 5; |
| 1187 |
|
| 1188 |
$schema->storage->txn_begin; |
| 1189 |
|
| 1190 |
my $register = $builder->build_object( { class => 'Koha::Cash::Registers' } ); |
| 1191 |
my $manager = $builder->build_object( { class => 'Koha::Patrons' } ); |
| 1192 |
|
| 1193 |
# Test 1: Valid parameters |
| 1194 |
subtest 'valid_parameters' => sub { |
| 1195 |
plan tests => 3; |
| 1196 |
|
| 1197 |
my $register1 = $builder->build_object( { class => 'Koha::Cash::Registers' } ); |
| 1198 |
my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
| 1199 |
|
| 1200 |
# Add cash transaction before starting cashup |
| 1201 |
$builder->build_object( |
| 1202 |
{ |
| 1203 |
class => 'Koha::Account::Lines', |
| 1204 |
value => { |
| 1205 |
register_id => $register1->id, |
| 1206 |
borrowernumber => $patron->id, |
| 1207 |
amount => -5.00, |
| 1208 |
credit_type_code => 'PAYMENT', |
| 1209 |
payment_type => 'CASH', |
| 1210 |
} |
| 1211 |
} |
| 1212 |
); |
| 1213 |
|
| 1214 |
my $cashup_start = $register1->start_cashup( { manager_id => $manager->id } ); |
| 1215 |
|
| 1216 |
is( ref($cashup_start), 'Koha::Cash::Register::Cashup', 'start_cashup returns correct object type' ); |
| 1217 |
is( $cashup_start->manager_id, $manager->id, 'manager_id set correctly' ); |
| 1218 |
is( $cashup_start->code, 'CASHUP_START', 'code set correctly to CASHUP_START' ); |
| 1219 |
}; |
| 1220 |
|
| 1221 |
# Test 2: Missing manager_id |
| 1222 |
subtest 'missing_manager_id' => sub { |
| 1223 |
plan tests => 1; |
| 1224 |
|
| 1225 |
my $register2 = $builder->build_object( { class => 'Koha::Cash::Registers' } ); |
| 1226 |
|
| 1227 |
eval { $register2->start_cashup( {} ); }; |
| 1228 |
ok( $@, 'start_cashup fails when manager_id is missing' ); |
| 1229 |
}; |
| 1230 |
|
| 1231 |
# Test 3: Invalid manager_id |
| 1232 |
subtest 'invalid_manager_id' => sub { |
| 1233 |
plan tests => 1; |
| 1234 |
|
| 1235 |
my $register3 = $builder->build_object( { class => 'Koha::Cash::Registers' } ); |
| 1236 |
my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
| 1237 |
|
| 1238 |
# Add cash transaction before starting cashup |
| 1239 |
$builder->build_object( |
| 1240 |
{ |
| 1241 |
class => 'Koha::Account::Lines', |
| 1242 |
value => { |
| 1243 |
register_id => $register3->id, |
| 1244 |
borrowernumber => $patron->id, |
| 1245 |
amount => -5.00, |
| 1246 |
credit_type_code => 'PAYMENT', |
| 1247 |
payment_type => 'CASH', |
| 1248 |
} |
| 1249 |
} |
| 1250 |
); |
| 1251 |
|
| 1252 |
eval { $register3->start_cashup( { manager_id => 99999999 } ); }; |
| 1253 |
ok( $@, 'start_cashup fails with invalid manager_id' ); |
| 1254 |
}; |
| 1255 |
|
| 1256 |
# Test 4: Duplicate start_cashup prevention |
| 1257 |
subtest 'duplicate_prevention' => sub { |
| 1258 |
plan tests => 2; |
| 1259 |
|
| 1260 |
my $register4 = $builder->build_object( { class => 'Koha::Cash::Registers' } ); |
| 1261 |
my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
| 1262 |
|
| 1263 |
# Add cash transaction before starting cashup |
| 1264 |
$builder->build_object( |
| 1265 |
{ |
| 1266 |
class => 'Koha::Account::Lines', |
| 1267 |
value => { |
| 1268 |
register_id => $register4->id, |
| 1269 |
borrowernumber => $patron->id, |
| 1270 |
amount => -5.00, |
| 1271 |
credit_type_code => 'PAYMENT', |
| 1272 |
payment_type => 'CASH', |
| 1273 |
} |
| 1274 |
} |
| 1275 |
); |
| 1276 |
|
| 1277 |
# First start should succeed |
| 1278 |
my $first_start = $register4->start_cashup( { manager_id => $manager->id } ); |
| 1279 |
ok( $first_start, 'First start_cashup succeeds' ); |
| 1280 |
|
| 1281 |
# Second start should fail |
| 1282 |
throws_ok { |
| 1283 |
$register4->start_cashup( { manager_id => $manager->id } ); |
| 1284 |
} |
| 1285 |
'Koha::Exceptions::Object::DuplicateID', |
| 1286 |
'Second start_cashup throws DuplicateID exception'; |
| 1287 |
}; |
| 1288 |
|
| 1289 |
# Test 5: Database transaction integrity |
| 1290 |
subtest 'transaction_integrity' => sub { |
| 1291 |
plan tests => 3; |
| 1292 |
|
| 1293 |
my $register5 = $builder->build_object( { class => 'Koha::Cash::Registers' } ); |
| 1294 |
|
| 1295 |
# Add some transactions to establish expected amount |
| 1296 |
my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
| 1297 |
my $account = $patron->account; |
| 1298 |
|
| 1299 |
my $fine = $account->add_debit( |
| 1300 |
{ |
| 1301 |
amount => '15.00', |
| 1302 |
type => 'OVERDUE', |
| 1303 |
interface => 'cron' |
| 1304 |
} |
| 1305 |
); |
| 1306 |
|
| 1307 |
my $payment = $account->pay( |
| 1308 |
{ |
| 1309 |
cash_register => $register5->id, |
| 1310 |
amount => '15.00', |
| 1311 |
credit_type => 'PAYMENT', |
| 1312 |
payment_type => 'CASH', |
| 1313 |
lines => [$fine] |
| 1314 |
} |
| 1315 |
); |
| 1316 |
|
| 1317 |
my $initial_action_count = $register5->_result->search_related('cash_register_actions')->count; |
| 1318 |
|
| 1319 |
my $start = $register5->start_cashup( { manager_id => $manager->id } ); |
| 1320 |
|
| 1321 |
# Verify action was created |
| 1322 |
my $final_action_count = $register5->_result->search_related('cash_register_actions')->count; |
| 1323 |
is( $final_action_count, $initial_action_count + 1, 'CASHUP_START action created in database' ); |
| 1324 |
|
| 1325 |
# Verify expected amount calculation |
| 1326 |
ok( $start->amount >= 0, 'Expected amount calculated correctly' ); |
| 1327 |
|
| 1328 |
# Verify timestamp is set |
| 1329 |
ok( defined $start->timestamp, 'Timestamp is set on CASHUP_START action' ); |
| 1330 |
}; |
| 1331 |
|
| 1332 |
$schema->storage->txn_rollback; |
| 1333 |
}; |
| 1334 |
|
| 1335 |
subtest 'add_cashup' => sub { |
| 1336 |
plan tests => 5; |
| 1337 |
|
| 1338 |
$schema->storage->txn_begin; |
| 1339 |
|
| 1340 |
my $register = $builder->build_object( { class => 'Koha::Cash::Registers' } ); |
| 1341 |
my $manager = $builder->build_object( { class => 'Koha::Patrons' } ); |
| 1342 |
|
| 1343 |
# Test 1: Valid parameters |
| 1344 |
subtest 'valid_parameters' => sub { |
| 1345 |
plan tests => 3; |
| 1346 |
|
| 1347 |
my $register1 = $builder->build_object( { class => 'Koha::Cash::Registers' } ); |
| 1348 |
|
| 1349 |
my $cashup = $register1->add_cashup( { manager_id => $manager->id, amount => '10.00' } ); |
| 1350 |
|
| 1351 |
is( ref($cashup), 'Koha::Cash::Register::Cashup', 'add_cashup returns correct object type' ); |
| 1352 |
is( $cashup->manager_id, $manager->id, 'manager_id set correctly' ); |
| 1353 |
is( $cashup->amount + 0, 10, 'amount set correctly' ); |
| 1354 |
}; |
| 1355 |
|
| 1356 |
# Test 2: Missing required parameters |
| 1357 |
subtest 'missing_parameters' => sub { |
| 1358 |
plan tests => 3; |
| 1359 |
|
| 1360 |
my $register2 = $builder->build_object( { class => 'Koha::Cash::Registers' } ); |
| 1361 |
|
| 1362 |
# Missing manager_id |
| 1363 |
eval { $register2->add_cashup( { amount => '10.00' } ); }; |
| 1364 |
ok( $@, 'add_cashup fails when manager_id is missing' ); |
| 1365 |
|
| 1366 |
# Missing amount |
| 1367 |
eval { $register2->add_cashup( { manager_id => $manager->id } ); }; |
| 1368 |
ok( $@, 'add_cashup fails when amount is missing' ); |
| 1369 |
|
| 1370 |
# Missing both |
| 1371 |
eval { $register2->add_cashup( {} ); }; |
| 1372 |
ok( $@, 'add_cashup fails when both parameters are missing' ); |
| 1373 |
}; |
| 1374 |
|
| 1375 |
# Test 3: Invalid amount parameter |
| 1376 |
subtest 'invalid_amount' => sub { |
| 1377 |
plan tests => 4; |
| 1378 |
|
| 1379 |
my $register3 = $builder->build_object( { class => 'Koha::Cash::Registers' } ); |
| 1380 |
|
| 1381 |
# Zero amount |
| 1382 |
throws_ok { |
| 1383 |
$register3->add_cashup( { manager_id => $manager->id, amount => '0.00' } ); |
| 1384 |
} |
| 1385 |
'Koha::Exceptions::Account::AmountNotPositive', |
| 1386 |
'Zero amount throws AmountNotPositive exception'; |
| 1387 |
|
| 1388 |
# Negative amount |
| 1389 |
throws_ok { |
| 1390 |
$register3->add_cashup( { manager_id => $manager->id, amount => '-5.00' } ); |
| 1391 |
} |
| 1392 |
'Koha::Exceptions::Account::AmountNotPositive', |
| 1393 |
'Negative amount throws AmountNotPositive exception'; |
| 1394 |
|
| 1395 |
# Non-numeric amount |
| 1396 |
throws_ok { |
| 1397 |
$register3->add_cashup( { manager_id => $manager->id, amount => 'invalid' } ); |
| 1398 |
} |
| 1399 |
'Koha::Exceptions::Account::AmountNotPositive', |
| 1400 |
'Non-numeric amount throws AmountNotPositive exception'; |
| 1401 |
|
| 1402 |
# Empty string amount |
| 1403 |
throws_ok { |
| 1404 |
$register3->add_cashup( { manager_id => $manager->id, amount => '' } ); |
| 1405 |
} |
| 1406 |
'Koha::Exceptions::Account::AmountNotPositive', |
| 1407 |
'Empty string amount throws AmountNotPositive exception'; |
| 1408 |
}; |
| 1409 |
|
| 1410 |
# Test 4: Reconciliation note handling |
| 1411 |
subtest 'reconciliation_note_handling' => sub { |
| 1412 |
plan tests => 4; |
| 1413 |
|
| 1414 |
my $register4 = $builder->build_object( { class => 'Koha::Cash::Registers' } ); |
| 1415 |
my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
| 1416 |
my $account = $patron->account; |
| 1417 |
|
| 1418 |
# Create transaction to enable surplus creation |
| 1419 |
my $fine = $account->add_debit( |
| 1420 |
{ |
| 1421 |
amount => '10.00', |
| 1422 |
type => 'OVERDUE', |
| 1423 |
interface => 'cron' |
| 1424 |
} |
| 1425 |
); |
| 1426 |
|
| 1427 |
my $payment = $account->pay( |
| 1428 |
{ |
| 1429 |
cash_register => $register4->id, |
| 1430 |
amount => '10.00', |
| 1431 |
credit_type => 'PAYMENT', |
| 1432 |
payment_type => 'CASH', |
| 1433 |
lines => [$fine] |
| 1434 |
} |
| 1435 |
); |
| 1436 |
|
| 1437 |
# Test normal note |
| 1438 |
my $cashup1 = $register4->add_cashup( |
| 1439 |
{ |
| 1440 |
manager_id => $manager->id, |
| 1441 |
amount => '15.00', # Creates surplus |
| 1442 |
reconciliation_note => 'Found extra money in drawer' |
| 1443 |
} |
| 1444 |
); |
| 1445 |
|
| 1446 |
my $surplus1 = Koha::Account::Lines->search( |
| 1447 |
{ |
| 1448 |
register_id => $register4->id, |
| 1449 |
credit_type_code => 'CASHUP_SURPLUS' |
| 1450 |
} |
| 1451 |
)->next; |
| 1452 |
is( $surplus1->note, 'Found extra money in drawer', 'Normal reconciliation note stored correctly' ); |
| 1453 |
|
| 1454 |
# Test very long note (should be truncated) |
| 1455 |
my $register5 = $builder->build_object( { class => 'Koha::Cash::Registers' } ); |
| 1456 |
my $long_note = 'x' x 1500; # Longer than 1000 character limit |
| 1457 |
|
| 1458 |
my $fine2 = $account->add_debit( |
| 1459 |
{ |
| 1460 |
amount => '10.00', |
| 1461 |
type => 'OVERDUE', |
| 1462 |
interface => 'cron' |
| 1463 |
} |
| 1464 |
); |
| 1465 |
|
| 1466 |
my $payment2 = $account->pay( |
| 1467 |
{ |
| 1468 |
cash_register => $register5->id, |
| 1469 |
amount => '10.00', |
| 1470 |
credit_type => 'PAYMENT', |
| 1471 |
payment_type => 'CASH', |
| 1472 |
lines => [$fine2] |
| 1473 |
} |
| 1474 |
); |
| 1475 |
|
| 1476 |
my $cashup2 = $register5->add_cashup( |
| 1477 |
{ |
| 1478 |
manager_id => $manager->id, |
| 1479 |
amount => '15.00', |
| 1480 |
reconciliation_note => $long_note |
| 1481 |
} |
| 1482 |
); |
| 1483 |
|
| 1484 |
my $surplus2 = Koha::Account::Lines->search( |
| 1485 |
{ |
| 1486 |
register_id => $register5->id, |
| 1487 |
credit_type_code => 'CASHUP_SURPLUS' |
| 1488 |
} |
| 1489 |
)->next; |
| 1490 |
is( length( $surplus2->note ), 1000, 'Long reconciliation note truncated to 1000 characters' ); |
| 1491 |
|
| 1492 |
# Test whitespace-only note (should be undef) |
| 1493 |
my $register6 = $builder->build_object( { class => 'Koha::Cash::Registers' } ); |
| 1494 |
|
| 1495 |
my $fine3 = $account->add_debit( |
| 1496 |
{ |
| 1497 |
amount => '10.00', |
| 1498 |
type => 'OVERDUE', |
| 1499 |
interface => 'cron' |
| 1500 |
} |
| 1501 |
); |
| 1502 |
|
| 1503 |
my $payment3 = $account->pay( |
| 1504 |
{ |
| 1505 |
cash_register => $register6->id, |
| 1506 |
amount => '10.00', |
| 1507 |
credit_type => 'PAYMENT', |
| 1508 |
payment_type => 'CASH', |
| 1509 |
lines => [$fine3] |
| 1510 |
} |
| 1511 |
); |
| 1512 |
|
| 1513 |
my $cashup3 = $register6->add_cashup( |
| 1514 |
{ |
| 1515 |
manager_id => $manager->id, |
| 1516 |
amount => '15.00', |
| 1517 |
reconciliation_note => ' ' # Whitespace only |
| 1518 |
} |
| 1519 |
); |
| 1520 |
|
| 1521 |
my $surplus3 = Koha::Account::Lines->search( |
| 1522 |
{ |
| 1523 |
register_id => $register6->id, |
| 1524 |
credit_type_code => 'CASHUP_SURPLUS' |
| 1525 |
} |
| 1526 |
)->next; |
| 1527 |
is( $surplus3->note, undef, 'Whitespace-only reconciliation note stored as undef' ); |
| 1528 |
|
| 1529 |
# Test empty string note (should be undef) |
| 1530 |
my $register7 = $builder->build_object( { class => 'Koha::Cash::Registers' } ); |
| 1531 |
|
| 1532 |
my $fine4 = $account->add_debit( |
| 1533 |
{ |
| 1534 |
amount => '10.00', |
| 1535 |
type => 'OVERDUE', |
| 1536 |
interface => 'cron' |
| 1537 |
} |
| 1538 |
); |
| 1539 |
|
| 1540 |
my $payment4 = $account->pay( |
| 1541 |
{ |
| 1542 |
cash_register => $register7->id, |
| 1543 |
amount => '10.00', |
| 1544 |
credit_type => 'PAYMENT', |
| 1545 |
payment_type => 'CASH', |
| 1546 |
lines => [$fine4] |
| 1547 |
} |
| 1548 |
); |
| 1549 |
|
| 1550 |
my $cashup4 = $register7->add_cashup( |
| 1551 |
{ |
| 1552 |
manager_id => $manager->id, |
| 1553 |
amount => '15.00', |
| 1554 |
reconciliation_note => '' # Empty string |
| 1555 |
} |
| 1556 |
); |
| 1557 |
|
| 1558 |
my $surplus4 = Koha::Account::Lines->search( |
| 1559 |
{ |
| 1560 |
register_id => $register7->id, |
| 1561 |
credit_type_code => 'CASHUP_SURPLUS' |
| 1562 |
} |
| 1563 |
)->next; |
| 1564 |
is( $surplus4->note, undef, 'Empty string reconciliation note stored as undef' ); |
| 1565 |
}; |
| 1566 |
|
| 1567 |
# Test 5: Invalid manager_id |
| 1568 |
subtest 'invalid_manager_id' => sub { |
| 1569 |
plan tests => 1; |
| 1570 |
|
| 1571 |
my $register9 = $builder->build_object( { class => 'Koha::Cash::Registers' } ); |
| 1572 |
|
| 1573 |
eval { $register9->add_cashup( { manager_id => 99999999, amount => '10.00' } ); }; |
| 1574 |
ok( $@, 'add_cashup fails with invalid manager_id' ); |
| 1575 |
diag($@); |
| 1576 |
}; |
| 1577 |
|
| 1578 |
$schema->storage->txn_rollback; |
| 1579 |
}; |