View | Details | Raw Unified | Return to bug 22377
Collapse All | Expand All

(-)a/t/db_dependent/Circulation.t (-2 / +103 lines)
Lines 1983-1989 subtest 'AddReturn | is_overdue' => sub { Link Here
1983
1983
1984
subtest '_FixAccountForLostAndReturned' => sub {
1984
subtest '_FixAccountForLostAndReturned' => sub {
1985
1985
1986
    plan tests => 5;
1986
    plan tests => 6;
1987
1987
1988
    t::lib::Mocks::mock_preference( 'WhenLostChargeReplacementFee', 1 );
1988
    t::lib::Mocks::mock_preference( 'WhenLostChargeReplacementFee', 1 );
1989
    t::lib::Mocks::mock_preference( 'WhenLostForgiveFine',          0 );
1989
    t::lib::Mocks::mock_preference( 'WhenLostForgiveFine',          0 );
Lines 2367-2372 subtest '_FixAccountForLostAndReturned' => sub { Link Here
2367
        my $manual_debit = Koha::Account::Lines->search({ borrowernumber => $patron->id, accounttype => 'OVERDUE', status => 'UNRETURNED' })->next;
2367
        my $manual_debit = Koha::Account::Lines->search({ borrowernumber => $patron->id, accounttype => 'OVERDUE', status => 'UNRETURNED' })->next;
2368
        is( $manual_debit->amountoutstanding + 0, $manual_debit_amount - $payment_amount, 'reconcile_balance was called' );
2368
        is( $manual_debit->amountoutstanding + 0, $manual_debit_amount - $payment_amount, 'reconcile_balance was called' );
2369
    };
2369
    };
2370
    
2371
    subtest 'Test that refund is applied to the correct debt' => sub {
2372
2373
        plan tests => 13;
2374
2375
        my $patron1 = $builder->build_object( { class => 'Koha::Patrons' } );
2376
        my $patron2 = $builder->build_object( { class => 'Koha::Patrons' } );
2377
        my $manager = $builder->build_object({ class => "Koha::Patrons" });
2378
        t::lib::Mocks::mock_userenv({ patron => $manager, branchcode => $manager->branchcode });
2379
2380
        my $item = $builder->build_sample_item(
2381
            {
2382
                biblionumber     => $biblio->biblionumber,
2383
                library          => $library->branchcode,
2384
                replacementprice => 23.00,
2385
                replacementprice => $replacement_amount,
2386
                itype            => $item_type->itemtype
2387
            }
2388
        );
2389
2390
        # Add overdue fine for patron2 (order of these actions is meaningful)
2391
        my $account2 = $patron2->account;
2392
        my $fine2 = $account2->add_debit(
2393
            {
2394
                type       => 'overdue',
2395
                amount     => '4.00',
2396
                item_id    => $item->itemnumber,
2397
                user_id    => $manager->borrowernumber,
2398
                interface  => 'commandline',
2399
                library_id => $manager->branchcode
2400
            }
2401
        )->status('RETURNED')->store;
2402
2403
        my $overdue_fee_lines = Koha::Account::Lines->search(
2404
            { borrowernumber => $patron2->id, itemnumber => $item->itemnumber, accounttype => 'OVERDUE' } );
2405
        is( $overdue_fee_lines->count, 1, 'Only one overdue item fee produced for patron 2' );
2406
        my $overdue_fee_line = $overdue_fee_lines->next;
2407
        is( $overdue_fee_line->amount + 0, '4', 'The right OVERDUE amount is generated for patron 2' );
2408
        is( $overdue_fee_line->amountoutstanding + 0,
2409
            '4', 'The right OVERDUE amountountstanding is generated for patron 2' );
2410
2411
        # Issue and mark as lost for patron1 (order of these actions is meaningful)
2412
        AddIssue( $patron1->unblessed, $item->barcode );
2413
        ModItem( { itemlost => 3 }, $biblio->biblionumber, $item->itemnumber );
2414
        LostItem( $item->itemnumber, 1 );
2415
2416
        # Writeoff overdue fine for patron2 (order of these actions is meaningful)
2417
        my $writeoff2 = $account2->pay(
2418
            {
2419
                amount       => '4.00',
2420
                library_id   => $manager->branchcode,
2421
                lines        => [$fine2],
2422
                type         => 'writeoff'
2423
            }
2424
        );
2425
2426
        my $processing_fee_lines = Koha::Account::Lines->search(
2427
            { borrowernumber => $patron1->id, itemnumber => $item->itemnumber, accounttype => 'PF' } );
2428
        is( $processing_fee_lines->count, 1, 'Only one processing fee produced for patron 1' );
2429
        my $processing_fee_line = $processing_fee_lines->next;
2430
        is( $processing_fee_line->amount + 0,
2431
            $processfee_amount, 'The right PF amount is generated for patron 1' );
2432
        is( $processing_fee_line->amountoutstanding + 0,
2433
            $processfee_amount, 'The right PF amountoutstanding is generated for patron 1' );
2434
2435
        my $lost_fee_lines = Koha::Account::Lines->search(
2436
            { borrowernumber => $patron1->id, itemnumber => $item->itemnumber, accounttype => 'LOST' } );
2437
        is( $lost_fee_lines->count, 1, 'Only one lost item fee produced for patron 1' );
2438
        my $lost_fee_line = $lost_fee_lines->next;
2439
        is( $lost_fee_line->amount + 0, $replacement_amount, 'The right LOST amount is generated for patron 1' );
2440
        is( $lost_fee_line->amountoutstanding + 0,
2441
            $replacement_amount, 'The right LOST amountountstanding is generated for patron 1' );
2442
2443
        my $overdue_writeoff_lines = Koha::Account::Lines->search(
2444
            { borrowernumber => $patron2->id, itemnumber => $item->itemnumber, accounttype => 'W' } );
2445
        is( $overdue_writeoff_lines->count, 1, 'Only one overdue item writeoff produced for patron 2' );
2446
        my $overdue_writeoff_line = $overdue_writeoff_lines->next;
2447
        is( $overdue_writeoff_line->amount + 0, '-4', 'The right W amount is generated for patron 2' );
2448
        is( $overdue_writeoff_line->amountoutstanding + 0,
2449
            '0', 'The right W amountountstanding is generated for patron 2' );
2450
        is( $overdue_fee_line->discard_changes->amountoutstanding + 0, 0, 'The OVERDUE fine amountoutstanding has been reduced to zero for patron 2');
2451
2452
        is ( DateTime->compare($overdue_fee_line->date, $lost_fee_line->date), -1, 'The overdue fine for patron 2 was added before the lost fee for patron 1');
2453
        is ( DateTime->compare($lost_fee_line->date, $overdue_writeoff_line->date), -1, 'The lost item fee for patron 1 was added before the overdue fine writeoff for patron 2');
2454
2455
        my $credit_return_id = C4::Circulation::_FixAccountForLostAndReturned( $item->itemnumber, undef );
2456
        my $credit_return = Koha::Account::Lines->find($credit_return_id);
2457
2458
        is( $credit_return->accounttype, 'CR', 'An account line of type CR is added' );
2459
        is( $credit_return->borrowernumber, $patron1->borrowernumber, 'The account line of type CR is associated with patron 1' );
2460
        is( $credit_return->amount + 0, -99.00, 'The account line of type CR has an amount of -99' );
2461
        is( $credit_return->amountoutstanding + 0, 0, 'The account line of type CR has an amountoutstanding of 0' );
2462
2463
        $lost_fee_line->discard_changes;
2464
        is( $lost_fee_line->amountoutstanding + 0, 0, 'Lost fee has no outstanding amount' );
2465
        is( $lost_fee_line->accounttype,
2466
            'LOST', 'Lost fee now still has account type of LOST' );
2467
        is( $lost_fee_line->status, 'RETURNED', "Lost fee now has account status of RETURNED");
2468
2469
        is( $patron1->account->balance, 20, 'The patron balance is 20, still owes the processing fee' );
2470
    };
2471
2370
};
2472
};
2371
2473
2372
subtest '_FixOverduesOnReturn' => sub {
2474
subtest '_FixOverduesOnReturn' => sub {
2373
- 

Return to bug 22377