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 borrowernumber IS NULL) AS NULL_BORROWERNUMBER,
28 (SELECT count(*) FROM statistics ) AS Total
30 my $sth = $dbh->prepare($query);
32 return $sth->fetchrow_hashref;
35 sub itemnumber_array() {
37 SELECT DISTINCT itemnumber
39 WHERE itemtype IS NULL
40 AND itemnumber IS NOT NULL
42 my $sth = $dbh->prepare($query);
44 my @itemnumbers = map {shift @$_} @{$sth->fetchall_arrayref};
47 sub null_borrower_lines() {
48 my $query = "SELECT * FROM statistics WHERE borrowernumber IS NULL";
49 my $sth = $dbh->prepare($query);
51 print "Number of lines with NULL_BORROWERNUMBER: ", scalar($sth->rows), "\n";
52 return $sth->fetchall_arrayref({});
56 print "\nThe following counts represent the number of (potential) errors in your statistics table:\n";
57 my $counts = get_counts;
58 foreach (sort keys %$counts) {
59 $_ eq 'Total' and next;
60 $counts->{Error_Total} += $counts->{$_};
61 print sprintf("%30s : %3d \n",$_ ,$counts->{$_});
63 print sprintf("%30s : %3d (potential) errors in %d lines\n",'Error_Total',$counts->{Error_Total}, $counts->{'Total'});
67 print "This operation may take a while.\n";
68 (scalar @ARGV) or show_counts;
69 print "\nAttempting to populate missing data.\n";
71 my (@itemnumbers) = (scalar @ARGV) ? @ARGV : &itemnumber_array;
72 $debug and print "itemnumbers: ", Dumper(\@itemnumbers);
73 print "Number of distinct itemnumbers paired with NULL_ITEMTYPE: ", scalar(@itemnumbers), "\n";
75 my $query = "UPDATE statistics SET itemtype = ? WHERE itemnumber = ?";
76 my $update = $dbh->prepare($query);
77 # $debug and print "Update Query: $query\n";
78 foreach (@itemnumbers) {
79 my $item = Koha::Items->find($_);
81 print STDERR "\tNo item found for itemnumber $_\n";
84 my $itemtype = $item->effective_itemtype;
85 $update->execute($itemtype,$_) or warn "Error in UPDATE execution";
86 printf "\titemnumber %5d : %7s (%s rows)\n", $_, $itemtype, $update->rows;
89 my $old_issues = $dbh->prepare("SELECT * FROM old_issues WHERE timestamp = ? AND itemnumber = ?");
90 my $issues = $dbh->prepare("SELECT * FROM issues WHERE timestamp = ? AND itemnumber = ?");
91 $update = $dbh->prepare("UPDATE statistics SET borrowernumber = ? WHERE datetime = ? AND itemnumber = ?");
92 my $nullborrs = null_borrower_lines;
93 $debug and print Dumper($nullborrs);
94 foreach (@$nullborrs) {
95 $old_issues->execute($_->{datetime},$_->{itemnumber});
97 if ($old_issues->rows != 1) {
98 print STDERR "Warning! Unexpected number of matches from old_issues: ",$old_issues->rows;
99 $issues->execute($_->{datetime},$_->{itemnumber});
100 if ($issues->rows != 1) {
101 print STDERR ", from issues: ",$issues->rows,"\tskipping this record\n";
105 $issue = $issues->fetchrow_hashref;
107 $issue = $old_issues->fetchrow_hashref;
109 printf "\titemnumber: %5d at %20s -- borrowernumber: %5d\n", $_->{itemnumber}, $_->{datetime}, $issue->{borrowernumber};
110 $update->execute($issue->{borrowernumber},$_->{datetime},$_->{itemnumber});
113 print "\nOperations complete.\n";
114 (scalar @ARGV) or show_counts;