From 32e308cf28439255eeb509cfbd00ac2564900c37 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adolfo=20Rodr=C3=ADguez?= <adolfo.rodriguez@xercode.es>
Date: Fri, 5 May 2023 16:11:26 +0200
Subject: [PATCH 1/1] Bug 31185 - Link authorities automatically doesn't detect
 duplicate authorities

---
 C4/Biblio.pm | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/C4/Biblio.pm b/C4/Biblio.pm
index dbb4026816..2e9a531692 100644
--- a/C4/Biblio.pm
+++ b/C4/Biblio.pm
@@ -674,8 +674,10 @@ sub LinkBibHeadingsToAuthorities {
             elsif ( C4::Context->preference('AutoCreateAuthorities') ) {
                 if ( _check_valid_auth_link( $current_link, $field ) ) {
                     $results{'linked'}->{ $heading->display_form() }++;
-                }
-                elsif ( !$match_count ) {
+                } elsif ( $match_count > 1 ) {
+                    $results{'unlinked'}->{ $heading->display_form() }++;
+                    push(@{$results{'details'}}, { tag => $field->tag(), authid => undef, status => 'MULTIPLE_MATCH', auth_type => $heading->auth_type(), tag_to_report => $authority_type->auth_tag_to_report}) if $verbose;
+                } elsif ( !$match_count ) {
                     my $authority_type = Koha::Authority::Types->find( $heading->auth_type() );
                     my $marcrecordauth = MARC::Record->new();
                     if ( C4::Context->preference('marcflavour') eq 'MARC21' ) {
@@ -760,6 +762,10 @@ sub LinkBibHeadingsToAuthorities {
                     push(@{$results{'details'}}, { tag => $field->tag(), authid => undef, status => 'NONE_FOUND', auth_type => $heading->auth_type(), tag_to_report => $authority_type->auth_tag_to_report}) if $verbose;
                 }
             }
+            elsif ( $match_count > 1 ) {
+                $results{'unlinked'}->{ $heading->display_form() }++;
+                push(@{$results{'details'}}, { tag => $field->tag(), authid => undef, status => 'MULTIPLE_MATCH', auth_type => $heading->auth_type(), tag_to_report => $authority_type->auth_tag_to_report}) if $verbose;
+            }
             else {
                 $results{'unlinked'}->{ $heading->display_form() }++;
                 push(@{$results{'details'}}, { tag => $field->tag(), authid => undef, status => 'NONE_FOUND', auth_type => $heading->auth_type(), tag_to_report => $authority_type->auth_tag_to_report}) if $verbose;
-- 
2.30.2