Bug 13098: Make _FixAccountForLostAndReturned refund the right thing
authorTomas Cohen Arazi <tomascohen@theke.io>
Thu, 18 Oct 2018 16:08:42 +0000 (13:08 -0300)
committerNick Clemens <nick@bywatersolutions.com>
Wed, 31 Oct 2018 13:13:37 +0000 (13:13 +0000)
This patch changes the logic inside the method, to make it match the
behaviour described on the tests.

It uses the existing offsets on the account_offsets table to gather
information about the right things to refund.

To test:
- Run:
  $ kshell
 k$ prove t/db_dependent/Circulation.t
=> FAIL: Tests don't pass!
- Apply this patch
- Run
 k$ prove t/db_dependent/Circulation.t
=> SUCCESS: Tests pass!
- Sign off :-D

Followed test plan, patch works as described. All three patches pass QA
test tool
Signed-off-by: Alex Buckley <alexbuckley@catalyst.net.nz>

Signed-off-by: Josef Moravec <josef.moravec@gmail.com>

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>

C4/Circulation.pm

index 50d5e6d..23d0b5f 100644 (file)
@@ -2392,8 +2392,10 @@ sub _FixAccountForLostAndReturned {
     my $borrowernumber = @_ ? shift : undef;
     my $item_id        = @_ ? shift : $itemnumber;  # Send the barcode if you want that logged in the description
 
+    my $credit;
+
     # check for charge made for lost book
-    my $accountline = Koha::Account::Lines->search(
+    my $accountlines = Koha::Account::Lines->search(
         {
             itemnumber  => $itemnumber,
             accounttype => { -in => [ 'L', 'Rep', 'W' ] },
@@ -2401,29 +2403,49 @@ sub _FixAccountForLostAndReturned {
         {
             order_by => { -desc => [ 'date', 'accountno' ] }
         }
-    )->next();
+    );
+
+    return unless $accountlines->count > 0;
+    my $accountline = $accountlines->next;
+
+    # Use cases
+    if ( $accountline->amount > $accountline->amountoutstanding ) {
+        # some amount has been cancelled. collect the offsets that are not writeoffs
+        # this works because the only way to subtract from a debt is
+        # using the UI buttons 'Pay' and 'Write off'
+        my $credits_offsets = Koha::Account::Offsets->search({
+            debit_id  => $accountline->id,
+            credit_id => { '!=' => undef }, # it is not the debit itself
+            type      => { '!=' => 'Writeoff' },
+            amount    => { '<'  => 0 } # credits are negative on the DB
+        });
+
+        my $total_to_refund = ( $credits_offsets->count > 0 )
+                                ? $credits_offsets->total * -1 # credits are negative on the DB
+                                : 0;
+
+        if ( $total_to_refund > 0 ) {
+            my $account = Koha::Patrons->find( $accountline->borrowernumber )->account;
+            $credit = $account->add_credit(
+                {
+                    amount      => $total_to_refund,
+                    description => 'Item Returned ' . $item_id,
+                    type        => 'lost_item_return'
+                }
+            );
+        }
 
-    return unless $accountline;
-    return if $accountline->accounttype eq 'W';    # Written off
+        ModItem( { paidfor => '' }, undef, $itemnumber, { log_action => 0 } );
+    }
+    # else {
+        # $accountline->amount == $accountline->amountoutstanding
+    #}
 
     $accountline->accounttype('LR');
+    $accountline->amountoutstanding(0);
     $accountline->store();
 
-    my $account = Koha::Account->new( { patron_id => $accountline->borrowernumber } );
-    my $credit_id = $account->pay(
-        {
-            amount       => $accountline->amount,
-            description  => "Item Returned " . $item_id,
-            account_type => 'CR',
-            offset_type  => 'Lost Item Return',
-            lines        => [$accountline],
-
-        }
-    );
-
-    ModItem( { paidfor => '' }, undef, $itemnumber, { log_action => 0 } );
-
-    return $credit_id;
+    return ($credit) ? $credit->id : undef;
 }
 
 =head2 _GetCircControlBranch