Bug 28092: Add reserve notes column to holds to pull
[koha.git] / admin / checkmarc.pl
1 #!/usr/bin/perl
2
3
4 # Copyright 2000-2002 Katipo Communications
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20
21 use Modern::Perl;
22 use C4::Output;
23 use C4::Auth;
24 use CGI qw ( -utf8 );
25 use C4::Context;
26 use C4::Biblio;
27
28
29 my $input = CGI->new;
30
31 my ($template, $borrowernumber, $cookie)
32     = get_template_and_user({template_name => "admin/checkmarc.tt",
33                              query => $input,
34                              type => "intranet",
35                  flagsrequired => { parameters => 'manage_marc_frameworks' },
36                              debug => 1,
37                              });
38
39 my $dbh = C4::Context->dbh;
40 my $total = 0;
41 # checks itemnum field
42 my $sth = $dbh->prepare("select tab from marc_subfield_structure where kohafield=\"items.itemnumber\"");
43 $sth->execute;
44 while (my ($res) = $sth->fetchrow) {
45     if ($res==-1) {
46             $template->param(itemnum => 0);
47     } else {
48             $template->param(itemnum => 1);
49             $total++;
50         last;
51     }
52 }
53
54 # checks biblio.biblionumber and biblioitem.biblioitemnumber (same tag and tab=-1)
55 $sth = $dbh->prepare("select tagfield,tab,frameworkcode from marc_subfield_structure where kohafield=\"biblio.biblionumber\"");
56 $sth->execute;
57 my $first = 1;
58 my $bibliotag = '';
59 while (my ($res,$tab,$frameworkcode) = $sth->fetchrow) {
60     if ($first) {
61         $bibliotag = $res;
62         $first = 0;
63     } else {
64         if ($bibliotag != $res) {
65                 $template->param(biblionumber => 1);
66                 $total++;
67             last;
68         }
69     }
70     my $sth2 = $dbh->prepare("SELECT tagfield,tab 
71                               FROM marc_subfield_structure 
72                               WHERE kohafield=\"biblioitems.biblioitemnumber\"
73                               AND frameworkcode = ? ");
74     $sth2->execute($frameworkcode);
75     my ($res2,$tab2) = $sth2->fetchrow;
76     if ($res && $res2 && $tab==-1 && $tab2==-1) {
77             $template->param(biblionumber => 0);
78     } else {
79             $template->param(biblionumber => 1);
80             $total++;
81         last;
82     }
83 }
84
85 # checks all item fields are in the same tag and in tab 10
86
87 $sth = $dbh->prepare("select tagfield,tab,kohafield from marc_subfield_structure where kohafield like \"items.%\" and tab >=0");
88 $sth->execute;
89 my $field;
90 my $res;
91 my $res2;
92 my $tab;
93 ($res,$res2,$field) = $sth->fetchrow;
94 my $tagfield = $res;
95 $tab = $res2;
96 my $subtotal=0;
97 #warn "TAGF : $tagfield";
98 while (($res,$res2,$field) = $sth->fetchrow) {
99         # (ignore itemnumber, that must be in -1 tab)
100         if (($res ne $tagfield) or ($res2 ne $tab)) {
101                 $subtotal++;
102         }
103 }
104 $sth = $dbh->prepare("select kohafield from marc_subfield_structure where tagfield=?");
105 $sth->execute($tagfield);
106 while (($res2) = $sth->fetchrow) {
107         if (!$res2 || $res2 =~ /^items/) {
108         } else {
109                 $subtotal++;
110         }
111 }
112 if ($subtotal == 0) {
113         $template->param(itemfields => 0);
114 } else {
115         $template->param(itemfields => 1);
116         $total++;
117 }
118
119 $sth = $dbh->prepare("select distinct tagfield from marc_subfield_structure where tab = 10");
120 $sth->execute;
121 my $totaltags = 0;
122 my $list = "";
123 while (($res2) = $sth->fetchrow) {
124         $totaltags++;
125         $list.=$res2.",";
126 }
127 if ($totaltags > 1) {
128         $template->param(itemtags => $list);
129         $total++;
130 } else {
131         $template->param(itemtags => 0);
132 }
133
134
135 # checks biblioitems.itemtype must be mapped and use authorised_value=itemtype
136 $sth = $dbh->prepare("select tagfield,tab,authorised_value from marc_subfield_structure where kohafield = \"biblioitems.itemtype\"");
137 $sth->execute;
138 while (($res,$res2,$field) = $sth->fetchrow) {
139     if ($res && $res2>=0 && $field eq "itemtypes") {
140             $template->param(itemtype => 0);
141     } else {
142             $template->param(itemtype => 1);
143             $total++;
144         last;
145     }
146 }
147
148 # checks items.homebranch must be mapped and use authorised_value=branches
149 $sth = $dbh->prepare("select tagfield,tab,authorised_value from marc_subfield_structure where kohafield = \"items.homebranch\"");
150 $sth->execute;
151 while (($res,$res2,$field) = $sth->fetchrow) {
152     if ($res && $res2 eq 10 && $field eq "branches") {
153             $template->param(branch => 0);
154     } else {
155             $template->param(branch => 1);
156             $total++;
157         last;
158     }
159 }
160
161 # checks items.homebranch must be mapped and use authorised_value=branches
162 $sth = $dbh->prepare("select tagfield,tab,authorised_value from marc_subfield_structure where kohafield = \"items.holdingbranch\"");
163 $sth->execute;
164 while (($res,$res2,$field) = $sth->fetchrow) {
165     if ($res && $res2 eq 10 && $field eq "branches") {
166             $template->param(holdingbranch => 0);
167     } else {
168             $template->param(holdingbranch => 1);
169             $total++;
170             last; #MR
171     }
172 }
173
174 # checks that itemtypes & branches tables are not empty
175 $sth = $dbh->prepare("select count(*) from itemtypes");
176 $sth->execute;
177 ($res) = $sth->fetchrow;
178 if ($res) {
179         $template->param(itemtypes_empty =>0);
180 } else {
181         $template->param(itemtypes_empty =>1);
182         $total++;
183 }
184
185
186 $sth = $dbh->prepare("select count(*) from branches");
187 $sth->execute;
188 ($res) = $sth->fetchrow;
189 if ($res) {
190         $template->param(branches_empty =>0);
191 } else {
192         $template->param(branches_empty =>1);
193         $total++;
194 }
195
196 $sth = $dbh->prepare("select count(*) from marc_subfield_structure where frameworkcode is NULL");
197 $sth->execute;
198 ($res) = $sth->fetchrow;
199 if ($res) {
200         $template->param(frameworknull =>1);
201         $total++;
202 }
203 $sth = $dbh->prepare("select count(*) from marc_tag_structure where frameworkcode is NULL");
204 $sth->execute;
205 ($res) = $sth->fetchrow;
206 if ($res) {
207         $template->param(frameworknull =>1);
208         $total++;
209 }
210
211 # verify that all of a field's subfields (except the ones explicitly ignored)
212 # are in the same tab
213 $sth = $dbh->prepare("SELECT tagfield, frameworkcode, frameworktext, GROUP_CONCAT(DISTINCT tab) AS tabs
214                       FROM marc_subfield_structure
215                       LEFT JOIN biblio_framework USING (frameworkcode)
216                       WHERE tab != -1
217                       GROUP BY tagfield, frameworkcode, frameworktext
218                       HAVING COUNT(DISTINCT tab) > 1");
219 $sth->execute;
220 my $inconsistent_tabs = $sth->fetchall_arrayref({});
221 if (scalar(@$inconsistent_tabs) > 0) {
222     $total++;
223     $template->param(inconsistent_tabs => 1);
224     $template->param(tab_info => $inconsistent_tabs);
225 }
226
227 # verify that authtypecodes used in the framework 
228 # are defined in auth_types
229 $sth = $dbh->prepare("SELECT frameworkcode, frameworktext, tagfield, tagsubfield, authtypecode
230                       FROM marc_subfield_structure
231                       LEFT JOIN biblio_framework USING (frameworkcode)
232                       WHERE authtypecode IS NOT NULL
233                       AND authtypecode <> ''
234                       AND tab > '-1'
235                       AND authtypecode NOT IN (SELECT authtypecode FROM auth_types)
236                       ORDER BY frameworkcode, tagfield, tagsubfield");
237 $sth->execute;
238 my $invalid_authtypecodes = $sth->fetchall_arrayref({});
239 if (scalar(@$invalid_authtypecodes) > 0) {
240     $total++;
241     $template->param(invalid_authtypecodes => 1);
242     $template->param(authtypecode_info => $invalid_authtypecodes);
243 }
244
245 # checks items.permanent_location is not mapped
246 $sth = $dbh->prepare("SELECT frameworkcode, frameworktext, tagfield, tagsubfield
247                       FROM marc_subfield_structure
248                       LEFT JOIN biblio_framework USING (frameworkcode)
249                       WHERE kohafield='permanent_location' OR
250                             kohafield='items.permanent_location'");
251 $sth->execute;
252 my $permanent_location_mapped = $sth->fetchall_arrayref({});
253 if (scalar(@$permanent_location_mapped) > 0) {
254     $total++;
255     $template->param(permanent_location_mapped => 1);
256     $template->param(mapped_permanent_location => $permanent_location_mapped);
257 }
258
259
260 $template->param(total => $total,
261                 );
262
263 output_html_with_http_headers $input, $cookie, $template->output;