From 15dd5bcb452ccfe529cc0887620ebff8fe698126 Mon Sep 17 00:00:00 2001 From: Matthias Le Gac Date: Wed, 7 Feb 2024 12:46:41 -0500 Subject: [PATCH] Bug 35836: search_for_data_inconsistencies.pl - Search for loops in dependencies --- .../search_for_data_inconsistencies.pl | 91 +++++++++++++++++++ 1 file changed, 91 insertions(+) diff --git a/misc/maintenance/search_for_data_inconsistencies.pl b/misc/maintenance/search_for_data_inconsistencies.pl index ddc6b457dd..2e8dc1c8a2 100755 --- a/misc/maintenance/search_for_data_inconsistencies.pl +++ b/misc/maintenance/search_for_data_inconsistencies.pl @@ -326,6 +326,97 @@ use C4::Biblio qw( GetMarcFromKohaField ); } } +{ + my @loop_borrowers_relationships; + my @relationships = Koha::Patron::Relationships->search(); + my @patrons_guarantors = Koha::Patron::Relationships::guarantors(@relationships)->as_list; + my @patrons_guarantees = Koha::Patron::Relationships::guarantees(@relationships)->as_list; + + foreach my $patron_guarantor (@patrons_guarantors) { + foreach my $patron_guarantee (@patrons_guarantees) { + if ($patron_guarantor->borrowernumber == $patron_guarantee->borrowernumber) { + + my $relationship_id; + my $guarantee_id; + my $size_list; + my $tmp_garantor_id = $patron_guarantor->borrowernumber; + my @relationship_ids; + my @guarantor_to_find; + + push @guarantor_to_find, $patron_guarantor->borrowernumber; + + do { + my @relationship_for_go = Koha::Patron::Relationships->search( + { + -or => [ + 'guarantor_id' => { '=', $tmp_garantor_id }, + ] + }, + )->as_list; + $size_list = scalar @relationship_for_go; + + foreach my $relation (@relationship_for_go) { + $relationship_id = $relation->id; + unless (grep { $_ == $relationship_id } @relationship_ids) { + push @relationship_ids, $relationship_id; + } + $guarantee_id = $relation->guarantee_id; + + if (grep { $_ eq $guarantee_id } @guarantor_to_find) { + last; + } + + my @relationship_for_go = Koha::Patron::Relationships->search( + { + -or => [ + 'guarantor_id' => { '=', $guarantee_id }, + ] + }, + )->as_list; + $size_list = scalar @relationship_for_go; + + push @guarantor_to_find, $guarantee_id; + + foreach my $relation (@relationship_for_go) { + $relationship_id = $relation->id; + unless (grep { $_ == $relationship_id } @relationship_ids) { + push @relationship_ids, $relationship_id; + } + $guarantee_id = $relation->guarantee_id; + + if (grep { $_ eq $guarantee_id } @guarantor_to_find) { + last; + } + } + if (grep { $_ eq $guarantee_id } @guarantor_to_find) { + last; + } + } + + $tmp_garantor_id = $guarantee_id; + } while ((!grep { $_ eq $guarantee_id } @guarantor_to_find) && $size_list > 0); + + if ($patron_guarantor->borrowernumber == $guarantee_id) { + unless (grep { join("", sort @$_) eq join("", sort @relationship_ids) } @loop_borrowers_relationships) { + push @loop_borrowers_relationships, \@relationship_ids; + } + } + } + } + } + + if (scalar @loop_borrowers_relationships > 0) { + new_section("The list of relationships id with guarantors who are also guarantee."); + foreach my $table (@loop_borrowers_relationships) { + new_item( + sprintf "Relationships connected : %s |", + join(" | ", @$table) + ); + } + new_hint("Relationships that form guarantor loops must be deleted"); + } +} + sub new_section { my ( $name ) = @_; say "\n== $name =="; -- 2.34.1