Bug 13795: Remove occurences of unused columns in code
[koha-equinox.git] / installer / data / mysql / backfill_statistics.pl
1 #!/usr/bin/perl
2
3 # Part of the Koha Library Software www.koha-community.org
4 # Licensed under the GPL.
5
6 use Modern::Perl;
7
8 # CPAN modules
9 use DBI;
10 use Getopt::Long;
11
12 # Koha modules
13 use C4::Context;
14 use C4::Items;
15 use C4::Debug;
16 use Data::Dumper;
17
18 use vars qw($debug $dbh);
19 $dbh = C4::Context->dbh;
20
21 sub get_counts() {
22         my $query = q(
23         SELECT
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
29         );
30         my $sth = $dbh->prepare($query);
31         $sth->execute;
32         return $sth->fetchrow_hashref;
33 }
34
35 sub itemnumber_array() {
36         my $query = q(
37                 SELECT DISTINCT itemnumber
38                 FROM statistics
39                 WHERE itemtype IS NULL
40                 AND itemnumber IS NOT NULL
41         );
42         my $sth = $dbh->prepare($query);
43         $sth->execute;
44         my @itemnumbers = map {shift @$_} @{$sth->fetchall_arrayref};
45         return @itemnumbers;
46 }
47 sub null_borrower_lines() {
48         my $query = "SELECT * FROM statistics WHERE borrowernumber IS NULL";
49         my $sth = $dbh->prepare($query);
50         $sth->execute;
51         print "Number of lines with NULL_BORROWERNUMBER: ", scalar($sth->rows), "\n";
52         return $sth->fetchall_arrayref({});
53 }
54
55 sub show_counts() {
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->{$_});
62         }
63         print sprintf("%30s : %3d (potential) errors in %d lines\n",'Error_Total',$counts->{Error_Total}, $counts->{'Total'});
64 }
65
66 ##### MAIN #####
67 print "This operation may take a while.\n";
68 (scalar @ARGV) or show_counts;
69 print "\nAttempting to populate missing data.\n";
70
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";
74
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($_);
80     unless ($item) {
81                 print STDERR "\tNo item found for itemnumber $_\n"; 
82                 next;
83         }
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;
87 }
88
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});
96         my $issue;
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";
102                         next;
103                 }
104                 print STDERR "\n";
105                 $issue = $issues->fetchrow_hashref;
106         } else {
107                 $issue = $old_issues->fetchrow_hashref;
108         }
109         printf "\titemnumber: %5d at %20s -- borrowernumber: %5d\n", $_->{itemnumber}, $_->{datetime}, $issue->{borrowernumber};
110         $update->execute($issue->{borrowernumber},$_->{datetime},$_->{itemnumber});
111 }
112
113 print "\nOperations complete.\n";
114 (scalar @ARGV) or show_counts;
115