From 0b5e599936ac1c613448806f570689df3e99b447 Mon Sep 17 00:00:00 2001
From: Tomas Cohen Arazi <tomascohen@theke.io>
Date: Thu, 19 Dec 2019 09:52:01 -0300
Subject: [PATCH] Bug 24266: Don't search for patrons if no borrowernumbers

This patch makes the reconcile_balances.pl script skip searching for
patrons from a given borrowernumbers list, if the list is empty.

The code was logically incorrect, and it doesn't break because of a bug
in SQL::Abstract that in recent versions started raising a noisy
warning.

I couldn't reproduce it in koha-testing-docker but it's been found in
the wild. I submit this obvious fix so people having the issue can test
it properly.

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
---
 misc/cronjobs/reconcile_balances.pl | 39 ++++++++++++++++-------------
 1 file changed, 21 insertions(+), 18 deletions(-)

diff --git a/misc/cronjobs/reconcile_balances.pl b/misc/cronjobs/reconcile_balances.pl
index 0dcae6cb13..f365cca1a2 100755
--- a/misc/cronjobs/reconcile_balances.pl
+++ b/misc/cronjobs/reconcile_balances.pl
@@ -89,31 +89,34 @@ my @patron_ids = map { $_->borrowernumber } Koha::Account::Lines->search(
         }
     );
 
-my $patrons = Koha::Patrons->search({ borrowernumber => { -in => \@patron_ids } });
+if ( scalar @patron_ids ) {
 
-while (my $patron = $patrons->next) {
+    my $patrons = Koha::Patrons->search({ borrowernumber => { -in => \@patron_ids } });
 
-    my $account = $patron->account;
-    my $total_outstanding_credit = $account->outstanding_credits->total_outstanding;
-    my $total_outstanding_debit  = $account->outstanding_debits->total_outstanding;
+    while (my $patron = $patrons->next) {
 
-    if ( $total_outstanding_credit < 0
-         and $total_outstanding_debit > 0) {
+        my $account = $patron->account;
+        my $total_outstanding_credit = $account->outstanding_credits->total_outstanding;
+        my $total_outstanding_debit  = $account->outstanding_debits->total_outstanding;
 
-        try {
+        if ( $total_outstanding_credit < 0
+            and $total_outstanding_debit > 0) {
 
-            $account->reconcile_balance;
+            try {
 
-            print $patron->id . ": credit: $total_outstanding_credit " .
-                                  "debit: $total_outstanding_debit " .
-                                  "=> outstanding " .
-                                  "credit: " . $account->outstanding_credits->total_outstanding .
-                                 " debit: " .  $account->outstanding_debits->total_outstanding . "\n"
-                if $verbose;
+                $account->reconcile_balance;
+
+                print $patron->id . ": credit: $total_outstanding_credit " .
+                                    "debit: $total_outstanding_debit " .
+                                    "=> outstanding " .
+                                    "credit: " . $account->outstanding_credits->total_outstanding .
+                                    " debit: " .  $account->outstanding_debits->total_outstanding . "\n"
+                    if $verbose;
+            }
+            catch {
+                print "Problem with patron " . $patron->borrowernumber . ": $_";
+            };
         }
-        catch {
-            print "Problem with patron " . $patron->borrowernumber . ": $_";
-        };
     }
 }
 
-- 
2.24.1