Bug 21915: Reconcile balance on _FixAccountForLostAndReturned call
authorTomas Cohen Arazi <tomascohen@theke.io>
Thu, 29 Nov 2018 19:46:36 +0000 (16:46 -0300)
committerMartin Renvoize <martin.renvoize@ptfs-europe.com>
Thu, 10 Jan 2019 16:17:48 +0000 (16:17 +0000)
This patch makes _FixAccountForLostAndReturned reconcile the patron's
account balance, when the AccountAutoReconcile syspref is set.

To test:
- Apply this patch
- Run:
  $ kshell
 k$ prove t/db_dependent/Circulation.t
=> SUCCESS: Tests pass, peace \o/
- Sign off :-D

Sponsored-by: ByWater Solutions
Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
(cherry picked from commit 49c9e93292b2f694857edc66e913b9f65d081e8b)
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

C4/Circulation.pm
t/db_dependent/Circulation.t

index fc1379d..7084da8 100644 (file)
@@ -2445,6 +2445,10 @@ sub _FixAccountForLostAndReturned {
 
     ModItem( { paidfor => '' }, undef, $itemnumber, { log_action => 0 } );
 
+    if ( defined $account and C4::Context->preference('AccountAutoReconcile') ) {
+        $account->reconcile_balance;
+    }
+
     return ($credit) ? $credit->id : undef;
 }
 
index fa21868..aaf8840 100755 (executable)
@@ -26,6 +26,7 @@ use POSIX qw( floor );
 use t::lib::Mocks;
 use t::lib::TestBuilder;
 
+use C4::Accounts;
 use C4::Calendar;
 use C4::Circulation;
 use C4::Biblio;
@@ -1992,7 +1993,7 @@ subtest 'AddReturn | is_overdue' => sub {
 
 subtest '_FixAccountForLostAndReturned' => sub {
 
-    plan tests => 4;
+    plan tests => 5;
 
     t::lib::Mocks::mock_preference( 'WhenLostChargeReplacementFee', 1 );
     t::lib::Mocks::mock_preference( 'WhenLostForgiveFine',          0 );
@@ -2296,6 +2297,83 @@ subtest '_FixAccountForLostAndReturned' => sub {
             'The patron balance is the difference between the PF and the credit'
         );
     };
+
+    subtest 'Partial payement, existing debits and AccountAutoReconcile' => sub {
+
+        plan tests => 8;
+
+        my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
+        my $barcode = 'KD123456793';
+        my $replacement_amount = 100;
+        my $processfee_amount  = 20;
+
+        my $item_type          = $builder->build_object(
+            {   class => 'Koha::ItemTypes',
+                value => {
+                    notforloan         => undef,
+                    rentalcharge       => 0,
+                    defaultreplacecost => undef,
+                    processfee         => 0
+                }
+            }
+        );
+        my ( undef, undef, $item_id ) = AddItem(
+            {   homebranch       => $library->branchcode,
+                holdingbranch    => $library->branchcode,
+                barcode          => $barcode,
+                replacementprice => $replacement_amount,
+                itype            => $item_type->itemtype
+            },
+            $biblionumber
+        );
+
+        AddIssue( $patron->unblessed, $barcode );
+
+        # Simulate item marked as lost
+        ModItem( { itemlost => 1 }, $biblionumber, $item_id );
+        LostItem( $item_id, 1 );
+
+        my $lost_fee_lines = Koha::Account::Lines->search(
+            { borrowernumber => $patron->id, itemnumber => $item_id, accounttype => 'L' } );
+        is( $lost_fee_lines->count, 1, 'Only one lost item fee produced' );
+        my $lost_fee_line = $lost_fee_lines->next;
+        is( $lost_fee_line->amount + 0, $replacement_amount, 'The right L amount is generated' );
+        is( $lost_fee_line->amountoutstanding + 0,
+            $replacement_amount, 'The right L amountountstanding is generated' );
+
+        my $account = $patron->account;
+        is( $account->balance, $replacement_amount, 'Balance is L' );
+
+        # Partially pay fee
+        my $payment_amount = 27;
+        my $payment        = $account->add_credit(
+            {   amount => $payment_amount,
+                type   => 'payment'
+            }
+        );
+        $payment->apply({ debits => $lost_fee_lines->reset, offset_type => 'Payment' });
+
+        is( $account->balance,
+            $replacement_amount - $payment_amount,
+            'Payment applied'
+        );
+
+        # TODO use add_debit when time comes
+        my $manual_debit_amount = 80;
+        C4::Accounts::manualinvoice( $patron->id, undef, undef, 'FU', $manual_debit_amount );
+
+        is( $account->balance, $manual_debit_amount + $replacement_amount - $payment_amount, 'Manual debit applied' );
+
+        t::lib::Mocks::mock_preference( 'AccountAutoReconcile', 1 );
+
+        my $credit_return_id = C4::Circulation::_FixAccountForLostAndReturned( $item_id, $patron->id );
+        my $credit_return = Koha::Account::Lines->find($credit_return_id);
+
+        is( $account->balance, $manual_debit_amount - $payment_amount, 'Balance is PF - payment (CR)' );
+
+        my $manual_debit = Koha::Account::Lines->search({ borrowernumber => $patron->id, accounttype => 'FU' })->next;
+        is( $manual_debit->amountoutstanding + 0, $manual_debit_amount - $payment_amount, 'reconcile_balance was called' );
+    };
 };
 
 subtest '_FixOverduesOnReturn' => sub {