3 # Part of the Koha Library Software www.koha-community.org
4 # Licensed under the GPL.
17 my $dbh = C4::Context->dbh;
22 (SELECT count(*) FROM statistics WHERE branch="NO_LIBRARY" ) AS NO_LIBRARY,
23 (SELECT count(*) FROM statistics WHERE branch IS NULL) AS NULL_BRANCH,
24 (SELECT count(*) FROM statistics WHERE itemtype IS NULL AND itemnumber IS NOT NULL) AS NULL_ITEMTYPE,
25 (SELECT count(*) FROM statistics WHERE borrowernumber IS NULL) AS NULL_BORROWERNUMBER,
26 (SELECT count(*) FROM statistics ) AS Total
28 my $sth = $dbh->prepare($query);
30 return $sth->fetchrow_hashref;
33 sub itemnumber_array() {
35 SELECT DISTINCT itemnumber
37 WHERE itemtype IS NULL
38 AND itemnumber IS NOT NULL
40 my $sth = $dbh->prepare($query);
42 my @itemnumbers = map {shift @$_} @{$sth->fetchall_arrayref};
45 sub null_borrower_lines() {
46 my $query = "SELECT * FROM statistics WHERE borrowernumber IS NULL";
47 my $sth = $dbh->prepare($query);
49 print "Number of lines with NULL_BORROWERNUMBER: ", scalar($sth->rows), "\n";
50 return $sth->fetchall_arrayref({});
54 print "\nThe following counts represent the number of (potential) errors in your statistics table:\n";
55 my $counts = get_counts;
56 foreach (sort keys %$counts) {
57 $_ eq 'Total' and next;
58 $counts->{Error_Total} += $counts->{$_};
59 print sprintf("%30s : %3d \n",$_ ,$counts->{$_});
61 print sprintf("%30s : %3d (potential) errors in %d lines\n",'Error_Total',$counts->{Error_Total}, $counts->{'Total'});
65 print "This operation may take a while.\n";
66 (scalar @ARGV) or show_counts;
67 print "\nAttempting to populate missing data.\n";
69 my (@itemnumbers) = (scalar @ARGV) ? @ARGV : &itemnumber_array;
70 print "Number of distinct itemnumbers paired with NULL_ITEMTYPE: ", scalar(@itemnumbers), "\n";
72 my $query = "UPDATE statistics SET itemtype = ? WHERE itemnumber = ?";
73 my $update = $dbh->prepare($query);
74 foreach (@itemnumbers) {
75 my $item = Koha::Items->find($_);
77 print STDERR "\tNo item found for itemnumber $_\n";
80 my $itemtype = $item->effective_itemtype;
81 $update->execute($itemtype,$_) or warn "Error in UPDATE execution";
82 printf "\titemnumber %5d : %7s (%s rows)\n", $_, $itemtype, $update->rows;
85 my $old_issues = $dbh->prepare("SELECT * FROM old_issues WHERE timestamp = ? AND itemnumber = ?");
86 my $issues = $dbh->prepare("SELECT * FROM issues WHERE timestamp = ? AND itemnumber = ?");
87 $update = $dbh->prepare("UPDATE statistics SET borrowernumber = ? WHERE datetime = ? AND itemnumber = ?");
88 my $nullborrs = null_borrower_lines;
89 foreach (@$nullborrs) {
90 $old_issues->execute($_->{datetime},$_->{itemnumber});
92 if ($old_issues->rows != 1) {
93 print STDERR "Warning! Unexpected number of matches from old_issues: ",$old_issues->rows;
94 $issues->execute($_->{datetime},$_->{itemnumber});
95 if ($issues->rows != 1) {
96 print STDERR ", from issues: ",$issues->rows,"\tskipping this record\n";
100 $issue = $issues->fetchrow_hashref;
102 $issue = $old_issues->fetchrow_hashref;
104 printf "\titemnumber: %5d at %20s -- borrowernumber: %5d\n", $_->{itemnumber}, $_->{datetime}, $issue->{borrowernumber};
105 $update->execute($issue->{borrowernumber},$_->{datetime},$_->{itemnumber});
108 print "\nOperations complete.\n";
109 (scalar @ARGV) or show_counts;