| 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 => 10; | 
            
              | 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 = C4::Circulation::_FixAccountForLostAndReturned( $item_id, $patron->id ); | 
            
              | 2181 |         is( $credit_return_id, undef, 'No CR account line added' ); | 
            
              | 2182 |  | 
            
              | 2183 |         $lost_fee_line->discard_changes; | 
            
              | 2184 |         is( $lost_fee_line->amountoutstanding + 0.00, 0.00, 'Lost fee has no outstanding amount' ); | 
            
              | 2185 |         is( $lost_fee_line->accounttype, 'LR', 'Lost fee now has account type of LR ( Lost Returned )' ); | 
            
              | 2186 |  | 
            
              | 2187 |         is( $patron->account->balance, 20, 'The patron balance is 20, still owes the processing fee' ); | 
            
              | 2188 |     }; | 
            
              | 2189 |  | 
            
              | 2190 |     subtest 'Test with partial payement and write off, and remaining debt' => sub { | 
            
              | 2191 |  | 
            
              | 2192 |         plan tests => 15; | 
            
              | 2193 |  | 
            
              | 2194 |         my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); | 
            
              | 2195 |         my $barcode = 'KD123456792'; | 
            
              | 2196 |  | 
            
              | 2197 |         my ( undef, undef, $item_id ) = AddItem( | 
            
              | 2198 |             {   homebranch       => $library->branchcode, | 
            
              | 2199 |                 holdingbranch    => $library->branchcode, | 
            
              | 2200 |                 barcode          => $barcode, | 
            
              | 2201 |                 replacementprice => $replacement_amount, | 
            
              | 2202 |                 itype            => $item_type->itemtype | 
            
              | 2203 |             }, | 
            
              | 2204 |             $biblionumber | 
            
              | 2205 |         ); | 
            
              | 2206 |  | 
            
              | 2207 |         AddIssue( $patron->unblessed, $barcode ); | 
            
              | 2208 |  | 
            
              | 2209 |         # Simulate item marked as lost | 
            
              | 2210 |         ModItem( { itemlost => 1 }, $biblionumber, $item_id ); | 
            
              | 2211 |         LostItem( $item_id, 1 ); | 
            
              | 2212 |  | 
            
              | 2213 |         my $processing_fee_lines = Koha::Account::Lines->search( | 
            
              | 2214 |             { borrowernumber => $patron->id, itemnumber => $item_id, accounttype => 'PF' } ); | 
            
              | 2215 |         is( $processing_fee_lines->count, 1, 'Only one processing fee produced' ); | 
            
              | 2216 |         my $processing_fee_line = $processing_fee_lines->next; | 
            
              | 2217 |         is( $processing_fee_line->amount + 0.00, | 
            
              | 2218 |             $processfee_amount, 'The right PF amount is generated' ); | 
            
              | 2219 |         is( $processing_fee_line->amountoutstanding + 0.00, | 
            
              | 2220 |             $processfee_amount, 'The right PF amountoutstanding is generated' ); | 
            
              | 2221 |  | 
            
              | 2222 |         my $lost_fee_lines = Koha::Account::Lines->search( | 
            
              | 2223 |             { borrowernumber => $patron->id, itemnumber => $item_id, accounttype => 'L' } ); | 
            
              | 2224 |         is( $lost_fee_lines->count, 1, 'Only one lost item fee produced' ); | 
            
              | 2225 |         my $lost_fee_line = $lost_fee_lines->next; | 
            
              | 2226 |         is( $lost_fee_line->amount + 0.00, $replacement_amount, 'The right L amount is generated' ); | 
            
              | 2227 |         is( $lost_fee_line->amountoutstanding + 0.00, | 
            
              | 2228 |             $replacement_amount, 'The right L amountountstanding is generated' ); | 
            
              | 2229 |  | 
            
              | 2230 |         my $account = $patron->account; | 
            
              | 2231 |         is( $account->balance, $processfee_amount + $replacement_amount, 'Balance is PF + L' ); | 
            
              | 2232 |  | 
            
              | 2233 |         # Partially pay fee | 
            
              | 2234 |         my $payment_amount = 27; | 
            
              | 2235 |         my $payment        = $account->add_credit( | 
            
              | 2236 |             {   amount => $payment_amount, | 
            
              | 2237 |                 type   => 'payment' | 
            
              | 2238 |             } | 
            
              | 2239 |         ); | 
            
              | 2240 |  | 
            
              | 2241 |         $payment->apply( { debits => $lost_fee_lines->reset, offset_type => 'Payment' } ); | 
            
              | 2242 |  | 
            
              | 2243 |         # Partially write off fee | 
            
              | 2244 |         my $write_off_amount = 25; | 
            
              | 2245 |         my $write_off        = $account->add_credit( | 
            
              | 2246 |             {   amount => $write_off_amount, | 
            
              | 2247 |                 type   => 'writeoff' | 
            
              | 2248 |             } | 
            
              | 2249 |         ); | 
            
              | 2250 |         $write_off->apply( { debits => $lost_fee_lines->reset, offset_type => 'Writeoff' } ); | 
            
              | 2251 |  | 
            
              | 2252 |         is( $account->balance, | 
            
              | 2253 |             $processfee_amount + $replacement_amount - $payment_amount - $write_off_amount, | 
            
              | 2254 |             'Payment and write off applied' | 
            
              | 2255 |         ); | 
            
              | 2256 |  | 
            
              | 2257 |         my $credit_return_id = C4::Circulation::_FixAccountForLostAndReturned( $item_id, $patron->id ); | 
            
              | 2258 |         my $credit_return = Koha::Account::Lines->find($credit_return_id); | 
            
              | 2259 |  | 
            
              | 2260 |         is( $account->balance, $processfee_amount - $payment_amount, 'Balance is PF - payment (CR)' ); | 
            
              | 2261 |  | 
            
              | 2262 |         is( $credit_return->accounttype, 'CR', 'An account line of type CR is added' ); | 
            
              | 2263 |         is( $credit_return->amount + 0.00, | 
            
              | 2264 |             $payment_amount * -1, | 
            
              | 2265 |             'The account line of type CR has an amount equal to the payment' | 
            
              | 2266 |         ); | 
            
              | 2267 |         is( $credit_return->amountoutstanding + 0.00, | 
            
              | 2268 |             $payment_amount * -1, | 
            
              | 2269 |             'The account line of type CR has an amountoutstanding equal to the payment' | 
            
              | 2270 |         ); | 
            
              | 2271 |  | 
            
              | 2272 |         $lost_fee_line->discard_changes; | 
            
              | 2273 |         is( $lost_fee_line->amountoutstanding + 0.00, 0.00, 'Lost fee has no outstanding amount' ); | 
            
              | 2274 |         is( $lost_fee_line->accounttype, | 
            
              | 2275 |             'LR', 'Lost fee now has account type of LR ( Lost Returned )' ); | 
            
              | 2276 |  | 
            
              | 2277 |         is( $account->balance, | 
            
              | 2278 |             $processfee_amount - $payment_amount, | 
            
              | 2279 |             'The patron balance is the difference between the PF and the credit' | 
            
              | 2280 |         ); | 
            
              | 2281 |     }; | 
        
          | 2033 | }; | 2282 | }; | 
        
          | 2034 |  | 2283 |  | 
        
          | 2035 | subtest '_FixOverduesOnReturn' => sub { | 2284 | subtest '_FixOverduesOnReturn' => sub { | 
            
              | 2036 | -  |  |  |