ddc6b457dd1e64613ca8b0641975d3b4cd1061ce
[koha.git] / misc / maintenance / search_for_data_inconsistencies.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use Koha::Script;
21 use Koha::AuthorisedValues;
22 use Koha::Authorities;
23 use Koha::Biblios;
24 use Koha::BiblioFrameworks;
25 use Koha::Biblioitems;
26 use Koha::Items;
27 use Koha::ItemTypes;
28 use Koha::Patrons;
29 use C4::Biblio qw( GetMarcFromKohaField );
30
31 {
32     my $items = Koha::Items->search({ -or => { homebranch => undef, holdingbranch => undef }});
33     if ( $items->count ) { new_section("Not defined items.homebranch and/or items.holdingbranch")}
34     while ( my $item = $items->next ) {
35         if ( not $item->homebranch and not $item->holdingbranch ) {
36             new_item(sprintf "Item with itemnumber=%s does not have homebranch and holdingbranch defined", $item->itemnumber);
37         } elsif ( not $item->homebranch ) {
38             new_item(sprintf "Item with itemnumber=%s does not have homebranch defined", $item->itemnumber);
39         } else {
40             new_item(sprintf "Item with itemnumber=%s does not have holdingbranch defined", $item->itemnumber);
41         }
42     }
43     if ( $items->count ) { new_hint("Edit these items and set valid homebranch and/or holdingbranch")}
44 }
45
46 {
47     # No join possible, FK is missing at DB level
48     my @auth_types = Koha::Authority::Types->search->get_column('authtypecode');
49     my $authorities = Koha::Authorities->search({authtypecode => { 'not in' => \@auth_types } });
50     if ( $authorities->count ) {new_section("Invalid auth_header.authtypecode")}
51     while ( my $authority = $authorities->next ) {
52         new_item(sprintf "Authority with authid=%s does not have a code defined (%s)", $authority->authid, $authority->authtypecode);
53     }
54     if ( $authorities->count ) {new_hint("Go to 'Home › Administration › Authority types' to define them")}
55 }
56
57 {
58     if ( C4::Context->preference('item-level_itypes') ) {
59         my $items_without_itype = Koha::Items->search( { -or => [itype => undef,itype => ''] } );
60         if ( $items_without_itype->count ) {
61             new_section("Items do not have itype defined");
62             while ( my $item = $items_without_itype->next ) {
63                 if (defined $item->biblioitem->itemtype && $item->biblioitem->itemtype ne '' ) {
64                     new_item(
65                         sprintf "Item with itemnumber=%s does not have a itype value, biblio's item type will be used (%s)",
66                         $item->itemnumber, $item->biblioitem->itemtype
67                     );
68                 } else {
69                     new_item(
70                         sprintf "Item with itemnumber=%s does not have a itype value, additionally no item type defined for biblionumber=%s",
71                         $item->itemnumber, $item->biblioitem->biblionumber
72                     );
73                }
74             }
75             new_hint("The system preference item-level_itypes expects item types to be defined at item level");
76         }
77     }
78     else {
79         my $biblioitems_without_itemtype = Koha::Biblioitems->search( { itemtype => undef } );
80         if ( $biblioitems_without_itemtype->count ) {
81             new_section("Biblioitems do not have itemtype defined");
82             while ( my $biblioitem = $biblioitems_without_itemtype->next ) {
83                 new_item(
84                     sprintf "Biblioitem with biblioitemnumber=%s does not have a itemtype value",
85                     $biblioitem->biblioitemnumber
86                 );
87             }
88             new_hint("The system preference item-level_itypes expects item types to be defined at biblio level");
89         }
90     }
91
92     my @itemtypes = Koha::ItemTypes->search->get_column('itemtype');
93     if ( C4::Context->preference('item-level_itypes') ) {
94         my $items_with_invalid_itype = Koha::Items->search( { -and => [itype => { not_in => \@itemtypes }, itype => { '!=' => '' }] } );
95         if ( $items_with_invalid_itype->count ) {
96             new_section("Items have invalid itype defined");
97             while ( my $item = $items_with_invalid_itype->next ) {
98                 new_item(
99                     sprintf "Item with itemnumber=%s, biblionumber=%s does not have a valid itype value (%s)",
100                     $item->itemnumber, $item->biblionumber, $item->itype
101                 );
102             }
103             new_hint("The items must have a itype value that is defined in the item types of Koha (Home › Administration › Item types administration)");
104         }
105     }
106     else {
107         my $biblioitems_with_invalid_itemtype = Koha::Biblioitems->search(
108             { itemtype => { not_in => \@itemtypes } }
109         );
110         if ( $biblioitems_with_invalid_itemtype->count ) {
111             new_section("Biblioitems do not have itemtype defined");
112             while ( my $biblioitem = $biblioitems_with_invalid_itemtype->next ) {
113                 new_item(
114                     sprintf "Biblioitem with biblioitemnumber=%s does not have a valid itemtype value",
115                     $biblioitem->biblioitemnumber
116                 );
117             }
118             new_hint("The biblioitems must have a itemtype value that is defined in the item types of Koha (Home › Administration › Item types administration)");
119         }
120     }
121
122     my ( @decoding_errors, @ids_not_in_marc );
123     my $biblios = Koha::Biblios->search;
124     my ( $biblio_tag,     $biblio_subfield )     = C4::Biblio::GetMarcFromKohaField( "biblio.biblionumber" );
125     my ( $biblioitem_tag, $biblioitem_subfield ) = C4::Biblio::GetMarcFromKohaField( "biblioitems.biblioitemnumber" );
126     while ( my $biblio = $biblios->next ) {
127         my $record = eval{$biblio->metadata->record;};
128         if ($@) {
129             push @decoding_errors, $@;
130             next;
131         }
132         my ( $biblionumber, $biblioitemnumber );
133         if ( $biblio_tag < 10 ) {
134             my $biblio_control_field = $record->field($biblio_tag);
135             $biblionumber = $biblio_control_field->data if $biblio_control_field;
136         } else {
137             $biblionumber = $record->subfield( $biblio_tag, $biblio_subfield );
138         }
139         if ( $biblioitem_tag < 10 ) {
140             my $biblioitem_control_field = $record->field($biblioitem_tag);
141             $biblioitemnumber = $biblioitem_control_field->data if $biblioitem_control_field;
142         } else {
143             $biblioitemnumber = $record->subfield( $biblioitem_tag, $biblioitem_subfield );
144         }
145         if ( $biblionumber != $biblio->biblionumber ) {
146             push @ids_not_in_marc,
147               {
148                 biblionumber         => $biblio->biblionumber,
149                 biblionumber_in_marc => $biblionumber,
150               };
151         }
152         if ( $biblioitemnumber != $biblio->biblioitem->biblioitemnumber ) {
153             push @ids_not_in_marc,
154             {
155                 biblionumber     => $biblio->biblionumber,
156                 biblioitemnumber => $biblio->biblioitem->biblioitemnumber,
157                 biblioitemnumber_in_marc => $biblionumber,
158             };
159         }
160     }
161     if ( @decoding_errors ) {
162         new_section("Bibliographic records have invalid MARCXML");
163         new_item($_) for @decoding_errors;
164         new_hint("The bibliographic records must have a valid MARCXML or you will face encoding issues or wrong displays");
165     }
166     if (@ids_not_in_marc) {
167         new_section("Bibliographic records have MARCXML without biblionumber or biblioitemnumber");
168         for my $id (@ids_not_in_marc) {
169             if ( exists $id->{biblioitemnumber} ) {
170                 new_item(
171                     sprintf(q{Biblionumber %s has biblioitemnumber '%s' but should be '%s' in %s$%s},
172                         $id->{biblionumber},
173                         $id->{biblioitemnumber},
174                         $id->{biblioitemnumber_in_marc},
175                         $biblioitem_tag,
176                         $biblioitem_subfield,
177                     )
178                 );
179             }
180             else {
181                 new_item(
182                     sprintf(q{Biblionumber %s has '%s' in %s$%s},
183                         $id->{biblionumber},
184                         $id->{biblionumber_in_marc},
185                         $biblio_tag,
186                         $biblio_subfield,
187                     )
188                 );
189             }
190         }
191         new_hint("The bibliographic records must have the biblionumber and biblioitemnumber in MARCXML");
192     }
193 }
194
195 {
196     my @framework_codes = Koha::BiblioFrameworks->search()->get_column('frameworkcode');
197     push @framework_codes,""; # The default is not stored in frameworks, we need to force it
198
199     my $invalid_av_per_framework = {};
200     foreach my $frameworkcode ( @framework_codes ) {
201         # We are only checking fields that are mapped to DB fields
202         my $msss = Koha::MarcSubfieldStructures->search({
203             frameworkcode => $frameworkcode,
204             authorised_value => {
205                 '!=' => [ -and => ( undef, '' ) ]
206             },
207             kohafield => {
208                 '!=' => [ -and => ( undef, '' ) ]
209             }
210         });
211         while ( my $mss = $msss->next ) {
212             my $kohafield = $mss->kohafield;
213             my $av = $mss->authorised_value;
214             next if grep {$_ eq $av} qw( branches itemtypes cn_source ); # internal categories
215
216             my $avs = Koha::AuthorisedValues->search_by_koha_field(
217                 {
218                     frameworkcode => $frameworkcode,
219                     kohafield     => $kohafield,
220                 }
221             );
222             my $tmp_kohafield = $kohafield;
223             if ( $tmp_kohafield =~ /^biblioitems/ ) {
224                 $tmp_kohafield =~ s|biblioitems|biblioitem|;
225             } else {
226                 $tmp_kohafield =~ s|items|me|;
227             }
228             # replace items.attr with me.attr
229
230             # We are only checking biblios with items
231             my $items = Koha::Items->search(
232                 {
233                     $tmp_kohafield =>
234                       {
235                           -not_in => [ $avs->get_column('authorised_value'), '' ],
236                           '!='    => undef,
237                       },
238                     'biblio.frameworkcode' => $frameworkcode
239                 },
240                 { join => [ 'biblioitem', 'biblio' ] }
241             );
242             if ( $items->count ) {
243                 $invalid_av_per_framework->{ $frameworkcode }->{$av} =
244                   { items => $items, kohafield => $kohafield };
245             }
246         }
247     }
248     if (%$invalid_av_per_framework) {
249         new_section('Wrong values linked to authorised values');
250         for my $frameworkcode ( keys %$invalid_av_per_framework ) {
251             while ( my ( $av_category, $v ) = each %{$invalid_av_per_framework->{$frameworkcode}} ) {
252                 my $items     = $v->{items};
253                 my $kohafield = $v->{kohafield};
254                 my ( $table, $column ) = split '\.', $kohafield;
255                 my $output;
256                 while ( my $i = $items->next ) {
257                     my $value = $table eq 'items'
258                         ? $i->$column
259                         : $table eq 'biblio'
260                         ? $i->biblio->$column
261                         : $i->biblioitem->$column;
262                     $output .= " {" . $i->itemnumber . " => " . $value . "}\n";
263                 }
264                 new_item(
265                     sprintf(
266                         "The Framework *%s* is using the authorised value's category *%s*, "
267                         . "but the following %s do not have a value defined ({itemnumber => value }):\n%s",
268                         $frameworkcode, $av_category, $kohafield, $output
269                     )
270                 );
271             }
272         }
273     }
274 }
275
276 {
277     my $biblios = Koha::Biblios->search({
278         -or => [
279             title => '',
280             title => undef,
281         ]
282     });
283     if ( $biblios->count ) {
284         my ( $title_tag, $title_subtag ) = C4::Biblio::GetMarcFromKohaField( 'biblio.title' );
285         my $title_field = $title_tag // '';
286         $title_field .= '$'.$title_subtag if $title_subtag;
287         new_section("Biblio without title $title_field");
288         while ( my $biblio = $biblios->next ) {
289             new_item(sprintf "Biblio with biblionumber=%s does not have title defined", $biblio->biblionumber);
290         }
291         new_hint("Edit these bibliographic records to define a title");
292     }
293 }
294
295 {
296     my $aging_patrons = Koha::Patrons->search(
297         {
298             -not => {
299                 -or => {
300                     'me.dateofbirth' => undef,
301                     -and => {
302                         'categorycode.dateofbirthrequired' => undef,
303                         'categorycode.upperagelimit'       => undef,
304                     }
305                 }
306             }
307         },
308         { prefetch => ['categorycode'] },
309         { order_by => [ 'me.categorycode', 'me.borrowernumber' ] },
310     );
311     my @invalid_patrons;
312     while ( my $aging_patron = $aging_patrons->next ) {
313         push @invalid_patrons, $aging_patron unless $aging_patron->is_expired || $aging_patron->is_valid_age;
314     }
315     if (@invalid_patrons) {
316         new_section("Patrons with invalid age for category");
317         foreach my $invalid_patron (@invalid_patrons) {
318             my $category = $invalid_patron->category;
319             new_item(
320                 sprintf "Patron borrowernumber=%s has an invalid age of %s for their category '%s' (%s to %s)",
321                 $invalid_patron->borrowernumber, $invalid_patron->get_age, $category->categorycode,
322                 $category->dateofbirthrequired // '0',  $category->upperagelimit // 'unlimited'
323             );
324         }
325         new_hint("You may change the patron's category automatically with misc/cronjobs/update_patrons_category.pl");
326     }
327 }
328
329 sub new_section {
330     my ( $name ) = @_;
331     say "\n== $name ==";
332 }
333
334 sub new_item {
335     my ( $name ) = @_;
336     say "\t* $name";
337 }
338 sub new_hint {
339     my ( $name ) = @_;
340     say "=> $name";
341 }
342
343 =head1 NAME
344
345 search_for_data_inconsistencies.pl
346
347 =head1 SYNOPSIS
348
349     perl search_for_data_inconsistencies.pl
350
351 =head1 DESCRIPTION
352
353 Catch data inconsistencies in Koha database
354
355 * Items with undefined homebranch and/or holdingbranch
356 * Authorities with undefined authtypecodes/authority types
357 * Item types:
358   * if item types are defined at item level (item-level_itypes=specific item),
359     then items.itype must be set else biblioitems.itemtype must be set
360   * Item types defined in items or biblioitems must be defined in the itemtypes table
361 * Invalid MARCXML in bibliographic records
362 * Patrons with invalid category types due to lower and upper age limits
363
364 =cut