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

(-)a/t/db_dependent/Circulation.t (-36 / +200 lines)
Lines 1982-2035 subtest 'AddReturn | is_overdue' => sub { Link Here
1982
};
1982
};
1983
1983
1984
subtest '_FixAccountForLostAndReturned' => sub {
1984
subtest '_FixAccountForLostAndReturned' => sub {
1985
    plan tests => 2;
1986
1985
1987
    # Generate test biblio
1986
    plan tests => 36;
1988
    my $title  = 'Koha for Dummies';
1989
    my ( $biblionumber, $biblioitemnumber ) = add_biblio($title, 'Hall, Daria');
1990
1987
1991
    my $barcode = 'KD123456789';
1988
    my $processfee_amount = 20.00;
1992
    my $branchcode  = $library2->{branchcode};
1989
    my $item_type         = $builder->build_object(
1990
        {   class => 'Koha::ItemTypes',
1991
            value => {
1992
                notforloan         => undef,
1993
                rentalcharge       => 0,
1994
                defaultreplacecost => undef,
1995
                processfee         => $processfee_amount
1996
            }
1997
        }
1998
    );
1999
    my $library = $builder->build_object( { class => 'Koha::Libraries' } );
1993
2000
1994
    my ( $item_bibnum, $item_bibitemnum, $itemnumber ) = AddItem(
2001
    # Generate test biblio
1995
        {
2002
    my $title = 'Koha for Dummies';
1996
            homebranch       => $branchcode,
2003
    my ( $biblionumber, $biblioitemnumber ) = add_biblio( $title, 'Hall, Daria' );
1997
            holdingbranch    => $branchcode,
2004
1998
            barcode          => $barcode,
2005
    my $barcode_1          = 'KD123456789';
1999
            replacementprice => 99.00,
2006
    my $replacement_amount = 99.00;
2000
            itype            => $itemtype
2007
2008
    my ( undef, undef, $item_1_id ) = AddItem(
2009
        {   homebranch       => $library->branchcode,
2010
            holdingbranch    => $library->branchcode,
2011
            barcode          => $barcode_1,
2012
            replacementprice => $replacement_amount,
2013
            itype            => $item_type->itemtype
2001
        },
2014
        },
2002
        $biblionumber
2015
        $biblionumber
2003
    );
2016
    );
2004
2017
2005
    my $patron = $builder->build( { source => 'Borrower' } );
2018
    ##
2006
2019
    ## Tests with write off
2007
    Koha::Account::Line->new(
2020
    ##
2008
        {
2021
2009
            borrowernumber => $patron->{borrowernumber},
2022
    my $patron_1 = $builder->build_object( { class => 'Koha::Patrons' } );
2010
            accounttype    => 'F',
2023
    AddIssue( $patron_1->unblessed, $barcode_1 );
2011
            itemnumber     => $itemnumber,
2024
    t::lib::Mocks::mock_preference( 'WhenLostChargeReplacementFee', 1 );
2012
            amount => 10.00,
2025
    t::lib::Mocks::mock_preference( 'WhenLostForgiveFine',          0 );
2013
            amountoutstanding => 10.00,
2026
2027
    # Simulate item marked as lost
2028
    ModItem( { itemlost => 3 }, $biblionumber, $item_1_id );
2029
    LostItem( $item_1_id, 1 );
2030
2031
    my $processing_fee_lines_1 = Koha::Account::Lines->search(
2032
        { borrowernumber => $patron_1->id, itemnumber => $item_1_id, accounttype => 'PF' } );
2033
    is( $processing_fee_lines_1->count, 1, 'Only one processing fee produced' );
2034
    my $processing_fee_line_1 = $processing_fee_lines_1->next;
2035
    is( $processing_fee_line_1->amount + 0.00,
2036
        $processfee_amount, 'The right PF amount is generated' );
2037
    is( $processing_fee_line_1->amountoutstanding + 0.00,
2038
        $processfee_amount, 'The right PF amountoutstanding is generated' );
2039
2040
    my $lost_fee_lines_1 = Koha::Account::Lines->search(
2041
        { borrowernumber => $patron_1->id, itemnumber => $item_1_id, accounttype => 'L' } );
2042
    is( $lost_fee_lines_1->count, 1, 'Only one lost item fee produced' );
2043
    my $lost_fee_line_1 = $lost_fee_lines_1->next;
2044
    is( $lost_fee_line_1->amount + 0.00, $replacement_amount, 'The right L amount is generated' );
2045
    is( $lost_fee_line_1->amountoutstanding + 0.00,
2046
        $replacement_amount, 'The right L amountountstanding is generated' );
2047
2048
    my $account_1 = $patron_1->account;
2049
    my $debts_1   = $account_1->outstanding_debits;
2050
2051
    # Write off the debt
2052
    my $credit_1 = $account_1->add_credit(
2053
        {   amount => $account_1->balance,
2054
            type   => 'writeoff'
2014
        }
2055
        }
2015
    )->store();
2056
    );
2057
    $credit_1->apply( { debits => $debts_1, offset_type => 'Writeoff' } );
2016
2058
2017
    my $accountline = Koha::Account::Line->new(
2059
    my $credit_return_1_id = C4::Circulation::_FixAccountForLostAndReturned( $item_1_id, $patron_1->id );
2018
        {
2060
    my $credit_return_1 = Koha::Account::Lines->find($credit_return_1_id);
2019
            borrowernumber => $patron->{borrowernumber},
2061
2020
            accounttype    => 'L',
2062
    is( $credit_return_1->accounttype, 'CR', 'An account line of type CR is added' );
2021
            itemnumber     => $itemnumber,
2063
    is( $credit_return_1->amount + 0.00, 0.00, 'The account has amount of 0' );
2022
            amount => 99.00,
2064
    is( $credit_return_1->amountoutstanding + 0.00, 0.00, 'The account has amountoutstanding of 0' );
2023
            amountoutstanding => 99.00,
2065
2066
    $lost_fee_line_1->discard_changes;
2067
    is( $lost_fee_line_1->amountoutstanding + 0.00, 0.00, 'Lost fee has no outstanding amount' );
2068
    is( $lost_fee_line_1->accounttype, 'LR',
2069
        'Lost fee now has account type of LR ( Lost Returned )' );
2070
2071
    is( $patron_1->account->balance, -0, 'The patron balance is 0, everything was written off' );
2072
2073
    ##
2074
    ## Tests with payment
2075
    ##
2076
2077
    my $patron_2  = $builder->build_object( { class => 'Koha::Patrons' } );
2078
    my $barcode_2 = 'KD123456790';
2079
2080
    t::lib::Mocks::mock_preference( 'WhenLostChargeReplacementFee', 1 );
2081
    t::lib::Mocks::mock_preference( 'WhenLostForgiveFine',          0 );
2082
2083
    my ( undef, undef, $item_2_id ) = AddItem(
2084
        {   homebranch       => $library->branchcode,
2085
            holdingbranch    => $library->branchcode,
2086
            barcode          => $barcode_2,
2087
            replacementprice => $replacement_amount,
2088
            itype            => $item_type->itemtype
2089
        },
2090
        $biblionumber
2091
    );
2092
2093
    AddIssue( $patron_2->unblessed, $barcode_2 );
2094
    t::lib::Mocks::mock_preference( 'WhenLostChargeReplacementFee', 1 );
2095
    t::lib::Mocks::mock_preference( 'WhenLostForgiveFine',          0 );
2096
2097
    # Simulate item marked as lost
2098
    ModItem( { itemlost => 1 }, $biblionumber, $item_2_id );
2099
    LostItem( $item_2_id, 1 );
2100
2101
    my $processing_fee_lines_2 = Koha::Account::Lines->search(
2102
        { borrowernumber => $patron_2->id, itemnumber => $item_2_id, accounttype => 'PF' } );
2103
    is( $processing_fee_lines_2->count, 1, 'Only one processing fee produced' );
2104
    my $processing_fee_line_2 = $processing_fee_lines_2->next;
2105
    is( $processing_fee_line_2->amount + 0.00,
2106
        $processfee_amount, 'The right PF amount is generated' );
2107
    is( $processing_fee_line_2->amountoutstanding + 0.00,
2108
        $processfee_amount, 'The right PF amountoutstanding is generated' );
2109
2110
    my $lost_fee_lines_2 = Koha::Account::Lines->search(
2111
        { borrowernumber => $patron_2->id, itemnumber => $item_2_id, accounttype => 'L' } );
2112
    is( $lost_fee_lines_2->count, 1, 'Only one lost item fee produced' );
2113
    my $lost_fee_line_2 = $lost_fee_lines_2->next;
2114
    is( $lost_fee_line_2->amount + 0.00, $replacement_amount, 'The right L amount is generated' );
2115
    is( $lost_fee_line_2->amountoutstanding + 0.00,
2116
        $replacement_amount, 'The right L amountountstanding is generated' );
2117
2118
    my $account_2 = $patron_2->account;
2119
    my $debts_2   = $account_2->outstanding_debits;
2120
2121
    # Write off the debt
2122
    my $credit_2 = $account_2->add_credit(
2123
        {   amount => $account_2->balance,
2124
            type   => 'payment'
2024
        }
2125
        }
2025
    )->store();
2126
    );
2127
    $credit_2->apply( { debits => $debts_2, offset_type => 'Payment' } );
2026
2128
2027
    C4::Circulation::_FixAccountForLostAndReturned( $itemnumber, $patron->{borrowernumber} );
2129
    my $credit_return_2_id = C4::Circulation::_FixAccountForLostAndReturned( $item_2_id, $patron_2->id );
2130
    my $credit_return_2 = Koha::Account::Lines->find($credit_return_2_id);
2028
2131
2029
    $accountline->_result()->discard_changes();
2132
    is( $credit_return_2->accounttype, 'CR', 'An account line of type CR is added' );
2133
    is( $credit_return_2->amount + 0.00, -99.00, 'The account has an amount of -99' );
2134
    is( $credit_return_2->amountoutstanding + 0.00, -99.00, 'The account has an amountoutstanding of -99' );
2135
2136
    $lost_fee_line_2->discard_changes;
2137
    is( $lost_fee_line_2->amountoutstanding + 0.00, 0.00, 'Lost fee has no outstanding amount' );
2138
    is( $lost_fee_line_2->accounttype, 'LR',
2139
        'Lost fee now has account type of LR ( Lost Returned )' );
2140
2141
    is( $patron_2->account->balance, -99, 'The patron balance is -99, a credit that equals the lost fee payment' );
2142
2143
    ##
2144
    ## Test without write-off or payment
2145
    ##
2146
2147
    my $patron_3  = $builder->build_object({ class => 'Koha::Patrons' });
2148
    my $barcode_3 = 'KD123456791';
2149
2150
    t::lib::Mocks::mock_preference( 'WhenLostChargeReplacementFee', 1 );
2151
    t::lib::Mocks::mock_preference( 'WhenLostForgiveFine',          0 );
2152
2153
    my ( undef, undef, $item_3_id ) = AddItem(
2154
        {   homebranch       => $library->branchcode,
2155
            holdingbranch    => $library->branchcode,
2156
            barcode          => $barcode_3,
2157
            replacementprice => $replacement_amount,
2158
            itype            => $item_type->itemtype
2159
        },
2160
        $biblionumber
2161
    );
2030
2162
2031
    is( $accountline->amountoutstanding, '0.000000', 'Lost fee has no outstanding amount' );
2163
    AddIssue( $patron_3->unblessed, $barcode_3 );
2032
    is( $accountline->accounttype, 'LR', 'Lost fee now has account type of LR ( Lost Returned )');
2164
    # Simulate item marked as lost
2165
    ModItem( { itemlost => 3 }, $biblionumber, $item_3_id );
2166
    LostItem( $item_3_id, 1 );
2167
2168
    my $processing_fee_lines_3 = Koha::Account::Lines->search(
2169
        { borrowernumber => $patron_3->id, itemnumber => $item_3_id, accounttype => 'PF' } );
2170
    is( $processing_fee_lines_3->count, 1, 'Only one processing fee produced' );
2171
    my $processing_fee_line_3 = $processing_fee_lines_3->next;
2172
    is( $processing_fee_line_3->amount + 0.00,
2173
        $processfee_amount, 'The right PF amount is generated' );
2174
    is( $processing_fee_line_3->amountoutstanding + 0.00,
2175
        $processfee_amount, 'The right PF amountoutstanding is generated' );
2176
2177
    my $lost_fee_lines_3 = Koha::Account::Lines->search(
2178
        { borrowernumber => $patron_3->id, itemnumber => $item_3_id, accounttype => 'L' } );
2179
    is( $lost_fee_lines_3->count, 1, 'Only one lost item fee produced' );
2180
    my $lost_fee_line_3 = $lost_fee_lines_3->next;
2181
    is( $lost_fee_line_3->amount + 0.00, $replacement_amount, 'The right L amount is generated' );
2182
    is( $lost_fee_line_3->amountoutstanding + 0.00,
2183
        $replacement_amount, 'The right L amountountstanding is generated' );
2184
2185
    my $credit_return_3_id = C4::Circulation::_FixAccountForLostAndReturned( $item_3_id, $patron_3->id );
2186
    my $credit_return_3 = Koha::Account::Lines->find($credit_return_3_id);
2187
2188
    is( $credit_return_3->accounttype, 'CR', 'An account line of type CR is added' );
2189
    is( $credit_return_3->amount * -1, 99.00, 'The account line of type CR has amount of 99' );
2190
    is( $credit_return_3->amountoutstanding + 0.00, 0.00, 'The account line of type CR has amountoutstanding of 0' );
2191
2192
    $lost_fee_line_3->discard_changes;
2193
    is( $lost_fee_line_3->amountoutstanding + 0.00, 0.00, 'Lost fee has no outstanding amount' );
2194
    is( $lost_fee_line_3->accounttype, 'LR',
2195
        'Lost fee now has account type of LR ( Lost Returned )' );
2196
2197
    is( $patron_3->account->balance, 20, 'The patron balance is 20, still owes the processing fee' );
2033
};
2198
};
2034
2199
2035
subtest '_FixOverduesOnReturn' => sub {
2200
subtest '_FixOverduesOnReturn' => sub {
2036
- 

Return to bug 13098