View | Details | Raw Unified | Return to bug 21466
Collapse All | Expand All

(-)a/misc/maintenance/search_for_data_inconsistencies.pl (-26 / +34 lines)
Lines 125-179 use Koha::ItemTypes; Link Here
125
125
126
{
126
{
127
    my $frameworks = Koha::BiblioFrameworks->search;
127
    my $frameworks = Koha::BiblioFrameworks->search;
128
    my $invalid_locations_per_framework;
128
    my $invalid_av_per_framework = {};
129
    while ( my $framework = $frameworks->next ) {
129
    while ( my $framework = $frameworks->next ) {
130
        my $msss = Koha::MarcSubfieldStructures->search({ frameworkcode => $framework->frameworkcode, authorised_value => { '!=' => [ -and => ( undef, '' ) ]} });
130
        my $msss = Koha::MarcSubfieldStructures->search({ frameworkcode => $framework->frameworkcode, authorised_value => { '!=' => [ -and => ( undef, '' ) ]} });
131
        while ( my $mss = $msss->next ) {
131
        while ( my $mss = $msss->next ) {
132
            my $kohafield = $mss->kohafield;
132
            my $kohafield = $mss->kohafield;
133
            next if grep {/$kohafield/} qw( branches itemtypes cn_source ); # internal categories
133
            my $av = $mss->authorised_value;
134
            next if grep {$_ eq $av} qw( branches itemtypes cn_source ); # internal categories
135
134
            my $avs = Koha::AuthorisedValues->search_by_koha_field(
136
            my $avs = Koha::AuthorisedValues->search_by_koha_field(
135
                {
137
                {
136
                    frameworkcode => $framework->frameworkcode,
138
                    frameworkcode => $framework->frameworkcode,
137
                    kohafield     => $kohafield,
139
                    kohafield     => $kohafield,
138
                }
140
                }
139
            );
141
            );
140
            (my $tmp_kohafield = $kohafield) =~ s|items|me|; # replace items.attr with me.attr
142
            my $tmp_kohafield = $kohafield;
143
            if ( $tmp_kohafield =~ /^biblioitems/ ) {
144
                $tmp_kohafield =~ s|biblioitems|biblioitem|;
145
            } else {
146
                $tmp_kohafield =~ s|items|me|;
147
            }
148
            # replace items.attr with me.attr
141
            my $items = Koha::Items->search(
149
            my $items = Koha::Items->search(
142
                {
150
                {
143
                    $tmp_kohafield =>
151
                    $tmp_kohafield =>
144
                      { -not_in => [ $avs->get_column('authorised_value') ] },
152
                      {
153
                          -not_in => [ $avs->get_column('authorised_value'), '' ],
154
                          '!='    => undef,
155
                      },
145
                    'biblio.frameworkcode' => $framework->frameworkcode
156
                    'biblio.frameworkcode' => $framework->frameworkcode
146
                },
157
                },
147
                { join => [ 'biblioitem', 'biblio' ] }
158
                { join => [ 'biblioitem', 'biblio' ] }
148
            );
159
            );
149
            if ( $items->count ) {
160
            if ( $items->count ) {
150
                $invalid_locations_per_framework->{ $framework->frameworkcode } =
161
                $invalid_av_per_framework->{ $framework->frameworkcode }->{$av} =
151
                  { items => $items, av_category => $mss->authorised_value, kohafield => $kohafield };
162
                  { items => $items, kohafield => $kohafield };
152
            }
163
            }
153
        }
164
        }
154
    }
165
    }
155
    if (%$invalid_locations_per_framework) {
166
    if (%$invalid_av_per_framework) {
156
        new_section('Wrong values linked to authorised values');
167
        new_section('Wrong values linked to authorised values');
157
        for my $frameworkcode ( keys %$invalid_locations_per_framework ) {
168
        for my $frameworkcode ( keys %$invalid_av_per_framework ) {
158
            my $output;
169
            my $output;
159
            my $items =
170
            while ( my ( $av_category, $v ) = each %{$invalid_av_per_framework->{$frameworkcode}} ) {
160
              $invalid_locations_per_framework->{$frameworkcode}->{items};
171
                my $items     = $v->{items};
161
            my $av_category =
172
                my $kohafield = $v->{kohafield};
162
              $invalid_locations_per_framework->{$frameworkcode}->{av_category};
173
                my ( $table, $column ) = split '\.', $kohafield;
163
            my $kohafield =
174
                while ( my $i = $items->next ) {
164
              $invalid_locations_per_framework->{$frameworkcode}->{kohafield};
175
                    my $value = $table eq 'items' ? $i->$column : $i->biblioitem->$column;
165
            my ( $table, $column ) = split '\.', $kohafield;
176
                    $output .= " {" . $i->itemnumber . " => " . $value . "}";
166
            while ( my $i = $items->next ) {
177
                }
167
                my $value = $table eq 'items' ? $i->$column : $i->biblioitem->$column;
178
                new_item(
168
                $output .= " {" . $i->itemnumber . " => " . $value . "}";
179
                    sprintf(
180
                        "The Framework *%s* is using the authorised value's category *%s*, "
181
                        . "but the following %s do not have a value defined ({itemnumber => value }):\n%s",
182
                        $frameworkcode, $av_category, $kohafield, $output
183
                    )
184
                );
169
            }
185
            }
170
            new_item(
171
                sprintf(
172
                    "The Framework *%s* is using the authorised value's category *%s*, "
173
                    . "but the following %s do not have a value defined ({itemnumber => value }):\n%s",
174
                    $frameworkcode, $av_category, $kohafield, $output
175
                )
176
            );
177
        }
186
        }
178
    }
187
    }
179
}
188
}
180
- 

Return to bug 21466