Translation updates for Koha 19.05.02
[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::Items;
22 use Koha::Biblioitems;
23 use Koha::ItemTypes;
24 use Koha::Authorities;
25
26 {
27     my $items = Koha::Items->search({ -or => { homebranch => undef, holdingbranch => undef }});
28     if ( $items->count ) { new_section("Not defined items.homebranch and/or items.holdingbranch")}
29     while ( my $item = $items->next ) {
30         if ( not $item->homebranch and not $item->holdingbranch ) {
31             new_item(sprintf "Item with itemnumber=%s does not have homebranch and holdingbranch defined", $item->itemnumber);
32         } elsif ( not $item->homebranch ) {
33             new_item(sprintf "Item with itemnumber=%s does not have homebranch defined", $item->itemnumber);
34         } else {
35             new_item(sprintf "Item with itemnumber=%s does not have holdingbranch defined", $item->itemnumber);
36         }
37     }
38     if ( $items->count ) { new_hint("Edit these items and set valid homebranch and/or holdingbranch")}
39 }
40
41 {
42     # No join possible, FK is missing at DB level
43     my @auth_types = Koha::Authority::Types->search->get_column('authtypecode');
44     my $authorities = Koha::Authorities->search({authtypecode => { 'not in' => \@auth_types } });
45     if ( $authorities->count ) {new_section("Invalid auth_header.authtypecode")}
46     while ( my $authority = $authorities->next ) {
47         new_item(sprintf "Authority with authid=%s does not have a code defined (%s)", $authority->authid, $authority->authtypecode);
48     }
49     if ( $authorities->count ) {new_hint("Go to 'Home › Administration › Authority types' to define them")}
50 }
51
52 {
53     if ( C4::Context->preference('item-level_itypes') ) {
54         my $items_without_itype = Koha::Items->search( { itype => undef } );
55         if ( $items_without_itype->count ) {
56             new_section("Items do not have itype defined");
57             while ( my $item = $items_without_itype->next ) {
58                 new_item(
59                     sprintf "Item with itemnumber=%s does not have a itype value, biblio's item type will be used (%s)",
60                     $item->itemnumber, $item->biblioitem->itemtype
61                 );
62             }
63             new_hint("The system preference item-level_itypes expects item types to be defined at item level");
64         }
65     }
66     else {
67         my $biblioitems_without_itemtype = Koha::Biblioitems->search( { itemtype => undef } );
68         if ( $biblioitems_without_itemtype->count ) {
69             new_section("Biblioitems do not have itemtype defined");
70             while ( my $biblioitem = $biblioitems_without_itemtype->next ) {
71                 new_item(
72                     sprintf "Biblioitem with biblioitemnumber=%s does not have a itemtype value",
73                     $biblioitem->biblioitemnumber
74                 );
75             }
76             new_hint("The system preference item-level_itypes expects item types to be defined at biblio level");
77         }
78     }
79
80     my @itemtypes = Koha::ItemTypes->search->get_column('itemtype');
81     if ( C4::Context->preference('item-level_itypes') ) {
82         my $items_with_invalid_itype = Koha::Items->search( { itype => { not_in => \@itemtypes } } );
83         if ( $items_with_invalid_itype->count ) {
84             new_section("Items have invalid itype defined");
85             while ( my $item = $items_with_invalid_itype->next ) {
86                 new_item(
87                     sprintf "Item with itemnumber=%s, biblionumber=%s does not have a valid itype value (%s)",
88                     $item->itemnumber, $item->biblionumber, $item->itype
89                 );
90             }
91             new_hint("The items must have a itype value that is defined in the item types of Koha (Home › Administration › Item types administration)");
92         }
93     }
94     else {
95         my $biblioitems_with_invalid_itemtype = Koha::Biblioitems->search(
96             { itemtype => { not_in => \@itemtypes } }
97         );
98         if ( $biblioitems_with_invalid_itemtype->count ) {
99             new_section("Biblioitems do not have itemtype defined");
100             while ( my $biblioitem = $biblioitems_with_invalid_itemtype->next ) {
101                 new_item(
102                     sprintf "Biblioitem with biblioitemnumber=%s does not have a valid itemtype value",
103                     $biblioitem->biblioitemnumber
104                 );
105             }
106             new_hint("The biblioitems must have a itemtype value that is defined in the item types of Koha (Home › Administration › Item types administration)");
107         }
108     }
109 }
110
111 sub new_section {
112     my ( $name ) = @_;
113     say "\n== $name ==";
114 }
115
116 sub new_item {
117     my ( $name ) = @_;
118     say "\t* $name";
119 }
120 sub new_hint {
121     my ( $name ) = @_;
122     say "=> $name";
123 }
124
125 =head1 NAME
126
127 search_for_data_inconsistencies.pl
128
129 =head1 SYNOPSIS
130
131     perl search_for_data_inconsistencies.pl
132
133 =head1 DESCRIPTION
134
135 Catch data inconsistencies in Koha database
136
137 * Items with undefined homebranch and/or holdingbranch
138 * Authorities with undefined authtypecodes/authority types
139 * Item types:
140   * if item types are defined at item level (item-level_itypes=specific item),
141     then items.itype must be set else biblioitems.itemtype must be set
142   * Item types defined in items or biblioitems must be defined in the itemtypes table
143
144 =cut