3 # This file is part of Koha.
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.
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.
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>.
21 use Koha::AuthorisedValues;
22 use Koha::Authorities;
24 use Koha::BiblioFrameworks;
25 use Koha::Biblioitems;
29 use C4::Biblio qw( GetMarcFromKohaField );
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);
40 new_item(sprintf "Item with itemnumber=%s does not have holdingbranch defined", $item->itemnumber);
43 if ( $items->count ) { new_hint("Edit these items and set valid homebranch and/or holdingbranch")}
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);
54 if ( $authorities->count ) {new_hint("Go to 'Home › Administration › Authority types' to define them")}
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 '' ) {
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
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
75 new_hint("The system preference item-level_itypes expects item types to be defined at item level");
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 ) {
84 sprintf "Biblioitem with biblioitemnumber=%s does not have a itemtype value",
85 $biblioitem->biblioitemnumber
88 new_hint("The system preference item-level_itypes expects item types to be defined at biblio level");
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 ) {
99 sprintf "Item with itemnumber=%s, biblionumber=%s does not have a valid itype value (%s)",
100 $item->itemnumber, $item->biblionumber, $item->itype
103 new_hint("The items must have a itype value that is defined in the item types of Koha (Home › Administration › Item types administration)");
107 my $biblioitems_with_invalid_itemtype = Koha::Biblioitems->search(
108 { itemtype => { not_in => \@itemtypes } }
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 ) {
114 sprintf "Biblioitem with biblioitemnumber=%s does not have a valid itemtype value",
115 $biblioitem->biblioitemnumber
118 new_hint("The biblioitems must have a itemtype value that is defined in the item types of Koha (Home › Administration › Item types administration)");
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;};
129 push @decoding_errors, $@;
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;
137 $biblionumber = $record->subfield( $biblio_tag, $biblio_subfield );
139 if ( $biblioitem_tag < 10 ) {
140 my $biblioitem_control_field = $record->field($biblioitem_tag);
141 $biblioitemnumber = $biblioitem_control_field->data if $biblioitem_control_field;
143 $biblioitemnumber = $record->subfield( $biblioitem_tag, $biblioitem_subfield );
145 if ( $biblionumber != $biblio->biblionumber ) {
146 push @ids_not_in_marc,
148 biblionumber => $biblio->biblionumber,
149 biblionumber_in_marc => $biblionumber,
152 if ( $biblioitemnumber != $biblio->biblioitem->biblioitemnumber ) {
153 push @ids_not_in_marc,
155 biblionumber => $biblio->biblionumber,
156 biblioitemnumber => $biblio->biblioitem->biblioitemnumber,
157 biblioitemnumber_in_marc => $biblionumber,
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");
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} ) {
171 sprintf(q{Biblionumber %s has biblioitemnumber '%s' but should be '%s' in %s$%s},
173 $id->{biblioitemnumber},
174 $id->{biblioitemnumber_in_marc},
176 $biblioitem_subfield,
182 sprintf(q{Biblionumber %s has '%s' in %s$%s},
184 $id->{biblionumber_in_marc},
191 new_hint("The bibliographic records must have the biblionumber and biblioitemnumber in MARCXML");
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
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, '' ) ]
208 '!=' => [ -and => ( undef, '' ) ]
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
216 my $avs = Koha::AuthorisedValues->search_by_koha_field(
218 frameworkcode => $frameworkcode,
219 kohafield => $kohafield,
222 my $tmp_kohafield = $kohafield;
223 if ( $tmp_kohafield =~ /^biblioitems/ ) {
224 $tmp_kohafield =~ s|biblioitems|biblioitem|;
226 $tmp_kohafield =~ s|items|me|;
228 # replace items.attr with me.attr
230 # We are only checking biblios with items
231 my $items = Koha::Items->search(
235 -not_in => [ $avs->get_column('authorised_value'), '' ],
238 'biblio.frameworkcode' => $frameworkcode
240 { join => [ 'biblioitem', 'biblio' ] }
242 if ( $items->count ) {
243 $invalid_av_per_framework->{ $frameworkcode }->{$av} =
244 { items => $items, kohafield => $kohafield };
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;
256 while ( my $i = $items->next ) {
257 my $value = $table eq 'items'
260 ? $i->biblio->$column
261 : $i->biblioitem->$column;
262 $output .= " {" . $i->itemnumber . " => " . $value . "}\n";
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
277 my $biblios = Koha::Biblios->search({
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);
291 new_hint("Edit these bibliographic records to define a title");
296 my $aging_patrons = Koha::Patrons->search(
300 'me.dateofbirth' => undef,
302 'categorycode.dateofbirthrequired' => undef,
303 'categorycode.upperagelimit' => undef,
308 { prefetch => ['categorycode'] },
309 { order_by => [ 'me.categorycode', 'me.borrowernumber' ] },
312 while ( my $aging_patron = $aging_patrons->next ) {
313 push @invalid_patrons, $aging_patron unless $aging_patron->is_expired || $aging_patron->is_valid_age;
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;
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'
325 new_hint("You may change the patron's category automatically with misc/cronjobs/update_patrons_category.pl");
330 my @loop_borrowers_relationships;
331 my $relationships = Koha::Patron::Relationships->search();
332 my @patrons_guarantors = Koha::Patron::Relationships::guarantors($relationships)->as_list;
333 my @patrons_guarantees = Koha::Patron::Relationships::guarantees($relationships)->as_list;
335 foreach my $patron_guarantor (@patrons_guarantors) {
336 foreach my $patron_guarantee (@patrons_guarantees) {
337 if ( $patron_guarantor->borrowernumber == $patron_guarantee->borrowernumber ) {
342 my $tmp_garantor_id = $patron_guarantor->borrowernumber;
346 my @relationship_for_go = Koha::Patron::Relationships->search(
349 'guarantor_id' => { '=', $tmp_garantor_id },
353 $size_list = scalar @relationship_for_go;
355 foreach my $relation (@relationship_for_go) {
356 $guarantor_id = $relation->guarantor_id;
357 unless ( grep { $_ == $guarantor_id } @guarantor_ids ) {
358 push @guarantor_ids, $guarantor_id;
360 $guarantee_id = $relation->guarantee_id;
362 my @relationship_for_go = Koha::Patron::Relationships->search(
365 'guarantor_id' => { '=', $guarantee_id },
369 $size_list = scalar @relationship_for_go;
371 if ( $patron_guarantor->borrowernumber == $guarantee_id ) {
375 foreach my $relation (@relationship_for_go) {
376 $guarantor_id = $relation->guarantor_id;
377 unless ( grep { $_ == $guarantor_id } @guarantor_ids ) {
378 push @guarantor_ids, $guarantor_id;
380 $guarantee_id = $relation->guarantee_id;
382 if ( $patron_guarantor->borrowernumber == $guarantee_id ) {
386 if ( $patron_guarantor->borrowernumber == $guarantee_id ) {
391 $tmp_garantor_id = $guarantee_id;
392 } while ( $patron_guarantor->borrowernumber != $guarantee_id && $size_list > 0 );
394 if ( $patron_guarantor->borrowernumber == $guarantee_id ) {
395 unless ( grep { join( "", sort @$_ ) eq join( "", sort @guarantor_ids ) }
396 @loop_borrowers_relationships )
398 push @loop_borrowers_relationships, \@guarantor_ids;
405 if ( scalar @loop_borrowers_relationships > 0 ) {
406 new_section("The list of guarantors who are also guarantee.");
408 foreach my $table (@loop_borrowers_relationships) {
410 print "Loop $count, borrowers id : ";
411 foreach my $borrower_id (@$table) {
412 print "$borrower_id | ";
416 new_hint("Relationships that form guarantor loops must be deleted");
436 search_for_data_inconsistencies.pl
440 perl search_for_data_inconsistencies.pl
444 Catch data inconsistencies in Koha database
446 * Items with undefined homebranch and/or holdingbranch
447 * Authorities with undefined authtypecodes/authority types
449 * if item types are defined at item level (item-level_itypes=specific item),
450 then items.itype must be set else biblioitems.itemtype must be set
451 * Item types defined in items or biblioitems must be defined in the itemtypes table
452 * Invalid MARCXML in bibliographic records
453 * Patrons with invalid category types due to lower and upper age limits