@@ -, +, @@ creating credits not recieve a credit for the already paid balance --- C4/Circulation.pm | 26 ++++---- .../prog/en/modules/admin/smart-rules.tt | 31 ++++++++-- t/db_dependent/Circulation.t | 71 ++++++++++++++++++++-- t/db_dependent/RefundLostItemFeeRule.t | 8 +-- 4 files changed, 109 insertions(+), 27 deletions(-) --- a/C4/Circulation.pm +++ a/C4/Circulation.pm @@ -1374,18 +1374,16 @@ sub AddIssue { ## If item was lost, it has now been found, reverse any list item charges if necessary. if ( $item->{'itemlost'} ) { - if ( - Koha::RefundLostItemFeeRules->should_refund( - { - current_branch => C4::Context->userenv->{branch}, - item_home_branch => $item->{homebranch}, - item_holding_branch => $item->{holdingbranch} - } - ) - ) - { - _FixAccountForLostAndReturned( $item->{'itemnumber'}, undef, - $item->{'barcode'} ); + my $should_refund = Koha::RefundLostItemFeeRules->should_refund( + { + current_branch => C4::Context->userenv->{branch}, + item_home_branch => $item->{homebranch}, + item_holding_branch => $item->{holdingbranch} + } + ); + + if ($should_refund) { + _FixAccountForLostAndReturned( $item->{'itemnumber'}, undef, $item->{'barcode'}, $should_refund == 2 ); } } @@ -2384,6 +2382,7 @@ sub _FixAccountForLostAndReturned { my $itemnumber = shift or return; my $borrowernumber = @_ ? shift : undef; my $item_id = @_ ? shift : $itemnumber; # Send the barcode if you want that logged in the description + my $skip_paid = @_ ? shift : 0; # check for charge made for lost book my $accountline = Koha::Account::Lines->search( @@ -2398,6 +2397,7 @@ sub _FixAccountForLostAndReturned { return unless $accountline; return if $accountline->accounttype eq 'W'; # Written off + return if $skip_paid && $accountline->amountoutstanding <= 0; $accountline->accounttype('LR'); $accountline->store(); @@ -2405,7 +2405,7 @@ sub _FixAccountForLostAndReturned { my $account = Koha::Account->new( { patron_id => $accountline->borrowernumber } ); my $credit_id = $account->pay( { - amount => $accountline->amount, + amount => $skip_paid ? $accountline->amountoutstanding : $accountline->amount, description => "Item Returned " . $item_id, account_type => 'CR', offset_type => 'Lost Item Return', --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/smart-rules.tt +++ a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/smart-rules.tt @@ -554,20 +554,29 @@ --- a/t/db_dependent/Circulation.t +++ a/t/db_dependent/Circulation.t @@ -1972,7 +1972,7 @@ subtest 'AddReturn | is_overdue' => sub { }; subtest '_FixAccountForLostAndReturned' => sub { - plan tests => 2; + plan tests => 11; # Generate test biblio my $title = 'Koha for Dummies'; @@ -1992,11 +1992,11 @@ subtest '_FixAccountForLostAndReturned' => sub { $biblionumber ); - my $patron = $builder->build( { source => 'Borrower' } ); + my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); my $accountline = Koha::Account::Line->new( { - borrowernumber => $patron->{borrowernumber}, + borrowernumber => $patron->borrowernumber, accounttype => 'L', itemnumber => $itemnumber, amount => 99.00, @@ -2004,12 +2004,73 @@ subtest '_FixAccountForLostAndReturned' => sub { } )->store(); - C4::Circulation::_FixAccountForLostAndReturned( $itemnumber, $patron->{borrowernumber} ); + C4::Circulation::_FixAccountForLostAndReturned( $itemnumber, $patron->borrowernumber ); $accountline->_result()->discard_changes(); is( $accountline->amountoutstanding, '0.000000', 'Lost fee has no outstanding amount' ); - is( $accountline->accounttype, 'LR', 'Lost fee now has account type of LR ( Lost Returned )'); + is( $accountline->accounttype, 'LR', 'Lost fee now has account type of LR ( Lost Returned )' ); + is( $patron->account->balance, 0, 'Account balance should be 0 after return of lost item' ); + + # Tests for only if unpaid + ## Unpaid + $accountline->delete(); + $accountline = Koha::Account::Line->new( + { + borrowernumber => $patron->borrowernumber, + accounttype => 'L', + itemnumber => $itemnumber, + amount => 99.00, + amountoutstanding => 99.00, + } + )->store(); + + C4::Circulation::_FixAccountForLostAndReturned( $itemnumber, $patron->borrowernumber, undef, 1 ); + + $accountline->_result()->discard_changes(); + + is( $accountline->amountoutstanding, '0.000000', 'Lost fee has no outstanding amount, with skip_paid = 1 and unpaid' ); + is( $accountline->accounttype, 'LR', 'Lost fee now has account type of LR ( Lost Returned ), with skip_paid = 1 and unpaid'); + is( $patron->account->balance, 0, 'Account balance should be 0 after return of unpaid lost item' ); + + ## Paid + $accountline->delete(); + $accountline = Koha::Account::Line->new( + { + borrowernumber => $patron->borrowernumber, + accounttype => 'L', + itemnumber => $itemnumber, + amount => 99.00, + amountoutstanding => 0.00, + } + )->store(); + + C4::Circulation::_FixAccountForLostAndReturned( $itemnumber, $patron->borrowernumber, undef, 1 ); + + $accountline->_result()->discard_changes(); + + is( $accountline->amountoutstanding, '0.000000', 'Lost fee still has outstanding amount of 0, with skip_paid = 1 and already paid' ); + is( $accountline->accounttype, 'L', 'Lost fee now has account type of L ( Lost ), with skip_paid = 1 and already paid'); + is( $patron->account->balance, 0, 'Account balance should be 0 after return of partially paid lost item' ); + + ## Partially paid + $accountline->delete(); + $accountline = Koha::Account::Line->new( + { + borrowernumber => $patron->borrowernumber, + accounttype => 'L', + itemnumber => $itemnumber, + amount => 99.00, + amountoutstanding => 33.00, + } + )->store(); + + C4::Circulation::_FixAccountForLostAndReturned( $itemnumber, $patron->borrowernumber, undef, 1 ); + + $accountline->_result()->discard_changes(); + + is( $accountline->amountoutstanding, '0.000000', 'Lost fee has no outstanding amount, with skip_paid = 1 and partially paid' ); + is( $accountline->accounttype, 'LR', 'Lost fee now has account type of L ( Lost ), with skip_paid = 1 and partially paid'); }; subtest '_FixOverduesOnReturn' => sub { --- a/t/db_dependent/RefundLostItemFeeRule.t +++ a/t/db_dependent/RefundLostItemFeeRule.t @@ -130,7 +130,7 @@ subtest 'Koha::RefundLostItemFeeRules::_effective_branch_rule() tests' => sub { source => 'RefundLostItemFeeRule', value => { branchcode => '*', - refund => 1 + refund => 2 } }); my $specific_rule_false = $builder->build({ @@ -153,7 +153,7 @@ subtest 'Koha::RefundLostItemFeeRules::_effective_branch_rule() tests' => sub { # Delete specific rules Koha::RefundLostItemFeeRules->find({ branchcode => $specific_rule_false->{ branchcode } })->delete; is( Koha::RefundLostItemFeeRules->_effective_branch_rule( $specific_rule_false->{ branchcode } ), - 1,'No specific rule defined, fallback to global (true)'); + 2,'No specific rule defined, fallback to global (true)'); # Rollback transaction $schema->storage->txn_rollback; @@ -243,7 +243,7 @@ subtest 'Koha::RefundLostItemFeeRules::should_refund() tests' => sub { my $specific_rule_true = $builder->build({ source => 'RefundLostItemFeeRule', value => { - refund => 1 + refund => 2 } }); # Make sure we have an unused branchcode @@ -264,7 +264,7 @@ subtest 'Koha::RefundLostItemFeeRules::should_refund() tests' => sub { t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'CheckinLibrary' ); is( Koha::RefundLostItemFeeRules->should_refund( $params ), - 1,'Specific rule is applied (true)'); + 2,'Specific rule is applied (true)'); t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'ItemHomeBranch' ); is( Koha::RefundLostItemFeeRules->should_refund( $params ), --