3 # Part of the Koha Library Software www.koha-community.org
4 # Licensed under the GPL.
18 use vars qw($debug $dbh);
19 $dbh = C4::Context->dbh;
24 (SELECT count(*) FROM statistics WHERE branch="NO_LIBRARY" ) AS NO_LIBRARY,
25 (SELECT count(*) FROM statistics WHERE branch IS NULL) AS NULL_BRANCH,
26 (SELECT count(*) FROM statistics WHERE itemtype IS NULL AND itemnumber IS NOT NULL) AS NULL_ITEMTYPE,
27 (SELECT count(*) FROM statistics WHERE usercode IS NULL) AS NULL_USERCODE,
28 (SELECT count(*) FROM statistics WHERE borrowernumber IS NULL) AS NULL_BORROWERNUMBER,
29 (SELECT count(*) FROM statistics WHERE associatedborrower IS NULL) AS NULL_ASSOCIATEDBORROWER,
30 (SELECT count(*) FROM statistics ) AS Total
32 my $sth = $dbh->prepare($query);
34 return $sth->fetchrow_hashref;
37 sub itemnumber_array() {
39 SELECT DISTINCT itemnumber
41 WHERE itemtype IS NULL
42 AND itemnumber IS NOT NULL
44 my $sth = $dbh->prepare($query);
46 my @itemnumbers = map {shift @$_} @{$sth->fetchall_arrayref};
49 sub null_borrower_lines() {
50 my $query = "SELECT * FROM statistics WHERE borrowernumber IS NULL";
51 my $sth = $dbh->prepare($query);
53 print "Number of lines with NULL_BORROWERNUMBER: ", scalar($sth->rows), "\n";
54 return $sth->fetchall_arrayref({});
58 print "\nThe following counts represent the number of (potential) errors in your statistics table:\n";
59 my $counts = get_counts;
60 foreach (sort keys %$counts) {
61 $_ eq 'Total' and next;
62 $counts->{Error_Total} += $counts->{$_};
63 print sprintf("%30s : %3d \n",$_ ,$counts->{$_});
65 print sprintf("%30s : %3d (potential) errors in %d lines\n",'Error_Total',$counts->{Error_Total}, $counts->{'Total'});
69 print "This operation may take a while.\n";
70 (scalar @ARGV) or show_counts;
71 print "\nAttempting to populate missing data.\n";
73 my (@itemnumbers) = (scalar @ARGV) ? @ARGV : &itemnumber_array;
74 $debug and print "itemnumbers: ", Dumper(\@itemnumbers);
75 print "Number of distinct itemnumbers paired with NULL_ITEMTYPE: ", scalar(@itemnumbers), "\n";
77 my $query = "UPDATE statistics SET itemtype = ? WHERE itemnumber = ?";
78 my $update = $dbh->prepare($query);
79 # $debug and print "Update Query: $query\n";
80 foreach (@itemnumbers) {
81 my $item = Koha::Items->find($_);
83 print STDERR "\tNo item found for itemnumber $_\n";
86 my $itemtype = $item->effective_itemtype;
87 $update->execute($itemtype,$_) or warn "Error in UPDATE execution";
88 printf "\titemnumber %5d : %7s (%s rows)\n", $_, $itemtype, $update->rows;
91 my $old_issues = $dbh->prepare("SELECT * FROM old_issues WHERE timestamp = ? AND itemnumber = ?");
92 my $issues = $dbh->prepare("SELECT * FROM issues WHERE timestamp = ? AND itemnumber = ?");
93 $update = $dbh->prepare("UPDATE statistics SET borrowernumber = ? WHERE datetime = ? AND itemnumber = ?");
94 my $nullborrs = null_borrower_lines;
95 $debug and print Dumper($nullborrs);
96 foreach (@$nullborrs) {
97 $old_issues->execute($_->{datetime},$_->{itemnumber});
99 if ($old_issues->rows != 1) {
100 print STDERR "Warning! Unexpected number of matches from old_issues: ",$old_issues->rows;
101 $issues->execute($_->{datetime},$_->{itemnumber});
102 if ($issues->rows != 1) {
103 print STDERR ", from issues: ",$issues->rows,"\tskipping this record\n";
107 $issue = $issues->fetchrow_hashref;
109 $issue = $old_issues->fetchrow_hashref;
111 printf "\titemnumber: %5d at %20s -- borrowernumber: %5d\n", $_->{itemnumber}, $_->{datetime}, $issue->{borrowernumber};
112 $update->execute($issue->{borrowernumber},$_->{datetime},$_->{itemnumber});
115 print "\nOperations complete.\n";
116 (scalar @ARGV) or show_counts;