From 1184b7ac1b3e83c85e49a2dd65f79bbd50ec49d6 Mon Sep 17 00:00:00 2001
From: Nick Clemens <nick@bywatersolutions.com>
Date: Mon, 1 Apr 2024 15:00:34 +0000
Subject: [PATCH] Bug 36474: Don't update records when total issues has not
 changed

This patch adds a new check in UpdateTotalIssues to check that we are changing the number
of total issues before calling ModBiblio

To test:
0 - Enable CataloguingLog
1 - Checkout an item
2 - Run : misc/cronjobs/update_totalissues.pl --use-stats --commit=1000 -v
3 - In report, note all biblios were updated
4 - Check action_logs - note a new entry for every biblio
5 - Apply patch
6 - Repeat
7 - Note no biblios reported updated
8 - Note no new cataloguing log entries
9 - Checkout the item again
10 - Run again
11 - Note biblionumber has updated count in verbose output
12 - Note report only rpeort 1 biblio modified, the rest only processed
13 - Only one line added to action_logs
14 - Run it again
15 - Confirm no updates

Signed-off-by: David Nind <david@davidnind.com>
---
 C4/Biblio.pm                        | 10 ++++++----
 misc/cronjobs/update_totalissues.pl | 10 +++++++---
 2 files changed, 13 insertions(+), 7 deletions(-)

diff --git a/C4/Biblio.pm b/C4/Biblio.pm
index 3924430b77..1aad460d44 100644
--- a/C4/Biblio.pm
+++ b/C4/Biblio.pm
@@ -3063,19 +3063,21 @@ sub UpdateTotalIssues {
         my $exception = $@;
         $exception->rethrow unless ( $exception->isa('Koha::Exceptions::Metadata::Invalid') );
         warn "UpdateTotalIssues could not get biblio record";
-        return;
+        return -1;
     }
     my $biblioitem = $biblio->biblioitem;
     my ($totalissuestag, $totalissuessubfield) = GetMarcFromKohaField( 'biblioitems.totalissues' );
     unless ($totalissuestag) {
-        return 1; # There is nothing to do
+        return 0; # There is nothing to do
     }
 
-    if (defined $value) {
+    my $current_issues = $biblioitem->totalissues // 0;
+    if ( defined $value ) {
         $totalissues = $value;
     } else {
-        $totalissues = $biblioitem->totalissues + $increase;
+        $totalissues = $current_issues + $increase;
     }
+    return 0 if $current_issues == $totalissues;    # No need to update if no changes
 
      my $field = $record->field($totalissuestag);
      if (defined $field) {
diff --git a/misc/cronjobs/update_totalissues.pl b/misc/cronjobs/update_totalissues.pl
index eee8fdef62..08c7181ef7 100755
--- a/misc/cronjobs/update_totalissues.pl
+++ b/misc/cronjobs/update_totalissues.pl
@@ -98,7 +98,8 @@ my $dbh = C4::Context->dbh;
 $dbh->{AutoCommit} = 0;
 
 my $num_bibs_processed = 0;
-my $num_bibs_error = 0;
+my $num_bibs_updated   = 0;
+my $num_bibs_error     = 0;
 
 my $starttime = time();
 
@@ -173,9 +174,11 @@ sub process_query {
             else {
                 $ret = UpdateTotalIssues( $biblionumber, 0, $totalissues );
             }
-            unless ($ret) {
+            if ( $ret == -1 ) {
                 print "Error while processing bib $biblionumber\n" if $verbose;
                 $num_bibs_error++;
+            } elsif ( $ret == 1 ) {
+                $num_bibs_updated++;
             }
         }
         if ( not $test_only and ( $num_bibs_processed % $commit ) == 0 ) {
@@ -199,7 +202,8 @@ Update total issues count script report
 Run started at:                         $starttime
 Run ended at:                           $endtime
 Total run time:                         $totaltime ms
-Number of bibs modified:                $num_bibs_processed
+Number of bibs processed:               $num_bibs_processed
+Number of bibs modified:                $num_bibs_updated
 Number of bibs with error:              $num_bibs_error
 _SUMMARY_
     $summary .= "\n****  Ran in test mode only  ****\n" if $test_only;
-- 
2.30.2