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

(-)a/t/db_dependent/Circulation.t (-39 / +294 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 => 4;
1988
    my $title  = 'Koha for Dummies';
1989
    my ( $biblionumber, $biblioitemnumber ) = add_biblio($title, 'Hall, Daria');
1990
1987
1991
    my $barcode = 'KD123456789';
1988
    t::lib::Mocks::mock_preference( 'WhenLostChargeReplacementFee', 1 );
1992
    my $branchcode  = $library2->{branchcode};
1989
    t::lib::Mocks::mock_preference( 'WhenLostForgiveFine',          0 );
1993
1990
1994
    my ( $item_bibnum, $item_bibitemnum, $itemnumber ) = AddItem(
1991
    my $processfee_amount  = 20.00;
1995
        {
1992
    my $replacement_amount = 99.00;
1996
            homebranch       => $branchcode,
1993
    my $item_type          = $builder->build_object(
1997
            holdingbranch    => $branchcode,
1994
        {   class => 'Koha::ItemTypes',
1998
            barcode          => $barcode,
1995
            value => {
1999
            replacementprice => 99.00,
1996
                notforloan         => undef,
2000
            itype            => $itemtype
1997
                rentalcharge       => 0,
2001
        },
1998
                defaultreplacecost => undef,
2002
        $biblionumber
1999
                processfee         => $processfee_amount
2000
            }
2001
        }
2003
    );
2002
    );
2003
    my $library = $builder->build_object( { class => 'Koha::Libraries' } );
2004
2004
2005
    my $patron = $builder->build( { source => 'Borrower' } );
2005
    # Generate test biblio
2006
    my $title = 'Koha for Dummies';
2007
    my ( $biblionumber, $biblioitemnumber ) = add_biblio( $title, 'Hall, Daria' );
2006
2008
2007
    Koha::Account::Line->new(
2009
    subtest 'Full write-off tests' => sub {
2008
        {
2009
            borrowernumber => $patron->{borrowernumber},
2010
            accounttype    => 'F',
2011
            itemnumber     => $itemnumber,
2012
            amount => 10.00,
2013
            amountoutstanding => 10.00,
2014
        }
2015
    )->store();
2016
2010
2017
    my $accountline = Koha::Account::Line->new(
2011
        plan tests => 10;
2018
        {
2019
            borrowernumber => $patron->{borrowernumber},
2020
            accounttype    => 'L',
2021
            itemnumber     => $itemnumber,
2022
            amount => 99.00,
2023
            amountoutstanding => 99.00,
2024
        }
2025
    )->store();
2026
2012
2027
    C4::Circulation::_FixAccountForLostAndReturned( $itemnumber, $patron->{borrowernumber} );
2013
        my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
2014
        my $barcode = 'KD123456789';
2028
2015
2029
    $accountline->_result()->discard_changes();
2016
        my ( undef, undef, $item_id ) = AddItem(
2017
            {   homebranch       => $library->branchcode,
2018
                holdingbranch    => $library->branchcode,
2019
                barcode          => $barcode,
2020
                replacementprice => $replacement_amount,
2021
                itype            => $item_type->itemtype
2022
            },
2023
            $biblionumber
2024
        );
2025
2026
        AddIssue( $patron->unblessed, $barcode );
2027
2028
        # Simulate item marked as lost
2029
        ModItem( { itemlost => 3 }, $biblionumber, $item_id );
2030
        LostItem( $item_id, 1 );
2031
2032
        my $processing_fee_lines = Koha::Account::Lines->search(
2033
            { borrowernumber => $patron->id, itemnumber => $item_id, accounttype => 'PF' } );
2034
        is( $processing_fee_lines->count, 1, 'Only one processing fee produced' );
2035
        my $processing_fee_line = $processing_fee_lines->next;
2036
        is( $processing_fee_line->amount + 0.00,
2037
            $processfee_amount, 'The right PF amount is generated' );
2038
        is( $processing_fee_line->amountoutstanding + 0.00,
2039
            $processfee_amount, 'The right PF amountoutstanding is generated' );
2040
2041
        my $lost_fee_lines = Koha::Account::Lines->search(
2042
            { borrowernumber => $patron->id, itemnumber => $item_id, accounttype => 'L' } );
2043
        is( $lost_fee_lines->count, 1, 'Only one lost item fee produced' );
2044
        my $lost_fee_line = $lost_fee_lines->next;
2045
        is( $lost_fee_line->amount + 0.00, $replacement_amount, 'The right L amount is generated' );
2046
        is( $lost_fee_line->amountoutstanding + 0.00,
2047
            $replacement_amount, 'The right L amountountstanding is generated' );
2048
2049
        my $account = $patron->account;
2050
        my $debts   = $account->outstanding_debits;
2051
2052
        # Write off the debt
2053
        my $credit = $account->add_credit(
2054
            {   amount => $account->balance,
2055
                type   => 'writeoff'
2056
            }
2057
        );
2058
        $credit->apply( { debits => $debts, offset_type => 'Writeoff' } );
2059
2060
        my $credit_return_id = C4::Circulation::_FixAccountForLostAndReturned( $item_id, $patron->id );
2061
        is( $credit_return_id, undef, 'No CR account line added' );
2062
2063
        $lost_fee_line->discard_changes; # reload from DB
2064
        is( $lost_fee_line->amountoutstanding + 0.00, 0.00, 'Lost fee has no outstanding amount' );
2065
        is( $lost_fee_line->accounttype,
2066
            'LR', 'Lost fee now has account type of LR ( Lost Returned )' );
2067
2068
        is( $patron->account->balance, -0, 'The patron balance is 0, everything was written off' );
2069
    };
2030
2070
2031
    is( $accountline->amountoutstanding, '0.000000', 'Lost fee has no outstanding amount' );
2071
    subtest 'Full payment tests' => sub {
2032
    is( $accountline->accounttype, 'LR', 'Lost fee now has account type of LR ( Lost Returned )');
2072
2073
        plan tests => 12;
2074
2075
        my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
2076
        my $barcode = 'KD123456790';
2077
2078
        my ( undef, undef, $item_id ) = AddItem(
2079
            {   homebranch       => $library->branchcode,
2080
                holdingbranch    => $library->branchcode,
2081
                barcode          => $barcode,
2082
                replacementprice => $replacement_amount,
2083
                itype            => $item_type->itemtype
2084
            },
2085
            $biblionumber
2086
        );
2087
2088
        AddIssue( $patron->unblessed, $barcode );
2089
2090
        # Simulate item marked as lost
2091
        ModItem( { itemlost => 1 }, $biblionumber, $item_id );
2092
        LostItem( $item_id, 1 );
2093
2094
        my $processing_fee_lines = Koha::Account::Lines->search(
2095
            { borrowernumber => $patron->id, itemnumber => $item_id, accounttype => 'PF' } );
2096
        is( $processing_fee_lines->count, 1, 'Only one processing fee produced' );
2097
        my $processing_fee_line = $processing_fee_lines->next;
2098
        is( $processing_fee_line->amount + 0.00,
2099
            $processfee_amount, 'The right PF amount is generated' );
2100
        is( $processing_fee_line->amountoutstanding + 0.00,
2101
            $processfee_amount, 'The right PF amountoutstanding is generated' );
2102
2103
        my $lost_fee_lines = Koha::Account::Lines->search(
2104
            { borrowernumber => $patron->id, itemnumber => $item_id, accounttype => 'L' } );
2105
        is( $lost_fee_lines->count, 1, 'Only one lost item fee produced' );
2106
        my $lost_fee_line = $lost_fee_lines->next;
2107
        is( $lost_fee_line->amount + 0.00, $replacement_amount, 'The right L amount is generated' );
2108
        is( $lost_fee_line->amountoutstanding + 0.00,
2109
            $replacement_amount, 'The right L amountountstanding is generated' );
2110
2111
        my $account = $patron->account;
2112
        my $debts   = $account->outstanding_debits;
2113
2114
        # Write off the debt
2115
        my $credit = $account->add_credit(
2116
            {   amount => $account->balance,
2117
                type   => 'payment'
2118
            }
2119
        );
2120
        $credit->apply( { debits => $debts, offset_type => 'Payment' } );
2121
2122
        my $credit_return_id = C4::Circulation::_FixAccountForLostAndReturned( $item_id, $patron->id );
2123
        my $credit_return = Koha::Account::Lines->find($credit_return_id);
2124
2125
        is( $credit_return->accounttype, 'CR', 'An account line of type CR is added' );
2126
        is( $credit_return->amount + 0.00,
2127
            -99.00, 'The account line of type CR has an amount of -99' );
2128
        is( $credit_return->amountoutstanding + 0.00,
2129
            -99.00, 'The account line of type CR has an amountoutstanding of -99' );
2130
2131
        $lost_fee_line->discard_changes;
2132
        is( $lost_fee_line->amountoutstanding + 0.00, 0.00, 'Lost fee has no outstanding amount' );
2133
        is( $lost_fee_line->accounttype,
2134
            'LR', 'Lost fee now has account type of LR ( Lost Returned )' );
2135
2136
        is( $patron->account->balance,
2137
            -99, 'The patron balance is -99, a credit that equals the lost fee payment' );
2138
    };
2139
2140
    subtest 'Test without payment or write off' => sub {
2141
2142
        plan tests => 12;
2143
2144
        my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
2145
        my $barcode = 'KD123456791';
2146
2147
        my ( undef, undef, $item_id ) = AddItem(
2148
            {   homebranch       => $library->branchcode,
2149
                holdingbranch    => $library->branchcode,
2150
                barcode          => $barcode,
2151
                replacementprice => $replacement_amount,
2152
                itype            => $item_type->itemtype
2153
            },
2154
            $biblionumber
2155
        );
2156
2157
        AddIssue( $patron->unblessed, $barcode );
2158
2159
        # Simulate item marked as lost
2160
        ModItem( { itemlost => 3 }, $biblionumber, $item_id );
2161
        LostItem( $item_id, 1 );
2162
2163
        my $processing_fee_lines = Koha::Account::Lines->search(
2164
            { borrowernumber => $patron->id, itemnumber => $item_id, accounttype => 'PF' } );
2165
        is( $processing_fee_lines->count, 1, 'Only one processing fee produced' );
2166
        my $processing_fee_line = $processing_fee_lines->next;
2167
        is( $processing_fee_line->amount + 0.00,
2168
            $processfee_amount, 'The right PF amount is generated' );
2169
        is( $processing_fee_line->amountoutstanding + 0.00,
2170
            $processfee_amount, 'The right PF amountoutstanding is generated' );
2171
2172
        my $lost_fee_lines = Koha::Account::Lines->search(
2173
            { borrowernumber => $patron->id, itemnumber => $item_id, accounttype => 'L' } );
2174
        is( $lost_fee_lines->count, 1, 'Only one lost item fee produced' );
2175
        my $lost_fee_line = $lost_fee_lines->next;
2176
        is( $lost_fee_line->amount + 0.00, $replacement_amount, 'The right L amount is generated' );
2177
        is( $lost_fee_line->amountoutstanding + 0.00,
2178
            $replacement_amount, 'The right L amountountstanding is generated' );
2179
2180
        my $credit_return_id
2181
            = C4::Circulation::_FixAccountForLostAndReturned( $item_id, $patron->id );
2182
        my $credit_return = Koha::Account::Lines->find($credit_return_id);
2183
2184
        is( $credit_return->accounttype, 'CR',  'An account line of type CR is added' );
2185
        is( $credit_return->amount * -1, 99.00, 'The account line of type CR has amount of 99' );
2186
        is( $credit_return->amountoutstanding + 0.00,
2187
            0.00, 'The account line of type CR has amountoutstanding of 0' );
2188
2189
        $lost_fee_line->discard_changes;
2190
        is( $lost_fee_line->amountoutstanding + 0.00, 0.00, 'Lost fee has no outstanding amount' );
2191
        is( $lost_fee_line->accounttype,
2192
            'LR', 'Lost fee now has account type of LR ( Lost Returned )' );
2193
2194
        is( $patron->account->balance,
2195
            20, 'The patron balance is 20, still owes the processing fee' );
2196
    };
2197
2198
    subtest 'Test with partial payement and write off, and remaining debt' => sub {
2199
2200
        plan tests => 15;
2201
2202
        my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
2203
        my $barcode = 'KD123456792';
2204
2205
        my ( undef, undef, $item_id ) = AddItem(
2206
            {   homebranch       => $library->branchcode,
2207
                holdingbranch    => $library->branchcode,
2208
                barcode          => $barcode,
2209
                replacementprice => $replacement_amount,
2210
                itype            => $item_type->itemtype
2211
            },
2212
            $biblionumber
2213
        );
2214
2215
        AddIssue( $patron->unblessed, $barcode );
2216
2217
        # Simulate item marked as lost
2218
        ModItem( { itemlost => 1 }, $biblionumber, $item_id );
2219
        LostItem( $item_id, 1 );
2220
2221
        my $processing_fee_lines = Koha::Account::Lines->search(
2222
            { borrowernumber => $patron->id, itemnumber => $item_id, accounttype => 'PF' } );
2223
        is( $processing_fee_lines->count, 1, 'Only one processing fee produced' );
2224
        my $processing_fee_line = $processing_fee_lines->next;
2225
        is( $processing_fee_line->amount + 0.00,
2226
            $processfee_amount, 'The right PF amount is generated' );
2227
        is( $processing_fee_line->amountoutstanding + 0.00,
2228
            $processfee_amount, 'The right PF amountoutstanding is generated' );
2229
2230
        my $lost_fee_lines = Koha::Account::Lines->search(
2231
            { borrowernumber => $patron->id, itemnumber => $item_id, accounttype => 'L' } );
2232
        is( $lost_fee_lines->count, 1, 'Only one lost item fee produced' );
2233
        my $lost_fee_line = $lost_fee_lines->next;
2234
        is( $lost_fee_line->amount + 0.00, $replacement_amount, 'The right L amount is generated' );
2235
        is( $lost_fee_line->amountoutstanding + 0.00,
2236
            $replacement_amount, 'The right L amountountstanding is generated' );
2237
2238
        my $account = $patron->account;
2239
        is( $account->balance, $processfee_amount + $replacement_amount, 'Balance is PF + L' );
2240
2241
        # Partially pay fee
2242
        my $payment_amount = 27;
2243
        my $payment        = $account->add_credit(
2244
            {   amount => $payment_amount,
2245
                type   => 'payment'
2246
            }
2247
        );
2248
        $payment->apply( { debits => $lost_fee_lines, offset_type => 'Payment' } );
2249
2250
        # Partially write off fee
2251
        my $write_off_amount = 25;
2252
        my $write_off        = $account->add_credit(
2253
            {   amount => $write_off_amount,
2254
                type   => 'writeoff'
2255
            }
2256
        );
2257
        $write_off->apply( { debits => $lost_fee_lines, offset_type => 'Writeoff' } );
2258
2259
        is( $account->balance,
2260
            $processfee_amount + $replacement_amount - $payment_amount - $write_off_amount,
2261
            'Payment and write off applied'
2262
        );
2263
2264
        my $credit_return_id = C4::Circulation::_FixAccountForLostAndReturned( $item_id, $patron->id );
2265
        my $credit_return = Koha::Account::Lines->find($credit_return_id);
2266
2267
        is( $account->balance, $processfee_amount, 'Patron only owes the PF' );
2268
2269
        is( $credit_return->accounttype, 'CR', 'An account line of type CR is added' );
2270
        is( $credit_return->amount + 0.00,
2271
            $payment_amount * -1,
2272
            'The account line of type CR has an amount equal to the payment'
2273
        );
2274
        is( $credit_return->amountoutstanding + 0.00,
2275
            $payment_amount * -1,
2276
            'The account line of type CR has an amountoutstanding equal to the payment'
2277
        );
2278
2279
        $lost_fee_line->discard_changes;
2280
        is( $lost_fee_line->amountoutstanding + 0.00, 0.00, 'Lost fee has no outstanding amount' );
2281
        is( $lost_fee_line->accounttype,
2282
            'LR', 'Lost fee now has account type of LR ( Lost Returned )' );
2283
2284
        is( $account->balance,
2285
            $processfee_amount - $payment_amount,
2286
            'The patron balance is the difference between the PF and the credit'
2287
        );
2288
    };
2033
};
2289
};
2034
2290
2035
subtest '_FixOverduesOnReturn' => sub {
2291
subtest '_FixOverduesOnReturn' => sub {
2036
- 

Return to bug 13098