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 |
|
910 |
# Test 1: No cashups ever performed |
911 |
subtest 'no_cashups_ever' => sub { |
912 |
plan tests => 1; |
913 |
|
914 |
my $in_progress = $register->cashup_in_progress; |
915 |
is( $in_progress, undef, 'cashup_in_progress returns undef when no cashups have ever been performed' ); |
916 |
}; |
917 |
|
918 |
# Test 2: Only quick cashups performed |
919 |
subtest 'only_quick_cashups' => sub { |
920 |
plan tests => 2; |
921 |
|
922 |
# Add a quick cashup |
923 |
my $quick_cashup = $register->add_cashup( { manager_id => $manager->id, amount => '10.00' } ); |
924 |
$quick_cashup->timestamp( \'NOW() - INTERVAL 30 MINUTE' )->store(); |
925 |
|
926 |
my $in_progress = $register->cashup_in_progress; |
927 |
is( $in_progress, undef, 'cashup_in_progress returns undef after quick cashup completion' ); |
928 |
|
929 |
# Add another quick cashup |
930 |
my $quick_cashup2 = $register->add_cashup( { manager_id => $manager->id, amount => '5.00' } ); |
931 |
|
932 |
$in_progress = $register->cashup_in_progress; |
933 |
is( $in_progress, undef, 'cashup_in_progress returns undef after multiple quick cashups' ); |
934 |
}; |
935 |
|
936 |
# Test 3: Multiple CASHUP_START actions |
937 |
subtest 'multiple_start_actions' => sub { |
938 |
plan tests => 2; |
939 |
|
940 |
my $register2 = $builder->build_object( { class => 'Koha::Cash::Registers' } ); |
941 |
|
942 |
# Create multiple CASHUP_START actions |
943 |
my $start1 = $register2->start_cashup( { manager_id => $manager->id } ); |
944 |
$start1->timestamp( \'NOW() - INTERVAL 60 MINUTE' )->store(); |
945 |
|
946 |
# Complete the first one |
947 |
my $complete1 = $register2->add_cashup( { manager_id => $manager->id, amount => '1.00' } ); |
948 |
$complete1->timestamp( \'NOW() - INTERVAL 50 MINUTE' )->store(); |
949 |
|
950 |
# Start another one |
951 |
my $start2 = $register2->start_cashup( { manager_id => $manager->id } ); |
952 |
|
953 |
my $in_progress = $register2->cashup_in_progress; |
954 |
is( ref($in_progress), 'Koha::Cash::Register::Action', 'Returns most recent CASHUP_START when multiple exist' ); |
955 |
is( $in_progress->id, $start2->id, 'Returns the correct (most recent) CASHUP_START action' ); |
956 |
}; |
957 |
|
958 |
# Test 4: Mixed quick and two-phase workflows |
959 |
subtest 'mixed_workflows' => sub { |
960 |
plan tests => 3; |
961 |
|
962 |
my $register3 = $builder->build_object( { class => 'Koha::Cash::Registers' } ); |
963 |
|
964 |
# Quick cashup first |
965 |
my $quick = $register3->add_cashup( { manager_id => $manager->id, amount => '5.00' } ); |
966 |
$quick->timestamp( \'NOW() - INTERVAL 40 MINUTE' )->store(); |
967 |
|
968 |
# Start two-phase |
969 |
my $start = $register3->start_cashup( { manager_id => $manager->id } ); |
970 |
$start->timestamp( \'NOW() - INTERVAL 30 MINUTE' )->store(); |
971 |
|
972 |
my $in_progress = $register3->cashup_in_progress; |
973 |
is( ref($in_progress), 'Koha::Cash::Register::Action', 'Detects two-phase in progress after quick cashup' ); |
974 |
is( $in_progress->id, $start->id, 'Returns correct CASHUP_START after mixed workflow' ); |
975 |
|
976 |
# Complete two-phase |
977 |
my $complete = $register3->add_cashup( { manager_id => $manager->id, amount => '3.00' } ); |
978 |
|
979 |
$in_progress = $register3->cashup_in_progress; |
980 |
is( $in_progress, undef, 'Returns undef after completing two-phase in mixed workflow' ); |
981 |
}; |
982 |
|
983 |
# Test 5: Timestamp edge cases |
984 |
subtest 'timestamp_edge_cases' => sub { |
985 |
plan tests => 2; |
986 |
|
987 |
my $register4 = $builder->build_object( { class => 'Koha::Cash::Registers' } ); |
988 |
|
989 |
# Create CASHUP_START |
990 |
my $start = $register4->start_cashup( { manager_id => $manager->id } ); |
991 |
my $start_time = $start->timestamp; |
992 |
|
993 |
# Create CASHUP with exactly the same timestamp (edge case) |
994 |
my $complete = $register4->add_cashup( { manager_id => $manager->id, amount => '1.00' } ); |
995 |
$complete->timestamp($start_time)->store(); |
996 |
|
997 |
my $in_progress = $register4->cashup_in_progress; |
998 |
is( $in_progress, undef, 'Handles same timestamp edge case correctly' ); |
999 |
|
1000 |
# Test with CASHUP timestamp slightly before CASHUP_START (edge case) |
1001 |
my $register5 = $builder->build_object( { class => 'Koha::Cash::Registers' } ); |
1002 |
my $start2 = $register5->start_cashup( { manager_id => $manager->id } ); |
1003 |
|
1004 |
my $complete2 = $register5->add_cashup( { manager_id => $manager->id, amount => '1.00' } ); |
1005 |
$complete2->timestamp( \'NOW() - INTERVAL 1 MINUTE' )->store(); |
1006 |
|
1007 |
$in_progress = $register5->cashup_in_progress; |
1008 |
is( |
1009 |
ref($in_progress), 'Koha::Cash::Register::Action', |
1010 |
'Correctly identifies active cashup when completion is backdated' |
1011 |
); |
1012 |
}; |
1013 |
|
1014 |
# Test 6: Performance with many cashups |
1015 |
subtest 'performance_with_many_cashups' => sub { |
1016 |
plan tests => 1; |
1017 |
|
1018 |
my $register6 = $builder->build_object( { class => 'Koha::Cash::Registers' } ); |
1019 |
|
1020 |
# Create many quick cashups |
1021 |
for my $i ( 1 .. 10 ) { |
1022 |
my $cashup = $register6->add_cashup( { manager_id => $manager->id, amount => '1.00' } ); |
1023 |
my $timestamp = "NOW() - INTERVAL $i MINUTE"; |
1024 |
$cashup->timestamp( \$timestamp )->store(); |
1025 |
} |
1026 |
|
1027 |
# Start a two-phase cashup |
1028 |
my $start = $register6->start_cashup( { manager_id => $manager->id } ); |
1029 |
|
1030 |
my $in_progress = $register6->cashup_in_progress; |
1031 |
is( ref($in_progress), 'Koha::Cash::Register::Action', 'Performs correctly with many previous cashups' ); |
1032 |
}; |
1033 |
|
1034 |
$schema->storage->txn_rollback; |
1035 |
}; |
1036 |
|
1037 |
subtest 'start_cashup_parameter_validation' => sub { |
1038 |
plan tests => 5; |
1039 |
|
1040 |
$schema->storage->txn_begin; |
1041 |
|
1042 |
my $register = $builder->build_object( { class => 'Koha::Cash::Registers' } ); |
1043 |
my $manager = $builder->build_object( { class => 'Koha::Patrons' } ); |
1044 |
|
1045 |
# Test 1: Valid parameters |
1046 |
subtest 'valid_parameters' => sub { |
1047 |
plan tests => 3; |
1048 |
|
1049 |
my $register1 = $builder->build_object( { class => 'Koha::Cash::Registers' } ); |
1050 |
|
1051 |
my $cashup_start = $register1->start_cashup( { manager_id => $manager->id } ); |
1052 |
|
1053 |
is( ref($cashup_start), 'Koha::Cash::Register::Cashup', 'start_cashup returns correct object type' ); |
1054 |
is( $cashup_start->manager_id, $manager->id, 'manager_id set correctly' ); |
1055 |
is( $cashup_start->code, 'CASHUP_START', 'code set correctly to CASHUP_START' ); |
1056 |
}; |
1057 |
|
1058 |
# Test 2: Missing manager_id |
1059 |
subtest 'missing_manager_id' => sub { |
1060 |
plan tests => 1; |
1061 |
|
1062 |
my $register2 = $builder->build_object( { class => 'Koha::Cash::Registers' } ); |
1063 |
|
1064 |
eval { $register2->start_cashup( {} ); }; |
1065 |
ok( $@, 'start_cashup fails when manager_id is missing' ); |
1066 |
}; |
1067 |
|
1068 |
# Test 3: Invalid manager_id |
1069 |
subtest 'invalid_manager_id' => sub { |
1070 |
plan tests => 1; |
1071 |
|
1072 |
my $register3 = $builder->build_object( { class => 'Koha::Cash::Registers' } ); |
1073 |
|
1074 |
eval { $register3->start_cashup( { manager_id => 99999999 } ); }; |
1075 |
ok( $@, 'start_cashup fails with invalid manager_id' ); |
1076 |
}; |
1077 |
|
1078 |
# Test 4: Duplicate start_cashup prevention |
1079 |
subtest 'duplicate_prevention' => sub { |
1080 |
plan tests => 2; |
1081 |
|
1082 |
my $register4 = $builder->build_object( { class => 'Koha::Cash::Registers' } ); |
1083 |
|
1084 |
# First start should succeed |
1085 |
my $first_start = $register4->start_cashup( { manager_id => $manager->id } ); |
1086 |
ok( $first_start, 'First start_cashup succeeds' ); |
1087 |
|
1088 |
# Second start should fail |
1089 |
throws_ok { |
1090 |
$register4->start_cashup( { manager_id => $manager->id } ); |
1091 |
} |
1092 |
'Koha::Exceptions::Object::DuplicateID', |
1093 |
'Second start_cashup throws DuplicateID exception'; |
1094 |
}; |
1095 |
|
1096 |
# Test 5: Database transaction integrity |
1097 |
subtest 'transaction_integrity' => sub { |
1098 |
plan tests => 3; |
1099 |
|
1100 |
my $register5 = $builder->build_object( { class => 'Koha::Cash::Registers' } ); |
1101 |
|
1102 |
# Add some transactions to establish expected amount |
1103 |
my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
1104 |
my $account = $patron->account; |
1105 |
|
1106 |
my $fine = $account->add_debit( |
1107 |
{ |
1108 |
amount => '15.00', |
1109 |
type => 'OVERDUE', |
1110 |
interface => 'cron' |
1111 |
} |
1112 |
); |
1113 |
|
1114 |
my $payment = $account->pay( |
1115 |
{ |
1116 |
cash_register => $register5->id, |
1117 |
amount => '15.00', |
1118 |
credit_type => 'PAYMENT', |
1119 |
payment_type => 'CASH', |
1120 |
lines => [$fine] |
1121 |
} |
1122 |
); |
1123 |
|
1124 |
my $initial_action_count = $register5->_result->search_related('cash_register_actions')->count; |
1125 |
|
1126 |
my $start = $register5->start_cashup( { manager_id => $manager->id } ); |
1127 |
|
1128 |
# Verify action was created |
1129 |
my $final_action_count = $register5->_result->search_related('cash_register_actions')->count; |
1130 |
is( $final_action_count, $initial_action_count + 1, 'CASHUP_START action created in database' ); |
1131 |
|
1132 |
# Verify expected amount calculation |
1133 |
ok( $start->amount >= 0, 'Expected amount calculated correctly' ); |
1134 |
|
1135 |
# Verify timestamp is set |
1136 |
ok( defined $start->timestamp, 'Timestamp is set on CASHUP_START action' ); |
1137 |
}; |
1138 |
|
1139 |
$schema->storage->txn_rollback; |
1140 |
}; |
1141 |
|
1142 |
subtest 'add_cashup' => sub { |
1143 |
plan tests => 5; |
1144 |
|
1145 |
$schema->storage->txn_begin; |
1146 |
|
1147 |
my $register = $builder->build_object( { class => 'Koha::Cash::Registers' } ); |
1148 |
my $manager = $builder->build_object( { class => 'Koha::Patrons' } ); |
1149 |
|
1150 |
# Test 1: Valid parameters |
1151 |
subtest 'valid_parameters' => sub { |
1152 |
plan tests => 3; |
1153 |
|
1154 |
my $register1 = $builder->build_object( { class => 'Koha::Cash::Registers' } ); |
1155 |
|
1156 |
my $cashup = $register1->add_cashup( { manager_id => $manager->id, amount => '10.00' } ); |
1157 |
|
1158 |
is( ref($cashup), 'Koha::Cash::Register::Cashup', 'add_cashup returns correct object type' ); |
1159 |
is( $cashup->manager_id, $manager->id, 'manager_id set correctly' ); |
1160 |
is( $cashup->amount, '10.000000', 'amount set correctly' ); |
1161 |
}; |
1162 |
|
1163 |
# Test 2: Missing required parameters |
1164 |
subtest 'missing_parameters' => sub { |
1165 |
plan tests => 3; |
1166 |
|
1167 |
my $register2 = $builder->build_object( { class => 'Koha::Cash::Registers' } ); |
1168 |
|
1169 |
# Missing manager_id |
1170 |
eval { $register2->add_cashup( { amount => '10.00' } ); }; |
1171 |
ok( $@, 'add_cashup fails when manager_id is missing' ); |
1172 |
|
1173 |
# Missing amount |
1174 |
eval { $register2->add_cashup( { manager_id => $manager->id } ); }; |
1175 |
ok( $@, 'add_cashup fails when amount is missing' ); |
1176 |
|
1177 |
# Missing both |
1178 |
eval { $register2->add_cashup( {} ); }; |
1179 |
ok( $@, 'add_cashup fails when both parameters are missing' ); |
1180 |
}; |
1181 |
|
1182 |
# Test 3: Invalid amount parameter |
1183 |
subtest 'invalid_amount' => sub { |
1184 |
plan tests => 4; |
1185 |
|
1186 |
my $register3 = $builder->build_object( { class => 'Koha::Cash::Registers' } ); |
1187 |
|
1188 |
# Zero amount |
1189 |
throws_ok { |
1190 |
$register3->add_cashup( { manager_id => $manager->id, amount => '0.00' } ); |
1191 |
} |
1192 |
'Koha::Exceptions::Account::AmountNotPositive', |
1193 |
'Zero amount throws AmountNotPositive exception'; |
1194 |
|
1195 |
# Negative amount |
1196 |
throws_ok { |
1197 |
$register3->add_cashup( { manager_id => $manager->id, amount => '-5.00' } ); |
1198 |
} |
1199 |
'Koha::Exceptions::Account::AmountNotPositive', |
1200 |
'Negative amount throws AmountNotPositive exception'; |
1201 |
|
1202 |
# Non-numeric amount |
1203 |
throws_ok { |
1204 |
$register3->add_cashup( { manager_id => $manager->id, amount => 'invalid' } ); |
1205 |
} |
1206 |
'Koha::Exceptions::Account::AmountNotPositive', |
1207 |
'Non-numeric amount throws AmountNotPositive exception'; |
1208 |
|
1209 |
# Empty string amount |
1210 |
throws_ok { |
1211 |
$register3->add_cashup( { manager_id => $manager->id, amount => '' } ); |
1212 |
} |
1213 |
'Koha::Exceptions::Account::AmountNotPositive', |
1214 |
'Empty string amount throws AmountNotPositive exception'; |
1215 |
}; |
1216 |
|
1217 |
# Test 4: Reconciliation note handling |
1218 |
subtest 'reconciliation_note_handling' => sub { |
1219 |
plan tests => 4; |
1220 |
|
1221 |
my $register4 = $builder->build_object( { class => 'Koha::Cash::Registers' } ); |
1222 |
my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
1223 |
my $account = $patron->account; |
1224 |
|
1225 |
# Create transaction to enable surplus creation |
1226 |
my $fine = $account->add_debit( |
1227 |
{ |
1228 |
amount => '10.00', |
1229 |
type => 'OVERDUE', |
1230 |
interface => 'cron' |
1231 |
} |
1232 |
); |
1233 |
|
1234 |
my $payment = $account->pay( |
1235 |
{ |
1236 |
cash_register => $register4->id, |
1237 |
amount => '10.00', |
1238 |
credit_type => 'PAYMENT', |
1239 |
payment_type => 'CASH', |
1240 |
lines => [$fine] |
1241 |
} |
1242 |
); |
1243 |
|
1244 |
# Test normal note |
1245 |
my $cashup1 = $register4->add_cashup( |
1246 |
{ |
1247 |
manager_id => $manager->id, |
1248 |
amount => '15.00', # Creates surplus |
1249 |
reconciliation_note => 'Found extra money in drawer' |
1250 |
} |
1251 |
); |
1252 |
|
1253 |
my $surplus1 = Koha::Account::Lines->search( |
1254 |
{ |
1255 |
register_id => $register4->id, |
1256 |
credit_type_code => 'CASHUP_SURPLUS' |
1257 |
} |
1258 |
)->next; |
1259 |
is( $surplus1->note, 'Found extra money in drawer', 'Normal reconciliation note stored correctly' ); |
1260 |
|
1261 |
# Test very long note (should be truncated) |
1262 |
my $register5 = $builder->build_object( { class => 'Koha::Cash::Registers' } ); |
1263 |
my $long_note = 'x' x 1500; # Longer than 1000 character limit |
1264 |
|
1265 |
my $fine2 = $account->add_debit( |
1266 |
{ |
1267 |
amount => '10.00', |
1268 |
type => 'OVERDUE', |
1269 |
interface => 'cron' |
1270 |
} |
1271 |
); |
1272 |
|
1273 |
my $payment2 = $account->pay( |
1274 |
{ |
1275 |
cash_register => $register5->id, |
1276 |
amount => '10.00', |
1277 |
credit_type => 'PAYMENT', |
1278 |
payment_type => 'CASH', |
1279 |
lines => [$fine2] |
1280 |
} |
1281 |
); |
1282 |
|
1283 |
my $cashup2 = $register5->add_cashup( |
1284 |
{ |
1285 |
manager_id => $manager->id, |
1286 |
amount => '15.00', |
1287 |
reconciliation_note => $long_note |
1288 |
} |
1289 |
); |
1290 |
|
1291 |
my $surplus2 = Koha::Account::Lines->search( |
1292 |
{ |
1293 |
register_id => $register5->id, |
1294 |
credit_type_code => 'CASHUP_SURPLUS' |
1295 |
} |
1296 |
)->next; |
1297 |
is( length( $surplus2->note ), 1000, 'Long reconciliation note truncated to 1000 characters' ); |
1298 |
|
1299 |
# Test whitespace-only note (should be undef) |
1300 |
my $register6 = $builder->build_object( { class => 'Koha::Cash::Registers' } ); |
1301 |
|
1302 |
my $fine3 = $account->add_debit( |
1303 |
{ |
1304 |
amount => '10.00', |
1305 |
type => 'OVERDUE', |
1306 |
interface => 'cron' |
1307 |
} |
1308 |
); |
1309 |
|
1310 |
my $payment3 = $account->pay( |
1311 |
{ |
1312 |
cash_register => $register6->id, |
1313 |
amount => '10.00', |
1314 |
credit_type => 'PAYMENT', |
1315 |
payment_type => 'CASH', |
1316 |
lines => [$fine3] |
1317 |
} |
1318 |
); |
1319 |
|
1320 |
my $cashup3 = $register6->add_cashup( |
1321 |
{ |
1322 |
manager_id => $manager->id, |
1323 |
amount => '15.00', |
1324 |
reconciliation_note => ' ' # Whitespace only |
1325 |
} |
1326 |
); |
1327 |
|
1328 |
my $surplus3 = Koha::Account::Lines->search( |
1329 |
{ |
1330 |
register_id => $register6->id, |
1331 |
credit_type_code => 'CASHUP_SURPLUS' |
1332 |
} |
1333 |
)->next; |
1334 |
is( $surplus3->note, undef, 'Whitespace-only reconciliation note stored as undef' ); |
1335 |
|
1336 |
# Test empty string note (should be undef) |
1337 |
my $register7 = $builder->build_object( { class => 'Koha::Cash::Registers' } ); |
1338 |
|
1339 |
my $fine4 = $account->add_debit( |
1340 |
{ |
1341 |
amount => '10.00', |
1342 |
type => 'OVERDUE', |
1343 |
interface => 'cron' |
1344 |
} |
1345 |
); |
1346 |
|
1347 |
my $payment4 = $account->pay( |
1348 |
{ |
1349 |
cash_register => $register7->id, |
1350 |
amount => '10.00', |
1351 |
credit_type => 'PAYMENT', |
1352 |
payment_type => 'CASH', |
1353 |
lines => [$fine4] |
1354 |
} |
1355 |
); |
1356 |
|
1357 |
my $cashup4 = $register7->add_cashup( |
1358 |
{ |
1359 |
manager_id => $manager->id, |
1360 |
amount => '15.00', |
1361 |
reconciliation_note => '' # Empty string |
1362 |
} |
1363 |
); |
1364 |
|
1365 |
my $surplus4 = Koha::Account::Lines->search( |
1366 |
{ |
1367 |
register_id => $register7->id, |
1368 |
credit_type_code => 'CASHUP_SURPLUS' |
1369 |
} |
1370 |
)->next; |
1371 |
is( $surplus4->note, undef, 'Empty string reconciliation note stored as undef' ); |
1372 |
}; |
1373 |
|
1374 |
# Test 5: Invalid manager_id |
1375 |
subtest 'invalid_manager_id' => sub { |
1376 |
plan tests => 1; |
1377 |
|
1378 |
my $register9 = $builder->build_object( { class => 'Koha::Cash::Registers' } ); |
1379 |
|
1380 |
eval { $register9->add_cashup( { manager_id => 99999999, amount => '10.00' } ); }; |
1381 |
ok( $@, 'add_cashup fails with invalid manager_id' ); |
1382 |
diag($@); |
1383 |
}; |
1384 |
|
1385 |
$schema->storage->txn_rollback; |
1386 |
}; |