3 # Part of the Koha Library Software www.koha-community.org
4 # Licensed under the GPL.
13 my $dbh = C4::Context->dbh;
18 (SELECT count(*) FROM statistics WHERE branch="NO_LIBRARY" ) AS NO_LIBRARY,
19 (SELECT count(*) FROM statistics WHERE branch IS NULL) AS NULL_BRANCH,
20 (SELECT count(*) FROM statistics WHERE itemtype IS NULL AND itemnumber IS NOT NULL) AS NULL_ITEMTYPE,
21 (SELECT count(*) FROM statistics WHERE borrowernumber IS NULL) AS NULL_BORROWERNUMBER,
22 (SELECT count(*) FROM statistics ) AS Total
24 my $sth = $dbh->prepare($query);
26 return $sth->fetchrow_hashref;
29 sub itemnumber_array() {
31 SELECT DISTINCT itemnumber
33 WHERE itemtype IS NULL
34 AND itemnumber IS NOT NULL
36 my $sth = $dbh->prepare($query);
38 my @itemnumbers = map {shift @$_} @{$sth->fetchall_arrayref};
41 sub null_borrower_lines() {
42 my $query = "SELECT * FROM statistics WHERE borrowernumber IS NULL";
43 my $sth = $dbh->prepare($query);
45 print "Number of lines with NULL_BORROWERNUMBER: ", scalar($sth->rows), "\n";
46 return $sth->fetchall_arrayref({});
50 print "\nThe following counts represent the number of (potential) errors in your statistics table:\n";
51 my $counts = get_counts;
52 foreach (sort keys %$counts) {
53 $_ eq 'Total' and next;
54 $counts->{Error_Total} += $counts->{$_};
55 print sprintf("%30s : %3d \n",$_ ,$counts->{$_});
57 print sprintf("%30s : %3d (potential) errors in %d lines\n",'Error_Total',$counts->{Error_Total}, $counts->{'Total'});
61 print "This operation may take a while.\n";
62 (scalar @ARGV) or show_counts;
63 print "\nAttempting to populate missing data.\n";
65 my (@itemnumbers) = (scalar @ARGV) ? @ARGV : &itemnumber_array;
66 print "Number of distinct itemnumbers paired with NULL_ITEMTYPE: ", scalar(@itemnumbers), "\n";
68 my $query = "UPDATE statistics SET itemtype = ? WHERE itemnumber = ?";
69 my $update = $dbh->prepare($query);
70 foreach (@itemnumbers) {
71 my $item = Koha::Items->find($_);
73 print STDERR "\tNo item found for itemnumber $_\n";
76 my $itemtype = $item->effective_itemtype;
77 $update->execute($itemtype,$_) or warn "Error in UPDATE execution";
78 printf "\titemnumber %5d : %7s (%s rows)\n", $_, $itemtype, $update->rows;
81 my $old_issues = $dbh->prepare("SELECT * FROM old_issues WHERE timestamp = ? AND itemnumber = ?");
82 my $issues = $dbh->prepare("SELECT * FROM issues WHERE timestamp = ? AND itemnumber = ?");
83 $update = $dbh->prepare("UPDATE statistics SET borrowernumber = ? WHERE datetime = ? AND itemnumber = ?");
84 my $nullborrs = null_borrower_lines;
85 foreach (@$nullborrs) {
86 $old_issues->execute($_->{datetime},$_->{itemnumber});
88 if ($old_issues->rows != 1) {
89 print STDERR "Warning! Unexpected number of matches from old_issues: ",$old_issues->rows;
90 $issues->execute($_->{datetime},$_->{itemnumber});
91 if ($issues->rows != 1) {
92 print STDERR ", from issues: ",$issues->rows,"\tskipping this record\n";
96 $issue = $issues->fetchrow_hashref;
98 $issue = $old_issues->fetchrow_hashref;
100 printf "\titemnumber: %5d at %20s -- borrowernumber: %5d\n", $_->{itemnumber}, $_->{datetime}, $issue->{borrowernumber};
101 $update->execute($issue->{borrowernumber},$_->{datetime},$_->{itemnumber});
104 print "\nOperations complete.\n";
105 (scalar @ARGV) or show_counts;